1 ;;; org.el --- Outline-based notes management and organizer
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
31 ;; project planning with a fast and effective plain-text system.
33 ;; Org-mode develops organizational tasks around NOTES files that contain
34 ;; information about projects as plain text. Org-mode is implemented on
35 ;; top of outline-mode, which makes it possible to keep the content of
36 ;; large files well structured. Visibility cycling and structure editing
37 ;; help to work with the tree. Tables are easily created with a built-in
38 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
39 ;; and scheduling. It dynamically compiles entries into an agenda that
40 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
41 ;; Plain text URL-like links connect to websites, emails, Usenet
42 ;; messages, BBDB entries, and any files related to the projects. For
43 ;; printing and sharing of notes, an Org-mode file can be exported as a
44 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
45 ;; iCalendar file. It can also serve as a publishing tool for a set of
48 ;; Installation and Activation
49 ;; ---------------------------
50 ;; See the corresponding sections in the manual at
52 ;; http://orgmode.org/org.html#Installation
56 ;; The documentation of Org-mode can be found in the TeXInfo file. The
57 ;; distribution also contains a PDF version of it. At the homepage of
58 ;; Org-mode, you can read the same text online as HTML. There is also an
59 ;; excellent reference card made by Philip Rooke. This card can be found
60 ;; in the etc/ directory of Emacs 22.
62 ;; A list of recent changes can be found at
63 ;; http://orgmode.org/Changes.html
67 ;;;; Require other packages
73 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
74 ;; the file noutline.el being loaded.
75 (if (featurep 'xemacs
) (condition-case nil
(require 'noutline
)))
76 ;; We require noutline, which might be provided in outline.el
77 (require 'outline
) (require 'noutline
)
78 ;; Other stuff we need.
80 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
83 ;;;; Customization variables
87 (defconst org-version
"5.23a"
88 "The version number of the file org.el.")
90 (defun org-version (&optional here
)
91 "Show the org-mode version in the echo area.
92 With prefix arg HERE, insert it at point."
94 (let ((version (format "Org-mode version %s" org-version
)))
99 ;;; Compatibility constants
100 (defconst org-xemacs-p
(featurep 'xemacs
)) ; not used by org.el itself
101 (defconst org-format-transports-properties-p
103 (add-text-properties 0 1 '(test t
) x
)
104 (get-text-property 0 'test
(format "%s" x
)))
105 "Does format transport text properties?")
107 (defmacro org-bound-and-true-p
(var)
108 "Return the value of symbol VAR if it is bound, else nil."
109 `(and (boundp (quote ,var
)) ,var
))
111 (defmacro org-unmodified
(&rest body
)
112 "Execute body without changing `buffer-modified-p'."
113 `(set-buffer-modified-p
114 (prog1 (buffer-modified-p) ,@body
)))
117 "Replace posix classes in regular expression."
118 (if (featurep 'xemacs
)
121 (while (string-match "\\[:alnum:\\]" ss
)
122 (setq ss
(replace-match "a-zA-Z0-9" t t ss
)))
123 (while (string-match "\\[:alpha:\\]" ss
)
124 (setq ss
(replace-match "a-zA-Z" t t ss
)))
128 (defmacro org-preserve-lc
(&rest body
)
129 `(let ((_line (org-current-line))
130 (_col (current-column)))
134 (move-to-column _col
))))
136 (defmacro org-without-partial-completion
(&rest body
)
137 `(let ((pc-mode (and (boundp 'partial-completion-mode
)
138 partial-completion-mode
)))
141 (if pc-mode
(partial-completion-mode -
1))
143 (if pc-mode
(partial-completion-mode 1)))))
145 ;;; The custom variables
148 "Outline-based notes management and organizer."
154 (defcustom org-load-hook nil
155 "Hook that is run after org.el has been loaded."
159 ;(defcustom org-default-extensions '(org-irc)
160 ; "Extensions that should always be loaded together with org.el.
161 ;If the description starts with <A>, this means the extension
162 ;will be autoloaded when needed, preloading is not necessary.
163 ;FIXME: this does not ork correctly, ignore it for now."
167 ; (const :tag " Mouse support (org-mouse.el)" org-mouse)
168 ; (const :tag "<A> Publishing (org-publish.el)" org-publish)
169 ; (const :tag "<A> LaTeX export (org-export-latex.el)" org-export-latex)
170 ; (const :tag " IRC/ERC links (org-irc.el)" org-irc)
171 ; (const :tag " Apple Mail message links under OS X (org-mac-message.el)" org-mac-message)))
173 ;(defun org-load-default-extensions ()
174 ; "Load all extensions listed in `org-default-extensions'."
175 ; (mapc (lambda (ext)
176 ; (condition-case nil (require ext)
177 ; (error (message "Problems while trying to load feature `%s'" ext))))
178 ; org-default-extensions))
180 ;(eval-after-load "org" '(org-load-default-extensions))
182 ;; FIXME: Needs a separate group...
183 (defcustom org-completion-fallback-command
'hippie-expand
184 "The expansion command called by \\[org-complete] in normal context.
185 Normal means, no org-mode-specific context."
189 (defgroup org-startup nil
190 "Options concerning startup of Org-mode."
194 (defcustom org-startup-folded t
195 "Non-nil means, entering Org-mode will switch to OVERVIEW.
196 This can also be configured on a per-file basis by adding one of
197 the following lines anywhere in the buffer:
204 (const :tag
"nofold: show all" nil
)
205 (const :tag
"fold: overview" t
)
206 (const :tag
"content: all headlines" content
)))
208 (defcustom org-startup-truncated t
209 "Non-nil means, entering Org-mode will set `truncate-lines'.
210 This is useful since some lines containing links can be very long and
211 uninteresting. Also tables look terrible when wrapped."
215 (defcustom org-startup-align-all-tables nil
216 "Non-nil means, align all tables when visiting a file.
217 This is useful when the column width in tables is forced with <N> cookies
218 in table fields. Such tables will look correct only after the first re-align.
219 This can also be configured on a per-file basis by adding one of
220 the following lines anywhere in the buffer:
226 (defcustom org-insert-mode-line-in-empty-file nil
227 "Non-nil means insert the first line setting Org-mode in empty files.
228 When the function `org-mode' is called interactively in an empty file, this
229 normally means that the file name does not automatically trigger Org-mode.
230 To ensure that the file will always be in Org-mode in the future, a
231 line enforcing Org-mode will be inserted into the buffer, if this option
236 (defcustom org-replace-disputed-keys nil
237 "Non-nil means use alternative key bindings for some keys.
238 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
239 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
240 If you want to use Org-mode together with one of these other modes,
241 or more generally if you would like to move some Org-mode commands to
242 other keys, set this variable and configure the keys with the variable
245 This option is only relevant at load-time of Org-mode, and must be set
246 *before* org.el is loaded. Changing it requires a restart of Emacs to
251 (if (fboundp 'defvaralias
)
252 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
254 (defcustom org-disputed-keys
255 '(([(shift up
)] .
[(meta p
)])
256 ([(shift down
)] .
[(meta n
)])
257 ([(shift left
)] .
[(meta -
)])
258 ([(shift right
)] .
[(meta +)])
259 ([(control shift right
)] .
[(meta shift
+)])
260 ([(control shift left
)] .
[(meta shift -
)]))
261 "Keys for which Org-mode and other modes compete.
262 This is an alist, cars are the default keys, second element specifies
263 the alternative to use when `org-replace-disputed-keys' is t.
265 Keys can be specified in any syntax supported by `define-key'.
266 The value of this option takes effect only at Org-mode's startup,
267 therefore you'll have to restart Emacs to apply it after changing."
272 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
273 Or return the original if not disputed."
274 (if org-replace-disputed-keys
275 (let* ((nkey (key-description key
))
276 (x (org-find-if (lambda (x)
277 (equal (key-description (car x
)) nkey
))
282 (defun org-find-if (predicate seq
)
285 (if (funcall predicate
(car seq
))
286 (throw 'exit
(car seq
))
289 (defun org-defkey (keymap key def
)
290 "Define a key, possibly translated, as returned by `org-key'."
291 (define-key keymap
(org-key key
) def
))
293 (defcustom org-ellipsis nil
294 "The ellipsis to use in the Org-mode outline.
295 When nil, just use the standard three dots. When a string, use that instead,
296 When a face, use the standart 3 dots, but with the specified face.
297 The change affects only Org-mode (which will then use its own display table).
298 Changing this requires executing `M-x org-mode' in a buffer to become
301 :type
'(choice (const :tag
"Default" nil
)
302 (face :tag
"Face" :value org-warning
)
303 (string :tag
"String" :value
"...#")))
305 (defvar org-display-table nil
306 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
308 (defgroup org-keywords nil
309 "Keywords in Org-mode."
313 (defcustom org-deadline-string
"DEADLINE:"
314 "String to mark deadline entries.
315 A deadline is this string, followed by a time stamp. Should be a word,
316 terminated by a colon. You can insert a schedule keyword and
317 a timestamp with \\[org-deadline].
318 Changes become only effective after restarting Emacs."
322 (defcustom org-scheduled-string
"SCHEDULED:"
323 "String to mark scheduled TODO entries.
324 A schedule is this string, followed by a time stamp. Should be a word,
325 terminated by a colon. You can insert a schedule keyword and
326 a timestamp with \\[org-schedule].
327 Changes become only effective after restarting Emacs."
331 (defcustom org-closed-string
"CLOSED:"
332 "String used as the prefix for timestamps logging closing a TODO entry."
336 (defcustom org-clock-string
"CLOCK:"
337 "String used as prefix for timestamps clocking work hours on an item."
341 (defcustom org-comment-string
"COMMENT"
342 "Entries starting with this keyword will never be exported.
343 An entry can be toggled between COMMENT and normal with
344 \\[org-toggle-comment].
345 Changes become only effective after restarting Emacs."
349 (defcustom org-quote-string
"QUOTE"
350 "Entries starting with this keyword will be exported in fixed-width font.
351 Quoting applies only to the text in the entry following the headline, and does
352 not extend beyond the next headline, even if that is lower level.
353 An entry can be toggled between QUOTE and normal with
354 \\[org-toggle-fixed-width-section]."
358 (defconst org-repeat-re
359 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
360 "Regular expression for specifying repeated events.
361 After a match, group 1 contains the repeat expression.")
363 (defgroup org-structure nil
364 "Options concerning the general structure of Org-mode files."
368 (defgroup org-reveal-location nil
369 "Options about how to make context of a location visible."
370 :tag
"Org Reveal Location"
371 :group
'org-structure
)
373 (defconst org-context-choice
375 (const :tag
"Always" t
)
376 (const :tag
"Never" nil
)
377 (repeat :greedy t
:tag
"Individual contexts"
379 (choice :tag
"Context"
386 (const bookmark-jump
)
390 "Contexts for the reveal options.")
392 (defcustom org-show-hierarchy-above
'((default . t
))
393 "Non-nil means, show full hierarchy when revealing a location.
394 Org-mode often shows locations in an org-mode file which might have
395 been invisible before. When this is set, the hierarchy of headings
396 above the exposed location is shown.
397 Turning this off for example for sparse trees makes them very compact.
398 Instead of t, this can also be an alist specifying this option for different
399 contexts. Valid contexts are
400 agenda when exposing an entry from the agenda
401 org-goto when using the command `org-goto' on key C-c C-j
402 occur-tree when using the command `org-occur' on key C-c /
403 tags-tree when constructing a sparse tree based on tags matches
404 link-search when exposing search matches associated with a link
405 mark-goto when exposing the jump goal of a mark
406 bookmark-jump when exposing a bookmark location
407 isearch when exiting from an incremental search
408 default default for all contexts not set explicitly"
409 :group
'org-reveal-location
410 :type org-context-choice
)
412 (defcustom org-show-following-heading
'((default . nil
))
413 "Non-nil means, show following heading when revealing a location.
414 Org-mode often shows locations in an org-mode file which might have
415 been invisible before. When this is set, the heading following the
417 Turning this off for example for sparse trees makes them very compact,
418 but makes it harder to edit the location of the match. In such a case,
419 use the command \\[org-reveal] to show more context.
420 Instead of t, this can also be an alist specifying this option for different
421 contexts. See `org-show-hierarchy-above' for valid contexts."
422 :group
'org-reveal-location
423 :type org-context-choice
)
425 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
426 "Non-nil means, show all sibling heading when revealing a location.
427 Org-mode often shows locations in an org-mode file which might have
428 been invisible before. When this is set, the sibling of the current entry
429 heading are all made visible. If `org-show-hierarchy-above' is t,
430 the same happens on each level of the hierarchy above the current entry.
432 By default this is on for the isearch context, off for all other contexts.
433 Turning this off for example for sparse trees makes them very compact,
434 but makes it harder to edit the location of the match. In such a case,
435 use the command \\[org-reveal] to show more context.
436 Instead of t, this can also be an alist specifying this option for different
437 contexts. See `org-show-hierarchy-above' for valid contexts."
438 :group
'org-reveal-location
439 :type org-context-choice
)
441 (defcustom org-show-entry-below
'((default . nil
))
442 "Non-nil means, show the entry below a headline when revealing a location.
443 Org-mode often shows locations in an org-mode file which might have
444 been invisible before. When this is set, the text below the headline that is
445 exposed is also shown.
447 By default this is off for all contexts.
448 Instead of t, this can also be an alist specifying this option for different
449 contexts. See `org-show-hierarchy-above' for valid contexts."
450 :group
'org-reveal-location
451 :type org-context-choice
)
453 (defgroup org-cycle nil
454 "Options concerning visibility cycling in Org-mode."
456 :group
'org-structure
)
458 (defcustom org-drawers
'("PROPERTIES" "CLOCK")
459 "Names of drawers. Drawers are not opened by cycling on the headline above.
460 Drawers only open with a TAB on the drawer line itself. A drawer looks like
465 The drawer \"PROPERTIES\" is special for capturing properties through
468 Drawers can be defined on the per-file basis with a line like:
470 #+DRAWERS: HIDDEN STATE PROPERTIES"
471 :group
'org-structure
472 :type
'(repeat (string :tag
"Drawer Name")))
474 (defcustom org-cycle-global-at-bob nil
475 "Cycle globally if cursor is at beginning of buffer and not at a headline.
476 This makes it possible to do global cycling without having to use S-TAB or
477 C-u TAB. For this special case to work, the first line of the buffer
478 must not be a headline - it may be empty ot some other text. When used in
479 this way, `org-cycle-hook' is disables temporarily, to make sure the
480 cursor stays at the beginning of the buffer.
481 When this option is nil, don't do anything special at the beginning
486 (defcustom org-cycle-emulate-tab t
487 "Where should `org-cycle' emulate TAB.
489 white Only in completely white lines
490 whitestart Only at the beginning of lines, before the first non-white char
491 t Everywhere except in headlines
492 exc-hl-bol Everywhere except at the start of a headline
493 If TAB is used in a place where it does not emulate TAB, the current subtree
494 visibility is cycled."
496 :type
'(choice (const :tag
"Never" nil
)
497 (const :tag
"Only in completely white lines" white
)
498 (const :tag
"Before first char in a line" whitestart
)
499 (const :tag
"Everywhere except in headlines" t
)
500 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
503 (defcustom org-cycle-separator-lines
2
504 "Number of empty lines needed to keep an empty line between collapsed trees.
505 If you leave an empty line between the end of a subtree and the following
506 headline, this empty line is hidden when the subtree is folded.
507 Org-mode will leave (exactly) one empty line visible if the number of
508 empty lines is equal or larger to the number given in this variable.
509 So the default 2 means, at least 2 empty lines after the end of a subtree
510 are needed to produce free space between a collapsed subtree and the
513 Special case: when 0, never leave empty lines in collapsed view."
517 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
518 org-cycle-hide-drawers
519 org-cycle-show-empty-lines
520 org-optimize-window-after-visibility-change
)
521 "Hook that is run after `org-cycle' has changed the buffer visibility.
522 The function(s) in this hook must accept a single argument which indicates
523 the new state that was set by the most recent `org-cycle' command. The
524 argument is a symbol. After a global state change, it can have the values
525 `overview', `content', or `all'. After a local state change, it can have
526 the values `folded', `children', or `subtree'."
530 (defgroup org-edit-structure nil
531 "Options concerning structure editing in Org-mode."
532 :tag
"Org Edit Structure"
533 :group
'org-structure
)
535 (defcustom org-odd-levels-only nil
536 "Non-nil means, skip even levels and only use odd levels for the outline.
537 This has the effect that two stars are being added/taken away in
538 promotion/demotion commands. It also influences how levels are
539 handled by the exporters.
540 Changing it requires restart of `font-lock-mode' to become effective
541 for fontification also in regions already fontified.
542 You may also set this on a per-file basis by adding one of the following
547 :group
'org-edit-structure
548 :group
'org-font-lock
551 (defcustom org-adapt-indentation t
552 "Non-nil means, adapt indentation when promoting and demoting.
553 When this is set and the *entire* text in an entry is indented, the
554 indentation is increased by one space in a demotion command, and
555 decreased by one in a promotion command. If any line in the entry
556 body starts at column 0, indentation is not changed at all."
557 :group
'org-edit-structure
560 (defcustom org-special-ctrl-a
/e nil
561 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
562 When t, `C-a' will bring back the cursor to the beginning of the
563 headline text, i.e. after the stars and after a possible TODO keyword.
564 In an item, this will be the position after the bullet.
565 When the cursor is already at that position, another `C-a' will bring
566 it to the beginning of the line.
567 `C-e' will jump to the end of the headline, ignoring the presence of tags
568 in the headline. A second `C-e' will then jump to the true end of the
569 line, after any tags.
570 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
571 and only a directly following, identical keypress will bring the cursor
572 to the special positions."
573 :group
'org-edit-structure
575 (const :tag
"off" nil
)
576 (const :tag
"after bullet first" t
)
577 (const :tag
"border first" reversed
)))
579 (if (fboundp 'defvaralias
)
580 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
582 (defcustom org-special-ctrl-k nil
583 "Non-nil means `C-k' will behave specially in headlines.
584 When nil, `C-k' will call the default `kill-line' command.
585 When t, the following will happen while the cursor is in the headline:
587 - When the cursor is at the beginning of a headline, kill the entire
588 line and possible the folded subtree below the line.
589 - When in the middle of the headline text, kill the headline up to the tags.
590 - When after the headline text, kill the tags."
591 :group
'org-edit-structure
594 (defcustom org-M-RET-may-split-line
'((default . t
))
595 "Non-nil means, M-RET will split the line at the cursor position.
596 When nil, it will go to the end of the line before making a
598 You may also set this option in a different way for different
599 contexts. Valid contexts are:
601 headline when creating a new headline
602 item when creating a new item
603 table in a table field
604 default the value to be used for all contexts not explicitly
606 :group
'org-structure
609 (const :tag
"Always" t
)
610 (const :tag
"Never" nil
)
611 (repeat :greedy t
:tag
"Individual contexts"
613 (choice :tag
"Context"
621 (defcustom org-blank-before-new-entry
'((heading . nil
)
622 (plain-list-item . nil
))
623 "Should `org-insert-heading' leave a blank line before new heading/item?
624 The value is an alist, with `heading' and `plain-list-item' as car,
625 and a boolean flag as cdr."
626 :group
'org-edit-structure
628 (cons (const heading
) (boolean))
629 (cons (const plain-list-item
) (boolean))))
631 (defcustom org-insert-heading-hook nil
632 "Hook being run after inserting a new heading."
633 :group
'org-edit-structure
636 (defcustom org-enable-fixed-width-editor t
637 "Non-nil means, lines starting with \":\" are treated as fixed-width.
638 This currently only means, they are never auto-wrapped.
639 When nil, such lines will be treated like ordinary lines.
640 See also the QUOTE keyword."
641 :group
'org-edit-structure
644 (defcustom org-goto-auto-isearch t
645 "Non-nil means, typing characters in org-goto starts incremental search."
646 :group
'org-edit-structure
649 (defgroup org-sparse-trees nil
650 "Options concerning sparse trees in Org-mode."
651 :tag
"Org Sparse Trees"
652 :group
'org-structure
)
654 (defcustom org-highlight-sparse-tree-matches t
655 "Non-nil means, highlight all matches that define a sparse tree.
656 The highlights will automatically disappear the next time the buffer is
657 changed by an edit command."
658 :group
'org-sparse-trees
661 (defcustom org-remove-highlights-with-change t
662 "Non-nil means, any change to the buffer will remove temporary highlights.
663 Such highlights are created by `org-occur' and `org-clock-display'.
664 When nil, `C-c C-c needs to be used to get rid of the highlights.
665 The highlights created by `org-preview-latex-fragment' always need
666 `C-c C-c' to be removed."
667 :group
'org-sparse-trees
672 (defcustom org-occur-hook
'(org-first-headline-recenter)
673 "Hook that is run after `org-occur' has constructed a sparse tree.
674 This can be used to recenter the window to show as much of the structure
676 :group
'org-sparse-trees
679 (defgroup org-plain-lists nil
680 "Options concerning plain lists in Org-mode."
681 :tag
"Org Plain lists"
682 :group
'org-structure
)
684 (defcustom org-cycle-include-plain-lists nil
685 "Non-nil means, include plain lists into visibility cycling.
686 This means that during cycling, plain list items will *temporarily* be
687 interpreted as outline headlines with a level given by 1000+i where i is the
688 indentation of the bullet. In all other operations, plain list items are
689 not seen as headlines. For example, you cannot assign a TODO keyword to
691 :group
'org-plain-lists
694 (defcustom org-plain-list-ordered-item-terminator t
695 "The character that makes a line with leading number an ordered list item.
696 Valid values are ?. and ?\). To get both terminators, use t. While
697 ?. may look nicer, it creates the danger that a line with leading
698 number may be incorrectly interpreted as an item. ?\) therefore is
700 :group
'org-plain-lists
701 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
702 (const :tag
"paren like in \"2)\"" ?\
))
703 (const :tab
"both" t
)))
705 (defcustom org-auto-renumber-ordered-lists t
706 "Non-nil means, automatically renumber ordered plain lists.
707 Renumbering happens when the sequence have been changed with
708 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
709 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
710 :group
'org-plain-lists
713 (defcustom org-provide-checkbox-statistics t
714 "Non-nil means, update checkbox statistics after insert and toggle.
715 When this is set, checkbox statistics is updated each time you either insert
716 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
717 with \\[org-ctrl-c-ctrl-c\\]."
718 :group
'org-plain-lists
721 (defgroup org-archive nil
722 "Options concerning archiving in Org-mode."
724 :group
'org-structure
)
726 (defcustom org-archive-tag
"ARCHIVE"
727 "The tag that marks a subtree as archived.
728 An archived subtree does not open during visibility cycling, and does
729 not contribute to the agenda listings.
730 After changing this, font-lock must be restarted in the relevant buffers to
731 get the proper fontification."
736 (defcustom org-agenda-skip-archived-trees t
737 "Non-nil means, the agenda will skip any items located in archived trees.
738 An archived tree is a tree marked with the tag ARCHIVE."
740 :group
'org-agenda-skip
743 (defcustom org-cycle-open-archived-trees nil
744 "Non-nil means, `org-cycle' will open archived trees.
745 An archived tree is a tree marked with the tag ARCHIVE.
746 When nil, archived trees will stay folded. You can still open them with
747 normal outline commands like `show-all', but not with the cycling commands."
752 (defcustom org-sparse-tree-open-archived-trees nil
753 "Non-nil means sparse tree construction shows matches in archived trees.
754 When nil, matches in these trees are highlighted, but the trees are kept in
757 :group
'org-sparse-trees
760 (defcustom org-archive-location
"%s_archive::"
761 "The location where subtrees should be archived.
762 This string consists of two parts, separated by a double-colon.
764 The first part is a file name - when omitted, archiving happens in the same
765 file. %s will be replaced by the current file name (without directory part).
766 Archiving to a different file is useful to keep archived entries from
767 contributing to the Org-mode Agenda.
769 The part after the double colon is a headline. The archived entries will be
770 filed under that headline. When omitted, the subtrees are simply filed away
771 at the end of the file, as top-level entries.
773 Here are a few examples:
775 If the current file is Projects.org, archive in file
776 Projects.org_archive, as top-level trees. This is the default.
778 \"::* Archived Tasks\"
779 Archive in the current file, under the top-level headline
780 \"* Archived Tasks\".
782 \"~/org/archive.org::\"
783 Archive in file ~/org/archive.org (absolute path), as top-level trees.
785 \"basement::** Finished Tasks\"
786 Archive in file ./basement (relative path), as level 3 trees
787 below the level 2 heading \"** Finished Tasks\".
789 You may set this option on a per-file basis by adding to the buffer a
792 #+ARCHIVE: basement::** Finished Tasks"
796 (defcustom org-archive-mark-done t
797 "Non-nil means, mark entries as DONE when they are moved to the archive file.
798 This can be a string to set the keyword to use. When t, Org-mode will
799 use the first keyword in its list that means done."
802 (const :tag
"No" nil
)
804 (string :tag
"Use this keyword")))
806 (defcustom org-archive-stamp-time t
807 "Non-nil means, add a time stamp to entries moved to an archive file.
808 This variable is obsolete and has no effect anymore, instead add ot remove
809 `time' from the variablle `org-archive-save-context-info'."
813 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
814 "Parts of context info that should be stored as properties when archiving.
815 When a subtree is moved to an archive file, it looses information given by
816 context, like inherited tags, the category, and possibly also the TODO
817 state (depending on the variable `org-archive-mark-done').
818 This variable can be a list of any of the following symbols:
820 time The time of archiving.
821 file The file where the entry originates.
822 itags The local tags, in the headline of the subtree.
823 ltags The tags the subtree inherits from further up the hierarchy.
824 todo The pre-archive TODO state.
825 category The category, taken from file name or #+CATEGORY lines.
826 olpath The outline path to the item. These are all headlines above
827 the current item, separated by /, like a file path.
829 For each symbol present in the list, a property will be created in
830 the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
833 :type
'(set :greedy t
834 (const :tag
"Time" time
)
835 (const :tag
"File" file
)
836 (const :tag
"Category" category
)
837 (const :tag
"TODO state" todo
)
838 (const :tag
"TODO state" priority
)
839 (const :tag
"Inherited tags" itags
)
840 (const :tag
"Outline path" olpath
)
841 (const :tag
"Local tags" ltags
)))
843 (defgroup org-imenu-and-speedbar nil
844 "Options concerning imenu and speedbar in Org-mode."
845 :tag
"Org Imenu and Speedbar"
846 :group
'org-structure
)
848 (defcustom org-imenu-depth
2
849 "The maximum level for Imenu access to Org-mode headlines.
850 This also applied for speedbar access."
851 :group
'org-imenu-and-speedbar
854 (defgroup org-table nil
855 "Options concerning tables in Org-mode."
859 (defcustom org-enable-table-editor
'optimized
860 "Non-nil means, lines starting with \"|\" are handled by the table editor.
861 When nil, such lines will be treated like ordinary lines.
863 When equal to the symbol `optimized', the table editor will be optimized to
865 - Automatic overwrite mode in front of whitespace in table fields.
866 This makes the structure of the table stay in tact as long as the edited
867 field does not exceed the column width.
868 - Minimize the number of realigns. Normally, the table is aligned each time
869 TAB or RET are pressed to move to another field. With optimization this
870 happens only if changes to a field might have changed the column width.
871 Optimization requires replacing the functions `self-insert-command',
872 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
873 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
874 very good at guessing when a re-align will be necessary, but you can always
875 force one with \\[org-ctrl-c-ctrl-c].
877 If you would like to use the optimized version in Org-mode, but the
878 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
880 This variable can be used to turn on and off the table editor during a session,
881 but in order to toggle optimization, a restart is required.
883 See also the variable `org-table-auto-blank-field'."
886 (const :tag
"off" nil
)
888 (const :tag
"on, optimized" optimized
)))
890 (defcustom orgtbl-optimized
(eq org-enable-table-editor
'optimized
)
891 "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
892 In the optimized version, the table editor takes over all simple keys that
893 normally just insert a character. In tables, the characters are inserted
894 in a way to minimize disturbing the table structure (i.e. in overwrite mode
895 for empty fields). Outside tables, the correct binding of the keys is
898 The default for this option is t if the optimized version is also used in
899 Org-mode. See the variable `org-enable-table-editor' for details. Changing
900 this variable requires a restart of Emacs to become effective."
904 (defcustom orgtbl-radio-table-templates
905 '((latex-mode "% BEGIN RECEIVE ORGTBL %n
906 % END RECEIVE ORGTBL %n
908 #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
911 (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
912 @c END RECEIVE ORGTBL %n
914 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
917 (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
918 <!-- END RECEIVE ORGTBL %n -->
920 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
923 "Templates for radio tables in different major modes.
924 All occurrences of %n in a template will be replaced with the name of the
925 table, obtained by prompting the user."
928 (list (symbol :tag
"Major mode")
929 (string :tag
"Format"))))
931 (defgroup org-table-settings nil
932 "Settings for tables in Org-mode."
933 :tag
"Org Table Settings"
936 (defcustom org-table-default-size
"5x2"
937 "The default size for newly created tables, Columns x Rows."
938 :group
'org-table-settings
941 (defcustom org-table-number-regexp
942 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
943 "Regular expression for recognizing numbers in table columns.
944 If a table column contains mostly numbers, it will be aligned to the
945 right. If not, it will be aligned to the left.
947 The default value of this option is a regular expression which allows
948 anything which looks remotely like a number as used in scientific
949 context. For example, all of the following will be considered a
951 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
953 Other options offered by the customize interface are more restrictive."
954 :group
'org-table-settings
956 (const :tag
"Positive Integers"
958 (const :tag
"Integers"
960 (const :tag
"Floating Point Numbers"
961 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
962 (const :tag
"Floating Point Number or Integer"
963 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
964 (const :tag
"Exponential, Floating point, Integer"
965 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
966 (const :tag
"Very General Number-Like, including hex"
967 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
968 (string :tag
"Regexp:")))
970 (defcustom org-table-number-fraction
0.5
971 "Fraction of numbers in a column required to make the column align right.
972 In a column all non-white fields are considered. If at least this
973 fraction of fields is matched by `org-table-number-fraction',
974 alignment to the right border applies."
975 :group
'org-table-settings
978 (defgroup org-table-editing nil
979 "Behavior of tables during editing in Org-mode."
980 :tag
"Org Table Editing"
983 (defcustom org-table-automatic-realign t
984 "Non-nil means, automatically re-align table when pressing TAB or RETURN.
985 When nil, aligning is only done with \\[org-table-align], or after column
987 :group
'org-table-editing
990 (defcustom org-table-auto-blank-field t
991 "Non-nil means, automatically blank table field when starting to type into it.
992 This only happens when typing immediately after a field motion
993 command (TAB, S-TAB or RET).
994 Only relevant when `org-enable-table-editor' is equal to `optimized'."
995 :group
'org-table-editing
998 (defcustom org-table-tab-jumps-over-hlines t
999 "Non-nil means, tab in the last column of a table with jump over a hline.
1000 If a horizontal separator line is following the current line,
1001 `org-table-next-field' can either create a new row before that line, or jump
1002 over the line. When this option is nil, a new line will be created before
1004 :group
'org-table-editing
1007 (defcustom org-table-tab-recognizes-table.el t
1008 "Non-nil means, TAB will automatically notice a table.el table.
1009 When it sees such a table, it moves point into it and - if necessary -
1010 calls `table-recognize-table'."
1011 :group
'org-table-editing
1014 (defgroup org-table-calculation nil
1015 "Options concerning tables in Org-mode."
1016 :tag
"Org Table Calculation"
1019 (defcustom org-table-use-standard-references t
1020 "Should org-mode work with table refrences like B3 instead of @3$2?
1021 Possible values are:
1023 from accept as input, do not present for editing
1024 t: accept as input and present for editing"
1025 :group
'org-table-calculation
1027 (const :tag
"Never, don't even check unser input for them" nil
)
1028 (const :tag
"Always, both as user input, and when editing" t
)
1029 (const :tag
"Convert user input, don't offer during editing" 'from
)))
1031 (defcustom org-table-copy-increment t
1032 "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
1033 :group
'org-table-calculation
1036 (defcustom org-calc-default-modes
1037 '(calc-internal-prec 12
1038 calc-float-format
(float 5)
1040 calc-prefer-frac nil
1041 calc-symbolic-mode nil
1042 calc-date-format
(YYYY "-" MM
"-" DD
" " Www
(" " HH
":" mm
))
1043 calc-display-working-message t
1045 "List with Calc mode settings for use in calc-eval for table formulas.
1046 The list must contain alternating symbols (Calc modes variables and values).
1047 Don't remove any of the default settings, just change the values. Org-mode
1048 relies on the variables to be present in the list."
1049 :group
'org-table-calculation
1052 (defcustom org-table-formula-evaluate-inline t
1053 "Non-nil means, TAB and RET evaluate a formula in current table field.
1054 If the current field starts with an equal sign, it is assumed to be a formula
1055 which should be evaluated as described in the manual and in the documentation
1056 string of the command `org-table-eval-formula'. This feature requires the
1058 When this variable is nil, formula calculation is only available through
1059 the command \\[org-table-eval-formula]."
1060 :group
'org-table-calculation
1063 (defcustom org-table-formula-use-constants t
1064 "Non-nil means, interpret constants in formulas in tables.
1065 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
1066 by the value given in `org-table-formula-constants', or by a value obtained
1067 from the `constants.el' package."
1068 :group
'org-table-calculation
1071 (defcustom org-table-formula-constants nil
1072 "Alist with constant names and values, for use in table formulas.
1073 The car of each element is a name of a constant, without the `$' before it.
1074 The cdr is the value as a string. For example, if you'd like to use the
1075 speed of light in a formula, you would configure
1077 (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
1079 and then use it in an equation like `$1*$c'.
1081 Constants can also be defined on a per-file basis using a line like
1083 #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
1084 :group
'org-table-calculation
1086 (cons (string :tag
"name")
1087 (string :tag
"value"))))
1089 (defvar org-table-formula-constants-local nil
1090 "Local version of `org-table-formula-constants'.")
1091 (make-variable-buffer-local 'org-table-formula-constants-local
)
1093 (defcustom org-table-allow-automatic-line-recalculation t
1094 "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
1095 Automatically means, when TAB or RET or C-c C-c are pressed in the line."
1096 :group
'org-table-calculation
1099 (defgroup org-link nil
1100 "Options concerning links in Org-mode."
1104 (defvar org-link-abbrev-alist-local nil
1105 "Buffer-local version of `org-link-abbrev-alist', which see.
1106 The value of this is taken from the #+LINK lines.")
1107 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1109 (defcustom org-link-abbrev-alist nil
1110 "Alist of link abbreviations.
1111 The car of each element is a string, to be replaced at the start of a link.
1112 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1113 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1115 [[linkkey:tag][description]]
1117 If REPLACE is a string, the tag will simply be appended to create the link.
1118 If the string contains \"%s\", the tag will be inserted there.
1120 REPLACE may also be a function that will be called with the tag as the
1121 only argument to create the link, which should be returned as a string.
1123 See the manual for examples."
1127 (defcustom org-descriptive-links t
1128 "Non-nil means, hide link part and only show description of bracket links.
1129 Bracket links are like [[link][descritpion]]. This variable sets the initial
1130 state in new org-mode buffers. The setting can then be toggled on a
1131 per-buffer basis from the Org->Hyperlinks menu."
1135 (defcustom org-link-file-path-type
'adaptive
1136 "How the path name in file links should be stored.
1139 relative Relative to the current directory, i.e. the directory of the file
1140 into which the link is being inserted.
1141 absolute Absolute path, if possible with ~ for home directory.
1142 noabbrev Absolute path, no abbreviation of home directory.
1143 adaptive Use relative path for files in the current directory and sub-
1144 directories of it. For other files, use an absolute path."
1152 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
1153 "Types of links that should be activated in Org-mode files.
1154 This is a list of symbols, each leading to the activation of a certain link
1155 type. In principle, it does not hurt to turn on most link types - there may
1156 be a small gain when turning off unused link types. The types are:
1158 bracket The recommended [[link][description]] or [[link]] links with hiding.
1159 angular Links in angular brackes that may contain whitespace like
1160 <bbdb:Carsten Dominik>.
1161 plain Plain links in normal text, no whitespace, like http://google.com.
1162 radio Text that is matched by a radio target, see manual for details.
1163 tag Tag settings in a headline (link to tag search).
1164 date Time stamps (link to calendar).
1166 Changing this variable requires a restart of Emacs to become effective."
1168 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
1169 (const :tag
"Angular bracket links (old style)" angular
)
1170 (const :tag
"Plain text links" plain
)
1171 (const :tag
"Radio target matches" radio
)
1172 (const :tag
"Tags" tag
)
1173 (const :tag
"Timestamps" date
)))
1175 (defgroup org-link-store nil
1176 "Options concerning storing links in Org-mode"
1177 :tag
"Org Store Link"
1180 (defcustom org-email-link-description-format
"Email %c: %.30s"
1181 "Format of the description part of a link to an email or usenet message.
1182 The following %-excapes will be replaced by corresponding information:
1184 %F full \"From\" field
1185 %f name, taken from \"From\" field, address if no name
1186 %T full \"To\" field
1187 %t first name in \"To\" field, address if no name
1188 %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
1189 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1193 You may use normal field width specification between the % and the letter.
1194 This is for example useful to limit the length of the subject.
1196 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1197 :group
'org-link-store
1200 (defcustom org-from-is-user-regexp
1202 (when (and user-mail-address
(not (string= user-mail-address
"")))
1203 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1204 (when (and user-full-name
(not (string= user-full-name
"")))
1205 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1206 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1207 "Regexp mached against the \"From:\" header of an email or usenet message.
1208 It should match if the message is from the user him/herself."
1209 :group
'org-link-store
1212 (defcustom org-context-in-file-links t
1213 "Non-nil means, file links from `org-store-link' contain context.
1214 A search string will be added to the file name with :: as separator and
1215 used to find the context when the link is activated by the command
1216 `org-open-at-point'.
1217 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1218 negates this setting for the duration of the command."
1219 :group
'org-link-store
1222 (defcustom org-keep-stored-link-after-insertion nil
1223 "Non-nil means, keep link in list for entire session.
1225 The command `org-store-link' adds a link pointing to the current
1226 location to an internal list. These links accumulate during a session.
1227 The command `org-insert-link' can be used to insert links into any
1228 Org-mode file (offering completion for all stored links). When this
1229 option is nil, every link which has been inserted once using \\[org-insert-link]
1230 will be removed from the list, to make completing the unused links
1232 :group
'org-link-store
1235 (defcustom org-usenet-links-prefer-google nil
1236 "Non-nil means, `org-store-link' will create web links to Google groups.
1237 When nil, Gnus will be used for such links.
1238 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1239 negates this setting for the duration of the command."
1240 :group
'org-link-store
1243 (defgroup org-link-follow nil
1244 "Options concerning following links in Org-mode"
1245 :tag
"Org Follow Link"
1248 (defcustom org-follow-link-hook nil
1249 "Hook that is run after a link has been followed."
1250 :group
'org-link-follow
1253 (defcustom org-tab-follows-link nil
1254 "Non-nil means, on links TAB will follow the link.
1255 Needs to be set before org.el is loaded."
1256 :group
'org-link-follow
1259 (defcustom org-return-follows-link nil
1260 "Non-nil means, on links RET will follow the link.
1261 Needs to be set before org.el is loaded."
1262 :group
'org-link-follow
1265 (defcustom org-mouse-1-follows-link
1266 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1267 "Non-nil means, mouse-1 on a link will follow the link.
1268 A longer mouse click will still set point. Does not work on XEmacs.
1269 Needs to be set before org.el is loaded."
1270 :group
'org-link-follow
1273 (defcustom org-mark-ring-length
4
1274 "Number of different positions to be recorded in the ring
1275 Changing this requires a restart of Emacs to work correctly."
1276 :group
'org-link-follow
1279 (defcustom org-link-frame-setup
1280 '((vm . vm-visit-folder-other-frame
)
1281 (gnus . gnus-other-frame
)
1282 (file . find-file-other-window
))
1283 "Setup the frame configuration for following links.
1284 When following a link with Emacs, it may often be useful to display
1285 this link in another window or frame. This variable can be used to
1286 set this up for the different types of links.
1289 `vm-visit-folder-other-frame'
1290 For Gnus, use any of
1293 For FILE, use any of
1295 `find-file-other-window'
1296 `find-file-other-frame'
1297 For the calendar, use the variable `calendar-setup'.
1298 For BBDB, it is currently only possible to display the matches in
1300 :group
'org-link-follow
1304 (const vm-visit-folder
)
1305 (const vm-visit-folder-other-window
)
1306 (const vm-visit-folder-other-frame
)))
1310 (const gnus-other-frame
)))
1314 (const find-file-other-window
)
1315 (const find-file-other-frame
)))))
1317 (defcustom org-display-internal-link-with-indirect-buffer nil
1318 "Non-nil means, use indirect buffer to display infile links.
1319 Activating internal links (from one location in a file to another location
1320 in the same file) normally just jumps to the location. When the link is
1321 activated with a C-u prefix (or with mouse-3), the link is displayed in
1322 another window. When this option is set, the other window actually displays
1323 an indirect buffer clone of the current buffer, to avoid any visibility
1324 changes to the current buffer."
1325 :group
'org-link-follow
1328 (defcustom org-open-non-existing-files nil
1329 "Non-nil means, `org-open-file' will open non-existing files.
1330 When nil, an error will be generated."
1331 :group
'org-link-follow
1334 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1335 "Function and arguments to call for following mailto links.
1336 This is a list with the first element being a lisp function, and the
1337 remaining elements being arguments to the function. In string arguments,
1338 %a will be replaced by the address, and %s will be replaced by the subject
1339 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1340 :group
'org-link-follow
1342 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1343 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1344 (const :tag
"message-mail" (message-mail "%a" "%s"))
1345 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1347 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1348 "Non-nil means, ask for confirmation before executing shell links.
1349 Shell links can be dangerous: just think about a link
1351 [[shell:rm -rf ~/*][Google Search]]
1353 This link would show up in your Org-mode document as \"Google Search\",
1354 but really it would remove your entire home directory.
1355 Therefore we advise against setting this variable to nil.
1356 Just change it to `y-or-n-p' of you want to confirm with a
1357 single keystroke rather than having to type \"yes\"."
1358 :group
'org-link-follow
1360 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1361 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1362 (const :tag
"no confirmation (dangerous)" nil
)))
1364 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1365 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1366 Elisp links can be dangerous: just think about a link
1368 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1370 This link would show up in your Org-mode document as \"Google Search\",
1371 but really it would remove your entire home directory.
1372 Therefore we advise against setting this variable to nil.
1373 Just change it to `y-or-n-p' of you want to confirm with a
1374 single keystroke rather than having to type \"yes\"."
1375 :group
'org-link-follow
1377 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1378 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1379 (const :tag
"no confirmation (dangerous)" nil
)))
1381 (defconst org-file-apps-defaults-gnu
1384 "Default file applications on a UNIX or GNU/Linux system.
1385 See `org-file-apps'.")
1387 (defconst org-file-apps-defaults-macosx
1393 ("eps.gz" .
"gv %s")
1395 ("fig" .
"xfig %s"))
1396 "Default file applications on a MacOS X system.
1397 The system \"open\" is known as a default, but we use X11 applications
1398 for some files for which the OS does not have a good default.
1399 See `org-file-apps'.")
1401 (defconst org-file-apps-defaults-windowsnt
1405 (list (if (featurep 'xemacs
)
1406 'mswindows-shell-execute
1409 "Default file applications on a Windows NT system.
1410 The system \"open\" is used for most files.
1411 See `org-file-apps'.")
1413 (defcustom org-file-apps
1422 "External applications for opening `file:path' items in a document.
1423 Org-mode uses system defaults for different file types, but
1424 you can use this variable to set the application for a given file
1425 extension. The entries in this list are cons cells where the car identifies
1426 files and the cdr the corresponding command. Possible values for the
1428 \"ext\" A string identifying an extension
1429 `directory' Matches a directory
1430 `remote' Matches a remote file, accessible through tramp or efs.
1431 Remote files most likely should be visited through Emacs
1432 because external applications cannot handle such paths.
1433 t Default for all remaining files
1435 Possible values for the command are:
1436 `emacs' The file will be visited by the current Emacs process.
1437 `default' Use the default application for this file type.
1438 string A command to be executed by a shell; %s will be replaced
1439 by the path to the file.
1440 sexp A Lisp form which will be evaluated. The file path will
1441 be available in the Lisp variable `file'.
1442 For more examples, see the system specific constants
1443 `org-file-apps-defaults-macosx'
1444 `org-file-apps-defaults-windowsnt'
1445 `org-file-apps-defaults-gnu'."
1446 :group
'org-link-follow
1448 (cons (choice :value
""
1449 (string :tag
"Extension")
1450 (const :tag
"Default for unrecognized files" t
)
1451 (const :tag
"Remote file" remote
)
1452 (const :tag
"Links to a directory" directory
))
1454 (const :tag
"Visit with Emacs" emacs
)
1455 (const :tag
"Use system default" default
)
1456 (string :tag
"Command")
1457 (sexp :tag
"Lisp form")))))
1459 (defcustom org-mhe-search-all-folders nil
1460 "Non-nil means, that the search for the mh-message will be extended to
1461 all folders if the message cannot be found in the folder given in the link.
1462 Searching all folders is very efficient with one of the search engines
1463 supported by MH-E, but will be slow with pick."
1464 :group
'org-link-follow
1467 (defgroup org-remember nil
1468 "Options concerning interaction with remember.el."
1472 (defcustom org-directory
"~/org"
1473 "Directory with org files.
1474 This directory will be used as default to prompt for org files.
1475 Used by the hooks for remember.el."
1476 :group
'org-remember
1479 (defcustom org-default-notes-file
"~/.notes"
1480 "Default target for storing notes.
1481 Used by the hooks for remember.el. This can be a string, or nil to mean
1482 the value of `remember-data-file'.
1483 You can set this on a per-template basis with the variable
1484 `org-remember-templates'."
1485 :group
'org-remember
1487 (const :tag
"Default from remember-data-file" nil
)
1490 (defcustom org-remember-store-without-prompt t
1491 "Non-nil means, `C-c C-c' stores remember note without further promts.
1492 In this case, you need `C-u C-c C-c' to get the prompts for
1493 note file and headline.
1494 When this variable is nil, `C-c C-c' give you the prompts, and
1495 `C-u C-c C-c' trigger the fasttrack."
1496 :group
'org-remember
1499 (defcustom org-remember-interactive-interface
'refile
1500 "The interface to be used for interactive filing of remember notes.
1501 This is only used when the interactive mode for selecting a filing
1502 location is used (see the variable `org-remember-store-without-prompt').
1504 outline The interface shows an outline of the relevant file
1505 and the correct heading is found by moving through
1506 the outline or by searching with incremental search.
1507 outline-path-completion Headlines in the current buffer are offered via
1509 refile Use the refile interface, and offer headlines,
1510 possibly from different buffers."
1511 :group
'org-remember
1513 (const :tag
"Refile" refile
)
1514 (const :tag
"Outline" outline
)
1515 (const :tag
"Outline-path-completion" outline-path-completion
)))
1517 (defcustom org-goto-interface
'outline
1518 "The default interface to be used for `org-goto'.
1520 outline The interface shows an outline of the relevant file
1521 and the correct heading is found by moving through
1522 the outline or by searching with incremental search.
1523 outline-path-completion Headlines in the current buffer are offered via
1525 :group
'org-remember
; FIXME: different group for org-goto and org-refile
1527 (const :tag
"Outline" outline
)
1528 (const :tag
"Outline-path-completion" outline-path-completion
)))
1530 (defcustom org-remember-default-headline
""
1531 "The headline that should be the default location in the notes file.
1532 When filing remember notes, the cursor will start at that position.
1533 You can set this on a per-template basis with the variable
1534 `org-remember-templates'."
1535 :group
'org-remember
1538 (defcustom org-remember-templates nil
1539 "Templates for the creation of remember buffers.
1540 When nil, just let remember make the buffer.
1541 When not nil, this is a list of 5-element lists. In each entry, the first
1542 element is the name of the template, which should be a single short word.
1543 The second element is a character, a unique key to select this template.
1544 The third element is the template. The fourth element is optional and can
1545 specify a destination file for remember items created with this template.
1546 The default file is given by `org-default-notes-file'. An optional fifth
1547 element can specify the headline in that file that should be offered
1548 first when the user is asked to file the entry. The default headline is
1549 given in the variable `org-remember-default-headline'.
1551 An optional sixth element specifies the contexts in which the user can
1552 select the template. This element can be either a list of major modes
1553 or a function. `org-remember' will first check whether the function
1554 returns `t' or if we are in any of the listed major mode, and select
1555 the template accordingly.
1557 The template specifies the structure of the remember buffer. It should have
1558 a first line starting with a star, to act as the org-mode headline.
1559 Furthermore, the following %-escapes will be replaced with content:
1561 %^{prompt} Prompt the user for a string and replace this sequence with it.
1562 A default value and a completion table ca be specified like this:
1563 %^{prompt|default|completion2|completion3|...}
1564 %t time stamp, date only
1565 %T time stamp with date and time
1566 %u, %U like the above, but inactive time stamps
1567 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
1568 You may define a prompt like %^{Please specify birthday}t
1569 %n user name (taken from `user-full-name')
1570 %a annotation, normally the link created with org-store-link
1571 %i initial content, the region active. If %i is indented,
1572 the entire inserted text will be indented as well.
1573 %c content of the clipboard, or current kill ring head
1574 %^g prompt for tags, with completion on tags in target file
1575 %^G prompt for tags, with completion all tags in all agenda files
1576 %:keyword specific information for certain link types, see below
1577 %[pathname] insert the contents of the file given by `pathname'
1578 %(sexp) evaluate elisp `(sexp)' and replace with the result
1579 %! Store this note immediately after filling the template
1581 %? After completing the template, position cursor here.
1583 Apart from these general escapes, you can access information specific to the
1584 link type that is created. For example, calling `remember' in emails or gnus
1585 will record the author and the subject of the message, which you can access
1586 with %:author and %:subject, respectively. Here is a complete list of what
1587 is recorded for each link type.
1589 Link type | Available information
1590 -------------------+------------------------------------------------------
1591 bbdb | %:type %:name %:company
1592 vm, wl, mh, rmail | %:type %:subject %:message-id
1593 | %:from %:fromname %:fromaddress
1594 | %:to %:toname %:toaddress
1595 | %:fromto (either \"to NAME\" or \"from NAME\")
1596 gnus | %:group, for messages also all email fields
1597 w3, w3m | %:type %:url
1598 info | %:type %:file %:node
1599 calendar | %:type %:date"
1600 :group
'org-remember
1601 :get
(lambda (var) ; Make sure all entries have at least 5 elements
1603 (if (not (stringp (car x
))) (setq x
(cons "" x
)))
1604 (cond ((= (length x
) 4) (append x
'("")))
1605 ((= (length x
) 3) (append x
'("" "")))
1607 (default-value var
)))
1610 (list :value
("" ?a
"\n" nil nil nil
)
1611 (string :tag
"Name")
1612 (character :tag
"Selection Key")
1613 (string :tag
"Template")
1615 (file :tag
"Destination file")
1616 (const :tag
"Prompt for file" nil
))
1618 (string :tag
"Destination headline")
1619 (const :tag
"Selection interface for heading"))
1621 (const :tag
"Use by default" nil
)
1622 (const :tag
"Use in all contexts" t
)
1623 (repeat :tag
"Use only if in major mode"
1624 (symbol :tag
"Major mode"))
1625 (function :tag
"Perform a check against function")))))
1627 (defcustom org-reverse-note-order nil
1628 "Non-nil means, store new notes at the beginning of a file or entry.
1629 When nil, new notes will be filed to the end of a file or entry.
1630 This can also be a list with cons cells of regular expressions that
1631 are matched against file names, and values."
1632 :group
'org-remember
1634 (const :tag
"Reverse always" t
)
1635 (const :tag
"Reverse never" nil
)
1636 (repeat :tag
"By file name regexp"
1637 (cons regexp boolean
))))
1639 (defcustom org-refile-targets nil
1640 "Targets for refiling entries with \\[org-refile].
1641 This is list of cons cells. Each cell contains:
1642 - a specification of the files to be considered, either a list of files,
1643 or a symbol whose function or value fields will be used to retrieve
1644 a file name or a list of file names. Nil means, refile to a different
1645 heading in the current buffer.
1646 - A specification of how to find candidate refile targets. This may be
1648 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1649 This tag has to be present in all target headlines, inheritance will
1651 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1653 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1654 headlines that are refiling targets.
1655 - a cons cell (:level . N). Any headline of level N is considered a target.
1656 - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
1657 ;; FIXME: what if there are a var and func with same name???
1658 :group
'org-remember
1661 (choice :value org-agenda-files
1662 (const :tag
"All agenda files" org-agenda-files
)
1663 (const :tag
"Current buffer" nil
)
1664 (function) (variable) (file))
1665 (choice :tag
"Identify target headline by"
1666 (cons :tag
"Specific tag" (const :tag
) (string))
1667 (cons :tag
"TODO keyword" (const :todo
) (string))
1668 (cons :tag
"Regular expression" (const :regexp
) (regexp))
1669 (cons :tag
"Level number" (const :level
) (integer))
1670 (cons :tag
"Max Level number" (const :maxlevel
) (integer))))))
1672 (defcustom org-refile-use-outline-path nil
1673 "Non-nil means, provide refile targets as paths.
1674 So a level 3 headline will be available as level1/level2/level3.
1675 When the value is `file', also include the file name (without directory)
1676 into the path. When `full-file-path', include the full file path."
1677 :group
'org-remember
1679 (const :tag
"Not" nil
)
1680 (const :tag
"Yes" t
)
1681 (const :tag
"Start with file name" file
)
1682 (const :tag
"Start with full file path" full-file-path
)))
1684 (defgroup org-todo nil
1685 "Options concerning TODO items in Org-mode."
1689 (defgroup org-progress nil
1690 "Options concerning Progress logging in Org-mode."
1694 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1695 "List of TODO entry keyword sequences and their interpretation.
1696 \\<org-mode-map>This is a list of sequences.
1698 Each sequence starts with a symbol, either `sequence' or `type',
1699 indicating if the keywords should be interpreted as a sequence of
1700 action steps, or as different types of TODO items. The first
1701 keywords are states requiring action - these states will select a headline
1702 for inclusion into the global TODO list Org-mode produces. If one of
1703 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1704 signify that no further action is necessary. If \"|\" is not found,
1705 the last keyword is treated as the only DONE state of the sequence.
1707 The command \\[org-todo] cycles an entry through these states, and one
1708 additional state where no keyword is present. For details about this
1709 cycling, see the manual.
1711 TODO keywords and interpretation can also be set on a per-file basis with
1712 the special #+SEQ_TODO and #+TYP_TODO lines.
1714 Each keyword can optionally specify a character for fast state selection
1715 \(in combination with the variable `org-use-fast-todo-selection')
1716 and specifiers for state change logging, using the same syntax
1717 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1718 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1719 indicates to record a time stamp each time this state is selected.
1721 Each keyword may also specify if a timestamp or a note should be
1722 recorded when entering or leaving the state, by adding additional
1723 characters in the parenthesis after the keyword. This looks like this:
1724 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1725 record only the time of the state change. With X and Y being either
1726 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1727 Y when leaving the state if and only if the *target* state does not
1728 define X. You may omit any of the fast-selection key or X or /Y,
1729 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1731 For backward compatibility, this variable may also be just a list
1732 of keywords - in this case the interptetation (sequence or type) will be
1733 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1735 :group
'org-keywords
1737 (repeat :tag
"Old syntax, just keywords"
1738 (string :tag
"Keyword"))
1739 (repeat :tag
"New syntax"
1742 :tag
"Interpretation"
1743 (const :tag
"Sequence (cycling hits every state)" sequence
)
1744 (const :tag
"Type (cycling directly to DONE)" type
))
1746 (string :tag
"Keyword"))))))
1748 (defvar org-todo-keywords-1 nil
1749 "All TODO and DONE keywords active in a buffer.")
1750 (make-variable-buffer-local 'org-todo-keywords-1
)
1751 (defvar org-todo-keywords-for-agenda nil
)
1752 (defvar org-done-keywords-for-agenda nil
)
1753 (defvar org-not-done-keywords nil
)
1754 (make-variable-buffer-local 'org-not-done-keywords
)
1755 (defvar org-done-keywords nil
)
1756 (make-variable-buffer-local 'org-done-keywords
)
1757 (defvar org-todo-heads nil
)
1758 (make-variable-buffer-local 'org-todo-heads
)
1759 (defvar org-todo-sets nil
)
1760 (make-variable-buffer-local 'org-todo-sets
)
1761 (defvar org-todo-log-states nil
)
1762 (make-variable-buffer-local 'org-todo-log-states
)
1763 (defvar org-todo-kwd-alist nil
)
1764 (make-variable-buffer-local 'org-todo-kwd-alist
)
1765 (defvar org-todo-key-alist nil
)
1766 (make-variable-buffer-local 'org-todo-key-alist
)
1767 (defvar org-todo-key-trigger nil
)
1768 (make-variable-buffer-local 'org-todo-key-trigger
)
1770 (defcustom org-todo-interpretation
'sequence
1771 "Controls how TODO keywords are interpreted.
1772 This variable is in principle obsolete and is only used for
1773 backward compatibility, if the interpretation of todo keywords is
1774 not given already in `org-todo-keywords'. See that variable for
1777 :group
'org-keywords
1778 :type
'(choice (const sequence
)
1781 (defcustom org-use-fast-todo-selection
'prefix
1782 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1783 This variable describes if and under what circumstances the cycling
1784 mechanism for TODO keywords will be replaced by a single-key, direct
1787 When nil, fast selection is never used.
1789 When the symbol `prefix', it will be used when `org-todo' is called with
1790 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1791 in an agenda buffer.
1793 When t, fast selection is used by default. In this case, the prefix
1794 argument forces cycling instead.
1796 In all cases, the special interface is only used if access keys have actually
1797 been assigned by the user, i.e. if keywords in the configuration are followed
1798 by a letter in parenthesis, like TODO(t)."
1801 (const :tag
"Never" nil
)
1802 (const :tag
"By default" t
)
1803 (const :tag
"Only with C-u C-c C-t" prefix
)))
1805 (defcustom org-after-todo-state-change-hook nil
1806 "Hook which is run after the state of a TODO item was changed.
1807 The new state (a string with a TODO keyword, or nil) is available in the
1808 Lisp variable `state'."
1812 (defcustom org-log-done nil
1813 "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
1814 When equal to the list (done), also prompt for a closing note.
1815 This can also be configured on a per-file basis by adding one of
1816 the following lines anywhere in the buffer:
1819 #+STARTUP: lognotedone
1820 #+STARTUP: nologdone"
1822 :group
'org-progress
1824 (const :tag
"No logging" nil
)
1825 (const :tag
"Record CLOSED timestamp" time
)
1826 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1828 ;; Normalize old uses of org-log-done.
1830 ((eq org-log-done t
) (setq org-log-done
'time
))
1831 ((and (listp org-log-done
) (memq 'done org-log-done
))
1832 (setq org-log-done
'note
)))
1835 (defcustom org-log-note-clock-out nil
1836 "Non-nil means, recored a note when clocking out of an item.
1837 This can also be configured on a per-file basis by adding one of
1838 the following lines anywhere in the buffer:
1840 #+STARTUP: lognoteclock-out
1841 #+STARTUP: nolognoteclock-out"
1843 :group
'org-progress
1846 (defcustom org-log-done-with-time t
1847 "Non-nil means, the CLOSED time stamp will contain date and time.
1848 When nil, only the date will be recorded."
1849 :group
'org-progress
1852 (defcustom org-log-note-headings
1853 '((done .
"CLOSING NOTE %t")
1854 (state .
"State %-12s %t")
1856 "Headings for notes added when clocking out or closing TODO items.
1857 The value is an alist, with the car being a symbol indicating the note
1858 context, and the cdr is the heading to be used. The heading may also be the
1860 %t in the heading will be replaced by a time stamp.
1861 %s will be replaced by the new TODO state, in double quotes.
1862 %u will be replaced by the user name.
1863 %U will be replaced by the full user name."
1865 :group
'org-progress
1866 :type
'(list :greedy t
1867 (cons (const :tag
"Heading when closing an item" done
) string
)
1869 "Heading when changing todo state (todo sequence only)"
1871 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1873 (defcustom org-log-states-order-reversed t
1874 "Non-nil means, the latest state change note will be directly after heading.
1875 When nil, the notes will be orderer according to time."
1877 :group
'org-progress
1880 (defcustom org-log-repeat
'time
1881 "Non-nil means, record moving through the DONE state when triggering repeat.
1882 An auto-repeating tasks is immediately switched back to TODO when marked
1883 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1884 the TODO keyword definition, or recording a cloing note by setting
1885 `org-log-done', there will be no record of the task moving trhough DONE.
1886 This variable forces taking a note anyway. Possible values are:
1888 nil Don't force a record
1889 time Record a time stamp
1892 This option can also be set with on a per-file-basis with
1894 #+STARTUP: logrepeat
1895 #+STARTUP: lognoterepeat
1896 #+STARTUP: nologrepeat
1898 You can have local logging settings for a subtree by setting the LOGGING
1899 property to one or more of these keywords."
1901 :group
'org-progress
1903 (const :tag
"Don't force a record" nil
)
1904 (const :tag
"Force recording the DONE state" time
)
1905 (const :tag
"Force recording a note with the DONE state" note
)))
1907 (defcustom org-clock-into-drawer
2
1908 "Should clocking info be wrapped into a drawer?
1909 When t, clocking info will always be inserted into a :CLOCK: drawer.
1910 If necessary, the drawer will be created.
1911 When nil, the drawer will not be created, but used when present.
1912 When an integer and the number of clocking entries in an item
1913 reaches or exceeds this number, a drawer will be created."
1915 :group
'org-progress
1917 (const :tag
"Always" t
)
1918 (const :tag
"Only when drawer exists" nil
)
1919 (integer :tag
"When at least N clock entries")))
1921 (defcustom org-clock-out-when-done t
1922 "When t, the clock will be stopped when the relevant entry is marked DONE.
1923 Nil means, clock will keep running until stopped explicitly with
1924 `C-c C-x C-o', or until the clock is started in a different item."
1925 :group
'org-progress
1928 (defcustom org-clock-in-switch-to-state nil
1929 "Set task to a special todo state while clocking it.
1930 The value should be the state to which the entry should be switched."
1931 :group
'org-progress
1934 (const :tag
"Don't force a state" nil
)
1935 (string :tag
"State")))
1937 (defgroup org-priorities nil
1938 "Priorities in Org-mode."
1939 :tag
"Org Priorities"
1942 (defcustom org-highest-priority ?A
1943 "The highest priority of TODO items. A character like ?A, ?B etc.
1944 Must have a smaller ASCII number than `org-lowest-priority'."
1945 :group
'org-priorities
1948 (defcustom org-lowest-priority ?C
1949 "The lowest priority of TODO items. A character like ?A, ?B etc.
1950 Must have a larger ASCII number than `org-highest-priority'."
1951 :group
'org-priorities
1954 (defcustom org-default-priority ?B
1955 "The default priority of TODO items.
1956 This is the priority an item get if no explicit priority is given."
1957 :group
'org-priorities
1960 (defcustom org-priority-start-cycle-with-default t
1961 "Non-nil means, start with default priority when starting to cycle.
1962 When this is nil, the first step in the cycle will be (depending on the
1963 command used) one higher or lower that the default priority."
1964 :group
'org-priorities
1967 (defgroup org-time nil
1968 "Options concerning time stamps and deadlines in Org-mode."
1972 (defcustom org-insert-labeled-timestamps-at-point nil
1973 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1974 When nil, these labeled time stamps are forces into the second line of an
1975 entry, just after the headline. When scheduling from the global TODO list,
1976 the time stamp will always be forced into the second line."
1980 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1981 "Formats for `format-time-string' which are used for time stamps.
1982 It is not recommended to change this constant.")
1984 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1985 "Number of minutes to round time stamps to.
1986 These are two values, the first applies when first creating a time stamp.
1987 The second applies when changing it with the commands `S-up' and `S-down'.
1988 When changing the time stamp, this means that it will change in steps
1989 of N minues, as given by the second value.
1991 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1992 numbers should be factors of 60, so for example 5, 10, 15.
1994 When this is larger than 1, you can still force an exact time-stamp by using
1995 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1996 and by using a prefix arg to `S-up/down' to specify the exact number
1997 of minutes to shift."
1999 :get
'(lambda (var) ; Make sure all entries have 5 elements
2000 (if (integerp (default-value var
))
2001 (list (default-value var
) 5)
2002 (default-value var
)))
2004 (integer :tag
"when inserting times")
2005 (integer :tag
"when modifying times")))
2007 ;; Make sure old customizations of this variable don't lead to problems.
2008 (when (integerp org-time-stamp-rounding-minutes
)
2009 (setq org-time-stamp-rounding-minutes
2010 (list org-time-stamp-rounding-minutes
2011 org-time-stamp-rounding-minutes
)))
2013 (defcustom org-display-custom-times nil
2014 "Non-nil means, overlay custom formats over all time stamps.
2015 The formats are defined through the variable `org-time-stamp-custom-formats'.
2016 To turn this on on a per-file basis, insert anywhere in the file:
2017 #+STARTUP: customtime"
2021 (make-variable-buffer-local 'org-display-custom-times
)
2023 (defcustom org-time-stamp-custom-formats
2024 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2025 "Custom formats for time stamps. See `format-time-string' for the syntax.
2026 These are overlayed over the default ISO format if the variable
2027 `org-display-custom-times' is set. Time like %H:%M should be at the
2028 end of the second format."
2032 (defun org-time-stamp-format (&optional long inactive
)
2033 "Get the right format for a time string."
2034 (let ((f (if long
(cdr org-time-stamp-formats
)
2035 (car org-time-stamp-formats
))))
2037 (concat "[" (substring f
1 -
1) "]")
2040 (defcustom org-read-date-prefer-future t
2041 "Non-nil means, assume future for incomplete date input from user.
2042 This affects the following situations:
2043 1. The user gives a day, but no month.
2044 For example, if today is the 15th, and you enter \"3\", Org-mode will
2045 read this as the third of *next* month. However, if you enter \"17\",
2046 it will be considered as *this* month.
2047 2. The user gives a month but not a year.
2048 For example, if it is april and you enter \"feb 2\", this will be read
2049 as feb 2, *next* year. \"May 5\", however, will be this year.
2051 When this option is nil, the current month and year will always be used
2056 (defcustom org-read-date-display-live t
2057 "Non-nil means, display current interpretation of date prompt live.
2058 This display will be in an overlay, in the minibuffer."
2062 (defcustom org-read-date-popup-calendar t
2063 "Non-nil means, pop up a calendar when prompting for a date.
2064 In the calendar, the date can be selected with mouse-1. However, the
2065 minibuffer will also be active, and you can simply enter the date as well.
2066 When nil, only the minibuffer will be available."
2069 (if (fboundp 'defvaralias
)
2070 (defvaralias 'org-popup-calendar-for-date-prompt
2071 'org-read-date-popup-calendar
))
2073 (defcustom org-extend-today-until
0
2074 "The hour when your day really ends.
2075 This has influence for the following applications:
2076 - When switching the agenda to \"today\". It it is still earlier than
2077 the time given here, the day recognized as TODAY is actually yesterday.
2078 - When a date is read from the user and it is still before the time given
2079 here, the current date and time will be assumed to be yesterday, 23:59.
2082 IMPORTANT: This is still a very experimental feature, it may disappear
2083 again or it may be extended to mean more things."
2087 (defcustom org-edit-timestamp-down-means-later nil
2088 "Non-nil means, S-down will increase the time in a time stamp.
2089 When nil, S-up will increase."
2093 (defcustom org-calendar-follow-timestamp-change t
2094 "Non-nil means, make the calendar window follow timestamp changes.
2095 When a timestamp is modified and the calendar window is visible, it will be
2096 moved to the new date."
2100 (defcustom org-clock-heading-function nil
2101 "When non-nil, should be a function to create `org-clock-heading'.
2102 This is the string shown in the mode line when a clock is running.
2103 The function is called with point at the beginning of the headline."
2104 :group
'org-time
; FIXME: Should we have a separate group????
2107 (defgroup org-tags nil
2108 "Options concerning tags in Org-mode."
2112 (defcustom org-tag-alist nil
2113 "List of tags allowed in Org-mode files.
2114 When this list is nil, Org-mode will base TAG input on what is already in the
2116 The value of this variable is an alist, the car of each entry must be a
2117 keyword as a string, the cdr may be a character that is used to select
2118 that tag through the fast-tag-selection interface.
2119 See the manual for details."
2123 (cons (string :tag
"Tag name")
2124 (character :tag
"Access char"))
2125 (const :tag
"Start radio group" (:startgroup
))
2126 (const :tag
"End radio group" (:endgroup
)))))
2128 (defcustom org-use-fast-tag-selection
'auto
2129 "Non-nil means, use fast tag selection scheme.
2130 This is a special interface to select and deselect tags with single keys.
2131 When nil, fast selection is never used.
2132 When the symbol `auto', fast selection is used if and only if selection
2133 characters for tags have been configured, either through the variable
2134 `org-tag-alist' or through a #+TAGS line in the buffer.
2135 When t, fast selection is always used and selection keys are assigned
2136 automatically if necessary."
2139 (const :tag
"Always" t
)
2140 (const :tag
"Never" nil
)
2141 (const :tag
"When selection characters are configured" 'auto
)))
2143 (defcustom org-fast-tag-selection-single-key nil
2144 "Non-nil means, fast tag selection exits after first change.
2145 When nil, you have to press RET to exit it.
2146 During fast tag selection, you can toggle this flag with `C-c'.
2147 This variable can also have the value `expert'. In this case, the window
2148 displaying the tags menu is not even shown, until you press C-c again."
2151 (const :tag
"No" nil
)
2152 (const :tag
"Yes" t
)
2153 (const :tag
"Expert" expert
)))
2155 (defvar org-fast-tag-selection-include-todo nil
2156 "Non-nil means, fast tags selection interface will also offer TODO states.
2157 This is an undocumented feature, you should not rely on it.")
2159 (defcustom org-tags-column -
80
2160 "The column to which tags should be indented in a headline.
2161 If this number is positive, it specifies the column. If it is negative,
2162 it means that the tags should be flushright to that column. For example,
2163 -80 works well for a normal 80 character screen."
2167 (defcustom org-auto-align-tags t
2168 "Non-nil means, realign tags after pro/demotion of TODO state change.
2169 These operations change the length of a headline and therefore shift
2170 the tags around. With this options turned on, after each such operation
2171 the tags are again aligned to `org-tags-column'."
2175 (defcustom org-use-tag-inheritance t
2176 "Non-nil means, tags in levels apply also for sublevels.
2177 When nil, only the tags directly given in a specific line apply there.
2178 If you turn off this option, you very likely want to turn on the
2179 companion option `org-tags-match-list-sublevels'."
2183 (defcustom org-tags-match-list-sublevels nil
2184 "Non-nil means list also sublevels of headlines matching tag search.
2185 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2186 the sublevels of a headline matching a tag search often also match
2187 the same search. Listing all of them can create very long lists.
2188 Setting this variable to nil causes subtrees of a match to be skipped.
2189 This option is off by default, because inheritance in on. If you turn
2190 inheritance off, you very likely want to turn this option on.
2192 As a special case, if the tag search is restricted to TODO items, the
2193 value of this variable is ignored and sublevels are always checked, to
2194 make sure all corresponding TODO items find their way into the list."
2198 (defvar org-tags-history nil
2199 "History of minibuffer reads for tags.")
2200 (defvar org-last-tags-completion-table nil
2201 "The last used completion table for tags.")
2202 (defvar org-after-tags-change-hook nil
2203 "Hook that is run after the tags in a line have changed.")
2205 (defgroup org-properties nil
2206 "Options concerning properties in Org-mode."
2207 :tag
"Org Properties"
2210 (defcustom org-property-format
"%-10s %s"
2211 "How property key/value pairs should be formatted by `indent-line'.
2212 When `indent-line' hits a property definition, it will format the line
2213 according to this format, mainly to make sure that the values are
2214 lined-up with respect to each other."
2215 :group
'org-properties
2218 (defcustom org-use-property-inheritance nil
2219 "Non-nil means, properties apply also for sublevels.
2220 This setting is only relevant during property searches, not when querying
2221 an entry with `org-entry-get'. To retrieve a property with inheritance,
2222 you need to call `org-entry-get' with the inheritance flag.
2223 Turning this on can cause significant overhead when doing a search, so
2224 this is turned off by default.
2225 When nil, only the properties directly given in the current entry count.
2226 The value may also be a list of properties that shouldhave inheritance.
2228 However, note that some special properties use inheritance under special
2229 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2230 and the properties ending in \"_ALL\" when they are used as descriptor
2231 for valid values of a property."
2232 :group
'org-properties
2234 (const :tag
"Not" nil
)
2235 (const :tag
"Always" nil
)
2236 (repeat :tag
"Specific properties" (string :tag
"Property"))))
2238 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2239 "The default column format, if no other format has been defined.
2240 This variable can be set on the per-file basis by inserting a line
2242 #+COLUMNS: %25ITEM ....."
2243 :group
'org-properties
2246 (defcustom org-global-properties nil
2247 "List of property/value pairs that can be inherited by any entry.
2248 You can set buffer-local values for this by adding lines like
2250 #+PROPERTY: NAME VALUE"
2251 :group
'org-properties
2253 (cons (string :tag
"Property")
2254 (string :tag
"Value"))))
2256 (defvar org-local-properties nil
2257 "List of property/value pairs that can be inherited by any entry.
2258 Valid for the current buffer.
2259 This variable is populated from #+PROPERTY lines.")
2261 (defgroup org-agenda nil
2262 "Options concerning agenda views in Org-mode."
2266 (defvar org-category nil
2267 "Variable used by org files to set a category for agenda display.
2268 Such files should use a file variable to set it, for example
2270 # -*- mode: org; org-category: \"ELisp\"
2272 or contain a special line
2276 If the file does not specify a category, then file's base name
2278 (make-variable-buffer-local 'org-category
)
2280 (defcustom org-agenda-files nil
2281 "The files to be used for agenda display.
2282 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2283 \\[org-remove-file]. You can also use customize to edit the list.
2285 If an entry is a directory, all files in that directory that are matched by
2286 `org-agenda-file-regexp' will be part of the file list.
2288 If the value of the variable is not a list but a single file name, then
2289 the list of agenda files is actually stored and maintained in that file, one
2290 agenda file per line."
2293 (repeat :tag
"List of files and directories" file
)
2294 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2296 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2297 "Regular expression to match files for `org-agenda-files'.
2298 If any element in the list in that variable contains a directory instead
2299 of a normal file, all files in that directory that are matched by this
2300 regular expression will be included."
2304 (defcustom org-agenda-skip-unavailable-files nil
2305 "t means to just skip non-reachable files in `org-agenda-files'.
2306 Nil means to remove them, after a query, from the list."
2310 (defcustom org-agenda-text-search-extra-files nil
2311 "List of extra files to be searched by text search commands.
2312 These files will be search in addition to the agenda files bu the
2313 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2314 Note that these files will only be searched for text search commands,
2315 not for the other agenda views like todo lists, tag earches or the weekly
2316 agenda. This variable is intended to list notes and possibly archive files
2317 that should also be searched by these two commands."
2319 :type
'(repeat file
))
2321 (if (fboundp 'defvaralias
)
2322 (defvaralias 'org-agenda-multi-occur-extra-files
2323 'org-agenda-text-search-extra-files
))
2325 (defcustom org-agenda-confirm-kill
1
2326 "When set, remote killing from the agenda buffer needs confirmation.
2327 When t, a confirmation is always needed. When a number N, confirmation is
2328 only needed when the text to be killed contains more than N non-white lines."
2331 (const :tag
"Never" nil
)
2332 (const :tag
"Always" t
)
2333 (number :tag
"When more than N lines")))
2335 (defcustom org-calendar-to-agenda-key
[?c
]
2336 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2337 The command `org-calendar-goto-agenda' will be bound to this key. The
2338 default is the character `c' because then `c' can be used to switch back and
2339 forth between agenda and calendar."
2343 (defcustom org-agenda-compact-blocks nil
2344 "Non-nil means, make the block agenda more compact.
2345 This is done by leaving out unnecessary lines."
2349 (defgroup org-agenda-export nil
2350 "Options concerning exporting agenda views in Org-mode."
2351 :tag
"Org Agenda Export"
2354 (defcustom org-agenda-with-colors t
2355 "Non-nil means, use colors in agenda views."
2356 :group
'org-agenda-export
2359 (defcustom org-agenda-exporter-settings nil
2360 "Alist of variable/value pairs that should be active during agenda export.
2361 This is a good place to set uptions for ps-print and for htmlize."
2362 :group
'org-agenda-export
2366 (sexp :tag
"Value"))))
2368 (defcustom org-agenda-export-html-style
""
2369 "The style specification for exported HTML Agenda files.
2370 If this variable contains a string, it will replace the default <style>
2371 section as produced by `htmlize'.
2372 Since there are different ways of setting style information, this variable
2373 needs to contain the full HTML structure to provide a style, including the
2374 surrounding HTML tags. The style specifications should include definitions
2375 the fonts used by the agenda, here is an example:
2377 <style type=\"text/css\">
2378 p { font-weight: normal; color: gray; }
2379 .org-agenda-structure {
2391 .title { text-align: center; }
2392 .todo, .deadline { color: red; }
2393 .done { color: green; }
2396 or, if you want to keep the style in a file,
2398 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
2400 As the value of this option simply gets inserted into the HTML <head> header,
2401 you can \"misuse\" it to also add other text to the header. However,
2402 <style>...</style> is required, if not present the variable will be ignored."
2403 :group
'org-agenda-export
2404 :group
'org-export-html
2407 (defgroup org-agenda-custom-commands nil
2408 "Options concerning agenda views in Org-mode."
2409 :tag
"Org Agenda Custom Commands"
2412 (defconst org-sorting-choice
2414 (const time-up
) (const time-down
)
2415 (const category-keep
) (const category-up
) (const category-down
)
2416 (const tag-down
) (const tag-up
)
2417 (const priority-up
) (const priority-down
))
2420 (defconst org-agenda-custom-commands-local-options
2421 `(repeat :tag
"Local settings for this command. Remember to quote values"
2422 (choice :tag
"Setting"
2423 (list :tag
"Any variable"
2424 (variable :tag
"Variable")
2425 (sexp :tag
"Value"))
2426 (list :tag
"Files to be searched"
2427 (const org-agenda-files
)
2429 (const :format
"" quote
)
2432 (list :tag
"Sorting strategy"
2433 (const org-agenda-sorting-strategy
)
2435 (const :format
"" quote
)
2437 ,org-sorting-choice
)))
2438 (list :tag
"Prefix format"
2439 (const org-agenda-prefix-format
:value
" %-12:c%?-12t% s")
2441 (list :tag
"Number of days in agenda"
2442 (const org-agenda-ndays
)
2444 (list :tag
"Fixed starting date"
2445 (const org-agenda-start-day
)
2446 (string :value
"2007-11-01"))
2447 (list :tag
"Start on day of week"
2448 (const org-agenda-start-on-weekday
)
2450 (const :tag
"Today" nil
)
2451 (number :tag
"Weekday No.")))
2452 (list :tag
"Include data from diary"
2453 (const org-agenda-include-diary
)
2455 (list :tag
"Deadline Warning days"
2456 (const org-deadline-warning-days
)
2458 (list :tag
"Standard skipping condition"
2459 :value
(org-agenda-skip-function '(org-agenda-skip-entry-if))
2460 (const org-agenda-skip-function
)
2462 (const :format
"" quote
)
2465 :tag
"Skiping range"
2466 (const :tag
"Skip entry" org-agenda-skip-entry-if
)
2467 (const :tag
"Skip subtree" org-agenda-skip-subtree-if
))
2468 (repeat :inline t
:tag
"Conditions for skipping"
2470 :tag
"Condition type"
2471 (list :tag
"Regexp matches" :inline t
(const :format
"" 'regexp
) (regexp))
2472 (list :tag
"Regexp does not match" :inline t
(const :format
"" 'notregexp
) (regexp))
2473 (const :tag
"scheduled" 'scheduled
)
2474 (const :tag
"not scheduled" 'notscheduled
)
2475 (const :tag
"deadline" 'deadline
)
2476 (const :tag
"no deadline" 'notdeadline
))))))
2477 (list :tag
"Non-standard skipping condition"
2478 :value
(org-agenda-skip-function)
2480 (const org-agenda-skip-function
)
2481 (sexp :tag
"Function or form (quoted!)")))))
2482 "Selection of examples for agenda command settings.
2483 This will be spliced into the custom type of
2484 `org-agenda-custom-commands'.")
2487 (defcustom org-agenda-custom-commands nil
2488 "Custom commands for the agenda.
2489 These commands will be offered on the splash screen displayed by the
2490 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
2492 (key desc type match settings files)
2494 key The key (one or more characters as a string) to be associated
2496 desc A description of the command, when omitted or nil, a default
2497 description is built using MATCH.
2498 type The command type, any of the following symbols:
2499 agenda The daily/weekly agenda.
2500 todo Entries with a specific TODO keyword, in all agenda files.
2501 search Entries containing search words entry or headline.
2502 tags Tags/Property/TODO match in all agenda files.
2503 tags-todo Tags/P/T match in all agenda files, TODO entries only.
2504 todo-tree Sparse tree of specific TODO keyword in *current* file.
2505 tags-tree Sparse tree with all tags matches in *current* file.
2506 occur-tree Occur sparse tree for *current* file.
2507 ... A user-defined function.
2508 match What to search for:
2509 - a single keyword for TODO keyword searches
2510 - a tags match expression for tags searches
2511 - a word search expression for text searches.
2512 - a regular expression for occur searches
2513 For all other commands, this should be the empty string.
2514 settings A list of option settings, similar to that in a let form, so like
2515 this: ((opt1 val1) (opt2 val2) ...). The values will be
2516 evaluated at the moment of execution, so quote them when needed.
2517 files A list of files file to write the produced agenda buffer to
2518 with the command `org-store-agenda-views'.
2519 If a file name ends in \".html\", an HTML version of the buffer
2520 is written out. If it ends in \".ps\", a postscript version is
2521 produced. Otherwide, only the plain text is written to the file.
2523 You can also define a set of commands, to create a composite agenda buffer.
2524 In this case, an entry looks like this:
2526 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
2530 desc A description string to be displayed in the dispatcher menu.
2531 cmd An agenda command, similar to the above. However, tree commands
2532 are no allowed, but instead you can get agenda and global todo list.
2533 So valid commands for a set are:
2534 (agenda \"\" settings)
2535 (alltodo \"\" settings)
2536 (stuck \"\" settings)
2537 (todo \"match\" settings files)
2538 (search \"match\" settings files)
2539 (tags \"match\" settings files)
2540 (tags-todo \"match\" settings files)
2542 Each command can carry a list of options, and another set of options can be
2543 given for the whole set of commands. Individual command options take
2544 precedence over the general options.
2546 When using several characters as key to a command, the first characters
2547 are prefix commands. For the dispatcher to display useful information, you
2548 should provide a description for the prefix, like
2550 (setq org-agenda-custom-commands
2551 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
2552 (\"hl\" tags \"+HOME+Lisa\")
2553 (\"hp\" tags \"+HOME+Peter\")
2554 (\"hk\" tags \"+HOME+Kim\")))"
2555 :group
'org-agenda-custom-commands
2557 (choice :value
("x" "Describe command here" tags
"" nil
)
2558 (list :tag
"Single command"
2559 (string :tag
"Access Key(s) ")
2560 (option (string :tag
"Description"))
2562 (const :tag
"Agenda" agenda
)
2563 (const :tag
"TODO list" alltodo
)
2564 (const :tag
"Search words" search
)
2565 (const :tag
"Stuck projects" stuck
)
2566 (const :tag
"Tags search (all agenda files)" tags
)
2567 (const :tag
"Tags search of TODO entries (all agenda files)" tags-todo
)
2568 (const :tag
"TODO keyword search (all agenda files)" todo
)
2569 (const :tag
"Tags sparse tree (current buffer)" tags-tree
)
2570 (const :tag
"TODO keyword tree (current buffer)" todo-tree
)
2571 (const :tag
"Occur tree (current buffer)" occur-tree
)
2572 (sexp :tag
"Other, user-defined function"))
2573 (string :tag
"Match (only for some commands)")
2574 ,org-agenda-custom-commands-local-options
2575 (option (repeat :tag
"Export" (file :tag
"Export to"))))
2576 (list :tag
"Command series, all agenda files"
2577 (string :tag
"Access Key(s)")
2578 (string :tag
"Description ")
2579 (repeat :tag
"Component"
2582 (const :format
"" agenda
)
2583 (const :tag
"" :format
"" "")
2584 ,org-agenda-custom-commands-local-options
)
2585 (list :tag
"TODO list (all keywords)"
2586 (const :format
"" alltodo
)
2587 (const :tag
"" :format
"" "")
2588 ,org-agenda-custom-commands-local-options
)
2589 (list :tag
"Search words"
2590 (const :format
"" search
)
2591 (string :tag
"Match")
2592 ,org-agenda-custom-commands-local-options
)
2593 (list :tag
"Stuck projects"
2594 (const :format
"" stuck
)
2595 (const :tag
"" :format
"" "")
2596 ,org-agenda-custom-commands-local-options
)
2597 (list :tag
"Tags search"
2598 (const :format
"" tags
)
2599 (string :tag
"Match")
2600 ,org-agenda-custom-commands-local-options
)
2601 (list :tag
"Tags search, TODO entries only"
2602 (const :format
"" tags-todo
)
2603 (string :tag
"Match")
2604 ,org-agenda-custom-commands-local-options
)
2605 (list :tag
"TODO keyword search"
2606 (const :format
"" todo
)
2607 (string :tag
"Match")
2608 ,org-agenda-custom-commands-local-options
)
2609 (list :tag
"Other, user-defined function"
2610 (symbol :tag
"function")
2611 (string :tag
"Match")
2612 ,org-agenda-custom-commands-local-options
)))
2614 (repeat :tag
"Settings for entire command set"
2615 (list (variable :tag
"Any variable")
2616 (sexp :tag
"Value")))
2617 (option (repeat :tag
"Export" (file :tag
"Export to"))))
2618 (cons :tag
"Prefix key documentation"
2619 (string :tag
"Access Key(s)")
2620 (string :tag
"Description ")))))
2622 (defcustom org-agenda-query-register ?o
2623 "The register holding the current query string.
2624 The prupose of this is that if you construct a query string interactively,
2625 you can then use it to define a custom command."
2626 :group
'org-agenda-custom-commands
2629 (defcustom org-stuck-projects
2630 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil
"")
2631 "How to identify stuck projects.
2632 This is a list of four items:
2633 1. A tags/todo matcher string that is used to identify a project.
2634 The entire tree below a headline matched by this is considered one project.
2635 2. A list of TODO keywords identifying non-stuck projects.
2636 If the project subtree contains any headline with one of these todo
2637 keywords, the project is considered to be not stuck. If you specify
2638 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
2639 3. A list of tags identifying non-stuck projects.
2640 If the project subtree contains any headline with one of these tags,
2641 the project is considered to be not stuck. If you specify \"*\" as
2642 a tag, any tag will mark the project unstuck.
2643 4. An arbitrary regular expression matching non-stuck projects.
2645 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
2646 or `C-c a #' to produce the list."
2647 :group
'org-agenda-custom-commands
2649 (string :tag
"Tags/TODO match to identify a project")
2650 (repeat :tag
"Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
2651 (repeat :tag
"Projects are *not* stuck if they have an entry with TAG being any of" (string))
2652 (regexp :tag
"Projects are *not* stuck if this regexp matches\ninside the subtree")))
2655 (defgroup org-agenda-skip nil
2656 "Options concerning skipping parts of agenda files."
2657 :tag
"Org Agenda Skip"
2660 (defcustom org-agenda-todo-list-sublevels t
2661 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
2662 When nil, the sublevels of a TODO entry are not checked, resulting in
2663 potentially much shorter TODO lists."
2664 :group
'org-agenda-skip
2668 (defcustom org-agenda-todo-ignore-with-date nil
2669 "Non-nil means, don't show entries with a date in the global todo list.
2670 You can use this if you prefer to mark mere appointments with a TODO keyword,
2671 but don't want them to show up in the TODO list.
2672 When this is set, it also covers deadlines and scheduled items, the settings
2673 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
2675 :group
'org-agenda-skip
2679 (defcustom org-agenda-todo-ignore-scheduled nil
2680 "Non-nil means, don't show scheduled entries in the global todo list.
2681 The idea behind this is that by scheduling it, you have already taken care
2683 See also `org-agenda-todo-ignore-with-date'."
2684 :group
'org-agenda-skip
2688 (defcustom org-agenda-todo-ignore-deadlines nil
2689 "Non-nil means, don't show near deadline entries in the global todo list.
2690 Near means closer than `org-deadline-warning-days' days.
2691 The idea behind this is that such items will appear in the agenda anyway.
2692 See also `org-agenda-todo-ignore-with-date'."
2693 :group
'org-agenda-skip
2697 (defcustom org-agenda-skip-scheduled-if-done nil
2698 "Non-nil means don't show scheduled items in agenda when they are done.
2699 This is relevant for the daily/weekly agenda, not for the TODO list. And
2700 it applies only to the actual date of the scheduling. Warnings about
2701 an item with a past scheduling dates are always turned off when the item
2703 :group
'org-agenda-skip
2706 (defcustom org-agenda-skip-deadline-if-done nil
2707 "Non-nil means don't show deadines when the corresponding item is done.
2708 When nil, the deadline is still shown and should give you a happy feeling.
2709 This is relevant for the daily/weekly agenda. And it applied only to the
2710 actualy date of the deadline. Warnings about approching and past-due
2711 deadlines are always turned off when the item is DONE."
2712 :group
'org-agenda-skip
2715 (defcustom org-agenda-skip-timestamp-if-done nil
2716 "Non-nil means don't select item by timestamp or -range if it is DONE."
2717 :group
'org-agenda-skip
2720 (defcustom org-timeline-show-empty-dates
3
2721 "Non-nil means, `org-timeline' also shows dates without an entry.
2722 When nil, only the days which actually have entries are shown.
2723 When t, all days between the first and the last date are shown.
2724 When an integer, show also empty dates, but if there is a gap of more than
2725 N days, just insert a special line indicating the size of the gap."
2726 :group
'org-agenda-skip
2728 (const :tag
"None" nil
)
2729 (const :tag
"All" t
)
2730 (number :tag
"at most")))
2733 (defgroup org-agenda-startup nil
2734 "Options concerning initial settings in the Agenda in Org Mode."
2735 :tag
"Org Agenda Startup"
2738 (defcustom org-finalize-agenda-hook nil
2739 "Hook run just before displaying an agenda buffer."
2740 :group
'org-agenda-startup
2743 (defcustom org-agenda-mouse-1-follows-link nil
2744 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
2745 A longer mouse click will still set point. Does not work on XEmacs.
2746 Needs to be set before org.el is loaded."
2747 :group
'org-agenda-startup
2750 (defcustom org-agenda-start-with-follow-mode nil
2751 "The initial value of follow-mode in a newly created agenda window."
2752 :group
'org-agenda-startup
2755 (defgroup org-agenda-windows nil
2756 "Options concerning the windows used by the Agenda in Org Mode."
2757 :tag
"Org Agenda Windows"
2760 (defcustom org-agenda-window-setup
'reorganize-frame
2761 "How the agenda buffer should be displayed.
2762 Possible values for this option are:
2764 current-window Show agenda in the current window, keeping all other windows.
2765 other-frame Use `switch-to-buffer-other-frame' to display agenda.
2766 other-window Use `switch-to-buffer-other-window' to display agenda.
2767 reorganize-frame Show only two windows on the current frame, the current
2768 window and the agenda.
2769 See also the variable `org-agenda-restore-windows-after-quit'."
2770 :group
'org-agenda-windows
2772 (const current-window
)
2774 (const other-window
)
2775 (const reorganize-frame
)))
2777 (defcustom org-agenda-window-frame-fractions
'(0.5 .
0.75)
2778 "The min and max height of the agenda window as a fraction of frame height.
2779 The value of the variable is a cons cell with two numbers between 0 and 1.
2780 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
2781 :group
'org-agenda-windows
2782 :type
'(cons (number :tag
"Minimum") (number :tag
"Maximum")))
2784 (defcustom org-agenda-restore-windows-after-quit nil
2785 "Non-nil means, restore window configuration open exiting agenda.
2786 Before the window configuration is changed for displaying the agenda,
2787 the current status is recorded. When the agenda is exited with
2788 `q' or `x' and this option is set, the old state is restored. If
2789 `org-agenda-window-setup' is `other-frame', the value of this
2790 option will be ignored.."
2791 :group
'org-agenda-windows
2794 (defcustom org-indirect-buffer-display
'other-window
2795 "How should indirect tree buffers be displayed?
2796 This applies to indirect buffers created with the commands
2797 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
2799 current-window Display in the current window
2800 other-window Just display in another window.
2801 dedicated-frame Create one new frame, and re-use it each time.
2802 new-frame Make a new frame each time. Note that in this case
2803 previously-made indirect buffers are kept, and you need to
2804 kill these buffers yourself."
2805 :group
'org-structure
2806 :group
'org-agenda-windows
2808 (const :tag
"In current window" current-window
)
2809 (const :tag
"In current frame, other window" other-window
)
2810 (const :tag
"Each time a new frame" new-frame
)
2811 (const :tag
"One dedicated frame" dedicated-frame
)))
2813 (defgroup org-agenda-daily
/weekly nil
2814 "Options concerning the daily/weekly agenda."
2815 :tag
"Org Agenda Daily/Weekly"
2818 (defcustom org-agenda-ndays
7
2819 "Number of days to include in overview display.
2821 :group
'org-agenda-daily
/weekly
2824 (defcustom org-agenda-start-on-weekday
1
2825 "Non-nil means, start the overview always on the specified weekday.
2826 0 denotes Sunday, 1 denotes Monday etc.
2827 When nil, always start on the current day."
2828 :group
'org-agenda-daily
/weekly
2829 :type
'(choice (const :tag
"Today" nil
)
2830 (number :tag
"Weekday No.")))
2832 (defcustom org-agenda-show-all-dates t
2833 "Non-nil means, `org-agenda' shows every day in the selected range.
2834 When nil, only the days which actually have entries are shown."
2835 :group
'org-agenda-daily
/weekly
2838 (defcustom org-agenda-format-date
'org-agenda-format-date-aligned
2839 "Format string for displaying dates in the agenda.
2840 Used by the daily/weekly agenda and by the timeline. This should be
2841 a format string understood by `format-time-string', or a function returning
2842 the formatted date as a string. The function must take a single argument,
2843 a calendar-style date list like (month day year)."
2844 :group
'org-agenda-daily
/weekly
2846 (string :tag
"Format string")
2847 (function :tag
"Function")))
2849 (defun org-agenda-format-date-aligned (date)
2850 "Format a date string for display in the daily/weekly agenda, or timeline.
2851 This function makes sure that dates are aligned for easy reading."
2852 (format "%-9s %2d %s %4d"
2853 (calendar-day-name date
)
2854 (extract-calendar-day date
)
2855 (calendar-month-name (extract-calendar-month date
))
2856 (extract-calendar-year date
)))
2858 (defcustom org-agenda-include-diary nil
2859 "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
2860 :group
'org-agenda-daily
/weekly
2863 (defcustom org-agenda-include-all-todo nil
2864 "Set means weekly/daily agenda will always contain all TODO entries.
2865 The TODO entries will be listed at the top of the agenda, before
2866 the entries for specific days."
2867 :group
'org-agenda-daily
/weekly
2870 (defcustom org-agenda-repeating-timestamp-show-all t
2871 "Non-nil means, show all occurences of a repeating stamp in the agenda.
2872 When nil, only one occurence is shown, either today or the
2873 nearest into the future."
2874 :group
'org-agenda-daily
/weekly
2877 (defcustom org-deadline-warning-days
14
2878 "No. of days before expiration during which a deadline becomes active.
2879 This variable governs the display in sparse trees and in the agenda.
2880 When 0 or negative, it means use this number (the absolute value of it)
2881 even if a deadline has a different individual lead time specified."
2883 :group
'org-agenda-daily
/weekly
2886 (defcustom org-scheduled-past-days
10000
2887 "No. of days to continue listing scheduled items that are not marked DONE.
2888 When an item is scheduled on a date, it shows up in the agenda on this
2889 day and will be listed until it is marked done for the number of days
2891 :group
'org-agenda-daily
/weekly
2894 (defgroup org-agenda-time-grid nil
2895 "Options concerning the time grid in the Org-mode Agenda."
2896 :tag
"Org Agenda Time Grid"
2899 (defcustom org-agenda-use-time-grid t
2900 "Non-nil means, show a time grid in the agenda schedule.
2901 A time grid is a set of lines for specific times (like every two hours between
2902 8:00 and 20:00). The items scheduled for a day at specific times are
2903 sorted in between these lines.
2904 For details about when the grid will be shown, and what it will look like, see
2905 the variable `org-agenda-time-grid'."
2906 :group
'org-agenda-time-grid
2909 (defcustom org-agenda-time-grid
2910 '((daily today require-timed
)
2912 (800 1000 1200 1400 1600 1800 2000))
2914 "The settings for time grid for agenda display.
2915 This is a list of three items. The first item is again a list. It contains
2916 symbols specifying conditions when the grid should be displayed:
2918 daily if the agenda shows a single day
2919 weekly if the agenda shows an entire week
2920 today show grid on current date, independent of daily/weekly display
2921 require-timed show grid only if at least one item has a time specification
2923 The second item is a string which will be places behing the grid time.
2925 The third item is a list of integers, indicating the times that should have
2927 :group
'org-agenda-time-grid
2930 (set :greedy t
:tag
"Grid Display Options"
2931 (const :tag
"Show grid in single day agenda display" daily
)
2932 (const :tag
"Show grid in weekly agenda display" weekly
)
2933 (const :tag
"Always show grid for today" today
)
2934 (const :tag
"Show grid only if any timed entries are present"
2936 (const :tag
"Skip grid times already present in an entry"
2938 (string :tag
"Grid String")
2939 (repeat :tag
"Grid Times" (integer :tag
"Time"))))
2941 (defgroup org-agenda-sorting nil
2942 "Options concerning sorting in the Org-mode Agenda."
2943 :tag
"Org Agenda Sorting"
2946 (defcustom org-agenda-sorting-strategy
2947 '((agenda time-up category-keep priority-down
)
2948 (todo category-keep priority-down
)
2949 (tags category-keep priority-down
)
2950 (search category-keep
))
2951 "Sorting structure for the agenda items of a single day.
2952 This is a list of symbols which will be used in sequence to determine
2953 if an entry should be listed before another entry. The following
2954 symbols are recognized:
2956 time-up Put entries with time-of-day indications first, early first
2957 time-down Put entries with time-of-day indications first, late first
2958 category-keep Keep the default order of categories, corresponding to the
2959 sequence in `org-agenda-files'.
2960 category-up Sort alphabetically by category, A-Z.
2961 category-down Sort alphabetically by category, Z-A.
2962 tag-up Sort alphabetically by last tag, A-Z.
2963 tag-down Sort alphabetically by last tag, Z-A.
2964 priority-up Sort numerically by priority, high priority last.
2965 priority-down Sort numerically by priority, high priority first.
2967 The different possibilities will be tried in sequence, and testing stops
2968 if one comparison returns a \"not-equal\". For example, the default
2969 '(time-up category-keep priority-down)
2970 means: Pull out all entries having a specified time of day and sort them,
2971 in order to make a time schedule for the current day the first thing in the
2972 agenda listing for the day. Of the entries without a time indication, keep
2973 the grouped in categories, don't sort the categories, but keep them in
2974 the sequence given in `org-agenda-files'. Within each category sort by
2977 Leaving out `category-keep' would mean that items will be sorted across
2978 categories by priority.
2980 Instead of a single list, this can also be a set of list for specific
2981 contents, with a context symbol in the car of the list, any of
2982 `agenda', `todo', `tags' for the corresponding agenda views."
2983 :group
'org-agenda-sorting
2985 (repeat :tag
"General" ,org-sorting-choice
)
2986 (list :tag
"Individually"
2987 (cons (const :tag
"Strategy for Weekly/Daily agenda" agenda
)
2988 (repeat ,org-sorting-choice
))
2989 (cons (const :tag
"Strategy for TODO lists" todo
)
2990 (repeat ,org-sorting-choice
))
2991 (cons (const :tag
"Strategy for Tags matches" tags
)
2992 (repeat ,org-sorting-choice
)))))
2994 (defcustom org-sort-agenda-notime-is-late t
2995 "Non-nil means, items without time are considered late.
2996 This is only relevant for sorting. When t, items which have no explicit
2997 time like 15:30 will be considered as 99:01, i.e. later than any items which
2998 do have a time. When nil, the default time is before 0:00. You can use this
2999 option to decide if the schedule for today should come before or after timeless
3001 :group
'org-agenda-sorting
3004 (defgroup org-agenda-line-format nil
3005 "Options concerning the entry prefix in the Org-mode agenda display."
3006 :tag
"Org Agenda Line Format"
3009 (defcustom org-agenda-prefix-format
3010 '((agenda .
" %-12:c%?-12t% s")
3014 (search .
" %-12:c"))
3015 "Format specifications for the prefix of items in the agenda views.
3016 An alist with four entries, for the different agenda types. The keys to the
3017 sublists are `agenda', `timeline', `todo', and `tags'. The values
3019 This format works similar to a printf format, with the following meaning:
3021 %c the category of the item, \"Diary\" for entries from the diary, or
3022 as given by the CATEGORY keyword or derived from the file name.
3023 %T the *last* tag of the item. Last because inherited tags come
3025 %t the time-of-day specification if one applies to the entry, in the
3027 %s Scheduling/Deadline information, a short string
3029 All specifiers work basically like the standard `%s' of printf, but may
3030 contain two additional characters: A question mark just after the `%' and
3031 a whitespace/punctuation character just before the final letter.
3033 If the first character after `%' is a question mark, the entire field
3034 will only be included if the corresponding value applies to the
3035 current entry. This is useful for fields which should have fixed
3036 width when present, but zero width when absent. For example,
3037 \"%?-12t\" will result in a 12 character time field if a time of the
3038 day is specified, but will completely disappear in entries which do
3041 If there is punctuation or whitespace character just before the final
3042 format letter, this character will be appended to the field value if
3043 the value is not empty. For example, the format \"%-12:c\" leads to
3044 \"Diary: \" if the category is \"Diary\". If the category were be
3045 empty, no additional colon would be interted.
3047 The default value of this option is \" %-12:c%?-12t% s\", meaning:
3048 - Indent the line with two space characters
3049 - Give the category in a 12 chars wide field, padded with whitespace on
3050 the right (because of `-'). Append a colon if there is a category
3052 - If there is a time-of-day, put it into a 12 chars wide field. If no
3053 time, don't put in an empty field, just skip it (because of '?').
3054 - Finally, put the scheduling information and append a whitespace.
3056 As another example, if you don't want the time-of-day of entries in
3057 the prefix, you could use:
3059 (setq org-agenda-prefix-format \" %-11:c% s\")
3061 See also the variables `org-agenda-remove-times-when-in-prefix' and
3062 `org-agenda-remove-tags'."
3064 (string :tag
"General format")
3065 (list :greedy t
:tag
"View dependent"
3066 (cons (const agenda
) (string :tag
"Format"))
3067 (cons (const timeline
) (string :tag
"Format"))
3068 (cons (const todo
) (string :tag
"Format"))
3069 (cons (const tags
) (string :tag
"Format"))
3070 (cons (const search
) (string :tag
"Format"))))
3071 :group
'org-agenda-line-format
)
3073 (defvar org-prefix-format-compiled nil
3074 "The compiled version of the most recently used prefix format.
3075 See the variable `org-agenda-prefix-format'.")
3077 (defcustom org-agenda-todo-keyword-format
"%-1s"
3078 "Format for the TODO keyword in agenda lines.
3079 Set this to something like \"%-12s\" if you want all TODO keywords
3080 to occupy a fixed space in the agenda display."
3081 :group
'org-agenda-line-format
3084 (defcustom org-agenda-scheduled-leaders
'("Scheduled: " "Sched.%2dx: ")
3085 "Text preceeding scheduled items in the agenda view.
3086 This is a list with two strings. The first applies when the item is
3087 scheduled on the current day. The second applies when it has been scheduled
3088 previously, it may contain a %d to capture how many days ago the item was
3090 :group
'org-agenda-line-format
3092 (string :tag
"Scheduled today ")
3093 (string :tag
"Scheduled previously")))
3095 (defcustom org-agenda-deadline-leaders
'("Deadline: " "In %3d d.: ")
3096 "Text preceeding deadline items in the agenda view.
3097 This is a list with two strings. The first applies when the item has its
3098 deadline on the current day. The second applies when it is in the past or
3099 in the future, it may contain %d to capture how many days away the deadline
3101 :group
'org-agenda-line-format
3103 (string :tag
"Deadline today ")
3104 (string :tag
"Deadline relative")))
3106 (defcustom org-agenda-remove-times-when-in-prefix t
3107 "Non-nil means, remove duplicate time specifications in agenda items.
3108 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
3109 time-of-day specification in a headline or diary entry is extracted and
3110 placed into the prefix. If this option is non-nil, the original specification
3111 \(a timestamp or -range, or just a plain time(range) specification like
3112 11:30-4pm) will be removed for agenda display. This makes the agenda less
3114 The option can be t or nil. It may also be the symbol `beg', indicating
3115 that the time should only be removed what it is located at the beginning of
3116 the headline/diary entry."
3117 :group
'org-agenda-line-format
3119 (const :tag
"Always" t
)
3120 (const :tag
"Never" nil
)
3121 (const :tag
"When at beginning of entry" beg
)))
3124 (defcustom org-agenda-default-appointment-duration nil
3125 "Default duration for appointments that only have a starting time.
3126 When nil, no duration is specified in such cases.
3127 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
3128 :group
'org-agenda-line-format
3130 (integer :tag
"Minutes")
3131 (const :tag
"No default duration")))
3134 (defcustom org-agenda-remove-tags nil
3135 "Non-nil means, remove the tags from the headline copy in the agenda.
3136 When this is the symbol `prefix', only remove tags when
3137 `org-agenda-prefix-format' contains a `%T' specifier."
3138 :group
'org-agenda-line-format
3140 (const :tag
"Always" t
)
3141 (const :tag
"Never" nil
)
3142 (const :tag
"When prefix format contains %T" prefix
)))
3144 (if (fboundp 'defvaralias
)
3145 (defvaralias 'org-agenda-remove-tags-when-in-prefix
3146 'org-agenda-remove-tags
))
3148 (defcustom org-agenda-tags-column -
80
3149 "Shift tags in agenda items to this column.
3150 If this number is positive, it specifies the column. If it is negative,
3151 it means that the tags should be flushright to that column. For example,
3152 -80 works well for a normal 80 character screen."
3153 :group
'org-agenda-line-format
3156 (if (fboundp 'defvaralias
)
3157 (defvaralias 'org-agenda-align-tags-to-column
'org-agenda-tags-column
))
3159 (defcustom org-agenda-fontify-priorities t
3160 "Non-nil means, highlight low and high priorities in agenda.
3161 When t, the highest priority entries are bold, lowest priority italic.
3162 This may also be an association list of priority faces. The face may be
3163 a names face, or a list like `(:background \"Red\")'."
3164 :group
'org-agenda-line-format
3166 (const :tag
"Never" nil
)
3167 (const :tag
"Defaults" t
)
3168 (repeat :tag
"Specify"
3169 (list (character :tag
"Priority" :value ?A
)
3170 (sexp :tag
"face")))))
3172 (defgroup org-latex nil
3173 "Options for embedding LaTeX code into Org-mode"
3177 (defcustom org-format-latex-options
3178 '(:foreground default
:background default
:scale
1.0
3179 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
3180 :matchers
("begin" "$" "$$" "\\(" "\\["))
3181 "Options for creating images from LaTeX fragments.
3182 This is a property list with the following properties:
3183 :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
3184 `default' means use the forground of the default face.
3185 :background the background color, or \"Transparent\".
3186 `default' means use the background of the default face.
3187 :scale a scaling factor for the size of the images
3188 :html-foreground, :html-background, :html-scale
3189 The same numbers for HTML export.
3190 :matchers a list indicating which matchers should be used to
3191 find LaTeX fragments. Valid members of this list are:
3192 \"begin\" find environments
3193 \"$\" find math expressions surrounded by $...$
3194 \"$$\" find math expressions surrounded by $$....$$
3195 \"\\(\" find math expressions surrounded by \\(...\\)
3196 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3200 (defcustom org-format-latex-header
"\\documentclass{article}
3201 \\usepackage{fullpage} % do not remove
3202 \\usepackage{amssymb}
3203 \\usepackage[usenames]{color}
3204 \\usepackage{amsmath}
3205 \\usepackage{latexsym}
3206 \\usepackage[mathscr]{eucal}
3207 \\pagestyle{empty} % do not remove"
3208 "The document header used for processing LaTeX fragments."
3212 (defgroup org-export nil
3213 "Options for exporting org-listings."
3217 (defgroup org-export-general nil
3218 "General options for exporting Org-mode files."
3219 :tag
"Org Export General"
3223 (defvar org-export-publishing-directory nil
)
3225 (defcustom org-export-with-special-strings t
3226 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
3227 When this option is turned on, these strings will be exported as:
3230 -----+----------+--------
3236 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
3237 :group
'org-export-translation
3240 (defcustom org-export-language-setup
3241 '(("en" "Author" "Date" "Table of Contents")
3242 ("cs" "Autor" "Datum" "Obsah")
3243 ("da" "Ophavsmand" "Dato" "Indhold")
3244 ("de" "Autor" "Datum" "Inhaltsverzeichnis")
3245 ("es" "Autor" "Fecha" "\xcdndice")
3246 ("fr" "Auteur" "Date" "Table des mati\xe8res")
3247 ("it" "Autore" "Data" "Indice")
3248 ("nl" "Auteur" "Datum" "Inhoudsopgave")
3249 ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
3250 ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
3251 "Terms used in export text, translated to different languages.
3252 Use the variable `org-export-default-language' to set the language,
3253 or use the +OPTION lines for a per-file setting."
3254 :group
'org-export-general
3257 (string :tag
"HTML language tag")
3258 (string :tag
"Author")
3259 (string :tag
"Date")
3260 (string :tag
"Table of Contents"))))
3262 (defcustom org-export-default-language
"en"
3263 "The default language of HTML export, as a string.
3264 This should have an association in `org-export-language-setup'."
3265 :group
'org-export-general
3268 (defcustom org-export-skip-text-before-1st-heading t
3269 "Non-nil means, skip all text before the first headline when exporting.
3270 When nil, that text is exported as well."
3271 :group
'org-export-general
3274 (defcustom org-export-headline-levels
3
3275 "The last level which is still exported as a headline.
3276 Inferior levels will produce itemize lists when exported.
3277 Note that a numeric prefix argument to an exporter function overrides
3280 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
3281 :group
'org-export-general
3284 (defcustom org-export-with-section-numbers t
3285 "Non-nil means, add section numbers to headlines when exporting.
3287 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
3288 :group
'org-export-general
3291 (defcustom org-export-with-toc t
3292 "Non-nil means, create a table of contents in exported files.
3293 The TOC contains headlines with levels up to`org-export-headline-levels'.
3294 When an integer, include levels up to N in the toc, this may then be
3295 different from `org-export-headline-levels', but it will not be allowed
3296 to be larger than the number of headline levels.
3297 When nil, no table of contents is made.
3299 Headlines which contain any TODO items will be marked with \"(*)\" in
3300 ASCII export, and with red color in HTML output, if the option
3301 `org-export-mark-todo-in-toc' is set.
3303 In HTML output, the TOC will be clickable.
3305 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
3307 :group
'org-export-general
3309 (const :tag
"No Table of Contents" nil
)
3310 (const :tag
"Full Table of Contents" t
)
3311 (integer :tag
"TOC to level")))
3313 (defcustom org-export-mark-todo-in-toc nil
3314 "Non-nil means, mark TOC lines that contain any open TODO items."
3315 :group
'org-export-general
3318 (defcustom org-export-preserve-breaks nil
3319 "Non-nil means, preserve all line breaks when exporting.
3320 Normally, in HTML output paragraphs will be reformatted. In ASCII
3321 export, line breaks will always be preserved, regardless of this variable.
3323 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
3324 :group
'org-export-general
3327 (defcustom org-export-with-archived-trees
'headline
3328 "Whether subtrees with the ARCHIVE tag should be exported.
3329 This can have three different values
3330 nil Do not export, pretend this tree is not present
3331 t Do export the entire tree
3332 headline Only export the headline, but skip the tree below it."
3333 :group
'org-export-general
3336 (const :tag
"not at all" nil
)
3337 (const :tag
"headline only" 'headline
)
3338 (const :tag
"entirely" t
)))
3340 (defcustom org-export-author-info t
3341 "Non-nil means, insert author name and email into the exported file.
3343 This option can also be set with the +OPTIONS line,
3344 e.g. \"author-info:nil\"."
3345 :group
'org-export-general
3348 (defcustom org-export-time-stamp-file t
3349 "Non-nil means, insert a time stamp into the exported file.
3350 The time stamp shows when the file was created.
3352 This option can also be set with the +OPTIONS line,
3353 e.g. \"timestamp:nil\"."
3354 :group
'org-export-general
3357 (defcustom org-export-with-timestamps t
3358 "If nil, do not export time stamps and associated keywords."
3359 :group
'org-export-general
3362 (defcustom org-export-remove-timestamps-from-toc t
3363 "If nil, remove timestamps from the table of contents entries."
3364 :group
'org-export-general
3367 (defcustom org-export-with-tags
'not-in-toc
3368 "If nil, do not export tags, just remove them from headlines.
3369 If this is the symbol `not-in-toc', tags will be removed from table of
3370 contents entries, but still be shown in the headlines of the document.
3372 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
3373 :group
'org-export-general
3375 (const :tag
"Off" nil
)
3376 (const :tag
"Not in TOC" not-in-toc
)
3377 (const :tag
"On" t
)))
3379 (defcustom org-export-with-drawers nil
3380 "Non-nil means, export with drawers like the property drawer.
3381 When t, all drawers are exported. This may also be a list of
3382 drawer names to export."
3383 :group
'org-export-general
3385 (const :tag
"All drawers" t
)
3386 (const :tag
"None" nil
)
3387 (repeat :tag
"Selected drawers"
3388 (string :tag
"Drawer name"))))
3390 (defgroup org-export-translation nil
3391 "Options for translating special ascii sequences for the export backends."
3392 :tag
"Org Export Translation"
3395 (defcustom org-export-with-emphasize t
3396 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
3397 If the export target supports emphasizing text, the word will be
3398 typeset in bold, italic, or underlined, respectively. Works only for
3399 single words, but you can say: I *really* *mean* *this*.
3400 Not all export backends support this.
3402 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
3403 :group
'org-export-translation
3406 (defcustom org-export-with-footnotes t
3407 "If nil, export [1] as a footnote marker.
3408 Lines starting with [1] will be formatted as footnotes.
3410 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
3411 :group
'org-export-translation
3414 (defcustom org-export-with-sub-superscripts t
3415 "Non-nil means, interpret \"_\" and \"^\" for export.
3416 When this option is turned on, you can use TeX-like syntax for sub- and
3417 superscripts. Several characters after \"_\" or \"^\" will be
3418 considered as a single item - so grouping with {} is normally not
3419 needed. For example, the following things will be parsed as single
3420 sub- or superscripts.
3422 10^24 or 10^tau several digits will be considered 1 item.
3423 10^-12 or 10^-tau a leading sign with digits or a word
3424 x^2-y^3 will be read as x^2 - y^3, because items are
3425 terminated by almost any nonword/nondigit char.
3426 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
3428 Still, ambiguity is possible - so when in doubt use {} to enclose the
3429 sub/superscript. If you set this variable to the symbol `{}',
3430 the braces are *required* in order to trigger interpretations as
3431 sub/superscript. This can be helpful in documents that need \"_\"
3432 frequently in plain text.
3434 Not all export backends support this, but HTML does.
3436 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
3437 :group
'org-export-translation
3439 (const :tag
"Always interpret" t
)
3440 (const :tag
"Only with braces" {})
3441 (const :tag
"Never interpret" nil
)))
3443 (defcustom org-export-with-special-strings t
3444 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
3445 When this option is turned on, these strings will be exported as:
3451 Not all export backends support this, but HTML does.
3453 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
3454 :group
'org-export-translation
3457 (defcustom org-export-with-TeX-macros t
3458 "Non-nil means, interpret simple TeX-like macros when exporting.
3459 For example, HTML export converts \\alpha to α and \\AA to Å.
3460 No only real TeX macros will work here, but the standard HTML entities
3461 for math can be used as macro names as well. For a list of supported
3462 names in HTML export, see the constant `org-html-entities'.
3463 Not all export backends support this.
3465 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
3466 :group
'org-export-translation
3467 :group
'org-export-latex
3470 (defcustom org-export-with-LaTeX-fragments nil
3471 "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
3472 When set, the exporter will find LaTeX environments if the \\begin line is
3473 the first non-white thing on a line. It will also find the math delimiters
3474 like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
3477 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
3478 :group
'org-export-translation
3479 :group
'org-export-latex
3482 (defcustom org-export-with-fixed-width t
3483 "Non-nil means, lines starting with \":\" will be in fixed width font.
3484 This can be used to have pre-formatted text, fragments of code etc. For
3486 : ;; Some Lisp examples
3489 will be looking just like this in also HTML. See also the QUOTE keyword.
3490 Not all export backends support this.
3492 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
3493 :group
'org-export-translation
3496 (defcustom org-match-sexp-depth
3
3497 "Number of stacked braces for sub/superscript matching.
3498 This has to be set before loading org.el to be effective."
3499 :group
'org-export-translation
3502 (defgroup org-export-tables nil
3503 "Options for exporting tables in Org-mode."
3504 :tag
"Org Export Tables"
3507 (defcustom org-export-with-tables t
3508 "If non-nil, lines starting with \"|\" define a table.
3511 | Name | Address | Birthday |
3512 |-------------+----------+-----------|
3513 | Arthur Dent | England | 29.2.2100 |
3515 Not all export backends support this.
3517 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
3518 :group
'org-export-tables
3521 (defcustom org-export-highlight-first-table-line t
3522 "Non-nil means, highlight the first table line.
3523 In HTML export, this means use <th> instead of <td>.
3524 In tables created with table.el, this applies to the first table line.
3525 In Org-mode tables, all lines before the first horizontal separator
3526 line will be formatted with <th> tags."
3527 :group
'org-export-tables
3530 (defcustom org-export-table-remove-special-lines t
3531 "Remove special lines and marking characters in calculating tables.
3532 This removes the special marking character column from tables that are set
3533 up for spreadsheet calculations. It also removes the entire lines
3534 marked with `!', `_', or `^'. The lines with `$' are kept, because
3535 the values of constants may be useful to have."
3536 :group
'org-export-tables
3539 (defcustom org-export-prefer-native-exporter-for-tables nil
3540 "Non-nil means, always export tables created with table.el natively.
3541 Natively means, use the HTML code generator in table.el.
3542 When nil, Org-mode's own HTML generator is used when possible (i.e. if
3543 the table does not use row- or column-spanning). This has the
3544 advantage, that the automatic HTML conversions for math symbols and
3545 sub/superscripts can be applied. Org-mode's HTML generator is also
3547 :group
'org-export-tables
3550 (defgroup org-export-ascii nil
3551 "Options specific for ASCII export of Org-mode files."
3552 :tag
"Org Export ASCII"
3555 (defcustom org-export-ascii-underline
'(?\$ ?\
# ?^ ?\~ ?\
= ?\-
)
3556 "Characters for underlining headings in ASCII export.
3557 In the given sequence, these characters will be used for level 1, 2, ..."
3558 :group
'org-export-ascii
3559 :type
'(repeat character
))
3561 (defcustom org-export-ascii-bullets
'(?
* ?
+ ?-
)
3562 "Bullet characters for headlines converted to lists in ASCII export.
3563 The first character is used for the first lest level generated in this
3564 way, and so on. If there are more levels than characters given here,
3565 the list will be repeated.
3566 Note that plain lists will keep the same bullets as the have in the
3568 :group
'org-export-ascii
3569 :type
'(repeat character
))
3571 (defgroup org-export-xml nil
3572 "Options specific for XML export of Org-mode files."
3573 :tag
"Org Export XML"
3576 (defgroup org-export-html nil
3577 "Options specific for HTML export of Org-mode files."
3578 :tag
"Org Export HTML"
3581 (defcustom org-export-html-coding-system nil
3583 :group
'org-export-html
3584 :type
'coding-system
)
3586 (defcustom org-export-html-extension
"html"
3587 "The extension for exported HTML files."
3588 :group
'org-export-html
3591 (defcustom org-export-html-style
3592 "<style type=\"text/css\">
3594 font-family: Times, serif;
3597 .title { text-align: center; }
3598 .todo { color: red; }
3599 .done { color: green; }
3600 .timestamp { color: grey }
3601 .timestamp-kwd { color: CadetBlue }
3602 .tag { background-color:lightblue; font-weight:normal }
3603 .target { background-color: lavender; }
3605 border: 1pt solid #AEBDCC;
3606 background-color: #F3F5F7;
3608 font-family: courier, monospace;
3610 table { border-collapse: collapse; }
3612 vertical-align: top;
3613 <!--border: 1pt solid #ADB9CC;-->
3616 "The default style specification for exported HTML files.
3617 Since there are different ways of setting style information, this variable
3618 needs to contain the full HTML structure to provide a style, including the
3619 surrounding HTML tags. The style specifications should include definitions
3620 for new classes todo, done, title, and deadline. For example, valid values
3623 <style type=\"text/css\">
3624 p { font-weight: normal; color: gray; }
3625 h1 { color: black; }
3626 .title { text-align: center; }
3627 .todo, .deadline { color: red; }
3628 .done { color: green; }
3631 or, if you want to keep the style in a file,
3633 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
3635 As the value of this option simply gets inserted into the HTML <head> header,
3636 you can \"misuse\" it to add arbitrary text to the header."
3637 :group
'org-export-html
3641 (defcustom org-export-html-title-format
"<h1 class=\"title\">%s</h1>\n"
3642 "Format for typesetting the document title in HTML export."
3643 :group
'org-export-html
3646 (defcustom org-export-html-toplevel-hlevel
2
3647 "The <H> level for level 1 headings in HTML export."
3648 :group
'org-export-html
3651 (defcustom org-export-html-link-org-files-as-html t
3652 "Non-nil means, make file links to `file.org' point to `file.html'.
3653 When org-mode is exporting an org-mode file to HTML, links to
3654 non-html files are directly put into a href tag in HTML.
3655 However, links to other Org-mode files (recognized by the
3656 extension `.org.) should become links to the corresponding html
3657 file, assuming that the linked org-mode file will also be
3659 When nil, the links still point to the plain `.org' file."
3660 :group
'org-export-html
3663 (defcustom org-export-html-inline-images
'maybe
3664 "Non-nil means, inline images into exported HTML pages.
3665 This is done using an <img> tag. When nil, an anchor with href is used to
3666 link to the image. If this option is `maybe', then images in links with
3667 an empty description will be inlined, while images with a description will
3669 :group
'org-export-html
3670 :type
'(choice (const :tag
"Never" nil
)
3671 (const :tag
"Always" t
)
3672 (const :tag
"When there is no description" maybe
)))
3675 (defcustom org-export-html-expand t
3676 "Non-nil means, for HTML export, treat @<...> as HTML tag.
3677 When nil, these tags will be exported as plain text and therefore
3678 not be interpreted by a browser.
3680 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
3681 :group
'org-export-html
3684 (defcustom org-export-html-table-tag
3685 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
3686 "The HTML tag that is used to start a table.
3687 This must be a <table> tag, but you may change the options like
3688 borders and spacing."
3689 :group
'org-export-html
3692 (defcustom org-export-table-header-tags
'("<th>" .
"</th>")
3693 "The opening tag for table header fields.
3694 This is customizable so that alignment options can be specified."
3695 :group
'org-export-tables
3696 :type
'(cons (string :tag
"Opening tag") (string :tag
"Closing tag")))
3698 (defcustom org-export-table-data-tags
'("<td>" .
"</td>")
3699 "The opening tag for table data fields.
3700 This is customizable so that alignment options can be specified."
3701 :group
'org-export-tables
3702 :type
'(cons (string :tag
"Opening tag") (string :tag
"Closing tag")))
3704 (defcustom org-export-html-with-timestamp nil
3705 "If non-nil, write `org-export-html-html-helper-timestamp'
3706 into the exported HTML text. Otherwise, the buffer will just be saved
3708 :group
'org-export-html
3711 (defcustom org-export-html-html-helper-timestamp
3712 "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
3713 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
3714 :group
'org-export-html
3717 (defgroup org-export-icalendar nil
3718 "Options specific for iCalendar export of Org-mode files."
3719 :tag
"Org Export iCalendar"
3722 (defcustom org-combined-agenda-icalendar-file
"~/org.ics"
3723 "The file name for the iCalendar file covering all agenda files.
3724 This file is created with the command \\[org-export-icalendar-all-agenda-files].
3725 The file name should be absolute, the file will be overwritten without warning."
3726 :group
'org-export-icalendar
3729 (defcustom org-icalendar-include-todo nil
3730 "Non-nil means, export to iCalendar files should also cover TODO items."
3731 :group
'org-export-icalendar
3733 (const :tag
"None" nil
)
3734 (const :tag
"Unfinished" t
)
3735 (const :tag
"All" all
)))
3737 (defcustom org-icalendar-include-sexps t
3738 "Non-nil means, export to iCalendar files should also cover sexp entries.
3739 These are entries like in the diary, but directly in an Org-mode file."
3740 :group
'org-export-icalendar
3743 (defcustom org-icalendar-include-body
100
3744 "Amount of text below headline to be included in iCalendar export.
3745 This is a number of characters that should maximally be included.
3746 Properties, scheduling and clocking lines will always be removed.
3747 The text will be inserted into the DESCRIPTION field."
3748 :group
'org-export-icalendar
3750 (const :tag
"Nothing" nil
)
3751 (const :tag
"Everything" t
)
3752 (integer :tag
"Max characters")))
3754 (defcustom org-icalendar-combined-name
"OrgMode"
3755 "Calendar name for the combined iCalendar representing all agenda files."
3756 :group
'org-export-icalendar
3759 (defgroup org-font-lock nil
3760 "Font-lock settings for highlighting in Org-mode."
3761 :tag
"Org Font Lock"
3764 (defcustom org-level-color-stars-only nil
3765 "Non-nil means fontify only the stars in each headline.
3766 When nil, the entire headline is fontified.
3767 Changing it requires restart of `font-lock-mode' to become effective
3768 also in regions already fontified."
3769 :group
'org-font-lock
3772 (defcustom org-hide-leading-stars nil
3773 "Non-nil means, hide the first N-1 stars in a headline.
3774 This works by using the face `org-hide' for these stars. This
3775 face is white for a light background, and black for a dark
3776 background. You may have to customize the face `org-hide' to
3778 Changing it requires restart of `font-lock-mode' to become effective
3779 also in regions already fontified.
3780 You may also set this on a per-file basis by adding one of the following
3781 lines to the buffer:
3783 #+STARTUP: hidestars
3784 #+STARTUP: showstars"
3785 :group
'org-font-lock
3788 (defcustom org-fontify-done-headline nil
3789 "Non-nil means, change the face of a headline if it is marked DONE.
3790 Normally, only the TODO/DONE keyword indicates the state of a headline.
3791 When this is non-nil, the headline after the keyword is set to the
3792 `org-headline-done' as an additional indication."
3793 :group
'org-font-lock
3796 (defcustom org-fontify-emphasized-text t
3797 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3798 Changing this variable requires a restart of Emacs to take effect."
3799 :group
'org-font-lock
3802 (defcustom org-highlight-latex-fragments-and-specials nil
3803 "Non-nil means, fontify what is treated specially by the exporters."
3804 :group
'org-font-lock
3807 (defcustom org-hide-emphasis-markers nil
3808 "Non-nil mean font-lock should hide the emphasis marker characters."
3809 :group
'org-font-lock
3812 (defvar org-emph-re nil
3813 "Regular expression for matching emphasis.")
3814 (defvar org-verbatim-re nil
3815 "Regular expression for matching verbatim text.")
3816 (defvar org-emphasis-regexp-components
) ; defined just below
3817 (defvar org-emphasis-alist
) ; defined just below
3818 (defun org-set-emph-re (var val
)
3819 "Set variable and compute the emphasis regular expression."
3821 (when (and (boundp 'org-emphasis-alist
)
3822 (boundp 'org-emphasis-regexp-components
)
3823 org-emphasis-alist org-emphasis-regexp-components
)
3824 (let* ((e org-emphasis-regexp-components
)
3830 (stacked (and nil
(nth 5 e
))) ; stacked is no longer allowed, forced to nil
3831 (body1 (concat body
"*?"))
3832 (markers (mapconcat 'car org-emphasis-alist
""))
3833 (vmarkers (mapconcat
3834 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3835 org-emphasis-alist
"")))
3836 ;; make sure special characters appear at the right position in the class
3837 (if (string-match "\\^" markers
)
3838 (setq markers
(concat (replace-match "" t t markers
) "^")))
3839 (if (string-match "-" markers
)
3840 (setq markers
(concat (replace-match "" t t markers
) "-")))
3841 (if (string-match "\\^" vmarkers
)
3842 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3843 (if (string-match "-" vmarkers
)
3844 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3846 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3847 (int-to-string nl
) "\\}")))
3850 (concat "\\([" pre
(if (and nil stacked
) markers
) "]\\|^\\)"
3852 "\\([" markers
"]\\)"
3855 "[^" border
(if (and nil stacked
) markers
) "]"
3857 "[^" border
(if (and nil stacked
) markers
) "]"
3860 "\\([" post
(if (and nil stacked
) markers
) "]\\|$\\)"))
3861 (setq org-verbatim-re
3862 (concat "\\([" pre
"]\\|^\\)"
3864 "\\([" vmarkers
"]\\)"
3872 "\\([" post
"]\\|$\\)")))))
3874 (defcustom org-emphasis-regexp-components
3875 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
3876 "Components used to build the regular expression for emphasis.
3877 This is a list with 6 entries. Terminology: In an emphasis string
3878 like \" *strong word* \", we call the initial space PREMATCH, the final
3879 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3880 and \"trong wor\" is the body. The different components in this variable
3881 specify what is allowed/forbidden in each part:
3883 pre Chars allowed as prematch. Beginning of line will be allowed too.
3884 post Chars allowed as postmatch. End of line will be allowed too.
3885 border The chars *forbidden* as border characters.
3886 body-regexp A regexp like \".\" to match a body character. Don't use
3887 non-shy groups here, and don't allow newline here.
3888 newline The maximum number of newlines allowed in an emphasis exp.
3890 Use customize to modify this, or restart Emacs after changing it."
3891 :group
'org-font-lock
3892 :set
'org-set-emph-re
3894 (sexp :tag
"Allowed chars in pre ")
3895 (sexp :tag
"Allowed chars in post ")
3896 (sexp :tag
"Forbidden chars in border ")
3897 (sexp :tag
"Regexp for body ")
3898 (integer :tag
"number of newlines allowed")
3899 (option (boolean :tag
"Stacking (DISABLED) "))))
3901 (defcustom org-emphasis-alist
3902 '(("*" bold
"<b>" "</b>")
3903 ("/" italic
"<i>" "</i>")
3904 ("_" underline
"<u>" "</u>")
3905 ("=" org-code
"<code>" "</code>" verbatim
)
3906 ("~" org-verbatim
"" "" verbatim
)
3907 ("+" (:strike-through t
) "<del>" "</del>")
3909 "Special syntax for emphasized text.
3910 Text starting and ending with a special character will be emphasized, for
3911 example *bold*, _underlined_ and /italic/. This variable sets the marker
3912 characters, the face to be used by font-lock for highlighting in Org-mode
3913 Emacs buffers, and the HTML tags to be used for this.
3914 Use customize to modify this, or restart Emacs after changing it."
3915 :group
'org-font-lock
3916 :set
'org-set-emph-re
3919 (string :tag
"Marker character")
3921 (face :tag
"Font-lock-face")
3922 (plist :tag
"Face property list"))
3923 (string :tag
"HTML start tag")
3924 (string :tag
"HTML end tag")
3925 (option (const verbatim
)))))
3929 (defgroup org-faces nil
3930 "Faces in Org-mode."
3932 :group
'org-font-lock
)
3934 (defun org-compatible-face (inherits specs
)
3935 "Make a compatible face specification.
3936 If INHERITS is an existing face and if the Emacs version supports it,
3937 just inherit the face. If not, use SPECS to define the face.
3938 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
3939 For them we convert a (min-colors 8) entry to a `tty' entry and move it
3940 to the top of the list. The `min-colors' attribute will be removed from
3941 any other entries, and any resulting duplicates will be removed entirely."
3943 ((and inherits
(facep inherits
)
3944 (not (featurep 'xemacs
)) (> emacs-major-version
22))
3945 ;; In Emacs 23, we use inheritance where possible.
3946 ;; We only do this in Emacs 23, because only there the outline
3947 ;; faces have been changed to the original org-mode-level-faces.
3948 (list (list t
:inherit inherits
)))
3949 ((or (featurep 'xemacs
) (< emacs-major-version
22))
3950 ;; These do not understand the `min-colors' attribute.
3952 (while (setq e
(pop specs
))
3954 ((memq (car e
) '(t default
)) (push e r
))
3955 ((setq a
(member '(min-colors 8) (car e
)))
3956 (nconc r
(list (cons (cons '(type tty
) (delq (car a
) (car e
)))
3958 ((setq a
(assq 'min-colors
(car e
)))
3959 (setq e
(cons (delq a
(car e
)) (cdr e
)))
3960 (or (assoc (car e
) r
) (push e r
)))
3961 (t (or (assoc (car e
) r
) (push e r
)))))
3964 (put 'org-compatible-face
'lisp-indent-function
1)
3967 '((((background light
)) (:foreground
"white"))
3968 (((background dark
)) (:foreground
"black")))
3969 "Face used to hide leading stars in headlines.
3970 The forground color of this face should be equal to the background
3971 color of the frame."
3974 (defface org-level-1
;; font-lock-function-name-face
3975 (org-compatible-face 'outline-1
3976 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3977 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3978 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3979 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3980 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
3982 "Face used for level 1 headlines."
3985 (defface org-level-2
;; font-lock-variable-name-face
3986 (org-compatible-face 'outline-2
3987 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
3988 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
3989 (((class color
) (min-colors 8) (background light
)) (:foreground
"yellow"))
3990 (((class color
) (min-colors 8) (background dark
)) (:foreground
"yellow" :bold t
))
3992 "Face used for level 2 headlines."
3995 (defface org-level-3
;; font-lock-keyword-face
3996 (org-compatible-face 'outline-3
3997 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Purple"))
3998 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Cyan1"))
3999 (((class color
) (min-colors 16) (background light
)) (:foreground
"Purple"))
4000 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Cyan"))
4001 (((class color
) (min-colors 8) (background light
)) (:foreground
"purple" :bold t
))
4002 (((class color
) (min-colors 8) (background dark
)) (:foreground
"cyan" :bold t
))
4004 "Face used for level 3 headlines."
4007 (defface org-level-4
;; font-lock-comment-face
4008 (org-compatible-face 'outline-4
4009 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4010 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4011 (((class color
) (min-colors 16) (background light
)) (:foreground
"red"))
4012 (((class color
) (min-colors 16) (background dark
)) (:foreground
"red1"))
4013 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
4014 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4016 "Face used for level 4 headlines."
4019 (defface org-level-5
;; font-lock-type-face
4020 (org-compatible-face 'outline-5
4021 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen"))
4022 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen"))
4023 (((class color
) (min-colors 8)) (:foreground
"green"))))
4024 "Face used for level 5 headlines."
4027 (defface org-level-6
;; font-lock-constant-face
4028 (org-compatible-face 'outline-6
4029 '((((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
4030 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
4031 (((class color
) (min-colors 8)) (:foreground
"magenta"))))
4032 "Face used for level 6 headlines."
4035 (defface org-level-7
;; font-lock-builtin-face
4036 (org-compatible-face 'outline-7
4037 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Orchid"))
4038 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSteelBlue"))
4039 (((class color
) (min-colors 8)) (:foreground
"blue"))))
4040 "Face used for level 7 headlines."
4043 (defface org-level-8
;; font-lock-string-face
4044 (org-compatible-face 'outline-8
4045 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
4046 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
4047 (((class color
) (min-colors 8)) (:foreground
"green"))))
4048 "Face used for level 8 headlines."
4051 (defface org-special-keyword
;; font-lock-string-face
4052 (org-compatible-face nil
4053 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
4054 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
4056 "Face used for special keywords."
4059 (defface org-drawer
;; font-lock-function-name-face
4060 (org-compatible-face nil
4061 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
4062 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
4063 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
4064 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
4065 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
4067 "Face used for drawers."
4070 (defface org-property-value nil
4071 "Face used for the value of a property."
4075 (org-compatible-face nil
4076 '((((class color
) (min-colors 16) (background light
))
4077 (:background
"grey90"))
4078 (((class color
) (min-colors 16) (background dark
))
4079 (:background
"grey30"))
4080 (((class color
) (min-colors 8))
4081 (:background
"cyan" :foreground
"black"))
4082 (t (:inverse-video t
))))
4083 "Face for column display of entry properties."
4086 (when (fboundp 'set-face-attribute
)
4087 ;; Make sure that a fixed-width face is used when we have a column table.
4088 (set-face-attribute 'org-column nil
4089 :height
(face-attribute 'default
:height
)
4090 :family
(face-attribute 'default
:family
)))
4092 (defface org-warning
4093 (org-compatible-face 'font-lock-warning-face
4094 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
4095 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
4096 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
4097 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4099 "Face for deadlines and TODO keywords."
4102 (defface org-archived
; similar to shadow
4103 (org-compatible-face 'shadow
4104 '((((class color grayscale
) (min-colors 88) (background light
))
4105 (:foreground
"grey50"))
4106 (((class color grayscale
) (min-colors 88) (background dark
))
4107 (:foreground
"grey70"))
4108 (((class color
) (min-colors 8) (background light
))
4109 (:foreground
"green"))
4110 (((class color
) (min-colors 8) (background dark
))
4111 (:foreground
"yellow"))))
4112 "Face for headline with the ARCHIVE tag."
4116 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
4117 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
4122 (defface org-ellipsis
4123 '((((class color
) (background light
)) (:foreground
"DarkGoldenrod" :underline t
))
4124 (((class color
) (background dark
)) (:foreground
"LightGoldenrod" :underline t
))
4125 (t (:strike-through t
)))
4126 "Face for the ellipsis in folded text."
4130 '((((class color
) (background light
)) (:underline t
))
4131 (((class color
) (background dark
)) (:underline t
))
4137 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
4138 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
4143 (defface org-sexp-date
4144 '((((class color
) (background light
)) (:foreground
"Purple"))
4145 (((class color
) (background dark
)) (:foreground
"Cyan"))
4155 (defface org-todo
; font-lock-warning-face
4156 (org-compatible-face nil
4157 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
4158 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
4159 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
4160 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4161 (t (:inverse-video t
:bold t
))))
4162 "Face for TODO keywords."
4165 (defface org-done
;; font-lock-type-face
4166 (org-compatible-face nil
4167 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen" :bold t
))
4168 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen" :bold t
))
4169 (((class color
) (min-colors 8)) (:foreground
"green"))
4171 "Face used for todo keywords that indicate DONE items."
4174 (defface org-headline-done
;; font-lock-string-face
4175 (org-compatible-face nil
4176 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
4177 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
4178 (((class color
) (min-colors 8) (background light
)) (:bold nil
))))
4179 "Face used to indicate that a headline is DONE.
4180 This face is only used if `org-fontify-done-headline' is set. If applies
4181 to the part of the headline after the DONE keyword."
4184 (defcustom org-todo-keyword-faces nil
4185 "Faces for specific TODO keywords.
4186 This is a list of cons cells, with TODO keywords in the car
4187 and faces in the cdr. The face can be a symbol, or a property
4188 list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
4193 (string :tag
"keyword")
4194 (sexp :tag
"face"))))
4196 (defface org-table
;; font-lock-function-name-face
4197 (org-compatible-face nil
4198 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
4199 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
4200 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
4201 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
4202 (((class color
) (min-colors 8) (background light
)) (:foreground
"blue"))
4203 (((class color
) (min-colors 8) (background dark
)))))
4204 "Face used for tables."
4207 (defface org-formula
4208 (org-compatible-face nil
4209 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4210 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4211 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
4212 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red"))
4213 (t (:bold t
:italic t
))))
4214 "Face for formulas."
4218 (org-compatible-face nil
4219 '((((class color grayscale
) (min-colors 88) (background light
))
4220 (:foreground
"grey50"))
4221 (((class color grayscale
) (min-colors 88) (background dark
))
4222 (:foreground
"grey70"))
4223 (((class color
) (min-colors 8) (background light
))
4224 (:foreground
"green"))
4225 (((class color
) (min-colors 8) (background dark
))
4226 (:foreground
"yellow"))))
4227 "Face for fixed-with text like code snippets."
4231 (defface org-verbatim
4232 (org-compatible-face nil
4233 '((((class color grayscale
) (min-colors 88) (background light
))
4234 (:foreground
"grey50" :underline t
))
4235 (((class color grayscale
) (min-colors 88) (background dark
))
4236 (:foreground
"grey70" :underline t
))
4237 (((class color
) (min-colors 8) (background light
))
4238 (:foreground
"green" :underline t
))
4239 (((class color
) (min-colors 8) (background dark
))
4240 (:foreground
"yellow" :underline t
))))
4241 "Face for fixed-with text like code snippets."
4245 (defface org-agenda-structure
;; font-lock-function-name-face
4246 (org-compatible-face nil
4247 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
4248 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
4249 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
4250 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
4251 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
4253 "Face used in agenda for captions and dates."
4256 (defface org-scheduled-today
4257 (org-compatible-face nil
4258 '((((class color
) (min-colors 88) (background light
)) (:foreground
"DarkGreen"))
4259 (((class color
) (min-colors 88) (background dark
)) (:foreground
"PaleGreen"))
4260 (((class color
) (min-colors 8)) (:foreground
"green"))
4261 (t (:bold t
:italic t
))))
4262 "Face for items scheduled for a certain day."
4265 (defface org-scheduled-previously
4266 (org-compatible-face nil
4267 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4268 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4269 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
4270 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4272 "Face for items scheduled previously, and not yet done."
4275 (defface org-upcoming-deadline
4276 (org-compatible-face nil
4277 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4278 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4279 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
4280 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4282 "Face for items scheduled previously, and not yet done."
4285 (defcustom org-agenda-deadline-faces
4286 '((1.0 . org-warning
)
4287 (0.5 . org-upcoming-deadline
)
4289 "Faces for showing deadlines in the agenda.
4290 This is a list of cons cells. The cdr of each cell is a face to be used,
4291 and it can also just be like '(:foreground \"yellow\").
4292 Each car is a fraction of the head-warning time that must have passed for
4293 this the face in the cdr to be used for display. The numbers must be
4294 given in descending order. The head-warning time is normally taken
4295 from `org-deadline-warning-days', but can also be specified in the deadline
4296 timestamp itself, like this:
4298 DEADLINE: <2007-08-13 Mon -8d>
4300 You may use d for days, w for weeks, m for months and y for years. Months
4301 and years will only be treated in an approximate fashion (30.4 days for a
4302 month and 365.24 days for a year)."
4304 :group
'org-agenda-daily
/weekly
4307 (number :tag
"Fraction of head-warning time passed")
4308 (sexp :tag
"Face"))))
4310 ;; FIXME: this is not a good face yet.
4311 (defface org-agenda-restriction-lock
4312 (org-compatible-face nil
4313 '((((class color
) (min-colors 88) (background light
)) (:background
"yellow1"))
4314 (((class color
) (min-colors 88) (background dark
)) (:background
"skyblue4"))
4315 (((class color
) (min-colors 16) (background light
)) (:background
"yellow1"))
4316 (((class color
) (min-colors 16) (background dark
)) (:background
"skyblue4"))
4317 (((class color
) (min-colors 8)) (:background
"cyan" :foreground
"black"))
4318 (t (:inverse-video t
))))
4319 "Face for showing the agenda restriction lock."
4322 (defface org-time-grid
;; font-lock-variable-name-face
4323 (org-compatible-face nil
4324 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
4325 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
4326 (((class color
) (min-colors 8)) (:foreground
"yellow" :weight light
))))
4327 "Face used for time grids."
4330 (defconst org-level-faces
4331 '(org-level-1 org-level-2 org-level-3 org-level-4
4332 org-level-5 org-level-6 org-level-7 org-level-8
4335 (defcustom org-n-level-faces
(length org-level-faces
)
4336 "The number of different faces to be used for headlines.
4337 Org-mode defines 8 different headline faces, so this can be at most 8.
4338 If it is less than 8, the level-1 face gets re-used for level N+1 etc."
4342 ;;; Functions and variables from ther packages
4343 ;; Declared here to avoid compiler warnings
4346 (unless (fboundp 'declare-function
)
4347 (defmacro declare-function
(fn file
&optional arglist fileonly
))))
4350 (defvar outline-mode-menu-heading
)
4351 (defvar outline-mode-menu-show
)
4352 (defvar outline-mode-menu-hide
)
4353 (defvar zmacs-regions
) ; XEmacs regions
4356 (defvar mark-active
)
4359 ;; FIXME: get the argument lists for the UNKNOWN stuff
4360 (declare-function add-to-diary-list
"diary-lib"
4361 (date string specifier
&optional marker globcolor literal
))
4362 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
4363 (declare-function Info-find-node
"info" (filename nodename
&optional no-going-back
))
4364 (declare-function bbdb
"ext:bbdb-com" (string elidep
))
4365 (declare-function bbdb-company
"ext:bbdb-com" (string elidep
))
4366 (declare-function bbdb-current-record
"ext:bbdb-com" (&optional planning-on-modifying
))
4367 (declare-function bbdb-name
"ext:bbdb-com" (string elidep
))
4368 (declare-function bbdb-record-getprop
"ext:bbdb" (record property
))
4369 (declare-function bbdb-record-name
"ext:bbdb" (record))
4370 (declare-function bibtex-beginning-of-entry
"bibtex" ())
4371 (declare-function bibtex-generate-autokey
"bibtex" ())
4372 (declare-function bibtex-parse-entry
"bibtex" (&optional content
))
4373 (declare-function bibtex-url
"bibtex" (&optional pos no-browse
))
4374 (defvar calc-embedded-close-formula
)
4375 (defvar calc-embedded-open-formula
)
4376 (declare-function calendar-astro-date-string
"cal-julian" (&optional date
))
4377 (declare-function calendar-bahai-date-string
"cal-bahai" (&optional date
))
4378 (declare-function calendar-check-holidays
"holidays" (date))
4379 (declare-function calendar-chinese-date-string
"cal-china" (&optional date
))
4380 (declare-function calendar-coptic-date-string
"cal-coptic" (&optional date
))
4381 (declare-function calendar-ethiopic-date-string
"cal-coptic" (&optional date
))
4382 (declare-function calendar-forward-day
"cal-move" (arg))
4383 (declare-function calendar-french-date-string
"cal-french" (&optional date
))
4384 (declare-function calendar-goto-date
"cal-move" (date))
4385 (declare-function calendar-goto-today
"cal-move" ())
4386 (declare-function calendar-hebrew-date-string
"cal-hebrew" (&optional date
))
4387 (declare-function calendar-islamic-date-string
"cal-islam" (&optional date
))
4388 (declare-function calendar-iso-date-string
"cal-iso" (&optional date
))
4389 (declare-function calendar-julian-date-string
"cal-julian" (&optional date
))
4390 (declare-function calendar-mayan-date-string
"cal-mayan" (&optional date
))
4391 (declare-function calendar-persian-date-string
"cal-persia" (&optional date
))
4392 (defvar calendar-mode-map
)
4393 (defvar original-date
) ; dynamically scoped in calendar.el does scope this
4394 (declare-function cdlatex-tab
"ext:cdlatex" ())
4395 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
4396 (declare-function elmo-folder-exists-p
"ext:elmo" (folder) t
)
4397 (declare-function elmo-message-entity-field
"ext:elmo-msgdb" (entity field
&optional type
))
4398 (declare-function elmo-message-field
"ext:elmo" (folder number field
&optional type
) t
)
4399 (declare-function elmo-msgdb-overview-get-entity
"ext:elmo" (&rest unknown
) t
)
4400 (defvar font-lock-unfontify-region-function
)
4401 (declare-function gnus-article-show-summary
"gnus-art" ())
4402 (declare-function gnus-summary-last-subject
"gnus-sum" ())
4403 (defvar gnus-other-frame-object
)
4404 (defvar gnus-group-name
)
4405 (defvar gnus-article-current
)
4406 (defvar Info-current-file
)
4407 (defvar Info-current-node
)
4408 (declare-function mh-display-msg
"mh-show" (msg-num folder-name
))
4409 (declare-function mh-find-path
"mh-utils" ())
4410 (declare-function mh-get-header-field
"mh-utils" (field))
4411 (declare-function mh-get-msg-num
"mh-utils" (error-if-no-message))
4412 (declare-function mh-header-display
"mh-show" ())
4413 (declare-function mh-index-previous-folder
"mh-search" ())
4414 (declare-function mh-normalize-folder-name
"mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty
))
4415 (declare-function mh-search
"mh-search" (folder search-regexp
&optional redo-search-flag window-config
))
4416 (declare-function mh-search-choose
"mh-search" (&optional searcher
))
4417 (declare-function mh-show
"mh-show" (&optional message redisplay-flag
))
4418 (declare-function mh-show-buffer-message-number
"mh-comp" (&optional buffer
))
4419 (declare-function mh-show-header-display
"mh-show" t t
)
4420 (declare-function mh-show-msg
"mh-show" (msg))
4421 (declare-function mh-show-show
"mh-show" t t
)
4422 (declare-function mh-visit-folder
"mh-folder" (folder &optional range index-data
))
4424 (defvar mh-current-folder
)
4425 (defvar mh-show-folder-buffer
)
4426 (defvar mh-index-folder
)
4427 (defvar mh-searcher
)
4428 (declare-function org-export-latex-cleaned-string
"org-export-latex" ())
4429 (declare-function parse-time-string
"parse-time" (string))
4430 (declare-function remember
"remember" (&optional initial
))
4431 (declare-function remember-buffer-desc
"remember" ())
4432 (declare-function remember-finalize
"remember" ())
4433 (defvar remember-save-after-remembering
)
4434 (defvar remember-data-file
)
4435 (defvar remember-register
)
4436 (defvar remember-buffer
)
4437 (defvar remember-handler-functions
)
4438 (defvar remember-annotation-functions
)
4439 (declare-function rmail-narrow-to-non-pruned-header
"rmail" ())
4440 (declare-function rmail-show-message
"rmail" (&optional n no-summary
))
4441 (declare-function rmail-what-message
"rmail" ())
4442 (defvar rmail-current-message
)
4443 (defvar texmathp-why
)
4444 (declare-function vm-beginning-of-message
"ext:vm-page" ())
4445 (declare-function vm-follow-summary-cursor
"ext:vm-motion" ())
4446 (declare-function vm-get-header-contents
"ext:vm-summary" (message header-name-regexp
&optional clump-sep
))
4447 (declare-function vm-isearch-narrow
"ext:vm-search" ())
4448 (declare-function vm-isearch-update
"ext:vm-search" ())
4449 (declare-function vm-select-folder-buffer
"ext:vm-macro" ())
4450 (declare-function vm-su-message-id
"ext:vm-summary" (m))
4451 (declare-function vm-su-subject
"ext:vm-summary" (m))
4452 (declare-function vm-summarize
"ext:vm-summary" (&optional display raise
))
4453 (defvar vm-message-pointer
)
4454 (defvar vm-folder-directory
)
4455 (defvar w3m-current-url
)
4456 (defvar w3m-current-title
)
4457 ;; backward compatibility to old version of wl
4458 (declare-function wl-summary-buffer-msgdb
"ext:wl-folder" (&rest unknown
) t
)
4459 (declare-function wl-folder-get-elmo-folder
"ext:wl-folder" (entity &optional no-cache
))
4460 (declare-function wl-summary-goto-folder-subr
"ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit
))
4461 (declare-function wl-summary-jump-to-msg-by-message-id
"ext:wl-summary" (&optional id
))
4462 (declare-function wl-summary-line-from
"ext:wl-summary" ())
4463 (declare-function wl-summary-line-subject
"ext:wl-summary" ())
4464 (declare-function wl-summary-message-number
"ext:wl-summary" ())
4465 (declare-function wl-summary-redisplay
"ext:wl-summary" (&optional arg
))
4466 (defvar wl-summary-buffer-elmo-folder
)
4467 (defvar wl-summary-buffer-folder-name
)
4468 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
4470 (defvar org-latex-regexps
)
4471 (defvar constants-unit-system
)
4473 ;;; Variables for pre-computed regular expressions, all buffer local
4475 (defvar org-drawer-regexp nil
4476 "Matches first line of a hidden block.")
4477 (make-variable-buffer-local 'org-drawer-regexp
)
4478 (defvar org-todo-regexp nil
4479 "Matches any of the TODO state keywords.")
4480 (make-variable-buffer-local 'org-todo-regexp
)
4481 (defvar org-not-done-regexp nil
4482 "Matches any of the TODO state keywords except the last one.")
4483 (make-variable-buffer-local 'org-not-done-regexp
)
4484 (defvar org-todo-line-regexp nil
4485 "Matches a headline and puts TODO state into group 2 if present.")
4486 (make-variable-buffer-local 'org-todo-line-regexp
)
4487 (defvar org-complex-heading-regexp nil
4488 "Matches a headline and puts everything into groups:
4490 group 2: The todo keyword, maybe
4491 group 3: Priority cookie
4492 group 4: True headline
4494 (make-variable-buffer-local 'org-complex-heading-regexp
)
4495 (defvar org-todo-line-tags-regexp nil
4496 "Matches a headline and puts TODO state into group 2 if present.
4497 Also put tags into group 4 if tags are present.")
4498 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4499 (defvar org-nl-done-regexp nil
4500 "Matches newline followed by a headline with the DONE keyword.")
4501 (make-variable-buffer-local 'org-nl-done-regexp
)
4502 (defvar org-looking-at-done-regexp nil
4503 "Matches the DONE keyword a point.")
4504 (make-variable-buffer-local 'org-looking-at-done-regexp
)
4505 (defvar org-ds-keyword-length
12
4506 "Maximum length of the Deadline and SCHEDULED keywords.")
4507 (make-variable-buffer-local 'org-ds-keyword-length
)
4508 (defvar org-deadline-regexp nil
4509 "Matches the DEADLINE keyword.")
4510 (make-variable-buffer-local 'org-deadline-regexp
)
4511 (defvar org-deadline-time-regexp nil
4512 "Matches the DEADLINE keyword together with a time stamp.")
4513 (make-variable-buffer-local 'org-deadline-time-regexp
)
4514 (defvar org-deadline-line-regexp nil
4515 "Matches the DEADLINE keyword and the rest of the line.")
4516 (make-variable-buffer-local 'org-deadline-line-regexp
)
4517 (defvar org-scheduled-regexp nil
4518 "Matches the SCHEDULED keyword.")
4519 (make-variable-buffer-local 'org-scheduled-regexp
)
4520 (defvar org-scheduled-time-regexp nil
4521 "Matches the SCHEDULED keyword together with a time stamp.")
4522 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4523 (defvar org-closed-time-regexp nil
4524 "Matches the CLOSED keyword together with a time stamp.")
4525 (make-variable-buffer-local 'org-closed-time-regexp
)
4527 (defvar org-keyword-time-regexp nil
4528 "Matches any of the 4 keywords, together with the time stamp.")
4529 (make-variable-buffer-local 'org-keyword-time-regexp
)
4530 (defvar org-keyword-time-not-clock-regexp nil
4531 "Matches any of the 3 keywords, together with the time stamp.")
4532 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4533 (defvar org-maybe-keyword-time-regexp nil
4534 "Matches a timestamp, possibly preceeded by a keyword.")
4535 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4536 (defvar org-planning-or-clock-line-re nil
4537 "Matches a line with planning or clock info.")
4538 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
4540 (defconst org-rm-props
'(invisible t face t keymap t intangible t mouse-face t
4541 rear-nonsticky t mouse-map t fontified t
)
4542 "Properties to remove when a string without properties is wanted.")
4544 (defsubst org-match-string-no-properties
(num &optional string
)
4545 (if (featurep 'xemacs
)
4546 (let ((s (match-string num string
)))
4547 (remove-text-properties 0 (length s
) org-rm-props s
)
4549 (match-string-no-properties num string
)))
4551 (defsubst org-no-properties
(s)
4552 (if (fboundp 'set-text-properties
)
4553 (set-text-properties 0 (length s
) nil s
)
4554 (remove-text-properties 0 (length s
) org-rm-props s
))
4557 (defsubst org-get-alist-option
(option key
)
4558 (cond ((eq key t
) t
)
4560 ((assoc key option
) (cdr (assoc key option
)))
4561 (t (cdr (assq 'default option
)))))
4563 (defsubst org-inhibit-invisibility
()
4564 "Modified `buffer-invisibility-spec' for Emacs 21.
4565 Some ops with invisible text do not work correctly on Emacs 21. For these
4566 we turn off invisibility temporarily. Use this in a `let' form."
4567 (if (< emacs-major-version
22) nil buffer-invisibility-spec
))
4569 (defsubst org-set-local
(var value
)
4570 "Make VAR local in current buffer and set it to VALUE."
4571 (set (make-variable-buffer-local var
) value
))
4573 (defsubst org-mode-p
()
4574 "Check if the current buffer is in Org-mode."
4575 (eq major-mode
'org-mode
))
4577 (defsubst org-last
(list)
4578 "Return the last element of LIST."
4581 (defun org-let (list &rest body
)
4582 (eval (cons 'let
(cons list body
))))
4583 (put 'org-let
'lisp-indent-function
1)
4585 (defun org-let2 (list1 list2
&rest body
)
4586 (eval (cons 'let
(cons list1
(list (cons 'let
(cons list2 body
)))))))
4587 (put 'org-let2
'lisp-indent-function
2)
4588 (defconst org-startup-options
4589 '(("fold" org-startup-folded t
)
4590 ("overview" org-startup-folded t
)
4591 ("nofold" org-startup-folded nil
)
4592 ("showall" org-startup-folded nil
)
4593 ("content" org-startup-folded content
)
4594 ("hidestars" org-hide-leading-stars t
)
4595 ("showstars" org-hide-leading-stars nil
)
4596 ("odd" org-odd-levels-only t
)
4597 ("oddeven" org-odd-levels-only nil
)
4598 ("align" org-startup-align-all-tables t
)
4599 ("noalign" org-startup-align-all-tables nil
)
4600 ("customtime" org-display-custom-times t
)
4601 ("logdone" org-log-done time
)
4602 ("lognotedone" org-log-done note
)
4603 ("nologdone" org-log-done nil
)
4604 ("lognoteclock-out" org-log-note-clock-out t
)
4605 ("nolognoteclock-out" org-log-note-clock-out nil
)
4606 ("logrepeat" org-log-repeat state
)
4607 ("lognoterepeat" org-log-repeat note
)
4608 ("nologrepeat" org-log-repeat nil
)
4609 ("constcgs" constants-unit-system cgs
)
4610 ("constSI" constants-unit-system SI
))
4611 "Variable associated with STARTUP options for org-mode.
4612 Each element is a list of three items: The startup options as written
4613 in the #+STARTUP line, the corresponding variable, and the value to
4614 set this variable to if the option is found. An optional forth element PUSH
4615 means to push this value onto the list in the variable.")
4617 (defun org-set-regexps-and-options ()
4618 "Precompute regular expressions for current buffer."
4620 (org-set-local 'org-todo-kwd-alist nil
)
4621 (org-set-local 'org-todo-key-alist nil
)
4622 (org-set-local 'org-todo-key-trigger nil
)
4623 (org-set-local 'org-todo-keywords-1 nil
)
4624 (org-set-local 'org-done-keywords nil
)
4625 (org-set-local 'org-todo-heads nil
)
4626 (org-set-local 'org-todo-sets nil
)
4627 (org-set-local 'org-todo-log-states nil
)
4628 (let ((re (org-make-options-regexp
4629 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
4630 "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
4631 "CONSTANTS" "PROPERTY" "DRAWERS")))
4633 kwds kws0 kwsa key log value cat arch tags const links hw dws
4634 tail sep kws1 prio props drawers
)
4638 (goto-char (point-min))
4639 (while (re-search-forward re nil t
)
4640 (setq key
(match-string 1) value
(org-match-string-no-properties 2))
4642 ((equal key
"CATEGORY")
4643 (if (string-match "[ \t]+$" value
)
4644 (setq value
(replace-match "" t t value
)))
4646 ((member key
'("SEQ_TODO" "TODO"))
4647 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4648 ((equal key
"TYP_TODO")
4649 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4651 (setq tags
(append tags
(org-split-string value splitre
))))
4652 ((equal key
"COLUMNS")
4653 (org-set-local 'org-columns-default-format value
))
4655 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4656 (push (cons (match-string 1 value
)
4657 (org-trim (match-string 2 value
)))
4659 ((equal key
"PRIORITIES")
4660 (setq prio
(org-split-string value
" +")))
4661 ((equal key
"PROPERTY")
4662 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4663 (push (cons (match-string 1 value
) (match-string 2 value
))
4665 ((equal key
"DRAWERS")
4666 (setq drawers
(org-split-string value splitre
)))
4667 ((equal key
"CONSTANTS")
4668 (setq const
(append const
(org-split-string value splitre
))))
4669 ((equal key
"STARTUP")
4670 (let ((opts (org-split-string value splitre
))
4672 (while (setq l
(pop opts
))
4673 (when (setq l
(assoc l org-startup-options
))
4674 (setq var
(nth 1 l
) val
(nth 2 l
))
4676 (set (make-local-variable var
) val
)
4677 (if (not (listp (symbol-value var
)))
4678 (set (make-local-variable var
) nil
))
4679 (set (make-local-variable var
) (symbol-value var
))
4680 (add-to-list var val
))))))
4681 ((equal key
"ARCHIVE")
4682 (string-match " *$" value
)
4683 (setq arch
(replace-match "" t t value
))
4684 (remove-text-properties 0 (length arch
)
4685 '(face t fontified t
) arch
)))
4688 (org-set-local 'org-category
(intern cat
))
4689 (push (cons "CATEGORY" cat
) props
))
4691 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4692 (setq prio
(mapcar 'string-to-char prio
))
4693 (org-set-local 'org-highest-priority
(nth 0 prio
))
4694 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4695 (org-set-local 'org-default-priority
(nth 2 prio
)))
4696 (and props
(org-set-local 'org-local-properties
(nreverse props
)))
4697 (and drawers
(org-set-local 'org-drawers drawers
))
4698 (and arch
(org-set-local 'org-archive-location arch
))
4699 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4700 ;; Process the TODO keywords
4702 ;; Use the global values as if they had been given locally.
4703 (setq kwds
(default-value 'org-todo-keywords
))
4704 (if (stringp (car kwds
))
4705 (setq kwds
(list (cons org-todo-interpretation
4706 (default-value 'org-todo-keywords
)))))
4707 (setq kwds
(reverse kwds
)))
4708 (setq kwds
(nreverse kwds
))
4710 (while (setq kws
(pop kwds
))
4711 (setq inter
(pop kws
) sep
(member "|" kws
)
4712 kws0
(delete "|" (copy-sequence kws
))
4717 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4719 (setq kw
(match-string 1 x
)
4720 key
(and (match-end 2) (match-string 2 x
))
4721 log
(org-extract-log-state-settings x
))
4722 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4723 (and log
(push log org-todo-log-states
))
4725 (error "Invalid TODO keyword %s" x
)))
4727 kwsa
(if kwsa
(append '((:startgroup
))
4731 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4732 tail
(list inter hw
(car dws
) (org-last dws
)))
4733 (add-to-list 'org-todo-heads hw
'append
)
4734 (push kws1 org-todo-sets
)
4735 (setq org-done-keywords
(append org-done-keywords dws nil
))
4736 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4737 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4738 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4739 (setq org-todo-sets
(nreverse org-todo-sets
)
4740 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4741 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4742 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4743 ;; Process the constants
4746 (while (setq e
(pop const
))
4747 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4748 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4749 (setq org-table-formula-constants-local cst
)))
4751 ;; Process the tags.
4754 (while (setq e
(pop tags
))
4756 ((equal e
"{") (push '(:startgroup
) tgs
))
4757 ((equal e
"}") (push '(:endgroup
) tgs
))
4758 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
4759 (push (cons (match-string 1 e
)
4760 (string-to-char (match-string 2 e
)))
4762 (t (push (list e
) tgs
))))
4763 (org-set-local 'org-tag-alist nil
)
4764 (while (setq e
(pop tgs
))
4765 (or (and (stringp (car e
))
4766 (assoc (car e
) org-tag-alist
))
4767 (push e org-tag-alist
))))))
4769 ;; Compute the regular expressions and other local variables
4770 (if (not org-done-keywords
)
4771 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
4772 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4773 (length org-scheduled-string
)))
4775 (concat "^[ \t]*:\\("
4776 (mapconcat 'regexp-quote org-drawers
"\\|")
4778 org-not-done-keywords
4779 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4781 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
4785 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4787 org-todo-line-regexp
4788 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4789 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4790 "\\)\\>\\)?[ \t]*\\(.*\\)")
4791 org-complex-heading-regexp
4792 (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
4793 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4794 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
4795 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4797 (concat "\n\\*+[ \t]+"
4798 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
4800 org-todo-line-tags-regexp
4801 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4802 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4804 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
4805 org-looking-at-done-regexp
4807 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
4809 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4810 org-deadline-time-regexp
4811 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4812 org-deadline-line-regexp
4813 (concat "\\<\\(" org-deadline-string
"\\).*")
4814 org-scheduled-regexp
4815 (concat "\\<" org-scheduled-string
)
4816 org-scheduled-time-regexp
4817 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4818 org-closed-time-regexp
4819 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4820 org-keyword-time-regexp
4821 (concat "\\<\\(" org-scheduled-string
4822 "\\|" org-deadline-string
4823 "\\|" org-closed-string
4824 "\\|" org-clock-string
"\\)"
4825 " *[[<]\\([^]>]+\\)[]>]")
4826 org-keyword-time-not-clock-regexp
4827 (concat "\\<\\(" org-scheduled-string
4828 "\\|" org-deadline-string
4829 "\\|" org-closed-string
4831 " *[[<]\\([^]>]+\\)[]>]")
4832 org-maybe-keyword-time-regexp
4833 (concat "\\(\\<\\(" org-scheduled-string
4834 "\\|" org-deadline-string
4835 "\\|" org-closed-string
4836 "\\|" org-clock-string
"\\)\\)?"
4837 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4838 org-planning-or-clock-line-re
4839 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4840 "\\|" org-deadline-string
4841 "\\|" org-closed-string
"\\|" org-clock-string
4844 (org-compute-latex-and-specials-regexp)
4845 (org-set-font-lock-defaults)))
4847 (defun org-extract-log-state-settings (x)
4848 "Extract the log state setting from a TODO keyword string.
4849 This will extract info from a string like \"WAIT(w@/!)\"."
4850 (let (kw key log1 log2
)
4851 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4852 (setq kw
(match-string 1 x
)
4853 key
(and (match-end 2) (match-string 2 x
))
4854 log1
(and (match-end 3) (match-string 3 x
))
4855 log2
(and (match-end 4) (match-string 4 x
)))
4858 (and log1
(if (equal log1
"!") 'time
'note
))
4859 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4861 (defun org-remove-keyword-keys (list)
4862 "Remove a pair of parenthesis at the end of each string in LIST."
4864 (if (string-match "(.*)$" x
)
4865 (substring x
0 (match-beginning 0))
4869 ;; FIXME: this could be done much better, using second characters etc.
4870 (defun org-assign-fast-keys (alist)
4871 "Assign fast keys to a keyword-key alist.
4872 Respect keys that are already there."
4873 (let (new e k c c1 c2
(char ?a
))
4874 (while (setq e
(pop alist
))
4876 ((equal e
'(:startgroup
)) (push e new
))
4877 ((equal e
'(:endgroup
)) (push e new
))
4879 (setq k
(car e
) c2 nil
)
4882 ;; automatically assign a character.
4883 (setq c1
(string-to-char
4884 (downcase (substring
4885 k
(if (= (string-to-char k
) ?
@) 1 0)))))
4886 (if (or (rassoc c1 new
) (rassoc c1 alist
))
4887 (while (or (rassoc char new
) (rassoc char alist
))
4888 (setq char
(1+ char
)))
4890 (setq c
(or c2 char
)))
4891 (push (cons k c
) new
))))
4894 ;;; Some variables ujsed in various places
4896 (defvar org-window-configuration nil
4897 "Used in various places to store a window configuration.")
4898 (defvar org-finish-function nil
4899 "Function to be called when `C-c C-c' is used.
4900 This is for getting out of special buffers like remember.")
4903 ;; FIXME: Occasionally check by commenting these, to make sure
4904 ;; no other functions uses these, forgetting to let-bind them.
4909 (defvar description
)
4911 ;; Defined somewhere in this file, but used before definition.
4912 (defvar orgtbl-mode-menu
) ; defined when orgtbl mode get initialized
4913 (defvar org-agenda-buffer-name
)
4914 (defvar org-agenda-undo-list
)
4915 (defvar org-agenda-pending-undo-list
)
4916 (defvar org-agenda-overriding-header
)
4917 (defvar orgtbl-mode
)
4918 (defvar org-html-entities
)
4919 (defvar org-struct-menu
)
4920 (defvar org-org-menu
)
4921 (defvar org-tbl-menu
)
4922 (defvar org-agenda-keymap
)
4924 ;;;; Emacs/XEmacs compatibility
4926 ;; Overlay compatibility functions
4927 (defun org-make-overlay (beg end
&optional buffer
)
4928 (if (featurep 'xemacs
)
4929 (make-extent beg end buffer
)
4930 (make-overlay beg end buffer
)))
4931 (defun org-delete-overlay (ovl)
4932 (if (featurep 'xemacs
) (delete-extent ovl
) (delete-overlay ovl
)))
4933 (defun org-detach-overlay (ovl)
4934 (if (featurep 'xemacs
) (detach-extent ovl
) (delete-overlay ovl
)))
4935 (defun org-move-overlay (ovl beg end
&optional buffer
)
4936 (if (featurep 'xemacs
)
4937 (set-extent-endpoints ovl beg end
(or buffer
(current-buffer)))
4938 (move-overlay ovl beg end buffer
)))
4939 (defun org-overlay-put (ovl prop value
)
4940 (if (featurep 'xemacs
)
4941 (set-extent-property ovl prop value
)
4942 (overlay-put ovl prop value
)))
4943 (defun org-overlay-display (ovl text
&optional face evap
)
4944 "Make overlay OVL display TEXT with face FACE."
4945 (if (featurep 'xemacs
)
4946 (let ((gl (make-glyph text
)))
4947 (and face
(set-glyph-face gl face
))
4948 (set-extent-property ovl
'invisible t
)
4949 (set-extent-property ovl
'end-glyph gl
))
4950 (overlay-put ovl
'display text
)
4951 (if face
(overlay-put ovl
'face face
))
4952 (if evap
(overlay-put ovl
'evaporate t
))))
4953 (defun org-overlay-before-string (ovl text
&optional face evap
)
4954 "Make overlay OVL display TEXT with face FACE."
4955 (if (featurep 'xemacs
)
4956 (let ((gl (make-glyph text
)))
4957 (and face
(set-glyph-face gl face
))
4958 (set-extent-property ovl
'begin-glyph gl
))
4959 (if face
(org-add-props text nil
'face face
))
4960 (overlay-put ovl
'before-string text
)
4961 (if evap
(overlay-put ovl
'evaporate t
))))
4962 (defun org-overlay-get (ovl prop
)
4963 (if (featurep 'xemacs
)
4964 (extent-property ovl prop
)
4965 (overlay-get ovl prop
)))
4966 (defun org-overlays-at (pos)
4967 (if (featurep 'xemacs
) (extents-at pos
) (overlays-at pos
)))
4968 (defun org-overlays-in (&optional start end
)
4969 (if (featurep 'xemacs
)
4970 (extent-list nil start end
)
4971 (overlays-in start end
)))
4972 (defun org-overlay-start (o)
4973 (if (featurep 'xemacs
) (extent-start-position o
) (overlay-start o
)))
4974 (defun org-overlay-end (o)
4975 (if (featurep 'xemacs
) (extent-end-position o
) (overlay-end o
)))
4976 (defun org-find-overlays (prop &optional pos delete
)
4977 "Find all overlays specifying PROP at POS or point.
4978 If DELETE is non-nil, delete all those overlays."
4979 (let ((overlays (org-overlays-at (or pos
(point))))
4981 (while (setq ov
(pop overlays
))
4982 (if (org-overlay-get ov prop
)
4983 (if delete
(org-delete-overlay ov
) (push ov found
))))
4986 ;; Region compatibility
4988 (defun org-add-hook (hook function
&optional append local
)
4989 "Add-hook, compatible with both Emacsen."
4990 (if (and local
(featurep 'xemacs
))
4991 (add-local-hook hook function append
)
4992 (add-hook hook function append local
)))
4994 (defvar org-ignore-region nil
4995 "To temporarily disable the active region.")
4997 (defun org-region-active-p ()
4998 "Is `transient-mark-mode' on and the region active?
4999 Works on both Emacs and XEmacs."
5000 (if org-ignore-region
5002 (if (featurep 'xemacs
)
5003 (and zmacs-regions
(region-active-p))
5004 (if (fboundp 'use-region-p
)
5006 (and transient-mark-mode mark-active
))))) ; Emacs 22 and before
5008 ;; Invisibility compatibility
5010 (defun org-add-to-invisibility-spec (arg)
5011 "Add elements to `buffer-invisibility-spec'.
5012 See documentation for `buffer-invisibility-spec' for the kind of elements
5015 ((fboundp 'add-to-invisibility-spec
)
5016 (add-to-invisibility-spec arg
))
5017 ((or (null buffer-invisibility-spec
) (eq buffer-invisibility-spec t
))
5018 (setq buffer-invisibility-spec
(list arg
)))
5020 (setq buffer-invisibility-spec
5021 (cons arg buffer-invisibility-spec
)))))
5023 (defun org-remove-from-invisibility-spec (arg)
5024 "Remove elements from `buffer-invisibility-spec'."
5025 (if (fboundp 'remove-from-invisibility-spec
)
5026 (remove-from-invisibility-spec arg
)
5027 (if (consp buffer-invisibility-spec
)
5028 (setq buffer-invisibility-spec
5029 (delete arg buffer-invisibility-spec
)))))
5031 (defun org-in-invisibility-spec-p (arg)
5032 "Is ARG a member of `buffer-invisibility-spec'?"
5033 (if (consp buffer-invisibility-spec
)
5034 (member arg buffer-invisibility-spec
)
5037 ;;;; Define the Org-mode
5039 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
5040 (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."))
5043 ;; We use a before-change function to check if a table might need
5045 (defvar org-table-may-need-update t
5046 "Indicates that a table might need an update.
5047 This variable is set by `org-before-change-function'.
5048 `org-table-align' sets it back to nil.")
5049 (defvar org-mode-map
)
5050 (defvar org-mode-hook nil
)
5051 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
5052 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
5053 (defvar org-table-buffer-is-an nil
)
5054 (defconst org-outline-regexp
"\\*+ ")
5057 (define-derived-mode org-mode outline-mode
"Org"
5058 "Outline-based notes management and organizer, alias
5059 \"Carsten's outline-mode for keeping track of everything.\"
5061 Org-mode develops organizational tasks around a NOTES file which
5062 contains information about projects as plain text. Org-mode is
5063 implemented on top of outline-mode, which is ideal to keep the content
5064 of large files well structured. It supports ToDo items, deadlines and
5065 time stamps, which magically appear in the diary listing of the Emacs
5066 calendar. Tables are easily created with a built-in table editor.
5067 Plain text URL-like links connect to websites, emails (VM), Usenet
5068 messages (Gnus), BBDB entries, and any files related to the project.
5069 For printing and sharing of notes, an Org-mode file (or a part of it)
5070 can be exported as a structured ASCII or HTML file.
5072 The following commands are available:
5076 ;; Get rid of Outline menus, they are not needed
5077 ;; Need to do this here because define-derived-mode sets up
5078 ;; the keymap so late. Still, it is a waste to call this each time
5079 ;; we switch another buffer into org-mode.
5080 (if (featurep 'xemacs
)
5081 (when (boundp 'outline-mode-menu-heading
)
5082 ;; Assume this is Greg's port, it used easymenu
5083 (easy-menu-remove outline-mode-menu-heading
)
5084 (easy-menu-remove outline-mode-menu-show
)
5085 (easy-menu-remove outline-mode-menu-hide
))
5086 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5087 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5088 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5090 (easy-menu-add org-org-menu
)
5091 (easy-menu-add org-tbl-menu
)
5092 (org-install-agenda-files-menu)
5093 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
5094 (org-add-to-invisibility-spec '(org-cwidth))
5095 (when (featurep 'xemacs
)
5096 (org-set-local 'line-move-ignore-invisible t
))
5097 (org-set-local 'outline-regexp org-outline-regexp
)
5098 (org-set-local 'outline-level
'org-outline-level
)
5099 (when (and org-ellipsis
5100 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5101 (fboundp 'make-glyph-code
))
5102 (unless org-display-table
5103 (setq org-display-table
(make-display-table)))
5104 (set-display-table-slot
5107 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5109 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5110 (setq buffer-display-table org-display-table
))
5111 (org-set-regexps-and-options)
5113 (org-set-local 'calc-embedded-open-mode
"# ")
5114 (modify-syntax-entry ?
# "<")
5115 (modify-syntax-entry ?
@ "w")
5116 (if org-startup-truncated
(setq truncate-lines t
))
5117 (org-set-local 'font-lock-unfontify-region-function
5118 'org-unfontify-region
)
5119 ;; Activate before-change-function
5120 (org-set-local 'org-table-may-need-update t
)
5121 (org-add-hook 'before-change-functions
'org-before-change-function nil
5123 ;; Check for running clock before killing a buffer
5124 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5125 ;; Paragraphs and auto-filling
5126 (org-set-autofill-regexps)
5127 (setq indent-line-function
'org-indent-line-function
)
5128 (org-update-radio-target-regexp)
5130 ;; Comment characters
5131 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
5132 (org-set-local 'comment-padding
" ")
5134 ;; Align options lines
5136 'align-mode-rules-list
5137 '((org-in-buffer-settings
5138 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5139 (modes .
'(org-mode)))))
5142 (org-set-local 'imenu-create-index-function
5143 'org-imenu-get-tree
)
5145 ;; Make isearch reveal context
5146 (if (or (featurep 'xemacs
)
5147 (not (boundp 'outline-isearch-open-invisible-function
)))
5148 ;; Emacs 21 and XEmacs make use of the hook
5149 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5150 ;; Emacs 22 deals with this through a special variable
5151 (org-set-local 'outline-isearch-open-invisible-function
5152 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5154 ;; If empty file that did not turn on org-mode automatically, make it to.
5155 (if (and org-insert-mode-line-in-empty-file
5157 (= (point-min) (point-max)))
5158 (insert "# -*- mode: org -*-\n\n"))
5160 (unless org-inhibit-startup
5161 (when org-startup-align-all-tables
5162 (let ((bmp (buffer-modified-p)))
5163 (org-table-map-tables 'org-table-align
)
5164 (set-buffer-modified-p bmp
)))
5165 (org-cycle-hide-drawers 'all
)
5167 ((eq org-startup-folded t
)
5169 ((eq org-startup-folded
'content
)
5170 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
5171 (org-cycle '(4)) (org-cycle '(4)))))))
5173 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5175 (defsubst org-call-with-arg
(command arg
)
5176 "Call COMMAND interactively, but pretend prefix are was ARG."
5177 (let ((current-prefix-arg arg
)) (call-interactively command
)))
5179 (defsubst org-current-line
(&optional pos
)
5181 (and pos
(goto-char pos
))
5182 ;; works also in narrowed buffer, because we start at 1, not point-min
5183 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
5185 (defun org-current-time ()
5186 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5187 (if (> (car org-time-stamp-rounding-minutes
) 1)
5188 (let ((r (car org-time-stamp-rounding-minutes
))
5189 (time (decode-time)))
5191 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5195 (defun org-add-props (string plist
&rest props
)
5196 "Add text properties to entire string, from beginning to end.
5197 PLIST may be a list of properties, PROPS are individual properties and values
5198 that will be added to PLIST. Returns the string that was modified."
5199 (add-text-properties
5200 0 (length string
) (if props
(append plist props
) plist
) string
)
5202 (put 'org-add-props
'lisp-indent-function
2)
5205 ;;;; Font-Lock stuff, including the activators
5207 (defvar org-mouse-map
(make-sparse-keymap))
5208 (org-defkey org-mouse-map
5209 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
5210 (org-defkey org-mouse-map
5211 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
5212 (when org-mouse-1-follows-link
5213 (org-defkey org-mouse-map [follow-link] 'mouse-face))
5214 (when org-tab-follows-link
5215 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
5216 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
5217 (when org-return-follows-link
5218 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
5219 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
5221 (require 'font-lock)
5223 (defconst org-non-link-chars "]\t\n\r<>")
5224 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
5225 "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
5226 (defvar org-link-re-with-space nil
5227 "Matches a link with spaces, optional angular brackets around it.")
5228 (defvar org-link-re-with-space2 nil
5229 "Matches a link with spaces, optional angular brackets around it.")
5230 (defvar org-angle-link-re nil
5231 "Matches link with angular brackets, spaces are allowed.")
5232 (defvar org-plain-link-re nil
5233 "Matches plain link, without spaces.")
5234 (defvar org-bracket-link-regexp nil
5235 "Matches a link in double brackets.")
5236 (defvar org-bracket-link-analytic-regexp nil
5237 "Regular expression used to analyze links.
5238 Here is what the match groups contain after a match:
5244 (defvar org-any-link-re nil
5245 "Regular expression matching any link.")
5247 (defun org-make-link-regexps ()
5248 "Update the link regular expressions.
5249 This should be called after the variable `org-link-types' has changed."
5250 (setq org-link-re-with-space
5252 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5253 "\\([^" org-non-link-chars " ]"
5254 "[^" org-non-link-chars "]*"
5255 "[^" org-non-link-chars " ]\\)>?")
5256 org-link-re-with-space2
5258 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5259 "\\([^" org-non-link-chars " ]"
5261 "[^" org-non-link-chars " ]\\)>?")
5264 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5265 "\\([^" org-non-link-chars " ]"
5266 "[^" org-non-link-chars "]*"
5270 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5271 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5272 org-bracket-link-regexp
5273 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5274 org-bracket-link-analytic-regexp
5277 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
5280 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5283 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
5284 org-angle-link-re "\\)\\|\\("
5285 org-plain-link-re "\\)")))
5287 (org-make-link-regexps)
5289 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
5290 "Regular expression for fast time stamp matching.")
5291 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
5292 "Regular expression for fast time stamp matching.")
5293 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5294 "Regular expression matching time strings for analysis.
5295 This one does not require the space after the date, so it can be used
5296 on a string that terminates immediately after the date.")
5297 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5298 "Regular expression matching time strings for analysis.")
5299 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
5300 "Regular expression matching time stamps, with groups.")
5301 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
5302 "Regular expression matching time stamps (also [..]), with groups.")
5303 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
5304 "Regular expression matching a time stamp range.")
5305 (defconst org-tr-regexp-both
5306 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
5307 "Regular expression matching a time stamp range.")
5308 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
5309 org-ts-regexp "\\)?")
5310 "Regular expression matching a time stamp or time stamp range.")
5311 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
5312 org-ts-regexp-both "\\)?")
5313 "Regular expression matching a time stamp or time stamp range.
5314 The time stamps may be either active or inactive.")
5316 (defvar org-emph-face nil)
5318 (defun org-do-emphasis-faces (limit)
5319 "Run through the buffer and add overlays to links."
5321 (while (and (not rtn) (re-search-forward org-emph-re limit t))
5322 (if (not (= (char-after (match-beginning 3))
5323 (char-after (match-beginning 4))))
5326 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5328 (nth 1 (assoc (match-string 3)
5329 org-emphasis-alist)))
5330 (add-text-properties (match-beginning 2) (match-end 2)
5331 '(font-lock-multiline t))
5332 (when org-hide-emphasis-markers
5333 (add-text-properties (match-end 4) (match-beginning 5)
5334 '(invisible org-link))
5335 (add-text-properties (match-beginning 3) (match-end 3)
5336 '(invisible org-link)))))
5340 (defun org-emphasize (&optional char)
5341 "Insert or change an emphasis, i.e. a font like bold or italic.
5342 If there is an active region, change that region to a new emphasis.
5343 If there is no region, just insert the marker characters and position
5344 the cursor between them.
5345 CHAR should be either the marker character, or the first character of the
5346 HTML tag associated with that emphasis. If CHAR is a space, the means
5347 to remove the emphasis of the selected region.
5348 If char is not given (for example in an interactive call) it
5349 will be prompted for."
5351 (let ((eal org-emphasis-alist) e det
5352 (erc org-emphasis-regexp-components)
5354 (string "") beg end move tag c s)
5355 (if (org-region-active-p)
5356 (setq beg (region-beginning) end (region-end)
5357 string (buffer-substring beg end))
5360 (while (setq e (pop eal))
5361 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
5363 (push (cons c (string-to-char (car e))) det)
5364 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
5365 (substring tag 1)))))
5367 (message "%s" (concat "Emphasis marker or tag:" prompt))
5368 (setq char (read-char-exclusive)))
5369 (setq char (or (cdr (assoc char det)) char))
5370 (if (equal char ?\ )
5371 (setq s "" move nil)
5372 (unless (assoc (char-to-string char) org-emphasis-alist)
5373 (error "No such emphasis marker: \"%c\"" char))
5374 (setq s (char-to-string char)))
5375 (while (and (> (length string) 1)
5376 (equal (substring string 0 1) (substring string -1))
5377 (assoc (substring string 0 1) org-emphasis-alist))
5378 (setq string (substring string 1 -1)))
5379 (setq string (concat s string s))
5380 (if beg (delete-region beg end))
5382 (string-match (concat "[" (nth 0 erc) "\n]")
5383 (char-to-string (char-before (point)))))
5385 (unless (string-match (concat "[" (nth 1 erc) "\n]")
5386 (char-to-string (char-after (point))))
5387 (insert " ") (backward-char 1))
5389 (and move (backward-char 1))))
5391 (defconst org-nonsticky-props
5392 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
5395 (defun org-activate-plain-links (limit)
5396 "Run through the buffer and add overlays to links."
5399 (while (re-search-forward org-plain-link-re limit t)
5400 (setq f (get-text-property (match-beginning 0) 'face))
5401 (if (or (eq f 'org-tag)
5402 (and (listp f) (memq 'org-tag f)))
5404 (add-text-properties (match-beginning 0) (match-end 0)
5405 (list 'mouse-face 'highlight
5406 'rear-nonsticky org-nonsticky-props
5407 'keymap org-mouse-map
5409 (throw 'exit t))))))
5411 (defun org-activate-code (limit)
5412 (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
5413 (unless (get-text-property (match-beginning 1) 'face)
5414 (remove-text-properties (match-beginning 0) (match-end 0)
5415 '(display t invisible t intangible t))
5418 (defun org-activate-angle-links (limit)
5419 "Run through the buffer and add overlays to links."
5420 (if (re-search-forward org-angle-link-re limit t)
5422 (add-text-properties (match-beginning 0) (match-end 0)
5423 (list 'mouse-face 'highlight
5424 'rear-nonsticky org-nonsticky-props
5425 'keymap org-mouse-map
5429 (defmacro org-maybe-intangible (props)
5430 "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
5431 In emacs 21, invisible text is not avoided by the command loop, so the
5432 intangible property is needed to make sure point skips this text.
5433 In Emacs 22, this is not necessary. The intangible text property has
5434 led to problems with flyspell. These problems are fixed in flyspell.el,
5435 but we still avoid setting the property in Emacs 22 and later.
5436 We use a macro so that the test can happen at compilation time."
5437 (if (< emacs-major-version 22)
5438 `(append '(intangible t) ,props)
5441 (defun org-activate-bracket-links (limit)
5442 "Run through the buffer and add overlays to bracketed links."
5443 (if (re-search-forward org-bracket-link-regexp limit t)
5444 (let* ((help (concat "LINK: "
5445 (org-match-string-no-properties 1)))
5446 ;; FIXME: above we should remove the escapes.
5447 ;; but that requires another match, protecting match data,
5448 ;; a lot of overhead for font-lock.
5449 (ip (org-maybe-intangible
5450 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
5451 'keymap org-mouse-map 'mouse-face 'highlight
5452 'font-lock-multiline t 'help-echo help)))
5453 (vp (list 'rear-nonsticky org-nonsticky-props
5454 'keymap org-mouse-map 'mouse-face 'highlight
5455 ' font-lock-multiline t 'help-echo help)))
5456 ;; We need to remove the invisible property here. Table narrowing
5457 ;; may have made some of this invisible.
5458 (remove-text-properties (match-beginning 0) (match-end 0)
5462 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
5463 (add-text-properties (match-beginning 3) (match-end 3) vp)
5464 (add-text-properties (match-end 3) (match-end 0) ip))
5465 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
5466 (add-text-properties (match-beginning 1) (match-end 1) vp)
5467 (add-text-properties (match-end 1) (match-end 0) ip))
5470 (defun org-activate-dates (limit)
5471 "Run through the buffer and add overlays to dates."
5472 (if (re-search-forward org-tsr-regexp-both limit t)
5474 (add-text-properties (match-beginning 0) (match-end 0)
5475 (list 'mouse-face 'highlight
5476 'rear-nonsticky org-nonsticky-props
5477 'keymap org-mouse-map))
5478 (when org-display-custom-times
5480 (org-display-custom-time (match-beginning 3) (match-end 3)))
5481 (org-display-custom-time (match-beginning 1) (match-end 1)))
5484 (defvar org-target-link-regexp nil
5485 "Regular expression matching radio targets in plain text.")
5486 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
5487 "Regular expression matching a link target.")
5488 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
5489 "Regular expression matching a radio target.")
5490 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5491 "Regular expression matching any target.")
5493 (defun org-activate-target-links (limit)
5494 "Run through the buffer and add overlays to target matches."
5495 (when org-target-link-regexp
5496 (let ((case-fold-search t))
5497 (if (re-search-forward org-target-link-regexp limit t)
5499 (add-text-properties (match-beginning 0) (match-end 0)
5500 (list 'mouse-face 'highlight
5501 'rear-nonsticky org-nonsticky-props
5502 'keymap org-mouse-map
5503 'help-echo "Radio target link"
5504 'org-linked-text t))
5507 (defun org-update-radio-target-regexp ()
5508 "Find all radio targets in this file and update the regular expression."
5510 (when (memq 'radio org-activate-links)
5511 (setq org-target-link-regexp
5512 (org-make-target-link-regexp (org-all-targets 'radio)))
5513 (org-restart-font-lock)))
5515 (defun org-hide-wide-columns (limit)
5517 (setq s (text-property-any (point) (or limit (point-max))
5520 (setq e (next-single-property-change s 'org-cwidth))
5521 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
5525 (defvar org-latex-and-specials-regexp nil
5526 "Regular expression for highlighting export special stuff.")
5527 (defvar org-match-substring-regexp)
5528 (defvar org-match-substring-with-braces-regexp)
5529 (defvar org-export-html-special-string-regexps)
5531 (defun org-compute-latex-and-specials-regexp ()
5532 "Compute regular expression for stuff treated specially by exporters."
5533 (if (not org-highlight-latex-fragments-and-specials)
5534 (org-set-local 'org-latex-and-specials-regexp nil)
5536 ((matchers (plist-get org-format-latex-options :matchers))
5537 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
5538 org-latex-regexps)))
5539 (options (org-combine-plists (org-default-export-plist)
5540 (org-infile-export-plist)))
5541 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
5542 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
5543 (org-export-with-TeX-macros (plist-get options :TeX-macros))
5544 (org-export-html-expand (plist-get options :expand-quoted-html))
5545 (org-export-with-special-strings (plist-get options :special-strings))
5548 ((equal org-export-with-sub-superscripts '{})
5549 (list org-match-substring-with-braces-regexp))
5550 (org-export-with-sub-superscripts
5551 (list org-match-substring-regexp))
5554 (if org-export-with-LaTeX-fragments
5555 (mapcar (lambda (x) (nth 1 x)) latexs)))
5557 (if org-export-with-TeX-macros
5558 (list (concat "\\\\"
5560 (append (mapcar 'car org-html-entities)
5561 (if (boundp 'org-latex-entities)
5562 org-latex-entities nil))
5565 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5566 (re-special (if org-export-with-special-strings
5567 (mapcar (lambda (x) (car x))
5568 org-export-html-special-string-regexps)))
5572 (if org-export-html-expand "@<[^>\n]+>")
5575 'org-latex-and-specials-regexp
5576 (mapconcat 'identity (append re-latex re-sub re-macros re-special
5579 (defface org-latex-and-export-specials
5580 (let ((font (cond ((assq :inherit custom-face-attributes)
5581 '(:inherit underline))
5582 (t '(:underline t)))))
5583 `((((class grayscale) (background light))
5584 (:foreground "DimGray" ,@font))
5585 (((class grayscale) (background dark))
5586 (:foreground "LightGray" ,@font))
5587 (((class color) (background light))
5588 (:foreground "SaddleBrown"))
5589 (((class color) (background dark))
5590 (:foreground "burlywood"))
5592 "Face used to highlight math latex and other special exporter stuff."
5595 (defun org-do-latex-and-special-faces (limit)
5596 "Run through the buffer and add overlays to links."
5597 (when org-latex-and-specials-regexp
5599 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
5601 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5603 '(org-code org-verbatim underline)))
5606 d (cond ((member (char-after (1+ (match-beginning 0)))
5609 (font-lock-prepend-text-property
5610 (+ d (match-beginning 0)) (match-end 0)
5611 'face 'org-latex-and-export-specials)
5612 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
5613 '(font-lock-multiline t)))))
5616 (defun org-restart-font-lock ()
5617 "Restart font-lock-mode, to force refontification."
5618 (when (and (boundp 'font-lock-mode) font-lock-mode)
5620 (font-lock-mode 1)))
5622 (defun org-all-targets (&optional radio)
5623 "Return a list of all targets in this file.
5624 With optional argument RADIO, only find radio targets."
5625 (let ((re (if radio org-radio-target-regexp org-target-regexp))
5628 (goto-char (point-min))
5629 (while (re-search-forward re nil t)
5630 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
5633 (defun org-make-target-link-regexp (targets)
5634 "Make regular expression matching all strings in TARGETS.
5635 The regular expression finds the targets also if there is a line break
5642 (while (string-match " +" x)
5643 (setq x (replace-match "\\s-+" t t x)))
5649 (defun org-activate-tags (limit)
5650 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
5652 (add-text-properties (match-beginning 1) (match-end 1)
5653 (list 'mouse-face 'highlight
5654 'rear-nonsticky org-nonsticky-props
5655 'keymap org-mouse-map))
5658 (defun org-outline-level ()
5660 (looking-at outline-regexp)
5661 (if (match-beginning 1)
5662 (+ (org-get-string-indentation (match-string 1)) 1000)
5663 (1- (- (match-end 0) (match-beginning 0))))))
5665 (defvar org-font-lock-keywords nil)
5667 (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
5668 "Regular expression matching a property line.")
5670 (defun org-set-font-lock-defaults ()
5671 (let* ((em org-fontify-emphasized-text)
5672 (lk org-activate-links)
5673 (org-font-lock-extra-keywords
5676 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
5677 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
5679 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5682 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
5683 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
5684 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
5686 (list org-drawer-regexp '(0 'org-special-keyword t))
5687 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
5689 (list org-property-re
5690 '(1 'org-special-keyword t)
5691 '(3 'org-property-value t))
5692 (if org-format-transports-properties-p
5693 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
5695 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
5696 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
5697 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
5698 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
5699 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
5700 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
5701 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
5702 '(org-hide-wide-columns (0 nil append))
5704 (list (concat "^\\*+[ \t]+" org-todo-regexp)
5705 '(1 (org-get-todo-face 1) t))
5707 (if org-fontify-done-headline
5708 (list (concat "^[*]+ +\\<\\("
5709 (mapconcat 'regexp-quote org-done-keywords "\\|")
5711 '(2 'org-headline-done t))
5714 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
5716 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
5717 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
5718 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
5719 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
5722 (if (featurep 'xemacs)
5723 '(org-do-emphasis-faces (0 nil append))
5724 '(org-do-emphasis-faces)))
5726 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
5728 (if org-provide-checkbox-statistics
5729 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5730 (0 (org-get-checkbox-statistics-face) t)))
5731 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
5732 '(1 'org-archived prepend))
5734 '(org-do-latex-and-special-faces)
5736 '(org-activate-code (1 'org-code t))
5738 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
5739 "\\|" org-quote-string "\\)\\>")
5740 '(1 'org-special-keyword t))
5741 '("^#.*" (0 'font-lock-comment-face t))
5743 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
5744 ;; Now set the full font-lock-keywords
5745 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
5746 (org-set-local 'font-lock-defaults
5747 '(org-font-lock-keywords t nil nil backward-paragraph))
5748 (kill-local-variable 'font-lock-keywords) nil))
5753 (defun org-get-level-face (n)
5754 "Get the right face for match N in font-lock matching of healdines."
5755 (setq org-l (- (match-end 2) (match-beginning 1) 1))
5756 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
5757 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
5759 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
5761 (t (if org-level-color-stars-only nil org-f))))
5763 (defun org-get-todo-face (kwd)
5764 "Get the right face for a TODO keyword KWD.
5765 If KWD is a number, get the corresponding match group."
5766 (if (numberp kwd) (setq kwd (match-string kwd)))
5767 (or (cdr (assoc kwd org-todo-keyword-faces))
5768 (and (member kwd org-done-keywords) 'org-done)
5771 (defun org-unfontify-region (beg end &optional maybe_loudly)
5772 "Remove fontification and activation overlays from links."
5773 (font-lock-default-unfontify-region beg end)
5774 (let* ((buffer-undo-list t)
5775 (inhibit-read-only t) (inhibit-point-motion-hooks t)
5776 (inhibit-modification-hooks t)
5777 deactivate-mark buffer-file-name buffer-file-truename)
5778 (remove-text-properties beg end
5779 '(mouse-face t keymap t org-linked-text t
5780 invisible t intangible t))))
5782 ;;;; Visibility cycling, including org-goto and indirect buffer
5786 (defvar org-cycle-global-status nil)
5787 (make-variable-buffer-local 'org-cycle-global-status)
5788 (defvar org-cycle-subtree-status nil)
5789 (make-variable-buffer-local 'org-cycle-subtree-status)
5792 (defun org-cycle (&optional arg)
5793 "Visibility cycling for Org-mode.
5795 - When this function is called with a prefix argument, rotate the entire
5796 buffer through 3 states (global cycling)
5797 1. OVERVIEW: Show only top-level headlines.
5798 2. CONTENTS: Show all headlines of all levels, but no body text.
5799 3. SHOW ALL: Show everything.
5801 - When point is at the beginning of a headline, rotate the subtree started
5802 by this line through 3 different states (local cycling)
5803 1. FOLDED: Only the main headline is shown.
5804 2. CHILDREN: The main headline and the direct children are shown.
5805 From this state, you can move to one of the children
5806 and zoom in further.
5807 3. SUBTREE: Show the entire subtree, including body text.
5809 - When there is a numeric prefix, go up to a heading with level ARG, do
5810 a `show-subtree' and return to the previous cursor position. If ARG
5811 is negative, go up that many levels.
5813 - When point is not at the beginning of a headline, execute
5814 `indent-relative', like TAB normally does. See the option
5815 `org-cycle-emulate-tab' for details.
5817 - Special case: if point is at the beginning of the buffer and there is
5818 no headline in line 1, this function will act as if called with prefix arg.
5819 But only if also the variable `org-cycle-global-at-bob' is t."
5821 (let* ((outline-regexp
5822 (if (and (org-mode-p) org-cycle-include-plain-lists)
5823 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
5825 (bob-special (and org-cycle-global-at-bob (bobp)
5826 (not (looking-at outline-regexp))))
5829 (delq 'org-optimize-window-after-visibility-change
5830 (copy-sequence org-cycle-hook))
5834 (if (or bob-special (equal arg '(4)))
5835 ;; special case: use global cycling
5840 ((org-at-table-p 'any)
5841 ;; Enter the table or move to the next field in the table
5842 (or (org-table-recognize-table.el)
5844 (if arg (org-table-edit-field t)
5845 (org-table-justify-field-maybe)
5846 (call-interactively 'org-table-next-field)))))
5848 ((eq arg t) ;; Global cycling
5851 ((and (eq last-command this-command)
5852 (eq org-cycle-global-status 'overview))
5853 ;; We just created the overview - now do table of contents
5854 ;; This can be slow in very large buffers, so indicate action
5855 (message "CONTENTS...")
5857 (message "CONTENTS...done")
5858 (setq org-cycle-global-status 'contents)
5859 (run-hook-with-args 'org-cycle-hook 'contents))
5861 ((and (eq last-command this-command)
5862 (eq org-cycle-global-status 'contents))
5863 ;; We just showed the table of contents - now show everything
5865 (message "SHOW ALL")
5866 (setq org-cycle-global-status 'all)
5867 (run-hook-with-args 'org-cycle-hook 'all))
5870 ;; Default action: go to overview
5872 (message "OVERVIEW")
5873 (setq org-cycle-global-status 'overview)
5874 (run-hook-with-args 'org-cycle-hook 'overview))))
5876 ((and org-drawers org-drawer-regexp
5878 (beginning-of-line 1)
5879 (looking-at org-drawer-regexp)))
5880 ;; Toggle block visibility
5882 (not (get-char-property (match-end 0) 'invisible))))
5885 ;; Show-subtree, ARG levels up from here.
5887 (org-back-to-heading)
5888 (outline-up-heading (if (< arg 0) (- arg)
5889 (- (funcall outline-level) arg)))
5890 (org-show-subtree)))
5892 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
5893 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
5894 ;; At a heading: rotate between three different views
5895 (org-back-to-heading)
5896 (let ((goal-column 0) eoh eol eos)
5897 ;; First, some boundaries
5899 (org-back-to-heading)
5901 (beginning-of-line 2)
5902 (while (and (not (eobp)) ;; this is like `next-line'
5903 (get-char-property (1- (point)) 'invisible))
5904 (beginning-of-line 2)) (setq eol (point)))
5905 (outline-end-of-heading) (setq eoh (point))
5906 (org-end-of-subtree t)
5908 (skip-chars-forward " \t\n")
5909 (beginning-of-line 1) ; in case this is an item
5911 (setq eos (1- (point))))
5912 ;; Find out what to do next and set `this-command'
5915 ;; Nothing is hidden behind this heading
5916 (message "EMPTY ENTRY")
5917 (setq org-cycle-subtree-status nil)
5920 (outline-next-heading)
5921 (if (org-invisible-p) (org-flag-heading nil))))
5923 (not (string-match "\\S-" (buffer-substring eol eos))))
5924 ;; Entire subtree is hidden in one line: open it
5927 (message "CHILDREN")
5930 (outline-next-heading)
5931 (if (org-invisible-p) (org-flag-heading nil)))
5932 (setq org-cycle-subtree-status 'children)
5933 (run-hook-with-args 'org-cycle-hook 'children))
5934 ((and (eq last-command this-command)
5935 (eq org-cycle-subtree-status 'children))
5936 ;; We just showed the children, now show everything.
5939 (setq org-cycle-subtree-status 'subtree)
5940 (run-hook-with-args 'org-cycle-hook 'subtree))
5942 ;; Default action: hide the subtree.
5945 (setq org-cycle-subtree-status 'folded)
5946 (run-hook-with-args 'org-cycle-hook 'folded)))))
5949 (buffer-read-only (org-back-to-heading))
5951 ((org-try-cdlatex-tab))
5953 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
5955 (not (looking-at outline-regexp))))
5956 (call-interactively (global-key-binding "\t")))
5958 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5959 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5960 (or (and (eq org-cycle-emulate-tab 'white)
5961 (= (match-end 0) (point-at-eol)))
5962 (and (eq org-cycle-emulate-tab 'whitestart)
5963 (>= (match-end 0) pos))))
5965 (eq org-cycle-emulate-tab t))
5966 ; (if (and (looking-at "[ \n\r\t]")
5967 ; (string-match "^[ \t]*$" (buffer-substring
5968 ; (point-at-bol) (point))))
5970 ; (beginning-of-line 1)
5971 ; (and (looking-at "[ \t]+") (replace-match ""))))
5972 (call-interactively (global-key-binding "\t")))
5975 (org-back-to-heading)
5979 (defun org-global-cycle (&optional arg)
5980 "Cycle the global visibility. For details see `org-cycle'."
5982 (let ((org-cycle-include-plain-lists
5983 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5987 (hide-sublevels arg)
5988 (setq org-cycle-global-status 'contents))
5991 (defun org-overview ()
5992 "Switch to overview mode, shoing only top-level headlines.
5993 Really, this shows all headlines with level equal or greater than the level
5994 of the first headline in the buffer. This is important, because if the
5995 first headline is not level one, then (hide-sublevels 1) gives confusing
5998 (let ((level (save-excursion
5999 (goto-char (point-min))
6000 (if (re-search-forward (concat "^" outline-regexp) nil t)
6002 (goto-char (match-beginning 0))
6003 (funcall outline-level))))))
6004 (and level (hide-sublevels level))))
6006 (defun org-content (&optional arg)
6007 "Show all headlines in the buffer, like a table of contents.
6008 With numerical argument N, show content up to level N."
6011 ;; Visit all headings and show their offspring
6012 (and (integerp arg) (org-overview))
6013 (goto-char (point-max))
6015 (while (and (progn (condition-case nil
6016 (outline-previous-visible-heading 1)
6017 (error (goto-char (point-min))))
6019 (looking-at outline-regexp))
6021 (show-children (1- arg))
6023 (if (bobp) (throw 'exit nil))))))
6026 (defun org-optimize-window-after-visibility-change (state)
6027 "Adjust the window after a change in outline visibility.
6028 This function is the default value of the hook `org-cycle-hook'."
6029 (when (get-buffer-window (current-buffer))
6031 ; ((eq state 'overview) (org-first-headline-recenter 1))
6032 ; ((eq state 'overview) (org-beginning-of-line))
6033 ((eq state 'content) nil)
6034 ((eq state 'all) nil)
6035 ((eq state 'folded) nil)
6036 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
6037 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
6039 (defun org-compact-display-after-subtree-move ()
6042 (if (org-up-heading-safe)
6047 (org-cycle-show-empty-lines 'children)
6048 (org-cycle-hide-drawers 'children))
6051 (defun org-cycle-show-empty-lines (state)
6052 "Show empty lines above all visible headlines.
6053 The region to be covered depends on STATE when called through
6054 `org-cycle-hook'. Lisp program can use t for STATE to get the
6055 entire buffer covered. Note that an empty line is only shown if there
6056 are at least `org-cycle-separator-lines' empty lines before the headeline."
6057 (when (> org-cycle-separator-lines 0)
6059 (let* ((n org-cycle-separator-lines)
6061 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
6062 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6063 (t (let ((ns (number-to-string (- n 2))))
6064 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
6065 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6068 ((memq state '(overview contents t))
6069 (setq beg (point-min) end (point-max)))
6070 ((memq state '(children folded))
6071 (setq beg (point) end (progn (org-end-of-subtree t t)
6072 (beginning-of-line 2)
6076 (while (re-search-forward re end t)
6077 (if (not (get-char-property (match-end 1) 'invisible))
6078 (outline-flag-region
6079 (match-beginning 1) (match-end 1) nil)))))))
6080 ;; Never hide empty lines at the end of the file.
6082 (goto-char (point-max))
6083 (outline-previous-heading)
6084 (outline-end-of-heading)
6085 (if (and (looking-at "[ \t\n]+")
6086 (= (match-end 0) (point-max)))
6087 (outline-flag-region (point) (match-end 0) nil))))
6089 (defun org-subtree-end-visible-p ()
6090 "Is the end of the current subtree visible?"
6091 (pos-visible-in-window-p
6092 (save-excursion (org-end-of-subtree t) (point))))
6094 (defun org-first-headline-recenter (&optional N)
6095 "Move cursor to the first headline and recenter the headline.
6096 Optional argument N means, put the headline into the Nth line of the window."
6097 (goto-char (point-min))
6098 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
6100 (recenter (prefix-numeric-value N))))
6104 (defvar org-goto-window-configuration nil)
6105 (defvar org-goto-marker nil)
6106 (defvar org-goto-map
6107 (let ((map (make-sparse-keymap)))
6108 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
6109 (while (setq cmd (pop cmds))
6110 (substitute-key-definition cmd cmd map global-map)))
6111 (suppress-keymap map)
6112 (org-defkey map "\C-m" 'org-goto-ret)
6113 (org-defkey map [(return)] 'org-goto-ret)
6114 (org-defkey map [(left)] 'org-goto-left)
6115 (org-defkey map [(right)] 'org-goto-right)
6116 (org-defkey map [(control ?g)] 'org-goto-quit)
6117 (org-defkey map "\C-i" 'org-cycle)
6118 (org-defkey map [(tab)] 'org-cycle)
6119 (org-defkey map [(down)] 'outline-next-visible-heading)
6120 (org-defkey map [(up)] 'outline-previous-visible-heading)
6121 (if org-goto-auto-isearch
6122 (if (fboundp 'define-key-after)
6123 (define-key-after map [t] 'org-goto-local-auto-isearch)
6125 (org-defkey map "q" 'org-goto-quit)
6126 (org-defkey map "n" 'outline-next-visible-heading)
6127 (org-defkey map "p" 'outline-previous-visible-heading)
6128 (org-defkey map "f" 'outline-forward-same-level)
6129 (org-defkey map "b" 'outline-backward-same-level)
6130 (org-defkey map "u" 'outline-up-heading))
6131 (org-defkey map "/" 'org-occur)
6132 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6133 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6134 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6135 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6136 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6139 (defconst org-goto-help
6140 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6141 RET=jump to location [Q]uit and return to previous location
6142 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6144 (defvar org-goto-start-pos) ; dynamically scoped parameter
6146 (defun org-goto (&optional alternative-interface)
6147 "Look up a different location in the current file, keeping current visibility.
6149 When you want look-up or go to a different location in a document, the
6150 fastest way is often to fold the entire buffer and then dive into the tree.
6151 This method has the disadvantage, that the previous location will be folded,
6152 which may not be what you want.
6154 This command works around this by showing a copy of the current buffer
6155 in an indirect buffer, in overview mode. You can dive into the tree in
6156 that copy, use org-occur and incremental search to find a location.
6157 When pressing RET or `Q', the command returns to the original buffer in
6158 which the visibility is still unchanged. After RET is will also jump to
6159 the location selected in the indirect buffer and expose the
6160 the headline hierarchy above."
6162 (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
6163 (org-refile-use-outline-path t)
6165 (if (not alternative-interface)
6167 (if (eq org-goto-interface 'outline)
6168 'outline-path-completion
6170 (org-goto-start-pos (point))
6172 (if (eq interface 'outline)
6173 (car (org-get-location (current-buffer) org-goto-help))
6174 (nth 3 (org-refile-get-location "Goto: ")))))
6177 (org-mark-ring-push org-goto-start-pos)
6178 (goto-char selected-point)
6179 (if (or (org-invisible-p) (org-invisible-p2))
6180 (org-show-context 'org-goto)))
6183 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6184 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6185 (defvar org-goto-local-auto-isearch-map) ; defined below
6187 (defun org-get-location (buf help)
6188 "Let the user select a location in the Org-mode buffer BUF.
6189 This function uses a recursive edit. It returns the selected position
6191 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6192 (isearch-hide-immediately nil)
6193 (isearch-search-fun-function
6194 (lambda () 'org-goto-local-search-forward-headings))
6195 (org-goto-selected-point org-goto-exit-command))
6197 (save-window-excursion
6198 (delete-other-windows)
6199 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6202 (make-indirect-buffer (current-buffer) "*org-goto*")
6203 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6204 (with-output-to-temp-buffer "*Help*"
6206 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
6207 (setq buffer-read-only nil)
6208 (let ((org-startup-truncated t)
6209 (org-startup-folded nil)
6210 (org-startup-align-all-tables nil))
6213 (setq buffer-read-only t)
6214 (if (and (boundp 'org-goto-start-pos)
6215 (integer-or-marker-p org-goto-start-pos))
6216 (let ((org-show-hierarchy-above t)
6217 (org-show-siblings t)
6218 (org-show-following-heading t))
6219 (goto-char org-goto-start-pos)
6220 (and (org-invisible-p) (org-show-context)))
6221 (goto-char (point-min)))
6222 (org-beginning-of-line)
6223 (message "Select location and press RET")
6224 (use-local-map org-goto-map)
6227 (kill-buffer "*org-goto*")
6228 (cons org-goto-selected-point org-goto-exit-command)))
6230 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6231 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6232 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6233 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6235 (defun org-goto-local-search-forward-headings (string bound noerror)
6236 "Search and make sure that anu matches are in headlines."
6238 (while (search-forward string bound noerror)
6239 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6240 (and (member :headline context)
6241 (not (member :tags context))))
6242 (throw 'return (point))))))
6244 (defun org-goto-local-auto-isearch ()
6247 (goto-char (point-min))
6248 (let ((keys (this-command-keys)))
6249 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6251 (isearch-process-search-char (string-to-char keys)))))
6253 (defun org-goto-ret (&optional arg)
6254 "Finish `org-goto' by going to the new location."
6256 (setq org-goto-selected-point (point)
6257 org-goto-exit-command 'return)
6260 (defun org-goto-left ()
6261 "Finish `org-goto' by going to the new location."
6263 (if (org-on-heading-p)
6265 (beginning-of-line 1)
6266 (setq org-goto-selected-point (point)
6267 org-goto-exit-command 'left)
6269 (error "Not on a heading")))
6271 (defun org-goto-right ()
6272 "Finish `org-goto' by going to the new location."
6274 (if (org-on-heading-p)
6276 (setq org-goto-selected-point (point)
6277 org-goto-exit-command 'right)
6279 (error "Not on a heading")))
6281 (defun org-goto-quit ()
6282 "Finish `org-goto' without cursor motion."
6284 (setq org-goto-selected-point nil)
6285 (setq org-goto-exit-command 'quit)
6288 ;;; Indirect buffer display of subtrees
6290 (defvar org-indirect-dedicated-frame nil
6291 "This is the frame being used for indirect tree display.")
6292 (defvar org-last-indirect-buffer nil)
6294 (defun org-tree-to-indirect-buffer (&optional arg)
6295 "Create indirect buffer and narrow it to current subtree.
6296 With numerical prefix ARG, go up to this level and then take that tree.
6297 If ARG is negative, go up that many levels.
6298 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6299 indirect buffer previously made with this command, to avoid proliferation of
6300 indirect buffers. However, when you call the command with a `C-u' prefix, or
6301 when `org-indirect-buffer-display' is `new-frame', the last buffer
6302 is kept so that you can work with several indirect buffers at the same time.
6303 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
6304 requests that a new frame be made for the new buffer, so that the dedicated
6305 frame is not changed."
6307 (let ((cbuf (current-buffer))
6308 (cwin (selected-window))
6310 beg end level heading ibuf)
6312 (org-back-to-heading t)
6314 (setq level (org-outline-level))
6315 (if (< arg 0) (setq arg (+ level arg)))
6316 (while (> (setq level (org-outline-level)) arg)
6317 (outline-up-heading 1 t)))
6319 heading (org-get-heading))
6320 (org-end-of-subtree t) (setq end (point)))
6321 (if (and (buffer-live-p org-last-indirect-buffer)
6322 (not (eq org-indirect-buffer-display 'new-frame))
6324 (kill-buffer org-last-indirect-buffer))
6325 (setq ibuf (org-get-indirect-buffer cbuf)
6326 org-last-indirect-buffer ibuf)
6328 ((or (eq org-indirect-buffer-display 'new-frame)
6329 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6330 (select-frame (make-frame))
6331 (delete-other-windows)
6332 (switch-to-buffer ibuf)
6333 (org-set-frame-title heading))
6334 ((eq org-indirect-buffer-display 'dedicated-frame)
6336 (select-frame (or (and org-indirect-dedicated-frame
6337 (frame-live-p org-indirect-dedicated-frame)
6338 org-indirect-dedicated-frame)
6339 (setq org-indirect-dedicated-frame (make-frame)))))
6340 (delete-other-windows)
6341 (switch-to-buffer ibuf)
6342 (org-set-frame-title (concat "Indirect: " heading)))
6343 ((eq org-indirect-buffer-display 'current-window)
6344 (switch-to-buffer ibuf))
6345 ((eq org-indirect-buffer-display 'other-window)
6346 (pop-to-buffer ibuf))
6347 (t (error "Invalid value.")))
6348 (if (featurep 'xemacs)
6349 (save-excursion (org-mode) (turn-on-font-lock)))
6350 (narrow-to-region beg end)
6353 (and (window-live-p cwin) (select-window cwin))))
6355 (defun org-get-indirect-buffer (&optional buffer)
6356 (setq buffer (or buffer (current-buffer)))
6357 (let ((n 1) (base (buffer-name buffer)) bname)
6358 (while (buffer-live-p
6359 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
6362 (make-indirect-buffer buffer bname 'clone)
6363 (error (make-indirect-buffer buffer bname)))))
6365 (defun org-set-frame-title (title)
6366 "Set the title of the current frame to the string TITLE."
6367 ;; FIXME: how to name a single frame in XEmacs???
6368 (unless (featurep 'xemacs)
6369 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
6371 ;;;; Structure editing
6373 ;;; Inserting headlines
6375 (defun org-insert-heading (&optional force-heading)
6376 "Insert a new heading or item with same depth at point.
6377 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
6378 If point is at the beginning of a headline, insert a sibling before the
6379 current headline. If point is not at the beginning, do not split the line,
6380 but create the new hedline after the current line."
6382 (if (= (buffer-size) 0)
6384 (when (or force-heading (not (org-insert-item)))
6385 (let* ((head (save-excursion
6388 (org-back-to-heading)
6391 (blank (cdr (assq 'heading org-blank-before-new-entry)))
6394 ((and (org-on-heading-p) (bolp)
6396 (save-excursion (backward-char 1) (not (org-invisible-p)))))
6397 ;; insert before the current line
6398 (open-line (if blank 2 1)))
6402 (backward-char 1) (not (org-invisible-p)))))
6403 ;; insert right here
6406 ; ;; in the middle of the line
6408 ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
6410 ; (org-on-heading-p)
6411 ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
6412 ; ;; protect the tags
6413 ;; (let ((tags (match-string 2)) pos)
6414 ; (delete-region (match-beginning 1) (match-end 1))
6415 ; (setq pos (point-at-bol))
6416 ; (newline (if blank 2 1))
6421 ; (org-set-tags nil 'align)))
6422 ; (newline (if blank 2 1)))
6423 ; (newline (if blank 2 1))))
6426 ;; in the middle of the line
6429 (org-get-alist-option org-M-RET-may-split-line 'headline))
6431 (if (org-on-heading-p)
6433 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
6434 (setq tags (and (match-end 2) (match-string 2)))
6436 (delete-region (match-beginning 1) (match-end 1)))
6437 (setq pos (point-at-bol))
6438 (or split (end-of-line 1))
6439 (delete-horizontal-space)
6440 (newline (if blank 2 1))
6446 (org-set-tags nil 'align))))
6447 (or split (end-of-line 1))
6448 (newline (if blank 2 1))))))
6449 (insert head) (just-one-space)
6452 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
6453 (run-hooks 'org-insert-heading-hook)))))
6455 (defun org-insert-heading-after-current ()
6456 "Insert a new heading with same level as current, after current subtree."
6458 (org-back-to-heading)
6459 (org-insert-heading)
6460 (org-move-subtree-down)
6463 (defun org-insert-todo-heading (arg)
6464 "Insert a new heading with the same level and TODO state as current heading.
6465 If the heading has no TODO state, or if the state is DONE, use the first
6466 state (TODO by default). Also with prefix arg, force first state."
6468 (when (not (org-insert-item 'checkbox))
6469 (org-insert-heading)
6471 (org-back-to-heading)
6472 (outline-previous-heading)
6473 (looking-at org-todo-line-regexp))
6475 (not (match-beginning 2))
6476 (member (match-string 2) org-done-keywords))
6477 (insert (car org-todo-keywords-1) " ")
6478 (insert (match-string 2) " "))))
6480 (defun org-insert-subheading (arg)
6481 "Insert a new subheading and demote it.
6482 Works for outline headings and for plain lists alike."
6484 (org-insert-heading arg)
6486 ((org-on-heading-p) (org-do-demote))
6487 ((org-at-item-p) (org-indent-item 1))))
6489 (defun org-insert-todo-subheading (arg)
6490 "Insert a new subheading with TODO keyword or checkbox and demote it.
6491 Works for outline headings and for plain lists alike."
6493 (org-insert-todo-heading arg)
6495 ((org-on-heading-p) (org-do-demote))
6496 ((org-at-item-p) (org-indent-item 1))))
6498 ;;; Promotion and Demotion
6500 (defun org-promote-subtree ()
6501 "Promote the entire subtree.
6502 See also `org-promote'."
6505 (org-map-tree 'org-promote))
6506 (org-fix-position-after-promote))
6508 (defun org-demote-subtree ()
6509 "Demote the entire subtree. See `org-demote'.
6510 See also `org-promote'."
6513 (org-map-tree 'org-demote))
6514 (org-fix-position-after-promote))
6517 (defun org-do-promote ()
6518 "Promote the current heading higher up the tree.
6519 If the region is active in `transient-mark-mode', promote all headings
6523 (if (org-region-active-p)
6524 (org-map-region 'org-promote (region-beginning) (region-end))
6526 (org-fix-position-after-promote))
6528 (defun org-do-demote ()
6529 "Demote the current heading lower down the tree.
6530 If the region is active in `transient-mark-mode', demote all headings
6534 (if (org-region-active-p)
6535 (org-map-region 'org-demote (region-beginning) (region-end))
6537 (org-fix-position-after-promote))
6539 (defun org-fix-position-after-promote ()
6540 "Make sure that after pro/demotion cursor position is right."
6541 (let ((pos (point)))
6542 (when (save-excursion
6543 (beginning-of-line 1)
6544 (looking-at org-todo-line-regexp)
6545 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6546 (cond ((eobp) (insert " "))
6547 ((eolp) (insert " "))
6548 ((equal (char-after) ?\ ) (forward-char 1))))))
6550 (defun org-reduced-level (l)
6551 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6553 (defun org-get-valid-level (level &optional change)
6554 "Rectify a level change under the influence of `org-odd-levels-only'
6555 LEVEL is a current level, CHANGE is by how much the level should be
6556 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6557 even level numbers will become the next higher odd number."
6558 (if org-odd-levels-only
6559 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6560 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6561 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6562 (max 1 (+ level change))))
6564 (define-obsolete-function-alias 'org-get-legal-level
6565 'org-get-valid-level "23.1")
6567 (defun org-promote ()
6568 "Promote the current heading higher up the tree.
6569 If the region is active in `transient-mark-mode', promote all headings
6571 (org-back-to-heading t)
6572 (let* ((level (save-match-data (funcall outline-level)))
6573 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6574 (diff (abs (- level (length up-head) -1))))
6575 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6576 (replace-match up-head nil t)
6577 ;; Fixup tag positioning
6578 (and org-auto-align-tags (org-set-tags nil t))
6579 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
6581 (defun org-demote ()
6582 "Demote the current heading lower down the tree.
6583 If the region is active in `transient-mark-mode', demote all headings
6585 (org-back-to-heading t)
6586 (let* ((level (save-match-data (funcall outline-level)))
6587 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
6588 (diff (abs (- level (length down-head) -1))))
6589 (replace-match down-head nil t)
6590 ;; Fixup tag positioning
6591 (and org-auto-align-tags (org-set-tags nil t))
6592 (if org-adapt-indentation (org-fixup-indentation diff))))
6594 (defun org-map-tree (fun)
6595 "Call FUN for every heading underneath the current one."
6596 (org-back-to-heading)
6597 (let ((level (funcall outline-level)))
6601 (outline-next-heading)
6602 (> (funcall outline-level) level))
6606 (defun org-map-region (fun beg end)
6607 "Call FUN for every heading between BEG and END."
6608 (let ((org-ignore-region t))
6610 (setq end (copy-marker end))
6612 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6616 (outline-next-heading)
6621 (defun org-fixup-indentation (diff)
6622 "Change the indentation in the current entry by DIFF
6623 However, if any line in the current entry has no indentation, or if it
6624 would end up with no indentation after the change, nothing at all is done."
6626 (let ((end (save-excursion (outline-next-heading)
6628 (prohibit (if (> diff 0)
6630 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6632 (unless (save-excursion (end-of-line 1)
6633 (re-search-forward prohibit end t))
6634 (while (and (< (point) end)
6635 (re-search-forward "^[ \t]+" end t))
6636 (goto-char (match-end 0))
6637 (setq col (current-column))
6638 (if (< diff 0) (replace-match ""))
6639 (indent-to (+ diff col))))
6640 (move-marker end nil))))
6642 (defun org-convert-to-odd-levels ()
6643 "Convert an org-mode file with all levels allowed to one with odd levels.
6644 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6647 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6648 (let ((org-odd-levels-only nil) n)
6650 (goto-char (point-min))
6651 (while (re-search-forward "^\\*\\*+ " nil t)
6652 (setq n (- (length (match-string 0)) 2))
6653 (while (>= (setq n (1- n)) 0)
6655 (end-of-line 1))))))
6658 (defun org-convert-to-oddeven-levels ()
6659 "Convert an org-mode file with only odd levels to one with odd and even levels.
6660 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6661 section with an even level, conversion would destroy the structure of the file. An error
6662 is signaled in this case."
6664 (goto-char (point-min))
6665 ;; First check if there are no even levels
6666 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6667 (org-show-context t)
6668 (error "Not all levels are odd in this file. Conversion not possible."))
6669 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6670 (let ((org-odd-levels-only nil) n)
6672 (goto-char (point-min))
6673 (while (re-search-forward "^\\*\\*+ " nil t)
6674 (setq n (/ (1- (length (match-string 0))) 2))
6675 (while (>= (setq n (1- n)) 0)
6677 (end-of-line 1))))))
6679 (defun org-tr-level (n)
6680 "Make N odd if required."
6681 (if org-odd-levels-only (1+ (/ n 2)) n))
6683 ;;; Vertical tree motion, cutting and pasting of subtrees
6685 (defun org-move-subtree-up (&optional arg)
6686 "Move the current subtree up past ARG headlines of the same level."
6688 (org-move-subtree-down (- (prefix-numeric-value arg))))
6690 (defun org-move-subtree-down (&optional arg)
6691 "Move the current subtree down past ARG headlines of the same level."
6693 (setq arg (prefix-numeric-value arg))
6694 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
6695 'outline-get-last-sibling))
6696 (ins-point (make-marker))
6698 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6700 (org-back-to-heading)
6703 (setq ne-beg (org-back-over-empty-lines))
6706 (save-excursion (outline-end-of-heading)
6707 (setq folded (org-invisible-p)))
6708 (outline-end-of-subtree))
6709 (outline-next-heading)
6710 (setq ne-end (org-back-over-empty-lines))
6713 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6714 ;; include less whitespace
6717 (forward-line (- ne-beg ne-end))
6718 (setq beg (point))))
6719 ;; Find insertion point, with error handling
6721 (or (and (funcall movfunc) (looking-at outline-regexp))
6722 (progn (goto-char beg0)
6723 (error "Cannot move past superior level or buffer limit")))
6724 (setq cnt (1- cnt)))
6726 ;; Moving forward - still need to move over subtree
6727 (progn (org-end-of-subtree t t)
6729 (org-back-over-empty-lines)
6730 (or (bolp) (newline)))))
6731 (setq ne-ins (org-back-over-empty-lines))
6732 (move-marker ins-point (point))
6733 (setq txt (buffer-substring beg end))
6734 (delete-region beg end)
6735 (outline-flag-region (1- beg) beg nil)
6736 (outline-flag-region (1- (point)) (point) nil)
6738 (or (bolp) (insert "\n"))
6739 (setq ins-end (point))
6740 (goto-char ins-point)
6741 (org-skip-whitespace)
6742 (when (and (< arg 0)
6743 (org-first-sibling-p)
6745 ;; Move whitespace back to beginning
6748 (let ((kill-whole-line t))
6749 (kill-line (- ne-ins ne-beg)) (point)))
6750 (insert (make-string (- ne-ins ne-beg) ?\n)))
6751 (move-marker ins-point nil)
6752 (org-compact-display-after-subtree-move)
6756 (org-cycle-hide-drawers 'children))))
6758 (defvar org-subtree-clip ""
6759 "Clipboard for cut and paste of subtrees.
6760 This is actually only a copy of the kill, because we use the normal kill
6761 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6763 (defvar org-subtree-clip-folded nil
6764 "Was the last copied subtree folded?
6765 This is used to fold the tree back after pasting.")
6767 (defun org-cut-subtree (&optional n)
6768 "Cut the current subtree into the clipboard.
6769 With prefix arg N, cut this many sequential subtrees.
6770 This is a short-hand for marking the subtree and then cutting it."
6772 (org-copy-subtree n 'cut))
6774 (defun org-copy-subtree (&optional n cut)
6775 "Cut the current subtree into the clipboard.
6776 With prefix arg N, cut this many sequential subtrees.
6777 This is a short-hand for marking the subtree and then copying it.
6778 If CUT is non-nil, actually cut the subtree."
6780 (let (beg end folded (beg0 (point)))
6782 (org-back-to-heading nil) ; take what looks like a subtree
6783 (org-back-to-heading t)) ; take what is really there
6784 (org-back-over-empty-lines)
6786 (skip-chars-forward " \t\r\n")
6788 (save-excursion (outline-end-of-heading)
6789 (setq folded (org-invisible-p)))
6791 (outline-forward-same-level (1- n))
6793 (org-end-of-subtree t t))
6794 (org-back-over-empty-lines)
6798 (setq org-subtree-clip-folded folded)
6799 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6800 (setq org-subtree-clip (current-kill 0))
6801 (message "%s: Subtree(s) with %d characters"
6802 (if cut "Cut" "Copied")
6803 (length org-subtree-clip)))))
6805 (defun org-paste-subtree (&optional level tree)
6806 "Paste the clipboard as a subtree, with modification of headline level.
6807 The entire subtree is promoted or demoted in order to match a new headline
6808 level. By default, the new level is derived from the visible headings
6809 before and after the insertion point, and taken to be the inferior headline
6810 level of the two. So if the previous visible heading is level 3 and the
6811 next is level 4 (or vice versa), level 4 will be used for insertion.
6812 This makes sure that the subtree remains an independent subtree and does
6813 not swallow low level entries.
6815 You can also force a different level, either by using a numeric prefix
6816 argument, or by inserting the heading marker by hand. For example, if the
6817 cursor is after \"*****\", then the tree will be shifted to level 5.
6819 If you want to insert the tree as is, just use \\[yank].
6821 If optional TREE is given, use this text instead of the kill ring."
6823 (unless (org-kill-is-subtree-p tree)
6825 (substitute-command-keys
6826 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6827 (let* ((txt (or tree (and kill-ring (current-kill 0))))
6828 (^re (concat "^\\(" outline-regexp "\\)"))
6829 (re (concat "\\(" outline-regexp "\\)"))
6830 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6832 (old-level (if (string-match ^re txt)
6833 (- (match-end 0) (match-beginning 0) 1)
6835 (force-level (cond (level (prefix-numeric-value level))
6837 ^re_ (buffer-substring (point-at-bol) (point)))
6838 (- (match-end 1) (match-beginning 1)))
6840 (previous-level (save-excursion
6843 (outline-previous-visible-heading 1)
6845 (- (match-end 0) (match-beginning 0) 1)
6848 (next-level (save-excursion
6851 (or (looking-at outline-regexp)
6852 (outline-next-visible-heading 1))
6854 (- (match-end 0) (match-beginning 0) 1)
6857 (new-level (or force-level (max previous-level next-level)))
6858 (shift (if (or (= old-level -1)
6860 (= old-level new-level))
6862 (- new-level old-level)))
6863 (delta (if (> shift 0) -1 1))
6864 (func (if (> shift 0) 'org-demote 'org-promote))
6865 (org-odd-levels-only nil)
6867 ;; Remove the forced level indicator
6869 (delete-region (point-at-bol) (point)))
6871 (beginning-of-line 1)
6872 (org-back-over-empty-lines) ;; FIXME: correct fix????
6874 (insert-before-markers txt) ;; FIXME: correct fix????
6875 (unless (string-match "\n\\'" txt) (insert "\n"))
6878 (skip-chars-forward " \t\n\r")
6880 ;; Shift if necessary
6883 (narrow-to-region beg end)
6884 (while (not (= shift 0))
6885 (org-map-region func (point-min) (point-max))
6886 (setq shift (+ delta shift)))
6887 (goto-char (point-min))))
6888 (when (interactive-p)
6889 (message "Clipboard pasted as level %d subtree" new-level))
6891 (eq org-subtree-clip (current-kill 0))
6892 org-subtree-clip-folded)
6893 ;; The tree was folded before it was killed/copied
6896 (defun org-kill-is-subtree-p (&optional txt)
6897 "Check if the current kill is an outline subtree, or a set of trees.
6898 Returns nil if kill does not start with a headline, or if the first
6899 headline level is not the largest headline level in the tree.
6900 So this will actually accept several entries of equal levels as well,
6901 which is OK for `org-paste-subtree'.
6902 If optional TXT is given, check this string instead of the current kill."
6903 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6904 (start-level (and kill
6905 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6906 org-outline-regexp "\\)")
6908 (- (match-end 2) (match-beginning 2) 1)))
6909 (re (concat "^" org-outline-regexp))
6910 (start (1+ (match-beginning 2))))
6911 (if (not start-level)
6913 nil) ;; does not even start with a heading
6915 (while (setq start (string-match re kill (1+ start)))
6916 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6920 (defun org-narrow-to-subtree ()
6921 "Narrow buffer to the current subtree."
6926 (progn (org-back-to-heading) (point))
6927 (progn (org-end-of-subtree t t) (point))))))
6932 (defun org-sort (with-case)
6933 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
6934 Optional argument WITH-CASE means sort case-sensitively."
6936 (if (org-at-table-p)
6937 (org-call-with-arg 'org-table-sort-lines with-case)
6938 (org-call-with-arg 'org-sort-entries-or-items with-case)))
6940 (defvar org-priority-regexp) ; defined later in the file
6942 (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
6943 "Sort entries on a certain level of an outline tree.
6944 If there is an active region, the entries in the region are sorted.
6945 Else, if the cursor is before the first entry, sort the top-level items.
6946 Else, the children of the entry at point are sorted.
6948 Sorting can be alphabetically, numerically, and by date/time as given by
6949 the first time stamp in the entry. The command prompts for the sorting
6950 type unless it has been given to the function through the SORTING-TYPE
6951 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
6952 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
6953 called with point at the beginning of the record. It must return either
6954 a string or a number that should serve as the sorting key for that record.
6956 Comparing entries ignores case by default. However, with an optional argument
6957 WITH-CASE, the sorting considers case as well."
6959 (let ((case-func (if with-case 'identity 'downcase))
6960 start beg end stars re re2
6961 txt what tmp plain-list-p)
6962 ;; Find beginning and end of region to sort
6964 ((org-region-active-p)
6965 ;; we will sort the region
6966 (setq end (region-end)
6968 (goto-char (region-beginning))
6969 (if (not (org-on-heading-p)) (outline-next-heading))
6970 (setq start (point)))
6972 ;; we will sort this plain list
6973 (org-beginning-of-item-list) (setq start (point))
6974 (org-end-of-item-list) (setq end (point))
6976 (setq plain-list-p t
6978 ((or (org-on-heading-p)
6979 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
6980 ;; we will sort the children of the current headline
6981 (org-back-to-heading)
6983 end (progn (org-end-of-subtree t t)
6984 (org-back-over-empty-lines)
6989 (outline-next-heading))
6991 ;; we will sort the top-level entries in this file
6992 (goto-char (point-min))
6993 (or (org-on-heading-p) (outline-next-heading))
6994 (setq start (point) end (point-max) what "top-level")
6999 (if (>= beg end) (error "Nothing to sort"))
7001 (unless plain-list-p
7002 (looking-at "\\(\\*+\\)")
7003 (setq stars (match-string 1)
7004 re (concat "^" (regexp-quote stars) " +")
7005 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
7006 txt (buffer-substring beg end))
7007 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
7008 (if (and (not (equal stars "*")) (string-match re2 txt))
7009 (error "Region to sort contains a level above the first entry")))
7011 (unless sorting-type
7014 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
7015 "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
7017 (setq sorting-type (read-char-exclusive))
7019 (and (= (downcase sorting-type) ?f)
7021 (completing-read "Sort using function: "
7022 obarray 'fboundp t nil nil))
7023 (setq getkey-func (intern getkey-func)))
7025 (and (= (downcase sorting-type) ?r)
7027 (completing-read "Property: "
7028 (mapcar 'list (org-buffer-property-keys t))
7031 (message "Sorting entries...")
7034 (narrow-to-region start end)
7036 (let ((dcst (downcase sorting-type))
7037 (now (current-time)))
7039 (/= dcst sorting-type)
7040 ;; This function moves to the beginning character of the "record" to
7044 (if (org-at-item-p) t (goto-char (point-max))))
7046 (if (re-search-forward re nil t)
7047 (goto-char (match-beginning 0))
7048 (goto-char (point-max)))))
7049 ;; This function moves to the last character of the "record" being
7056 (outline-forward-same-level 1)
7058 (goto-char (point-max)))))))
7060 ;; This function returns the value that gets sorted against.
7063 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
7066 (string-to-number (buffer-substring (match-end 0)
7069 (buffer-substring (match-end 0) (point-at-eol)))
7071 (if (re-search-forward org-ts-regexp
7073 (org-time-string-to-time (match-string 0))
7078 (setq tmp (funcall getkey-func))
7079 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7081 (error "Invalid key function `%s'" getkey-func)))
7082 (t (error "Invalid sorting type `%c'" sorting-type)))))
7086 (if (looking-at outline-regexp)
7087 (string-to-number (buffer-substring (match-end 0)
7091 (funcall case-func (buffer-substring (point-at-bol)
7094 (if (re-search-forward org-ts-regexp
7098 (org-time-string-to-time (match-string 0))
7101 (if (re-search-forward org-priority-regexp (point-at-eol) t)
7102 (string-to-char (match-string 2))
7103 org-default-priority))
7105 (or (org-entry-get nil property) ""))
7109 (setq tmp (funcall getkey-func))
7110 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7112 (error "Invalid key function `%s'" getkey-func)))
7113 (t (error "Invalid sorting type `%c'" sorting-type)))))
7116 ((= dcst ?a) 'string<)
7117 ((= dcst ?t) 'time-less-p)
7119 (message "Sorting entries...done")))
7121 (defun org-do-sort (table what &optional with-case sorting-type)
7122 "Sort TABLE of WHAT according to SORTING-TYPE.
7123 The user will be prompted for the SORTING-TYPE if the call to this
7124 function does not specify it. WHAT is only for the prompt, to indicate
7125 what is being sorted. The sorting key will be extracted from
7126 the car of the elements of the table.
7127 If WITH-CASE is non-nil, the sorting will be case-sensitive."
7128 (unless sorting-type
7130 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
7132 (setq sorting-type (read-char-exclusive)))
7133 (let ((dcst (downcase sorting-type))
7134 extractfun comparefun)
7135 ;; Define the appropriate functions
7138 (setq extractfun 'string-to-number
7139 comparefun (if (= dcst sorting-type) '< '>)))
7141 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
7142 (lambda(x) (downcase (org-sort-remove-invisible x))))
7143 comparefun (if (= dcst sorting-type)
7145 (lambda (a b) (and (not (string< a b))
7146 (not (string= a b)))))))
7150 (if (string-match org-ts-regexp x)
7152 (org-time-string-to-time (match-string 0 x)))
7154 comparefun (if (= dcst sorting-type) '< '>)))
7155 (t (error "Invalid sorting type `%c'" sorting-type)))
7157 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
7159 (lambda (a b) (funcall comparefun (car a) (car b))))))
7161 ;;;; Plain list items, including checkboxes
7163 ;;; Plain list items
7165 (defun org-at-item-p ()
7166 "Is point in a line starting a hand-formatted item?"
7167 (let ((llt org-plain-list-ordered-item-terminator))
7169 (goto-char (point-at-bol))
7172 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
7173 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
7174 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
7175 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
7177 (defun org-in-item-p ()
7178 "It the cursor inside a plain list item.
7179 Does not have to be the first line."
7183 (org-beginning-of-item)
7188 (defun org-insert-item (&optional checkbox)
7189 "Insert a new item at the current level.
7190 Return t when things worked, nil when we are not in an item."
7191 (when (save-excursion
7194 (org-beginning-of-item)
7196 (if (org-invisible-p) (error "Invisible item"))
7199 (let* ((bul (match-string 0))
7200 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
7202 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
7205 ((and (org-at-item-p) (<= (point) eow))
7206 ;; before the bullet
7207 (beginning-of-line 1)
7208 (open-line (if blank 2 1)))
7210 (beginning-of-line 1))
7212 (unless (org-get-alist-option org-M-RET-may-split-line 'item)
7214 (delete-horizontal-space))
7215 (newline (if blank 2 1))))
7216 (insert bul (if checkbox "[ ]" ""))
7220 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
7221 (org-maybe-renumber-ordered-list)
7222 (and checkbox (org-update-checkbox-count-maybe))
7227 (defun org-at-item-checkbox-p ()
7228 "Is point at a line starting a plain-list item with a checklet?"
7229 (and (org-at-item-p)
7231 (goto-char (match-end 0))
7232 (skip-chars-forward " \t")
7233 (looking-at "\\[[- X]\\]"))))
7235 (defun org-toggle-checkbox (&optional arg)
7236 "Toggle the checkbox in the current line."
7239 (let (beg end status (firstnew 'unknown))
7241 ((org-region-active-p)
7242 (setq beg (region-beginning) end (region-end)))
7244 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
7245 ((org-at-item-checkbox-p)
7246 (let ((pos (point)))
7249 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
7254 (t (error "Not at a checkbox or heading, and no active region")))
7257 (while (< (point) end)
7258 (when (org-at-item-checkbox-p)
7259 (setq status (equal (match-string 0) "[X]"))
7260 (when (eq firstnew 'unknown)
7261 (setq firstnew (not status)))
7263 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
7264 (beginning-of-line 2)))))
7265 (org-update-checkbox-count-maybe))
7267 (defun org-update-checkbox-count-maybe ()
7268 "Update checkbox statistics unless turned off by user."
7269 (when org-provide-checkbox-statistics
7270 (org-update-checkbox-count)))
7272 (defun org-update-checkbox-count (&optional all)
7273 "Update the checkbox statistics in the current section.
7274 This will find all statistic cookies like [57%] and [6/12] and update them
7275 with the current numbers. With optional prefix argument ALL, do this for
7279 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
7280 (beg (condition-case nil
7281 (progn (outline-back-to-heading) (point))
7282 (error (point-min))))
7283 (end (move-marker (make-marker)
7284 (progn (outline-next-heading) (point))))
7285 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
7286 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
7287 (re-find (concat re "\\|" re-box))
7288 beg-cookie end-cookie is-percent c-on c-off lim
7289 eline curr-ind next-ind continue-from startsearch
7293 (goto-char (point-min))
7294 (outline-next-heading)
7295 (setq beg (point) end (point-max)))
7297 ;; find each statistic cookie
7298 (while (re-search-backward re-find beg t)
7299 (setq beg-cookie (match-beginning 1)
7300 end-cookie (match-end 1)
7301 cstat (+ cstat (if end-cookie 1 0))
7302 startsearch (point-at-eol)
7303 continue-from (point-at-bol)
7304 is-percent (match-beginning 2)
7306 ((org-on-heading-p) (outline-next-heading) (point))
7307 ((org-at-item-p) (org-end-of-item) (point))
7312 ;; find first checkbox for this cookie and gather
7313 ;; statistics from all that are at this indentation level
7314 (goto-char startsearch)
7315 (if (re-search-forward re-box lim t)
7317 (org-beginning-of-item)
7318 (setq curr-ind (org-get-indentation))
7319 (setq next-ind curr-ind)
7320 (while (= curr-ind next-ind)
7321 (save-excursion (end-of-line) (setq eline (point)))
7322 (if (re-search-forward re-box eline t)
7323 (if (member (match-string 2) '("[ ]" "[-]"))
7324 (setq c-off (1+ c-off))
7325 (setq c-on (1+ c-on))
7329 (setq next-ind (org-get-indentation))
7331 (goto-char continue-from)
7334 (delete-region beg-cookie end-cookie)
7335 (goto-char beg-cookie)
7338 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
7339 (format "[%d/%d]" c-on (+ c-on c-off)))))
7340 ;; update items checkbox if it has one
7341 (when (org-at-item-p)
7342 (org-beginning-of-item)
7343 (when (and (> (+ c-on c-off) 0)
7344 (re-search-forward re-box (point-at-eol) t))
7345 (setq beg-cookie (match-beginning 2)
7346 end-cookie (match-end 2))
7347 (delete-region beg-cookie end-cookie)
7348 (goto-char beg-cookie)
7349 (cond ((= c-off 0) (insert "[X]"))
7350 ((= c-on 0) (insert "[ ]"))
7353 (goto-char continue-from))
7354 (when (interactive-p)
7355 (message "Checkbox satistics updated %s (%d places)"
7356 (if all "in entire file" "in current outline entry") cstat)))))
7358 (defun org-get-checkbox-statistics-face ()
7359 "Select the face for checkbox statistics.
7360 The face will be `org-done' when all relevant boxes are checked. Otherwise
7361 it will be `org-todo'."
7363 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
7364 (if (and (> (match-end 2) (match-beginning 2))
7365 (equal (match-string 2) (match-string 3)))
7369 (defun org-get-indentation (&optional line)
7370 "Get the indentation of the current line, interpreting tabs.
7371 When LINE is given, assume it represents a line and compute its indentation."
7373 (if (string-match "^ *" (org-remove-tabs line))
7376 (beginning-of-line 1)
7377 (skip-chars-forward " \t")
7380 (defun org-remove-tabs (s &optional width)
7381 "Replace tabulators in S with spaces.
7382 Assumes that s is a single line, starting in column 0."
7383 (setq width (or width tab-width))
7384 (while (string-match "\t" s)
7385 (setq s (replace-match
7387 (- (* width (/ (+ (match-beginning 0) width) width))
7388 (match-beginning 0)) ?\ )
7392 (defun org-fix-indentation (line ind)
7393 "Fix indentation in LINE.
7394 IND is a cons cell with target and minimum indentation.
7395 If the current indenation in LINE is smaller than the minimum,
7396 leave it alone. If it is larger than ind, set it to the target."
7397 (let* ((l (org-remove-tabs line))
7398 (i (org-get-indentation l))
7399 (i1 (car ind)) (i2 (cdr ind)))
7400 (if (>= i i2) (setq l (substring line i2)))
7402 (concat (make-string i1 ?\ ) l)
7405 (defcustom org-empty-line-terminates-plain-lists nil
7406 "Non-nil means, an empty line ends all plain list levels.
7407 When nil, empty lines are part of the preceeding item."
7408 :group 'org-plain-lists
7411 (defun org-beginning-of-item ()
7412 "Go to the beginning of the current hand-formatted item.
7413 If the cursor is not in an item, throw an error."
7416 (limit (save-excursion
7419 (org-back-to-heading)
7420 (beginning-of-line 2) (point))
7421 (error (point-min)))))
7422 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
7425 (beginning-of-line 1)
7426 (beginning-of-line 1)
7427 (skip-chars-forward " \t")
7428 (setq ind (current-column))
7431 (beginning-of-line 0)
7432 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
7434 (if (looking-at "[ \t]*$")
7435 (setq ind1 ind-empty)
7436 (skip-chars-forward " \t")
7437 (setq ind1 (current-column)))
7439 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
7442 (error "Not in an item")))))
7444 (defun org-end-of-item ()
7445 "Go to the end of the current hand-formatted item.
7446 If the cursor is not in an item, throw an error."
7448 (let* ((pos (point))
7450 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
7451 (limit (save-excursion (outline-next-heading) (point)))
7452 (ind (save-excursion
7453 (org-beginning-of-item)
7454 (skip-chars-forward " \t")
7458 (beginning-of-line 2)
7459 (if (eobp) (throw 'exit (point)))
7460 (if (>= (point) limit) (throw 'exit (point-at-bol)))
7461 (if (looking-at "[ \t]*$")
7462 (setq ind1 ind-empty)
7463 (skip-chars-forward " \t")
7464 (setq ind1 (current-column)))
7466 (throw 'exit (point-at-bol)))))))
7470 (error "Not in an item"))))
7472 (defun org-next-item ()
7473 "Move to the beginning of the next item in the current plain list.
7474 Error if not at a plain list, or if this is the last item in the list."
7476 (let (ind ind1 (pos (point)))
7477 (org-beginning-of-item)
7478 (setq ind (org-get-indentation))
7480 (setq ind1 (org-get-indentation))
7481 (unless (and (org-at-item-p) (= ind ind1))
7483 (error "On last item"))))
7485 (defun org-previous-item ()
7486 "Move to the beginning of the previous item in the current plain list.
7487 Error if not at a plain list, or if this is the first item in the list."
7489 (let (beg ind ind1 (pos (point)))
7490 (org-beginning-of-item)
7492 (setq ind (org-get-indentation))
7496 (beginning-of-line 0)
7497 (if (looking-at "[ \t]*$")
7499 (if (<= (setq ind1 (org-get-indentation)) ind)
7502 (if (or (not (org-at-item-p))
7505 (org-beginning-of-item))
7506 (error (goto-char pos)
7507 (error "On first item")))))
7509 (defun org-first-list-item-p ()
7510 "Is this heading the item in a plain list?"
7511 (unless (org-at-item-p)
7512 (error "Not at a plain list item"))
7513 (org-beginning-of-item)
7514 (= (point) (save-excursion (org-beginning-of-item-list))))
7516 (defun org-move-item-down ()
7517 "Move the plain list item at point down, i.e. swap with following item.
7518 Subitems (items with larger indentation) are considered part of the item,
7519 so this really moves item trees."
7521 (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
7522 (org-beginning-of-item)
7525 (setq ne-beg (org-back-over-empty-lines))
7528 (setq ind (org-get-indentation))
7531 (setq ind1 (org-get-indentation))
7532 (setq ne-end (org-back-over-empty-lines))
7535 (when (and (org-first-list-item-p) (< ne-end ne-beg))
7536 ;; include less whitespace
7539 (forward-line (- ne-beg ne-end))
7540 (setq beg (point))))
7542 (if (and (org-at-item-p) (= ind ind1))
7545 (org-back-over-empty-lines)
7546 (setq txt (buffer-substring beg end))
7548 (delete-region beg end))
7551 (goto-char pos) (org-skip-whitespace)
7552 (org-maybe-renumber-ordered-list))
7554 (error "Cannot move this item further down"))))
7556 (defun org-move-item-up (arg)
7557 "Move the plain list item at point up, i.e. swap with previous item.
7558 Subitems (items with larger indentation) are considered part of the item,
7559 so this really moves item trees."
7561 (let (beg beg0 end ind ind1 (pos (point)) txt
7562 ne-beg ne-ins ins-end)
7563 (org-beginning-of-item)
7565 (setq ind (org-get-indentation))
7567 (setq ne-beg (org-back-over-empty-lines))
7575 (beginning-of-line 0)
7576 (if (looking-at "[ \t]*$")
7577 (if org-empty-line-terminates-plain-lists
7580 (error "Cannot move this item further up"))
7582 (if (<= (setq ind1 (org-get-indentation)) ind)
7585 (org-beginning-of-item)
7586 (error (goto-char beg)
7587 (error "Cannot move this item further up")))
7588 (setq ind1 (org-get-indentation))
7589 (if (and (org-at-item-p) (= ind ind1))
7591 (setq ne-ins (org-back-over-empty-lines))
7592 (setq txt (buffer-substring beg end))
7594 (delete-region beg end))
7597 (setq ins-end (point))
7598 (goto-char pos) (org-skip-whitespace)
7600 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
7601 ;; Move whitespace back to beginning
7604 (let ((kill-whole-line t))
7605 (kill-line (- ne-ins ne-beg)) (point)))
7606 (insert (make-string (- ne-ins ne-beg) ?\n)))
7608 (org-maybe-renumber-ordered-list))
7610 (error "Cannot move this item further up"))))
7612 (defun org-maybe-renumber-ordered-list ()
7613 "Renumber the ordered list at point if setup allows it.
7614 This tests the user option `org-auto-renumber-ordered-lists' before
7615 doing the renumbering."
7617 (when (and org-auto-renumber-ordered-lists
7619 (if (match-beginning 3)
7620 (org-renumber-ordered-list 1)
7621 (org-fix-bullet-type))))
7623 (defun org-maybe-renumber-ordered-list-safe ()
7626 (org-maybe-renumber-ordered-list))
7629 (defun org-cycle-list-bullet (&optional which)
7630 "Cycle through the different itemize/enumerate bullets.
7631 This cycle the entire list level through the sequence:
7633 `-' -> `+' -> `*' -> `1.' -> `1)'
7635 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
7636 0 meand `-', 1 means `+' etc."
7639 (org-beginning-of-item-list)
7641 (beginning-of-line 1)
7642 (let ((current (match-string 0))
7643 (prevp (eq which 'previous))
7646 ((and (numberp which)
7647 (nth (1- which) '("-" "+" "*" "1." "1)"))))
7648 ((string-match "-" current) (if prevp "1)" "+"))
7649 ((string-match "\\+" current)
7650 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
7651 ((string-match "\\*" current) (if prevp "+" "1."))
7652 ((string-match "\\." current) (if prevp "*" "1)"))
7653 ((string-match ")" current) (if prevp "1." "-"))
7654 (t (error "This should not happen"))))
7655 (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
7656 (org-fix-bullet-type)
7657 (org-maybe-renumber-ordered-list))))
7659 (defun org-get-string-indentation (s)
7660 "What indentation has S due to SPACE and TAB at the beginning of the string?"
7661 (let ((n -1) (i 0) (w tab-width) c)
7663 (while (< (setq n (1+ n)) (length s))
7665 (cond ((= c ?\ ) (setq i (1+ i)))
7666 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
7667 (t (throw 'exit t)))))
7670 (defun org-renumber-ordered-list (arg)
7671 "Renumber an ordered plain list.
7672 Cursor needs to be in the first line of an item, the line that starts
7673 with something like \"1.\" or \"2)\"."
7675 (unless (and (org-at-item-p)
7676 (match-beginning 3))
7677 (error "This is not an ordered list"))
7678 (let ((line (org-current-line))
7679 (col (current-column))
7680 (ind (org-get-string-indentation
7681 (buffer-substring (point-at-bol) (match-beginning 3))))
7682 ;; (term (substring (match-string 3) -1))
7685 ;; find where this list begins
7686 (org-beginning-of-item-list)
7687 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
7688 (setq fmt (concat "%d" (match-string 1)))
7689 (beginning-of-line 0)
7690 ;; walk forward and replace these numbers
7694 (beginning-of-line 2)
7695 (if (eobp) (throw 'exit nil))
7696 (if (looking-at "[ \t]*$") (throw 'next nil))
7697 (skip-chars-forward " \t") (setq ind1 (current-column))
7698 (if (> ind1 ind) (throw 'next t))
7699 (if (< ind1 ind) (throw 'exit t))
7700 (if (not (org-at-item-p)) (throw 'exit nil))
7701 (delete-region (match-beginning 2) (match-end 2))
7702 (goto-char (match-beginning 2))
7703 (insert (format fmt (setq n (1+ n)))))))
7705 (move-to-column col)))
7707 (defun org-fix-bullet-type ()
7708 "Make sure all items in this list have the same bullet as the firsst item."
7710 (unless (org-at-item-p) (error "This is not a list"))
7711 (let ((line (org-current-line))
7712 (col (current-column))
7713 (ind (current-indentation))
7715 ;; find where this list begins
7716 (org-beginning-of-item-list)
7717 (beginning-of-line 1)
7718 ;; find out what the bullet type is
7719 (looking-at "[ \t]*\\(\\S-+\\)")
7720 (setq bullet (match-string 1))
7721 ;; walk forward and replace these numbers
7722 (beginning-of-line 0)
7726 (beginning-of-line 2)
7727 (if (eobp) (throw 'exit nil))
7728 (if (looking-at "[ \t]*$") (throw 'next nil))
7729 (skip-chars-forward " \t") (setq ind1 (current-column))
7730 (if (> ind1 ind) (throw 'next t))
7731 (if (< ind1 ind) (throw 'exit t))
7732 (if (not (org-at-item-p)) (throw 'exit nil))
7733 (skip-chars-forward " \t")
7734 (looking-at "\\S-+")
7735 (replace-match bullet))))
7737 (move-to-column col)
7738 (if (string-match "[0-9]" bullet)
7739 (org-renumber-ordered-list 1))))
7741 (defun org-beginning-of-item-list ()
7742 "Go to the beginning of the current item list.
7743 I.e. to the first item in this list."
7745 (org-beginning-of-item)
7746 (let ((pos (point-at-bol))
7747 (ind (org-get-indentation))
7749 ;; find where this list begins
7753 (beginning-of-line 0)
7754 (if (looking-at "[ \t]*$")
7755 (throw (if (bobp) 'exit 'next) t))
7756 (skip-chars-forward " \t") (setq ind1 (current-column))
7757 (if (or (< ind1 ind)
7759 (not (org-at-item-p)))
7762 (when (org-at-item-p) (setq pos (point-at-bol)))))))
7766 (defun org-end-of-item-list ()
7767 "Go to the end of the current item list.
7768 I.e. to the text after the last item."
7770 (org-beginning-of-item)
7771 (let ((pos (point-at-bol))
7772 (ind (org-get-indentation))
7774 ;; find where this list begins
7778 (beginning-of-line 2)
7779 (if (looking-at "[ \t]*$")
7780 (throw (if (eobp) 'exit 'next) t))
7781 (skip-chars-forward " \t") (setq ind1 (current-column))
7782 (if (or (< ind1 ind)
7784 (not (org-at-item-p)))
7787 (setq pos (point-at-bol))
7788 (throw 'exit t))))))
7792 (defvar org-last-indent-begin-marker (make-marker))
7793 (defvar org-last-indent-end-marker (make-marker))
7795 (defun org-outdent-item (arg)
7796 "Outdent a local list item."
7798 (org-indent-item (- arg)))
7800 (defun org-indent-item (arg)
7801 "Indent a local list item."
7803 (unless (org-at-item-p)
7804 (error "Not on an item"))
7806 (let (beg end ind ind1 tmp delta ind-down ind-up)
7807 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
7808 (setq beg org-last-indent-begin-marker
7809 end org-last-indent-end-marker)
7810 (org-beginning-of-item)
7811 (setq beg (move-marker org-last-indent-begin-marker (point)))
7813 (setq end (move-marker org-last-indent-end-marker (point))))
7815 (setq tmp (org-item-indent-positions)
7817 ind-down (nth 2 tmp)
7820 (if ind-down (- ind-down ind) 2)
7821 (if ind-up (- ind-up ind) -2)))
7822 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
7823 (while (< (point) end)
7824 (beginning-of-line 1)
7825 (skip-chars-forward " \t") (setq ind1 (current-column))
7826 (delete-region (point-at-bol) (point))
7827 (or (eolp) (indent-to-column (+ ind1 delta)))
7828 (beginning-of-line 2))))
7829 (org-fix-bullet-type)
7830 (org-maybe-renumber-ordered-list-safe)
7832 (beginning-of-line 0)
7833 (condition-case nil (org-beginning-of-item) (error nil))
7834 (org-maybe-renumber-ordered-list-safe)))
7836 (defun org-item-indent-positions ()
7837 "Return indentation for plain list items.
7838 This returns a list with three values: The current indentation, the
7839 parent indentation and the indentation a child should habe.
7840 Assumes cursor in item line."
7841 (let* ((bolpos (point-at-bol))
7842 (ind (org-get-indentation))
7843 ind-down ind-up pos)
7845 (org-beginning-of-item-list)
7846 (skip-chars-backward "\n\r \t")
7847 (when (org-in-item-p)
7848 (org-beginning-of-item)
7849 (setq ind-up (org-get-indentation))))
7853 ((and (condition-case nil (progn (org-previous-item) t)
7855 (or (forward-char 1) t)
7856 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
7857 (setq ind-down (org-get-indentation)))
7858 ((and (goto-char pos)
7860 (goto-char (match-end 0))
7861 (skip-chars-forward " \t")
7862 (setq ind-down (current-column)))))
7863 (list ind ind-up ind-down)))
7865 ;;; The orgstruct minor mode
7867 ;; Define a minor mode which can be used in other modes in order to
7868 ;; integrate the org-mode structure editing commands.
7870 ;; This is really a hack, because the org-mode structure commands use
7871 ;; keys which normally belong to the major mode. Here is how it
7872 ;; works: The minor mode defines all the keys necessary to operate the
7873 ;; structure commands, but wraps the commands into a function which
7874 ;; tests if the cursor is currently at a headline or a plain list
7875 ;; item. If that is the case, the structure command is used,
7876 ;; temporarily setting many Org-mode variables like regular
7877 ;; expressions for filling etc. However, when any of those keys is
7878 ;; used at a different location, function uses `key-binding' to look
7879 ;; up if the key has an associated command in another currently active
7880 ;; keymap (minor modes, major mode, global), and executes that
7881 ;; command. There might be problems if any of the keys is otherwise
7882 ;; used as a prefix key.
7884 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
7885 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
7886 ;; addresses this by checking explicitly for both bindings.
7888 (defvar orgstruct-mode-map (make-sparse-keymap)
7889 "Keymap for the minor `orgstruct-mode'.")
7891 (defvar org-local-vars nil
7892 "List of local variables, for use by `orgstruct-mode'")
7895 (define-minor-mode orgstruct-mode
7896 "Toggle the minor more `orgstruct-mode'.
7897 This mode is for using Org-mode structure commands in other modes.
7898 The following key behave as if Org-mode was active, if the cursor
7899 is on a headline, or on a plain list item (both in the definition
7902 M-up Move entry/item up
7903 M-down Move entry/item down
7906 M-S-up Move entry/item up
7907 M-S-down Move entry/item down
7908 M-S-left Promote subtree
7909 M-S-right Demote subtree
7910 M-q Fill paragraph and items like in Org-mode
7912 C-c - Cycle list bullet
7913 TAB Cycle item visibility
7914 M-RET Insert new heading/item
7915 S-M-RET Insert new TODO heading / Chekbox item
7916 C-c C-c Set tags / toggle checkbox"
7917 nil " OrgStruct" nil
7918 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7921 (defun turn-on-orgstruct ()
7922 "Unconditionally turn on `orgstruct-mode'."
7926 (defun turn-on-orgstruct++ ()
7927 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
7928 In addition to setting orgstruct-mode, this also exports all indentation and
7929 autofilling variables from org-mode into the buffer. Note that turning
7930 off orgstruct-mode will *not* remove these additional settings."
7936 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7937 (symbol-name (car x)))
7938 (setq var (car x) val (nth 1 x))
7939 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7942 (defun orgstruct-error ()
7943 "Error when there is no default binding for a structure key."
7945 (error "This key has no function outside structure elements"))
7947 (defun orgstruct-setup ()
7948 "Setup orgstruct keymaps."
7952 '([(meta up)] org-metaup)
7953 '([(meta down)] org-metadown)
7954 '([(meta left)] org-metaleft)
7955 '([(meta right)] org-metaright)
7956 '([(meta shift up)] org-shiftmetaup)
7957 '([(meta shift down)] org-shiftmetadown)
7958 '([(meta shift left)] org-shiftmetaleft)
7959 '([(meta shift right)] org-shiftmetaright)
7960 '([(shift up)] org-shiftup)
7961 '([(shift down)] org-shiftdown)
7962 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7963 '("\M-q" fill-paragraph)
7965 '("\C-c-" org-cycle-list-bullet)))
7967 (while (setq elt (pop bindings))
7968 (setq nfunc (1+ nfunc))
7969 (setq key (org-key (car elt))
7971 cmd (orgstruct-make-binding fun nfunc key))
7972 (org-defkey orgstruct-mode-map key cmd))
7974 ;; Special treatment needed for TAB and RET
7975 (org-defkey orgstruct-mode-map [(tab)]
7976 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7977 (org-defkey orgstruct-mode-map "\C-i"
7978 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7980 (org-defkey orgstruct-mode-map "\M-\C-m"
7981 (orgstruct-make-binding 'org-insert-heading 105
7982 "\M-\C-m" [(meta return)]))
7983 (org-defkey orgstruct-mode-map [(meta return)]
7984 (orgstruct-make-binding 'org-insert-heading 106
7985 [(meta return)] "\M-\C-m"))
7987 (org-defkey orgstruct-mode-map [(shift meta return)]
7988 (orgstruct-make-binding 'org-insert-todo-heading 107
7989 [(meta return)] "\M-\C-m"))
7991 (unless org-local-vars
7992 (setq org-local-vars (org-get-local-variables)))
7996 (defun orgstruct-make-binding (fun n &rest keys)
7997 "Create a function for binding in the structure minor mode.
7998 FUN is the command to call inside a table. N is used to create a unique
7999 command name. KEYS are keys that should be checked in for a command
8000 to execute outside of tables."
8003 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8005 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8006 "Outside of structure, run the binding of `"
8007 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8011 '(org-context-p 'headline 'item)
8012 (list 'org-run-like-in-org-mode (list 'quote fun))
8013 (list 'let '(orgstruct-mode)
8014 (list 'call-interactively
8017 (list 'key-binding k))
8019 '('orgstruct-error))))))))
8021 (defun org-context-p (&rest contexts)
8022 "Check if local context is and of CONTEXTS.
8023 Possible values in the list of contexts are `table', `headline', and `item'."
8024 (let ((pos (point)))
8025 (goto-char (point-at-bol))
8026 (prog1 (or (and (memq 'table contexts)
8027 (looking-at "[ \t]*|"))
8028 (and (memq 'headline contexts)
8029 (looking-at "\\*+"))
8030 (and (memq 'item contexts)
8031 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
8034 (defun org-get-local-variables ()
8035 "Return a list of all local variables in an org-mode buffer."
8037 (with-current-buffer (get-buffer-create "*Org tmp*")
8040 (setq varlist (buffer-local-variables)))
8041 (kill-buffer "*Org tmp*")
8048 (list (car x) (list 'quote (cdr x)))))
8050 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8051 (symbol-name (car x)))
8056 (defun org-run-like-in-org-mode (cmd)
8057 (unless org-local-vars
8058 (setq org-local-vars (org-get-local-variables)))
8059 (eval (list 'let org-local-vars
8060 (list 'call-interactively (list 'quote cmd)))))
8064 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
8066 (defun org-archive-subtree (&optional find-done)
8067 "Move the current subtree to the archive.
8068 The archive can be a certain top-level heading in the current file, or in
8069 a different file. The tree will be moved to that location, the subtree
8070 heading be marked DONE, and the current time will be added.
8072 When called with prefix argument FIND-DONE, find whole trees without any
8073 open TODO items and archive them (after getting confirmation from the user).
8074 If the cursor is not at a headline when this comand is called, try all level
8075 1 trees. If the cursor is on a headline, only try the direct children of
8079 (org-archive-all-done)
8080 ;; Save all relevant TODO keyword-relatex variables
8082 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
8083 (tr-org-todo-keywords-1 org-todo-keywords-1)
8084 (tr-org-todo-kwd-alist org-todo-kwd-alist)
8085 (tr-org-done-keywords org-done-keywords)
8086 (tr-org-todo-regexp org-todo-regexp)
8087 (tr-org-todo-line-regexp org-todo-line-regexp)
8088 (tr-org-odd-levels-only org-odd-levels-only)
8089 (this-buffer (current-buffer))
8090 (org-archive-location org-archive-location)
8091 (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
8092 ;; start of variables that will be used for saving context
8093 ;; The compiler complains about them - keep them anyway!
8094 (file (abbreviate-file-name (buffer-file-name)))
8095 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
8096 (time (format-time-string
8097 (substring (cdr org-time-stamp-formats) 1 -1)
8099 afile heading buffer level newfile-p
8100 category todo priority
8101 ;; start of variables that will be used for savind context
8104 ;; Try to find a local archive location
8108 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
8109 (if (and prop (string-match "\\S-" prop))
8110 (setq org-archive-location prop)
8111 (if (or (re-search-backward re nil t)
8112 (re-search-forward re nil t))
8113 (setq org-archive-location (match-string 1))))))
8115 (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
8117 (setq afile (format (match-string 1 org-archive-location)
8118 (file-name-nondirectory buffer-file-name))
8119 heading (match-string 2 org-archive-location)))
8120 (error "Invalid `org-archive-location'"))
8121 (if (> (length afile) 0)
8122 (setq newfile-p (not (file-exists-p afile))
8123 buffer (find-file-noselect afile))
8124 (setq buffer (current-buffer)))
8126 (error "Cannot access file \"%s\"" afile))
8127 (if (and (> (length heading) 0)
8128 (string-match "^\\*+" heading))
8129 (setq level (match-end 0))
8130 (setq heading nil level 0))
8132 (org-back-to-heading t)
8133 ;; Get context information that will be lost by moving the tree
8134 (org-refresh-category-properties)
8135 (setq category (org-get-category)
8136 todo (and (looking-at org-todo-line-regexp)
8138 priority (org-get-priority (if (match-end 3) (match-string 3) ""))
8139 ltags (org-get-tags)
8140 itags (org-delete-all ltags (org-get-tags-at)))
8141 (setq ltags (mapconcat 'identity ltags " ")
8142 itags (mapconcat 'identity itags " "))
8143 ;; We first only copy, in case something goes wrong
8144 ;; we need to protect this-command, to avoid kill-region sets it,
8145 ;; which would lead to duplication of subtrees
8146 (let (this-command) (org-copy-subtree))
8148 ;; Enforce org-mode for the archive buffer
8149 (if (not (org-mode-p))
8150 ;; Force the mode for future visits.
8151 (let ((org-insert-mode-line-in-empty-file t)
8152 (org-inhibit-startup t))
8153 (call-interactively 'org-mode)))
8155 (goto-char (point-max))
8156 (insert (format "\nArchived entries from file %s\n\n"
8157 (buffer-file-name this-buffer))))
8158 ;; Force the TODO keywords of the original buffer
8159 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
8160 (org-todo-keywords-1 tr-org-todo-keywords-1)
8161 (org-todo-kwd-alist tr-org-todo-kwd-alist)
8162 (org-done-keywords tr-org-done-keywords)
8163 (org-todo-regexp tr-org-todo-regexp)
8164 (org-todo-line-regexp tr-org-todo-line-regexp)
8165 (org-odd-levels-only
8166 (if (local-variable-p 'org-odd-levels-only (current-buffer))
8168 tr-org-odd-levels-only)))
8169 (goto-char (point-min))
8173 (if (re-search-forward
8174 (concat "^" (regexp-quote heading)
8175 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
8177 (goto-char (match-end 0))
8178 ;; Heading not found, just insert it at the end
8179 (goto-char (point-max))
8180 (or (bolp) (insert "\n"))
8181 (insert "\n" heading "\n")
8183 ;; Make the subtree visible
8185 (org-end-of-subtree t)
8186 (skip-chars-backward " \t\r\n")
8187 (and (looking-at "[ \t\r\n]*")
8188 (replace-match "\n\n")))
8189 ;; No specific heading, just go to end of file.
8190 (goto-char (point-max)) (insert "\n"))
8192 (org-paste-subtree (org-get-valid-level level 1))
8194 ;; Mark the entry as done
8195 (when (and org-archive-mark-done
8196 (looking-at org-todo-line-regexp)
8197 (or (not (match-end 2))
8198 (not (member (match-string 2) org-done-keywords))))
8199 (let (org-log-done org-todo-log-states)
8201 (car (or (member org-archive-mark-done org-done-keywords)
8202 org-done-keywords)))))
8204 ;; Add the context info
8205 (when org-archive-save-context-info
8206 (let ((l org-archive-save-context-info) e n v)
8207 (while (setq e (pop l))
8208 (when (and (setq v (symbol-value e))
8209 (stringp v) (string-match "\\S-" v))
8210 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
8211 (org-entry-put (point) n v)))))
8213 ;; Save and kill the buffer, if it is not the same buffer.
8214 (if (not (eq this-buffer buffer))
8215 (progn (save-buffer) (kill-buffer buffer)))))
8216 ;; Here we are back in the original buffer. Everything seems to have
8217 ;; worked. So now cut the tree and finish up.
8218 (let (this-command) (org-cut-subtree))
8219 (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
8220 (message "Subtree archived %s"
8221 (if (eq this-buffer buffer)
8222 (concat "under heading: " heading)
8223 (concat "in file: " (abbreviate-file-name afile)))))))
8225 (defun org-refresh-category-properties ()
8226 "Refresh category text properties in teh buffer."
8227 (let ((def-cat (cond
8228 ((null org-category)
8229 (if buffer-file-name
8230 (file-name-sans-extension
8231 (file-name-nondirectory buffer-file-name))
8233 ((symbolp org-category) (symbol-name org-category))
8235 beg end cat pos optionp)
8240 (goto-char (point-min))
8241 (put-text-property (point) (point-max) 'org-category def-cat)
8242 (while (re-search-forward
8243 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8244 (setq pos (match-end 0)
8245 optionp (equal (char-after (match-beginning 0)) ?#)
8246 cat (org-trim (match-string 2)))
8248 (setq beg (point-at-bol) end (point-max))
8249 (org-back-to-heading t)
8250 (setq beg (point) end (org-end-of-subtree t t)))
8251 (put-text-property beg end 'org-category cat)
8252 (goto-char pos)))))))
8254 (defun org-archive-all-done (&optional tag)
8255 "Archive sublevels of the current tree without open TODO items.
8256 If the cursor is not on a headline, try all level 1 trees. If
8257 it is on a headline, try all direct children.
8258 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
8259 (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
8260 (rea (concat ".*:" org-archive-tag ":"))
8261 (begm (make-marker))
8262 (endm (make-marker))
8263 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
8264 "Move subtree to archive (no open TODO items)? "))
8265 beg end (cntarch 0))
8266 (if (org-on-heading-p)
8268 (setq re1 (concat "^" (regexp-quote
8270 (1+ (- (match-end 0) (match-beginning 0) 1))
8273 (move-marker begm (point))
8274 (move-marker endm (org-end-of-subtree t)))
8276 (move-marker begm (point-min))
8277 (move-marker endm (point-max)))
8280 (while (re-search-forward re1 endm t)
8281 (setq beg (match-beginning 0)
8282 end (save-excursion (org-end-of-subtree t) (point)))
8284 (if (re-search-forward re end t)
8287 (if (and (or (not tag) (not (looking-at rea)))
8288 (y-or-n-p question))
8291 (org-toggle-tag org-archive-tag 'on)
8292 (org-archive-subtree))
8293 (setq cntarch (1+ cntarch)))
8295 (message "%d trees archived" cntarch)))
8297 (defun org-cycle-hide-drawers (state)
8298 "Re-hide all drawers after a visibility state change."
8299 (when (and (org-mode-p)
8300 (not (memq state '(overview folded))))
8302 (let* ((globalp (memq state '(contents all)))
8303 (beg (if globalp (point-min) (point)))
8304 (end (if globalp (point-max) (org-end-of-subtree t))))
8306 (while (re-search-forward org-drawer-regexp end t)
8307 (org-flag-drawer t))))))
8309 (defun org-flag-drawer (flag)
8311 (beginning-of-line 1)
8312 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
8313 (let ((b (match-end 0))
8314 (outline-regexp org-outline-regexp))
8315 (if (re-search-forward
8317 (save-excursion (outline-next-heading) (point)) t)
8318 (outline-flag-region b (point-at-eol) flag)
8319 (error ":END: line missing"))))))
8321 (defun org-cycle-hide-archived-subtrees (state)
8322 "Re-hide all archived subtrees after a visibility state change."
8323 (when (and (not org-cycle-open-archived-trees)
8324 (not (memq state '(overview folded))))
8326 (let* ((globalp (memq state '(contents all)))
8327 (beg (if globalp (point-min) (point)))
8328 (end (if globalp (point-max) (org-end-of-subtree t))))
8329 (org-hide-archived-subtrees beg end)
8331 (if (looking-at (concat ".*:" org-archive-tag ":"))
8332 (message "%s" (substitute-command-keys
8333 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
8335 (defun org-force-cycle-archived ()
8336 "Cycle subtree even if it is archived."
8338 (setq this-command 'org-cycle)
8339 (let ((org-cycle-open-archived-trees t))
8340 (call-interactively 'org-cycle)))
8342 (defun org-hide-archived-subtrees (beg end)
8343 "Re-hide all archived subtrees after a visibility state change."
8345 (let* ((re (concat ":" org-archive-tag ":")))
8347 (while (re-search-forward re end t)
8348 (and (org-on-heading-p) (hide-subtree))
8349 (org-end-of-subtree t)))))
8351 (defun org-toggle-tag (tag &optional onoff)
8352 "Toggle the tag TAG for the current line.
8353 If ONOFF is `on' or `off', don't toggle but set to this state."
8354 (unless (org-on-heading-p t) (error "Not on headling"))
8358 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
8361 (setq current (match-string 1))
8364 (setq current (nreverse (org-split-string current ":")))
8368 (or (member tag current) (push tag current)))
8370 (or (not (member tag current)) (setq current (delete tag current))))
8371 (t (if (member tag current)
8372 (setq current (delete tag current))
8374 (push tag current))))
8378 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
8379 (org-set-tags nil t))
8380 (delete-horizontal-space))
8381 (run-hooks 'org-after-tags-change-hook))
8384 (defun org-toggle-archive-tag (&optional arg)
8385 "Toggle the archive tag for the current headline.
8386 With prefix ARG, check all children of current headline and offer tagging
8387 the children that do not contain any open TODO items."
8390 (org-archive-all-done 'tag)
8393 (org-back-to-heading t)
8394 (setq set (org-toggle-tag org-archive-tag))
8395 (when set (hide-subtree)))
8396 (and set (beginning-of-line 1))
8397 (message "Subtree %s" (if set "archived" "unarchived")))))
8402 ;;; The table editor
8404 ;; Watch out: Here we are talking about two different kind of tables.
8405 ;; Most of the code is for the tables created with the Org-mode table editor.
8406 ;; Sometimes, we talk about tables created and edited with the table.el
8407 ;; Emacs package. We call the former org-type tables, and the latter
8408 ;; table.el-type tables.
8410 (defun org-before-change-function (beg end)
8411 "Every change indicates that a table might need an update."
8412 (setq org-table-may-need-update t))
8414 (defconst org-table-line-regexp "^[ \t]*|"
8415 "Detects an org-type table line.")
8416 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
8417 "Detects an org-type table line.")
8418 (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
8419 "Detects a table line marked for automatic recalculation.")
8420 (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
8421 "Detects a table line marked for automatic recalculation.")
8422 (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
8423 "Detects a table line marked for automatic recalculation.")
8424 (defconst org-table-hline-regexp "^[ \t]*|-"
8425 "Detects an org-type table hline.")
8426 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
8427 "Detects a table-type table hline.")
8428 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
8429 "Detects an org-type or table-type table.")
8430 (defconst org-table-border-regexp "^[ \t]*[^| \t]"
8431 "Searching from within a table (any type) this finds the first line
8432 outside the table.")
8433 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
8434 "Searching from within a table (any type) this finds the first line
8435 outside the table.")
8437 (defvar org-table-last-highlighted-reference nil)
8438 (defvar org-table-formula-history nil)
8440 (defvar org-table-column-names nil
8441 "Alist with column names, derived from the `!' line.")
8442 (defvar org-table-column-name-regexp nil
8443 "Regular expression matching the current column names.")
8444 (defvar org-table-local-parameters nil
8445 "Alist with parameter names, derived from the `$' line.")
8446 (defvar org-table-named-field-locations nil
8447 "Alist with locations of named fields.")
8449 (defvar org-table-current-line-types nil
8450 "Table row types, non-nil only for the duration of a comand.")
8451 (defvar org-table-current-begin-line nil
8452 "Table begin line, non-nil only for the duration of a comand.")
8453 (defvar org-table-current-begin-pos nil
8454 "Table begin position, non-nil only for the duration of a comand.")
8455 (defvar org-table-dlines nil
8456 "Vector of data line line numbers in the current table.")
8457 (defvar org-table-hlines nil
8458 "Vector of hline line numbers in the current table.")
8460 (defconst org-table-range-regexp
8461 "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
8463 "Regular expression for matching ranges in formulas.")
8465 (defconst org-table-range-regexp2
8467 "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
8469 "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
8470 "Match a range for reference display.")
8472 (defconst org-table-translate-regexp
8473 (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
8474 "Match a reference that needs translation, for reference display.")
8476 (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
8478 (defun org-table-create-with-table.el ()
8479 "Use the table.el package to insert a new table.
8480 If there is already a table at point, convert between Org-mode tables
8481 and table.el tables."
8485 ((org-at-table.el-p)
8486 (if (y-or-n-p "Convert table to Org-mode table? ")
8487 (org-table-convert)))
8489 (if (y-or-n-p "Convert table to table.el table? ")
8490 (org-table-convert)))
8491 (t (call-interactively 'table-insert))))
8493 (defun org-table-create-or-convert-from-region (arg)
8494 "Convert region to table, or create an empty table.
8495 If there is an active region, convert it to a table, using the function
8496 `org-table-convert-region'. See the documentation of that function
8497 to learn how the prefix argument is interpreted to determine the field
8499 If there is no such region, create an empty table with `org-table-create'."
8501 (if (org-region-active-p)
8502 (org-table-convert-region (region-beginning) (region-end) arg)
8503 (org-table-create arg)))
8505 (defun org-table-create (&optional size)
8506 "Query for a size and insert a table skeleton.
8507 SIZE is a string Columns x Rows like for example \"3x2\"."
8510 (setq size (read-string
8511 (concat "Table size Columns x Rows [e.g. "
8512 org-table-default-size "]: ")
8513 "" nil org-table-default-size)))
8515 (let* ((pos (point))
8516 (indent (make-string (current-column) ?\ ))
8517 (split (org-split-string size " *x *"))
8518 (rows (string-to-number (nth 1 split)))
8519 (columns (string-to-number (car split)))
8520 (line (concat (apply 'concat indent "|" (make-list columns " |"))
8522 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
8523 (point-at-bol) (point)))
8524 (beginning-of-line 1)
8526 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
8527 (dotimes (i rows) (insert line))
8530 ;; Insert a hline after the first row.
8537 (defun org-table-convert-region (beg0 end0 &optional separator)
8538 "Convert region to a table.
8539 The region goes from BEG0 to END0, but these borders will be moved
8540 slightly, to make sure a beginning of line in the first line is included.
8542 SEPARATOR specifies the field separator in the lines. It can have the
8545 '(4) Use the comma as a field separator
8546 '(16) Use a TAB as field separator
8547 integer When a number, use that many spaces as field separator
8548 nil When nil, the command tries to be smart and figure out the
8549 separator in the following way:
8550 - when each line contains a TAB, assume TAB-separated material
8551 - when each line contains a comme, assume CSV material
8552 - else, assume one or more SPACE charcters as separator."
8554 (let* ((beg (min beg0 end0))
8555 (end (max beg0 end0))
8558 (beginning-of-line 1)
8559 (setq beg (move-marker (make-marker) (point)))
8561 (if (bolp) (backward-char 1) (end-of-line 1))
8562 (setq end (move-marker (make-marker) (point)))
8563 ;; Get the right field separator
8568 ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
8569 ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
8572 ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
8573 ((equal separator '(16)) "^\\|\t")
8574 ((integerp separator)
8575 (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
8576 (t (error "This should not happen"))))
8578 (while (re-search-forward re end t)
8579 (replace-match "| " t t))
8584 (defun org-table-import (file arg)
8585 "Import FILE as a table.
8586 The file is assumed to be tab-separated. Such files can be produced by most
8587 spreadsheet and database applications. If no tabs (at least one per line)
8588 are found, lines will be split on whitespace into fields."
8589 (interactive "f\nP")
8590 (or (bolp) (newline))
8593 (insert-file-contents file)
8594 (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
8596 (defun org-table-export ()
8597 "Export table as a tab-separated file.
8598 Such a file can be imported into a spreadsheet program like Excel."
8600 (let* ((beg (org-table-begin))
8601 (end (org-table-end))
8602 (table (buffer-substring beg end))
8603 (file (read-file-name "Export table to: "))
8605 (unless (or (not (file-exists-p file))
8606 (y-or-n-p (format "Overwrite file %s? " file)))
8608 (with-current-buffer (find-file-noselect file)
8609 (setq buf (current-buffer))
8613 (goto-char (point-min))
8614 (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
8615 (replace-match "" t t)
8617 (goto-char (point-min))
8618 (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
8619 (replace-match "" t t)
8620 (goto-char (min (1+ (point)) (point-max))))
8621 (goto-char (point-min))
8622 (while (re-search-forward "^-[-+]*$" nil t)
8624 (if (looking-at "\n")
8626 (goto-char (point-min))
8627 (while (re-search-forward "[ \t]*|[ \t]*" nil t)
8628 (replace-match "\t" t t))
8632 (defvar org-table-aligned-begin-marker (make-marker)
8633 "Marker at the beginning of the table last aligned.
8634 Used to check if cursor still is in that table, to minimize realignment.")
8635 (defvar org-table-aligned-end-marker (make-marker)
8636 "Marker at the end of the table last aligned.
8637 Used to check if cursor still is in that table, to minimize realignment.")
8638 (defvar org-table-last-alignment nil
8639 "List of flags for flushright alignment, from the last re-alignment.
8640 This is being used to correctly align a single field after TAB or RET.")
8641 (defvar org-table-last-column-widths nil
8642 "List of max width of fields in each column.
8643 This is being used to correctly align a single field after TAB or RET.")
8644 (defvar org-table-overlay-coordinates nil
8645 "Overlay coordinates after each align of a table.")
8646 (make-variable-buffer-local 'org-table-overlay-coordinates)
8648 (defvar org-last-recalc-line nil)
8649 (defconst org-narrow-column-arrow "=>"
8650 "Used as display property in narrowed table columns.")
8652 (defun org-table-align ()
8653 "Align the table at point by aligning all vertical bars."
8657 (beg (org-table-begin))
8658 (end (org-table-end))
8659 ;; Current cursor position
8660 (linepos (org-current-line))
8661 (colpos (org-table-current-column))
8662 (winstart (window-start))
8663 (winstartline (org-current-line (min winstart (1- (point-max)))))
8664 lines (new "") lengths l typenums ty fields maxfields i
8666 (indent "") cnt frac
8672 (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
8674 (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
8675 emptystrings links dates emph narrow fmax f1 len c e)
8677 (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
8678 ;; Check if we have links or dates
8680 (setq links (re-search-forward org-bracket-link-regexp end t))
8682 (setq emph (and org-hide-emphasis-markers
8683 (re-search-forward org-emph-re end t)))
8685 (setq dates (and org-display-custom-times
8686 (re-search-forward org-ts-regexp-both end t)))
8687 ;; Make sure the link properties are right
8688 (when links (goto-char beg) (while (org-activate-bracket-links end)))
8689 ;; Make sure the date properties are right
8690 (when dates (goto-char beg) (while (org-activate-dates end)))
8691 (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
8693 ;; Check if we are narrowing any columns
8695 (setq narrow (and org-format-transports-properties-p
8696 (re-search-forward "<[0-9]+>" end t)))
8698 (setq lines (org-split-string
8699 (buffer-substring beg end) "\n"))
8700 ;; Store the indentation of the first line
8701 (if (string-match "^ *" (car lines))
8702 (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
8703 ;; Mark the hlines by setting the corresponding element to nil
8704 ;; At the same time, we remove trailing space.
8705 (setq lines (mapcar (lambda (l)
8706 (if (string-match "^ *|-" l)
8708 (if (string-match "[ \t]+$" l)
8709 (substring l 0 (match-beginning 0))
8712 ;; Get the data fields by splitting the lines.
8713 (setq fields (mapcar
8715 (org-split-string l " *| *"))
8716 (delq nil (copy-sequence lines))))
8717 ;; How many fields in the longest line?
8719 (setq maxfields (apply 'max (mapcar 'length fields)))
8721 (kill-region beg end)
8722 (org-table-create org-table-default-size)
8723 (error "Empty table - created default table")))
8724 ;; A list of empty strings to fill any short rows on output
8725 (setq emptystrings (make-list maxfields ""))
8726 ;; Check for special formatting.
8728 (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
8729 (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
8730 ;; Check if there is an explicit width specified
8732 (setq c column fmax nil)
8735 (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
8736 (setq fmax (string-to-number (match-string 1 e)) c nil)))
8737 ;; Find fields that are wider than fmax, and shorten them
8739 (loop for xx in column do
8740 (when (and (stringp xx)
8741 (> (org-string-width xx) fmax))
8742 (org-add-props xx nil
8744 (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
8745 (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
8747 (error "Cannot narrow field starting with wide link \"%s\""
8748 (match-string 0 xx)))
8749 (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
8750 (add-text-properties (- f1 2) f1
8751 (list 'display org-narrow-column-arrow)
8753 ;; Get the maximum width for each column
8754 (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
8755 ;; Get the fraction of numbers, to decide about alignment of the column
8756 (setq cnt 0 frac 0.0)
8757 (loop for x in column do
8760 (setq frac ( / (+ (* frac cnt)
8761 (if (string-match org-table-number-regexp x) 1 0))
8762 (setq cnt (1+ cnt))))))
8763 (push (>= frac org-table-number-fraction) typenums))
8764 (setq lengths (nreverse lengths) typenums (nreverse typenums))
8766 ;; Store the alignment of this table, for later editing of single fields
8767 (setq org-table-last-alignment typenums
8768 org-table-last-column-widths lengths)
8770 ;; With invisible characters, `format' does not get the field width right
8771 ;; So we need to make these fields wide by hand.
8772 (when (or links emph)
8773 (loop for i from 0 upto (1- maxfields) do
8774 (setq len (nth i lengths))
8775 (loop for j from 0 upto (1- (length fields)) do
8776 (setq c (nthcdr i (car (nthcdr j fields))))
8777 (if (and (stringp (car c))
8778 (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
8779 ; (string-match org-bracket-link-regexp (car c))
8780 (< (org-string-width (car c)) len))
8781 (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
8783 ;; Compute the formats needed for output of the table
8784 (setq rfmt (concat indent "|") hfmt (concat indent "|"))
8785 (while (setq l (pop lengths))
8786 (setq ty (if (pop typenums) "" "-")) ; number types flushright
8787 (setq rfmt (concat rfmt (format rfmt1 ty l))
8788 hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
8789 (setq rfmt (concat rfmt "\n")
8790 hfmt (concat (substring hfmt 0 -1) "|\n"))
8792 (setq new (mapconcat
8794 (if l (apply 'format rfmt
8795 (append (pop fields) emptystrings))
8798 ;; Replace the old one
8799 (delete-region beg end)
8800 (move-marker end nil)
8801 (move-marker org-table-aligned-begin-marker (point))
8803 (move-marker org-table-aligned-end-marker (point))
8804 (when (and orgtbl-mode (not (org-mode-p)))
8805 (goto-char org-table-aligned-begin-marker)
8806 (while (org-hide-wide-columns org-table-aligned-end-marker)))
8807 ;; Try to move to the old location
8808 (goto-line winstartline)
8809 (setq winstart (point-at-bol))
8811 (set-window-start (selected-window) winstart 'noforce)
8812 (org-table-goto-column colpos)
8813 (and org-table-overlay-coordinates (org-table-overlay-coordinates))
8814 (setq org-table-may-need-update nil)
8817 (defun org-string-width (s)
8818 "Compute width of string, ignoring invisible characters.
8819 This ignores character with invisibility property `org-link', and also
8820 characters with property `org-cwidth', because these will become invisible
8821 upon the next fontification round."
8823 (when (or (eq t buffer-invisibility-spec)
8824 (assq 'org-link buffer-invisibility-spec))
8825 (while (setq b (text-property-any 0 (length s)
8826 'invisible 'org-link s))
8827 (setq s (concat (substring s 0 b)
8828 (substring s (or (next-single-property-change
8829 b 'invisible s) (length s)))))))
8830 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
8831 (setq s (concat (substring s 0 b)
8832 (substring s (or (next-single-property-change
8833 b 'org-cwidth s) (length s))))))
8834 (setq l (string-width s) b -1)
8835 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
8836 (setq l (- l (get-text-property b 'org-dwidth-n s))))
8839 (defun org-table-begin (&optional table-type)
8840 "Find the beginning of the table and return its position.
8841 With argument TABLE-TYPE, go to the beginning of a table.el-type table."
8843 (if (not (re-search-backward
8844 (if table-type org-table-any-border-regexp
8845 org-table-border-regexp)
8847 (progn (goto-char (point-min)) (point))
8848 (goto-char (match-beginning 0))
8849 (beginning-of-line 2)
8852 (defun org-table-end (&optional table-type)
8853 "Find the end of the table and return its position.
8854 With argument TABLE-TYPE, go to the end of a table.el-type table."
8856 (if (not (re-search-forward
8857 (if table-type org-table-any-border-regexp
8858 org-table-border-regexp)
8860 (goto-char (point-max))
8861 (goto-char (match-beginning 0)))
8864 (defun org-table-justify-field-maybe (&optional new)
8865 "Justify the current field, text to left, number to right.
8866 Optional argument NEW may specify text to replace the current field content."
8868 ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
8869 ((org-at-table-hline-p))
8871 (or (not (equal (marker-buffer org-table-aligned-begin-marker)
8873 (< (point) org-table-aligned-begin-marker)
8874 (>= (point) org-table-aligned-end-marker)))
8875 ;; This is not the same table, force a full re-align
8876 (setq org-table-may-need-update t))
8877 (t ;; realign the current field, based on previous full realign
8878 (let* ((pos (point)) s
8879 (col (org-table-current-column))
8880 (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
8883 (skip-chars-backward "^|\n")
8884 (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
8886 (setq s (match-string 1)
8888 l (max 1 (- (match-end 0) (match-beginning 0) 3))
8889 e (not (= (match-beginning 2) (match-end 2))))
8890 (setq f (format (if num " %%%ds %s" " %%-%ds %s")
8891 l (if e "|" (setq org-table-may-need-update t) ""))
8894 (if (<= (length new) l) ;; FIXME: length -> str-width?
8895 (setq n (format f new))
8896 (setq n (concat new "|") org-table-may-need-update t)))
8898 (let (org-table-may-need-update)
8899 (replace-match n t t))))
8900 (setq org-table-may-need-update t))
8901 (goto-char pos))))))
8903 (defun org-table-next-field ()
8904 "Go to the next field in the current table, creating new lines as needed.
8905 Before doing so, re-align the table if necessary."
8907 (org-table-maybe-eval-formula)
8908 (org-table-maybe-recalculate-line)
8909 (if (and org-table-automatic-realign
8910 org-table-may-need-update)
8912 (let ((end (org-table-end)))
8913 (if (org-at-table-hline-p)
8917 (re-search-forward "|" end)
8918 (if (looking-at "[ \t]*$")
8919 (re-search-forward "|" end))
8920 (if (and (looking-at "-")
8921 org-table-tab-jumps-over-hlines
8922 (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
8923 (goto-char (match-beginning 1)))
8924 (if (looking-at "-")
8926 (beginning-of-line 0)
8927 (org-table-insert-row 'below))
8928 (if (looking-at " ") (forward-char 1))))
8930 (org-table-insert-row 'below)))))
8932 (defun org-table-previous-field ()
8933 "Go to the previous field in the table.
8934 Before doing so, re-align the table if necessary."
8936 (org-table-justify-field-maybe)
8937 (org-table-maybe-recalculate-line)
8938 (if (and org-table-automatic-realign
8939 org-table-may-need-update)
8941 (if (org-at-table-hline-p)
8943 (re-search-backward "|" (org-table-begin))
8944 (re-search-backward "|" (org-table-begin))
8945 (while (looking-at "|\\(-\\|[ \t]*$\\)")
8946 (re-search-backward "|" (org-table-begin)))
8947 (if (looking-at "| ?")
8948 (goto-char (match-end 0))))
8950 (defun org-table-next-row ()
8951 "Go to the next row (same column) in the current table.
8952 Before doing so, re-align the table if necessary."
8954 (org-table-maybe-eval-formula)
8955 (org-table-maybe-recalculate-line)
8956 (if (or (looking-at "[ \t]*$")
8957 (save-excursion (skip-chars-backward " \t") (bolp)))
8959 (if (and org-table-automatic-realign
8960 org-table-may-need-update)
8962 (let ((col (org-table-current-column)))
8963 (beginning-of-line 2)
8964 (if (or (not (org-at-table-p))
8965 (org-at-table-hline-p))
8967 (beginning-of-line 0)
8968 (org-table-insert-row 'below)))
8969 (org-table-goto-column col)
8970 (skip-chars-backward "^|\n\r")
8971 (if (looking-at " ") (forward-char 1)))))
8973 (defun org-table-copy-down (n)
8974 "Copy a field down in the current column.
8975 If the field at the cursor is empty, copy into it the content of the nearest
8976 non-empty field above. With argument N, use the Nth non-empty field.
8977 If the current field is not empty, it is copied down to the next row, and
8978 the cursor is moved with it. Therefore, repeating this command causes the
8979 column to be filled row-by-row.
8980 If the variable `org-table-copy-increment' is non-nil and the field is an
8981 integer or a timestamp, it will be incremented while copying. In the case of
8982 a timestamp, if the cursor is on the year, change the year. If it is on the
8983 month or the day, change that. Point will stay on the current date field
8984 in order to easily repeat the interval."
8986 (let* ((colpos (org-table-current-column))
8987 (col (current-column))
8988 (field (org-table-get-field))
8989 (non-empty (string-match "[^ \t]" field))
8990 (beg (org-table-begin))
8992 (org-table-check-inside-data-field)
8995 (setq txt (org-trim field))
8996 (org-table-next-row)
8997 (org-table-blank-field))
9001 (while (progn (beginning-of-line 1)
9002 (re-search-backward org-table-dataline-regexp
9004 (org-table-goto-column colpos t)
9005 (if (and (looking-at
9006 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
9007 (= (setq n (1- n)) 0))
9008 (throw 'exit (match-string 1))))))))
9011 (if (and org-table-copy-increment
9012 (string-match "^[0-9]+$" txt))
9013 (setq txt (format "%d" (+ (string-to-number txt) 1))))
9015 (move-to-column col)
9016 (if (and org-table-copy-increment (org-at-timestamp-p t))
9017 (org-timestamp-up 1)
9018 (org-table-maybe-recalculate-line))
9020 (move-to-column col))
9021 (error "No non-empty field found"))))
9023 (defun org-table-check-inside-data-field ()
9024 "Is point inside a table data field?
9025 I.e. not on a hline or before the first or after the last column?
9026 This actually throws an error, so it aborts the current command."
9027 (if (or (not (org-at-table-p))
9028 (= (org-table-current-column) 0)
9029 (org-at-table-hline-p)
9030 (looking-at "[ \t]*$"))
9031 (error "Not in table data field")))
9033 (defvar org-table-clip nil
9034 "Clipboard for table regions.")
9036 (defun org-table-blank-field ()
9037 "Blank the current table field or active region."
9039 (org-table-check-inside-data-field)
9040 (if (and (interactive-p) (org-region-active-p))
9041 (let (org-table-clip)
9042 (org-table-cut-region (region-beginning) (region-end)))
9043 (skip-chars-backward "^|")
9045 (if (looking-at "|[^|\n]+")
9046 (let* ((pos (match-beginning 0))
9047 (match (match-string 0))
9048 (len (org-string-width match)))
9049 (replace-match (concat "|" (make-string (1- len) ?\ )))
9050 (goto-char (+ 2 pos))
9051 (substring match 1)))))
9053 (defun org-table-get-field (&optional n replace)
9054 "Return the value of the field in column N of current row.
9055 N defaults to current field.
9056 If REPLACE is a string, replace field with this value. The return value
9057 is always the old value."
9058 (and n (org-table-goto-column n))
9059 (skip-chars-backward "^|\n")
9061 (if (looking-at "|[^|\r\n]*")
9062 (let* ((pos (match-beginning 0))
9063 (val (buffer-substring (1+ pos) (match-end 0))))
9065 (replace-match (concat "|" replace) t t))
9066 (goto-char (min (point-at-eol) (+ 2 pos)))
9068 (forward-char 1) ""))
9070 (defun org-table-field-info (arg)
9071 "Show info about the current field, and highlight any reference at point."
9073 (org-table-get-specials)
9075 (let* ((pos (point))
9076 (col (org-table-current-column))
9077 (cname (car (rassoc (int-to-string col) org-table-column-names)))
9078 (name (car (rassoc (list (org-current-line) col)
9079 org-table-named-field-locations)))
9080 (eql (org-table-get-stored-formulas))
9081 (dline (org-table-current-dline))
9082 (ref (format "@%d$%d" dline col))
9083 (ref1 (org-table-convert-refs-to-an ref))
9084 (fequation (or (assoc name eql) (assoc ref eql)))
9085 (cequation (assoc (int-to-string col) eql))
9086 (eqn (or fequation cequation)))
9089 (org-table-show-reference 'local)
9091 (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
9093 (if cname (concat " or $" cname) "")
9095 (if name (concat " or $" name) "")
9096 ;; FIXME: formula info not correct if special table line
9098 (concat ", formula: "
9099 (org-table-formula-to-user
9101 (if (string-match "^[$@]"(car eqn)) "" "$")
9102 (car eqn) "=" (cdr eqn))))
9105 (defun org-table-current-column ()
9106 "Find out which column we are in.
9107 When called interactively, column is also displayed in echo area."
9109 (if (interactive-p) (org-table-check-inside-data-field))
9111 (let ((cnt 0) (pos (point)))
9112 (beginning-of-line 1)
9113 (while (search-forward "|" pos t)
9114 (setq cnt (1+ cnt)))
9115 (if (interactive-p) (message "This is table column %d" cnt))
9118 (defun org-table-current-dline ()
9119 "Find out what table data line we are in.
9120 Only datalins count for this."
9122 (if (interactive-p) (org-table-check-inside-data-field))
9124 (let ((cnt 0) (pos (point)))
9125 (goto-char (org-table-begin))
9126 (while (<= (point) pos)
9127 (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
9128 (beginning-of-line 2))
9129 (if (interactive-p) (message "This is table line %d" cnt))
9132 (defun org-table-goto-column (n &optional on-delim force)
9133 "Move the cursor to the Nth column in the current table line.
9134 With optional argument ON-DELIM, stop with point before the left delimiter
9136 If there are less than N fields, just go to after the last delimiter.
9137 However, when FORCE is non-nil, create new columns if necessary."
9139 (let ((pos (point-at-eol)))
9140 (beginning-of-line 1)
9142 (while (and (> (setq n (1- n)) -1)
9143 (or (search-forward "|" pos t)
9145 (progn (end-of-line 1)
9146 (skip-chars-backward "^|")
9148 ; (backward-char 2) t)))))
9149 (when (and force (not (looking-at ".*|")))
9150 (save-excursion (end-of-line 1) (insert " | ")))
9153 (if (looking-at " ") (forward-char 1))))))
9155 (defun org-at-table-p (&optional table-type)
9156 "Return t if the cursor is inside an org-type table.
9157 If TABLE-TYPE is non-nil, also check for table.el-type tables."
9158 (if org-enable-table-editor
9160 (beginning-of-line 1)
9161 (looking-at (if table-type org-table-any-line-regexp
9162 org-table-line-regexp)))
9165 (defun org-at-table.el-p ()
9166 "Return t if and only if we are at a table.el table."
9167 (and (org-at-table-p 'any)
9169 (goto-char (org-table-begin 'any))
9170 (looking-at org-table1-hline-regexp))))
9172 (defun org-table-recognize-table.el ()
9173 "If there is a table.el table nearby, recognize it and move into it."
9174 (if org-table-tab-recognizes-table.el
9175 (if (org-at-table.el-p)
9177 (beginning-of-line 1)
9178 (if (looking-at org-table-dataline-regexp)
9180 (if (looking-at org-table1-hline-regexp)
9182 (beginning-of-line 2)
9183 (if (looking-at org-table-any-border-regexp)
9184 (beginning-of-line -1)))))
9185 (if (re-search-forward "|" (org-table-end t) t)
9188 (if (table--at-cell-p (point))
9190 (message "recognizing table.el table...")
9191 (table-recognize-table)
9192 (message "recognizing table.el table...done")))
9193 (error "This should not happen..."))
9198 (defun org-at-table-hline-p ()
9199 "Return t if the cursor is inside a hline in a table."
9200 (if org-enable-table-editor
9202 (beginning-of-line 1)
9203 (looking-at org-table-hline-regexp))
9206 (defun org-table-insert-column ()
9207 "Insert a new column into the table."
9209 (if (not (org-at-table-p))
9210 (error "Not at a table"))
9211 (org-table-find-dataline)
9212 (let* ((col (max 1 (org-table-current-column)))
9213 (beg (org-table-begin))
9214 (end (org-table-end))
9215 ;; Current cursor position
9216 (linepos (org-current-line))
9219 (while (< (point) end)
9220 (if (org-at-table-hline-p)
9222 (org-table-goto-column col t)
9224 (beginning-of-line 2))
9225 (move-marker end nil)
9227 (org-table-goto-column colpos)
9229 (org-table-fix-formulas "$" nil (1- col) 1)))
9231 (defun org-table-find-dataline ()
9232 "Find a dataline in the current table, which is needed for column commands."
9233 (if (and (org-at-table-p)
9234 (not (org-at-table-hline-p)))
9236 (let ((col (current-column))
9237 (end (org-table-end)))
9238 (move-to-column col)
9239 (while (and (< (point) end)
9240 (or (not (= (current-column) col))
9241 (org-at-table-hline-p)))
9242 (beginning-of-line 2)
9243 (move-to-column col))
9244 (if (and (org-at-table-p)
9245 (not (org-at-table-hline-p)))
9248 "Please position cursor in a data line for column operations")))))
9250 (defun org-table-delete-column ()
9251 "Delete a column from the table."
9253 (if (not (org-at-table-p))
9254 (error "Not at a table"))
9255 (org-table-find-dataline)
9256 (org-table-check-inside-data-field)
9257 (let* ((col (org-table-current-column))
9258 (beg (org-table-begin))
9259 (end (org-table-end))
9260 ;; Current cursor position
9261 (linepos (org-current-line))
9264 (while (< (point) end)
9265 (if (org-at-table-hline-p)
9267 (org-table-goto-column col t)
9268 (and (looking-at "|[^|\n]+|")
9269 (replace-match "|")))
9270 (beginning-of-line 2))
9271 (move-marker end nil)
9273 (org-table-goto-column colpos)
9275 (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
9278 (defun org-table-move-column-right ()
9279 "Move column to the right."
9281 (org-table-move-column nil))
9282 (defun org-table-move-column-left ()
9283 "Move column to the left."
9285 (org-table-move-column 'left))
9287 (defun org-table-move-column (&optional left)
9288 "Move the current column to the right. With arg LEFT, move to the left."
9290 (if (not (org-at-table-p))
9291 (error "Not at a table"))
9292 (org-table-find-dataline)
9293 (org-table-check-inside-data-field)
9294 (let* ((col (org-table-current-column))
9295 (col1 (if left (1- col) col))
9296 (beg (org-table-begin))
9297 (end (org-table-end))
9298 ;; Current cursor position
9299 (linepos (org-current-line))
9300 (colpos (if left (1- col) (1+ col))))
9301 (if (and left (= col 1))
9302 (error "Cannot move column further left"))
9303 (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
9304 (error "Cannot move column further right"))
9306 (while (< (point) end)
9307 (if (org-at-table-hline-p)
9309 (org-table-goto-column col1 t)
9310 (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
9311 (replace-match "|\\2|\\1|")))
9312 (beginning-of-line 2))
9313 (move-marker end nil)
9315 (org-table-goto-column colpos)
9317 (org-table-fix-formulas
9318 "$" (list (cons (number-to-string col) (number-to-string colpos))
9319 (cons (number-to-string colpos) (number-to-string col))))))
9321 (defun org-table-move-row-down ()
9322 "Move table row down."
9324 (org-table-move-row nil))
9325 (defun org-table-move-row-up ()
9326 "Move table row up."
9328 (org-table-move-row 'up))
9330 (defun org-table-move-row (&optional up)
9331 "Move the current table line down. With arg UP, move it up."
9333 (let* ((col (current-column))
9335 (hline1p (save-excursion (beginning-of-line 1)
9336 (looking-at org-table-hline-regexp)))
9337 (dline1 (org-table-current-dline))
9338 (dline2 (+ dline1 (if up -1 1)))
9341 (beginning-of-line tonew)
9342 (unless (org-at-table-p)
9344 (error "Cannot move row further"))
9345 (setq hline2p (looking-at org-table-hline-regexp))
9347 (beginning-of-line 1)
9349 (setq txt (buffer-substring (point) (1+ (point-at-eol))))
9350 (delete-region (point) (1+ (point-at-eol)))
9351 (beginning-of-line tonew)
9353 (beginning-of-line 0)
9354 (move-to-column col)
9355 (unless (or hline1p hline2p)
9356 (org-table-fix-formulas
9357 "@" (list (cons (number-to-string dline1) (number-to-string dline2))
9358 (cons (number-to-string dline2) (number-to-string dline1)))))))
9360 (defun org-table-insert-row (&optional arg)
9361 "Insert a new row above the current line into the table.
9362 With prefix ARG, insert below the current line."
9364 (if (not (org-at-table-p))
9365 (error "Not at a table"))
9366 (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
9367 (new (org-table-clean-line line)))
9368 ;; Fix the first field if necessary
9369 (if (string-match "^[ \t]*| *[#$] *|" line)
9370 (setq new (replace-match (match-string 0 line) t t new)))
9371 (beginning-of-line (if arg 2 1))
9372 (let (org-table-may-need-update) (insert-before-markers new "\n"))
9373 (beginning-of-line 0)
9374 (re-search-forward "| ?" (point-at-eol) t)
9375 (and (or org-table-may-need-update org-table-overlay-coordinates)
9377 (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
9379 (defun org-table-insert-hline (&optional above)
9380 "Insert a horizontal-line below the current line into the table.
9381 With prefix ABOVE, insert above the current line."
9383 (if (not (org-at-table-p))
9384 (error "Not at a table"))
9385 (let ((line (org-table-clean-line
9386 (buffer-substring (point-at-bol) (point-at-eol))))
9387 (col (current-column)))
9388 (while (string-match "|\\( +\\)|" line)
9389 (setq line (replace-match
9390 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
9391 ?-) "|") t t line)))
9392 (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
9393 (beginning-of-line (if above 1 2))
9395 (beginning-of-line (if above 1 -1))
9396 (move-to-column col)
9397 (and org-table-overlay-coordinates (org-table-align))))
9399 (defun org-table-hline-and-move (&optional same-column)
9400 "Insert a hline and move to the row below that line."
9402 (let ((col (org-table-current-column)))
9403 (org-table-maybe-eval-formula)
9404 (org-table-maybe-recalculate-line)
9405 (org-table-insert-hline)
9407 (if (looking-at "\n[ \t]*|-")
9408 (progn (insert "\n|") (org-table-align))
9409 (org-table-next-field))
9410 (if same-column (org-table-goto-column col))))
9412 (defun org-table-clean-line (s)
9413 "Convert a table line S into a string with only \"|\" and space.
9414 In particular, this does handle wide and invisible characters."
9415 (if (string-match "^[ \t]*|-" s)
9416 ;; It's a hline, just map the characters
9417 (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
9418 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
9419 (setq s (replace-match
9420 (concat "|" (make-string (org-string-width (match-string 1 s))
9425 (defun org-table-kill-row ()
9426 "Delete the current row or horizontal line from the table."
9428 (if (not (org-at-table-p))
9429 (error "Not at a table"))
9430 (let ((col (current-column))
9431 (dline (org-table-current-dline)))
9432 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
9433 (if (not (org-at-table-p)) (beginning-of-line 0))
9434 (move-to-column col)
9435 (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
9438 (defun org-table-sort-lines (with-case &optional sorting-type)
9439 "Sort table lines according to the column at point.
9441 The position of point indicates the column to be used for
9442 sorting, and the range of lines is the range between the nearest
9443 horizontal separator lines, or the entire table of no such lines
9444 exist. If point is before the first column, you will be prompted
9445 for the sorting column. If there is an active region, the mark
9446 specifies the first line and the sorting column, while point
9447 should be in the last line to be included into the sorting.
9449 The command then prompts for the sorting type which can be
9450 alphabetically, numerically, or by time (as given in a time stamp
9451 in the field). Sorting in reverse order is also possible.
9453 With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
9455 If SORTING-TYPE is specified when this function is called from a Lisp
9456 program, no prompting will take place. SORTING-TYPE must be a character,
9457 any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
9458 should be done in reverse order."
9460 (let* ((thisline (org-current-line))
9461 (thiscol (org-table-current-column))
9462 beg end bcol ecol tend tbeg column lns pos)
9463 (when (equal thiscol 0)
9467 (read-string "Use column N for sorting: ")))
9469 (org-table-goto-column thiscol))
9470 (org-table-check-inside-data-field)
9471 (if (org-region-active-p)
9473 (setq beg (region-beginning) end (region-end))
9475 (setq column (org-table-current-column)
9478 (setq end (point-at-bol 2)))
9479 (setq column (org-table-current-column)
9481 tbeg (org-table-begin)
9482 tend (org-table-end))
9483 (if (re-search-backward org-table-hline-regexp tbeg t)
9484 (setq beg (point-at-bol 2))
9486 (setq beg (point-at-bol 1)))
9488 (if (re-search-forward org-table-hline-regexp tend t)
9489 (setq end (point-at-bol 1))
9491 (setq end (point-at-bol))))
9492 (setq beg (move-marker (make-marker) beg)
9493 end (move-marker (make-marker) end))
9496 (org-table-goto-column column)
9497 (skip-chars-backward "^|")
9498 (setq bcol (current-column))
9499 (org-table-goto-column (1+ column))
9500 (skip-chars-backward "^|")
9501 (setq ecol (1- (current-column)))
9502 (org-table-goto-column column)
9503 (setq lns (mapcar (lambda(x) (cons
9504 (org-sort-remove-invisible
9506 (org-split-string x "[ \t]*|[ \t]*")))
9508 (org-split-string (buffer-substring beg end) "\n")))
9509 (setq lns (org-do-sort lns "Table" with-case sorting-type))
9510 (delete-region beg end)
9511 (move-marker beg nil)
9512 (move-marker end nil)
9513 (insert (mapconcat 'cdr lns "\n") "\n")
9514 (goto-line thisline)
9515 (org-table-goto-column thiscol)
9516 (message "%d lines sorted, based on column %d" (length lns) column)))
9518 ;; FIXME: maybe we will not need this? Table sorting is broken....
9519 (defun org-sort-remove-invisible (s)
9520 (remove-text-properties 0 (length s) org-rm-props s)
9521 (while (string-match org-bracket-link-regexp s)
9522 (setq s (replace-match (if (match-end 2)
9524 (match-string 1 s)) t t s)))
9527 (defun org-table-cut-region (beg end)
9528 "Copy region in table to the clipboard and blank all relevant fields."
9530 (org-table-copy-region beg end 'cut))
9532 (defun org-table-copy-region (beg end &optional cut)
9533 "Copy rectangular region in table to clipboard.
9534 A special clipboard is used which can only be accessed
9535 with `org-table-paste-rectangle'."
9537 (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
9539 (rpl (if cut " " nil)))
9541 (org-table-check-inside-data-field)
9542 (setq l01 (org-current-line)
9543 c01 (org-table-current-column))
9545 (org-table-check-inside-data-field)
9546 (setq l02 (org-current-line)
9547 c02 (org-table-current-column))
9548 (setq l1 (min l01 l02) l2 (max l01 l02)
9549 c1 (min c01 c02) c2 (max c01 c02))
9553 (if (> l1 l2) (throw 'exit t))
9555 (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
9556 (setq cols nil ic1 c1 ic2 c2)
9557 (while (< ic1 (1+ ic2))
9558 (push (org-table-get-field ic1 rpl) cols)
9559 (setq ic1 (1+ ic1)))
9560 (push (nreverse cols) region)
9561 (setq l1 (1+ l1)))))
9562 (setq org-table-clip (nreverse region))
9563 (if cut (org-table-align))
9566 (defun org-table-paste-rectangle ()
9567 "Paste a rectangular region into a table.
9568 The upper right corner ends up in the current field. All involved fields
9569 will be overwritten. If the rectangle does not fit into the present table,
9570 the table is enlarged as needed. The process ignores horizontal separator
9573 (unless (and org-table-clip (listp org-table-clip))
9574 (error "First cut/copy a region to paste!"))
9575 (org-table-check-inside-data-field)
9576 (let* ((clip org-table-clip)
9577 (line (org-current-line))
9578 (col (org-table-current-column))
9579 (org-enable-table-editor t)
9580 (org-table-automatic-realign nil)
9582 (while (setq cols (pop clip))
9583 (while (org-at-table-hline-p) (beginning-of-line 2))
9584 (if (not (org-at-table-p))
9585 (progn (end-of-line 0) (org-table-next-field)))
9587 (while (setq field (pop cols))
9588 (org-table-goto-column c nil 'force)
9589 (org-table-get-field nil field)
9591 (beginning-of-line 2))
9593 (org-table-goto-column col)
9596 (defun org-table-convert ()
9597 "Convert from `org-mode' table to table.el and back.
9598 Obviously, this only works within limits. When an Org-mode table is
9599 converted to table.el, all horizontal separator lines get lost, because
9600 table.el uses these as cell boundaries and has no notion of horizontal lines.
9601 A table.el table can be converted to an Org-mode table only if it does not
9602 do row or column spanning. Multiline cells will become multiple cells.
9603 Beware, Org-mode does not test if the table can be successfully converted - it
9604 blindly applies a recipe that works for simple tables."
9607 (if (org-at-table.el-p)
9608 ;; convert to Org-mode table
9609 (let ((beg (move-marker (make-marker) (org-table-begin t)))
9610 (end (move-marker (make-marker) (org-table-end t))))
9611 (table-unrecognize-region beg end)
9613 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
9616 (if (org-at-table-p)
9617 ;; convert to table.el table
9618 (let ((beg (move-marker (make-marker) (org-table-begin)))
9619 (end (move-marker (make-marker) (org-table-end))))
9620 ;; first, get rid of all horizontal lines
9622 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
9624 ;; insert a hline before first
9626 (org-table-insert-hline 'above)
9627 (beginning-of-line -1)
9628 ;; insert a hline after each line
9629 (while (progn (beginning-of-line 3) (< (point) end))
9630 (org-table-insert-hline))
9632 (setq end (move-marker end (org-table-end)))
9633 ;; replace "+" at beginning and ending of hlines
9634 (while (re-search-forward "^\\([ \t]*\\)|-" end t)
9635 (replace-match "\\1+-"))
9637 (while (re-search-forward "-|[ \t]*$" end t)
9638 (replace-match "-+"))
9641 (defun org-table-wrap-region (arg)
9642 "Wrap several fields in a column like a paragraph.
9643 This is useful if you'd like to spread the contents of a field over several
9644 lines, in order to keep the table compact.
9646 If there is an active region, and both point and mark are in the same column,
9647 the text in the column is wrapped to minimum width for the given number of
9648 lines. Generally, this makes the table more compact. A prefix ARG may be
9649 used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
9650 formats the selected text to two lines. If the region was longer than two
9651 lines, the remaining lines remain empty. A negative prefix argument reduces
9652 the current number of lines by that amount. The wrapped text is pasted back
9653 into the table. If you formatted it to more lines than it was before, fields
9654 further down in the table get overwritten - so you might need to make space in
9657 If there is no region, the current field is split at the cursor position and
9658 the text fragment to the right of the cursor is prepended to the field one
9661 If there is no region, but you specify a prefix ARG, the current field gets
9662 blank, and the content is appended to the field above."
9664 (org-table-check-inside-data-field)
9665 (if (org-region-active-p)
9666 ;; There is a region: fill as a paragraph
9667 (let* ((beg (region-beginning))
9668 (cline (save-excursion (goto-char beg) (org-current-line)))
9669 (ccol (save-excursion (goto-char beg) (org-table-current-column)))
9671 (org-table-cut-region (region-beginning) (region-end))
9672 (if (> (length (car org-table-clip)) 1)
9673 (error "Region must be limited to single column"))
9674 (setq nlines (if arg
9676 (+ (length org-table-clip) arg)
9678 (length org-table-clip)))
9679 (setq org-table-clip
9680 (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
9683 (org-table-goto-column ccol)
9684 (org-table-paste-rectangle))
9685 ;; No region, split the current field at point
9686 (unless (org-get-alist-option org-M-RET-may-split-line 'table)
9687 (skip-chars-forward "^\r\n|"))
9689 ;; combine with field above
9690 (let ((s (org-table-blank-field))
9691 (col (org-table-current-column)))
9692 (beginning-of-line 0)
9693 (while (org-at-table-hline-p) (beginning-of-line 0))
9694 (org-table-goto-column col)
9695 (skip-chars-forward "^|")
9696 (skip-chars-backward " ")
9697 (insert " " (org-trim s))
9700 (if (looking-at "\\([^|]+\\)+|")
9701 (let ((s (match-string 1)))
9702 (replace-match " |")
9703 (goto-char (match-beginning 0))
9704 (org-table-next-row)
9705 (insert (org-trim s) " ")
9707 (org-table-next-row)))))
9709 (defvar org-field-marker nil)
9711 (defun org-table-edit-field (arg)
9712 "Edit table field in a different window.
9713 This is mainly useful for fields that contain hidden parts.
9714 When called with a \\[universal-argument] prefix, just make the full field visible so that
9715 it can be edited in place."
9718 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
9719 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
9720 (remove-text-properties b e '(org-cwidth t invisible t
9721 display t intangible t))
9722 (if (and (boundp 'font-lock-mode) font-lock-mode)
9723 (font-lock-fontify-block)))
9724 (let ((pos (move-marker (make-marker) (point)))
9725 (field (org-table-get-field))
9726 (cw (current-window-configuration))
9728 (org-switch-to-buffer-other-window "*Org tmp*")
9730 (insert "#\n# Edit field and finish with C-c C-c\n#\n")
9731 (let ((org-inhibit-startup t)) (org-mode))
9732 (goto-char (setq p (point-max)))
9733 (insert (org-trim field))
9734 (remove-text-properties p (point-max)
9735 '(invisible t org-cwidth t display t
9738 (org-set-local 'org-finish-function 'org-table-finish-edit-field)
9739 (org-set-local 'org-window-configuration cw)
9740 (org-set-local 'org-field-marker pos)
9741 (message "Edit and finish with C-c C-c"))))
9743 (defun org-table-finish-edit-field ()
9744 "Finish editing a table data field.
9745 Remove all newline characters, insert the result into the table, realign
9746 the table and kill the editing buffer."
9747 (let ((pos org-field-marker)
9748 (cw org-window-configuration)
9749 (cb (current-buffer))
9751 (goto-char (point-min))
9752 (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
9753 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
9754 (replace-match " "))
9755 (setq text (org-trim (buffer-string)))
9756 (set-window-configuration cw)
9758 (select-window (get-buffer-window (marker-buffer pos)))
9760 (move-marker pos nil)
9761 (org-table-check-inside-data-field)
9762 (org-table-get-field nil text)
9764 (message "New field value inserted")))
9767 "Remove whitespace at beginning and end of string."
9768 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
9769 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
9772 (defun org-wrap (string &optional width lines)
9773 "Wrap string to either a number of lines, or a width in characters.
9774 If WIDTH is non-nil, the string is wrapped to that width, however many lines
9775 that costs. If there is a word longer than WIDTH, the text is actually
9776 wrapped to the length of that word.
9777 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
9778 many lines, whatever width that takes.
9779 The return value is a list of lines, without newlines at the end."
9780 (let* ((words (org-split-string string "[ \t\n]+"))
9781 (maxword (apply 'max (mapcar 'org-string-width words)))
9784 (org-do-wrap words (max maxword width)))
9787 (setq ll (org-do-wrap words maxword))
9788 (if (<= (length ll) lines)
9791 (while (> (length ll) lines)
9793 (setq ll (org-do-wrap words w)))
9795 (t (error "Cannot wrap this")))))
9798 (defun org-do-wrap (words width)
9799 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
9802 (setq line (pop words))
9803 (while (and words (< (+ (length line) (length (car words))) width))
9804 (setq line (concat line " " (pop words))))
9805 (setq lines (push line lines)))
9808 (defun org-split-string (string &optional separators)
9809 "Splits STRING into substrings at SEPARATORS.
9810 No empty strings are returned if there are matches at the beginning
9812 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
9816 (while (and (string-match rexp string
9818 (= start (match-beginning 0))
9819 (< start (length string)))
9821 (< (match-beginning 0) (length string)))
9823 (or (eq (match-beginning 0) 0)
9824 (and (eq (match-beginning 0) (match-end 0))
9825 (eq (match-beginning 0) start))
9827 (cons (substring string start (match-beginning 0))
9829 (setq start (match-end 0)))
9830 (or (eq start (length string))
9832 (cons (substring string start)
9836 (defun org-table-map-tables (function)
9837 "Apply FUNCTION to the start of all tables in the buffer."
9841 (goto-char (point-min))
9842 (while (re-search-forward org-table-any-line-regexp nil t)
9843 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
9844 (beginning-of-line 1)
9845 (if (looking-at org-table-line-regexp)
9846 (save-excursion (funcall function)))
9847 (re-search-forward org-table-any-border-regexp nil 1))))
9848 (message "Mapping tables: done"))
9850 (defvar org-timecnt) ; dynamically scoped parameter
9852 (defun org-table-sum (&optional beg end nlast)
9853 "Sum numbers in region of current table column.
9854 The result will be displayed in the echo area, and will be available
9855 as kill to be inserted with \\[yank].
9857 If there is an active region, it is interpreted as a rectangle and all
9858 numbers in that rectangle will be summed. If there is no active
9859 region and point is located in a table column, sum all numbers in that
9862 If at least one number looks like a time HH:MM or HH:MM:SS, all other
9863 numbers are assumed to be times as well (in decimal hours) and the
9864 numbers are added as such.
9866 If NLAST is a number, only the NLAST fields will actually be summed."
9869 (let (col (org-timecnt 0) diff h m s org-table-clip)
9871 ((and beg end)) ; beg and end given explicitly
9872 ((org-region-active-p)
9873 (setq beg (region-beginning) end (region-end)))
9875 (setq col (org-table-current-column))
9876 (goto-char (org-table-begin))
9877 (unless (re-search-forward "^[ \t]*|[^-]" nil t)
9878 (error "No table data"))
9879 (org-table-goto-column col)
9881 (goto-char (org-table-end))
9882 (unless (re-search-backward "^[ \t]*|[^-]" nil t)
9883 (error "No table data"))
9884 (org-table-goto-column col)
9885 (setq end (point))))
9886 (let* ((items (apply 'append (org-table-copy-region beg end)))
9887 (items1 (cond ((not nlast) items)
9888 ((>= nlast (length items)) items)
9889 (t (setq items (reverse items))
9890 (setcdr (nthcdr (1- nlast) items) nil)
9892 (numbers (delq nil (mapcar 'org-table-get-number-for-summing
9894 (res (apply '+ numbers))
9895 (sres (if (= org-timecnt 0)
9897 (setq diff (* 3600 res)
9898 h (floor (/ diff 3600)) diff (mod diff 3600)
9899 m (floor (/ diff 60)) diff (mod diff 60)
9901 (format "%d:%02d:%02d" h m s))))
9905 (substitute-command-keys
9906 (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
9907 (length numbers) sres))))
9910 (defun org-table-get-number-for-summing (s)
9912 (if (string-match "^ *|? *" s)
9913 (setq s (replace-match "" nil nil s)))
9914 (if (string-match " *|? *$" s)
9915 (setq s (replace-match "" nil nil s)))
9916 (setq n (string-to-number s))
9918 ((and (string-match "0" s)
9919 (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
9920 ((string-match "\\`[ \t]+\\'" s) nil)
9921 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
9922 (let ((h (string-to-number (or (match-string 1 s) "0")))
9923 (m (string-to-number (or (match-string 2 s) "0")))
9924 (s (string-to-number (or (match-string 4 s) "0"))))
9925 (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
9926 (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
9930 (defun org-table-current-field-formula (&optional key noerror)
9931 "Return the formula active for the current field.
9932 Assumes that specials are in place.
9933 If KEY is given, return the key to this formula.
9934 Otherwise return the formula preceeded with \"=\" or \":=\"."
9935 (let* ((name (car (rassoc (list (org-current-line)
9936 (org-table-current-column))
9937 org-table-named-field-locations)))
9938 (col (org-table-current-column))
9939 (scol (int-to-string col))
9940 (ref (format "@%d$%d" (org-table-current-dline) col))
9941 (stored-list (org-table-get-stored-formulas noerror))
9942 (ass (or (assoc name stored-list)
9943 (assoc ref stored-list)
9944 (assoc scol stored-list))))
9947 (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
9950 (defun org-table-get-formula (&optional equation named)
9951 "Read a formula from the minibuffer, offer stored formula as default.
9952 When NAMED is non-nil, look for a named equation."
9953 (let* ((stored-list (org-table-get-stored-formulas))
9954 (name (car (rassoc (list (org-current-line)
9955 (org-table-current-column))
9956 org-table-named-field-locations)))
9957 (ref (format "@%d$%d" (org-table-current-dline)
9958 (org-table-current-column)))
9959 (refass (assoc ref stored-list))
9962 (int-to-string (org-table-current-column))))
9963 (dummy (and (or name refass) (not named)
9964 (not (y-or-n-p "Replace field formula with column formula? " ))
9966 (name (or name ref))
9967 (org-table-may-need-update nil)
9968 (stored (cdr (assoc scol stored-list)))
9970 ((and stored equation (string-match "^ *=? *$" equation))
9974 (t (org-table-formula-from-user
9976 (org-table-formula-to-user
9977 (format "%s formula %s%s="
9978 (if named "Field" "Column")
9979 (if (member (string-to-char scol) '(?$ ?@)) "" "$")
9981 (if stored (org-table-formula-to-user stored) "")
9982 'org-table-formula-history
9985 (when (not (string-match "\\S-" eq))
9987 (setq stored-list (delq (assoc scol stored-list) stored-list))
9988 (org-table-store-formulas stored-list)
9989 (error "Formula removed"))
9990 (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
9991 (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
9992 (if (and name (not named))
9993 ;; We set the column equation, delete the named one.
9994 (setq stored-list (delq (assoc name stored-list) stored-list)
9997 (setcdr (assoc scol stored-list) eq)
9998 (setq stored-list (cons (cons scol eq) stored-list)))
9999 (if (or mustsave (not (equal stored eq)))
10000 (org-table-store-formulas stored-list))
10003 (defun org-table-store-formulas (alist)
10004 "Store the list of formulas below the current table."
10005 (setq alist (sort alist 'org-table-formula-less-p))
10007 (goto-char (org-table-end))
10008 (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
10010 ;; don't overwrite TBLFM, we might use text properties to store stuff
10011 (goto-char (match-beginning 2))
10012 (delete-region (match-beginning 2) (match-end 0)))
10013 (insert "#+TBLFM:"))
10015 (mapconcat (lambda (x)
10017 (if (equal (string-to-char (car x)) ?@) "" "$")
10018 (car x) "=" (cdr x)))
10022 (defsubst org-table-formula-make-cmp-string (a)
10023 (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
10025 (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
10026 (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
10027 (if (match-end 5) (concat "@@" (match-string 5 a))))))
10029 (defun org-table-formula-less-p (a b)
10030 "Compare two formulas for sorting."
10031 (let ((as (org-table-formula-make-cmp-string (car a)))
10032 (bs (org-table-formula-make-cmp-string (car b))))
10033 (and as bs (string< as bs))))
10035 (defun org-table-get-stored-formulas (&optional noerror)
10036 "Return an alist with the stored formulas directly after current table."
10038 (let (scol eq eq-alist strings string seen)
10040 (goto-char (org-table-end))
10041 (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
10042 (setq strings (org-split-string (match-string 2) " *:: *"))
10043 (while (setq string (pop strings))
10044 (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
10045 (setq scol (if (match-end 2)
10046 (match-string 2 string)
10047 (match-string 1 string))
10048 eq (match-string 3 string)
10049 eq-alist (cons (cons scol eq) eq-alist))
10050 (if (member scol seen)
10053 (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
10056 (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
10057 (push scol seen))))))
10058 (nreverse eq-alist)))
10060 (defun org-table-fix-formulas (key replace &optional limit delta remove)
10061 "Modify the equations after the table structure has been edited.
10062 KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
10063 For all numbers larger than LIMIT, shift them by DELTA."
10065 (goto-char (org-table-end))
10066 (when (looking-at "#\\+TBLFM:")
10067 (let ((re (concat key "\\([0-9]+\\)"))
10070 (if (equal key "$")
10071 (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
10072 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
10075 (while (re-search-forward re2 (point-at-eol) t)
10076 (replace-match "")))
10077 (while (re-search-forward re (point-at-eol) t)
10078 (setq s (match-string 1) n (string-to-number s))
10080 ((setq a (assoc s replace))
10081 (replace-match (concat key (cdr a)) t t))
10082 ((and limit (> n limit))
10083 (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
10085 (defun org-table-get-specials ()
10086 "Get the column names and local parameters for this table."
10088 (let ((beg (org-table-begin)) (end (org-table-end))
10089 names name fields fields1 field cnt
10090 c v l line col types dlines hlines)
10091 (setq org-table-column-names nil
10092 org-table-local-parameters nil
10093 org-table-named-field-locations nil
10094 org-table-current-begin-line nil
10095 org-table-current-begin-pos nil
10096 org-table-current-line-types nil)
10098 (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
10099 (setq names (org-split-string (match-string 1) " *| *")
10101 (while (setq name (pop names))
10102 (setq cnt (1+ cnt))
10103 (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
10104 (push (cons name (int-to-string cnt)) org-table-column-names))))
10105 (setq org-table-column-names (nreverse org-table-column-names))
10106 (setq org-table-column-name-regexp
10107 (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
10109 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
10110 (setq fields (org-split-string (match-string 1) " *| *"))
10111 (while (setq field (pop fields))
10112 (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
10113 (push (cons (match-string 1 field) (match-string 2 field))
10114 org-table-local-parameters))))
10116 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
10117 (setq c (match-string 1)
10118 fields (org-split-string (match-string 2) " *| *"))
10120 (beginning-of-line (if (equal c "_") 2 0))
10121 (setq line (org-current-line) col 1)
10122 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
10123 (setq fields1 (org-split-string (match-string 1) " *| *"))))
10124 (while (and fields1 (setq field (pop fields)))
10125 (setq v (pop fields1) col (1+ col))
10126 (when (and (stringp field) (stringp v)
10127 (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
10128 (push (cons field v) org-table-local-parameters)
10129 (push (list field line col) org-table-named-field-locations))))
10130 ;; Analyse the line types
10132 (setq org-table-current-begin-line (org-current-line)
10133 org-table-current-begin-pos (point)
10134 l org-table-current-begin-line)
10135 (while (looking-at "[ \t]*|\\(-\\)?")
10136 (push (if (match-end 1) 'hline 'dline) types)
10137 (if (match-end 1) (push l hlines) (push l dlines))
10138 (beginning-of-line 2)
10140 (setq org-table-current-line-types (apply 'vector (nreverse types))
10141 org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
10142 org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
10144 (defun org-table-maybe-eval-formula ()
10145 "Check if the current field starts with \"=\" or \":=\".
10146 If yes, store the formula and apply it."
10147 ;; We already know we are in a table. Get field will only return a formula
10148 ;; when appropriate. It might return a separator line, but no problem.
10149 (when org-table-formula-evaluate-inline
10150 (let* ((field (org-trim (or (org-table-get-field) "")))
10152 (when (string-match "^:?=\\(.*\\)" field)
10153 (setq named (equal (string-to-char field) ?:)
10154 eq (match-string 1 field))
10155 (if (or (fboundp 'calc-eval)
10156 (equal (substring eq 0 (min 2 (length eq))) "'("))
10157 (org-table-eval-formula (if named '(4) nil)
10158 (org-table-formula-from-user eq))
10159 (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
10161 (defvar org-recalc-commands nil
10162 "List of commands triggering the recalculation of a line.
10163 Will be filled automatically during use.")
10165 (defvar org-recalc-marks
10166 '((" " . "Unmarked: no special line, no automatic recalculation")
10167 ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
10168 ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
10169 ("!" . "Column name definition line. Reference in formula as $name.")
10170 ("$" . "Parameter definition line name=value. Reference in formula as $name.")
10171 ("_" . "Names for values in row below this one.")
10172 ("^" . "Names for values in row above this one.")))
10174 (defun org-table-rotate-recalc-marks (&optional newchar)
10175 "Rotate the recalculation mark in the first column.
10176 If in any row, the first field is not consistent with a mark,
10177 insert a new column for the markers.
10178 When there is an active region, change all the lines in the region,
10179 after prompting for the marking character.
10180 After each change, a message will be displayed indicating the meaning
10183 (unless (org-at-table-p) (error "Not at a table"))
10184 (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
10185 (beg (org-table-begin))
10186 (end (org-table-end))
10187 (l (org-current-line))
10188 (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
10189 (l2 (if (org-region-active-p) (org-current-line (region-end))))
10193 (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
10194 (col (org-table-current-column))
10195 (forcenew (car (assoc newchar org-recalc-marks)))
10198 (message "Change region to what mark? Type # * ! $ or SPC: ")
10199 (setq newchar (char-to-string (read-char-exclusive))
10200 forcenew (car (assoc newchar org-recalc-marks))))
10201 (if (and newchar (not forcenew))
10202 (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
10204 (if l1 (goto-line l1))
10206 (beginning-of-line 1)
10207 (unless (looking-at org-table-dataline-regexp)
10208 (error "Not at a table data line")))
10210 (org-table-goto-column 1)
10211 (org-table-insert-column)
10212 (org-table-goto-column (1+ col)))
10213 (setq epos (point-at-eol))
10215 (beginning-of-line 1)
10216 (org-table-get-field
10217 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
10219 (setq new (or forcenew
10220 (cadr (member (match-string 1) marks))))
10226 (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
10227 (and (looking-at org-table-dataline-regexp)
10228 (org-table-get-field 1 (concat " " new " "))))
10230 (if (not (= epos (point-at-eol))) (org-table-align))
10232 (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
10234 (defun org-table-maybe-recalculate-line ()
10235 "Recompute the current line if marked for it, and if we haven't just done it."
10237 (and org-table-allow-automatic-line-recalculation
10238 (not (and (memq last-command org-recalc-commands)
10239 (equal org-last-recalc-line (org-current-line))))
10240 (save-excursion (beginning-of-line 1)
10241 (looking-at org-table-auto-recalculate-regexp))
10242 (org-table-recalculate) t))
10244 (defvar org-table-formula-debug nil
10245 "Non-nil means, debug table formulas.
10246 When nil, simply write \"#ERROR\" in corrupted fields.")
10247 (make-variable-buffer-local 'org-table-formula-debug)
10250 (defsubst org-set-calc-mode (var &optional value)
10252 (setq var (assoc var '(("D" calc-angle-mode deg)
10253 ("R" calc-angle-mode rad)
10254 ("F" calc-prefer-frac t)
10255 ("S" calc-symbolic-mode t)))
10256 value (nth 2 var) var (nth 1 var)))
10257 (if (memq var modes)
10258 (setcar (cdr (memq var modes)) value)
10259 (cons var (cons value modes)))
10262 (defun org-table-eval-formula (&optional arg equation
10263 suppress-align suppress-const
10264 suppress-store suppress-analysis)
10265 "Replace the table field value at the cursor by the result of a calculation.
10267 This function makes use of Dave Gillespie's Calc package, in my view the
10268 most exciting program ever written for GNU Emacs. So you need to have Calc
10269 installed in order to use this function.
10271 In a table, this command replaces the value in the current field with the
10272 result of a formula. It also installs the formula as the \"current\" column
10273 formula, by storing it in a special line below the table. When called
10274 with a `C-u' prefix, the current field must ba a named field, and the
10275 formula is installed as valid in only this specific field.
10277 When called with two `C-u' prefixes, insert the active equation
10278 for the field back into the current field, so that it can be
10279 edited there. This is useful in order to use \\[org-table-show-reference]
10280 to check the referenced fields.
10282 When called, the command first prompts for a formula, which is read in
10283 the minibuffer. Previously entered formulas are available through the
10284 history list, and the last used formula is offered as a default.
10285 These stored formulas are adapted correctly when moving, inserting, or
10286 deleting columns with the corresponding commands.
10288 The formula can be any algebraic expression understood by the Calc package.
10289 For details, see the Org-mode manual.
10291 This function can also be called from Lisp programs and offers
10292 additional arguments: EQUATION can be the formula to apply. If this
10293 argument is given, the user will not be prompted. SUPPRESS-ALIGN is
10294 used to speed-up recursive calls by by-passing unnecessary aligns.
10295 SUPPRESS-CONST suppresses the interpretation of constants in the
10296 formula, assuming that this has been done already outside the function.
10297 SUPPRESS-STORE means the formula should not be stored, either because
10298 it is already stored, or because it is a modified equation that should
10299 not overwrite the stored one."
10301 (org-table-check-inside-data-field)
10302 (or suppress-analysis (org-table-get-specials))
10303 (if (equal arg '(16))
10304 (let ((eq (org-table-current-field-formula)))
10305 (or eq (error "No equation active for current field"))
10306 (org-table-get-field nil eq)
10308 (setq org-table-may-need-update t))
10310 (ndown (if (integerp arg) arg 1))
10311 (org-table-automatic-realign nil)
10312 (case-fold-search nil)
10314 (formula (if (and equation suppress-store)
10316 (org-table-get-formula equation (equal arg '(4)))))
10317 (n0 (org-table-current-column))
10318 (modes (copy-sequence org-calc-default-modes))
10319 (numbers nil) ; was a variable, now fixed default
10321 n form form0 bw fmt x ev orig c lispp literal)
10322 ;; Parse the format string. Since we have a lot of modes, this is
10323 ;; a lot of work. However, I think calc still uses most of the time.
10324 (if (string-match ";" formula)
10325 (let ((tmp (org-split-string formula ";")))
10326 (setq formula (car tmp)
10327 fmt (concat (cdr (assoc "%" org-table-local-parameters))
10329 (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
10330 (setq c (string-to-char (match-string 1 fmt))
10331 n (string-to-number (match-string 2 fmt)))
10333 (setq modes (org-set-calc-mode 'calc-internal-prec n))
10334 (setq modes (org-set-calc-mode
10336 (list (cdr (assoc c '((?n . float) (?f . fix)
10337 (?s . sci) (?e . eng))))
10339 (setq fmt (replace-match "" t t fmt)))
10340 (if (string-match "[NT]" fmt)
10341 (setq numbers (equal (match-string 0 fmt) "N")
10342 fmt (replace-match "" t t fmt)))
10343 (if (string-match "L" fmt)
10345 fmt (replace-match "" t t fmt)))
10346 (if (string-match "E" fmt)
10348 fmt (replace-match "" t t fmt)))
10349 (while (string-match "[DRFS]" fmt)
10350 (setq modes (org-set-calc-mode (match-string 0 fmt)))
10351 (setq fmt (replace-match "" t t fmt)))
10352 (unless (string-match "\\S-" fmt)
10354 (if (and (not suppress-const) org-table-formula-use-constants)
10355 (setq formula (org-table-formula-substitute-names formula)))
10356 (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
10358 (setq fields (org-split-string
10360 (buffer-substring (point-at-bol) (point-at-eol)))
10363 (setq fields (mapcar
10364 (lambda (x) (number-to-string (string-to-number x)))
10366 (setq ndown (1- ndown))
10367 (setq form (copy-sequence formula)
10368 lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
10369 (if (and lispp literal) (setq lispp 'literal))
10370 ;; Check for old vertical references
10371 (setq form (org-rewrite-old-row-references form))
10372 ;; Insert complex ranges
10373 (while (string-match org-table-range-regexp form)
10377 (org-table-make-reference
10378 (org-table-get-range (match-string 0 form) nil n0)
10379 keep-empty numbers lispp))
10381 ;; Insert simple ranges
10382 (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
10386 (org-table-make-reference
10388 fields (string-to-number (match-string 1 form))
10389 (string-to-number (match-string 2 form)))
10390 keep-empty numbers lispp))
10393 ;; Insert the references to fields in same row
10394 (while (string-match "\\$\\([0-9]+\\)" form)
10395 (setq n (string-to-number (match-string 1 form))
10396 x (nth (1- (if (= n 0) n0 n)) fields))
10397 (unless x (error "Invalid field specifier \"%s\""
10398 (match-string 0 form)))
10399 (setq form (replace-match
10401 (org-table-make-reference x nil numbers lispp))
10405 (setq ev (condition-case nil
10406 (eval (eval (read form)))
10408 ev (if (numberp ev) (number-to-string ev) ev))
10409 (or (fboundp 'calc-eval)
10410 (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
10411 (setq ev (calc-eval (cons form modes)
10412 (if numbers 'num))))
10414 (when org-table-formula-debug
10415 (with-output-to-temp-buffer "*Substitution History*"
10416 (princ (format "Substitution history of formula
10420 $1-> %s\n" orig formula form0 form))
10422 (princ (format " %s^\nError: %s"
10423 (make-string (car ev) ?\-) (nth 1 ev)))
10424 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
10426 (if fmt (format fmt (string-to-number ev)) ev)))))
10427 (setq bw (get-buffer-window "*Substitution History*"))
10428 (shrink-window-if-larger-than-buffer bw)
10429 (unless (and (interactive-p) (not ndown))
10430 (unless (let (inhibit-redisplay)
10431 (y-or-n-p "Debugging Formula. Continue to next? "))
10436 (if (listp ev) (setq fmt nil ev "#ERROR"))
10437 (org-table-justify-field-maybe
10438 (if fmt (format fmt (string-to-number ev)) ev))
10439 (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
10440 (call-interactively 'org-return)
10442 (and down (org-table-maybe-recalculate-line))
10443 (or suppress-align (and org-table-may-need-update
10444 (org-table-align))))))
10446 (defun org-table-put-field-property (prop value)
10448 (put-text-property (progn (skip-chars-backward "^|") (point))
10449 (progn (skip-chars-forward "^|") (point))
10452 (defun org-table-get-range (desc &optional tbeg col highlight)
10453 "Get a calc vector from a column, accorting to descriptor DESC.
10454 Optional arguments TBEG and COL can give the beginning of the table and
10455 the current column, to avoid unnecessary parsing.
10456 HIGHLIGHT means, just highlight the range."
10457 (if (not (equal (string-to-char desc) ?@))
10458 (setq desc (concat "@" desc)))
10460 (or tbeg (setq tbeg (org-table-begin)))
10461 (or col (setq col (org-table-current-column)))
10462 (let ((thisline (org-current-line))
10463 beg end c1 c2 r1 r2 rangep tmp)
10464 (unless (string-match org-table-range-regexp desc)
10465 (error "Invalid table range specifier `%s'" desc))
10466 (setq rangep (match-end 3)
10467 r1 (and (match-end 1) (match-string 1 desc))
10468 r2 (and (match-end 4) (match-string 4 desc))
10469 c1 (and (match-end 2) (substring (match-string 2 desc) 1))
10470 c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
10472 (and c1 (setq c1 (+ (string-to-number c1)
10473 (if (memq (string-to-char c1) '(?- ?+)) col 0))))
10474 (and c2 (setq c2 (+ (string-to-number c2)
10475 (if (memq (string-to-char c2) '(?- ?+)) col 0))))
10476 (if (equal r1 "") (setq r1 nil))
10477 (if (equal r2 "") (setq r2 nil))
10478 (if r1 (setq r1 (org-table-get-descriptor-line r1)))
10479 (if r2 (setq r2 (org-table-get-descriptor-line r2)))
10480 ; (setq r2 (or r2 r1) c2 (or c2 c1))
10481 (if (not r1) (setq r1 thisline))
10482 (if (not r2) (setq r2 thisline))
10483 (if (not c1) (setq c1 col))
10484 (if (not c2) (setq c2 col))
10485 (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
10489 (while (not (looking-at org-table-dataline-regexp))
10490 (beginning-of-line 2))
10491 (prog1 (org-trim (org-table-get-field c1))
10492 (if highlight (org-table-highlight-rectangle (point) (point)))))
10493 ;; A range, return a vector
10494 ;; First sort the numbers to get a regular ractangle
10495 (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
10496 (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
10498 (while (not (looking-at org-table-dataline-regexp))
10499 (beginning-of-line 2))
10500 (org-table-goto-column c1)
10503 (while (not (looking-at org-table-dataline-regexp))
10504 (beginning-of-line 0))
10505 (org-table-goto-column c2)
10508 (org-table-highlight-rectangle
10509 beg (progn (skip-chars-forward "^|\n") (point))))
10510 ;; return string representation of calc vector
10512 (apply 'append (org-table-copy-region beg end)))))))
10514 (defun org-table-get-descriptor-line (desc &optional cline bline table)
10515 "Analyze descriptor DESC and retrieve the corresponding line number.
10516 The cursor is currently in line CLINE, the table begins in line BLINE,
10517 and TABLE is a vector with line types."
10518 (if (string-match "^[0-9]+$" desc)
10519 (aref org-table-dlines (string-to-number desc))
10520 (setq cline (or cline (org-current-line))
10521 bline (or bline org-table-current-begin-line)
10522 table (or table org-table-current-line-types))
10524 (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
10526 (and (not (match-end 3)) (not (match-end 6)))
10527 (and (match-end 3) (match-end 6) (not (match-end 5))))
10528 (error "invalid row descriptor `%s'" desc))
10529 (let* ((hdir (and (match-end 2) (match-string 2 desc)))
10530 (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
10531 (odir (and (match-end 5) (match-string 5 desc)))
10532 (on (if (match-end 6) (string-to-number (match-string 6 desc))))
10533 (i (- cline bline))
10534 (rel (and (match-end 6)
10535 (or (and (match-end 1) (not (match-end 3)))
10537 (if (and hn (not hdir))
10539 (setq i 0 hdir "+")
10540 (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
10541 (if (and (not hn) on (not odir))
10542 (error "should never happen");;(aref org-table-dlines on)
10543 (if (and hn (> hn 0))
10544 (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
10546 (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
10549 (defun org-find-row-type (table i type backwards relative n)
10550 (let ((l (length table)))
10552 (while (and (setq i (+ i (if backwards -1 1)))
10554 (not (eq (aref table i) type))
10555 (if (and relative (eq (aref table i) 'hline))
10556 (progn (setq i (- i (if backwards -1 1)) n 1) nil)
10559 (if (or (< i 0) (>= i l))
10560 (error "Row descriptior leads outside table")
10563 (defun org-rewrite-old-row-references (s)
10564 (if (string-match "&[-+0-9I]" s)
10565 (error "Formula contains old &row reference, please rewrite using @-syntax")
10568 (defun org-table-make-reference (elements keep-empty numbers lispp)
10569 "Convert list ELEMENTS to something appropriate to insert into formula.
10570 KEEP-EMPTY indicated to keep empty fields, default is to skip them.
10571 NUMBERS indicates that everything should be converted to numbers.
10572 LISPP means to return something appropriate for a Lisp list."
10573 (if (stringp elements) ; just a single val
10575 (if (eq lispp 'literal)
10577 (prin1-to-string (if numbers (string-to-number elements) elements)))
10578 (if (equal elements "") (setq elements "0"))
10579 (if numbers (number-to-string (string-to-number elements)) elements))
10583 (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
10585 (setq elements (or elements '("0")))
10589 (if (eq lispp 'literal)
10591 (prin1-to-string (if numbers (string-to-number x) x))))
10593 (concat "[" (mapconcat
10595 (if numbers (number-to-string (string-to-number x)) x))
10599 (defun org-table-recalculate (&optional all noalign)
10600 "Recalculate the current table line by applying all stored formulas.
10601 With prefix arg ALL, do this for all lines in the table."
10603 (or (memq this-command org-recalc-commands)
10604 (setq org-recalc-commands (cons this-command org-recalc-commands)))
10605 (unless (org-at-table-p) (error "Not at a table"))
10606 (if (equal all '(16))
10607 (org-table-iterate)
10608 (org-table-get-specials)
10609 (let* ((eqlist (sort (org-table-get-stored-formulas)
10610 (lambda (a b) (string< (car a) (car b)))))
10611 (inhibit-redisplay (not debug-on-error))
10612 (line-re org-table-dataline-regexp)
10613 (thisline (org-current-line))
10614 (thiscol (org-table-current-column))
10615 beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
10616 ;; Insert constants in all formulas
10618 (mapcar (lambda (x)
10619 (setcdr x (org-table-formula-substitute-names (cdr x)))
10622 ;; Split the equation list
10623 (while (setq eq (pop eqlist))
10624 (if (<= (string-to-char (car eq)) ?9)
10626 (push eq eqlname)))
10627 (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
10630 (setq end (move-marker (make-marker) (1+ (org-table-end))))
10631 (goto-char (setq beg (org-table-begin)))
10632 (if (re-search-forward org-table-calculate-mark-regexp end t)
10633 ;; This is a table with marked lines, compute selected lines
10634 (setq line-re org-table-recalculate-regexp)
10635 ;; Move forward to the first non-header line
10636 (if (and (re-search-forward org-table-dataline-regexp end t)
10637 (re-search-forward org-table-hline-regexp end t)
10638 (re-search-forward org-table-dataline-regexp end t))
10639 (setq beg (match-beginning 0))
10640 nil))) ;; just leave beg where it is
10641 (setq beg (point-at-bol)
10642 end (move-marker (make-marker) (1+ (point-at-eol)))))
10644 (and all (message "Re-applying formulas to full table..."))
10646 ;; First find the named fields, and mark them untouchanble
10647 (remove-text-properties beg end '(org-untouchable t))
10648 (while (setq eq (pop eqlname))
10649 (setq name (car eq)
10650 a (assoc name org-table-named-field-locations))
10652 (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
10654 (aref org-table-dlines
10655 (string-to-number (match-string 1 name)))
10656 (string-to-number (match-string 2 name)))))
10657 (when (and a (or all (equal (nth 1 a) thisline)))
10658 (message "Re-applying formula to field: %s" name)
10659 (goto-line (nth 1 a))
10660 (org-table-goto-column (nth 2 a))
10661 (push (append a (list (cdr eq))) eqlname1)
10662 (org-table-put-field-property :org-untouchable t)))
10664 ;; Now evauluate the column formulas, but skip fields covered by
10667 (while (re-search-forward line-re end t)
10668 (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
10669 ;; Unprotected line, recalculate
10670 (and all (message "Re-applying formulas to full table...(line %d)"
10671 (setq cnt (1+ cnt))))
10672 (setq org-last-recalc-line (org-current-line))
10674 (while (setq entry (pop eql))
10675 (goto-line org-last-recalc-line)
10676 (org-table-goto-column (string-to-number (car entry)) nil 'force)
10677 (unless (get-text-property (point) :org-untouchable)
10678 (org-table-eval-formula nil (cdr entry)
10679 'noalign 'nocst 'nostore 'noanalysis)))))
10681 ;; Now evaluate the field formulas
10682 (while (setq eq (pop eqlname1))
10683 (message "Re-applying formula to field: %s" (car eq))
10684 (goto-line (nth 1 eq))
10685 (org-table-goto-column (nth 2 eq))
10686 (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
10687 'nostore 'noanalysis))
10689 (goto-line thisline)
10690 (org-table-goto-column thiscol)
10691 (remove-text-properties (point-min) (point-max) '(org-untouchable t))
10692 (or noalign (and org-table-may-need-update (org-table-align))
10693 (and all (message "Re-applying formulas to %d lines...done" cnt)))
10695 ;; back to initial position
10696 (message "Re-applying formulas...done")
10697 (goto-line thisline)
10698 (org-table-goto-column thiscol)
10699 (or noalign (and org-table-may-need-update (org-table-align))
10700 (and all (message "Re-applying formulas...done"))))))
10702 (defun org-table-iterate (&optional arg)
10703 "Recalculate the table until it does not change anymore."
10705 (let ((imax (if arg (prefix-numeric-value arg) 10))
10707 (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
10712 (org-table-recalculate 'all)
10713 (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
10714 (if (not (string= lasttbl thistbl))
10715 (setq lasttbl thistbl)
10717 (message "Convergence after %d iterations" i)
10718 (message "Table was already stable"))
10720 (error "No convergence after %d iterations" i))))
10722 (defun org-table-formula-substitute-names (f)
10723 "Replace $const with values in string F."
10724 (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
10725 ;; First, check for column names
10726 (while (setq start (string-match org-table-column-name-regexp f start))
10727 (setq start (1+ start))
10728 (setq a (assoc (match-string 1 f) org-table-column-names))
10729 (setq f (replace-match (concat "$" (cdr a)) t t f)))
10730 ;; Parameters and constants
10732 (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
10733 (setq start (1+ start))
10734 (if (setq a (save-match-data
10735 (org-table-get-constant (match-string 1 f))))
10736 (setq f (replace-match
10737 (concat (if pp "(") a (if pp ")")) t t f))))
10738 (if org-table-formula-debug
10739 (put-text-property 0 (length f) :orig-formula f1 f))
10742 (defun org-table-get-constant (const)
10743 "Find the value for a parameter or constant in a formula.
10744 Parameters get priority."
10745 (or (cdr (assoc const org-table-local-parameters))
10746 (cdr (assoc const org-table-formula-constants-local))
10747 (cdr (assoc const org-table-formula-constants))
10748 (and (fboundp 'constants-get) (constants-get const))
10749 (and (string= (substring const 0 (min 5 (length const))) "PROP_")
10750 (org-entry-get nil (substring const 5) 'inherit))
10751 "#UNDEFINED_NAME"))
10753 (defvar org-table-fedit-map
10754 (let ((map (make-sparse-keymap)))
10755 (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
10756 (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
10757 (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
10758 (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
10759 (org-defkey map "\C-c?" 'org-table-show-reference)
10760 (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
10761 (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
10762 (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
10763 (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
10764 (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
10765 (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
10766 (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
10767 (org-defkey map [(meta down)] 'org-table-fedit-scroll)
10768 (org-defkey map [(meta tab)] 'lisp-complete-symbol)
10769 (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
10770 (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
10771 (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
10772 (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
10773 (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
10776 (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
10778 ["Finish and Install" org-table-fedit-finish t]
10779 ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
10780 ["Abort" org-table-fedit-abort t]
10782 ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
10783 ["Complete Lisp Symbol" lisp-complete-symbol t]
10785 "Shift Reference at Point"
10786 ["Up" org-table-fedit-ref-up t]
10787 ["Down" org-table-fedit-ref-down t]
10788 ["Left" org-table-fedit-ref-left t]
10789 ["Right" org-table-fedit-ref-right t]
10791 "Change Test Row for Column Formulas"
10792 ["Up" org-table-fedit-line-up t]
10793 ["Down" org-table-fedit-line-down t]
10795 ["Scroll Table Window" org-table-fedit-scroll t]
10796 ["Scroll Table Window down" org-table-fedit-scroll-down t]
10797 ["Show Table Grid" org-table-fedit-toggle-coordinates
10798 :style toggle :selected (with-current-buffer (marker-buffer org-pos)
10799 org-table-overlay-coordinates)]
10801 ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
10802 :style toggle :selected org-table-buffer-is-an]))
10806 (defun org-table-edit-formulas ()
10807 "Edit the formulas of the current table in a separate buffer."
10809 (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
10810 (beginning-of-line 0))
10811 (unless (org-at-table-p) (error "Not at a table"))
10812 (org-table-get-specials)
10813 (let ((key (org-table-current-field-formula 'key 'noerror))
10814 (eql (sort (org-table-get-stored-formulas 'noerror)
10815 'org-table-formula-less-p))
10816 (pos (move-marker (make-marker) (point)))
10818 (wc (current-window-configuration))
10819 (titles '((column . "# Column Formulas\n")
10820 (field . "# Field Formulas\n")
10821 (named . "# Named Field Formulas\n")))
10822 entry s type title)
10823 (org-switch-to-buffer-other-window "*Edit Formulas*")
10825 ;; Keep global-font-lock-mode from turning on font-lock-mode
10826 (let ((font-lock-global-modes '(not fundamental-mode)))
10827 (fundamental-mode))
10828 (org-set-local 'font-lock-global-modes (list 'not major-mode))
10829 (org-set-local 'org-pos pos)
10830 (org-set-local 'org-window-configuration wc)
10831 (use-local-map org-table-fedit-map)
10832 (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
10833 (easy-menu-add org-table-fedit-menu)
10834 (setq startline (org-current-line))
10835 (while (setq entry (pop eql))
10837 ((equal (string-to-char (car entry)) ?@) 'field)
10838 ((string-match "^[0-9]" (car entry)) 'column)
10840 (when (setq title (assq type titles))
10841 (or (bobp) (insert "\n"))
10842 (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
10843 (setq titles (delq title titles)))
10844 (if (equal key (car entry)) (setq startline (org-current-line)))
10845 (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
10846 (car entry) " = " (cdr entry) "\n"))
10847 (remove-text-properties 0 (length s) '(face nil) s)
10849 (if (eq org-table-use-standard-references t)
10850 (org-table-fedit-toggle-ref-type))
10851 (goto-line startline)
10852 (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
10854 (defun org-table-fedit-post-command ()
10855 (when (not (memq this-command '(lisp-complete-symbol)))
10856 (let ((win (selected-window)))
10858 (condition-case nil
10859 (org-table-show-reference)
10861 (select-window win)))))
10863 (defun org-table-formula-to-user (s)
10864 "Convert a formula from internal to user representation."
10865 (if (eq org-table-use-standard-references t)
10866 (org-table-convert-refs-to-an s)
10869 (defun org-table-formula-from-user (s)
10870 "Convert a formula from user to internal representation."
10871 (if org-table-use-standard-references
10872 (org-table-convert-refs-to-rc s)
10875 (defun org-table-convert-refs-to-rc (s)
10876 "Convert spreadsheet references from AB7 to @7$28.
10877 Works for single references, but also for entire formulas and even the
10880 (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
10883 ;; format match, just advance
10884 (setq start (match-end 0)))
10885 ((and (> (match-beginning 0) 0)
10886 (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
10887 (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
10888 ;; 3.e5 or something like this.
10889 (setq start (match-end 0)))
10891 (setq start (match-beginning 0)
10893 (if (equal (match-string 2 s) "&")
10894 (format "$%d" (org-letters-to-number (match-string 1 s)))
10896 (string-to-number (match-string 2 s))
10897 (org-letters-to-number (match-string 1 s))))
10901 (defun org-table-convert-refs-to-an (s)
10902 "Convert spreadsheet references from to @7$28 to AB7.
10903 Works for single references, but also for entire formulas and even the
10905 (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
10906 (setq s (replace-match
10908 (org-number-to-letters
10909 (string-to-number (match-string 2 s)))
10910 (string-to-number (match-string 1 s)))
10912 (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
10913 (setq s (replace-match (concat "\\1"
10914 (org-number-to-letters
10915 (string-to-number (match-string 2 s))) "&")
10919 (defun org-letters-to-number (s)
10920 "Convert a base 26 number represented by letters into an integer.
10921 For example: AB -> 28."
10923 (setq s (upcase s))
10924 (while (> (length s) 0)
10925 (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
10926 s (substring s 1)))
10929 (defun org-number-to-letters (n)
10930 "Convert an integer into a base 26 number represented by letters.
10931 For example: 28 -> AB."
10934 (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
10938 (defun org-table-fedit-convert-buffer (function)
10939 "Convert all references in this buffer, using FUNTION."
10940 (let ((line (org-current-line)))
10941 (goto-char (point-min))
10942 (while (not (eobp))
10943 (insert (funcall function (buffer-substring (point) (point-at-eol))))
10944 (delete-region (point) (point-at-eol))
10945 (or (eobp) (forward-char 1)))
10948 (defun org-table-fedit-toggle-ref-type ()
10949 "Convert all references in the buffer from B3 to @3$2 and back."
10951 (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
10952 (org-table-fedit-convert-buffer
10953 (if org-table-buffer-is-an
10954 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
10955 (message "Reference type switched to %s"
10956 (if org-table-buffer-is-an "A1 etc" "@row$column")))
10958 (defun org-table-fedit-ref-up ()
10959 "Shift the reference at point one row/hline up."
10961 (org-table-fedit-shift-reference 'up))
10962 (defun org-table-fedit-ref-down ()
10963 "Shift the reference at point one row/hline down."
10965 (org-table-fedit-shift-reference 'down))
10966 (defun org-table-fedit-ref-left ()
10967 "Shift the reference at point one field to the left."
10969 (org-table-fedit-shift-reference 'left))
10970 (defun org-table-fedit-ref-right ()
10971 "Shift the reference at point one field to the right."
10973 (org-table-fedit-shift-reference 'right))
10975 (defun org-table-fedit-shift-reference (dir)
10977 ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
10978 (if (memq dir '(left right))
10979 (org-rematch-and-replace 1 (eq dir 'left))
10980 (error "Cannot shift reference in this direction")))
10981 ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
10982 ;; A B3-like reference
10983 (if (memq dir '(up down))
10984 (org-rematch-and-replace 2 (eq dir 'up))
10985 (org-rematch-and-replace 1 (eq dir 'left))))
10987 "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
10988 ;; An internal reference
10989 (if (memq dir '(up down))
10990 (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
10991 (org-rematch-and-replace 5 (eq dir 'left))))))
10993 (defun org-rematch-and-replace (n &optional decr hline)
10994 "Re-match the group N, and replace it with the shifted refrence."
10995 (or (match-end n) (error "Cannot shift reference in this direction"))
10996 (goto-char (match-beginning n))
10997 (and (looking-at (regexp-quote (match-string n)))
10998 (replace-match (org-shift-refpart (match-string 0) decr hline)
11001 (defun org-shift-refpart (ref &optional decr hline)
11002 "Shift a refrence part REF.
11003 If DECR is set, decrease the references row/column, else increase.
11004 If HLINE is set, this may be a hline reference, it certainly is not
11005 a translation reference."
11007 (let* ((sign (string-match "^[-+]" ref)) n)
11009 (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
11011 ((and hline (string-match "^I+" ref))
11012 (setq n (string-to-number (concat sign (number-to-string (length ref)))))
11013 (setq n (+ n (if decr -1 1)))
11014 (if (= n 0) (setq n (+ n (if decr -1 1))))
11016 (setq sign (if (< n 0) "-" "+") n (abs n))
11017 (setq n (max 1 n)))
11018 (concat sign (make-string n ?I)))
11020 ((string-match "^[0-9]+" ref)
11021 (setq n (string-to-number (concat sign ref)))
11022 (setq n (+ n (if decr -1 1)))
11024 (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
11025 (number-to-string (max 1 n))))
11027 ((string-match "^[a-zA-Z]+" ref)
11028 (org-number-to-letters
11029 (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
11031 (t (error "Cannot shift reference"))))))
11033 (defun org-table-fedit-toggle-coordinates ()
11034 "Toggle the display of coordinates in the refrenced table."
11036 (let ((pos (marker-position org-pos)))
11037 (with-current-buffer (marker-buffer org-pos)
11040 (org-table-toggle-coordinate-overlays)))))
11042 (defun org-table-fedit-finish (&optional arg)
11043 "Parse the buffer for formula definitions and install them.
11044 With prefix ARG, apply the new formulas to the table."
11046 (org-table-remove-rectangle-highlight)
11047 (if org-table-use-standard-references
11049 (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
11050 (setq org-table-buffer-is-an nil)))
11051 (let ((pos org-pos) eql var form)
11052 (goto-char (point-min))
11053 (while (re-search-forward
11054 "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
11056 (setq var (if (match-end 2) (match-string 2) (match-string 1))
11057 form (match-string 3))
11058 (setq form (org-trim form))
11059 (when (not (equal form ""))
11060 (while (string-match "[ \t]*\n[ \t]*" form)
11061 (setq form (replace-match " " t t form)))
11062 (when (assoc var eql)
11063 (error "Double formulas for %s" var))
11064 (push (cons var form) eql)))
11066 (set-window-configuration org-window-configuration)
11067 (select-window (get-buffer-window (marker-buffer pos)))
11069 (unless (org-at-table-p)
11070 (error "Lost table position - cannot install formulae"))
11071 (org-table-store-formulas eql)
11072 (move-marker pos nil)
11073 (kill-buffer "*Edit Formulas*")
11075 (org-table-recalculate 'all)
11076 (message "New formulas installed - press C-u C-c C-c to apply."))))
11078 (defun org-table-fedit-abort ()
11079 "Abort editing formulas, without installing the changes."
11081 (org-table-remove-rectangle-highlight)
11082 (let ((pos org-pos))
11083 (set-window-configuration org-window-configuration)
11084 (select-window (get-buffer-window (marker-buffer pos)))
11086 (move-marker pos nil)
11087 (message "Formula editing aborted without installing changes")))
11089 (defun org-table-fedit-lisp-indent ()
11090 "Pretty-print and re-indent Lisp expressions in the Formula Editor."
11092 (let ((pos (point)) beg end ind)
11093 (beginning-of-line 1)
11095 ((looking-at "[ \t]")
11097 (call-interactively 'lisp-indent-line))
11098 ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
11099 ((not (fboundp 'pp-buffer))
11100 (error "Cannot pretty-print. Command `pp-buffer' is not available."))
11101 ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
11102 (goto-char (- (match-end 0) 2))
11104 (setq ind (make-string (current-column) ?\ ))
11105 (condition-case nil (forward-sexp 1)
11107 (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
11110 (narrow-to-region beg end)
11111 (if (eq last-command this-command)
11113 (goto-char (point-min))
11114 (setq this-command nil)
11115 (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
11116 (replace-match " ")))
11118 (untabify (point-min) (point-max))
11119 (goto-char (1+ (point-min)))
11120 (while (re-search-forward "^." nil t)
11121 (beginning-of-line 1)
11123 (goto-char (point-max))
11124 (backward-delete-char 1)))
11128 (defvar org-show-positions nil)
11130 (defun org-table-show-reference (&optional local)
11131 "Show the location/value of the $ expression at point."
11133 (org-table-remove-rectangle-highlight)
11135 (let ((pos (if local (point) org-pos))
11137 (org-inhibit-highlight-removal t)
11138 (win (selected-window))
11139 (org-show-positions nil)
11140 var name e what match dest)
11141 (if local (org-table-get-specials))
11143 ((or (org-at-regexp-p org-table-range-regexp2)
11144 (org-at-regexp-p org-table-translate-regexp)
11145 (org-at-regexp-p org-table-range-regexp))
11148 (org-table-convert-refs-to-rc (match-string 0))))
11150 ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
11151 ((org-at-regexp-p "\\$[0-9]+") 'column)
11153 (t (error "No reference at point")))
11154 match (and what (or match (match-string 0))))
11155 (when (and match (not (equal (match-beginning 0) (point-at-bol))))
11156 (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
11157 'secondary-selection))
11158 (org-add-hook 'before-change-functions
11159 'org-table-remove-rectangle-highlight)
11160 (if (eq what 'name) (setq var (substring match 1)))
11161 (when (eq what 'range)
11162 (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
11163 (setq match (org-table-formula-substitute-names match)))
11167 (re-search-backward "^\\S-" nil t)
11168 (beginning-of-line 1)
11169 (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
11172 (org-table-convert-refs-to-rc (match-string 1))))
11173 (org-table-add-rectangle-overlay
11174 (match-beginning 1) (match-end 1) face2))))
11175 (if (and (markerp pos) (marker-buffer pos))
11176 (if (get-buffer-window (marker-buffer pos))
11177 (select-window (get-buffer-window (marker-buffer pos)))
11178 (org-switch-to-buffer-other-window (get-buffer-window
11179 (marker-buffer pos)))))
11181 (org-table-force-dataline)
11183 (setq name (substring dest 1))
11185 ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
11186 (setq e (assoc name org-table-named-field-locations))
11187 (goto-line (nth 1 e))
11188 (org-table-goto-column (nth 2 e)))
11189 ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
11190 (let ((l (string-to-number (match-string 1 dest)))
11191 (c (string-to-number (match-string 2 dest))))
11192 (goto-line (aref org-table-dlines l))
11193 (org-table-goto-column c)))
11194 (t (org-table-goto-column (string-to-number name))))
11195 (move-marker pos (point))
11196 (org-table-highlight-rectangle nil nil face2))
11198 ((equal dest match))
11201 (condition-case nil
11203 (org-table-get-range match nil nil 'highlight))
11205 ((setq e (assoc var org-table-named-field-locations))
11206 (goto-line (nth 1 e))
11207 (org-table-goto-column (nth 2 e))
11208 (org-table-highlight-rectangle (point) (point))
11209 (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
11210 ((setq e (assoc var org-table-column-names))
11211 (org-table-goto-column (string-to-number (cdr e)))
11212 (org-table-highlight-rectangle (point) (point))
11213 (goto-char (org-table-begin))
11214 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
11217 (goto-char (match-beginning 1))
11218 (org-table-highlight-rectangle)
11219 (message "Named column (column %s)" (cdr e)))
11220 (error "Column name not found")))
11223 (org-table-goto-column (string-to-number (substring match 1)))
11224 (org-table-highlight-rectangle (point) (point))
11225 (message "Column %s" (substring match 1)))
11226 ((setq e (assoc var org-table-local-parameters))
11227 (goto-char (org-table-begin))
11228 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
11230 (goto-char (match-beginning 1))
11231 (org-table-highlight-rectangle)
11232 (message "Local parameter."))
11233 (error "Parameter not found")))
11236 ((not var) (error "No reference at point"))
11237 ((setq e (assoc var org-table-formula-constants-local))
11238 (message "Local Constant: $%s=%s in #+CONSTANTS line."
11240 ((setq e (assoc var org-table-formula-constants))
11241 (message "Constant: $%s=%s in `org-table-formula-constants'."
11243 ((setq e (and (fboundp 'constants-get) (constants-get var)))
11244 (message "Constant: $%s=%s, from `constants.el'%s."
11245 var e (format " (%s units)" constants-unit-system)))
11246 (t (error "Undefined name $%s" var)))))
11248 (when (and org-show-positions
11249 (not (memq this-command '(org-table-fedit-scroll
11250 org-table-fedit-scroll-down))))
11251 (push pos org-show-positions)
11252 (push org-table-current-begin-pos org-show-positions)
11253 (let ((min (apply 'min org-show-positions))
11254 (max (apply 'max org-show-positions)))
11255 (goto-char min) (recenter 0)
11257 (or (pos-visible-in-window-p max) (recenter -1))))
11258 (select-window win))))
11260 (defun org-table-force-dataline ()
11261 "Make sure the cursor is in a dataline in a table."
11262 (unless (save-excursion
11263 (beginning-of-line 1)
11264 (looking-at org-table-dataline-regexp))
11265 (let* ((re org-table-dataline-regexp)
11266 (p1 (save-excursion (re-search-forward re nil 'move)))
11267 (p2 (save-excursion (re-search-backward re nil 'move))))
11269 (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
11271 ((or p1 p2) (goto-char (or p1 p2)))
11272 (t (error "No table dataline around here"))))))
11274 (defun org-table-fedit-line-up ()
11275 "Move cursor one line up in the window showing the table."
11277 (org-table-fedit-move 'previous-line))
11279 (defun org-table-fedit-line-down ()
11280 "Move cursor one line down in the window showing the table."
11282 (org-table-fedit-move 'next-line))
11284 (defun org-table-fedit-move (command)
11285 "Move the cursor in the window shoinw the table.
11286 Use COMMAND to do the motion, repeat if necessary to end up in a data line."
11287 (let ((org-table-allow-automatic-line-recalculation nil)
11288 (pos org-pos) (win (selected-window)) p)
11289 (select-window (get-buffer-window (marker-buffer org-pos)))
11291 (call-interactively command)
11292 (while (and (org-at-table-p)
11293 (org-at-table-hline-p))
11294 (call-interactively command))
11295 (or (org-at-table-p) (goto-char p))
11296 (move-marker pos (point))
11297 (select-window win)))
11299 (defun org-table-fedit-scroll (N)
11301 (let ((other-window-scroll-buffer (marker-buffer org-pos)))
11302 (scroll-other-window N)))
11304 (defun org-table-fedit-scroll-down (N)
11306 (org-table-fedit-scroll (- N)))
11308 (defvar org-table-rectangle-overlays nil)
11310 (defun org-table-add-rectangle-overlay (beg end &optional face)
11311 "Add a new overlay."
11312 (let ((ov (org-make-overlay beg end)))
11313 (org-overlay-put ov 'face (or face 'secondary-selection))
11314 (push ov org-table-rectangle-overlays)))
11316 (defun org-table-highlight-rectangle (&optional beg end face)
11317 "Highlight rectangular region in a table."
11318 (setq beg (or beg (point)) end (or end (point)))
11319 (let ((b (min beg end))
11322 (and (boundp 'org-show-positions)
11323 (setq org-show-positions (cons b (cons e org-show-positions))))
11324 (goto-char (min beg end))
11325 (setq l1 (org-current-line)
11326 c1 (org-table-current-column))
11327 (goto-char (max beg end))
11328 (setq l2 (org-current-line)
11329 c2 (org-table-current-column))
11330 (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
11332 (beginning-of-line 1)
11333 (loop for line from l1 to l2 do
11334 (when (looking-at org-table-dataline-regexp)
11335 (org-table-goto-column c1)
11336 (skip-chars-backward "^|\n") (setq beg (point))
11337 (org-table-goto-column c2)
11338 (skip-chars-forward "^|\n") (setq end (point))
11339 (org-table-add-rectangle-overlay beg end face))
11340 (beginning-of-line 2))
11342 (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
11344 (defun org-table-remove-rectangle-highlight (&rest ignore)
11345 "Remove the rectangle overlays."
11346 (unless org-inhibit-highlight-removal
11347 (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
11348 (mapc 'org-delete-overlay org-table-rectangle-overlays)
11349 (setq org-table-rectangle-overlays nil)))
11351 (defvar org-table-coordinate-overlays nil
11352 "Collects the cooordinate grid overlays, so that they can be removed.")
11353 (make-variable-buffer-local 'org-table-coordinate-overlays)
11355 (defun org-table-overlay-coordinates ()
11356 "Add overlays to the table at point, to show row/column coordinates."
11358 (mapc 'org-delete-overlay org-table-coordinate-overlays)
11359 (setq org-table-coordinate-overlays nil)
11361 (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
11362 (goto-char (org-table-begin))
11363 (while (org-at-table-p)
11364 (setq eol (point-at-eol))
11365 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
11366 (push ov org-table-coordinate-overlays)
11367 (setq hline (looking-at org-table-hline-regexp))
11368 (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
11369 (format "%4d" (setq id (1+ id)))))
11370 (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
11373 (while (re-search-forward "[+|]\\(-+\\)" eol t)
11374 (setq beg (1+ (match-beginning 0))
11376 s1 (concat "$" (int-to-string ic))
11377 s2 (org-number-to-letters ic)
11378 str (if (eq org-table-use-standard-references t) s2 s1))
11379 (setq ov (org-make-overlay beg (+ beg (length str))))
11380 (push ov org-table-coordinate-overlays)
11381 (org-overlay-display ov str 'org-special-keyword 'evaporate)))
11382 (beginning-of-line 2)))))
11384 (defun org-table-toggle-coordinate-overlays ()
11385 "Toggle the display of Row/Column numbers in tables."
11387 (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
11388 (message "Row/Column number display turned %s"
11389 (if org-table-overlay-coordinates "on" "off"))
11390 (if (and (org-at-table-p) org-table-overlay-coordinates)
11392 (unless org-table-overlay-coordinates
11393 (mapc 'org-delete-overlay org-table-coordinate-overlays)
11394 (setq org-table-coordinate-overlays nil)))
11396 (defun org-table-toggle-formula-debugger ()
11397 "Toggle the formula debugger in tables."
11399 (setq org-table-formula-debug (not org-table-formula-debug))
11400 (message "Formula debugging has been turned %s"
11401 (if org-table-formula-debug "on" "off")))
11403 ;;; The orgtbl minor mode
11405 ;; Define a minor mode which can be used in other modes in order to
11406 ;; integrate the org-mode table editor.
11408 ;; This is really a hack, because the org-mode table editor uses several
11409 ;; keys which normally belong to the major mode, for example the TAB and
11410 ;; RET keys. Here is how it works: The minor mode defines all the keys
11411 ;; necessary to operate the table editor, but wraps the commands into a
11412 ;; function which tests if the cursor is currently inside a table. If that
11413 ;; is the case, the table editor command is executed. However, when any of
11414 ;; those keys is used outside a table, the function uses `key-binding' to
11415 ;; look up if the key has an associated command in another currently active
11416 ;; keymap (minor modes, major mode, global), and executes that command.
11417 ;; There might be problems if any of the keys used by the table editor is
11418 ;; otherwise used as a prefix key.
11420 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
11421 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
11422 ;; addresses this by checking explicitly for both bindings.
11424 ;; The optimized version (see variable `orgtbl-optimized') takes over
11425 ;; all keys which are bound to `self-insert-command' in the *global map*.
11426 ;; Some modes bind other commands to simple characters, for example
11427 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
11428 ;; active, this binding is ignored inside tables and replaced with a
11429 ;; modified self-insert.
11431 (defvar orgtbl-mode nil
11432 "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
11433 table editor in arbitrary modes.")
11434 (make-variable-buffer-local 'orgtbl-mode)
11436 (defvar orgtbl-mode-map (make-keymap)
11437 "Keymap for `orgtbl-mode'.")
11440 (defun turn-on-orgtbl ()
11441 "Unconditionally turn on `orgtbl-mode'."
11444 (defvar org-old-auto-fill-inhibit-regexp nil
11445 "Local variable used by `orgtbl-mode'")
11447 (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
11448 "Matches a line belonging to an orgtbl.")
11450 (defconst orgtbl-extra-font-lock-keywords
11451 (list (list (concat "^" orgtbl-line-start-regexp ".*")
11452 0 (quote 'org-table) 'prepend))
11453 "Extra font-lock-keywords to be added when orgtbl-mode is active.")
11456 (defun orgtbl-mode (&optional arg)
11457 "The `org-mode' table editor as a minor mode for use in other modes."
11460 ;; Exit without error, in case some hook functions calls this
11461 ;; by accident in org-mode.
11462 (message "Orgtbl-mode is not useful in org-mode, command ignored")
11464 (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
11467 (and (orgtbl-setup) (defun orgtbl-setup () nil))
11468 ;; Make sure we are first in minor-mode-map-alist
11469 (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
11470 (and c (setq minor-mode-map-alist
11471 (cons c (delq c minor-mode-map-alist)))))
11472 (org-set-local (quote org-table-may-need-update) t)
11473 (org-add-hook 'before-change-functions 'org-before-change-function
11475 (org-set-local 'org-old-auto-fill-inhibit-regexp
11476 auto-fill-inhibit-regexp)
11477 (org-set-local 'auto-fill-inhibit-regexp
11478 (if auto-fill-inhibit-regexp
11479 (concat orgtbl-line-start-regexp "\\|"
11480 auto-fill-inhibit-regexp)
11481 orgtbl-line-start-regexp))
11482 (org-add-to-invisibility-spec '(org-cwidth))
11483 (when (fboundp 'font-lock-add-keywords)
11484 (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
11485 (org-restart-font-lock))
11486 (easy-menu-add orgtbl-mode-menu)
11487 (run-hooks 'orgtbl-mode-hook))
11488 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
11489 (org-cleanup-narrow-column-properties)
11490 (org-remove-from-invisibility-spec '(org-cwidth))
11491 (remove-hook 'before-change-functions 'org-before-change-function t)
11492 (when (fboundp 'font-lock-remove-keywords)
11493 (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
11494 (org-restart-font-lock))
11495 (easy-menu-remove orgtbl-mode-menu)
11496 (force-mode-line-update 'all))))
11498 (defun org-cleanup-narrow-column-properties ()
11499 "Remove all properties related to narrow-column invisibility."
11501 (while (setq s (text-property-any s (point-max)
11502 'display org-narrow-column-arrow))
11503 (remove-text-properties s (1+ s) '(display t)))
11505 (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
11506 (remove-text-properties s (1+ s) '(org-cwidth t)))
11508 (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
11509 (remove-text-properties s (1+ s) '(invisible t)))))
11511 ;; Install it as a minor mode.
11512 (put 'orgtbl-mode :included t)
11513 (put 'orgtbl-mode :menu-tag "Org Table Mode")
11514 (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
11516 (defun orgtbl-make-binding (fun n &rest keys)
11517 "Create a function for binding in the table minor mode.
11518 FUN is the command to call inside a table. N is used to create a unique
11519 command name. KEYS are keys that should be checked in for a command
11520 to execute outside of tables."
11523 (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
11525 (concat "In tables, run `" (symbol-name fun) "'.\n"
11526 "Outside of tables, run the binding of `"
11527 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
11532 (list 'call-interactively (list 'quote fun))
11533 (list 'let '(orgtbl-mode)
11534 (list 'call-interactively
11536 (mapcar (lambda (k)
11537 (list 'key-binding k))
11539 '('orgtbl-error))))))))
11541 (defun orgtbl-error ()
11542 "Error when there is no default binding for a table key."
11544 (error "This key has no function outside tables"))
11546 (defun orgtbl-setup ()
11547 "Setup orgtbl keymaps."
11551 '([(meta shift left)] org-table-delete-column)
11552 '([(meta left)] org-table-move-column-left)
11553 '([(meta right)] org-table-move-column-right)
11554 '([(meta shift right)] org-table-insert-column)
11555 '([(meta shift up)] org-table-kill-row)
11556 '([(meta shift down)] org-table-insert-row)
11557 '([(meta up)] org-table-move-row-up)
11558 '([(meta down)] org-table-move-row-down)
11559 '("\C-c\C-w" org-table-cut-region)
11560 '("\C-c\M-w" org-table-copy-region)
11561 '("\C-c\C-y" org-table-paste-rectangle)
11562 '("\C-c-" org-table-insert-hline)
11563 '("\C-c}" org-table-toggle-coordinate-overlays)
11564 '("\C-c{" org-table-toggle-formula-debugger)
11565 '("\C-m" org-table-next-row)
11566 '([(shift return)] org-table-copy-down)
11567 '("\C-c\C-q" org-table-wrap-region)
11568 '("\C-c?" org-table-field-info)
11569 '("\C-c " org-table-blank-field)
11570 '("\C-c+" org-table-sum)
11571 '("\C-c=" org-table-eval-formula)
11572 '("\C-c'" org-table-edit-formulas)
11573 '("\C-c`" org-table-edit-field)
11574 '("\C-c*" org-table-recalculate)
11575 '("\C-c|" org-table-create-or-convert-from-region)
11576 '("\C-c^" org-table-sort-lines)
11577 '([(control ?#)] org-table-rotate-recalc-marks)))
11579 (while (setq elt (pop bindings))
11580 (setq nfunc (1+ nfunc))
11581 (setq key (org-key (car elt))
11583 cmd (orgtbl-make-binding fun nfunc key))
11584 (org-defkey orgtbl-mode-map key cmd))
11586 ;; Special treatment needed for TAB and RET
11587 (org-defkey orgtbl-mode-map [(return)]
11588 (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
11589 (org-defkey orgtbl-mode-map "\C-m"
11590 (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
11592 (org-defkey orgtbl-mode-map [(tab)]
11593 (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
11594 (org-defkey orgtbl-mode-map "\C-i"
11595 (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
11597 (org-defkey orgtbl-mode-map [(shift tab)]
11598 (orgtbl-make-binding 'org-table-previous-field 104
11599 [(shift tab)] [(tab)] "\C-i"))
11601 (org-defkey orgtbl-mode-map "\M-\C-m"
11602 (orgtbl-make-binding 'org-table-wrap-region 105
11603 "\M-\C-m" [(meta return)]))
11604 (org-defkey orgtbl-mode-map [(meta return)]
11605 (orgtbl-make-binding 'org-table-wrap-region 106
11606 [(meta return)] "\M-\C-m"))
11608 (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
11609 (when orgtbl-optimized
11610 ;; If the user wants maximum table support, we need to hijack
11611 ;; some standard editing functions
11612 (org-remap orgtbl-mode-map
11613 'self-insert-command 'orgtbl-self-insert-command
11614 'delete-char 'org-delete-char
11615 'delete-backward-char 'org-delete-backward-char)
11616 (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
11617 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
11619 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
11620 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
11621 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
11622 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
11624 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
11625 ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
11626 ["Copy Field from Above"
11627 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
11630 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
11631 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
11632 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
11633 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
11635 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
11636 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
11637 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
11638 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
11639 ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
11641 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
11643 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
11644 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
11645 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
11646 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
11649 ["Insert table template" orgtbl-insert-radio-table
11650 (assq major-mode orgtbl-radio-table-templates)]
11651 ["Comment/uncomment table" orgtbl-toggle-comment t])
11653 ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
11654 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
11655 ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
11656 ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
11657 ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
11658 ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
11659 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
11660 ["Sum Column/Rectangle" org-table-sum
11661 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
11662 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
11664 org-table-toggle-formula-debugger :active (org-at-table-p)
11666 :style toggle :selected org-table-formula-debug]
11667 ["Show Col/Row Numbers"
11668 org-table-toggle-coordinate-overlays :active (org-at-table-p)
11670 :style toggle :selected org-table-overlay-coordinates]
11674 (defun orgtbl-ctrl-c-ctrl-c (arg)
11675 "If the cursor is inside a table, realign the table.
11676 It it is a table to be sent away to a receiver, do it.
11677 With prefix arg, also recompute table."
11679 (let ((pos (point)) action)
11681 (beginning-of-line 1)
11682 (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
11683 ((looking-at "[ \t]*|") pos)
11684 ((looking-at "#\\+TBLFM:") 'recalc))))
11688 (org-table-maybe-eval-formula)
11690 (call-interactively 'org-table-recalculate)
11691 (org-table-maybe-recalculate-line))
11692 (call-interactively 'org-table-align)
11693 (orgtbl-send-table 'maybe))
11694 ((eq action 'recalc)
11696 (beginning-of-line 1)
11697 (skip-chars-backward " \r\n\t")
11698 (if (org-at-table-p)
11699 (org-call-with-arg 'org-table-recalculate t))))
11700 (t (let (orgtbl-mode)
11701 (call-interactively (key-binding "\C-c\C-c")))))))
11703 (defun orgtbl-tab (arg)
11704 "Justification and field motion for `orgtbl-mode'."
11706 (if arg (org-table-edit-field t)
11707 (org-table-justify-field-maybe)
11708 (org-table-next-field)))
11710 (defun orgtbl-ret ()
11711 "Justification and field motion for `orgtbl-mode'."
11713 (org-table-justify-field-maybe)
11714 (org-table-next-row))
11716 (defun orgtbl-self-insert-command (N)
11717 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
11718 If the cursor is in a table looking at whitespace, the whitespace is
11719 overwritten, and the table is not marked as requiring realignment."
11721 (if (and (org-at-table-p)
11723 (and org-table-auto-blank-field
11724 (member last-command
11725 '(orgtbl-hijacker-command-100
11726 orgtbl-hijacker-command-101
11727 orgtbl-hijacker-command-102
11728 orgtbl-hijacker-command-103
11729 orgtbl-hijacker-command-104
11730 orgtbl-hijacker-command-105))
11731 (org-table-blank-field))
11734 (looking-at "[^|\n]* +|"))
11735 (let (org-table-may-need-update)
11736 (goto-char (1- (match-end 0)))
11737 (delete-backward-char 1)
11738 (goto-char (match-beginning 0))
11739 (self-insert-command N))
11740 (setq org-table-may-need-update t)
11742 (call-interactively (key-binding (vector last-input-event))))))
11744 (defun org-force-self-insert (N)
11745 "Needed to enforce self-insert under remapping."
11747 (self-insert-command N))
11749 (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
11750 "Regula expression matching exponentials as produced by calc.")
11752 (defvar org-table-clean-did-remove-column nil)
11754 (defun orgtbl-export (table target)
11755 (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
11756 (lines (org-split-string table "[ \t]*\n[ \t]*"))
11757 org-table-last-alignment org-table-last-column-widths
11759 (if (not (fboundp func))
11760 (error "Cannot export orgtbl table to %s" target))
11761 (setq lines (org-table-clean-before-export lines))
11765 (if (string-match org-table-hline-regexp x)
11767 (org-split-string (org-trim x) "\\s-*|\\s-*")))
11769 (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
11771 (loop for i from (1- maxcol) downto 0 do
11772 (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
11773 (setq column (delq nil column))
11774 (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
11775 (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))
11776 (funcall func table nil)))
11778 (defun orgtbl-send-table (&optional maybe)
11779 "Send a tranformed version of this table to the receiver position.
11780 With argument MAYBE, fail quietly if no transformation is defined for
11784 (unless (org-at-table-p) (error "Not at a table"))
11785 ;; when non-interactive, we assume align has just happened.
11786 (when (interactive-p) (org-table-align))
11788 (goto-char (org-table-begin))
11789 (beginning-of-line 0)
11790 (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
11793 (error "Don't know how to transform this table."))))
11794 (let* ((name (match-string 1))
11796 (transform (intern (match-string 2)))
11797 (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
11798 (skip (plist-get params :skip))
11799 (skipcols (plist-get params :skipcols))
11800 (txt (buffer-substring-no-properties
11801 (org-table-begin) (org-table-end)))
11802 (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
11803 (lines (org-table-clean-before-export lines))
11804 (i0 (if org-table-clean-did-remove-column 2 1))
11807 (if (string-match org-table-hline-regexp x)
11809 (org-remove-by-index
11810 (org-split-string (org-trim x) "\\s-*|\\s-*")
11813 (fun (if (= i0 2) 'cdr 'identity))
11814 (org-table-last-alignment
11815 (org-remove-by-index (funcall fun org-table-last-alignment)
11817 (org-table-last-column-widths
11818 (org-remove-by-index (funcall fun org-table-last-column-widths)
11821 (unless (fboundp transform)
11822 (error "No such transformation function %s" transform))
11823 (setq txt (funcall transform table params))
11824 ;; Find the insertion place
11826 (goto-char (point-min))
11827 (unless (re-search-forward
11828 (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
11829 (error "Don't know where to insert translated table"))
11830 (goto-char (match-beginning 0))
11831 (beginning-of-line 2)
11833 (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
11834 (error "Cannot find end of insertion region"))
11835 (beginning-of-line 1)
11836 (delete-region beg (point))
11839 (message "Table converted and installed at receiver location"))))
11841 (defun org-remove-by-index (list indices &optional i0)
11842 "Remove the elements in LIST with indices in INDICES.
11843 First element has index 0, or I0 if given."
11846 (if (integerp indices) (setq indices (list indices)))
11847 (setq i0 (1- (or i0 0)))
11848 (delq :rm (mapcar (lambda (x)
11850 (if (memq i0 indices) :rm x))
11853 (defun orgtbl-toggle-comment ()
11854 "Comment or uncomment the orgtbl at point."
11856 (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
11857 (re2 (concat "^" orgtbl-line-start-regexp))
11858 (commented (save-excursion (beginning-of-line 1)
11859 (cond ((looking-at re1) t)
11860 ((looking-at re2) nil)
11861 (t (error "Not at an org table")))))
11862 (re (if commented re1 re2))
11865 (beginning-of-line 1)
11866 (while (looking-at re) (beginning-of-line 0))
11867 (beginning-of-line 2)
11869 (while (looking-at re) (beginning-of-line 2))
11870 (setq end (point)))
11871 (comment-region beg end (if commented '(4) nil))))
11873 (defun orgtbl-insert-radio-table ()
11874 "Insert a radio table template appropriate for this major mode."
11876 (let* ((e (assq major-mode orgtbl-radio-table-templates))
11879 (unless e (error "No radio table setup defined for %s" major-mode))
11880 (setq name (read-string "Table name: "))
11881 (while (string-match "%n" txt)
11882 (setq txt (replace-match name t t txt)))
11883 (or (bolp) (insert "\n"))
11888 (defun org-get-param (params header i sym &optional hsym)
11889 "Get parameter value for symbol SYM.
11890 If this is a header line, actually get the value for the symbol with an
11891 additional \"h\" inserted after the colon.
11892 If the value is a protperty list, get the element for the current column.
11893 Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
11894 (let ((val (plist-get params sym)))
11895 (and hsym header (setq val (or (plist-get params hsym) val)))
11896 (if (consp val) (plist-get val i) val)))
11898 (defun orgtbl-to-generic (table params)
11899 "Convert the orgtbl-mode TABLE to some other format.
11900 This generic routine can be used for many standard cases.
11901 TABLE is a list, each entry either the symbol `hline' for a horizontal
11902 separator line, or a list of fields for that line.
11903 PARAMS is a property list of parameters that can influence the conversion.
11904 For the generic converter, some parameters are obligatory: You need to
11905 specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
11906 :splice, you must have :tstart and :tend.
11908 Valid parameters are
11910 :tstart String to start the table. Ignored when :splice is t.
11911 :tend String to end the table. Ignored when :splice is t.
11913 :splice When set to t, return only table body lines, don't wrap
11914 them into :tstart and :tend. Default is nil.
11916 :hline String to be inserted on horizontal separation lines.
11917 May be nil to ignore hlines.
11919 :lstart String to start a new table line.
11920 :lend String to end a table line
11921 :sep Separator between two fields
11922 :lfmt Format for entire line, with enough %s to capture all fields.
11923 If this is present, :lstart, :lend, and :sep are ignored.
11924 :fmt A format to be used to wrap the field, should contain
11925 %s for the original field value. For example, to wrap
11926 everything in dollars, you could use :fmt \"$%s$\".
11927 This may also be a property list with column numbers and
11928 formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
11930 :hlstart :hlend :hlsep :hlfmt :hfmt
11931 Same as above, specific for the header lines in the table.
11932 All lines before the first hline are treated as header.
11933 If any of these is not present, the data line value is used.
11935 :efmt Use this format to print numbers with exponentials.
11936 The format should have %s twice for inserting mantissa
11937 and exponent, for example \"%s\\\\times10^{%s}\". This
11938 may also be a property list with column numbers and
11939 formats. :fmt will still be applied after :efmt.
11941 In addition to this, the parameters :skip and :skipcols are always handled
11942 directly by `orgtbl-send-table'. See manual."
11945 (splicep (plist-get p :splice))
11946 (hline (plist-get p :hline))
11947 rtn line i fm efm lfmt h)
11949 ;; Do we have a header?
11950 (if (and (not splicep) (listp (car table)) (memq 'hline table))
11955 (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
11957 ;; Now loop over all lines
11958 (while (setq line (pop table))
11959 (if (eq line 'hline)
11960 ;; A horizontal separator line
11961 (progn (if hline (push hline rtn))
11962 (setq h nil)) ; no longer in header
11963 ;; A normal line. Convert the fields, push line onto the result list
11969 fm (org-get-param p h i :fmt :hfmt)
11970 efm (org-get-param p h i :efmt))
11971 (if (and efm (string-match orgtbl-exp-regexp f))
11973 efm (match-string 1 f) (match-string 2 f))))
11974 (if fm (setq f (format fm f)))
11977 (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
11978 (push (apply 'format lfmt line) rtn)
11980 (org-get-param p h i :lstart :hlstart)
11981 (mapconcat 'identity line (org-get-param p h i :sep :hsep))
11982 (org-get-param p h i :lend :hlend))
11986 (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
11988 (mapconcat 'identity (nreverse rtn) "\n")))
11990 (defun orgtbl-to-latex (table params)
11991 "Convert the orgtbl-mode TABLE to LaTeX.
11992 TABLE is a list, each entry either the symbol `hline' for a horizontal
11993 separator line, or a list of fields for that line.
11994 PARAMS is a property list of parameters that can influence the conversion.
11995 Supports all parameters from `orgtbl-to-generic'. Most important for
11998 :splice When set to t, return only table body lines, don't wrap
11999 them into a tabular environment. Default is nil.
12001 :fmt A format to be used to wrap the field, should contain %s for the
12002 original field value. For example, to wrap everything in dollars,
12003 use :fmt \"$%s$\". This may also be a property list with column
12004 numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
12006 :efmt Format for transforming numbers with exponentials. The format
12007 should have %s twice for inserting mantissa and exponent, for
12008 example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
12009 This may also be a property list with column numbers and formats.
12011 The general parameters :skip and :skipcols have already been applied when
12012 this function is called."
12013 (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
12014 org-table-last-alignment ""))
12017 :tstart (concat "\\begin{tabular}{" alignment "}")
12018 :tend "\\end{tabular}"
12019 :lstart "" :lend " \\\\" :sep " & "
12020 :efmt "%s\\,(%s)" :hline "\\hline")))
12021 (orgtbl-to-generic table (org-combine-plists params2 params))))
12023 (defun orgtbl-to-html (table params)
12024 "Convert the orgtbl-mode TABLE to LaTeX.
12025 TABLE is a list, each entry either the symbol `hline' for a horizontal
12026 separator line, or a list of fields for that line.
12027 PARAMS is a property list of parameters that can influence the conversion.
12028 Currently this function recognizes the following parameters:
12030 :splice When set to t, return only table body lines, don't wrap
12031 them into a <table> environment. Default is nil.
12033 The general parameters :skip and :skipcols have already been applied when
12034 this function is called. The function does *not* use `orgtbl-to-generic',
12035 so you cannot specify parameters for it."
12036 (let* ((splicep (plist-get params :splice))
12038 ;; Just call the formatter we already have
12039 ;; We need to make text lines for it, so put the fields back together.
12040 (setq html (org-format-org-table-html
12045 (concat "| " (mapconcat 'identity x " | ") " |")))
12048 (if (string-match "\n+\\'" html)
12049 (setq html (replace-match "" t t html)))
12052 (defun orgtbl-to-texinfo (table params)
12053 "Convert the orgtbl-mode TABLE to TeXInfo.
12054 TABLE is a list, each entry either the symbol `hline' for a horizontal
12055 separator line, or a list of fields for that line.
12056 PARAMS is a property list of parameters that can influence the conversion.
12057 Supports all parameters from `orgtbl-to-generic'. Most important for
12060 :splice nil/t When set to t, return only table body lines, don't wrap
12061 them into a multitable environment. Default is nil.
12063 :fmt fmt A format to be used to wrap the field, should contain
12064 %s for the original field value. For example, to wrap
12065 everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
12066 This may also be a property list with column numbers and
12067 formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
12069 :cf \"f1 f2..\" The column fractions for the table. By default these
12070 are computed automatically from the width of the columns
12073 The general parameters :skip and :skipcols have already been applied when
12074 this function is called."
12075 (let* ((total (float (apply '+ org-table-last-column-widths)))
12076 (colfrac (or (plist-get params :cf)
12078 (lambda (x) (format "%.3f" (/ (float x) total)))
12079 org-table-last-column-widths " ")))
12082 :tstart (concat "@multitable @columnfractions " colfrac)
12083 :tend "@end multitable"
12084 :lstart "@item " :lend "" :sep " @tab "
12085 :hlstart "@headitem ")))
12086 (orgtbl-to-generic table (org-combine-plists params2 params))))
12090 ;;; Link abbreviations
12092 (defun org-link-expand-abbrev (link)
12093 "Apply replacements as defined in `org-link-abbrev-alist."
12094 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
12095 (let* ((key (match-string 1 link))
12096 (as (or (assoc key org-link-abbrev-alist-local)
12097 (assoc key org-link-abbrev-alist)))
12098 (tag (and (match-end 2) (match-string 3 link)))
12102 (setq rpl (cdr as))
12104 ((symbolp rpl) (funcall rpl tag))
12105 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
12106 (t (concat rpl tag)))))
12109 ;;; Storing and inserting links
12111 (defvar org-insert-link-history nil
12112 "Minibuffer history for links inserted with `org-insert-link'.")
12114 (defvar org-stored-links nil
12115 "Contains the links stored with `org-store-link'.")
12117 (defvar org-store-link-plist nil
12118 "Plist with info about the most recently link created with `org-store-link'.")
12120 (defvar org-link-protocols nil
12121 "Link protocols added to Org-mode using `org-add-link-type'.")
12123 (defvar org-store-link-functions nil
12124 "List of functions that are called to create and store a link.
12125 Each function will be called in turn until one returns a non-nil
12126 value. Each function should check if it is responsible for creating
12127 this link (for example by looking at the major mode).
12128 If not, it must exit and return nil.
12129 If yes, it should return a non-nil value after a calling
12130 `org-store-link-props' with a list of properties and values.
12131 Special properties are:
12133 :type The link prefix. like \"http\". This must be given.
12134 :link The link, like \"http://www.astro.uva.nl/~dominik\".
12135 This is obligatory as well.
12136 :description Optional default description for the second pair
12137 of brackets in an Org-mode link. The user can still change
12138 this when inserting this link into an Org-mode buffer.
12140 In addition to these, any additional properties can be specified
12141 and then used in remember templates.")
12143 (defun org-add-link-type (type &optional follow publish)
12144 "Add TYPE to the list of `org-link-types'.
12145 Re-compute all regular expressions depending on `org-link-types'
12146 FOLLOW and PUBLISH are two functions. Both take the link path as
12148 FOLLOW should do whatever is necessary to follow the link, for example
12149 to find a file or display a mail message.
12151 PUBLISH takes the path and retuns the string that should be used when
12152 this document is published. FIMXE: This is actually not yet implemented."
12153 (add-to-list 'org-link-types type t)
12154 (org-make-link-regexps)
12155 (add-to-list 'org-link-protocols
12156 (list type follow publish)))
12158 (defun org-add-agenda-custom-command (entry)
12159 "Replace or add a command in `org-agenda-custom-commands'.
12160 This is mostly for hacking and trying a new command - once the command
12161 works you probably want to add it to `org-agenda-custom-commands' for good."
12162 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
12164 (setcdr ass (cdr entry))
12165 (push entry org-agenda-custom-commands))))
12168 (defun org-store-link (arg)
12169 "\\<org-mode-map>Store an org-link to the current location.
12170 This link is added to `org-stored-links' and can later be inserted
12171 into an org-buffer with \\[org-insert-link].
12173 For some link types, a prefix arg is interpreted:
12174 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
12175 For file links, arg negates `org-context-in-file-links'."
12178 (setq org-store-link-plist nil) ; reset
12179 (let (link cpltxt desc description search txt)
12182 ((run-hook-with-args-until-success 'org-store-link-functions)
12183 (setq link (plist-get org-store-link-plist :link)
12184 desc (or (plist-get org-store-link-plist :description) link)))
12186 ((eq major-mode 'bbdb-mode)
12187 (let ((name (bbdb-record-name (bbdb-current-record)))
12188 (company (bbdb-record-getprop (bbdb-current-record) 'company)))
12189 (setq cpltxt (concat "bbdb:" (or name company))
12190 link (org-make-link cpltxt))
12191 (org-store-link-props :type "bbdb" :name name :company company)))
12193 ((eq major-mode 'Info-mode)
12194 (setq link (org-make-link "info:"
12195 (file-name-nondirectory Info-current-file)
12196 ":" Info-current-node))
12197 (setq cpltxt (concat (file-name-nondirectory Info-current-file)
12198 ":" Info-current-node))
12199 (org-store-link-props :type "info" :file Info-current-file
12200 :node Info-current-node))
12202 ((eq major-mode 'calendar-mode)
12203 (let ((cd (calendar-cursor-to-date)))
12205 (format-time-string
12206 (car org-time-stamp-formats)
12207 (apply 'encode-time
12208 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
12210 (org-store-link-props :type "calendar" :date cd)))
12212 ((or (eq major-mode 'vm-summary-mode)
12213 (eq major-mode 'vm-presentation-mode))
12214 (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
12215 (vm-follow-summary-cursor)
12217 (vm-select-folder-buffer)
12218 (let* ((message (car vm-message-pointer))
12219 (folder buffer-file-name)
12220 (subject (vm-su-subject message))
12221 (to (vm-get-header-contents message "To"))
12222 (from (vm-get-header-contents message "From"))
12223 (message-id (vm-su-message-id message)))
12224 (org-store-link-props :type "vm" :from from :to to :subject subject
12225 :message-id message-id)
12226 (setq message-id (org-remove-angle-brackets message-id))
12227 (setq folder (abbreviate-file-name folder))
12228 (if (string-match (concat "^" (regexp-quote vm-folder-directory))
12230 (setq folder (replace-match "" t t folder)))
12231 (setq cpltxt (org-email-link-description))
12232 (setq link (org-make-link "vm:" folder "#" message-id)))))
12234 ((eq major-mode 'wl-summary-mode)
12235 (let* ((msgnum (wl-summary-message-number))
12236 (message-id (elmo-message-field wl-summary-buffer-elmo-folder
12237 msgnum 'message-id))
12239 (if (fboundp 'elmo-message-entity)
12240 (elmo-message-entity
12241 wl-summary-buffer-elmo-folder msgnum)
12242 (elmo-msgdb-overview-get-entity
12243 msgnum (wl-summary-buffer-msgdb))))
12244 (from (wl-summary-line-from))
12245 (to (car (elmo-message-entity-field wl-message-entity 'to)))
12246 (subject (let (wl-thr-indent-string wl-parent-message-entity)
12247 (wl-summary-line-subject))))
12248 (org-store-link-props :type "wl" :from from :to to
12249 :subject subject :message-id message-id)
12250 (setq message-id (org-remove-angle-brackets message-id))
12251 (setq cpltxt (org-email-link-description))
12252 (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
12255 ((or (equal major-mode 'mh-folder-mode)
12256 (equal major-mode 'mh-show-mode))
12257 (let ((from (org-mhe-get-header "From:"))
12258 (to (org-mhe-get-header "To:"))
12259 (message-id (org-mhe-get-header "Message-Id:"))
12260 (subject (org-mhe-get-header "Subject:")))
12261 (org-store-link-props :type "mh" :from from :to to
12262 :subject subject :message-id message-id)
12263 (setq cpltxt (org-email-link-description))
12264 (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
12265 (org-remove-angle-brackets message-id)))))
12267 ((or (eq major-mode 'rmail-mode)
12268 (eq major-mode 'rmail-summary-mode))
12269 (save-window-excursion
12271 (when (eq major-mode 'rmail-summary-mode)
12272 (rmail-show-message rmail-current-message))
12273 (rmail-narrow-to-non-pruned-header)
12274 (let ((folder buffer-file-name)
12275 (message-id (mail-fetch-field "message-id"))
12276 (from (mail-fetch-field "from"))
12277 (to (mail-fetch-field "to"))
12278 (subject (mail-fetch-field "subject")))
12279 (org-store-link-props
12280 :type "rmail" :from from :to to
12281 :subject subject :message-id message-id)
12282 (setq message-id (org-remove-angle-brackets message-id))
12283 (setq cpltxt (org-email-link-description))
12284 (setq link (org-make-link "rmail:" folder "#" message-id)))
12285 (rmail-show-message rmail-current-message))))
12287 ((eq major-mode 'gnus-group-mode)
12288 (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
12289 (gnus-group-group-name)) ; version
12290 ((fboundp 'gnus-group-name)
12293 (unless group (error "Not on a group"))
12294 (org-store-link-props :type "gnus" :group group)
12295 (setq cpltxt (concat
12296 (if (org-xor arg org-usenet-links-prefer-google)
12297 "http://groups.google.com/groups?group="
12300 link (org-make-link cpltxt))))
12302 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
12303 (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
12304 (let* ((group gnus-newsgroup-name)
12305 (article (gnus-summary-article-number))
12306 (header (gnus-summary-article-header article))
12307 (from (mail-header-from header))
12308 (message-id (mail-header-id header))
12309 (date (mail-header-date header))
12310 (subject (gnus-summary-subject-string)))
12311 (org-store-link-props :type "gnus" :from from :subject subject
12312 :message-id message-id :group group)
12313 (setq cpltxt (org-email-link-description))
12314 (if (org-xor arg org-usenet-links-prefer-google)
12318 (format "http://groups.google.com/groups?as_umsgid=%s"
12319 (org-fixup-message-id-for-http message-id))))
12320 (setq link (org-make-link "gnus:" group
12321 "#" (number-to-string article))))))
12323 ((eq major-mode 'w3-mode)
12324 (setq cpltxt (url-view-url t)
12325 link (org-make-link cpltxt))
12326 (org-store-link-props :type "w3" :url (url-view-url t)))
12328 ((eq major-mode 'w3m-mode)
12329 (setq cpltxt (or w3m-current-title w3m-current-url)
12330 link (org-make-link w3m-current-url))
12331 (org-store-link-props :type "w3m" :url (url-view-url t)))
12333 ((setq search (run-hook-with-args-until-success
12334 'org-create-file-search-functions))
12335 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
12337 (setq cpltxt (or description link)))
12339 ((eq major-mode 'image-mode)
12340 (setq cpltxt (concat "file:"
12341 (abbreviate-file-name buffer-file-name))
12342 link (org-make-link cpltxt))
12343 (org-store-link-props :type "image" :file buffer-file-name))
12345 ((eq major-mode 'dired-mode)
12346 ;; link to the file in the current line
12347 (setq cpltxt (concat "file:"
12348 (abbreviate-file-name
12350 (dired-get-filename nil t))))
12351 link (org-make-link cpltxt)))
12353 ((and buffer-file-name (org-mode-p))
12354 ;; Just link to current headline
12355 (setq cpltxt (concat "file:"
12356 (abbreviate-file-name buffer-file-name)))
12357 ;; Add a context search string
12358 (when (org-xor org-context-in-file-links arg)
12359 ;; Check if we are on a target
12360 (if (org-in-regexp "<<\\(.*?\\)>>")
12361 (setq cpltxt (concat cpltxt "::" (match-string 1)))
12363 ((org-on-heading-p) nil)
12364 ((org-region-active-p)
12365 (buffer-substring (region-beginning) (region-end)))
12366 (t (buffer-substring (point-at-bol) (point-at-eol)))))
12367 (when (or (null txt) (string-match "\\S-" txt))
12369 (concat cpltxt "::" (org-make-org-heading-search-string txt))
12371 (if (string-match "::\\'" cpltxt)
12372 (setq cpltxt (substring cpltxt 0 -2)))
12373 (setq link (org-make-link cpltxt)))
12375 ((buffer-file-name (buffer-base-buffer))
12376 ;; Just link to this file here.
12377 (setq cpltxt (concat "file:"
12378 (abbreviate-file-name
12379 (buffer-file-name (buffer-base-buffer)))))
12380 ;; Add a context string
12381 (when (org-xor org-context-in-file-links arg)
12382 (setq txt (if (org-region-active-p)
12383 (buffer-substring (region-beginning) (region-end))
12384 (buffer-substring (point-at-bol) (point-at-eol))))
12385 ;; Only use search option if there is some text.
12386 (when (string-match "\\S-" txt)
12388 (concat cpltxt "::" (org-make-org-heading-search-string txt))
12390 (setq link (org-make-link cpltxt)))
12393 (error "Cannot link to a buffer which is not visiting a file"))
12395 (t (setq link nil)))
12397 (if (consp link) (setq cpltxt (car link) link (cdr link)))
12398 (setq link (or link cpltxt)
12399 desc (or desc cpltxt))
12400 (if (equal desc "NONE") (setq desc nil))
12402 (if (and (interactive-p) link)
12404 (setq org-stored-links
12405 (cons (list link desc) org-stored-links))
12406 (message "Stored: %s" (or desc link)))
12407 (and link (org-make-link-string link desc)))))
12409 (defun org-store-link-props (&rest plist)
12410 "Store link properties, extract names and addresses."
12412 (when (setq x (plist-get plist :from))
12413 (setq adr (mail-extract-address-components x))
12414 (plist-put plist :fromname (car adr))
12415 (plist-put plist :fromaddress (nth 1 adr)))
12416 (when (setq x (plist-get plist :to))
12417 (setq adr (mail-extract-address-components x))
12418 (plist-put plist :toname (car adr))
12419 (plist-put plist :toaddress (nth 1 adr))))
12420 (let ((from (plist-get plist :from))
12421 (to (plist-get plist :to)))
12422 (when (and from to org-from-is-user-regexp)
12423 (plist-put plist :fromto
12424 (if (string-match org-from-is-user-regexp from)
12426 (concat "from %f")))))
12427 (setq org-store-link-plist plist))
12429 (defun org-email-link-description (&optional fmt)
12430 "Return the description part of an email link.
12431 This takes information from `org-store-link-plist' and formats it
12432 according to FMT (default from `org-email-link-description-format')."
12433 (setq fmt (or fmt org-email-link-description-format))
12434 (let* ((p org-store-link-plist)
12435 (to (plist-get p :toaddress))
12436 (from (plist-get p :fromaddress))
12439 (cons "%c" (plist-get p :fromto))
12440 (cons "%F" (plist-get p :from))
12441 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
12442 (cons "%T" (plist-get p :to))
12443 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
12444 (cons "%s" (plist-get p :subject))
12445 (cons "%m" (plist-get p :message-id)))))
12446 (when (string-match "%c" fmt)
12447 ;; Check if the user wrote this message
12448 (if (and org-from-is-user-regexp from to
12449 (save-match-data (string-match org-from-is-user-regexp from)))
12450 (setq fmt (replace-match "to %t" t t fmt))
12451 (setq fmt (replace-match "from %f" t t fmt))))
12452 (org-replace-escapes fmt table)))
12454 (defun org-make-org-heading-search-string (&optional string heading)
12455 "Make search string for STRING or current headline."
12457 (let ((s (or string (org-get-heading))))
12458 (unless (and string (not heading))
12459 ;; We are using a headline, clean up garbage in there.
12460 (if (string-match org-todo-regexp s)
12461 (setq s (replace-match "" t t s)))
12462 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
12463 (setq s (replace-match "" t t s)))
12464 (setq s (org-trim s))
12465 (if (string-match (concat "^\\(" org-quote-string "\\|"
12466 org-comment-string "\\)") s)
12467 (setq s (replace-match "" t t s)))
12468 (while (string-match org-ts-regexp s)
12469 (setq s (replace-match "" t t s))))
12470 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
12471 (setq s (replace-match " " t t s)))
12472 (or string (setq s (concat "*" s))) ; Add * for headlines
12473 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
12475 (defun org-make-link (&rest strings)
12476 "Concatenate STRINGS."
12477 (apply 'concat strings))
12479 (defun org-make-link-string (link &optional description)
12480 "Make a link with brackets, consisting of LINK and DESCRIPTION."
12481 (unless (string-match "\\S-" link)
12482 (error "Empty link"))
12483 (when (stringp description)
12484 ;; Remove brackets from the description, they are fatal.
12485 (while (string-match "\\[" description)
12486 (setq description (replace-match "{" t t description)))
12487 (while (string-match "\\]" description)
12488 (setq description (replace-match "}" t t description))))
12489 (when (equal (org-link-escape link) description)
12490 ;; No description needed, it is identical
12491 (setq description nil))
12492 (when (and (not description)
12493 (not (equal link (org-link-escape link))))
12494 (setq description link))
12495 (concat "[[" (org-link-escape link) "]"
12496 (if description (concat "[" description "]") "")
12499 (defconst org-link-escape-chars
12503 (?\340 . "%E0") ; `a
12504 (?\342 . "%E2") ; ^a
12505 (?\347 . "%E7") ; ,c
12506 (?\350 . "%E8") ; `e
12507 (?\351 . "%E9") ; 'e
12508 (?\352 . "%EA") ; ^e
12509 (?\356 . "%EE") ; ^i
12510 (?\364 . "%F4") ; ^o
12511 (?\371 . "%F9") ; `u
12512 (?\373 . "%FB") ; ^u
12518 "Association list of escapes for some characters problematic in links.
12519 This is the list that is used for internal purposes.")
12521 (defconst org-link-escape-chars-browser
12522 '((?\ . "%20")) ; 32 for the SPC char
12523 "Association list of escapes for some characters problematic in links.
12524 This is the list that is used before handing over to the browser.")
12526 (defun org-link-escape (text &optional table)
12527 "Escape charaters in TEXT that are problematic for links."
12528 (setq table (or table org-link-escape-chars))
12530 (let ((re (mapconcat (lambda (x) (regexp-quote
12531 (char-to-string (car x))))
12533 (while (string-match re text)
12536 (cdr (assoc (string-to-char (match-string 0 text))
12541 (defun org-link-unescape (text &optional table)
12542 "Reverse the action of `org-link-escape'."
12543 (setq table (or table org-link-escape-chars))
12545 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
12547 (while (string-match re text)
12550 (char-to-string (car (rassoc (match-string 0 text) table)))
12554 (defun org-xor (a b)
12558 (defun org-get-header (header)
12559 "Find a header field in the current buffer."
12561 (goto-char (point-min))
12562 (let ((case-fold-search t) s)
12565 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
12566 (setq s (match-string 1)))
12567 (while (string-match "\"" s)
12568 (setq s (replace-match "" t t s)))
12569 (if (string-match "[<(].*" s)
12570 (setq s (replace-match "" t t s))))
12571 ((eq header 'message-id)
12572 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
12573 (setq s (match-string 1))))
12574 ((eq header 'subject)
12575 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
12576 (setq s (match-string 1)))))
12577 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
12578 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
12582 (defun org-fixup-message-id-for-http (s)
12583 "Replace special characters in a message id, so it can be used in an http query."
12584 (while (string-match "<" s)
12585 (setq s (replace-match "%3C" t t s)))
12586 (while (string-match ">" s)
12587 (setq s (replace-match "%3E" t t s)))
12588 (while (string-match "@" s)
12589 (setq s (replace-match "%40" t t s)))
12593 (defun org-insert-link-global ()
12594 "Insert a link like Org-mode does.
12595 This command can be called in any mode to insert a link in Org-mode syntax."
12597 (org-run-like-in-org-mode 'org-insert-link))
12599 (defun org-insert-link (&optional complete-file)
12600 "Insert a link. At the prompt, enter the link.
12602 Completion can be used to select a link previously stored with
12603 `org-store-link'. When the empty string is entered (i.e. if you just
12604 press RET at the prompt), the link defaults to the most recently
12605 stored link. As SPC triggers completion in the minibuffer, you need to
12606 use M-SPC or C-q SPC to force the insertion of a space character.
12608 You will also be prompted for a description, and if one is given, it will
12609 be displayed in the buffer instead of the link.
12611 If there is already a link at point, this command will allow you to edit link
12612 and description parts.
12614 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
12615 selected using completion. The path to the file will be relative to
12616 the current directory if the file is in the current directory or a
12617 subdirectory. Otherwise, the link will be the absolute path as
12618 completed in the minibuffer (i.e. normally ~/path/to/file).
12620 With two \\[universal-argument] prefixes, enforce an absolute path even if the file
12621 is in the current directory or below.
12622 With three \\[universal-argument] prefixes, negate the meaning of
12623 `org-keep-stored-link-after-insertion'."
12625 (let* ((wcf (current-window-configuration))
12626 (region (if (org-region-active-p)
12627 (buffer-substring (region-beginning) (region-end))))
12628 (remove (and region (list (region-beginning) (region-end))))
12630 tmphist ; byte-compile incorrectly complains about this
12633 ((org-in-regexp org-bracket-link-regexp 1)
12634 ;; We do have a link at point, and we are going to edit it.
12635 (setq remove (list (match-beginning 0) (match-end 0)))
12636 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
12637 (setq link (read-string "Link: "
12639 (org-match-string-no-properties 1)))))
12640 ((or (org-in-regexp org-angle-link-re)
12641 (org-in-regexp org-plain-link-re))
12642 ;; Convert to bracket link
12643 (setq remove (list (match-beginning 0) (match-end 0))
12644 link (read-string "Link: "
12645 (org-remove-angle-brackets (match-string 0)))))
12646 ((equal complete-file '(4))
12647 ;; Completing read for file names.
12648 (setq file (read-file-name "File: "))
12649 (let ((pwd (file-name-as-directory (expand-file-name ".")))
12650 (pwd1 (file-name-as-directory (abbreviate-file-name
12651 (expand-file-name ".")))))
12653 ((equal complete-file '(16))
12654 (setq link (org-make-link
12656 (abbreviate-file-name (expand-file-name file)))))
12657 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
12658 (setq link (org-make-link "file:" (match-string 1 file))))
12659 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
12660 (expand-file-name file))
12661 (setq link (org-make-link
12662 "file:" (match-string 1 (expand-file-name file)))))
12663 (t (setq link (org-make-link "file:" file))))))
12665 ;; Read link, with completion for stored links.
12666 (with-output-to-temp-buffer "*Org Links*"
12667 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
12668 (when org-stored-links
12669 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
12672 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
12673 (reverse org-stored-links) "\n"))))
12674 (let ((cw (selected-window)))
12675 (select-window (get-buffer-window "*Org Links*"))
12676 (shrink-window-if-larger-than-buffer)
12677 (setq truncate-lines t)
12678 (select-window cw))
12679 ;; Fake a link history, containing the stored links.
12680 (setq tmphist (append (mapcar 'car org-stored-links)
12681 org-insert-link-history))
12683 (setq link (org-completing-read
12686 (mapcar (lambda (x) (list (concat (car x) ":")))
12687 (append org-link-abbrev-alist-local org-link-abbrev-alist))
12688 (mapcar (lambda (x) (list (concat x ":")))
12692 (or (car (car org-stored-links)))))
12693 (set-window-configuration wcf)
12694 (kill-buffer "*Org Links*"))
12695 (setq entry (assoc link org-stored-links))
12696 (or entry (push link org-insert-link-history))
12697 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
12698 (not org-keep-stored-link-after-insertion))
12699 (setq org-stored-links (delq (assoc link org-stored-links)
12700 org-stored-links)))
12701 (setq desc (or desc (nth 1 entry)))))
12703 (if (string-match org-plain-link-re link)
12704 ;; URL-like link, normalize the use of angular brackets.
12705 (setq link (org-make-link (org-remove-angle-brackets link))))
12707 ;; Check if we are linking to the current file with a search option
12708 ;; If yes, simplify the link by using only the search option.
12709 (when (and buffer-file-name
12710 (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
12711 (let* ((path (match-string 1 link))
12712 (case-fold-search nil)
12713 (search (match-string 2 link)))
12715 (if (equal (file-truename buffer-file-name) (file-truename path))
12716 ;; We are linking to this same file, with a search option
12717 (setq link search)))))
12719 ;; Check if we can/should use a relative path. If yes, simplify the link
12720 (when (string-match "\\<file:\\(.*\\)" link)
12721 (let* ((path (match-string 1 link))
12723 (case-fold-search nil))
12725 ((eq org-link-file-path-type 'absolute)
12726 (setq path (abbreviate-file-name (expand-file-name path))))
12727 ((eq org-link-file-path-type 'noabbrev)
12728 (setq path (expand-file-name path)))
12729 ((eq org-link-file-path-type 'relative)
12730 (setq path (file-relative-name path)))
12733 (if (string-match (concat "^" (regexp-quote
12734 (file-name-as-directory
12735 (expand-file-name "."))))
12736 (expand-file-name path))
12737 ;; We are linking a file with relative path name.
12738 (setq path (substring (expand-file-name path)
12739 (match-end 0)))))))
12740 (setq link (concat "file:" path))
12741 (if (equal desc origpath)
12742 (setq desc path))))
12744 (setq desc (read-string "Description: " desc))
12745 (unless (string-match "\\S-" desc) (setq desc nil))
12746 (if remove (apply 'delete-region remove))
12747 (insert (org-make-link-string link desc))))
12749 (defun org-completing-read (&rest args)
12750 (let ((minibuffer-local-completion-map
12751 (copy-keymap minibuffer-local-completion-map)))
12752 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
12753 (apply 'completing-read args)))
12755 ;;; Opening/following a link
12756 (defvar org-link-search-failed nil)
12758 (defun org-next-link ()
12759 "Move forward to the next link.
12760 If the link is in hidden text, expose it."
12762 (when (and org-link-search-failed (eq this-command last-command))
12763 (goto-char (point-min))
12764 (message "Link search wrapped back to beginning of buffer"))
12765 (setq org-link-search-failed nil)
12766 (let* ((pos (point))
12768 (a (assoc :link ct)))
12769 (if a (goto-char (nth 2 a)))
12770 (if (re-search-forward org-any-link-re nil t)
12772 (goto-char (match-beginning 0))
12773 (if (org-invisible-p) (org-show-context)))
12775 (setq org-link-search-failed t)
12776 (error "No further link found"))))
12778 (defun org-previous-link ()
12779 "Move backward to the previous link.
12780 If the link is in hidden text, expose it."
12782 (when (and org-link-search-failed (eq this-command last-command))
12783 (goto-char (point-max))
12784 (message "Link search wrapped back to end of buffer"))
12785 (setq org-link-search-failed nil)
12786 (let* ((pos (point))
12788 (a (assoc :link ct)))
12789 (if a (goto-char (nth 1 a)))
12790 (if (re-search-backward org-any-link-re nil t)
12792 (goto-char (match-beginning 0))
12793 (if (org-invisible-p) (org-show-context)))
12795 (setq org-link-search-failed t)
12796 (error "No further link found"))))
12798 (defun org-find-file-at-mouse (ev)
12799 "Open file link or URL at mouse."
12801 (mouse-set-point ev)
12802 (org-open-at-point 'in-emacs))
12804 (defun org-open-at-mouse (ev)
12805 "Open file link or URL at mouse."
12807 (mouse-set-point ev)
12808 (org-open-at-point))
12810 (defvar org-window-config-before-follow-link nil
12811 "The window configuration before following a link.
12812 This is saved in case the need arises to restore it.")
12814 (defvar org-open-link-marker (make-marker)
12815 "Marker pointing to the location where `org-open-at-point; was called.")
12818 (defun org-open-at-point-global ()
12819 "Follow a link like Org-mode does.
12820 This command can be called in any mode to follow a link that has
12823 (org-run-like-in-org-mode 'org-open-at-point))
12825 (defun org-open-at-point (&optional in-emacs)
12826 "Open link at or after point.
12827 If there is no link at point, this function will search forward up to
12828 the end of the current subtree.
12829 Normally, files will be opened by an appropriate application. If the
12830 optional argument IN-EMACS is non-nil, Emacs will visit the file."
12833 (move-marker org-open-link-marker (point))
12834 (setq org-window-config-before-follow-link (current-window-configuration))
12835 (org-remove-occur-highlights nil nil t)
12836 (if (org-at-timestamp-p t)
12837 (org-follow-timestamp-link)
12838 (let (type path link line search (pos (point)))
12841 (skip-chars-forward "^]\n\r")
12842 (when (org-in-regexp org-bracket-link-regexp)
12843 (setq link (org-link-unescape (org-match-string-no-properties 1)))
12844 (while (string-match " *\n *" link)
12845 (setq link (replace-match " " t t link)))
12846 (setq link (org-link-expand-abbrev link))
12847 (if (string-match org-link-re-with-space2 link)
12848 (setq type (match-string 1 link) path (match-string 2 link))
12849 (setq type "thisfile" path link))
12852 (when (get-text-property (point) 'org-linked-text)
12853 (setq type "thisfile"
12854 pos (if (get-text-property (1+ (point)) 'org-linked-text)
12855 (1+ (point)) (point))
12856 path (buffer-substring
12857 (previous-single-property-change pos 'org-linked-text)
12858 (next-single-property-change pos 'org-linked-text)))
12862 (when (or (org-in-regexp org-angle-link-re)
12863 (org-in-regexp org-plain-link-re))
12864 (setq type (match-string 1) path (match-string 2))
12866 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
12867 (setq type "tree-match"
12868 path (match-string 1))
12871 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
12873 path (match-string 1))
12874 (while (string-match ":" path)
12875 (setq path (replace-match "+" t t path)))
12876 (throw 'match t))))
12878 (error "No link found"))
12879 ;; Remove any trailing spaces in path
12880 (if (string-match " +\\'" path)
12881 (setq path (replace-match "" t t path)))
12885 ((assoc type org-link-protocols)
12886 (funcall (nth 1 (assoc type org-link-protocols)) path))
12888 ((equal type "mailto")
12889 (let ((cmd (car org-link-mailto-program))
12890 (args (cdr org-link-mailto-program)) args1
12891 (address path) (subject "") a)
12892 (if (string-match "\\(.*\\)::\\(.*\\)" path)
12893 (setq address (match-string 1 path)
12894 subject (org-link-escape (match-string 2 path))))
12897 ((not (stringp (car args))) (push (pop args) args1))
12898 (t (setq a (pop args))
12899 (if (string-match "%a" a)
12900 (setq a (replace-match address t t a)))
12901 (if (string-match "%s" a)
12902 (setq a (replace-match subject t t a)))
12904 (apply cmd (nreverse args1))))
12906 ((member type '("http" "https" "ftp" "news"))
12907 (browse-url (concat type ":" (org-link-escape
12908 path org-link-escape-chars-browser))))
12910 ((member type '("message"))
12911 (browse-url (concat type ":" path)))
12913 ((string= type "tags")
12914 (org-tags-view in-emacs path))
12915 ((string= type "thisfile")
12917 (switch-to-buffer-other-window
12918 (org-get-buffer-for-internal-link (current-buffer)))
12919 (org-mark-ring-push))
12920 (let ((cmd `(org-link-search
12922 ,(cond ((equal in-emacs '(4)) 'occur)
12923 ((equal in-emacs '(16)) 'org-occur)
12926 (condition-case nil (eval cmd)
12927 (error (progn (widen) (eval cmd))))))
12929 ((string= type "tree-match")
12930 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
12932 ((string= type "file")
12933 (if (string-match "::\\([0-9]+\\)\\'" path)
12934 (setq line (string-to-number (match-string 1 path))
12935 path (substring path 0 (match-beginning 0)))
12936 (if (string-match "::\\(.+\\)\\'" path)
12937 (setq search (match-string 1 path)
12938 path (substring path 0 (match-beginning 0)))))
12939 (if (string-match "[*?{]" (file-name-nondirectory path))
12941 (org-open-file path in-emacs line search)))
12943 ((string= type "news")
12944 (org-follow-gnus-link path))
12946 ((string= type "bbdb")
12947 (org-follow-bbdb-link path))
12949 ((string= type "info")
12950 (org-follow-info-link path))
12952 ((string= type "gnus")
12953 (let (group article)
12954 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12955 (error "Error in Gnus link"))
12956 (setq group (match-string 1 path)
12957 article (match-string 3 path))
12958 (org-follow-gnus-link group article)))
12960 ((string= type "vm")
12961 (let (folder article)
12962 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12963 (error "Error in VM link"))
12964 (setq folder (match-string 1 path)
12965 article (match-string 3 path))
12966 ;; in-emacs is the prefix arg, will be interpreted as read-only
12967 (org-follow-vm-link folder article in-emacs)))
12969 ((string= type "wl")
12970 (let (folder article)
12971 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12972 (error "Error in Wanderlust link"))
12973 (setq folder (match-string 1 path)
12974 article (match-string 3 path))
12975 (org-follow-wl-link folder article)))
12977 ((string= type "mhe")
12978 (let (folder article)
12979 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12980 (error "Error in MHE link"))
12981 (setq folder (match-string 1 path)
12982 article (match-string 3 path))
12983 (org-follow-mhe-link folder article)))
12985 ((string= type "rmail")
12986 (let (folder article)
12987 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12988 (error "Error in RMAIL link"))
12989 (setq folder (match-string 1 path)
12990 article (match-string 3 path))
12991 (org-follow-rmail-link folder article)))
12993 ((string= type "shell")
12995 (if (or (not org-confirm-shell-link-function)
12996 (funcall org-confirm-shell-link-function
12997 (format "Execute \"%s\" in shell? "
12998 (org-add-props cmd nil
12999 'face 'org-warning))))
13001 (message "Executing %s" cmd)
13002 (shell-command cmd))
13005 ((string= type "elisp")
13007 (if (or (not org-confirm-elisp-link-function)
13008 (funcall org-confirm-elisp-link-function
13009 (format "Execute \"%s\" as elisp? "
13010 (org-add-props cmd nil
13011 'face 'org-warning))))
13012 (message "%s => %s" cmd (eval (read cmd)))
13016 (browse-url-at-point)))))
13017 (move-marker org-open-link-marker nil)
13018 (run-hook-with-args 'org-follow-link-hook))
13022 (defvar org-create-file-search-functions nil
13023 "List of functions to construct the right search string for a file link.
13024 These functions are called in turn with point at the location to
13025 which the link should point.
13027 A function in the hook should first test if it would like to
13028 handle this file type, for example by checking the major-mode or
13029 the file extension. If it decides not to handle this file, it
13030 should just return nil to give other functions a chance. If it
13031 does handle the file, it must return the search string to be used
13032 when following the link. The search string will be part of the
13033 file link, given after a double colon, and `org-open-at-point'
13034 will automatically search for it. If special measures must be
13035 taken to make the search successful, another function should be
13036 added to the companion hook `org-execute-file-search-functions',
13039 A function in this hook may also use `setq' to set the variable
13040 `description' to provide a suggestion for the descriptive text to
13041 be used for this link when it gets inserted into an Org-mode
13042 buffer with \\[org-insert-link].")
13044 (defvar org-execute-file-search-functions nil
13045 "List of functions to execute a file search triggered by a link.
13047 Functions added to this hook must accept a single argument, the
13048 search string that was part of the file link, the part after the
13049 double colon. The function must first check if it would like to
13050 handle this search, for example by checking the major-mode or the
13051 file extension. If it decides not to handle this search, it
13052 should just return nil to give other functions a chance. If it
13053 does handle the search, it must return a non-nil value to keep
13054 other functions from trying.
13056 Each function can access the current prefix argument through the
13057 variable `current-prefix-argument'. Note that a single prefix is
13058 used to force opening a link in Emacs, so it may be good to only
13059 use a numeric or double prefix to guide the search function.
13061 In case this is needed, a function in this hook can also restore
13062 the window configuration before `org-open-at-point' was called using:
13064 (set-window-configuration org-window-config-before-follow-link)")
13066 (defun org-link-search (s &optional type avoid-pos)
13067 "Search for a link search option.
13068 If S is surrounded by forward slashes, it is interpreted as a
13069 regular expression. In org-mode files, this will create an `org-occur'
13070 sparse tree. In ordinary files, `occur' will be used to list matches.
13071 If the current buffer is in `dired-mode', grep will be used to search
13072 in all files. If AVOID-POS is given, ignore matches near that position."
13073 (let ((case-fold-search t)
13074 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
13075 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
13076 (append '(("") (" ") ("\t") ("\n"))
13077 org-emphasis-alist)
13081 words re0 re1 re2 re3 re4 re5 re2a reall)
13083 ;; First check if there are any special
13084 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
13085 ;; Now try the builtin stuff
13087 (goto-char (point-min))
13090 (concat "<<" (regexp-quote s0) ">>") nil t)
13091 (setq pos (match-beginning 0))))
13092 ;; There is an exact target for this
13094 ((string-match "^/\\(.*\\)/$" s)
13095 ;; A regular expression
13098 (org-occur (match-string 1 s)))
13099 ;;((eq major-mode 'dired-mode)
13100 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
13101 (t (org-do-occur (match-string 1 s)))))
13103 ;; A normal search strings
13104 (when (equal (string-to-char s) ?*)
13105 ;; Anchor on headlines, post may include tags.
13106 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
13107 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
13108 s (substring s 1)))
13109 (remove-text-properties
13111 '(face nil mouse-face nil keymap nil fontified nil) s)
13112 ;; Make a series of regular expressions to find a match
13113 (setq words (org-split-string s "[ \n\r\t]+")
13114 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
13115 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
13117 re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
13118 re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
13119 re1 (concat pre re2 post)
13120 re3 (concat pre re4 post)
13121 re5 (concat pre ".*" re4)
13122 re2 (concat pre re2)
13123 re2a (concat pre re2a)
13124 re4 (concat pre re4)
13125 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
13126 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
13130 ((eq type 'org-occur) (org-occur reall))
13131 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
13132 (t (goto-char (point-min))
13133 (if (or (org-search-not-self 1 re0 nil t)
13134 (org-search-not-self 1 re1 nil t)
13135 (org-search-not-self 1 re2 nil t)
13136 (org-search-not-self 1 re2a nil t)
13137 (org-search-not-self 1 re3 nil t)
13138 (org-search-not-self 1 re4 nil t)
13139 (org-search-not-self 1 re5 nil t)
13141 (goto-char (match-beginning 1))
13143 (error "No match")))))
13145 ;; Normal string-search
13146 (goto-char (point-min))
13147 (if (search-forward s nil t)
13148 (goto-char (match-beginning 0))
13149 (error "No match"))))
13150 (and (org-mode-p) (org-show-context 'link-search))))
13152 (defun org-search-not-self (group &rest args)
13153 "Execute `re-search-forward', but only accept matches that do not
13154 enclose the position of `org-open-link-marker'."
13155 (let ((m org-open-link-marker))
13157 (while (apply 're-search-forward args)
13158 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
13159 (goto-char (match-end group))
13160 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
13161 (> (match-beginning 0) (marker-position m))
13162 (< (match-end 0) (marker-position m)))
13164 (or (not (org-in-regexp
13165 org-bracket-link-analytic-regexp 1))
13166 (not (match-end 4)) ; no description
13167 (and (<= (match-beginning 4) (point))
13168 (>= (match-end 4) (point))))))
13169 (throw 'exit (point))))))))
13171 (defun org-get-buffer-for-internal-link (buffer)
13172 "Return a buffer to be used for displaying the link target of internal links."
13174 ((not org-display-internal-link-with-indirect-buffer)
13176 ((string-match "(Clone)$" (buffer-name buffer))
13177 (message "Buffer is already a clone, not making another one")
13178 ;; we also do not modify visibility in this case
13180 (t ; make a new indirect buffer for displaying the link
13181 (let* ((bn (buffer-name buffer))
13182 (ibn (concat bn "(Clone)"))
13183 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
13184 (with-current-buffer ib (org-overview))
13187 (defun org-do-occur (regexp &optional cleanup)
13188 "Call the Emacs command `occur'.
13189 If CLEANUP is non-nil, remove the printout of the regular expression
13190 in the *Occur* buffer. This is useful if the regex is long and not useful
13194 (let ((cwin (selected-window)) win beg end)
13195 (when (setq win (get-buffer-window "*Occur*"))
13196 (select-window win))
13197 (goto-char (point-min))
13198 (when (re-search-forward "match[a-z]+" nil t)
13199 (setq beg (match-end 0))
13200 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
13201 (setq end (1- (match-beginning 0)))))
13202 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
13203 (goto-char (point-min))
13204 (select-window cwin))))
13206 ;;; The mark ring for links jumps
13208 (defvar org-mark-ring nil
13209 "Mark ring for positions before jumps in Org-mode.")
13210 (defvar org-mark-ring-last-goto nil
13211 "Last position in the mark ring used to go back.")
13212 ;; Fill and close the ring
13213 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
13214 (loop for i from 1 to org-mark-ring-length do
13215 (push (make-marker) org-mark-ring))
13216 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
13219 (defun org-mark-ring-push (&optional pos buffer)
13220 "Put the current position or POS into the mark ring and rotate it."
13222 (setq pos (or pos (point)))
13223 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
13224 (move-marker (car org-mark-ring)
13226 (or buffer (current-buffer)))
13228 (substitute-command-keys
13229 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
13231 (defun org-mark-ring-goto (&optional n)
13232 "Jump to the previous position in the mark ring.
13233 With prefix arg N, jump back that many stored positions. When
13234 called several times in succession, walk through the entire ring.
13235 Org-mode commands jumping to a different position in the current file,
13236 or to another Org-mode file, automatically push the old position
13240 (if (eq last-command this-command)
13241 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
13242 (setq p org-mark-ring))
13243 (setq org-mark-ring-last-goto p)
13245 (switch-to-buffer (marker-buffer m))
13247 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
13249 (defun org-remove-angle-brackets (s)
13250 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
13251 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
13253 (defun org-add-angle-brackets (s)
13254 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
13255 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
13258 ;;; Following specific links
13260 (defun org-follow-timestamp-link ()
13262 ((org-at-date-range-p t)
13263 (let ((org-agenda-start-on-weekday)
13264 (t1 (match-string 1))
13265 (t2 (match-string 2)))
13266 (setq t1 (time-to-days (org-time-string-to-time t1))
13267 t2 (time-to-days (org-time-string-to-time t2)))
13268 (org-agenda-list nil t1 (1+ (- t2 t1)))))
13269 ((org-at-timestamp-p t)
13270 (org-agenda-list nil (time-to-days (org-time-string-to-time
13271 (substring (match-string 1) 0 10)))
13273 (t (error "This should not happen"))))
13276 (defun org-follow-bbdb-link (name)
13277 "Follow a BBDB link to NAME."
13279 (let ((inhibit-redisplay (not debug-on-error))
13280 (bbdb-electric-p nil))
13282 ;; Exact match on name
13283 (bbdb-name (concat "\\`" name "\\'") nil)
13284 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13285 ;; Exact match on name
13286 (bbdb-company (concat "\\`" name "\\'") nil)
13287 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13288 ;; Partial match on name
13289 (bbdb-name name nil)
13290 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13291 ;; Partial match on company
13292 (bbdb-company name nil)
13293 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13294 ;; General match including network address and notes
13296 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
13297 (delete-window (get-buffer-window "*BBDB*"))
13298 (error "No matching BBDB record")))))
13300 (defun org-follow-info-link (name)
13301 "Follow an info file & node link to NAME."
13302 (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
13303 (string-match "\\(.*\\)" name))
13306 (if (match-string 2 name) ; If there isn't a node, choose "Top"
13307 (Info-find-node (match-string 1 name) (match-string 2 name))
13308 (Info-find-node (match-string 1 name) "Top")))
13309 (message "Could not open: %s" name)))
13311 (defun org-follow-gnus-link (&optional group article)
13312 "Follow a Gnus link to GROUP and ARTICLE."
13314 (funcall (cdr (assq 'gnus org-link-frame-setup)))
13315 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
13316 (cond ((and group article)
13317 (gnus-group-read-group 1 nil group)
13318 (gnus-summary-goto-article (string-to-number article) nil t))
13319 (group (gnus-group-jump-to-group group))))
13321 (defun org-follow-vm-link (&optional folder article readonly)
13322 "Follow a VM link to FOLDER and ARTICLE."
13324 (setq article (org-add-angle-brackets article))
13325 (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
13326 ;; ange-ftp or efs or tramp access
13327 (let ((user (or (match-string 1 folder) (user-login-name)))
13328 (host (match-string 2 folder))
13329 (file (match-string 3 folder)))
13332 ;; use tramp to access the file
13333 (if (featurep 'xemacs)
13334 (setq folder (format "[%s@%s]%s" user host file))
13335 (setq folder (format "/%s@%s:%s" user host file))))
13337 ;; use ange-ftp or efs
13338 (require (if (featurep 'xemacs) 'efs 'ange-ftp))
13339 (setq folder (format "/%s@%s:%s" user host file))))))
13341 (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
13344 (vm-select-folder-buffer)
13346 (let ((case-fold-search t))
13347 (goto-char (point-min))
13348 (if (not (re-search-forward
13349 (concat "^" "message-id: *" (regexp-quote article))))
13350 (error "Could not find the specified message in this folder"))
13351 (vm-isearch-update)
13352 (vm-isearch-narrow)
13353 (vm-beginning-of-message)
13356 (defun org-follow-wl-link (folder article)
13357 "Follow a Wanderlust link to FOLDER and ARTICLE."
13358 (if (and (string= folder "%")
13360 (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
13361 ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
13362 ;; Thus, we recompose folder and article ids.
13363 (setq folder (format "%s#%s" folder (match-string 1 article))
13364 article (match-string 3 article)))
13365 (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
13366 (error "No such folder: %s" folder))
13367 (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
13369 (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
13370 (wl-summary-redisplay)))
13372 (defun org-follow-rmail-link (folder article)
13373 "Follow an RMAIL link to FOLDER and ARTICLE."
13374 (setq article (org-add-angle-brackets article))
13375 (let (message-number)
13377 (save-window-excursion
13378 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
13379 (setq message-number
13382 (goto-char (point-max))
13383 (if (re-search-backward
13384 (concat "^Message-ID:\\s-+" (regexp-quote
13387 (rmail-what-message))))))
13390 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
13391 (rmail-show-message message-number)
13393 (error "Message not found"))))
13395 ;;; mh-e integration based on planner-mode
13396 (defun org-mhe-get-message-real-folder ()
13397 "Return the name of the current message real folder, so if you use
13398 sequences, it will now work."
13401 (if (equal major-mode 'mh-folder-mode)
13403 ;; Refer to the show buffer
13404 mh-show-folder-buffer))
13406 (if (boundp 'mh-index-folder)
13407 (min (length mh-index-folder) (length folder))))
13409 ;; a simple test on mh-index-data does not work, because
13410 ;; mh-index-data is always nil in a show buffer.
13411 (if (and (boundp 'mh-index-folder)
13412 (string= mh-index-folder (substring folder 0 end-index)))
13413 (if (equal major-mode 'mh-show-mode)
13414 (save-window-excursion
13415 (let (pop-up-frames)
13416 (when (buffer-live-p (get-buffer folder))
13418 (pop-to-buffer folder)
13419 (org-mhe-get-message-folder-from-index)
13422 (org-mhe-get-message-folder-from-index)
13428 (defun org-mhe-get-message-folder-from-index ()
13429 "Returns the name of the message folder in a index folder buffer."
13431 (mh-index-previous-folder)
13432 (re-search-forward "^\\(+.*\\)$" nil t)
13433 (message "%s" (match-string 1))))
13435 (defun org-mhe-get-message-folder ()
13436 "Return the name of the current message folder. Be careful if you
13439 (if (equal major-mode 'mh-folder-mode)
13441 ;; Refer to the show buffer
13442 mh-show-folder-buffer)))
13444 (defun org-mhe-get-message-num ()
13445 "Return the number of the current message. Be careful if you
13448 (if (equal major-mode 'mh-folder-mode)
13449 (mh-get-msg-num nil)
13450 ;; Refer to the show buffer
13451 (mh-show-buffer-message-number))))
13453 (defun org-mhe-get-header (header)
13454 "Return a header of the message in folder mode. This will create a
13455 show buffer for the corresponding message. If you have a more clever
13457 (let* ((folder (org-mhe-get-message-folder))
13458 (num (org-mhe-get-message-num))
13459 (buffer (get-buffer-create (concat "show-" folder)))
13461 (with-current-buffer buffer
13462 (mh-display-msg num folder)
13463 (if (equal major-mode 'mh-folder-mode)
13464 (mh-header-display)
13465 (mh-show-header-display))
13466 (set-buffer buffer)
13467 (setq header-field (mh-get-header-field header))
13468 (if (equal major-mode 'mh-folder-mode)
13473 (defun org-follow-mhe-link (folder article)
13474 "Follow an MHE link to FOLDER and ARTICLE.
13475 If ARTICLE is nil FOLDER is shown. If the configuration variable
13476 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
13477 ARTICLE is searched in all folders. Indexed searches (swish++,
13478 namazu, and others supported by MH-E) will always search in all
13481 (require 'mh-search)
13482 (require 'mh-utils)
13485 (mh-visit-folder (mh-normalize-folder-name folder))
13486 (setq article (org-add-angle-brackets article))
13488 (if (equal mh-searcher 'pick)
13490 (mh-search folder (list "--message-id" article))
13491 (when (and org-mhe-search-all-folders
13492 (not (org-mhe-get-message-real-folder)))
13494 (mh-search "+" (list "--message-id" article))))
13495 (mh-search "+" article))
13496 (if (org-mhe-get-message-real-folder)
13499 (error "Message not found"))))
13503 ;; Use the custom search meachnism to construct and use search strings for
13504 ;; file links to BibTeX database entries.
13506 (defun org-create-file-search-in-bibtex ()
13507 "Create the search string and description for a BibTeX database entry."
13508 (when (eq major-mode 'bibtex-mode)
13509 ;; yes, we want to construct this search string.
13510 ;; Make a good description for this entry, using names, year and the title
13511 ;; Put it into the `description' variable which is dynamically scoped.
13512 (let ((bibtex-autokey-names 1)
13513 (bibtex-autokey-names-stretch 1)
13514 (bibtex-autokey-name-case-convert-function 'identity)
13515 (bibtex-autokey-name-separator " & ")
13516 (bibtex-autokey-additional-names " et al.")
13517 (bibtex-autokey-year-length 4)
13518 (bibtex-autokey-name-year-separator " ")
13519 (bibtex-autokey-titlewords 3)
13520 (bibtex-autokey-titleword-separator " ")
13521 (bibtex-autokey-titleword-case-convert-function 'identity)
13522 (bibtex-autokey-titleword-length 'infty)
13523 (bibtex-autokey-year-title-separator ": "))
13524 (setq description (bibtex-generate-autokey)))
13525 ;; Now parse the entry, get the key and return it.
13527 (bibtex-beginning-of-entry)
13528 (cdr (assoc "=key=" (bibtex-parse-entry))))))
13530 (defun org-execute-file-search-in-bibtex (s)
13531 "Find the link search string S as a key for a database entry."
13532 (when (eq major-mode 'bibtex-mode)
13533 ;; Yes, we want to do the search in this file.
13534 ;; We construct a regexp that searches for "@entrytype{" followed by the key
13535 (goto-char (point-min))
13536 (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
13537 (regexp-quote s) "[ \t\n]*,") nil t)
13538 (goto-char (match-beginning 0)))
13539 (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
13540 ;; Use double prefix to indicate that any web link should be browsed
13541 (let ((b (current-buffer)) (p (point)))
13542 ;; Restore the window configuration because we just use the web link
13543 (set-window-configuration org-window-config-before-follow-link)
13544 (save-excursion (set-buffer b) (goto-char p)
13546 (recenter 0)) ; Move entry start to beginning of window
13547 ;; return t to indicate that the search is done.
13550 ;; Finally add the functions to the right hooks.
13551 (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
13552 (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
13554 ;; end of Bibtex link setup
13556 ;;; Following file links
13558 (defun org-open-file (path &optional in-emacs line search)
13559 "Open the file at PATH.
13560 First, this expands any special file name abbreviations. Then the
13561 configuration variable `org-file-apps' is checked if it contains an
13562 entry for this file type, and if yes, the corresponding command is launched.
13563 If no application is found, Emacs simply visits the file.
13564 With optional argument IN-EMACS, Emacs will visit the file.
13565 Optional LINE specifies a line to go to, optional SEARCH a string to
13566 search for. If LINE or SEARCH is given, the file will always be
13568 If the file does not exist, an error is thrown."
13569 (setq in-emacs (or in-emacs line search))
13570 (let* ((file (if (equal path "")
13572 (substitute-in-file-name (expand-file-name path))))
13573 (apps (append org-file-apps (org-default-apps)))
13574 (remp (and (assq 'remote apps) (org-file-remote-p file)))
13575 (dirp (if remp nil (file-directory-p file)))
13576 (dfile (downcase file))
13577 (old-buffer (current-buffer))
13579 (old-mode major-mode)
13581 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
13582 (setq ext (match-string 1 dfile))
13583 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
13584 (setq ext (match-string 1 dfile))))
13587 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
13588 (and dirp (cdr (assoc 'directory apps)))
13589 (cdr (assoc ext apps))
13590 (cdr (assoc t apps)))))
13591 (when (eq cmd 'mailcap)
13593 (mailcap-parse-mailcaps)
13594 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
13595 (command (mailcap-mime-info mime-type)))
13596 (if (stringp command)
13598 (setq cmd 'emacs))))
13599 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
13600 (not (file-exists-p file))
13601 (not org-open-non-existing-files))
13602 (error "No such file: %s" file))
13604 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
13605 ;; Remove quotes around the file name - we'll use shell-quote-argument.
13606 (while (string-match "['\"]%s['\"]" cmd)
13607 (setq cmd (replace-match "%s" t t cmd)))
13608 (while (string-match "%s" cmd)
13609 (setq cmd (replace-match
13610 (save-match-data (shell-quote-argument file))
13612 (save-window-excursion
13613 (start-process-shell-command cmd nil cmd)))
13616 (funcall (cdr (assq 'file org-link-frame-setup)) file)
13618 (if line (goto-line line)
13619 (if search (org-link-search search))))
13622 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
13623 (and (org-mode-p) (eq old-mode 'org-mode)
13624 (or (not (equal old-buffer (current-buffer)))
13625 (not (equal old-pos (point))))
13626 (org-mark-ring-push old-pos old-buffer))))
13628 (defun org-default-apps ()
13629 "Return the default applications for this operating system."
13631 ((eq system-type 'darwin)
13632 org-file-apps-defaults-macosx)
13633 ((eq system-type 'windows-nt)
13634 org-file-apps-defaults-windowsnt)
13635 (t org-file-apps-defaults-gnu)))
13637 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
13638 (defun org-file-remote-p (file)
13639 "Test whether FILE specifies a location on a remote system.
13640 Return non-nil if the location is indeed remote.
13642 For example, the filename \"/user@host:/foo\" specifies a location
13643 on the system \"/user@host:\"."
13644 (cond ((fboundp 'file-remote-p)
13645 (file-remote-p file))
13646 ((fboundp 'tramp-handle-file-remote-p)
13647 (tramp-handle-file-remote-p file))
13648 ((and (boundp 'ange-ftp-name-format)
13649 (string-match (car ange-ftp-name-format) file))
13654 ;;;; Hooks for remember.el, and refiling
13656 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
13657 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
13660 (defun org-remember-insinuate ()
13661 "Setup remember.el for use wiht Org-mode."
13662 (require 'remember)
13663 (setq remember-annotation-functions '(org-remember-annotation))
13664 (setq remember-handler-functions '(org-remember-handler))
13665 (add-hook 'remember-mode-hook 'org-remember-apply-template))
13668 (defun org-remember-annotation ()
13669 "Return a link to the current location as an annotation for remember.el.
13670 If you are using Org-mode files as target for data storage with
13671 remember.el, then the annotations should include a link compatible with the
13672 conventions in Org-mode. This function returns such a link."
13673 (org-store-link nil))
13675 (defconst org-remember-help
13676 "Select a destination location for the note.
13677 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
13678 RET on headline -> Store as sublevel entry to current headline
13679 RET at beg-of-buf -> Append to file as level 2 headline
13680 <left>/<right> -> before/after current headline, same headings level")
13682 (defvar org-remember-previous-location nil)
13683 (defvar org-force-remember-template-char) ;; dynamically scoped
13685 ;; Save the major mode of the buffer we called remember from
13686 (defvar org-select-template-temp-major-mode nil)
13688 ;; Temporary store the buffer where remember was called from
13689 (defvar org-select-template-original-buffer nil)
13691 (defun org-select-remember-template (&optional use-char)
13692 (when org-remember-templates
13693 (let* ((pre-selected-templates
13696 (let ((ctxt (nth 5 tpl))
13697 (mode org-select-template-temp-major-mode)
13698 (buf org-select-template-original-buffer))
13699 (and (or (not ctxt) (eq ctxt t)
13700 (and (listp ctxt) (memq mode ctxt))
13701 (and (functionp ctxt)
13702 (with-current-buffer buf
13703 ;; Protect the user-defined function from error
13704 (condition-case nil (funcall ctxt) (error nil)))))
13706 org-remember-templates))
13707 ;; If no template at this point, add the default templates:
13708 (pre-selected-templates1
13709 (if (not (delq nil pre-selected-templates))
13710 (mapcar (lambda(x) (if (not (nth 5 x)) x))
13711 org-remember-templates)
13712 pre-selected-templates))
13713 ;; Then unconditionnally add template for any contexts
13714 (pre-selected-templates2
13715 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
13716 org-remember-templates)
13717 (delq nil pre-selected-templates1)))
13718 (templates (mapcar (lambda (x)
13719 (if (stringp (car x))
13720 (append (list (nth 1 x) (car x)) (cddr x))
13721 (append (list (car x) "") (cdr x))))
13722 (delq nil pre-selected-templates2)))
13725 ((= (length templates) 1)
13727 ((and (boundp 'org-force-remember-template-char)
13728 org-force-remember-template-char)
13729 (if (stringp org-force-remember-template-char)
13730 (string-to-char org-force-remember-template-char)
13731 org-force-remember-template-char))
13733 (message "Select template: %s"
13737 ((not (string-match "\\S-" (nth 1 x)))
13738 (format "[%c]" (car x)))
13739 ((equal (downcase (car x))
13740 (downcase (aref (nth 1 x) 0)))
13741 (format "[%c]%s" (car x)
13742 (substring (nth 1 x) 1)))
13743 (t (format "[%c]%s" (car x) (nth 1 x)))))
13745 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
13746 (when (equal char0 ?\C-g)
13747 (jump-to-register remember-register)
13748 (kill-buffer remember-buffer))
13750 (cddr (assoc char templates)))))
13752 (defvar x-last-selected-text)
13753 (defvar x-last-selected-text-primary)
13756 (defun org-remember-apply-template (&optional use-char skip-interactive)
13757 "Initialize *remember* buffer with template, invoke `org-mode'.
13758 This function should be placed into `remember-mode-hook' and in fact requires
13759 to be run from that hook to function properly."
13760 (if org-remember-templates
13761 (let* ((entry (org-select-remember-template use-char))
13763 (plist-p (if org-store-link-plist t nil))
13764 (file (if (and (nth 1 entry) (stringp (nth 1 entry))
13765 (string-match "\\S-" (nth 1 entry)))
13767 org-default-notes-file))
13768 (headline (nth 2 entry))
13769 (v-c (or (and (eq window-system 'x)
13770 (fboundp 'x-cut-buffer-or-selection-value)
13771 (x-cut-buffer-or-selection-value))
13772 (org-bound-and-true-p x-last-selected-text)
13773 (org-bound-and-true-p x-last-selected-text-primary)
13774 (and (> (length kill-ring) 0) (current-kill 0))))
13775 (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
13776 (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
13777 (v-u (concat "[" (substring v-t 1 -1) "]"))
13778 (v-U (concat "[" (substring v-T 1 -1) "]"))
13779 ;; `initial' and `annotation' are bound in `remember'
13780 (v-i (if (boundp 'initial) initial))
13781 (v-a (if (and (boundp 'annotation) annotation)
13782 (if (equal annotation "[[]]") "" annotation)
13785 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
13786 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
13788 (v-n user-full-name)
13789 (org-startup-folded nil)
13790 org-time-was-given org-end-time-was-given x
13791 prompt completions char time pos default histvar)
13792 (setq org-store-link-plist
13793 (append (list :annotation v-a :initial v-i)
13794 org-store-link-plist))
13795 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
13797 (insert (substitute-command-keys
13799 "## Filing location: Select interactively, default, or last used:
13800 ## %s to select file and header location interactively.
13801 ## %s \"%s\" -> \"* %s\"
13802 ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
13803 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
13804 (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
13805 (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
13806 (abbreviate-file-name (or file org-default-notes-file))
13808 (or (car org-remember-previous-location) "???")
13809 (or (cdr org-remember-previous-location) "???"))))
13810 (insert tpl) (goto-char (point-min))
13811 ;; Simple %-escapes
13812 (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
13813 (when (and initial (equal (match-string 0) "%i"))
13815 (let* ((lead (buffer-substring
13816 (point-at-bol) (match-beginning 0))))
13817 (setq v-i (mapconcat 'identity
13818 (org-split-string initial "\n")
13819 (concat "\n" lead))))))
13821 (or (eval (intern (concat "v-" (match-string 1)))) "")
13824 ;; %[] Insert contents of a file.
13825 (goto-char (point-min))
13826 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
13827 (let ((start (match-beginning 0))
13828 (end (match-end 0))
13829 (filename (expand-file-name (match-string 1))))
13831 (delete-region start end)
13832 (condition-case error
13833 (insert-file-contents filename)
13834 (error (insert (format "%%![Couldn't insert %s: %s]"
13835 filename error))))))
13836 ;; %() embedded elisp
13837 (goto-char (point-min))
13838 (while (re-search-forward "%\\((.+)\\)" nil t)
13839 (goto-char (match-beginning 0))
13840 (let ((template-start (point)))
13843 (condition-case error
13844 (eval (read (current-buffer)))
13845 (error (format "%%![Error: %s]" error)))))
13846 (delete-region template-start (point))
13849 ;; From the property list
13851 (goto-char (point-min))
13852 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
13853 (and (setq x (or (plist-get org-store-link-plist
13854 (intern (match-string 1))) ""))
13855 (replace-match x t t))))
13857 ;; Turn on org-mode in the remember buffer, set local variables
13859 (org-set-local 'org-finish-function 'org-remember-finalize)
13860 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
13861 (org-set-local 'org-default-notes-file file))
13862 (if (and headline (stringp headline) (string-match "\\S-" headline))
13863 (org-set-local 'org-remember-default-headline headline))
13864 ;; Interactive template entries
13865 (goto-char (point-min))
13866 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
13867 (setq char (if (match-end 3) (match-string 3))
13868 prompt (if (match-end 2) (match-string 2)))
13869 (goto-char (match-beginning 0))
13871 (setq completions nil default nil)
13873 (setq completions (org-split-string prompt "|")
13874 prompt (pop completions)
13875 default (car completions)
13876 histvar (intern (concat
13877 "org-remember-template-prompt-history::"
13879 completions (mapcar 'list completions)))
13881 ((member char '("G" "g"))
13882 (let* ((org-last-tags-completion-table
13883 (org-global-tags-completion-table
13884 (if (equal char "G") (org-agenda-files) (and file (list file)))))
13885 (org-add-colon-after-tag-completion t)
13886 (ins (completing-read
13887 (if prompt (concat prompt ": ") "Tags: ")
13888 'org-tags-completion-function nil nil nil
13889 'org-tags-history)))
13890 (setq ins (mapconcat 'identity
13891 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
13893 (when (string-match "\\S-" ins)
13894 (or (equal (char-before) ?:) (insert ":"))
13896 (or (equal (char-after) ?:) (insert ":")))))
13898 (setq org-time-was-given (equal (upcase char) char))
13899 (setq time (org-read-date (equal (upcase char) "U") t nil
13901 (org-insert-time-stamp time org-time-was-given
13902 (member char '("u" "U"))
13903 nil nil (list org-end-time-was-given)))
13905 (insert (org-completing-read
13906 (concat (if prompt prompt "Enter string")
13907 (if default (concat " [" default "]"))
13909 completions nil nil nil histvar default)))))
13910 (goto-char (point-min))
13911 (if (re-search-forward "%\\?" nil t)
13913 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
13915 (org-set-local 'org-finish-function 'org-remember-finalize))
13916 (when (save-excursion
13917 (goto-char (point-min))
13918 (re-search-forward "%!" nil t))
13920 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
13922 (defun org-remember-finish-immediately ()
13923 "File remember note immediately.
13924 This should be run in `post-command-hook' and will remove itself
13926 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
13927 (when org-finish-function
13928 (funcall org-finish-function)))
13930 (defvar org-clock-marker) ; Defined below
13931 (defun org-remember-finalize ()
13932 "Finalize the remember process."
13933 (unless (fboundp 'remember-finalize)
13934 (defalias 'remember-finalize 'remember-buffer))
13935 (when (and org-clock-marker
13936 (equal (marker-buffer org-clock-marker) (current-buffer)))
13937 ;; FIXME: test this, this is w/o notetaking!
13938 (let (org-log-note-clock-out) (org-clock-out)))
13939 (when buffer-file-name
13941 (setq buffer-file-name nil))
13942 (remember-finalize))
13945 (defun org-remember (&optional goto org-force-remember-template-char)
13946 "Call `remember'. If this is already a remember buffer, re-apply template.
13947 If there is an active region, make sure remember uses it as initial content
13948 of the remember buffer.
13950 When called interactively with a `C-u' prefix argument GOTO, don't remember
13951 anything, just go to the file/headline where the selected template usually
13952 stores its notes. With a double prefix arg `C-u C-u', go to the last
13953 note stored by remember.
13955 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
13956 associated with a template in `org-remember-templates'."
13959 ((equal goto '(4)) (org-go-to-remember-target))
13960 ((equal goto '(16)) (org-remember-goto-last-stored))
13962 ;; set temporary variables that will be needed in
13963 ;; `org-select-remember-template'
13964 (setq org-select-template-temp-major-mode major-mode)
13965 (setq org-select-template-original-buffer (current-buffer))
13966 (if (memq org-finish-function '(remember-buffer remember-finalize))
13968 (when (< (length org-remember-templates) 2)
13969 (error "No other template available"))
13971 (let ((annotation (plist-get org-store-link-plist :annotation))
13972 (initial (plist-get org-store-link-plist :initial)))
13973 (org-remember-apply-template))
13974 (message "Press C-c C-c to remember data"))
13975 (if (org-region-active-p)
13976 (remember (buffer-substring (point) (mark)))
13977 (call-interactively 'remember))))))
13979 (defun org-remember-goto-last-stored ()
13980 "Go to the location where the last remember note was stored."
13982 (bookmark-jump "org-remember-last-stored")
13983 (message "This is the last note stored by remember"))
13985 (defun org-go-to-remember-target (&optional template-key)
13986 "Go to the target location of a remember template.
13987 The user is queried for the template."
13989 (let* (org-select-template-temp-major-mode
13990 (entry (org-select-remember-template template-key))
13991 (file (nth 1 entry))
13992 (heading (nth 2 entry))
13994 (unless (and file (stringp file) (string-match "\\S-" file))
13995 (setq file org-default-notes-file))
13996 (unless (and heading (stringp heading) (string-match "\\S-" heading))
13997 (setq heading org-remember-default-headline))
13998 (setq visiting (org-find-base-buffer-visiting file))
13999 (if (not visiting) (find-file-noselect file))
14000 (switch-to-buffer (or visiting (get-file-buffer file)))
14002 (goto-char (point-min))
14003 (if (re-search-forward
14004 (concat "^\\*+[ \t]+" (regexp-quote heading)
14005 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
14007 (goto-char (match-beginning 0))
14008 (error "Target headline not found: %s" heading))))
14010 (defvar org-note-abort nil) ; dynamically scoped
14013 (defun org-remember-handler ()
14014 "Store stuff from remember.el into an org file.
14015 First prompts for an org file. If the user just presses return, the value
14016 of `org-default-notes-file' is used.
14017 Then the command offers the headings tree of the selected file in order to
14018 file the text at a specific location.
14019 You can either immediately press RET to get the note appended to the
14020 file, or you can use vertical cursor motion and visibility cycling (TAB) to
14021 find a better place. Then press RET or <left> or <right> in insert the note.
14023 Key Cursor position Note gets inserted
14024 -----------------------------------------------------------------------------
14025 RET buffer-start as level 1 heading at end of file
14026 RET on headline as sublevel of the heading at cursor
14027 RET no heading at cursor position, level taken from context.
14028 Or use prefix arg to specify level manually.
14029 <left> on headline as same level, before current heading
14030 <right> on headline as same level, after current heading
14032 So the fastest way to store the note is to press RET RET to append it to
14033 the default file. This way your current train of thought is not
14034 interrupted, in accordance with the principles of remember.el.
14035 You can also get the fast execution without prompting by using
14036 C-u C-c C-c to exit the remember buffer. See also the variable
14037 `org-remember-store-without-prompt'.
14039 Before being stored away, the function ensures that the text has a
14040 headline, i.e. a first line that starts with a \"*\". If not, a headline
14041 is constructed from the current date and some additional data.
14043 If the variable `org-adapt-indentation' is non-nil, the entire text is
14044 also indented so that it starts in the same column as the headline
14045 \(i.e. after the stars).
14047 See also the variable `org-reverse-note-order'."
14048 (goto-char (point-min))
14049 (while (looking-at "^[ \t]*\n\\|^##.*\n")
14050 (replace-match ""))
14051 (goto-char (point-max))
14052 (beginning-of-line 1)
14053 (while (looking-at "[ \t]*$\\|##.*")
14054 (delete-region (1- (point)) (point-max))
14055 (beginning-of-line 1))
14057 (if org-note-abort (throw 'quit nil))
14058 (let* ((txt (buffer-substring (point-min) (point-max)))
14059 (fastp (org-xor (equal current-prefix-arg '(4))
14060 org-remember-store-without-prompt))
14062 (fastp org-default-notes-file)
14063 ((and (eq org-remember-interactive-interface 'refile)
14064 org-refile-targets)
14065 org-default-notes-file)
14066 ((not (and (equal current-prefix-arg '(16))
14067 org-remember-previous-location))
14068 (org-get-org-file))))
14069 (heading org-remember-default-headline)
14070 (visiting (and file (org-find-base-buffer-visiting file)))
14071 (org-startup-folded nil)
14072 (org-startup-align-all-tables nil)
14073 (org-goto-start-pos 1)
14074 spos exitcmd level indent reversed)
14075 (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
14076 (setq file (car org-remember-previous-location)
14077 heading (cdr org-remember-previous-location)
14079 (setq current-prefix-arg nil)
14080 (if (string-match "[ \t\n]+\\'" txt)
14081 (setq txt (replace-match "" t t txt)))
14082 ;; Modify text so that it becomes a nice subtree which can be inserted
14083 ;; into an org tree.
14084 (let* ((lines (split-string txt "\n"))
14086 (setq first (car lines) lines (cdr lines))
14087 (if (string-match "^\\*+ " first)
14088 ;; Is already a headline
14090 ;; We need to add a headline: Use time and first buffer line
14091 (setq lines (cons first lines)
14092 first (concat "* " (current-time-string)
14093 " (" (remember-buffer-desc) ")")
14095 (if (and org-adapt-indentation indent)
14096 (setq lines (mapcar
14098 (if (string-match "\\S-" x)
14099 (concat indent x) x))
14101 (setq txt (concat first "\n"
14102 (mapconcat 'identity lines "\n"))))
14103 (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
14104 (setq txt (replace-match "\n\n" t t txt))
14105 (if (string-match "[ \t\n]*\\'" txt)
14106 (setq txt (replace-match "\n" t t txt))))
14107 ;; Put the modified text back into the remember buffer, for refile.
14110 (goto-char (point-min))
14111 (when (and (eq org-remember-interactive-interface 'refile)
14113 (org-refile nil (or visiting (find-file-noselect file)))
14116 (if (not visiting) (find-file-noselect file))
14117 (with-current-buffer (or visiting (get-file-buffer file))
14118 (unless (org-mode-p)
14119 (error "Target files for remember notes must be in Org-mode"))
14123 (and (goto-char (point-min))
14124 (not (re-search-forward "^\\* " nil t))
14125 (insert "\n* " (or heading "Notes") "\n"))
14126 (setq reversed (org-notes-order-reversed-p))
14128 ;; Find the default location
14129 (when (and heading (stringp heading) (string-match "\\S-" heading))
14130 (goto-char (point-min))
14131 (if (re-search-forward
14132 (concat "^\\*+[ \t]+" (regexp-quote heading)
14133 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
14135 (setq org-goto-start-pos (match-beginning 0))
14137 (goto-char (point-max))
14138 (unless (bolp) (newline))
14139 (insert "* " heading "\n")
14140 (setq org-goto-start-pos (point-at-bol 0)))))
14142 ;; Ask the User for a location, using the appropriate interface
14144 (fastp (setq spos org-goto-start-pos
14146 ((eq org-remember-interactive-interface 'outline)
14147 (setq spos (org-get-location (current-buffer)
14151 ((eq org-remember-interactive-interface 'outline-path-completion)
14152 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
14153 (org-refile-use-outline-path t))
14154 (setq spos (org-refile-get-location "Heading: ")
14156 spos (nth 3 spos))))
14157 (t (error "this should not hapen")))
14158 (if (not spos) (throw 'quit nil)) ; return nil to show we did
14159 ; not handle this note
14161 (cond ((org-on-heading-p t)
14162 (org-back-to-heading t)
14163 (setq level (funcall outline-level))
14165 ((eq exitcmd 'return)
14166 ;; sublevel of current
14167 (setq org-remember-previous-location
14168 (cons (abbreviate-file-name file)
14169 (org-get-heading 'notags)))
14171 (outline-next-heading)
14172 (org-end-of-subtree t)
14174 (if (looking-at "[ \t]*\n")
14175 (beginning-of-line 2)
14178 (bookmark-set "org-remember-last-stored")
14179 (org-paste-subtree (org-get-valid-level level 1) txt))
14180 ((eq exitcmd 'left)
14182 (bookmark-set "org-remember-last-stored")
14183 (org-paste-subtree level txt))
14184 ((eq exitcmd 'right)
14186 (org-end-of-subtree t)
14187 (bookmark-set "org-remember-last-stored")
14188 (org-paste-subtree level txt))
14189 (t (error "This should not happen"))))
14191 ((and (bobp) (not reversed))
14192 ;; Put it at the end, one level below level 1
14195 (goto-char (point-max))
14196 (if (not (bolp)) (newline))
14197 (bookmark-set "org-remember-last-stored")
14198 (org-paste-subtree (org-get-valid-level 1 1) txt)))
14200 ((and (bobp) reversed)
14201 ;; Put it at the start, as level 1
14204 (goto-char (point-min))
14205 (re-search-forward "^\\*+ " nil t)
14206 (beginning-of-line 1)
14207 (bookmark-set "org-remember-last-stored")
14208 (org-paste-subtree 1 txt)))
14210 ;; Put it right there, with automatic level determined by
14211 ;; org-paste-subtree or from prefix arg
14212 (bookmark-set "org-remember-last-stored")
14214 (if (numberp current-prefix-arg) current-prefix-arg)
14216 (when remember-save-after-remembering
14218 (if (not visiting) (kill-buffer (current-buffer)))))))))
14220 t) ;; return t to indicate that we took care of this note.
14222 (defun org-get-org-file ()
14223 "Read a filename, with default directory `org-directory'."
14224 (let ((default (or org-default-notes-file remember-data-file)))
14225 (read-file-name (format "File name [%s]: " default)
14226 (file-name-as-directory org-directory)
14229 (defun org-notes-order-reversed-p ()
14230 "Check if the current file should receive notes in reversed order."
14232 ((not org-reverse-note-order) nil)
14233 ((eq t org-reverse-note-order) t)
14234 ((not (listp org-reverse-note-order)) nil)
14236 (let ((all org-reverse-note-order)
14238 (while (setq entry (pop all))
14239 (if (string-match (car entry) buffer-file-name)
14240 (throw 'exit (cdr entry))))
14245 (defvar org-refile-target-table nil
14246 "The list of refile targets, created by `org-refile'.")
14248 (defvar org-agenda-new-buffers nil
14249 "Buffers created to visit agenda files.")
14251 (defun org-get-refile-targets (&optional default-buffer)
14252 "Produce a table with refile targets."
14253 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
14254 targets txt re files f desc descre)
14255 (with-current-buffer (or default-buffer (current-buffer))
14256 (while (setq entry (pop entries))
14257 (setq files (car entry) desc (cdr entry))
14259 ((null files) (setq files (list (current-buffer))))
14260 ((eq files 'org-agenda-files)
14261 (setq files (org-agenda-files 'unrestricted)))
14262 ((and (symbolp files) (fboundp files))
14263 (setq files (funcall files)))
14264 ((and (symbolp files) (boundp files))
14265 (setq files (symbol-value files))))
14266 (if (stringp files) (setq files (list files)))
14268 ((eq (car desc) :tag)
14269 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
14270 ((eq (car desc) :todo)
14271 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
14272 ((eq (car desc) :regexp)
14273 (setq descre (cdr desc)))
14274 ((eq (car desc) :level)
14275 (setq descre (concat "^\\*\\{" (number-to-string
14276 (if org-odd-levels-only
14277 (1- (* 2 (cdr desc)))
14280 ((eq (car desc) :maxlevel)
14281 (setq descre (concat "^\\*\\{1," (number-to-string
14282 (if org-odd-levels-only
14283 (1- (* 2 (cdr desc)))
14286 (t (error "Bad refiling target description %s" desc)))
14287 (while (setq f (pop files))
14289 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
14290 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
14294 (goto-char (point-min))
14295 (while (re-search-forward descre nil t)
14296 (goto-char (point-at-bol))
14297 (when (looking-at org-complex-heading-regexp)
14298 (setq txt (match-string 4)
14299 re (concat "^" (regexp-quote
14300 (buffer-substring (match-beginning 1)
14302 (if (match-end 5) (setq re (concat re "[ \t]+"
14304 (match-string 5)))))
14305 (setq re (concat re "[ \t]*$"))
14306 (when org-refile-use-outline-path
14307 (setq txt (mapconcat 'identity
14309 (if (eq org-refile-use-outline-path 'file)
14310 (list (file-name-nondirectory
14311 (buffer-file-name (buffer-base-buffer))))
14312 (if (eq org-refile-use-outline-path 'full-file-path)
14313 (list (buffer-file-name (buffer-base-buffer)))))
14314 (org-get-outline-path)
14317 (push (list txt f re (point)) targets))
14318 (goto-char (point-at-eol))))))))
14319 (nreverse targets))))
14321 (defun org-get-outline-path ()
14322 "Return the outline path to the current entry, as a list."
14325 (while (org-up-heading-safe)
14326 (when (looking-at org-complex-heading-regexp)
14327 (push (org-match-string-no-properties 4) rtn)))
14330 (defvar org-refile-history nil
14331 "History for refiling operations.")
14333 (defun org-refile (&optional goto default-buffer)
14334 "Move the entry at point to another heading.
14335 The list of target headings is compiled using the information in
14336 `org-refile-targets', which see. This list is created upon first use, and
14337 you can update it by calling this command with a double prefix (`C-u C-u').
14338 FIXME: Can we find a better way of updating?
14340 At the target location, the entry is filed as a subitem of the target heading.
14341 Depending on `org-reverse-note-order', the new subitem will either be the
14342 first of the last subitem.
14344 With prefix arg GOTO, the command will only visit the target location,
14345 not actually move anything.
14346 With a double prefix `C-c C-c', go to the location where the last refiling
14347 operation has put the subtree.
14349 With a double prefix argument, the command can be used to jump to any
14350 heading in the current buffer."
14352 (let* ((cbuf (current-buffer))
14353 (filename (buffer-file-name (buffer-base-buffer cbuf)))
14354 pos it nbuf file re level reversed)
14355 (if (equal goto '(16))
14356 (org-refile-goto-last-stored)
14357 (when (setq it (org-refile-get-location
14358 (if goto "Goto: " "Refile to: ") default-buffer))
14359 (setq file (nth 1 it)
14362 (setq nbuf (or (find-buffer-visiting file)
14363 (find-file-noselect file)))
14366 (switch-to-buffer nbuf)
14368 (org-show-context 'org-goto))
14371 (set-buffer (setq nbuf (or (find-buffer-visiting file)
14372 (find-file-noselect file))))
14373 (setq reversed (org-notes-order-reversed-p))
14378 (looking-at outline-regexp)
14379 (setq level (org-get-valid-level (funcall outline-level) 1))
14382 (outline-next-heading)
14383 (or (save-excursion (outline-get-next-sibling))
14384 (org-end-of-subtree t t)
14386 (bookmark-set "org-refile-last-stored")
14387 (org-paste-subtree level))))
14389 (message "Entry refiled to \"%s\"" (car it)))))))
14391 (defun org-refile-goto-last-stored ()
14392 "Go to the location where the last refile was stored."
14394 (bookmark-jump "org-refile-last-stored")
14395 (message "This is the location of the last refile"))
14397 (defun org-refile-get-location (&optional prompt default-buffer)
14398 "Prompt the user for a refile location, using PROMPT."
14399 (let ((org-refile-targets org-refile-targets)
14400 (org-refile-use-outline-path org-refile-use-outline-path))
14401 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
14402 (unless org-refile-target-table
14403 (error "No refile targets"))
14404 (let* ((cbuf (current-buffer))
14405 (filename (buffer-file-name (buffer-base-buffer cbuf)))
14406 (fname (and filename (file-truename filename)))
14409 (if (not (equal fname (file-truename (nth 1 x))))
14410 (cons (concat (car x) " (" (file-name-nondirectory
14414 org-refile-target-table))
14415 (completion-ignore-case t))
14416 (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
14419 ;;;; Dynamic blocks
14421 (defun org-find-dblock (name)
14422 "Find the first dynamic block with name NAME in the buffer.
14423 If not found, stay at current position and return nil."
14426 (goto-char (point-min))
14427 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
14429 (match-beginning 0))))
14430 (if pos (goto-char pos))
14433 (defconst org-dblock-start-re
14434 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
14435 "Matches the startline of a dynamic block, with parameters.")
14437 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
14438 "Matches the end of a dyhamic block.")
14440 (defun org-create-dblock (plist)
14441 "Create a dynamic block section, with parameters taken from PLIST.
14442 PLIST must containe a :name entry which is used as name of the block."
14443 (unless (bolp) (newline))
14444 (let ((name (plist-get plist :name)))
14445 (insert "#+BEGIN: " name)
14447 (if (eq (car plist) :name)
14448 (setq plist (cddr plist))
14449 (insert " " (prin1-to-string (pop plist)))))
14450 (insert "\n\n#+END:\n")
14451 (beginning-of-line -2)))
14453 (defun org-prepare-dblock ()
14454 "Prepare dynamic block for refresh.
14455 This empties the block, puts the cursor at the insert position and returns
14456 the property list including an extra property :name with the block name."
14457 (unless (looking-at org-dblock-start-re)
14458 (error "Not at a dynamic block"))
14459 (let* ((begdel (1+ (match-end 0)))
14460 (name (org-no-properties (match-string 1)))
14461 (params (append (list :name name)
14462 (read (concat "(" (match-string 3) ")")))))
14463 (unless (re-search-forward org-dblock-end-re nil t)
14464 (error "Dynamic block not terminated"))
14467 (list :content (buffer-substring
14468 begdel (match-beginning 0)))))
14469 (delete-region begdel (match-beginning 0))
14474 (defun org-map-dblocks (&optional command)
14475 "Apply COMMAND to all dynamic blocks in the current buffer.
14476 If COMMAND is not given, use `org-update-dblock'."
14477 (let ((cmd (or command 'org-update-dblock))
14480 (goto-char (point-min))
14481 (while (re-search-forward org-dblock-start-re nil t)
14482 (goto-char (setq pos (match-beginning 0)))
14483 (condition-case nil
14485 (error (message "Error during update of dynamic block")))
14487 (unless (re-search-forward org-dblock-end-re nil t)
14488 (error "Dynamic block not terminated"))))))
14490 (defun org-dblock-update (&optional arg)
14491 "User command for updating dynamic blocks.
14492 Update the dynamic block at point. With prefix ARG, update all dynamic
14493 blocks in the buffer."
14496 (org-update-all-dblocks)
14497 (or (looking-at org-dblock-start-re)
14498 (org-beginning-of-dblock))
14499 (org-update-dblock)))
14501 (defun org-update-dblock ()
14502 "Update the dynamic block at point
14503 This means to empty the block, parse for parameters and then call
14504 the correct writing function."
14505 (save-window-excursion
14506 (let* ((pos (point))
14507 (line (org-current-line))
14508 (params (org-prepare-dblock))
14509 (name (plist-get params :name))
14510 (cmd (intern (concat "org-dblock-write:" name))))
14511 (message "Updating dynamic block `%s' at line %d..." name line)
14512 (funcall cmd params)
14513 (message "Updating dynamic block `%s' at line %d...done" name line)
14516 (defun org-beginning-of-dblock ()
14517 "Find the beginning of the dynamic block at point.
14518 Error if there is no scuh block at point."
14519 (let ((pos (point))
14522 (if (and (re-search-backward org-dblock-start-re nil t)
14523 (setq beg (match-beginning 0))
14524 (re-search-forward org-dblock-end-re nil t)
14525 (> (match-end 0) pos))
14528 (error "Not in a dynamic block"))))
14530 (defun org-update-all-dblocks ()
14531 "Update all dynamic blocks in the buffer.
14532 This function can be used in a hook."
14534 (org-map-dblocks 'org-update-dblock)))
14539 (defconst org-additional-option-like-keywords
14540 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
14541 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
14542 "BEGIN_EXAMPLE" "END_EXAMPLE"))
14544 (defun org-complete (&optional arg)
14545 "Perform completion on word at point.
14546 At the beginning of a headline, this completes TODO keywords as given in
14547 `org-todo-keywords'.
14548 If the current word is preceded by a backslash, completes the TeX symbols
14549 that are supported for HTML support.
14550 If the current word is preceded by \"#+\", completes special words for
14551 setting file options.
14552 In the line after \"#+STARTUP:, complete valid keywords.\"
14553 At all other locations, this simply calls the value of
14554 `org-completion-fallback-command'."
14556 (org-without-partial-completion
14558 (let* ((end (point))
14559 (beg1 (save-excursion
14560 (skip-chars-backward (org-re "[:alnum:]_@"))
14562 (beg (save-excursion
14563 (skip-chars-backward "a-zA-Z0-9_:$")
14565 (confirm (lambda (x) (stringp (car x))))
14566 (searchhead (equal (char-before beg) ?*))
14567 (tag (and (equal (char-before beg1) ?:)
14568 (equal (char-after (point-at-bol)) ?*)))
14569 (prop (and (equal (char-before beg1) ?:)
14570 (not (equal (char-after (point-at-bol)) ?*))))
14571 (texp (equal (char-before beg) ?\\))
14572 (link (equal (char-before beg) ?\[))
14573 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
14576 (startup (string-match "^#\\+STARTUP:.*"
14577 (buffer-substring (point-at-bol) (point))))
14578 (completion-ignore-case opt)
14587 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
14588 (cons (match-string 2 x) (match-string 1 x)))
14589 (org-split-string (org-get-current-options) "\n"))
14590 (mapcar 'list org-additional-option-like-keywords)))
14592 (setq type :startup)
14593 org-startup-options)
14594 (link (append org-link-abbrev-alist-local
14595 org-link-abbrev-alist))
14599 ((string-match "\\`\\*+[ \t]+\\'"
14600 (buffer-substring (point-at-bol) beg))
14602 (mapcar 'list org-todo-keywords-1))
14604 (setq type :searchhead)
14606 (goto-char (point-min))
14607 (while (re-search-forward org-todo-line-regexp nil t)
14609 (org-make-org-heading-search-string
14610 (match-string 3) t))
14613 (tag (setq type :tag beg beg1)
14614 (or org-tag-alist (org-get-buffer-tags)))
14615 (prop (setq type :prop beg beg1)
14616 (mapcar 'list (org-buffer-property-keys nil t t)))
14618 (call-interactively org-completion-fallback-command)
14619 (throw 'exit nil)))))
14620 (pattern (buffer-substring-no-properties beg end))
14621 (completion (try-completion pattern table confirm)))
14622 (cond ((eq completion t)
14623 (if (not (assoc (upcase pattern) table))
14624 (message "Already complete")
14625 (if (equal type :opt)
14626 (insert (substring (cdr (assoc (upcase pattern) table))
14628 (if (memq type '(:tag :prop)) (insert ":")))))
14630 (message "Can't find completion for \"%s\"" pattern)
14632 ((not (string= pattern completion))
14633 (delete-region beg end)
14634 (if (string-match " +$" completion)
14635 (setq completion (replace-match "" t t completion)))
14636 (insert completion)
14637 (if (get-buffer-window "*Completions*")
14638 (delete-window (get-buffer-window "*Completions*")))
14639 (if (assoc completion table)
14640 (if (eq type :todo) (insert " ")
14641 (if (memq type '(:tag :prop)) (insert ":"))))
14642 (if (and (equal type :opt) (assoc completion table))
14643 (message "%s" (substitute-command-keys
14644 "Press \\[org-complete] again to insert example settings"))))
14646 (message "Making completion list...")
14647 (let ((list (sort (all-completions pattern table confirm)
14649 (with-output-to-temp-buffer "*Completions*"
14650 (condition-case nil
14651 ;; Protection needed for XEmacs and emacs 21
14652 (display-completion-list list pattern)
14653 (error (display-completion-list list)))))
14654 (message "Making completion list...%s" "done")))))))
14656 ;;;; TODO, DEADLINE, Comments
14658 (defun org-toggle-comment ()
14659 "Change the COMMENT state of an entry."
14662 (org-back-to-heading)
14663 (let (case-fold-search)
14664 (if (looking-at (concat outline-regexp
14665 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
14666 (replace-match "" t t nil 1)
14667 (if (looking-at outline-regexp)
14669 (goto-char (match-end 0))
14670 (insert org-comment-string " ")))))))
14672 (defvar org-last-todo-state-is-todo nil
14673 "This is non-nil when the last TODO state change led to a TODO state.
14674 If the last change removed the TODO tag or switched to DONE, then
14677 (defvar org-setting-tags nil) ; dynamically skiped
14679 ;; FIXME: better place
14680 (defun org-property-or-variable-value (var &optional inherit)
14681 "Check if there is a property fixing the value of VAR.
14682 If yes, return this value. If not, return the current value of the variable."
14683 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14684 (if (and prop (stringp prop) (string-match "\\S-" prop))
14686 (symbol-value var))))
14688 (defun org-parse-local-options (string var)
14689 "Parse STRING for startup setting relevant for variable VAR."
14690 (let ((rtn (symbol-value var))
14693 (if (or (not string) (not (string-match "\\S-" string)))
14695 (setq opts (delq nil (mapcar (lambda (x)
14696 (setq e (assoc x org-startup-options))
14697 (if (eq (nth 1 e) var) e nil))
14698 (org-split-string string "[ \t]+"))))
14702 (while (setq e (pop opts))
14703 (if (not (nth 3 e))
14704 (setq rtn (nth 2 e))
14705 (if (not (listp rtn)) (setq rtn nil))
14706 (push (nth 2 e) rtn)))
14709 (defvar org-blocker-hook nil
14710 "Hook for functions that are allowed to block a state change.
14712 Each function gets as its single argument a property list, see
14713 `org-trigger-hook' for more information about this list.
14715 If any of the functions in this hook returns nil, the state change
14718 (defvar org-trigger-hook nil
14719 "Hook for functions that are triggered by a state change.
14721 Each function gets as its single argument a property list with at least
14722 the following elements:
14724 (:type type-of-change :position pos-at-entry-start
14725 :from old-state :to new-state)
14727 Depending on the type, more properties may be present.
14729 This mechanism is currently implemented for:
14733 :type todo-state-change
14734 :from previous state (keyword as a string), or nil
14735 :to new state (keyword as a string), or nil")
14738 (defun org-todo (&optional arg)
14739 "Change the TODO state of an item.
14740 The state of an item is given by a keyword at the start of the heading,
14742 *** TODO Write paper
14745 The different keywords are specified in the variable `org-todo-keywords'.
14746 By default the available states are \"TODO\" and \"DONE\".
14747 So for this example: when the item starts with TODO, it is changed to DONE.
14748 When it starts with DONE, the DONE is removed. And when neither TODO nor
14749 DONE are present, add TODO at the beginning of the heading.
14751 With C-u prefix arg, use completion to determine the new state.
14752 With numeric prefix arg, switch to that state.
14754 For calling through lisp, arg is also interpreted in the following way:
14755 'none -> empty state
14756 \"\"(empty string) -> switch to empty state
14757 'done -> switch to DONE
14758 'nextset -> switch to the next set of keywords
14759 'previousset -> switch to the previous set of keywords
14760 \"WAITING\" -> switch to the specified keyword, but only if it
14761 really is a member of `org-todo-keywords'."
14765 (org-back-to-heading)
14766 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
14767 (or (looking-at (concat " +" org-todo-regexp " *"))
14769 (let* ((match-data (match-data))
14770 (startpos (point-at-bol))
14771 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
14772 (org-log-done org-log-done)
14773 (org-log-repeat org-log-repeat)
14774 (org-todo-log-states org-todo-log-states)
14775 (this (match-string 1))
14776 (hl-pos (match-beginning 0))
14777 (head (org-get-todo-sequence-head this))
14778 (ass (assoc head org-todo-kwd-alist))
14779 (interpret (nth 1 ass))
14780 (done-word (nth 3 ass))
14781 (final-done-word (nth 4 ass))
14782 (last-state (or this ""))
14783 (completion-ignore-case t)
14784 (member (member this org-todo-keywords-1))
14785 (tail (cdr member))
14787 ((and org-todo-key-trigger
14788 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
14789 (and (not arg) org-use-fast-todo-selection
14790 (not (eq org-use-fast-todo-selection 'prefix)))))
14791 ;; Use fast selection
14792 (org-fast-todo-selection))
14793 ((and (equal arg '(4))
14794 (or (not org-use-fast-todo-selection)
14795 (not org-todo-key-trigger)))
14796 ;; Read a state with completion
14797 (completing-read "State: " (mapcar (lambda(x) (list x))
14798 org-todo-keywords-1)
14802 (if tail (car tail) nil)
14803 (car org-todo-keywords-1)))
14805 (if (equal member org-todo-keywords-1)
14808 (nth (- (length org-todo-keywords-1) (length tail) 2)
14809 org-todo-keywords-1)
14810 (org-last org-todo-keywords-1))))
14811 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
14812 (setq arg nil))) ; hack to fall back to cycling
14814 ;; user or caller requests a specific state
14816 ((equal arg "") nil)
14817 ((eq arg 'none) nil)
14818 ((eq arg 'done) (or done-word (car org-done-keywords)))
14820 (or (car (cdr (member head org-todo-heads)))
14821 (car org-todo-heads)))
14822 ((eq arg 'previousset)
14823 (let ((org-todo-heads (reverse org-todo-heads)))
14824 (or (car (cdr (member head org-todo-heads)))
14825 (car org-todo-heads))))
14826 ((car (member arg org-todo-keywords-1)))
14827 ((nth (1- (prefix-numeric-value arg))
14828 org-todo-keywords-1))))
14829 ((null member) (or head (car org-todo-keywords-1)))
14830 ((equal this final-done-word) nil) ;; -> make empty
14831 ((null tail) nil) ;; -> first entry
14832 ((eq interpret 'sequence)
14834 ((memq interpret '(type priority))
14835 (if (eq this-command last-command)
14837 (if (> (length tail) 0)
14838 (or done-word (car org-done-keywords))
14841 (next (if state (concat " " state " ") " "))
14842 (change-plist (list :type 'todo-state-change :from this :to state
14843 :position startpos))
14845 (when org-blocker-hook
14846 (unless (save-excursion
14848 (run-hook-with-args-until-failure
14849 'org-blocker-hook change-plist)))
14850 (if (interactive-p)
14851 (error "TODO state change from %s to %s blocked" this state)
14853 (message "TODO state change from %s to %s blocked" this state)
14854 (throw 'exit nil))))
14855 (store-match-data match-data)
14856 (replace-match next t t)
14857 (unless (pos-visible-in-window-p hl-pos)
14858 (message "TODO state changed to %s" (org-trim next)))
14860 (setq head (org-get-todo-sequence-head state)
14861 ass (assoc head org-todo-kwd-alist)
14862 interpret (nth 1 ass)
14863 done-word (nth 3 ass)
14864 final-done-word (nth 4 ass)))
14865 (when (memq arg '(nextset previousset))
14866 (message "Keyword-Set %d/%d: %s"
14867 (- (length org-todo-sets) -1
14868 (length (memq (assoc state org-todo-sets) org-todo-sets)))
14869 (length org-todo-sets)
14870 (mapconcat 'identity (assoc state org-todo-sets) " ")))
14871 (setq org-last-todo-state-is-todo
14872 (not (member state org-done-keywords)))
14873 (setq now-done-p (and (member state org-done-keywords)
14874 (not (member this org-done-keywords))))
14875 (and logging (org-local-logging logging))
14876 (when (and (or org-todo-log-states org-log-done)
14877 (not (memq arg '(nextset previousset))))
14878 ;; we need to look at recording a time and note
14879 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
14880 (nth 2 (assoc this org-todo-log-states))))
14882 (member state org-not-done-keywords)
14883 (not (member this org-not-done-keywords)))
14884 ;; This is now a todo state and was not one before
14885 ;; If there was a CLOSED time stamp, get rid of it.
14886 (org-add-planning-info nil nil 'closed))
14887 (when (and now-done-p org-log-done)
14888 ;; It is now done, and it was not done before
14889 (org-add-planning-info 'closed (org-current-time))
14890 (if (and (not dolog) (eq 'note org-log-done))
14891 (org-add-log-maybe 'done state 'findpos 'note)))
14892 (when (and state dolog)
14893 ;; This is a non-nil state, and we need to log it
14894 (org-add-log-maybe 'state state 'findpos dolog)))
14895 ;; Fixup tag positioning
14896 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
14897 (run-hooks 'org-after-todo-state-change-hook)
14898 (if (and arg (not (member state org-done-keywords)))
14899 (setq head (org-get-todo-sequence-head state)))
14900 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
14901 ;; Do we need to trigger a repeat?
14902 (when now-done-p (org-auto-repeat-maybe state))
14903 ;; Fixup cursor location if close to the keyword
14904 (if (and (outline-on-heading-p)
14906 (save-excursion (beginning-of-line 1)
14907 (looking-at org-todo-line-regexp))
14908 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
14910 (goto-char (or (match-end 2) (match-end 1)))
14912 (when org-trigger-hook
14914 (run-hook-with-args 'org-trigger-hook change-plist)))))))
14916 (defun org-local-logging (value)
14917 "Get logging settings from a property VALUE."
14919 ;; directly set the variables, they are already local.
14920 (setq org-log-done nil
14922 org-todo-log-states nil)
14923 (setq words (org-split-string value))
14924 (while (setq w (pop words))
14926 ((setq a (assoc w org-startup-options))
14927 (and (member (nth 1 a) '(org-log-done org-log-repeat))
14928 (set (nth 1 a) (nth 2 a))))
14929 ((setq a (org-extract-log-state-settings w))
14930 (and (member (car a) org-todo-keywords-1)
14931 (push a org-todo-log-states)))))))
14933 (defun org-get-todo-sequence-head (kwd)
14934 "Return the head of the TODO sequence to which KWD belongs.
14935 If KWD is not set, check if there is a text property remembering the
14940 (or (get-text-property (point-at-bol) 'org-todo-head)
14942 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
14943 nil (point-at-eol)))
14944 (get-text-property p 'org-todo-head))))
14945 ((not (member kwd org-todo-keywords-1))
14946 (car org-todo-keywords-1))
14947 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
14949 (defun org-fast-todo-selection ()
14950 "Fast TODO keyword selection with single keys.
14951 Returns the new TODO keyword, or nil if no state change should occur."
14952 (let* ((fulltable org-todo-key-alist)
14953 (done-keywords org-done-keywords) ;; needed for the faces.
14954 (maxlen (apply 'max (mapcar
14956 (if (stringp (car x)) (string-width (car x)) 0))
14959 (fwidth (+ maxlen 3 1 3))
14960 (ncol (/ (- (window-width) 4) fwidth))
14963 (save-window-excursion
14965 (set-buffer (get-buffer-create " *Org todo*"))
14966 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
14968 (org-set-local 'org-done-keywords done-keywords)
14969 (setq tbl fulltable cnt 0)
14970 (while (setq e (pop tbl))
14972 ((equal e '(:startgroup))
14973 (push '() groups) (setq ingroup t)
14974 (when (not (= cnt 0))
14978 ((equal e '(:endgroup))
14979 (setq ingroup nil cnt 0)
14982 (setq tg (car e) c (cdr e))
14983 (if ingroup (push tg (car groups)))
14984 (setq tg (org-add-props tg nil 'face
14985 (org-get-todo-face tg)))
14986 (if (and (= cnt 0) (not ingroup)) (insert " "))
14987 (insert "[" c "] " tg (make-string
14988 (- fwidth 4 (length tg)) ?\ ))
14989 (when (= (setq cnt (1+ cnt)) ncol)
14991 (if ingroup (insert " "))
14994 (goto-char (point-min))
14995 (if (and (not expert) (fboundp 'fit-window-to-buffer))
14996 (fit-window-to-buffer))
14997 (message "[a-z..]:Set [SPC]:clear")
14998 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
15001 (and (= c ?q) (not (rassoc c fulltable))))
15002 (setq quit-flag t))
15004 ((setq e (rassoc c fulltable) tg (car e))
15006 (t (setq quit-flag t))))))
15008 (defun org-get-repeat ()
15009 "Check if tere is a deadline/schedule with repeater in this entry."
15012 (org-back-to-heading t)
15013 (if (re-search-forward
15014 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
15015 (match-string 1)))))
15017 (defvar org-last-changed-timestamp)
15018 (defvar org-log-post-message)
15019 (defvar org-log-note-purpose)
15020 (defun org-auto-repeat-maybe (done-word)
15021 "Check if the current headline contains a repeated deadline/schedule.
15022 If yes, set TODO state back to what it was and change the base date
15023 of repeating deadline/scheduled time stamps to new date.
15024 This function is run automatically after each state change to a DONE state."
15025 ;; last-state is dynamically scoped into this function
15026 (let* ((repeat (org-get-repeat))
15027 (aa (assoc last-state org-todo-kwd-alist))
15028 (interpret (nth 1 aa))
15030 (whata '(("d" . day) ("m" . month) ("y" . year)))
15031 (msg "Entry repeats: ")
15033 (org-todo-log-states nil)
15034 (nshiftmax 10) (nshift 0)
15035 re type n what ts mb0 time)
15037 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
15038 (org-todo (if (eq interpret 'type) last-state head))
15039 (when (and org-log-repeat
15040 (or (not (memq 'org-add-log-note
15041 (default-value 'post-command-hook)))
15042 (eq org-log-note-purpose 'done)))
15043 ;; Make sure a note is taken;
15044 (org-add-log-maybe 'state (or done-word (car org-done-keywords))
15045 'findpos org-log-repeat))
15046 (org-back-to-heading t)
15047 (org-add-planning-info nil nil 'closed)
15048 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
15049 org-deadline-time-regexp "\\)\\|\\("
15050 org-ts-regexp "\\)"))
15051 (while (re-search-forward
15052 re (save-excursion (outline-next-heading) (point)) t)
15053 (setq type (if (match-end 1) org-scheduled-string
15054 (if (match-end 3) org-deadline-string "Plain:"))
15055 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
15056 mb0 (match-beginning 0))
15057 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
15058 (setq n (string-to-number (match-string 2 ts))
15059 what (match-string 3 ts))
15060 (if (equal what "w") (setq n (* n 7) what "d"))
15061 ;; Preparation, see if we need to modify the start date for the change
15062 (when (match-end 1)
15063 (setq time (save-match-data (org-time-string-to-time ts)))
15065 ((equal (match-string 1 ts) ".")
15066 ;; Shift starting date to today
15067 (org-timestamp-change
15068 (- (time-to-days (current-time)) (time-to-days time))
15070 ((equal (match-string 1 ts) "+")
15071 (while (< (time-to-days time) (time-to-days (current-time)))
15072 (when (= (incf nshift) nshiftmax)
15073 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
15075 (org-timestamp-change n (cdr (assoc what whata)))
15076 (sit-for .0001) ;; so we can watch the date shifting
15077 (org-at-timestamp-p t)
15078 (setq ts (match-string 1))
15079 (setq time (save-match-data (org-time-string-to-time ts))))
15080 (org-timestamp-change (- n) (cdr (assoc what whata)))
15081 ;; rematch, so that we have everything in place for the real shift
15082 (org-at-timestamp-p t)
15083 (setq ts (match-string 1))
15084 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
15085 (org-timestamp-change n (cdr (assoc what whata)))
15086 (setq msg (concat msg type org-last-changed-timestamp " "))))
15087 (setq org-log-post-message msg)
15088 (message "%s" msg))))
15090 (defun org-show-todo-tree (arg)
15091 "Make a compact tree which shows all headlines marked with TODO.
15092 The tree will show the lines where the regexp matches, and all higher
15093 headlines above the match.
15094 With a \\[universal-argument] prefix, also show the DONE entries.
15095 With a numeric prefix N, construct a sparse tree for the Nth element
15096 of `org-todo-keywords-1'."
15098 (let ((case-fold-search nil)
15100 (cond ((null arg) org-not-done-regexp)
15102 (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
15103 (mapcar 'list org-todo-keywords-1))))
15105 (mapconcat 'identity (org-split-string kwd "|") "\\|")
15107 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
15108 (regexp-quote (nth (1- (prefix-numeric-value arg))
15109 org-todo-keywords-1)))
15110 (t (error "Invalid prefix argument: %s" arg)))))
15111 (message "%d TODO entries found"
15112 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
15114 (defun org-deadline (&optional remove)
15115 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
15116 With argument REMOVE, remove any deadline from the item."
15120 (org-remove-timestamp-with-keyword org-deadline-string)
15121 (message "Item no longer has a deadline."))
15122 (org-add-planning-info 'deadline nil 'closed)))
15124 (defun org-schedule (&optional remove)
15125 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
15126 With argument REMOVE, remove any scheduling date from the item."
15130 (org-remove-timestamp-with-keyword org-scheduled-string)
15131 (message "Item is no longer scheduled."))
15132 (org-add-planning-info 'scheduled nil 'closed)))
15134 (defun org-remove-timestamp-with-keyword (keyword)
15135 "Remove all time stamps with KEYWORD in the current entry."
15136 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
15139 (org-back-to-heading t)
15141 (org-end-of-subtree t t)
15142 (while (re-search-backward re beg t)
15144 (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
15145 (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
15147 (defun org-add-planning-info (what &optional time &rest remove)
15148 "Insert new timestamp with keyword in the line directly after the headline.
15149 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
15150 If non is given, the user is prompted for a date.
15151 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
15154 (let (org-time-was-given org-end-time-was-given)
15155 (when what (setq time (or time (org-read-date nil 'to-time))))
15156 (when (and org-insert-labeled-timestamps-at-point
15157 (member what '(scheduled deadline)))
15159 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
15160 (org-insert-time-stamp time org-time-was-given
15161 nil nil nil (list org-end-time-was-given))
15165 (let (col list elt ts buffer-invisibility-spec)
15166 (org-back-to-heading t)
15167 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
15168 (goto-char (match-end 1))
15169 (setq col (current-column))
15170 (goto-char (match-end 0))
15171 (if (eobp) (insert "\n") (forward-char 1))
15172 (if (and (not (looking-at outline-regexp))
15173 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
15175 (not (equal (match-string 1) org-clock-string)))
15176 (narrow-to-region (match-beginning 0) (match-end 0))
15177 (insert-before-markers "\n")
15179 (narrow-to-region (point) (point))
15180 (indent-to-column col))
15181 ;; Check if we have to remove something.
15182 (setq list (cons what remove))
15184 (setq elt (pop list))
15185 (goto-char (point-min))
15186 (when (or (and (eq elt 'scheduled)
15187 (re-search-forward org-scheduled-time-regexp nil t))
15188 (and (eq elt 'deadline)
15189 (re-search-forward org-deadline-time-regexp nil t))
15190 (and (eq elt 'closed)
15191 (re-search-forward org-closed-time-regexp nil t)))
15193 (if (looking-at "--+<[^>]+>") (replace-match ""))
15194 (if (looking-at " +") (replace-match ""))))
15195 (goto-char (point-max))
15198 (if (not (equal (char-before) ?\ )) " " "")
15199 (cond ((eq what 'scheduled) org-scheduled-string)
15200 ((eq what 'deadline) org-deadline-string)
15201 ((eq what 'closed) org-closed-string))
15203 (setq ts (org-insert-time-stamp
15205 (or org-time-was-given
15206 (and (eq what 'closed) org-log-done-with-time))
15208 nil nil (list org-end-time-was-given)))
15210 (goto-char (point-min))
15212 (if (looking-at "[ \t]+\r?\n")
15213 (replace-match ""))
15216 (defvar org-log-note-marker (make-marker))
15217 (defvar org-log-note-purpose nil)
15218 (defvar org-log-note-state nil)
15219 (defvar org-log-note-how nil)
15220 (defvar org-log-note-window-configuration nil)
15221 (defvar org-log-note-return-to (make-marker))
15222 (defvar org-log-post-message nil
15223 "Message to be displayed after a log note has been stored.
15224 The auto-repeater uses this.")
15226 (defun org-add-log-maybe (&optional purpose state findpos how)
15227 "Set up the post command hook to take a note.
15228 If this is about to TODO state change, the new state is expected in STATE.
15229 When FINDPOS is non-nil, find the correct position for the note in
15230 the current entry. If not, assume that it can be inserted at point."
15233 (org-back-to-heading t)
15234 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
15235 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
15237 (goto-char (match-end 0))
15238 (unless org-log-states-order-reversed
15239 (and (= (char-after) ?\n) (forward-char 1))
15240 (org-skip-over-state-notes)
15241 (skip-chars-backward " \t\n\r")))
15242 (move-marker org-log-note-marker (point))
15243 (setq org-log-note-purpose purpose
15244 org-log-note-state state
15245 org-log-note-how how)
15246 (add-hook 'post-command-hook 'org-add-log-note 'append)))
15248 (defun org-skip-over-state-notes ()
15249 "Skip past the list of State notes in an entry."
15250 (if (looking-at "\n[ \t]*- State") (forward-char 1))
15251 (while (looking-at "[ \t]*- State")
15252 (condition-case nil
15254 (error (org-end-of-item)))))
15256 (defun org-add-log-note (&optional purpose)
15257 "Pop up a window for taking a note, and add this note later at point."
15258 (remove-hook 'post-command-hook 'org-add-log-note)
15259 (setq org-log-note-window-configuration (current-window-configuration))
15260 (delete-other-windows)
15261 (move-marker org-log-note-return-to (point))
15262 (switch-to-buffer (marker-buffer org-log-note-marker))
15263 (goto-char org-log-note-marker)
15264 (org-switch-to-buffer-other-window "*Org Note*")
15266 (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
15267 (org-store-log-note)
15268 (let ((org-inhibit-startup t)) (org-mode))
15269 (insert (format "# Insert note for %s.
15270 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
15272 ((eq org-log-note-purpose 'clock-out) "stopped clock")
15273 ((eq org-log-note-purpose 'done) "closed todo item")
15274 ((eq org-log-note-purpose 'state)
15275 (format "state change to \"%s\"" org-log-note-state))
15276 (t (error "This should not happen")))))
15277 (org-set-local 'org-finish-function 'org-store-log-note)))
15279 (defun org-store-log-note ()
15280 "Finish taking a log note, and insert it to where it belongs."
15281 (let ((txt (buffer-string))
15282 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
15284 (kill-buffer (current-buffer))
15285 (while (string-match "\\`#.*\n[ \t\n]*" txt)
15286 (setq txt (replace-match "" t t txt)))
15287 (if (string-match "\\s-+\\'" txt)
15288 (setq txt (replace-match "" t t txt)))
15289 (setq lines (org-split-string txt "\n"))
15290 (when (and note (string-match "\\S-" note))
15292 (org-replace-escapes
15294 (list (cons "%u" (user-login-name))
15295 (cons "%U" user-full-name)
15296 (cons "%t" (format-time-string
15297 (org-time-stamp-format 'long 'inactive)
15299 (cons "%s" (if org-log-note-state
15300 (concat "\"" org-log-note-state "\"")
15302 (if lines (setq note (concat note " \\\\")))
15304 (when (or current-prefix-arg org-note-abort) (setq lines nil))
15307 (set-buffer (marker-buffer org-log-note-marker))
15309 (goto-char org-log-note-marker)
15310 (move-marker org-log-note-marker nil)
15312 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
15313 (indent-relative nil)
15314 (insert "- " (pop lines))
15315 (org-indent-line-function)
15316 (beginning-of-line 1)
15317 (looking-at "[ \t]*")
15318 (setq ind (concat (match-string 0) " "))
15320 (while lines (insert "\n" ind (pop lines)))))))
15321 (set-window-configuration org-log-note-window-configuration)
15322 (with-current-buffer (marker-buffer org-log-note-return-to)
15323 (goto-char org-log-note-return-to))
15324 (move-marker org-log-note-return-to nil)
15325 (and org-log-post-message (message "%s" org-log-post-message)))
15327 ;; FIXME: what else would be useful?
15331 (defun org-sparse-tree (&optional arg)
15332 "Create a sparse tree, prompt for the details.
15333 This command can create sparse trees. You first need to select the type
15334 of match used to create the tree:
15336 t Show entries with a specific TODO keyword.
15337 T Show entries selected by a tags match.
15338 p Enter a property name and its value (both with completion on existing
15339 names/values) and show entries with that property.
15340 r Show entries matching a regular expression
15341 d Show deadlines due within `org-deadline-warning-days'."
15343 (let (ans kwd value)
15344 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
15345 (setq ans (read-char-exclusive))
15348 (call-interactively 'org-check-deadlines))
15350 (call-interactively 'org-check-before-date))
15352 (org-show-todo-tree '(4)))
15354 (call-interactively 'org-tags-sparse-tree))
15355 ((member ans '(?p ?P))
15356 (setq kwd (completing-read "Property: "
15357 (mapcar 'list (org-buffer-property-keys))))
15358 (setq value (completing-read "Value: "
15359 (mapcar 'list (org-property-values kwd))))
15360 (unless (string-match "\\`{.*}\\'" value)
15361 (setq value (concat "\"" value "\"")))
15362 (org-tags-sparse-tree arg (concat kwd "=" value)))
15363 ((member ans '(?r ?R ?/))
15364 (call-interactively 'org-occur))
15365 (t (error "No such sparse tree command \"%c\"" ans)))))
15367 (defvar org-occur-highlights nil
15368 "List of overlays used for occur matches.")
15369 (make-variable-buffer-local 'org-occur-highlights)
15370 (defvar org-occur-parameters nil
15371 "Parameters of the active org-occur calls.
15372 This is a list, each call to org-occur pushes as cons cell,
15373 containing the regular expression and the callback, onto the list.
15374 The list can contain several entries if `org-occur' has been called
15375 several time with the KEEP-PREVIOUS argument. Otherwise, this list
15376 will only contain one set of parameters. When the highlights are
15377 removed (for example with `C-c C-c', or with the next edit (depending
15378 on `org-remove-highlights-with-change'), this variable is emptied
15380 (make-variable-buffer-local 'org-occur-parameters)
15382 (defun org-occur (regexp &optional keep-previous callback)
15383 "Make a compact tree which shows all matches of REGEXP.
15384 The tree will show the lines where the regexp matches, and all higher
15385 headlines above the match. It will also show the heading after the match,
15386 to make sure editing the matching entry is easy.
15387 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
15388 call to `org-occur' will be kept, to allow stacking of calls to this
15390 If CALLBACK is non-nil, it is a function which is called to confirm
15391 that the match should indeed be shown."
15392 (interactive "sRegexp: \nP")
15393 (unless keep-previous
15394 (org-remove-occur-highlights nil nil t))
15395 (push (cons regexp callback) org-occur-parameters)
15398 (goto-char (point-min))
15399 (if (or (not keep-previous) ; do not want to keep
15400 (not org-occur-highlights)) ; no previous matches
15403 (while (re-search-forward regexp nil t)
15404 (when (or (not callback)
15405 (save-match-data (funcall callback)))
15406 (setq cnt (1+ cnt))
15407 (when org-highlight-sparse-tree-matches
15408 (org-highlight-new-match (match-beginning 0) (match-end 0)))
15409 (org-show-context 'occur-tree))))
15410 (when org-remove-highlights-with-change
15411 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
15413 (unless org-sparse-tree-open-archived-trees
15414 (org-hide-archived-subtrees (point-min) (point-max)))
15415 (run-hooks 'org-occur-hook)
15416 (if (interactive-p)
15417 (message "%d match(es) for regexp %s" cnt regexp))
15420 (defun org-show-context (&optional key)
15421 "Make sure point and context and visible.
15422 How much context is shown depends upon the variables
15423 `org-show-hierarchy-above', `org-show-following-heading'. and
15424 `org-show-siblings'."
15425 (let ((heading-p (org-on-heading-p t))
15426 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
15427 (following-p (org-get-alist-option org-show-following-heading key))
15428 (entry-p (org-get-alist-option org-show-entry-below key))
15429 (siblings-p (org-get-alist-option org-show-siblings key)))
15431 ;; Show heading or entry text
15432 (if (and heading-p (not entry-p))
15433 (org-flag-heading nil) ; only show the heading
15434 (and (or entry-p (org-invisible-p) (org-invisible-p2))
15435 (org-show-hidden-entry))) ; show entire entry
15437 ;; Show next sibling, or heading below text
15439 (and (if heading-p (org-goto-sibling) (outline-next-heading))
15440 (org-flag-heading nil))))
15441 (when siblings-p (org-show-siblings))
15443 ;; show all higher headings, possibly with siblings
15445 (while (and (condition-case nil
15446 (progn (org-up-heading-all 1) t)
15449 (org-flag-heading nil)
15450 (when siblings-p (org-show-siblings))))))))
15452 (defun org-reveal (&optional siblings)
15453 "Show current entry, hierarchy above it, and the following headline.
15454 This can be used to show a consistent set of context around locations
15455 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
15456 not t for the search context.
15458 With optional argument SIBLINGS, on each level of the hierarchy all
15459 siblings are shown. This repairs the tree structure to what it would
15460 look like when opened with hierarchical calls to `org-cycle'."
15462 (let ((org-show-hierarchy-above t)
15463 (org-show-following-heading t)
15464 (org-show-siblings (if siblings t org-show-siblings)))
15465 (org-show-context nil)))
15467 (defun org-highlight-new-match (beg end)
15468 "Highlight from BEG to END and mark the highlight is an occur headline."
15469 (let ((ov (org-make-overlay beg end)))
15470 (org-overlay-put ov 'face 'secondary-selection)
15471 (push ov org-occur-highlights)))
15473 (defun org-remove-occur-highlights (&optional beg end noremove)
15474 "Remove the occur highlights from the buffer.
15475 BEG and END are ignored. If NOREMOVE is nil, remove this function
15476 from the `before-change-functions' in the current buffer."
15478 (unless org-inhibit-highlight-removal
15479 (mapc 'org-delete-overlay org-occur-highlights)
15480 (setq org-occur-highlights nil)
15481 (setq org-occur-parameters nil)
15483 (remove-hook 'before-change-functions
15484 'org-remove-occur-highlights 'local))))
15488 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
15489 "Regular expression matching the priority indicator.")
15491 (defvar org-remove-priority-next-time nil)
15493 (defun org-priority-up ()
15494 "Increase the priority of the current item."
15496 (org-priority 'up))
15498 (defun org-priority-down ()
15499 "Decrease the priority of the current item."
15501 (org-priority 'down))
15503 (defun org-priority (&optional action)
15504 "Change the priority of an item by ARG.
15505 ACTION can be `set', `up', `down', or a character."
15507 (setq action (or action 'set))
15508 (let (current new news have remove)
15510 (org-back-to-heading)
15511 (if (looking-at org-priority-regexp)
15512 (setq current (string-to-char (match-string 2))
15514 (setq current org-default-priority))
15516 ((or (eq action 'set) (integerp action))
15517 (if (integerp action)
15519 (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
15520 (setq new (read-char-exclusive)))
15521 (if (and (= (upcase org-highest-priority) org-highest-priority)
15522 (= (upcase org-lowest-priority) org-lowest-priority))
15523 (setq new (upcase new)))
15524 (cond ((equal new ?\ ) (setq remove t))
15525 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
15526 (error "Priority must be between `%c' and `%c'"
15527 org-highest-priority org-lowest-priority))))
15529 (if (and (not have) (eq last-command this-command))
15530 (setq new org-lowest-priority)
15531 (setq new (if (and org-priority-start-cycle-with-default (not have))
15532 org-default-priority (1- current)))))
15534 (if (and (not have) (eq last-command this-command))
15535 (setq new org-highest-priority)
15536 (setq new (if (and org-priority-start-cycle-with-default (not have))
15537 org-default-priority (1+ current)))))
15538 (t (error "Invalid action")))
15539 (if (or (< (upcase new) org-highest-priority)
15540 (> (upcase new) org-lowest-priority))
15542 (setq news (format "%c" new))
15545 (replace-match "" t t nil 1)
15546 (replace-match news t t nil 2))
15548 (error "No priority cookie found in line")
15549 (looking-at org-todo-line-regexp)
15552 (goto-char (match-end 2))
15553 (insert " [#" news "]"))
15554 (goto-char (match-beginning 3))
15555 (insert "[#" news "] ")))))
15556 (org-preserve-lc (org-set-tags nil 'align))
15558 (message "Priority removed")
15559 (message "Priority of current item set to %s" news))))
15562 (defun org-get-priority (s)
15563 "Find priority cookie and return priority."
15565 (if (not (string-match org-priority-regexp s))
15566 (* 1000 (- org-lowest-priority org-default-priority))
15567 (* 1000 (- org-lowest-priority
15568 (string-to-char (match-string 2 s)))))))
15572 (defun org-scan-tags (action matcher &optional todo-only)
15573 "Scan headline tags with inheritance and produce output ACTION.
15574 ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
15575 evaluated, testing if a given set of tags qualifies a headline for
15576 inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
15577 are included in the output."
15578 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
15579 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
15581 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
15582 (props (list 'face nil
15583 'done-face 'org-done
15585 'mouse-face 'highlight
15586 'org-not-done-regexp org-not-done-regexp
15587 'org-todo-regexp org-todo-regexp
15588 'keymap org-agenda-keymap
15590 (format "mouse-2 or RET jump to org file %s"
15591 (abbreviate-file-name
15592 (or (buffer-file-name (buffer-base-buffer))
15593 (buffer-name (buffer-base-buffer)))))))
15594 (case-fold-search nil)
15596 tags tags-list tags-alist (llast 0) rtn level category i txt
15597 todo marker entry priority)
15599 (goto-char (point-min))
15600 (when (eq action 'sparse-tree)
15602 (org-remove-occur-highlights))
15603 (while (re-search-forward re nil t)
15605 (setq todo (if (match-end 1) (match-string 2))
15606 tags (if (match-end 4) (match-string 4)))
15607 (goto-char (setq lspos (1+ (match-beginning 0))))
15608 (setq level (org-reduced-level (funcall outline-level))
15609 category (org-get-category))
15610 (setq i llast llast level)
15611 ;; remove tag lists from same and sublevels
15612 (while (>= i level)
15613 (when (setq entry (assoc i tags-alist))
15614 (setq tags-alist (delete entry tags-alist)))
15616 ;; add the nex tags
15618 (setq tags (mapcar 'downcase (org-split-string tags ":"))
15620 (cons (cons level tags) tags-alist)))
15621 ;; compile tags for current headline
15623 (if org-use-tag-inheritance
15624 (apply 'append (mapcar 'cdr tags-alist))
15626 (when (and (or (not todo-only) (member todo org-not-done-keywords))
15628 (or (not org-agenda-skip-archived-trees)
15629 (not (member org-archive-tag tags-list))))
15630 (and (eq action 'agenda) (org-agenda-skip))
15631 ;; list this headline
15633 (if (eq action 'sparse-tree)
15635 (and org-highlight-sparse-tree-matches
15636 (org-get-heading) (match-end 0)
15637 (org-highlight-new-match
15638 (match-beginning 0) (match-beginning 1)))
15639 (org-show-context 'tags-tree))
15640 (setq txt (org-format-agenda-item
15643 (if org-tags-match-list-sublevels
15644 (make-string (1- level) ?.) "")
15646 category tags-list)
15647 priority (org-get-priority txt))
15649 (setq marker (org-agenda-new-marker))
15650 (org-add-props txt props
15651 'org-marker marker 'org-hd-marker marker 'org-category category
15652 'priority priority 'type "tagsmatch")
15654 ;; if we are to skip sublevels, jump to end of subtree
15655 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
15656 (when (and (eq action 'sparse-tree)
15657 (not org-sparse-tree-open-archived-trees))
15658 (org-hide-archived-subtrees (point-min) (point-max)))
15661 (defvar todo-only) ;; dynamically scoped
15663 (defun org-tags-sparse-tree (&optional todo-only match)
15664 "Create a sparse tree according to tags string MATCH.
15665 MATCH can contain positive and negative selection of tags, like
15666 \"+WORK+URGENT-WITHBOSS\".
15667 If optional argument TODO_ONLY is non-nil, only select lines that are
15670 (org-prepare-agenda-buffers (list (current-buffer)))
15671 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
15673 (defvar org-cached-props nil)
15674 (defun org-cached-entry-get (pom property)
15675 (if (or (eq t org-use-property-inheritance)
15676 (member property org-use-property-inheritance))
15677 ;; Caching is not possible, check it directly
15678 (org-entry-get pom property 'inherit)
15679 ;; Get all properties, so that we can do complicated checks easily
15680 (cdr (assoc property (or org-cached-props
15681 (setq org-cached-props
15682 (org-entry-properties pom)))))))
15684 (defun org-global-tags-completion-table (&optional files)
15685 "Return the list of all tags in all agenda buffer/files."
15692 (set-buffer (find-file-noselect file))
15693 (append (org-get-buffer-tags)
15694 (mapcar (lambda (x) (if (stringp (car-safe x))
15695 (list (car-safe x)) nil))
15697 (if (and files (car files))
15699 (org-agenda-files))))))))
15701 (defun org-make-tags-matcher (match)
15702 "Create the TAGS//TODO matcher form for the selection string MATCH."
15703 ;; todo-only is scoped dynamically into this function, and the function
15704 ;; may change it it the matcher asksk for it.
15706 ;; Get a new match request, with completion
15707 (let ((org-last-tags-completion-table
15708 (org-global-tags-completion-table)))
15709 (setq match (completing-read
15710 "Match: " 'org-tags-completion-function nil nil nil
15711 'org-tags-history))))
15713 ;; Parse the string and create a lisp form
15714 (let ((match0 match)
15715 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
15717 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
15718 orterms term orlist re-p level-p prop-p pn pv cat-p gv)
15719 (if (string-match "/+" match)
15720 ;; match contains also a todo-matching request
15722 (setq tagsmatch (substring match 0 (match-beginning 0))
15723 todomatch (substring match (match-end 0)))
15724 (if (string-match "^!" todomatch)
15725 (setq todo-only t todomatch (substring todomatch 1)))
15726 (if (string-match "^\\s-*$" todomatch)
15727 (setq todomatch nil)))
15728 ;; only matching tags
15729 (setq tagsmatch match todomatch nil))
15731 ;; Make the tags matcher
15732 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
15733 (setq tagsmatcher t)
15734 (setq orterms (org-split-string tagsmatch "|") orlist nil)
15735 (while (setq term (pop orterms))
15736 (while (and (equal (substring term -1) "\\") orterms)
15737 (setq term (concat term "|" (pop orterms)))) ; repair bad split
15738 (while (string-match re term)
15739 (setq minus (and (match-end 1)
15740 (equal (match-string 1 term) "-"))
15741 tag (match-string 2 term)
15742 re-p (equal (string-to-char tag) ?{)
15743 level-p (match-end 3)
15744 prop-p (match-end 4)
15746 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
15747 (level-p `(= level ,(string-to-number
15748 (match-string 3 term))))
15750 (setq pn (match-string 4 term)
15751 pv (match-string 5 term)
15752 cat-p (equal pn "CATEGORY")
15753 re-p (equal (string-to-char pv) ?{)
15754 pv (substring pv 1 -1))
15755 (if (equal pn "CATEGORY")
15756 (setq gv '(get-text-property (point) 'org-category))
15757 (setq gv `(org-cached-entry-get nil ,pn)))
15759 `(string-match ,pv (or ,gv ""))
15760 `(equal ,pv (or ,gv ""))))
15761 (t `(member ,(downcase tag) tags-list)))
15762 mm (if minus (list 'not mm) mm)
15763 term (substring term (match-end 0)))
15764 (push mm tagsmatcher))
15765 (push (if (> (length tagsmatcher) 1)
15766 (cons 'and tagsmatcher)
15769 (setq tagsmatcher nil))
15770 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
15772 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
15774 ;; Make the todo matcher
15775 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
15776 (setq todomatcher t)
15777 (setq orterms (org-split-string todomatch "|") orlist nil)
15778 (while (setq term (pop orterms))
15779 (while (string-match re term)
15780 (setq minus (and (match-end 1)
15781 (equal (match-string 1 term) "-"))
15782 kwd (match-string 2 term)
15783 re-p (equal (string-to-char kwd) ?{)
15784 term (substring term (match-end 0))
15786 `(string-match ,(substring kwd 1 -1) todo)
15787 (list 'equal 'todo kwd))
15788 mm (if minus (list 'not mm) mm))
15789 (push mm todomatcher))
15790 (push (if (> (length todomatcher) 1)
15791 (cons 'and todomatcher)
15794 (setq todomatcher nil))
15795 (setq todomatcher (if (> (length orlist) 1)
15796 (cons 'or orlist) (car orlist))))
15798 ;; Return the string and lisp forms of the matcher
15799 (setq matcher (if todomatcher
15800 (list 'and tagsmatcher todomatcher)
15802 (cons match0 matcher)))
15804 (defun org-match-any-p (re list)
15805 "Does re match any element of list?"
15806 (setq list (mapcar (lambda (x) (string-match re x)) list))
15809 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
15810 (defvar org-tags-overlay (org-make-overlay 1 1))
15811 (org-detach-overlay org-tags-overlay)
15813 (defun org-align-tags-here (to-col)
15814 ;; Assumes that this is a headline
15815 (let ((pos (point)) (col (current-column)) tags)
15816 (beginning-of-line 1)
15817 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
15818 (< pos (match-beginning 2)))
15820 (setq tags (match-string 2))
15821 (goto-char (match-beginning 1))
15823 (delete-region (point) (1+ (match-end 0)))
15826 (max (1+ (current-column))
15830 (- (abs to-col) (length tags))))
15833 (move-to-column (min (current-column) col) t))
15836 (defun org-set-tags (&optional arg just-align)
15837 "Set the tags for the current headline.
15838 With prefix ARG, realign all tags in headings in the current buffer."
15840 (let* ((re (concat "^" outline-regexp))
15841 (current (org-get-tags-string))
15842 (col (current-column))
15843 (org-setting-tags t)
15844 table current-tags inherited-tags ; computed below when needed
15848 (goto-char (point-min))
15849 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
15850 (while (re-search-forward re nil t)
15851 (org-set-tags nil t)
15853 (message "All tags realigned to column %d" org-tags-column))
15855 (setq tags current)
15856 ;; Get a new set of tags from the user
15858 (setq table (or org-tag-alist (org-get-buffer-tags))
15859 org-last-tags-completion-table table
15860 current-tags (org-split-string current ":")
15861 inherited-tags (nreverse
15862 (nthcdr (length current-tags)
15863 (nreverse (org-get-tags-at))))
15865 (if (or (eq t org-use-fast-tag-selection)
15866 (and org-use-fast-tag-selection
15867 (delq nil (mapcar 'cdr table))))
15868 (org-fast-tag-selection
15869 current-tags inherited-tags table
15870 (if org-fast-tag-selection-include-todo org-todo-key-alist))
15871 (let ((org-add-colon-after-tag-completion t))
15873 (org-without-partial-completion
15874 (completing-read "Tags: " 'org-tags-completion-function
15875 nil nil current 'org-tags-history)))))))
15876 (while (string-match "[-+&]+" tags)
15877 ;; No boolean logic, just a list
15878 (setq tags (replace-match ":" t t tags))))
15880 (if (string-match "\\`[\t ]*\\'" tags)
15882 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
15883 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
15885 ;; Insert new tags at the correct column
15886 (beginning-of-line 1)
15888 ((and (equal current "") (equal tags "")))
15889 ((re-search-forward
15890 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
15892 (if (equal tags "")
15894 (goto-char (match-beginning 0))
15895 (setq c0 (current-column) p0 (point)
15896 c1 (max (1+ c0) (if (> org-tags-column 0)
15898 (- (- org-tags-column) (length tags))))
15899 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
15900 (replace-match rpl t t)
15901 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
15903 (t (error "Tags alignment failed")))
15904 (move-to-column col)
15906 (run-hooks 'org-after-tags-change-hook)))))
15908 (defun org-change-tag-in-region (beg end tag off)
15909 "Add or remove TAG for each entry in the region.
15910 This works in the agenda, and also in an org-mode buffer."
15912 (list (region-beginning) (region-end)
15913 (let ((org-last-tags-completion-table
15915 (org-get-buffer-tags)
15916 (org-global-tags-completion-table))))
15918 "Tag: " 'org-tags-completion-function nil nil nil
15919 'org-tags-history))
15921 (message "[s]et or [r]emove? ")
15922 (equal (read-char-exclusive) ?r))))
15923 (if (fboundp 'deactivate-mark) (deactivate-mark))
15924 (let ((agendap (equal major-mode 'org-agenda-mode))
15925 l1 l2 m buf pos newhead (cnt 0))
15927 (setq l2 (1- (org-current-line)))
15929 (setq l1 (org-current-line))
15930 (loop for l from l1 to l2 do
15932 (setq m (get-text-property (point) 'org-hd-marker))
15933 (when (or (and (org-mode-p) (org-on-heading-p))
15935 (setq buf (if agendap (marker-buffer m) (current-buffer))
15936 pos (if agendap m (point)))
15937 (with-current-buffer buf
15941 (setq cnt (1+ cnt))
15942 (org-toggle-tag tag (if off 'off 'on))
15943 (setq newhead (org-get-heading)))))
15944 (and agendap (org-agenda-change-all-lines newhead m))))
15945 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
15947 (defun org-tags-completion-function (string predicate &optional flag)
15948 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
15949 (confirm (lambda (x) (stringp (car x)))))
15950 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
15951 (setq s1 (match-string 1 string)
15952 s2 (match-string 2 string))
15953 (setq s1 "" s2 string))
15957 (setq rtn (try-completion s2 ctable confirm))
15960 (concat s1 s2 (substring rtn (length s2))
15961 (if (and org-add-colon-after-tag-completion
15962 (assoc rtn ctable))
15967 (all-completions s2 ctable confirm)
15971 (assoc s2 ctable)))
15974 (defun org-fast-tag-insert (kwd tags face &optional end)
15975 "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
15976 (insert (format "%-12s" (concat kwd ":"))
15977 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
15980 (defun org-fast-tag-show-exit (flag)
15983 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
15984 (replace-match ""))
15987 (move-to-column (- (window-width) 19) t)
15988 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
15990 (defun org-set-current-tags-overlay (current prefix)
15991 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
15992 (if (featurep 'xemacs)
15993 (org-overlay-display org-tags-overlay (concat prefix s)
15994 'secondary-selection)
15995 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
15996 (org-overlay-display org-tags-overlay (concat prefix s)))))
15998 (defun org-fast-tag-selection (current inherited table &optional todo-table)
15999 "Fast tag selection with single keys.
16000 CURRENT is the current list of tags in the headline, INHERITED is the
16001 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
16002 possibly with grouping information. TODO-TABLE is a similar table with
16003 TODO keywords, should these have keys assigned to them.
16004 If the keys are nil, a-z are automatically assigned.
16005 Returns the new tags string, or nil to not change the current settings."
16006 (let* ((fulltable (append table todo-table))
16007 (maxlen (apply 'max (mapcar
16009 (if (stringp (car x)) (string-width (car x)) 0))
16011 (buf (current-buffer))
16012 (expert (eq org-fast-tag-selection-single-key 'expert))
16014 (fwidth (+ maxlen 3 1 3))
16015 (ncol (/ (- (window-width) 4) fwidth))
16018 tg cnt e c char c1 c2 ntable tbl rtn
16019 ov-start ov-end ov-prefix
16020 (exit-after-next org-fast-tag-selection-single-key)
16021 (done-keywords org-done-keywords)
16024 (beginning-of-line 1)
16026 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
16027 (setq ov-start (match-beginning 1)
16028 ov-end (match-end 1)
16030 (setq ov-start (1- (point-at-eol))
16031 ov-end (1+ ov-start))
16032 (skip-chars-forward "^\n\r")
16035 (buffer-substring (1- (point)) (point))
16036 (if (> (current-column) org-tags-column)
16038 (make-string (- org-tags-column (current-column)) ?\ ))))))
16039 (org-move-overlay org-tags-overlay ov-start ov-end)
16040 (save-window-excursion
16042 (set-buffer (get-buffer-create " *Org tags*"))
16043 (delete-other-windows)
16044 (split-window-vertically)
16045 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
16047 (org-set-local 'org-done-keywords done-keywords)
16048 (org-fast-tag-insert "Inherited" inherited i-face "\n")
16049 (org-fast-tag-insert "Current" current c-face "\n\n")
16050 (org-fast-tag-show-exit exit-after-next)
16051 (org-set-current-tags-overlay current ov-prefix)
16052 (setq tbl fulltable char ?a cnt 0)
16053 (while (setq e (pop tbl))
16055 ((equal e '(:startgroup))
16056 (push '() groups) (setq ingroup t)
16057 (when (not (= cnt 0))
16061 ((equal e '(:endgroup))
16062 (setq ingroup nil cnt 0)
16065 (setq tg (car e) c2 nil)
16068 ;; automatically assign a character.
16069 (setq c1 (string-to-char
16070 (downcase (substring
16071 tg (if (= (string-to-char tg) ?@) 1 0)))))
16072 (if (or (rassoc c1 ntable) (rassoc c1 table))
16073 (while (or (rassoc char ntable) (rassoc char table))
16074 (setq char (1+ char)))
16076 (setq c (or c2 char)))
16077 (if ingroup (push tg (car groups)))
16078 (setq tg (org-add-props tg nil 'face
16080 ((not (assoc tg table))
16081 (org-get-todo-face tg))
16082 ((member tg current) c-face)
16083 ((member tg inherited) i-face)
16085 (if (and (= cnt 0) (not ingroup)) (insert " "))
16086 (insert "[" c "] " tg (make-string
16087 (- fwidth 4 (length tg)) ?\ ))
16088 (push (cons tg c) ntable)
16089 (when (= (setq cnt (1+ cnt)) ncol)
16091 (if ingroup (insert " "))
16093 (setq ntable (nreverse ntable))
16095 (goto-char (point-min))
16096 (if (and (not expert) (fboundp 'fit-window-to-buffer))
16097 (fit-window-to-buffer))
16101 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
16102 (if groups " [!] no groups" " [!]groups")
16103 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
16104 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
16106 ((= c ?\r) (throw 'exit t))
16108 (setq groups (not groups))
16109 (goto-char (point-min))
16110 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
16113 (org-fast-tag-show-exit
16114 (setq exit-after-next (not exit-after-next)))
16116 (delete-other-windows)
16117 (split-window-vertically)
16118 (org-switch-to-buffer-other-window " *Org tags*")
16119 (and (fboundp 'fit-window-to-buffer)
16120 (fit-window-to-buffer))))
16122 (and (= c ?q) (not (rassoc c ntable))))
16123 (org-detach-overlay org-tags-overlay)
16124 (setq quit-flag t))
16127 (if exit-after-next (setq exit-after-next 'now)))
16129 (condition-case nil
16130 (setq tg (completing-read
16133 (with-current-buffer buf
16134 (org-get-buffer-tags)))))
16135 (quit (setq tg "")))
16136 (when (string-match "\\S-" tg)
16137 (add-to-list 'buffer-tags (list tg))
16138 (if (member tg current)
16139 (setq current (delete tg current))
16140 (push tg current)))
16141 (if exit-after-next (setq exit-after-next 'now)))
16142 ((setq e (rassoc c todo-table) tg (car e))
16143 (with-current-buffer buf
16144 (save-excursion (org-todo tg)))
16145 (if exit-after-next (setq exit-after-next 'now)))
16146 ((setq e (rassoc c ntable) tg (car e))
16147 (if (member tg current)
16148 (setq current (delete tg current))
16149 (loop for g in groups do
16152 (setq current (delete x current)))
16155 (if exit-after-next (setq exit-after-next 'now))))
16157 ;; Create a sorted list
16161 (assoc b (cdr (memq (assoc a ntable) ntable))))))
16162 (if (eq exit-after-next 'now) (throw 'exit t))
16163 (goto-char (point-min))
16164 (beginning-of-line 2)
16165 (delete-region (point) (point-at-eol))
16166 (org-fast-tag-insert "Current" current c-face)
16167 (org-set-current-tags-overlay current ov-prefix)
16168 (while (re-search-forward
16169 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
16170 (setq tg (match-string 1))
16171 (add-text-properties
16172 (match-beginning 1) (match-end 1)
16175 ((member tg current) c-face)
16176 ((member tg inherited) i-face)
16177 (t (get-text-property (match-beginning 1) 'face))))))
16178 (goto-char (point-min)))))
16179 (org-detach-overlay org-tags-overlay)
16181 (mapconcat 'identity current ":")
16184 (defun org-get-tags-string ()
16185 "Get the TAGS string in the current headline."
16186 (unless (org-on-heading-p t)
16187 (error "Not on a heading"))
16189 (beginning-of-line 1)
16190 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
16191 (org-match-string-no-properties 1)
16194 (defun org-get-tags ()
16195 "Get the list of tags specified in the current headline."
16196 (org-split-string (org-get-tags-string) ":"))
16198 (defun org-get-buffer-tags ()
16199 "Get a table of all tags used in the buffer, for completion."
16202 (goto-char (point-min))
16203 (while (re-search-forward
16204 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
16205 (when (equal (char-after (point-at-bol 0)) ?*)
16206 (mapc (lambda (x) (add-to-list 'tags x))
16207 (org-split-string (org-match-string-no-properties 1) ":")))))
16208 (mapcar 'list tags)))
16213 ;;; Setting and retrieving properties
16215 (defconst org-special-properties
16216 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
16217 "TIMESTAMP" "TIMESTAMP_IA")
16218 "The special properties valid in Org-mode.
16220 These are properties that are not defined in the property drawer,
16221 but in some other way.")
16223 (defconst org-default-properties
16224 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
16225 "LOCATION" "LOGGING" "COLUMNS")
16226 "Some properties that are used by Org-mode for various purposes.
16227 Being in this list makes sure that they are offered for completion.")
16229 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
16230 "Regular expression matching the first line of a property drawer.")
16232 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
16233 "Regular expression matching the first line of a property drawer.")
16235 (defun org-property-action ()
16236 "Do an action on properties."
16239 (org-at-property-p)
16240 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
16241 (setq c (read-char-exclusive))
16244 (call-interactively 'org-set-property))
16246 (call-interactively 'org-delete-property))
16248 (call-interactively 'org-delete-property-globally))
16250 (call-interactively 'org-compute-property-at-point))
16251 (t (error "No such property action %c" c)))))
16253 (defun org-at-property-p ()
16254 "Is the cursor in a property line?"
16255 ;; FIXME: Does not check if we are actually in the drawer.
16256 ;; FIXME: also returns true on any drawers.....
16257 ;; This is used by C-c C-c for property action.
16259 (beginning-of-line 1)
16260 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
16262 (defmacro org-with-point-at (pom &rest body)
16263 "Move to buffer and point of point-or-marker POM for the duration of BODY."
16264 (declare (indent 1) (debug t))
16266 (if (markerp pom) (set-buffer (marker-buffer pom)))
16268 (goto-char (or pom (point)))
16271 (defun org-get-property-block (&optional beg end force)
16272 "Return the (beg . end) range of the body of the property drawer.
16273 BEG and END can be beginning and end of subtree, if not given
16274 they will be found.
16275 If the drawer does not exist and FORCE is non-nil, create the drawer."
16278 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
16279 (end (or end (progn (outline-next-heading) (point)))))
16281 (if (re-search-forward org-property-start-re end t)
16282 (setq beg (1+ (match-end 0)))
16285 (org-insert-property-drawer)
16286 (setq end (progn (outline-next-heading) (point))))
16289 (if (re-search-forward org-property-start-re end t)
16290 (setq beg (1+ (match-end 0)))))
16291 (if (re-search-forward org-property-end-re end t)
16292 (setq end (match-beginning 0))
16293 (or force (throw 'exit nil))
16296 (org-indent-line-function)
16297 (insert ":END:\n"))
16300 (defun org-entry-properties (&optional pom which)
16301 "Get all properties of the entry at point-or-marker POM.
16302 This includes the TODO keyword, the tags, time strings for deadline,
16303 scheduled, and clocking, and any additional properties defined in the
16304 entry. The return value is an alist, keys may occur multiple times
16305 if the property key was used several times.
16306 POM may also be nil, in which case the current entry is used.
16307 If WHICH is nil or `all', get all properties. If WHICH is
16308 `special' or `standard', only get that subclass."
16309 (setq which (or which 'all))
16310 (org-with-point-at pom
16311 (let ((clockstr (substring org-clock-string 0 -1))
16312 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
16313 beg end range props sum-props key value string clocksum)
16315 (when (condition-case nil (org-back-to-heading t) (error nil))
16317 (setq sum-props (get-text-property (point) 'org-summaries))
16318 (setq clocksum (get-text-property (point) :org-clock-minutes))
16319 (outline-next-heading)
16321 (when (memq which '(all special))
16322 ;; Get the special properties, like TODO and tags
16324 (when (and (looking-at org-todo-line-regexp) (match-end 2))
16325 (push (cons "TODO" (org-match-string-no-properties 2)) props))
16326 (when (looking-at org-priority-regexp)
16327 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
16328 (when (and (setq value (org-get-tags-string))
16329 (string-match "\\S-" value))
16330 (push (cons "TAGS" value) props))
16331 (when (setq value (org-get-tags-at))
16332 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
16334 (while (re-search-forward org-maybe-keyword-time-regexp end t)
16335 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
16336 string (if (equal key clockstr)
16340 (match-beginning 3) (goto-char (point-at-eol)))))
16341 (substring (org-match-string-no-properties 3) 1 -1)))
16343 (if (= (char-after (match-beginning 3)) ?\[)
16344 (setq key "TIMESTAMP_IA")
16345 (setq key "TIMESTAMP")))
16346 (when (or (equal key clockstr) (not (assoc key props)))
16347 (push (cons key string) props)))
16351 (when (memq which '(all standard))
16352 ;; Get the standard properties, like :PORP: ...
16353 (setq range (org-get-property-block beg end))
16355 (goto-char (car range))
16356 (while (re-search-forward
16357 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
16359 (setq key (org-match-string-no-properties 1)
16360 value (org-trim (or (org-match-string-no-properties 2) "")))
16361 (unless (member key excluded)
16362 (push (cons key (or value "")) props)))))
16364 (push (cons "CLOCKSUM"
16365 (org-column-number-to-string (/ (float clocksum) 60.)
16368 (append sum-props (nreverse props)))))))
16370 (defun org-entry-get (pom property &optional inherit)
16371 "Get value of PROPERTY for entry at point-or-marker POM.
16372 If INHERIT is non-nil and the entry does not have the property,
16373 then also check higher levels of the hierarchy.
16374 If the property is present but empty, the return value is the empty string.
16375 If the property is not present at all, nil is returned."
16376 (org-with-point-at pom
16378 (org-entry-get-with-inheritance property)
16379 (if (member property org-special-properties)
16380 ;; We need a special property. Use brute force, get all properties.
16381 (cdr (assoc property (org-entry-properties nil 'special)))
16382 (let ((range (org-get-property-block)))
16384 (goto-char (car range))
16386 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
16388 ;; Found the property, return it.
16390 (org-match-string-no-properties 1)
16393 (defun org-entry-delete (pom property)
16394 "Delete the property PROPERTY from entry at point-or-marker POM."
16395 (org-with-point-at pom
16396 (if (member property org-special-properties)
16397 nil ; cannot delete these properties.
16398 (let ((range (org-get-property-block)))
16400 (goto-char (car range))
16402 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
16405 (delete-region (match-beginning 0) (1+ (point-at-eol)))
16409 ;; Multi-values properties are properties that contain multiple values
16410 ;; These values are assumed to be single words, separated by whitespace.
16411 (defun org-entry-add-to-multivalued-property (pom property value)
16412 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
16413 (let* ((old (org-entry-get pom property))
16414 (values (and old (org-split-string old "[ \t]"))))
16415 (unless (member value values)
16416 (setq values (cons value values))
16417 (org-entry-put pom property
16418 (mapconcat 'identity values " ")))))
16420 (defun org-entry-remove-from-multivalued-property (pom property value)
16421 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
16422 (let* ((old (org-entry-get pom property))
16423 (values (and old (org-split-string old "[ \t]"))))
16424 (when (member value values)
16425 (setq values (delete value values))
16426 (org-entry-put pom property
16427 (mapconcat 'identity values " ")))))
16429 (defun org-entry-member-in-multivalued-property (pom property value)
16430 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
16431 (let* ((old (org-entry-get pom property))
16432 (values (and old (org-split-string old "[ \t]"))))
16433 (member value values)))
16435 (defvar org-entry-property-inherited-from (make-marker))
16437 (defun org-entry-get-with-inheritance (property)
16438 "Get entry property, and search higher levels if not present."
16445 (when (setq tmp (org-entry-get nil property))
16446 (org-back-to-heading t)
16447 (move-marker org-entry-property-inherited-from (point))
16449 (or (org-up-heading-safe) (throw 'ex nil)))))
16450 (or tmp (cdr (assoc property org-local-properties))
16451 (cdr (assoc property org-global-properties))))))
16453 (defun org-entry-put (pom property value)
16454 "Set PROPERTY to VALUE for entry at point-or-marker POM."
16455 (org-with-point-at pom
16456 (org-back-to-heading t)
16457 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
16460 ((equal property "TODO")
16461 (when (and (stringp value) (string-match "\\S-" value)
16462 (not (member value org-todo-keywords-1)))
16463 (error "\"%s\" is not a valid TODO state" value))
16464 (if (or (not value)
16465 (not (string-match "\\S-" value)))
16466 (setq value 'none))
16468 (org-set-tags nil 'align))
16469 ((equal property "PRIORITY")
16470 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
16471 (string-to-char value) ?\ ))
16472 (org-set-tags nil 'align))
16473 ((equal property "SCHEDULED")
16474 (if (re-search-forward org-scheduled-time-regexp end t)
16476 ((eq value 'earlier) (org-timestamp-change -1 'day))
16477 ((eq value 'later) (org-timestamp-change 1 'day))
16478 (t (call-interactively 'org-schedule)))
16479 (call-interactively 'org-schedule)))
16480 ((equal property "DEADLINE")
16481 (if (re-search-forward org-deadline-time-regexp end t)
16483 ((eq value 'earlier) (org-timestamp-change -1 'day))
16484 ((eq value 'later) (org-timestamp-change 1 'day))
16485 (t (call-interactively 'org-deadline)))
16486 (call-interactively 'org-deadline)))
16487 ((member property org-special-properties)
16488 (error "The %s property can not yet be set with `org-entry-put'"
16490 (t ; a non-special property
16491 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
16492 (setq range (org-get-property-block beg end 'force))
16493 (goto-char (car range))
16494 (if (re-search-forward
16495 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
16497 (delete-region (match-beginning 1) (match-end 1))
16498 (goto-char (match-beginning 1)))
16499 (goto-char (cdr range))
16502 (org-indent-line-function)
16503 (insert ":" property ":"))
16504 (and value (insert " " value))
16505 (org-indent-line-function)))))))
16507 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
16508 "Get all property keys in the current buffer.
16509 With INCLUDE-SPECIALS, also list the special properties that relect things
16510 like tags and TODO state.
16511 With INCLUDE-DEFAULTS, also include properties that has special meaning
16512 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
16513 With INCLUDE-COLUMNS, also include property names given in COLUMN
16514 formats in the current buffer."
16515 (let (rtn range cfmt cols s p)
16519 (goto-char (point-min))
16520 (while (re-search-forward org-property-start-re nil t)
16521 (setq range (org-get-property-block))
16522 (goto-char (car range))
16523 (while (re-search-forward
16524 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
16526 (add-to-list 'rtn (org-match-string-no-properties 1)))
16527 (outline-next-heading))))
16529 (when include-specials
16530 (setq rtn (append org-special-properties rtn)))
16532 (when include-defaults
16533 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
16535 (when include-columns
16539 (goto-char (point-min))
16540 (while (re-search-forward
16541 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
16543 (setq cfmt (match-string 2) s 0)
16544 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
16546 (setq s (match-end 0)
16547 p (match-string 1 cfmt))
16548 (unless (or (equal p "ITEM")
16549 (member p org-special-properties))
16550 (add-to-list 'rtn (match-string 1 cfmt))))))))
16552 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
16554 (defun org-property-values (key)
16555 "Return a list of all values of property KEY."
16559 (goto-char (point-min))
16560 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
16562 (while (re-search-forward re nil t)
16563 (add-to-list 'values (org-trim (match-string 1))))
16564 (delete "" values)))))
16566 (defun org-insert-property-drawer ()
16567 "Insert a property drawer into the current entry."
16569 (org-back-to-heading t)
16570 (looking-at outline-regexp)
16571 (let ((indent (- (match-end 0)(match-beginning 0)))
16573 (re (concat "^[ \t]*" org-keyword-time-regexp))
16575 (outline-next-heading)
16578 (while (re-search-forward re end t))
16579 (setq hiddenp (org-invisible-p))
16581 (and (equal (char-after) ?\n) (forward-char 1))
16582 (org-skip-over-state-notes)
16583 (skip-chars-backward " \t\n\r")
16584 (if (eq (char-before) ?*) (forward-char 1))
16585 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
16586 (beginning-of-line 0)
16587 (indent-to-column indent)
16588 (beginning-of-line 2)
16589 (indent-to-column indent)
16590 (beginning-of-line 0)
16593 (org-back-to-heading t)
16595 (org-flag-drawer t))))
16597 (defun org-set-property (property value)
16598 "In the current entry, set PROPERTY to VALUE.
16599 When called interactively, this will prompt for a property name, offering
16600 completion on existing and default properties. And then it will prompt
16601 for a value, offering competion either on allowed values (via an inherited
16602 xxx_ALL property) or on existing values in other instances of this property
16603 in the current file."
16605 (let* ((prop (completing-read
16606 "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
16607 (cur (org-entry-get nil prop))
16608 (allowed (org-property-get-allowed-values nil prop 'table))
16609 (existing (mapcar 'list (org-property-values prop)))
16611 (completing-read "Value: " allowed nil 'req-match)
16613 (concat "Value" (if (and cur (string-match "\\S-" cur))
16614 (concat "[" cur "]") "")
16616 existing nil nil "" nil cur))))
16617 (list prop (if (equal val "") cur val))))
16618 (unless (equal (org-entry-get nil property) value)
16619 (org-entry-put nil property value)))
16621 (defun org-delete-property (property)
16622 "In the current entry, delete PROPERTY."
16624 (let* ((prop (completing-read
16625 "Property: " (org-entry-properties nil 'standard))))
16627 (message "Property %s %s" property
16628 (if (org-entry-delete nil property)
16630 "was not present in the entry")))
16632 (defun org-delete-property-globally (property)
16633 "Remove PROPERTY globally, from all entries."
16635 (let* ((prop (completing-read
16636 "Globally remove property: "
16637 (mapcar 'list (org-buffer-property-keys)))))
16642 (goto-char (point-min))
16644 (while (re-search-forward
16645 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
16647 (setq cnt (1+ cnt))
16648 (replace-match ""))
16649 (message "Property \"%s\" removed from %d entries" property cnt)))))
16651 (defvar org-columns-current-fmt-compiled) ; defined below
16653 (defun org-compute-property-at-point ()
16654 "Compute the property at point.
16655 This looks for an enclosing column format, extracts the operator and
16656 then applies it to the proerty in the column format's scope."
16658 (unless (org-at-property-p)
16659 (error "Not at a property"))
16660 (let ((prop (org-match-string-no-properties 2)))
16661 (org-columns-get-format-and-top-level)
16662 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
16663 (error "No operator defined for property %s" prop))
16664 (org-columns-compute prop)))
16666 (defun org-property-get-allowed-values (pom property &optional table)
16667 "Get allowed values for the property PROPERTY.
16668 When TABLE is non-nil, return an alist that can directly be used for
16672 ((equal property "TODO")
16673 (setq vals (org-with-point-at pom
16674 (append org-todo-keywords-1 '("")))))
16675 ((equal property "PRIORITY")
16676 (let ((n org-lowest-priority))
16677 (while (>= n org-highest-priority)
16678 (push (char-to-string n) vals)
16680 ((member property org-special-properties))
16682 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
16684 (when (and vals (string-match "\\S-" vals))
16685 (setq vals (car (read-from-string (concat "(" vals ")"))))
16686 (setq vals (mapcar (lambda (x)
16687 (cond ((stringp x) x)
16688 ((numberp x) (number-to-string x))
16689 ((symbolp x) (symbol-name x))
16692 (if table (mapcar 'list vals) vals)))
16694 (defun org-property-previous-allowed-value (&optional previous)
16695 "Switch to the next allowed value for this property."
16697 (org-property-next-allowed-value t))
16699 (defun org-property-next-allowed-value (&optional previous)
16700 "Switch to the next allowed value for this property."
16702 (unless (org-at-property-p)
16703 (error "Not at a property"))
16704 (let* ((key (match-string 2))
16705 (value (match-string 3))
16706 (allowed (or (org-property-get-allowed-values (point) key)
16707 (and (member value '("[ ]" "[-]" "[X]"))
16711 (error "Allowed values for this property have not been defined"))
16712 (if previous (setq allowed (reverse allowed)))
16713 (if (member value allowed)
16714 (setq nval (car (cdr (member value allowed)))))
16715 (setq nval (or nval (car allowed)))
16716 (if (equal nval value)
16717 (error "Only one allowed value for this property"))
16718 (org-at-property-p)
16719 (replace-match (concat " :" key ": " nval) t t)
16720 (org-indent-line-function)
16721 (beginning-of-line 1)
16722 (skip-chars-forward " \t")))
16724 (defun org-find-entry-with-id (ident)
16725 "Locate the entry that contains the ID property with exact value IDENT.
16726 IDENT can be a string, a symbol or a number, this function will search for
16727 the string representation of it.
16728 Return the position where this entry starts, or nil if there is no such entry."
16730 ((stringp ident) ident)
16731 ((symbol-name ident) (symbol-name ident))
16732 ((numberp ident) (number-to-string ident))
16733 (t (error "IDENT %s must be a string, symbol or number" ident))))
16734 (case-fold-search nil))
16738 (goto-char (point-min))
16739 (when (re-search-forward
16740 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
16742 (org-back-to-heading)
16747 (defvar org-columns-overlays nil
16748 "Holds the list of current column overlays.")
16750 (defvar org-columns-current-fmt nil
16751 "Local variable, holds the currently active column format.")
16752 (defvar org-columns-current-fmt-compiled nil
16753 "Local variable, holds the currently active column format.
16754 This is the compiled version of the format.")
16755 (defvar org-columns-current-widths nil
16756 "Loval variable, holds the currently widths of fields.")
16757 (defvar org-columns-current-maxwidths nil
16758 "Loval variable, holds the currently active maximum column widths.")
16759 (defvar org-columns-begin-marker (make-marker)
16760 "Points to the position where last a column creation command was called.")
16761 (defvar org-columns-top-level-marker (make-marker)
16762 "Points to the position where current columns region starts.")
16764 (defvar org-columns-map (make-sparse-keymap)
16765 "The keymap valid in column display.")
16767 (defun org-columns-content ()
16768 "Switch to contents view while in columns view."
16773 (org-defkey org-columns-map "c" 'org-columns-content)
16774 (org-defkey org-columns-map "o" 'org-overview)
16775 (org-defkey org-columns-map "e" 'org-columns-edit-value)
16776 (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
16777 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
16778 (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
16779 (org-defkey org-columns-map "v" 'org-columns-show-value)
16780 (org-defkey org-columns-map "q" 'org-columns-quit)
16781 (org-defkey org-columns-map "r" 'org-columns-redo)
16782 (org-defkey org-columns-map "g" 'org-columns-redo)
16783 (org-defkey org-columns-map [left] 'backward-char)
16784 (org-defkey org-columns-map "\M-b" 'backward-char)
16785 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
16786 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
16787 (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
16788 (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
16789 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
16790 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
16791 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
16792 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
16793 (org-defkey org-columns-map "<" 'org-columns-narrow)
16794 (org-defkey org-columns-map ">" 'org-columns-widen)
16795 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
16796 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
16797 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
16798 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
16800 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
16802 ["Edit property" org-columns-edit-value t]
16803 ["Next allowed value" org-columns-next-allowed-value t]
16804 ["Previous allowed value" org-columns-previous-allowed-value t]
16805 ["Show full value" org-columns-show-value t]
16806 ["Edit allowed values" org-columns-edit-allowed t]
16808 ["Edit column attributes" org-columns-edit-attributes t]
16809 ["Increase column width" org-columns-widen t]
16810 ["Decrease column width" org-columns-narrow t]
16812 ["Move column right" org-columns-move-right t]
16813 ["Move column left" org-columns-move-left t]
16814 ["Add column" org-columns-new t]
16815 ["Delete column" org-columns-delete t]
16817 ["CONTENTS" org-columns-content t]
16818 ["OVERVIEW" org-overview t]
16819 ["Refresh columns display" org-columns-redo t]
16821 ["Open link" org-columns-open-link t]
16823 ["Quit" org-columns-quit t]))
16825 (defun org-columns-new-overlay (beg end &optional string face)
16826 "Create a new column overlay and add it to the list."
16827 (let ((ov (org-make-overlay beg end)))
16828 (org-overlay-put ov 'face (or face 'secondary-selection))
16829 (org-overlay-display ov string face)
16830 (push ov org-columns-overlays)
16833 (defun org-columns-display-here (&optional props)
16834 "Overlay the current line with column display."
16836 (let* ((fmt org-columns-current-fmt-compiled)
16837 (beg (point-at-bol))
16838 (level-face (save-excursion
16839 (beginning-of-line 1)
16840 (and (looking-at "\\(\\**\\)\\(\\* \\)")
16841 (org-get-level-face 2))))
16842 (color (list :foreground
16843 (face-attribute (or level-face 'default) :foreground)))
16844 props pom property ass width f string ov column val modval)
16845 ;; Check if the entry is in another buffer.
16847 (if (eq major-mode 'org-agenda-mode)
16848 (setq pom (or (get-text-property (point) 'org-hd-marker)
16849 (get-text-property (point) 'org-marker))
16850 props (if pom (org-entry-properties pom) nil))
16851 (setq props (org-entry-properties nil))))
16853 (while (setq column (pop fmt))
16854 (setq property (car column)
16855 ass (if (equal property "ITEM")
16860 (buffer-substring-no-properties
16861 (point-at-bol) (point-at-eol))))))
16862 (assoc property props))
16863 width (or (cdr (assoc property org-columns-current-maxwidths))
16866 f (format "%%-%d.%ds | " width width)
16867 val (or (cdr ass) "")
16868 modval (if (equal property "ITEM")
16869 (org-columns-cleanup-item val org-columns-current-fmt-compiled))
16870 string (format f (or modval val)))
16871 ;; Create the overlay
16873 (setq ov (org-columns-new-overlay
16874 beg (setq beg (1+ beg)) string
16875 (list color 'org-column)))
16876 ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
16877 (org-overlay-put ov 'keymap org-columns-map)
16878 (org-overlay-put ov 'org-columns-key property)
16879 (org-overlay-put ov 'org-columns-value (cdr ass))
16880 (org-overlay-put ov 'org-columns-value-modified modval)
16881 (org-overlay-put ov 'org-columns-pom pom)
16882 (org-overlay-put ov 'org-columns-format f))
16883 (if (or (not (char-after beg))
16884 (equal (char-after beg) ?\n))
16885 (let ((inhibit-read-only t))
16888 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
16889 ;; Make the rest of the line disappear.
16891 (setq ov (org-columns-new-overlay beg (point-at-eol)))
16892 (org-overlay-put ov 'invisible t)
16893 (org-overlay-put ov 'keymap org-columns-map)
16894 (org-overlay-put ov 'intangible t)
16895 (push ov org-columns-overlays)
16896 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
16897 (org-overlay-put ov 'keymap org-columns-map)
16898 (push ov org-columns-overlays)
16899 (let ((inhibit-read-only t))
16900 (put-text-property (max (point-min) (1- (point-at-bol)))
16901 (min (point-max) (1+ (point-at-eol)))
16902 'read-only "Type `e' to edit property")))))
16904 (defvar org-previous-header-line-format nil
16905 "The header line format before column view was turned on.")
16906 (defvar org-columns-inhibit-recalculation nil
16907 "Inhibit recomputing of columns on column view startup.")
16910 (defvar header-line-format)
16911 (defun org-columns-display-here-title ()
16912 "Overlay the newline before the current line with the table title."
16914 (let ((fmt org-columns-current-fmt-compiled)
16916 property width f column str widths)
16917 (while (setq column (pop fmt))
16918 (setq property (car column)
16919 str (or (nth 1 column) property)
16920 width (or (cdr (assoc property org-columns-current-maxwidths))
16923 widths (push width widths)
16924 f (format "%%-%d.%ds | " width width)
16925 string (format f str)
16926 title (concat title string)))
16927 (setq title (concat
16928 (org-add-props " " nil 'display '(space :align-to 0))
16929 (org-add-props title nil 'face '(:weight bold :underline t))))
16930 (org-set-local 'org-previous-header-line-format header-line-format)
16931 (org-set-local 'org-columns-current-widths (nreverse widths))
16932 (setq header-line-format title)))
16934 (defun org-columns-remove-overlays ()
16935 "Remove all currently active column overlays."
16937 (when (marker-buffer org-columns-begin-marker)
16938 (with-current-buffer (marker-buffer org-columns-begin-marker)
16939 (when (local-variable-p 'org-previous-header-line-format)
16940 (setq header-line-format org-previous-header-line-format)
16941 (kill-local-variable 'org-previous-header-line-format))
16942 (move-marker org-columns-begin-marker nil)
16943 (move-marker org-columns-top-level-marker nil)
16945 (mapc 'org-delete-overlay org-columns-overlays)
16946 (setq org-columns-overlays nil)
16947 (let ((inhibit-read-only t))
16948 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
16950 (defun org-columns-cleanup-item (item fmt)
16951 "Remove from ITEM what is a column in the format FMT."
16952 (if (not org-complex-heading-regexp)
16954 (when (string-match org-complex-heading-regexp item)
16956 (org-add-props (concat (match-string 1 item) " ") nil
16957 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
16958 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
16959 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
16960 " " (match-string 4 item)
16961 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
16963 (defun org-columns-show-value ()
16964 "Show the full value of the property."
16966 (let ((value (get-char-property (point) 'org-columns-value)))
16967 (message "Value is: %s" (or value ""))))
16969 (defun org-columns-quit ()
16970 "Remove the column overlays and in this way exit column editing."
16973 (org-columns-remove-overlays)
16974 (let ((inhibit-read-only t))
16975 (remove-text-properties (point-min) (point-max) '(read-only t))))
16976 (when (eq major-mode 'org-agenda-mode)
16978 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
16980 (defun org-columns-check-computed ()
16981 "Check if this column value is computed.
16982 If yes, throw an error indicating that changing it does not make sense."
16983 (let ((val (get-char-property (point) 'org-columns-value)))
16984 (when (and (stringp val)
16985 (get-char-property 0 'org-computed val))
16986 (error "This value is computed from the entry's children"))))
16988 (defun org-columns-todo (&optional arg)
16989 "Change the TODO state during column view."
16991 (org-columns-edit-value "TODO"))
16993 (defun org-columns-set-tags-or-toggle (&optional arg)
16994 "Toggle checkbox at point, or set tags for current headline."
16996 (if (string-match "\\`\\[[ xX-]\\]\\'"
16997 (get-char-property (point) 'org-columns-value))
16998 (org-columns-next-allowed-value)
16999 (org-columns-edit-value "TAGS")))
17001 (defun org-columns-edit-value (&optional key)
17002 "Edit the value of the property at point in column view.
17003 Where possible, use the standard interface for changing this line."
17005 (org-columns-check-computed)
17006 (let* ((external-key key)
17007 (col (current-column))
17008 (key (or key (get-char-property (point) 'org-columns-key)))
17009 (value (get-char-property (point) 'org-columns-value))
17010 (bol (point-at-bol)) (eol (point-at-eol))
17011 (pom (or (get-text-property bol 'org-hd-marker)
17012 (point))) ; keep despite of compiler waring
17014 (delq nil (mapcar (lambda (x)
17015 (and (eq (overlay-buffer x) (current-buffer))
17016 (>= (overlay-start x) bol)
17017 (<= (overlay-start x) eol)
17019 org-columns-overlays)))
17022 ((equal key "CLOCKSUM")
17023 (error "This special column cannot be edited"))
17024 ((equal key "ITEM")
17025 (setq eval '(org-with-point-at pom
17026 (org-edit-headline))))
17027 ((equal key "TODO")
17028 (setq eval '(org-with-point-at pom
17029 (let ((current-prefix-arg
17030 (if external-key current-prefix-arg '(4))))
17031 (call-interactively 'org-todo)))))
17032 ((equal key "PRIORITY")
17033 (setq eval '(org-with-point-at pom
17034 (call-interactively 'org-priority))))
17035 ((equal key "TAGS")
17036 (setq eval '(org-with-point-at pom
17037 (let ((org-fast-tag-selection-single-key
17038 (if (eq org-fast-tag-selection-single-key 'expert)
17039 t org-fast-tag-selection-single-key)))
17040 (call-interactively 'org-set-tags)))))
17041 ((equal key "DEADLINE")
17042 (setq eval '(org-with-point-at pom
17043 (call-interactively 'org-deadline))))
17044 ((equal key "SCHEDULED")
17045 (setq eval '(org-with-point-at pom
17046 (call-interactively 'org-schedule))))
17048 (setq allowed (org-property-get-allowed-values pom key 'table))
17050 (setq nval (completing-read "Value: " allowed nil t))
17051 (setq nval (read-string "Edit: " value)))
17052 (setq nval (org-trim nval))
17053 (when (not (equal nval value))
17054 (setq eval '(org-entry-put pom key nval)))))
17056 (let ((inhibit-read-only t))
17057 (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
17060 (setq org-columns-overlays
17061 (org-delete-all line-overlays org-columns-overlays))
17062 (mapc 'org-delete-overlay line-overlays)
17063 (org-columns-eval eval))
17064 (org-columns-display-here))))
17065 (move-to-column col)
17066 (if (and (org-mode-p)
17067 (nth 3 (assoc key org-columns-current-fmt-compiled)))
17068 (org-columns-update key))))
17070 (defun org-edit-headline () ; FIXME: this is not columns specific
17071 "Edit the current headline, the part without TODO keyword, TAGS."
17072 (org-back-to-heading)
17073 (when (looking-at org-todo-line-regexp)
17074 (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
17075 (txt (match-string 3))
17078 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
17079 (setq post (match-string 0 txt)
17080 txt (substring txt 0 (match-beginning 0))))
17081 (setq txt2 (read-string "Edit: " txt))
17082 (when (not (equal txt txt2))
17083 (beginning-of-line 1)
17084 (insert pre txt2 post)
17085 (delete-region (point) (point-at-eol))
17086 (org-set-tags nil t)))))
17088 (defun org-columns-edit-allowed ()
17089 "Edit the list of allowed values for the current property."
17091 (let* ((key (get-char-property (point) 'org-columns-key))
17092 (key1 (concat key "_ALL"))
17093 (allowed (org-entry-get (point) key1 t))
17095 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
17096 (setq nval (read-string "Allowed: " allowed))
17098 (cond ((marker-position org-entry-property-inherited-from)
17099 org-entry-property-inherited-from)
17100 ((marker-position org-columns-top-level-marker)
17101 org-columns-top-level-marker))
17104 (defmacro org-no-warnings (&rest body)
17105 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
17107 (defun org-columns-eval (form)
17110 (beginning-of-line 1)
17111 ;; `next-line' is needed here, because it skips invisible line.
17112 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
17113 (setq hidep (org-on-heading-p 1)))
17115 (and hidep (hide-entry))))
17117 (defun org-columns-previous-allowed-value ()
17118 "Switch to the previous allowed value for this column."
17120 (org-columns-next-allowed-value t))
17122 (defun org-columns-next-allowed-value (&optional previous)
17123 "Switch to the next allowed value for this column."
17125 (org-columns-check-computed)
17126 (let* ((col (current-column))
17127 (key (get-char-property (point) 'org-columns-key))
17128 (value (get-char-property (point) 'org-columns-value))
17129 (bol (point-at-bol)) (eol (point-at-eol))
17130 (pom (or (get-text-property bol 'org-hd-marker)
17131 (point))) ; keep despite of compiler waring
17133 (delq nil (mapcar (lambda (x)
17134 (and (eq (overlay-buffer x) (current-buffer))
17135 (>= (overlay-start x) bol)
17136 (<= (overlay-start x) eol)
17138 org-columns-overlays)))
17139 (allowed (or (org-property-get-allowed-values pom key)
17141 (nth 4 (assoc key org-columns-current-fmt-compiled))
17142 '(checkbox checkbox-n-of-m checkbox-percent))
17145 (when (equal key "ITEM")
17146 (error "Cannot edit item headline from here"))
17147 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
17148 (error "Allowed values for this property have not been defined"))
17149 (if (member key '("SCHEDULED" "DEADLINE"))
17150 (setq nval (if previous 'earlier 'later))
17151 (if previous (setq allowed (reverse allowed)))
17152 (if (member value allowed)
17153 (setq nval (car (cdr (member value allowed)))))
17154 (setq nval (or nval (car allowed)))
17155 (if (equal nval value)
17156 (error "Only one allowed value for this property")))
17157 (let ((inhibit-read-only t))
17158 (remove-text-properties (1- bol) eol '(read-only t))
17161 (setq org-columns-overlays
17162 (org-delete-all line-overlays org-columns-overlays))
17163 (mapc 'org-delete-overlay line-overlays)
17164 (org-columns-eval '(org-entry-put pom key nval)))
17165 (org-columns-display-here)))
17166 (move-to-column col)
17167 (if (and (org-mode-p)
17168 (nth 3 (assoc key org-columns-current-fmt-compiled)))
17169 (org-columns-update key))))
17171 (defun org-verify-version (task)
17173 ((eq task 'columns)
17174 (if (or (featurep 'xemacs)
17175 (< emacs-major-version 22))
17176 (error "Emacs 22 is required for the columns feature")))))
17178 (defun org-columns-open-link (&optional arg)
17180 (let ((value (get-char-property (point) 'org-columns-value)))
17181 (org-open-link-from-string value arg)))
17183 (defun org-open-link-from-string (s &optional arg)
17184 "Open a link in the string S, as if it was in Org-mode."
17187 (let ((org-inhibit-startup t))
17190 (goto-char (point-min))
17191 (org-open-at-point arg))))
17193 (defun org-columns-get-format-and-top-level ()
17195 (when (condition-case nil (org-back-to-heading) (error nil))
17196 (move-marker org-entry-property-inherited-from nil)
17197 (setq fmt (org-entry-get nil "COLUMNS" t)))
17198 (setq fmt (or fmt org-columns-default-format))
17199 (org-set-local 'org-columns-current-fmt fmt)
17200 (org-columns-compile-format fmt)
17201 (if (marker-position org-entry-property-inherited-from)
17202 (move-marker org-columns-top-level-marker
17203 org-entry-property-inherited-from)
17204 (move-marker org-columns-top-level-marker (point)))
17207 (defun org-columns ()
17208 "Turn on column view on an org-mode file."
17210 (org-verify-version 'columns)
17211 (org-columns-remove-overlays)
17212 (move-marker org-columns-begin-marker (point))
17213 (let (beg end fmt cache maxwidths)
17214 (setq fmt (org-columns-get-format-and-top-level))
17216 (goto-char org-columns-top-level-marker)
17218 (unless org-columns-inhibit-recalculation
17219 (org-columns-compute-all))
17220 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
17222 ;; Get and cache the properties
17224 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
17227 (narrow-to-region beg end)
17229 (while (re-search-forward (concat "^" outline-regexp) end t)
17230 (push (cons (org-current-line) (org-entry-properties)) cache))
17232 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
17233 (org-set-local 'org-columns-current-maxwidths maxwidths)
17234 (org-columns-display-here-title)
17236 (goto-line (car x))
17237 (org-columns-display-here (cdr x)))
17240 (defun org-columns-new (&optional prop title width op fmt &rest rest)
17241 "Insert a new column, to the leeft o the current column."
17243 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
17245 (setq prop (completing-read
17246 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
17248 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
17249 (setq width (read-string "Column width: " (if width (number-to-string width))))
17250 (if (string-match "\\S-" width)
17251 (setq width (string-to-number width))
17253 (setq fmt (completing-read "Summary [none]: "
17254 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
17256 (if (string-match "\\S-" fmt)
17257 (setq fmt (intern fmt))
17259 (if (eq fmt 'none) (setq fmt nil))
17262 (setcar editp prop)
17263 (setcdr editp (list title width nil fmt)))
17264 (setq cell (nthcdr (1- (current-column))
17265 org-columns-current-fmt-compiled))
17266 (setcdr cell (cons (list prop title width nil fmt)
17268 (org-columns-store-format)
17269 (org-columns-redo)))
17271 (defun org-columns-delete ()
17272 "Delete the column at point from columns view."
17274 (let* ((n (current-column))
17275 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
17277 (format "Are you sure you want to remove column \"%s\"? " title))
17278 (setq org-columns-current-fmt-compiled
17279 (delq (nth n org-columns-current-fmt-compiled)
17280 org-columns-current-fmt-compiled))
17281 (org-columns-store-format)
17283 (if (>= (current-column) (length org-columns-current-fmt-compiled))
17284 (backward-char 1)))))
17286 (defun org-columns-edit-attributes ()
17287 "Edit the attributes of the current column."
17289 (let* ((n (current-column))
17290 (info (nth n org-columns-current-fmt-compiled)))
17291 (apply 'org-columns-new info)))
17293 (defun org-columns-widen (arg)
17294 "Make the column wider by ARG characters."
17296 (let* ((n (current-column))
17297 (entry (nth n org-columns-current-fmt-compiled))
17298 (width (or (nth 2 entry)
17299 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
17300 (setq width (max 1 (+ width arg)))
17301 (setcar (nthcdr 2 entry) width)
17302 (org-columns-store-format)
17303 (org-columns-redo)))
17305 (defun org-columns-narrow (arg)
17306 "Make the column nrrower by ARG characters."
17308 (org-columns-widen (- arg)))
17310 (defun org-columns-move-right ()
17311 "Swap this column with the one to the right."
17313 (let* ((n (current-column))
17314 (cell (nthcdr n org-columns-current-fmt-compiled))
17316 (when (>= n (1- (length org-columns-current-fmt-compiled)))
17317 (error "Cannot shift this column further to the right"))
17318 (setq e (car cell))
17319 (setcar cell (car (cdr cell)))
17320 (setcdr cell (cons e (cdr (cdr cell))))
17321 (org-columns-store-format)
17325 (defun org-columns-move-left ()
17326 "Swap this column with the one to the left."
17328 (let* ((n (current-column)))
17330 (error "Cannot shift this column further to the left"))
17332 (org-columns-move-right)
17333 (backward-char 1)))
17335 (defun org-columns-store-format ()
17336 "Store the text version of the current columns format in appropriate place.
17337 This is either in the COLUMNS property of the node starting the current column
17338 display, or in the #+COLUMNS line of the current buffer."
17340 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
17341 (org-set-local 'org-columns-current-fmt fmt)
17342 (if (marker-position org-columns-top-level-marker)
17344 (goto-char org-columns-top-level-marker)
17345 (if (and (org-at-heading-p)
17346 (org-entry-get nil "COLUMNS"))
17347 (org-entry-put nil "COLUMNS" fmt)
17348 (goto-char (point-min))
17349 ;; Overwrite all #+COLUMNS lines....
17350 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
17351 (setq cnt (1+ cnt))
17352 (replace-match (concat "#+COLUMNS: " fmt) t t))
17354 (goto-char (point-min))
17355 (or (org-on-heading-p t) (outline-next-heading))
17356 (let ((inhibit-read-only t))
17357 (insert-before-markers "#+COLUMNS: " fmt "\n")))
17358 (org-set-local 'org-columns-default-format fmt))))))
17360 (defvar org-overriding-columns-format nil
17361 "When set, overrides any other definition.")
17362 (defvar org-agenda-view-columns-initially nil
17363 "When set, switch to columns view immediately after creating the agenda.")
17365 (defun org-agenda-columns ()
17366 "Turn on column view in the agenda."
17368 (org-verify-version 'columns)
17369 (org-columns-remove-overlays)
17370 (move-marker org-columns-begin-marker (point))
17371 (let (fmt cache maxwidths m)
17373 ((and (local-variable-p 'org-overriding-columns-format)
17374 org-overriding-columns-format)
17375 (setq fmt org-overriding-columns-format))
17376 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
17377 (setq fmt (org-entry-get m "COLUMNS" t)))
17378 ((and (boundp 'org-columns-current-fmt)
17379 (local-variable-p 'org-columns-current-fmt)
17380 org-columns-current-fmt)
17381 (setq fmt org-columns-current-fmt))
17382 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
17383 (setq m (get-text-property m 'org-hd-marker))
17384 (setq fmt (org-entry-get m "COLUMNS" t))))
17385 (setq fmt (or fmt org-columns-default-format))
17386 (org-set-local 'org-columns-current-fmt fmt)
17387 (org-columns-compile-format fmt)
17389 ;; Get and cache the properties
17390 (goto-char (point-min))
17391 (while (not (eobp))
17392 (when (setq m (or (get-text-property (point) 'org-hd-marker)
17393 (get-text-property (point) 'org-marker)))
17394 (push (cons (org-current-line) (org-entry-properties m)) cache))
17395 (beginning-of-line 2))
17397 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
17398 (org-set-local 'org-columns-current-maxwidths maxwidths)
17399 (org-columns-display-here-title)
17401 (goto-line (car x))
17402 (org-columns-display-here (cdr x)))
17405 (defun org-columns-get-autowidth-alist (s cache)
17406 "Derive the maximum column widths from the format and the cache."
17407 (let ((start 0) rtn)
17408 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
17409 (push (cons (match-string 1 s) 1) rtn)
17410 (setq start (match-end 0)))
17412 (setcdr x (apply 'max
17415 (length (or (cdr (assoc (car x) (cdr y))) " ")))
17420 (defun org-columns-compute-all ()
17421 "Compute all columns that have operators defined."
17423 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
17424 (let ((columns org-columns-current-fmt-compiled) col)
17425 (while (setq col (pop columns))
17428 (org-columns-compute (car col)))))))
17430 (defun org-columns-update (property)
17431 "Recompute PROPERTY, and update the columns display for it."
17432 (org-columns-compute property)
17436 (when (equal (org-overlay-get ov 'org-columns-key) property)
17437 (setq pos (org-overlay-start ov))
17439 (when (setq val (cdr (assoc property
17441 (point-at-bol) 'org-summaries))))
17442 (setq fmt (org-overlay-get ov 'org-columns-format))
17443 (org-overlay-put ov 'org-columns-value val)
17444 (org-overlay-put ov 'display (format fmt val)))))
17445 org-columns-overlays))))
17447 (defun org-columns-compute (property)
17448 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
17450 (let* ((re (concat "^" outline-regexp))
17451 (lmax 30) ; Does anyone use deeper levels???
17452 (lsum (make-vector lmax 0))
17453 (lflag (make-vector lmax nil))
17455 (ass (assoc property org-columns-current-fmt-compiled))
17456 (format (nth 4 ass))
17457 (printf (nth 5 ass))
17458 (beg org-columns-top-level-marker)
17459 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
17461 ;; Find the region to compute
17463 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
17465 ;; Walk the tree from the back and do the computations
17466 (while (re-search-backward re beg t)
17467 (setq sumpos (match-beginning 0)
17469 level (org-outline-level)
17470 val (org-entry-get nil property)
17471 valflag (and val (string-match "\\S-" val)))
17473 ((< level last-level)
17474 ;; put the sum of lower levels here as a property
17475 (setq sum (aref lsum last-level) ; current sum
17476 flag (aref lflag last-level) ; any valid entries from children?
17477 str (org-column-number-to-string sum format printf)
17478 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
17479 useval (if flag str1 (if valflag val ""))
17480 sum-alist (get-text-property sumpos 'org-summaries))
17481 (if (assoc property sum-alist)
17482 (setcdr (assoc property sum-alist) useval)
17483 (push (cons property useval) sum-alist)
17485 (add-text-properties sumpos (1+ sumpos)
17486 (list 'org-summaries sum-alist))))
17488 (org-entry-put nil property (if flag str val)))
17489 ;; add current to current level accumulator
17490 (when (or flag valflag)
17491 (aset lsum level (+ (aref lsum level)
17492 (if flag sum (org-column-string-to-number
17493 (if flag str val) format))))
17494 (aset lflag level t))
17495 ;; clear accumulators for deeper levels
17496 (loop for l from (1+ level) to (1- lmax) do
17498 (aset lflag l nil)))
17499 ((>= level last-level)
17500 ;; add what we have here to the accumulator for this level
17501 (aset lsum level (+ (aref lsum level)
17502 (org-column-string-to-number (or val "0") format)))
17503 (and valflag (aset lflag level t)))
17504 (t (error "This should not happen")))))))
17506 (defun org-columns-redo ()
17507 "Construct the column display again."
17509 (message "Recomputing columns...")
17511 (if (marker-position org-columns-begin-marker)
17512 (goto-char org-columns-begin-marker))
17513 (org-columns-remove-overlays)
17515 (call-interactively 'org-columns)
17516 (call-interactively 'org-agenda-columns)))
17517 (message "Recomputing columns...done"))
17519 (defun org-columns-not-in-agenda ()
17520 (if (eq major-mode 'org-agenda-mode)
17521 (error "This command is only allowed in Org-mode buffers")))
17524 (defun org-string-to-number (s)
17525 "Convert string to number, and interpret hh:mm:ss."
17526 (if (not (string-match ":" s))
17527 (string-to-number s)
17528 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
17530 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
17533 (defun org-column-number-to-string (n fmt &optional printf)
17534 "Convert a computed column number to a string value, according to FMT."
17536 ((eq fmt 'add_times)
17537 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
17538 (format "%d:%02d" h m)))
17539 ((eq fmt 'checkbox)
17540 (cond ((= n (floor n)) "[X]")
17543 ((memq fmt '(checkbox-n-of-m checkbox-percent))
17544 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
17545 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
17546 (printf (format printf n))
17547 ((eq fmt 'currency)
17549 (t (number-to-string n))))
17551 (defun org-nofm-to-completion (n m &optional percent)
17553 (format "[%d/%d]" n m)
17554 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
17556 (defun org-column-string-to-number (s fmt)
17557 "Convert a column value to a number that can be used for column computing."
17559 ((string-match ":" s)
17560 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
17562 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
17564 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
17565 (if (equal s "[X]") 1. 0.000001))
17566 (t (string-to-number s))))
17568 (defun org-columns-uncompile-format (cfmt)
17569 "Turn the compiled columns format back into a string representation."
17570 (let ((rtn "") e s prop title op width fmt printf)
17571 (while (setq e (pop cfmt))
17579 ((eq fmt 'add_times) (setq op ":"))
17580 ((eq fmt 'checkbox) (setq op "X"))
17581 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
17582 ((eq fmt 'checkbox-percent) (setq op "X%"))
17583 ((eq fmt 'add_numbers) (setq op "+"))
17584 ((eq fmt 'currency) (setq op "$")))
17585 (if (and op printf) (setq op (concat op ";" printf)))
17586 (if (equal title prop) (setq title nil))
17587 (setq s (concat "%" (if width (number-to-string width))
17589 (if title (concat "(" title ")"))
17590 (if op (concat "{" op "}"))))
17591 (setq rtn (concat rtn " " s)))
17594 (defun org-columns-compile-format (fmt)
17595 "Turn a column format string into an alist of specifications.
17596 The alist has one entry for each column in the format. The elements of
17598 property the property
17599 title the title field for the columns
17600 width the column width in characters, can be nil for automatic
17601 operator the operator if any
17602 format the output format for computed results, derived from operator
17603 printf a printf format for computed values"
17604 (let ((start 0) width prop title op f printf)
17605 (setq org-columns-current-fmt-compiled nil)
17606 (while (string-match
17607 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
17609 (setq start (match-end 0)
17610 width (match-string 1 fmt)
17611 prop (match-string 2 fmt)
17612 title (or (match-string 3 fmt) prop)
17613 op (match-string 4 fmt)
17616 (if width (setq width (string-to-number width)))
17617 (when (and op (string-match ";" op))
17618 (setq printf (substring op (match-end 0))
17619 op (substring op 0 (match-beginning 0))))
17621 ((equal op "+") (setq f 'add_numbers))
17622 ((equal op "$") (setq f 'currency))
17623 ((equal op ":") (setq f 'add_times))
17624 ((equal op "X") (setq f 'checkbox))
17625 ((equal op "X/") (setq f 'checkbox-n-of-m))
17626 ((equal op "X%") (setq f 'checkbox-percent))
17628 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
17629 (setq org-columns-current-fmt-compiled
17630 (nreverse org-columns-current-fmt-compiled))))
17633 ;;; Dynamic block for Column view
17635 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
17636 "Get the column view of the current buffer or subtree.
17637 The first optional argument MAXLEVEL sets the level limit. A
17638 second optional argument SKIP-EMPTY-ROWS tells whether to skip
17639 empty rows, an empty row being one where all the column view
17640 specifiers except ITEM are empty. This function returns a list
17641 containing the title row and all other rows. Each row is a list
17644 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
17645 (n (length title)) row tbl)
17646 (goto-char (point-min))
17647 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
17648 (or (null maxlevel)
17650 (if org-odd-levels-only
17651 (/ (1+ (length (match-string 1))) 2)
17652 (length (match-string 1))))))
17653 (when (get-char-property (match-beginning 0) 'org-columns-key)
17655 (loop for i from 0 to (1- n) do
17656 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
17657 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
17660 (setq row (nreverse row))
17661 (unless (and skip-empty-rows
17662 (eq 1 (length (delete "" (delete-dups row)))))
17664 (append (list title 'hline) (nreverse tbl)))))
17666 (defun org-dblock-write:columnview (params)
17667 "Write the column view table.
17668 PARAMS is a property list of parameters:
17670 :width enforce same column widths with <N> specifiers.
17671 :id the :ID: property of the entry where the columns view
17672 should be built, as a string. When `local', call locally.
17673 When `global' call column view with the cursor at the beginning
17674 of the buffer (usually this means that the whole buffer switches
17676 :hlines When t, insert a hline before each item. When a number, insert
17677 a hline before each level <= that number.
17678 :vlines When t, make each column a colgroup to enforce vertical lines.
17679 :maxlevel When set to a number, don't capture headlines below this level.
17681 When t, skip rows where all specifiers other than ITEM are empty."
17682 (let ((pos (move-marker (make-marker) (point)))
17683 (hlines (plist-get params :hlines))
17684 (vlines (plist-get params :vlines))
17685 (maxlevel (plist-get params :maxlevel))
17686 (skip-empty-rows (plist-get params :skip-empty-rows))
17687 tbl id idpos nfields tmp)
17690 (when (setq id (plist-get params :id))
17691 (cond ((not id) nil)
17692 ((eq id 'global) (goto-char (point-min)))
17693 ((eq id 'local) nil)
17694 ((setq idpos (org-find-entry-with-id id))
17696 (t (error "Cannot find entry with :ID: %s" id))))
17698 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
17699 (setq nfields (length (car tbl)))
17700 (org-columns-quit)))
17702 (move-marker pos nil)
17704 (when (plist-get params :hlines)
17707 (if (eq (car tbl) 'hline)
17708 (push (pop tbl) tmp)
17709 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
17710 (if (and (not (eq (car tmp) 'hline))
17712 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
17713 (push 'hline tmp)))
17714 (push (pop tbl) tmp)))
17715 (setq tbl (nreverse tmp)))
17717 (setq tbl (mapcar (lambda (x)
17718 (if (eq 'hline x) x (cons "" x)))
17720 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
17722 (insert (org-listtable-to-string tbl))
17723 (when (plist-get params :width)
17724 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
17725 org-columns-current-widths "|")))
17727 (org-table-align))))
17729 (defun org-listtable-to-string (tbl)
17730 "Convert a listtable TBL to a string that contains the Org-mode table.
17731 The table still need to be alligned. The resulting string has no leading
17732 and tailing newline characters."
17737 (concat "|" (mapconcat 'identity x "|") "|"))
17738 ((eq x 'hline) "|-|")
17739 (t (error "Garbage in listtable: %s" x))))
17742 (defun org-insert-columns-dblock ()
17743 "Create a dynamic block capturing a column view table."
17745 (let ((defaults '(:name "columnview" :hlines 1))
17746 (id (completing-read
17747 "Capture columns (local, global, entry with :ID: property) [local]: "
17748 (append '(("global") ("local"))
17749 (mapcar 'list (org-property-values "ID"))))))
17750 (if (equal id "") (setq id 'local))
17751 (if (equal id "global") (setq id 'global))
17752 (setq defaults (append defaults (list :id id)))
17753 (org-create-dblock defaults)
17754 (org-update-dblock)))
17758 (defvar org-last-changed-timestamp nil)
17759 (defvar org-time-was-given) ; dynamically scoped parameter
17760 (defvar org-end-time-was-given) ; dynamically scoped parameter
17761 (defvar org-ts-what) ; dynamically scoped parameter
17763 (defun org-time-stamp (arg)
17764 "Prompt for a date/time and insert a time stamp.
17765 If the user specifies a time like HH:MM, or if this command is called
17766 with a prefix argument, the time stamp will contain date and time.
17767 Otherwise, only the date will be included. All parts of a date not
17768 specified by the user will be filled in from the current date/time.
17769 So if you press just return without typing anything, the time stamp
17770 will represent the current date/time. If there is already a timestamp
17771 at the cursor, it will be modified."
17775 ;; Default time is either today, or, when entering a range,
17776 ;; the range start.
17777 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
17779 (re-search-backward
17780 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
17781 (- (point) 20) t)))
17782 (apply 'encode-time (org-parse-time-string (match-string 1)))
17784 (default-input (and ts (org-get-compact-tod ts)))
17785 org-time-was-given org-end-time-was-given time)
17787 ((and (org-at-timestamp-p)
17788 (eq last-command 'org-time-stamp)
17789 (eq this-command 'org-time-stamp))
17791 (setq time (let ((this-command this-command))
17792 (org-read-date arg 'totime nil nil default-time default-input)))
17793 (org-insert-time-stamp time (or org-time-was-given arg)))
17794 ((org-at-timestamp-p)
17795 (setq time (let ((this-command this-command))
17796 (org-read-date arg 'totime nil nil default-time default-input)))
17797 (when (org-at-timestamp-p) ; just to get the match data
17799 (setq org-last-changed-timestamp
17800 (org-insert-time-stamp
17801 time (or org-time-was-given arg)
17802 nil nil nil (list org-end-time-was-given))))
17803 (message "Timestamp updated"))
17805 (setq time (let ((this-command this-command))
17806 (org-read-date arg 'totime nil nil default-time default-input)))
17807 (org-insert-time-stamp time (or org-time-was-given arg)
17808 nil nil nil (list org-end-time-was-given))))))
17810 ;; FIXME: can we use this for something else????
17811 ;; like computing time differences?????
17812 (defun org-get-compact-tod (s)
17813 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
17814 (let* ((t1 (match-string 1 s))
17815 (h1 (string-to-number (match-string 2 s)))
17816 (m1 (string-to-number (match-string 3 s)))
17817 (t2 (and (match-end 4) (match-string 5 s)))
17818 (h2 (and t2 (string-to-number (match-string 6 s))))
17819 (m2 (and t2 (string-to-number (match-string 7 s))))
17823 (setq dh (- h2 h1) dm (- m2 m1))
17824 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
17825 (concat t1 "+" (number-to-string dh)
17826 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
17828 (defun org-time-stamp-inactive (&optional arg)
17829 "Insert an inactive time stamp.
17830 An inactive time stamp is enclosed in square brackets instead of angle
17831 brackets. It is inactive in the sense that it does not trigger agenda entries,
17832 does not link to the calendar and cannot be changed with the S-cursor keys.
17833 So these are more for recording a certain time/date."
17835 (let (org-time-was-given org-end-time-was-given time)
17836 (setq time (org-read-date arg 'totime))
17837 (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
17838 nil nil (list org-end-time-was-given))))
17840 (defvar org-date-ovl (org-make-overlay 1 1))
17841 (org-overlay-put org-date-ovl 'face 'org-warning)
17842 (org-detach-overlay org-date-ovl)
17844 (defvar org-ans1) ; dynamically scoped parameter
17845 (defvar org-ans2) ; dynamically scoped parameter
17847 (defvar org-plain-time-of-day-regexp) ; defined below
17849 (defvar org-read-date-overlay nil)
17850 (defvar org-dcst nil) ; dynamically scoped
17852 (defun org-read-date (&optional with-time to-time from-string prompt
17853 default-time default-input)
17854 "Read a date, possibly a time, and make things smooth for the user.
17855 The prompt will suggest to enter an ISO date, but you can also enter anything
17856 which will at least partially be understood by `parse-time-string'.
17857 Unrecognized parts of the date will default to the current day, month, year,
17858 hour and minute. If this command is called to replace a timestamp at point,
17859 of to enter the second timestamp of a range, the default time is taken from the
17860 existing stamp. For example,
17861 3-2-5 --> 2003-02-05
17862 feb 15 --> currentyear-02-15
17863 sep 12 9 --> 2009-09-12
17864 12:45 --> today 12:45
17865 22 sept 0:34 --> currentyear-09-22 0:34
17866 12 --> currentyear-currentmonth-12
17867 Fri --> nearest Friday (today or later)
17870 Furthermore you can specify a relative date by giving, as the *first* thing
17871 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
17872 change in days weeks, months, years.
17873 With a single plus or minus, the date is relative to today. With a double
17874 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
17875 +4d --> four days from today
17876 +4 --> same as above
17877 +2w --> two weeks from today
17878 ++5 --> five days from default date
17880 The function understands only English month and weekday abbreviations,
17881 but this can be configured with the variables `parse-time-months' and
17882 `parse-time-weekdays'.
17884 While prompting, a calendar is popped up - you can also select the
17885 date with the mouse (button 1). The calendar shows a period of three
17886 months. To scroll it to other months, use the keys `>' and `<'.
17887 If you don't like the calendar, turn it off with
17888 \(setq org-read-date-popup-calendar nil)
17890 With optional argument TO-TIME, the date will immediately be converted
17891 to an internal time.
17892 With an optional argument WITH-TIME, the prompt will suggest to also
17893 insert a time. Note that when WITH-TIME is not set, you can still
17894 enter a time, and this function will inform the calling routine about
17895 this change. The calling routine may then choose to change the format
17896 used to insert the time stamp into the buffer to include the time.
17897 With optional argument FROM-STRING, read from this string instead from
17898 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
17899 the time/date that is used for everything that is not specified by the
17901 (require 'parse-time)
17902 (let* ((org-time-stamp-rounding-minutes
17903 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
17904 (org-dcst org-display-custom-times)
17905 (ct (org-current-time))
17906 (def (or default-time ct))
17907 (defdecode (decode-time def))
17909 (when (< (nth 2 defdecode) org-extend-today-until)
17910 (setcar (nthcdr 2 defdecode) -1)
17911 (setcar (nthcdr 1 defdecode) 59)
17912 (setq def (apply 'encode-time defdecode)
17913 defdecode (decode-time def)))))
17914 (calendar-move-hook nil)
17915 (view-diary-entries-initially nil)
17916 (view-calendar-holidays-initially nil)
17917 (timestr (format-time-string
17918 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
17919 (prompt (concat (if prompt (concat prompt " ") "")
17920 (format "Date+time [%s]: " timestr)))
17921 ans (org-ans0 "") org-ans1 org-ans2 final)
17924 (from-string (setq ans from-string))
17925 (org-read-date-popup-calendar
17927 (save-window-excursion
17929 (calendar-forward-day (- (time-to-days def)
17930 (calendar-absolute-from-gregorian
17931 (calendar-current-date))))
17932 (org-eval-in-calendar nil t)
17933 (let* ((old-map (current-local-map))
17934 (map (copy-keymap calendar-mode-map))
17935 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
17936 (org-defkey map (kbd "RET") 'org-calendar-select)
17937 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
17938 'org-calendar-select-mouse)
17939 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
17940 'org-calendar-select-mouse
)
17941 (org-defkey minibuffer-local-map
[(meta shift left
)]
17942 (lambda () (interactive)
17943 (org-eval-in-calendar '(calendar-backward-month 1))))
17944 (org-defkey minibuffer-local-map
[(meta shift right
)]
17945 (lambda () (interactive)
17946 (org-eval-in-calendar '(calendar-forward-month 1))))
17947 (org-defkey minibuffer-local-map
[(meta shift up
)]
17948 (lambda () (interactive)
17949 (org-eval-in-calendar '(calendar-backward-year 1))))
17950 (org-defkey minibuffer-local-map
[(meta shift down
)]
17951 (lambda () (interactive)
17952 (org-eval-in-calendar '(calendar-forward-year 1))))
17953 (org-defkey minibuffer-local-map
[(shift up
)]
17954 (lambda () (interactive)
17955 (org-eval-in-calendar '(calendar-backward-week 1))))
17956 (org-defkey minibuffer-local-map
[(shift down
)]
17957 (lambda () (interactive)
17958 (org-eval-in-calendar '(calendar-forward-week 1))))
17959 (org-defkey minibuffer-local-map
[(shift left
)]
17960 (lambda () (interactive)
17961 (org-eval-in-calendar '(calendar-backward-day 1))))
17962 (org-defkey minibuffer-local-map
[(shift right
)]
17963 (lambda () (interactive)
17964 (org-eval-in-calendar '(calendar-forward-day 1))))
17965 (org-defkey minibuffer-local-map
">"
17966 (lambda () (interactive)
17967 (org-eval-in-calendar '(scroll-calendar-left 1))))
17968 (org-defkey minibuffer-local-map
"<"
17969 (lambda () (interactive)
17970 (org-eval-in-calendar '(scroll-calendar-right 1))))
17973 (use-local-map map
)
17974 (add-hook 'post-command-hook
'org-read-date-display
)
17975 (setq org-ans0
(read-string prompt default-input nil nil
))
17976 ;; org-ans0: from prompt
17977 ;; org-ans1: from mouse click
17978 ;; org-ans2: from calendar motion
17979 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
17980 (remove-hook 'post-command-hook
'org-read-date-display
)
17981 (use-local-map old-map
)
17982 (when org-read-date-overlay
17983 (org-delete-overlay org-read-date-overlay
)
17984 (setq org-read-date-overlay nil
)))))))
17986 (t ; Naked prompt only
17988 (setq ans
(read-string prompt default-input nil timestr
))
17989 (when org-read-date-overlay
17990 (org-delete-overlay org-read-date-overlay
)
17991 (setq org-read-date-overlay nil
)))))
17993 (setq final
(org-read-date-analyze ans def defdecode
))
17996 (apply 'encode-time final
)
17997 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
17998 (format "%04d-%02d-%02d %02d:%02d"
17999 (nth 5 final
) (nth 4 final
) (nth 3 final
)
18000 (nth 2 final
) (nth 1 final
))
18001 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
18005 (defun org-read-date-display ()
18006 "Display the currrent date prompt interpretation in the minibuffer."
18007 (when org-read-date-display-live
18008 (when org-read-date-overlay
18009 (org-delete-overlay org-read-date-overlay
))
18012 (while (not (equal (buffer-substring
18013 (max (point-min) (- (point) 4)) (point))
18017 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
18018 " " (or org-ans1 org-ans2
)))
18019 (org-end-time-was-given nil
)
18020 (f (org-read-date-analyze ans def defdecode
))
18022 org-time-stamp-custom-formats
18023 org-time-stamp-formats
))
18024 (fmt (if (or with-time
18025 (and (boundp 'org-time-was-given
) org-time-was-given
))
18028 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
18029 (when (and org-end-time-was-given
18030 (string-match org-plain-time-of-day-regexp txt
))
18031 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
18032 org-end-time-was-given
18033 (substring txt
(match-end 0)))))
18034 (setq org-read-date-overlay
18035 (make-overlay (1- (point-at-eol)) (point-at-eol)))
18036 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
18038 (defun org-read-date-analyze (ans def defdecode
)
18039 "Analyze the combined answer of the date prompt."
18040 ;; FIXME: cleanup and comment
18041 (let (delta deltan deltaw deltadef year month day
18042 hour minute second wday pm h2 m2 tl wday1
)
18044 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
18045 (setq ans
(replace-match "" t t ans
)
18047 deltaw
(nth 1 delta
)
18048 deltadef
(nth 2 delta
)))
18050 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
18051 (when (string-match
18052 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
18053 (setq year
(if (match-end 2)
18054 (string-to-number (match-string 2 ans
))
18055 (string-to-number (format-time-string "%Y")))
18056 month
(string-to-number (match-string 3 ans
))
18057 day
(string-to-number (match-string 4 ans
)))
18058 (if (< year
100) (setq year
(+ 2000 year
)))
18059 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
18061 ;; Help matching am/pm times, because `parse-time-string' does not do that.
18062 ;; If there is a time with am/pm, and *no* time without it, we convert
18063 ;; so that matching will be successful.
18064 (loop for i from
1 to
2 do
; twice, for end time as well
18065 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
18066 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
18067 (setq hour
(string-to-number (match-string 1 ans
))
18068 minute
(if (match-end 3)
18069 (string-to-number (match-string 3 ans
))
18072 (string-to-char (downcase (match-string 4 ans
)))))
18073 (if (and (= hour
12) (not pm
))
18075 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
18076 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
18079 ;; Check if a time range is given as a duration
18080 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
18081 (setq hour
(string-to-number (match-string 1 ans
))
18082 h2
(+ hour
(string-to-number (match-string 3 ans
)))
18083 minute
(string-to-number (match-string 2 ans
))
18084 m2
(+ minute
(if (match-end 5) (string-to-number (match-string 5 ans
))0)))
18085 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
18086 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
) t t ans
)))
18088 ;; Check if there is a time range
18089 (when (boundp 'org-end-time-was-given
)
18090 (setq org-time-was-given nil
)
18091 (when (and (string-match org-plain-time-of-day-regexp ans
)
18093 (setq org-end-time-was-given
(match-string 8 ans
))
18094 (setq ans
(concat (substring ans
0 (match-beginning 7))
18095 (substring ans
(match-end 7))))))
18097 (setq tl
(parse-time-string ans
)
18098 day
(or (nth 3 tl
) (nth 3 defdecode
))
18099 month
(or (nth 4 tl
)
18100 (if (and org-read-date-prefer-future
18101 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
18102 (1+ (nth 4 defdecode
))
18103 (nth 4 defdecode
)))
18104 year
(or (nth 5 tl
)
18105 (if (and org-read-date-prefer-future
18106 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
18107 (1+ (nth 5 defdecode
))
18108 (nth 5 defdecode
)))
18109 hour
(or (nth 2 tl
) (nth 2 defdecode
))
18110 minute
(or (nth 1 tl
) (nth 1 defdecode
))
18111 second
(or (nth 0 tl
) 0)
18115 (let ((now (decode-time (current-time))))
18116 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
18117 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
18118 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
18119 ((equal deltaw
"m") (setq month
(+ month deltan
)))
18120 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
18121 (when (and wday
(not (nth 3 tl
)))
18122 ;; Weekday was given, but no day, so pick that day in the week
18123 ;; on or after the derived date.
18124 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
18125 (unless (equal wday wday1
)
18126 (setq day
(+ day
(%
(- wday wday1 -
7) 7)))))
18127 (if (and (boundp 'org-time-was-given
)
18129 (setq org-time-was-given t
))
18130 (if (< year
100) (setq year
(+ 2000 year
)))
18131 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
18132 (list second minute hour day month year
)))
18134 (defvar parse-time-weekdays
)
18136 (defun org-read-date-get-relative (s today default
)
18137 "Check string S for special relative date string.
18138 TODAY and DEFAULT are internal times, for today and for a default.
18139 Return shift list (N what def-flag)
18140 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
18141 N is the number of WHATs to shift.
18142 DEF-FLAG is t when a double ++ or -- indicates shift relative to
18143 the DEFAULT date rather than TODAY."
18144 (when (string-match
18146 "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
18148 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
18149 "\\([ \t]\\|$\\)") s
)
18150 (let* ((dir (if (match-end 1)
18151 (string-to-char (substring (match-string 1 s
) -
1))
18153 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
18154 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
18155 (what (if (match-end 3) (match-string 3 s
) "d"))
18156 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
18157 (date (if rel default today
))
18158 (wday (nth 6 (decode-time date
)))
18162 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
18163 (if (= dir ?-
) (setq delta
(- delta
7)))
18164 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
18165 (list delta
"d" rel
))
18166 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
18168 (defun org-eval-in-calendar (form &optional keepdate
)
18169 "Eval FORM in the calendar window and return to current window.
18170 Also, store the cursor date in variable org-ans2."
18171 (let ((sw (selected-window)))
18172 (select-window (get-buffer-window "*Calendar*"))
18174 (when (and (not keepdate
) (calendar-cursor-to-date))
18175 (let* ((date (calendar-cursor-to-date))
18176 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
18177 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
18178 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
18179 (select-window sw
)))
18181 ; ;; Update the prompt to show new default date
18183 ; (goto-char (point-min))
18184 ; (when (and org-ans2
18185 ; (re-search-forward "\\[[-0-9]+\\]" nil t)
18186 ; (get-text-property (match-end 0) 'field))
18187 ; (let ((inhibit-read-only t))
18188 ; (replace-match (concat "[" org-ans2 "]") t t)
18189 ; (add-text-properties (point-min) (1+ (match-end 0))
18190 ; (text-properties-at (1+ (point-min)))))))))
18192 (defun org-calendar-select ()
18193 "Return to `org-read-date' with the date currently selected.
18194 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
18196 (when (calendar-cursor-to-date)
18197 (let* ((date (calendar-cursor-to-date))
18198 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
18199 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
18200 (if (active-minibuffer-window) (exit-minibuffer))))
18202 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
18203 "Insert a date stamp for the date given by the internal TIME.
18204 WITH-HM means, use the stamp format that includes the time of the day.
18205 INACTIVE means use square brackets instead of angular ones, so that the
18206 stamp will not contribute to the agenda.
18207 PRE and POST are optional strings to be inserted before and after the
18209 The command returns the inserted time stamp."
18210 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
18212 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
18213 (insert-before-markers (or pre
""))
18214 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
18215 (when (listp extra
)
18216 (setq extra
(car extra
))
18217 (if (and (stringp extra
)
18218 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
18219 (setq extra
(format "-%02d:%02d"
18220 (string-to-number (match-string 1 extra
))
18221 (string-to-number (match-string 2 extra
))))
18225 (insert-before-markers extra
)
18227 (insert-before-markers (or post
""))
18230 (defun org-toggle-time-stamp-overlays ()
18231 "Toggle the use of custom time stamp formats."
18233 (setq org-display-custom-times
(not org-display-custom-times
))
18234 (unless org-display-custom-times
18235 (let ((p (point-min)) (bmp (buffer-modified-p)))
18236 (while (setq p
(next-single-property-change p
'display
))
18237 (if (and (get-text-property p
'display
)
18238 (eq (get-text-property p
'face
) 'org-date
))
18239 (remove-text-properties
18240 p
(setq p
(next-single-property-change p
'display
))
18242 (set-buffer-modified-p bmp
)))
18243 (if (featurep 'xemacs
)
18244 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
18245 (org-restart-font-lock)
18246 (setq org-table-may-need-update t
)
18247 (if org-display-custom-times
18248 (message "Time stamps are overlayed with custom format")
18249 (message "Time stamp overlays removed")))
18251 (defun org-display-custom-time (beg end
)
18252 "Overlay modified time stamp format over timestamp between BED and END."
18253 (let* ((ts (buffer-substring beg end
))
18254 t1 w1 with-hm tf time str w2
(off 0))
18256 (setq t1
(org-parse-time-string ts t
))
18257 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
18258 (setq off
(- (match-end 0) (match-beginning 0)))))
18259 (setq end
(- end off
))
18260 (setq w1
(- end beg
)
18261 with-hm
(and (nth 1 t1
) (nth 2 t1
))
18262 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
18263 time
(org-fix-decoded-time t1
)
18265 (format-time-string
18266 (substring tf
1 -
1) (apply 'encode-time time
))
18267 nil
'mouse-face
'highlight
)
18269 (if (not (= w2 w1
))
18270 (add-text-properties (1+ beg
) (+ 2 beg
)
18271 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
18272 (if (featurep 'xemacs
)
18274 (put-text-property beg end
'invisible t
)
18275 (put-text-property beg end
'end-glyph
(make-glyph str
)))
18276 (put-text-property beg end
'display str
))))
18278 (defun org-translate-time (string)
18279 "Translate all timestamps in STRING to custom format.
18280 But do this only if the variable `org-display-custom-times' is set."
18281 (when org-display-custom-times
18284 (re org-ts-regexp-both
)
18285 t1 with-hm inactive tf time str beg end
)
18286 (while (setq start
(string-match re string start
))
18287 (setq beg
(match-beginning 0)
18289 t1
(save-match-data
18290 (org-parse-time-string (substring string beg end
) t
))
18291 with-hm
(and (nth 1 t1
) (nth 2 t1
))
18292 inactive
(equal (substring string beg
(1+ beg
)) "[")
18293 tf
(funcall (if with-hm
'cdr
'car
)
18294 org-time-stamp-custom-formats
)
18295 time
(org-fix-decoded-time t1
)
18296 str
(format-time-string
18298 (if inactive
"[" "<") (substring tf
1 -
1)
18299 (if inactive
"]" ">"))
18300 (apply 'encode-time time
))
18301 string
(replace-match str t t string
)
18302 start
(+ start
(length str
)))))))
18305 (defun org-fix-decoded-time (time)
18306 "Set 0 instead of nil for the first 6 elements of time.
18307 Don't touch the rest."
18309 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
18311 (defun org-days-to-time (timestamp-string)
18312 "Difference between TIMESTAMP-STRING and now in days."
18313 (- (time-to-days (org-time-string-to-time timestamp-string
))
18314 (time-to-days (current-time))))
18316 (defun org-deadline-close (timestamp-string &optional ndays
)
18317 "Is the time in TIMESTAMP-STRING close to the current date?"
18318 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
18319 (and (< (org-days-to-time timestamp-string
) ndays
)
18320 (not (org-entry-is-done-p))))
18322 (defun org-get-wdays (ts)
18323 "Get the deadline lead time appropriate for timestring TS."
18325 ((<= org-deadline-warning-days
0)
18326 ;; 0 or negative, enforce this value no matter what
18327 (- org-deadline-warning-days
))
18328 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts
)
18329 ;; lead time is specified.
18330 (floor (* (string-to-number (match-string 1 ts
))
18331 (cdr (assoc (match-string 2 ts
)
18332 '(("d" .
1) ("w" .
7)
18333 ("m" .
30.4) ("y" .
365.25)))))))
18334 ;; go for the default.
18335 (t org-deadline-warning-days
)))
18337 (defun org-calendar-select-mouse (ev)
18338 "Return to `org-read-date' with the date currently selected.
18339 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
18341 (mouse-set-point ev
)
18342 (when (calendar-cursor-to-date)
18343 (let* ((date (calendar-cursor-to-date))
18344 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
18345 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
18346 (if (active-minibuffer-window) (exit-minibuffer))))
18348 (defun org-check-deadlines (ndays)
18349 "Check if there are any deadlines due or past due.
18350 A deadline is considered due if it happens within `org-deadline-warning-days'
18351 days from today's date. If the deadline appears in an entry marked DONE,
18352 it is not shown. The prefix arg NDAYS can be used to test that many
18353 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
18355 (let* ((org-warn-days
18357 ((equal ndays
'(4)) 100000)
18358 (ndays (prefix-numeric-value ndays
))
18359 (t (abs org-deadline-warning-days
))))
18360 (case-fold-search nil
)
18361 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
18363 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
18365 (message "%d deadlines past-due or due within %d days"
18366 (org-occur regexp nil callback
)
18369 (defun org-check-before-date (date)
18370 "Check if there are deadlines or scheduled entries before DATE."
18371 (interactive (list (org-read-date)))
18372 (let ((case-fold-search nil
)
18373 (regexp (concat "\\<\\(" org-deadline-string
18374 "\\|" org-scheduled-string
18375 "\\) *<\\([^>]+\\)>"))
18377 (lambda () (time-less-p
18378 (org-time-string-to-time (match-string 2))
18379 (org-time-string-to-time date
)))))
18380 (message "%d entries before %s"
18381 (org-occur regexp nil callback
) date
)))
18383 (defun org-evaluate-time-range (&optional to-buffer
)
18384 "Evaluate a time range by computing the difference between start and end.
18385 Normally the result is just printed in the echo area, but with prefix arg
18386 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
18387 If the time range is actually in a table, the result is inserted into the
18389 For time difference computation, a year is assumed to be exactly 365
18390 days in order to avoid rounding problems."
18393 (org-clock-update-time-maybe)
18395 (unless (org-at-date-range-p t
)
18396 (goto-char (point-at-bol))
18397 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
18398 (if (not (org-at-date-range-p t
))
18399 (error "Not at a time-stamp range, and none found in current line")))
18400 (let* ((ts1 (match-string 1))
18401 (ts2 (match-string 2))
18402 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
18403 (match-end (match-end 0))
18404 (time1 (org-time-string-to-time ts1
))
18405 (time2 (org-time-string-to-time ts2
))
18406 (t1 (time-to-seconds time1
))
18407 (t2 (time-to-seconds time2
))
18408 (diff (abs (- t2 t1
)))
18409 (negative (< (- t2 t1
) 0))
18410 ;; (ys (floor (* 365 24 60 60)))
18413 (fy "%dy %dd %02d:%02d")
18415 (fd "%dd %02d:%02d")
18420 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
18422 d
(floor (/ diff ds
)) diff
(mod diff ds
)
18423 h
(floor (/ diff hs
)) diff
(mod diff hs
)
18424 m
(floor (/ diff
60)))
18425 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
18427 d
(floor (+ (/ diff ds
) 0.5))
18429 (if (not to-buffer
)
18430 (message "%s" (org-make-tdiff-string y d h m
))
18431 (if (org-at-table-p)
18433 (goto-char match-end
)
18435 (and (looking-at " *|") (goto-char (match-end 0))))
18436 (goto-char match-end
))
18438 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
18439 (replace-match ""))
18440 (if negative
(insert " -"))
18441 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
18442 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
18443 (insert " " (format fh h m
))))
18444 (if align
(org-table-align))
18445 (message "Time difference inserted")))))
18447 (defun org-make-tdiff-string (y d h m
)
18450 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
18452 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
18454 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
18456 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
18458 (apply 'format fmt
(nreverse l
))))
18460 (defun org-time-string-to-time (s)
18461 (apply 'encode-time
(org-parse-time-string s
)))
18463 (defun org-time-string-to-absolute (s &optional daynr prefer
)
18464 "Convert a time stamp to an absolute day number.
18465 If there is a specifyer for a cyclic time stamp, get the closest date to
18468 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
18469 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
18472 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
18473 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
18474 (time-to-days (current-time))) (match-string 0 s
)
18476 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
18478 (defun org-time-from-absolute (d)
18479 "Return the time corresponding to date D.
18480 D may be an absolute day number, or a calendar-type list (month day year)."
18481 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
18482 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
18484 (defun org-calendar-holiday ()
18485 "List of holidays, for Diary display in Org-mode."
18486 (require 'holidays
)
18488 (if (fboundp 'calendar-check-holidays
)
18489 'calendar-check-holidays
'check-calendar-holidays
) date
)))
18490 (if hl
(mapconcat 'identity hl
"; "))))
18492 (defun org-diary-sexp-entry (sexp entry date
)
18493 "Process a SEXP diary ENTRY for DATE."
18494 (require 'diary-lib
)
18495 (let ((result (if calendar-debug-sexp
18496 (let ((stack-trace-on-error t
))
18497 (eval (car (read-from-string sexp
))))
18498 (condition-case nil
18499 (eval (car (read-from-string sexp
)))
18502 (message "Bad sexp at line %d in %s: %s"
18504 (buffer-file-name) sexp
)
18506 (cond ((stringp result
) result
)
18507 ((and (consp result
)
18508 (stringp (cdr result
))) (cdr result
))
18512 (defun org-diary-to-ical-string (frombuf)
18513 "Get iCalendar entries from diary entries in buffer FROMBUF.
18514 This uses the icalendar.el library."
18515 (let* ((tmpdir (if (featurep 'xemacs
)
18517 temporary-file-directory
))
18518 (tmpfile (make-temp-name
18519 (expand-file-name "orgics" tmpdir
)))
18522 (set-buffer frombuf
)
18523 (icalendar-export-region (point-min) (point-max) tmpfile
)
18524 (setq buf
(find-buffer-visiting tmpfile
))
18526 (goto-char (point-min))
18527 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
18528 (setq b
(match-beginning 0)))
18529 (goto-char (point-max))
18530 (if (re-search-backward "^END:VEVENT" nil t
)
18531 (setq e
(match-end 0)))
18532 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
18534 (kill-buffer frombuf
)
18535 (delete-file tmpfile
)
18538 (defun org-closest-date (start current change prefer
)
18539 "Find the date closest to CURRENT that is consistent with START and CHANGE.
18540 When PREFER is `past' return a date that is either CURRENT or past.
18541 When PREFER is `future', return a date that is either CURRENT or future."
18542 ;; Make the proper lists from the dates
18544 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
18545 dn dw sday cday n1 n2
18546 d m y y1 y2 date1 date2 nmonths nm ny m2
)
18548 (setq start
(org-date-to-gregorian start
)
18549 current
(org-date-to-gregorian
18550 (if org-agenda-repeating-timestamp-show-all
18552 (time-to-days (current-time))))
18553 sday
(calendar-absolute-from-gregorian start
)
18554 cday
(calendar-absolute-from-gregorian current
))
18556 (if (<= cday sday
) (throw 'exit sday
))
18558 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
18559 (setq dn
(string-to-number (match-string 1 change
))
18560 dw
(cdr (assoc (match-string 2 change
) a1
)))
18561 (error "Invalid change specifyer: %s" change
))
18562 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
18565 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
18568 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
18569 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
18570 (setq date1
(list m d y1
)
18571 n1
(calendar-absolute-from-gregorian date1
)
18572 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
18573 n2
(calendar-absolute-from-gregorian date2
)))
18575 ;; approx number of month between the tow dates
18576 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
18577 ;; How often does dn fit in there?
18578 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
18579 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
18581 ny
(floor (/ m
12))
18584 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
18585 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
18586 (setq m2
(+ m dn
) y2 y
)
18587 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
18588 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
18590 (setq n1 n2 m m2 y y2
)
18591 (setq m2
(+ m dn
) y2 y
)
18592 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
18593 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
18595 (if org-agenda-repeating-timestamp-show-all
18597 ((eq prefer
'past
) n1
)
18598 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
18599 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
18601 ((eq prefer
'past
) n1
)
18602 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
18603 (t (if (= cday n1
) n1 n2
)))))))
18605 (defun org-date-to-gregorian (date)
18606 "Turn any specification of DATE into a gregorian date for the calendar."
18607 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
18608 ((and (listp date
) (= (length date
) 3)) date
)
18610 (setq date
(org-parse-time-string date
))
18611 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
18613 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
18615 (defun org-parse-time-string (s &optional nodefault
)
18616 "Parse the standard Org-mode time string.
18617 This should be a lot faster than the normal `parse-time-string'.
18618 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
18619 hour and minute fields will be nil if not given."
18620 (if (string-match org-ts-regexp0 s
)
18622 (if (or (match-beginning 8) (not nodefault
))
18623 (string-to-number (or (match-string 8 s
) "0")))
18624 (if (or (match-beginning 7) (not nodefault
))
18625 (string-to-number (or (match-string 7 s
) "0")))
18626 (string-to-number (match-string 4 s
))
18627 (string-to-number (match-string 3 s
))
18628 (string-to-number (match-string 2 s
))
18632 (defun org-timestamp-up (&optional arg
)
18633 "Increase the date item at the cursor by one.
18634 If the cursor is on the year, change the year. If it is on the month or
18635 the day, change that.
18636 With prefix ARG, change by that many units."
18638 (org-timestamp-change (prefix-numeric-value arg
)))
18640 (defun org-timestamp-down (&optional arg
)
18641 "Decrease the date item at the cursor by one.
18642 If the cursor is on the year, change the year. If it is on the month or
18643 the day, change that.
18644 With prefix ARG, change by that many units."
18646 (org-timestamp-change (- (prefix-numeric-value arg
))))
18648 (defun org-timestamp-up-day (&optional arg
)
18649 "Increase the date in the time stamp by one day.
18650 With prefix ARG, change that many days."
18652 (if (and (not (org-at-timestamp-p t
))
18653 (org-on-heading-p))
18655 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
18657 (defun org-timestamp-down-day (&optional arg
)
18658 "Decrease the date in the time stamp by one day.
18659 With prefix ARG, change that many days."
18661 (if (and (not (org-at-timestamp-p t
))
18662 (org-on-heading-p))
18664 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
18666 (defsubst org-pos-in-match-range
(pos n
)
18667 (and (match-beginning n
)
18668 (<= (match-beginning n
) pos
)
18669 (>= (match-end n
) pos
)))
18671 (defun org-at-timestamp-p (&optional inactive-ok
)
18672 "Determine if the cursor is in or at a timestamp."
18674 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
18676 (ans (or (looking-at tsr
)
18678 (skip-chars-backward "^[<\n\r\t")
18679 (if (> (point) (point-min)) (backward-char 1))
18680 (and (looking-at tsr
)
18681 (> (- (match-end 0) pos
) -
1))))))
18683 (boundp 'org-ts-what
)
18686 ((= pos
(match-beginning 0)) 'bracket
)
18687 ((= pos
(1- (match-end 0))) 'bracket
)
18688 ((org-pos-in-match-range pos
2) 'year
)
18689 ((org-pos-in-match-range pos
3) 'month
)
18690 ((org-pos-in-match-range pos
7) 'hour
)
18691 ((org-pos-in-match-range pos
8) 'minute
)
18692 ((or (org-pos-in-match-range pos
4)
18693 (org-pos-in-match-range pos
5)) 'day
)
18694 ((and (> pos
(or (match-end 8) (match-end 5)))
18695 (< pos
(match-end 0)))
18696 (- pos
(or (match-end 8) (match-end 5))))
18700 (defun org-toggle-timestamp-type ()
18701 "Toggle the type (<active> or [inactive]) of a time stamp."
18703 (when (org-at-timestamp-p t
)
18705 (goto-char (match-beginning 0))
18706 (insert (if (equal (char-after) ?
<) "[" "<")) (delete-char 1)
18707 (goto-char (1- (match-end 0)))
18708 (insert (if (equal (char-after) ?
>) "]" ">")) (delete-char 1))
18709 (message "Timestamp is now %sactive"
18710 (if (equal (char-before) ?
>) "in" ""))))
18712 (defun org-timestamp-change (n &optional what
)
18713 "Change the date in the time stamp at point.
18714 The date will be changed by N times WHAT. WHAT can be `day', `month',
18715 `year', `minute', `second'. If WHAT is not given, the cursor position
18716 in the timestamp determines what will be changed."
18717 (let ((pos (point))
18719 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
18723 (if (not (org-at-timestamp-p t
))
18724 (error "Not at a timestamp"))
18725 (if (and (not what
) (eq org-ts-what
'bracket
))
18726 (org-toggle-timestamp-type)
18727 (if (and (not what
) (not (eq org-ts-what
'day
))
18728 org-display-custom-times
18729 (get-text-property (point) 'display
)
18730 (not (get-text-property (1- (point)) 'display
)))
18731 (setq org-ts-what
'day
))
18732 (setq org-ts-what
(or what org-ts-what
)
18733 inactive
(= (char-after (match-beginning 0)) ?\
[)
18734 ts
(match-string 0))
18737 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
18739 (setq extra
(match-string 1 ts
)))
18740 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
18742 (setq time0
(org-parse-time-string ts
))
18743 (when (and (eq org-ts-what
'minute
)
18744 (eq current-prefix-arg nil
))
18745 (setq n
(* dm
(org-no-warnings (signum n
))))
18746 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
18747 (setcar (cdr time0
) (+ (nth 1 time0
)
18748 (if (> n
0) (- rem
) (- dm rem
))))))
18750 (encode-time (or (car time0
) 0)
18751 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
18752 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
18753 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
18754 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
18755 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
18757 (when (integerp org-ts-what
)
18758 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
18759 (if (eq what
'calendar
)
18760 (let ((cal-date (org-get-date-from-calendar)))
18761 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
18762 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
18763 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
18764 (setcar time0
(or (car time0
) 0))
18765 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
18766 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
18767 (setq time
(apply 'encode-time time0
))))
18768 (setq org-last-changed-timestamp
18769 (org-insert-time-stamp time with-hm inactive nil nil extra
))
18770 (org-clock-update-time-maybe)
18772 ;; Try to recenter the calendar window, if any
18773 (if (and org-calendar-follow-timestamp-change
18774 (get-buffer-window "*Calendar*" t
)
18775 (memq org-ts-what
'(day month year
)))
18776 (org-recenter-calendar (time-to-days time
))))))
18778 ;; FIXME: does not yet work for lead times
18779 (defun org-modify-ts-extra (s pos n dm
)
18780 "Change the different parts of the lead-time and repeat fields in timestamp."
18781 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
18783 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
18785 ((or (org-pos-in-match-range pos
2)
18786 (org-pos-in-match-range pos
3))
18787 (setq m
(string-to-number (match-string 3 s
))
18788 h
(string-to-number (match-string 2 s
)))
18789 (if (org-pos-in-match-range pos
2)
18791 (setq n
(* dm
(org-no-warnings (signum n
))))
18792 (when (not (= 0 (setq rem
(% m dm
))))
18793 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
18795 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
18796 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
18797 (setq h
(min 24 (max 0 h
)))
18798 (setq ng
1 new
(format "-%02d:%02d" h m
)))
18799 ((org-pos-in-match-range pos
6)
18800 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
18801 ((org-pos-in-match-range pos
5)
18802 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
18804 ((org-pos-in-match-range pos
9)
18805 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
18806 ((org-pos-in-match-range pos
8)
18807 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
18811 (substring s
0 (match-beginning ng
))
18813 (substring s
(match-end ng
))))))
18816 (defun org-recenter-calendar (date)
18817 "If the calendar is visible, recenter it to DATE."
18818 (let* ((win (selected-window))
18819 (cwin (get-buffer-window "*Calendar*" t
))
18820 (calendar-move-hook nil
))
18822 (select-window cwin
)
18823 (calendar-goto-date (if (listp date
) date
18824 (calendar-gregorian-from-absolute date
)))
18825 (select-window win
))))
18827 (defun org-goto-calendar (&optional arg
)
18828 "Go to the Emacs calendar at the current date.
18829 If there is a time stamp in the current line, go to that date.
18830 A prefix ARG can be used to force the current date."
18832 (let ((tsr org-ts-regexp
) diff
18833 (calendar-move-hook nil
)
18834 (view-calendar-holidays-initially nil
)
18835 (view-diary-entries-initially nil
))
18836 (if (or (org-at-timestamp-p)
18838 (beginning-of-line 1)
18839 (looking-at (concat ".*" tsr
))))
18840 (let ((d1 (time-to-days (current-time)))
18842 (org-time-string-to-time (match-string 1)))))
18843 (setq diff
(- d2 d1
))))
18845 (calendar-goto-today)
18846 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
18848 (defun org-get-date-from-calendar ()
18849 "Return a list (month day year) of date at point in calendar."
18850 (with-current-buffer "*Calendar*"
18852 (calendar-cursor-to-date))))
18854 (defun org-date-from-calendar ()
18855 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
18856 If there is already a time stamp at the cursor position, update it."
18858 (if (org-at-timestamp-p t
)
18859 (org-timestamp-change 0 'calendar
)
18860 (let ((cal-date (org-get-date-from-calendar)))
18861 (org-insert-time-stamp
18862 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
18864 (defvar appt-time-msg-list
)
18867 (defun org-agenda-to-appt (&optional refresh filter
)
18868 "Activate appointments found in `org-agenda-files'.
18869 With a \\[universal-argument] prefix, refresh the list of
18872 If FILTER is t, interactively prompt the user for a regular
18873 expression, and filter out entries that don't match it.
18875 If FILTER is a string, use this string as a regular expression
18876 for filtering entries out.
18878 FILTER can also be an alist with the car of each cell being
18879 either 'headline or 'category. For example:
18881 '((headline \"IMPORTANT\")
18882 (category \"Work\"))
18884 will only add headlines containing IMPORTANT or headlines
18885 belonging to the \"Work\" category."
18887 (require 'calendar
)
18888 (if refresh
(setq appt-time-msg-list nil
))
18890 (setq filter
(read-from-minibuffer "Regexp filter: ")))
18891 (let* ((cnt 0) ; count added events
18892 (org-agenda-new-buffers nil
)
18893 (org-deadline-warning-days 0)
18894 (today (org-date-to-gregorian
18895 (time-to-days (current-time))))
18896 (files (org-agenda-files)) entries file
)
18897 ;; Get all entries which may contain an appt
18898 (while (setq file
(pop files
))
18901 (org-agenda-get-day-entries
18902 file today
:timestamp
:scheduled
:deadline
))))
18903 (setq entries
(delq nil entries
))
18904 ;; Map thru entries and find if we should filter them out
18907 (let* ((evt (org-trim (get-text-property 1 'txt x
)))
18908 (cat (get-text-property 1 'org-category x
))
18909 (tod (get-text-property 1 'time-of-day x
))
18910 (ok (or (null filter
)
18911 (and (stringp filter
) (string-match filter evt
))
18912 (and (listp filter
)
18914 (cadr (assoc 'category filter
)) cat
)
18916 (cadr (assoc 'headline filter
)) evt
))))))
18917 ;; FIXME: Shall we remove text-properties for the appt text?
18918 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
18920 (setq tod
(number-to-string tod
)
18921 tod
(when (string-match
18922 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod
)
18923 (concat (match-string 1 tod
) ":"
18924 (match-string 2 tod
))))
18926 (setq cnt
(1+ cnt
))))) entries
)
18927 (org-release-buffers org-agenda-new-buffers
)
18929 (message "No event to add")
18930 (message "Added %d event%s for today" cnt
(if (> cnt
1) "s" "")))))
18932 ;;; The clock for measuring work time.
18934 (defvar org-mode-line-string
"")
18935 (put 'org-mode-line-string
'risky-local-variable t
)
18937 (defvar org-mode-line-timer nil
)
18938 (defvar org-clock-heading
"")
18939 (defvar org-clock-start-time
"")
18941 (defun org-update-mode-line ()
18942 (let* ((delta (- (time-to-seconds (current-time))
18943 (time-to-seconds org-clock-start-time
)))
18944 (h (floor delta
3600))
18945 (m (floor (- delta
(* 3600 h
)) 60)))
18946 (setq org-mode-line-string
18947 (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading
)
18948 'help-echo
"Org-mode clock is running"))
18949 (force-mode-line-update)))
18951 (defvar org-clock-marker
(make-marker)
18952 "Marker recording the last clock-in.")
18953 (defvar org-clock-mode-line-entry nil
18954 "Information for the modeline about the running clock.")
18956 (defun org-clock-in ()
18957 "Start the clock on the current item.
18958 If necessary, clock-out of the currently active clock."
18963 (org-back-to-heading t
)
18964 (when (and org-clock-in-switch-to-state
18965 (not (looking-at (concat outline-regexp
"[ \t]*"
18966 org-clock-in-switch-to-state
18968 (org-todo org-clock-in-switch-to-state
))
18969 (if (and org-clock-heading-function
18970 (functionp org-clock-heading-function
))
18971 (setq org-clock-heading
(funcall org-clock-heading-function
))
18972 (if (looking-at org-complex-heading-regexp
)
18973 (setq org-clock-heading
(match-string 4))
18974 (setq org-clock-heading
"???")))
18975 (setq org-clock-heading
(propertize org-clock-heading
'face nil
))
18976 (org-clock-find-position)
18978 (insert "\n") (backward-char 1)
18980 (insert org-clock-string
" ")
18981 (setq org-clock-start-time
(current-time))
18982 (setq ts
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
18983 (move-marker org-clock-marker
(point) (buffer-base-buffer))
18984 (or global-mode-string
(setq global-mode-string
'("")))
18985 (or (memq 'org-mode-line-string global-mode-string
)
18986 (setq global-mode-string
18987 (append global-mode-string
'(org-mode-line-string))))
18988 (org-update-mode-line)
18989 (setq org-mode-line-timer
(run-with-timer 60 60 'org-update-mode-line
))
18990 (message "Clock started at %s" ts
))))
18992 (defun org-clock-find-position ()
18993 "Find the location where the next clock line should be inserted."
18994 (org-back-to-heading t
)
18996 (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
18997 (re (concat "^[ \t]*" org-clock-string
))
19001 (when (eobp) (newline) (setq end
(max (point) end
)))
19002 (when (re-search-forward "^[ \t]*:CLOCK:" end t
)
19003 ;; we seem to have a CLOCK drawer, so go there.
19004 (beginning-of-line 2)
19006 ;; Lets count the CLOCK lines
19008 (while (re-search-forward re end t
)
19009 (setq first
(or first
(match-beginning 0))
19010 last
(match-beginning 0)
19012 (when (and (integerp org-clock-into-drawer
)
19013 (>= (1+ cnt
) org-clock-into-drawer
))
19014 ;; Wrap current entries into a new drawer
19016 (beginning-of-line 2)
19017 (if (org-at-item-p) (org-end-of-item))
19019 (beginning-of-line 0)
19020 (org-indent-line-function)
19022 (insert ":CLOCK:\n")
19023 (beginning-of-line 0)
19024 (org-indent-line-function)
19025 (org-flag-drawer t
)
19026 (beginning-of-line 2)
19030 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
19031 (not (equal (match-string 1) org-clock-string
)))
19032 ;; Planning info, skip to after it
19033 (beginning-of-line 2)
19034 (or (bolp) (newline)))
19035 (when (eq t org-clock-into-drawer
)
19036 (insert ":CLOCK:\n:END:\n")
19037 (beginning-of-line -
1)
19038 (org-indent-line-function)
19039 (org-flag-drawer t
)
19040 (beginning-of-line 2)
19041 (org-indent-line-function)))))
19043 (defun org-clock-out (&optional fail-quietly
)
19044 "Stop the currently running clock.
19045 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
19048 (if (not (marker-buffer org-clock-marker
))
19049 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
19052 (set-buffer (marker-buffer org-clock-marker
))
19053 (goto-char org-clock-marker
)
19054 (beginning-of-line 1)
19055 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
19056 (equal (match-string 1) org-clock-string
))
19057 (setq ts
(match-string 2))
19058 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
19059 (goto-char (match-end 0))
19060 (delete-region (point) (point-at-eol))
19062 (setq te
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
19063 (setq s
(- (time-to-seconds (apply 'encode-time
(org-parse-time-string te
)))
19064 (time-to-seconds (apply 'encode-time
(org-parse-time-string ts
))))
19065 h
(floor (/ s
3600))
19069 (insert " => " (format "%2d:%02d" h m
))
19070 (move-marker org-clock-marker nil
)
19071 (when org-log-note-clock-out
19072 (org-add-log-maybe 'clock-out
))
19073 (when org-mode-line-timer
19074 (cancel-timer org-mode-line-timer
)
19075 (setq org-mode-line-timer nil
))
19076 (setq global-mode-string
19077 (delq 'org-mode-line-string global-mode-string
))
19078 (force-mode-line-update)
19079 (message "Clock stopped at %s after HH:MM = %d:%02d" te h m
)))))
19081 (defun org-clock-cancel ()
19082 "Cancel the running clock be removing the start timestamp."
19084 (if (not (marker-buffer org-clock-marker
))
19085 (error "No active clock"))
19087 (set-buffer (marker-buffer org-clock-marker
))
19088 (goto-char org-clock-marker
)
19089 (delete-region (1- (point-at-bol)) (point-at-eol)))
19090 (setq global-mode-string
19091 (delq 'org-mode-line-string global-mode-string
))
19092 (force-mode-line-update)
19093 (message "Clock canceled"))
19095 (defun org-clock-goto (&optional delete-windows
)
19096 "Go to the currently clocked-in entry."
19098 (if (not (marker-buffer org-clock-marker
))
19099 (error "No active clock"))
19100 (switch-to-buffer-other-window
19101 (marker-buffer org-clock-marker
))
19102 (if delete-windows
(delete-other-windows))
19103 (goto-char org-clock-marker
)
19105 (org-back-to-heading)
19108 (defvar org-clock-file-total-minutes nil
19109 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
19110 (make-variable-buffer-local 'org-clock-file-total-minutes
)
19112 (defun org-clock-sum (&optional tstart tend
)
19113 "Sum the times for each subtree.
19114 Puts the resulting times in minutes as a text property on each headline."
19116 (let* ((bmp (buffer-modified-p))
19117 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
19119 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
19121 (ltimes (make-vector lmax
0))
19126 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
19128 (goto-char (point-max))
19129 (while (re-search-backward re nil t
)
19133 (setq ts
(match-string 2)
19134 te
(match-string 3)
19135 ts
(time-to-seconds
19136 (apply 'encode-time
(org-parse-time-string ts
)))
19137 te
(time-to-seconds
19138 (apply 'encode-time
(org-parse-time-string te
)))
19139 ts
(if tstart
(max ts tstart
) ts
)
19140 te
(if tend
(min te tend
) te
)
19142 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
19145 (setq t1
(+ t1
(string-to-number (match-string 5))
19146 (* 60 (string-to-number (match-string 4))))))
19148 (setq level
(- (match-end 1) (match-beginning 1)))
19149 (when (or (> t1
0) (> (aref ltimes level
) 0))
19150 (loop for l from
0 to level do
19151 (aset ltimes l
(+ (aref ltimes l
) t1
)))
19152 (setq t1
0 time
(aref ltimes level
))
19153 (loop for l from level to
(1- lmax
) do
19155 (goto-char (match-beginning 0))
19156 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
19157 (setq org-clock-file-total-minutes
(aref ltimes
0)))
19158 (set-buffer-modified-p bmp
)))
19160 (defun org-clock-display (&optional total-only
)
19161 "Show subtree times in the entire buffer.
19162 If TOTAL-ONLY is non-nil, only show the total time for the entire file
19165 (org-remove-clock-overlays)
19170 (goto-char (point-min))
19171 (while (or (and (equal (setq p
(point)) (point-min))
19172 (get-text-property p
:org-clock-minutes
))
19173 (setq p
(next-single-property-change
19174 (point) :org-clock-minutes
)))
19176 (when (setq time
(get-text-property p
:org-clock-minutes
))
19177 (org-put-clock-overlay time
(funcall outline-level
))))
19178 (setq h
(/ org-clock-file-total-minutes
60)
19179 m
(- org-clock-file-total-minutes
(* 60 h
)))
19180 ;; Arrange to remove the overlays upon next change.
19181 (when org-remove-highlights-with-change
19182 (org-add-hook 'before-change-functions
'org-remove-clock-overlays
19184 (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m
)))
19186 (defvar org-clock-overlays nil
)
19187 (make-variable-buffer-local 'org-clock-overlays
)
19189 (defun org-put-clock-overlay (time &optional level
)
19190 "Put an overlays on the current line, displaying TIME.
19191 If LEVEL is given, prefix time with a corresponding number of stars.
19192 This creates a new overlay and stores it in `org-clock-overlays', so that it
19193 will be easy to remove."
19194 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
19195 (l (if level
(org-get-valid-level level
0) 0))
19199 (unless (eolp) (skip-chars-backward "^ \t"))
19200 (skip-chars-backward " \t")
19201 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
19202 tx
(concat (buffer-substring (1- (point)) (point))
19203 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
19204 (org-add-props (format "%s %2d:%02d%s"
19205 (make-string l ?
*) h m
19206 (make-string (- 16 l
) ?\
))
19207 '(face secondary-selection
))
19209 (if (not (featurep 'xemacs
))
19210 (org-overlay-put ov
'display tx
)
19211 (org-overlay-put ov
'invisible t
)
19212 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
19213 (push ov org-clock-overlays
)))
19215 (defun org-remove-clock-overlays (&optional beg end noremove
)
19216 "Remove the occur highlights from the buffer.
19217 BEG and END are ignored. If NOREMOVE is nil, remove this function
19218 from the `before-change-functions' in the current buffer."
19220 (unless org-inhibit-highlight-removal
19221 (mapc 'org-delete-overlay org-clock-overlays
)
19222 (setq org-clock-overlays nil
)
19224 (remove-hook 'before-change-functions
19225 'org-remove-clock-overlays
'local
))))
19227 (defun org-clock-out-if-current ()
19228 "Clock out if the current entry contains the running clock.
19229 This is used to stop the clock after a TODO entry is marked DONE,
19230 and is only done if the variable `org-clock-out-when-done' is not nil."
19231 (when (and org-clock-out-when-done
19232 (member state org-done-keywords
)
19233 (equal (marker-buffer org-clock-marker
) (current-buffer))
19234 (< (point) org-clock-marker
)
19235 (> (save-excursion (outline-next-heading) (point))
19237 ;; Clock out, but don't accept a logging message for this.
19238 (let ((org-log-note-clock-out nil
))
19241 (add-hook 'org-after-todo-state-change-hook
19242 'org-clock-out-if-current
)
19244 (defun org-check-running-clock ()
19245 "Check if the current buffer contains the running clock.
19246 If yes, offer to stop it and to save the buffer with the changes."
19247 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
19248 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
19251 (when (y-or-n-p "Save changed buffer?")
19254 (defun org-clock-report (&optional arg
)
19255 "Create a table containing a report about clocked time.
19256 If the cursor is inside an existing clocktable block, then the table
19257 will be updated. If not, a new clocktable will be inserted.
19258 When called with a prefix argument, move to the first clock table in the
19259 buffer and update it."
19261 (org-remove-clock-overlays)
19263 (org-find-dblock "clocktable")
19265 (if (org-in-clocktable-p)
19266 (goto-char (org-in-clocktable-p))
19267 (org-create-dblock (list :name
"clocktable"
19268 :maxlevel
2 :scope
'file
)))
19269 (org-update-dblock))
19271 (defun org-in-clocktable-p ()
19272 "Check if the cursor is in a clocktable."
19273 (let ((pos (point)) start
)
19276 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
19277 (setq start
(match-beginning 0))
19278 (re-search-forward "^#\\+END:.*" nil t
)
19279 (>= (match-end 0) pos
)
19282 (defun org-clock-update-time-maybe ()
19283 "If this is a CLOCK line, update it and return t.
19284 Otherwise, return nil."
19287 (beginning-of-line 1)
19288 (skip-chars-forward " \t")
19289 (when (looking-at org-clock-string
)
19290 (let ((re (concat "[ \t]*" org-clock-string
19291 " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
19292 "\\([ \t]*=>.*\\)?"))
19294 (if (not (looking-at re
))
19296 (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
19298 (setq ts
(match-string 1)
19299 te
(match-string 2))
19300 (setq s
(- (time-to-seconds
19301 (apply 'encode-time
(org-parse-time-string te
)))
19303 (apply 'encode-time
(org-parse-time-string ts
))))
19304 h
(floor (/ s
3600))
19308 (insert " => " (format "%2d:%02d" h m
))
19311 (defun org-clock-special-range (key &optional time as-strings
)
19312 "Return two times bordering a special time range.
19313 Key is a symbol specifying the range and can be one of `today', `yesterday',
19314 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
19315 A week starts Monday 0:00 and ends Sunday 24:00.
19316 The range is determined relative to TIME. TIME defaults to the current time.
19317 The return value is a cons cell with two internal times like the ones
19318 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
19319 the returned times will be formatted strings."
19320 (let* ((tm (decode-time (or time
(current-time))))
19321 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
19322 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
19324 s1 m1 h1 d1 month1 y1 diff ts te fm
)
19327 (setq h
0 m
0 h1
24 m1
0))
19328 ((eq key
'yesterday
)
19329 (setq d
(1- d
) h
0 m
0 h1
24 m1
0))
19330 ((eq key
'thisweek
)
19331 (setq diff
(if (= dow
0) 6 (1- dow
))
19332 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
19333 ((eq key
'lastweek
)
19334 (setq diff
(+ 7 (if (= dow
0) 6 (1- dow
)))
19335 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
19336 ((eq key
'thismonth
)
19337 (setq d
1 h
0 m
0 d1
1 month1
(1+ month
) h1
0 m1
0))
19338 ((eq key
'lastmonth
)
19339 (setq d
1 h
0 m
0 d1
1 month
(1- month
) month1
(1+ month
) h1
0 m1
0))
19340 ((eq key
'thisyear
)
19341 (setq m
0 h
0 d
1 month
1 y1
(1+ y
)))
19342 ((eq key
'lastyear
)
19343 (setq m
0 h
0 d
1 month
1 y
(1- y
) y1
(1+ y
)))
19344 (t (error "No such time block %s" key
)))
19345 (setq ts
(encode-time s m h d month y
)
19346 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
19347 (or d1 d
) (or month1 month
) (or y1 y
)))
19348 (setq fm
(cdr org-time-stamp-formats
))
19350 (cons (format-time-string fm ts
) (format-time-string fm te
))
19353 (defun org-dblock-write:clocktable
(params)
19354 "Write the standard clocktable."
19356 (let* ((hlchars '((1 .
"*") (2 .
"/")))
19357 (ins (make-marker))
19359 (scope (plist-get params
:scope
))
19360 (tostring (plist-get params
:tostring
))
19361 (multifile (plist-get params
:multifile
))
19362 (header (plist-get params
:header
))
19363 (maxlevel (or (plist-get params
:maxlevel
) 3))
19364 (step (plist-get params
:step
))
19365 (emph (plist-get params
:emphasize
))
19366 (ts (plist-get params
:tstart
))
19367 (te (plist-get params
:tend
))
19368 (block (plist-get params
:block
))
19369 (link (plist-get params
:link
))
19370 ipos time h m p level hlc hdl
19371 cc beg end pos tbl
)
19373 (org-clocktable-steps params
)
19376 (setq cc
(org-clock-special-range block nil t
)
19377 ts
(car cc
) te
(cdr cc
)))
19378 (if ts
(setq ts
(time-to-seconds
19379 (apply 'encode-time
(org-parse-time-string ts
)))))
19380 (if te
(setq te
(time-to-seconds
19381 (apply 'encode-time
(org-parse-time-string te
)))))
19382 (move-marker ins
(point))
19383 (setq ipos
(point))
19385 ;; Get the right scope
19390 ((eq scope
'file
) (widen))
19391 ((eq scope
'subtree
) (org-narrow-to-subtree))
19393 (while (org-up-heading-safe))
19394 (org-narrow-to-subtree))
19395 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
19396 (symbol-name scope
)))
19397 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
19399 (while (org-up-heading-safe)
19400 (looking-at outline-regexp
)
19401 (if (<= (org-reduced-level (funcall outline-level
)) level
)
19402 (throw 'exit nil
))))
19403 (org-narrow-to-subtree))
19404 ((or (listp scope
) (eq scope
'agenda
))
19405 (let* ((files (if (listp scope
) scope
(org-agenda-files)))
19407 (p1 (copy-sequence params
))
19409 (plist-put p1
:tostring t
)
19410 (plist-put p1
:multifile t
)
19411 (plist-put p1
:scope
'file
)
19412 (org-prepare-agenda-buffers files
)
19413 (while (setq file
(pop files
))
19414 (with-current-buffer (find-buffer-visiting file
)
19415 (push (org-clocktable-add-file
19416 file
(org-dblock-write:clocktable p1
)) tbl
)
19417 (setq total-time
(+ (or total-time
0)
19418 org-clock-file-total-minutes
)))))))
19421 (unless (eq scope
'agenda
)
19422 (org-clock-sum ts te
)
19423 (goto-char (point-min))
19424 (while (setq p
(next-single-property-change (point) :org-clock-minutes
))
19426 (when (setq time
(get-text-property p
:org-clock-minutes
))
19428 (beginning-of-line 1)
19429 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
19430 (setq level
(org-reduced-level
19431 (- (match-end 1) (match-beginning 1))))
19432 (<= level maxlevel
))
19433 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
19436 (org-make-link-string
19437 (format "file:%s::%s"
19440 (org-make-org-heading-search-string
19441 (match-string 2))))
19444 m
(- time
(* 60 h
)))
19445 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
19447 "| " (int-to-string level
) "|" hlc hdl hlc
" |"
19448 (make-string (1- level
) ?|
)
19449 hlc
(format "%d:%02d" h m
) hlc
19451 (setq tbl
(nreverse tbl
))
19453 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
19455 (insert-before-markers
19458 "Clock summary at ["
19460 (format-time-string (cdr org-time-stamp-formats
))
19464 (format " Considered range is /%s/." block
)
19467 (if (eq scope
'agenda
) "|File" "")
19468 "|L|Headline|Time|\n")
19469 (setq total-time
(or total-time org-clock-file-total-minutes
)
19470 h
(/ total-time
60)
19471 m
(- total-time
(* 60 h
)))
19472 (insert-before-markers
19474 (if (eq scope
'agenda
) "|" "")
19477 (format "*%d:%02d*" h m
)
19479 (setq tbl
(delq nil tbl
))
19480 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
19481 (equal (substring (car tbl
) 0 2) "|-"))
19483 (insert-before-markers (mapconcat
19484 'identity
(delq nil tbl
)
19485 (if (eq scope
'agenda
) "\n|-\n" "\n")))
19486 (backward-delete-char 1)
19488 (skip-chars-forward "^|")
19489 (org-table-align))))))
19491 (defun org-clocktable-steps (params)
19492 (let* ((p1 (copy-sequence params
))
19493 (ts (plist-get p1
:tstart
))
19494 (te (plist-get p1
:tend
))
19495 (step0 (plist-get p1
:step
))
19496 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
19497 (block (plist-get p1
:block
))
19500 (setq cc
(org-clock-special-range block nil t
)
19501 ts
(car cc
) te
(cdr cc
)))
19502 (if ts
(setq ts
(time-to-seconds
19503 (apply 'encode-time
(org-parse-time-string ts
)))))
19504 (if te
(setq te
(time-to-seconds
19505 (apply 'encode-time
(org-parse-time-string te
)))))
19506 (plist-put p1
:header
"")
19507 (plist-put p1
:step nil
)
19508 (plist-put p1
:block nil
)
19510 (or (bolp) (insert "\n"))
19511 (plist-put p1
:tstart
(format-time-string
19512 (car org-time-stamp-formats
)
19513 (seconds-to-time ts
)))
19514 (plist-put p1
:tend
(format-time-string
19515 (car org-time-stamp-formats
)
19516 (seconds-to-time (setq ts
(+ ts step
)))))
19517 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
19518 (plist-get p1
:tstart
) "\n")
19519 (org-dblock-write:clocktable p1
)
19520 (re-search-forward "#\\+END:")
19524 (defun org-clocktable-add-file (file table
)
19526 (let ((lines (org-split-string table
"\n"))
19527 (ff (file-name-nondirectory file
)))
19528 (mapconcat 'identity
19529 (mapcar (lambda (x)
19530 (if (string-match org-table-dataline-regexp x
)
19536 ;; FIXME: I don't think anybody uses this, ask David
19537 (defun org-collect-clock-time-entries ()
19538 "Return an internal list with clocking information.
19539 This list has one entry for each CLOCK interval.
19540 FIXME: describe the elements."
19542 (let ((re (concat "^[ \t]*" org-clock-string
19543 " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
19544 rtn beg end next cont level title total closedp leafp
19545 clockpos titlepos h m donep
)
19548 (goto-char (point-min))
19549 (while (re-search-forward re nil t
)
19550 (setq clockpos
(match-beginning 0)
19551 beg
(match-string 1) end
(match-string 2)
19552 cont
(match-end 0))
19553 (setq beg
(apply 'encode-time
(org-parse-time-string beg
))
19554 end
(apply 'encode-time
(org-parse-time-string end
)))
19555 (org-back-to-heading t
)
19556 (setq donep
(org-entry-is-done-p))
19557 (setq titlepos
(point)
19558 total
(or (get-text-property (1+ (point)) :org-clock-minutes
) 0)
19559 h
(/ total
60) m
(- total
(* 60 h
))
19561 (looking-at "\\(\\*+\\) +\\(.*\\)")
19562 (setq level
(- (match-end 1) (match-beginning 1))
19563 title
(org-match-string-no-properties 2))
19564 (save-excursion (outline-next-heading) (setq next
(point)))
19565 (setq closedp
(re-search-forward org-closed-time-regexp next t
))
19567 (setq leafp
(and (looking-at "^\\*+ ")
19568 (<= (- (match-end 0) (point)) level
)))
19569 (push (list beg end clockpos closedp donep
19570 total title titlepos level leafp
)
19575 ;;;; Agenda, and Diary Integration
19577 ;;; Define the Org-agenda-mode
19579 (defvar org-agenda-mode-map
(make-sparse-keymap)
19580 "Keymap for `org-agenda-mode'.")
19582 (defvar org-agenda-menu
) ; defined later in this file.
19583 (defvar org-agenda-follow-mode nil
)
19584 (defvar org-agenda-show-log nil
)
19585 (defvar org-agenda-redo-command nil
)
19586 (defvar org-agenda-query-string nil
)
19587 (defvar org-agenda-mode-hook nil
)
19588 (defvar org-agenda-type nil
)
19589 (defvar org-agenda-force-single-file nil
)
19591 (defun org-agenda-mode ()
19592 "Mode for time-sorted view on action items in Org-mode files.
19594 The following commands are available:
19596 \\{org-agenda-mode-map}"
19598 (kill-all-local-variables)
19599 (setq org-agenda-undo-list nil
19600 org-agenda-pending-undo-list nil
)
19601 (setq major-mode
'org-agenda-mode
)
19602 ;; Keep global-font-lock-mode from turning on font-lock-mode
19603 (org-set-local 'font-lock-global-modes
(list 'not major-mode
))
19604 (setq mode-name
"Org-Agenda")
19605 (use-local-map org-agenda-mode-map
)
19606 (easy-menu-add org-agenda-menu
)
19607 (if org-startup-truncated
(setq truncate-lines t
))
19608 (org-add-hook 'post-command-hook
'org-agenda-post-command-hook nil
'local
)
19609 (org-add-hook 'pre-command-hook
'org-unhighlight nil
'local
)
19610 ;; Make sure properties are removed when copying text
19611 (when (boundp 'buffer-substring-filters
)
19612 (org-set-local 'buffer-substring-filters
19614 (set-text-properties 0 (length x
) nil x
) x
)
19615 buffer-substring-filters
)))
19616 (unless org-agenda-keep-modes
19617 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
19618 org-agenda-show-log nil
))
19620 '("Agenda") "Agenda Files"
19624 (if (get 'org-agenda-files
'org-restrict
)
19625 "Restricted to single file"
19627 '(org-edit-agenda-file-list)
19628 (not (get 'org-agenda-files
'org-restrict
)))
19630 (mapcar 'org-file-menu-entry
(org-agenda-files))))
19631 (org-agenda-set-mode-name)
19633 (if (fboundp 'run-mode-hooks
) 'run-mode-hooks
'run-hooks
)
19634 (list 'org-agenda-mode-hook
)))
19636 (substitute-key-definition 'undo
'org-agenda-undo
19637 org-agenda-mode-map global-map
)
19638 (org-defkey org-agenda-mode-map
"\C-i" 'org-agenda-goto
)
19639 (org-defkey org-agenda-mode-map
[(tab)] 'org-agenda-goto
)
19640 (org-defkey org-agenda-mode-map
"\C-m" 'org-agenda-switch-to
)
19641 (org-defkey org-agenda-mode-map
"\C-k" 'org-agenda-kill
)
19642 (org-defkey org-agenda-mode-map
"\C-c$" 'org-agenda-archive
)
19643 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-s" 'org-agenda-archive
)
19644 (org-defkey org-agenda-mode-map
"$" 'org-agenda-archive
)
19645 (org-defkey org-agenda-mode-map
"\C-c\C-o" 'org-agenda-open-link
)
19646 (org-defkey org-agenda-mode-map
" " 'org-agenda-show
)
19647 (org-defkey org-agenda-mode-map
"\C-c\C-t" 'org-agenda-todo
)
19648 (org-defkey org-agenda-mode-map
[(control shift right
)] 'org-agenda-todo-nextset
)
19649 (org-defkey org-agenda-mode-map
[(control shift left
)] 'org-agenda-todo-previousset
)
19650 (org-defkey org-agenda-mode-map
"\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer
)
19651 (org-defkey org-agenda-mode-map
"b" 'org-agenda-tree-to-indirect-buffer
)
19652 (org-defkey org-agenda-mode-map
"o" 'delete-other-windows
)
19653 (org-defkey org-agenda-mode-map
"L" 'org-agenda-recenter
)
19654 (org-defkey org-agenda-mode-map
"t" 'org-agenda-todo
)
19655 (org-defkey org-agenda-mode-map
"a" 'org-agenda-toggle-archive-tag
)
19656 (org-defkey org-agenda-mode-map
":" 'org-agenda-set-tags
)
19657 (org-defkey org-agenda-mode-map
"." 'org-agenda-goto-today
)
19658 (org-defkey org-agenda-mode-map
"j" 'org-agenda-goto-date
)
19659 (org-defkey org-agenda-mode-map
"d" 'org-agenda-day-view
)
19660 (org-defkey org-agenda-mode-map
"w" 'org-agenda-week-view
)
19661 (org-defkey org-agenda-mode-map
"m" 'org-agenda-month-view
)
19662 (org-defkey org-agenda-mode-map
"y" 'org-agenda-year-view
)
19663 (org-defkey org-agenda-mode-map
[(shift right
)] 'org-agenda-date-later
)
19664 (org-defkey org-agenda-mode-map
[(shift left
)] 'org-agenda-date-earlier
)
19665 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(right)] 'org-agenda-date-later
)
19666 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(left)] 'org-agenda-date-earlier
)
19668 (org-defkey org-agenda-mode-map
">" 'org-agenda-date-prompt
)
19669 (org-defkey org-agenda-mode-map
"\C-c\C-s" 'org-agenda-schedule
)
19670 (org-defkey org-agenda-mode-map
"\C-c\C-d" 'org-agenda-deadline
)
19671 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
19672 (while l
(org-defkey org-agenda-mode-map
19673 (int-to-string (pop l
)) 'digit-argument
)))
19675 (org-defkey org-agenda-mode-map
"f" 'org-agenda-follow-mode
)
19676 (org-defkey org-agenda-mode-map
"l" 'org-agenda-log-mode
)
19677 (org-defkey org-agenda-mode-map
"D" 'org-agenda-toggle-diary
)
19678 (org-defkey org-agenda-mode-map
"G" 'org-agenda-toggle-time-grid
)
19679 (org-defkey org-agenda-mode-map
"r" 'org-agenda-redo
)
19680 (org-defkey org-agenda-mode-map
"g" 'org-agenda-redo
)
19681 (org-defkey org-agenda-mode-map
"e" 'org-agenda-execute
)
19682 (org-defkey org-agenda-mode-map
"q" 'org-agenda-quit
)
19683 (org-defkey org-agenda-mode-map
"x" 'org-agenda-exit
)
19684 (org-defkey org-agenda-mode-map
"\C-x\C-w" 'org-write-agenda
)
19685 (org-defkey org-agenda-mode-map
"s" 'org-save-all-org-buffers
)
19686 (org-defkey org-agenda-mode-map
"\C-x\C-s" 'org-save-all-org-buffers
)
19687 (org-defkey org-agenda-mode-map
"P" 'org-agenda-show-priority
)
19688 (org-defkey org-agenda-mode-map
"T" 'org-agenda-show-tags
)
19689 (org-defkey org-agenda-mode-map
"n" 'next-line
)
19690 (org-defkey org-agenda-mode-map
"p" 'previous-line
)
19691 (org-defkey org-agenda-mode-map
"\C-c\C-n" 'org-agenda-next-date-line
)
19692 (org-defkey org-agenda-mode-map
"\C-c\C-p" 'org-agenda-previous-date-line
)
19693 (org-defkey org-agenda-mode-map
"," 'org-agenda-priority
)
19694 (org-defkey org-agenda-mode-map
"\C-c," 'org-agenda-priority
)
19695 (org-defkey org-agenda-mode-map
"i" 'org-agenda-diary-entry
)
19696 (org-defkey org-agenda-mode-map
"c" 'org-agenda-goto-calendar
)
19697 (eval-after-load "calendar"
19698 '(org-defkey calendar-mode-map org-calendar-to-agenda-key
19699 'org-calendar-goto-agenda
))
19700 (org-defkey org-agenda-mode-map
"C" 'org-agenda-convert-date
)
19701 (org-defkey org-agenda-mode-map
"M" 'org-agenda-phases-of-moon
)
19702 (org-defkey org-agenda-mode-map
"S" 'org-agenda-sunrise-sunset
)
19703 (org-defkey org-agenda-mode-map
"h" 'org-agenda-holidays
)
19704 (org-defkey org-agenda-mode-map
"H" 'org-agenda-holidays
)
19705 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-i" 'org-agenda-clock-in
)
19706 (org-defkey org-agenda-mode-map
"I" 'org-agenda-clock-in
)
19707 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-o" 'org-agenda-clock-out
)
19708 (org-defkey org-agenda-mode-map
"O" 'org-agenda-clock-out
)
19709 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-x" 'org-agenda-clock-cancel
)
19710 (org-defkey org-agenda-mode-map
"X" 'org-agenda-clock-cancel
)
19711 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-j" 'org-clock-goto
)
19712 (org-defkey org-agenda-mode-map
"J" 'org-clock-goto
)
19713 (org-defkey org-agenda-mode-map
"+" 'org-agenda-priority-up
)
19714 (org-defkey org-agenda-mode-map
"-" 'org-agenda-priority-down
)
19715 (org-defkey org-agenda-mode-map
[(shift up
)] 'org-agenda-priority-up
)
19716 (org-defkey org-agenda-mode-map
[(shift down
)] 'org-agenda-priority-down
)
19717 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(up)] 'org-agenda-priority-up
)
19718 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(down)] 'org-agenda-priority-down
)
19719 (org-defkey org-agenda-mode-map
[(right)] 'org-agenda-later
)
19720 (org-defkey org-agenda-mode-map
[(left)] 'org-agenda-earlier
)
19721 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-c" 'org-agenda-columns
)
19723 (org-defkey org-agenda-mode-map
"[" 'org-agenda-manipulate-query-add
)
19724 (org-defkey org-agenda-mode-map
"]" 'org-agenda-manipulate-query-subtract
)
19725 (org-defkey org-agenda-mode-map
"{" 'org-agenda-manipulate-query-add-re
)
19726 (org-defkey org-agenda-mode-map
"}" 'org-agenda-manipulate-query-subtract-re
)
19728 (defvar org-agenda-keymap
(copy-keymap org-agenda-mode-map
)
19729 "Local keymap for agenda entries from Org-mode.")
19731 (org-defkey org-agenda-keymap
19732 (if (featurep 'xemacs
) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse
)
19733 (org-defkey org-agenda-keymap
19734 (if (featurep 'xemacs
) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse
)
19735 (when org-agenda-mouse-1-follows-link
19736 (org-defkey org-agenda-keymap
[follow-link
] 'mouse-face
))
19737 (easy-menu-define org-agenda-menu org-agenda-mode-map
"Agenda menu"
19741 ["Show" org-agenda-show t
]
19742 ["Go To (other window)" org-agenda-goto t
]
19743 ["Go To (this window)" org-agenda-switch-to t
]
19744 ["Follow Mode" org-agenda-follow-mode
19745 :style toggle
:selected org-agenda-follow-mode
:active t
]
19746 ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t
]
19748 ["Cycle TODO" org-agenda-todo t
]
19749 ["Archive subtree" org-agenda-archive t
]
19750 ["Delete subtree" org-agenda-kill t
]
19752 ["Goto Today" org-agenda-goto-today
(org-agenda-check-type nil
'agenda
'timeline
)]
19753 ["Next Dates" org-agenda-later
(org-agenda-check-type nil
'agenda
)]
19754 ["Previous Dates" org-agenda-earlier
(org-agenda-check-type nil
'agenda
)]
19755 ["Jump to date" org-agenda-goto-date
(org-agenda-check-type nil
'agenda
)]
19757 ("Tags and Properties"
19758 ["Show all Tags" org-agenda-show-tags t
]
19759 ["Set Tags current line" org-agenda-set-tags
(not (org-region-active-p))]
19760 ["Change tag in region" org-agenda-set-tags
(org-region-active-p)]
19762 ["Column View" org-columns t
])
19764 ["Schedule" org-agenda-schedule t
]
19765 ["Set Deadline" org-agenda-deadline t
]
19767 ["Change Date +1 day" org-agenda-date-later
(org-agenda-check-type nil
'agenda
'timeline
)]
19768 ["Change Date -1 day" org-agenda-date-earlier
(org-agenda-check-type nil
'agenda
'timeline
)]
19769 ["Change Date to ..." org-agenda-date-prompt
(org-agenda-check-type nil
'agenda
'timeline
)])
19771 ["Clock in" org-agenda-clock-in t
]
19772 ["Clock out" org-agenda-clock-out t
]
19773 ["Clock cancel" org-agenda-clock-cancel t
]
19774 ["Goto running clock" org-clock-goto t
])
19776 ["Set Priority" org-agenda-priority t
]
19777 ["Increase Priority" org-agenda-priority-up t
]
19778 ["Decrease Priority" org-agenda-priority-down t
]
19779 ["Show Priority" org-agenda-show-priority t
])
19781 ["New Diary Entry" org-agenda-diary-entry
(org-agenda-check-type nil
'agenda
'timeline
)]
19782 ["Goto Calendar" org-agenda-goto-calendar
(org-agenda-check-type nil
'agenda
'timeline
)]
19783 ["Phases of the Moon" org-agenda-phases-of-moon
(org-agenda-check-type nil
'agenda
'timeline
)]
19784 ["Sunrise/Sunset" org-agenda-sunrise-sunset
(org-agenda-check-type nil
'agenda
'timeline
)]
19785 ["Holidays" org-agenda-holidays
(org-agenda-check-type nil
'agenda
'timeline
)]
19786 ["Convert" org-agenda-convert-date
(org-agenda-check-type nil
'agenda
'timeline
)]
19788 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t
])
19791 ["Day View" org-agenda-day-view
:active
(org-agenda-check-type nil
'agenda
)
19792 :style radio
:selected
(equal org-agenda-ndays
1)]
19793 ["Week View" org-agenda-week-view
:active
(org-agenda-check-type nil
'agenda
)
19794 :style radio
:selected
(equal org-agenda-ndays
7)]
19795 ["Month View" org-agenda-month-view
:active
(org-agenda-check-type nil
'agenda
)
19796 :style radio
:selected
(member org-agenda-ndays
'(28 29 30 31))]
19797 ["Year View" org-agenda-year-view
:active
(org-agenda-check-type nil
'agenda
)
19798 :style radio
:selected
(member org-agenda-ndays
'(365 366))]
19800 ["Show Logbook entries" org-agenda-log-mode
19801 :style toggle
:selected org-agenda-show-log
:active
(org-agenda-check-type nil
'agenda
'timeline
)]
19802 ["Include Diary" org-agenda-toggle-diary
19803 :style toggle
:selected org-agenda-include-diary
:active
(org-agenda-check-type nil
'agenda
)]
19804 ["Use Time Grid" org-agenda-toggle-time-grid
19805 :style toggle
:selected org-agenda-use-time-grid
:active
(org-agenda-check-type nil
'agenda
)])
19806 ["Write view to file" org-write-agenda t
]
19807 ["Rebuild buffer" org-agenda-redo t
]
19808 ["Save all Org-mode Buffers" org-save-all-org-buffers t
]
19810 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list
]
19812 ["Quit" org-agenda-quit t
]
19813 ["Exit and Release Buffers" org-agenda-exit t
]
19818 (defvar org-agenda-allow-remote-undo t
19819 "Non-nil means, allow remote undo from the agenda buffer.")
19820 (defvar org-agenda-undo-list nil
19821 "List of undoable operations in the agenda since last refresh.")
19822 (defvar org-agenda-undo-has-started-in nil
19823 "Buffers that have already seen `undo-start' in the current undo sequence.")
19824 (defvar org-agenda-pending-undo-list nil
19825 "In a series of undo commands, this is the list of remaning undo items.")
19827 (defmacro org-if-unprotected
(&rest body
)
19828 "Execute BODY if there is no `org-protected' text property at point."
19829 (declare (debug t
))
19830 `(unless (get-text-property (point) 'org-protected
)
19833 (defmacro org-with-remote-undo
(_buffer &rest _body
)
19834 "Execute BODY while recording undo information in two buffers."
19835 (declare (indent 1) (debug t
))
19836 `(let ((_cline (org-current-line))
19837 (_cmd this-command
)
19838 (_buf1 (current-buffer))
19840 (_undo1 buffer-undo-list
)
19841 (_undo2 (with-current-buffer ,_buffer buffer-undo-list
))
19844 (when org-agenda-allow-remote-undo
19845 (setq _c1
(org-verify-change-for-undo
19846 _undo1
(with-current-buffer _buf1 buffer-undo-list
))
19847 _c2
(org-verify-change-for-undo
19848 _undo2
(with-current-buffer _buf2 buffer-undo-list
)))
19850 ;; make sure there are undo boundaries
19851 (and _c1
(with-current-buffer _buf1
(undo-boundary)))
19852 (and _c2
(with-current-buffer _buf2
(undo-boundary)))
19853 ;; remember which buffer to undo
19854 (push (list _cmd _cline _buf1 _c1 _buf2 _c2
)
19855 org-agenda-undo-list
)))))
19857 (defun org-agenda-undo ()
19858 "Undo a remote editing step in the agenda.
19859 This undoes changes both in the agenda buffer and in the remote buffer
19860 that have been changed along."
19862 (or org-agenda-allow-remote-undo
19863 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
19864 (if (not (eq this-command last-command
))
19865 (setq org-agenda-undo-has-started-in nil
19866 org-agenda-pending-undo-list org-agenda-undo-list
))
19867 (if (not org-agenda-pending-undo-list
)
19868 (error "No further undo information"))
19869 (let* ((entry (pop org-agenda-pending-undo-list
))
19870 buf line cmd rembuf
)
19871 (setq cmd
(pop entry
) line
(pop entry
))
19872 (setq rembuf
(nth 2 entry
))
19873 (org-with-remote-undo rembuf
19874 (while (bufferp (setq buf
(pop entry
)))
19876 (with-current-buffer buf
19877 (let ((last-undo-buffer buf
)
19878 (inhibit-read-only t
))
19879 (unless (memq buf org-agenda-undo-has-started-in
)
19880 (push buf org-agenda-undo-has-started-in
)
19881 (make-local-variable 'pending-undo-list
)
19883 (while (and pending-undo-list
19884 (listp pending-undo-list
)
19885 (not (car pending-undo-list
)))
19886 (pop pending-undo-list
))
19889 (message "`%s' undone (buffer %s)" cmd
(buffer-name rembuf
))))
19891 (defun org-verify-change-for-undo (l1 l2
)
19892 "Verify that a real change occurred between the undo lists L1 and L2."
19893 (while (and l1
(listp l1
) (null (car l1
))) (pop l1
))
19894 (while (and l2
(listp l2
) (null (car l2
))) (pop l2
))
19897 ;;; Agenda dispatch
19899 (defvar org-agenda-restrict nil
)
19900 (defvar org-agenda-restrict-begin
(make-marker))
19901 (defvar org-agenda-restrict-end
(make-marker))
19902 (defvar org-agenda-last-dispatch-buffer nil
)
19903 (defvar org-agenda-overriding-restriction nil
)
19906 (defun org-agenda (arg &optional keys restriction
)
19907 "Dispatch agenda commands to collect entries to the agenda buffer.
19908 Prompts for a command to execute. Any prefix arg will be passed
19909 on to the selected command. The default selections are:
19911 a Call `org-agenda-list' to display the agenda for current day or week.
19912 t Call `org-todo-list' to display the global todo list.
19913 T Call `org-todo-list' to display the global todo list, select only
19914 entries with a specific TODO keyword (the user gets a prompt).
19915 m Call `org-tags-view' to display headlines with tags matching
19916 a condition (the user is prompted for the condition).
19917 M Like `m', but select only TODO entries, no ordinary headlines.
19918 L Create a timeline for the current buffer.
19919 e Export views to associated files.
19921 More commands can be added by configuring the variable
19922 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
19923 searches can be pre-defined in this way.
19925 If the current buffer is in Org-mode and visiting a file, you can also
19926 first press `<' once to indicate that the agenda should be temporarily
19927 \(until the next use of \\[org-agenda]) restricted to the current file.
19928 Pressing `<' twice means to restrict to the current subtree or region
19932 (let* ((prefix-descriptions nil
)
19933 (org-agenda-custom-commands-orig org-agenda-custom-commands
)
19934 (org-agenda-custom-commands
19935 ;; normalize different versions
19939 (cond ((stringp (cdr x
))
19940 (push x prefix-descriptions
)
19942 ((stringp (nth 1 x
)) x
)
19943 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
19944 (t (cons (car x
) (cons "" (cdr x
))))))
19945 org-agenda-custom-commands
)))
19946 (buf (current-buffer))
19947 (bfn (buffer-file-name (buffer-base-buffer)))
19948 entry key type match lprops ans
)
19949 ;; Turn off restriction unless there is an overriding one
19950 (unless org-agenda-overriding-restriction
19951 (put 'org-agenda-files
'org-restrict nil
)
19952 (setq org-agenda-restrict nil
)
19953 (move-marker org-agenda-restrict-begin nil
)
19954 (move-marker org-agenda-restrict-end nil
))
19955 ;; Delete old local properties
19956 (put 'org-agenda-redo-command
'org-lprops nil
)
19957 ;; Remember where this call originated
19958 (setq org-agenda-last-dispatch-buffer
(current-buffer))
19960 (setq ans
(org-agenda-get-restriction-and-command prefix-descriptions
)
19962 restriction
(cdr ans
)))
19963 ;; Estabish the restriction, if any
19964 (when (and (not org-agenda-overriding-restriction
) restriction
)
19965 (put 'org-agenda-files
'org-restrict
(list bfn
))
19967 ((eq restriction
'region
)
19968 (setq org-agenda-restrict t
)
19969 (move-marker org-agenda-restrict-begin
(region-beginning))
19970 (move-marker org-agenda-restrict-end
(region-end)))
19971 ((eq restriction
'subtree
)
19973 (setq org-agenda-restrict t
)
19974 (org-back-to-heading t
)
19975 (move-marker org-agenda-restrict-begin
(point))
19976 (move-marker org-agenda-restrict-end
19977 (progn (org-end-of-subtree t
)))))))
19979 (require 'calendar
) ; FIXME: can we avoid this for some commands?
19980 ;; For example the todo list should not need it (but does...)
19982 ((setq entry
(assoc keys org-agenda-custom-commands
))
19983 (if (or (symbolp (nth 2 entry
)) (functionp (nth 2 entry
)))
19985 (setq type
(nth 2 entry
) match
(nth 3 entry
) lprops
(nth 4 entry
))
19986 (put 'org-agenda-redo-command
'org-lprops lprops
)
19989 (org-let lprops
'(org-agenda-list current-prefix-arg
)))
19990 ((eq type
'alltodo
)
19991 (org-let lprops
'(org-todo-list current-prefix-arg
)))
19993 (org-let lprops
'(org-search-view current-prefix-arg match
)))
19995 (org-let lprops
'(org-agenda-list-stuck-projects
19996 current-prefix-arg
)))
19998 (org-let lprops
'(org-tags-view current-prefix-arg match
)))
19999 ((eq type
'tags-todo
)
20000 (org-let lprops
'(org-tags-view '(4) match
)))
20002 (org-let lprops
'(org-todo-list match
)))
20003 ((eq type
'tags-tree
)
20004 (org-check-for-org-mode)
20005 (org-let lprops
'(org-tags-sparse-tree current-prefix-arg match
)))
20006 ((eq type
'todo-tree
)
20007 (org-check-for-org-mode)
20009 '(org-occur (concat "^" outline-regexp
"[ \t]*"
20010 (regexp-quote match
) "\\>"))))
20011 ((eq type
'occur-tree
)
20012 (org-check-for-org-mode)
20013 (org-let lprops
'(org-occur match
)))
20015 (org-let lprops
'(funcall type match
)))
20017 (org-let lprops
'(funcall type match
)))
20018 (t (error "Invalid custom agenda command type %s" type
))))
20019 (org-run-agenda-series (nth 1 entry
) (cddr entry
))))
20021 (setq org-agenda-custom-commands org-agenda-custom-commands-orig
)
20022 (customize-variable 'org-agenda-custom-commands
))
20023 ((equal keys
"a") (call-interactively 'org-agenda-list
))
20024 ((equal keys
"s") (call-interactively 'org-search-view
))
20025 ((equal keys
"t") (call-interactively 'org-todo-list
))
20026 ((equal keys
"T") (org-call-with-arg 'org-todo-list
(or arg
'(4))))
20027 ((equal keys
"m") (call-interactively 'org-tags-view
))
20028 ((equal keys
"M") (org-call-with-arg 'org-tags-view
(or arg
'(4))))
20029 ((equal keys
"e") (call-interactively 'org-store-agenda-views
))
20031 (unless (org-mode-p)
20032 (error "This is not an Org-mode file"))
20033 (unless restriction
20034 (put 'org-agenda-files
'org-restrict
(list bfn
))
20035 (org-call-with-arg 'org-timeline arg
)))
20036 ((equal keys
"#") (call-interactively 'org-agenda-list-stuck-projects
))
20037 ((equal keys
"/") (call-interactively 'org-occur-in-agenda-files
))
20038 ((equal keys
"!") (customize-variable 'org-stuck-projects
))
20039 (t (error "Invalid agenda key"))))))
20041 (defun org-agenda-normalize-custom-commands (cmds)
20045 (cond ((stringp (cdr x
)) nil
)
20046 ((stringp (nth 1 x
)) x
)
20047 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
20048 (t (cons (car x
) (cons "" (cdr x
))))))
20051 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
20052 "The user interface for selecting an agenda command."
20054 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
20055 (restrict-ok (and bfn
(org-mode-p)))
20056 (region-p (org-region-active-p))
20057 (custom org-agenda-custom-commands
)
20059 restriction second-time
20060 c entry key type match prefixes rmheader header-end custom1 desc
)
20061 (save-window-excursion
20062 (delete-other-windows)
20063 (org-switch-to-buffer-other-window " *Agenda Commands*")
20065 (insert (eval-when-compile
20068 Press key for an agenda command: < Buffer,subtree/region restriction
20069 -------------------------------- > Remove restriction
20070 a Agenda for current week or day e Export agenda views
20071 t List of all TODO entries T Entries with special TODO kwd
20072 m Match a TAGS query M Like m, but only TODO entries
20073 L Timeline for current buffer # List stuck projects (!=configure)
20074 s Search for keywords C Configure custom agenda commands
20078 (while (string-match
20079 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
20081 (setq start
(match-end 0))
20082 (add-text-properties (match-beginning 2) (match-end 2)
20083 '(face bold
) header
))
20085 (setq header-end
(move-marker (make-marker) (point)))
20087 (setq custom1 custom
)
20088 (when (eq rmheader t
)
20090 (re-search-forward ":" nil t
)
20091 (delete-region (match-end 0) (point-at-eol))
20094 (delete-region (match-end 0) (point-at-eol))
20095 (move-marker header-end
(match-end 0)))
20096 (goto-char header-end
)
20097 (delete-region (point) (point-max))
20098 (while (setq entry
(pop custom1
))
20099 (setq key
(car entry
) desc
(nth 1 entry
)
20100 type
(nth 2 entry
) match
(nth 3 entry
))
20101 (if (> (length key
) 1)
20102 (add-to-list 'prefixes
(string-to-char key
))
20106 (org-add-props (copy-sequence key
)
20109 ((string-match "\\S-" desc
) desc
)
20110 ((eq type
'agenda
) "Agenda for current week or day")
20111 ((eq type
'alltodo
) "List of all TODO entries")
20112 ((eq type
'search
) "Word search")
20113 ((eq type
'stuck
) "List of stuck projects")
20114 ((eq type
'todo
) "TODO keyword")
20115 ((eq type
'tags
) "Tags query")
20116 ((eq type
'tags-todo
) "Tags (TODO)")
20117 ((eq type
'tags-tree
) "Tags tree")
20118 ((eq type
'todo-tree
) "TODO kwd tree")
20119 ((eq type
'occur-tree
) "Occur tree")
20120 ((functionp type
) (if (symbolp type
)
20122 "Lambda expression"))
20126 (org-add-props match nil
'face
'org-warning
))
20128 (format "set of %d commands" (length match
)))
20134 (org-add-props (char-to-string x
)
20136 (or (cdr (assoc (concat selstring
(char-to-string x
))
20137 prefix-descriptions
))
20140 (goto-char (point-min))
20141 (when (fboundp 'fit-window-to-buffer
)
20143 (if (not (pos-visible-in-window-p (point-max)))
20144 (fit-window-to-buffer))
20145 (setq second-time t
)
20146 (fit-window-to-buffer)))
20147 (message "Press key for agenda command%s:"
20148 (if (or restrict-ok org-agenda-overriding-restriction
)
20149 (if org-agenda-overriding-restriction
20150 " (restriction lock active)"
20152 (format " (restricted to %s)" restriction
)
20153 " (unrestricted)"))
20155 (setq c
(read-char-exclusive))
20158 ((assoc (char-to-string c
) custom
)
20159 (setq selstring
(concat selstring
(char-to-string c
)))
20160 (throw 'exit
(cons selstring restriction
)))
20162 (setq selstring
(concat selstring
(char-to-string c
))
20164 rmheader
(or rmheader t
)
20165 custom
(delq nil
(mapcar
20167 (if (or (= (length (car x
)) 1)
20168 (/= (string-to-char (car x
)) c
))
20170 (cons (substring (car x
) 1) (cdr x
))))
20172 ((and (not restrict-ok
) (memq c
'(?
1 ?
0 ?
<)))
20173 (message "Restriction is only possible in Org-mode buffers")
20174 (ding) (sit-for 1))
20176 (org-agenda-remove-restriction-lock 'noupdate
)
20177 (setq restriction
'buffer
))
20179 (org-agenda-remove-restriction-lock 'noupdate
)
20180 (setq restriction
(if region-p
'region
'subtree
)))
20182 (org-agenda-remove-restriction-lock 'noupdate
)
20185 ((eq restriction
'buffer
)
20186 (if region-p
'region
'subtree
))
20187 ((memq restriction
'(subtree region
))
20191 (org-agenda-remove-restriction-lock 'noupdate
)
20192 (setq restriction nil
))
20193 ((and (equal selstring
"") (memq c
'(?s ?a ?t ?m ?L ?C ?e ?T ?M ?
# ?
! ?
/)))
20194 (throw 'exit
(cons (setq selstring
(char-to-string c
)) restriction
)))
20195 ((and (> (length selstring
) 0) (eq c ?\d
))
20197 (org-agenda-get-restriction-and-command prefix-descriptions
))
20199 ((equal c ?q
) (error "Abort"))
20200 (t (error "Invalid key %c" c
))))))))
20202 (defun org-run-agenda-series (name series
)
20203 (org-prepare-agenda name
)
20204 (let* ((org-agenda-multi t
)
20205 (redo (list 'org-run-agenda-series name
(list 'quote series
)))
20206 (cmds (car series
))
20207 (gprops (nth 1 series
))
20208 match
;; The byte compiler incorrectly complains about this. Keep it!
20210 (while (setq cmd
(pop cmds
))
20211 (setq type
(car cmd
) match
(nth 1 cmd
) lprops
(nth 2 cmd
))
20214 (org-let2 gprops lprops
20215 '(call-interactively 'org-agenda-list
)))
20216 ((eq type
'alltodo
)
20217 (org-let2 gprops lprops
20218 '(call-interactively 'org-todo-list
)))
20220 (org-let2 gprops lprops
20221 '(org-search-view current-prefix-arg match
)))
20223 (org-let2 gprops lprops
20224 '(call-interactively 'org-agenda-list-stuck-projects
)))
20226 (org-let2 gprops lprops
20227 '(org-tags-view current-prefix-arg match
)))
20228 ((eq type
'tags-todo
)
20229 (org-let2 gprops lprops
20230 '(org-tags-view '(4) match
)))
20232 (org-let2 gprops lprops
20233 '(org-todo-list match
)))
20235 (org-let2 gprops lprops
20236 '(funcall type match
)))
20237 (t (error "Invalid type in command series"))))
20239 (setq org-agenda-redo-command redo
)
20240 (goto-char (point-min)))
20241 (org-finalize-agenda))
20244 (defmacro org-batch-agenda
(cmd-key &rest parameters
)
20245 "Run an agenda command in batch mode and send the result to STDOUT.
20246 If CMD-KEY is a string of length 1, it is used as a key in
20247 `org-agenda-custom-commands' and triggers this command. If it is a
20248 longer string it is used as a tags/todo match string.
20249 Paramters are alternating variable names and values that will be bound
20250 before running the agenda command."
20253 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
20254 (if (> (length cmd-key
) 2)
20255 (eval (list 'let
(nreverse pars
)
20256 (list 'org-tags-view nil cmd-key
)))
20257 (eval (list 'let
(nreverse pars
) (list 'org-agenda nil cmd-key
))))
20258 (set-buffer org-agenda-buffer-name
)
20259 (princ (org-encode-for-stdout (buffer-string)))))
20261 (defun org-encode-for-stdout (string)
20262 (if (fboundp 'encode-coding-string
)
20263 (encode-coding-string string buffer-file-coding-system
)
20266 (defvar org-agenda-info nil
)
20269 (defmacro org-batch-agenda-csv
(cmd-key &rest parameters
)
20270 "Run an agenda command in batch mode and send the result to STDOUT.
20271 If CMD-KEY is a string of length 1, it is used as a key in
20272 `org-agenda-custom-commands' and triggers this command. If it is a
20273 longer string it is used as a tags/todo match string.
20274 Paramters are alternating variable names and values that will be bound
20275 before running the agenda command.
20277 The output gives a line for each selected agenda item. Each
20278 item is a list of comma-separated values, like this:
20280 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
20282 category The category of the item
20283 head The headline, without TODO kwd, TAGS and PRIORITY
20284 type The type of the agenda entry, can be
20285 todo selected in TODO match
20286 tagsmatch selected in tags match
20287 diary imported from diary
20288 deadline a deadline on given date
20289 scheduled scheduled on given date
20290 timestamp entry has timestamp on given date
20291 closed entry was closed on given date
20292 upcoming-deadline warning about deadline
20293 past-scheduled forwarded scheduled item
20294 block entry has date block including g. date
20295 todo The todo keyword, if any
20296 tags All tags including inherited ones, separated by colons
20297 date The relevant date, like 2007-2-14
20298 time The time, like 15:00-16:50
20299 extra Sting with extra planning info
20300 priority-l The priority letter if any was given
20301 priority-n The computed numerical priority
20302 agenda-day The day in the agenda where this is listed"
20306 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
20307 (push (list 'org-agenda-remove-tags t
) pars
)
20308 (if (> (length cmd-key
) 2)
20309 (eval (list 'let
(nreverse pars
)
20310 (list 'org-tags-view nil cmd-key
)))
20311 (eval (list 'let
(nreverse pars
) (list 'org-agenda nil cmd-key
))))
20312 (set-buffer org-agenda-buffer-name
)
20313 (let* ((lines (org-split-string (buffer-string) "\n"))
20315 (while (setq line
(pop lines
))
20317 (if (not (get-text-property 0 'org-category line
)) (throw 'next nil
))
20318 (setq org-agenda-info
20319 (org-fix-agenda-info (text-properties-at 0 line
)))
20321 (org-encode-for-stdout
20322 (mapconcat 'org-agenda-export-csv-mapper
20323 '(org-category txt type todo tags date time-of-day extra
20324 priority-letter priority agenda-day
)
20328 (defun org-fix-agenda-info (props)
20329 "Make sure all properties on an agenda item have a canonical form,
20330 so the export commands can easily use it."
20332 (when (setq tmp
(plist-get props
'tags
))
20333 (setq props
(plist-put props
'tags
(mapconcat 'identity tmp
":"))))
20334 (when (setq tmp
(plist-get props
'date
))
20335 (if (integerp tmp
) (setq tmp
(calendar-gregorian-from-absolute tmp
)))
20336 (let ((calendar-date-display-form '(year "-" month
"-" day
)))
20337 '((format "%4d, %9s %2s, %4s" dayname monthname day year
))
20339 (setq tmp
(calendar-date-string tmp
)))
20340 (setq props
(plist-put props
'date tmp
)))
20341 (when (setq tmp
(plist-get props
'day
))
20342 (if (integerp tmp
) (setq tmp
(calendar-gregorian-from-absolute tmp
)))
20343 (let ((calendar-date-display-form '(year "-" month
"-" day
)))
20344 (setq tmp
(calendar-date-string tmp
)))
20345 (setq props
(plist-put props
'day tmp
))
20346 (setq props
(plist-put props
'agenda-day tmp
)))
20347 (when (setq tmp
(plist-get props
'txt
))
20348 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp
)
20349 (plist-put props
'priority-letter
(match-string 1 tmp
))
20350 (setq tmp
(replace-match "" t t tmp
)))
20351 (when (and (setq re
(plist-get props
'org-todo-regexp
))
20352 (setq re
(concat "\\`\\.*" re
" ?"))
20353 (string-match re tmp
))
20354 (plist-put props
'todo
(match-string 1 tmp
))
20355 (setq tmp
(replace-match "" t t tmp
)))
20356 (plist-put props
'txt tmp
)))
20359 (defun org-agenda-export-csv-mapper (prop)
20360 (let ((res (plist-get org-agenda-info prop
)))
20364 ((stringp res
) res
)
20365 (t (prin1-to-string res
))))
20366 (while (string-match "," res
)
20367 (setq res
(replace-match ";" t t res
)))
20372 (defun org-store-agenda-views (&rest parameters
)
20374 (eval (list 'org-batch-store-agenda-views
)))
20376 ;; FIXME, why is this a macro?????
20378 (defmacro org-batch-store-agenda-views
(&rest parameters
)
20379 "Run all custom agenda commands that have a file argument."
20380 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands
))
20381 (pop-up-frames nil
)
20382 (dir default-directory
)
20383 pars cmd thiscmdkey files opts
)
20385 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
20386 (setq pars
(reverse pars
))
20387 (save-window-excursion
20389 (setq cmd
(pop cmds
)
20390 thiscmdkey
(car cmd
)
20393 (if (stringp files
) (setq files
(list files
)))
20395 (eval (list 'let
(append org-agenda-exporter-settings opts pars
)
20396 (list 'org-agenda nil thiscmdkey
)))
20397 (set-buffer org-agenda-buffer-name
)
20399 (eval (list 'let
(append org-agenda-exporter-settings opts pars
)
20400 (list 'org-write-agenda
20401 (expand-file-name (pop files
) dir
) t
))))
20402 (and (get-buffer org-agenda-buffer-name
)
20403 (kill-buffer org-agenda-buffer-name
)))))))
20405 (defun org-write-agenda (file &optional nosettings
)
20406 "Write the current buffer (an agenda view) as a file.
20407 Depending on the extension of the file name, plain text (.txt),
20408 HTML (.html or .htm) or Postscript (.ps) is produced.
20409 If the extension is .ics, run icalendar export over all files used
20410 to construct the agenda and limit the export to entries listed in the
20412 If NOSETTINGS is given, do not scope the settings of
20413 `org-agenda-exporter-settings' into the export commands. This is used when
20414 the settings have already been scoped and we do not wish to overrule other,
20415 higher priority settings."
20416 (interactive "FWrite agenda to file: ")
20417 (if (not (file-writable-p file
))
20418 (error "Cannot write agenda to file %s" file
))
20420 ((string-match "\\.html?\\'" file
) (require 'htmlize
))
20421 ((string-match "\\.ps\\'" file
) (require 'ps-print
)))
20422 (org-let (if nosettings nil org-agenda-exporter-settings
)
20424 (save-window-excursion
20426 ((string-match "\\.html?\\'" file
)
20427 (set-buffer (htmlize-buffer (current-buffer)))
20429 (when (and org-agenda-export-html-style
20430 (string-match "<style>" org-agenda-export-html-style
))
20431 ;; replace <style> section with org-agenda-export-html-style
20432 (goto-char (point-min))
20433 (kill-region (- (search-forward "<style") 6)
20434 (search-forward "</style>"))
20435 (insert org-agenda-export-html-style
))
20437 (kill-buffer (current-buffer))
20438 (message "HTML written to %s" file
))
20439 ((string-match "\\.ps\\'" file
)
20440 (ps-print-buffer-with-faces file
)
20441 (message "Postscript written to %s" file
))
20442 ((string-match "\\.ics\\'" file
)
20443 (let ((org-agenda-marker-table
20444 (org-create-marker-find-array
20445 (org-agenda-collect-markers)))
20446 (org-icalendar-verify-function 'org-check-agenda-marker-table
)
20447 (org-combined-agenda-icalendar-file file
))
20448 (apply 'org-export-icalendar
'combine
(org-agenda-files))))
20450 (let ((bs (buffer-string)))
20454 (kill-buffer (current-buffer))
20455 (message "Plain text written to %s" file
))))))
20456 (set-buffer org-agenda-buffer-name
)))
20458 (defun org-agenda-collect-markers ()
20459 "Collect the markers pointing to entries in the agenda buffer."
20462 (goto-char (point-min))
20463 (while (not (eobp))
20464 (when (setq m
(or (get-text-property (point) 'org-hd-marker
)
20465 (get-text-property (point) 'org-marker
)))
20467 (beginning-of-line 2)))
20468 (nreverse markers
)))
20470 (defun org-create-marker-find-array (marker-list)
20471 "Create a alist of files names with all marker positions in that file."
20473 (while (setq m
(pop marker-list
))
20474 (setq p
(marker-position m
)
20475 f
(buffer-file-name (or (buffer-base-buffer
20477 (marker-buffer m
))))
20478 (if (setq a
(assoc f tbl
))
20479 (push (marker-position m
) (cdr a
))
20480 (push (list f p
) tbl
)))
20481 (mapcar (lambda (x) (setcdr x
(sort (copy-sequence (cdr x
)) '<)) x
)
20484 (defvar org-agenda-marker-table nil
) ; dyamically scoped parameter
20485 (defun org-check-agenda-marker-table ()
20486 "Check of the current entry is on the marker list."
20487 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
20489 (and (setq a
(assoc file org-agenda-marker-table
))
20492 (org-back-to-heading t
)
20493 (member (point) (cdr a
)))))))
20495 (defmacro org-no-read-only
(&rest body
)
20496 "Inhibit read-only for BODY."
20497 `(let ((inhibit-read-only t
)) ,@body
))
20499 (defun org-check-for-org-mode ()
20500 "Make sure current buffer is in org-mode. Error if not."
20502 (error "Cannot execute org-mode agenda command on buffer in %s."
20505 (defun org-fit-agenda-window ()
20506 "Fit the window to the buffer size."
20507 (and (memq org-agenda-window-setup
'(reorganize-frame))
20508 (fboundp 'fit-window-to-buffer
)
20509 (fit-window-to-buffer
20511 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions
)))
20512 (floor (* (frame-height) (car org-agenda-window-frame-fractions
))))))
20514 ;;; Agenda file list
20516 (defun org-agenda-files (&optional unrestricted
)
20517 "Get the list of agenda files.
20518 Optional UNRESTRICTED means return the full list even if a restriction
20519 is currently in place."
20522 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
20523 ((stringp org-agenda-files
) (org-read-agenda-file-list))
20524 ((listp org-agenda-files
) org-agenda-files
)
20525 (t (error "Invalid value of `org-agenda-files'")))))
20526 (setq files
(apply 'append
20527 (mapcar (lambda (f)
20528 (if (file-directory-p f
)
20529 (directory-files f t
20530 org-agenda-file-regexp
)
20533 (if org-agenda-skip-unavailable-files
20537 (and (file-readable-p file
) file
)))
20539 files
))) ; `org-check-agenda-file' will remove them from the list
20541 (defun org-edit-agenda-file-list ()
20542 "Edit the list of agenda files.
20543 Depending on setup, this either uses customize to edit the variable
20544 `org-agenda-files', or it visits the file that is holding the list. In the
20545 latter case, the buffer is set up in a way that saving it automatically kills
20546 the buffer and restores the previous window configuration."
20548 (if (stringp org-agenda-files
)
20549 (let ((cw (current-window-configuration)))
20550 (find-file org-agenda-files
)
20551 (org-set-local 'org-window-configuration cw
)
20552 (org-add-hook 'after-save-hook
20554 (set-window-configuration
20555 (prog1 org-window-configuration
20556 (kill-buffer (current-buffer))))
20557 (org-install-agenda-files-menu)
20558 (message "New agenda file list installed"))
20560 (message "%s" (substitute-command-keys
20561 "Edit list and finish with \\[save-buffer]")))
20562 (customize-variable 'org-agenda-files
)))
20564 (defun org-store-new-agenda-file-list (list)
20565 "Set new value for the agenda file list and save it correcly."
20566 (if (stringp org-agenda-files
)
20567 (let ((f org-agenda-files
) b
)
20568 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
20570 (insert (mapconcat 'identity list
"\n") "\n")))
20571 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
20572 (setq org-agenda-files list
)
20573 (customize-save-variable 'org-agenda-files org-agenda-files
))))
20575 (defun org-read-agenda-file-list ()
20576 "Read the list of agenda files from a file."
20577 (when (stringp org-agenda-files
)
20579 (insert-file-contents org-agenda-files
)
20580 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
20584 (defun org-cycle-agenda-files ()
20585 "Cycle through the files in `org-agenda-files'.
20586 If the current buffer visits an agenda file, find the next one in the list.
20587 If the current buffer does not, find the first agenda file."
20589 (let* ((fs (org-agenda-files t
))
20590 (files (append fs
(list (car fs
))))
20591 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
20593 (unless files
(error "No agenda files"))
20595 (while (setq file
(pop files
))
20596 (if (equal (file-truename file
) tcf
)
20598 (find-file (car files
))
20600 (find-file (car fs
)))
20601 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
20603 (defun org-agenda-file-to-front (&optional to-end
)
20604 "Move/add the current file to the top of the agenda file list.
20605 If the file is not present in the list, it is added to the front. If it is
20606 present, it is moved there. With optional argument TO-END, add/move to the
20609 (let ((org-agenda-skip-unavailable-files nil
)
20610 (file-alist (mapcar (lambda (x)
20611 (cons (file-truename x
) x
))
20612 (org-agenda-files t
)))
20613 (ctf (file-truename buffer-file-name
))
20615 (setq x
(assoc ctf file-alist
) had x
)
20617 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
20619 (setq file-alist
(append (delq x file-alist
) (list x
)))
20620 (setq file-alist
(cons x
(delq x file-alist
))))
20621 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
20622 (org-install-agenda-files-menu)
20623 (message "File %s to %s of agenda file list"
20624 (if had
"moved" "added") (if to-end
"end" "front"))))
20626 (defun org-remove-file (&optional file
)
20627 "Remove current file from the list of files in variable `org-agenda-files'.
20628 These are the files which are being checked for agenda entries.
20629 Optional argument FILE means, use this file instead of the current."
20631 (let* ((org-agenda-skip-unavailable-files nil
)
20632 (file (or file buffer-file-name
))
20633 (true-file (file-truename file
))
20634 (afile (abbreviate-file-name file
))
20635 (files (delq nil
(mapcar
20637 (if (equal true-file
20640 (org-agenda-files t
)))))
20641 (if (not (= (length files
) (length (org-agenda-files t
))))
20643 (org-store-new-agenda-file-list files
)
20644 (org-install-agenda-files-menu)
20645 (message "Removed file: %s" afile
))
20646 (message "File was not in list: %s (not removed)" afile
))))
20648 (defun org-file-menu-entry (file)
20649 (vector file
(list 'find-file file
) t
))
20651 (defun org-check-agenda-file (file)
20652 "Make sure FILE exists. If not, ask user what to do."
20653 (when (not (file-exists-p file
))
20654 (message "non-existent file %s. [R]emove from list or [A]bort?"
20655 (abbreviate-file-name file
))
20656 (let ((r (downcase (read-char-exclusive))))
20659 (org-remove-file file
)
20660 (throw 'nextfile t
))
20661 (t (error "Abort"))))))
20663 ;;; Agenda prepare and finalize
20665 (defvar org-agenda-multi nil
) ; dynammically scoped
20666 (defvar org-agenda-buffer-name
"*Org Agenda*")
20667 (defvar org-pre-agenda-window-conf nil
)
20668 (defvar org-agenda-name nil
)
20669 (defun org-prepare-agenda (&optional name
)
20670 (setq org-todo-keywords-for-agenda nil
)
20671 (setq org-done-keywords-for-agenda nil
)
20672 (if org-agenda-multi
20674 (setq buffer-read-only nil
)
20675 (goto-char (point-max))
20676 (unless (or (bobp) org-agenda-compact-blocks
)
20677 (insert "\n" (make-string (window-width) ?
=) "\n"))
20678 (narrow-to-region (point) (point-max)))
20679 (org-agenda-reset-markers)
20680 (org-prepare-agenda-buffers (org-agenda-files))
20681 (setq org-todo-keywords-for-agenda
20682 (org-uniquify org-todo-keywords-for-agenda
))
20683 (setq org-done-keywords-for-agenda
20684 (org-uniquify org-done-keywords-for-agenda
))
20685 (let* ((abuf (get-buffer-create org-agenda-buffer-name
))
20686 (awin (get-buffer-window abuf
)))
20688 ((equal (current-buffer) abuf
) nil
)
20689 (awin (select-window awin
))
20690 ((not (setq org-pre-agenda-window-conf
(current-window-configuration))))
20691 ((equal org-agenda-window-setup
'current-window
)
20692 (switch-to-buffer abuf
))
20693 ((equal org-agenda-window-setup
'other-window
)
20694 (org-switch-to-buffer-other-window abuf
))
20695 ((equal org-agenda-window-setup
'other-frame
)
20696 (switch-to-buffer-other-frame abuf
))
20697 ((equal org-agenda-window-setup
'reorganize-frame
)
20698 (delete-other-windows)
20699 (org-switch-to-buffer-other-window abuf
))))
20700 (setq buffer-read-only nil
)
20703 (and name
(not org-agenda-name
)
20704 (org-set-local 'org-agenda-name name
)))
20705 (setq buffer-read-only nil
))
20707 (defun org-finalize-agenda ()
20708 "Finishing touch for the agenda buffer, called just before displaying it."
20709 (unless org-agenda-multi
20711 (let ((inhibit-read-only t
))
20712 (goto-char (point-min))
20713 (while (org-activate-bracket-links (point-max))
20714 (add-text-properties (match-beginning 0) (match-end 0)
20716 (org-agenda-align-tags)
20717 (unless org-agenda-with-colors
20718 (remove-text-properties (point-min) (point-max) '(face nil
))))
20719 (if (and (boundp 'org-overriding-columns-format
)
20720 org-overriding-columns-format
)
20721 (org-set-local 'org-overriding-columns-format
20722 org-overriding-columns-format
))
20723 (if (and (boundp 'org-agenda-view-columns-initially
)
20724 org-agenda-view-columns-initially
)
20725 (org-agenda-columns))
20726 (when org-agenda-fontify-priorities
20727 (org-fontify-priorities))
20728 (run-hooks 'org-finalize-agenda-hook
)
20729 (setq org-agenda-type
(get-text-property (point) 'org-agenda-type
))
20732 (defun org-fontify-priorities ()
20733 "Make highest priority lines bold, and lowest italic."
20735 (mapc (lambda (o) (if (eq (org-overlay-get o
'org-type
) 'org-priority
)
20736 (org-delete-overlay o
)))
20737 (org-overlays-in (point-min) (point-max)))
20739 (let ((inhibit-read-only t
)
20741 (goto-char (point-min))
20742 (while (re-search-forward "\\[#\\(.\\)\\]" nil t
)
20743 (setq h
(or (get-char-property (point) 'org-highest-priority
)
20744 org-highest-priority
)
20745 l
(or (get-char-property (point) 'org-lowest-priority
)
20746 org-lowest-priority
)
20747 p
(string-to-char (match-string 1))
20748 b
(match-beginning 0) e
(point-at-eol)
20749 ov
(org-make-overlay b e
))
20752 (cond ((listp org-agenda-fontify-priorities
)
20753 (cdr (assoc p org-agenda-fontify-priorities
)))
20754 ((equal p l
) 'italic
)
20755 ((equal p h
) 'bold
)))
20756 (org-overlay-put ov
'org-type
'org-priority
)))))
20758 (defun org-prepare-agenda-buffers (files)
20759 "Create buffers for all agenda files, protect archived trees and comments."
20761 (let ((pa '(:org-archived t
))
20762 (pc '(:org-comment t
))
20763 (pall '(:org-archived t
:org-comment t
))
20764 (inhibit-read-only t
)
20765 (rea (concat ":" org-archive-tag
":"))
20769 (while (setq file
(pop files
))
20772 (org-check-agenda-file file
)
20773 (set-buffer (org-get-agenda-file-buffer file
)))
20775 (setq bmp
(buffer-modified-p))
20776 (org-refresh-category-properties)
20777 (setq org-todo-keywords-for-agenda
20778 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
20779 (setq org-done-keywords-for-agenda
20780 (append org-done-keywords-for-agenda org-done-keywords
))
20782 (remove-text-properties (point-min) (point-max) pall
)
20783 (when org-agenda-skip-archived-trees
20784 (goto-char (point-min))
20785 (while (re-search-forward rea nil t
)
20786 (if (org-on-heading-p t
)
20787 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
20788 (goto-char (point-min))
20789 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
20790 (while (re-search-forward re nil t
)
20791 (add-text-properties
20792 (match-beginning 0) (org-end-of-subtree t
) pc
)))
20793 (set-buffer-modified-p bmp
))))))
20795 (defvar org-agenda-skip-function nil
20796 "Function to be called at each match during agenda construction.
20797 If this function returns nil, the current match should not be skipped.
20798 Otherwise, the function must return a position from where the search
20799 should be continued.
20800 This may also be a Lisp form, it will be evaluated.
20801 Never set this variable using `setq' or so, because then it will apply
20802 to all future agenda commands. Instead, bind it with `let' to scope
20803 it dynamically into the agenda-constructing command. A good way to set
20804 it is through options in org-agenda-custom-commands.")
20806 (defun org-agenda-skip ()
20807 "Throw to `:skip' in places that should be skipped.
20808 Also moves point to the end of the skipped region, so that search can
20809 continue from there."
20810 (let ((p (point-at-bol)) to fp
)
20811 (and org-agenda-skip-archived-trees
20812 (get-text-property p
:org-archived
)
20813 (org-end-of-subtree t
)
20815 (and (get-text-property p
:org-comment
)
20816 (org-end-of-subtree t
)
20818 (if (equal (char-after p
) ?
#) (throw :skip t
))
20819 (when (and (or (setq fp
(functionp org-agenda-skip-function
))
20820 (consp org-agenda-skip-function
))
20821 (setq to
(save-excursion
20824 (funcall org-agenda-skip-function
)
20825 (eval org-agenda-skip-function
))))))
20829 (defvar org-agenda-markers nil
20830 "List of all currently active markers created by `org-agenda'.")
20831 (defvar org-agenda-last-marker-time
(time-to-seconds (current-time))
20832 "Creation time of the last agenda marker.")
20834 (defun org-agenda-new-marker (&optional pos
)
20835 "Return a new agenda marker.
20836 Org-mode keeps a list of these markers and resets them when they are
20838 (let ((m (copy-marker (or pos
(point)))))
20839 (setq org-agenda-last-marker-time
(time-to-seconds (current-time)))
20840 (push m org-agenda-markers
)
20843 (defun org-agenda-reset-markers ()
20844 "Reset markers created by `org-agenda'."
20845 (while org-agenda-markers
20846 (move-marker (pop org-agenda-markers
) nil
)))
20848 (defun org-get-agenda-file-buffer (file)
20849 "Get a buffer visiting FILE. If the buffer needs to be created, add
20850 it to the list of buffers which might be released later."
20851 (let ((buf (org-find-base-buffer-visiting file
)))
20853 buf
; just return it
20854 ;; Make a new buffer and remember it
20855 (setq buf
(find-file-noselect file
))
20856 (if buf
(push buf org-agenda-new-buffers
))
20859 (defun org-release-buffers (blist)
20860 "Release all buffers in list, asking the user for confirmation when needed.
20861 When a buffer is unmodified, it is just killed. When modified, it is saved
20862 \(if the user agrees) and then killed."
20864 (while (setq buf
(pop blist
))
20865 (setq file
(buffer-file-name buf
))
20866 (when (and (buffer-modified-p buf
)
20868 (y-or-n-p (format "Save file %s? " file
)))
20869 (with-current-buffer buf
(save-buffer)))
20870 (kill-buffer buf
))))
20872 (defun org-get-category (&optional pos
)
20873 "Get the category applying to position POS."
20874 (get-text-property (or pos
(point)) 'org-category
))
20876 ;;; Agenda timeline
20878 (defvar org-agenda-only-exact-dates nil
) ; dynamically scoped
20880 (defun org-timeline (&optional include-all
)
20881 "Show a time-sorted view of the entries in the current org file.
20882 Only entries with a time stamp of today or later will be listed. With
20883 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
20884 under the current date.
20885 If the buffer contains an active region, only check the region for
20888 (require 'calendar
)
20889 (org-compile-prefix-format 'timeline
)
20890 (org-set-sorting-strategy 'timeline
)
20892 (dotodo include-all
)
20893 (doclosed org-agenda-show-log
)
20894 (entry buffer-file-name
)
20895 (date (calendar-current-date))
20896 (beg (if (org-region-active-p) (region-beginning) (point-min)))
20897 (end (if (org-region-active-p) (region-end) (point-max)))
20898 (day-numbers (org-get-all-dates beg end
'no-ranges
20899 t doclosed
; always include today
20900 org-timeline-show-empty-dates
))
20901 (org-deadline-warning-days 0)
20902 (org-agenda-only-exact-dates t
)
20903 (today (time-to-days (current-time)))
20907 (setq org-agenda-redo-command
20909 (list 'org-switch-to-buffer-other-window
(current-buffer))
20910 (list 'org-timeline
(list 'quote include-all
))))
20912 ;; Remove past dates from the list of dates.
20913 (setq day-numbers
(delq nil
(mapcar (lambda(x)
20914 (if (>= x today
) x nil
))
20916 (org-prepare-agenda (concat "Timeline "
20917 (file-name-nondirectory buffer-file-name
)))
20918 (if doclosed
(push :closed args
))
20919 (push :timestamp args
)
20920 (push :deadline args
)
20921 (push :scheduled args
)
20923 (if dotodo
(push :todo args
))
20924 (while (setq d
(pop day-numbers
))
20925 (if (and (listp d
) (eq (car d
) :omitted
))
20928 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d
)))
20929 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
))
20930 (if (listp d
) (setq d
(car d
) emptyp t
) (setq emptyp nil
))
20931 (if (and (>= d today
)
20936 (insert (make-string 79 ?-
) "\n")))
20937 (setq date
(calendar-gregorian-from-absolute d
))
20939 (setq rtn
(and (not emptyp
)
20940 (apply 'org-agenda-get-day-entries entry
20942 (if (or rtn
(equal d today
) org-timeline-show-empty-dates
)
20945 (if (stringp org-agenda-format-date
)
20946 (format-time-string org-agenda-format-date
20947 (org-time-from-absolute date
))
20948 (funcall org-agenda-format-date date
))
20950 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
)
20951 (put-text-property s
(1- (point)) 'org-date-line t
)
20952 (if (equal d today
)
20953 (put-text-property s
(1- (point)) 'org-today t
))
20954 (and rtn
(insert (org-finalize-agenda-entries rtn
) "\n"))
20955 (put-text-property s
(1- (point)) 'day d
)))))
20956 (goto-char (point-min))
20957 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t
)
20959 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline
))
20960 (org-finalize-agenda)
20961 (setq buffer-read-only t
)))
20963 (defun org-get-all-dates (beg end
&optional no-ranges force-today inactive empty pre-re
)
20964 "Return a list of all relevant day numbers from BEG to END buffer positions.
20965 If NO-RANGES is non-nil, include only the start and end dates of a range,
20966 not every single day in the range. If FORCE-TODAY is non-nil, make
20967 sure that TODAY is included in the list. If INACTIVE is non-nil, also
20968 inactive time stamps (those in square brackets) are included.
20969 When EMPTY is non-nil, also include days without any entries."
20971 (if pre-re pre-re
"")
20972 (if inactive org-ts-regexp-both org-ts-regexp
)))
20973 dates dates1 date day day1 day2 ts1 ts2
)
20975 (setq dates
(list (time-to-days (current-time)))))
20978 (while (re-search-forward re end t
)
20979 (setq day
(time-to-days (org-time-string-to-time
20980 (substring (match-string 1) 0 10))))
20981 (or (memq day dates
) (push day dates
)))
20984 (while (re-search-forward org-tr-regexp end t
)
20985 (setq ts1
(substring (match-string 1) 0 10)
20986 ts2
(substring (match-string 2) 0 10)
20987 day1
(time-to-days (org-time-string-to-time ts1
))
20988 day2
(time-to-days (org-time-string-to-time ts2
)))
20989 (while (< (setq day1
(1+ day1
)) day2
)
20990 (or (memq day1 dates
) (push day1 dates
)))))
20991 (setq dates
(sort dates
'<))
20993 (while (setq day
(pop dates
))
20994 (setq day2
(car dates
))
20996 (when (and day2 empty
)
20997 (if (or (eq empty t
)
20998 (and (numberp empty
) (<= (- day2 day
) empty
)))
20999 (while (< (setq day
(1+ day
)) day2
)
21000 (push (list day
) dates1
))
21001 (push (cons :omitted
(- day2 day
)) dates1
))))
21002 (setq dates
(nreverse dates1
)))
21005 ;;; Agenda Daily/Weekly
21007 (defvar org-agenda-overriding-arguments nil
) ; dynamically scoped parameter
21008 (defvar org-agenda-start-day nil
) ; dynamically scoped parameter
21009 (defvar org-agenda-last-arguments nil
21010 "The arguments of the previous call to org-agenda")
21011 (defvar org-starting-day nil
) ; local variable in the agenda buffer
21012 (defvar org-agenda-span nil
) ; local variable in the agenda buffer
21013 (defvar org-include-all-loc nil
) ; local variable
21014 (defvar org-agenda-remove-date nil
) ; dynamically scoped FIXME: not used???
21017 (defun org-agenda-list (&optional include-all start-day ndays
)
21018 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
21019 The view will be for the current day or week, but from the overview buffer
21020 you will be able to go to other days/weeks.
21022 With one \\[universal-argument] prefix argument INCLUDE-ALL,
21023 all unfinished TODO items will also be shown, before the agenda.
21024 This feature is considered obsolete, please use the TODO list or a block
21027 With a numeric prefix argument in an interactive call, the agenda will
21028 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
21029 the number of days. NDAYS defaults to `org-agenda-ndays'.
21031 START-DAY defaults to TODAY, or to the most recent match for the weekday
21032 given in `org-agenda-start-on-weekday'."
21034 (if (and (integerp include-all
) (> include-all
0))
21035 (setq ndays include-all include-all nil
))
21036 (setq ndays
(or ndays org-agenda-ndays
)
21037 start-day
(or start-day org-agenda-start-day
))
21038 (if org-agenda-overriding-arguments
21039 (setq include-all
(car org-agenda-overriding-arguments
)
21040 start-day
(nth 1 org-agenda-overriding-arguments
)
21041 ndays
(nth 2 org-agenda-overriding-arguments
)))
21042 (if (stringp start-day
)
21043 ;; Convert to an absolute day number
21044 (setq start-day
(time-to-days (org-read-date nil t start-day
))))
21045 (setq org-agenda-last-arguments
(list include-all start-day ndays
))
21046 (org-compile-prefix-format 'agenda
)
21047 (org-set-sorting-strategy 'agenda
)
21048 (require 'calendar
)
21049 (let* ((org-agenda-start-on-weekday
21050 (if (or (equal ndays
7) (and (null ndays
) (equal 7 org-agenda-ndays
)))
21051 org-agenda-start-on-weekday nil
))
21052 (thefiles (org-agenda-files))
21054 (today (time-to-days
21055 (time-subtract (current-time)
21056 (list 0 (* 3600 org-extend-today-until
) 0))))
21057 (sd (or start-day today
))
21058 (start (if (or (null org-agenda-start-on-weekday
)
21059 (< org-agenda-ndays
7))
21061 (let* ((nt (calendar-day-of-week
21062 (calendar-gregorian-from-absolute sd
)))
21063 (n1 org-agenda-start-on-weekday
)
21065 (- sd
(+ (if (< d
0) 7 0) d
)))))
21066 (day-numbers (list start
))
21068 (inhibit-redisplay (not debug-on-error
))
21069 s e rtn rtnall file date d start-pos end-pos todayp nd
)
21070 (setq org-agenda-redo-command
21071 (list 'org-agenda-list
(list 'quote include-all
) start-day ndays
))
21072 ;; Make the list of days
21073 (setq ndays
(or ndays org-agenda-ndays
)
21076 (push (1+ (car day-numbers
)) day-numbers
)
21077 (setq ndays
(1- ndays
)))
21078 (setq day-numbers
(nreverse day-numbers
))
21079 (org-prepare-agenda "Day/Week")
21080 (org-set-local 'org-starting-day
(car day-numbers
))
21081 (org-set-local 'org-include-all-loc include-all
)
21082 (org-set-local 'org-agenda-span
21083 (org-agenda-ndays-to-span nd
))
21084 (when (and (or include-all org-agenda-include-all-todo
)
21085 (member today day-numbers
))
21086 (setq files thefiles
21088 (while (setq file
(pop files
))
21090 (org-check-agenda-file file
)
21091 (setq date
(calendar-gregorian-from-absolute today
)
21092 rtn
(org-agenda-get-day-entries
21094 (setq rtnall
(append rtnall rtn
))))
21096 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
21097 (add-text-properties (point-min) (1- (point))
21098 (list 'face
'org-agenda-structure
))
21099 (insert (org-finalize-agenda-entries rtnall
) "\n")))
21100 (unless org-agenda-compact-blocks
21102 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd
)))
21104 (add-text-properties s
(1- (point)) (list 'face
'org-agenda-structure
21105 'org-date-line t
)))
21106 (while (setq d
(pop day-numbers
))
21107 (setq date
(calendar-gregorian-from-absolute d
)
21109 (if (or (setq todayp
(= d today
))
21110 (and (not start-pos
) (= d sd
)))
21111 (setq start-pos
(point))
21112 (if (and start-pos
(not end-pos
))
21113 (setq end-pos
(point))))
21114 (setq files thefiles
21116 (while (setq file
(pop files
))
21118 (org-check-agenda-file file
)
21119 (if org-agenda-show-log
21120 (setq rtn
(org-agenda-get-day-entries
21122 :deadline
:scheduled
:timestamp
:sexp
:closed
))
21123 (setq rtn
(org-agenda-get-day-entries
21125 :deadline
:scheduled
:sexp
:timestamp
)))
21126 (setq rtnall
(append rtnall rtn
))))
21127 (if org-agenda-include-diary
21129 (require 'diary-lib
)
21130 (setq rtn
(org-get-entries-from-diary date
))
21131 (setq rtnall
(append rtnall rtn
))))
21132 (if (or rtnall org-agenda-show-all-dates
)
21134 (setq day-cnt
(1+ day-cnt
))
21136 (if (stringp org-agenda-format-date
)
21137 (format-time-string org-agenda-format-date
21138 (org-time-from-absolute date
))
21139 (funcall org-agenda-format-date date
))
21141 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
)
21142 (put-text-property s
(1- (point)) 'org-date-line t
)
21143 (put-text-property s
(1- (point)) 'org-day-cnt day-cnt
)
21144 (if todayp
(put-text-property s
(1- (point)) 'org-today t
))
21146 (org-finalize-agenda-entries
21147 (org-agenda-add-time-grid-maybe
21150 (put-text-property s
(1- (point)) 'day d
)
21151 (put-text-property s
(1- (point)) 'org-day-cnt day-cnt
))))
21152 (goto-char (point-min))
21153 (org-fit-agenda-window)
21154 (unless (and (pos-visible-in-window-p (point-min))
21155 (pos-visible-in-window-p (point-max)))
21156 (goto-char (1- (point-max)))
21158 (if (not (pos-visible-in-window-p (or start-pos
1)))
21160 (goto-char (or start-pos
1))
21162 (goto-char (or start-pos
1))
21163 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda
))
21164 (org-finalize-agenda)
21165 (setq buffer-read-only t
)
21168 (defun org-agenda-ndays-to-span (n)
21169 (cond ((< n
7) 'day
) ((= n
7) 'week
) ((< n
32) 'month
) (t 'year
)))
21171 ;;; Agenda word search
21173 (defvar org-agenda-search-history nil
)
21176 (defun org-search-view (&optional arg string
)
21177 "Show all entries that contain words or regular expressions.
21178 If the first character of the search string is an asterisks,
21179 search only the headlines.
21181 The search string is broken into \"words\" by splitting at whitespace.
21182 The individual words are then interpreted as a boolean expression with
21183 logical AND. Words prefixed with a minus must not occur in the entry.
21184 Words without a prefix or prefixed with a plus must occur in the entry.
21185 Matching is case-insensitive and the words are enclosed by word delimiters.
21187 Words enclosed by curly braces are interpreted as regular expressions
21188 that must or must not match in the entry.
21190 This command searches the agenda files, and in addition the files listed
21191 in `org-agenda-text-search-extra-files'."
21193 (org-compile-prefix-format 'search
)
21194 (org-set-sorting-strategy 'search
)
21195 (org-prepare-agenda "SEARCH")
21196 (let* ((props (list 'face nil
21197 'done-face
'org-done
21198 'org-not-done-regexp org-not-done-regexp
21199 'org-todo-regexp org-todo-regexp
21200 'mouse-face
'highlight
21201 'keymap org-agenda-keymap
21202 'help-echo
(format "mouse-2 or RET jump to location")))
21203 regexp rtn rtnall files file pos
21204 marker priority category tags c neg re
21205 ee txt beg end words regexps
+ regexps- hdl-only buffer beg1 str
)
21206 (unless (and (not arg
)
21208 (string-match "\\S-" string
))
21209 (setq string
(read-string "[+-]Word/{Regexp} ...: "
21211 ((integerp arg
) (cons string arg
))
21213 'org-agenda-search-history
)))
21214 (setq org-agenda-redo-command
21215 (list 'org-search-view
'current-prefix-arg string
))
21216 (setq org-agenda-query-string string
)
21218 (if (equal (string-to-char string
) ?
*)
21220 words
(substring string
1))
21221 (setq words string
))
21222 (setq words
(org-split-string words
))
21224 (setq c
(string-to-char w
))
21226 (setq neg t w
(substring w
1))
21228 (setq neg nil w
(substring w
1))
21230 (if (string-match "\\`{.*}\\'" w
)
21231 (setq re
(substring w
1 -
1))
21232 (setq re
(concat "\\<" (regexp-quote (downcase w
)) "\\>")))
21233 (if neg
(push re regexps-
) (push re regexps
+)))
21235 (setq regexps
+ (sort regexps
+ (lambda (a b
) (> (length a
) (length b
)))))
21237 (setq regexp
(concat "^" org-outline-regexp
))
21238 (setq regexp
(pop regexps
+))
21239 (if hdl-only
(setq regexp
(concat "^" org-outline-regexp
".*?"
21241 (setq files
(append (org-agenda-files) org-agenda-text-search-extra-files
)
21243 (while (setq file
(pop files
))
21246 (org-check-agenda-file file
)
21247 (setq buffer
(if (file-exists-p file
)
21248 (org-get-agenda-file-buffer file
)
21249 (error "No such file %s" file
)))
21251 ;; If file does not exist, make sure an error message is sent
21252 (setq rtn
(list (format "ORG-AGENDA-ERROR: No such org-file %s"
21254 (with-current-buffer buffer
21255 (unless (org-mode-p)
21256 (error "Agenda file %s is not in `org-mode'" file
))
21257 (let ((case-fold-search t
))
21260 (if org-agenda-restrict
21261 (narrow-to-region org-agenda-restrict-begin
21262 org-agenda-restrict-end
)
21264 (goto-char (point-min))
21265 (unless (or (org-on-heading-p)
21266 (outline-next-heading))
21267 (throw 'nextfile t
))
21268 (goto-char (max (point-min) (1- (point))))
21269 (while (re-search-forward regexp nil t
)
21270 (org-back-to-heading t
)
21271 (skip-chars-forward "* ")
21272 (setq beg
(point-at-bol)
21274 end
(progn (outline-next-heading) (point)))
21278 (setq str
(buffer-substring-no-properties
21280 (if hdl-only
(point-at-eol) end
)))
21281 (mapc (lambda (wr) (when (string-match wr str
)
21282 (goto-char (1- end
))
21285 (mapc (lambda (wr) (unless (string-match wr str
)
21286 (goto-char (1- end
))
21290 (setq marker
(org-agenda-new-marker (point))
21291 category
(org-get-category)
21292 tags
(org-get-tags-at (point))
21293 txt
(org-format-agenda-item
21295 (buffer-substring-no-properties
21296 beg1
(point-at-eol))
21298 (org-add-props txt props
21299 'org-marker marker
'org-hd-marker marker
21300 'priority
1000 'org-category category
21303 (goto-char (1- end
)))))))))
21304 (setq rtn
(nreverse ee
))
21305 (setq rtnall
(append rtnall rtn
)))
21306 (if org-agenda-overriding-header
21307 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
21308 nil
'face
'org-agenda-structure
) "\n")
21309 (insert "Search words: ")
21310 (add-text-properties (point-min) (1- (point))
21311 (list 'face
'org-agenda-structure
))
21313 (insert string
"\n")
21314 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
21316 (unless org-agenda-multi
21317 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
21318 (add-text-properties pos
(1- (point))
21319 (list 'face
'org-agenda-structure
))))
21321 (insert (org-finalize-agenda-entries rtnall
) "\n"))
21322 (goto-char (point-min))
21323 (org-fit-agenda-window)
21324 (add-text-properties (point-min) (point-max) '(org-agenda-type search
))
21325 (org-finalize-agenda)
21326 (setq buffer-read-only t
)))
21328 ;;; Agenda TODO list
21330 (defvar org-select-this-todo-keyword nil
)
21331 (defvar org-last-arg nil
)
21334 (defun org-todo-list (arg)
21335 "Show all TODO entries from all agenda file in a single list.
21336 The prefix arg can be used to select a specific TODO keyword and limit
21337 the list to these. When using \\[universal-argument], you will be prompted
21338 for a keyword. A numeric prefix directly selects the Nth keyword in
21339 `org-todo-keywords-1'."
21341 (require 'calendar
)
21342 (org-compile-prefix-format 'todo
)
21343 (org-set-sorting-strategy 'todo
)
21344 (org-prepare-agenda "TODO")
21345 (let* ((today (time-to-days (current-time)))
21346 (date (calendar-gregorian-from-absolute today
))
21347 (kwds org-todo-keywords-for-agenda
)
21348 (completion-ignore-case t
)
21349 (org-select-this-todo-keyword
21350 (if (stringp arg
) arg
21351 (and arg
(integerp arg
) (> arg
0)
21352 (nth (1- arg
) kwds
))))
21353 rtn rtnall files file pos
)
21354 (when (equal arg
'(4))
21355 (setq org-select-this-todo-keyword
21356 (completing-read "Keyword (or KWD1|K2D2|...): "
21357 (mapcar 'list kwds
) nil nil
)))
21358 (and (equal 0 arg
) (setq org-select-this-todo-keyword nil
))
21359 (org-set-local 'org-last-arg arg
)
21360 (setq org-agenda-redo-command
21361 '(org-todo-list (or current-prefix-arg org-last-arg
)))
21362 (setq files
(org-agenda-files)
21364 (while (setq file
(pop files
))
21366 (org-check-agenda-file file
)
21367 (setq rtn
(org-agenda-get-day-entries file date
:todo
))
21368 (setq rtnall
(append rtnall rtn
))))
21369 (if org-agenda-overriding-header
21370 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
21371 nil
'face
'org-agenda-structure
) "\n")
21372 (insert "Global list of TODO items of type: ")
21373 (add-text-properties (point-min) (1- (point))
21374 (list 'face
'org-agenda-structure
))
21376 (insert (or org-select-this-todo-keyword
"ALL") "\n")
21377 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
21379 (unless org-agenda-multi
21380 (insert "Available with `N r': (0)ALL")
21383 (setq s
(format "(%d)%s" (setq n
(1+ n
)) x
))
21384 (if (> (+ (current-column) (string-width s
) 1) (frame-width))
21389 (add-text-properties pos
(1- (point)) (list 'face
'org-agenda-structure
)))
21391 (insert (org-finalize-agenda-entries rtnall
) "\n"))
21392 (goto-char (point-min))
21393 (org-fit-agenda-window)
21394 (add-text-properties (point-min) (point-max) '(org-agenda-type todo
))
21395 (org-finalize-agenda)
21396 (setq buffer-read-only t
)))
21398 ;;; Agenda tags match
21401 (defun org-tags-view (&optional todo-only match
)
21402 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
21403 The prefix arg TODO-ONLY limits the search to TODO entries."
21405 (org-compile-prefix-format 'tags
)
21406 (org-set-sorting-strategy 'tags
)
21407 (let* ((org-tags-match-list-sublevels
21408 (if todo-only t org-tags-match-list-sublevels
))
21409 (completion-ignore-case t
)
21410 rtn rtnall files file pos matcher
21412 (setq matcher
(org-make-tags-matcher match
)
21413 match
(car matcher
) matcher
(cdr matcher
))
21414 (org-prepare-agenda (concat "TAGS " match
))
21415 (setq org-agenda-query-string match
)
21416 (setq org-agenda-redo-command
21417 (list 'org-tags-view
(list 'quote todo-only
)
21418 (list 'if
'current-prefix-arg nil
'org-agenda-query-string
)))
21419 (setq files
(org-agenda-files)
21421 (while (setq file
(pop files
))
21423 (org-check-agenda-file file
)
21424 (setq buffer
(if (file-exists-p file
)
21425 (org-get-agenda-file-buffer file
)
21426 (error "No such file %s" file
)))
21428 ;; If file does not exist, merror message to agenda
21430 (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
21431 rtnall
(append rtnall rtn
))
21432 (with-current-buffer buffer
21433 (unless (org-mode-p)
21434 (error "Agenda file %s is not in `org-mode'" file
))
21437 (if org-agenda-restrict
21438 (narrow-to-region org-agenda-restrict-begin
21439 org-agenda-restrict-end
)
21441 (setq rtn
(org-scan-tags 'agenda matcher todo-only
))
21442 (setq rtnall
(append rtnall rtn
))))))))
21443 (if org-agenda-overriding-header
21444 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
21445 nil
'face
'org-agenda-structure
) "\n")
21446 (insert "Headlines with TAGS match: ")
21447 (add-text-properties (point-min) (1- (point))
21448 (list 'face
'org-agenda-structure
))
21450 (insert match
"\n")
21451 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
21453 (unless org-agenda-multi
21454 (insert "Press `C-u r' to search again with new search string\n"))
21455 (add-text-properties pos
(1- (point)) (list 'face
'org-agenda-structure
)))
21457 (insert (org-finalize-agenda-entries rtnall
) "\n"))
21458 (goto-char (point-min))
21459 (org-fit-agenda-window)
21460 (add-text-properties (point-min) (point-max) '(org-agenda-type tags
))
21461 (org-finalize-agenda)
21462 (setq buffer-read-only t
)))
21464 ;;; Agenda Finding stuck projects
21466 (defvar org-agenda-skip-regexp nil
21467 "Regular expression used in skipping subtrees for the agenda.
21468 This is basically a temporary global variable that can be set and then
21469 used by user-defined selections using `org-agenda-skip-function'.")
21471 (defvar org-agenda-overriding-header nil
21472 "When this is set during todo and tags searches, will replace header.")
21474 (defun org-agenda-skip-subtree-when-regexp-matches ()
21475 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
21476 If yes, it returns the end position of this tree, causing agenda commands
21477 to skip this subtree. This is a function that can be put into
21478 `org-agenda-skip-function' for the duration of a command."
21479 (let ((end (save-excursion (org-end-of-subtree t
)))
21482 (setq skip
(re-search-forward org-agenda-skip-regexp end t
)))
21485 (defun org-agenda-skip-entry-if (&rest conditions
)
21486 "Skip entry if any of CONDITIONS is true.
21487 See `org-agenda-skip-if' for details."
21488 (org-agenda-skip-if nil conditions
))
21490 (defun org-agenda-skip-subtree-if (&rest conditions
)
21491 "Skip entry if any of CONDITIONS is true.
21492 See `org-agenda-skip-if' for details."
21493 (org-agenda-skip-if t conditions
))
21495 (defun org-agenda-skip-if (subtree conditions
)
21496 "Checks current entity for CONDITIONS.
21497 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
21498 the entry, i.e. the text before the next heading is checked.
21500 CONDITIONS is a list of symbols, boolean OR is used to combine the results
21501 from different tests. Valid conditions are:
21503 scheduled Check if there is a scheduled cookie
21504 notscheduled Check if there is no scheduled cookie
21505 deadline Check if there is a deadline
21506 notdeadline Check if there is no deadline
21507 regexp Check if regexp matches
21508 notregexp Check if regexp does not match.
21510 The regexp is taken from the conditions list, it must come right after
21511 the `regexp' or `notregexp' element.
21513 If any of these conditions is met, this function returns the end point of
21514 the entity, causing the search to continue from there. This is a function
21515 that can be put into `org-agenda-skip-function' for the duration of a command."
21517 (org-back-to-heading t
)
21520 (progn (org-end-of-subtree t
) (point))
21521 (progn (outline-next-heading) (1- (point)))))
21525 (and (memq 'scheduled conditions
)
21526 (re-search-forward org-scheduled-time-regexp end t
))
21527 (and (memq 'notscheduled conditions
)
21528 (not (re-search-forward org-scheduled-time-regexp end t
)))
21529 (and (memq 'deadline conditions
)
21530 (re-search-forward org-deadline-time-regexp end t
))
21531 (and (memq 'notdeadline conditions
)
21532 (not (re-search-forward org-deadline-time-regexp end t
)))
21533 (and (setq m
(memq 'regexp conditions
))
21534 (stringp (nth 1 m
))
21535 (re-search-forward (nth 1 m
) end t
))
21536 (and (setq m
(memq 'notregexp conditions
))
21537 (stringp (nth 1 m
))
21538 (not (re-search-forward (nth 1 m
) end t
))))
21542 (defun org-agenda-list-stuck-projects (&rest ignore
)
21543 "Create agenda view for projects that are stuck.
21544 Stuck projects are project that have no next actions. For the definitions
21545 of what a project is and how to check if it stuck, customize the variable
21546 `org-stuck-projects'.
21547 MATCH is being ignored."
21549 (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches
)
21550 ;; FIXME: we could have used org-agenda-skip-if here.
21551 (org-agenda-overriding-header "List of stuck projects: ")
21552 (matcher (nth 0 org-stuck-projects
))
21553 (todo (nth 1 org-stuck-projects
))
21554 (todo-wds (if (member "*" todo
)
21556 (org-prepare-agenda-buffers (org-agenda-files))
21558 org-done-keywords-for-agenda
21559 (copy-sequence org-todo-keywords-for-agenda
)))
21561 (todo-re (concat "^\\*+[ \t]+\\("
21562 (mapconcat 'identity todo-wds
"\\|")
21564 (tags (nth 2 org-stuck-projects
))
21565 (tags-re (if (member "*" tags
)
21566 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
21567 (concat "^\\*+ .*:\\("
21568 (mapconcat 'identity tags
"\\|")
21569 (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
21570 (gen-re (nth 3 org-stuck-projects
))
21576 (and gen-re
(stringp gen-re
) (string-match "\\S-" gen-re
)
21578 (setq org-agenda-skip-regexp
21580 (mapconcat 'identity re-list
"\\|")
21581 (error "No information how to identify unstuck projects")))
21582 (org-tags-view nil matcher
)
21583 (with-current-buffer org-agenda-buffer-name
21584 (setq org-agenda-redo-command
21585 '(org-agenda-list-stuck-projects
21586 (or current-prefix-arg org-last-arg
))))))
21588 ;;; Diary integration
21590 (defvar org-disable-agenda-to-diary nil
) ;Dynamically-scoped param.
21591 (defvar list-diary-entries-hook
)
21593 (defun org-get-entries-from-diary (date)
21594 "Get the (Emacs Calendar) diary entries for DATE."
21595 (require 'diary-lib
)
21596 (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
21597 (diary-display-hook '(fancy-diary-display))
21598 (pop-up-frames nil
)
21599 (list-diary-entries-hook
21600 (cons 'org-diary-default-entry list-diary-entries-hook
))
21601 (diary-file-name-prefix-function nil
) ; turn this feature off
21602 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string
)
21604 (org-disable-agenda-to-diary t
))
21606 (save-window-excursion
21607 (funcall (if (fboundp 'diary-list-entries
)
21608 'diary-list-entries
'list-diary-entries
)
21610 (if (not (get-buffer fancy-diary-buffer
))
21612 (with-current-buffer fancy-diary-buffer
21613 (setq buffer-read-only nil
)
21614 (if (zerop (buffer-size))
21617 ;; Omit the date and other unnecessary stuff
21618 (org-agenda-cleanup-fancy-diary)
21619 ;; Add prefix to each line and extend the text properties
21620 (if (zerop (buffer-size))
21622 (setq entries
(buffer-substring (point-min) (- (point-max) 1)))))
21623 (set-buffer-modified-p nil
)
21624 (kill-buffer fancy-diary-buffer
)))
21626 (setq entries
(org-split-string entries
"\n"))
21630 (setq x
(org-format-agenda-item "" x
"Diary" nil
'time
))
21631 ;; Extend the text properties to the beginning of the line
21632 (org-add-props x
(text-properties-at (1- (length x
)) x
)
21633 'type
"diary" 'date date
))
21636 (defun org-agenda-cleanup-fancy-diary ()
21637 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
21638 This gets rid of the date, the underline under the date, and
21639 the dummy entry installed by `org-mode' to ensure non-empty diary for each
21640 date. It also removes lines that contain only whitespace."
21641 (goto-char (point-min))
21642 (if (looking-at ".*?:[ \t]*")
21645 (re-search-forward "\n=+$" nil t
)
21647 (while (re-search-backward "^ +\n?" nil t
) (replace-match "")))
21648 (re-search-forward "\n=+$" nil t
)
21649 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
21650 (goto-char (point-min))
21651 (while (re-search-forward "^ +\n" nil t
)
21652 (replace-match ""))
21653 (goto-char (point-min))
21654 (if (re-search-forward "^Org-mode dummy\n?" nil t
)
21655 (replace-match "")))
21657 ;; Make sure entries from the diary have the right text properties.
21658 (eval-after-load "diary-lib"
21659 '(if (boundp 'diary-modify-entry-list-string-function
)
21660 ;; We can rely on the hook, nothing to do
21662 ;; Hook not avaiable, must use advice to make this work
21663 (defadvice add-to-diary-list
(before org-mark-diary-entry activate
)
21664 "Make the position visible."
21665 (if (and org-disable-agenda-to-diary
;; called from org-agenda
21668 (setq string
(org-modify-diary-entry-string string
))))))
21670 (defun org-modify-diary-entry-string (string)
21671 "Add text properties to string, allowing org-mode to act on it."
21672 (org-add-props string nil
21673 'mouse-face
'highlight
21674 'keymap org-agenda-keymap
21675 'help-echo
(if buffer-file-name
21676 (format "mouse-2 or RET jump to diary file %s"
21677 (abbreviate-file-name buffer-file-name
))
21679 'org-agenda-diary-link t
21680 'org-marker
(org-agenda-new-marker (point-at-bol))))
21682 (defun org-diary-default-entry ()
21683 "Add a dummy entry to the diary.
21684 Needed to avoid empty dates which mess up holiday display."
21685 ;; Catch the error if dealing with the new add-to-diary-alist
21686 (when org-disable-agenda-to-diary
21687 (condition-case nil
21688 (add-to-diary-list original-date
"Org-mode dummy" "")
21690 (add-to-diary-list original-date
"Org-mode dummy" "" nil
)))))
21693 (defun org-diary (&rest args
)
21694 "Return diary information from org-files.
21695 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
21696 It accesses org files and extracts information from those files to be
21697 listed in the diary. The function accepts arguments specifying what
21698 items should be listed. The following arguments are allowed:
21700 :timestamp List the headlines of items containing a date stamp or
21701 date range matching the selected date. Deadlines will
21702 also be listed, on the expiration day.
21704 :sexp List entries resulting from diary-like sexps.
21706 :deadline List any deadlines past due, or due within
21707 `org-deadline-warning-days'. The listing occurs only
21708 in the diary for *today*, not at any other date. If
21709 an entry is marked DONE, it is no longer listed.
21711 :scheduled List all items which are scheduled for the given date.
21712 The diary for *today* also contains items which were
21713 scheduled earlier and are not yet marked DONE.
21715 :todo List all TODO items from the org-file. This may be a
21716 long list - so this is not turned on by default.
21717 Like deadlines, these entries only show up in the
21718 diary for *today*, not at any other date.
21720 The call in the diary file should look like this:
21722 &%%(org-diary) ~/path/to/some/orgfile.org
21724 Use a separate line for each org file to check. Or, if you omit the file name,
21725 all files listed in `org-agenda-files' will be checked automatically:
21729 If you don't give any arguments (as in the example above), the default
21730 arguments (:deadline :scheduled :timestamp :sexp) are used.
21731 So the example above may also be written as
21733 &%%(org-diary :deadline :timestamp :sexp :scheduled)
21735 The function expects the lisp variables `entry' and `date' to be provided
21736 by the caller, because this is how the calendar works. Don't use this
21737 function from a program - use `org-agenda-get-day-entries' instead."
21738 (when (> (- (time-to-seconds (current-time))
21739 org-agenda-last-marker-time
)
21741 (org-agenda-reset-markers))
21742 (org-compile-prefix-format 'agenda
)
21743 (org-set-sorting-strategy 'agenda
)
21744 (setq args
(or args
'(:deadline
:scheduled
:timestamp
:sexp
)))
21745 (let* ((files (if (and entry
(stringp entry
) (string-match "\\S-" entry
))
21747 (org-agenda-files t
)))
21749 (org-prepare-agenda-buffers files
)
21750 ;; If this is called during org-agenda, don't return any entries to
21751 ;; the calendar. Org Agenda will list these entries itself.
21752 (if org-disable-agenda-to-diary
(setq files nil
))
21753 (while (setq file
(pop files
))
21754 (setq rtn
(apply 'org-agenda-get-day-entries file date args
))
21755 (setq results
(append results rtn
)))
21757 (concat (org-finalize-agenda-entries results
) "\n"))))
21759 ;;; Agenda entry finders
21761 (defun org-agenda-get-day-entries (file date
&rest args
)
21762 "Does the work for `org-diary' and `org-agenda'.
21763 FILE is the path to a file to be checked for entries. DATE is date like
21764 the one returned by `calendar-current-date'. ARGS are symbols indicating
21765 which kind of entries should be extracted. For details about these, see
21766 the documentation of `org-diary'."
21767 (setq args
(or args
'(:deadline
:scheduled
:timestamp
:sexp
)))
21768 (let* ((org-startup-folded nil
)
21769 (org-startup-align-all-tables nil
)
21770 (buffer (if (file-exists-p file
)
21771 (org-get-agenda-file-buffer file
)
21772 (error "No such file %s" file
)))
21775 ;; If file does not exist, make sure an error message ends up in diary
21776 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
21777 (with-current-buffer buffer
21778 (unless (org-mode-p)
21779 (error "Agenda file %s is not in `org-mode'" file
))
21780 (let ((case-fold-search nil
))
21783 (if org-agenda-restrict
21784 (narrow-to-region org-agenda-restrict-begin
21785 org-agenda-restrict-end
)
21787 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
21788 (while (setq arg
(pop args
))
21790 ((and (eq arg
:todo
)
21791 (equal date
(calendar-current-date)))
21792 (setq rtn
(org-agenda-get-todos))
21793 (setq results
(append results rtn
)))
21794 ((eq arg
:timestamp
)
21795 (setq rtn
(org-agenda-get-blocks))
21796 (setq results
(append results rtn
))
21797 (setq rtn
(org-agenda-get-timestamps))
21798 (setq results
(append results rtn
)))
21800 (setq rtn
(org-agenda-get-sexps))
21801 (setq results
(append results rtn
)))
21802 ((eq arg
:scheduled
)
21803 (setq rtn
(org-agenda-get-scheduled))
21804 (setq results
(append results rtn
)))
21806 (setq rtn
(org-agenda-get-closed))
21807 (setq results
(append results rtn
)))
21808 ((eq arg
:deadline
)
21809 (setq rtn
(org-agenda-get-deadlines))
21810 (setq results
(append results rtn
))))))))
21813 (defun org-entry-is-todo-p ()
21814 (member (org-get-todo-state) org-not-done-keywords
))
21816 (defun org-entry-is-done-p ()
21817 (member (org-get-todo-state) org-done-keywords
))
21819 (defun org-get-todo-state ()
21821 (org-back-to-heading t
)
21822 (and (looking-at org-todo-line-regexp
)
21824 (match-string 2))))
21826 (defun org-at-date-range-p (&optional inactive-ok
)
21827 "Is the cursor inside a date range?"
21831 (let ((pos (point)))
21832 (skip-chars-backward "^[<\r\n")
21833 (skip-chars-backward "<[")
21834 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
21835 (>= (match-end 0) pos
)
21837 (skip-chars-backward "^<[\r\n")
21838 (skip-chars-backward "<[")
21839 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
21840 (>= (match-end 0) pos
)
21844 (defun org-agenda-get-todos ()
21845 "Return the TODO information for agenda display."
21846 (let* ((props (list 'face nil
21847 'done-face
'org-done
21848 'org-not-done-regexp org-not-done-regexp
21849 'org-todo-regexp org-todo-regexp
21850 'mouse-face
'highlight
21851 'keymap org-agenda-keymap
21853 (format "mouse-2 or RET jump to org file %s"
21854 (abbreviate-file-name buffer-file-name
))))
21855 ;; FIXME: get rid of the \n at some point but watch out
21856 (regexp (concat "^\\*+[ \t]+\\("
21857 (if org-select-this-todo-keyword
21858 (if (equal org-select-this-todo-keyword
"*")
21861 (mapconcat 'identity
(org-split-string org-select-this-todo-keyword
"|") "\\|")
21863 org-not-done-regexp
)
21865 marker priority category tags
21867 (goto-char (point-min))
21868 (while (re-search-forward regexp nil t
)
21871 (beginning-of-line)
21872 (setq beg
(point) end
(progn (outline-next-heading) (point)))
21873 (when (or (and org-agenda-todo-ignore-with-date
(goto-char beg
)
21874 (re-search-forward org-ts-regexp end t
))
21875 (and org-agenda-todo-ignore-scheduled
(goto-char beg
)
21876 (re-search-forward org-scheduled-time-regexp end t
))
21877 (and org-agenda-todo-ignore-deadlines
(goto-char beg
)
21878 (re-search-forward org-deadline-time-regexp end t
)
21879 (org-deadline-close (match-string 1))))
21880 (goto-char (1+ beg
))
21881 (or org-agenda-todo-list-sublevels
(org-end-of-subtree 'invisible
))
21882 (throw :skip nil
)))
21885 (goto-char (match-beginning 1))
21886 (setq marker
(org-agenda-new-marker (match-beginning 0))
21887 category
(org-get-category)
21888 tags
(org-get-tags-at (point))
21889 txt
(org-format-agenda-item "" (match-string 1) category tags
)
21890 priority
(1+ (org-get-priority txt
)))
21891 (org-add-props txt props
21892 'org-marker marker
'org-hd-marker marker
21893 'priority priority
'org-category category
21896 (if org-agenda-todo-list-sublevels
21897 (goto-char (match-end 1))
21898 (org-end-of-subtree 'invisible
))))
21901 (defconst org-agenda-no-heading-message
21902 "No heading for this item in buffer or region.")
21904 (defun org-agenda-get-timestamps ()
21905 "Return the date stamp information for agenda display."
21906 (let* ((props (list 'face nil
21907 'org-not-done-regexp org-not-done-regexp
21908 'org-todo-regexp org-todo-regexp
21909 'mouse-face
'highlight
21910 'keymap org-agenda-keymap
21912 (format "mouse-2 or RET jump to org file %s"
21913 (abbreviate-file-name buffer-file-name
))))
21914 (d1 (calendar-absolute-from-gregorian date
))
21918 (format-time-string
21920 (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
21926 (format-time-string
21927 (car org-time-stamp-formats
)
21928 (apply 'encode-time
; DATE bound by calendar
21929 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
21931 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
21932 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
21933 marker hdmarker deadlinep scheduledp donep tmp priority category
21934 ee txt timestr tags b0 b3 e3 head
)
21935 (goto-char (point-min))
21936 (while (re-search-forward regexp nil t
)
21937 (setq b0
(match-beginning 0)
21938 b3
(match-beginning 3) e3
(match-end 3))
21940 (and (org-at-date-range-p) (throw :skip nil
))
21942 (if (and (match-end 1)
21943 (not (= d1
(org-time-string-to-absolute (match-string 1) d1
))))
21946 (not (org-diary-sexp-entry (buffer-substring b3 e3
) "" date
)))
21948 (setq marker
(org-agenda-new-marker b0
)
21949 category
(org-get-category b0
)
21950 tmp
(buffer-substring (max (point-min)
21951 (- b0 org-ds-keyword-length
))
21953 timestr
(if b3
"" (buffer-substring b0
(point-at-eol)))
21954 deadlinep
(string-match org-deadline-regexp tmp
)
21955 scheduledp
(string-match org-scheduled-regexp tmp
)
21956 donep
(org-entry-is-done-p))
21957 (if (or scheduledp deadlinep
) (throw :skip t
))
21958 (if (string-match ">" timestr
)
21959 ;; substring should only run to end of time stamp
21960 (setq timestr
(substring timestr
0 (match-end 0))))
21962 (if (re-search-backward "^\\*+ " nil t
)
21964 (goto-char (match-beginning 0))
21965 (setq hdmarker
(org-agenda-new-marker)
21966 tags
(org-get-tags-at))
21967 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
21968 (setq head
(match-string 1))
21969 (and org-agenda-skip-timestamp-if-done donep
(throw :skip t
))
21970 (setq txt
(org-format-agenda-item
21971 nil head category tags timestr nil
21973 (setq txt org-agenda-no-heading-message
))
21974 (setq priority
(org-get-priority txt
))
21975 (org-add-props txt props
21976 'org-marker marker
'org-hd-marker hdmarker
)
21977 (org-add-props txt nil
'priority priority
21978 'org-category category
'date date
21981 (outline-next-heading)))
21984 (defun org-agenda-get-sexps ()
21985 "Return the sexp information for agenda display."
21986 (require 'diary-lib
)
21987 (let* ((props (list 'face nil
21988 'mouse-face
'highlight
21989 'keymap org-agenda-keymap
21991 (format "mouse-2 or RET jump to org file %s"
21992 (abbreviate-file-name buffer-file-name
))))
21994 marker category ee txt tags entry result beg b sexp sexp-entry
)
21995 (goto-char (point-min))
21996 (while (re-search-forward regexp nil t
)
21999 (setq beg
(match-beginning 0))
22000 (goto-char (1- (match-end 0)))
22003 (setq sexp
(buffer-substring b
(point)))
22004 (setq sexp-entry
(if (looking-at "[ \t]*\\(\\S-.*\\)")
22005 (org-trim (match-string 1))
22007 (setq result
(org-diary-sexp-entry sexp sexp-entry date
))
22009 (setq marker
(org-agenda-new-marker beg
)
22010 category
(org-get-category beg
))
22012 (if (string-match "\\S-" result
)
22014 (setq txt
"SEXP entry returned empty string"))
22016 (setq txt
(org-format-agenda-item
22017 "" txt category tags
'time
))
22018 (org-add-props txt props
'org-marker marker
)
22019 (org-add-props txt nil
22020 'org-category category
'date date
22025 (defun org-agenda-get-closed ()
22026 "Return the logged TODO entries for agenda display."
22027 (let* ((props (list 'mouse-face
'highlight
22028 'org-not-done-regexp org-not-done-regexp
22029 'org-todo-regexp org-todo-regexp
22030 'keymap org-agenda-keymap
22032 (format "mouse-2 or RET jump to org file %s"
22033 (abbreviate-file-name buffer-file-name
))))
22035 "\\<\\(" org-closed-string
"\\|" org-clock-string
"\\) *\\["
22038 (format-time-string
22039 (car org-time-stamp-formats
)
22040 (apply 'encode-time
; DATE bound by calendar
22041 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
22043 marker hdmarker priority category tags closedp
22045 (goto-char (point-min))
22046 (while (re-search-forward regexp nil t
)
22049 (setq marker
(org-agenda-new-marker (match-beginning 0))
22050 closedp
(equal (match-string 1) org-closed-string
)
22051 category
(org-get-category (match-beginning 0))
22052 timestr
(buffer-substring (match-beginning 0) (point-at-eol))
22053 ;; donep (org-entry-is-done-p)
22055 (if (string-match "\\]" timestr
)
22056 ;; substring should only run to end of time stamp
22057 (setq timestr
(substring timestr
0 (match-end 0))))
22059 (if (re-search-backward "^\\*+ " nil t
)
22061 (goto-char (match-beginning 0))
22062 (setq hdmarker
(org-agenda-new-marker)
22063 tags
(org-get-tags-at))
22064 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
22065 (setq txt
(org-format-agenda-item
22066 (if closedp
"Closed: " "Clocked: ")
22067 (match-string 1) category tags timestr
)))
22068 (setq txt org-agenda-no-heading-message
))
22069 (setq priority
100000)
22070 (org-add-props txt props
22071 'org-marker marker
'org-hd-marker hdmarker
'face
'org-done
22072 'priority priority
'org-category category
22073 'type
"closed" 'date date
22074 'undone-face
'org-warning
'done-face
'org-done
)
22076 (goto-char (point-at-eol))))
22079 (defun org-agenda-get-deadlines ()
22080 "Return the deadline information for agenda display."
22081 (let* ((props (list 'mouse-face
'highlight
22082 'org-not-done-regexp org-not-done-regexp
22083 'org-todo-regexp org-todo-regexp
22084 'keymap org-agenda-keymap
22086 (format "mouse-2 or RET jump to org file %s"
22087 (abbreviate-file-name buffer-file-name
))))
22088 (regexp org-deadline-time-regexp
)
22089 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
22090 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
22091 d2 diff dfrac wdays pos pos1 category tags
22092 ee txt head face s upcomingp donep timestr
)
22093 (goto-char (point-min))
22094 (while (re-search-forward regexp nil t
)
22097 (setq s
(match-string 1)
22098 pos
(1- (match-beginning 1))
22099 d2
(org-time-string-to-absolute (match-string 1) d1
'past
)
22101 wdays
(org-get-wdays s
)
22102 dfrac
(/ (* 1.0 (- wdays diff
)) (max wdays
1))
22103 upcomingp
(and todayp
(> diff
0)))
22104 ;; When to show a deadline in the calendar:
22105 ;; If the expiration is within wdays warning time.
22106 ;; Past-due deadlines are only shown on the current date
22107 (if (or (and (<= diff wdays
)
22108 (and todayp
(not org-agenda-only-exact-dates
)))
22111 (setq category
(org-get-category))
22112 (if (re-search-backward "^\\*+[ \t]+" nil t
)
22114 (goto-char (match-end 0))
22115 (setq pos1
(match-beginning 0))
22116 (setq tags
(org-get-tags-at pos1
))
22117 (setq head
(buffer-substring-no-properties
22119 (progn (skip-chars-forward "^\r\n")
22121 (setq donep
(string-match org-looking-at-done-regexp head
))
22122 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s
)
22124 (concat (substring s
(match-beginning 1)) " "))
22125 (setq timestr
'time
))
22127 (or org-agenda-skip-deadline-if-done
22130 (setq txt
(org-format-agenda-item
22132 (car org-agenda-deadline-leaders
)
22133 (format (nth 1 org-agenda-deadline-leaders
)
22135 head category tags timestr
))))
22136 (setq txt org-agenda-no-heading-message
))
22138 (setq face
(org-agenda-deadline-face dfrac wdays
))
22139 (org-add-props txt props
22140 'org-marker
(org-agenda-new-marker pos
)
22141 'org-hd-marker
(org-agenda-new-marker pos1
)
22142 'priority
(+ (- diff
)
22143 (org-get-priority txt
))
22144 'org-category category
22145 'type
(if upcomingp
"upcoming-deadline" "deadline")
22146 'date
(if upcomingp date d2
)
22147 'face
(if donep
'org-done face
)
22148 'undone-face face
'done-face
'org-done
)
22152 (defun org-agenda-deadline-face (fraction &optional wdays
)
22153 "Return the face to displaying a deadline item.
22154 FRACTION is what fraction of the head-warning time has passed."
22155 (if (equal wdays
0) (setq fraction
1.
))
22156 (let ((faces org-agenda-deadline-faces
) f
)
22158 (while (setq f
(pop faces
))
22159 (if (>= fraction
(car f
)) (throw 'exit
(cdr f
)))))))
22161 (defun org-agenda-get-scheduled ()
22162 "Return the scheduled information for agenda display."
22163 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
22164 'org-todo-regexp org-todo-regexp
22165 'done-face
'org-done
22166 'mouse-face
'highlight
22167 'keymap org-agenda-keymap
22169 (format "mouse-2 or RET jump to org file %s"
22170 (abbreviate-file-name buffer-file-name
))))
22171 (regexp org-scheduled-time-regexp
)
22172 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
22173 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
22174 d2 diff pos pos1 category tags
22175 ee txt head pastschedp donep face timestr s
)
22176 (goto-char (point-min))
22177 (while (re-search-forward regexp nil t
)
22180 (setq s
(match-string 1)
22181 pos
(1- (match-beginning 1))
22182 d2
(org-time-string-to-absolute (match-string 1) d1
'past
)
22184 ;;; do we need to do this for deadleine too????
22185 ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
22187 (setq pastschedp
(and todayp
(< diff
0)))
22188 ;; When to show a scheduled item in the calendar:
22189 ;; If it is on or past the date.
22190 (if (or (and (< diff
0)
22191 (and todayp
(not org-agenda-only-exact-dates
)))
22194 (setq category
(org-get-category))
22195 (if (re-search-backward "^\\*+[ \t]+" nil t
)
22197 (goto-char (match-end 0))
22198 (setq pos1
(match-beginning 0))
22199 (setq tags
(org-get-tags-at))
22200 (setq head
(buffer-substring-no-properties
22202 (progn (skip-chars-forward "^\r\n") (point))))
22203 (setq donep
(string-match org-looking-at-done-regexp head
))
22204 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s
)
22206 (concat (substring s
(match-beginning 1)) " "))
22207 (setq timestr
'time
))
22209 (or org-agenda-skip-scheduled-if-done
22212 (setq txt
(org-format-agenda-item
22214 (car org-agenda-scheduled-leaders
)
22215 (format (nth 1 org-agenda-scheduled-leaders
)
22217 head category tags timestr
))))
22218 (setq txt org-agenda-no-heading-message
))
22220 (setq face
(if pastschedp
22221 'org-scheduled-previously
22222 'org-scheduled-today
))
22223 (org-add-props txt props
22225 'face
(if donep
'org-done face
)
22226 'org-marker
(org-agenda-new-marker pos
)
22227 'org-hd-marker
(org-agenda-new-marker pos1
)
22228 'type
(if pastschedp
"past-scheduled" "scheduled")
22229 'date
(if pastschedp d2 date
)
22230 'priority
(+ 94 (- 5 diff
) (org-get-priority txt
))
22231 'org-category category
)
22235 (defun org-agenda-get-blocks ()
22236 "Return the date-range information for agenda display."
22237 (let* ((props (list 'face nil
22238 'org-not-done-regexp org-not-done-regexp
22239 'org-todo-regexp org-todo-regexp
22240 'mouse-face
'highlight
22241 'keymap org-agenda-keymap
22243 (format "mouse-2 or RET jump to org file %s"
22244 (abbreviate-file-name buffer-file-name
))))
22245 (regexp org-tr-regexp
)
22246 (d0 (calendar-absolute-from-gregorian date
))
22247 marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
22249 (goto-char (point-min))
22250 (while (re-search-forward regexp nil t
)
22254 (setq timestr
(match-string 0)
22255 s1
(match-string 1)
22256 s2
(match-string 2)
22257 d1
(time-to-days (org-time-string-to-time s1
))
22258 d2
(time-to-days (org-time-string-to-time s2
)))
22259 (if (and (> (- d0 d1
) -
1) (> (- d2 d0
) -
1))
22260 ;; Only allow days between the limits, because the normal
22261 ;; date stamps will catch the limits.
22263 (setq marker
(org-agenda-new-marker (point)))
22264 (setq category
(org-get-category))
22265 (if (re-search-backward "^\\*+ " nil t
)
22267 (goto-char (match-beginning 0))
22268 (setq hdmarker
(org-agenda-new-marker (point)))
22269 (setq tags
(org-get-tags-at))
22270 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
22271 (setq head
(match-string 1))
22272 (and org-agenda-skip-timestamp-if-done
22273 (org-entry-is-done-p)
22275 (setq txt
(org-format-agenda-item
22276 (format (if (= d1 d2
) "" "(%d/%d): ")
22277 (1+ (- d0 d1
)) (1+ (- d2 d1
)))
22279 (if (= d0 d1
) timestr
))))
22280 (setq txt org-agenda-no-heading-message
))
22281 (org-add-props txt props
22282 'org-marker marker
'org-hd-marker hdmarker
22283 'type
"block" 'date date
22284 'priority
(org-get-priority txt
) 'org-category category
)
22287 ;; Sort the entries by expiration date.
22290 ;;; Agenda presentation and sorting
22292 (defconst org-plain-time-of-day-regexp
22294 "\\(\\<[012]?[0-9]"
22295 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
22297 "\\(\\<[012]?[0-9]"
22298 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
22300 "Regular expression to match a plain time or time range.
22301 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
22302 groups carry important information:
22304 1 the first time, range or not
22305 8 the second time, if it is a range.")
22307 (defconst org-plain-time-extension-regexp
22309 "\\(\\<[012]?[0-9]"
22310 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
22311 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
22312 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
22313 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
22314 groups carry important information:
22316 7 hours of duration
22317 9 minutes of duration")
22319 (defconst org-stamp-time-of-day-regexp
22321 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
22322 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
22324 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
22325 "Regular expression to match a timestamp time or time range.
22326 After a match, the following groups carry important information:
22328 1 date plus weekday, for backreferencing to make sure both times on same day
22329 2 the first time, range or not
22330 4 the second time, if it is a range.")
22332 (defvar org-prefix-has-time nil
22333 "A flag, set by `org-compile-prefix-format'.
22334 The flag is set if the currently compiled format contains a `%t'.")
22335 (defvar org-prefix-has-tag nil
22336 "A flag, set by `org-compile-prefix-format'.
22337 The flag is set if the currently compiled format contains a `%T'.")
22339 (defun org-format-agenda-item (extra txt
&optional category tags dotime
22340 noprefix remove-re
)
22341 "Format TXT to be inserted into the agenda buffer.
22342 In particular, it adds the prefix and corresponding text properties. EXTRA
22343 must be a string and replaces the `%s' specifier in the prefix format.
22344 CATEGORY (string, symbol or nil) may be used to overrule the default
22345 category taken from local variable or file name. It will replace the `%c'
22346 specifier in the format. DOTIME, when non-nil, indicates that a
22347 time-of-day should be extracted from TXT for sorting of this entry, and for
22348 the `%t' specifier in the format. When DOTIME is a string, this string is
22349 searched for a time before TXT is. NOPREFIX is a flag and indicates that
22350 only the correctly processes TXT should be returned - this is used by
22351 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
22352 Any match of REMOVE-RE will be removed from TXT."
22354 ;; Diary entries sometimes have extra whitespace at the beginning
22355 (if (string-match "^ +" txt
) (setq txt
(replace-match "" nil nil txt
)))
22356 (let* ((category (or category
22358 (if buffer-file-name
22359 (file-name-sans-extension
22360 (file-name-nondirectory buffer-file-name
))
22362 (tag (if tags
(nth (1- (length tags
)) tags
) ""))
22363 time
; time and tag are needed for the eval of the prefix format
22364 (ts (if dotime
(concat (if (stringp dotime
) dotime
"") txt
)))
22365 (time-of-day (and dotime
(org-get-time-of-day ts
)))
22366 stamp plain s0 s1 s2 rtn srp
)
22367 (when (and dotime time-of-day org-prefix-has-time
)
22368 ;; Extract starting and ending time and move them to prefix
22369 (when (or (setq stamp
(string-match org-stamp-time-of-day-regexp ts
))
22370 (setq plain
(string-match org-plain-time-of-day-regexp ts
)))
22371 (setq s0
(match-string 0 ts
)
22372 srp
(and stamp
(match-end 3))
22373 s1
(match-string (if plain
1 2) ts
)
22374 s2
(match-string (if plain
8 (if srp
4 6)) ts
))
22376 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
22377 ;; them, we might want to remove them there to avoid duplication.
22378 ;; The user can turn this off with a variable.
22379 (if (and org-agenda-remove-times-when-in-prefix
(or stamp plain
)
22380 (string-match (concat (regexp-quote s0
) " *") txt
)
22381 (not (equal ?\
] (string-to-char (substring txt
(match-end 0)))))
22382 (if (eq org-agenda-remove-times-when-in-prefix
'beg
)
22383 (= (match-beginning 0) 0)
22385 (setq txt
(replace-match "" nil nil txt
))))
22386 ;; Normalize the time(s) to 24 hour
22387 (if s1
(setq s1
(org-get-time-of-day s1
'string t
)))
22388 (if s2
(setq s2
(org-get-time-of-day s2
'string t
))))
22390 (when (and s1
(not s2
) org-agenda-default-appointment-duration
22391 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1
))
22392 (let ((m (+ (string-to-number (match-string 2 s1
))
22393 (* 60 (string-to-number (match-string 1 s1
)))
22394 org-agenda-default-appointment-duration
))
22396 (setq h
(/ m
60) m
(- m
(* h
60)))
22397 (setq s2
(format "%02d:%02d" h m
))))
22399 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
22401 ;; Tags are in the string
22402 (if (or (eq org-agenda-remove-tags t
)
22403 (and org-agenda-remove-tags
22404 org-prefix-has-tag
))
22405 (setq txt
(replace-match "" t t txt
))
22406 (setq txt
(replace-match
22407 (concat (make-string (max (- 50 (length txt
)) 1) ?\
)
22408 (match-string 2 txt
))
22412 (while (string-match remove-re txt
)
22413 (setq txt
(replace-match "" t t txt
))))
22415 ;; Create the final string
22418 ;; Prepare the variables needed in the eval of the compiled format
22419 (setq time
(cond (s2 (concat s1
"-" s2
))
22420 (s1 (concat s1
"......"))
22422 extra
(or extra
"")
22423 category
(if (symbolp category
) (symbol-name category
) category
))
22424 ;; Evaluate the compiled format
22425 (setq rtn
(concat (eval org-prefix-format-compiled
) txt
)))
22427 ;; And finally add the text properties
22428 (org-add-props rtn nil
22429 'org-category
(downcase category
) 'tags tags
22430 'org-highest-priority org-highest-priority
22431 'org-lowest-priority org-lowest-priority
22432 'prefix-length
(- (length rtn
) (length txt
))
22433 'time-of-day time-of-day
22439 (defvar org-agenda-sorting-strategy
) ;; because the def is in a let form
22440 (defvar org-agenda-sorting-strategy-selected nil
)
22442 (defun org-agenda-add-time-grid-maybe (list ndays todayp
)
22444 (cond ((not org-agenda-use-time-grid
) (throw 'exit list
))
22445 ((and todayp
(member 'today
(car org-agenda-time-grid
))))
22446 ((and (= ndays
1) (member 'daily
(car org-agenda-time-grid
))))
22447 ((member 'weekly
(car org-agenda-time-grid
)))
22448 (t (throw 'exit list
)))
22449 (let* ((have (delq nil
(mapcar
22450 (lambda (x) (get-text-property 1 'time-of-day x
))
22452 (string (nth 1 org-agenda-time-grid
))
22453 (gridtimes (nth 2 org-agenda-time-grid
))
22454 (req (car org-agenda-time-grid
))
22455 (remove (member 'remove-match req
))
22457 (if (and (member 'require-timed req
) (not have
))
22458 ;; don't show empty grid
22459 (throw 'exit list
))
22460 (while (setq time
(pop gridtimes
))
22461 (unless (and remove
(member time have
))
22462 (setq time
(int-to-string time
))
22463 (push (org-format-agenda-item
22465 (concat (substring time
0 -
2) ":" (substring time -
2)))
22468 1 (length (car new
)) 'face
'org-time-grid
(car new
))))
22469 (if (member 'time-up org-agenda-sorting-strategy-selected
)
22471 (append list new
)))))
22473 (defun org-compile-prefix-format (key)
22474 "Compile the prefix format into a Lisp form that can be evaluated.
22475 The resulting form is returned and stored in the variable
22476 `org-prefix-format-compiled'."
22477 (setq org-prefix-has-time nil org-prefix-has-tag nil
)
22479 ((stringp org-agenda-prefix-format
)
22480 org-agenda-prefix-format
)
22481 ((assq key org-agenda-prefix-format
)
22482 (cdr (assq key org-agenda-prefix-format
)))
22483 (t " %-12:c%?-12t% s")))
22485 varform vars var e c f opt
)
22486 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
22488 (setq var
(cdr (assoc (match-string 4 s
)
22489 '(("c" . category
) ("t" . time
) ("s" . extra
)
22491 c
(or (match-string 3 s
) "")
22492 opt
(match-beginning 1)
22493 start
(1+ (match-beginning 0)))
22494 (if (equal var
'time
) (setq org-prefix-has-time t
))
22495 (if (equal var
'tag
) (setq org-prefix-has-tag t
))
22496 (setq f
(concat "%" (match-string 2 s
) "s"))
22499 `(if (equal "" ,var
)
22501 (format ,f
(if (equal "" ,var
) "" (concat ,var
,c
)))))
22502 (setq varform
`(format ,f
(if (equal ,var
"") "" (concat ,var
,c
)))))
22503 (setq s
(replace-match "%s" t nil s
))
22504 (push varform vars
))
22505 (setq vars
(nreverse vars
))
22506 (setq org-prefix-format-compiled
`(format ,s
,@vars
))))
22508 (defun org-set-sorting-strategy (key)
22509 (if (symbolp (car org-agenda-sorting-strategy
))
22511 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy
)
22512 (setq org-agenda-sorting-strategy-selected
22513 (or (cdr (assq key org-agenda-sorting-strategy
))
22514 (cdr (assq 'agenda org-agenda-sorting-strategy
))
22515 '(time-up category-keep priority-down
)))))
22517 (defun org-get-time-of-day (s &optional string mod24
)
22518 "Check string S for a time of day.
22519 If found, return it as a military time number between 0 and 2400.
22520 If not found, return nil.
22521 The optional STRING argument forces conversion into a 5 character wide string
22525 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s
)
22526 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s
))
22527 (let* ((h (string-to-number (match-string 1 s
)))
22528 (m (if (match-end 3) (string-to-number (match-string 3 s
)) 0))
22529 (ampm (if (match-end 4) (downcase (match-string 4 s
))))
22530 (am-p (equal ampm
"am"))
22531 (h1 (cond ((not ampm
) h
)
22532 ((= h
12) (if am-p
0 12))
22533 (t (+ h
(if am-p
0 12)))))
22534 (h2 (if (and string mod24
(not (and (= m
0) (= h1
24))))
22536 (t0 (+ (* 100 h2
) m
))
22537 (t1 (concat (if (>= h1
24) "+" " ")
22538 (if (< t0
100) "0" "")
22539 (if (< t0
10) "0" "")
22540 (int-to-string t0
))))
22541 (if string
(concat (substring t1 -
4 -
2) ":" (substring t1 -
2)) t0
)))))
22543 (defun org-finalize-agenda-entries (list &optional nosort
)
22544 "Sort and concatenate the agenda items."
22545 (setq list
(mapcar 'org-agenda-highlight-todo list
))
22548 (mapconcat 'identity
(sort list
'org-entries-lessp
) "\n")))
22550 (defun org-agenda-highlight-todo (x)
22554 (beginning-of-line 1)
22555 (setq re
(get-text-property (point) 'org-todo-regexp
))
22556 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length
) 0)))
22557 (when (looking-at (concat "[ \t]*\\.*" re
" +"))
22558 (add-text-properties (match-beginning 0) (match-end 0)
22559 (list 'face
(org-get-todo-face 0)))
22560 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
22561 (delete-region (match-beginning 1) (1- (match-end 0)))
22562 (goto-char (match-beginning 1))
22563 (insert (format org-agenda-todo-keyword-format s
)))))
22564 (setq re
(concat (get-text-property 0 'org-todo-regexp x
))
22565 pl
(get-text-property 0 'prefix-length x
))
22567 (equal (string-match (concat "\\(\\.*\\)" re
"\\( +\\)")
22569 (add-text-properties
22570 (or (match-end 1) (match-end 0)) (match-end 0)
22571 (list 'face
(org-get-todo-face (match-string 2 x
)))
22573 (setq x
(concat (substring x
0 (match-end 1))
22574 (format org-agenda-todo-keyword-format
22575 (match-string 2 x
))
22577 (substring x
(match-end 3)))))
22580 (defsubst org-cmp-priority
(a b
)
22581 "Compare the priorities of string A and B."
22582 (let ((pa (or (get-text-property 1 'priority a
) 0))
22583 (pb (or (get-text-property 1 'priority b
) 0)))
22584 (cond ((> pa pb
) +1)
22588 (defsubst org-cmp-category
(a b
)
22589 "Compare the string values of categories of strings A and B."
22590 (let ((ca (or (get-text-property 1 'org-category a
) ""))
22591 (cb (or (get-text-property 1 'org-category b
) "")))
22592 (cond ((string-lessp ca cb
) -
1)
22593 ((string-lessp cb ca
) +1)
22596 (defsubst org-cmp-tag
(a b
)
22597 "Compare the string values of categories of strings A and B."
22598 (let ((ta (car (last (get-text-property 1 'tags a
))))
22599 (tb (car (last (get-text-property 1 'tags b
)))))
22600 (cond ((not ta
) +1)
22602 ((string-lessp ta tb
) -
1)
22603 ((string-lessp tb ta
) +1)
22606 (defsubst org-cmp-time
(a b
)
22607 "Compare the time-of-day values of strings A and B."
22608 (let* ((def (if org-sort-agenda-notime-is-late
9901 -
1))
22609 (ta (or (get-text-property 1 'time-of-day a
) def
))
22610 (tb (or (get-text-property 1 'time-of-day b
) def
)))
22611 (cond ((< ta tb
) -
1)
22615 (defun org-entries-lessp (a b
)
22616 "Predicate for sorting agenda entries."
22617 ;; The following variables will be used when the form is evaluated.
22618 ;; So even though the compiler complains, keep them.
22619 (let* ((time-up (org-cmp-time a b
))
22620 (time-down (if time-up
(- time-up
) nil
))
22621 (priority-up (org-cmp-priority a b
))
22622 (priority-down (if priority-up
(- priority-up
) nil
))
22623 (category-up (org-cmp-category a b
))
22624 (category-down (if category-up
(- category-up
) nil
))
22625 (category-keep (if category-up
+1 nil
))
22626 (tag-up (org-cmp-tag a b
))
22627 (tag-down (if tag-up
(- tag-up
) nil
)))
22629 (eval (cons 'or org-agenda-sorting-strategy-selected
))
22630 '((-1 . t
) (1 . nil
) (nil . nil
))))))
22632 ;;; Agenda restriction lock
22634 (defvar org-agenda-restriction-lock-overlay
(org-make-overlay 1 1)
22635 "Overlay to mark the headline to which arenda commands are restricted.")
22636 (org-overlay-put org-agenda-restriction-lock-overlay
22637 'face
'org-agenda-restriction-lock
)
22638 (org-overlay-put org-agenda-restriction-lock-overlay
22639 'help-echo
"Agendas are currently limited to this subtree.")
22640 (org-detach-overlay org-agenda-restriction-lock-overlay
)
22641 (defvar org-speedbar-restriction-lock-overlay
(org-make-overlay 1 1)
22642 "Overlay marking the agenda restriction line in speedbar.")
22643 (org-overlay-put org-speedbar-restriction-lock-overlay
22644 'face
'org-agenda-restriction-lock
)
22645 (org-overlay-put org-speedbar-restriction-lock-overlay
22646 'help-echo
"Agendas are currently limited to this item.")
22647 (org-detach-overlay org-speedbar-restriction-lock-overlay
)
22649 (defun org-agenda-set-restriction-lock (&optional type
)
22650 "Set restriction lock for agenda, to current subtree or file.
22651 Restriction will be the file if TYPE is `file', or if type is the
22652 universal prefix '(4), or if the cursor is before the first headline
22653 in the file. Otherwise, restriction will be to the current subtree."
22655 (and (equal type
'(4)) (setq type
'file
))
22658 ((org-at-heading-p) 'subtree
)
22659 ((condition-case nil
(org-back-to-heading t
) (error nil
))
22662 (if (eq type
'subtree
)
22664 (setq org-agenda-restrict t
)
22665 (setq org-agenda-overriding-restriction
'subtree
)
22666 (put 'org-agenda-files
'org-restrict
22667 (list (buffer-file-name (buffer-base-buffer))))
22668 (org-back-to-heading t
)
22669 (org-move-overlay org-agenda-restriction-lock-overlay
(point) (point-at-eol))
22670 (move-marker org-agenda-restrict-begin
(point))
22671 (move-marker org-agenda-restrict-end
22672 (save-excursion (org-end-of-subtree t
)))
22673 (message "Locking agenda restriction to subtree"))
22674 (put 'org-agenda-files
'org-restrict
22675 (list (buffer-file-name (buffer-base-buffer))))
22676 (setq org-agenda-restrict nil
)
22677 (setq org-agenda-overriding-restriction
'file
)
22678 (move-marker org-agenda-restrict-begin nil
)
22679 (move-marker org-agenda-restrict-end nil
)
22680 (message "Locking agenda restriction to file"))
22681 (setq current-prefix-arg nil
)
22682 (org-agenda-maybe-redo))
22684 (defun org-agenda-remove-restriction-lock (&optional noupdate
)
22685 "Remove the agenda restriction lock."
22687 (org-detach-overlay org-agenda-restriction-lock-overlay
)
22688 (org-detach-overlay org-speedbar-restriction-lock-overlay
)
22689 (setq org-agenda-overriding-restriction nil
)
22690 (setq org-agenda-restrict nil
)
22691 (put 'org-agenda-files
'org-restrict nil
)
22692 (move-marker org-agenda-restrict-begin nil
)
22693 (move-marker org-agenda-restrict-end nil
)
22694 (setq current-prefix-arg nil
)
22695 (message "Agenda restriction lock removed")
22696 (or noupdate
(org-agenda-maybe-redo)))
22698 (defun org-agenda-maybe-redo ()
22699 "If there is any window showing the agenda view, update it."
22700 (let ((w (get-buffer-window org-agenda-buffer-name t
))
22701 (w0 (selected-window)))
22706 (if org-agenda-overriding-restriction
22707 (message "Agenda view shifted to new %s restriction"
22708 org-agenda-overriding-restriction
)
22709 (message "Agenda restriction lock removed")))))
22711 ;;; Agenda commands
22713 (defun org-agenda-check-type (error &rest types
)
22714 "Check if agenda buffer is of allowed type.
22715 If ERROR is non-nil, throw an error, otherwise just return nil."
22716 (if (memq org-agenda-type types
)
22719 (error "Not allowed in %s-type agenda buffers" org-agenda-type
)
22722 (defun org-agenda-quit ()
22723 "Exit agenda by removing the window or the buffer."
22725 (let ((buf (current-buffer)))
22726 (if (not (one-window-p)) (delete-window))
22728 (org-agenda-reset-markers)
22729 (org-columns-remove-overlays))
22730 ;; Maybe restore the pre-agenda window configuration.
22731 (and org-agenda-restore-windows-after-quit
22732 (not (eq org-agenda-window-setup
'other-frame
))
22733 org-pre-agenda-window-conf
22734 (set-window-configuration org-pre-agenda-window-conf
)))
22736 (defun org-agenda-exit ()
22737 "Exit agenda by removing the window or the buffer.
22738 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
22739 Org-mode buffers visited directly by the user will not be touched."
22741 (org-release-buffers org-agenda-new-buffers
)
22742 (setq org-agenda-new-buffers nil
)
22745 (defun org-agenda-execute (arg)
22746 "Execute another agenda command, keeping same window.\\<global-map>
22747 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
22749 (let ((org-agenda-window-setup 'current-window
))
22752 (defun org-save-all-org-buffers ()
22753 "Save all Org-mode buffers without user confirmation."
22755 (message "Saving all Org-mode buffers...")
22756 (save-some-buffers t
'org-mode-p
)
22757 (message "Saving all Org-mode buffers... done"))
22759 (defun org-agenda-redo ()
22761 When this is the global TODO list, a prefix argument will be interpreted."
22763 (let* ((org-agenda-keep-modes t
)
22764 (line (org-current-line))
22765 (window-line (- line
(org-current-line (window-start))))
22766 (lprops (get 'org-agenda-redo-command
'org-lprops
)))
22767 (message "Rebuilding agenda buffer...")
22768 (org-let lprops
'(eval org-agenda-redo-command
))
22769 (setq org-agenda-undo-list nil
22770 org-agenda-pending-undo-list nil
)
22771 (message "Rebuilding agenda buffer...done")
22773 (recenter window-line
)))
22775 (defun org-agenda-manipulate-query-add ()
22776 "Manipulate the query by adding a search term with positive selection.
22777 Positive selection means, the term must be matched for selection of an entry."
22779 (org-agenda-manipulate-query ?\
[))
22780 (defun org-agenda-manipulate-query-subtract ()
22781 "Manipulate the query by adding a search term with negative selection.
22782 Negative selection means, term must not be matched for selection of an entry."
22784 (org-agenda-manipulate-query ?\
]))
22785 (defun org-agenda-manipulate-query-add-re ()
22786 "Manipulate the query by adding a search regexp with positive selection.
22787 Positive selection means, the regexp must match for selection of an entry."
22789 (org-agenda-manipulate-query ?\
{))
22790 (defun org-agenda-manipulate-query-subtract-re ()
22791 "Manipulate the query by adding a search regexp with negative selection.
22792 Negative selection means, regexp must not match for selection of an entry."
22794 (org-agenda-manipulate-query ?\
}))
22795 (defun org-agenda-manipulate-query (char)
22797 ((eq org-agenda-type
'search
)
22799 'org-agenda-query-string
22800 (cdr (assoc char
'((?\
[ .
" +") (?\
] .
" -")
22801 (?\
{ .
" +{}") (?\
} .
" -{}")))))
22802 (setq org-agenda-redo-command
22803 (list 'org-search-view
22804 (+ (length org-agenda-query-string
)
22805 (if (member char
'(?\
{ ?\
})) 0 1))
22806 org-agenda-query-string
))
22807 (set-register org-agenda-query-register org-agenda-query-string
)
22809 (t (error "Canot manipulate query for %s-type agenda buffers"
22810 org-agenda-type
))))
22812 (defun org-add-to-string (var string
)
22813 (set var
(concat (symbol-value var
) string
)))
22815 (defun org-agenda-goto-date (date)
22816 "Jump to DATE in agenda."
22817 (interactive (list (org-read-date)))
22818 (org-agenda-list nil date
))
22820 (defun org-agenda-goto-today ()
22823 (org-agenda-check-type t
'timeline
'agenda
)
22824 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t
)))
22826 (tdpos (goto-char tdpos
))
22827 ((eq org-agenda-type
'agenda
)
22828 (let* ((sd (time-to-days
22829 (time-subtract (current-time)
22830 (list 0 (* 3600 org-extend-today-until
) 0))))
22831 (comp (org-agenda-compute-time-span sd org-agenda-span
))
22832 (org-agenda-overriding-arguments org-agenda-last-arguments
))
22833 (setf (nth 1 org-agenda-overriding-arguments
) (car comp
))
22834 (setf (nth 2 org-agenda-overriding-arguments
) (cdr comp
))
22836 (org-agenda-find-same-or-today-or-agenda)))
22837 (t (error "Cannot find today")))))
22839 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt
)
22841 (or (and cnt
(text-property-any (point-min) (point-max) 'org-day-cnt cnt
))
22842 (text-property-any (point-min) (point-max) 'org-today t
)
22843 (text-property-any (point-min) (point-max) 'org-agenda-type
'agenda
)
22846 (defun org-agenda-later (arg)
22847 "Go forward in time by thee current span.
22848 With prefix ARG, go forward that many times the current span."
22850 (org-agenda-check-type t
'agenda
)
22851 (let* ((span org-agenda-span
)
22852 (sd org-starting-day
)
22853 (greg (calendar-gregorian-from-absolute sd
))
22854 (cnt (get-text-property (point) 'org-day-cnt
))
22858 (setq sd
(+ arg sd
) nd
1))
22860 (setq sd
(+ (* 7 arg
) sd
) nd
7))
22862 (setq greg2
(list (+ (car greg
) arg
) (nth 1 greg
) (nth 2 greg
))
22863 sd
(calendar-absolute-from-gregorian greg2
))
22864 (setcar greg2
(1+ (car greg2
)))
22865 (setq nd
(- (calendar-absolute-from-gregorian greg2
) sd
)))
22867 (setq greg2
(list (car greg
) (nth 1 greg
) (+ arg
(nth 2 greg
)))
22868 sd
(calendar-absolute-from-gregorian greg2
))
22869 (setcar (nthcdr 2 greg2
) (1+ (nth 2 greg2
)))
22870 (setq nd
(- (calendar-absolute-from-gregorian greg2
) sd
))))
22871 (let ((org-agenda-overriding-arguments
22872 (list (car org-agenda-last-arguments
) sd nd t
)))
22874 (org-agenda-find-same-or-today-or-agenda cnt
))))
22876 (defun org-agenda-earlier (arg)
22877 "Go backward in time by the current span.
22878 With prefix ARG, go backward that many times the current span."
22880 (org-agenda-later (- arg
)))
22882 (defun org-agenda-day-view ()
22883 "Switch to daily view for agenda."
22885 (setq org-agenda-ndays
1)
22886 (org-agenda-change-time-span 'day
))
22887 (defun org-agenda-week-view ()
22888 "Switch to daily view for agenda."
22890 (setq org-agenda-ndays
7)
22891 (org-agenda-change-time-span 'week
))
22892 (defun org-agenda-month-view ()
22893 "Switch to daily view for agenda."
22895 (org-agenda-change-time-span 'month
))
22896 (defun org-agenda-year-view ()
22897 "Switch to daily view for agenda."
22899 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
22900 (org-agenda-change-time-span 'year
)
22903 (defun org-agenda-change-time-span (span)
22904 "Change the agenda view to SPAN.
22905 SPAN may be `day', `week', `month', `year'."
22906 (org-agenda-check-type t
'agenda
)
22907 (if (equal org-agenda-span span
)
22908 (error "Viewing span is already \"%s\"" span
))
22909 (let* ((sd (or (get-text-property (point) 'day
)
22911 (computed (org-agenda-compute-time-span sd span
))
22912 (org-agenda-overriding-arguments
22913 (list (car org-agenda-last-arguments
)
22914 (car computed
) (cdr computed
) t
)))
22916 (org-agenda-find-same-or-today-or-agenda))
22917 (org-agenda-set-mode-name)
22918 (message "Switched to %s view" span
))
22920 (defun org-agenda-compute-time-span (sd span
)
22921 "Compute starting date and number of days for agenda.
22922 SPAN may be `day', `week', `month', `year'. The return value
22923 is a cons cell with the starting date and the number of days,
22924 so that the date SD will be in that range."
22925 (let* ((greg (calendar-gregorian-from-absolute sd
))
22931 (let* ((nt (calendar-day-of-week
22932 (calendar-gregorian-from-absolute sd
)))
22933 (d (if org-agenda-start-on-weekday
22934 (- nt org-agenda-start-on-weekday
)
22936 (setq sd
(- sd
(+ (if (< d
0) 7 0) d
)))
22939 (setq sd
(calendar-absolute-from-gregorian
22940 (list (car greg
) 1 (nth 2 greg
)))
22941 nd
(- (calendar-absolute-from-gregorian
22942 (list (1+ (car greg
)) 1 (nth 2 greg
)))
22945 (setq sd
(calendar-absolute-from-gregorian
22946 (list 1 1 (nth 2 greg
)))
22947 nd
(- (calendar-absolute-from-gregorian
22948 (list 1 1 (1+ (nth 2 greg
))))
22952 ;; FIXME: does not work if user makes date format that starts with a blank
22953 (defun org-agenda-next-date-line (&optional arg
)
22954 "Jump to the next line indicating a date in agenda buffer."
22956 (org-agenda-check-type t
'agenda
'timeline
)
22957 (beginning-of-line 1)
22958 (if (looking-at "^\\S-") (forward-char 1))
22959 (if (not (re-search-forward "^\\S-" nil t arg
))
22962 (error "No next date after this line in this buffer")))
22963 (goto-char (match-beginning 0)))
22965 (defun org-agenda-previous-date-line (&optional arg
)
22966 "Jump to the previous line indicating a date in agenda buffer."
22968 (org-agenda-check-type t
'agenda
'timeline
)
22969 (beginning-of-line 1)
22970 (if (not (re-search-backward "^\\S-" nil t arg
))
22971 (error "No previous date before this line in this buffer")))
22973 ;; Initialize the highlight
22974 (defvar org-hl
(org-make-overlay 1 1))
22975 (org-overlay-put org-hl
'face
'highlight
)
22977 (defun org-highlight (begin end
&optional buffer
)
22978 "Highlight a region with overlay."
22979 (funcall (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
)
22980 org-hl begin end
(or buffer
(current-buffer))))
22982 (defun org-unhighlight ()
22983 "Detach overlay INDEX."
22984 (funcall (if (featurep 'xemacs
) 'detach-extent
'delete-overlay
) org-hl
))
22986 ;; FIXME this is currently not used.
22987 (defun org-highlight-until-next-command (beg end
&optional buffer
)
22988 (org-highlight beg end buffer
)
22989 (add-hook 'pre-command-hook
'org-unhighlight-once
))
22990 (defun org-unhighlight-once ()
22991 (remove-hook 'pre-command-hook
'org-unhighlight-once
)
22994 (defun org-agenda-follow-mode ()
22995 "Toggle follow mode in an agenda buffer."
22997 (setq org-agenda-follow-mode
(not org-agenda-follow-mode
))
22998 (org-agenda-set-mode-name)
22999 (message "Follow mode is %s"
23000 (if org-agenda-follow-mode
"on" "off")))
23002 (defun org-agenda-log-mode ()
23003 "Toggle log mode in an agenda buffer."
23005 (org-agenda-check-type t
'agenda
'timeline
)
23006 (setq org-agenda-show-log
(not org-agenda-show-log
))
23007 (org-agenda-set-mode-name)
23009 (message "Log mode is %s"
23010 (if org-agenda-show-log
"on" "off")))
23012 (defun org-agenda-toggle-diary ()
23013 "Toggle diary inclusion in an agenda buffer."
23015 (org-agenda-check-type t
'agenda
)
23016 (setq org-agenda-include-diary
(not org-agenda-include-diary
))
23018 (org-agenda-set-mode-name)
23019 (message "Diary inclusion turned %s"
23020 (if org-agenda-include-diary
"on" "off")))
23022 (defun org-agenda-toggle-time-grid ()
23023 "Toggle time grid in an agenda buffer."
23025 (org-agenda-check-type t
'agenda
)
23026 (setq org-agenda-use-time-grid
(not org-agenda-use-time-grid
))
23028 (org-agenda-set-mode-name)
23029 (message "Time-grid turned %s"
23030 (if org-agenda-use-time-grid
"on" "off")))
23032 (defun org-agenda-set-mode-name ()
23033 "Set the mode name to indicate all the small mode settings."
23035 (concat "Org-Agenda"
23036 (if (equal org-agenda-ndays
1) " Day" "")
23037 (if (equal org-agenda-ndays
7) " Week" "")
23038 (if org-agenda-follow-mode
" Follow" "")
23039 (if org-agenda-include-diary
" Diary" "")
23040 (if org-agenda-use-time-grid
" Grid" "")
23041 (if org-agenda-show-log
" Log" "")))
23042 (force-mode-line-update))
23044 (defun org-agenda-post-command-hook ()
23045 (and (eolp) (not (bolp)) (backward-char 1))
23046 (setq org-agenda-type
(get-text-property (point) 'org-agenda-type
))
23047 (if (and org-agenda-follow-mode
23048 (get-text-property (point) 'org-marker
))
23049 (org-agenda-show)))
23051 (defun org-agenda-show-priority ()
23052 "Show the priority of the current item.
23053 This priority is composed of the main priority given with the [#A] cookies,
23054 and by additional input from the age of a schedules or deadline entry."
23056 (let* ((pri (get-text-property (point-at-bol) 'priority
)))
23057 (message "Priority is %d" (if pri pri -
1000))))
23059 (defun org-agenda-show-tags ()
23060 "Show the tags applicable to the current item."
23062 (let* ((tags (get-text-property (point-at-bol) 'tags
)))
23064 (message "Tags are :%s:"
23065 (org-no-properties (mapconcat 'identity tags
":")))
23066 (message "No tags associated with this line"))))
23068 (defun org-agenda-goto (&optional highlight
)
23069 "Go to the Org-mode file which contains the item at point."
23071 (let* ((marker (or (get-text-property (point) 'org-marker
)
23072 (org-agenda-error)))
23073 (buffer (marker-buffer marker
))
23074 (pos (marker-position marker
)))
23075 (switch-to-buffer-other-window buffer
)
23079 (org-show-context 'agenda
)
23081 (and (outline-next-heading)
23082 (org-flag-heading nil
)))) ; show the next heading
23083 (recenter (/ (window-height) 2))
23084 (run-hooks 'org-agenda-after-show-hook
)
23085 (and highlight
(org-highlight (point-at-bol) (point-at-eol)))))
23087 (defvar org-agenda-after-show-hook nil
23088 "Normal hook run after an item has been shown from the agenda.
23089 Point is in the buffer where the item originated.")
23091 (defun org-agenda-kill ()
23092 "Kill the entry or subtree belonging to the current agenda entry."
23094 (or (eq major-mode
'org-agenda-mode
) (error "Not in agenda"))
23095 (let* ((marker (or (get-text-property (point) 'org-marker
)
23096 (org-agenda-error)))
23097 (buffer (marker-buffer marker
))
23098 (pos (marker-position marker
))
23099 (type (get-text-property (point) 'type
))
23100 dbeg dend
(n 0) conf
)
23101 (org-with-remote-undo buffer
23102 (with-current-buffer buffer
23105 (if (and (org-mode-p) (not (member type
'("sexp"))))
23106 (setq dbeg
(progn (org-back-to-heading t
) (point))
23107 dend
(org-end-of-subtree t t
))
23108 (setq dbeg
(point-at-bol)
23109 dend
(min (point-max) (1+ (point-at-eol)))))
23111 (while (re-search-forward "^[ \t]*\\S-" dend t
) (setq n
(1+ n
)))))
23112 (setq conf
(or (eq t org-agenda-confirm-kill
)
23113 (and (numberp org-agenda-confirm-kill
)
23114 (> n org-agenda-confirm-kill
))))
23117 (format "Delete entry with %d lines in buffer \"%s\"? "
23118 n
(buffer-name buffer
))))
23120 (org-remove-subtree-entries-from-agenda buffer dbeg dend
)
23121 (with-current-buffer buffer
(delete-region dbeg dend
))
23122 (message "Agenda item and source killed"))))
23124 (defun org-agenda-archive ()
23125 "Kill the entry or subtree belonging to the current agenda entry."
23127 (or (eq major-mode
'org-agenda-mode
) (error "Not in agenda"))
23128 (let* ((marker (or (get-text-property (point) 'org-marker
)
23129 (org-agenda-error)))
23130 (buffer (marker-buffer marker
))
23131 (pos (marker-position marker
)))
23132 (org-with-remote-undo buffer
23133 (with-current-buffer buffer
23137 (org-remove-subtree-entries-from-agenda)
23138 (org-back-to-heading t
)
23139 (org-archive-subtree))
23140 (error "Archiving works only in Org-mode files"))))))
23142 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end
)
23143 "Remove all lines in the agenda that correspond to a given subtree.
23144 The subtree is the one in buffer BUF, starting at BEG and ending at END.
23145 If this information is not given, the function uses the tree at point."
23146 (let ((buf (or buf
(current-buffer))) m p
)
23148 (unless (and beg end
)
23149 (org-back-to-heading t
)
23151 (org-end-of-subtree t
)
23152 (setq end
(point)))
23153 (set-buffer (get-buffer org-agenda-buffer-name
))
23155 (goto-char (point-max))
23156 (beginning-of-line 1)
23157 (while (not (bobp))
23158 (when (and (setq m
(get-text-property (point) 'org-marker
))
23159 (equal buf
(marker-buffer m
))
23160 (setq p
(marker-position m
))
23163 (let ((inhibit-read-only t
))
23164 (delete-region (point-at-bol) (1+ (point-at-eol)))))
23165 (beginning-of-line 0))))))
23167 (defun org-agenda-open-link ()
23168 "Follow the link in the current line, if any."
23170 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local
)
23173 (narrow-to-region (point-at-bol) (point-at-eol))
23174 (org-open-at-point))))
23176 (defun org-agenda-copy-local-variable (var)
23177 "Get a variable from a referenced buffer and install it here."
23178 (let ((m (get-text-property (point) 'org-marker
)))
23179 (when (and m
(buffer-live-p (marker-buffer m
)))
23180 (org-set-local var
(with-current-buffer (marker-buffer m
)
23181 (symbol-value var
))))))
23183 (defun org-agenda-switch-to (&optional delete-other-windows
)
23184 "Go to the Org-mode file which contains the item at point."
23186 (let* ((marker (or (get-text-property (point) 'org-marker
)
23187 (org-agenda-error)))
23188 (buffer (marker-buffer marker
))
23189 (pos (marker-position marker
)))
23190 (switch-to-buffer buffer
)
23191 (and delete-other-windows
(delete-other-windows))
23195 (org-show-context 'agenda
)
23197 (and (outline-next-heading)
23198 (org-flag-heading nil
)))))) ; show the next heading
23200 (defun org-agenda-goto-mouse (ev)
23201 "Go to the Org-mode file which contains the item at the mouse click."
23203 (mouse-set-point ev
)
23206 (defun org-agenda-show ()
23207 "Display the Org-mode file which contains the item at point."
23209 (let ((win (selected-window)))
23210 (org-agenda-goto t
)
23211 (select-window win
)))
23213 (defun org-agenda-recenter (arg)
23214 "Display the Org-mode file which contains the item at point and recenter."
23216 (let ((win (selected-window)))
23217 (org-agenda-goto t
)
23219 (select-window win
)))
23221 (defun org-agenda-show-mouse (ev)
23222 "Display the Org-mode file which contains the item at the mouse click."
23224 (mouse-set-point ev
)
23227 (defun org-agenda-check-no-diary ()
23228 "Check if the entry is a diary link and abort if yes."
23229 (if (get-text-property (point) 'org-agenda-diary-link
)
23230 (org-agenda-error)))
23232 (defun org-agenda-error ()
23233 (error "Command not allowed in this line"))
23235 (defun org-agenda-tree-to-indirect-buffer ()
23236 "Show the subtree corresponding to the current entry in an indirect buffer.
23237 This calls the command `org-tree-to-indirect-buffer' from the original
23239 With numerical prefix arg ARG, go up to this level and then take that tree.
23240 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
23243 (org-agenda-check-no-diary)
23244 (let* ((marker (or (get-text-property (point) 'org-marker
)
23245 (org-agenda-error)))
23246 (buffer (marker-buffer marker
))
23247 (pos (marker-position marker
)))
23248 (with-current-buffer buffer
23251 (call-interactively 'org-tree-to-indirect-buffer
)))))
23253 (defvar org-last-heading-marker
(make-marker)
23254 "Marker pointing to the headline that last changed its TODO state
23255 by a remote command from the agenda.")
23257 (defun org-agenda-todo-nextset ()
23258 "Switch TODO entry to next sequence."
23260 (org-agenda-todo 'nextset
))
23262 (defun org-agenda-todo-previousset ()
23263 "Switch TODO entry to previous sequence."
23265 (org-agenda-todo 'previousset
))
23267 (defun org-agenda-todo (&optional arg
)
23268 "Cycle TODO state of line at point, also in Org-mode file.
23269 This changes the line at point, all other lines in the agenda referring to
23270 the same tree node, and the headline of the tree node in the Org-mode file."
23272 (org-agenda-check-no-diary)
23273 (let* ((col (current-column))
23274 (marker (or (get-text-property (point) 'org-marker
)
23275 (org-agenda-error)))
23276 (buffer (marker-buffer marker
))
23277 (pos (marker-position marker
))
23278 (hdmarker (get-text-property (point) 'org-hd-marker
))
23279 (inhibit-read-only t
)
23281 (org-with-remote-undo buffer
23282 (with-current-buffer buffer
23285 (org-show-context 'agenda
)
23287 (and (outline-next-heading)
23288 (org-flag-heading nil
))) ; show the next heading
23290 (and (bolp) (forward-char 1))
23291 (setq newhead
(org-get-heading))
23293 (org-back-to-heading)
23294 (move-marker org-last-heading-marker
(point))))
23295 (beginning-of-line 1)
23297 (org-agenda-change-all-lines newhead hdmarker
'fixface
))
23298 (move-to-column col
))))
23300 (defun org-agenda-change-all-lines (newhead hdmarker
&optional fixface
)
23301 "Change all lines in the agenda buffer which match HDMARKER.
23302 The new content of the line will be NEWHEAD (as modified by
23303 `org-format-agenda-item'). HDMARKER is checked with
23304 `equal' against all `org-hd-marker' text properties in the file.
23305 If FIXFACE is non-nil, the face of each item is modified acording to
23306 the new TODO state."
23307 (let* ((inhibit-read-only t
)
23308 props m pl undone-face done-face finish new dotime cat tags
)
23310 (goto-char (point-max))
23311 (beginning-of-line 1)
23312 (while (not finish
)
23313 (setq finish
(bobp))
23314 (when (and (setq m
(get-text-property (point) 'org-hd-marker
))
23315 (equal m hdmarker
))
23316 (setq props
(text-properties-at (point))
23317 dotime
(get-text-property (point) 'dotime
)
23318 cat
(get-text-property (point) 'org-category
)
23319 tags
(get-text-property (point) 'tags
)
23320 new
(org-format-agenda-item "x" newhead cat tags dotime
'noprefix
)
23321 pl
(get-text-property (point) 'prefix-length
)
23322 undone-face
(get-text-property (point) 'undone-face
)
23323 done-face
(get-text-property (point) 'done-face
))
23324 (move-to-column pl
)
23327 (beginning-of-line 1)
23328 (and (looking-at ".*\n?") (replace-match "")))
23330 (replace-match new t t
)
23331 (beginning-of-line 1)
23332 (add-text-properties (point-at-bol) (point-at-eol) props
)
23334 (add-text-properties
23335 (point-at-bol) (point-at-eol)
23337 (if org-last-todo-state-is-todo
23338 undone-face done-face
))))
23339 (org-agenda-highlight-todo 'line
)
23340 (beginning-of-line 1))
23341 (t (error "Line update did not work"))))
23342 (beginning-of-line 0)))
23343 (org-finalize-agenda)))
23345 (defun org-agenda-align-tags (&optional line
)
23346 "Align all tags in agenda items to `org-agenda-tags-column'."
23347 (let ((inhibit-read-only t
) l c
)
23349 (goto-char (if line
(point-at-bol) (point-min)))
23350 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
23351 (if line
(point-at-eol) nil
) t
)
23352 (add-text-properties
23353 (match-beginning 2) (match-end 2)
23354 (list 'face
(delq nil
(list 'org-tag
(get-text-property
23355 (match-beginning 2) 'face
)))))
23356 (setq l
(- (match-end 2) (match-beginning 2))
23357 c
(if (< org-agenda-tags-column
0)
23358 (- (abs org-agenda-tags-column
) l
)
23359 org-agenda-tags-column
))
23360 (delete-region (match-beginning 1) (match-end 1))
23361 (goto-char (match-beginning 1))
23362 (insert (org-add-props
23363 (make-string (max 1 (- c
(current-column))) ?\
)
23364 (text-properties-at (point))))))))
23366 (defun org-agenda-priority-up ()
23367 "Increase the priority of line at point, also in Org-mode file."
23369 (org-agenda-priority 'up
))
23371 (defun org-agenda-priority-down ()
23372 "Decrease the priority of line at point, also in Org-mode file."
23374 (org-agenda-priority 'down
))
23376 (defun org-agenda-priority (&optional force-direction
)
23377 "Set the priority of line at point, also in Org-mode file.
23378 This changes the line at point, all other lines in the agenda referring to
23379 the same tree node, and the headline of the tree node in the Org-mode file."
23381 (org-agenda-check-no-diary)
23382 (let* ((marker (or (get-text-property (point) 'org-marker
)
23383 (org-agenda-error)))
23384 (hdmarker (get-text-property (point) 'org-hd-marker
))
23385 (buffer (marker-buffer hdmarker
))
23386 (pos (marker-position hdmarker
))
23387 (inhibit-read-only t
)
23389 (org-with-remote-undo buffer
23390 (with-current-buffer buffer
23393 (org-show-context 'agenda
)
23395 (and (outline-next-heading)
23396 (org-flag-heading nil
))) ; show the next heading
23397 (funcall 'org-priority force-direction
)
23399 (setq newhead
(org-get-heading)))
23400 (org-agenda-change-all-lines newhead hdmarker
)
23401 (beginning-of-line 1))))
23403 (defun org-get-tags-at (&optional pos
)
23404 "Get a list of all headline tags applicable at POS.
23405 POS defaults to point. If tags are inherited, the list contains
23406 the targets in the same sequence as the headlines appear, i.e.
23407 the tags of the current headline come last."
23409 (let (tags lastpos
)
23413 (goto-char (or pos
(point)))
23415 (condition-case nil
23417 (org-back-to-heading t
)
23418 (while (not (equal lastpos
(point)))
23419 (setq lastpos
(point))
23420 (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
23421 (setq tags
(append (org-split-string
23422 (org-match-string-no-properties 1) ":")
23424 (or org-use-tag-inheritance
(error ""))
23425 (org-up-heading-all 1)))
23429 ;; FIXME: should fix the tags property of the agenda line.
23430 (defun org-agenda-set-tags ()
23431 "Set tags for the current headline."
23433 (org-agenda-check-no-diary)
23434 (if (and (org-region-active-p) (interactive-p))
23435 (call-interactively 'org-change-tag-in-region
)
23436 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
23437 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
23438 (org-agenda-error)))
23439 (buffer (marker-buffer hdmarker
))
23440 (pos (marker-position hdmarker
))
23441 (inhibit-read-only t
)
23443 (org-with-remote-undo buffer
23444 (with-current-buffer buffer
23448 (org-show-context 'agenda
))
23450 (and (outline-next-heading)
23451 (org-flag-heading nil
))) ; show the next heading
23453 (call-interactively 'org-set-tags
)
23455 (setq newhead
(org-get-heading)))
23456 (org-agenda-change-all-lines newhead hdmarker
)
23457 (beginning-of-line 1)))))
23459 (defun org-agenda-toggle-archive-tag ()
23460 "Toggle the archive tag for the current entry."
23462 (org-agenda-check-no-diary)
23463 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
23464 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
23465 (org-agenda-error)))
23466 (buffer (marker-buffer hdmarker
))
23467 (pos (marker-position hdmarker
))
23468 (inhibit-read-only t
)
23470 (org-with-remote-undo buffer
23471 (with-current-buffer buffer
23474 (org-show-context 'agenda
)
23476 (and (outline-next-heading)
23477 (org-flag-heading nil
))) ; show the next heading
23478 (call-interactively 'org-toggle-archive-tag
)
23480 (setq newhead
(org-get-heading)))
23481 (org-agenda-change-all-lines newhead hdmarker
)
23482 (beginning-of-line 1))))
23484 (defun org-agenda-date-later (arg &optional what
)
23485 "Change the date of this item to one day later."
23487 (org-agenda-check-type t
'agenda
'timeline
)
23488 (org-agenda-check-no-diary)
23489 (let* ((marker (or (get-text-property (point) 'org-marker
)
23490 (org-agenda-error)))
23491 (buffer (marker-buffer marker
))
23492 (pos (marker-position marker
)))
23493 (org-with-remote-undo buffer
23494 (with-current-buffer buffer
23497 (if (not (org-at-timestamp-p))
23498 (error "Cannot find time stamp"))
23499 (org-timestamp-change arg
(or what
'day
)))
23500 (org-agenda-show-new-time marker org-last-changed-timestamp
))
23501 (message "Time stamp changed to %s" org-last-changed-timestamp
)))
23503 (defun org-agenda-date-earlier (arg &optional what
)
23504 "Change the date of this item to one day earlier."
23506 (org-agenda-date-later (- arg
) what
))
23508 (defun org-agenda-show-new-time (marker stamp
&optional prefix
)
23509 "Show new date stamp via text properties."
23510 ;; We use text properties to make this undoable
23511 (let ((inhibit-read-only t
))
23512 (setq stamp
(concat " " prefix
" => " stamp
))
23514 (goto-char (point-max))
23515 (while (not (bobp))
23516 (when (equal marker
(get-text-property (point) 'org-marker
))
23517 (move-to-column (- (window-width) (length stamp
)) t
)
23518 (if (featurep 'xemacs
)
23519 ;; Use `duplicable' property to trigger undo recording
23520 (let ((ex (make-extent nil nil
))
23521 (gl (make-glyph stamp
)))
23522 (set-glyph-face gl
'secondary-selection
)
23523 (set-extent-properties
23524 ex
(list 'invisible t
'end-glyph gl
'duplicable t
))
23525 (insert-extent ex
(1- (point)) (point-at-eol)))
23526 (add-text-properties
23527 (1- (point)) (point-at-eol)
23528 (list 'display
(org-add-props stamp nil
23529 'face
'secondary-selection
))))
23530 (beginning-of-line 1))
23531 (beginning-of-line 0)))))
23533 (defun org-agenda-date-prompt (arg)
23534 "Change the date of this item. Date is prompted for, with default today.
23535 The prefix ARG is passed to the `org-time-stamp' command and can therefore
23536 be used to request time specification in the time stamp."
23538 (org-agenda-check-type t
'agenda
'timeline
)
23539 (org-agenda-check-no-diary)
23540 (let* ((marker (or (get-text-property (point) 'org-marker
)
23541 (org-agenda-error)))
23542 (buffer (marker-buffer marker
))
23543 (pos (marker-position marker
)))
23544 (org-with-remote-undo buffer
23545 (with-current-buffer buffer
23548 (if (not (org-at-timestamp-p))
23549 (error "Cannot find time stamp"))
23550 (org-time-stamp arg
)
23551 (message "Time stamp changed to %s" org-last-changed-timestamp
)))))
23553 (defun org-agenda-schedule (arg)
23554 "Schedule the item at point."
23556 (org-agenda-check-type t
'agenda
'timeline
'todo
'tags
)
23557 (org-agenda-check-no-diary)
23558 (let* ((marker (or (get-text-property (point) 'org-marker
)
23559 (org-agenda-error)))
23560 (type (marker-insertion-type marker
))
23561 (buffer (marker-buffer marker
))
23562 (pos (marker-position marker
))
23563 (org-insert-labeled-timestamps-at-point nil
)
23565 (when type
(message "%s" type
) (sit-for 3))
23566 (set-marker-insertion-type marker t
)
23567 (org-with-remote-undo buffer
23568 (with-current-buffer buffer
23571 (setq ts
(org-schedule arg
)))
23572 (org-agenda-show-new-time marker ts
"S"))
23573 (message "Item scheduled for %s" ts
)))
23575 (defun org-agenda-deadline (arg)
23576 "Schedule the item at point."
23578 (org-agenda-check-type t
'agenda
'timeline
'todo
'tags
)
23579 (org-agenda-check-no-diary)
23580 (let* ((marker (or (get-text-property (point) 'org-marker
)
23581 (org-agenda-error)))
23582 (buffer (marker-buffer marker
))
23583 (pos (marker-position marker
))
23584 (org-insert-labeled-timestamps-at-point nil
)
23586 (org-with-remote-undo buffer
23587 (with-current-buffer buffer
23590 (setq ts
(org-deadline arg
)))
23591 (org-agenda-show-new-time marker ts
"S"))
23592 (message "Deadline for this item set to %s" ts
)))
23594 (defun org-get-heading (&optional no-tags
)
23595 "Return the heading of the current entry, without the stars."
23597 (org-back-to-heading t
)
23600 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
23601 "\\*+[ \t]+\\([^\r\n]*\\)"))
23602 (match-string 1) "")))
23604 (defun org-agenda-clock-in (&optional arg
)
23605 "Start the clock on the currently selected item."
23607 (org-agenda-check-no-diary)
23608 (let* ((marker (or (get-text-property (point) 'org-marker
)
23609 (org-agenda-error)))
23610 (pos (marker-position marker
)))
23611 (org-with-remote-undo (marker-buffer marker
)
23612 (with-current-buffer (marker-buffer marker
)
23617 (defun org-agenda-clock-out (&optional arg
)
23618 "Stop the currently running clock."
23620 (unless (marker-buffer org-clock-marker
)
23621 (error "No running clock"))
23622 (org-with-remote-undo (marker-buffer org-clock-marker
)
23625 (defun org-agenda-clock-cancel (&optional arg
)
23626 "Cancel the currently running clock."
23628 (unless (marker-buffer org-clock-marker
)
23629 (error "No running clock"))
23630 (org-with-remote-undo (marker-buffer org-clock-marker
)
23631 (org-clock-cancel)))
23633 (defun org-agenda-diary-entry ()
23634 "Make a diary entry, like the `i' command from the calendar.
23635 All the standard commands work: block, weekly etc."
23637 (org-agenda-check-type t
'agenda
'timeline
)
23638 (require 'diary-lib
)
23639 (let* ((char (progn
23640 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
23641 (read-char-exclusive)))
23642 (cmd (cdr (assoc char
23643 '((?d . insert-diary-entry
)
23644 (?w . insert-weekly-diary-entry
)
23645 (?m . insert-monthly-diary-entry
)
23646 (?y . insert-yearly-diary-entry
)
23647 (?a . insert-anniversary-diary-entry
)
23648 (?b . insert-block-diary-entry
)
23649 (?c . insert-cyclic-diary-entry
)))))
23650 (oldf (symbol-function 'calendar-cursor-to-date
))
23651 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
23653 (mark (or (mark t
) (point))))
23655 (error "No command associated with <%c>" char
))
23656 (unless (and (get-text-property point
'day
)
23657 (or (not (equal ?b char
))
23658 (get-text-property mark
'day
)))
23659 (error "Don't know which date to use for diary entry"))
23660 ;; We implement this by hacking the `calendar-cursor-to-date' function
23661 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
23662 (let ((calendar-mark-ring
23663 (list (calendar-gregorian-from-absolute
23664 (or (get-text-property mark
'day
)
23665 (get-text-property point
'day
))))))
23668 (fset 'calendar-cursor-to-date
23669 (lambda (&optional error
)
23670 (calendar-gregorian-from-absolute
23671 (get-text-property point
'day
))))
23672 (call-interactively cmd
))
23673 (fset 'calendar-cursor-to-date oldf
)))))
23676 (defun org-agenda-execute-calendar-command (cmd)
23677 "Execute a calendar command from the agenda, with the date associated to
23678 the cursor position."
23679 (org-agenda-check-type t
'agenda
'timeline
)
23680 (require 'diary-lib
)
23681 (unless (get-text-property (point) 'day
)
23682 (error "Don't know which date to use for calendar command"))
23683 (let* ((oldf (symbol-function 'calendar-cursor-to-date
))
23685 (date (calendar-gregorian-from-absolute
23686 (get-text-property point
'day
)))
23687 ;; the following 3 vars are needed in the calendar
23688 (displayed-day (extract-calendar-day date
))
23689 (displayed-month (extract-calendar-month date
))
23690 (displayed-year (extract-calendar-year date
)))
23693 (fset 'calendar-cursor-to-date
23694 (lambda (&optional error
)
23695 (calendar-gregorian-from-absolute
23696 (get-text-property point
'day
))))
23697 (call-interactively cmd
))
23698 (fset 'calendar-cursor-to-date oldf
))))
23700 (defun org-agenda-phases-of-moon ()
23701 "Display the phases of the moon for the 3 months around the cursor date."
23703 (org-agenda-execute-calendar-command 'calendar-phases-of-moon
))
23705 (defun org-agenda-holidays ()
23706 "Display the holidays for the 3 months around the cursor date."
23708 (org-agenda-execute-calendar-command 'list-calendar-holidays
))
23710 (defvar calendar-longitude
)
23711 (defvar calendar-latitude
)
23712 (defvar calendar-location-name
)
23714 (defun org-agenda-sunrise-sunset (arg)
23715 "Display sunrise and sunset for the cursor date.
23716 Latitude and longitude can be specified with the variables
23717 `calendar-latitude' and `calendar-longitude'. When called with prefix
23718 argument, latitude and longitude will be prompted for."
23721 (let ((calendar-longitude (if arg nil calendar-longitude
))
23722 (calendar-latitude (if arg nil calendar-latitude
))
23723 (calendar-location-name
23724 (if arg
"the given coordinates" calendar-location-name
)))
23725 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset
)))
23727 (defun org-agenda-goto-calendar ()
23728 "Open the Emacs calendar with the date at the cursor."
23730 (org-agenda-check-type t
'agenda
'timeline
)
23731 (let* ((day (or (get-text-property (point) 'day
)
23732 (error "Don't know which date to open in calendar")))
23733 (date (calendar-gregorian-from-absolute day
))
23734 (calendar-move-hook nil
)
23735 (view-calendar-holidays-initially nil
)
23736 (view-diary-entries-initially nil
))
23738 (calendar-goto-date date
)))
23740 (defun org-calendar-goto-agenda ()
23741 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
23742 This is a command that has to be installed in `calendar-mode-map'."
23744 (org-agenda-list nil
(calendar-absolute-from-gregorian
23745 (calendar-cursor-to-date))
23748 (defun org-agenda-convert-date ()
23750 (org-agenda-check-type t
'agenda
'timeline
)
23751 (let ((day (get-text-property (point) 'day
))
23754 (error "Don't know which date to convert"))
23755 (setq date
(calendar-gregorian-from-absolute day
))
23757 "Gregorian: " (calendar-date-string date
) "\n"
23758 "ISO: " (calendar-iso-date-string date
) "\n"
23759 "Day of Yr: " (calendar-day-of-year-string date
) "\n"
23760 "Julian: " (calendar-julian-date-string date
) "\n"
23761 "Astron. JD: " (calendar-astro-date-string date
)
23762 " (Julian date number at noon UTC)\n"
23763 "Hebrew: " (calendar-hebrew-date-string date
) " (until sunset)\n"
23764 "Islamic: " (calendar-islamic-date-string date
) " (until sunset)\n"
23765 "French: " (calendar-french-date-string date
) "\n"
23766 "Baha'i: " (calendar-bahai-date-string date
) " (until sunset)\n"
23767 "Mayan: " (calendar-mayan-date-string date
) "\n"
23768 "Coptic: " (calendar-coptic-date-string date
) "\n"
23769 "Ethiopic: " (calendar-ethiopic-date-string date
) "\n"
23770 "Persian: " (calendar-persian-date-string date
) "\n"
23771 "Chinese: " (calendar-chinese-date-string date
) "\n"))
23772 (with-output-to-temp-buffer "*Dates*"
23774 (if (fboundp 'fit-window-to-buffer
)
23775 (fit-window-to-buffer (get-buffer-window "*Dates*")))))
23778 ;;;; Embedded LaTeX
23780 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
23781 "Keymap for the minor `org-cdlatex-mode'.")
23783 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
23784 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
23785 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
23786 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
23787 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
23789 (defvar org-cdlatex-texmathp-advice-is-done nil
23790 "Flag remembering if we have applied the advice to texmathp already.")
23792 (define-minor-mode org-cdlatex-mode
23793 "Toggle the minor `org-cdlatex-mode'.
23794 This mode supports entering LaTeX environment and math in LaTeX fragments
23796 \\{org-cdlatex-mode-map}"
23798 (when org-cdlatex-mode
(require 'cdlatex
))
23799 (unless org-cdlatex-texmathp-advice-is-done
23800 (setq org-cdlatex-texmathp-advice-is-done t
)
23801 (defadvice texmathp
(around org-math-always-on activate
)
23802 "Always return t in org-mode buffers.
23803 This is because we want to insert math symbols without dollars even outside
23804 the LaTeX math segments. If Orgmode thinks that point is actually inside
23805 en embedded LaTeX fragement, let texmathp do its job.
23806 \\[org-cdlatex-mode-map]"
23810 ((not (org-mode-p)) ad-do-it
)
23811 ((eq this-command
'cdlatex-math-symbol
)
23812 (setq ad-return-value t
23813 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
23815 (let ((p (org-inside-LaTeX-fragment-p)))
23816 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
23817 (setq ad-return-value t
23818 texmathp-why
'("Org-mode embedded math" .
0))
23819 (if p ad-do-it
)))))))))
23821 (defun turn-on-org-cdlatex ()
23822 "Unconditionally turn on `org-cdlatex-mode'."
23823 (org-cdlatex-mode 1))
23825 (defun org-inside-LaTeX-fragment-p ()
23826 "Test if point is inside a LaTeX fragment.
23827 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
23828 sequence appearing also before point.
23829 Even though the matchers for math are configurable, this function assumes
23830 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
23831 delimiters are skipped when they have been removed by customization.
23832 The return value is nil, or a cons cell with the delimiter and
23833 and the position of this delimiter.
23835 This function does a reasonably good job, but can locally be fooled by
23836 for example currency specifications. For example it will assume being in
23837 inline math after \"$22.34\". The LaTeX fragment formatter will only format
23838 fragments that are properly closed, but during editing, we have to live
23839 with the uncertainty caused by missing closing delimiters. This function
23840 looks only before point, not after."
23842 (let ((pos (point))
23843 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
23845 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
23847 dd-on str
(start 0) m re
)
23850 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
23851 re
(nth 1 (assoc "$" org-latex-regexps
)))
23852 (while (string-match re str start
)
23854 ((= (match-end 0) (length str
))
23855 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
23856 ((= (match-end 0) (- (length str
) 5))
23858 (t (setq start
(match-end 0))))))
23859 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
23861 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
23862 (and (match-beginning 2) (throw 'exit nil
))
23864 (while (re-search-backward "\\$\\$" lim t
)
23865 (setq dd-on
(not dd-on
)))
23867 (if dd-on
(cons "$$" m
))))))
23870 (defun org-try-cdlatex-tab ()
23871 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
23872 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
23873 - inside a LaTeX fragment, or
23874 - after the first word in a line, where an abbreviation expansion could
23875 insert a LaTeX environment."
23876 (when org-cdlatex-mode
23879 (skip-chars-backward "a-zA-Z0-9*")
23880 (skip-chars-backward " \t")
23883 ((org-inside-LaTeX-fragment-p)
23887 (defun org-cdlatex-underscore-caret (&optional arg
)
23888 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
23889 Revert to the normal definition outside of these fragments."
23891 (if (org-inside-LaTeX-fragment-p)
23892 (call-interactively 'cdlatex-sub-superscript
)
23893 (let (org-cdlatex-mode)
23894 (call-interactively (key-binding (vector last-input-event
))))))
23896 (defun org-cdlatex-math-modify (&optional arg
)
23897 "Execute `cdlatex-math-modify' in LaTeX fragments.
23898 Revert to the normal definition outside of these fragments."
23900 (if (org-inside-LaTeX-fragment-p)
23901 (call-interactively 'cdlatex-math-modify
)
23902 (let (org-cdlatex-mode)
23903 (call-interactively (key-binding (vector last-input-event
))))))
23905 (defvar org-latex-fragment-image-overlays nil
23906 "List of overlays carrying the images of latex fragments.")
23907 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
23909 (defun org-remove-latex-fragment-image-overlays ()
23910 "Remove all overlays with LaTeX fragment images in current buffer."
23911 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
23912 (setq org-latex-fragment-image-overlays nil
))
23914 (defun org-preview-latex-fragment (&optional subtree
)
23915 "Preview the LaTeX fragment at point, or all locally or globally.
23916 If the cursor is in a LaTeX fragment, create the image and overlay
23917 it over the source code. If there is no fragment at point, display
23918 all fragments in the current text, from one headline to the next. With
23919 prefix SUBTREE, display all fragments in the current subtree. With a
23920 double prefix `C-u C-u', or when the cursor is before the first headline,
23921 display all fragments in the buffer.
23922 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
23924 (org-remove-latex-fragment-image-overlays)
23927 (let (beg end at msg
)
23929 ((or (equal subtree
'(16))
23930 (not (save-excursion
23931 (re-search-backward (concat "^" outline-regexp
) nil t
))))
23932 (setq beg
(point-min) end
(point-max)
23933 msg
"Creating images for buffer...%s"))
23934 ((equal subtree
'(4))
23935 (org-back-to-heading)
23936 (setq beg
(point) end
(org-end-of-subtree t
)
23937 msg
"Creating images for subtree...%s"))
23939 (if (setq at
(org-inside-LaTeX-fragment-p))
23940 (goto-char (max (point-min) (- (cdr at
) 2)))
23941 (org-back-to-heading))
23942 (setq beg
(point) end
(progn (outline-next-heading) (point))
23943 msg
(if at
"Creating image...%s"
23944 "Creating images for entry...%s"))))
23946 (narrow-to-region beg end
)
23949 (concat "ltxpng/" (file-name-sans-extension
23950 (file-name-nondirectory
23951 buffer-file-name
)))
23952 default-directory
'overlays msg at
'forbuffer
)
23953 (message msg
"done. Use `C-c C-c' to remove images.")))))
23955 (defvar org-latex-regexps
23956 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
23957 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
23958 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
23959 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil
)
23960 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
23961 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
23962 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
23963 "Regular expressions for matching embedded LaTeX.")
23965 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
23966 "Replace LaTeX fragments with links to an image, and produce images."
23967 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
23968 (let* ((prefixnodir (file-name-nondirectory prefix
))
23969 (absprefix (expand-file-name prefix dir
))
23970 (todir (file-name-directory absprefix
))
23971 (opt org-format-latex-options
)
23972 (matchers (plist-get opt
:matchers
))
23973 (re-list org-latex-regexps
)
23974 (cnt 0) txt link beg end re e checkdir
23975 m n block linkfile movefile ov
)
23976 ;; Check if there are old images files with this prefix, and remove them
23977 (when (file-directory-p todir
)
23981 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
23982 ;; Check the different regular expressions
23983 (while (setq e
(pop re-list
))
23984 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
23985 block
(if (nth 3 e
) "\n\n" ""))
23986 (when (member m matchers
)
23987 (goto-char (point-min))
23988 (while (re-search-forward re nil t
)
23989 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
23990 (setq txt
(match-string n
)
23991 beg
(match-beginning n
) end
(match-end n
)
23993 linkfile
(format "%s_%04d.png" prefix cnt
)
23994 movefile
(format "%s_%04d.png" absprefix cnt
)
23995 link
(concat block
"[[file:" linkfile
"]]" block
))
23996 (if msg
(message msg cnt
))
23998 (unless checkdir
; make sure the directory exists
24000 (or (file-directory-p todir
) (make-directory todir
)))
24001 (org-create-formula-image
24002 txt movefile opt forbuffer
)
24005 (setq ov
(org-make-overlay beg end
))
24006 (if (featurep 'xemacs
)
24008 (org-overlay-put ov
'invisible t
)
24011 (make-glyph (vector 'png
:file movefile
))))
24014 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
24015 (push ov org-latex-fragment-image-overlays
)
24017 (delete-region beg end
)
24018 (insert link
))))))))
24020 ;; This function borrows from Ganesh Swami's latex2png.el
24021 (defun org-create-formula-image (string tofile options buffer
)
24022 (let* ((tmpdir (if (featurep 'xemacs
)
24024 temporary-file-directory
))
24025 (texfilebase (make-temp-name
24026 (expand-file-name "orgtex" tmpdir
)))
24027 (texfile (concat texfilebase
".tex"))
24028 (dvifile (concat texfilebase
".dvi"))
24029 (pngfile (concat texfilebase
".png"))
24030 (fnh (face-attribute 'default
:height nil
))
24031 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
24032 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
24033 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
24035 (bg (or (plist-get options
(if buffer
:background
:html-background
))
24037 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
24038 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
24039 (with-temp-file texfile
24040 (insert org-format-latex-header
24041 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
24042 (let ((dir default-directory
))
24043 (condition-case nil
24046 (call-process "latex" nil nil nil texfile
))
24049 (if (not (file-exists-p dvifile
))
24050 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
24051 (call-process "dvipng" nil nil nil
24052 "-E" "-fg" fg
"-bg" bg
24054 ;;"-x" scale "-y" scale
24058 (if (not (file-exists-p pngfile
))
24059 (progn (message "Failed to create png file from %s" texfile
) nil
)
24060 ;; Use the requested file name and clean up
24061 (copy-file pngfile tofile
'replace
)
24062 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
24063 (delete-file (concat texfilebase e
)))
24066 (defun org-dvipng-color (attr)
24067 "Return an rgb color specification for dvipng."
24068 (apply 'format
"rgb %s %s %s"
24069 (mapcar 'org-normalize-color
24070 (color-values (face-attribute 'default attr nil
)))))
24072 (defun org-normalize-color (value)
24073 "Return string to be used as color value for an RGB component."
24074 (format "%g" (/ value
65535.0)))
24078 ;;; Variables, constants, and parameter plists
24080 (defconst org-level-max
20)
24082 (defvar org-export-html-preamble nil
24083 "Preamble, to be inserted just after <body>. Set by publishing functions.")
24084 (defvar org-export-html-postamble nil
24085 "Preamble, to be inserted just before </body>. Set by publishing functions.")
24086 (defvar org-export-html-auto-preamble t
24087 "Should default preamble be inserted? Set by publishing functions.")
24088 (defvar org-export-html-auto-postamble t
24089 "Should default postamble be inserted? Set by publishing functions.")
24090 (defvar org-current-export-file nil
) ; dynamically scoped parameter
24091 (defvar org-current-export-dir nil
) ; dynamically scoped parameter
24094 (defconst org-export-plist-vars
24095 '((:language . org-export-default-language
)
24096 (:customtime . org-display-custom-times
)
24097 (:headline-levels . org-export-headline-levels
)
24098 (:section-numbers . org-export-with-section-numbers
)
24099 (:table-of-contents . org-export-with-toc
)
24100 (:preserve-breaks . org-export-preserve-breaks
)
24101 (:archived-trees . org-export-with-archived-trees
)
24102 (:emphasize . org-export-with-emphasize
)
24103 (:sub-superscript . org-export-with-sub-superscripts
)
24104 (:special-strings . org-export-with-special-strings
)
24105 (:footnotes . org-export-with-footnotes
)
24106 (:drawers . org-export-with-drawers
)
24107 (:tags . org-export-with-tags
)
24108 (:TeX-macros . org-export-with-TeX-macros
)
24109 (:LaTeX-fragments . org-export-with-LaTeX-fragments
)
24110 (:skip-before-1st-heading . org-export-skip-text-before-1st-heading
)
24111 (:fixed-width . org-export-with-fixed-width
)
24112 (:timestamps . org-export-with-timestamps
)
24113 (:author-info . org-export-author-info
)
24114 (:time-stamp-file . org-export-time-stamp-file
)
24115 (:tables . org-export-with-tables
)
24116 (:table-auto-headline . org-export-highlight-first-table-line
)
24117 (:style . org-export-html-style
)
24118 (:agenda-style . org-agenda-export-html-style
)
24119 (:convert-org-links . org-export-html-link-org-files-as-html
)
24120 (:inline-images . org-export-html-inline-images
)
24121 (:html-extension . org-export-html-extension
)
24122 (:html-table-tag . org-export-html-table-tag
)
24123 (:expand-quoted-html . org-export-html-expand
)
24124 (:timestamp . org-export-html-with-timestamp
)
24125 (:publishing-directory . org-export-publishing-directory
)
24126 (:preamble . org-export-html-preamble
)
24127 (:postamble . org-export-html-postamble
)
24128 (:auto-preamble . org-export-html-auto-preamble
)
24129 (:auto-postamble . org-export-html-auto-postamble
)
24130 (:author . user-full-name
)
24131 (:email . user-mail-address
)))
24133 (defun org-default-export-plist ()
24134 "Return the property list with default settings for the export variables."
24135 (let ((l org-export-plist-vars
) rtn e
)
24136 (while (setq e
(pop l
))
24137 (setq rtn
(cons (car e
) (cons (symbol-value (cdr e
)) rtn
))))
24140 (defun org-infile-export-plist ()
24141 "Return the property list with file-local settings for export."
24146 (let ((re (org-make-options-regexp
24147 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
24148 p key val text options
)
24149 (while (re-search-forward re nil t
)
24150 (setq key
(org-match-string-no-properties 1)
24151 val
(org-match-string-no-properties 2))
24153 ((string-equal key
"TITLE") (setq p
(plist-put p
:title val
)))
24154 ((string-equal key
"AUTHOR")(setq p
(plist-put p
:author val
)))
24155 ((string-equal key
"EMAIL") (setq p
(plist-put p
:email val
)))
24156 ((string-equal key
"DATE") (setq p
(plist-put p
:date val
)))
24157 ((string-equal key
"LANGUAGE") (setq p
(plist-put p
:language val
)))
24158 ((string-equal key
"TEXT")
24159 (setq text
(if text
(concat text
"\n" val
) val
)))
24160 ((string-equal key
"OPTIONS") (setq options val
))))
24161 (setq p
(plist-put p
:text text
))
24163 (let ((op '(("H" .
:headline-levels
)
24164 ("num" .
:section-numbers
)
24165 ("toc" .
:table-of-contents
)
24166 ("\\n" .
:preserve-breaks
)
24167 ("@" .
:expand-quoted-html
)
24168 (":" .
:fixed-width
)
24170 ("^" .
:sub-superscript
)
24171 ("-" .
:special-strings
)
24176 ("TeX" .
:TeX-macros
)
24177 ("LaTeX" .
:LaTeX-fragments
)
24178 ("skip" .
:skip-before-1st-heading
)
24179 ("author" .
:author-info
)
24180 ("timestamp" .
:time-stamp-file
)))
24182 (while (setq o
(pop op
))
24183 (if (string-match (concat (regexp-quote (car o
))
24184 ":\\([^ \t\n\r;,.]*\\)")
24186 (setq p
(plist-put p
(cdr o
)
24187 (car (read-from-string
24188 (match-string 1 options
)))))))))
24191 (defun org-export-directory (type plist
)
24192 (let* ((val (plist-get plist
:publishing-directory
))
24193 (dir (if (listp val
)
24194 (or (cdr (assoc type val
)) ".")
24198 (defun org-skip-comments (lines)
24199 "Skip lines starting with \"#\" and subtrees starting with COMMENT."
24200 (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string
))
24201 (re2 "^\\(\\*+\\)[ \t\n\r]")
24202 (case-fold-search nil
)
24204 (while (setq line
(pop lines
))
24206 ((and (string-match re1 line
)
24207 (setq level
(- (match-end 1) (match-beginning 1))))
24208 ;; Beginning of a COMMENT subtree. Skip it.
24209 (while (and (setq line
(pop lines
))
24210 (or (not (string-match re2 line
))
24211 (> (- (match-end 1) (match-beginning 1)) level
))))
24212 (setq lines
(cons line lines
)))
24213 ((string-match "^#" line
)
24214 ;; an ordinary comment line
24216 ((and org-export-table-remove-special-lines
24217 (string-match "^[ \t]*|" line
)
24218 (or (string-match "^[ \t]*| *[!_^] *|" line
)
24219 (and (string-match "| *<[0-9]+> *|" line
)
24220 (not (string-match "| *[^ <|]" line
)))))
24221 ;; a special table line that should be removed
24223 (t (setq rtn
(cons line rtn
)))))
24226 (defun org-export (&optional arg
)
24228 (let ((help "[t] insert the export option template
24229 \[v] limit export to visible part of outline tree
24231 \[a] export as ASCII
24233 \[h] export as HTML
24234 \[H] export as HTML to temporary buffer
24235 \[R] export region as HTML
24236 \[b] export as HTML and browse immediately
24237 \[x] export as XOXO
24239 \[l] export as LaTeX
24240 \[L] export as LaTeX to temporary buffer
24242 \[i] export current file as iCalendar file
24243 \[I] export all agenda files as iCalendar files
24244 \[c] export agenda files into combined iCalendar file
24246 \[F] publish current file
24247 \[P] publish current project
24248 \[X] publish... (project will be prompted for)
24249 \[A] publish all projects")
24251 '((?t . org-insert-export-options-template
)
24252 (?v . org-export-visible
)
24253 (?a . org-export-as-ascii
)
24254 (?h . org-export-as-html
)
24255 (?b . org-export-as-html-and-open
)
24256 (?H . org-export-as-html-to-buffer
)
24257 (?R . org-export-region-as-html
)
24258 (?x . org-export-as-xoxo
)
24259 (?l . org-export-as-latex
)
24260 (?L . org-export-as-latex-to-buffer
)
24261 (?i . org-export-icalendar-this-file
)
24262 (?I . org-export-icalendar-all-agenda-files
)
24263 (?c . org-export-icalendar-combine-agenda-files
)
24264 (?F . org-publish-current-file
)
24265 (?P . org-publish-current-project
)
24267 (?A . org-publish-all
)))
24269 (save-window-excursion
24270 (delete-other-windows)
24271 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
24273 (message "Select command: ")
24274 (setq r1
(read-char-exclusive)))
24275 (setq r2
(if (< r1
27) (+ r1
96) r1
))
24276 (if (setq ass
(assq r2 cmds
))
24277 (call-interactively (cdr ass
))
24278 (error "No command associated with key %c" r1
))))
24280 (defconst org-html-entities
24288 ("vert" .
"|")
24321 ("Aring") ("AA".
"Å")
24410 ("varepsilon".
"ε")
24423 ("sigmaf") ("varsigma".
"ς")
24431 ("thetasym") ("vartheta".
"ϑ")
24434 ("bull") ("bullet".
"•")
24435 ("hellip") ("dots".
"…")
24445 ("larr") ("leftarrow".
"←") ("gets".
"←")
24446 ("uarr") ("uparrow".
"↑")
24447 ("rarr") ("to".
"→") ("rightarrow".
"→")
24448 ("darr")("downarrow".
"↓")
24449 ("harr") ("leftrightarrow".
"↔")
24450 ("crarr") ("hookleftarrow".
"↵") ; has round hook, not quite CR
24451 ("lArr") ("Leftarrow".
"⇐")
24452 ("uArr") ("Uparrow".
"⇑")
24453 ("rArr") ("Rightarrow".
"⇒")
24454 ("dArr") ("Downarrow".
"⇓")
24455 ("hArr") ("Leftrightarrow".
"⇔")
24457 ("part") ("partial".
"∂")
24458 ("exist") ("exists".
"∃")
24459 ("empty") ("emptyset".
"∅")
24461 ("isin") ("in".
"∈")
24467 ("lowast") ("ast".
"∗")
24469 ("prop") ("proptp".
"∝")
24470 ("infin") ("infty".
"∞")
24471 ("ang") ("angle".
"∠")
24472 ("and") ("wedge".
"∧")
24473 ("or") ("vee".
"∨")
24479 ("cong") ("simeq".
"≅")
24480 ("asymp")("approx".
"≈")
24481 ("ne") ("neq".
"≠")
24485 ("sub") ("subset".
"⊂")
24486 ("sup") ("supset".
"⊃")
24493 ("sdot") ("cdot".
"⋅")
24500 ("loz") ("Diamond".
"◊")
24501 ("spades") ("spadesuit".
"♠")
24502 ("clubs") ("clubsuit".
"♣")
24503 ("hearts") ("diamondsuit".
"♥")
24504 ("diams") ("diamondsuit".
"♦")
24505 ("smile".
"☺") ("blacksmile".
"☻") ("sad".
"☹")
24539 ("arccos".
"arccos")
24540 ("arcsin".
"arcsin")
24541 ("arctan".
"arctan")
24558 ("liminf".
"liminf")
24559 ("limsup".
"limsup")
24572 "Entities for TeX->HTML translation.
24573 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
24574 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
24575 In that case, \"\\ent\" will be translated to \"&other;\".
24576 The list contains HTML entities for Latin-1, Greek and other symbols.
24577 It is supplemented by a number of commonly used TeX macros with appropriate
24578 translations. There is currently no way for users to extend this.")
24580 ;;; General functions for all backends
24582 (defun org-cleaned-string-for-export (string &rest parameters
)
24583 "Cleanup a buffer STRING so that links can be created safely."
24585 (let* ((re-radio (and org-target-link-regexp
24586 (concat "\\([^<]\\)\\(" org-target-link-regexp
"\\)")))
24587 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re
))
24588 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re
))
24589 (re-archive (concat ":" org-archive-tag
":"))
24590 (re-quote (concat "^\\*+[ \t]+" org-quote-string
"\\>"))
24591 (re-commented (concat "^\\*+[ \t]+" org-comment-string
"\\>"))
24592 (htmlp (plist-get parameters
:for-html
))
24593 (asciip (plist-get parameters
:for-ascii
))
24594 (latexp (plist-get parameters
:for-LaTeX
))
24595 (commentsp (plist-get parameters
:comments
))
24596 (archived-trees (plist-get parameters
:archived-trees
))
24597 (inhibit-read-only t
)
24598 (drawers org-drawers
)
24599 (exp-drawers (plist-get parameters
:drawers
))
24600 (outline-regexp "\\*+ ")
24603 (with-current-buffer (get-buffer-create " org-mode-tmp")
24606 ;; Remove license-to-kill stuff
24607 (while (setq p
(text-property-any (point-min) (point-max)
24608 :org-license-to-kill t
))
24609 (delete-region p
(next-single-property-change p
:org-license-to-kill
)))
24611 (let ((org-inhibit-startup t
)) (org-mode))
24612 (untabify (point-min) (point-max))
24614 ;; Get rid of drawers
24615 (unless (eq t exp-drawers
)
24616 (goto-char (point-min))
24617 (let ((re (concat "^[ \t]*:\\("
24620 (org-delete-all exp-drawers
24621 (copy-sequence drawers
))
24623 "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
24624 (while (re-search-forward re nil t
)
24625 (replace-match ""))))
24627 ;; Get the correct stuff before the first headline
24628 (when (plist-get parameters
:skip-before-1st-heading
)
24629 (goto-char (point-min))
24630 (when (re-search-forward "^\\*+[ \t]" nil t
)
24631 (delete-region (point-min) (match-beginning 0))
24632 (goto-char (point-min))
24634 (when (plist-get parameters
:add-text
)
24635 (goto-char (point-min))
24636 (insert (plist-get parameters
:add-text
) "\n"))
24638 ;; Get rid of archived trees
24639 (when (not (eq archived-trees t
))
24640 (goto-char (point-min))
24641 (while (re-search-forward re-archive nil t
)
24642 (if (not (org-on-heading-p t
))
24643 (org-end-of-subtree t
)
24644 (beginning-of-line 1)
24645 (setq a
(if archived-trees
24646 (1+ (point-at-eol)) (point))
24647 b
(org-end-of-subtree t
))
24648 (if (> b a
) (delete-region a b
)))))
24650 ;; Find targets in comments and move them out of comments,
24651 ;; but mark them as targets that should be invisible
24652 (goto-char (point-min))
24653 (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t
)
24654 (replace-match "\\1(INVISIBLE)"))
24656 ;; Protect backend specific stuff, throw away the others.
24658 `((,htmlp
"HTML" "BEGIN_HTML" "END_HTML")
24659 (,asciip
"ASCII" "BEGIN_ASCII" "END_ASCII")
24660 (,latexp
"LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
24662 (goto-char (point-min))
24663 (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t
)
24664 (goto-char (match-end 0))
24665 (while (not (looking-at "#\\+END_EXAMPLE"))
24667 (beginning-of-line 2)))
24668 (goto-char (point-min))
24669 (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t
)
24670 (add-text-properties (match-beginning 0) (match-end 0)
24671 '(org-protected t
)))
24673 (setq fmt
(pop formatters
))
24675 (goto-char (point-min))
24676 (while (re-search-forward (concat "^#\\+" (cadr fmt
)
24677 ":[ \t]*\\(.*\\)") nil t
)
24678 (replace-match "\\1" t
)
24679 (add-text-properties
24680 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
24681 '(org-protected t
))))
24682 (goto-char (point-min))
24683 (while (re-search-forward
24685 (caddr fmt
) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
24686 (cadddr fmt
) "\\>.*\n?") nil t
)
24688 (add-text-properties (match-beginning 1) (1+ (match-end 1))
24689 '(org-protected t
))
24690 (delete-region (match-beginning 0) (match-end 0))))))
24692 ;; Protect quoted subtrees
24693 (goto-char (point-min))
24694 (while (re-search-forward re-quote nil t
)
24695 (goto-char (match-beginning 0))
24697 (add-text-properties (point) (org-end-of-subtree t
)
24698 '(org-protected t
)))
24700 ;; Protect verbatim elements
24701 (goto-char (point-min))
24702 (while (re-search-forward org-verbatim-re nil t
)
24703 (add-text-properties (match-beginning 4) (match-end 4)
24704 '(org-protected t
))
24705 (goto-char (1+ (match-end 4))))
24707 ;; Remove subtrees that are commented
24708 (goto-char (point-min))
24709 (while (re-search-forward re-commented nil t
)
24710 (goto-char (match-beginning 0))
24711 (delete-region (point) (org-end-of-subtree t
)))
24713 ;; Remove special table lines
24714 (when org-export-table-remove-special-lines
24715 (goto-char (point-min))
24716 (while (re-search-forward "^[ \t]*|" nil t
)
24717 (beginning-of-line 1)
24718 (if (or (looking-at "[ \t]*| *[!_^] *|")
24719 (and (looking-at ".*?| *<[0-9]+> *|")
24720 (not (looking-at ".*?| *[^ <|]"))))
24721 (delete-region (max (point-min) (1- (point-at-bol)))
24725 ;; Specific LaTeX stuff
24727 (require 'org-export-latex nil
)
24728 (org-export-latex-cleaned-string))
24731 (org-export-ascii-clean-string))
24733 ;; Specific HTML stuff
24735 ;; Convert LaTeX fragments to images
24736 (when (plist-get parameters
:LaTeX-fragments
)
24738 (concat "ltxpng/" (file-name-sans-extension
24739 (file-name-nondirectory
24740 org-current-export-file
)))
24741 org-current-export-dir nil
"Creating LaTeX image %s"))
24742 (message "Exporting..."))
24744 ;; Remove or replace comments
24745 (goto-char (point-min))
24746 (while (re-search-forward "^#\\(.*\n?\\)" nil t
)
24748 (progn (add-text-properties
24749 (match-beginning 0) (match-end 0) '(org-protected t
))
24750 (replace-match (format commentsp
(match-string 1)) t t
))
24751 (replace-match "")))
24753 ;; Find matches for radio targets and turn them into internal links
24754 (goto-char (point-min))
24756 (while (re-search-forward re-radio nil t
)
24757 (org-if-unprotected
24758 (replace-match "\\1[[\\2]]"))))
24760 ;; Find all links that contain a newline and put them into a single line
24761 (goto-char (point-min))
24762 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t
)
24763 (org-if-unprotected
24764 (replace-match "\\1 \\3")
24765 (goto-char (match-beginning 0))))
24768 ;; Normalize links: Convert angle and plain links into bracket links
24769 ;; Expand link abbreviations
24770 (goto-char (point-min))
24771 (while (re-search-forward re-plain-link nil t
)
24772 (goto-char (1- (match-end 0)))
24773 (org-if-unprotected
24774 (let* ((s (concat (match-string 1) "[[" (match-string 2)
24775 ":" (match-string 3) "]]")))
24776 ;; added 'org-link face to links
24777 (put-text-property 0 (length s
) 'face
'org-link s
)
24778 (replace-match s t t
))))
24779 (goto-char (point-min))
24780 (while (re-search-forward re-angle-link nil t
)
24781 (goto-char (1- (match-end 0)))
24782 (org-if-unprotected
24783 (let* ((s (concat (match-string 1) "[[" (match-string 2)
24784 ":" (match-string 3) "]]")))
24785 (put-text-property 0 (length s
) 'face
'org-link s
)
24786 (replace-match s t t
))))
24787 (goto-char (point-min))
24788 (while (re-search-forward org-bracket-link-regexp nil t
)
24789 (org-if-unprotected
24790 (let* ((s (concat "[[" (setq xx
(save-match-data
24791 (org-link-expand-abbrev (match-string 1))))
24795 (concat "[" xx
"]"))
24797 (put-text-property 0 (length s
) 'face
'org-link s
)
24798 (replace-match s t t
))))
24800 ;; Find multiline emphasis and put them into single line
24801 (when (plist-get parameters
:emph-multiline
)
24802 (goto-char (point-min))
24803 (while (re-search-forward org-emph-re nil t
)
24804 (if (not (= (char-after (match-beginning 3))
24805 (char-after (match-beginning 4))))
24806 (org-if-unprotected
24807 (subst-char-in-region (match-beginning 0) (match-end 0)
24809 (goto-char (1- (match-end 0))))
24810 (goto-char (1+ (match-beginning 0))))))
24812 (setq rtn
(buffer-string)))
24813 (kill-buffer " org-mode-tmp")
24816 (defun org-export-grab-title-from-buffer ()
24817 "Get a title for the current document, from looking at the buffer."
24818 (let ((inhibit-read-only t
))
24820 (goto-char (point-min))
24821 (let ((end (save-excursion (outline-next-heading) (point))))
24822 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t
)
24823 ;; Mark the line so that it will not be exported as normal text.
24825 (add-text-properties (match-beginning 0) (match-end 0)
24826 (list :org-license-to-kill t
)))
24827 ;; Return the title string
24828 (org-trim (match-string 0)))))))
24830 (defun org-export-get-title-from-subtree ()
24831 "Return subtree title and exclude it from export."
24832 (let (title (m (mark)))
24834 (goto-char (region-beginning))
24835 (when (and (org-at-heading-p)
24836 (>= (org-end-of-subtree t t
) (region-end)))
24837 ;; This is a subtree, we take the title from the first heading
24838 (goto-char (region-beginning))
24839 (looking-at org-todo-line-regexp
)
24840 (setq title
(match-string 3))
24842 (add-text-properties (point) (1+ (point-at-eol))
24843 (list :org-license-to-kill t
)))))
24846 (defun org-solidify-link-text (s &optional alist
)
24847 "Take link text and make a safe target out of it."
24852 (org-split-string s
"[ \t\r\n]+") "--"))
24853 (a (assoc rtn alist
)))
24854 (or (cdr a
) rtn
))))
24856 (defun org-get-min-level (lines)
24857 "Get the minimum level in LINES."
24858 (let ((re "^\\(\\*+\\) ") l min
)
24860 (while (setq l
(pop lines
))
24861 (if (string-match re l
)
24862 (throw 'exit
(org-tr-level (length (match-string 1 l
))))))
24865 ;; Variable holding the vector with section numbers
24866 (defvar org-section-numbers
(make-vector org-level-max
0))
24868 (defun org-init-section-numbers ()
24869 "Initialize the vector for the section numbers."
24871 (numbers (nreverse (org-split-string "" "\\.")))
24872 (depth (1- (length org-section-numbers
)))
24873 (i depth
) number-string
)
24876 (aset org-section-numbers i
0)
24877 (setq number-string
(or (car numbers
) "0"))
24878 (if (string-match "\\`[A-Z]\\'" number-string
)
24879 (aset org-section-numbers i
24880 (- (string-to-char number-string
) ?A -
1))
24881 (aset org-section-numbers i
(string-to-number number-string
)))
24885 (defun org-section-number (&optional level
)
24886 "Return a string with the current section number.
24887 When LEVEL is non-nil, increase section numbers on that level."
24888 (let* ((depth (1- (length org-section-numbers
))) idx n
(string ""))
24891 (aset org-section-numbers
24892 level
(1+ (aref org-section-numbers level
))))
24893 (setq idx
(1+ level
))
24894 (while (<= idx depth
)
24895 (if (not (= idx
1))
24896 (aset org-section-numbers idx
0))
24897 (setq idx
(1+ idx
))))
24899 (while (<= idx depth
)
24900 (setq n
(aref org-section-numbers idx
))
24901 (setq string
(concat string
(if (not (string= string
"")) "." "")
24902 (int-to-string n
)))
24903 (setq idx
(1+ idx
)))
24905 (if (string-match "\\`\\([@0]\\.\\)+" string
)
24906 (setq string
(replace-match "" t nil string
)))
24907 (if (string-match "\\(\\.0\\)+\\'" string
)
24908 (setq string
(replace-match "" t nil string
))))
24913 (defvar org-last-level nil
) ; dynamically scoped variable
24914 (defvar org-min-level nil
) ; dynamically scoped variable
24915 (defvar org-levels-open nil
) ; dynamically scoped parameter
24916 (defvar org-ascii-current-indentation nil
) ; For communication
24918 (defun org-export-as-ascii (arg)
24919 "Export the outline as a pretty ASCII file.
24920 If there is an active region, export only the region.
24921 The prefix ARG specifies how many levels of the outline should become
24922 underlined headlines. The default is 3."
24924 (setq-default org-todo-line-regexp org-todo-line-regexp
)
24925 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
24926 (org-infile-export-plist)))
24927 (region-p (org-region-active-p))
24931 (goto-char (region-beginning))
24932 (and (org-at-heading-p)
24933 (>= (org-end-of-subtree t t
) (region-end))))))
24934 (custom-times org-display-custom-times
)
24935 (org-ascii-current-indentation '(0 .
0))
24939 (case-fold-search nil
)
24940 (filename (concat (file-name-as-directory
24941 (org-export-directory :ascii opt-plist
))
24942 (file-name-sans-extension
24944 (org-entry-get (region-beginning)
24945 "EXPORT_FILE_NAME" t
))
24946 (file-name-nondirectory buffer-file-name
)))
24948 (filename (if (equal (file-truename filename
)
24949 (file-truename buffer-file-name
))
24950 (concat filename
".txt")
24952 (buffer (find-file-noselect filename
))
24953 (org-levels-open (make-vector org-level-max nil
))
24954 (odd org-odd-levels-only
)
24955 (date (plist-get opt-plist
:date
))
24956 (author (plist-get opt-plist
:author
))
24957 (title (or (and subtree-p
(org-export-get-title-from-subtree))
24958 (plist-get opt-plist
:title
)
24960 (plist-get opt-plist
:skip-before-1st-heading
))
24961 (org-export-grab-title-from-buffer))
24962 (file-name-sans-extension
24963 (file-name-nondirectory buffer-file-name
))))
24964 (email (plist-get opt-plist
:email
))
24965 (language (plist-get opt-plist
:language
))
24966 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
24967 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
24972 (if (org-region-active-p) (region-beginning) (point-min))
24973 (if (org-region-active-p) (region-end) (point-max))))
24974 (lines (org-split-string
24975 (org-cleaned-string-for-export
24978 :skip-before-1st-heading
24979 (plist-get opt-plist
:skip-before-1st-heading
)
24980 :drawers
(plist-get opt-plist
:drawers
)
24981 :verbatim-multiline t
24983 (plist-get opt-plist
:archived-trees
)
24984 :add-text
(plist-get opt-plist
:text
))
24986 thetoc have-headings first-heading-pos
24987 table-open table-buffer
)
24989 (let ((inhibit-read-only t
))
24991 (remove-text-properties (point-min) (point-max)
24992 '(:org-license-to-kill t
))))
24994 (setq org-min-level
(org-get-min-level lines
))
24995 (setq org-last-level org-min-level
)
24996 (org-init-section-numbers)
24998 (find-file-noselect filename
)
25000 (setq lang-words
(or (assoc language org-export-language-setup
)
25001 (assoc "en" org-export-language-setup
)))
25002 (switch-to-buffer-other-window buffer
)
25005 ;; create local variables for all options, to make sure all called
25006 ;; functions get the correct information
25008 (set (make-local-variable (cdr x
))
25009 (plist-get opt-plist
(car x
))))
25010 org-export-plist-vars
)
25011 (org-set-local 'org-odd-levels-only odd
)
25012 (setq umax
(if arg
(prefix-numeric-value arg
)
25013 org-export-headline-levels
))
25014 (setq umax-toc
(if (integerp org-export-with-toc
)
25015 (min org-export-with-toc umax
)
25019 (if title
(org-insert-centered title ?
=))
25021 (if (and (or author email
)
25022 org-export-author-info
)
25023 (insert (concat (nth 1 lang-words
) ": " (or author
"")
25024 (if email
(concat " <" email
">") "")
25028 ((and date
(string-match "%" date
))
25029 (setq date
(format-time-string date
(current-time))))
25031 (t (setq date
(format-time-string "%Y/%m/%d %X" (current-time)))))
25033 (if (and date org-export-time-stamp-file
)
25034 (insert (concat (nth 2 lang-words
) ": " date
"\n")))
25038 (if org-export-with-toc
25040 (push (concat (nth 3 lang-words
) "\n") thetoc
)
25041 (push (concat (make-string (length (nth 3 lang-words
)) ?
=) "\n") thetoc
)
25042 (mapc '(lambda (line)
25043 (if (string-match org-todo-line-regexp
25045 ;; This is a headline
25047 (setq have-headings t
)
25048 (setq level
(- (match-end 1) (match-beginning 1))
25049 level
(org-tr-level level
)
25050 txt
(match-string 3 line
)
25052 (or (and org-export-mark-todo-in-toc
25053 (match-beginning 2)
25054 (not (member (match-string 2 line
)
25055 org-done-keywords
)))
25057 (and org-export-mark-todo-in-toc
25059 (org-search-todo-below
25060 line lines level
))))
25061 (setq txt
(org-html-expand-for-ascii txt
))
25063 (while (string-match org-bracket-link-regexp txt
)
25066 (match-string (if (match-end 2) 3 1) txt
)
25069 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
25071 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
25073 (setq txt
(replace-match "" t t txt
)))
25074 (if (string-match quote-re0 txt
)
25075 (setq txt
(replace-match "" t t txt
)))
25077 (if org-export-with-section-numbers
25078 (setq txt
(concat (org-section-number level
)
25080 (if (<= level umax-toc
)
25085 (* (max 0 (- level org-min-level
)) 4) ?\
)
25086 (format (if todo
"%s (*)\n" "%s\n") txt
))
25088 (setq org-last-level level
))
25091 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
25093 (org-init-section-numbers)
25094 (while (setq line
(pop lines
))
25095 ;; Remove the quoted HTML tags.
25096 (setq line
(org-html-expand-for-ascii line
))
25098 (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line
)
25099 (setq line
(replace-match "" t t line
)))
25100 ;; Replace internal links
25101 (while (string-match org-bracket-link-regexp line
)
25102 (setq line
(replace-match
25103 (if (match-end 3) "[\\3]" "[\\1]")
25106 (setq line
(org-translate-time line
)))
25108 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
25110 (setq first-heading-pos
(or first-heading-pos
(point)))
25111 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
25112 txt
(match-string 2 line
))
25113 (org-ascii-level-start level txt umax lines
))
25115 ((and org-export-with-tables
25116 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
25117 (if (not table-open
)
25118 ;; New table starts
25119 (setq table-open t table-buffer nil
))
25120 ;; Accumulate lines
25121 (setq table-buffer
(cons line table-buffer
))
25122 (when (or (not lines
)
25123 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
25125 (setq table-open nil
25126 table-buffer
(nreverse table-buffer
))
25129 (org-fix-indentation x org-ascii-current-indentation
))
25130 (org-format-table-ascii table-buffer
)
25133 (setq line
(org-fix-indentation line org-ascii-current-indentation
))
25134 (if (and org-export-with-fixed-width
25135 (string-match "^\\([ \t]*\\)\\(:\\)" line
))
25136 (setq line
(replace-match "\\1" nil nil line
)))
25137 (insert line
"\n"))))
25141 ;; insert the table of contents
25143 (goto-char (point-min))
25144 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
25146 (goto-char (match-beginning 0))
25147 (replace-match ""))
25148 (goto-char first-heading-pos
))
25149 (mapc 'insert thetoc
)
25150 (or (looking-at "[ \t]*\n[ \t]*\n")
25153 ;; Convert whitespace place holders
25154 (goto-char (point-min))
25156 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
25157 (setq end
(next-single-property-change beg
'org-whitespace
))
25159 (delete-region beg end
)
25160 (insert (make-string (- end beg
) ?\
))))
25163 ;; remove display and invisible chars
25165 (goto-char (point-min))
25166 (while (setq beg
(next-single-property-change (point) 'display
))
25167 (setq end
(next-single-property-change beg
'display
))
25168 (delete-region beg end
)
25171 (goto-char (point-min))
25172 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
25173 (setq end
(next-single-property-change beg
'org-cwidth
))
25174 (delete-region beg end
)
25176 (goto-char (point-min))))
25178 (defun org-export-ascii-clean-string ()
25179 "Do extra work for ASCII export"
25180 (goto-char (point-min))
25181 (while (re-search-forward org-verbatim-re nil t
)
25182 (goto-char (match-end 2))
25183 (backward-delete-char 1) (insert "'")
25184 (goto-char (match-beginning 2))
25185 (delete-char 1) (insert "`")
25186 (goto-char (match-end 2))))
25188 (defun org-search-todo-below (line lines level
)
25189 "Search the subtree below LINE for any TODO entries."
25190 (let ((rest (cdr (memq line lines
)))
25191 (re org-todo-line-regexp
)
25194 (while (setq line
(pop rest
))
25195 (if (string-match re line
)
25197 (setq lv
(- (match-end 1) (match-beginning 1))
25198 todo
(and (match-beginning 2)
25199 (not (member (match-string 2 line
)
25200 org-done-keywords
))))
25202 (if (<= lv level
) (throw 'exit nil
))
25203 (if todo
(throw 'exit t
))))))))
25205 (defun org-html-expand-for-ascii (line)
25206 "Handle quoted HTML for ASCII export."
25207 (if org-export-html-expand
25208 (while (string-match "@<[^<>\n]*>" line
)
25209 ;; We just remove the tags for now.
25210 (setq line
(replace-match "" nil nil line
))))
25213 (defun org-insert-centered (s &optional underline
)
25214 "Insert the string S centered and underline it with character UNDERLINE."
25215 (let ((ind (max (/ (- 80 (string-width s
)) 2) 0)))
25216 (insert (make-string ind ?\
) s
"\n")
25218 (insert (make-string ind ?\
)
25219 (make-string (string-width s
) underline
)
25222 (defun org-ascii-level-start (level title umax
&optional lines
)
25223 "Insert a new level in ASCII export."
25224 (let (char (n (- level umax
1)) (ind 0))
25227 (insert (make-string (* 2 n
) ?\
)
25228 (char-to-string (nth (% n
(length org-export-ascii-bullets
))
25229 org-export-ascii-bullets
))
25231 ;; find the indentation of the next non-empty line
25234 (if (string-match "^\\* " (car lines
)) (throw 'stop nil
))
25235 (if (string-match "^\\([ \t]*\\)\\S-" (car lines
))
25236 (throw 'stop
(setq ind
(org-get-indentation (car lines
)))))
25238 (setq org-ascii-current-indentation
(cons (* 2 (1+ n
)) ind
)))
25239 (if (or (not (equal (char-before) ?
\n))
25240 (not (equal (char-before (1- (point))) ?
\n)))
25242 (setq char
(nth (- umax level
) (reverse org-export-ascii-underline
)))
25243 (unless org-export-with-tags
25244 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
25245 (setq title
(replace-match "" t t title
))))
25246 (if org-export-with-section-numbers
25247 (setq title
(concat (org-section-number level
) " " title
)))
25248 (insert title
"\n" (make-string (string-width title
) char
) "\n")
25249 (setq org-ascii-current-indentation
'(0 .
0)))))
25251 (defun org-export-visible (type arg
)
25252 "Create a copy of the visible part of the current buffer, and export it.
25253 The copy is created in a temporary buffer and removed after use.
25254 TYPE is the final key (as a string) that also select the export command in
25255 the `C-c C-e' export dispatcher.
25256 As a special case, if the you type SPC at the prompt, the temporary
25257 org-mode file will not be removed but presented to you so that you can
25258 continue to use it. The prefix arg ARG is passed through to the exporting
25262 (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
25263 (read-char-exclusive))
25264 current-prefix-arg
))
25265 (if (not (member type
'(?a ?\C-a ?b ?\C-b ?h ?x ?\
)))
25266 (error "Invalid export key"))
25267 (let* ((binding (cdr (assoc type
25268 '((?a . org-export-as-ascii
)
25269 (?\C-a . org-export-as-ascii
)
25270 (?b . org-export-as-html-and-open
)
25271 (?\C-b . org-export-as-html-and-open
)
25272 (?h . org-export-as-html
)
25273 (?H . org-export-as-html-to-buffer
)
25274 (?R . org-export-region-as-html
)
25275 (?x . org-export-as-xoxo
)))))
25276 (keepp (equal type ?\
))
25277 (file buffer-file-name
)
25278 (buffer (get-buffer-create "*Org Export Visible*"))
25280 ;; Need to hack the drawers here.
25282 (goto-char (point-min))
25283 (while (re-search-forward org-drawer-regexp nil t
)
25284 (goto-char (match-beginning 1))
25285 (or (org-invisible-p) (org-flag-drawer nil
))))
25286 (with-current-buffer buffer
(erase-buffer))
25288 (setq s
(goto-char (point-min)))
25289 (while (not (= (point) (point-max)))
25290 (goto-char (org-find-invisible))
25291 (append-to-buffer buffer s
(point))
25292 (setq s
(goto-char (org-find-visible))))
25293 (org-cycle-hide-drawers 'all
)
25294 (goto-char (point-min))
25296 ;; Copy all comment lines to the end, to make sure #+ settings are
25297 ;; still available for the second export step. Kind of a hack, but
25298 ;; does do the trick.
25299 (if (looking-at "#[^\r\n]*")
25300 (append-to-buffer buffer
(match-beginning 0) (1+ (match-end 0))))
25301 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t
)
25302 (append-to-buffer buffer
(1+ (match-beginning 0))
25303 (min (point-max) (1+ (match-end 0))))))
25304 (set-buffer buffer
)
25305 (let ((buffer-file-name file
)
25306 (org-inhibit-startup t
))
25309 (unless keepp
(funcall binding arg
))))
25311 (kill-buffer buffer
)
25312 (switch-to-buffer-other-window buffer
)
25313 (goto-char (point-min)))))
25315 (defun org-find-visible ()
25317 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
25318 (get-char-property s
'invisible
)))
25320 (defun org-find-invisible ()
25322 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
25323 (not (get-char-property s
'invisible
))))
25328 (defun org-get-current-options ()
25329 "Return a string with current options as keyword options.
25330 Does include HTML export options as well as TODO and CATEGORY stuff."
25336 #+TEXT: Some descriptive text to be emitted. Several lines OK.
25337 #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s -:%s f:%s *:%s TeX:%s LaTeX:%s skip:%s d:%s tags:%s
25341 #+PRIORITIES: %c %c %c
25343 #+STARTUP: %s %s %s %s %s
25348 (buffer-name) (user-full-name) user-mail-address org-export-default-language
25349 org-export-headline-levels
25350 org-export-with-section-numbers
25351 org-export-with-toc
25352 org-export-preserve-breaks
25353 org-export-html-expand
25354 org-export-with-fixed-width
25355 org-export-with-tables
25356 org-export-with-sub-superscripts
25357 org-export-with-special-strings
25358 org-export-with-footnotes
25359 org-export-with-emphasize
25360 org-export-with-TeX-macros
25361 org-export-with-LaTeX-fragments
25362 org-export-skip-text-before-1st-heading
25363 org-export-with-drawers
25364 org-export-with-tags
25365 (file-name-nondirectory buffer-file-name
)
25366 "TODO FEEDBACK VERIFY DONE"
25367 "Me Jason Marie DONE"
25368 org-highest-priority org-lowest-priority org-default-priority
25369 (mapconcat 'identity org-drawers
" ")
25370 (cdr (assoc org-startup-folded
25371 '((nil .
"showall") (t .
"overview") (content .
"content"))))
25372 (if org-odd-levels-only
"odd" "oddeven")
25373 (if org-hide-leading-stars
"hidestars" "showstars")
25374 (if org-startup-align-all-tables
"align" "noalign")
25375 (cond ((eq org-log-done t
) "logdone")
25376 ((equal org-log-done
'note
) "lognotedone")
25377 ((not org-log-done
) "nologdone"))
25378 (or (mapconcat (lambda (x)
25380 ((equal '(:startgroup
) x
) "{")
25381 ((equal '(:endgroup
) x
) "}")
25382 ((cdr x
) (format "%s(%c)" (car x
) (cdr x
)))
25384 (or org-tag-alist
(org-get-buffer-tags)) " ") "")
25385 org-archive-location
25386 "org file:~/org/%s.org"
25389 (defun org-insert-export-options-template ()
25390 "Insert into the buffer a template with information for exporting."
25392 (if (not (bolp)) (newline))
25393 (let ((s (org-get-current-options)))
25394 (and (string-match "#\\+CATEGORY" s
)
25395 (setq s
(substring s
0 (match-beginning 0))))
25398 (defun org-toggle-fixed-width-section (arg)
25399 "Toggle the fixed-width export.
25400 If there is no active region, the QUOTE keyword at the current headline is
25401 inserted or removed. When present, it causes the text between this headline
25402 and the next to be exported as fixed-width text, and unmodified.
25403 If there is an active region, this command adds or removes a colon as the
25404 first character of this line. If the first character of a line is a colon,
25405 this line is also exported in fixed-width font."
25408 (regionp (org-region-active-p))
25409 (beg (if regionp
(region-beginning) (point)))
25410 (end (if regionp
(region-end)))
25411 (nlines (or arg
(if (and beg end
) (count-lines beg end
) 1)))
25412 (case-fold-search nil
)
25413 (re "[ \t]*\\(:\\)")
25418 (setq cc
(current-column))
25419 (beginning-of-line 1)
25420 (setq off
(looking-at re
))
25421 (while (> nlines
0)
25422 (setq nlines
(1- nlines
))
25423 (beginning-of-line 1)
25426 (move-to-column cc t
)
25429 ((and off
(looking-at re
))
25430 (replace-match "" t t nil
1))
25431 ((not off
) (move-to-column cc t
) (insert ":")))
25434 (org-back-to-heading)
25435 (if (looking-at (concat outline-regexp
25436 "\\( *\\<" org-quote-string
"\\>[ \t]*\\)"))
25437 (replace-match "" t t nil
1)
25438 (if (looking-at outline-regexp
)
25440 (goto-char (match-end 0))
25441 (insert org-quote-string
" "))))))))
25443 (defun org-export-as-html-and-open (arg)
25444 "Export the outline as HTML and immediately open it with a browser.
25445 If there is an active region, export only the region.
25446 The prefix ARG specifies how many levels of the outline should become
25447 headlines. The default is 3. Lower levels will become bulleted lists."
25449 (org-export-as-html arg
'hidden
)
25450 (org-open-file buffer-file-name
))
25452 (defun org-export-as-html-batch ()
25453 "Call `org-export-as-html', may be used in batch processing as
25455 --load=$HOME/lib/emacs/org.el
25456 --eval \"(setq org-export-headline-levels 2)\"
25457 --visit=MyFile --funcall org-export-as-html-batch"
25458 (org-export-as-html org-export-headline-levels
'hidden
))
25460 (defun org-export-as-html-to-buffer (arg)
25461 "Call `org-exort-as-html` with output to a temporary buffer.
25462 No file is created. The prefix ARG is passed through to `org-export-as-html'."
25464 (org-export-as-html arg nil nil
"*Org HTML Export*")
25465 (switch-to-buffer-other-window "*Org HTML Export*"))
25467 (defun org-replace-region-by-html (beg end
)
25468 "Assume the current region has org-mode syntax, and convert it to HTML.
25469 This can be used in any buffer. For example, you could write an
25470 itemized list in org-mode syntax in an HTML buffer and then use this
25471 command to convert it."
25473 (let (reg html buf pop-up-frames
)
25474 (save-window-excursion
25476 (setq html
(org-export-region-as-html
25477 beg end t
'string
))
25478 (setq reg
(buffer-substring beg end
)
25479 buf
(get-buffer-create "*Org tmp*"))
25480 (with-current-buffer buf
25484 (setq html
(org-export-region-as-html
25485 (point-min) (point-max) t
'string
)))
25486 (kill-buffer buf
)))
25487 (delete-region beg end
)
25490 (defun org-export-region-as-html (beg end
&optional body-only buffer
)
25491 "Convert region from BEG to END in org-mode buffer to HTML.
25492 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
25493 contents, and only produce the region of converted text, useful for
25494 cut-and-paste operations.
25495 If BUFFER is a buffer or a string, use/create that buffer as a target
25496 of the converted HTML. If BUFFER is the symbol `string', return the
25497 produced HTML as a string and leave not buffer behind. For example,
25498 a Lisp program could call this function in the following way:
25500 (setq html (org-export-region-as-html beg end t 'string))
25502 When called interactively, the output buffer is selected, and shown
25503 in a window. A non-interactive call will only retunr the buffer."
25504 (interactive "r\nP")
25505 (when (interactive-p)
25506 (setq buffer
"*Org HTML Export*"))
25507 (let ((transient-mark-mode t
) (zmacs-regions t
)
25510 (set-mark (point)) ;; to activate the region
25512 (setq rtn
(org-export-as-html
25515 (if (fboundp 'deactivate-mark
) (deactivate-mark))
25516 (if (and (interactive-p) (bufferp rtn
))
25517 (switch-to-buffer-other-window rtn
)
25520 (defvar html-table-tag nil
) ; dynamically scoped into this.
25521 (defun org-export-as-html (arg &optional hidden ext-plist
25522 to-buffer body-only pub-dir
)
25523 "Export the outline as a pretty HTML file.
25524 If there is an active region, export only the region. The prefix
25525 ARG specifies how many levels of the outline should become
25526 headlines. The default is 3. Lower levels will become bulleted
25527 lists. When HIDDEN is non-nil, don't display the HTML buffer.
25528 EXT-PLIST is a property list with external parameters overriding
25529 org-mode's default settings, but still inferior to file-local
25530 settings. When TO-BUFFER is non-nil, create a buffer with that
25531 name and export to that buffer. If TO-BUFFER is the symbol
25532 `string', don't leave any buffer behind but just return the
25533 resulting HTML as a string. When BODY-ONLY is set, don't produce
25534 the file header and footer, simply return the content of
25535 <body>...</body>, without even the body tags themselves. When
25536 PUB-DIR is set, use this as the publishing directory."
25539 ;; Make sure we have a file name when we need it.
25540 (when (and (not (or to-buffer body-only
))
25541 (not buffer-file-name
))
25542 (if (buffer-base-buffer)
25543 (org-set-local 'buffer-file-name
25544 (with-current-buffer (buffer-base-buffer)
25546 (error "Need a file name to be able to export.")))
25548 (message "Exporting...")
25549 (setq-default org-todo-line-regexp org-todo-line-regexp
)
25550 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
25551 (setq-default org-done-keywords org-done-keywords
)
25552 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
25553 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
25555 (org-infile-export-plist)))
25557 (style (plist-get opt-plist
:style
))
25558 (html-extension (plist-get opt-plist
:html-extension
))
25559 (link-validate (plist-get opt-plist
:link-validation-function
))
25560 valid thetoc have-headings first-heading-pos
25561 (odd org-odd-levels-only
)
25562 (region-p (org-region-active-p))
25566 (goto-char (region-beginning))
25567 (and (org-at-heading-p)
25568 (>= (org-end-of-subtree t t
) (region-end))))))
25569 ;; The following two are dynamically scoped into other
25571 (org-current-export-dir
25572 (or pub-dir
(org-export-directory :html opt-plist
)))
25573 (org-current-export-file buffer-file-name
)
25574 (level 0) (line "") (origline "") txt todo
25577 (filename (if to-buffer nil
25580 (file-name-sans-extension
25582 (org-entry-get (region-beginning)
25583 "EXPORT_FILE_NAME" t
))
25584 (file-name-nondirectory buffer-file-name
)))
25585 "." html-extension
)
25586 (file-name-as-directory
25587 (or pub-dir
(org-export-directory :html opt-plist
))))))
25588 (current-dir (if buffer-file-name
25589 (file-name-directory buffer-file-name
)
25590 default-directory
))
25591 (buffer (if to-buffer
25593 ((eq to-buffer
'string
) (get-buffer-create "*Org HTML Export*"))
25594 (t (get-buffer-create to-buffer
)))
25595 (find-file-noselect filename
)))
25596 (org-levels-open (make-vector org-level-max nil
))
25597 (date (plist-get opt-plist
:date
))
25598 (author (plist-get opt-plist
:author
))
25599 (title (or (and subtree-p
(org-export-get-title-from-subtree))
25600 (plist-get opt-plist
:title
)
25602 (plist-get opt-plist
:skip-before-1st-heading
))
25603 (org-export-grab-title-from-buffer))
25604 (and buffer-file-name
25605 (file-name-sans-extension
25606 (file-name-nondirectory buffer-file-name
)))
25608 (html-table-tag (plist-get opt-plist
:html-table-tag
))
25609 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
25610 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string
"\\>\\)"))
25613 (in-local-list nil
)
25614 (local-list-num nil
)
25615 (local-list-indent nil
)
25616 (llt org-plain-list-ordered-item-terminator
)
25617 (email (plist-get opt-plist
:email
))
25618 (language (plist-get opt-plist
:language
))
25620 (target-alist nil
) tg
25623 (coding-system (and (boundp 'buffer-file-coding-system
)
25624 buffer-file-coding-system
))
25625 (coding-system-for-write (or org-export-html-coding-system
25627 (save-buffer-coding-system (or org-export-html-coding-system
25629 (charset (and coding-system-for-write
25630 (fboundp 'coding-system-get
)
25631 (coding-system-get coding-system-for-write
25635 (if region-p
(region-beginning) (point-min))
25636 (if region-p
(region-end) (point-max))))
25639 (org-cleaned-string-for-export
25643 :skip-before-1st-heading
25644 (plist-get opt-plist
:skip-before-1st-heading
)
25645 :drawers
(plist-get opt-plist
:drawers
)
25647 (plist-get opt-plist
:archived-trees
)
25649 (plist-get opt-plist
:text
)
25651 (plist-get opt-plist
:LaTeX-fragments
))
25654 table-buffer table-orig-buffer
25655 ind start-is-num starter didclose
25656 rpl path desc descp desc1 desc2 link
25659 (let ((inhibit-read-only t
))
25661 (remove-text-properties (point-min) (point-max)
25662 '(:org-license-to-kill t
))))
25664 (message "Exporting...")
25666 (setq org-min-level
(org-get-min-level lines
))
25667 (setq org-last-level org-min-level
)
25668 (org-init-section-numbers)
25671 ((and date
(string-match "%" date
))
25672 (setq date
(format-time-string date
(current-time))))
25674 (t (setq date
(format-time-string "%Y/%m/%d %X" (current-time)))))
25676 ;; Get the language-dependent settings
25677 (setq lang-words
(or (assoc language org-export-language-setup
)
25678 (assoc "en" org-export-language-setup
)))
25680 ;; Switch to the output buffer
25681 (set-buffer buffer
)
25682 (let ((inhibit-read-only t
)) (erase-buffer))
25685 (and (fboundp 'set-buffer-file-coding-system
)
25686 (set-buffer-file-coding-system coding-system-for-write
))
25688 (let ((case-fold-search nil
)
25689 (org-odd-levels-only odd
))
25690 ;; create local variables for all options, to make sure all called
25691 ;; functions get the correct information
25693 (set (make-local-variable (cdr x
))
25694 (plist-get opt-plist
(car x
))))
25695 org-export-plist-vars
)
25696 (setq umax
(if arg
(prefix-numeric-value arg
)
25697 org-export-headline-levels
))
25698 (setq umax-toc
(if (integerp org-export-with-toc
)
25699 (min org-export-with-toc umax
)
25704 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
25705 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
25706 <html xmlns=\"http://www.w3.org/1999/xhtml\"
25707 lang=\"%s\" xml:lang=\"%s\">
25710 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
25711 <meta name=\"generator\" content=\"Org-mode\"/>
25712 <meta name=\"generated\" content=\"%s\"/>
25713 <meta name=\"author\" content=\"%s\"/>
25717 language language
(org-html-expand title
)
25718 (or charset
"iso-8859-1") date author style
))
25720 (insert (or (plist-get opt-plist
:preamble
) ""))
25722 (when (plist-get opt-plist
:auto-preamble
)
25723 (if title
(insert (format org-export-html-title-format
25724 (org-html-expand title
))))))
25726 (if (and org-export-with-toc
(not body-only
))
25728 (push (format "<h%d>%s</h%d>\n"
25729 org-export-html-toplevel-hlevel
25731 org-export-html-toplevel-hlevel
)
25733 (push "<ul>\n<li>" thetoc
)
25735 (mapcar '(lambda (line)
25736 (if (string-match org-todo-line-regexp line
)
25737 ;; This is a headline
25739 (setq have-headings t
)
25740 (setq level
(- (match-end 1) (match-beginning 1))
25741 level
(org-tr-level level
)
25742 txt
(save-match-data
25744 (org-export-cleanup-toc-line
25745 (match-string 3 line
))))
25747 (or (and org-export-mark-todo-in-toc
25748 (match-beginning 2)
25749 (not (member (match-string 2 line
)
25750 org-done-keywords
)))
25752 (and org-export-mark-todo-in-toc
25754 (org-search-todo-below
25755 line lines level
))))
25757 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt
)
25758 (setq txt
(replace-match " <span class=\"tag\"> \\1</span>" t nil txt
)))
25759 (if (string-match quote-re0 txt
)
25760 (setq txt
(replace-match "" t t txt
)))
25761 (if org-export-with-section-numbers
25762 (setq txt
(concat (org-section-number level
)
25764 (if (<= level
(max umax umax-toc
))
25765 (setq head-count
(+ head-count
1)))
25766 (if (<= level umax-toc
)
25768 (if (> level org-last-level
)
25770 (setq cnt
(- level org-last-level
))
25771 (while (>= (setq cnt
(1- cnt
)) 0)
25772 (push "\n<ul>\n<li>" thetoc
))
25773 (push "\n" thetoc
)))
25774 (if (< level org-last-level
)
25776 (setq cnt
(- org-last-level level
))
25777 (while (>= (setq cnt
(1- cnt
)) 0)
25778 (push "</li>\n</ul>" thetoc
))
25779 (push "\n" thetoc
)))
25780 ;; Check for targets
25781 (while (string-match org-target-regexp line
)
25782 (setq tg
(match-string 1 line
)
25783 line
(replace-match
25784 (concat "@<span class=\"target\">" tg
"@</span> ")
25786 (push (cons (org-solidify-link-text tg
)
25787 (format "sec-%d" head-count
))
25789 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
25790 (setq txt
(replace-match "" t t txt
)))
25794 "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
25795 "</li>\n<li><a href=\"#sec-%d\">%s</a>")
25796 head-count txt
) thetoc
)
25798 (setq org-last-level level
))
25802 (while (> org-last-level
(1- org-min-level
))
25803 (setq org-last-level
(1- org-last-level
))
25804 (push "</li>\n</ul>\n" thetoc
))
25805 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
25807 (setq head-count
0)
25808 (org-init-section-numbers)
25810 (while (setq line
(pop lines
) origline line
)
25813 ;; end of quote section?
25814 (when (and inquote
(string-match "^\\*+ " line
))
25815 (insert "</pre>\n")
25816 (setq inquote nil
))
25817 ;; inside a quote section?
25819 (insert (org-html-protect line
) "\n")
25820 (throw 'nextline nil
))
25823 (when (and org-export-with-fixed-width
25824 (string-match "^[ \t]*:\\(.*\\)" line
))
25825 (when (not infixed
)
25827 (insert "<pre>\n"))
25828 (insert (org-html-protect (match-string 1 line
)) "\n")
25830 (not (string-match "^[ \t]*\\(:.*\\)"
25833 (insert "</pre>\n"))
25834 (throw 'nextline nil
))
25837 (when (get-text-property 0 'org-protected line
)
25839 (when (re-search-backward
25840 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t
)
25841 (setq par
(match-string 1))
25842 (replace-match "\\2\n"))
25845 (or (= (length (car lines
)) 0)
25846 (get-text-property 0 'org-protected
(car lines
))))
25847 (insert (pop lines
) "\n"))
25848 (and par
(insert "<p>\n")))
25849 (throw 'nextline nil
))
25852 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line
)
25853 (insert "\n<hr/>\n")
25854 (throw 'nextline nil
))
25856 ;; make targets to anchors
25857 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line
)
25860 (setq line
(replace-match
25861 (concat "@<a name=\""
25862 (org-solidify-link-text (match-string 1 line
))
25865 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
25866 (setq line
(replace-match
25867 (concat "@<span class=\"target\">" (match-string 1 line
) "@</span> ")
25868 ; (concat "@<i>" (match-string 1 line) "@</i> ")
25871 (setq line
(replace-match
25872 (concat "@<a name=\""
25873 (org-solidify-link-text (match-string 1 line
))
25874 "\" class=\"target\">" (match-string 1 line
) "@</a> ")
25877 (setq line
(org-html-handle-time-stamps line
))
25879 ;; replace "&" by "&", "<" and ">" by "<" and ">"
25880 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
25881 ;; Also handle sub_superscripts and checkboxes
25882 (or (string-match org-table-hline-regexp line
)
25883 (setq line
(org-html-expand line
)))
25885 ;; Format the links
25887 (while (string-match org-bracket-link-analytic-regexp line start
)
25888 (setq start
(match-beginning 0))
25889 (setq type
(if (match-end 2) (match-string 2 line
) "internal"))
25890 (setq path
(match-string 3 line
))
25891 (setq desc1
(if (match-end 5) (match-string 5 line
))
25892 desc2
(if (match-end 2) (concat type
":" path
) path
)
25893 descp
(and desc1
(not (equal desc1 desc2
)))
25894 desc
(or desc1 desc2
))
25895 ;; Make an image out of the description if that is so wanted
25896 (when (and descp
(org-file-image-p desc
))
25898 (if (string-match "^file:" desc
)
25899 (setq desc
(substring desc
(match-end 0)))))
25900 (setq desc
(concat "<img src=\"" desc
"\"/>")))
25901 ;; FIXME: do we need to unescape here somewhere?
25903 ((equal type
"internal")
25907 (org-solidify-link-text
25908 (save-match-data (org-link-unescape path
)) target-alist
)
25909 "\">" desc
"</a>")))
25910 ((member type
'("http" "https"))
25911 ;; standard URL, just check if we need to inline an image
25912 (if (and (or (eq t org-export-html-inline-images
)
25913 (and org-export-html-inline-images
(not descp
)))
25914 (org-file-image-p path
))
25915 (setq rpl
(concat "<img src=\"" type
":" path
"\"/>"))
25916 (setq link
(concat type
":" path
))
25917 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>"))))
25918 ((member type
'("ftp" "mailto" "news"))
25920 (setq link
(concat type
":" path
))
25921 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>")))
25922 ((string= type
"file")
25924 (let* ((filename path
)
25925 (abs-p (file-name-absolute-p filename
))
25926 thefile file-is-image-p search
)
25928 (if (string-match "::\\(.*\\)" filename
)
25929 (setq search
(match-string 1 filename
)
25930 filename
(replace-match "" t nil filename
)))
25932 (if (functionp link-validate
)
25933 (funcall link-validate filename current-dir
)
25935 (setq file-is-image-p
(org-file-image-p filename
))
25936 (setq thefile
(if abs-p
(expand-file-name filename
) filename
))
25937 (when (and org-export-html-link-org-files-as-html
25938 (string-match "\\.org$" thefile
))
25939 (setq thefile
(concat (substring thefile
0
25940 (match-beginning 0))
25941 "." html-extension
))
25943 ;; make sure this is can be used as target search
25944 (not (string-match "^[0-9]*$" search
))
25945 (not (string-match "^\\*" search
))
25946 (not (string-match "^/.*/$" search
)))
25947 (setq thefile
(concat thefile
"#"
25948 (org-solidify-link-text
25949 (org-link-unescape search
)))))
25950 (when (string-match "^file:" desc
)
25951 (setq desc
(replace-match "" t t desc
))
25952 (if (string-match "\\.org$" desc
)
25953 (setq desc
(replace-match "" t t desc
))))))
25954 (setq rpl
(if (and file-is-image-p
25955 (or (eq t org-export-html-inline-images
)
25956 (and org-export-html-inline-images
25958 (concat "<img src=\"" thefile
"\"/>")
25959 (concat "<a href=\"" thefile
"\">" desc
"</a>")))
25960 (if (not valid
) (setq rpl desc
))))
25961 ((member type
'("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
25962 (setq rpl
(concat "<i><" type
":"
25963 (save-match-data (org-link-unescape path
))
25965 (setq line
(replace-match rpl t t line
)
25966 start
(+ start
(length rpl
))))
25969 (if (and (string-match org-todo-line-regexp line
)
25970 (match-beginning 2))
25973 (concat (substring line
0 (match-beginning 2))
25975 (if (member (match-string 2 line
)
25978 "\">" (match-string 2 line
)
25979 "</span>" (substring line
(match-end 2)))))
25981 ;; Does this contain a reference to a footnote?
25982 (when org-export-with-footnotes
25984 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start
)
25985 (if (get-text-property (match-beginning 2) 'org-protected line
)
25986 (setq start
(match-end 2))
25987 (let ((n (match-string 2 line
)))
25991 "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
25992 (match-string 1 line
) n n n
)
25996 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
25997 ;; This is a headline
25998 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
25999 txt
(match-string 2 line
))
26000 (if (string-match quote-re0 txt
)
26001 (setq txt
(replace-match "" t t txt
)))
26002 (if (<= level
(max umax umax-toc
))
26003 (setq head-count
(+ head-count
1)))
26004 (when in-local-list
26005 ;; Close any local lists before inserting a new header line
26006 (while local-list-num
26008 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
26009 (pop local-list-num
))
26010 (setq local-list-indent nil
26011 in-local-list nil
))
26012 (setq first-heading-pos
(or first-heading-pos
(point)))
26013 (org-html-level-start level txt umax
26014 (and org-export-with-toc
(<= level umax
))
26017 (when (string-match quote-re line
)
26021 ((and org-export-with-tables
26022 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
26023 (if (not table-open
)
26024 ;; New table starts
26025 (setq table-open t table-buffer nil table-orig-buffer nil
))
26026 ;; Accumulate lines
26027 (setq table-buffer
(cons line table-buffer
)
26028 table-orig-buffer
(cons origline table-orig-buffer
))
26029 (when (or (not lines
)
26030 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
26032 (setq table-open nil
26033 table-buffer
(nreverse table-buffer
)
26034 table-orig-buffer
(nreverse table-orig-buffer
))
26035 (org-close-par-maybe)
26036 (insert (org-format-table-html table-buffer table-orig-buffer
))))
26039 (when (string-match
26041 ((eq llt t
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
26042 ((= llt ?.
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
26043 ((= llt ?\
)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
26044 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
26046 (setq ind
(org-get-string-indentation line
)
26047 start-is-num
(match-beginning 4)
26048 starter
(if (match-beginning 2)
26049 (substring (match-string 2 line
) 0 -
1))
26050 line
(substring line
(match-beginning 5)))
26051 (unless (string-match "[^ \t]" line
)
26052 ;; empty line. Pretend indentation is large.
26053 (setq ind
(if org-empty-line-terminates-plain-lists
26055 (1+ (or (car local-list-indent
) 1)))))
26056 (setq didclose nil
)
26057 (while (and in-local-list
26058 (or (and (= ind
(car local-list-indent
))
26060 (< ind
(car local-list-indent
))))
26063 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
26064 (pop local-list-num
) (pop local-list-indent
)
26065 (setq in-local-list local-list-indent
))
26068 (or (not in-local-list
)
26069 (> ind
(car local-list-indent
))))
26070 ;; Start new (level of) list
26071 (org-close-par-maybe)
26072 (insert (if start-is-num
"<ol>\n<li>\n" "<ul>\n<li>\n"))
26073 (push start-is-num local-list-num
)
26074 (push ind local-list-indent
)
26075 (setq in-local-list t
))
26077 ;; continue current list
26081 ;; we did close a list, normal text follows: need <p>
26083 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line
)
26086 (if (equal (match-string 1 line
) "X")
26088 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
26091 ;; Empty lines start a new paragraph. If hand-formatted lists
26092 ;; are not fully interpreted, lines starting with "-", "+", "*"
26093 ;; also start a new paragraph.
26094 (if (string-match "^ [-+*]-\\|^[ \t]*$" line
) (org-open-par))
26096 ;; Is this the start of a footnote?
26097 (when org-export-with-footnotes
26098 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
26099 (org-close-par-maybe)
26100 (let ((n (match-string 1 line
)))
26101 (setq line
(replace-match
26102 (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n
) t t line
)))))
26104 ;; Check if the line break needs to be conserved
26106 ((string-match "\\\\\\\\[ \t]*$" line
)
26107 (setq line
(replace-match "<br/>" t t line
)))
26108 (org-export-preserve-breaks
26109 (setq line
(concat line
"<br/>"))))
26111 (insert line
"\n")))))
26113 ;; Properly close all local lists and other lists
26114 (when inquote
(insert "</pre>\n"))
26115 (when in-local-list
26116 ;; Close any local lists before inserting a new header line
26117 (while local-list-num
26119 (insert (if (car local-list-num
) "</ol>\n" "</ul>\n"))
26120 (pop local-list-num
))
26121 (setq local-list-indent nil
26122 in-local-list nil
))
26123 (org-html-level-start 1 nil umax
26124 (and org-export-with-toc
(<= level umax
))
26128 (when (plist-get opt-plist
:auto-postamble
)
26129 (insert "<div id=\"postamble\">")
26130 (when (and org-export-author-info author
)
26131 (insert "<p class=\"author\"> "
26132 (nth 1 lang-words
) ": " author
"\n")
26134 (if (listp (split-string email
",+ *"))
26136 (insert "<a href=\"mailto:" e
"\"><"
26138 (split-string email
",+ *"))
26139 (insert "<a href=\"mailto:" email
"\"><"
26140 email
"></a>\n")))
26142 (when (and date org-export-time-stamp-file
)
26143 (insert "<p class=\"date\"> "
26144 (nth 2 lang-words
) ": "
26148 (if org-export-html-with-timestamp
26149 (insert org-export-html-html-helper-timestamp
))
26150 (insert (or (plist-get opt-plist
:postamble
) ""))
26151 (insert "</body>\n</html>\n"))
26154 (if (eq major-mode default-major-mode
) (html-mode))
26156 ;; insert the table of contents
26157 (goto-char (point-min))
26159 (if (or (re-search-forward
26160 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t
)
26162 "\\[TABLE-OF-CONTENTS\\]" nil t
))
26164 (goto-char (match-beginning 0))
26165 (replace-match ""))
26166 (goto-char first-heading-pos
)
26167 (when (looking-at "\\s-*</p>")
26168 (goto-char (match-end 0))
26170 (insert "<div id=\"table-of-contents\">\n")
26171 (mapc 'insert thetoc
)
26172 (insert "</div>\n"))
26173 ;; remove empty paragraphs and lists
26174 (goto-char (point-min))
26175 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t
)
26176 (replace-match ""))
26177 (goto-char (point-min))
26178 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t
)
26179 (replace-match ""))
26180 (goto-char (point-min))
26181 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t
)
26182 (replace-match ""))
26183 ;; Convert whitespace place holders
26184 (goto-char (point-min))
26186 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
26187 (setq n
(get-text-property beg
'org-whitespace
)
26188 end
(next-single-property-change beg
'org-whitespace
))
26190 (delete-region beg end
)
26191 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
26192 (make-string n ?x
)))))
26193 (or to-buffer
(save-buffer))
26194 (goto-char (point-min))
26195 (message "Exporting... done")
26196 (if (eq to-buffer
'string
)
26197 (prog1 (buffer-substring (point-min) (point-max))
26198 (kill-buffer (current-buffer)))
26199 (current-buffer)))))
26201 (defvar org-table-colgroup-info nil
)
26202 (defun org-format-table-ascii (lines)
26203 "Format a table for ascii export."
26204 (if (stringp lines
)
26205 (setq lines
(org-split-string lines
"\n")))
26206 (if (not (string-match "^[ \t]*|" (car lines
)))
26207 ;; Table made by table.el - test for spanning
26210 ;; A normal org table
26211 ;; Get rid of hlines at beginning and end
26212 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
26213 (setq lines
(nreverse lines
))
26214 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
26215 (setq lines
(nreverse lines
))
26216 (when org-export-table-remove-special-lines
26217 ;; Check if the table has a marking column. If yes remove the
26218 ;; column and the special lines
26219 (setq lines
(org-table-clean-before-export lines
)))
26220 ;; Get rid of the vertical lines except for grouping
26221 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
26222 rtn line vl1 start
)
26223 (while (setq line
(pop lines
))
26224 (if (string-match org-table-hline-regexp line
)
26225 (and (string-match "|\\(.*\\)|" line
)
26226 (setq line
(replace-match " \\1" t nil line
)))
26227 (setq start
0 vl1 vl
)
26228 (while (string-match "|" line start
)
26229 (setq start
(match-end 0))
26230 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
26234 (defun org-colgroup-info-to-vline-list (info)
26237 (setq last new new
(pop info
))
26238 (if (or (memq last
'(:end
:startend
))
26239 (memq new
'(:start
:startend
)))
26242 (setq vl
(nreverse vl
))
26243 (and vl
(setcar vl nil
))
26246 (defun org-format-table-html (lines olines
)
26247 "Find out which HTML converter to use and return the HTML code."
26248 (if (stringp lines
)
26249 (setq lines
(org-split-string lines
"\n")))
26250 (if (string-match "^[ \t]*|" (car lines
))
26251 ;; A normal org table
26252 (org-format-org-table-html lines
)
26253 ;; Table made by table.el - test for spanning
26254 (let* ((hlines (delq nil
(mapcar
26256 (if (string-match "^[ \t]*\\+-" x
) x
26259 (first (car hlines
))
26260 (ll (and (string-match "\\S-+" first
)
26261 (match-string 0 first
)))
26262 (re (concat "^[ \t]*" (regexp-quote ll
)))
26263 (spanning (delq nil
(mapcar (lambda (x) (not (string-match re x
)))
26265 (if (and (not spanning
)
26266 (not org-export-prefer-native-exporter-for-tables
))
26267 ;; We can use my own converter with HTML conversions
26268 (org-format-table-table-html lines
)
26269 ;; Need to use the code generator in table.el, with the original text.
26270 (org-format-table-table-html-using-table-generate-source olines
)))))
26272 (defun org-format-org-table-html (lines &optional splice
)
26273 "Format a table into HTML."
26274 ;; Get rid of hlines at beginning and end
26275 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
26276 (setq lines
(nreverse lines
))
26277 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
26278 (setq lines
(nreverse lines
))
26279 (when org-export-table-remove-special-lines
26280 ;; Check if the table has a marking column. If yes remove the
26281 ;; column and the special lines
26282 (setq lines
(org-table-clean-before-export lines
)))
26284 (let ((head (and org-export-highlight-first-table-line
26286 (lambda (x) (string-match "^[ \t]*|-" x
))
26289 tbopen line fields html gr colgropen
)
26290 (if splice
(setq head nil
))
26291 (unless splice
(push (if head
"<thead>" "<tbody>") html
))
26293 (while (setq line
(pop lines
))
26295 (if (string-match "^[ \t]*|-" line
)
26298 (push (if head
"</thead>" "</tbody>") html
)
26299 (if lines
(push "<tbody>" html
) (setq tbopen nil
)))
26300 (setq head nil
) ;; head ends here, first time around
26301 ;; ignore this line
26302 (throw 'next-line t
)))
26303 ;; Break the line into fields
26304 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
26305 (unless fnum
(setq fnum
(make-vector (length fields
) 0)))
26306 (setq nlines
(1+ nlines
) i -
1)
26307 (push (concat "<tr>"
26311 (if (and (< i nlines
)
26312 (string-match org-table-number-regexp x
))
26313 (incf (aref fnum i
)))
26315 (concat (car org-export-table-header-tags
) x
26316 (cdr org-export-table-header-tags
))
26317 (concat (car org-export-table-data-tags
) x
26318 (cdr org-export-table-data-tags
))))
26322 (unless splice
(if tbopen
(push "</tbody>" html
)))
26323 (unless splice
(push "</table>\n" html
))
26324 (setq html
(nreverse html
))
26326 ;; Put in col tags with the alignment (unfortuntely often ignored...)
26329 (setq gr
(pop org-table-colgroup-info
))
26330 (format "%s<col align=\"%s\"></col>%s"
26331 (if (memq gr
'(:start
:startend
))
26333 (if colgropen
"</colgroup>\n<colgroup>" "<colgroup>")
26334 (setq colgropen t
))
26336 (if (> (/ (float x
) nlines
) org-table-number-fraction
)
26338 (if (memq gr
'(:end
:startend
))
26339 (progn (setq colgropen nil
) "</colgroup>")
26343 (if colgropen
(setq html
(cons (car html
) (cons "</colgroup>" (cdr html
)))))
26344 (push html-table-tag html
))
26345 (concat (mapconcat 'identity html
"\n") "\n")))
26347 (defun org-table-clean-before-export (lines)
26348 "Check if the table has a marking column.
26349 If yes remove the column and the special lines."
26350 (setq org-table-colgroup-info nil
)
26353 (lambda (x) (or (string-match "^[ \t]*|-" x
)
26354 (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x
)))
26357 (setq org-table-clean-did-remove-column nil
)
26362 ((string-match "^[ \t]*| */ *|" x
)
26363 (setq org-table-colgroup-info
26364 (mapcar (lambda (x)
26365 (cond ((member x
'("<" "<")) :start
)
26366 ((member x
'(">" ">")) :end
)
26367 ((member x
'("<>" "<>")) :startend
)
26369 (org-split-string x
"[ \t]*|[ \t]*")))
26373 (setq org-table-clean-did-remove-column t
)
26378 ((string-match "^[ \t]*| */ *|" x
)
26379 (setq org-table-colgroup-info
26380 (mapcar (lambda (x)
26381 (cond ((member x
'("<" "<")) :start
)
26382 ((member x
'(">" ">")) :end
)
26383 ((member x
'("<>" "<>")) :startend
)
26385 (cdr (org-split-string x
"[ \t]*|[ \t]*"))))
26387 ((string-match "^[ \t]*| *[!_^/] *|" x
)
26388 nil
) ; ignore this line
26389 ((or (string-match "^\\([ \t]*\\)|-+\\+" x
)
26390 (string-match "^\\([ \t]*\\)|[^|]*|" x
))
26391 ;; remove the first column
26392 (replace-match "\\1|" t nil x
))))
26395 (defun org-format-table-table-html (lines)
26396 "Format a table generated by table.el into HTML.
26397 This conversion does *not* use `table-generate-source' from table.el.
26398 This has the advantage that Org-mode's HTML conversions can be used.
26399 But it has the disadvantage, that no cell- or row-spanning is allowed."
26400 (let (line field-buffer
26401 (head org-export-highlight-first-table-line
)
26403 (setq html
(concat html-table-tag
"\n"))
26404 (while (setq line
(pop lines
))
26405 (setq empty
" ")
26407 (if (string-match "^[ \t]*\\+-" line
)
26418 (if (equal x
"") (setq x empty
))
26420 (concat (car org-export-table-header-tags
) x
26421 (cdr org-export-table-header-tags
))
26422 (concat (car org-export-table-data-tags
) x
26423 (cdr org-export-table-data-tags
))))
26427 (setq field-buffer nil
)))
26428 ;; Ignore this line
26429 (throw 'next-line t
)))
26430 ;; Break the line into fields and store the fields
26431 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
26433 (setq field-buffer
(mapcar
26435 (concat x
"<br/>" (pop fields
)))
26437 (setq field-buffer fields
))))
26438 (setq html
(concat html
"</table>\n"))
26441 (defun org-format-table-table-html-using-table-generate-source (lines)
26442 "Format a table into html, using `table-generate-source' from table.el.
26443 This has the advantage that cell- or row-spanning is allowed.
26444 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
26446 (with-current-buffer (get-buffer-create " org-tmp1 ")
26448 (insert (mapconcat 'identity lines
"\n"))
26449 (goto-char (point-min))
26450 (if (not (re-search-forward "|[^+]" nil t
))
26451 (error "Error processing table"))
26452 (table-recognize-table)
26453 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
26454 (table-generate-source 'html
" org-tmp2 ")
26455 (set-buffer " org-tmp2 ")
26456 (buffer-substring (point-min) (point-max))))
26458 (defun org-html-handle-time-stamps (s)
26459 "Format time stamps in string S, or remove them."
26462 (while (string-match org-maybe-keyword-time-regexp s
)
26463 (if (and (match-end 1) (equal (match-string 1 s
) org-clock-string
))
26464 ;; never export CLOCK
26466 (or b
(setq b
(substring s
0 (match-beginning 0))))
26467 (if (not org-export-with-timestamps
)
26468 (setq r
(concat r
(substring s
0 (match-beginning 0)))
26469 s
(substring s
(match-end 0)))
26471 r
(substring s
0 (match-beginning 0))
26473 (format "@<span class=\"timestamp-kwd\">%s @</span>"
26474 (match-string 1 s
)))
26475 (format " @<span class=\"timestamp\">%s@</span>"
26477 (org-translate-time (match-string 3 s
)) 1 -
1)))
26478 s
(substring s
(match-end 0)))))
26479 ;; Line break if line started and ended with time stamp stuff
26482 (setq r
(concat r s
))
26483 (unless (string-match "\\S-" (concat b s
))
26484 (setq r
(concat r
"@<br/>")))
26487 (defun org-html-protect (s)
26488 ;; convert & to &, < to < and > to >
26490 (while (string-match "&" s start
)
26491 (setq s
(replace-match "&" t t s
)
26492 start
(1+ (match-beginning 0))))
26493 (while (string-match "<" s
)
26494 (setq s
(replace-match "<" t t s
)))
26495 (while (string-match ">" s
)
26496 (setq s
(replace-match ">" t t s
))))
26499 (defun org-export-cleanup-toc-line (s)
26500 "Remove tags and time staps from lines going into the toc."
26501 (when (memq org-export-with-tags
'(not-in-toc nil
))
26502 (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s
)
26503 (setq s
(replace-match "" t t s
))))
26504 (when org-export-remove-timestamps-from-toc
26505 (while (string-match org-maybe-keyword-time-regexp s
)
26506 (setq s
(replace-match "" t t s
))))
26507 (while (string-match org-bracket-link-regexp s
)
26508 (setq s
(replace-match (match-string (if (match-end 3) 3 1) s
)
26512 (defun org-html-expand (string)
26513 "Prepare STRING for HTML export. Applies all active conversions.
26514 If there are links in the string, don't modify these."
26515 (let* ((re (concat org-bracket-link-regexp
"\\|"
26516 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
26518 (while (setq m
(string-match re string
))
26519 (setq s
(substring string
0 m
)
26520 l
(match-string 0 string
)
26521 string
(substring string
(match-end 0)))
26522 (push (org-html-do-expand s
) res
)
26524 (push (org-html-do-expand string
) res
)
26525 (apply 'concat
(nreverse res
))))
26527 (defun org-html-do-expand (s)
26528 "Apply all active conversions to translate special ASCII to HTML."
26529 (setq s
(org-html-protect s
))
26530 (if org-export-html-expand
26532 (while (string-match "@<\\([^&]*\\)>" s
)
26533 (setq s
(replace-match "<\\1>" t nil s
)))))
26534 (if org-export-with-emphasize
26535 (setq s
(org-export-html-convert-emphasize s
)))
26536 (if org-export-with-special-strings
26537 (setq s
(org-export-html-convert-special-strings s
)))
26538 (if org-export-with-sub-superscripts
26539 (setq s
(org-export-html-convert-sub-super s
)))
26540 (if org-export-with-TeX-macros
26541 (let ((start 0) wd ass
)
26542 (while (setq start
(string-match "\\\\\\([a-zA-Z]+\\)" s start
))
26543 (if (get-text-property (match-beginning 0) 'org-protected s
)
26544 (setq start
(match-end 0))
26545 (setq wd
(match-string 1 s
))
26546 (if (setq ass
(assoc wd org-html-entities
))
26547 (setq s
(replace-match (or (cdr ass
)
26548 (concat "&" (car ass
) ";"))
26550 (setq start
(+ start
(length wd
))))))))
26553 (defun org-create-multibrace-regexp (left right n
)
26554 "Create a regular expression which will match a balanced sexp.
26555 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
26556 as single character strings.
26557 The regexp returned will match the entire expression including the
26558 delimiters. It will also define a single group which contains the
26559 match except for the outermost delimiters. The maximum depth of
26560 stacked delimiters is N. Escaping delimiters is not possible."
26561 (let* ((nothing (concat "[^" "\\" left
"\\" right
"]*?"))
26564 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
26567 re
(concat re or next
)
26568 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
26569 (concat left
"\\(" re
"\\)" right
)))
26571 (defvar org-match-substring-regexp
26573 "\\([^\\]\\)\\([_^]\\)\\("
26574 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
26576 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
26578 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
26579 "The regular expression matching a sub- or superscript.")
26581 (defvar org-match-substring-with-braces-regexp
26583 "\\([^\\]\\)\\([_^]\\)\\("
26584 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
26586 "The regular expression matching a sub- or superscript, forcing braces.")
26588 (defconst org-export-html-special-string-regexps
26589 '(("\\\\-" .
"­")
26590 ("---\\([^-]\\)" .
"—\\1")
26591 ("--\\([^-]\\)" .
"–\\1")
26592 ("\\.\\.\\." .
"…"))
26593 "Regular expressions for special string conversion.")
26595 (defun org-export-html-convert-special-strings (string)
26596 "Convert special characters in STRING to HTML."
26597 (let ((all org-export-html-special-string-regexps
)
26599 (while (setq a
(pop all
))
26600 (setq re
(car a
) rpl
(cdr a
) start
0)
26601 (while (string-match re string start
)
26602 (if (get-text-property (match-beginning 0) 'org-protected string
)
26603 (setq start
(match-end 0))
26604 (setq string
(replace-match rpl t nil string
)))))
26607 (defun org-export-html-convert-sub-super (string)
26608 "Convert sub- and superscripts in STRING to HTML."
26609 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
26610 (while (string-match org-match-substring-regexp string s
)
26612 ((and requireb
(match-end 8)) (setq s
(match-end 2)))
26613 ((get-text-property (match-beginning 2) 'org-protected string
)
26614 (setq s
(match-end 2)))
26616 (setq s
(match-end 1)
26617 key
(if (string= (match-string 2 string
) "_") "sub" "sup")
26618 c
(or (match-string 8 string
)
26619 (match-string 6 string
)
26620 (match-string 5 string
))
26621 string
(replace-match
26622 (concat (match-string 1 string
)
26623 "<" key
">" c
"</" key
">")
26625 (while (string-match "\\\\\\([_^]\\)" string
)
26626 (setq string
(replace-match (match-string 1 string
) t t string
)))
26629 (defun org-export-html-convert-emphasize (string)
26632 (while (string-match org-emph-re string s
)
26634 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
26635 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
26636 (setq s
(match-beginning 0)
26639 (match-string 1 string
)
26640 (nth 2 (assoc (match-string 3 string
) org-emphasis-alist
))
26641 (match-string 4 string
)
26642 (nth 3 (assoc (match-string 3 string
)
26643 org-emphasis-alist
))
26644 (match-string 5 string
))
26645 string
(replace-match rpl t t string
)
26646 s
(+ s
(- (length rpl
) 2)))
26650 (defvar org-par-open nil
)
26651 (defun org-open-par ()
26652 "Insert <p>, but first close previous paragraph if any."
26653 (org-close-par-maybe)
26655 (setq org-par-open t
))
26656 (defun org-close-par-maybe ()
26657 "Close paragraph if there is one open."
26660 (setq org-par-open nil
)))
26661 (defun org-close-li ()
26662 "Close <li> if necessary."
26663 (org-close-par-maybe)
26664 (insert "</li>\n"))
26666 (defvar body-only
) ; dynamically scoped into this.
26667 (defun org-html-level-start (level title umax with-toc head-count
)
26668 "Insert a new level in HTML export.
26669 When TITLE is nil, just close all open levels."
26670 (org-close-par-maybe)
26671 (let ((l org-level-max
))
26672 (while (>= l level
)
26673 (if (aref org-levels-open
(1- l
))
26675 (org-html-level-close l umax
)
26676 (aset org-levels-open
(1- l
) nil
)))
26679 ;; If title is nil, this means this function is called to close
26680 ;; all levels, so the rest is done only if title is given
26681 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
26682 (setq title
(replace-match
26683 (if org-export-with-tags
26686 " <span class=\"tag\">"
26687 (mapconcat 'identity
(org-split-string
26688 (match-string 1 title
) ":")
26695 (if (aref org-levels-open
(1- level
))
26698 (insert "<li>" title
"<br/>\n"))
26699 (aset org-levels-open
(1- level
) t
)
26700 (org-close-par-maybe)
26701 (insert "<ul>\n<li>" title
"<br/>\n")))
26702 (aset org-levels-open
(1- level
) t
)
26703 (if (and org-export-with-section-numbers
(not body-only
))
26704 (setq title
(concat (org-section-number level
) " " title
)))
26705 (setq level
(+ level org-export-html-toplevel-hlevel -
1))
26707 (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
26708 level level head-count title level
))
26709 (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level
)))
26712 (defun org-html-level-close (level max-outline-level
)
26713 "Terminate one level in HTML export."
26714 (if (<= level max-outline-level
)
26715 (insert "</div>\n")
26717 (insert "</ul>\n")))
26719 ;;; iCalendar export
26722 (defun org-export-icalendar-this-file ()
26723 "Export current file as an iCalendar file.
26724 The iCalendar file will be located in the same directory as the Org-mode
26725 file, but with extension `.ics'."
26727 (org-export-icalendar nil buffer-file-name
))
26730 (defun org-export-icalendar-all-agenda-files ()
26731 "Export all files in `org-agenda-files' to iCalendar .ics files.
26732 Each iCalendar file will be located in the same directory as the Org-mode
26733 file, but with extension `.ics'."
26735 (apply 'org-export-icalendar nil
(org-agenda-files t
)))
26738 (defun org-export-icalendar-combine-agenda-files ()
26739 "Export all files in `org-agenda-files' to a single combined iCalendar file.
26740 The file is stored under the name `org-combined-agenda-icalendar-file'."
26742 (apply 'org-export-icalendar t
(org-agenda-files t
)))
26744 (defun org-export-icalendar (combine &rest files
)
26745 "Create iCalendar files for all elements of FILES.
26746 If COMBINE is non-nil, combine all calendar entries into a single large
26747 file and store it under the name `org-combined-agenda-icalendar-file'."
26749 (org-prepare-agenda-buffers files
)
26750 (let* ((dir (org-export-directory
26751 :ical
(list :publishing-directory
26752 org-export-publishing-directory
)))
26753 file ical-file ical-buffer category started org-agenda-new-buffers
)
26754 (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
26757 (if (file-name-absolute-p org-combined-agenda-icalendar-file
)
26758 org-combined-agenda-icalendar-file
26759 (expand-file-name org-combined-agenda-icalendar-file dir
))
26760 ical-buffer
(org-get-agenda-file-buffer ical-file
))
26761 (set-buffer ical-buffer
) (erase-buffer))
26762 (while (setq file
(pop files
))
26764 (org-check-agenda-file file
)
26765 (set-buffer (org-get-agenda-file-buffer file
))
26767 (setq ical-file
(concat (file-name-as-directory dir
)
26768 (file-name-sans-extension
26769 (file-name-nondirectory buffer-file-name
))
26771 (setq ical-buffer
(org-get-agenda-file-buffer ical-file
))
26772 (with-current-buffer ical-buffer
(erase-buffer)))
26773 (setq category
(or org-category
26774 (file-name-sans-extension
26775 (file-name-nondirectory buffer-file-name
))))
26776 (if (symbolp category
) (setq category
(symbol-name category
)))
26777 (let ((standard-output ical-buffer
))
26779 (and (not started
) (setq started t
)
26780 (org-start-icalendar-file org-icalendar-combined-name
))
26781 (org-start-icalendar-file category
))
26782 (org-print-icalendar-entries combine
)
26783 (when (or (and combine
(not files
)) (not combine
))
26784 (org-finish-icalendar-file)
26785 (set-buffer ical-buffer
)
26787 (run-hooks 'org-after-save-iCalendar-file-hook
)))))
26788 (org-release-buffers org-agenda-new-buffers
))))
26790 (defvar org-after-save-iCalendar-file-hook nil
26791 "Hook run after an iCalendar file has been saved.
26792 The iCalendar buffer is still current when this hook is run.
26793 A good way to use this is to tell a desktop calenndar application to re-read
26794 the iCalendar file.")
26796 (defun org-print-icalendar-entries (&optional combine
)
26797 "Print iCalendar entries for the current Org-mode file to `standard-output'.
26798 When COMBINE is non nil, add the category to each line."
26799 (let ((re1 (concat org-ts-regexp
"\\|<%%([^>\n]+>"))
26800 (re2 (concat "--?-?\\(" org-ts-regexp
"\\)"))
26801 (dts (org-ical-ts-to-string
26802 (format-time-string (cdr org-time-stamp-formats
) (current-time))
26804 hd ts ts2 state status
(inc t
) pos b sexp rrule
26805 scheduledp deadlinep tmp pri category entry location summary desc
26806 (sexp-buffer (get-buffer-create "*ical-tmp*")))
26807 (org-refresh-category-properties)
26809 (goto-char (point-min))
26810 (while (re-search-forward re1 nil t
)
26813 (when (boundp 'org-icalendar-verify-function
)
26814 (unless (funcall org-icalendar-verify-function
)
26815 (outline-next-heading)
26817 (throw :skip nil
)))
26818 (setq pos
(match-beginning 0)
26819 ts
(match-string 0)
26821 hd
(org-get-heading)
26822 summary
(org-icalendar-cleanup-string
26823 (org-entry-get nil
"SUMMARY"))
26824 desc
(org-icalendar-cleanup-string
26825 (or (org-entry-get nil
"DESCRIPTION")
26826 (and org-icalendar-include-body
(org-get-entry)))
26827 t org-icalendar-include-body
)
26828 location
(org-icalendar-cleanup-string
26829 (org-entry-get nil
"LOCATION"))
26830 category
(org-get-category))
26831 (if (looking-at re2
)
26833 (goto-char (match-end 0))
26834 (setq ts2
(match-string 1) inc nil
))
26835 (setq tmp
(buffer-substring (max (point-min)
26836 (- pos org-ds-keyword-length
))
26838 ts2
(if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts
)
26841 (replace-match "\\1" t nil ts
))
26843 deadlinep
(string-match org-deadline-regexp tmp
)
26844 scheduledp
(string-match org-scheduled-regexp tmp
)
26845 ;; donep (org-entry-is-done-p)
26847 (if (or (string-match org-tr-regexp hd
)
26848 (string-match org-ts-regexp hd
))
26849 (setq hd
(replace-match "" t t hd
)))
26850 (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts
)
26852 (concat "\nRRULE:FREQ="
26854 (match-string 2 ts
)
26855 '(("d" .
"DAILY")("w" .
"WEEKLY")
26856 ("m" .
"MONTHLY")("y" .
"YEARLY"))))
26857 ";INTERVAL=" (match-string 1 ts
)))
26859 (setq summary
(or summary hd
))
26860 (if (string-match org-bracket-link-regexp summary
)
26862 (replace-match (if (match-end 3)
26863 (match-string 3 summary
)
26864 (match-string 1 summary
))
26866 (if deadlinep
(setq summary
(concat "DL: " summary
)))
26867 (if scheduledp
(setq summary
(concat "S: " summary
)))
26868 (if (string-match "\\`<%%" ts
)
26869 (with-current-buffer sexp-buffer
26870 (insert (substring ts
1 -
1) " " summary
"\n"))
26871 (princ (format "BEGIN:VEVENT
26877 (org-ical-ts-to-string ts
"DTSTART")
26878 (org-ical-ts-to-string ts2
"DTEND" inc
)
26880 (if (and desc
(string-match "\\S-" desc
))
26881 (concat "\nDESCRIPTION: " desc
) "")
26882 (if (and location
(string-match "\\S-" location
))
26883 (concat "\nLOCATION: " location
) "")
26886 (when (and org-icalendar-include-sexps
26887 (condition-case nil
(require 'icalendar
) (error nil
))
26888 (fboundp 'icalendar-export-region
))
26889 ;; Get all the literal sexps
26890 (goto-char (point-min))
26891 (while (re-search-forward "^&?%%(" nil t
)
26894 (setq b
(match-beginning 0))
26895 (goto-char (1- (match-end 0)))
26898 (setq sexp
(buffer-substring b
(point)))
26899 (with-current-buffer sexp-buffer
26900 (insert sexp
"\n"))
26901 (princ (org-diary-to-ical-string sexp-buffer
)))))
26903 (when org-icalendar-include-todo
26904 (goto-char (point-min))
26905 (while (re-search-forward org-todo-line-regexp nil t
)
26908 (when (boundp 'org-icalendar-verify-function
)
26909 (unless (funcall org-icalendar-verify-function
)
26910 (outline-next-heading)
26912 (throw :skip nil
)))
26913 (setq state
(match-string 2))
26914 (setq status
(if (member state org-done-keywords
)
26915 "COMPLETED" "NEEDS-ACTION"))
26917 (or (not (member state org-done-keywords
))
26918 (eq org-icalendar-include-todo
'all
))
26919 (not (member org-archive-tag
(org-get-tags-at)))
26921 (setq hd
(match-string 3)
26922 summary
(org-icalendar-cleanup-string
26923 (org-entry-get nil
"SUMMARY"))
26924 desc
(org-icalendar-cleanup-string
26925 (or (org-entry-get nil
"DESCRIPTION")
26926 (and org-icalendar-include-body
(org-get-entry)))
26927 t org-icalendar-include-body
)
26928 location
(org-icalendar-cleanup-string
26929 (org-entry-get nil
"LOCATION")))
26930 (if (string-match org-bracket-link-regexp hd
)
26931 (setq hd
(replace-match (if (match-end 3) (match-string 3 hd
)
26932 (match-string 1 hd
))
26934 (if (string-match org-priority-regexp hd
)
26935 (setq pri
(string-to-char (match-string 2 hd
))
26936 hd
(concat (substring hd
0 (match-beginning 1))
26937 (substring hd
(match-end 1))))
26938 (setq pri org-default-priority
))
26939 (setq pri
(floor (1+ (* 8.
(/ (float (- org-lowest-priority pri
))
26940 (- org-lowest-priority org-highest-priority
))))))
26942 (princ (format "BEGIN:VTODO
26952 (if (and location
(string-match "\\S-" location
))
26953 (concat "\nLOCATION: " location
) "")
26954 (if (and desc
(string-match "\\S-" desc
))
26955 (concat "\nDESCRIPTION: " desc
) "")
26956 category pri status
)))))))))
26958 (defun org-icalendar-cleanup-string (s &optional is-body maxlength
)
26959 "Take out stuff and quote what needs to be quoted.
26960 When IS-BODY is non-nil, assume that this is the body of an item, clean up
26961 whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
26966 (let ((re (concat "\\(" org-drawer-regexp
"\\)[^\000]*?:END:.*\n?"))
26967 (re2 (concat "^[ \t]*" org-keyword-time-regexp
".*\n?")))
26968 (while (string-match re s
) (setq s
(replace-match "" t t s
)))
26969 (while (string-match re2 s
) (setq s
(replace-match "" t t s
)))))
26971 (while (string-match "\\([,;\\]\\)" s start
)
26972 (setq start
(+ (match-beginning 0) 2)
26973 s
(replace-match "\\\\\\1" nil nil s
))))
26975 (while (string-match "[ \t]*\n[ \t]*" s
)
26976 (setq s
(replace-match "\\n" t t s
))))
26977 (setq s
(org-trim s
))
26980 (if (and (numberp maxlength
)
26981 (> (length s
) maxlength
))
26982 (setq s
(substring s
0 maxlength
)))))
26985 (defun org-get-entry ()
26986 "Clean-up description string."
26988 (org-back-to-heading t
)
26989 (buffer-substring (point-at-bol 2) (org-end-of-subtree t
))))
26991 (defun org-start-icalendar-file (name)
26992 "Start an iCalendar file by inserting the header."
26993 (let ((user user-full-name
)
26994 (name (or name
"unknown"))
26995 (timezone (cadr (current-time-zone))))
26997 (format "BEGIN:VCALENDAR
27000 PRODID:-//%s//Emacs with Org-mode//EN
27002 CALSCALE:GREGORIAN\n" name user timezone
))))
27004 (defun org-finish-icalendar-file ()
27005 "Finish an iCalendar file by inserting the END statement."
27006 (princ "END:VCALENDAR\n"))
27008 (defun org-ical-ts-to-string (s keyword
&optional inc
)
27009 "Take a time string S and convert it to iCalendar format.
27010 KEYWORD is added in front, to make a complete line like DTSTART....
27011 When INC is non-nil, increase the hour by two (if time string contains
27012 a time), or the day by one (if it does not contain a time)."
27013 (let ((t1 (org-parse-time-string s
'nodefault
))
27014 t2 fmt have-time time
)
27015 (if (and (car t1
) (nth 1 t1
) (nth 2 t1
))
27016 (setq t2 t1 have-time t
)
27017 (setq t2
(org-parse-time-string s
)))
27018 (let ((s (car t2
)) (mi (nth 1 t2
)) (h (nth 2 t2
))
27019 (d (nth 3 t2
)) (m (nth 4 t2
)) (y (nth 5 t2
)))
27022 (if org-agenda-default-appointment-duration
27023 (setq mi
(+ org-agenda-default-appointment-duration mi
))
27026 (setq time
(encode-time s mi h d m y
)))
27027 (setq fmt
(if have-time
":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
27028 (concat keyword
(format-time-string fmt time
))))
27032 (defun org-export-as-xoxo-insert-into (buffer &rest output
)
27033 (with-current-buffer buffer
27034 (apply 'insert output
)))
27035 (put 'org-export-as-xoxo-insert-into
'lisp-indent-function
1)
27037 (defun org-export-as-xoxo (&optional buffer
)
27038 "Export the org buffer as XOXO.
27039 The XOXO buffer is named *xoxo-<source buffer name>*"
27040 (interactive (list (current-buffer)))
27041 ;; A quickie abstraction
27043 ;; Output everything as XOXO
27044 (with-current-buffer (get-buffer buffer
)
27045 (let* ((pos (point))
27046 (opt-plist (org-combine-plists (org-default-export-plist)
27047 (org-infile-export-plist)))
27048 (filename (concat (file-name-as-directory
27049 (org-export-directory :xoxo opt-plist
))
27050 (file-name-sans-extension
27051 (file-name-nondirectory buffer-file-name
))
27053 (out (find-file-noselect filename
))
27056 (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
27057 ;; Check the output buffer is empty.
27058 (with-current-buffer out
(erase-buffer))
27059 ;; Kick off the output
27060 (org-export-as-xoxo-insert-into out
"<ol class='xoxo'>\n")
27061 (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't
)
27062 (let* ((hd (match-string-no-properties 1))
27063 (level (length hd
))
27065 (match-string-no-properties 2)
27067 (goto-char (match-end 0))
27072 (if (looking-at "^[ \t]\\(.*\\)")
27073 (setq str
(concat str
(match-string-no-properties 1)))
27074 (throw 'loop str
)))))))))
27076 ;; Handle level rendering
27078 ((> level last-level
)
27079 (org-export-as-xoxo-insert-into out
"\n<ol>\n"))
27081 ((< level last-level
)
27082 (dotimes (- (- last-level level
) 1)
27084 (org-export-as-xoxo-insert-into out
"</li>\n"))
27085 (org-export-as-xoxo-insert-into out
"</ol>\n"))
27087 (org-export-as-xoxo-insert-into out
"</li>\n")
27088 (setq hanging-li nil
)))
27090 ((equal level last-level
)
27092 (org-export-as-xoxo-insert-into out
"</li>\n")))
27095 (setq last-level level
)
27097 ;; And output the new li
27098 (setq hanging-li
't
)
27099 (if (equal ?
+ (elt text
0))
27100 (org-export-as-xoxo-insert-into out
"<li class='" (substring text
1) "'>")
27101 (org-export-as-xoxo-insert-into out
"<li>" text
))))
27103 ;; Finally finish off the ol
27104 (dotimes (- last-level
1)
27106 (org-export-as-xoxo-insert-into out
"</li>\n"))
27107 (org-export-as-xoxo-insert-into out
"</ol>\n"))
27110 ;; Finish the buffer off and clean it up.
27111 (switch-to-buffer-other-window out
)
27112 (indent-region (point-min) (point-max) nil
)
27114 (goto-char (point-min))
27120 ;; Make `C-c C-x' a prefix key
27121 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
27123 ;; TAB key with modifiers
27124 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
27125 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
27126 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
27127 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
27128 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
27129 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
27130 ;; The following line is necessary under Suse GNU/Linux
27131 (unless (featurep 'xemacs
)
27132 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
27133 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
27134 (define-key org-mode-map
[backtab] 'org-shifttab)
27136 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
27137 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
27138 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
27140 ;; Cursor keys with modifiers
27141 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
27142 (org-defkey org-mode-map [(meta right)] 'org-metaright)
27143 (org-defkey org-mode-map [(meta up)] 'org-metaup)
27144 (org-defkey org-mode-map [(meta down)] 'org-metadown)
27146 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
27147 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
27148 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
27149 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
27151 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
27152 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
27153 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
27154 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
27156 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
27157 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
27159 ;;; Extra keys for tty access.
27160 ;; We only set them when really needed because otherwise the
27161 ;; menus don't show the simple keys
27163 (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
27164 (not window-system))
27165 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
27166 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
27167 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
27168 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
27169 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
27170 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
27171 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
27172 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
27173 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
27174 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
27175 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
27176 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
27177 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
27178 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
27179 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
27180 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
27181 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
27182 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
27183 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
27184 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
27185 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
27186 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
27188 ;; All the other keys
27190 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
27191 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
27192 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
27193 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
27194 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
27195 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
27196 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
27197 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
27198 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
27199 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
27200 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
27201 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
27202 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
27203 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
27204 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
27205 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
27206 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
27207 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
27208 (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
27209 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
27210 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
27211 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
27212 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
27213 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
27214 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
27215 (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
27216 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
27217 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
27218 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
27219 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
27220 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
27221 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
27222 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
27223 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
27224 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
27225 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
27226 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
27227 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
27228 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
27229 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
27230 (org-defkey org-mode-map "\C-c^" 'org-sort)
27231 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
27232 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
27233 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
27234 (org-defkey org-mode-map "\C-m" 'org-return)
27235 (org-defkey org-mode-map "\C-j" 'org-return-indent)
27236 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
27237 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
27238 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
27239 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
27240 (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
27241 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
27242 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
27243 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
27244 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
27245 (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
27246 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
27247 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
27248 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
27249 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
27250 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
27252 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
27253 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
27254 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
27255 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
27257 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
27258 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
27259 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
27260 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
27261 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
27262 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
27263 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
27264 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
27265 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
27266 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
27267 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
27268 (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
27270 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
27272 (when (featurep 'xemacs)
27273 (org-defkey org-mode-map 'button3 'popup-mode-menu))
27275 (defsubst org-table-p () (org-at-table-p))
27277 (defun org-self-insert-command (N)
27278 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
27279 If the cursor is in a table looking at whitespace, the whitespace is
27280 overwritten, and the table is not marked as requiring realignment."
27282 (if (and (org-table-p)
27284 ;; check if we blank the field, and if that triggers align
27285 (and org-table-auto-blank-field
27286 (member last-command
27287 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
27288 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
27289 ;; got extra space, this field does not determine column width
27290 (let (org-table-may-need-update) (org-table-blank-field))
27291 ;; no extra space, this field may determine column width
27292 (org-table-blank-field)))
27295 (looking-at "[^|\n]* |"))
27296 (let (org-table-may-need-update)
27297 (goto-char (1- (match-end 0)))
27298 (delete-backward-char 1)
27299 (goto-char (match-beginning 0))
27300 (self-insert-command N))
27301 (setq org-table-may-need-update t)
27302 (self-insert-command N)
27303 (org-fix-tags-on-the-fly)))
27305 (defun org-fix-tags-on-the-fly ()
27306 (when (and (equal (char-after (point-at-bol)) ?*)
27307 (org-on-heading-p))
27308 (org-align-tags-here org-tags-column)))
27310 (defun org-delete-backward-char (N)
27311 "Like `delete-backward-char', insert whitespace at field end in tables.
27312 When deleting backwards, in tables this function will insert whitespace in
27313 front of the next \"|\" separator, to keep the table aligned. The table will
27314 still be marked for re-alignment if the field did fill the entire column,
27315 because, in this case the deletion might narrow the column."
27317 (if (and (org-table-p)
27319 (string-match "|" (buffer-substring (point-at-bol) (point)))
27320 (looking-at ".*?|"))
27321 (let ((pos (point))
27322 (noalign (looking-at "[^|\n\r]* |"))
27323 (c org-table-may-need-update))
27324 (backward-delete-char N)
27325 (skip-chars-forward "^|")
27327 (goto-char (1- pos))
27328 ;; noalign: if there were two spaces at the end, this field
27329 ;; does not determine the width of the column.
27330 (if noalign (setq org-table-may-need-update c)))
27331 (backward-delete-char N)
27332 (org-fix-tags-on-the-fly)))
27334 (defun org-delete-char (N)
27335 "Like `delete-char', but insert whitespace at field end in tables.
27336 When deleting characters, in tables this function will insert whitespace in
27337 front of the next \"|\" separator, to keep the table aligned. The table will
27338 still be marked for re-alignment if the field did fill the entire column,
27339 because, in this case the deletion might narrow the column."
27341 (if (and (org-table-p)
27343 (not (= (char-after) ?|))
27345 (if (looking-at ".*?|")
27346 (let ((pos (point))
27347 (noalign (looking-at "[^|\n\r]* |"))
27348 (c org-table-may-need-update))
27349 (replace-match (concat
27350 (substring (match-string 0) 1 -1)
27353 ;; noalign: if there were two spaces at the end, this field
27354 ;; does not determine the width of the column.
27355 (if noalign (setq org-table-may-need-update c)))
27358 (org-fix-tags-on-the-fly)))
27360 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
27361 (put 'org-self-insert-command 'delete-selection t)
27362 (put 'orgtbl-self-insert-command 'delete-selection t)
27363 (put 'org-delete-char 'delete-selection 'supersede)
27364 (put 'org-delete-backward-char 'delete-selection 'supersede)
27366 ;; Make `flyspell-mode' delay after some commands
27367 (put 'org-self-insert-command 'flyspell-delayed t)
27368 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
27369 (put 'org-delete-char 'flyspell-delayed t)
27370 (put 'org-delete-backward-char 'flyspell-delayed t)
27372 ;; Make pabbrev-mode expand after org-mode commands
27373 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
27374 (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
27376 ;; How to do this: Measure non-white length of current string
27377 ;; If equal to column width, we should realign.
27379 (defun org-remap (map &rest commands)
27380 "In MAP, remap the functions given in COMMANDS.
27381 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
27384 (setq old (pop commands) new (pop commands))
27385 (if (fboundp 'command-remapping)
27386 (org-defkey map (vector 'remap old) new)
27387 (substitute-key-definition old new map global-map)))))
27389 (when (eq org-enable-table-editor 'optimized)
27390 ;; If the user wants maximum table support, we need to hijack
27391 ;; some standard editing functions
27392 (org-remap org-mode-map
27393 'self-insert-command 'org-self-insert-command
27394 'delete-char 'org-delete-char
27395 'delete-backward-char 'org-delete-backward-char)
27396 (org-defkey org-mode-map "|" 'org-force-self-insert))
27398 (defun org-shiftcursor-error ()
27399 "Throw an error because Shift-Cursor command was applied in wrong context."
27400 (error "This command is active in special context like tables, headlines or timestamps"))
27402 (defun org-shifttab (&optional arg)
27403 "Global visibility cycling or move to previous table field.
27404 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
27406 See the individual commands for more information."
27409 ((org-at-table-p) (call-interactively 'org-table-previous-field))
27410 (arg (message "Content view to level: ")
27411 (org-content (prefix-numeric-value arg))
27412 (setq org-cycle-global-status 'overview))
27413 (t (call-interactively 'org-global-cycle))))
27415 (defun org-shiftmetaleft ()
27416 "Promote subtree or delete table column.
27417 Calls `org-promote-subtree', `org-outdent-item',
27418 or `org-table-delete-column', depending on context.
27419 See the individual commands for more information."
27422 ((org-at-table-p) (call-interactively 'org-table-delete-column))
27423 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
27424 ((org-at-item-p) (call-interactively 'org-outdent-item))
27425 (t (org-shiftcursor-error))))
27427 (defun org-shiftmetaright ()
27428 "Demote subtree or insert table column.
27429 Calls `org-demote-subtree', `org-indent-item',
27430 or `org-table-insert-column', depending on context.
27431 See the individual commands for more information."
27434 ((org-at-table-p) (call-interactively 'org-table-insert-column))
27435 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
27436 ((org-at-item-p) (call-interactively 'org-indent-item))
27437 (t (org-shiftcursor-error))))
27439 (defun org-shiftmetaup (&optional arg)
27440 "Move subtree up or kill table row.
27441 Calls `org-move-subtree-up' or `org-table-kill-row' or
27442 `org-move-item-up' depending on context. See the individual commands
27443 for more information."
27446 ((org-at-table-p) (call-interactively 'org-table-kill-row))
27447 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
27448 ((org-at-item-p) (call-interactively 'org-move-item-up))
27449 (t (org-shiftcursor-error))))
27450 (defun org-shiftmetadown (&optional arg)
27451 "Move subtree down or insert table row.
27452 Calls `org-move-subtree-down' or `org-table-insert-row' or
27453 `org-move-item-down', depending on context. See the individual
27454 commands for more information."
27457 ((org-at-table-p) (call-interactively 'org-table-insert-row))
27458 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
27459 ((org-at-item-p) (call-interactively 'org-move-item-down))
27460 (t (org-shiftcursor-error))))
27462 (defun org-metaleft (&optional arg)
27463 "Promote heading or move table column to left.
27464 Calls `org-do-promote' or `org-table-move-column', depending on context.
27465 With no specific context, calls the Emacs default `backward-word'.
27466 See the individual commands for more information."
27469 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
27470 ((or (org-on-heading-p) (org-region-active-p))
27471 (call-interactively 'org-do-promote))
27472 ((org-at-item-p) (call-interactively 'org-outdent-item))
27473 (t (call-interactively 'backward-word))))
27475 (defun org-metaright (&optional arg)
27476 "Demote subtree or move table column to right.
27477 Calls `org-do-demote' or `org-table-move-column', depending on context.
27478 With no specific context, calls the Emacs default `forward-word'.
27479 See the individual commands for more information."
27482 ((org-at-table-p) (call-interactively 'org-table-move-column))
27483 ((or (org-on-heading-p) (org-region-active-p))
27484 (call-interactively 'org-do-demote))
27485 ((org-at-item-p) (call-interactively 'org-indent-item))
27486 (t (call-interactively 'forward-word))))
27488 (defun org-metaup (&optional arg)
27489 "Move subtree up or move table row up.
27490 Calls `org-move-subtree-up' or `org-table-move-row' or
27491 `org-move-item-up', depending on context. See the individual commands
27492 for more information."
27495 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
27496 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
27497 ((org-at-item-p) (call-interactively 'org-move-item-up))
27498 (t (transpose-lines 1) (beginning-of-line -1))))
27500 (defun org-metadown (&optional arg)
27501 "Move subtree down or move table row down.
27502 Calls `org-move-subtree-down' or `org-table-move-row' or
27503 `org-move-item-down', depending on context. See the individual
27504 commands for more information."
27507 ((org-at-table-p) (call-interactively 'org-table-move-row))
27508 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
27509 ((org-at-item-p) (call-interactively 'org-move-item-down))
27510 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
27512 (defun org-shiftup (&optional arg)
27513 "Increase item in timestamp or increase priority of current headline.
27514 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
27515 depending on context. See the individual commands for more information."
27518 ((org-at-timestamp-p t)
27519 (call-interactively (if org-edit-timestamp-down-means-later
27520 'org-timestamp-down 'org-timestamp-up)))
27521 ((org-on-heading-p) (call-interactively 'org-priority-up))
27522 ((org-at-item-p) (call-interactively 'org-previous-item))
27523 (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
27525 (defun org-shiftdown (&optional arg)
27526 "Decrease item in timestamp or decrease priority of current headline.
27527 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
27528 depending on context. See the individual commands for more information."
27531 ((org-at-timestamp-p t)
27532 (call-interactively (if org-edit-timestamp-down-means-later
27533 'org-timestamp-up 'org-timestamp-down)))
27534 ((org-on-heading-p) (call-interactively 'org-priority-down))
27535 (t (call-interactively 'org-next-item))))
27537 (defun org-shiftright ()
27538 "Next TODO keyword or timestamp one day later, depending on context."
27541 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
27542 ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
27543 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
27544 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
27545 (t (org-shiftcursor-error))))
27547 (defun org-shiftleft ()
27548 "Previous TODO keyword or timestamp one day earlier, depending on context."
27551 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
27552 ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
27553 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
27554 ((org-at-property-p)
27555 (call-interactively 'org-property-previous-allowed-value))
27556 (t (org-shiftcursor-error))))
27558 (defun org-shiftcontrolright ()
27559 "Switch to next TODO set."
27562 ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
27563 (t (org-shiftcursor-error))))
27565 (defun org-shiftcontrolleft ()
27566 "Switch to previous TODO set."
27569 ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
27570 (t (org-shiftcursor-error))))
27572 (defun org-ctrl-c-ret ()
27573 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
27576 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
27577 (t (call-interactively 'org-insert-heading))))
27579 (defun org-copy-special ()
27580 "Copy region in table or copy current subtree.
27581 Calls `org-table-copy' or `org-copy-subtree', depending on context.
27582 See the individual commands for more information."
27584 (call-interactively
27585 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
27587 (defun org-cut-special ()
27588 "Cut region in table or cut current subtree.
27589 Calls `org-table-copy' or `org-cut-subtree', depending on context.
27590 See the individual commands for more information."
27592 (call-interactively
27593 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
27595 (defun org-paste-special (arg)
27596 "Paste rectangular region into table, or past subtree relative to level.
27597 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
27598 See the individual commands for more information."
27600 (if (org-at-table-p)
27601 (org-table-paste-rectangle)
27602 (org-paste-subtree arg)))
27604 (defun org-ctrl-c-ctrl-c (&optional arg)
27605 "Set tags in headline, or update according to changed information at point.
27607 This command does many different things, depending on context:
27609 - If the cursor is in a headline, prompt for tags and insert them
27610 into the current line, aligned to `org-tags-column'. When called
27611 with prefix arg, realign all tags in the current buffer.
27613 - If the cursor is in one of the special #+KEYWORD lines, this
27614 triggers scanning the buffer for these lines and updating the
27617 - If the cursor is inside a table, realign the table. This command
27618 works even if the automatic table editor has been turned off.
27620 - If the cursor is on a #+TBLFM line, re-apply the formulas to
27623 - If the cursor is a the beginning of a dynamic block, update it.
27625 - If the cursor is inside a table created by the table.el package,
27626 activate that table.
27628 - If the current buffer is a remember buffer, close note and file it.
27629 with a prefix argument, file it without further interaction to the default
27632 - If the cursor is on a <<<target>>>, update radio targets and corresponding
27633 links in this buffer.
27635 - If the cursor is on a numbered item in a plain list, renumber the
27638 - If the cursor is on a checkbox, toggle it."
27640 (let ((org-enable-table-editor t))
27642 ((or org-clock-overlays
27643 org-occur-highlights
27644 org-latex-fragment-image-overlays)
27645 (org-remove-clock-overlays)
27646 (org-remove-occur-highlights)
27647 (org-remove-latex-fragment-image-overlays)
27648 (message "Temporary highlights/overlays removed from current buffer"))
27649 ((and (local-variable-p 'org-finish-function (current-buffer))
27650 (fboundp org-finish-function))
27651 (funcall org-finish-function))
27652 ((org-at-property-p)
27653 (call-interactively 'org-property-action))
27654 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
27655 ((org-on-heading-p) (call-interactively 'org-set-tags))
27656 ((org-at-table.el-p)
27658 (beginning-of-line 1)
27659 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
27660 (call-interactively 'table-recognize-table))
27662 (org-table-maybe-eval-formula)
27664 (call-interactively 'org-table-recalculate)
27665 (org-table-maybe-recalculate-line))
27666 (call-interactively 'org-table-align))
27667 ((org-at-item-checkbox-p)
27668 (call-interactively 'org-toggle-checkbox))
27670 (call-interactively 'org-maybe-renumber-ordered-list))
27671 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
27673 (beginning-of-line 1)
27674 (org-update-dblock))
27675 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
27677 ((equal (match-string 1) "TBLFM")
27678 ;; Recalculate the table before this line
27680 (beginning-of-line 1)
27681 (skip-chars-backward " \r\n\t")
27682 (if (org-at-table-p)
27683 (org-call-with-arg 'org-table-recalculate t))))
27685 (call-interactively 'org-mode-restart))))
27686 (t (error "C-c C-c can do nothing useful at this location.")))))
27688 (defun org-mode-restart ()
27689 "Restart Org-mode, to scan again for special lines.
27690 Also updates the keyword regular expressions."
27692 (let ((org-inhibit-startup t)) (org-mode))
27693 (message "Org-mode restarted to refresh keyword and special line setup"))
27695 (defun org-kill-note-or-show-branches ()
27696 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
27698 (if (not org-finish-function)
27699 (call-interactively 'show-branches)
27700 (let ((org-note-abort t))
27701 (funcall org-finish-function))))
27703 (defun org-return (&optional indent)
27704 "Goto next table row or insert a newline.
27705 Calls `org-table-next-row' or `newline', depending on context.
27706 See the individual commands for more information."
27709 ((bobp) (if indent (newline-and-indent) (newline)))
27710 ((and (org-at-heading-p)
27712 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
27717 (org-table-justify-field-maybe)
27718 (call-interactively 'org-table-next-row))
27719 (t (if indent (newline-and-indent) (newline)))))
27721 (defun org-return-indent ()
27722 "Goto next table row or insert a newline and indent.
27723 Calls `org-table-next-row' or `newline-and-indent', depending on
27724 context. See the individual commands for more information."
27728 (defun org-ctrl-c-star ()
27729 "Compute table, or change heading status of lines.
27730 Calls `org-table-recalculate' or `org-toggle-region-headlines',
27731 depending on context. This will also turn a plain list item or a normal
27732 line into a subheading."
27736 (call-interactively 'org-table-recalculate))
27737 ((org-region-active-p)
27738 ;; Convert all lines in region to list items
27739 (call-interactively 'org-toggle-region-headings))
27740 ((org-on-heading-p)
27741 (org-toggle-region-headings (point-at-bol)
27742 (min (1+ (point-at-eol)) (point-max))))
27744 ;; Convert to heading
27745 (let ((level (save-match-data
27747 (condition-case nil
27749 (org-back-to-heading t)
27750 (funcall outline-level))
27753 (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
27754 (t (org-toggle-region-headings (point-at-bol)
27755 (min (1+ (point-at-eol)) (point-max))))))
27757 (defun org-ctrl-c-minus ()
27758 "Insert separator line in table or modify bullet status of line.
27759 Also turns a plain line or a region of lines into list items.
27760 Calls `org-table-insert-hline', `org-toggle-region-items', or
27761 `org-cycle-list-bullet', depending on context."
27765 (call-interactively 'org-table-insert-hline))
27766 ((org-on-heading-p)
27769 (beginning-of-line 1)
27770 (if (looking-at "\\*+ ")
27771 (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
27772 ((org-region-active-p)
27773 ;; Convert all lines in region to list items
27774 (call-interactively 'org-toggle-region-items))
27776 (call-interactively 'org-cycle-list-bullet))
27777 (t (org-toggle-region-items (point-at-bol)
27778 (min (1+ (point-at-eol)) (point-max))))))
27780 (defun org-toggle-region-items (beg end)
27781 "Convert all lines in region to list items.
27782 If the first line is already an item, convert all list items in the region
27788 (setq l2 (org-current-line))
27790 (beginning-of-line 1)
27791 (setq l (1- (org-current-line)))
27792 (if (org-at-item-p)
27793 ;; We already have items, de-itemize
27794 (while (< (setq l (1+ l)) l2)
27795 (when (org-at-item-p)
27796 (goto-char (match-beginning 2))
27797 (delete-region (match-beginning 2) (match-end 2))
27798 (and (looking-at "[ \t]+") (replace-match "")))
27799 (beginning-of-line 2))
27800 (while (< (setq l (1+ l)) l2)
27801 (unless (org-at-item-p)
27802 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
27803 (replace-match "\\1- \\2")))
27804 (beginning-of-line 2))))))
27806 (defun org-toggle-region-headings (beg end)
27807 "Convert all lines in region to list items.
27808 If the first line is already an item, convert all list items in the region
27814 (setq l2 (org-current-line))
27816 (beginning-of-line 1)
27817 (setq l (1- (org-current-line)))
27818 (if (org-on-heading-p)
27819 ;; We already have headlines, de-star them
27820 (while (< (setq l (1+ l)) l2)
27821 (when (org-on-heading-p t)
27822 (and (looking-at outline-regexp) (replace-match "")))
27823 (beginning-of-line 2))
27824 (let* ((stars (save-excursion
27825 (re-search-backward org-complex-heading-regexp nil t)
27826 (or (match-string 1) "*")))
27827 (add-stars (if org-odd-levels-only "**" "*"))
27828 (rpl (concat stars add-stars " \\2")))
27829 (while (< (setq l (1+ l)) l2)
27830 (unless (org-on-heading-p)
27831 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
27832 (replace-match rpl)))
27833 (beginning-of-line 2)))))))
27835 (defun org-meta-return (&optional arg)
27836 "Insert a new heading or wrap a region in a table.
27837 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
27838 See the individual commands for more information."
27842 (call-interactively 'org-table-wrap-region))
27843 (t (call-interactively 'org-insert-heading))))
27847 ;; Define the Org-mode menus
27848 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
27850 ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
27851 ["Next Field" org-cycle (org-at-table-p)]
27852 ["Previous Field" org-shifttab (org-at-table-p)]
27853 ["Next Row" org-return (org-at-table-p)]
27855 ["Blank Field" org-table-blank-field (org-at-table-p)]
27856 ["Edit Field" org-table-edit-field (org-at-table-p)]
27857 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
27860 ["Move Column Left" org-metaleft (org-at-table-p)]
27861 ["Move Column Right" org-metaright (org-at-table-p)]
27862 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
27863 ["Insert Column" org-shiftmetaright (org-at-table-p)])
27865 ["Move Row Up" org-metaup (org-at-table-p)]
27866 ["Move Row Down" org-metadown (org-at-table-p)]
27867 ["Delete Row" org-shiftmetaup (org-at-table-p)]
27868 ["Insert Row" org-shiftmetadown (org-at-table-p)]
27869 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
27871 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
27873 ["Copy Rectangle" org-copy-special (org-at-table-p)]
27874 ["Cut Rectangle" org-cut-special (org-at-table-p)]
27875 ["Paste Rectangle" org-paste-special (org-at-table-p)]
27876 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
27879 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
27880 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
27881 ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
27883 ["Recalculate line" org-table-recalculate (org-at-table-p)]
27884 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
27885 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
27887 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
27889 ["Sum Column/Rectangle" org-table-sum
27890 (or (org-at-table-p) (org-region-active-p))]
27891 ["Which Column?" org-table-current-column (org-at-table-p)])
27893 org-table-toggle-formula-debugger
27894 :style toggle :selected org-table-formula-debug]
27895 ["Show Col/Row Numbers"
27896 org-table-toggle-coordinate-overlays
27897 :style toggle :selected org-table-overlay-coordinates]
27899 ["Create" org-table-create (and (not (org-at-table-p))
27900 org-enable-table-editor)]
27901 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
27902 ["Import from File" org-table-import (not (org-at-table-p))]
27903 ["Export to File" org-table-export (org-at-table-p)]
27905 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
27907 (easy-menu-define org-org-menu org-mode-map "Org menu"
27910 ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
27911 ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
27912 ["Sparse Tree" org-occur t]
27913 ["Reveal Context" org-reveal t]
27914 ["Show All" show-all t]
27916 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
27918 ["New Heading" org-insert-heading t]
27919 ("Navigate Headings"
27920 ["Up" outline-up-heading t]
27921 ["Next" outline-next-visible-heading t]
27922 ["Previous" outline-previous-visible-heading t]
27923 ["Next Same Level" outline-forward-same-level t]
27924 ["Previous Same Level" outline-backward-same-level t]
27926 ["Jump" org-goto t])
27928 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
27929 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
27931 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
27932 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
27933 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
27935 ["Promote Heading" org-metaleft (not (org-at-table-p))]
27936 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
27937 ["Demote Heading" org-metaright (not (org-at-table-p))]
27938 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
27940 ["Sort Region/Children" org-sort (not (org-at-table-p))]
27942 ["Convert to odd levels" org-convert-to-odd-levels t]
27943 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
27945 ["Emphasis..." org-emphasize t])
27947 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
27948 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
27949 ; :active t :keys "C-u C-c C-x C-a"]
27950 ["Sparse trees open ARCHIVE trees"
27951 (setq org-sparse-tree-open-archived-trees
27952 (not org-sparse-tree-open-archived-trees))
27953 :style toggle :selected org-sparse-tree-open-archived-trees]
27954 ["Cycling opens ARCHIVE trees"
27955 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
27956 :style toggle :selected org-cycle-open-archived-trees]
27957 ["Agenda includes ARCHIVE trees"
27958 (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
27959 :style toggle :selected (not org-agenda-skip-archived-trees)]
27961 ["Move Subtree to Archive" org-advertized-archive-subtree t]
27962 ; ["Check and Move Children" (org-archive-subtree '(4))
27963 ; :active t :keys "C-u C-c C-x C-s"]
27967 ["TODO/DONE/-" org-todo t]
27969 ["Next keyword" org-shiftright (org-on-heading-p)]
27970 ["Previous keyword" org-shiftleft (org-on-heading-p)]
27971 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
27972 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
27973 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
27974 ["Show TODO Tree" org-show-todo-tree t]
27975 ["Global TODO list" org-todo-list t]
27977 ["Set Priority" org-priority t]
27978 ["Priority Up" org-shiftup t]
27979 ["Priority Down" org-shiftdown t])
27980 ("TAGS and Properties"
27981 ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
27982 ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
27984 ["Set property" 'org-set-property t]
27985 ["Column view of properties" org-columns t]
27986 ["Insert Column View DBlock" org-insert-columns-dblock t])
27987 ("Dates and Scheduling"
27988 ["Timestamp" org-time-stamp t]
27989 ["Timestamp (inactive)" org-time-stamp-inactive t]
27991 ["1 Day Later" org-shiftright t]
27992 ["1 Day Earlier" org-shiftleft t]
27993 ["1 ... Later" org-shiftup t]
27994 ["1 ... Earlier" org-shiftdown t])
27995 ["Compute Time Range" org-evaluate-time-range t]
27996 ["Schedule Item" org-schedule t]
27997 ["Deadline" org-deadline t]
27999 ["Custom time format" org-toggle-time-stamp-overlays
28000 :style radio :selected org-display-custom-times]
28002 ["Goto Calendar" org-goto-calendar t]
28003 ["Date from Calendar" org-date-from-calendar t])
28005 ["Clock in" org-clock-in t]
28006 ["Clock out" org-clock-out t]
28007 ["Clock cancel" org-clock-cancel t]
28008 ["Goto running clock" org-clock-goto t]
28009 ["Display times" org-clock-display t]
28010 ["Create clock table" org-clock-report t]
28012 ["Record DONE time"
28013 (progn (setq org-log-done (not org-log-done))
28014 (message "Switching to %s will %s record a timestamp"
28015 (car org-done-keywords)
28016 (if org-log-done "automatically" "not")))
28017 :style toggle :selected org-log-done])
28019 ["Agenda Command..." org-agenda t]
28020 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
28021 ("File List for Agenda")
28022 ("Special views current file"
28023 ["TODO Tree" org-show-todo-tree t]
28024 ["Check Deadlines" org-check-deadlines t]
28025 ["Timeline" org-timeline t]
28026 ["Tags Tree" org-tags-sparse-tree t])
28029 ["Store Link (Global)" org-store-link t]
28030 ["Insert Link" org-insert-link t]
28031 ["Follow Link" org-open-at-point t]
28033 ["Next link" org-next-link t]
28034 ["Previous link" org-previous-link t]
28036 ["Descriptive Links"
28037 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
28038 :style radio :selected (member '(org-link) buffer-invisibility-spec)]
28041 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
28042 :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
28044 ["Export/Publish..." org-export t]
28046 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
28047 :selected org-cdlatex-mode]
28048 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
28049 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
28050 ["Modify math symbol" org-cdlatex-math-modify
28051 (org-inside-LaTeX-fragment-p)]
28052 ["Export LaTeX fragments as images"
28053 (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
28054 :style toggle :selected org-export-with-LaTeX-fragments])
28057 ["Show Version" org-version t]
28058 ["Info Documentation" org-info t])
28060 ["Browse Org Group" org-customize t]
28062 ["Expand This Menu" org-create-customize-menu
28063 (fboundp 'customize-menu-create)])
28065 ["Refresh setup" org-mode-restart t]
28068 (defun org-info (&optional node)
28069 "Read documentation for Org-mode in the info system.
28070 With optional NODE, go directly to that node."
28072 (info (format "(org)%s" (or node ""))))
28074 (defun org-install-agenda-files-menu ()
28075 (let ((bl (buffer-list)))
28078 (set-buffer (pop bl))
28079 (if (org-mode-p) (setq bl nil)))
28082 '("Org") "File List for Agenda"
28085 ["Edit File List" (org-edit-agenda-file-list) t]
28086 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
28087 ["Remove Current File from List" org-remove-file t]
28088 ["Cycle through agenda files" org-cycle-agenda-files t]
28089 ["Occur in all agenda files" org-occur-in-agenda-files t]
28091 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
28095 (defun org-customize ()
28096 "Call the customize function with org as argument."
28098 (customize-browse 'org))
28100 (defun org-create-customize-menu ()
28101 "Create a full customization menu for Org-mode, insert it into the menu."
28103 (if (fboundp 'customize-menu-create)
28106 '("Org") "Customize"
28107 `(["Browse Org group" org-customize t]
28109 ,(customize-menu-create 'org)
28110 ["Set" Custom-set t]
28111 ["Save" Custom-save t]
28112 ["Reset to Current" Custom-reset-current t]
28113 ["Reset to Saved" Custom-reset-saved t]
28114 ["Reset to Standard Settings" Custom-reset-standard t]))
28115 (message "\"Org\"-menu now contains full customization menu"))
28116 (error "Cannot expand menu (outdated version of cus-edit.el)")))
28118 ;;;; Miscellaneous stuff
28121 ;;; Generally useful functions
28123 (defun org-context ()
28124 "Return a list of contexts of the current cursor position.
28125 If several contexts apply, all are returned.
28126 Each context entry is a list with a symbol naming the context, and
28127 two positions indicating start and end of the context. Possible
28130 :headline anywhere in a headline
28131 :headline-stars on the leading stars in a headline
28132 :todo-keyword on a TODO keyword (including DONE) in a headline
28133 :tags on the TAGS in a headline
28134 :priority on the priority cookie in a headline
28135 :item on the first line of a plain list item
28136 :item-bullet on the bullet/number of a plain list item
28137 :checkbox on the checkbox in a plain list item
28138 :table in an org-mode table
28139 :table-special on a special filed in a table
28140 :table-table in a table.el table
28141 :link on a hyperlink
28142 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
28143 :target on a <<target>>
28144 :radio-target on a <<<radio-target>>>
28145 :latex-fragment on a LaTeX fragment
28146 :latex-preview on a LaTeX fragment with overlayed preview image
28148 This function expects the position to be visible because it uses font-lock
28149 faces as a help to recognize the following contexts: :table-special, :link,
28151 (let* ((f (get-text-property (point) 'face))
28152 (faces (if (listp f) f (list f)))
28153 (p (point)) clist o)
28154 ;; First the large context
28156 ((org-on-heading-p t)
28157 (push (list :headline (point-at-bol) (point-at-eol)) clist)
28159 (beginning-of-line 1)
28160 (looking-at org-todo-line-tags-regexp))
28161 (push (org-point-in-group p 1 :headline-stars) clist)
28162 (push (org-point-in-group p 2 :todo-keyword) clist)
28163 (push (org-point-in-group p 4 :tags) clist))
28165 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
28166 (if (looking-at "\\[#[A-Z0-9]\\]")
28167 (push (org-point-in-group p 0 :priority) clist)))
28170 (push (org-point-in-group p 2 :item-bullet) clist)
28171 (push (list :item (point-at-bol)
28172 (save-excursion (org-end-of-item) (point)))
28174 (and (org-at-item-checkbox-p)
28175 (push (org-point-in-group p 0 :checkbox) clist)))
28178 (push (list :table (org-table-begin) (org-table-end)) clist)
28179 (if (memq 'org-formula faces)
28180 (push (list :table-special
28181 (previous-single-property-change p 'face)
28182 (next-single-property-change p 'face)) clist)))
28183 ((org-at-table-p 'any)
28184 (push (list :table-table) clist)))
28187 ;; Now the small context
28189 ((org-at-timestamp-p)
28190 (push (org-point-in-group p 0 :timestamp) clist))
28191 ((memq 'org-link faces)
28193 (previous-single-property-change p 'face)
28194 (next-single-property-change p 'face)) clist))
28195 ((memq 'org-special-keyword faces)
28196 (push (list :keyword
28197 (previous-single-property-change p 'face)
28198 (next-single-property-change p 'face)) clist))
28200 (push (org-point-in-group p 0 :target) clist)
28201 (goto-char (1- (match-beginning 0)))
28202 (if (looking-at org-radio-target-regexp)
28203 (push (org-point-in-group p 0 :radio-target) clist))
28205 ((setq o (car (delq nil
28208 (if (memq x org-latex-fragment-image-overlays) x))
28209 (org-overlays-at (point))))))
28210 (push (list :latex-fragment
28211 (org-overlay-start o) (org-overlay-end o)) clist)
28212 (push (list :latex-preview
28213 (org-overlay-start o) (org-overlay-end o)) clist))
28214 ((org-inside-LaTeX-fragment-p)
28215 ;; FIXME: positions wrong.
28216 (push (list :latex-fragment (point) (point)) clist)))
28218 (setq clist (nreverse (delq nil clist)))
28221 ;; FIXME: Compare with at-regexp-p Do we need both?
28222 (defun org-in-regexp (re &optional nlines visually)
28223 "Check if point is inside a match of regexp.
28224 Normally only the current line is checked, but you can include NLINES extra
28225 lines both before and after point into the search.
28226 If VISUALLY is set, require that the cursor is not after the match but
28227 really on, so that the block visually is on the match."
28229 (let ((pos (point))
28230 (eol (point-at-eol (+ 1 (or nlines 0))))
28231 (inc (if visually 1 0)))
28233 (beginning-of-line (- 1 (or nlines 0)))
28234 (while (re-search-forward re eol t)
28235 (if (and (<= (match-beginning 0) pos)
28236 (>= (+ inc (match-end 0)) pos))
28237 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
28239 (defun org-at-regexp-p (regexp)
28240 "Is point inside a match of REGEXP in the current line?"
28243 (let ((pos (point)) (end (point-at-eol)))
28244 (beginning-of-line 1)
28245 (while (re-search-forward regexp end t)
28246 (if (and (<= (match-beginning 0) pos)
28247 (>= (match-end 0) pos))
28251 (defun org-occur-in-agenda-files (regexp &optional nlines)
28252 "Call `multi-occur' with buffers for all agenda files."
28253 (interactive "sOrg-files matching: \np")
28254 (let* ((files (org-agenda-files))
28255 (tnames (mapcar 'file-truename files))
28256 (extra org-agenda-text-search-extra-files)
28258 (while (setq f (pop extra))
28259 (unless (member (file-truename f) tnames)
28260 (add-to-list 'files f 'append)
28261 (add-to-list 'tnames (file-truename f) 'append)))
28263 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
28266 (if (boundp 'occur-mode-find-occurrence-hook)
28268 (add-hook 'occur-mode-find-occurrence-hook
28273 (defadvice occur-mode-goto-occurrence
28274 (after org-occur-reveal activate)
28275 (and (org-mode-p) (org-reveal)))
28276 (defadvice occur-mode-goto-occurrence-other-window
28277 (after org-occur-reveal activate)
28278 (and (org-mode-p) (org-reveal)))
28279 (defadvice occur-mode-display-occurrence
28280 (after org-occur-reveal activate)
28282 (let ((pos (occur-mode-find-occurrence)))
28283 (with-current-buffer (marker-buffer pos)
28288 (defun org-uniquify (list)
28289 "Remove duplicate elements from LIST."
28291 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
28294 (defun org-delete-all (elts list)
28295 "Remove all elements in ELTS from LIST."
28297 (setq list (delete (pop elts) list)))
28300 (defun org-back-over-empty-lines ()
28301 "Move backwards over witespace, to the beginning of the first empty line.
28302 Returns the number o empty lines passed."
28303 (let ((pos (point)))
28304 (skip-chars-backward " \t\n\r")
28305 (beginning-of-line 2)
28306 (goto-char (min (point) pos))
28307 (count-lines (point) pos)))
28309 (defun org-skip-whitespace ()
28310 (skip-chars-forward " \t\n\r"))
28312 (defun org-point-in-group (point group &optional context)
28313 "Check if POINT is in match-group GROUP.
28314 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
28315 match. If the match group does ot exist or point is not inside it,
28317 (and (match-beginning group)
28318 (>= point (match-beginning group))
28319 (<= point (match-end group))
28321 (list context (match-beginning group) (match-end group))
28324 (defun org-switch-to-buffer-other-window (&rest args)
28325 "Switch to buffer in a second window on the current frame.
28326 In particular, do not allow pop-up frames."
28327 (let (pop-up-frames special-display-buffer-names special-display-regexps
28328 special-display-function)
28329 (apply 'switch-to-buffer-other-window args)))
28331 (defun org-combine-plists (&rest plists)
28332 "Create a single property list from all plists in PLISTS.
28333 The process starts by copying the first list, and then setting properties
28334 from the other lists. Settings in the last list are the most significant
28335 ones and overrule settings in the other lists."
28336 (let ((rtn (copy-sequence (pop plists)))
28339 (setq ls (pop plists))
28341 (setq p (pop ls) v (pop ls))
28342 (setq rtn (plist-put rtn p v))))
28345 (defun org-move-line-down (arg)
28346 "Move the current line down. With prefix argument, move it past ARG lines."
28348 (let ((col (current-column))
28350 (beginning-of-line 1) (setq beg (point))
28351 (beginning-of-line 2) (setq end (point))
28352 (beginning-of-line (+ 1 arg))
28353 (setq pos (move-marker (make-marker) (point)))
28354 (insert (delete-and-extract-region beg end))
28356 (move-to-column col)))
28358 (defun org-move-line-up (arg)
28359 "Move the current line up. With prefix argument, move it past ARG lines."
28361 (let ((col (current-column))
28363 (beginning-of-line 1) (setq beg (point))
28364 (beginning-of-line 2) (setq end (point))
28365 (beginning-of-line (- arg))
28366 (setq pos (move-marker (make-marker) (point)))
28367 (insert (delete-and-extract-region beg end))
28369 (move-to-column col)))
28371 (defun org-replace-escapes (string table)
28372 "Replace %-escapes in STRING with values in TABLE.
28373 TABLE is an association list with keys like \"%a\" and string values.
28374 The sequences in STRING may contain normal field width and padding information,
28375 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
28376 so values can contain further %-escapes if they are define later in TABLE."
28377 (let ((case-fold-search nil)
28379 (while (setq e (pop table))
28380 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
28381 (while (string-match re string)
28382 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
28384 (setq string (replace-match rpl t t string))))
28388 (defun org-sublist (list start end)
28389 "Return a section of LIST, from START to END.
28390 Counting starts at 1."
28391 (let (rtn (c start))
28392 (setq list (nthcdr (1- start) list))
28393 (while (and list (<= c end))
28394 (push (pop list) rtn)
28398 (defun org-find-base-buffer-visiting (file)
28399 "Like `find-buffer-visiting' but alway return the base buffer and
28400 not an indirect buffer"
28401 (let ((buf (find-buffer-visiting file)))
28403 (or (buffer-base-buffer buf) buf)
28406 (defun org-image-file-name-regexp ()
28407 "Return regexp matching the file names of images."
28408 (if (fboundp 'image-file-name-regexp)
28409 (image-file-name-regexp)
28410 (let ((image-file-name-extensions
28411 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
28412 "xbm" "xpm" "pbm" "pgm" "ppm")))
28414 (regexp-opt (nconc (mapcar 'upcase
28415 image-file-name-extensions)
28416 image-file-name-extensions)
28420 (defun org-file-image-p (file)
28421 "Return non-nil if FILE is an image."
28423 (string-match (org-image-file-name-regexp) file)))
28425 ;;; Paragraph filling stuff.
28426 ;; We want this to be just right, so use the full arsenal.
28428 (defun org-indent-line-function ()
28429 "Indent line like previous, but further if previous was headline or item."
28431 (let* ((pos (point))
28432 (itemp (org-at-item-p))
28433 column bpos bcol tpos tcol bullet btype bullet-type)
28434 ;; Find the previous relevant line
28435 (beginning-of-line 1)
28437 ((looking-at "#") (setq column 0))
28438 ((looking-at "\\*+ ") (setq column 0))
28440 (beginning-of-line 0)
28441 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
28442 (beginning-of-line 0))
28444 ((looking-at "\\*+[ \t]+")
28445 (goto-char (match-end 0))
28446 (setq column (current-column)))
28448 (org-beginning-of-item)
28449 ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
28450 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
28451 (setq bpos (match-beginning 1) tpos (match-end 0)
28452 bcol (progn (goto-char bpos) (current-column))
28453 tcol (progn (goto-char tpos) (current-column))
28454 bullet (match-string 1)
28455 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
28459 (beginning-of-line 1)
28460 (if (looking-at "\\S-")
28462 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
28463 (setq bullet (match-string 1)
28464 btype (if (string-match "[0-9]" bullet) "n" bullet))
28465 (setq column (if (equal btype bullet-type) bcol tcol)))
28466 (setq column (org-get-indentation)))))
28467 (t (setq column (org-get-indentation))))))
28469 (if (<= (current-column) (current-indentation))
28470 (indent-line-to column)
28471 (save-excursion (indent-line-to column)))
28472 (setq column (current-column))
28473 (beginning-of-line 1)
28475 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
28476 (replace-match (concat "\\1" (format org-property-format
28477 (match-string 2) (match-string 3)))
28479 (move-to-column column)))
28481 (defun org-set-autofill-regexps ()
28483 ;; In the paragraph separator we include headlines, because filling
28484 ;; text in a line directly attached to a headline would otherwise
28485 ;; fill the headline as well.
28486 (org-set-local 'comment-start-skip "^#+[ \t]*")
28487 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
28488 ;; The paragraph starter includes hand-formatted lists.
28489 (org-set-local 'paragraph-start
28490 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
28491 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
28492 ;; But only if the user has not turned off tables or fixed-width regions
28494 'auto-fill-inhibit-regexp
28495 (concat "\\*+ \\|#\\+"
28496 "\\|[ \t]*" org-keyword-time-regexp
28497 (if (or org-enable-table-editor org-enable-fixed-width-editor)
28500 (if org-enable-table-editor "|" "")
28501 (if org-enable-fixed-width-editor ":" "")
28503 ;; We use our own fill-paragraph function, to make sure that tables
28504 ;; and fixed-width regions are not wrapped. That function will pass
28505 ;; through to `fill-paragraph' when appropriate.
28506 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
28507 ; Adaptive filling: To get full control, first make sure that
28508 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
28509 (org-set-local 'adaptive-fill-regexp "\000")
28510 (org-set-local 'adaptive-fill-function
28511 'org-adaptive-fill-function)
28513 'align-mode-rules-list
28514 '((org-in-buffer-settings
28515 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
28516 (modes . '(org-mode))))))
28518 (defun org-fill-paragraph (&optional justify)
28519 "Re-align a table, pass through to fill-paragraph if no table."
28520 (let ((table-p (org-at-table-p))
28521 (table.el-p (org-at-table.el-p)))
28522 (cond ((and (equal (char-after (point-at-bol)) ?*)
28523 (save-excursion (goto-char (point-at-bol))
28524 (looking-at outline-regexp)))
28525 t) ; skip headlines
28526 (table.el-p t) ; skip table.el tables
28527 (table-p (org-table-align) t) ; align org-mode tables
28528 (t nil)))) ; call paragraph-fill
28530 ;; For reference, this is the default value of adaptive-fill-regexp
28531 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
28533 (defun org-adaptive-fill-function ()
28534 "Return a fill prefix for org-mode files.
28535 In particular, this makes sure hanging paragraphs for hand-formatted lists
28537 (cond ((looking-at "#[ \t]+")
28539 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
28541 (goto-char (match-end 0))
28542 (make-string (current-column) ?\ )))
28545 ;;;; Functions extending outline functionality
28548 (defun org-beginning-of-line (&optional arg)
28549 "Go to the beginning of the current line. If that is invisible, continue
28550 to a visible line beginning. This makes the function of C-a more intuitive.
28551 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
28552 first attempt, and only move to after the tags when the cursor is already
28553 beyond the end of the headline."
28555 (let ((pos (point)))
28556 (beginning-of-line 1)
28560 (if (org-invisible-p)
28561 (while (and (not (bobp)) (org-invisible-p))
28563 (beginning-of-line 1))
28565 (when org-special-ctrl-a/e
28567 ((and (looking-at org-todo-line-regexp)
28568 (= (char-after (match-end 1)) ?\ ))
28570 (if (eq org-special-ctrl-a/e t)
28571 (cond ((> pos (match-beginning 3)) (match-beginning 3))
28572 ((= pos (point)) (match-beginning 3))
28574 (cond ((> pos (point)) (point))
28575 ((not (eq last-command this-command)) (point))
28576 (t (match-beginning 3))))))
28579 (if (eq org-special-ctrl-a/e t)
28580 (cond ((> pos (match-end 4)) (match-end 4))
28581 ((= pos (point)) (match-end 4))
28583 (cond ((> pos (point)) (point))
28584 ((not (eq last-command this-command)) (point))
28585 (t (match-end 4))))))))))
28587 (defun org-end-of-line (&optional arg)
28588 "Go to the end of the line.
28589 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
28590 first attempt, and only move to after the tags when the cursor is already
28591 beyond the end of the headline."
28593 (if (or (not org-special-ctrl-a/e)
28594 (not (org-on-heading-p)))
28596 (let ((pos (point)))
28597 (beginning-of-line 1)
28598 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
28599 (if (eq org-special-ctrl-a/e t)
28600 (if (or (< pos (match-beginning 1))
28601 (= pos (match-end 0)))
28602 (goto-char (match-beginning 1))
28603 (goto-char (match-end 0)))
28604 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
28605 (goto-char (match-end 0))
28606 (goto-char (match-beginning 1))))
28607 (end-of-line arg)))))
28609 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
28610 (define-key org-mode-map "\C-e" 'org-end-of-line)
28612 (defun org-kill-line (&optional arg)
28613 "Kill line, to tags or end of line."
28616 ((or (not org-special-ctrl-k)
28618 (not (org-on-heading-p)))
28619 (call-interactively 'kill-line))
28620 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
28621 (kill-region (point) (match-beginning 1))
28622 (org-set-tags nil t))
28623 (t (kill-region (point) (point-at-eol)))))
28625 (define-key org-mode-map "\C-k" 'org-kill-line)
28627 (defun org-invisible-p ()
28628 "Check if point is at a character currently not visible."
28629 ;; Early versions of noutline don't have `outline-invisible-p'.
28630 (if (fboundp 'outline-invisible-p)
28631 (outline-invisible-p)
28632 (get-char-property (point) 'invisible)))
28634 (defun org-invisible-p2 ()
28635 "Check if point is at a character currently not visible."
28637 (if (and (eolp) (not (bobp))) (backward-char 1))
28638 ;; Early versions of noutline don't have `outline-invisible-p'.
28639 (if (fboundp 'outline-invisible-p)
28640 (outline-invisible-p)
28641 (get-char-property (point) 'invisible))))
28643 (defalias 'org-back-to-heading 'outline-back-to-heading)
28644 (defalias 'org-on-heading-p 'outline-on-heading-p)
28645 (defalias 'org-at-heading-p 'outline-on-heading-p)
28646 (defun org-at-heading-or-item-p ()
28647 (or (org-on-heading-p) (org-at-item-p)))
28649 (defun org-on-target-p ()
28650 (or (org-in-regexp org-radio-target-regexp)
28651 (org-in-regexp org-target-regexp)))
28653 (defun org-up-heading-all (arg)
28654 "Move to the heading line of which the present line is a subheading.
28655 This function considers both visible and invisible heading lines.
28656 With argument, move up ARG levels."
28657 (if (fboundp 'outline-up-heading-all)
28658 (outline-up-heading-all arg) ; emacs 21 version of outline.el
28659 (outline-up-heading arg t))) ; emacs 22 version of outline.el
28661 (defun org-up-heading-safe ()
28662 "Move to the heading line of which the present line is a subheading.
28663 This version will not throw an error. It will return the level of the
28664 headline found, or nil if no higher level is found."
28665 (let ((pos (point)) start-level level
28666 (re (concat "^" outline-regexp)))
28668 (outline-back-to-heading t)
28669 (setq start-level (funcall outline-level))
28670 (if (equal start-level 1) (throw 'exit nil))
28671 (while (re-search-backward re nil t)
28672 (setq level (funcall outline-level))
28673 (if (< level start-level) (throw 'exit level)))
28676 (defun org-first-sibling-p ()
28677 "Is this heading the first child of its parents?"
28679 (let ((re (concat "^" outline-regexp))
28681 (unless (org-at-heading-p t)
28682 (error "Not at a heading"))
28683 (setq level (funcall outline-level))
28685 (if (not (re-search-backward re nil t))
28687 (setq l (funcall outline-level))
28690 (defun org-goto-sibling (&optional previous)
28691 "Goto the next sibling, even if it is invisible.
28692 When PREVIOUS is set, go to the previous sibling instead. Returns t
28693 when a sibling was found. When none is found, return nil and don't
28695 (let ((fun (if previous 're-search-backward 're-search-forward))
28697 (re (concat "^" outline-regexp))
28699 (when (condition-case nil (org-back-to-heading t) (error nil))
28700 (setq level (funcall outline-level))
28702 (or previous (forward-char 1))
28703 (while (funcall fun re nil t)
28704 (setq l (funcall outline-level))
28705 (when (< l level) (goto-char pos) (throw 'exit nil))
28706 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
28710 (defun org-show-siblings ()
28711 "Show all siblings of the current headline."
28713 (while (org-goto-sibling) (org-flag-heading nil)))
28715 (while (org-goto-sibling 'previous)
28716 (org-flag-heading nil))))
28718 (defun org-show-hidden-entry ()
28719 "Show an entry where even the heading is hidden."
28723 (defun org-flag-heading (flag &optional entry)
28724 "Flag the current heading. FLAG non-nil means make invisible.
28725 When ENTRY is non-nil, show the entire entry."
28727 (org-back-to-heading t)
28728 ;; Check if we should show the entire entry
28733 (and (outline-next-heading)
28734 (org-flag-heading nil))))
28735 (outline-flag-region (max (point-min) (1- (point)))
28736 (save-excursion (outline-end-of-heading) (point))
28739 (defun org-end-of-subtree (&optional invisible-OK to-heading)
28740 ;; This is an exact copy of the original function, but it uses
28741 ;; `org-back-to-heading', to make it work also in invisible
28742 ;; trees. And is uses an invisible-OK argument.
28743 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
28744 (org-back-to-heading invisible-OK)
28746 (level (funcall outline-level)))
28747 (while (and (not (eobp))
28748 (or first (> (funcall outline-level) level)))
28750 (outline-next-heading))
28752 (if (memq (preceding-char) '(?\n ?\^M))
28754 ;; Go to end of line before heading
28756 (if (memq (preceding-char) '(?\n ?\^M))
28757 ;; leave blank line before heading
28758 (forward-char -1))))))
28761 (defun org-show-subtree ()
28762 "Show everything after this heading at deeper levels."
28763 (outline-flag-region
28766 (outline-end-of-subtree) (outline-next-heading) (point))
28769 (defun org-show-entry ()
28770 "Show the body directly following this heading.
28771 Show the heading too, if it is currently invisible."
28774 (condition-case nil
28776 (org-back-to-heading t)
28777 (outline-flag-region
28778 (max (point-min) (1- (point)))
28781 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
28782 (or (match-beginning 1) (point-max)))
28786 (defun org-make-options-regexp (kwds)
28787 "Make a regular expression for keyword lines."
28791 (mapconcat 'regexp-quote kwds "\\|")
28795 ;; Make isearch reveal the necessary context
28796 (defun org-isearch-end ()
28797 "Reveal context after isearch exits."
28798 (when isearch-success ; only if search was successful
28799 (if (featurep 'xemacs)
28800 ;; Under XEmacs, the hook is run in the correct place,
28801 ;; we directly show the context.
28802 (org-show-context 'isearch)
28803 ;; In Emacs the hook runs *before* restoring the overlays.
28804 ;; So we have to use a one-time post-command-hook to do this.
28805 ;; (Emacs 22 has a special variable, see function `org-mode')
28806 (unless (and (boundp 'isearch-mode-end-hook-quit)
28807 isearch-mode-end-hook-quit)
28808 ;; Only when the isearch was not quitted.
28809 (org-add-hook 'post-command-hook 'org-isearch-post-command
28810 'append 'local)))))
28812 (defun org-isearch-post-command ()
28813 "Remove self from hook, and show context."
28814 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
28815 (org-show-context 'isearch))
28818 ;;;; Integration with and fixes for other packages
28822 (defvar org-imenu-markers nil
28823 "All markers currently used by Imenu.")
28824 (make-variable-buffer-local 'org-imenu-markers)
28826 (defun org-imenu-new-marker (&optional pos)
28827 "Return a new marker for use by Imenu, and remember the marker."
28828 (let ((m (make-marker)))
28829 (move-marker m (or pos (point)))
28830 (push m org-imenu-markers)
28833 (defun org-imenu-get-tree ()
28834 "Produce the index for Imenu."
28835 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
28836 (setq org-imenu-markers nil)
28837 (let* ((n org-imenu-depth)
28838 (re (concat "^" outline-regexp))
28839 (subs (make-vector (1+ n) nil))
28845 (goto-char (point-max))
28846 (while (re-search-backward re nil t)
28847 (setq level (org-reduced-level (funcall outline-level)))
28849 (looking-at org-complex-heading-regexp)
28850 (setq head (org-match-string-no-properties 4)
28851 m (org-imenu-new-marker))
28852 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
28853 (if (>= level last-level)
28854 (push (cons head m) (aref subs level))
28855 (push (cons head (aref subs (1+ level))) (aref subs level))
28856 (loop for i from (1+ level) to n do (aset subs i nil)))
28857 (setq last-level level)))))
28860 (eval-after-load "imenu"
28862 (add-hook 'imenu-after-jump-hook
28863 (lambda () (org-show-context 'org-goto)))))
28865 ;; Speedbar support
28867 (defun org-speedbar-set-agenda-restriction ()
28868 "Restrict future agenda commands to the location at point in speedbar.
28869 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
28871 (let (p m tp np dir txt w)
28873 ((setq p (text-property-any (point-at-bol) (point-at-eol)
28875 (setq m (get-text-property p 'org-imenu-marker))
28878 (set-buffer (marker-buffer m))
28880 (org-agenda-set-restriction-lock 'subtree))))
28881 ((setq p (text-property-any (point-at-bol) (point-at-eol)
28882 'speedbar-function 'speedbar-find-file))
28883 (setq tp (previous-single-property-change
28884 (1+ p) 'speedbar-function)
28885 np (next-single-property-change
28886 tp 'speedbar-function)
28887 dir (speedbar-line-directory)
28888 txt (buffer-substring-no-properties (or tp (point-min))
28889 (or np (point-max))))
28892 (set-buffer (find-file-noselect
28893 (let ((default-directory dir))
28894 (expand-file-name txt))))
28895 (unless (org-mode-p)
28896 (error "Cannot restrict to non-Org-mode file"))
28897 (org-agenda-set-restriction-lock 'file))))
28898 (t (error "Don't know how to restrict Org-mode's agenda")))
28899 (org-move-overlay org-speedbar-restriction-lock-overlay
28900 (point-at-bol) (point-at-eol))
28901 (setq current-prefix-arg nil)
28902 (org-agenda-maybe-redo)))
28904 (eval-after-load "speedbar"
28906 (speedbar-add-supported-extension ".org")
28907 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
28908 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
28909 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
28910 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
28911 (add-hook 'speedbar-visiting-tag-hook
28912 (lambda () (org-show-context 'org-goto)))))
28915 ;;; Fixes and Hacks
28917 ;; Make flyspell not check words in links, to not mess up our keymap
28918 (defun org-mode-flyspell-verify ()
28919 "Don't let flyspell put overlays at active buttons."
28920 (not (get-text-property (point) 'keymap)))
28922 ;; Make `bookmark-jump' show the jump location if it was hidden.
28923 (eval-after-load "bookmark"
28924 '(if (boundp 'bookmark-after-jump-hook)
28925 ;; We can use the hook
28926 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
28927 ;; Hook not available, use advice
28928 (defadvice bookmark-jump (after org-make-visible activate)
28929 "Make the position visible."
28930 (org-bookmark-jump-unhide))))
28932 (defun org-bookmark-jump-unhide ()
28933 "Unhide the current position, to show the bookmark location."
28935 (or (org-invisible-p)
28936 (save-excursion (goto-char (max (point-min) (1- (point))))
28937 (org-invisible-p)))
28938 (org-show-context 'bookmark-jump)))
28940 ;; Make session.el ignore our circular variable
28941 (eval-after-load "session"
28942 '(add-to-list 'session-globals-exclude 'org-mark-ring))
28944 ;;;; Experimental code
28946 (defun org-closed-in-range ()
28947 "Sparse tree of items closed in a certain time range.
28948 Still experimental, may disappear in the future."
28950 ;; Get the time interval from the user.
28951 (let* ((time1 (time-to-seconds
28952 (org-read-date nil 'to-time nil "Starting date: ")))
28953 (time2 (time-to-seconds
28954 (org-read-date nil 'to-time nil "End date:")))
28955 ;; callback function
28956 (callback (lambda ()
28959 (apply 'encode-time
28960 (org-parse-time-string
28961 (match-string 1))))))
28962 ;; check if time in interval
28963 (and (>= time time1) (<= time time2))))))
28964 ;; make tree, check each match with the callback
28965 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
28972 (run-hooks 'org-load-hook)
28974 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
28975 ;;; org.el ends here