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.22a+"
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."
166 (const :tag
" Mouse support (org-mouse.el)" org-mouse
)
167 (const :tag
"<A> Publishing (org-publish.el)" org-publish
)
168 (const :tag
"<A> LaTeX export (org-export-latex.el)" org-export-latex
)
169 (const :tag
" IRC/ERC links (org-irc.el)" org-irc
)
170 (const :tag
" Apple Mail message links under OS X (org-mac-message.el)" org-mac-message
)))
172 (defun org-load-default-extensions ()
173 "Load all extensions listed in `org-default-extensions'."
175 (condition-case nil
(require ext
)
176 (error (message "Problems while trying to load feature `%s'" ext
))))
177 org-default-extensions
))
179 (eval-after-load "org" '(org-load-default-extensions))
181 ;; FIXME: Needs a separate group...
182 (defcustom org-completion-fallback-command
'hippie-expand
183 "The expansion command called by \\[org-complete] in normal context.
184 Normal means, no org-mode-specific context."
188 (defgroup org-startup nil
189 "Options concerning startup of Org-mode."
193 (defcustom org-startup-folded t
194 "Non-nil means, entering Org-mode will switch to OVERVIEW.
195 This can also be configured on a per-file basis by adding one of
196 the following lines anywhere in the buffer:
203 (const :tag
"nofold: show all" nil
)
204 (const :tag
"fold: overview" t
)
205 (const :tag
"content: all headlines" content
)))
207 (defcustom org-startup-truncated t
208 "Non-nil means, entering Org-mode will set `truncate-lines'.
209 This is useful since some lines containing links can be very long and
210 uninteresting. Also tables look terrible when wrapped."
214 (defcustom org-startup-align-all-tables nil
215 "Non-nil means, align all tables when visiting a file.
216 This is useful when the column width in tables is forced with <N> cookies
217 in table fields. Such tables will look correct only after the first re-align.
218 This can also be configured on a per-file basis by adding one of
219 the following lines anywhere in the buffer:
225 (defcustom org-insert-mode-line-in-empty-file nil
226 "Non-nil means insert the first line setting Org-mode in empty files.
227 When the function `org-mode' is called interactively in an empty file, this
228 normally means that the file name does not automatically trigger Org-mode.
229 To ensure that the file will always be in Org-mode in the future, a
230 line enforcing Org-mode will be inserted into the buffer, if this option
235 (defcustom org-replace-disputed-keys nil
236 "Non-nil means use alternative key bindings for some keys.
237 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
238 These keys are also used by other packages like `CUA-mode' or `windmove.el'.
239 If you want to use Org-mode together with one of these other modes,
240 or more generally if you would like to move some Org-mode commands to
241 other keys, set this variable and configure the keys with the variable
244 This option is only relevant at load-time of Org-mode, and must be set
245 *before* org.el is loaded. Changing it requires a restart of Emacs to
250 (if (fboundp 'defvaralias
)
251 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
253 (defcustom org-disputed-keys
254 '(([(shift up
)] .
[(meta p
)])
255 ([(shift down
)] .
[(meta n
)])
256 ([(shift left
)] .
[(meta -
)])
257 ([(shift right
)] .
[(meta +)])
258 ([(control shift right
)] .
[(meta shift
+)])
259 ([(control shift left
)] .
[(meta shift -
)]))
260 "Keys for which Org-mode and other modes compete.
261 This is an alist, cars are the default keys, second element specifies
262 the alternative to use when `org-replace-disputed-keys' is t.
264 Keys can be specified in any syntax supported by `define-key'.
265 The value of this option takes effect only at Org-mode's startup,
266 therefore you'll have to restart Emacs to apply it after changing."
271 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
272 Or return the original if not disputed."
273 (if org-replace-disputed-keys
274 (let* ((nkey (key-description key
))
275 (x (org-find-if (lambda (x)
276 (equal (key-description (car x
)) nkey
))
281 (defun org-find-if (predicate seq
)
284 (if (funcall predicate
(car seq
))
285 (throw 'exit
(car seq
))
288 (defun org-defkey (keymap key def
)
289 "Define a key, possibly translated, as returned by `org-key'."
290 (define-key keymap
(org-key key
) def
))
292 (defcustom org-ellipsis nil
293 "The ellipsis to use in the Org-mode outline.
294 When nil, just use the standard three dots. When a string, use that instead,
295 When a face, use the standart 3 dots, but with the specified face.
296 The change affects only Org-mode (which will then use its own display table).
297 Changing this requires executing `M-x org-mode' in a buffer to become
300 :type
'(choice (const :tag
"Default" nil
)
301 (face :tag
"Face" :value org-warning
)
302 (string :tag
"String" :value
"...#")))
304 (defvar org-display-table nil
305 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
307 (defgroup org-keywords nil
308 "Keywords in Org-mode."
312 (defcustom org-deadline-string
"DEADLINE:"
313 "String to mark deadline entries.
314 A deadline is this string, followed by a time stamp. Should be a word,
315 terminated by a colon. You can insert a schedule keyword and
316 a timestamp with \\[org-deadline].
317 Changes become only effective after restarting Emacs."
321 (defcustom org-scheduled-string
"SCHEDULED:"
322 "String to mark scheduled TODO entries.
323 A schedule is this string, followed by a time stamp. Should be a word,
324 terminated by a colon. You can insert a schedule keyword and
325 a timestamp with \\[org-schedule].
326 Changes become only effective after restarting Emacs."
330 (defcustom org-closed-string
"CLOSED:"
331 "String used as the prefix for timestamps logging closing a TODO entry."
335 (defcustom org-clock-string
"CLOCK:"
336 "String used as prefix for timestamps clocking work hours on an item."
340 (defcustom org-comment-string
"COMMENT"
341 "Entries starting with this keyword will never be exported.
342 An entry can be toggled between COMMENT and normal with
343 \\[org-toggle-comment].
344 Changes become only effective after restarting Emacs."
348 (defcustom org-quote-string
"QUOTE"
349 "Entries starting with this keyword will be exported in fixed-width font.
350 Quoting applies only to the text in the entry following the headline, and does
351 not extend beyond the next headline, even if that is lower level.
352 An entry can be toggled between QUOTE and normal with
353 \\[org-toggle-fixed-width-section]."
357 (defconst org-repeat-re
358 ; (concat "\\(?:\\<\\(?:" org-scheduled-string "\\|" org-deadline-string "\\)"
359 ; " +<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\)\\(\\+[0-9]+[dwmy]\\)")
360 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\(\\+[0-9]+[dwmy]\\)"
361 "Regular expression for specifying repeated events.
362 After a match, group 1 contains the repeat expression.")
364 (defgroup org-structure nil
365 "Options concerning the general structure of Org-mode files."
369 (defgroup org-reveal-location nil
370 "Options about how to make context of a location visible."
371 :tag
"Org Reveal Location"
372 :group
'org-structure
)
374 (defconst org-context-choice
376 (const :tag
"Always" t
)
377 (const :tag
"Never" nil
)
378 (repeat :greedy t
:tag
"Individual contexts"
380 (choice :tag
"Context"
387 (const bookmark-jump
)
391 "Contexts for the reveal options.")
393 (defcustom org-show-hierarchy-above
'((default . t
))
394 "Non-nil means, show full hierarchy when revealing a location.
395 Org-mode often shows locations in an org-mode file which might have
396 been invisible before. When this is set, the hierarchy of headings
397 above the exposed location is shown.
398 Turning this off for example for sparse trees makes them very compact.
399 Instead of t, this can also be an alist specifying this option for different
400 contexts. Valid contexts are
401 agenda when exposing an entry from the agenda
402 org-goto when using the command `org-goto' on key C-c C-j
403 occur-tree when using the command `org-occur' on key C-c /
404 tags-tree when constructing a sparse tree based on tags matches
405 link-search when exposing search matches associated with a link
406 mark-goto when exposing the jump goal of a mark
407 bookmark-jump when exposing a bookmark location
408 isearch when exiting from an incremental search
409 default default for all contexts not set explicitly"
410 :group
'org-reveal-location
411 :type org-context-choice
)
413 (defcustom org-show-following-heading
'((default . nil
))
414 "Non-nil means, show following heading when revealing a location.
415 Org-mode often shows locations in an org-mode file which might have
416 been invisible before. When this is set, the heading following the
418 Turning this off for example for sparse trees makes them very compact,
419 but makes it harder to edit the location of the match. In such a case,
420 use the command \\[org-reveal] to show more context.
421 Instead of t, this can also be an alist specifying this option for different
422 contexts. See `org-show-hierarchy-above' for valid contexts."
423 :group
'org-reveal-location
424 :type org-context-choice
)
426 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
427 "Non-nil means, show all sibling heading when revealing a location.
428 Org-mode often shows locations in an org-mode file which might have
429 been invisible before. When this is set, the sibling of the current entry
430 heading are all made visible. If `org-show-hierarchy-above' is t,
431 the same happens on each level of the hierarchy above the current entry.
433 By default this is on for the isearch context, off for all other contexts.
434 Turning this off for example for sparse trees makes them very compact,
435 but makes it harder to edit the location of the match. In such a case,
436 use the command \\[org-reveal] to show more context.
437 Instead of t, this can also be an alist specifying this option for different
438 contexts. See `org-show-hierarchy-above' for valid contexts."
439 :group
'org-reveal-location
440 :type org-context-choice
)
442 (defcustom org-show-entry-below
'((default . nil
))
443 "Non-nil means, show the entry below a headline when revealing a location.
444 Org-mode often shows locations in an org-mode file which might have
445 been invisible before. When this is set, the text below the headline that is
446 exposed is also shown.
448 By default this is off for all contexts.
449 Instead of t, this can also be an alist specifying this option for different
450 contexts. See `org-show-hierarchy-above' for valid contexts."
451 :group
'org-reveal-location
452 :type org-context-choice
)
454 (defgroup org-cycle nil
455 "Options concerning visibility cycling in Org-mode."
457 :group
'org-structure
)
459 (defcustom org-drawers
'("PROPERTIES" "CLOCK")
460 "Names of drawers. Drawers are not opened by cycling on the headline above.
461 Drawers only open with a TAB on the drawer line itself. A drawer looks like
466 The drawer \"PROPERTIES\" is special for capturing properties through
469 Drawers can be defined on the per-file basis with a line like:
471 #+DRAWERS: HIDDEN STATE PROPERTIES"
472 :group
'org-structure
473 :type
'(repeat (string :tag
"Drawer Name")))
475 (defcustom org-cycle-global-at-bob nil
476 "Cycle globally if cursor is at beginning of buffer and not at a headline.
477 This makes it possible to do global cycling without having to use S-TAB or
478 C-u TAB. For this special case to work, the first line of the buffer
479 must not be a headline - it may be empty ot some other text. When used in
480 this way, `org-cycle-hook' is disables temporarily, to make sure the
481 cursor stays at the beginning of the buffer.
482 When this option is nil, don't do anything special at the beginning
487 (defcustom org-cycle-emulate-tab t
488 "Where should `org-cycle' emulate TAB.
490 white Only in completely white lines
491 whitestart Only at the beginning of lines, before the first non-white char
492 t Everywhere except in headlines
493 exc-hl-bol Everywhere except at the start of a headline
494 If TAB is used in a place where it does not emulate TAB, the current subtree
495 visibility is cycled."
497 :type
'(choice (const :tag
"Never" nil
)
498 (const :tag
"Only in completely white lines" white
)
499 (const :tag
"Before first char in a line" whitestart
)
500 (const :tag
"Everywhere except in headlines" t
)
501 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
504 (defcustom org-cycle-separator-lines
2
505 "Number of empty lines needed to keep an empty line between collapsed trees.
506 If you leave an empty line between the end of a subtree and the following
507 headline, this empty line is hidden when the subtree is folded.
508 Org-mode will leave (exactly) one empty line visible if the number of
509 empty lines is equal or larger to the number given in this variable.
510 So the default 2 means, at least 2 empty lines after the end of a subtree
511 are needed to produce free space between a collapsed subtree and the
514 Special case: when 0, never leave empty lines in collapsed view."
518 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
519 org-cycle-hide-drawers
520 org-cycle-show-empty-lines
521 org-optimize-window-after-visibility-change
)
522 "Hook that is run after `org-cycle' has changed the buffer visibility.
523 The function(s) in this hook must accept a single argument which indicates
524 the new state that was set by the most recent `org-cycle' command. The
525 argument is a symbol. After a global state change, it can have the values
526 `overview', `content', or `all'. After a local state change, it can have
527 the values `folded', `children', or `subtree'."
531 (defgroup org-edit-structure nil
532 "Options concerning structure editing in Org-mode."
533 :tag
"Org Edit Structure"
534 :group
'org-structure
)
536 (defcustom org-odd-levels-only nil
537 "Non-nil means, skip even levels and only use odd levels for the outline.
538 This has the effect that two stars are being added/taken away in
539 promotion/demotion commands. It also influences how levels are
540 handled by the exporters.
541 Changing it requires restart of `font-lock-mode' to become effective
542 for fontification also in regions already fontified.
543 You may also set this on a per-file basis by adding one of the following
548 :group
'org-edit-structure
549 :group
'org-font-lock
552 (defcustom org-adapt-indentation t
553 "Non-nil means, adapt indentation when promoting and demoting.
554 When this is set and the *entire* text in an entry is indented, the
555 indentation is increased by one space in a demotion command, and
556 decreased by one in a promotion command. If any line in the entry
557 body starts at column 0, indentation is not changed at all."
558 :group
'org-edit-structure
561 (defcustom org-special-ctrl-a
/e nil
562 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
563 When t, `C-a' will bring back the cursor to the beginning of the
564 headline text, i.e. after the stars and after a possible TODO keyword.
565 In an item, this will be the position after the bullet.
566 When the cursor is already at that position, another `C-a' will bring
567 it to the beginning of the line.
568 `C-e' will jump to the end of the headline, ignoring the presence of tags
569 in the headline. A second `C-e' will then jump to the true end of the
570 line, after any tags.
571 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
572 and only a directly following, identical keypress will bring the cursor
573 to the special positions."
574 :group
'org-edit-structure
576 (const :tag
"off" nil
)
577 (const :tag
"after bullet first" t
)
578 (const :tag
"border first" reversed
)))
580 (if (fboundp 'defvaralias
)
581 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
583 (defcustom org-special-ctrl-k nil
584 "Non-nil means `C-k' will behave specially in headlines.
585 When nil, `C-k' will call the default `kill-line' command.
586 When t, the following will happen while the cursor is in the headline:
588 - When the cursor is at the beginning of a headline, kill the entire
589 line and possible the folded subtree below the line.
590 - When in the middle of the headline text, kill the headline up to the tags.
591 - When after the headline text, kill the tags."
592 :group
'org-edit-structure
595 (defcustom org-M-RET-may-split-line
'((default . t
))
596 "Non-nil means, M-RET will split the line at the cursor position.
597 When nil, it will go to the end of the line before making a
599 You may also set this option in a different way for different
600 contexts. Valid contexts are:
602 headline when creating a new headline
603 item when creating a new item
604 table in a table field
605 default the value to be used for all contexts not explicitly
607 :group
'org-structure
610 (const :tag
"Always" t
)
611 (const :tag
"Never" nil
)
612 (repeat :greedy t
:tag
"Individual contexts"
614 (choice :tag
"Context"
622 (defcustom org-blank-before-new-entry
'((heading . nil
)
623 (plain-list-item . nil
))
624 "Should `org-insert-heading' leave a blank line before new heading/item?
625 The value is an alist, with `heading' and `plain-list-item' as car,
626 and a boolean flag as cdr."
627 :group
'org-edit-structure
629 (cons (const heading
) (boolean))
630 (cons (const plain-list-item
) (boolean))))
632 (defcustom org-insert-heading-hook nil
633 "Hook being run after inserting a new heading."
634 :group
'org-edit-structure
637 (defcustom org-enable-fixed-width-editor t
638 "Non-nil means, lines starting with \":\" are treated as fixed-width.
639 This currently only means, they are never auto-wrapped.
640 When nil, such lines will be treated like ordinary lines.
641 See also the QUOTE keyword."
642 :group
'org-edit-structure
645 (defcustom org-goto-auto-isearch t
646 "Non-nil means, typing characters in org-goto starts incremental search."
647 :group
'org-edit-structure
650 (defgroup org-sparse-trees nil
651 "Options concerning sparse trees in Org-mode."
652 :tag
"Org Sparse Trees"
653 :group
'org-structure
)
655 (defcustom org-highlight-sparse-tree-matches t
656 "Non-nil means, highlight all matches that define a sparse tree.
657 The highlights will automatically disappear the next time the buffer is
658 changed by an edit command."
659 :group
'org-sparse-trees
662 (defcustom org-remove-highlights-with-change t
663 "Non-nil means, any change to the buffer will remove temporary highlights.
664 Such highlights are created by `org-occur' and `org-clock-display'.
665 When nil, `C-c C-c needs to be used to get rid of the highlights.
666 The highlights created by `org-preview-latex-fragment' always need
667 `C-c C-c' to be removed."
668 :group
'org-sparse-trees
673 (defcustom org-occur-hook
'(org-first-headline-recenter)
674 "Hook that is run after `org-occur' has constructed a sparse tree.
675 This can be used to recenter the window to show as much of the structure
677 :group
'org-sparse-trees
680 (defgroup org-plain-lists nil
681 "Options concerning plain lists in Org-mode."
682 :tag
"Org Plain lists"
683 :group
'org-structure
)
685 (defcustom org-cycle-include-plain-lists nil
686 "Non-nil means, include plain lists into visibility cycling.
687 This means that during cycling, plain list items will *temporarily* be
688 interpreted as outline headlines with a level given by 1000+i where i is the
689 indentation of the bullet. In all other operations, plain list items are
690 not seen as headlines. For example, you cannot assign a TODO keyword to
692 :group
'org-plain-lists
695 (defcustom org-plain-list-ordered-item-terminator t
696 "The character that makes a line with leading number an ordered list item.
697 Valid values are ?. and ?\). To get both terminators, use t. While
698 ?. may look nicer, it creates the danger that a line with leading
699 number may be incorrectly interpreted as an item. ?\) therefore is
701 :group
'org-plain-lists
702 :type
'(choice (const :tag
"dot like in \"2.\"" ?.
)
703 (const :tag
"paren like in \"2)\"" ?\
))
704 (const :tab
"both" t
)))
706 (defcustom org-auto-renumber-ordered-lists t
707 "Non-nil means, automatically renumber ordered plain lists.
708 Renumbering happens when the sequence have been changed with
709 \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
710 use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
711 :group
'org-plain-lists
714 (defcustom org-provide-checkbox-statistics t
715 "Non-nil means, update checkbox statistics after insert and toggle.
716 When this is set, checkbox statistics is updated each time you either insert
717 a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
718 with \\[org-ctrl-c-ctrl-c\\]."
719 :group
'org-plain-lists
722 (defgroup org-archive nil
723 "Options concerning archiving in Org-mode."
725 :group
'org-structure
)
727 (defcustom org-archive-tag
"ARCHIVE"
728 "The tag that marks a subtree as archived.
729 An archived subtree does not open during visibility cycling, and does
730 not contribute to the agenda listings.
731 After changing this, font-lock must be restarted in the relevant buffers to
732 get the proper fontification."
737 (defcustom org-agenda-skip-archived-trees t
738 "Non-nil means, the agenda will skip any items located in archived trees.
739 An archived tree is a tree marked with the tag ARCHIVE."
741 :group
'org-agenda-skip
744 (defcustom org-cycle-open-archived-trees nil
745 "Non-nil means, `org-cycle' will open archived trees.
746 An archived tree is a tree marked with the tag ARCHIVE.
747 When nil, archived trees will stay folded. You can still open them with
748 normal outline commands like `show-all', but not with the cycling commands."
753 (defcustom org-sparse-tree-open-archived-trees nil
754 "Non-nil means sparse tree construction shows matches in archived trees.
755 When nil, matches in these trees are highlighted, but the trees are kept in
758 :group
'org-sparse-trees
761 (defcustom org-archive-location
"%s_archive::"
762 "The location where subtrees should be archived.
763 This string consists of two parts, separated by a double-colon.
765 The first part is a file name - when omitted, archiving happens in the same
766 file. %s will be replaced by the current file name (without directory part).
767 Archiving to a different file is useful to keep archived entries from
768 contributing to the Org-mode Agenda.
770 The part after the double colon is a headline. The archived entries will be
771 filed under that headline. When omitted, the subtrees are simply filed away
772 at the end of the file, as top-level entries.
774 Here are a few examples:
776 If the current file is Projects.org, archive in file
777 Projects.org_archive, as top-level trees. This is the default.
779 \"::* Archived Tasks\"
780 Archive in the current file, under the top-level headline
781 \"* Archived Tasks\".
783 \"~/org/archive.org::\"
784 Archive in file ~/org/archive.org (absolute path), as top-level trees.
786 \"basement::** Finished Tasks\"
787 Archive in file ./basement (relative path), as level 3 trees
788 below the level 2 heading \"** Finished Tasks\".
790 You may set this option on a per-file basis by adding to the buffer a
793 #+ARCHIVE: basement::** Finished Tasks"
797 (defcustom org-archive-mark-done t
798 "Non-nil means, mark entries as DONE when they are moved to the archive file.
799 This can be a string to set the keyword to use. When t, Org-mode will
800 use the first keyword in its list that means done."
803 (const :tag
"No" nil
)
805 (string :tag
"Use this keyword")))
807 (defcustom org-archive-stamp-time t
808 "Non-nil means, add a time stamp to entries moved to an archive file.
809 This variable is obsolete and has no effect anymore, instead add ot remove
810 `time' from the variablle `org-archive-save-context-info'."
814 (defcustom org-archive-save-context-info
'(time file olpath category todo itags
)
815 "Parts of context info that should be stored as properties when archiving.
816 When a subtree is moved to an archive file, it looses information given by
817 context, like inherited tags, the category, and possibly also the TODO
818 state (depending on the variable `org-archive-mark-done').
819 This variable can be a list of any of the following symbols:
821 time The time of archiving.
822 file The file where the entry originates.
823 itags The local tags, in the headline of the subtree.
824 ltags The tags the subtree inherits from further up the hierarchy.
825 todo The pre-archive TODO state.
826 category The category, taken from file name or #+CATEGORY lines.
827 olpath The outline path to the item. These are all headlines above
828 the current item, separated by /, like a file path.
830 For each symbol present in the list, a property will be created in
831 the archived entry, with a prefix \"PRE_ARCHIVE_\", to remember this
834 :type
'(set :greedy t
835 (const :tag
"Time" time
)
836 (const :tag
"File" file
)
837 (const :tag
"Category" category
)
838 (const :tag
"TODO state" todo
)
839 (const :tag
"TODO state" priority
)
840 (const :tag
"Inherited tags" itags
)
841 (const :tag
"Outline path" olpath
)
842 (const :tag
"Local tags" ltags
)))
844 (defgroup org-imenu-and-speedbar nil
845 "Options concerning imenu and speedbar in Org-mode."
846 :tag
"Org Imenu and Speedbar"
847 :group
'org-structure
)
849 (defcustom org-imenu-depth
2
850 "The maximum level for Imenu access to Org-mode headlines.
851 This also applied for speedbar access."
852 :group
'org-imenu-and-speedbar
855 (defgroup org-table nil
856 "Options concerning tables in Org-mode."
860 (defcustom org-enable-table-editor
'optimized
861 "Non-nil means, lines starting with \"|\" are handled by the table editor.
862 When nil, such lines will be treated like ordinary lines.
864 When equal to the symbol `optimized', the table editor will be optimized to
866 - Automatic overwrite mode in front of whitespace in table fields.
867 This makes the structure of the table stay in tact as long as the edited
868 field does not exceed the column width.
869 - Minimize the number of realigns. Normally, the table is aligned each time
870 TAB or RET are pressed to move to another field. With optimization this
871 happens only if changes to a field might have changed the column width.
872 Optimization requires replacing the functions `self-insert-command',
873 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
874 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
875 very good at guessing when a re-align will be necessary, but you can always
876 force one with \\[org-ctrl-c-ctrl-c].
878 If you would like to use the optimized version in Org-mode, but the
879 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
881 This variable can be used to turn on and off the table editor during a session,
882 but in order to toggle optimization, a restart is required.
884 See also the variable `org-table-auto-blank-field'."
887 (const :tag
"off" nil
)
889 (const :tag
"on, optimized" optimized
)))
891 (defcustom orgtbl-optimized
(eq org-enable-table-editor
'optimized
)
892 "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
893 In the optimized version, the table editor takes over all simple keys that
894 normally just insert a character. In tables, the characters are inserted
895 in a way to minimize disturbing the table structure (i.e. in overwrite mode
896 for empty fields). Outside tables, the correct binding of the keys is
899 The default for this option is t if the optimized version is also used in
900 Org-mode. See the variable `org-enable-table-editor' for details. Changing
901 this variable requires a restart of Emacs to become effective."
905 (defcustom orgtbl-radio-table-templates
906 '((latex-mode "% BEGIN RECEIVE ORGTBL %n
907 % END RECEIVE ORGTBL %n
909 #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
912 (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
913 @c END RECEIVE ORGTBL %n
915 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
918 (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
919 <!-- END RECEIVE ORGTBL %n -->
921 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
924 "Templates for radio tables in different major modes.
925 All occurrences of %n in a template will be replaced with the name of the
926 table, obtained by prompting the user."
929 (list (symbol :tag
"Major mode")
930 (string :tag
"Format"))))
932 (defgroup org-table-settings nil
933 "Settings for tables in Org-mode."
934 :tag
"Org Table Settings"
937 (defcustom org-table-default-size
"5x2"
938 "The default size for newly created tables, Columns x Rows."
939 :group
'org-table-settings
942 (defcustom org-table-number-regexp
943 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$"
944 "Regular expression for recognizing numbers in table columns.
945 If a table column contains mostly numbers, it will be aligned to the
946 right. If not, it will be aligned to the left.
948 The default value of this option is a regular expression which allows
949 anything which looks remotely like a number as used in scientific
950 context. For example, all of the following will be considered a
952 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
954 Other options offered by the customize interface are more restrictive."
955 :group
'org-table-settings
957 (const :tag
"Positive Integers"
959 (const :tag
"Integers"
961 (const :tag
"Floating Point Numbers"
962 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
963 (const :tag
"Floating Point Number or Integer"
964 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
965 (const :tag
"Exponential, Floating point, Integer"
966 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
967 (const :tag
"Very General Number-Like, including hex"
968 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|\\(0[xX]\\)[0-9a-fA-F]+\\|nan\\)$")
969 (string :tag
"Regexp:")))
971 (defcustom org-table-number-fraction
0.5
972 "Fraction of numbers in a column required to make the column align right.
973 In a column all non-white fields are considered. If at least this
974 fraction of fields is matched by `org-table-number-fraction',
975 alignment to the right border applies."
976 :group
'org-table-settings
979 (defgroup org-table-editing nil
980 "Behavior of tables during editing in Org-mode."
981 :tag
"Org Table Editing"
984 (defcustom org-table-automatic-realign t
985 "Non-nil means, automatically re-align table when pressing TAB or RETURN.
986 When nil, aligning is only done with \\[org-table-align], or after column
988 :group
'org-table-editing
991 (defcustom org-table-auto-blank-field t
992 "Non-nil means, automatically blank table field when starting to type into it.
993 This only happens when typing immediately after a field motion
994 command (TAB, S-TAB or RET).
995 Only relevant when `org-enable-table-editor' is equal to `optimized'."
996 :group
'org-table-editing
999 (defcustom org-table-tab-jumps-over-hlines t
1000 "Non-nil means, tab in the last column of a table with jump over a hline.
1001 If a horizontal separator line is following the current line,
1002 `org-table-next-field' can either create a new row before that line, or jump
1003 over the line. When this option is nil, a new line will be created before
1005 :group
'org-table-editing
1008 (defcustom org-table-tab-recognizes-table.el t
1009 "Non-nil means, TAB will automatically notice a table.el table.
1010 When it sees such a table, it moves point into it and - if necessary -
1011 calls `table-recognize-table'."
1012 :group
'org-table-editing
1015 (defgroup org-table-calculation nil
1016 "Options concerning tables in Org-mode."
1017 :tag
"Org Table Calculation"
1020 (defcustom org-table-use-standard-references t
1021 "Should org-mode work with table refrences like B3 instead of @3$2?
1022 Possible values are:
1024 from accept as input, do not present for editing
1025 t: accept as input and present for editing"
1026 :group
'org-table-calculation
1028 (const :tag
"Never, don't even check unser input for them" nil
)
1029 (const :tag
"Always, both as user input, and when editing" t
)
1030 (const :tag
"Convert user input, don't offer during editing" 'from
)))
1032 (defcustom org-table-copy-increment t
1033 "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
1034 :group
'org-table-calculation
1037 (defcustom org-calc-default-modes
1038 '(calc-internal-prec 12
1039 calc-float-format
(float 5)
1041 calc-prefer-frac nil
1042 calc-symbolic-mode nil
1043 calc-date-format
(YYYY "-" MM
"-" DD
" " Www
(" " HH
":" mm
))
1044 calc-display-working-message t
1046 "List with Calc mode settings for use in calc-eval for table formulas.
1047 The list must contain alternating symbols (Calc modes variables and values).
1048 Don't remove any of the default settings, just change the values. Org-mode
1049 relies on the variables to be present in the list."
1050 :group
'org-table-calculation
1053 (defcustom org-table-formula-evaluate-inline t
1054 "Non-nil means, TAB and RET evaluate a formula in current table field.
1055 If the current field starts with an equal sign, it is assumed to be a formula
1056 which should be evaluated as described in the manual and in the documentation
1057 string of the command `org-table-eval-formula'. This feature requires the
1059 When this variable is nil, formula calculation is only available through
1060 the command \\[org-table-eval-formula]."
1061 :group
'org-table-calculation
1064 (defcustom org-table-formula-use-constants t
1065 "Non-nil means, interpret constants in formulas in tables.
1066 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
1067 by the value given in `org-table-formula-constants', or by a value obtained
1068 from the `constants.el' package."
1069 :group
'org-table-calculation
1072 (defcustom org-table-formula-constants nil
1073 "Alist with constant names and values, for use in table formulas.
1074 The car of each element is a name of a constant, without the `$' before it.
1075 The cdr is the value as a string. For example, if you'd like to use the
1076 speed of light in a formula, you would configure
1078 (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
1080 and then use it in an equation like `$1*$c'.
1082 Constants can also be defined on a per-file basis using a line like
1084 #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
1085 :group
'org-table-calculation
1087 (cons (string :tag
"name")
1088 (string :tag
"value"))))
1090 (defvar org-table-formula-constants-local nil
1091 "Local version of `org-table-formula-constants'.")
1092 (make-variable-buffer-local 'org-table-formula-constants-local
)
1094 (defcustom org-table-allow-automatic-line-recalculation t
1095 "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
1096 Automatically means, when TAB or RET or C-c C-c are pressed in the line."
1097 :group
'org-table-calculation
1100 (defgroup org-link nil
1101 "Options concerning links in Org-mode."
1105 (defvar org-link-abbrev-alist-local nil
1106 "Buffer-local version of `org-link-abbrev-alist', which see.
1107 The value of this is taken from the #+LINK lines.")
1108 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1110 (defcustom org-link-abbrev-alist nil
1111 "Alist of link abbreviations.
1112 The car of each element is a string, to be replaced at the start of a link.
1113 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1114 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1116 [[linkkey:tag][description]]
1118 If REPLACE is a string, the tag will simply be appended to create the link.
1119 If the string contains \"%s\", the tag will be inserted there.
1121 REPLACE may also be a function that will be called with the tag as the
1122 only argument to create the link, which should be returned as a string.
1124 See the manual for examples."
1128 (defcustom org-descriptive-links t
1129 "Non-nil means, hide link part and only show description of bracket links.
1130 Bracket links are like [[link][descritpion]]. This variable sets the initial
1131 state in new org-mode buffers. The setting can then be toggled on a
1132 per-buffer basis from the Org->Hyperlinks menu."
1136 (defcustom org-link-file-path-type
'adaptive
1137 "How the path name in file links should be stored.
1140 relative Relative to the current directory, i.e. the directory of the file
1141 into which the link is being inserted.
1142 absolute Absolute path, if possible with ~ for home directory.
1143 noabbrev Absolute path, no abbreviation of home directory.
1144 adaptive Use relative path for files in the current directory and sub-
1145 directories of it. For other files, use an absolute path."
1153 (defcustom org-activate-links
'(bracket angle plain radio tag date
)
1154 "Types of links that should be activated in Org-mode files.
1155 This is a list of symbols, each leading to the activation of a certain link
1156 type. In principle, it does not hurt to turn on most link types - there may
1157 be a small gain when turning off unused link types. The types are:
1159 bracket The recommended [[link][description]] or [[link]] links with hiding.
1160 angular Links in angular brackes that may contain whitespace like
1161 <bbdb:Carsten Dominik>.
1162 plain Plain links in normal text, no whitespace, like http://google.com.
1163 radio Text that is matched by a radio target, see manual for details.
1164 tag Tag settings in a headline (link to tag search).
1165 date Time stamps (link to calendar).
1167 Changing this variable requires a restart of Emacs to become effective."
1169 :type
'(set (const :tag
"Double bracket links (new style)" bracket
)
1170 (const :tag
"Angular bracket links (old style)" angular
)
1171 (const :tag
"Plain text links" plain
)
1172 (const :tag
"Radio target matches" radio
)
1173 (const :tag
"Tags" tag
)
1174 (const :tag
"Timestamps" date
)))
1176 (defgroup org-link-store nil
1177 "Options concerning storing links in Org-mode"
1178 :tag
"Org Store Link"
1181 (defcustom org-email-link-description-format
"Email %c: %.30s"
1182 "Format of the description part of a link to an email or usenet message.
1183 The following %-excapes will be replaced by corresponding information:
1185 %F full \"From\" field
1186 %f name, taken from \"From\" field, address if no name
1187 %T full \"To\" field
1188 %t first name in \"To\" field, address if no name
1189 %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
1190 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1194 You may use normal field width specification between the % and the letter.
1195 This is for example useful to limit the length of the subject.
1197 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1198 :group
'org-link-store
1201 (defcustom org-from-is-user-regexp
1203 (when (and user-mail-address
(not (string= user-mail-address
"")))
1204 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1205 (when (and user-full-name
(not (string= user-full-name
"")))
1206 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1207 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1208 "Regexp mached against the \"From:\" header of an email or usenet message.
1209 It should match if the message is from the user him/herself."
1210 :group
'org-link-store
1213 (defcustom org-context-in-file-links t
1214 "Non-nil means, file links from `org-store-link' contain context.
1215 A search string will be added to the file name with :: as separator and
1216 used to find the context when the link is activated by the command
1217 `org-open-at-point'.
1218 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1219 negates this setting for the duration of the command."
1220 :group
'org-link-store
1223 (defcustom org-keep-stored-link-after-insertion nil
1224 "Non-nil means, keep link in list for entire session.
1226 The command `org-store-link' adds a link pointing to the current
1227 location to an internal list. These links accumulate during a session.
1228 The command `org-insert-link' can be used to insert links into any
1229 Org-mode file (offering completion for all stored links). When this
1230 option is nil, every link which has been inserted once using \\[org-insert-link]
1231 will be removed from the list, to make completing the unused links
1233 :group
'org-link-store
1236 (defcustom org-usenet-links-prefer-google nil
1237 "Non-nil means, `org-store-link' will create web links to Google groups.
1238 When nil, Gnus will be used for such links.
1239 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1240 negates this setting for the duration of the command."
1241 :group
'org-link-store
1244 (defgroup org-link-follow nil
1245 "Options concerning following links in Org-mode"
1246 :tag
"Org Follow Link"
1249 (defcustom org-follow-link-hook nil
1250 "Hook that is run after a link has been followed."
1251 :group
'org-link-follow
1254 (defcustom org-tab-follows-link nil
1255 "Non-nil means, on links TAB will follow the link.
1256 Needs to be set before org.el is loaded."
1257 :group
'org-link-follow
1260 (defcustom org-return-follows-link nil
1261 "Non-nil means, on links RET will follow the link.
1262 Needs to be set before org.el is loaded."
1263 :group
'org-link-follow
1266 (defcustom org-mouse-1-follows-link
1267 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1268 "Non-nil means, mouse-1 on a link will follow the link.
1269 A longer mouse click will still set point. Does not work on XEmacs.
1270 Needs to be set before org.el is loaded."
1271 :group
'org-link-follow
1274 (defcustom org-mark-ring-length
4
1275 "Number of different positions to be recorded in the ring
1276 Changing this requires a restart of Emacs to work correctly."
1277 :group
'org-link-follow
1280 (defcustom org-link-frame-setup
1281 '((vm . vm-visit-folder-other-frame
)
1282 (gnus . gnus-other-frame
)
1283 (file . find-file-other-window
))
1284 "Setup the frame configuration for following links.
1285 When following a link with Emacs, it may often be useful to display
1286 this link in another window or frame. This variable can be used to
1287 set this up for the different types of links.
1290 `vm-visit-folder-other-frame'
1291 For Gnus, use any of
1294 For FILE, use any of
1296 `find-file-other-window'
1297 `find-file-other-frame'
1298 For the calendar, use the variable `calendar-setup'.
1299 For BBDB, it is currently only possible to display the matches in
1301 :group
'org-link-follow
1305 (const vm-visit-folder
)
1306 (const vm-visit-folder-other-window
)
1307 (const vm-visit-folder-other-frame
)))
1311 (const gnus-other-frame
)))
1315 (const find-file-other-window
)
1316 (const find-file-other-frame
)))))
1318 (defcustom org-display-internal-link-with-indirect-buffer nil
1319 "Non-nil means, use indirect buffer to display infile links.
1320 Activating internal links (from one location in a file to another location
1321 in the same file) normally just jumps to the location. When the link is
1322 activated with a C-u prefix (or with mouse-3), the link is displayed in
1323 another window. When this option is set, the other window actually displays
1324 an indirect buffer clone of the current buffer, to avoid any visibility
1325 changes to the current buffer."
1326 :group
'org-link-follow
1329 (defcustom org-open-non-existing-files nil
1330 "Non-nil means, `org-open-file' will open non-existing files.
1331 When nil, an error will be generated."
1332 :group
'org-link-follow
1335 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1336 "Function and arguments to call for following mailto links.
1337 This is a list with the first element being a lisp function, and the
1338 remaining elements being arguments to the function. In string arguments,
1339 %a will be replaced by the address, and %s will be replaced by the subject
1340 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1341 :group
'org-link-follow
1343 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1344 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1345 (const :tag
"message-mail" (message-mail "%a" "%s"))
1346 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1348 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1349 "Non-nil means, ask for confirmation before executing shell links.
1350 Shell links can be dangerous: just think about a link
1352 [[shell:rm -rf ~/*][Google Search]]
1354 This link would show up in your Org-mode document as \"Google Search\",
1355 but really it would remove your entire home directory.
1356 Therefore we advise against setting this variable to nil.
1357 Just change it to `y-or-n-p' of you want to confirm with a
1358 single keystroke rather than having to type \"yes\"."
1359 :group
'org-link-follow
1361 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1362 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1363 (const :tag
"no confirmation (dangerous)" nil
)))
1365 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1366 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1367 Elisp links can be dangerous: just think about a link
1369 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1371 This link would show up in your Org-mode document as \"Google Search\",
1372 but really it would remove your entire home directory.
1373 Therefore we advise against setting this variable to nil.
1374 Just change it to `y-or-n-p' of you want to confirm with a
1375 single keystroke rather than having to type \"yes\"."
1376 :group
'org-link-follow
1378 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1379 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1380 (const :tag
"no confirmation (dangerous)" nil
)))
1382 (defconst org-file-apps-defaults-gnu
1385 "Default file applications on a UNIX or GNU/Linux system.
1386 See `org-file-apps'.")
1388 (defconst org-file-apps-defaults-macosx
1394 ("eps.gz" .
"gv %s")
1396 ("fig" .
"xfig %s"))
1397 "Default file applications on a MacOS X system.
1398 The system \"open\" is known as a default, but we use X11 applications
1399 for some files for which the OS does not have a good default.
1400 See `org-file-apps'.")
1402 (defconst org-file-apps-defaults-windowsnt
1406 (list (if (featurep 'xemacs
)
1407 'mswindows-shell-execute
1410 "Default file applications on a Windows NT system.
1411 The system \"open\" is used for most files.
1412 See `org-file-apps'.")
1414 (defcustom org-file-apps
1423 "External applications for opening `file:path' items in a document.
1424 Org-mode uses system defaults for different file types, but
1425 you can use this variable to set the application for a given file
1426 extension. The entries in this list are cons cells where the car identifies
1427 files and the cdr the corresponding command. Possible values for the
1429 \"ext\" A string identifying an extension
1430 `directory' Matches a directory
1431 `remote' Matches a remote file, accessible through tramp or efs.
1432 Remote files most likely should be visited through Emacs
1433 because external applications cannot handle such paths.
1434 t Default for all remaining files
1436 Possible values for the command are:
1437 `emacs' The file will be visited by the current Emacs process.
1438 `default' Use the default application for this file type.
1439 string A command to be executed by a shell; %s will be replaced
1440 by the path to the file.
1441 sexp A Lisp form which will be evaluated. The file path will
1442 be available in the Lisp variable `file'.
1443 For more examples, see the system specific constants
1444 `org-file-apps-defaults-macosx'
1445 `org-file-apps-defaults-windowsnt'
1446 `org-file-apps-defaults-gnu'."
1447 :group
'org-link-follow
1449 (cons (choice :value
""
1450 (string :tag
"Extension")
1451 (const :tag
"Default for unrecognized files" t
)
1452 (const :tag
"Remote file" remote
)
1453 (const :tag
"Links to a directory" directory
))
1455 (const :tag
"Visit with Emacs" emacs
)
1456 (const :tag
"Use system default" default
)
1457 (string :tag
"Command")
1458 (sexp :tag
"Lisp form")))))
1460 (defcustom org-mhe-search-all-folders nil
1461 "Non-nil means, that the search for the mh-message will be extended to
1462 all folders if the message cannot be found in the folder given in the link.
1463 Searching all folders is very efficient with one of the search engines
1464 supported by MH-E, but will be slow with pick."
1465 :group
'org-link-follow
1468 (defgroup org-remember nil
1469 "Options concerning interaction with remember.el."
1473 (defcustom org-directory
"~/org"
1474 "Directory with org files.
1475 This directory will be used as default to prompt for org files.
1476 Used by the hooks for remember.el."
1477 :group
'org-remember
1480 (defcustom org-default-notes-file
"~/.notes"
1481 "Default target for storing notes.
1482 Used by the hooks for remember.el. This can be a string, or nil to mean
1483 the value of `remember-data-file'.
1484 You can set this on a per-template basis with the variable
1485 `org-remember-templates'."
1486 :group
'org-remember
1488 (const :tag
"Default from remember-data-file" nil
)
1491 (defcustom org-remember-store-without-prompt t
1492 "Non-nil means, `C-c C-c' stores remember note without further promts.
1493 In this case, you need `C-u C-c C-c' to get the prompts for
1494 note file and headline.
1495 When this variable is nil, `C-c C-c' give you the prompts, and
1496 `C-u C-c C-c' trigger the fasttrack."
1497 :group
'org-remember
1500 (defcustom org-remember-interactive-interface
'refile
1501 "The interface to be used for interactive filing of remember notes.
1502 This is only used when the interactive mode for selecting a filing
1503 location is used (see the variable `org-remember-store-without-prompt').
1505 outline The interface shows an outline of the relevant file
1506 and the correct heading is found by moving through
1507 the outline or by searching with incremental search.
1508 outline-path-completion Headlines in the current buffer are offered via
1510 refile Use the refile interface, and offer headlines,
1511 possibly from different buffers."
1512 :group
'org-remember
1514 (const :tag
"Refile" refile
)
1515 (const :tag
"Outline" outline
)
1516 (const :tag
"Outline-path-completion" outline-path-completion
)))
1518 (defcustom org-goto-interface
'outline
1519 "The default interface to be used for `org-goto'.
1521 outline The interface shows an outline of the relevant file
1522 and the correct heading is found by moving through
1523 the outline or by searching with incremental search.
1524 outline-path-completion Headlines in the current buffer are offered via
1526 :group
'org-remember
; FIXME: different group for org-goto and org-refile
1528 (const :tag
"Outline" outline
)
1529 (const :tag
"Outline-path-completion" outline-path-completion
)))
1531 (defcustom org-remember-default-headline
""
1532 "The headline that should be the default location in the notes file.
1533 When filing remember notes, the cursor will start at that position.
1534 You can set this on a per-template basis with the variable
1535 `org-remember-templates'."
1536 :group
'org-remember
1539 (defcustom org-remember-templates nil
1540 "Templates for the creation of remember buffers.
1541 When nil, just let remember make the buffer.
1542 When not nil, this is a list of 5-element lists. In each entry, the first
1543 element is the name of the template, which should be a single short word.
1544 The second element is a character, a unique key to select this template.
1545 The third element is the template. The fourth element is optional and can
1546 specify a destination file for remember items created with this template.
1547 The default file is given by `org-default-notes-file'. An optional fifth
1548 element can specify the headline in that file that should be offered
1549 first when the user is asked to file the entry. The default headline is
1550 given in the variable `org-remember-default-headline'.
1552 An optional sixth element can specify the context in which the user should
1553 be able to select this template. If this element is a list of major modes,
1554 the template will only be available while invoking `org-remember' from a
1555 buffer in one of these modes. If it is a function, the template will only
1556 be selected if the function returns `t'. A value of `t' means select
1557 this template in any context. When the element is `nil', the template
1558 will be selected by default, i.e. when all contextual checks failed.
1560 The template specifies the structure of the remember buffer. It should have
1561 a first line starting with a star, to act as the org-mode headline.
1562 Furthermore, the following %-escapes will be replaced with content:
1564 %^{prompt} Prompt the user for a string and replace this sequence with it.
1565 A default value and a completion table ca be specified like this:
1566 %^{prompt|default|completion2|completion3|...}
1567 %t time stamp, date only
1568 %T time stamp with date and time
1569 %u, %U like the above, but inactive time stamps
1570 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U
1571 You may define a prompt like %^{Please specify birthday}t
1572 %n user name (taken from `user-full-name')
1573 %a annotation, normally the link created with org-store-link
1574 %i initial content, the region active. If %i is indented,
1575 the entire inserted text will be indented as well.
1576 %c content of the clipboard, or current kill ring head
1577 %^g prompt for tags, with completion on tags in target file
1578 %^G prompt for tags, with completion all tags in all agenda files
1579 %:keyword specific information for certain link types, see below
1580 %[pathname] insert the contents of the file given by `pathname'
1581 %(sexp) evaluate elisp `(sexp)' and replace with the result
1582 %! Store this note immediately after filling the template
1584 %? After completing the template, position cursor here.
1586 Apart from these general escapes, you can access information specific to the
1587 link type that is created. For example, calling `remember' in emails or gnus
1588 will record the author and the subject of the message, which you can access
1589 with %:author and %:subject, respectively. Here is a complete list of what
1590 is recorded for each link type.
1592 Link type | Available information
1593 -------------------+------------------------------------------------------
1594 bbdb | %:type %:name %:company
1595 vm, wl, mh, rmail | %:type %:subject %:message-id
1596 | %:from %:fromname %:fromaddress
1597 | %:to %:toname %:toaddress
1598 | %:fromto (either \"to NAME\" or \"from NAME\")
1599 gnus | %:group, for messages also all email fields
1600 w3, w3m | %:type %:url
1601 info | %:type %:file %:node
1602 calendar | %:type %:date"
1603 :group
'org-remember
1604 :get
(lambda (var) ; Make sure all entries have at least 5 elements
1606 (if (not (stringp (car x
))) (setq x
(cons "" x
)))
1607 (cond ((= (length x
) 4) (append x
'("")))
1608 ((= (length x
) 3) (append x
'("" "")))
1610 (default-value var
)))
1613 (list :value
("" ?a
"\n" nil nil nil
)
1614 (string :tag
"Name")
1615 (character :tag
"Selection Key")
1616 (string :tag
"Template")
1618 (file :tag
"Destination file")
1619 (const :tag
"Prompt for file" nil
))
1621 (string :tag
"Destination headline")
1622 (const :tag
"Selection interface for heading"))
1624 (const :tag
"Use by default" nil
)
1625 (const :tag
"Use in all contexts" t
)
1626 (repeat :tag
"Use only if in major mode"
1627 (symbol :tag
"Major mode"))
1628 (function :tag
"Perform a check against function")))))
1630 (defcustom org-reverse-note-order nil
1631 "Non-nil means, store new notes at the beginning of a file or entry.
1632 When nil, new notes will be filed to the end of a file or entry.
1633 This can also be a list with cons cells of regular expressions that
1634 are matched against file names, and values."
1635 :group
'org-remember
1637 (const :tag
"Reverse always" t
)
1638 (const :tag
"Reverse never" nil
)
1639 (repeat :tag
"By file name regexp"
1640 (cons regexp boolean
))))
1642 (defcustom org-refile-targets nil
1643 "Targets for refiling entries with \\[org-refile].
1644 This is list of cons cells. Each cell contains:
1645 - a specification of the files to be considered, either a list of files,
1646 or a symbol whose function or value fields will be used to retrieve
1647 a file name or a list of file names. Nil means, refile to a different
1648 heading in the current buffer.
1649 - A specification of how to find candidate refile targets. This may be
1651 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1652 This tag has to be present in all target headlines, inheritance will
1654 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1656 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1657 headlines that are refiling targets.
1658 - a cons cell (:level . N). Any headline of level N is considered a target.
1659 - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
1660 ;; FIXME: what if there are a var and func with same name???
1661 :group
'org-remember
1664 (choice :value org-agenda-files
1665 (const :tag
"All agenda files" org-agenda-files
)
1666 (const :tag
"Current buffer" nil
)
1667 (function) (variable) (file))
1668 (choice :tag
"Identify target headline by"
1669 (cons :tag
"Specific tag" (const :tag
) (string))
1670 (cons :tag
"TODO keyword" (const :todo
) (string))
1671 (cons :tag
"Regular expression" (const :regexp
) (regexp))
1672 (cons :tag
"Level number" (const :level
) (integer))
1673 (cons :tag
"Max Level number" (const :maxlevel
) (integer))))))
1675 (defcustom org-refile-use-outline-path nil
1676 "Non-nil means, provide refile targets as paths.
1677 So a level 3 headline will be available as level1/level2/level3.
1678 When the value is `file', also include the file name (without directory)
1679 into the path. When `full-file-path', include the full file path."
1680 :group
'org-remember
1682 (const :tag
"Not" nil
)
1683 (const :tag
"Yes" t
)
1684 (const :tag
"Start with file name" file
)
1685 (const :tag
"Start with full file path" full-file-path
)))
1687 (defgroup org-todo nil
1688 "Options concerning TODO items in Org-mode."
1692 (defgroup org-progress nil
1693 "Options concerning Progress logging in Org-mode."
1697 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1698 "List of TODO entry keyword sequences and their interpretation.
1699 \\<org-mode-map>This is a list of sequences.
1701 Each sequence starts with a symbol, either `sequence' or `type',
1702 indicating if the keywords should be interpreted as a sequence of
1703 action steps, or as different types of TODO items. The first
1704 keywords are states requiring action - these states will select a headline
1705 for inclusion into the global TODO list Org-mode produces. If one of
1706 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1707 signify that no further action is necessary. If \"|\" is not found,
1708 the last keyword is treated as the only DONE state of the sequence.
1710 The command \\[org-todo] cycles an entry through these states, and one
1711 additional state where no keyword is present. For details about this
1712 cycling, see the manual.
1714 TODO keywords and interpretation can also be set on a per-file basis with
1715 the special #+SEQ_TODO and #+TYP_TODO lines.
1717 Each keyword can optionally specify a character for fast state selection
1718 \(in combination with the variable `org-use-fast-todo-selection')
1719 and specifiers for state change logging, using the same syntax
1720 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1721 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1722 indicates to record a time stamp each time this state is selected.
1724 Each keyword may also specify if a timestamp or a note should be
1725 recorded when entering or leaving the state, by adding additional
1726 characters in the parenthesis after the keyword. This looks like this:
1727 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1728 record only the time of the state change. With X and Y being either
1729 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1730 Y when leaving the state if and only if the *target* state does not
1731 define X. You may omit any of the fast-selection key or X or /Y,
1732 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1734 For backward compatibility, this variable may also be just a list
1735 of keywords - in this case the interptetation (sequence or type) will be
1736 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1738 :group
'org-keywords
1740 (repeat :tag
"Old syntax, just keywords"
1741 (string :tag
"Keyword"))
1742 (repeat :tag
"New syntax"
1745 :tag
"Interpretation"
1746 (const :tag
"Sequence (cycling hits every state)" sequence
)
1747 (const :tag
"Type (cycling directly to DONE)" type
))
1749 (string :tag
"Keyword"))))))
1751 (defvar org-todo-keywords-1 nil
1752 "All TODO and DONE keywords active in a buffer.")
1753 (make-variable-buffer-local 'org-todo-keywords-1
)
1754 (defvar org-todo-keywords-for-agenda nil
)
1755 (defvar org-done-keywords-for-agenda nil
)
1756 (defvar org-not-done-keywords nil
)
1757 (make-variable-buffer-local 'org-not-done-keywords
)
1758 (defvar org-done-keywords nil
)
1759 (make-variable-buffer-local 'org-done-keywords
)
1760 (defvar org-todo-heads nil
)
1761 (make-variable-buffer-local 'org-todo-heads
)
1762 (defvar org-todo-sets nil
)
1763 (make-variable-buffer-local 'org-todo-sets
)
1764 (defvar org-todo-log-states nil
)
1765 (make-variable-buffer-local 'org-todo-log-states
)
1766 (defvar org-todo-kwd-alist nil
)
1767 (make-variable-buffer-local 'org-todo-kwd-alist
)
1768 (defvar org-todo-key-alist nil
)
1769 (make-variable-buffer-local 'org-todo-key-alist
)
1770 (defvar org-todo-key-trigger nil
)
1771 (make-variable-buffer-local 'org-todo-key-trigger
)
1773 (defcustom org-todo-interpretation
'sequence
1774 "Controls how TODO keywords are interpreted.
1775 This variable is in principle obsolete and is only used for
1776 backward compatibility, if the interpretation of todo keywords is
1777 not given already in `org-todo-keywords'. See that variable for
1780 :group
'org-keywords
1781 :type
'(choice (const sequence
)
1784 (defcustom org-use-fast-todo-selection
'prefix
1785 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1786 This variable describes if and under what circumstances the cycling
1787 mechanism for TODO keywords will be replaced by a single-key, direct
1790 When nil, fast selection is never used.
1792 When the symbol `prefix', it will be used when `org-todo' is called with
1793 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1794 in an agenda buffer.
1796 When t, fast selection is used by default. In this case, the prefix
1797 argument forces cycling instead.
1799 In all cases, the special interface is only used if access keys have actually
1800 been assigned by the user, i.e. if keywords in the configuration are followed
1801 by a letter in parenthesis, like TODO(t)."
1804 (const :tag
"Never" nil
)
1805 (const :tag
"By default" t
)
1806 (const :tag
"Only with C-u C-c C-t" prefix
)))
1808 (defcustom org-after-todo-state-change-hook nil
1809 "Hook which is run after the state of a TODO item was changed.
1810 The new state (a string with a TODO keyword, or nil) is available in the
1811 Lisp variable `state'."
1815 (defcustom org-log-done nil
1816 "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
1817 When equal to the list (done), also prompt for a closing note.
1818 This can also be configured on a per-file basis by adding one of
1819 the following lines anywhere in the buffer:
1822 #+STARTUP: lognotedone
1823 #+STARTUP: nologdone"
1825 :group
'org-progress
1827 (const :tag
"No logging" nil
)
1828 (const :tag
"Record CLOSED timestamp" time
)
1829 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1831 ;; Normalize old uses of org-log-done.
1833 ((eq org-log-done t
) (setq org-log-done
'time
))
1834 ((and (listp org-log-done
) (memq 'done org-log-done
))
1835 (setq org-log-done
'note
)))
1838 (defcustom org-log-note-clock-out nil
1839 "Non-nil means, recored a note when clocking out of an item.
1840 This can also be configured on a per-file basis by adding one of
1841 the following lines anywhere in the buffer:
1843 #+STARTUP: lognoteclock-out
1844 #+STARTUP: nolognoteclock-out"
1846 :group
'org-progress
1849 (defcustom org-log-done-with-time t
1850 "Non-nil means, the CLOSED time stamp will contain date and time.
1851 When nil, only the date will be recorded."
1852 :group
'org-progress
1855 (defcustom org-log-note-headings
1856 '((done .
"CLOSING NOTE %t")
1857 (state .
"State %-12s %t")
1859 "Headings for notes added when clocking out or closing TODO items.
1860 The value is an alist, with the car being a symbol indicating the note
1861 context, and the cdr is the heading to be used. The heading may also be the
1863 %t in the heading will be replaced by a time stamp.
1864 %s will be replaced by the new TODO state, in double quotes.
1865 %u will be replaced by the user name.
1866 %U will be replaced by the full user name."
1868 :group
'org-progress
1869 :type
'(list :greedy t
1870 (cons (const :tag
"Heading when closing an item" done
) string
)
1872 "Heading when changing todo state (todo sequence only)"
1874 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1876 (defcustom org-log-states-order-reversed t
1877 "Non-nil means, the latest state change note will be directly after heading.
1878 When nil, the notes will be orderer according to time."
1880 :group
'org-progress
1883 (defcustom org-log-repeat
'time
1884 "Non-nil means, record moving through the DONE state when triggering repeat.
1885 An auto-repeating tasks is immediately switched back to TODO when marked
1886 done. If you are not logging state changes (by adding \"@\" or \"!\" to
1887 the TODO keyword definition, or recording a cloing note by setting
1888 `org-log-done', there will be no record of the task moving trhough DONE.
1889 This variable forces taking a note anyway. Possible values are:
1891 nil Don't force a record
1892 time Record a time stamp
1895 This option can also be set with on a per-file-basis with
1897 #+STARTUP: logrepeat
1898 #+STARTUP: lognoterepeat
1899 #+STARTUP: nologrepeat
1901 You can have local logging settings for a subtree by setting the LOGGING
1902 property to one or more of these keywords."
1904 :group
'org-progress
1906 (const :tag
"Don't force a record" nil
)
1907 (const :tag
"Force recording the DONE state" time
)
1908 (const :tag
"Force recording a note with the DONE state" note
)))
1910 (defcustom org-clock-into-drawer
2
1911 "Should clocking info be wrapped into a drawer?
1912 When t, clocking info will always be inserted into a :CLOCK: drawer.
1913 If necessary, the drawer will be created.
1914 When nil, the drawer will not be created, but used when present.
1915 When an integer and the number of clocking entries in an item
1916 reaches or exceeds this number, a drawer will be created."
1918 :group
'org-progress
1920 (const :tag
"Always" t
)
1921 (const :tag
"Only when drawer exists" nil
)
1922 (integer :tag
"When at least N clock entries")))
1924 (defcustom org-clock-out-when-done t
1925 "When t, the clock will be stopped when the relevant entry is marked DONE.
1926 Nil means, clock will keep running until stopped explicitly with
1927 `C-c C-x C-o', or until the clock is started in a different item."
1928 :group
'org-progress
1931 (defcustom org-clock-in-switch-to-state nil
1932 "Set task to a special todo state while clocking it.
1933 The value should be the state to which the entry should be switched."
1934 :group
'org-progress
1937 (const :tag
"Don't force a state" nil
)
1938 (string :tag
"State")))
1940 (defgroup org-priorities nil
1941 "Priorities in Org-mode."
1942 :tag
"Org Priorities"
1945 (defcustom org-highest-priority ?A
1946 "The highest priority of TODO items. A character like ?A, ?B etc.
1947 Must have a smaller ASCII number than `org-lowest-priority'."
1948 :group
'org-priorities
1951 (defcustom org-lowest-priority ?C
1952 "The lowest priority of TODO items. A character like ?A, ?B etc.
1953 Must have a larger ASCII number than `org-highest-priority'."
1954 :group
'org-priorities
1957 (defcustom org-default-priority ?B
1958 "The default priority of TODO items.
1959 This is the priority an item get if no explicit priority is given."
1960 :group
'org-priorities
1963 (defcustom org-priority-start-cycle-with-default t
1964 "Non-nil means, start with default priority when starting to cycle.
1965 When this is nil, the first step in the cycle will be (depending on the
1966 command used) one higher or lower that the default priority."
1967 :group
'org-priorities
1970 (defgroup org-time nil
1971 "Options concerning time stamps and deadlines in Org-mode."
1975 (defcustom org-insert-labeled-timestamps-at-point nil
1976 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
1977 When nil, these labeled time stamps are forces into the second line of an
1978 entry, just after the headline. When scheduling from the global TODO list,
1979 the time stamp will always be forced into the second line."
1983 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
1984 "Formats for `format-time-string' which are used for time stamps.
1985 It is not recommended to change this constant.")
1987 (defcustom org-time-stamp-rounding-minutes
'(0 5)
1988 "Number of minutes to round time stamps to.
1989 These are two values, the first applies when first creating a time stamp.
1990 The second applies when changing it with the commands `S-up' and `S-down'.
1991 When changing the time stamp, this means that it will change in steps
1992 of N minues, as given by the second value.
1994 When a setting is 0 or 1, insert the time unmodified. Useful rounding
1995 numbers should be factors of 60, so for example 5, 10, 15.
1997 When this is larger than 1, you can still force an exact time-stamp by using
1998 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
1999 and by using a prefix arg to `S-up/down' to specify the exact number
2000 of minutes to shift."
2002 :get
'(lambda (var) ; Make sure all entries have 5 elements
2003 (if (integerp (default-value var
))
2004 (list (default-value var
) 5)
2005 (default-value var
)))
2007 (integer :tag
"when inserting times")
2008 (integer :tag
"when modifying times")))
2010 (defcustom org-display-custom-times nil
2011 "Non-nil means, overlay custom formats over all time stamps.
2012 The formats are defined through the variable `org-time-stamp-custom-formats'.
2013 To turn this on on a per-file basis, insert anywhere in the file:
2014 #+STARTUP: customtime"
2018 (make-variable-buffer-local 'org-display-custom-times
)
2020 (defcustom org-time-stamp-custom-formats
2021 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2022 "Custom formats for time stamps. See `format-time-string' for the syntax.
2023 These are overlayed over the default ISO format if the variable
2024 `org-display-custom-times' is set. Time like %H:%M should be at the
2025 end of the second format."
2029 (defun org-time-stamp-format (&optional long inactive
)
2030 "Get the right format for a time string."
2031 (let ((f (if long
(cdr org-time-stamp-formats
)
2032 (car org-time-stamp-formats
))))
2034 (concat "[" (substring f
1 -
1) "]")
2037 (defcustom org-read-date-prefer-future t
2038 "Non-nil means, assume future for incomplete date input from user.
2039 This affects the following situations:
2040 1. The user gives a day, but no month.
2041 For example, if today is the 15th, and you enter \"3\", Org-mode will
2042 read this as the third of *next* month. However, if you enter \"17\",
2043 it will be considered as *this* month.
2044 2. The user gives a month but not a year.
2045 For example, if it is april and you enter \"feb 2\", this will be read
2046 as feb 2, *next* year. \"May 5\", however, will be this year.
2048 When this option is nil, the current month and year will always be used
2053 (defcustom org-read-date-display-live t
2054 "Non-nil means, display current interpretation of date prompt live.
2055 This display will be in an overlay, in the minibuffer."
2059 (defcustom org-read-date-popup-calendar t
2060 "Non-nil means, pop up a calendar when prompting for a date.
2061 In the calendar, the date can be selected with mouse-1. However, the
2062 minibuffer will also be active, and you can simply enter the date as well.
2063 When nil, only the minibuffer will be available."
2066 (if (fboundp 'defvaralias
)
2067 (defvaralias 'org-popup-calendar-for-date-prompt
2068 'org-read-date-popup-calendar
))
2070 (defcustom org-extend-today-until
0
2071 "The hour when your day really ends.
2072 This has influence for the following applications:
2073 - When switching the agenda to \"today\". It it is still earlier than
2074 the time given here, the day recognized as TODAY is actually yesterday.
2075 - When a date is read from the user and it is still before the time given
2076 here, the current date and time will be assumed to be yesterday, 23:59.
2079 IMPORTANT: This is still a very experimental feature, it may disappear
2080 again or it may be extended to mean more things."
2084 (defcustom org-edit-timestamp-down-means-later nil
2085 "Non-nil means, S-down will increase the time in a time stamp.
2086 When nil, S-up will increase."
2090 (defcustom org-calendar-follow-timestamp-change t
2091 "Non-nil means, make the calendar window follow timestamp changes.
2092 When a timestamp is modified and the calendar window is visible, it will be
2093 moved to the new date."
2097 (defcustom org-clock-heading-function nil
2098 "When non-nil, should be a function to create `org-clock-heading'.
2099 This is the string shown in the mode line when a clock is running.
2100 The function is called with point at the beginning of the headline."
2101 :group
'org-time
; FIXME: Should we have a separate group????
2104 (defgroup org-tags nil
2105 "Options concerning tags in Org-mode."
2109 (defcustom org-tag-alist nil
2110 "List of tags allowed in Org-mode files.
2111 When this list is nil, Org-mode will base TAG input on what is already in the
2113 The value of this variable is an alist, the car of each entry must be a
2114 keyword as a string, the cdr may be a character that is used to select
2115 that tag through the fast-tag-selection interface.
2116 See the manual for details."
2120 (cons (string :tag
"Tag name")
2121 (character :tag
"Access char"))
2122 (const :tag
"Start radio group" (:startgroup
))
2123 (const :tag
"End radio group" (:endgroup
)))))
2125 (defcustom org-use-fast-tag-selection
'auto
2126 "Non-nil means, use fast tag selection scheme.
2127 This is a special interface to select and deselect tags with single keys.
2128 When nil, fast selection is never used.
2129 When the symbol `auto', fast selection is used if and only if selection
2130 characters for tags have been configured, either through the variable
2131 `org-tag-alist' or through a #+TAGS line in the buffer.
2132 When t, fast selection is always used and selection keys are assigned
2133 automatically if necessary."
2136 (const :tag
"Always" t
)
2137 (const :tag
"Never" nil
)
2138 (const :tag
"When selection characters are configured" 'auto
)))
2140 (defcustom org-fast-tag-selection-single-key nil
2141 "Non-nil means, fast tag selection exits after first change.
2142 When nil, you have to press RET to exit it.
2143 During fast tag selection, you can toggle this flag with `C-c'.
2144 This variable can also have the value `expert'. In this case, the window
2145 displaying the tags menu is not even shown, until you press C-c again."
2148 (const :tag
"No" nil
)
2149 (const :tag
"Yes" t
)
2150 (const :tag
"Expert" expert
)))
2152 (defvar org-fast-tag-selection-include-todo nil
2153 "Non-nil means, fast tags selection interface will also offer TODO states.
2154 This is an undocumented feature, you should not rely on it.")
2156 (defcustom org-tags-column -
80
2157 "The column to which tags should be indented in a headline.
2158 If this number is positive, it specifies the column. If it is negative,
2159 it means that the tags should be flushright to that column. For example,
2160 -80 works well for a normal 80 character screen."
2164 (defcustom org-auto-align-tags t
2165 "Non-nil means, realign tags after pro/demotion of TODO state change.
2166 These operations change the length of a headline and therefore shift
2167 the tags around. With this options turned on, after each such operation
2168 the tags are again aligned to `org-tags-column'."
2172 (defcustom org-use-tag-inheritance t
2173 "Non-nil means, tags in levels apply also for sublevels.
2174 When nil, only the tags directly given in a specific line apply there.
2175 If you turn off this option, you very likely want to turn on the
2176 companion option `org-tags-match-list-sublevels'."
2180 (defcustom org-tags-match-list-sublevels nil
2181 "Non-nil means list also sublevels of headlines matching tag search.
2182 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2183 the sublevels of a headline matching a tag search often also match
2184 the same search. Listing all of them can create very long lists.
2185 Setting this variable to nil causes subtrees of a match to be skipped.
2186 This option is off by default, because inheritance in on. If you turn
2187 inheritance off, you very likely want to turn this option on.
2189 As a special case, if the tag search is restricted to TODO items, the
2190 value of this variable is ignored and sublevels are always checked, to
2191 make sure all corresponding TODO items find their way into the list."
2195 (defvar org-tags-history nil
2196 "History of minibuffer reads for tags.")
2197 (defvar org-last-tags-completion-table nil
2198 "The last used completion table for tags.")
2199 (defvar org-after-tags-change-hook nil
2200 "Hook that is run after the tags in a line have changed.")
2202 (defgroup org-properties nil
2203 "Options concerning properties in Org-mode."
2204 :tag
"Org Properties"
2207 (defcustom org-property-format
"%-10s %s"
2208 "How property key/value pairs should be formatted by `indent-line'.
2209 When `indent-line' hits a property definition, it will format the line
2210 according to this format, mainly to make sure that the values are
2211 lined-up with respect to each other."
2212 :group
'org-properties
2215 (defcustom org-use-property-inheritance nil
2216 "Non-nil means, properties apply also for sublevels.
2217 This setting is only relevant during property searches, not when querying
2218 an entry with `org-entry-get'. To retrieve a property with inheritance,
2219 you need to call `org-entry-get' with the inheritance flag.
2220 Turning this on can cause significant overhead when doing a search, so
2221 this is turned off by default.
2222 When nil, only the properties directly given in the current entry count.
2223 The value may also be a list of properties that shouldhave inheritance.
2225 However, note that some special properties use inheritance under special
2226 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2227 and the properties ending in \"_ALL\" when they are used as descriptor
2228 for valid values of a property."
2229 :group
'org-properties
2231 (const :tag
"Not" nil
)
2232 (const :tag
"Always" nil
)
2233 (repeat :tag
"Specific properties" (string :tag
"Property"))))
2235 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2236 "The default column format, if no other format has been defined.
2237 This variable can be set on the per-file basis by inserting a line
2239 #+COLUMNS: %25ITEM ....."
2240 :group
'org-properties
2243 (defcustom org-global-properties nil
2244 "List of property/value pairs that can be inherited by any entry.
2245 You can set buffer-local values for this by adding lines like
2247 #+PROPERTY: NAME VALUE"
2248 :group
'org-properties
2250 (cons (string :tag
"Property")
2251 (string :tag
"Value"))))
2253 (defvar org-local-properties nil
2254 "List of property/value pairs that can be inherited by any entry.
2255 Valid for the current buffer.
2256 This variable is populated from #+PROPERTY lines.")
2258 (defgroup org-agenda nil
2259 "Options concerning agenda views in Org-mode."
2263 (defvar org-category nil
2264 "Variable used by org files to set a category for agenda display.
2265 Such files should use a file variable to set it, for example
2267 # -*- mode: org; org-category: \"ELisp\"
2269 or contain a special line
2273 If the file does not specify a category, then file's base name
2275 (make-variable-buffer-local 'org-category
)
2277 (defcustom org-agenda-files nil
2278 "The files to be used for agenda display.
2279 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2280 \\[org-remove-file]. You can also use customize to edit the list.
2282 If an entry is a directory, all files in that directory that are matched by
2283 `org-agenda-file-regexp' will be part of the file list.
2285 If the value of the variable is not a list but a single file name, then
2286 the list of agenda files is actually stored and maintained in that file, one
2287 agenda file per line."
2290 (repeat :tag
"List of files and directories" file
)
2291 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2293 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2294 "Regular expression to match files for `org-agenda-files'.
2295 If any element in the list in that variable contains a directory instead
2296 of a normal file, all files in that directory that are matched by this
2297 regular expression will be included."
2301 (defcustom org-agenda-skip-unavailable-files nil
2302 "t means to just skip non-reachable files in `org-agenda-files'.
2303 Nil means to remove them, after a query, from the list."
2307 (defcustom org-agenda-text-search-extra-files nil
2308 "List of extra files to be searched by text search commands.
2309 These files will be search in addition to the agenda files bu the
2310 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2311 Note that these files will only be searched for text search commands,
2312 not for the other agenda views like todo lists, tag earches or the weekly
2313 agenda. This variable is intended to list notes and possibly archive files
2314 that should also be searched by these two commands."
2316 :type
'(repeat file
))
2318 (if (fboundp 'defvaralias
)
2319 (defvaralias 'org-agenda-multi-occur-extra-files
2320 'org-agenda-text-search-extra-files
))
2322 (defcustom org-agenda-confirm-kill
1
2323 "When set, remote killing from the agenda buffer needs confirmation.
2324 When t, a confirmation is always needed. When a number N, confirmation is
2325 only needed when the text to be killed contains more than N non-white lines."
2328 (const :tag
"Never" nil
)
2329 (const :tag
"Always" t
)
2330 (number :tag
"When more than N lines")))
2332 (defcustom org-calendar-to-agenda-key
[?c
]
2333 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2334 The command `org-calendar-goto-agenda' will be bound to this key. The
2335 default is the character `c' because then `c' can be used to switch back and
2336 forth between agenda and calendar."
2340 (defcustom org-agenda-compact-blocks nil
2341 "Non-nil means, make the block agenda more compact.
2342 This is done by leaving out unnecessary lines."
2346 (defgroup org-agenda-export nil
2347 "Options concerning exporting agenda views in Org-mode."
2348 :tag
"Org Agenda Export"
2351 (defcustom org-agenda-with-colors t
2352 "Non-nil means, use colors in agenda views."
2353 :group
'org-agenda-export
2356 (defcustom org-agenda-exporter-settings nil
2357 "Alist of variable/value pairs that should be active during agenda export.
2358 This is a good place to set uptions for ps-print and for htmlize."
2359 :group
'org-agenda-export
2363 (sexp :tag
"Value"))))
2365 (defcustom org-agenda-export-html-style
""
2366 "The style specification for exported HTML Agenda files.
2367 If this variable contains a string, it will replace the default <style>
2368 section as produced by `htmlize'.
2369 Since there are different ways of setting style information, this variable
2370 needs to contain the full HTML structure to provide a style, including the
2371 surrounding HTML tags. The style specifications should include definitions
2372 the fonts used by the agenda, here is an example:
2374 <style type=\"text/css\">
2375 p { font-weight: normal; color: gray; }
2376 .org-agenda-structure {
2382 color: #cc6666;Week-agenda:
2388 .title { text-align: center; }
2389 .todo, .deadline { color: red; }
2390 .done { color: green; }
2393 or, if you want to keep the style in a file,
2395 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
2397 As the value of this option simply gets inserted into the HTML <head> header,
2398 you can \"misuse\" it to also add other text to the header. However,
2399 <style>...</style> is required, if not present the variable will be ignored."
2400 :group
'org-agenda-export
2401 :group
'org-export-html
2404 (defgroup org-agenda-custom-commands nil
2405 "Options concerning agenda views in Org-mode."
2406 :tag
"Org Agenda Custom Commands"
2409 (defcustom org-agenda-custom-commands nil
2410 "Custom commands for the agenda.
2411 These commands will be offered on the splash screen displayed by the
2412 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
2414 (key desc type match options files)
2416 key The key (one or more characters as a string) to be associated
2418 desc A description of the commend, when omitted or nil, a default
2419 description is built using MATCH.
2420 type The command type, any of the following symbols:
2421 agenda The daily/weekly agenda.
2422 todo Entries with a specific TODO keyword, in all agenda files.
2423 search Entries containing search words entry or headline.
2424 tags Tags/Property/TODO match in all agenda files.
2425 tags-todo Tags/P/T match in all agenda files, TODO entries only.
2426 todo-tree Sparse tree of specific TODO keyword in *current* file.
2427 tags-tree Sparse tree with all tags matches in *current* file.
2428 occur-tree Occur sparse tree for *current* file.
2429 ... A user-defined function.
2430 match What to search for:
2431 - a single keyword for TODO keyword searches
2432 - a tags match expression for tags searches
2433 - a regular expression for occur searches
2434 options A list of option settings, similar to that in a let form, so like
2435 this: ((opt1 val1) (opt2 val2) ...)
2436 files A list of files file to write the produced agenda buffer to
2437 with the command `org-store-agenda-views'.
2438 If a file name ends in \".html\", an HTML version of the buffer
2439 is written out. If it ends in \".ps\", a postscript version is
2440 produced. Otherwide, only the plain text is written to the file.
2442 You can also define a set of commands, to create a composite agenda buffer.
2443 In this case, an entry looks like this:
2445 (key desc (cmd1 cmd2 ...) general-options file)
2449 desc A description string to be displayed in the dispatcher menu.
2450 cmd An agenda command, similar to the above. However, tree commands
2451 are no allowed, but instead you can get agenda and global todo list.
2452 So valid commands for a set are:
2456 (todo \"match\" options files)
2457 (search \"match\" options files)
2458 (tags \"match\" options files)
2459 (tags-todo \"match\" options files)
2461 Each command can carry a list of options, and another set of options can be
2462 given for the whole set of commands. Individual command options take
2463 precedence over the general options.
2465 When using several characters as key to a command, the first characters
2466 are prefix commands. For the dispatcher to display useful information, you
2467 should provide a description for the prefix, like
2469 (setq org-agenda-custom-commands
2470 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
2471 (\"hl\" tags \"+HOME+Lisa\")
2472 (\"hp\" tags \"+HOME+Peter\")
2473 (\"hk\" tags \"+HOME+Kim\")))"
2474 :group
'org-agenda-custom-commands
2476 (choice :value
("a" "" tags
"" nil
)
2477 (list :tag
"Single command"
2478 (string :tag
"Access Key(s) ")
2479 (option (string :tag
"Description"))
2481 (const :tag
"Agenda" agenda
)
2482 (const :tag
"TODO list" alltodo
)
2483 (const :tag
"Search words" search
)
2484 (const :tag
"Stuck projects" stuck
)
2485 (const :tag
"Tags search (all agenda files)" tags
)
2486 (const :tag
"Tags search of TODO entries (all agenda files)" tags-todo
)
2487 (const :tag
"TODO keyword search (all agenda files)" todo
)
2488 (const :tag
"Tags sparse tree (current buffer)" tags-tree
)
2489 (const :tag
"TODO keyword tree (current buffer)" todo-tree
)
2490 (const :tag
"Occur tree (current buffer)" occur-tree
)
2491 (sexp :tag
"Other, user-defined function"))
2492 (string :tag
"Match")
2493 (repeat :tag
"Local options"
2494 (list (variable :tag
"Option") (sexp :tag
"Value")))
2495 (option (repeat :tag
"Export" (file :tag
"Export to"))))
2496 (list :tag
"Command series, all agenda files"
2497 (string :tag
"Access Key(s)")
2498 (string :tag
"Description ")
2499 (repeat :tag
"Component"
2501 (const :tag
"Agenda" (agenda))
2502 (const :tag
"TODO list" (alltodo))
2503 (list :tag
"Search words"
2504 (const :format
"" search
)
2505 (string :tag
"Match")
2506 (repeat :tag
"Local options"
2507 (list (variable :tag
"Option")
2508 (sexp :tag
"Value"))))
2509 (const :tag
"Stuck projects" (stuck))
2510 (list :tag
"Tags search"
2511 (const :format
"" tags
)
2512 (string :tag
"Match")
2513 (repeat :tag
"Local options"
2514 (list (variable :tag
"Option")
2515 (sexp :tag
"Value"))))
2517 (list :tag
"Tags search, TODO entries only"
2518 (const :format
"" tags-todo
)
2519 (string :tag
"Match")
2520 (repeat :tag
"Local options"
2521 (list (variable :tag
"Option")
2522 (sexp :tag
"Value"))))
2524 (list :tag
"TODO keyword search"
2525 (const :format
"" todo
)
2526 (string :tag
"Match")
2527 (repeat :tag
"Local options"
2528 (list (variable :tag
"Option")
2529 (sexp :tag
"Value"))))
2531 (list :tag
"Other, user-defined function"
2532 (symbol :tag
"function")
2533 (string :tag
"Match")
2534 (repeat :tag
"Local options"
2535 (list (variable :tag
"Option")
2536 (sexp :tag
"Value"))))))
2538 (repeat :tag
"General options"
2539 (list (variable :tag
"Option")
2540 (sexp :tag
"Value")))
2541 (option (repeat :tag
"Export" (file :tag
"Export to"))))
2542 (cons :tag
"Prefix key documentation"
2543 (string :tag
"Access Key(s)")
2544 (string :tag
"Description ")))))
2546 (defcustom org-agenda-query-register ?o
2547 "The register holding the current query string.
2548 The prupose of this is that if you construct a query string interactively,
2549 you can then use it to define a custom command."
2550 :group
'org-agenda-custom-commands
2553 (defcustom org-stuck-projects
2554 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil
"")
2555 "How to identify stuck projects.
2556 This is a list of four items:
2557 1. A tags/todo matcher string that is used to identify a project.
2558 The entire tree below a headline matched by this is considered one project.
2559 2. A list of TODO keywords identifying non-stuck projects.
2560 If the project subtree contains any headline with one of these todo
2561 keywords, the project is considered to be not stuck. If you specify
2562 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
2563 3. A list of tags identifying non-stuck projects.
2564 If the project subtree contains any headline with one of these tags,
2565 the project is considered to be not stuck. If you specify \"*\" as
2566 a tag, any tag will mark the project unstuck.
2567 4. An arbitrary regular expression matching non-stuck projects.
2569 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
2570 or `C-c a #' to produce the list."
2571 :group
'org-agenda-custom-commands
2573 (string :tag
"Tags/TODO match to identify a project")
2574 (repeat :tag
"Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
2575 (repeat :tag
"Projects are *not* stuck if they have an entry with TAG being any of" (string))
2576 (regexp :tag
"Projects are *not* stuck if this regexp matches\ninside the subtree")))
2579 (defgroup org-agenda-skip nil
2580 "Options concerning skipping parts of agenda files."
2581 :tag
"Org Agenda Skip"
2584 (defcustom org-agenda-todo-list-sublevels t
2585 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
2586 When nil, the sublevels of a TODO entry are not checked, resulting in
2587 potentially much shorter TODO lists."
2588 :group
'org-agenda-skip
2592 (defcustom org-agenda-todo-ignore-with-date nil
2593 "Non-nil means, don't show entries with a date in the global todo list.
2594 You can use this if you prefer to mark mere appointments with a TODO keyword,
2595 but don't want them to show up in the TODO list.
2596 When this is set, it also covers deadlines and scheduled items, the settings
2597 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
2599 :group
'org-agenda-skip
2603 (defcustom org-agenda-todo-ignore-scheduled nil
2604 "Non-nil means, don't show scheduled entries in the global todo list.
2605 The idea behind this is that by scheduling it, you have already taken care
2607 See also `org-agenda-todo-ignore-with-date'."
2608 :group
'org-agenda-skip
2612 (defcustom org-agenda-todo-ignore-deadlines nil
2613 "Non-nil means, don't show near deadline entries in the global todo list.
2614 Near means closer than `org-deadline-warning-days' days.
2615 The idea behind this is that such items will appear in the agenda anyway.
2616 See also `org-agenda-todo-ignore-with-date'."
2617 :group
'org-agenda-skip
2621 (defcustom org-agenda-skip-scheduled-if-done nil
2622 "Non-nil means don't show scheduled items in agenda when they are done.
2623 This is relevant for the daily/weekly agenda, not for the TODO list. And
2624 it applies only to the actual date of the scheduling. Warnings about
2625 an item with a past scheduling dates are always turned off when the item
2627 :group
'org-agenda-skip
2630 (defcustom org-agenda-skip-deadline-if-done nil
2631 "Non-nil means don't show deadines when the corresponding item is done.
2632 When nil, the deadline is still shown and should give you a happy feeling.
2633 This is relevant for the daily/weekly agenda. And it applied only to the
2634 actualy date of the deadline. Warnings about approching and past-due
2635 deadlines are always turned off when the item is DONE."
2636 :group
'org-agenda-skip
2639 (defcustom org-agenda-skip-timestamp-if-done nil
2640 "Non-nil means don't select item by timestamp or -range if it is DONE."
2641 :group
'org-agenda-skip
2644 (defcustom org-timeline-show-empty-dates
3
2645 "Non-nil means, `org-timeline' also shows dates without an entry.
2646 When nil, only the days which actually have entries are shown.
2647 When t, all days between the first and the last date are shown.
2648 When an integer, show also empty dates, but if there is a gap of more than
2649 N days, just insert a special line indicating the size of the gap."
2650 :group
'org-agenda-skip
2652 (const :tag
"None" nil
)
2653 (const :tag
"All" t
)
2654 (number :tag
"at most")))
2657 (defgroup org-agenda-startup nil
2658 "Options concerning initial settings in the Agenda in Org Mode."
2659 :tag
"Org Agenda Startup"
2662 (defcustom org-finalize-agenda-hook nil
2663 "Hook run just before displaying an agenda buffer."
2664 :group
'org-agenda-startup
2667 (defcustom org-agenda-mouse-1-follows-link nil
2668 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
2669 A longer mouse click will still set point. Does not work on XEmacs.
2670 Needs to be set before org.el is loaded."
2671 :group
'org-agenda-startup
2674 (defcustom org-agenda-start-with-follow-mode nil
2675 "The initial value of follow-mode in a newly created agenda window."
2676 :group
'org-agenda-startup
2679 (defgroup org-agenda-windows nil
2680 "Options concerning the windows used by the Agenda in Org Mode."
2681 :tag
"Org Agenda Windows"
2684 (defcustom org-agenda-window-setup
'reorganize-frame
2685 "How the agenda buffer should be displayed.
2686 Possible values for this option are:
2688 current-window Show agenda in the current window, keeping all other windows.
2689 other-frame Use `switch-to-buffer-other-frame' to display agenda.
2690 other-window Use `switch-to-buffer-other-window' to display agenda.
2691 reorganize-frame Show only two windows on the current frame, the current
2692 window and the agenda.
2693 See also the variable `org-agenda-restore-windows-after-quit'."
2694 :group
'org-agenda-windows
2696 (const current-window
)
2698 (const other-window
)
2699 (const reorganize-frame
)))
2701 (defcustom org-agenda-window-frame-fractions
'(0.5 .
0.75)
2702 "The min and max height of the agenda window as a fraction of frame height.
2703 The value of the variable is a cons cell with two numbers between 0 and 1.
2704 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
2705 :group
'org-agenda-windows
2706 :type
'(cons (number :tag
"Minimum") (number :tag
"Maximum")))
2708 (defcustom org-agenda-restore-windows-after-quit nil
2709 "Non-nil means, restore window configuration open exiting agenda.
2710 Before the window configuration is changed for displaying the agenda,
2711 the current status is recorded. When the agenda is exited with
2712 `q' or `x' and this option is set, the old state is restored. If
2713 `org-agenda-window-setup' is `other-frame', the value of this
2714 option will be ignored.."
2715 :group
'org-agenda-windows
2718 (defcustom org-indirect-buffer-display
'other-window
2719 "How should indirect tree buffers be displayed?
2720 This applies to indirect buffers created with the commands
2721 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
2723 current-window Display in the current window
2724 other-window Just display in another window.
2725 dedicated-frame Create one new frame, and re-use it each time.
2726 new-frame Make a new frame each time. Note that in this case
2727 previously-made indirect buffers are kept, and you need to
2728 kill these buffers yourself."
2729 :group
'org-structure
2730 :group
'org-agenda-windows
2732 (const :tag
"In current window" current-window
)
2733 (const :tag
"In current frame, other window" other-window
)
2734 (const :tag
"Each time a new frame" new-frame
)
2735 (const :tag
"One dedicated frame" dedicated-frame
)))
2737 (defgroup org-agenda-daily
/weekly nil
2738 "Options concerning the daily/weekly agenda."
2739 :tag
"Org Agenda Daily/Weekly"
2742 (defcustom org-agenda-ndays
7
2743 "Number of days to include in overview display.
2745 :group
'org-agenda-daily
/weekly
2748 (defcustom org-agenda-start-on-weekday
1
2749 "Non-nil means, start the overview always on the specified weekday.
2750 0 denotes Sunday, 1 denotes Monday etc.
2751 When nil, always start on the current day."
2752 :group
'org-agenda-daily
/weekly
2753 :type
'(choice (const :tag
"Today" nil
)
2754 (number :tag
"Weekday No.")))
2756 (defcustom org-agenda-show-all-dates t
2757 "Non-nil means, `org-agenda' shows every day in the selected range.
2758 When nil, only the days which actually have entries are shown."
2759 :group
'org-agenda-daily
/weekly
2762 (defcustom org-agenda-format-date
'org-agenda-format-date-aligned
2763 "Format string for displaying dates in the agenda.
2764 Used by the daily/weekly agenda and by the timeline. This should be
2765 a format string understood by `format-time-string', or a function returning
2766 the formatted date as a string. The function must take a single argument,
2767 a calendar-style date list like (month day year)."
2768 :group
'org-agenda-daily
/weekly
2770 (string :tag
"Format string")
2771 (function :tag
"Function")))
2773 (defun org-agenda-format-date-aligned (date)
2774 "Format a date string for display in the daily/weekly agenda, or timeline.
2775 This function makes sure that dates are aligned for easy reading."
2776 (format "%-9s %2d %s %4d"
2777 (calendar-day-name date
)
2778 (extract-calendar-day date
)
2779 (calendar-month-name (extract-calendar-month date
))
2780 (extract-calendar-year date
)))
2782 (defcustom org-agenda-include-diary nil
2783 "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
2784 :group
'org-agenda-daily
/weekly
2787 (defcustom org-agenda-include-all-todo nil
2788 "Set means weekly/daily agenda will always contain all TODO entries.
2789 The TODO entries will be listed at the top of the agenda, before
2790 the entries for specific days."
2791 :group
'org-agenda-daily
/weekly
2794 (defcustom org-agenda-repeating-timestamp-show-all t
2795 "Non-nil means, show all occurences of a repeating stamp in the agenda.
2796 When nil, only one occurence is shown, either today or the
2797 nearest into the future."
2798 :group
'org-agenda-daily
/weekly
2801 (defcustom org-deadline-warning-days
14
2802 "No. of days before expiration during which a deadline becomes active.
2803 This variable governs the display in sparse trees and in the agenda.
2804 When 0 or negative, it means use this number (the absolute value of it)
2805 even if a deadline has a different individual lead time specified."
2807 :group
'org-agenda-daily
/weekly
2810 (defcustom org-scheduled-past-days
10000
2811 "No. of days to continue listing scheduled items that are not marked DONE.
2812 When an item is scheduled on a date, it shows up in the agenda on this
2813 day and will be listed until it is marked done for the number of days
2815 :group
'org-agenda-daily
/weekly
2818 (defgroup org-agenda-time-grid nil
2819 "Options concerning the time grid in the Org-mode Agenda."
2820 :tag
"Org Agenda Time Grid"
2823 (defcustom org-agenda-use-time-grid t
2824 "Non-nil means, show a time grid in the agenda schedule.
2825 A time grid is a set of lines for specific times (like every two hours between
2826 8:00 and 20:00). The items scheduled for a day at specific times are
2827 sorted in between these lines.
2828 For details about when the grid will be shown, and what it will look like, see
2829 the variable `org-agenda-time-grid'."
2830 :group
'org-agenda-time-grid
2833 (defcustom org-agenda-time-grid
2834 '((daily today require-timed
)
2836 (800 1000 1200 1400 1600 1800 2000))
2838 "The settings for time grid for agenda display.
2839 This is a list of three items. The first item is again a list. It contains
2840 symbols specifying conditions when the grid should be displayed:
2842 daily if the agenda shows a single day
2843 weekly if the agenda shows an entire week
2844 today show grid on current date, independent of daily/weekly display
2845 require-timed show grid only if at least one item has a time specification
2847 The second item is a string which will be places behing the grid time.
2849 The third item is a list of integers, indicating the times that should have
2851 :group
'org-agenda-time-grid
2854 (set :greedy t
:tag
"Grid Display Options"
2855 (const :tag
"Show grid in single day agenda display" daily
)
2856 (const :tag
"Show grid in weekly agenda display" weekly
)
2857 (const :tag
"Always show grid for today" today
)
2858 (const :tag
"Show grid only if any timed entries are present"
2860 (const :tag
"Skip grid times already present in an entry"
2862 (string :tag
"Grid String")
2863 (repeat :tag
"Grid Times" (integer :tag
"Time"))))
2865 (defgroup org-agenda-sorting nil
2866 "Options concerning sorting in the Org-mode Agenda."
2867 :tag
"Org Agenda Sorting"
2870 (defconst org-sorting-choice
2872 (const time-up
) (const time-down
)
2873 (const category-keep
) (const category-up
) (const category-down
)
2874 (const tag-down
) (const tag-up
)
2875 (const priority-up
) (const priority-down
))
2878 (defcustom org-agenda-sorting-strategy
2879 '((agenda time-up category-keep priority-down
)
2880 (todo category-keep priority-down
)
2881 (tags category-keep priority-down
)
2882 (search category-keep
))
2883 "Sorting structure for the agenda items of a single day.
2884 This is a list of symbols which will be used in sequence to determine
2885 if an entry should be listed before another entry. The following
2886 symbols are recognized:
2888 time-up Put entries with time-of-day indications first, early first
2889 time-down Put entries with time-of-day indications first, late first
2890 category-keep Keep the default order of categories, corresponding to the
2891 sequence in `org-agenda-files'.
2892 category-up Sort alphabetically by category, A-Z.
2893 category-down Sort alphabetically by category, Z-A.
2894 tag-up Sort alphabetically by last tag, A-Z.
2895 tag-down Sort alphabetically by last tag, Z-A.
2896 priority-up Sort numerically by priority, high priority last.
2897 priority-down Sort numerically by priority, high priority first.
2899 The different possibilities will be tried in sequence, and testing stops
2900 if one comparison returns a \"not-equal\". For example, the default
2901 '(time-up category-keep priority-down)
2902 means: Pull out all entries having a specified time of day and sort them,
2903 in order to make a time schedule for the current day the first thing in the
2904 agenda listing for the day. Of the entries without a time indication, keep
2905 the grouped in categories, don't sort the categories, but keep them in
2906 the sequence given in `org-agenda-files'. Within each category sort by
2909 Leaving out `category-keep' would mean that items will be sorted across
2910 categories by priority.
2912 Instead of a single list, this can also be a set of list for specific
2913 contents, with a context symbol in the car of the list, any of
2914 `agenda', `todo', `tags' for the corresponding agenda views."
2915 :group
'org-agenda-sorting
2917 (repeat :tag
"General" ,org-sorting-choice
)
2918 (list :tag
"Individually"
2919 (cons (const :tag
"Strategy for Weekly/Daily agenda" agenda
)
2920 (repeat ,org-sorting-choice
))
2921 (cons (const :tag
"Strategy for TODO lists" todo
)
2922 (repeat ,org-sorting-choice
))
2923 (cons (const :tag
"Strategy for Tags matches" tags
)
2924 (repeat ,org-sorting-choice
)))))
2926 (defcustom org-sort-agenda-notime-is-late t
2927 "Non-nil means, items without time are considered late.
2928 This is only relevant for sorting. When t, items which have no explicit
2929 time like 15:30 will be considered as 99:01, i.e. later than any items which
2930 do have a time. When nil, the default time is before 0:00. You can use this
2931 option to decide if the schedule for today should come before or after timeless
2933 :group
'org-agenda-sorting
2936 (defgroup org-agenda-line-format nil
2937 "Options concerning the entry prefix in the Org-mode agenda display."
2938 :tag
"Org Agenda Line Format"
2941 (defcustom org-agenda-prefix-format
2942 '((agenda .
" %-12:c%?-12t% s")
2946 (search .
" %-12:c"))
2947 "Format specifications for the prefix of items in the agenda views.
2948 An alist with four entries, for the different agenda types. The keys to the
2949 sublists are `agenda', `timeline', `todo', and `tags'. The values
2951 This format works similar to a printf format, with the following meaning:
2953 %c the category of the item, \"Diary\" for entries from the diary, or
2954 as given by the CATEGORY keyword or derived from the file name.
2955 %T the *last* tag of the item. Last because inherited tags come
2957 %t the time-of-day specification if one applies to the entry, in the
2959 %s Scheduling/Deadline information, a short string
2961 All specifiers work basically like the standard `%s' of printf, but may
2962 contain two additional characters: A question mark just after the `%' and
2963 a whitespace/punctuation character just before the final letter.
2965 If the first character after `%' is a question mark, the entire field
2966 will only be included if the corresponding value applies to the
2967 current entry. This is useful for fields which should have fixed
2968 width when present, but zero width when absent. For example,
2969 \"%?-12t\" will result in a 12 character time field if a time of the
2970 day is specified, but will completely disappear in entries which do
2973 If there is punctuation or whitespace character just before the final
2974 format letter, this character will be appended to the field value if
2975 the value is not empty. For example, the format \"%-12:c\" leads to
2976 \"Diary: \" if the category is \"Diary\". If the category were be
2977 empty, no additional colon would be interted.
2979 The default value of this option is \" %-12:c%?-12t% s\", meaning:
2980 - Indent the line with two space characters
2981 - Give the category in a 12 chars wide field, padded with whitespace on
2982 the right (because of `-'). Append a colon if there is a category
2984 - If there is a time-of-day, put it into a 12 chars wide field. If no
2985 time, don't put in an empty field, just skip it (because of '?').
2986 - Finally, put the scheduling information and append a whitespace.
2988 As another example, if you don't want the time-of-day of entries in
2989 the prefix, you could use:
2991 (setq org-agenda-prefix-format \" %-11:c% s\")
2993 See also the variables `org-agenda-remove-times-when-in-prefix' and
2994 `org-agenda-remove-tags'."
2996 (string :tag
"General format")
2997 (list :greedy t
:tag
"View dependent"
2998 (cons (const agenda
) (string :tag
"Format"))
2999 (cons (const timeline
) (string :tag
"Format"))
3000 (cons (const todo
) (string :tag
"Format"))
3001 (cons (const tags
) (string :tag
"Format"))
3002 (cons (const search
) (string :tag
"Format"))))
3003 :group
'org-agenda-line-format
)
3005 (defvar org-prefix-format-compiled nil
3006 "The compiled version of the most recently used prefix format.
3007 See the variable `org-agenda-prefix-format'.")
3009 (defcustom org-agenda-todo-keyword-format
"%-1s"
3010 "Format for the TODO keyword in agenda lines.
3011 Set this to something like \"%-12s\" if you want all TODO keywords
3012 to occupy a fixed space in the agenda display."
3013 :group
'org-agenda-line-format
3016 (defcustom org-agenda-scheduled-leaders
'("Scheduled: " "Sched.%2dx: ")
3017 "Text preceeding scheduled items in the agenda view.
3018 This is a list with two strings. The first applies when the item is
3019 scheduled on the current day. The second applies when it has been scheduled
3020 previously, it may contain a %d to capture how many days ago the item was
3022 :group
'org-agenda-line-format
3024 (string :tag
"Scheduled today ")
3025 (string :tag
"Scheduled previously")))
3027 (defcustom org-agenda-deadline-leaders
'("Deadline: " "In %3d d.: ")
3028 "Text preceeding deadline items in the agenda view.
3029 This is a list with two strings. The first applies when the item has its
3030 deadline on the current day. The second applies when it is in the past or
3031 in the future, it may contain %d to capture how many days away the deadline
3033 :group
'org-agenda-line-format
3035 (string :tag
"Deadline today ")
3036 (string :tag
"Deadline relative")))
3038 (defcustom org-agenda-remove-times-when-in-prefix t
3039 "Non-nil means, remove duplicate time specifications in agenda items.
3040 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
3041 time-of-day specification in a headline or diary entry is extracted and
3042 placed into the prefix. If this option is non-nil, the original specification
3043 \(a timestamp or -range, or just a plain time(range) specification like
3044 11:30-4pm) will be removed for agenda display. This makes the agenda less
3046 The option can be t or nil. It may also be the symbol `beg', indicating
3047 that the time should only be removed what it is located at the beginning of
3048 the headline/diary entry."
3049 :group
'org-agenda-line-format
3051 (const :tag
"Always" t
)
3052 (const :tag
"Never" nil
)
3053 (const :tag
"When at beginning of entry" beg
)))
3056 (defcustom org-agenda-default-appointment-duration nil
3057 "Default duration for appointments that only have a starting time.
3058 When nil, no duration is specified in such cases.
3059 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
3060 :group
'org-agenda-line-format
3062 (integer :tag
"Minutes")
3063 (const :tag
"No default duration")))
3066 (defcustom org-agenda-remove-tags nil
3067 "Non-nil means, remove the tags from the headline copy in the agenda.
3068 When this is the symbol `prefix', only remove tags when
3069 `org-agenda-prefix-format' contains a `%T' specifier."
3070 :group
'org-agenda-line-format
3072 (const :tag
"Always" t
)
3073 (const :tag
"Never" nil
)
3074 (const :tag
"When prefix format contains %T" prefix
)))
3076 (if (fboundp 'defvaralias
)
3077 (defvaralias 'org-agenda-remove-tags-when-in-prefix
3078 'org-agenda-remove-tags
))
3080 (defcustom org-agenda-tags-column -
80
3081 "Shift tags in agenda items to this column.
3082 If this number is positive, it specifies the column. If it is negative,
3083 it means that the tags should be flushright to that column. For example,
3084 -80 works well for a normal 80 character screen."
3085 :group
'org-agenda-line-format
3088 (if (fboundp 'defvaralias
)
3089 (defvaralias 'org-agenda-align-tags-to-column
'org-agenda-tags-column
))
3091 (defcustom org-agenda-fontify-priorities t
3092 "Non-nil means, highlight low and high priorities in agenda.
3093 When t, the highest priority entries are bold, lowest priority italic.
3094 This may also be an association list of priority faces. The face may be
3095 a names face, or a list like `(:background \"Red\")'."
3096 :group
'org-agenda-line-format
3098 (const :tag
"Never" nil
)
3099 (const :tag
"Defaults" t
)
3100 (repeat :tag
"Specify"
3101 (list (character :tag
"Priority" :value ?A
)
3102 (sexp :tag
"face")))))
3104 (defgroup org-latex nil
3105 "Options for embedding LaTeX code into Org-mode"
3109 (defcustom org-format-latex-options
3110 '(:foreground default
:background default
:scale
1.0
3111 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
3112 :matchers
("begin" "$" "$$" "\\(" "\\["))
3113 "Options for creating images from LaTeX fragments.
3114 This is a property list with the following properties:
3115 :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
3116 `default' means use the forground of the default face.
3117 :background the background color, or \"Transparent\".
3118 `default' means use the background of the default face.
3119 :scale a scaling factor for the size of the images
3120 :html-foreground, :html-background, :html-scale
3121 The same numbers for HTML export.
3122 :matchers a list indicating which matchers should be used to
3123 find LaTeX fragments. Valid members of this list are:
3124 \"begin\" find environments
3125 \"$\" find math expressions surrounded by $...$
3126 \"$$\" find math expressions surrounded by $$....$$
3127 \"\\(\" find math expressions surrounded by \\(...\\)
3128 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3132 (defcustom org-format-latex-header
"\\documentclass{article}
3133 \\usepackage{fullpage} % do not remove
3134 \\usepackage{amssymb}
3135 \\usepackage[usenames]{color}
3136 \\usepackage{amsmath}
3137 \\usepackage{latexsym}
3138 \\usepackage[mathscr]{eucal}
3139 \\pagestyle{empty} % do not remove"
3140 "The document header used for processing LaTeX fragments."
3144 (defgroup org-export nil
3145 "Options for exporting org-listings."
3149 (defgroup org-export-general nil
3150 "General options for exporting Org-mode files."
3151 :tag
"Org Export General"
3155 (defvar org-export-publishing-directory nil
)
3157 (defcustom org-export-with-special-strings t
3158 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
3159 When this option is turned on, these strings will be exported as:
3162 -----+----------+--------
3168 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
3169 :group
'org-export-translation
3172 (defcustom org-export-language-setup
3173 '(("en" "Author" "Date" "Table of Contents")
3174 ("cs" "Autor" "Datum" "Obsah")
3175 ("da" "Ophavsmand" "Dato" "Indhold")
3176 ("de" "Autor" "Datum" "Inhaltsverzeichnis")
3177 ("es" "Autor" "Fecha" "\xcdndice")
3178 ("fr" "Auteur" "Date" "Table des mati\xe8res")
3179 ("it" "Autore" "Data" "Indice")
3180 ("nl" "Auteur" "Datum" "Inhoudsopgave")
3181 ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
3182 ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
3183 "Terms used in export text, translated to different languages.
3184 Use the variable `org-export-default-language' to set the language,
3185 or use the +OPTION lines for a per-file setting."
3186 :group
'org-export-general
3189 (string :tag
"HTML language tag")
3190 (string :tag
"Author")
3191 (string :tag
"Date")
3192 (string :tag
"Table of Contents"))))
3194 (defcustom org-export-default-language
"en"
3195 "The default language of HTML export, as a string.
3196 This should have an association in `org-export-language-setup'."
3197 :group
'org-export-general
3200 (defcustom org-export-skip-text-before-1st-heading t
3201 "Non-nil means, skip all text before the first headline when exporting.
3202 When nil, that text is exported as well."
3203 :group
'org-export-general
3206 (defcustom org-export-headline-levels
3
3207 "The last level which is still exported as a headline.
3208 Inferior levels will produce itemize lists when exported.
3209 Note that a numeric prefix argument to an exporter function overrides
3212 This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
3213 :group
'org-export-general
3216 (defcustom org-export-with-section-numbers t
3217 "Non-nil means, add section numbers to headlines when exporting.
3219 This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
3220 :group
'org-export-general
3223 (defcustom org-export-with-toc t
3224 "Non-nil means, create a table of contents in exported files.
3225 The TOC contains headlines with levels up to`org-export-headline-levels'.
3226 When an integer, include levels up to N in the toc, this may then be
3227 different from `org-export-headline-levels', but it will not be allowed
3228 to be larger than the number of headline levels.
3229 When nil, no table of contents is made.
3231 Headlines which contain any TODO items will be marked with \"(*)\" in
3232 ASCII export, and with red color in HTML output, if the option
3233 `org-export-mark-todo-in-toc' is set.
3235 In HTML output, the TOC will be clickable.
3237 This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"
3239 :group
'org-export-general
3241 (const :tag
"No Table of Contents" nil
)
3242 (const :tag
"Full Table of Contents" t
)
3243 (integer :tag
"TOC to level")))
3245 (defcustom org-export-mark-todo-in-toc nil
3246 "Non-nil means, mark TOC lines that contain any open TODO items."
3247 :group
'org-export-general
3250 (defcustom org-export-preserve-breaks nil
3251 "Non-nil means, preserve all line breaks when exporting.
3252 Normally, in HTML output paragraphs will be reformatted. In ASCII
3253 export, line breaks will always be preserved, regardless of this variable.
3255 This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
3256 :group
'org-export-general
3259 (defcustom org-export-with-archived-trees
'headline
3260 "Whether subtrees with the ARCHIVE tag should be exported.
3261 This can have three different values
3262 nil Do not export, pretend this tree is not present
3263 t Do export the entire tree
3264 headline Only export the headline, but skip the tree below it."
3265 :group
'org-export-general
3268 (const :tag
"not at all" nil
)
3269 (const :tag
"headline only" 'headline
)
3270 (const :tag
"entirely" t
)))
3272 (defcustom org-export-author-info t
3273 "Non-nil means, insert author name and email into the exported file.
3275 This option can also be set with the +OPTIONS line,
3276 e.g. \"author-info:nil\"."
3277 :group
'org-export-general
3280 (defcustom org-export-time-stamp-file t
3281 "Non-nil means, insert a time stamp into the exported file.
3282 The time stamp shows when the file was created.
3284 This option can also be set with the +OPTIONS line,
3285 e.g. \"timestamp:nil\"."
3286 :group
'org-export-general
3289 (defcustom org-export-with-timestamps t
3290 "If nil, do not export time stamps and associated keywords."
3291 :group
'org-export-general
3294 (defcustom org-export-remove-timestamps-from-toc t
3295 "If nil, remove timestamps from the table of contents entries."
3296 :group
'org-export-general
3299 (defcustom org-export-with-tags
'not-in-toc
3300 "If nil, do not export tags, just remove them from headlines.
3301 If this is the symbol `not-in-toc', tags will be removed from table of
3302 contents entries, but still be shown in the headlines of the document.
3304 This option can also be set with the +OPTIONS line, e.g. \"tags:nil\"."
3305 :group
'org-export-general
3307 (const :tag
"Off" nil
)
3308 (const :tag
"Not in TOC" not-in-toc
)
3309 (const :tag
"On" t
)))
3311 (defcustom org-export-with-drawers nil
3312 "Non-nil means, export with drawers like the property drawer.
3313 When t, all drawers are exported. This may also be a list of
3314 drawer names to export."
3315 :group
'org-export-general
3317 (const :tag
"All drawers" t
)
3318 (const :tag
"None" nil
)
3319 (repeat :tag
"Selected drawers"
3320 (string :tag
"Drawer name"))))
3322 (defgroup org-export-translation nil
3323 "Options for translating special ascii sequences for the export backends."
3324 :tag
"Org Export Translation"
3327 (defcustom org-export-with-emphasize t
3328 "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
3329 If the export target supports emphasizing text, the word will be
3330 typeset in bold, italic, or underlined, respectively. Works only for
3331 single words, but you can say: I *really* *mean* *this*.
3332 Not all export backends support this.
3334 This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
3335 :group
'org-export-translation
3338 (defcustom org-export-with-footnotes t
3339 "If nil, export [1] as a footnote marker.
3340 Lines starting with [1] will be formatted as footnotes.
3342 This option can also be set with the +OPTIONS line, e.g. \"f:nil\"."
3343 :group
'org-export-translation
3346 (defcustom org-export-with-sub-superscripts t
3347 "Non-nil means, interpret \"_\" and \"^\" for export.
3348 When this option is turned on, you can use TeX-like syntax for sub- and
3349 superscripts. Several characters after \"_\" or \"^\" will be
3350 considered as a single item - so grouping with {} is normally not
3351 needed. For example, the following things will be parsed as single
3352 sub- or superscripts.
3354 10^24 or 10^tau several digits will be considered 1 item.
3355 10^-12 or 10^-tau a leading sign with digits or a word
3356 x^2-y^3 will be read as x^2 - y^3, because items are
3357 terminated by almost any nonword/nondigit char.
3358 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
3360 Still, ambiguity is possible - so when in doubt use {} to enclose the
3361 sub/superscript. If you set this variable to the symbol `{}',
3362 the braces are *required* in order to trigger interpretations as
3363 sub/superscript. This can be helpful in documents that need \"_\"
3364 frequently in plain text.
3366 Not all export backends support this, but HTML does.
3368 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
3369 :group
'org-export-translation
3371 (const :tag
"Always interpret" t
)
3372 (const :tag
"Only with braces" {})
3373 (const :tag
"Never interpret" nil
)))
3375 (defcustom org-export-with-special-strings t
3376 "Non-nil means, interpret \"\-\", \"--\" and \"---\" for export.
3377 When this option is turned on, these strings will be exported as:
3383 Not all export backends support this, but HTML does.
3385 This option can also be set with the +OPTIONS line, e.g. \"-:nil\"."
3386 :group
'org-export-translation
3389 (defcustom org-export-with-TeX-macros t
3390 "Non-nil means, interpret simple TeX-like macros when exporting.
3391 For example, HTML export converts \\alpha to α and \\AA to Å.
3392 No only real TeX macros will work here, but the standard HTML entities
3393 for math can be used as macro names as well. For a list of supported
3394 names in HTML export, see the constant `org-html-entities'.
3395 Not all export backends support this.
3397 This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
3398 :group
'org-export-translation
3399 :group
'org-export-latex
3402 (defcustom org-export-with-LaTeX-fragments nil
3403 "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
3404 When set, the exporter will find LaTeX environments if the \\begin line is
3405 the first non-white thing on a line. It will also find the math delimiters
3406 like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
3409 This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
3410 :group
'org-export-translation
3411 :group
'org-export-latex
3414 (defcustom org-export-with-fixed-width t
3415 "Non-nil means, lines starting with \":\" will be in fixed width font.
3416 This can be used to have pre-formatted text, fragments of code etc. For
3418 : ;; Some Lisp examples
3421 will be looking just like this in also HTML. See also the QUOTE keyword.
3422 Not all export backends support this.
3424 This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
3425 :group
'org-export-translation
3428 (defcustom org-match-sexp-depth
3
3429 "Number of stacked braces for sub/superscript matching.
3430 This has to be set before loading org.el to be effective."
3431 :group
'org-export-translation
3434 (defgroup org-export-tables nil
3435 "Options for exporting tables in Org-mode."
3436 :tag
"Org Export Tables"
3439 (defcustom org-export-with-tables t
3440 "If non-nil, lines starting with \"|\" define a table.
3443 | Name | Address | Birthday |
3444 |-------------+----------+-----------|
3445 | Arthur Dent | England | 29.2.2100 |
3447 Not all export backends support this.
3449 This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
3450 :group
'org-export-tables
3453 (defcustom org-export-highlight-first-table-line t
3454 "Non-nil means, highlight the first table line.
3455 In HTML export, this means use <th> instead of <td>.
3456 In tables created with table.el, this applies to the first table line.
3457 In Org-mode tables, all lines before the first horizontal separator
3458 line will be formatted with <th> tags."
3459 :group
'org-export-tables
3462 (defcustom org-export-table-remove-special-lines t
3463 "Remove special lines and marking characters in calculating tables.
3464 This removes the special marking character column from tables that are set
3465 up for spreadsheet calculations. It also removes the entire lines
3466 marked with `!', `_', or `^'. The lines with `$' are kept, because
3467 the values of constants may be useful to have."
3468 :group
'org-export-tables
3471 (defcustom org-export-prefer-native-exporter-for-tables nil
3472 "Non-nil means, always export tables created with table.el natively.
3473 Natively means, use the HTML code generator in table.el.
3474 When nil, Org-mode's own HTML generator is used when possible (i.e. if
3475 the table does not use row- or column-spanning). This has the
3476 advantage, that the automatic HTML conversions for math symbols and
3477 sub/superscripts can be applied. Org-mode's HTML generator is also
3479 :group
'org-export-tables
3482 (defgroup org-export-ascii nil
3483 "Options specific for ASCII export of Org-mode files."
3484 :tag
"Org Export ASCII"
3487 (defcustom org-export-ascii-underline
'(?\$ ?\
# ?^ ?\~ ?\
= ?\-
)
3488 "Characters for underlining headings in ASCII export.
3489 In the given sequence, these characters will be used for level 1, 2, ..."
3490 :group
'org-export-ascii
3491 :type
'(repeat character
))
3493 (defcustom org-export-ascii-bullets
'(?
* ?
+ ?-
)
3494 "Bullet characters for headlines converted to lists in ASCII export.
3495 The first character is used for the first lest level generated in this
3496 way, and so on. If there are more levels than characters given here,
3497 the list will be repeated.
3498 Note that plain lists will keep the same bullets as the have in the
3500 :group
'org-export-ascii
3501 :type
'(repeat character
))
3503 (defgroup org-export-xml nil
3504 "Options specific for XML export of Org-mode files."
3505 :tag
"Org Export XML"
3508 (defgroup org-export-html nil
3509 "Options specific for HTML export of Org-mode files."
3510 :tag
"Org Export HTML"
3513 (defcustom org-export-html-coding-system nil
3515 :group
'org-export-html
3516 :type
'coding-system
)
3518 (defcustom org-export-html-extension
"html"
3519 "The extension for exported HTML files."
3520 :group
'org-export-html
3523 (defcustom org-export-html-style
3524 "<style type=\"text/css\">
3526 font-family: Times, serif;
3529 .title { text-align: center; }
3530 .todo { color: red; }
3531 .done { color: green; }
3532 .timestamp { color: grey }
3533 .timestamp-kwd { color: CadetBlue }
3534 .tag { background-color:lightblue; font-weight:normal }
3535 .target { background-color: lavender; }
3537 border: 1pt solid #AEBDCC;
3538 background-color: #F3F5F7;
3540 font-family: courier, monospace;
3542 table { border-collapse: collapse; }
3544 vertical-align: top;
3545 <!--border: 1pt solid #ADB9CC;-->
3548 "The default style specification for exported HTML files.
3549 Since there are different ways of setting style information, this variable
3550 needs to contain the full HTML structure to provide a style, including the
3551 surrounding HTML tags. The style specifications should include definitions
3552 for new classes todo, done, title, and deadline. For example, legal values
3555 <style type=\"text/css\">
3556 p { font-weight: normal; color: gray; }
3557 h1 { color: black; }
3558 .title { text-align: center; }
3559 .todo, .deadline { color: red; }
3560 .done { color: green; }
3563 or, if you want to keep the style in a file,
3565 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
3567 As the value of this option simply gets inserted into the HTML <head> header,
3568 you can \"misuse\" it to add arbitrary text to the header."
3569 :group
'org-export-html
3573 (defcustom org-export-html-title-format
"<h1 class=\"title\">%s</h1>\n"
3574 "Format for typesetting the document title in HTML export."
3575 :group
'org-export-html
3578 (defcustom org-export-html-toplevel-hlevel
2
3579 "The <H> level for level 1 headings in HTML export."
3580 :group
'org-export-html
3583 (defcustom org-export-html-link-org-files-as-html t
3584 "Non-nil means, make file links to `file.org' point to `file.html'.
3585 When org-mode is exporting an org-mode file to HTML, links to
3586 non-html files are directly put into a href tag in HTML.
3587 However, links to other Org-mode files (recognized by the
3588 extension `.org.) should become links to the corresponding html
3589 file, assuming that the linked org-mode file will also be
3591 When nil, the links still point to the plain `.org' file."
3592 :group
'org-export-html
3595 (defcustom org-export-html-inline-images
'maybe
3596 "Non-nil means, inline images into exported HTML pages.
3597 This is done using an <img> tag. When nil, an anchor with href is used to
3598 link to the image. If this option is `maybe', then images in links with
3599 an empty description will be inlined, while images with a description will
3601 :group
'org-export-html
3602 :type
'(choice (const :tag
"Never" nil
)
3603 (const :tag
"Always" t
)
3604 (const :tag
"When there is no description" maybe
)))
3607 (defcustom org-export-html-expand t
3608 "Non-nil means, for HTML export, treat @<...> as HTML tag.
3609 When nil, these tags will be exported as plain text and therefore
3610 not be interpreted by a browser.
3612 This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
3613 :group
'org-export-html
3616 (defcustom org-export-html-table-tag
3617 "<table border=\"2\" cellspacing=\"0\" cellpadding=\"6\" rules=\"groups\" frame=\"hsides\">"
3618 "The HTML tag that is used to start a table.
3619 This must be a <table> tag, but you may change the options like
3620 borders and spacing."
3621 :group
'org-export-html
3624 (defcustom org-export-table-header-tags
'("<th>" .
"</th>")
3625 "The opening tag for table header fields.
3626 This is customizable so that alignment options can be specified."
3627 :group
'org-export-tables
3628 :type
'(cons (string :tag
"Opening tag") (string :tag
"Closing tag")))
3630 (defcustom org-export-table-data-tags
'("<td>" .
"</td>")
3631 "The opening tag for table data fields.
3632 This is customizable so that alignment options can be specified."
3633 :group
'org-export-tables
3634 :type
'(cons (string :tag
"Opening tag") (string :tag
"Closing tag")))
3636 (defcustom org-export-html-with-timestamp nil
3637 "If non-nil, write `org-export-html-html-helper-timestamp'
3638 into the exported HTML text. Otherwise, the buffer will just be saved
3640 :group
'org-export-html
3643 (defcustom org-export-html-html-helper-timestamp
3644 "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
3645 "The HTML tag used as timestamp delimiter for HTML-helper-mode."
3646 :group
'org-export-html
3649 (defgroup org-export-icalendar nil
3650 "Options specific for iCalendar export of Org-mode files."
3651 :tag
"Org Export iCalendar"
3654 (defcustom org-combined-agenda-icalendar-file
"~/org.ics"
3655 "The file name for the iCalendar file covering all agenda files.
3656 This file is created with the command \\[org-export-icalendar-all-agenda-files].
3657 The file name should be absolute, the file will be overwritten without warning."
3658 :group
'org-export-icalendar
3661 (defcustom org-icalendar-include-todo nil
3662 "Non-nil means, export to iCalendar files should also cover TODO items."
3663 :group
'org-export-icalendar
3665 (const :tag
"None" nil
)
3666 (const :tag
"Unfinished" t
)
3667 (const :tag
"All" all
)))
3669 (defcustom org-icalendar-include-sexps t
3670 "Non-nil means, export to iCalendar files should also cover sexp entries.
3671 These are entries like in the diary, but directly in an Org-mode file."
3672 :group
'org-export-icalendar
3675 (defcustom org-icalendar-include-body
100
3676 "Amount of text below headline to be included in iCalendar export.
3677 This is a number of characters that should maximally be included.
3678 Properties, scheduling and clocking lines will always be removed.
3679 The text will be inserted into the DESCRIPTION field."
3680 :group
'org-export-icalendar
3682 (const :tag
"Nothing" nil
)
3683 (const :tag
"Everything" t
)
3684 (integer :tag
"Max characters")))
3686 (defcustom org-icalendar-combined-name
"OrgMode"
3687 "Calendar name for the combined iCalendar representing all agenda files."
3688 :group
'org-export-icalendar
3691 (defgroup org-font-lock nil
3692 "Font-lock settings for highlighting in Org-mode."
3693 :tag
"Org Font Lock"
3696 (defcustom org-level-color-stars-only nil
3697 "Non-nil means fontify only the stars in each headline.
3698 When nil, the entire headline is fontified.
3699 Changing it requires restart of `font-lock-mode' to become effective
3700 also in regions already fontified."
3701 :group
'org-font-lock
3704 (defcustom org-hide-leading-stars nil
3705 "Non-nil means, hide the first N-1 stars in a headline.
3706 This works by using the face `org-hide' for these stars. This
3707 face is white for a light background, and black for a dark
3708 background. You may have to customize the face `org-hide' to
3710 Changing it requires restart of `font-lock-mode' to become effective
3711 also in regions already fontified.
3712 You may also set this on a per-file basis by adding one of the following
3713 lines to the buffer:
3715 #+STARTUP: hidestars
3716 #+STARTUP: showstars"
3717 :group
'org-font-lock
3720 (defcustom org-fontify-done-headline nil
3721 "Non-nil means, change the face of a headline if it is marked DONE.
3722 Normally, only the TODO/DONE keyword indicates the state of a headline.
3723 When this is non-nil, the headline after the keyword is set to the
3724 `org-headline-done' as an additional indication."
3725 :group
'org-font-lock
3728 (defcustom org-fontify-emphasized-text t
3729 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3730 Changing this variable requires a restart of Emacs to take effect."
3731 :group
'org-font-lock
3734 (defcustom org-highlight-latex-fragments-and-specials nil
3735 "Non-nil means, fontify what is treated specially by the exporters."
3736 :group
'org-font-lock
3739 (defcustom org-hide-emphasis-markers nil
3740 "Non-nil mean font-lock should hide the emphasis marker characters."
3741 :group
'org-font-lock
3744 (defvar org-emph-re nil
3745 "Regular expression for matching emphasis.")
3746 (defvar org-verbatim-re nil
3747 "Regular expression for matching verbatim text.")
3748 (defvar org-emphasis-regexp-components
) ; defined just below
3749 (defvar org-emphasis-alist
) ; defined just below
3750 (defun org-set-emph-re (var val
)
3751 "Set variable and compute the emphasis regular expression."
3753 (when (and (boundp 'org-emphasis-alist
)
3754 (boundp 'org-emphasis-regexp-components
)
3755 org-emphasis-alist org-emphasis-regexp-components
)
3756 (let* ((e org-emphasis-regexp-components
)
3762 (stacked (and nil
(nth 5 e
))) ; stacked is no longer allowed, forced to nil
3763 (body1 (concat body
"*?"))
3764 (markers (mapconcat 'car org-emphasis-alist
""))
3765 (vmarkers (mapconcat
3766 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3767 org-emphasis-alist
"")))
3768 ;; make sure special characters appear at the right position in the class
3769 (if (string-match "\\^" markers
)
3770 (setq markers
(concat (replace-match "" t t markers
) "^")))
3771 (if (string-match "-" markers
)
3772 (setq markers
(concat (replace-match "" t t markers
) "-")))
3773 (if (string-match "\\^" vmarkers
)
3774 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3775 (if (string-match "-" vmarkers
)
3776 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3778 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3779 (int-to-string nl
) "\\}")))
3782 (concat "\\([" pre
(if (and nil stacked
) markers
) "]\\|^\\)"
3784 "\\([" markers
"]\\)"
3787 "[^" border
(if (and nil stacked
) markers
) "]"
3789 "[^" border
(if (and nil stacked
) markers
) "]"
3792 "\\([" post
(if (and nil stacked
) markers
) "]\\|$\\)"))
3793 (setq org-verbatim-re
3794 (concat "\\([" pre
"]\\|^\\)"
3796 "\\([" vmarkers
"]\\)"
3804 "\\([" post
"]\\|$\\)")))))
3806 (defcustom org-emphasis-regexp-components
3807 '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
3808 "Components used to build the regular expression for emphasis.
3809 This is a list with 6 entries. Terminology: In an emphasis string
3810 like \" *strong word* \", we call the initial space PREMATCH, the final
3811 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3812 and \"trong wor\" is the body. The different components in this variable
3813 specify what is allowed/forbidden in each part:
3815 pre Chars allowed as prematch. Beginning of line will be allowed too.
3816 post Chars allowed as postmatch. End of line will be allowed too.
3817 border The chars *forbidden* as border characters.
3818 body-regexp A regexp like \".\" to match a body character. Don't use
3819 non-shy groups here, and don't allow newline here.
3820 newline The maximum number of newlines allowed in an emphasis exp.
3822 Use customize to modify this, or restart Emacs after changing it."
3823 :group
'org-font-lock
3824 :set
'org-set-emph-re
3826 (sexp :tag
"Allowed chars in pre ")
3827 (sexp :tag
"Allowed chars in post ")
3828 (sexp :tag
"Forbidden chars in border ")
3829 (sexp :tag
"Regexp for body ")
3830 (integer :tag
"number of newlines allowed")
3831 (option (boolean :tag
"Stacking (DISABLED) "))))
3833 (defcustom org-emphasis-alist
3834 '(("*" bold
"<b>" "</b>")
3835 ("/" italic
"<i>" "</i>")
3836 ("_" underline
"<u>" "</u>")
3837 ("=" org-code
"<code>" "</code>" verbatim
)
3838 ("~" org-verbatim
"" "" verbatim
)
3839 ("+" (:strike-through t
) "<del>" "</del>")
3841 "Special syntax for emphasized text.
3842 Text starting and ending with a special character will be emphasized, for
3843 example *bold*, _underlined_ and /italic/. This variable sets the marker
3844 characters, the face to be used by font-lock for highlighting in Org-mode
3845 Emacs buffers, and the HTML tags to be used for this.
3846 Use customize to modify this, or restart Emacs after changing it."
3847 :group
'org-font-lock
3848 :set
'org-set-emph-re
3851 (string :tag
"Marker character")
3853 (face :tag
"Font-lock-face")
3854 (plist :tag
"Face property list"))
3855 (string :tag
"HTML start tag")
3856 (string :tag
"HTML end tag")
3857 (option (const verbatim
)))))
3861 (defgroup org-faces nil
3862 "Faces in Org-mode."
3864 :group
'org-font-lock
)
3866 (defun org-compatible-face (inherits specs
)
3867 "Make a compatible face specification.
3868 If INHERITS is an existing face and if the Emacs version supports it,
3869 just inherit the face. If not, use SPECS to define the face.
3870 XEmacs and Emacs 21 do not know about the `min-colors' attribute.
3871 For them we convert a (min-colors 8) entry to a `tty' entry and move it
3872 to the top of the list. The `min-colors' attribute will be removed from
3873 any other entries, and any resulting duplicates will be removed entirely."
3875 ((and inherits
(facep inherits
)
3876 (not (featurep 'xemacs
)) (> emacs-major-version
22))
3877 ;; In Emacs 23, we use inheritance where possible.
3878 ;; We only do this in Emacs 23, because only there the outline
3879 ;; faces have been changed to the original org-mode-level-faces.
3880 (list (list t
:inherit inherits
)))
3881 ((or (featurep 'xemacs
) (< emacs-major-version
22))
3882 ;; These do not understand the `min-colors' attribute.
3884 (while (setq e
(pop specs
))
3886 ((memq (car e
) '(t default
)) (push e r
))
3887 ((setq a
(member '(min-colors 8) (car e
)))
3888 (nconc r
(list (cons (cons '(type tty
) (delq (car a
) (car e
)))
3890 ((setq a
(assq 'min-colors
(car e
)))
3891 (setq e
(cons (delq a
(car e
)) (cdr e
)))
3892 (or (assoc (car e
) r
) (push e r
)))
3893 (t (or (assoc (car e
) r
) (push e r
)))))
3896 (put 'org-compatible-face
'lisp-indent-function
1)
3899 '((((background light
)) (:foreground
"white"))
3900 (((background dark
)) (:foreground
"black")))
3901 "Face used to hide leading stars in headlines.
3902 The forground color of this face should be equal to the background
3903 color of the frame."
3906 (defface org-level-1
;; font-lock-function-name-face
3907 (org-compatible-face 'outline-1
3908 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3909 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3910 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3911 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3912 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
3914 "Face used for level 1 headlines."
3917 (defface org-level-2
;; font-lock-variable-name-face
3918 (org-compatible-face 'outline-2
3919 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
3920 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
3921 (((class color
) (min-colors 8) (background light
)) (:foreground
"yellow"))
3922 (((class color
) (min-colors 8) (background dark
)) (:foreground
"yellow" :bold t
))
3924 "Face used for level 2 headlines."
3927 (defface org-level-3
;; font-lock-keyword-face
3928 (org-compatible-face 'outline-3
3929 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Purple"))
3930 (((class color
) (min-colors 88) (background dark
)) (:foreground
"Cyan1"))
3931 (((class color
) (min-colors 16) (background light
)) (:foreground
"Purple"))
3932 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Cyan"))
3933 (((class color
) (min-colors 8) (background light
)) (:foreground
"purple" :bold t
))
3934 (((class color
) (min-colors 8) (background dark
)) (:foreground
"cyan" :bold t
))
3936 "Face used for level 3 headlines."
3939 (defface org-level-4
;; font-lock-comment-face
3940 (org-compatible-face 'outline-4
3941 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
3942 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
3943 (((class color
) (min-colors 16) (background light
)) (:foreground
"red"))
3944 (((class color
) (min-colors 16) (background dark
)) (:foreground
"red1"))
3945 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
3946 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
3948 "Face used for level 4 headlines."
3951 (defface org-level-5
;; font-lock-type-face
3952 (org-compatible-face 'outline-5
3953 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen"))
3954 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen"))
3955 (((class color
) (min-colors 8)) (:foreground
"green"))))
3956 "Face used for level 5 headlines."
3959 (defface org-level-6
;; font-lock-constant-face
3960 (org-compatible-face 'outline-6
3961 '((((class color
) (min-colors 16) (background light
)) (:foreground
"CadetBlue"))
3962 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Aquamarine"))
3963 (((class color
) (min-colors 8)) (:foreground
"magenta"))))
3964 "Face used for level 6 headlines."
3967 (defface org-level-7
;; font-lock-builtin-face
3968 (org-compatible-face 'outline-7
3969 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Orchid"))
3970 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSteelBlue"))
3971 (((class color
) (min-colors 8)) (:foreground
"blue"))))
3972 "Face used for level 7 headlines."
3975 (defface org-level-8
;; font-lock-string-face
3976 (org-compatible-face 'outline-8
3977 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
3978 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
3979 (((class color
) (min-colors 8)) (:foreground
"green"))))
3980 "Face used for level 8 headlines."
3983 (defface org-special-keyword
;; font-lock-string-face
3984 (org-compatible-face nil
3985 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
3986 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
3988 "Face used for special keywords."
3991 (defface org-drawer
;; font-lock-function-name-face
3992 (org-compatible-face nil
3993 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
3994 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
3995 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
3996 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
3997 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
3999 "Face used for drawers."
4002 (defface org-property-value nil
4003 "Face used for the value of a property."
4007 (org-compatible-face nil
4008 '((((class color
) (min-colors 16) (background light
))
4009 (:background
"grey90"))
4010 (((class color
) (min-colors 16) (background dark
))
4011 (:background
"grey30"))
4012 (((class color
) (min-colors 8))
4013 (:background
"cyan" :foreground
"black"))
4014 (t (:inverse-video t
))))
4015 "Face for column display of entry properties."
4018 (when (fboundp 'set-face-attribute
)
4019 ;; Make sure that a fixed-width face is used when we have a column table.
4020 (set-face-attribute 'org-column nil
4021 :height
(face-attribute 'default
:height
)
4022 :family
(face-attribute 'default
:family
)))
4024 (defface org-warning
4025 (org-compatible-face 'font-lock-warning-face
4026 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
4027 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
4028 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
4029 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4031 "Face for deadlines and TODO keywords."
4034 (defface org-archived
; similar to shadow
4035 (org-compatible-face 'shadow
4036 '((((class color grayscale
) (min-colors 88) (background light
))
4037 (:foreground
"grey50"))
4038 (((class color grayscale
) (min-colors 88) (background dark
))
4039 (:foreground
"grey70"))
4040 (((class color
) (min-colors 8) (background light
))
4041 (:foreground
"green"))
4042 (((class color
) (min-colors 8) (background dark
))
4043 (:foreground
"yellow"))))
4044 "Face for headline with the ARCHIVE tag."
4048 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
4049 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
4054 (defface org-ellipsis
4055 '((((class color
) (background light
)) (:foreground
"DarkGoldenrod" :underline t
))
4056 (((class color
) (background dark
)) (:foreground
"LightGoldenrod" :underline t
))
4057 (t (:strike-through t
)))
4058 "Face for the ellipsis in folded text."
4062 '((((class color
) (background light
)) (:underline t
))
4063 (((class color
) (background dark
)) (:underline t
))
4069 '((((class color
) (background light
)) (:foreground
"Purple" :underline t
))
4070 (((class color
) (background dark
)) (:foreground
"Cyan" :underline t
))
4075 (defface org-sexp-date
4076 '((((class color
) (background light
)) (:foreground
"Purple"))
4077 (((class color
) (background dark
)) (:foreground
"Cyan"))
4087 (defface org-todo
; font-lock-warning-face
4088 (org-compatible-face nil
4089 '((((class color
) (min-colors 16) (background light
)) (:foreground
"Red1" :bold t
))
4090 (((class color
) (min-colors 16) (background dark
)) (:foreground
"Pink" :bold t
))
4091 (((class color
) (min-colors 8) (background light
)) (:foreground
"red" :bold t
))
4092 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4093 (t (:inverse-video t
:bold t
))))
4094 "Face for TODO keywords."
4097 (defface org-done
;; font-lock-type-face
4098 (org-compatible-face nil
4099 '((((class color
) (min-colors 16) (background light
)) (:foreground
"ForestGreen" :bold t
))
4100 (((class color
) (min-colors 16) (background dark
)) (:foreground
"PaleGreen" :bold t
))
4101 (((class color
) (min-colors 8)) (:foreground
"green"))
4103 "Face used for todo keywords that indicate DONE items."
4106 (defface org-headline-done
;; font-lock-string-face
4107 (org-compatible-face nil
4108 '((((class color
) (min-colors 16) (background light
)) (:foreground
"RosyBrown"))
4109 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSalmon"))
4110 (((class color
) (min-colors 8) (background light
)) (:bold nil
))))
4111 "Face used to indicate that a headline is DONE.
4112 This face is only used if `org-fontify-done-headline' is set. If applies
4113 to the part of the headline after the DONE keyword."
4116 (defcustom org-todo-keyword-faces nil
4117 "Faces for specific TODO keywords.
4118 This is a list of cons cells, with TODO keywords in the car
4119 and faces in the cdr. The face can be a symbol, or a property
4120 list of attributes, like (:foreground \"blue\" :weight bold :underline t)."
4125 (string :tag
"keyword")
4126 (sexp :tag
"face"))))
4128 (defface org-table
;; font-lock-function-name-face
4129 (org-compatible-face nil
4130 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
4131 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
4132 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
4133 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
4134 (((class color
) (min-colors 8) (background light
)) (:foreground
"blue"))
4135 (((class color
) (min-colors 8) (background dark
)))))
4136 "Face used for tables."
4139 (defface org-formula
4140 (org-compatible-face nil
4141 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4142 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4143 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
4144 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red"))
4145 (t (:bold t
:italic t
))))
4146 "Face for formulas."
4150 (org-compatible-face nil
4151 '((((class color grayscale
) (min-colors 88) (background light
))
4152 (:foreground
"grey50"))
4153 (((class color grayscale
) (min-colors 88) (background dark
))
4154 (:foreground
"grey70"))
4155 (((class color
) (min-colors 8) (background light
))
4156 (:foreground
"green"))
4157 (((class color
) (min-colors 8) (background dark
))
4158 (:foreground
"yellow"))))
4159 "Face for fixed-with text like code snippets."
4163 (defface org-verbatim
4164 (org-compatible-face nil
4165 '((((class color grayscale
) (min-colors 88) (background light
))
4166 (:foreground
"grey50" :underline t
))
4167 (((class color grayscale
) (min-colors 88) (background dark
))
4168 (:foreground
"grey70" :underline t
))
4169 (((class color
) (min-colors 8) (background light
))
4170 (:foreground
"green" :underline t
))
4171 (((class color
) (min-colors 8) (background dark
))
4172 (:foreground
"yellow" :underline t
))))
4173 "Face for fixed-with text like code snippets."
4177 (defface org-agenda-structure
;; font-lock-function-name-face
4178 (org-compatible-face nil
4179 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Blue1"))
4180 (((class color
) (min-colors 88) (background dark
)) (:foreground
"LightSkyBlue"))
4181 (((class color
) (min-colors 16) (background light
)) (:foreground
"Blue"))
4182 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightSkyBlue"))
4183 (((class color
) (min-colors 8)) (:foreground
"blue" :bold t
))
4185 "Face used in agenda for captions and dates."
4188 (defface org-scheduled-today
4189 (org-compatible-face nil
4190 '((((class color
) (min-colors 88) (background light
)) (:foreground
"DarkGreen"))
4191 (((class color
) (min-colors 88) (background dark
)) (:foreground
"PaleGreen"))
4192 (((class color
) (min-colors 8)) (:foreground
"green"))
4193 (t (:bold t
:italic t
))))
4194 "Face for items scheduled for a certain day."
4197 (defface org-scheduled-previously
4198 (org-compatible-face nil
4199 '((((class color
) (min-colors 88) (background light
)) (:foreground
"Firebrick"))
4200 (((class color
) (min-colors 88) (background dark
)) (:foreground
"chocolate1"))
4201 (((class color
) (min-colors 8) (background light
)) (:foreground
"red"))
4202 (((class color
) (min-colors 8) (background dark
)) (:foreground
"red" :bold t
))
4204 "Face for items scheduled previously, and not yet done."
4207 (defface org-upcoming-deadline
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" :bold t
))
4214 "Face for items scheduled previously, and not yet done."
4217 (defcustom org-agenda-deadline-faces
4218 '((1.0 . org-warning
)
4219 (0.5 . org-upcoming-deadline
)
4221 "Faces for showing deadlines in the agenda.
4222 This is a list of cons cells. The cdr of each cell is a face to be used,
4223 and it can also just be like '(:foreground \"yellow\").
4224 Each car is a fraction of the head-warning time that must have passed for
4225 this the face in the cdr to be used for display. The numbers must be
4226 given in descending order. The head-warning time is normally taken
4227 from `org-deadline-warning-days', but can also be specified in the deadline
4228 timestamp itself, like this:
4230 DEADLINE: <2007-08-13 Mon -8d>
4232 You may use d for days, w for weeks, m for months and y for years. Months
4233 and years will only be treated in an approximate fashion (30.4 days for a
4234 month and 365.24 days for a year)."
4236 :group
'org-agenda-daily
/weekly
4239 (number :tag
"Fraction of head-warning time passed")
4240 (sexp :tag
"Face"))))
4242 ;; FIXME: this is not a good face yet.
4243 (defface org-agenda-restriction-lock
4244 (org-compatible-face nil
4245 '((((class color
) (min-colors 88) (background light
)) (:background
"yellow1"))
4246 (((class color
) (min-colors 88) (background dark
)) (:background
"skyblue4"))
4247 (((class color
) (min-colors 16) (background light
)) (:background
"yellow1"))
4248 (((class color
) (min-colors 16) (background dark
)) (:background
"skyblue4"))
4249 (((class color
) (min-colors 8)) (:background
"cyan" :foreground
"black"))
4250 (t (:inverse-video t
))))
4251 "Face for showing the agenda restriction lock."
4254 (defface org-time-grid
;; font-lock-variable-name-face
4255 (org-compatible-face nil
4256 '((((class color
) (min-colors 16) (background light
)) (:foreground
"DarkGoldenrod"))
4257 (((class color
) (min-colors 16) (background dark
)) (:foreground
"LightGoldenrod"))
4258 (((class color
) (min-colors 8)) (:foreground
"yellow" :weight light
))))
4259 "Face used for time grids."
4262 (defconst org-level-faces
4263 '(org-level-1 org-level-2 org-level-3 org-level-4
4264 org-level-5 org-level-6 org-level-7 org-level-8
4267 (defcustom org-n-level-faces
(length org-level-faces
)
4268 "The number of different faces to be used for headlines.
4269 Org-mode defines 8 different headline faces, so this can be at most 8.
4270 If it is less than 8, the level-1 face gets re-used for level N+1 etc."
4274 ;;; Functions and variables from ther packages
4275 ;; Declared here to avoid compiler warnings
4278 (unless (fboundp 'declare-function
)
4279 (defmacro declare-function
(fn file
&optional arglist fileonly
))))
4282 (defvar outline-mode-menu-heading
)
4283 (defvar outline-mode-menu-show
)
4284 (defvar outline-mode-menu-hide
)
4285 (defvar zmacs-regions
) ; XEmacs regions
4288 (defvar mark-active
)
4291 ;; FIXME: get the argument lists for the UNKNOWN stuff
4292 (declare-function add-to-diary-list
"diary-lib"
4293 (date string specifier
&optional marker globcolor literal
))
4294 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
4295 (declare-function Info-find-node
"info" (filename nodename
&optional no-going-back
))
4296 (declare-function bbdb
"ext:bbdb-com" (string elidep
))
4297 (declare-function bbdb-company
"ext:bbdb-com" (string elidep
))
4298 (declare-function bbdb-current-record
"ext:bbdb-com" (&optional planning-on-modifying
))
4299 (declare-function bbdb-name
"ext:bbdb-com" (string elidep
))
4300 (declare-function bbdb-record-getprop
"ext:bbdb" (record property
))
4301 (declare-function bbdb-record-name
"ext:bbdb" (record))
4302 (declare-function bibtex-beginning-of-entry
"bibtex" ())
4303 (declare-function bibtex-generate-autokey
"bibtex" ())
4304 (declare-function bibtex-parse-entry
"bibtex" (&optional content
))
4305 (declare-function bibtex-url
"bibtex" (&optional pos no-browse
))
4306 (defvar calc-embedded-close-formula
)
4307 (defvar calc-embedded-open-formula
)
4308 (declare-function calendar-astro-date-string
"cal-julian" (&optional date
))
4309 (declare-function calendar-bahai-date-string
"cal-bahai" (&optional date
))
4310 (declare-function calendar-check-holidays
"holidays" (date))
4311 (declare-function calendar-chinese-date-string
"cal-china" (&optional date
))
4312 (declare-function calendar-coptic-date-string
"cal-coptic" (&optional date
))
4313 (declare-function calendar-ethiopic-date-string
"cal-coptic" (&optional date
))
4314 (declare-function calendar-forward-day
"cal-move" (arg))
4315 (declare-function calendar-french-date-string
"cal-french" (&optional date
))
4316 (declare-function calendar-goto-date
"cal-move" (date))
4317 (declare-function calendar-goto-today
"cal-move" ())
4318 (declare-function calendar-hebrew-date-string
"cal-hebrew" (&optional date
))
4319 (declare-function calendar-islamic-date-string
"cal-islam" (&optional date
))
4320 (declare-function calendar-iso-date-string
"cal-iso" (&optional date
))
4321 (declare-function calendar-julian-date-string
"cal-julian" (&optional date
))
4322 (declare-function calendar-mayan-date-string
"cal-mayan" (&optional date
))
4323 (declare-function calendar-persian-date-string
"cal-persia" (&optional date
))
4324 (defvar calendar-mode-map
)
4325 (defvar original-date
) ; dynamically scoped in calendar.el does scope this
4326 (declare-function cdlatex-tab
"ext:cdlatex" ())
4327 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
4328 (declare-function elmo-folder-exists-p
"ext:elmo" (folder) t
)
4329 (declare-function elmo-message-entity-field
"ext:elmo-msgdb" (entity field
&optional type
))
4330 (declare-function elmo-message-field
"ext:elmo" (folder number field
&optional type
) t
)
4331 (declare-function elmo-msgdb-overview-get-entity
"ext:elmo" (&rest unknown
) t
)
4332 (defvar font-lock-unfontify-region-function
)
4333 (declare-function gnus-article-show-summary
"gnus-art" ())
4334 (declare-function gnus-summary-last-subject
"gnus-sum" ())
4335 (defvar gnus-other-frame-object
)
4336 (defvar gnus-group-name
)
4337 (defvar gnus-article-current
)
4338 (defvar Info-current-file
)
4339 (defvar Info-current-node
)
4340 (declare-function mh-display-msg
"mh-show" (msg-num folder-name
))
4341 (declare-function mh-find-path
"mh-utils" ())
4342 (declare-function mh-get-header-field
"mh-utils" (field))
4343 (declare-function mh-get-msg-num
"mh-utils" (error-if-no-message))
4344 (declare-function mh-header-display
"mh-show" ())
4345 (declare-function mh-index-previous-folder
"mh-search" ())
4346 (declare-function mh-normalize-folder-name
"mh-utils" (folder &optional empty-string-okay dont-remove-trailing-slash return-nil-if-folder-empty
))
4347 (declare-function mh-search
"mh-search" (folder search-regexp
&optional redo-search-flag window-config
))
4348 (declare-function mh-search-choose
"mh-search" (&optional searcher
))
4349 (declare-function mh-show
"mh-show" (&optional message redisplay-flag
))
4350 (declare-function mh-show-buffer-message-number
"mh-comp" (&optional buffer
))
4351 (declare-function mh-show-header-display
"mh-show" t t
)
4352 (declare-function mh-show-msg
"mh-show" (msg))
4353 (declare-function mh-show-show
"mh-show" t t
)
4354 (declare-function mh-visit-folder
"mh-folder" (folder &optional range index-data
))
4356 (defvar mh-current-folder
)
4357 (defvar mh-show-folder-buffer
)
4358 (defvar mh-index-folder
)
4359 (defvar mh-searcher
)
4360 (declare-function org-export-latex-cleaned-string
"org-export-latex" ())
4361 (declare-function parse-time-string
"parse-time" (string))
4362 (declare-function remember
"remember" (&optional initial
))
4363 (declare-function remember-buffer-desc
"remember" ())
4364 (declare-function remember-finalize
"remember" ())
4365 (defvar remember-save-after-remembering
)
4366 (defvar remember-data-file
)
4367 (defvar remember-register
)
4368 (defvar remember-buffer
)
4369 (defvar remember-handler-functions
)
4370 (defvar remember-annotation-functions
)
4371 (declare-function rmail-narrow-to-non-pruned-header
"rmail" ())
4372 (declare-function rmail-show-message
"rmail" (&optional n no-summary
))
4373 (declare-function rmail-what-message
"rmail" ())
4374 (defvar rmail-current-message
)
4375 (defvar texmathp-why
)
4376 (declare-function vm-beginning-of-message
"ext:vm-page" ())
4377 (declare-function vm-follow-summary-cursor
"ext:vm-motion" ())
4378 (declare-function vm-get-header-contents
"ext:vm-summary" (message header-name-regexp
&optional clump-sep
))
4379 (declare-function vm-isearch-narrow
"ext:vm-search" ())
4380 (declare-function vm-isearch-update
"ext:vm-search" ())
4381 (declare-function vm-select-folder-buffer
"ext:vm-macro" ())
4382 (declare-function vm-su-message-id
"ext:vm-summary" (m))
4383 (declare-function vm-su-subject
"ext:vm-summary" (m))
4384 (declare-function vm-summarize
"ext:vm-summary" (&optional display raise
))
4385 (defvar vm-message-pointer
)
4386 (defvar vm-folder-directory
)
4387 (defvar w3m-current-url
)
4388 (defvar w3m-current-title
)
4389 ;; backward compatibility to old version of wl
4390 (declare-function wl-summary-buffer-msgdb
"ext:wl-folder" (&rest unknown
) t
)
4391 (declare-function wl-folder-get-elmo-folder
"ext:wl-folder" (entity &optional no-cache
))
4392 (declare-function wl-summary-goto-folder-subr
"ext:wl-summary" (&optional name scan-type other-window sticky interactive scoring force-exit
))
4393 (declare-function wl-summary-jump-to-msg-by-message-id
"ext:wl-summary" (&optional id
))
4394 (declare-function wl-summary-line-from
"ext:wl-summary" ())
4395 (declare-function wl-summary-line-subject
"ext:wl-summary" ())
4396 (declare-function wl-summary-message-number
"ext:wl-summary" ())
4397 (declare-function wl-summary-redisplay
"ext:wl-summary" (&optional arg
))
4398 (defvar wl-summary-buffer-elmo-folder
)
4399 (defvar wl-summary-buffer-folder-name
)
4400 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
4402 (defvar org-latex-regexps
)
4403 (defvar constants-unit-system
)
4405 ;;; Variables for pre-computed regular expressions, all buffer local
4407 (defvar org-drawer-regexp nil
4408 "Matches first line of a hidden block.")
4409 (make-variable-buffer-local 'org-drawer-regexp
)
4410 (defvar org-todo-regexp nil
4411 "Matches any of the TODO state keywords.")
4412 (make-variable-buffer-local 'org-todo-regexp
)
4413 (defvar org-not-done-regexp nil
4414 "Matches any of the TODO state keywords except the last one.")
4415 (make-variable-buffer-local 'org-not-done-regexp
)
4416 (defvar org-todo-line-regexp nil
4417 "Matches a headline and puts TODO state into group 2 if present.")
4418 (make-variable-buffer-local 'org-todo-line-regexp
)
4419 (defvar org-complex-heading-regexp nil
4420 "Matches a headline and puts everything into groups:
4422 group 2: The todo keyword, maybe
4423 group 3: Priority cookie
4424 group 4: True headline
4426 (make-variable-buffer-local 'org-complex-heading-regexp
)
4427 (defvar org-todo-line-tags-regexp nil
4428 "Matches a headline and puts TODO state into group 2 if present.
4429 Also put tags into group 4 if tags are present.")
4430 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4431 (defvar org-nl-done-regexp nil
4432 "Matches newline followed by a headline with the DONE keyword.")
4433 (make-variable-buffer-local 'org-nl-done-regexp
)
4434 (defvar org-looking-at-done-regexp nil
4435 "Matches the DONE keyword a point.")
4436 (make-variable-buffer-local 'org-looking-at-done-regexp
)
4437 (defvar org-ds-keyword-length
12
4438 "Maximum length of the Deadline and SCHEDULED keywords.")
4439 (make-variable-buffer-local 'org-ds-keyword-length
)
4440 (defvar org-deadline-regexp nil
4441 "Matches the DEADLINE keyword.")
4442 (make-variable-buffer-local 'org-deadline-regexp
)
4443 (defvar org-deadline-time-regexp nil
4444 "Matches the DEADLINE keyword together with a time stamp.")
4445 (make-variable-buffer-local 'org-deadline-time-regexp
)
4446 (defvar org-deadline-line-regexp nil
4447 "Matches the DEADLINE keyword and the rest of the line.")
4448 (make-variable-buffer-local 'org-deadline-line-regexp
)
4449 (defvar org-scheduled-regexp nil
4450 "Matches the SCHEDULED keyword.")
4451 (make-variable-buffer-local 'org-scheduled-regexp
)
4452 (defvar org-scheduled-time-regexp nil
4453 "Matches the SCHEDULED keyword together with a time stamp.")
4454 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4455 (defvar org-closed-time-regexp nil
4456 "Matches the CLOSED keyword together with a time stamp.")
4457 (make-variable-buffer-local 'org-closed-time-regexp
)
4459 (defvar org-keyword-time-regexp nil
4460 "Matches any of the 4 keywords, together with the time stamp.")
4461 (make-variable-buffer-local 'org-keyword-time-regexp
)
4462 (defvar org-keyword-time-not-clock-regexp nil
4463 "Matches any of the 3 keywords, together with the time stamp.")
4464 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4465 (defvar org-maybe-keyword-time-regexp nil
4466 "Matches a timestamp, possibly preceeded by a keyword.")
4467 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4468 (defvar org-planning-or-clock-line-re nil
4469 "Matches a line with planning or clock info.")
4470 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
4472 (defconst org-rm-props
'(invisible t face t keymap t intangible t mouse-face t
4473 rear-nonsticky t mouse-map t fontified t
)
4474 "Properties to remove when a string without properties is wanted.")
4476 (defsubst org-match-string-no-properties
(num &optional string
)
4477 (if (featurep 'xemacs
)
4478 (let ((s (match-string num string
)))
4479 (remove-text-properties 0 (length s
) org-rm-props s
)
4481 (match-string-no-properties num string
)))
4483 (defsubst org-no-properties
(s)
4484 (if (fboundp 'set-text-properties
)
4485 (set-text-properties 0 (length s
) nil s
)
4486 (remove-text-properties 0 (length s
) org-rm-props s
))
4489 (defsubst org-get-alist-option
(option key
)
4490 (cond ((eq key t
) t
)
4492 ((assoc key option
) (cdr (assoc key option
)))
4493 (t (cdr (assq 'default option
)))))
4495 (defsubst org-inhibit-invisibility
()
4496 "Modified `buffer-invisibility-spec' for Emacs 21.
4497 Some ops with invisible text do not work correctly on Emacs 21. For these
4498 we turn off invisibility temporarily. Use this in a `let' form."
4499 (if (< emacs-major-version
22) nil buffer-invisibility-spec
))
4501 (defsubst org-set-local
(var value
)
4502 "Make VAR local in current buffer and set it to VALUE."
4503 (set (make-variable-buffer-local var
) value
))
4505 (defsubst org-mode-p
()
4506 "Check if the current buffer is in Org-mode."
4507 (eq major-mode
'org-mode
))
4509 (defsubst org-last
(list)
4510 "Return the last element of LIST."
4513 (defun org-let (list &rest body
)
4514 (eval (cons 'let
(cons list body
))))
4515 (put 'org-let
'lisp-indent-function
1)
4517 (defun org-let2 (list1 list2
&rest body
)
4518 (eval (cons 'let
(cons list1
(list (cons 'let
(cons list2 body
)))))))
4519 (put 'org-let2
'lisp-indent-function
2)
4520 (defconst org-startup-options
4521 '(("fold" org-startup-folded t
)
4522 ("overview" org-startup-folded t
)
4523 ("nofold" org-startup-folded nil
)
4524 ("showall" org-startup-folded nil
)
4525 ("content" org-startup-folded content
)
4526 ("hidestars" org-hide-leading-stars t
)
4527 ("showstars" org-hide-leading-stars nil
)
4528 ("odd" org-odd-levels-only t
)
4529 ("oddeven" org-odd-levels-only nil
)
4530 ("align" org-startup-align-all-tables t
)
4531 ("noalign" org-startup-align-all-tables nil
)
4532 ("customtime" org-display-custom-times t
)
4533 ("logdone" org-log-done time
)
4534 ("lognotedone" org-log-done note
)
4535 ("nologdone" org-log-done nil
)
4536 ("lognoteclock-out" org-log-note-clock-out t
)
4537 ("nolognoteclock-out" org-log-note-clock-out nil
)
4538 ("logrepeat" org-log-repeat state
)
4539 ("lognoterepeat" org-log-repeat note
)
4540 ("nologrepeat" org-log-repeat nil
)
4541 ("constcgs" constants-unit-system cgs
)
4542 ("constSI" constants-unit-system SI
))
4543 "Variable associated with STARTUP options for org-mode.
4544 Each element is a list of three items: The startup options as written
4545 in the #+STARTUP line, the corresponding variable, and the value to
4546 set this variable to if the option is found. An optional forth element PUSH
4547 means to push this value onto the list in the variable.")
4549 (defun org-set-regexps-and-options ()
4550 "Precompute regular expressions for current buffer."
4552 (org-set-local 'org-todo-kwd-alist nil
)
4553 (org-set-local 'org-todo-key-alist nil
)
4554 (org-set-local 'org-todo-key-trigger nil
)
4555 (org-set-local 'org-todo-keywords-1 nil
)
4556 (org-set-local 'org-done-keywords nil
)
4557 (org-set-local 'org-todo-heads nil
)
4558 (org-set-local 'org-todo-sets nil
)
4559 (org-set-local 'org-todo-log-states nil
)
4560 (let ((re (org-make-options-regexp
4561 '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
4562 "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
4563 "CONSTANTS" "PROPERTY" "DRAWERS")))
4565 kwds kws0 kwsa key log value cat arch tags const links hw dws
4566 tail sep kws1 prio props drawers
)
4570 (goto-char (point-min))
4571 (while (re-search-forward re nil t
)
4572 (setq key
(match-string 1) value
(org-match-string-no-properties 2))
4574 ((equal key
"CATEGORY")
4575 (if (string-match "[ \t]+$" value
)
4576 (setq value
(replace-match "" t t value
)))
4578 ((member key
'("SEQ_TODO" "TODO"))
4579 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4580 ((equal key
"TYP_TODO")
4581 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4583 (setq tags
(append tags
(org-split-string value splitre
))))
4584 ((equal key
"COLUMNS")
4585 (org-set-local 'org-columns-default-format value
))
4587 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4588 (push (cons (match-string 1 value
)
4589 (org-trim (match-string 2 value
)))
4591 ((equal key
"PRIORITIES")
4592 (setq prio
(org-split-string value
" +")))
4593 ((equal key
"PROPERTY")
4594 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4595 (push (cons (match-string 1 value
) (match-string 2 value
))
4597 ((equal key
"DRAWERS")
4598 (setq drawers
(org-split-string value splitre
)))
4599 ((equal key
"CONSTANTS")
4600 (setq const
(append const
(org-split-string value splitre
))))
4601 ((equal key
"STARTUP")
4602 (let ((opts (org-split-string value splitre
))
4604 (while (setq l
(pop opts
))
4605 (when (setq l
(assoc l org-startup-options
))
4606 (setq var
(nth 1 l
) val
(nth 2 l
))
4608 (set (make-local-variable var
) val
)
4609 (if (not (listp (symbol-value var
)))
4610 (set (make-local-variable var
) nil
))
4611 (set (make-local-variable var
) (symbol-value var
))
4612 (add-to-list var val
))))))
4613 ((equal key
"ARCHIVE")
4614 (string-match " *$" value
)
4615 (setq arch
(replace-match "" t t value
))
4616 (remove-text-properties 0 (length arch
)
4617 '(face t fontified t
) arch
)))
4620 (org-set-local 'org-category
(intern cat
))
4621 (push (cons "CATEGORY" cat
) props
))
4623 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4624 (setq prio
(mapcar 'string-to-char prio
))
4625 (org-set-local 'org-highest-priority
(nth 0 prio
))
4626 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4627 (org-set-local 'org-default-priority
(nth 2 prio
)))
4628 (and props
(org-set-local 'org-local-properties
(nreverse props
)))
4629 (and drawers
(org-set-local 'org-drawers drawers
))
4630 (and arch
(org-set-local 'org-archive-location arch
))
4631 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4632 ;; Process the TODO keywords
4634 ;; Use the global values as if they had been given locally.
4635 (setq kwds
(default-value 'org-todo-keywords
))
4636 (if (stringp (car kwds
))
4637 (setq kwds
(list (cons org-todo-interpretation
4638 (default-value 'org-todo-keywords
)))))
4639 (setq kwds
(reverse kwds
)))
4640 (setq kwds
(nreverse kwds
))
4642 (while (setq kws
(pop kwds
))
4643 (setq inter
(pop kws
) sep
(member "|" kws
)
4644 kws0
(delete "|" (copy-sequence kws
))
4649 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4651 (setq kw
(match-string 1 x
)
4652 key
(and (match-end 2) (match-string 2 x
))
4653 log
(org-extract-log-state-settings x
))
4654 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4655 (and log
(push log org-todo-log-states
))
4657 (error "Invalid TODO keyword %s" x
)))
4659 kwsa
(if kwsa
(append '((:startgroup
))
4663 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4664 tail
(list inter hw
(car dws
) (org-last dws
)))
4665 (add-to-list 'org-todo-heads hw
'append
)
4666 (push kws1 org-todo-sets
)
4667 (setq org-done-keywords
(append org-done-keywords dws nil
))
4668 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4669 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4670 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4671 (setq org-todo-sets
(nreverse org-todo-sets
)
4672 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4673 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4674 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4675 ;; Process the constants
4678 (while (setq e
(pop const
))
4679 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4680 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4681 (setq org-table-formula-constants-local cst
)))
4683 ;; Process the tags.
4686 (while (setq e
(pop tags
))
4688 ((equal e
"{") (push '(:startgroup
) tgs
))
4689 ((equal e
"}") (push '(:endgroup
) tgs
))
4690 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
4691 (push (cons (match-string 1 e
)
4692 (string-to-char (match-string 2 e
)))
4694 (t (push (list e
) tgs
))))
4695 (org-set-local 'org-tag-alist nil
)
4696 (while (setq e
(pop tgs
))
4697 (or (and (stringp (car e
))
4698 (assoc (car e
) org-tag-alist
))
4699 (push e org-tag-alist
))))))
4701 ;; Compute the regular expressions and other local variables
4702 (if (not org-done-keywords
)
4703 (setq org-done-keywords
(list (org-last org-todo-keywords-1
))))
4704 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4705 (length org-scheduled-string
)))
4707 (concat "^[ \t]*:\\("
4708 (mapconcat 'regexp-quote org-drawers
"\\|")
4710 org-not-done-keywords
4711 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4713 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
4717 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4719 org-todo-line-regexp
4720 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4721 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4722 "\\)\\>\\)?[ \t]*\\(.*\\)")
4723 org-complex-heading-regexp
4724 (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
4725 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4726 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
4727 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4729 (concat "\n\\*+[ \t]+"
4730 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
4732 org-todo-line-tags-regexp
4733 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
4734 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4736 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
4737 org-looking-at-done-regexp
4739 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
4741 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4742 org-deadline-time-regexp
4743 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4744 org-deadline-line-regexp
4745 (concat "\\<\\(" org-deadline-string
"\\).*")
4746 org-scheduled-regexp
4747 (concat "\\<" org-scheduled-string
)
4748 org-scheduled-time-regexp
4749 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4750 org-closed-time-regexp
4751 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4752 org-keyword-time-regexp
4753 (concat "\\<\\(" org-scheduled-string
4754 "\\|" org-deadline-string
4755 "\\|" org-closed-string
4756 "\\|" org-clock-string
"\\)"
4757 " *[[<]\\([^]>]+\\)[]>]")
4758 org-keyword-time-not-clock-regexp
4759 (concat "\\<\\(" org-scheduled-string
4760 "\\|" org-deadline-string
4761 "\\|" org-closed-string
4763 " *[[<]\\([^]>]+\\)[]>]")
4764 org-maybe-keyword-time-regexp
4765 (concat "\\(\\<\\(" org-scheduled-string
4766 "\\|" org-deadline-string
4767 "\\|" org-closed-string
4768 "\\|" org-clock-string
"\\)\\)?"
4769 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4770 org-planning-or-clock-line-re
4771 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4772 "\\|" org-deadline-string
4773 "\\|" org-closed-string
"\\|" org-clock-string
4776 (org-compute-latex-and-specials-regexp)
4777 (org-set-font-lock-defaults)))
4779 (defun org-extract-log-state-settings (x)
4780 "Extract the log state setting from a TODO keyword string.
4781 This will extract info from a string like \"WAIT(w@/!)\"."
4782 (let (kw key log1 log2
)
4783 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4784 (setq kw
(match-string 1 x
)
4785 key
(and (match-end 2) (match-string 2 x
))
4786 log1
(and (match-end 3) (match-string 3 x
))
4787 log2
(and (match-end 4) (match-string 4 x
)))
4790 (and log1
(if (equal log1
"!") 'time
'note
))
4791 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4793 (defun org-remove-keyword-keys (list)
4794 "Remove a pair of parenthesis at the end of each string in LIST."
4796 (if (string-match "(.*)$" x
)
4797 (substring x
0 (match-beginning 0))
4801 ;; FIXME: this could be done much better, using second characters etc.
4802 (defun org-assign-fast-keys (alist)
4803 "Assign fast keys to a keyword-key alist.
4804 Respect keys that are already there."
4805 (let (new e k c c1 c2
(char ?a
))
4806 (while (setq e
(pop alist
))
4808 ((equal e
'(:startgroup
)) (push e new
))
4809 ((equal e
'(:endgroup
)) (push e new
))
4811 (setq k
(car e
) c2 nil
)
4814 ;; automatically assign a character.
4815 (setq c1
(string-to-char
4816 (downcase (substring
4817 k
(if (= (string-to-char k
) ?
@) 1 0)))))
4818 (if (or (rassoc c1 new
) (rassoc c1 alist
))
4819 (while (or (rassoc char new
) (rassoc char alist
))
4820 (setq char
(1+ char
)))
4822 (setq c
(or c2 char
)))
4823 (push (cons k c
) new
))))
4826 ;;; Some variables ujsed in various places
4828 (defvar org-window-configuration nil
4829 "Used in various places to store a window configuration.")
4830 (defvar org-finish-function nil
4831 "Function to be called when `C-c C-c' is used.
4832 This is for getting out of special buffers like remember.")
4835 ;; FIXME: Occasionally check by commenting these, to make sure
4836 ;; no other functions uses these, forgetting to let-bind them.
4841 (defvar description
)
4843 ;; Defined somewhere in this file, but used before definition.
4844 (defvar orgtbl-mode-menu
) ; defined when orgtbl mode get initialized
4845 (defvar org-agenda-buffer-name
)
4846 (defvar org-agenda-undo-list
)
4847 (defvar org-agenda-pending-undo-list
)
4848 (defvar org-agenda-overriding-header
)
4849 (defvar orgtbl-mode
)
4850 (defvar org-html-entities
)
4851 (defvar org-struct-menu
)
4852 (defvar org-org-menu
)
4853 (defvar org-tbl-menu
)
4854 (defvar org-agenda-keymap
)
4856 ;;;; Emacs/XEmacs compatibility
4858 ;; Overlay compatibility functions
4859 (defun org-make-overlay (beg end
&optional buffer
)
4860 (if (featurep 'xemacs
)
4861 (make-extent beg end buffer
)
4862 (make-overlay beg end buffer
)))
4863 (defun org-delete-overlay (ovl)
4864 (if (featurep 'xemacs
) (delete-extent ovl
) (delete-overlay ovl
)))
4865 (defun org-detach-overlay (ovl)
4866 (if (featurep 'xemacs
) (detach-extent ovl
) (delete-overlay ovl
)))
4867 (defun org-move-overlay (ovl beg end
&optional buffer
)
4868 (if (featurep 'xemacs
)
4869 (set-extent-endpoints ovl beg end
(or buffer
(current-buffer)))
4870 (move-overlay ovl beg end buffer
)))
4871 (defun org-overlay-put (ovl prop value
)
4872 (if (featurep 'xemacs
)
4873 (set-extent-property ovl prop value
)
4874 (overlay-put ovl prop value
)))
4875 (defun org-overlay-display (ovl text
&optional face evap
)
4876 "Make overlay OVL display TEXT with face FACE."
4877 (if (featurep 'xemacs
)
4878 (let ((gl (make-glyph text
)))
4879 (and face
(set-glyph-face gl face
))
4880 (set-extent-property ovl
'invisible t
)
4881 (set-extent-property ovl
'end-glyph gl
))
4882 (overlay-put ovl
'display text
)
4883 (if face
(overlay-put ovl
'face face
))
4884 (if evap
(overlay-put ovl
'evaporate t
))))
4885 (defun org-overlay-before-string (ovl text
&optional face evap
)
4886 "Make overlay OVL display TEXT with face FACE."
4887 (if (featurep 'xemacs
)
4888 (let ((gl (make-glyph text
)))
4889 (and face
(set-glyph-face gl face
))
4890 (set-extent-property ovl
'begin-glyph gl
))
4891 (if face
(org-add-props text nil
'face face
))
4892 (overlay-put ovl
'before-string text
)
4893 (if evap
(overlay-put ovl
'evaporate t
))))
4894 (defun org-overlay-get (ovl prop
)
4895 (if (featurep 'xemacs
)
4896 (extent-property ovl prop
)
4897 (overlay-get ovl prop
)))
4898 (defun org-overlays-at (pos)
4899 (if (featurep 'xemacs
) (extents-at pos
) (overlays-at pos
)))
4900 (defun org-overlays-in (&optional start end
)
4901 (if (featurep 'xemacs
)
4902 (extent-list nil start end
)
4903 (overlays-in start end
)))
4904 (defun org-overlay-start (o)
4905 (if (featurep 'xemacs
) (extent-start-position o
) (overlay-start o
)))
4906 (defun org-overlay-end (o)
4907 (if (featurep 'xemacs
) (extent-end-position o
) (overlay-end o
)))
4908 (defun org-find-overlays (prop &optional pos delete
)
4909 "Find all overlays specifying PROP at POS or point.
4910 If DELETE is non-nil, delete all those overlays."
4911 (let ((overlays (org-overlays-at (or pos
(point))))
4913 (while (setq ov
(pop overlays
))
4914 (if (org-overlay-get ov prop
)
4915 (if delete
(org-delete-overlay ov
) (push ov found
))))
4918 ;; Region compatibility
4920 (defun org-add-hook (hook function
&optional append local
)
4921 "Add-hook, compatible with both Emacsen."
4922 (if (and local
(featurep 'xemacs
))
4923 (add-local-hook hook function append
)
4924 (add-hook hook function append local
)))
4926 (defvar org-ignore-region nil
4927 "To temporarily disable the active region.")
4929 (defun org-region-active-p ()
4930 "Is `transient-mark-mode' on and the region active?
4931 Works on both Emacs and XEmacs."
4932 (if org-ignore-region
4934 (if (featurep 'xemacs
)
4935 (and zmacs-regions
(region-active-p))
4936 (if (fboundp 'use-region-p
)
4938 (and transient-mark-mode mark-active
))))) ; Emacs 22 and before
4940 ;; Invisibility compatibility
4942 (defun org-add-to-invisibility-spec (arg)
4943 "Add elements to `buffer-invisibility-spec'.
4944 See documentation for `buffer-invisibility-spec' for the kind of elements
4947 ((fboundp 'add-to-invisibility-spec
)
4948 (add-to-invisibility-spec arg
))
4949 ((or (null buffer-invisibility-spec
) (eq buffer-invisibility-spec t
))
4950 (setq buffer-invisibility-spec
(list arg
)))
4952 (setq buffer-invisibility-spec
4953 (cons arg buffer-invisibility-spec
)))))
4955 (defun org-remove-from-invisibility-spec (arg)
4956 "Remove elements from `buffer-invisibility-spec'."
4957 (if (fboundp 'remove-from-invisibility-spec
)
4958 (remove-from-invisibility-spec arg
)
4959 (if (consp buffer-invisibility-spec
)
4960 (setq buffer-invisibility-spec
4961 (delete arg buffer-invisibility-spec
)))))
4963 (defun org-in-invisibility-spec-p (arg)
4964 "Is ARG a member of `buffer-invisibility-spec'?"
4965 (if (consp buffer-invisibility-spec
)
4966 (member arg buffer-invisibility-spec
)
4969 ;;;; Define the Org-mode
4971 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4972 (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."))
4975 ;; We use a before-change function to check if a table might need
4977 (defvar org-table-may-need-update t
4978 "Indicates that a table might need an update.
4979 This variable is set by `org-before-change-function'.
4980 `org-table-align' sets it back to nil.")
4981 (defvar org-mode-map
)
4982 (defvar org-mode-hook nil
)
4983 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4984 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4985 (defvar org-table-buffer-is-an nil
)
4986 (defconst org-outline-regexp
"\\*+ ")
4989 (define-derived-mode org-mode outline-mode
"Org"
4990 "Outline-based notes management and organizer, alias
4991 \"Carsten's outline-mode for keeping track of everything.\"
4993 Org-mode develops organizational tasks around a NOTES file which
4994 contains information about projects as plain text. Org-mode is
4995 implemented on top of outline-mode, which is ideal to keep the content
4996 of large files well structured. It supports ToDo items, deadlines and
4997 time stamps, which magically appear in the diary listing of the Emacs
4998 calendar. Tables are easily created with a built-in table editor.
4999 Plain text URL-like links connect to websites, emails (VM), Usenet
5000 messages (Gnus), BBDB entries, and any files related to the project.
5001 For printing and sharing of notes, an Org-mode file (or a part of it)
5002 can be exported as a structured ASCII or HTML file.
5004 The following commands are available:
5008 ;; Get rid of Outline menus, they are not needed
5009 ;; Need to do this here because define-derived-mode sets up
5010 ;; the keymap so late. Still, it is a waste to call this each time
5011 ;; we switch another buffer into org-mode.
5012 (if (featurep 'xemacs
)
5013 (when (boundp 'outline-mode-menu-heading
)
5014 ;; Assume this is Greg's port, it used easymenu
5015 (easy-menu-remove outline-mode-menu-heading
)
5016 (easy-menu-remove outline-mode-menu-show
)
5017 (easy-menu-remove outline-mode-menu-hide
))
5018 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
5019 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
5020 (define-key org-mode-map
[menu-bar show
] 'undefined
))
5022 (easy-menu-add org-org-menu
)
5023 (easy-menu-add org-tbl-menu
)
5024 (org-install-agenda-files-menu)
5025 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
5026 (org-add-to-invisibility-spec '(org-cwidth))
5027 (when (featurep 'xemacs
)
5028 (org-set-local 'line-move-ignore-invisible t
))
5029 (org-set-local 'outline-regexp org-outline-regexp
)
5030 (org-set-local 'outline-level
'org-outline-level
)
5031 (when (and org-ellipsis
5032 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5033 (fboundp 'make-glyph-code
))
5034 (unless org-display-table
5035 (setq org-display-table
(make-display-table)))
5036 (set-display-table-slot
5039 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5041 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5042 (setq buffer-display-table org-display-table
))
5043 (org-set-regexps-and-options)
5045 (org-set-local 'calc-embedded-open-mode
"# ")
5046 (modify-syntax-entry ?
# "<")
5047 (modify-syntax-entry ?
@ "w")
5048 (if org-startup-truncated
(setq truncate-lines t
))
5049 (org-set-local 'font-lock-unfontify-region-function
5050 'org-unfontify-region
)
5051 ;; Activate before-change-function
5052 (org-set-local 'org-table-may-need-update t
)
5053 (org-add-hook 'before-change-functions
'org-before-change-function nil
5055 ;; Check for running clock before killing a buffer
5056 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5057 ;; Paragraphs and auto-filling
5058 (org-set-autofill-regexps)
5059 (setq indent-line-function
'org-indent-line-function
)
5060 (org-update-radio-target-regexp)
5062 ;; Comment characters
5063 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
5064 (org-set-local 'comment-padding
" ")
5066 ;; Align options lines
5068 'align-mode-rules-list
5069 '((org-in-buffer-settings
5070 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5071 (modes .
'(org-mode)))))
5074 (org-set-local 'imenu-create-index-function
5075 'org-imenu-get-tree
)
5077 ;; Make isearch reveal context
5078 (if (or (featurep 'xemacs
)
5079 (not (boundp 'outline-isearch-open-invisible-function
)))
5080 ;; Emacs 21 and XEmacs make use of the hook
5081 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5082 ;; Emacs 22 deals with this through a special variable
5083 (org-set-local 'outline-isearch-open-invisible-function
5084 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5086 ;; If empty file that did not turn on org-mode automatically, make it to.
5087 (if (and org-insert-mode-line-in-empty-file
5089 (= (point-min) (point-max)))
5090 (insert "# -*- mode: org -*-\n\n"))
5092 (unless org-inhibit-startup
5093 (when org-startup-align-all-tables
5094 (let ((bmp (buffer-modified-p)))
5095 (org-table-map-tables 'org-table-align
)
5096 (set-buffer-modified-p bmp
)))
5097 (org-cycle-hide-drawers 'all
)
5099 ((eq org-startup-folded t
)
5101 ((eq org-startup-folded
'content
)
5102 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
5103 (org-cycle '(4)) (org-cycle '(4)))))))
5105 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5107 (defsubst org-call-with-arg
(command arg
)
5108 "Call COMMAND interactively, but pretend prefix are was ARG."
5109 (let ((current-prefix-arg arg
)) (call-interactively command
)))
5111 (defsubst org-current-line
(&optional pos
)
5113 (and pos
(goto-char pos
))
5114 ;; works also in narrowed buffer, because we start at 1, not point-min
5115 (+ (if (bolp) 1 0) (count-lines 1 (point)))))
5117 (defun org-current-time ()
5118 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5119 (if (> (car org-time-stamp-rounding-minutes
) 1)
5120 (let ((r (car org-time-stamp-rounding-minutes
))
5121 (time (decode-time)))
5123 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5127 (defun org-add-props (string plist
&rest props
)
5128 "Add text properties to entire string, from beginning to end.
5129 PLIST may be a list of properties, PROPS are individual properties and values
5130 that will be added to PLIST. Returns the string that was modified."
5131 (add-text-properties
5132 0 (length string
) (if props
(append plist props
) plist
) string
)
5134 (put 'org-add-props
'lisp-indent-function
2)
5137 ;;;; Font-Lock stuff, including the activators
5139 (defvar org-mouse-map
(make-sparse-keymap))
5140 (org-defkey org-mouse-map
5141 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
5142 (org-defkey org-mouse-map
5143 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
5144 (when org-mouse-1-follows-link
5145 (org-defkey org-mouse-map [follow-link] 'mouse-face))
5146 (when org-tab-follows-link
5147 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
5148 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
5149 (when org-return-follows-link
5150 (org-defkey org-mouse-map [(return)] 'org-open-at-point)
5151 (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
5153 (require 'font-lock)
5155 (defconst org-non-link-chars "]\t\n\r<>")
5156 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news" "bbdb" "vm"
5157 "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp" "message"))
5158 (defvar org-link-re-with-space nil
5159 "Matches a link with spaces, optional angular brackets around it.")
5160 (defvar org-link-re-with-space2 nil
5161 "Matches a link with spaces, optional angular brackets around it.")
5162 (defvar org-angle-link-re nil
5163 "Matches link with angular brackets, spaces are allowed.")
5164 (defvar org-plain-link-re nil
5165 "Matches plain link, without spaces.")
5166 (defvar org-bracket-link-regexp nil
5167 "Matches a link in double brackets.")
5168 (defvar org-bracket-link-analytic-regexp nil
5169 "Regular expression used to analyze links.
5170 Here is what the match groups contain after a match:
5176 (defvar org-any-link-re nil
5177 "Regular expression matching any link.")
5179 (defun org-make-link-regexps ()
5180 "Update the link regular expressions.
5181 This should be called after the variable `org-link-types' has changed."
5182 (setq org-link-re-with-space
5184 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5185 "\\([^" org-non-link-chars " ]"
5186 "[^" org-non-link-chars "]*"
5187 "[^" org-non-link-chars " ]\\)>?")
5188 org-link-re-with-space2
5190 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5191 "\\([^" org-non-link-chars " ]"
5193 "[^" org-non-link-chars " ]\\)>?")
5196 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5197 "\\([^" org-non-link-chars " ]"
5198 "[^" org-non-link-chars "]*"
5202 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
5203 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5204 org-bracket-link-regexp
5205 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5206 org-bracket-link-analytic-regexp
5209 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
5212 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5215 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
5216 org-angle-link-re "\\)\\|\\("
5217 org-plain-link-re "\\)")))
5219 (org-make-link-regexps)
5221 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
5222 "Regular expression for fast time stamp matching.")
5223 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
5224 "Regular expression for fast time stamp matching.")
5225 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5226 "Regular expression matching time strings for analysis.
5227 This one does not require the space after the date.")
5228 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5229 "Regular expression matching time strings for analysis.")
5230 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
5231 "Regular expression matching time stamps, with groups.")
5232 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
5233 "Regular expression matching time stamps (also [..]), with groups.")
5234 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
5235 "Regular expression matching a time stamp range.")
5236 (defconst org-tr-regexp-both
5237 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
5238 "Regular expression matching a time stamp range.")
5239 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
5240 org-ts-regexp "\\)?")
5241 "Regular expression matching a time stamp or time stamp range.")
5242 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
5243 org-ts-regexp-both "\\)?")
5244 "Regular expression matching a time stamp or time stamp range.
5245 The time stamps may be either active or inactive.")
5247 (defvar org-emph-face nil)
5249 (defun org-do-emphasis-faces (limit)
5250 "Run through the buffer and add overlays to links."
5252 (while (and (not rtn) (re-search-forward org-emph-re limit t))
5253 (if (not (= (char-after (match-beginning 3))
5254 (char-after (match-beginning 4))))
5257 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5259 (nth 1 (assoc (match-string 3)
5260 org-emphasis-alist)))
5261 (add-text-properties (match-beginning 2) (match-end 2)
5262 '(font-lock-multiline t))
5263 (when org-hide-emphasis-markers
5264 (add-text-properties (match-end 4) (match-beginning 5)
5265 '(invisible org-link))
5266 (add-text-properties (match-beginning 3) (match-end 3)
5267 '(invisible org-link)))))
5271 (defun org-emphasize (&optional char)
5272 "Insert or change an emphasis, i.e. a font like bold or italic.
5273 If there is an active region, change that region to a new emphasis.
5274 If there is no region, just insert the marker characters and position
5275 the cursor between them.
5276 CHAR should be either the marker character, or the first character of the
5277 HTML tag associated with that emphasis. If CHAR is a space, the means
5278 to remove the emphasis of the selected region.
5279 If char is not given (for example in an interactive call) it
5280 will be prompted for."
5282 (let ((eal org-emphasis-alist) e det
5283 (erc org-emphasis-regexp-components)
5285 (string "") beg end move tag c s)
5286 (if (org-region-active-p)
5287 (setq beg (region-beginning) end (region-end)
5288 string (buffer-substring beg end))
5291 (while (setq e (pop eal))
5292 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
5294 (push (cons c (string-to-char (car e))) det)
5295 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
5296 (substring tag 1)))))
5298 (message "%s" (concat "Emphasis marker or tag:" prompt))
5299 (setq char (read-char-exclusive)))
5300 (setq char (or (cdr (assoc char det)) char))
5301 (if (equal char ?\ )
5302 (setq s "" move nil)
5303 (unless (assoc (char-to-string char) org-emphasis-alist)
5304 (error "No such emphasis marker: \"%c\"" char))
5305 (setq s (char-to-string char)))
5306 (while (and (> (length string) 1)
5307 (equal (substring string 0 1) (substring string -1))
5308 (assoc (substring string 0 1) org-emphasis-alist))
5309 (setq string (substring string 1 -1)))
5310 (setq string (concat s string s))
5311 (if beg (delete-region beg end))
5313 (string-match (concat "[" (nth 0 erc) "\n]")
5314 (char-to-string (char-before (point)))))
5316 (unless (string-match (concat "[" (nth 1 erc) "\n]")
5317 (char-to-string (char-after (point))))
5318 (insert " ") (backward-char 1))
5320 (and move (backward-char 1))))
5322 (defconst org-nonsticky-props
5323 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
5326 (defun org-activate-plain-links (limit)
5327 "Run through the buffer and add overlays to links."
5330 (while (re-search-forward org-plain-link-re limit t)
5331 (setq f (get-text-property (match-beginning 0) 'face))
5332 (if (or (eq f 'org-tag)
5333 (and (listp f) (memq 'org-tag f)))
5335 (add-text-properties (match-beginning 0) (match-end 0)
5336 (list 'mouse-face 'highlight
5337 'rear-nonsticky org-nonsticky-props
5338 'keymap org-mouse-map
5340 (throw 'exit t))))))
5342 (defun org-activate-code (limit)
5343 (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
5344 (unless (get-text-property (match-beginning 1) 'face)
5345 (remove-text-properties (match-beginning 0) (match-end 0)
5346 '(display t invisible t intangible t))
5349 (defun org-activate-angle-links (limit)
5350 "Run through the buffer and add overlays to links."
5351 (if (re-search-forward org-angle-link-re limit t)
5353 (add-text-properties (match-beginning 0) (match-end 0)
5354 (list 'mouse-face 'highlight
5355 'rear-nonsticky org-nonsticky-props
5356 'keymap org-mouse-map
5360 (defmacro org-maybe-intangible (props)
5361 "Add '(intangigble t) to PROPS if Emacs version is earlier than Emacs 22.
5362 In emacs 21, invisible text is not avoided by the command loop, so the
5363 intangible property is needed to make sure point skips this text.
5364 In Emacs 22, this is not necessary. The intangible text property has
5365 led to problems with flyspell. These problems are fixed in flyspell.el,
5366 but we still avoid setting the property in Emacs 22 and later.
5367 We use a macro so that the test can happen at compilation time."
5368 (if (< emacs-major-version 22)
5369 `(append '(intangible t) ,props)
5372 (defun org-activate-bracket-links (limit)
5373 "Run through the buffer and add overlays to bracketed links."
5374 (if (re-search-forward org-bracket-link-regexp limit t)
5375 (let* ((help (concat "LINK: "
5376 (org-match-string-no-properties 1)))
5377 ;; FIXME: above we should remove the escapes.
5378 ;; but that requires another match, protecting match data,
5379 ;; a lot of overhead for font-lock.
5380 (ip (org-maybe-intangible
5381 (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
5382 'keymap org-mouse-map 'mouse-face 'highlight
5383 'font-lock-multiline t 'help-echo help)))
5384 (vp (list 'rear-nonsticky org-nonsticky-props
5385 'keymap org-mouse-map 'mouse-face 'highlight
5386 ' font-lock-multiline t 'help-echo help)))
5387 ;; We need to remove the invisible property here. Table narrowing
5388 ;; may have made some of this invisible.
5389 (remove-text-properties (match-beginning 0) (match-end 0)
5393 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
5394 (add-text-properties (match-beginning 3) (match-end 3) vp)
5395 (add-text-properties (match-end 3) (match-end 0) ip))
5396 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
5397 (add-text-properties (match-beginning 1) (match-end 1) vp)
5398 (add-text-properties (match-end 1) (match-end 0) ip))
5401 (defun org-activate-dates (limit)
5402 "Run through the buffer and add overlays to dates."
5403 (if (re-search-forward org-tsr-regexp-both limit t)
5405 (add-text-properties (match-beginning 0) (match-end 0)
5406 (list 'mouse-face 'highlight
5407 'rear-nonsticky org-nonsticky-props
5408 'keymap org-mouse-map))
5409 (when org-display-custom-times
5411 (org-display-custom-time (match-beginning 3) (match-end 3)))
5412 (org-display-custom-time (match-beginning 1) (match-end 1)))
5415 (defvar org-target-link-regexp nil
5416 "Regular expression matching radio targets in plain text.")
5417 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
5418 "Regular expression matching a link target.")
5419 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
5420 "Regular expression matching a radio target.")
5421 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5422 "Regular expression matching any target.")
5424 (defun org-activate-target-links (limit)
5425 "Run through the buffer and add overlays to target matches."
5426 (when org-target-link-regexp
5427 (let ((case-fold-search t))
5428 (if (re-search-forward org-target-link-regexp limit t)
5430 (add-text-properties (match-beginning 0) (match-end 0)
5431 (list 'mouse-face 'highlight
5432 'rear-nonsticky org-nonsticky-props
5433 'keymap org-mouse-map
5434 'help-echo "Radio target link"
5435 'org-linked-text t))
5438 (defun org-update-radio-target-regexp ()
5439 "Find all radio targets in this file and update the regular expression."
5441 (when (memq 'radio org-activate-links)
5442 (setq org-target-link-regexp
5443 (org-make-target-link-regexp (org-all-targets 'radio)))
5444 (org-restart-font-lock)))
5446 (defun org-hide-wide-columns (limit)
5448 (setq s (text-property-any (point) (or limit (point-max))
5451 (setq e (next-single-property-change s 'org-cwidth))
5452 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
5456 (defvar org-latex-and-specials-regexp nil
5457 "Regular expression for highlighting export special stuff.")
5458 (defvar org-match-substring-regexp)
5459 (defvar org-match-substring-with-braces-regexp)
5460 (defvar org-export-html-special-string-regexps)
5462 (defun org-compute-latex-and-specials-regexp ()
5463 "Compute regular expression for stuff treated specially by exporters."
5464 (if (not org-highlight-latex-fragments-and-specials)
5465 (org-set-local 'org-latex-and-specials-regexp nil)
5467 ((matchers (plist-get org-format-latex-options :matchers))
5468 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
5469 org-latex-regexps)))
5470 (options (org-combine-plists (org-default-export-plist)
5471 (org-infile-export-plist)))
5472 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
5473 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
5474 (org-export-with-TeX-macros (plist-get options :TeX-macros))
5475 (org-export-html-expand (plist-get options :expand-quoted-html))
5476 (org-export-with-special-strings (plist-get options :special-strings))
5479 ((equal org-export-with-sub-superscripts '{})
5480 (list org-match-substring-with-braces-regexp))
5481 (org-export-with-sub-superscripts
5482 (list org-match-substring-regexp))
5485 (if org-export-with-LaTeX-fragments
5486 (mapcar (lambda (x) (nth 1 x)) latexs)))
5488 (if org-export-with-TeX-macros
5489 (list (concat "\\\\"
5491 (append (mapcar 'car org-html-entities)
5492 (if (boundp 'org-latex-entities)
5493 org-latex-entities nil))
5496 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5497 (re-special (if org-export-with-special-strings
5498 (mapcar (lambda (x) (car x))
5499 org-export-html-special-string-regexps)))
5503 (if org-export-html-expand "@<[^>\n]+>")
5506 'org-latex-and-specials-regexp
5507 (mapconcat 'identity (append re-latex re-sub re-macros re-special
5510 (defface org-latex-and-export-specials
5511 (let ((font (cond ((assq :inherit custom-face-attributes)
5512 '(:inherit underline))
5513 (t '(:underline t)))))
5514 `((((class grayscale) (background light))
5515 (:foreground "DimGray" ,@font))
5516 (((class grayscale) (background dark))
5517 (:foreground "LightGray" ,@font))
5518 (((class color) (background light))
5519 (:foreground "SaddleBrown"))
5520 (((class color) (background dark))
5521 (:foreground "burlywood"))
5523 "Face used to highlight math latex and other special exporter stuff."
5526 (defun org-do-latex-and-special-faces (limit)
5527 "Run through the buffer and add overlays to links."
5528 (when org-latex-and-specials-regexp
5530 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
5532 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5534 '(org-code org-verbatim underline)))
5537 d (cond ((member (char-after (1+ (match-beginning 0)))
5540 (font-lock-prepend-text-property
5541 (+ d (match-beginning 0)) (match-end 0)
5542 'face 'org-latex-and-export-specials)
5543 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
5544 '(font-lock-multiline t)))))
5547 (defun org-restart-font-lock ()
5548 "Restart font-lock-mode, to force refontification."
5549 (when (and (boundp 'font-lock-mode) font-lock-mode)
5551 (font-lock-mode 1)))
5553 (defun org-all-targets (&optional radio)
5554 "Return a list of all targets in this file.
5555 With optional argument RADIO, only find radio targets."
5556 (let ((re (if radio org-radio-target-regexp org-target-regexp))
5559 (goto-char (point-min))
5560 (while (re-search-forward re nil t)
5561 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
5564 (defun org-make-target-link-regexp (targets)
5565 "Make regular expression matching all strings in TARGETS.
5566 The regular expression finds the targets also if there is a line break
5573 (while (string-match " +" x)
5574 (setq x (replace-match "\\s-+" t t x)))
5580 (defun org-activate-tags (limit)
5581 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
5583 (add-text-properties (match-beginning 1) (match-end 1)
5584 (list 'mouse-face 'highlight
5585 'rear-nonsticky org-nonsticky-props
5586 'keymap org-mouse-map))
5589 (defun org-outline-level ()
5591 (looking-at outline-regexp)
5592 (if (match-beginning 1)
5593 (+ (org-get-string-indentation (match-string 1)) 1000)
5594 (1- (- (match-end 0) (match-beginning 0))))))
5596 (defvar org-font-lock-keywords nil)
5598 (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
5599 "Regular expression matching a property line.")
5601 (defun org-set-font-lock-defaults ()
5602 (let* ((em org-fontify-emphasized-text)
5603 (lk org-activate-links)
5604 (org-font-lock-extra-keywords
5607 '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
5608 (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
5610 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5613 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
5614 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
5615 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
5617 (list org-drawer-regexp '(0 'org-special-keyword t))
5618 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
5620 (list org-property-re
5621 '(1 'org-special-keyword t)
5622 '(3 'org-property-value t))
5623 (if org-format-transports-properties-p
5624 '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
5626 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
5627 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
5628 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
5629 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
5630 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
5631 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
5632 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
5633 '(org-hide-wide-columns (0 nil append))
5635 (list (concat "^\\*+[ \t]+" org-todo-regexp)
5636 '(1 (org-get-todo-face 1) t))
5638 (if org-fontify-done-headline
5639 (list (concat "^[*]+ +\\<\\("
5640 (mapconcat 'regexp-quote org-done-keywords "\\|")
5642 '(2 'org-headline-done t))
5645 (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
5647 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
5648 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
5649 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
5650 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
5653 (if (featurep 'xemacs)
5654 '(org-do-emphasis-faces (0 nil append))
5655 '(org-do-emphasis-faces)))
5657 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
5659 (if org-provide-checkbox-statistics
5660 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5661 (0 (org-get-checkbox-statistics-face) t)))
5662 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
5663 '(1 'org-archived prepend))
5665 '(org-do-latex-and-special-faces)
5667 '(org-activate-code (1 'org-code t))
5669 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
5670 "\\|" org-quote-string "\\)\\>")
5671 '(1 'org-special-keyword t))
5672 '("^#.*" (0 'font-lock-comment-face t))
5674 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
5675 ;; Now set the full font-lock-keywords
5676 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
5677 (org-set-local 'font-lock-defaults
5678 '(org-font-lock-keywords t nil nil backward-paragraph))
5679 (kill-local-variable 'font-lock-keywords) nil))
5684 (defun org-get-level-face (n)
5685 "Get the right face for match N in font-lock matching of healdines."
5686 (setq org-l (- (match-end 2) (match-beginning 1) 1))
5687 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
5688 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
5690 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
5692 (t (if org-level-color-stars-only nil org-f))))
5694 (defun org-get-todo-face (kwd)
5695 "Get the right face for a TODO keyword KWD.
5696 If KWD is a number, get the corresponding match group."
5697 (if (numberp kwd) (setq kwd (match-string kwd)))
5698 (or (cdr (assoc kwd org-todo-keyword-faces))
5699 (and (member kwd org-done-keywords) 'org-done)
5702 (defun org-unfontify-region (beg end &optional maybe_loudly)
5703 "Remove fontification and activation overlays from links."
5704 (font-lock-default-unfontify-region beg end)
5705 (let* ((buffer-undo-list t)
5706 (inhibit-read-only t) (inhibit-point-motion-hooks t)
5707 (inhibit-modification-hooks t)
5708 deactivate-mark buffer-file-name buffer-file-truename)
5709 (remove-text-properties beg end
5710 '(mouse-face t keymap t org-linked-text t
5711 invisible t intangible t))))
5713 ;;;; Visibility cycling, including org-goto and indirect buffer
5717 (defvar org-cycle-global-status nil)
5718 (make-variable-buffer-local 'org-cycle-global-status)
5719 (defvar org-cycle-subtree-status nil)
5720 (make-variable-buffer-local 'org-cycle-subtree-status)
5723 (defun org-cycle (&optional arg)
5724 "Visibility cycling for Org-mode.
5726 - When this function is called with a prefix argument, rotate the entire
5727 buffer through 3 states (global cycling)
5728 1. OVERVIEW: Show only top-level headlines.
5729 2. CONTENTS: Show all headlines of all levels, but no body text.
5730 3. SHOW ALL: Show everything.
5732 - When point is at the beginning of a headline, rotate the subtree started
5733 by this line through 3 different states (local cycling)
5734 1. FOLDED: Only the main headline is shown.
5735 2. CHILDREN: The main headline and the direct children are shown.
5736 From this state, you can move to one of the children
5737 and zoom in further.
5738 3. SUBTREE: Show the entire subtree, including body text.
5740 - When there is a numeric prefix, go up to a heading with level ARG, do
5741 a `show-subtree' and return to the previous cursor position. If ARG
5742 is negative, go up that many levels.
5744 - When point is not at the beginning of a headline, execute
5745 `indent-relative', like TAB normally does. See the option
5746 `org-cycle-emulate-tab' for details.
5748 - Special case: if point is at the beginning of the buffer and there is
5749 no headline in line 1, this function will act as if called with prefix arg.
5750 But only if also the variable `org-cycle-global-at-bob' is t."
5752 (let* ((outline-regexp
5753 (if (and (org-mode-p) org-cycle-include-plain-lists)
5754 "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
5756 (bob-special (and org-cycle-global-at-bob (bobp)
5757 (not (looking-at outline-regexp))))
5760 (delq 'org-optimize-window-after-visibility-change
5761 (copy-sequence org-cycle-hook))
5765 (if (or bob-special (equal arg '(4)))
5766 ;; special case: use global cycling
5771 ((org-at-table-p 'any)
5772 ;; Enter the table or move to the next field in the table
5773 (or (org-table-recognize-table.el)
5775 (if arg (org-table-edit-field t)
5776 (org-table-justify-field-maybe)
5777 (call-interactively 'org-table-next-field)))))
5779 ((eq arg t) ;; Global cycling
5782 ((and (eq last-command this-command)
5783 (eq org-cycle-global-status 'overview))
5784 ;; We just created the overview - now do table of contents
5785 ;; This can be slow in very large buffers, so indicate action
5786 (message "CONTENTS...")
5788 (message "CONTENTS...done")
5789 (setq org-cycle-global-status 'contents)
5790 (run-hook-with-args 'org-cycle-hook 'contents))
5792 ((and (eq last-command this-command)
5793 (eq org-cycle-global-status 'contents))
5794 ;; We just showed the table of contents - now show everything
5796 (message "SHOW ALL")
5797 (setq org-cycle-global-status 'all)
5798 (run-hook-with-args 'org-cycle-hook 'all))
5801 ;; Default action: go to overview
5803 (message "OVERVIEW")
5804 (setq org-cycle-global-status 'overview)
5805 (run-hook-with-args 'org-cycle-hook 'overview))))
5807 ((and org-drawers org-drawer-regexp
5809 (beginning-of-line 1)
5810 (looking-at org-drawer-regexp)))
5811 ;; Toggle block visibility
5813 (not (get-char-property (match-end 0) 'invisible))))
5816 ;; Show-subtree, ARG levels up from here.
5818 (org-back-to-heading)
5819 (outline-up-heading (if (< arg 0) (- arg)
5820 (- (funcall outline-level) arg)))
5821 (org-show-subtree)))
5823 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
5824 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
5825 ;; At a heading: rotate between three different views
5826 (org-back-to-heading)
5827 (let ((goal-column 0) eoh eol eos)
5828 ;; First, some boundaries
5830 (org-back-to-heading)
5832 (beginning-of-line 2)
5833 (while (and (not (eobp)) ;; this is like `next-line'
5834 (get-char-property (1- (point)) 'invisible))
5835 (beginning-of-line 2)) (setq eol (point)))
5836 (outline-end-of-heading) (setq eoh (point))
5837 (org-end-of-subtree t)
5839 (skip-chars-forward " \t\n")
5840 (beginning-of-line 1) ; in case this is an item
5842 (setq eos (1- (point))))
5843 ;; Find out what to do next and set `this-command'
5846 ;; Nothing is hidden behind this heading
5847 (message "EMPTY ENTRY")
5848 (setq org-cycle-subtree-status nil)
5851 (outline-next-heading)
5852 (if (org-invisible-p) (org-flag-heading nil))))
5854 (not (string-match "\\S-" (buffer-substring eol eos))))
5855 ;; Entire subtree is hidden in one line: open it
5858 (message "CHILDREN")
5861 (outline-next-heading)
5862 (if (org-invisible-p) (org-flag-heading nil)))
5863 (setq org-cycle-subtree-status 'children)
5864 (run-hook-with-args 'org-cycle-hook 'children))
5865 ((and (eq last-command this-command)
5866 (eq org-cycle-subtree-status 'children))
5867 ;; We just showed the children, now show everything.
5870 (setq org-cycle-subtree-status 'subtree)
5871 (run-hook-with-args 'org-cycle-hook 'subtree))
5873 ;; Default action: hide the subtree.
5876 (setq org-cycle-subtree-status 'folded)
5877 (run-hook-with-args 'org-cycle-hook 'folded)))))
5880 (buffer-read-only (org-back-to-heading))
5882 ((org-try-cdlatex-tab))
5884 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
5886 (not (looking-at outline-regexp))))
5887 (call-interactively (global-key-binding "\t")))
5889 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5890 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5891 (or (and (eq org-cycle-emulate-tab 'white)
5892 (= (match-end 0) (point-at-eol)))
5893 (and (eq org-cycle-emulate-tab 'whitestart)
5894 (>= (match-end 0) pos))))
5896 (eq org-cycle-emulate-tab t))
5897 ; (if (and (looking-at "[ \n\r\t]")
5898 ; (string-match "^[ \t]*$" (buffer-substring
5899 ; (point-at-bol) (point))))
5901 ; (beginning-of-line 1)
5902 ; (and (looking-at "[ \t]+") (replace-match ""))))
5903 (call-interactively (global-key-binding "\t")))
5906 (org-back-to-heading)
5910 (defun org-global-cycle (&optional arg)
5911 "Cycle the global visibility. For details see `org-cycle'."
5913 (let ((org-cycle-include-plain-lists
5914 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5918 (hide-sublevels arg)
5919 (setq org-cycle-global-status 'contents))
5922 (defun org-overview ()
5923 "Switch to overview mode, shoing only top-level headlines.
5924 Really, this shows all headlines with level equal or greater than the level
5925 of the first headline in the buffer. This is important, because if the
5926 first headline is not level one, then (hide-sublevels 1) gives confusing
5929 (let ((level (save-excursion
5930 (goto-char (point-min))
5931 (if (re-search-forward (concat "^" outline-regexp) nil t)
5933 (goto-char (match-beginning 0))
5934 (funcall outline-level))))))
5935 (and level (hide-sublevels level))))
5937 (defun org-content (&optional arg)
5938 "Show all headlines in the buffer, like a table of contents.
5939 With numerical argument N, show content up to level N."
5942 ;; Visit all headings and show their offspring
5943 (and (integerp arg) (org-overview))
5944 (goto-char (point-max))
5946 (while (and (progn (condition-case nil
5947 (outline-previous-visible-heading 1)
5948 (error (goto-char (point-min))))
5950 (looking-at outline-regexp))
5952 (show-children (1- arg))
5954 (if (bobp) (throw 'exit nil))))))
5957 (defun org-optimize-window-after-visibility-change (state)
5958 "Adjust the window after a change in outline visibility.
5959 This function is the default value of the hook `org-cycle-hook'."
5960 (when (get-buffer-window (current-buffer))
5962 ; ((eq state 'overview) (org-first-headline-recenter 1))
5963 ; ((eq state 'overview) (org-beginning-of-line))
5964 ((eq state 'content) nil)
5965 ((eq state 'all) nil)
5966 ((eq state 'folded) nil)
5967 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5968 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5970 (defun org-compact-display-after-subtree-move ()
5973 (if (org-up-heading-safe)
5978 (org-cycle-show-empty-lines 'children)
5979 (org-cycle-hide-drawers 'children))
5982 (defun org-cycle-show-empty-lines (state)
5983 "Show empty lines above all visible headlines.
5984 The region to be covered depends on STATE when called through
5985 `org-cycle-hook'. Lisp program can use t for STATE to get the
5986 entire buffer covered. Note that an empty line is only shown if there
5987 are at least `org-cycle-separator-lines' empty lines before the headeline."
5988 (when (> org-cycle-separator-lines 0)
5990 (let* ((n org-cycle-separator-lines)
5992 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5993 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5994 (t (let ((ns (number-to-string (- n 2))))
5995 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5996 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5999 ((memq state '(overview contents t))
6000 (setq beg (point-min) end (point-max)))
6001 ((memq state '(children folded))
6002 (setq beg (point) end (progn (org-end-of-subtree t t)
6003 (beginning-of-line 2)
6007 (while (re-search-forward re end t)
6008 (if (not (get-char-property (match-end 1) 'invisible))
6009 (outline-flag-region
6010 (match-beginning 1) (match-end 1) nil)))))))
6011 ;; Never hide empty lines at the end of the file.
6013 (goto-char (point-max))
6014 (outline-previous-heading)
6015 (outline-end-of-heading)
6016 (if (and (looking-at "[ \t\n]+")
6017 (= (match-end 0) (point-max)))
6018 (outline-flag-region (point) (match-end 0) nil))))
6020 (defun org-subtree-end-visible-p ()
6021 "Is the end of the current subtree visible?"
6022 (pos-visible-in-window-p
6023 (save-excursion (org-end-of-subtree t) (point))))
6025 (defun org-first-headline-recenter (&optional N)
6026 "Move cursor to the first headline and recenter the headline.
6027 Optional argument N means, put the headline into the Nth line of the window."
6028 (goto-char (point-min))
6029 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
6031 (recenter (prefix-numeric-value N))))
6035 (defvar org-goto-window-configuration nil)
6036 (defvar org-goto-marker nil)
6037 (defvar org-goto-map
6038 (let ((map (make-sparse-keymap)))
6039 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
6040 (while (setq cmd (pop cmds))
6041 (substitute-key-definition cmd cmd map global-map)))
6042 (suppress-keymap map)
6043 (org-defkey map "\C-m" 'org-goto-ret)
6044 (org-defkey map [(return)] 'org-goto-ret)
6045 (org-defkey map [(left)] 'org-goto-left)
6046 (org-defkey map [(right)] 'org-goto-right)
6047 (org-defkey map [(control ?g)] 'org-goto-quit)
6048 (org-defkey map "\C-i" 'org-cycle)
6049 (org-defkey map [(tab)] 'org-cycle)
6050 (org-defkey map [(down)] 'outline-next-visible-heading)
6051 (org-defkey map [(up)] 'outline-previous-visible-heading)
6052 (if org-goto-auto-isearch
6053 (if (fboundp 'define-key-after)
6054 (define-key-after map [t] 'org-goto-local-auto-isearch)
6056 (org-defkey map "q" 'org-goto-quit)
6057 (org-defkey map "n" 'outline-next-visible-heading)
6058 (org-defkey map "p" 'outline-previous-visible-heading)
6059 (org-defkey map "f" 'outline-forward-same-level)
6060 (org-defkey map "b" 'outline-backward-same-level)
6061 (org-defkey map "u" 'outline-up-heading))
6062 (org-defkey map "/" 'org-occur)
6063 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6064 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6065 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6066 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6067 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6070 (defconst org-goto-help
6071 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6072 RET=jump to location [Q]uit and return to previous location
6073 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6075 (defvar org-goto-start-pos) ; dynamically scoped parameter
6077 (defun org-goto (&optional alternative-interface)
6078 "Look up a different location in the current file, keeping current visibility.
6080 When you want look-up or go to a different location in a document, the
6081 fastest way is often to fold the entire buffer and then dive into the tree.
6082 This method has the disadvantage, that the previous location will be folded,
6083 which may not be what you want.
6085 This command works around this by showing a copy of the current buffer
6086 in an indirect buffer, in overview mode. You can dive into the tree in
6087 that copy, use org-occur and incremental search to find a location.
6088 When pressing RET or `Q', the command returns to the original buffer in
6089 which the visibility is still unchanged. After RET is will also jump to
6090 the location selected in the indirect buffer and expose the
6091 the headline hierarchy above."
6093 (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
6094 (org-refile-use-outline-path t)
6096 (if (not alternative-interface)
6098 (if (eq org-goto-interface 'outline)
6099 'outline-path-completion
6101 (org-goto-start-pos (point))
6103 (if (eq interface 'outline)
6104 (car (org-get-location (current-buffer) org-goto-help))
6105 (nth 3 (org-refile-get-location "Goto: ")))))
6108 (org-mark-ring-push org-goto-start-pos)
6109 (goto-char selected-point)
6110 (if (or (org-invisible-p) (org-invisible-p2))
6111 (org-show-context 'org-goto)))
6114 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6115 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6116 (defvar org-goto-local-auto-isearch-map) ; defined below
6118 (defun org-get-location (buf help)
6119 "Let the user select a location in the Org-mode buffer BUF.
6120 This function uses a recursive edit. It returns the selected position
6122 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6123 (isearch-hide-immediately nil)
6124 (isearch-search-fun-function
6125 (lambda () 'org-goto-local-search-forward-headings))
6126 (org-goto-selected-point org-goto-exit-command))
6128 (save-window-excursion
6129 (delete-other-windows)
6130 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6133 (make-indirect-buffer (current-buffer) "*org-goto*")
6134 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6135 (with-output-to-temp-buffer "*Help*"
6137 (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
6138 (setq buffer-read-only nil)
6139 (let ((org-startup-truncated t)
6140 (org-startup-folded nil)
6141 (org-startup-align-all-tables nil))
6144 (setq buffer-read-only t)
6145 (if (and (boundp 'org-goto-start-pos)
6146 (integer-or-marker-p org-goto-start-pos))
6147 (let ((org-show-hierarchy-above t)
6148 (org-show-siblings t)
6149 (org-show-following-heading t))
6150 (goto-char org-goto-start-pos)
6151 (and (org-invisible-p) (org-show-context)))
6152 (goto-char (point-min)))
6153 (org-beginning-of-line)
6154 (message "Select location and press RET")
6155 (use-local-map org-goto-map)
6158 (kill-buffer "*org-goto*")
6159 (cons org-goto-selected-point org-goto-exit-command)))
6161 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6162 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6163 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6164 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6166 (defun org-goto-local-search-forward-headings (string bound noerror)
6167 "Search and make sure that anu matches are in headlines."
6169 (while (search-forward string bound noerror)
6170 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6171 (and (member :headline context)
6172 (not (member :tags context))))
6173 (throw 'return (point))))))
6175 (defun org-goto-local-auto-isearch ()
6178 (goto-char (point-min))
6179 (let ((keys (this-command-keys)))
6180 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6182 (isearch-process-search-char (string-to-char keys)))))
6184 (defun org-goto-ret (&optional arg)
6185 "Finish `org-goto' by going to the new location."
6187 (setq org-goto-selected-point (point)
6188 org-goto-exit-command 'return)
6191 (defun org-goto-left ()
6192 "Finish `org-goto' by going to the new location."
6194 (if (org-on-heading-p)
6196 (beginning-of-line 1)
6197 (setq org-goto-selected-point (point)
6198 org-goto-exit-command 'left)
6200 (error "Not on a heading")))
6202 (defun org-goto-right ()
6203 "Finish `org-goto' by going to the new location."
6205 (if (org-on-heading-p)
6207 (setq org-goto-selected-point (point)
6208 org-goto-exit-command 'right)
6210 (error "Not on a heading")))
6212 (defun org-goto-quit ()
6213 "Finish `org-goto' without cursor motion."
6215 (setq org-goto-selected-point nil)
6216 (setq org-goto-exit-command 'quit)
6219 ;;; Indirect buffer display of subtrees
6221 (defvar org-indirect-dedicated-frame nil
6222 "This is the frame being used for indirect tree display.")
6223 (defvar org-last-indirect-buffer nil)
6225 (defun org-tree-to-indirect-buffer (&optional arg)
6226 "Create indirect buffer and narrow it to current subtree.
6227 With numerical prefix ARG, go up to this level and then take that tree.
6228 If ARG is negative, go up that many levels.
6229 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6230 indirect buffer previously made with this command, to avoid proliferation of
6231 indirect buffers. However, when you call the command with a `C-u' prefix, or
6232 when `org-indirect-buffer-display' is `new-frame', the last buffer
6233 is kept so that you can work with several indirect buffers at the same time.
6234 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
6235 requests that a new frame be made for the new buffer, so that the dedicated
6236 frame is not changed."
6238 (let ((cbuf (current-buffer))
6239 (cwin (selected-window))
6241 beg end level heading ibuf)
6243 (org-back-to-heading t)
6245 (setq level (org-outline-level))
6246 (if (< arg 0) (setq arg (+ level arg)))
6247 (while (> (setq level (org-outline-level)) arg)
6248 (outline-up-heading 1 t)))
6250 heading (org-get-heading))
6251 (org-end-of-subtree t) (setq end (point)))
6252 (if (and (buffer-live-p org-last-indirect-buffer)
6253 (not (eq org-indirect-buffer-display 'new-frame))
6255 (kill-buffer org-last-indirect-buffer))
6256 (setq ibuf (org-get-indirect-buffer cbuf)
6257 org-last-indirect-buffer ibuf)
6259 ((or (eq org-indirect-buffer-display 'new-frame)
6260 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6261 (select-frame (make-frame))
6262 (delete-other-windows)
6263 (switch-to-buffer ibuf)
6264 (org-set-frame-title heading))
6265 ((eq org-indirect-buffer-display 'dedicated-frame)
6267 (select-frame (or (and org-indirect-dedicated-frame
6268 (frame-live-p org-indirect-dedicated-frame)
6269 org-indirect-dedicated-frame)
6270 (setq org-indirect-dedicated-frame (make-frame)))))
6271 (delete-other-windows)
6272 (switch-to-buffer ibuf)
6273 (org-set-frame-title (concat "Indirect: " heading)))
6274 ((eq org-indirect-buffer-display 'current-window)
6275 (switch-to-buffer ibuf))
6276 ((eq org-indirect-buffer-display 'other-window)
6277 (pop-to-buffer ibuf))
6278 (t (error "Invalid value.")))
6279 (if (featurep 'xemacs)
6280 (save-excursion (org-mode) (turn-on-font-lock)))
6281 (narrow-to-region beg end)
6284 (and (window-live-p cwin) (select-window cwin))))
6286 (defun org-get-indirect-buffer (&optional buffer)
6287 (setq buffer (or buffer (current-buffer)))
6288 (let ((n 1) (base (buffer-name buffer)) bname)
6289 (while (buffer-live-p
6290 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
6293 (make-indirect-buffer buffer bname 'clone)
6294 (error (make-indirect-buffer buffer bname)))))
6296 (defun org-set-frame-title (title)
6297 "Set the title of the current frame to the string TITLE."
6298 ;; FIXME: how to name a single frame in XEmacs???
6299 (unless (featurep 'xemacs)
6300 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
6302 ;;;; Structure editing
6304 ;;; Inserting headlines
6306 (defun org-insert-heading (&optional force-heading)
6307 "Insert a new heading or item with same depth at point.
6308 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
6309 If point is at the beginning of a headline, insert a sibling before the
6310 current headline. If point is not at the beginning, do not split the line,
6311 but create the new hedline after the current line."
6313 (if (= (buffer-size) 0)
6315 (when (or force-heading (not (org-insert-item)))
6316 (let* ((head (save-excursion
6319 (org-back-to-heading)
6322 (blank (cdr (assq 'heading org-blank-before-new-entry)))
6325 ((and (org-on-heading-p) (bolp)
6327 (save-excursion (backward-char 1) (not (org-invisible-p)))))
6328 ;; insert before the current line
6329 (open-line (if blank 2 1)))
6333 (backward-char 1) (not (org-invisible-p)))))
6334 ;; insert right here
6337 ; ;; in the middle of the line
6339 ; (if (org-get-alist-option org-M-RET-may-split-line 'headline)
6341 ; (org-on-heading-p)
6342 ; (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \r\n]"))
6343 ; ;; protect the tags
6344 ;; (let ((tags (match-string 2)) pos)
6345 ; (delete-region (match-beginning 1) (match-end 1))
6346 ; (setq pos (point-at-bol))
6347 ; (newline (if blank 2 1))
6352 ; (org-set-tags nil 'align)))
6353 ; (newline (if blank 2 1)))
6354 ; (newline (if blank 2 1))))
6357 ;; in the middle of the line
6360 (org-get-alist-option org-M-RET-may-split-line 'headline))
6362 (if (org-on-heading-p)
6364 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
6365 (setq tags (and (match-end 2) (match-string 2)))
6367 (delete-region (match-beginning 1) (match-end 1)))
6368 (setq pos (point-at-bol))
6369 (or split (end-of-line 1))
6370 (delete-horizontal-space)
6371 (newline (if blank 2 1))
6377 (org-set-tags nil 'align))))
6378 (or split (end-of-line 1))
6379 (newline (if blank 2 1))))))
6380 (insert head) (just-one-space)
6383 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
6384 (run-hooks 'org-insert-heading-hook)))))
6386 (defun org-insert-heading-after-current ()
6387 "Insert a new heading with same level as current, after current subtree."
6389 (org-back-to-heading)
6390 (org-insert-heading)
6391 (org-move-subtree-down)
6394 (defun org-insert-todo-heading (arg)
6395 "Insert a new heading with the same level and TODO state as current heading.
6396 If the heading has no TODO state, or if the state is DONE, use the first
6397 state (TODO by default). Also with prefix arg, force first state."
6399 (when (not (org-insert-item 'checkbox))
6400 (org-insert-heading)
6402 (org-back-to-heading)
6403 (outline-previous-heading)
6404 (looking-at org-todo-line-regexp))
6406 (not (match-beginning 2))
6407 (member (match-string 2) org-done-keywords))
6408 (insert (car org-todo-keywords-1) " ")
6409 (insert (match-string 2) " "))))
6411 (defun org-insert-subheading (arg)
6412 "Insert a new subheading and demote it.
6413 Works for outline headings and for plain lists alike."
6415 (org-insert-heading arg)
6417 ((org-on-heading-p) (org-do-demote))
6418 ((org-at-item-p) (org-indent-item 1))))
6420 (defun org-insert-todo-subheading (arg)
6421 "Insert a new subheading with TODO keyword or checkbox and demote it.
6422 Works for outline headings and for plain lists alike."
6424 (org-insert-todo-heading arg)
6426 ((org-on-heading-p) (org-do-demote))
6427 ((org-at-item-p) (org-indent-item 1))))
6429 ;;; Promotion and Demotion
6431 (defun org-promote-subtree ()
6432 "Promote the entire subtree.
6433 See also `org-promote'."
6436 (org-map-tree 'org-promote))
6437 (org-fix-position-after-promote))
6439 (defun org-demote-subtree ()
6440 "Demote the entire subtree. See `org-demote'.
6441 See also `org-promote'."
6444 (org-map-tree 'org-demote))
6445 (org-fix-position-after-promote))
6448 (defun org-do-promote ()
6449 "Promote the current heading higher up the tree.
6450 If the region is active in `transient-mark-mode', promote all headings
6454 (if (org-region-active-p)
6455 (org-map-region 'org-promote (region-beginning) (region-end))
6457 (org-fix-position-after-promote))
6459 (defun org-do-demote ()
6460 "Demote the current heading lower down the tree.
6461 If the region is active in `transient-mark-mode', demote all headings
6465 (if (org-region-active-p)
6466 (org-map-region 'org-demote (region-beginning) (region-end))
6468 (org-fix-position-after-promote))
6470 (defun org-fix-position-after-promote ()
6471 "Make sure that after pro/demotion cursor position is right."
6472 (let ((pos (point)))
6473 (when (save-excursion
6474 (beginning-of-line 1)
6475 (looking-at org-todo-line-regexp)
6476 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6477 (cond ((eobp) (insert " "))
6478 ((eolp) (insert " "))
6479 ((equal (char-after) ?\ ) (forward-char 1))))))
6481 (defun org-reduced-level (l)
6482 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6484 (defun org-get-legal-level (level &optional change)
6485 "Rectify a level change under the influence of `org-odd-levels-only'
6486 LEVEL is a current level, CHANGE is by how much the level should be
6487 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6488 even level numbers will become the next higher odd number."
6489 (if org-odd-levels-only
6490 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6491 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6492 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6493 (max 1 (+ level change))))
6495 (defun org-promote ()
6496 "Promote the current heading higher up the tree.
6497 If the region is active in `transient-mark-mode', promote all headings
6499 (org-back-to-heading t)
6500 (let* ((level (save-match-data (funcall outline-level)))
6501 (up-head (concat (make-string (org-get-legal-level level -1) ?*) " "))
6502 (diff (abs (- level (length up-head) -1))))
6503 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6504 (replace-match up-head nil t)
6505 ;; Fixup tag positioning
6506 (and org-auto-align-tags (org-set-tags nil t))
6507 (if org-adapt-indentation (org-fixup-indentation (- diff)))))
6509 (defun org-demote ()
6510 "Demote the current heading lower down the tree.
6511 If the region is active in `transient-mark-mode', demote all headings
6513 (org-back-to-heading t)
6514 (let* ((level (save-match-data (funcall outline-level)))
6515 (down-head (concat (make-string (org-get-legal-level level 1) ?*) " "))
6516 (diff (abs (- level (length down-head) -1))))
6517 (replace-match down-head nil t)
6518 ;; Fixup tag positioning
6519 (and org-auto-align-tags (org-set-tags nil t))
6520 (if org-adapt-indentation (org-fixup-indentation diff))))
6522 (defun org-map-tree (fun)
6523 "Call FUN for every heading underneath the current one."
6524 (org-back-to-heading)
6525 (let ((level (funcall outline-level)))
6529 (outline-next-heading)
6530 (> (funcall outline-level) level))
6534 (defun org-map-region (fun beg end)
6535 "Call FUN for every heading between BEG and END."
6536 (let ((org-ignore-region t))
6538 (setq end (copy-marker end))
6540 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6544 (outline-next-heading)
6549 (defun org-fixup-indentation (diff)
6550 "Change the indentation in the current entry by DIFF
6551 However, if any line in the current entry has no indentation, or if it
6552 would end up with no indentation after the change, nothing at all is done."
6554 (let ((end (save-excursion (outline-next-heading)
6556 (prohibit (if (> diff 0)
6558 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6560 (unless (save-excursion (end-of-line 1)
6561 (re-search-forward prohibit end t))
6562 (while (and (< (point) end)
6563 (re-search-forward "^[ \t]+" end t))
6564 (goto-char (match-end 0))
6565 (setq col (current-column))
6566 (if (< diff 0) (replace-match ""))
6567 (indent-to (+ diff col))))
6568 (move-marker end nil))))
6570 (defun org-convert-to-odd-levels ()
6571 "Convert an org-mode file with all levels allowed to one with odd levels.
6572 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6575 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6576 (let ((org-odd-levels-only nil) n)
6578 (goto-char (point-min))
6579 (while (re-search-forward "^\\*\\*+ " nil t)
6580 (setq n (- (length (match-string 0)) 2))
6581 (while (>= (setq n (1- n)) 0)
6583 (end-of-line 1))))))
6586 (defun org-convert-to-oddeven-levels ()
6587 "Convert an org-mode file with only odd levels to one with odd and even levels.
6588 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6589 section with an even level, conversion would destroy the structure of the file. An error
6590 is signaled in this case."
6592 (goto-char (point-min))
6593 ;; First check if there are no even levels
6594 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6595 (org-show-context t)
6596 (error "Not all levels are odd in this file. Conversion not possible."))
6597 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6598 (let ((org-odd-levels-only nil) n)
6600 (goto-char (point-min))
6601 (while (re-search-forward "^\\*\\*+ " nil t)
6602 (setq n (/ (1- (length (match-string 0))) 2))
6603 (while (>= (setq n (1- n)) 0)
6605 (end-of-line 1))))))
6607 (defun org-tr-level (n)
6608 "Make N odd if required."
6609 (if org-odd-levels-only (1+ (/ n 2)) n))
6611 ;;; Vertical tree motion, cutting and pasting of subtrees
6613 (defun org-move-subtree-up (&optional arg)
6614 "Move the current subtree up past ARG headlines of the same level."
6616 (org-move-subtree-down (- (prefix-numeric-value arg))))
6618 (defun org-move-subtree-down (&optional arg)
6619 "Move the current subtree down past ARG headlines of the same level."
6621 (setq arg (prefix-numeric-value arg))
6622 (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
6623 'outline-get-last-sibling))
6624 (ins-point (make-marker))
6626 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6628 (org-back-to-heading)
6631 (setq ne-beg (org-back-over-empty-lines))
6634 (save-excursion (outline-end-of-heading)
6635 (setq folded (org-invisible-p)))
6636 (outline-end-of-subtree))
6637 (outline-next-heading)
6638 (setq ne-end (org-back-over-empty-lines))
6641 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6642 ;; include less whitespace
6645 (forward-line (- ne-beg ne-end))
6646 (setq beg (point))))
6647 ;; Find insertion point, with error handling
6649 (or (and (funcall movfunc) (looking-at outline-regexp))
6650 (progn (goto-char beg0)
6651 (error "Cannot move past superior level or buffer limit")))
6652 (setq cnt (1- cnt)))
6654 ;; Moving forward - still need to move over subtree
6655 (progn (org-end-of-subtree t t)
6657 (org-back-over-empty-lines)
6658 (or (bolp) (newline)))))
6659 (setq ne-ins (org-back-over-empty-lines))
6660 (move-marker ins-point (point))
6661 (setq txt (buffer-substring beg end))
6662 (delete-region beg end)
6663 (outline-flag-region (1- beg) beg nil)
6664 (outline-flag-region (1- (point)) (point) nil)
6666 (or (bolp) (insert "\n"))
6667 (setq ins-end (point))
6668 (goto-char ins-point)
6669 (org-skip-whitespace)
6670 (when (and (< arg 0)
6671 (org-first-sibling-p)
6673 ;; Move whitespace back to beginning
6676 (let ((kill-whole-line t))
6677 (kill-line (- ne-ins ne-beg)) (point)))
6678 (insert (make-string (- ne-ins ne-beg) ?\n)))
6679 (move-marker ins-point nil)
6680 (org-compact-display-after-subtree-move)
6684 (org-cycle-hide-drawers 'children))))
6686 (defvar org-subtree-clip ""
6687 "Clipboard for cut and paste of subtrees.
6688 This is actually only a copy of the kill, because we use the normal kill
6689 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6691 (defvar org-subtree-clip-folded nil
6692 "Was the last copied subtree folded?
6693 This is used to fold the tree back after pasting.")
6695 (defun org-cut-subtree (&optional n)
6696 "Cut the current subtree into the clipboard.
6697 With prefix arg N, cut this many sequential subtrees.
6698 This is a short-hand for marking the subtree and then cutting it."
6700 (org-copy-subtree n 'cut))
6702 (defun org-copy-subtree (&optional n cut)
6703 "Cut the current subtree into the clipboard.
6704 With prefix arg N, cut this many sequential subtrees.
6705 This is a short-hand for marking the subtree and then copying it.
6706 If CUT is non-nil, actually cut the subtree."
6708 (let (beg end folded (beg0 (point)))
6710 (org-back-to-heading nil) ; take what looks like a subtree
6711 (org-back-to-heading t)) ; take what is really there
6712 (org-back-over-empty-lines)
6714 (skip-chars-forward " \t\r\n")
6716 (save-excursion (outline-end-of-heading)
6717 (setq folded (org-invisible-p)))
6719 (outline-forward-same-level (1- n))
6721 (org-end-of-subtree t t))
6722 (org-back-over-empty-lines)
6726 (setq org-subtree-clip-folded folded)
6727 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6728 (setq org-subtree-clip (current-kill 0))
6729 (message "%s: Subtree(s) with %d characters"
6730 (if cut "Cut" "Copied")
6731 (length org-subtree-clip)))))
6733 (defun org-paste-subtree (&optional level tree)
6734 "Paste the clipboard as a subtree, with modification of headline level.
6735 The entire subtree is promoted or demoted in order to match a new headline
6736 level. By default, the new level is derived from the visible headings
6737 before and after the insertion point, and taken to be the inferior headline
6738 level of the two. So if the previous visible heading is level 3 and the
6739 next is level 4 (or vice versa), level 4 will be used for insertion.
6740 This makes sure that the subtree remains an independent subtree and does
6741 not swallow low level entries.
6743 You can also force a different level, either by using a numeric prefix
6744 argument, or by inserting the heading marker by hand. For example, if the
6745 cursor is after \"*****\", then the tree will be shifted to level 5.
6747 If you want to insert the tree as is, just use \\[yank].
6749 If optional TREE is given, use this text instead of the kill ring."
6751 (unless (org-kill-is-subtree-p tree)
6753 (substitute-command-keys
6754 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6755 (let* ((txt (or tree (and kill-ring (current-kill 0))))
6756 (^re (concat "^\\(" outline-regexp "\\)"))
6757 (re (concat "\\(" outline-regexp "\\)"))
6758 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6760 (old-level (if (string-match ^re txt)
6761 (- (match-end 0) (match-beginning 0) 1)
6763 (force-level (cond (level (prefix-numeric-value level))
6765 ^re_ (buffer-substring (point-at-bol) (point)))
6766 (- (match-end 1) (match-beginning 1)))
6768 (previous-level (save-excursion
6771 (outline-previous-visible-heading 1)
6773 (- (match-end 0) (match-beginning 0) 1)
6776 (next-level (save-excursion
6779 (or (looking-at outline-regexp)
6780 (outline-next-visible-heading 1))
6782 (- (match-end 0) (match-beginning 0) 1)
6785 (new-level (or force-level (max previous-level next-level)))
6786 (shift (if (or (= old-level -1)
6788 (= old-level new-level))
6790 (- new-level old-level)))
6791 (delta (if (> shift 0) -1 1))
6792 (func (if (> shift 0) 'org-demote 'org-promote))
6793 (org-odd-levels-only nil)
6795 ;; Remove the forced level indicator
6797 (delete-region (point-at-bol) (point)))
6799 (beginning-of-line 1)
6800 (org-back-over-empty-lines) ;; FIXME: correct fix????
6802 (insert-before-markers txt) ;; FIXME: correct fix????
6803 (unless (string-match "\n\\'" txt) (insert "\n"))
6806 (skip-chars-forward " \t\n\r")
6808 ;; Shift if necessary
6811 (narrow-to-region beg end)
6812 (while (not (= shift 0))
6813 (org-map-region func (point-min) (point-max))
6814 (setq shift (+ delta shift)))
6815 (goto-char (point-min))))
6816 (when (interactive-p)
6817 (message "Clipboard pasted as level %d subtree" new-level))
6819 (eq org-subtree-clip (current-kill 0))
6820 org-subtree-clip-folded)
6821 ;; The tree was folded before it was killed/copied
6824 (defun org-kill-is-subtree-p (&optional txt)
6825 "Check if the current kill is an outline subtree, or a set of trees.
6826 Returns nil if kill does not start with a headline, or if the first
6827 headline level is not the largest headline level in the tree.
6828 So this will actually accept several entries of equal levels as well,
6829 which is OK for `org-paste-subtree'.
6830 If optional TXT is given, check this string instead of the current kill."
6831 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6832 (start-level (and kill
6833 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6834 org-outline-regexp "\\)")
6836 (- (match-end 2) (match-beginning 2) 1)))
6837 (re (concat "^" org-outline-regexp))
6838 (start (1+ (match-beginning 2))))
6839 (if (not start-level)
6841 nil) ;; does not even start with a heading
6843 (while (setq start (string-match re kill (1+ start)))
6844 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6848 (defun org-narrow-to-subtree ()
6849 "Narrow buffer to the current subtree."
6854 (progn (org-back-to-heading) (point))
6855 (progn (org-end-of-subtree t t) (point))))))
6860 (defun org-sort (with-case)
6861 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
6862 Optional argument WITH-CASE means sort case-sensitively."
6864 (if (org-at-table-p)
6865 (org-call-with-arg 'org-table-sort-lines with-case)
6866 (org-call-with-arg 'org-sort-entries-or-items with-case)))
6868 (defvar org-priority-regexp) ; defined later in the file
6870 (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
6871 "Sort entries on a certain level of an outline tree.
6872 If there is an active region, the entries in the region are sorted.
6873 Else, if the cursor is before the first entry, sort the top-level items.
6874 Else, the children of the entry at point are sorted.
6876 Sorting can be alphabetically, numerically, and by date/time as given by
6877 the first time stamp in the entry. The command prompts for the sorting
6878 type unless it has been given to the function through the SORTING-TYPE
6879 argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
6880 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
6881 called with point at the beginning of the record. It must return either
6882 a string or a number that should serve as the sorting key for that record.
6884 Comparing entries ignores case by default. However, with an optional argument
6885 WITH-CASE, the sorting considers case as well."
6887 (let ((case-func (if with-case 'identity 'downcase))
6888 start beg end stars re re2
6889 txt what tmp plain-list-p)
6890 ;; Find beginning and end of region to sort
6892 ((org-region-active-p)
6893 ;; we will sort the region
6894 (setq end (region-end)
6896 (goto-char (region-beginning))
6897 (if (not (org-on-heading-p)) (outline-next-heading))
6898 (setq start (point)))
6900 ;; we will sort this plain list
6901 (org-beginning-of-item-list) (setq start (point))
6902 (org-end-of-item-list) (setq end (point))
6904 (setq plain-list-p t
6906 ((or (org-on-heading-p)
6907 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
6908 ;; we will sort the children of the current headline
6909 (org-back-to-heading)
6911 end (progn (org-end-of-subtree t t)
6912 (org-back-over-empty-lines)
6917 (outline-next-heading))
6919 ;; we will sort the top-level entries in this file
6920 (goto-char (point-min))
6921 (or (org-on-heading-p) (outline-next-heading))
6922 (setq start (point) end (point-max) what "top-level")
6927 (if (>= beg end) (error "Nothing to sort"))
6929 (unless plain-list-p
6930 (looking-at "\\(\\*+\\)")
6931 (setq stars (match-string 1)
6932 re (concat "^" (regexp-quote stars) " +")
6933 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
6934 txt (buffer-substring beg end))
6935 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
6936 (if (and (not (equal stars "*")) (string-match re2 txt))
6937 (error "Region to sort contains a level above the first entry")))
6939 (unless sorting-type
6942 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
6943 "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty [f]unc A/N/T/P/F means reversed:")
6945 (setq sorting-type (read-char-exclusive))
6947 (and (= (downcase sorting-type) ?f)
6949 (completing-read "Sort using function: "
6950 obarray 'fboundp t nil nil))
6951 (setq getkey-func (intern getkey-func)))
6953 (and (= (downcase sorting-type) ?r)
6955 (completing-read "Property: "
6956 (mapcar 'list (org-buffer-property-keys t))
6959 (message "Sorting entries...")
6962 (narrow-to-region start end)
6964 (let ((dcst (downcase sorting-type))
6965 (now (current-time)))
6967 (/= dcst sorting-type)
6968 ;; This function moves to the beginning character of the "record" to
6972 (if (org-at-item-p) t (goto-char (point-max))))
6974 (if (re-search-forward re nil t)
6975 (goto-char (match-beginning 0))
6976 (goto-char (point-max)))))
6977 ;; This function moves to the last character of the "record" being
6984 (outline-forward-same-level 1)
6986 (goto-char (point-max)))))))
6988 ;; This function returns the value that gets sorted against.
6991 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
6994 (string-to-number (buffer-substring (match-end 0)
6997 (buffer-substring (match-end 0) (point-at-eol)))
6999 (if (re-search-forward org-ts-regexp
7001 (org-time-string-to-time (match-string 0))
7006 (setq tmp (funcall getkey-func))
7007 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7009 (error "Invalid key function `%s'" getkey-func)))
7010 (t (error "Invalid sorting type `%c'" sorting-type)))))
7014 (if (looking-at outline-regexp)
7015 (string-to-number (buffer-substring (match-end 0)
7019 (funcall case-func (buffer-substring (point-at-bol)
7022 (if (re-search-forward org-ts-regexp
7026 (org-time-string-to-time (match-string 0))
7029 (if (re-search-forward org-priority-regexp (point-at-eol) t)
7030 (string-to-char (match-string 2))
7031 org-default-priority))
7033 (or (org-entry-get nil property) ""))
7037 (setq tmp (funcall getkey-func))
7038 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
7040 (error "Invalid key function `%s'" getkey-func)))
7041 (t (error "Invalid sorting type `%c'" sorting-type)))))
7044 ((= dcst ?a) 'string<)
7045 ((= dcst ?t) 'time-less-p)
7047 (message "Sorting entries...done")))
7049 (defun org-do-sort (table what &optional with-case sorting-type)
7050 "Sort TABLE of WHAT according to SORTING-TYPE.
7051 The user will be prompted for the SORTING-TYPE if the call to this
7052 function does not specify it. WHAT is only for the prompt, to indicate
7053 what is being sorted. The sorting key will be extracted from
7054 the car of the elements of the table.
7055 If WITH-CASE is non-nil, the sorting will be case-sensitive."
7056 (unless sorting-type
7058 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
7060 (setq sorting-type (read-char-exclusive)))
7061 (let ((dcst (downcase sorting-type))
7062 extractfun comparefun)
7063 ;; Define the appropriate functions
7066 (setq extractfun 'string-to-number
7067 comparefun (if (= dcst sorting-type) '< '>)))
7069 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
7070 (lambda(x) (downcase (org-sort-remove-invisible x))))
7071 comparefun (if (= dcst sorting-type)
7073 (lambda (a b) (and (not (string< a b))
7074 (not (string= a b)))))))
7078 (if (string-match org-ts-regexp x)
7080 (org-time-string-to-time (match-string 0 x)))
7082 comparefun (if (= dcst sorting-type) '< '>)))
7083 (t (error "Invalid sorting type `%c'" sorting-type)))
7085 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
7087 (lambda (a b) (funcall comparefun (car a) (car b))))))
7089 ;;;; Plain list items, including checkboxes
7091 ;;; Plain list items
7093 (defun org-at-item-p ()
7094 "Is point in a line starting a hand-formatted item?"
7095 (let ((llt org-plain-list-ordered-item-terminator))
7097 (goto-char (point-at-bol))
7100 ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
7101 ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
7102 ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
7103 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
7105 (defun org-in-item-p ()
7106 "It the cursor inside a plain list item.
7107 Does not have to be the first line."
7111 (org-beginning-of-item)
7116 (defun org-insert-item (&optional checkbox)
7117 "Insert a new item at the current level.
7118 Return t when things worked, nil when we are not in an item."
7119 (when (save-excursion
7122 (org-beginning-of-item)
7124 (if (org-invisible-p) (error "Invisible item"))
7127 (let* ((bul (match-string 0))
7128 (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
7130 (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
7133 ((and (org-at-item-p) (<= (point) eow))
7134 ;; before the bullet
7135 (beginning-of-line 1)
7136 (open-line (if blank 2 1)))
7138 (beginning-of-line 1))
7140 (unless (org-get-alist-option org-M-RET-may-split-line 'item)
7142 (delete-horizontal-space))
7143 (newline (if blank 2 1))))
7144 (insert bul (if checkbox "[ ]" ""))
7148 (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
7149 (org-maybe-renumber-ordered-list)
7150 (and checkbox (org-update-checkbox-count-maybe))
7155 (defun org-at-item-checkbox-p ()
7156 "Is point at a line starting a plain-list item with a checklet?"
7157 (and (org-at-item-p)
7159 (goto-char (match-end 0))
7160 (skip-chars-forward " \t")
7161 (looking-at "\\[[- X]\\]"))))
7163 (defun org-toggle-checkbox (&optional arg)
7164 "Toggle the checkbox in the current line."
7167 (let (beg end status (firstnew 'unknown))
7169 ((org-region-active-p)
7170 (setq beg (region-beginning) end (region-end)))
7172 (setq beg (point) end (save-excursion (outline-next-heading) (point))))
7173 ((org-at-item-checkbox-p)
7174 (let ((pos (point)))
7177 ((member (match-string 0) '("[ ]" "[-]")) "[X]")
7182 (t (error "Not at a checkbox or heading, and no active region")))
7185 (while (< (point) end)
7186 (when (org-at-item-checkbox-p)
7187 (setq status (equal (match-string 0) "[X]"))
7188 (when (eq firstnew 'unknown)
7189 (setq firstnew (not status)))
7191 (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
7192 (beginning-of-line 2)))))
7193 (org-update-checkbox-count-maybe))
7195 (defun org-update-checkbox-count-maybe ()
7196 "Update checkbox statistics unless turned off by user."
7197 (when org-provide-checkbox-statistics
7198 (org-update-checkbox-count)))
7200 (defun org-update-checkbox-count (&optional all)
7201 "Update the checkbox statistics in the current section.
7202 This will find all statistic cookies like [57%] and [6/12] and update them
7203 with the current numbers. With optional prefix argument ALL, do this for
7207 (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
7208 (beg (condition-case nil
7209 (progn (outline-back-to-heading) (point))
7210 (error (point-min))))
7211 (end (move-marker (make-marker)
7212 (progn (outline-next-heading) (point))))
7213 (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
7214 (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
7215 (re-find (concat re "\\|" re-box))
7216 beg-cookie end-cookie is-percent c-on c-off lim
7217 eline curr-ind next-ind continue-from startsearch
7221 (goto-char (point-min))
7222 (outline-next-heading)
7223 (setq beg (point) end (point-max)))
7225 ;; find each statistic cookie
7226 (while (re-search-backward re-find beg t)
7227 (setq beg-cookie (match-beginning 1)
7228 end-cookie (match-end 1)
7229 cstat (+ cstat (if end-cookie 1 0))
7230 startsearch (point-at-eol)
7231 continue-from (point-at-bol)
7232 is-percent (match-beginning 2)
7234 ((org-on-heading-p) (outline-next-heading) (point))
7235 ((org-at-item-p) (org-end-of-item) (point))
7240 ;; find first checkbox for this cookie and gather
7241 ;; statistics from all that are at this indentation level
7242 (goto-char startsearch)
7243 (if (re-search-forward re-box lim t)
7245 (org-beginning-of-item)
7246 (setq curr-ind (org-get-indentation))
7247 (setq next-ind curr-ind)
7248 (while (= curr-ind next-ind)
7249 (save-excursion (end-of-line) (setq eline (point)))
7250 (if (re-search-forward re-box eline t)
7251 (if (member (match-string 2) '("[ ]" "[-]"))
7252 (setq c-off (1+ c-off))
7253 (setq c-on (1+ c-on))
7257 (setq next-ind (org-get-indentation))
7259 (goto-char continue-from)
7262 (delete-region beg-cookie end-cookie)
7263 (goto-char beg-cookie)
7266 (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
7267 (format "[%d/%d]" c-on (+ c-on c-off)))))
7268 ;; update items checkbox if it has one
7269 (when (org-at-item-p)
7270 (org-beginning-of-item)
7271 (when (and (> (+ c-on c-off) 0)
7272 (re-search-forward re-box (point-at-eol) t))
7273 (setq beg-cookie (match-beginning 2)
7274 end-cookie (match-end 2))
7275 (delete-region beg-cookie end-cookie)
7276 (goto-char beg-cookie)
7277 (cond ((= c-off 0) (insert "[X]"))
7278 ((= c-on 0) (insert "[ ]"))
7281 (goto-char continue-from))
7282 (when (interactive-p)
7283 (message "Checkbox satistics updated %s (%d places)"
7284 (if all "in entire file" "in current outline entry") cstat)))))
7286 (defun org-get-checkbox-statistics-face ()
7287 "Select the face for checkbox statistics.
7288 The face will be `org-done' when all relevant boxes are checked. Otherwise
7289 it will be `org-todo'."
7291 (if (equal (match-string 1) "100%") 'org-done 'org-todo)
7292 (if (and (> (match-end 2) (match-beginning 2))
7293 (equal (match-string 2) (match-string 3)))
7297 (defun org-get-indentation (&optional line)
7298 "Get the indentation of the current line, interpreting tabs.
7299 When LINE is given, assume it represents a line and compute its indentation."
7301 (if (string-match "^ *" (org-remove-tabs line))
7304 (beginning-of-line 1)
7305 (skip-chars-forward " \t")
7308 (defun org-remove-tabs (s &optional width)
7309 "Replace tabulators in S with spaces.
7310 Assumes that s is a single line, starting in column 0."
7311 (setq width (or width tab-width))
7312 (while (string-match "\t" s)
7313 (setq s (replace-match
7315 (- (* width (/ (+ (match-beginning 0) width) width))
7316 (match-beginning 0)) ?\ )
7320 (defun org-fix-indentation (line ind)
7321 "Fix indentation in LINE.
7322 IND is a cons cell with target and minimum indentation.
7323 If the current indenation in LINE is smaller than the minimum,
7324 leave it alone. If it is larger than ind, set it to the target."
7325 (let* ((l (org-remove-tabs line))
7326 (i (org-get-indentation l))
7327 (i1 (car ind)) (i2 (cdr ind)))
7328 (if (>= i i2) (setq l (substring line i2)))
7330 (concat (make-string i1 ?\ ) l)
7333 (defcustom org-empty-line-terminates-plain-lists nil
7334 "Non-nil means, an empty line ends all plain list levels.
7335 When nil, empty lines are part of the preceeding item."
7336 :group 'org-plain-lists
7339 (defun org-beginning-of-item ()
7340 "Go to the beginning of the current hand-formatted item.
7341 If the cursor is not in an item, throw an error."
7344 (limit (save-excursion
7347 (org-back-to-heading)
7348 (beginning-of-line 2) (point))
7349 (error (point-min)))))
7350 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
7353 (beginning-of-line 1)
7354 (beginning-of-line 1)
7355 (skip-chars-forward " \t")
7356 (setq ind (current-column))
7359 (beginning-of-line 0)
7360 (if (or (bobp) (< (point) limit)) (throw 'exit nil))
7362 (if (looking-at "[ \t]*$")
7363 (setq ind1 ind-empty)
7364 (skip-chars-forward " \t")
7365 (setq ind1 (current-column)))
7367 (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
7370 (error "Not in an item")))))
7372 (defun org-end-of-item ()
7373 "Go to the end of the current hand-formatted item.
7374 If the cursor is not in an item, throw an error."
7376 (let* ((pos (point))
7378 (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
7379 (limit (save-excursion (outline-next-heading) (point)))
7380 (ind (save-excursion
7381 (org-beginning-of-item)
7382 (skip-chars-forward " \t")
7386 (beginning-of-line 2)
7387 (if (eobp) (throw 'exit (point)))
7388 (if (>= (point) limit) (throw 'exit (point-at-bol)))
7389 (if (looking-at "[ \t]*$")
7390 (setq ind1 ind-empty)
7391 (skip-chars-forward " \t")
7392 (setq ind1 (current-column)))
7394 (throw 'exit (point-at-bol)))))))
7398 (error "Not in an item"))))
7400 (defun org-next-item ()
7401 "Move to the beginning of the next item in the current plain list.
7402 Error if not at a plain list, or if this is the last item in the list."
7404 (let (ind ind1 (pos (point)))
7405 (org-beginning-of-item)
7406 (setq ind (org-get-indentation))
7408 (setq ind1 (org-get-indentation))
7409 (unless (and (org-at-item-p) (= ind ind1))
7411 (error "On last item"))))
7413 (defun org-previous-item ()
7414 "Move to the beginning of the previous item in the current plain list.
7415 Error if not at a plain list, or if this is the first item in the list."
7417 (let (beg ind ind1 (pos (point)))
7418 (org-beginning-of-item)
7420 (setq ind (org-get-indentation))
7424 (beginning-of-line 0)
7425 (if (looking-at "[ \t]*$")
7427 (if (<= (setq ind1 (org-get-indentation)) ind)
7430 (if (or (not (org-at-item-p))
7433 (org-beginning-of-item))
7434 (error (goto-char pos)
7435 (error "On first item")))))
7437 (defun org-first-list-item-p ()
7438 "Is this heading the item in a plain list?"
7439 (unless (org-at-item-p)
7440 (error "Not at a plain list item"))
7441 (org-beginning-of-item)
7442 (= (point) (save-excursion (org-beginning-of-item-list))))
7444 (defun org-move-item-down ()
7445 "Move the plain list item at point down, i.e. swap with following item.
7446 Subitems (items with larger indentation) are considered part of the item,
7447 so this really moves item trees."
7449 (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
7450 (org-beginning-of-item)
7453 (setq ne-beg (org-back-over-empty-lines))
7456 (setq ind (org-get-indentation))
7459 (setq ind1 (org-get-indentation))
7460 (setq ne-end (org-back-over-empty-lines))
7463 (when (and (org-first-list-item-p) (< ne-end ne-beg))
7464 ;; include less whitespace
7467 (forward-line (- ne-beg ne-end))
7468 (setq beg (point))))
7470 (if (and (org-at-item-p) (= ind ind1))
7473 (org-back-over-empty-lines)
7474 (setq txt (buffer-substring beg end))
7476 (delete-region beg end))
7479 (goto-char pos) (org-skip-whitespace)
7480 (org-maybe-renumber-ordered-list))
7482 (error "Cannot move this item further down"))))
7484 (defun org-move-item-up (arg)
7485 "Move the plain list item at point up, i.e. swap with previous item.
7486 Subitems (items with larger indentation) are considered part of the item,
7487 so this really moves item trees."
7489 (let (beg beg0 end ind ind1 (pos (point)) txt
7490 ne-beg ne-ins ins-end)
7491 (org-beginning-of-item)
7493 (setq ind (org-get-indentation))
7495 (setq ne-beg (org-back-over-empty-lines))
7503 (beginning-of-line 0)
7504 (if (looking-at "[ \t]*$")
7505 (if org-empty-line-terminates-plain-lists
7508 (error "Cannot move this item further up"))
7510 (if (<= (setq ind1 (org-get-indentation)) ind)
7513 (org-beginning-of-item)
7514 (error (goto-char beg)
7515 (error "Cannot move this item further up")))
7516 (setq ind1 (org-get-indentation))
7517 (if (and (org-at-item-p) (= ind ind1))
7519 (setq ne-ins (org-back-over-empty-lines))
7520 (setq txt (buffer-substring beg end))
7522 (delete-region beg end))
7525 (setq ins-end (point))
7526 (goto-char pos) (org-skip-whitespace)
7528 (when (and (org-first-list-item-p) (> ne-ins ne-beg))
7529 ;; Move whitespace back to beginning
7532 (let ((kill-whole-line t))
7533 (kill-line (- ne-ins ne-beg)) (point)))
7534 (insert (make-string (- ne-ins ne-beg) ?\n)))
7536 (org-maybe-renumber-ordered-list))
7538 (error "Cannot move this item further up"))))
7540 (defun org-maybe-renumber-ordered-list ()
7541 "Renumber the ordered list at point if setup allows it.
7542 This tests the user option `org-auto-renumber-ordered-lists' before
7543 doing the renumbering."
7545 (when (and org-auto-renumber-ordered-lists
7547 (if (match-beginning 3)
7548 (org-renumber-ordered-list 1)
7549 (org-fix-bullet-type))))
7551 (defun org-maybe-renumber-ordered-list-safe ()
7554 (org-maybe-renumber-ordered-list))
7557 (defun org-cycle-list-bullet (&optional which)
7558 "Cycle through the different itemize/enumerate bullets.
7559 This cycle the entire list level through the sequence:
7561 `-' -> `+' -> `*' -> `1.' -> `1)'
7563 If WHICH is a string, use that as the new bullet. If WHICH is an integer,
7564 0 meand `-', 1 means `+' etc."
7567 (org-beginning-of-item-list)
7569 (beginning-of-line 1)
7570 (let ((current (match-string 0))
7571 (prevp (eq which 'previous))
7574 ((and (numberp which)
7575 (nth (1- which) '("-" "+" "*" "1." "1)"))))
7576 ((string-match "-" current) (if prevp "1)" "+"))
7577 ((string-match "\\+" current)
7578 (if prevp "-" (if (looking-at "\\S-") "1." "*")))
7579 ((string-match "\\*" current) (if prevp "+" "1."))
7580 ((string-match "\\." current) (if prevp "*" "1)"))
7581 ((string-match ")" current) (if prevp "1." "-"))
7582 (t (error "This should not happen"))))
7583 (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
7584 (org-fix-bullet-type)
7585 (org-maybe-renumber-ordered-list))))
7587 (defun org-get-string-indentation (s)
7588 "What indentation has S due to SPACE and TAB at the beginning of the string?"
7589 (let ((n -1) (i 0) (w tab-width) c)
7591 (while (< (setq n (1+ n)) (length s))
7593 (cond ((= c ?\ ) (setq i (1+ i)))
7594 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
7595 (t (throw 'exit t)))))
7598 (defun org-renumber-ordered-list (arg)
7599 "Renumber an ordered plain list.
7600 Cursor needs to be in the first line of an item, the line that starts
7601 with something like \"1.\" or \"2)\"."
7603 (unless (and (org-at-item-p)
7604 (match-beginning 3))
7605 (error "This is not an ordered list"))
7606 (let ((line (org-current-line))
7607 (col (current-column))
7608 (ind (org-get-string-indentation
7609 (buffer-substring (point-at-bol) (match-beginning 3))))
7610 ;; (term (substring (match-string 3) -1))
7613 ;; find where this list begins
7614 (org-beginning-of-item-list)
7615 (looking-at "[ \t]*[0-9]+\\([.)]\\)")
7616 (setq fmt (concat "%d" (match-string 1)))
7617 (beginning-of-line 0)
7618 ;; walk forward and replace these numbers
7622 (beginning-of-line 2)
7623 (if (eobp) (throw 'exit nil))
7624 (if (looking-at "[ \t]*$") (throw 'next nil))
7625 (skip-chars-forward " \t") (setq ind1 (current-column))
7626 (if (> ind1 ind) (throw 'next t))
7627 (if (< ind1 ind) (throw 'exit t))
7628 (if (not (org-at-item-p)) (throw 'exit nil))
7629 (delete-region (match-beginning 2) (match-end 2))
7630 (goto-char (match-beginning 2))
7631 (insert (format fmt (setq n (1+ n)))))))
7633 (move-to-column col)))
7635 (defun org-fix-bullet-type ()
7636 "Make sure all items in this list have the same bullet as the firsst item."
7638 (unless (org-at-item-p) (error "This is not a list"))
7639 (let ((line (org-current-line))
7640 (col (current-column))
7641 (ind (current-indentation))
7643 ;; find where this list begins
7644 (org-beginning-of-item-list)
7645 (beginning-of-line 1)
7646 ;; find out what the bullet type is
7647 (looking-at "[ \t]*\\(\\S-+\\)")
7648 (setq bullet (match-string 1))
7649 ;; walk forward and replace these numbers
7650 (beginning-of-line 0)
7654 (beginning-of-line 2)
7655 (if (eobp) (throw 'exit nil))
7656 (if (looking-at "[ \t]*$") (throw 'next nil))
7657 (skip-chars-forward " \t") (setq ind1 (current-column))
7658 (if (> ind1 ind) (throw 'next t))
7659 (if (< ind1 ind) (throw 'exit t))
7660 (if (not (org-at-item-p)) (throw 'exit nil))
7661 (skip-chars-forward " \t")
7662 (looking-at "\\S-+")
7663 (replace-match bullet))))
7665 (move-to-column col)
7666 (if (string-match "[0-9]" bullet)
7667 (org-renumber-ordered-list 1))))
7669 (defun org-beginning-of-item-list ()
7670 "Go to the beginning of the current item list.
7671 I.e. to the first item in this list."
7673 (org-beginning-of-item)
7674 (let ((pos (point-at-bol))
7675 (ind (org-get-indentation))
7677 ;; find where this list begins
7681 (beginning-of-line 0)
7682 (if (looking-at "[ \t]*$")
7683 (throw (if (bobp) 'exit 'next) t))
7684 (skip-chars-forward " \t") (setq ind1 (current-column))
7685 (if (or (< ind1 ind)
7687 (not (org-at-item-p)))
7690 (when (org-at-item-p) (setq pos (point-at-bol)))))))
7694 (defun org-end-of-item-list ()
7695 "Go to the end of the current item list.
7696 I.e. to the text after the last item."
7698 (org-beginning-of-item)
7699 (let ((pos (point-at-bol))
7700 (ind (org-get-indentation))
7702 ;; find where this list begins
7706 (beginning-of-line 2)
7707 (if (looking-at "[ \t]*$")
7708 (throw (if (eobp) 'exit 'next) t))
7709 (skip-chars-forward " \t") (setq ind1 (current-column))
7710 (if (or (< ind1 ind)
7712 (not (org-at-item-p)))
7715 (setq pos (point-at-bol))
7716 (throw 'exit t))))))
7720 (defvar org-last-indent-begin-marker (make-marker))
7721 (defvar org-last-indent-end-marker (make-marker))
7723 (defun org-outdent-item (arg)
7724 "Outdent a local list item."
7726 (org-indent-item (- arg)))
7728 (defun org-indent-item (arg)
7729 "Indent a local list item."
7731 (unless (org-at-item-p)
7732 (error "Not on an item"))
7734 (let (beg end ind ind1 tmp delta ind-down ind-up)
7735 (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
7736 (setq beg org-last-indent-begin-marker
7737 end org-last-indent-end-marker)
7738 (org-beginning-of-item)
7739 (setq beg (move-marker org-last-indent-begin-marker (point)))
7741 (setq end (move-marker org-last-indent-end-marker (point))))
7743 (setq tmp (org-item-indent-positions)
7745 ind-down (nth 2 tmp)
7748 (if ind-down (- ind-down ind) 2)
7749 (if ind-up (- ind-up ind) -2)))
7750 (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
7751 (while (< (point) end)
7752 (beginning-of-line 1)
7753 (skip-chars-forward " \t") (setq ind1 (current-column))
7754 (delete-region (point-at-bol) (point))
7755 (or (eolp) (indent-to-column (+ ind1 delta)))
7756 (beginning-of-line 2))))
7757 (org-fix-bullet-type)
7758 (org-maybe-renumber-ordered-list-safe)
7760 (beginning-of-line 0)
7761 (condition-case nil (org-beginning-of-item) (error nil))
7762 (org-maybe-renumber-ordered-list-safe)))
7764 (defun org-item-indent-positions ()
7765 "Return indentation for plain list items.
7766 This returns a list with three values: The current indentation, the
7767 parent indentation and the indentation a child should habe.
7768 Assumes cursor in item line."
7769 (let* ((bolpos (point-at-bol))
7770 (ind (org-get-indentation))
7771 ind-down ind-up pos)
7773 (org-beginning-of-item-list)
7774 (skip-chars-backward "\n\r \t")
7775 (when (org-in-item-p)
7776 (org-beginning-of-item)
7777 (setq ind-up (org-get-indentation))))
7781 ((and (condition-case nil (progn (org-previous-item) t)
7783 (or (forward-char 1) t)
7784 (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
7785 (setq ind-down (org-get-indentation)))
7786 ((and (goto-char pos)
7788 (goto-char (match-end 0))
7789 (skip-chars-forward " \t")
7790 (setq ind-down (current-column)))))
7791 (list ind ind-up ind-down)))
7793 ;;; The orgstruct minor mode
7795 ;; Define a minor mode which can be used in other modes in order to
7796 ;; integrate the org-mode structure editing commands.
7798 ;; This is really a hack, because the org-mode structure commands use
7799 ;; keys which normally belong to the major mode. Here is how it
7800 ;; works: The minor mode defines all the keys necessary to operate the
7801 ;; structure commands, but wraps the commands into a function which
7802 ;; tests if the cursor is currently at a headline or a plain list
7803 ;; item. If that is the case, the structure command is used,
7804 ;; temporarily setting many Org-mode variables like regular
7805 ;; expressions for filling etc. However, when any of those keys is
7806 ;; used at a different location, function uses `key-binding' to look
7807 ;; up if the key has an associated command in another currently active
7808 ;; keymap (minor modes, major mode, global), and executes that
7809 ;; command. There might be problems if any of the keys is otherwise
7810 ;; used as a prefix key.
7812 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
7813 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
7814 ;; addresses this by checking explicitly for both bindings.
7816 (defvar orgstruct-mode-map (make-sparse-keymap)
7817 "Keymap for the minor `orgstruct-mode'.")
7819 (defvar org-local-vars nil
7820 "List of local variables, for use by `orgstruct-mode'")
7823 (define-minor-mode orgstruct-mode
7824 "Toggle the minor more `orgstruct-mode'.
7825 This mode is for using Org-mode structure commands in other modes.
7826 The following key behave as if Org-mode was active, if the cursor
7827 is on a headline, or on a plain list item (both in the definition
7830 M-up Move entry/item up
7831 M-down Move entry/item down
7834 M-S-up Move entry/item up
7835 M-S-down Move entry/item down
7836 M-S-left Promote subtree
7837 M-S-right Demote subtree
7838 M-q Fill paragraph and items like in Org-mode
7840 C-c - Cycle list bullet
7841 TAB Cycle item visibility
7842 M-RET Insert new heading/item
7843 S-M-RET Insert new TODO heading / Chekbox item
7844 C-c C-c Set tags / toggle checkbox"
7845 nil " OrgStruct" nil
7846 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
7849 (defun turn-on-orgstruct ()
7850 "Unconditionally turn on `orgstruct-mode'."
7854 (defun turn-on-orgstruct++ ()
7855 "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
7856 In addition to setting orgstruct-mode, this also exports all indentation and
7857 autofilling variables from org-mode into the buffer. Note that turning
7858 off orgstruct-mode will *not* remove these additional settings."
7864 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7865 (symbol-name (car x)))
7866 (setq var (car x) val (nth 1 x))
7867 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
7870 (defun orgstruct-error ()
7871 "Error when there is no default binding for a structure key."
7873 (error "This key has no function outside structure elements"))
7875 (defun orgstruct-setup ()
7876 "Setup orgstruct keymaps."
7880 '([(meta up)] org-metaup)
7881 '([(meta down)] org-metadown)
7882 '([(meta left)] org-metaleft)
7883 '([(meta right)] org-metaright)
7884 '([(meta shift up)] org-shiftmetaup)
7885 '([(meta shift down)] org-shiftmetadown)
7886 '([(meta shift left)] org-shiftmetaleft)
7887 '([(meta shift right)] org-shiftmetaright)
7888 '([(shift up)] org-shiftup)
7889 '([(shift down)] org-shiftdown)
7890 '("\C-c\C-c" org-ctrl-c-ctrl-c)
7891 '("\M-q" fill-paragraph)
7893 '("\C-c-" org-cycle-list-bullet)))
7895 (while (setq elt (pop bindings))
7896 (setq nfunc (1+ nfunc))
7897 (setq key (org-key (car elt))
7899 cmd (orgstruct-make-binding fun nfunc key))
7900 (org-defkey orgstruct-mode-map key cmd))
7902 ;; Special treatment needed for TAB and RET
7903 (org-defkey orgstruct-mode-map [(tab)]
7904 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
7905 (org-defkey orgstruct-mode-map "\C-i"
7906 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
7908 (org-defkey orgstruct-mode-map "\M-\C-m"
7909 (orgstruct-make-binding 'org-insert-heading 105
7910 "\M-\C-m" [(meta return)]))
7911 (org-defkey orgstruct-mode-map [(meta return)]
7912 (orgstruct-make-binding 'org-insert-heading 106
7913 [(meta return)] "\M-\C-m"))
7915 (org-defkey orgstruct-mode-map [(shift meta return)]
7916 (orgstruct-make-binding 'org-insert-todo-heading 107
7917 [(meta return)] "\M-\C-m"))
7919 (unless org-local-vars
7920 (setq org-local-vars (org-get-local-variables)))
7924 (defun orgstruct-make-binding (fun n &rest keys)
7925 "Create a function for binding in the structure minor mode.
7926 FUN is the command to call inside a table. N is used to create a unique
7927 command name. KEYS are keys that should be checked in for a command
7928 to execute outside of tables."
7931 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7933 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7934 "Outside of structure, run the binding of `"
7935 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7939 '(org-context-p 'headline 'item)
7940 (list 'org-run-like-in-org-mode (list 'quote fun))
7941 (list 'let '(orgstruct-mode)
7942 (list 'call-interactively
7945 (list 'key-binding k))
7947 '('orgstruct-error))))))))
7949 (defun org-context-p (&rest contexts)
7950 "Check if local context is and of CONTEXTS.
7951 Possible values in the list of contexts are `table', `headline', and `item'."
7952 (let ((pos (point)))
7953 (goto-char (point-at-bol))
7954 (prog1 (or (and (memq 'table contexts)
7955 (looking-at "[ \t]*|"))
7956 (and (memq 'headline contexts)
7957 (looking-at "\\*+"))
7958 (and (memq 'item contexts)
7959 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
7962 (defun org-get-local-variables ()
7963 "Return a list of all local variables in an org-mode buffer."
7965 (with-current-buffer (get-buffer-create "*Org tmp*")
7968 (setq varlist (buffer-local-variables)))
7969 (kill-buffer "*Org tmp*")
7976 (list (car x) (list 'quote (cdr x)))))
7978 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7979 (symbol-name (car x)))
7984 (defun org-run-like-in-org-mode (cmd)
7985 (unless org-local-vars
7986 (setq org-local-vars (org-get-local-variables)))
7987 (eval (list 'let org-local-vars
7988 (list 'call-interactively (list 'quote cmd)))))
7992 (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
7994 (defun org-archive-subtree (&optional find-done)
7995 "Move the current subtree to the archive.
7996 The archive can be a certain top-level heading in the current file, or in
7997 a different file. The tree will be moved to that location, the subtree
7998 heading be marked DONE, and the current time will be added.
8000 When called with prefix argument FIND-DONE, find whole trees without any
8001 open TODO items and archive them (after getting confirmation from the user).
8002 If the cursor is not at a headline when this comand is called, try all level
8003 1 trees. If the cursor is on a headline, only try the direct children of
8007 (org-archive-all-done)
8008 ;; Save all relevant TODO keyword-relatex variables
8010 (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
8011 (tr-org-todo-keywords-1 org-todo-keywords-1)
8012 (tr-org-todo-kwd-alist org-todo-kwd-alist)
8013 (tr-org-done-keywords org-done-keywords)
8014 (tr-org-todo-regexp org-todo-regexp)
8015 (tr-org-todo-line-regexp org-todo-line-regexp)
8016 (tr-org-odd-levels-only org-odd-levels-only)
8017 (this-buffer (current-buffer))
8018 (org-archive-location org-archive-location)
8019 (re "^#\\+ARCHIVE:[ \t]+\\(\\S-.*\\S-\\)[ \t]*$")
8020 ;; start of variables that will be used for saving context
8021 ;; The compiler complains about them - keep them anyway!
8022 (file (abbreviate-file-name (buffer-file-name)))
8023 (olpath (mapconcat 'identity (org-get-outline-path) "/"))
8024 (time (format-time-string
8025 (substring (cdr org-time-stamp-formats) 1 -1)
8027 afile heading buffer level newfile-p
8028 category todo priority
8029 ;; start of variables that will be used for savind context
8032 ;; Try to find a local archive location
8036 (setq prop (org-entry-get nil "ARCHIVE" 'inherit))
8037 (if (and prop (string-match "\\S-" prop))
8038 (setq org-archive-location prop)
8039 (if (or (re-search-backward re nil t)
8040 (re-search-forward re nil t))
8041 (setq org-archive-location (match-string 1))))))
8043 (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
8045 (setq afile (format (match-string 1 org-archive-location)
8046 (file-name-nondirectory buffer-file-name))
8047 heading (match-string 2 org-archive-location)))
8048 (error "Invalid `org-archive-location'"))
8049 (if (> (length afile) 0)
8050 (setq newfile-p (not (file-exists-p afile))
8051 buffer (find-file-noselect afile))
8052 (setq buffer (current-buffer)))
8054 (error "Cannot access file \"%s\"" afile))
8055 (if (and (> (length heading) 0)
8056 (string-match "^\\*+" heading))
8057 (setq level (match-end 0))
8058 (setq heading nil level 0))
8060 (org-back-to-heading t)
8061 ;; Get context information that will be lost by moving the tree
8062 (org-refresh-category-properties)
8063 (setq category (org-get-category)
8064 todo (and (looking-at org-todo-line-regexp)
8066 priority (org-get-priority (if (match-end 3) (match-string 3) ""))
8067 ltags (org-get-tags)
8068 itags (org-delete-all ltags (org-get-tags-at)))
8069 (setq ltags (mapconcat 'identity ltags " ")
8070 itags (mapconcat 'identity itags " "))
8071 ;; We first only copy, in case something goes wrong
8072 ;; we need to protect this-command, to avoid kill-region sets it,
8073 ;; which would lead to duplication of subtrees
8074 (let (this-command) (org-copy-subtree))
8076 ;; Enforce org-mode for the archive buffer
8077 (if (not (org-mode-p))
8078 ;; Force the mode for future visits.
8079 (let ((org-insert-mode-line-in-empty-file t)
8080 (org-inhibit-startup t))
8081 (call-interactively 'org-mode)))
8083 (goto-char (point-max))
8084 (insert (format "\nArchived entries from file %s\n\n"
8085 (buffer-file-name this-buffer))))
8086 ;; Force the TODO keywords of the original buffer
8087 (let ((org-todo-line-regexp tr-org-todo-line-regexp)
8088 (org-todo-keywords-1 tr-org-todo-keywords-1)
8089 (org-todo-kwd-alist tr-org-todo-kwd-alist)
8090 (org-done-keywords tr-org-done-keywords)
8091 (org-todo-regexp tr-org-todo-regexp)
8092 (org-todo-line-regexp tr-org-todo-line-regexp)
8093 (org-odd-levels-only
8094 (if (local-variable-p 'org-odd-levels-only (current-buffer))
8096 tr-org-odd-levels-only)))
8097 (goto-char (point-min))
8101 (if (re-search-forward
8102 (concat "^" (regexp-quote heading)
8103 (org-re "[ \t]*\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\($\\|\r\\)"))
8105 (goto-char (match-end 0))
8106 ;; Heading not found, just insert it at the end
8107 (goto-char (point-max))
8108 (or (bolp) (insert "\n"))
8109 (insert "\n" heading "\n")
8111 ;; Make the subtree visible
8113 (org-end-of-subtree t)
8114 (skip-chars-backward " \t\r\n")
8115 (and (looking-at "[ \t\r\n]*")
8116 (replace-match "\n\n")))
8117 ;; No specific heading, just go to end of file.
8118 (goto-char (point-max)) (insert "\n"))
8120 (org-paste-subtree (org-get-legal-level level 1))
8122 ;; Mark the entry as done
8123 (when (and org-archive-mark-done
8124 (looking-at org-todo-line-regexp)
8125 (or (not (match-end 2))
8126 (not (member (match-string 2) org-done-keywords))))
8127 (let (org-log-done org-todo-log-states)
8129 (car (or (member org-archive-mark-done org-done-keywords)
8130 org-done-keywords)))))
8132 ;; Add the context info
8133 (when org-archive-save-context-info
8134 (let ((l org-archive-save-context-info) e n v)
8135 (while (setq e (pop l))
8136 (when (and (setq v (symbol-value e))
8137 (stringp v) (string-match "\\S-" v))
8138 (setq n (concat "ARCHIVE_" (upcase (symbol-name e))))
8139 (org-entry-put (point) n v)))))
8141 ;; Save and kill the buffer, if it is not the same buffer.
8142 (if (not (eq this-buffer buffer))
8143 (progn (save-buffer) (kill-buffer buffer)))))
8144 ;; Here we are back in the original buffer. Everything seems to have
8145 ;; worked. So now cut the tree and finish up.
8146 (let (this-command) (org-cut-subtree))
8147 (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
8148 (message "Subtree archived %s"
8149 (if (eq this-buffer buffer)
8150 (concat "under heading: " heading)
8151 (concat "in file: " (abbreviate-file-name afile)))))))
8153 (defun org-refresh-category-properties ()
8154 "Refresh category text properties in teh buffer."
8155 (let ((def-cat (cond
8156 ((null org-category)
8157 (if buffer-file-name
8158 (file-name-sans-extension
8159 (file-name-nondirectory buffer-file-name))
8161 ((symbolp org-category) (symbol-name org-category))
8163 beg end cat pos optionp)
8168 (goto-char (point-min))
8169 (put-text-property (point) (point-max) 'org-category def-cat)
8170 (while (re-search-forward
8171 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8172 (setq pos (match-end 0)
8173 optionp (equal (char-after (match-beginning 0)) ?#)
8174 cat (org-trim (match-string 2)))
8176 (setq beg (point-at-bol) end (point-max))
8177 (org-back-to-heading t)
8178 (setq beg (point) end (org-end-of-subtree t t)))
8179 (put-text-property beg end 'org-category cat)
8180 (goto-char pos)))))))
8182 (defun org-archive-all-done (&optional tag)
8183 "Archive sublevels of the current tree without open TODO items.
8184 If the cursor is not on a headline, try all level 1 trees. If
8185 it is on a headline, try all direct children.
8186 When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
8187 (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
8188 (rea (concat ".*:" org-archive-tag ":"))
8189 (begm (make-marker))
8190 (endm (make-marker))
8191 (question (if tag "Set ARCHIVE tag (no open TODO items)? "
8192 "Move subtree to archive (no open TODO items)? "))
8193 beg end (cntarch 0))
8194 (if (org-on-heading-p)
8196 (setq re1 (concat "^" (regexp-quote
8198 (1+ (- (match-end 0) (match-beginning 0) 1))
8201 (move-marker begm (point))
8202 (move-marker endm (org-end-of-subtree t)))
8204 (move-marker begm (point-min))
8205 (move-marker endm (point-max)))
8208 (while (re-search-forward re1 endm t)
8209 (setq beg (match-beginning 0)
8210 end (save-excursion (org-end-of-subtree t) (point)))
8212 (if (re-search-forward re end t)
8215 (if (and (or (not tag) (not (looking-at rea)))
8216 (y-or-n-p question))
8219 (org-toggle-tag org-archive-tag 'on)
8220 (org-archive-subtree))
8221 (setq cntarch (1+ cntarch)))
8223 (message "%d trees archived" cntarch)))
8225 (defun org-cycle-hide-drawers (state)
8226 "Re-hide all drawers after a visibility state change."
8227 (when (and (org-mode-p)
8228 (not (memq state '(overview folded))))
8230 (let* ((globalp (memq state '(contents all)))
8231 (beg (if globalp (point-min) (point)))
8232 (end (if globalp (point-max) (org-end-of-subtree t))))
8234 (while (re-search-forward org-drawer-regexp end t)
8235 (org-flag-drawer t))))))
8237 (defun org-flag-drawer (flag)
8239 (beginning-of-line 1)
8240 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
8241 (let ((b (match-end 0))
8242 (outline-regexp org-outline-regexp))
8243 (if (re-search-forward
8245 (save-excursion (outline-next-heading) (point)) t)
8246 (outline-flag-region b (point-at-eol) flag)
8247 (error ":END: line missing"))))))
8249 (defun org-cycle-hide-archived-subtrees (state)
8250 "Re-hide all archived subtrees after a visibility state change."
8251 (when (and (not org-cycle-open-archived-trees)
8252 (not (memq state '(overview folded))))
8254 (let* ((globalp (memq state '(contents all)))
8255 (beg (if globalp (point-min) (point)))
8256 (end (if globalp (point-max) (org-end-of-subtree t))))
8257 (org-hide-archived-subtrees beg end)
8259 (if (looking-at (concat ".*:" org-archive-tag ":"))
8260 (message "%s" (substitute-command-keys
8261 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
8263 (defun org-force-cycle-archived ()
8264 "Cycle subtree even if it is archived."
8266 (setq this-command 'org-cycle)
8267 (let ((org-cycle-open-archived-trees t))
8268 (call-interactively 'org-cycle)))
8270 (defun org-hide-archived-subtrees (beg end)
8271 "Re-hide all archived subtrees after a visibility state change."
8273 (let* ((re (concat ":" org-archive-tag ":")))
8275 (while (re-search-forward re end t)
8276 (and (org-on-heading-p) (hide-subtree))
8277 (org-end-of-subtree t)))))
8279 (defun org-toggle-tag (tag &optional onoff)
8280 "Toggle the tag TAG for the current line.
8281 If ONOFF is `on' or `off', don't toggle but set to this state."
8282 (unless (org-on-heading-p t) (error "Not on headling"))
8286 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
8289 (setq current (match-string 1))
8292 (setq current (nreverse (org-split-string current ":")))
8296 (or (member tag current) (push tag current)))
8298 (or (not (member tag current)) (setq current (delete tag current))))
8299 (t (if (member tag current)
8300 (setq current (delete tag current))
8302 (push tag current))))
8306 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
8307 (org-set-tags nil t))
8308 (delete-horizontal-space))
8309 (run-hooks 'org-after-tags-change-hook))
8312 (defun org-toggle-archive-tag (&optional arg)
8313 "Toggle the archive tag for the current headline.
8314 With prefix ARG, check all children of current headline and offer tagging
8315 the children that do not contain any open TODO items."
8318 (org-archive-all-done 'tag)
8321 (org-back-to-heading t)
8322 (setq set (org-toggle-tag org-archive-tag))
8323 (when set (hide-subtree)))
8324 (and set (beginning-of-line 1))
8325 (message "Subtree %s" (if set "archived" "unarchived")))))
8330 ;;; The table editor
8332 ;; Watch out: Here we are talking about two different kind of tables.
8333 ;; Most of the code is for the tables created with the Org-mode table editor.
8334 ;; Sometimes, we talk about tables created and edited with the table.el
8335 ;; Emacs package. We call the former org-type tables, and the latter
8336 ;; table.el-type tables.
8338 (defun org-before-change-function (beg end)
8339 "Every change indicates that a table might need an update."
8340 (setq org-table-may-need-update t))
8342 (defconst org-table-line-regexp "^[ \t]*|"
8343 "Detects an org-type table line.")
8344 (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
8345 "Detects an org-type table line.")
8346 (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
8347 "Detects a table line marked for automatic recalculation.")
8348 (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
8349 "Detects a table line marked for automatic recalculation.")
8350 (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
8351 "Detects a table line marked for automatic recalculation.")
8352 (defconst org-table-hline-regexp "^[ \t]*|-"
8353 "Detects an org-type table hline.")
8354 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
8355 "Detects a table-type table hline.")
8356 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
8357 "Detects an org-type or table-type table.")
8358 (defconst org-table-border-regexp "^[ \t]*[^| \t]"
8359 "Searching from within a table (any type) this finds the first line
8360 outside the table.")
8361 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
8362 "Searching from within a table (any type) this finds the first line
8363 outside the table.")
8365 (defvar org-table-last-highlighted-reference nil)
8366 (defvar org-table-formula-history nil)
8368 (defvar org-table-column-names nil
8369 "Alist with column names, derived from the `!' line.")
8370 (defvar org-table-column-name-regexp nil
8371 "Regular expression matching the current column names.")
8372 (defvar org-table-local-parameters nil
8373 "Alist with parameter names, derived from the `$' line.")
8374 (defvar org-table-named-field-locations nil
8375 "Alist with locations of named fields.")
8377 (defvar org-table-current-line-types nil
8378 "Table row types, non-nil only for the duration of a comand.")
8379 (defvar org-table-current-begin-line nil
8380 "Table begin line, non-nil only for the duration of a comand.")
8381 (defvar org-table-current-begin-pos nil
8382 "Table begin position, non-nil only for the duration of a comand.")
8383 (defvar org-table-dlines nil
8384 "Vector of data line line numbers in the current table.")
8385 (defvar org-table-hlines nil
8386 "Vector of hline line numbers in the current table.")
8388 (defconst org-table-range-regexp
8389 "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
8391 "Regular expression for matching ranges in formulas.")
8393 (defconst org-table-range-regexp2
8395 "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
8397 "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
8398 "Match a range for reference display.")
8400 (defconst org-table-translate-regexp
8401 (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
8402 "Match a reference that needs translation, for reference display.")
8404 (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
8406 (defun org-table-create-with-table.el ()
8407 "Use the table.el package to insert a new table.
8408 If there is already a table at point, convert between Org-mode tables
8409 and table.el tables."
8413 ((org-at-table.el-p)
8414 (if (y-or-n-p "Convert table to Org-mode table? ")
8415 (org-table-convert)))
8417 (if (y-or-n-p "Convert table to table.el table? ")
8418 (org-table-convert)))
8419 (t (call-interactively 'table-insert))))
8421 (defun org-table-create-or-convert-from-region (arg)
8422 "Convert region to table, or create an empty table.
8423 If there is an active region, convert it to a table, using the function
8424 `org-table-convert-region'. See the documentation of that function
8425 to learn how the prefix argument is interpreted to determine the field
8427 If there is no such region, create an empty table with `org-table-create'."
8429 (if (org-region-active-p)
8430 (org-table-convert-region (region-beginning) (region-end) arg)
8431 (org-table-create arg)))
8433 (defun org-table-create (&optional size)
8434 "Query for a size and insert a table skeleton.
8435 SIZE is a string Columns x Rows like for example \"3x2\"."
8438 (setq size (read-string
8439 (concat "Table size Columns x Rows [e.g. "
8440 org-table-default-size "]: ")
8441 "" nil org-table-default-size)))
8443 (let* ((pos (point))
8444 (indent (make-string (current-column) ?\ ))
8445 (split (org-split-string size " *x *"))
8446 (rows (string-to-number (nth 1 split)))
8447 (columns (string-to-number (car split)))
8448 (line (concat (apply 'concat indent "|" (make-list columns " |"))
8450 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
8451 (point-at-bol) (point)))
8452 (beginning-of-line 1)
8454 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
8455 (dotimes (i rows) (insert line))
8458 ;; Insert a hline after the first row.
8465 (defun org-table-convert-region (beg0 end0 &optional separator)
8466 "Convert region to a table.
8467 The region goes from BEG0 to END0, but these borders will be moved
8468 slightly, to make sure a beginning of line in the first line is included.
8470 SEPARATOR specifies the field separator in the lines. It can have the
8473 '(4) Use the comma as a field separator
8474 '(16) Use a TAB as field separator
8475 integer When a number, use that many spaces as field separator
8476 nil When nil, the command tries to be smart and figure out the
8477 separator in the following way:
8478 - when each line contains a TAB, assume TAB-separated material
8479 - when each line contains a comme, assume CSV material
8480 - else, assume one or more SPACE charcters as separator."
8482 (let* ((beg (min beg0 end0))
8483 (end (max beg0 end0))
8486 (beginning-of-line 1)
8487 (setq beg (move-marker (make-marker) (point)))
8489 (if (bolp) (backward-char 1) (end-of-line 1))
8490 (setq end (move-marker (make-marker) (point)))
8491 ;; Get the right field separator
8496 ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
8497 ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
8500 ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
8501 ((equal separator '(16)) "^\\|\t")
8502 ((integerp separator)
8503 (format "^ *\\| *\t *\\| \\{%d,\\}" separator))
8504 (t (error "This should not happen"))))
8506 (while (re-search-forward re end t)
8507 (replace-match "| " t t))
8512 (defun org-table-import (file arg)
8513 "Import FILE as a table.
8514 The file is assumed to be tab-separated. Such files can be produced by most
8515 spreadsheet and database applications. If no tabs (at least one per line)
8516 are found, lines will be split on whitespace into fields."
8517 (interactive "f\nP")
8518 (or (bolp) (newline))
8521 (insert-file-contents file)
8522 (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
8524 (defun org-table-export ()
8525 "Export table as a tab-separated file.
8526 Such a file can be imported into a spreadsheet program like Excel."
8528 (let* ((beg (org-table-begin))
8529 (end (org-table-end))
8530 (table (buffer-substring beg end))
8531 (file (read-file-name "Export table to: "))
8533 (unless (or (not (file-exists-p file))
8534 (y-or-n-p (format "Overwrite file %s? " file)))
8536 (with-current-buffer (find-file-noselect file)
8537 (setq buf (current-buffer))
8541 (goto-char (point-min))
8542 (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
8543 (replace-match "" t t)
8545 (goto-char (point-min))
8546 (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
8547 (replace-match "" t t)
8548 (goto-char (min (1+ (point)) (point-max))))
8549 (goto-char (point-min))
8550 (while (re-search-forward "^-[-+]*$" nil t)
8552 (if (looking-at "\n")
8554 (goto-char (point-min))
8555 (while (re-search-forward "[ \t]*|[ \t]*" nil t)
8556 (replace-match "\t" t t))
8560 (defvar org-table-aligned-begin-marker (make-marker)
8561 "Marker at the beginning of the table last aligned.
8562 Used to check if cursor still is in that table, to minimize realignment.")
8563 (defvar org-table-aligned-end-marker (make-marker)
8564 "Marker at the end of the table last aligned.
8565 Used to check if cursor still is in that table, to minimize realignment.")
8566 (defvar org-table-last-alignment nil
8567 "List of flags for flushright alignment, from the last re-alignment.
8568 This is being used to correctly align a single field after TAB or RET.")
8569 (defvar org-table-last-column-widths nil
8570 "List of max width of fields in each column.
8571 This is being used to correctly align a single field after TAB or RET.")
8572 (defvar org-table-overlay-coordinates nil
8573 "Overlay coordinates after each align of a table.")
8574 (make-variable-buffer-local 'org-table-overlay-coordinates)
8576 (defvar org-last-recalc-line nil)
8577 (defconst org-narrow-column-arrow "=>"
8578 "Used as display property in narrowed table columns.")
8580 (defun org-table-align ()
8581 "Align the table at point by aligning all vertical bars."
8585 (beg (org-table-begin))
8586 (end (org-table-end))
8587 ;; Current cursor position
8588 (linepos (org-current-line))
8589 (colpos (org-table-current-column))
8590 (winstart (window-start))
8591 (winstartline (org-current-line (min winstart (1- (point-max)))))
8592 lines (new "") lengths l typenums ty fields maxfields i
8594 (indent "") cnt frac
8600 (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
8602 (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
8603 emptystrings links dates emph narrow fmax f1 len c e)
8605 (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
8606 ;; Check if we have links or dates
8608 (setq links (re-search-forward org-bracket-link-regexp end t))
8610 (setq emph (and org-hide-emphasis-markers
8611 (re-search-forward org-emph-re end t)))
8613 (setq dates (and org-display-custom-times
8614 (re-search-forward org-ts-regexp-both end t)))
8615 ;; Make sure the link properties are right
8616 (when links (goto-char beg) (while (org-activate-bracket-links end)))
8617 ;; Make sure the date properties are right
8618 (when dates (goto-char beg) (while (org-activate-dates end)))
8619 (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
8621 ;; Check if we are narrowing any columns
8623 (setq narrow (and org-format-transports-properties-p
8624 (re-search-forward "<[0-9]+>" end t)))
8626 (setq lines (org-split-string
8627 (buffer-substring beg end) "\n"))
8628 ;; Store the indentation of the first line
8629 (if (string-match "^ *" (car lines))
8630 (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
8631 ;; Mark the hlines by setting the corresponding element to nil
8632 ;; At the same time, we remove trailing space.
8633 (setq lines (mapcar (lambda (l)
8634 (if (string-match "^ *|-" l)
8636 (if (string-match "[ \t]+$" l)
8637 (substring l 0 (match-beginning 0))
8640 ;; Get the data fields by splitting the lines.
8641 (setq fields (mapcar
8643 (org-split-string l " *| *"))
8644 (delq nil (copy-sequence lines))))
8645 ;; How many fields in the longest line?
8647 (setq maxfields (apply 'max (mapcar 'length fields)))
8649 (kill-region beg end)
8650 (org-table-create org-table-default-size)
8651 (error "Empty table - created default table")))
8652 ;; A list of empty strings to fill any short rows on output
8653 (setq emptystrings (make-list maxfields ""))
8654 ;; Check for special formatting.
8656 (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
8657 (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
8658 ;; Check if there is an explicit width specified
8660 (setq c column fmax nil)
8663 (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
8664 (setq fmax (string-to-number (match-string 1 e)) c nil)))
8665 ;; Find fields that are wider than fmax, and shorten them
8667 (loop for xx in column do
8668 (when (and (stringp xx)
8669 (> (org-string-width xx) fmax))
8670 (org-add-props xx nil
8672 (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
8673 (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
8675 (error "Cannot narrow field starting with wide link \"%s\""
8676 (match-string 0 xx)))
8677 (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
8678 (add-text-properties (- f1 2) f1
8679 (list 'display org-narrow-column-arrow)
8681 ;; Get the maximum width for each column
8682 (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
8683 ;; Get the fraction of numbers, to decide about alignment of the column
8684 (setq cnt 0 frac 0.0)
8685 (loop for x in column do
8688 (setq frac ( / (+ (* frac cnt)
8689 (if (string-match org-table-number-regexp x) 1 0))
8690 (setq cnt (1+ cnt))))))
8691 (push (>= frac org-table-number-fraction) typenums))
8692 (setq lengths (nreverse lengths) typenums (nreverse typenums))
8694 ;; Store the alignment of this table, for later editing of single fields
8695 (setq org-table-last-alignment typenums
8696 org-table-last-column-widths lengths)
8698 ;; With invisible characters, `format' does not get the field width right
8699 ;; So we need to make these fields wide by hand.
8700 (when (or links emph)
8701 (loop for i from 0 upto (1- maxfields) do
8702 (setq len (nth i lengths))
8703 (loop for j from 0 upto (1- (length fields)) do
8704 (setq c (nthcdr i (car (nthcdr j fields))))
8705 (if (and (stringp (car c))
8706 (text-property-any 0 (length (car c)) 'invisible 'org-link (car c))
8707 ; (string-match org-bracket-link-regexp (car c))
8708 (< (org-string-width (car c)) len))
8709 (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
8711 ;; Compute the formats needed for output of the table
8712 (setq rfmt (concat indent "|") hfmt (concat indent "|"))
8713 (while (setq l (pop lengths))
8714 (setq ty (if (pop typenums) "" "-")) ; number types flushright
8715 (setq rfmt (concat rfmt (format rfmt1 ty l))
8716 hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
8717 (setq rfmt (concat rfmt "\n")
8718 hfmt (concat (substring hfmt 0 -1) "|\n"))
8720 (setq new (mapconcat
8722 (if l (apply 'format rfmt
8723 (append (pop fields) emptystrings))
8726 ;; Replace the old one
8727 (delete-region beg end)
8728 (move-marker end nil)
8729 (move-marker org-table-aligned-begin-marker (point))
8731 (move-marker org-table-aligned-end-marker (point))
8732 (when (and orgtbl-mode (not (org-mode-p)))
8733 (goto-char org-table-aligned-begin-marker)
8734 (while (org-hide-wide-columns org-table-aligned-end-marker)))
8735 ;; Try to move to the old location
8736 (goto-line winstartline)
8737 (setq winstart (point-at-bol))
8739 (set-window-start (selected-window) winstart 'noforce)
8740 (org-table-goto-column colpos)
8741 (and org-table-overlay-coordinates (org-table-overlay-coordinates))
8742 (setq org-table-may-need-update nil)
8745 (defun org-string-width (s)
8746 "Compute width of string, ignoring invisible characters.
8747 This ignores character with invisibility property `org-link', and also
8748 characters with property `org-cwidth', because these will become invisible
8749 upon the next fontification round."
8751 (when (or (eq t buffer-invisibility-spec)
8752 (assq 'org-link buffer-invisibility-spec))
8753 (while (setq b (text-property-any 0 (length s)
8754 'invisible 'org-link s))
8755 (setq s (concat (substring s 0 b)
8756 (substring s (or (next-single-property-change
8757 b 'invisible s) (length s)))))))
8758 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
8759 (setq s (concat (substring s 0 b)
8760 (substring s (or (next-single-property-change
8761 b 'org-cwidth s) (length s))))))
8762 (setq l (string-width s) b -1)
8763 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
8764 (setq l (- l (get-text-property b 'org-dwidth-n s))))
8767 (defun org-table-begin (&optional table-type)
8768 "Find the beginning of the table and return its position.
8769 With argument TABLE-TYPE, go to the beginning of a table.el-type table."
8771 (if (not (re-search-backward
8772 (if table-type org-table-any-border-regexp
8773 org-table-border-regexp)
8775 (progn (goto-char (point-min)) (point))
8776 (goto-char (match-beginning 0))
8777 (beginning-of-line 2)
8780 (defun org-table-end (&optional table-type)
8781 "Find the end of the table and return its position.
8782 With argument TABLE-TYPE, go to the end of a table.el-type table."
8784 (if (not (re-search-forward
8785 (if table-type org-table-any-border-regexp
8786 org-table-border-regexp)
8788 (goto-char (point-max))
8789 (goto-char (match-beginning 0)))
8792 (defun org-table-justify-field-maybe (&optional new)
8793 "Justify the current field, text to left, number to right.
8794 Optional argument NEW may specify text to replace the current field content."
8796 ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
8797 ((org-at-table-hline-p))
8799 (or (not (equal (marker-buffer org-table-aligned-begin-marker)
8801 (< (point) org-table-aligned-begin-marker)
8802 (>= (point) org-table-aligned-end-marker)))
8803 ;; This is not the same table, force a full re-align
8804 (setq org-table-may-need-update t))
8805 (t ;; realign the current field, based on previous full realign
8806 (let* ((pos (point)) s
8807 (col (org-table-current-column))
8808 (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
8811 (skip-chars-backward "^|\n")
8812 (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
8814 (setq s (match-string 1)
8816 l (max 1 (- (match-end 0) (match-beginning 0) 3))
8817 e (not (= (match-beginning 2) (match-end 2))))
8818 (setq f (format (if num " %%%ds %s" " %%-%ds %s")
8819 l (if e "|" (setq org-table-may-need-update t) ""))
8822 (if (<= (length new) l) ;; FIXME: length -> str-width?
8823 (setq n (format f new))
8824 (setq n (concat new "|") org-table-may-need-update t)))
8826 (let (org-table-may-need-update)
8827 (replace-match n t t))))
8828 (setq org-table-may-need-update t))
8829 (goto-char pos))))))
8831 (defun org-table-next-field ()
8832 "Go to the next field in the current table, creating new lines as needed.
8833 Before doing so, re-align the table if necessary."
8835 (org-table-maybe-eval-formula)
8836 (org-table-maybe-recalculate-line)
8837 (if (and org-table-automatic-realign
8838 org-table-may-need-update)
8840 (let ((end (org-table-end)))
8841 (if (org-at-table-hline-p)
8845 (re-search-forward "|" end)
8846 (if (looking-at "[ \t]*$")
8847 (re-search-forward "|" end))
8848 (if (and (looking-at "-")
8849 org-table-tab-jumps-over-hlines
8850 (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
8851 (goto-char (match-beginning 1)))
8852 (if (looking-at "-")
8854 (beginning-of-line 0)
8855 (org-table-insert-row 'below))
8856 (if (looking-at " ") (forward-char 1))))
8858 (org-table-insert-row 'below)))))
8860 (defun org-table-previous-field ()
8861 "Go to the previous field in the table.
8862 Before doing so, re-align the table if necessary."
8864 (org-table-justify-field-maybe)
8865 (org-table-maybe-recalculate-line)
8866 (if (and org-table-automatic-realign
8867 org-table-may-need-update)
8869 (if (org-at-table-hline-p)
8871 (re-search-backward "|" (org-table-begin))
8872 (re-search-backward "|" (org-table-begin))
8873 (while (looking-at "|\\(-\\|[ \t]*$\\)")
8874 (re-search-backward "|" (org-table-begin)))
8875 (if (looking-at "| ?")
8876 (goto-char (match-end 0))))
8878 (defun org-table-next-row ()
8879 "Go to the next row (same column) in the current table.
8880 Before doing so, re-align the table if necessary."
8882 (org-table-maybe-eval-formula)
8883 (org-table-maybe-recalculate-line)
8884 (if (or (looking-at "[ \t]*$")
8885 (save-excursion (skip-chars-backward " \t") (bolp)))
8887 (if (and org-table-automatic-realign
8888 org-table-may-need-update)
8890 (let ((col (org-table-current-column)))
8891 (beginning-of-line 2)
8892 (if (or (not (org-at-table-p))
8893 (org-at-table-hline-p))
8895 (beginning-of-line 0)
8896 (org-table-insert-row 'below)))
8897 (org-table-goto-column col)
8898 (skip-chars-backward "^|\n\r")
8899 (if (looking-at " ") (forward-char 1)))))
8901 (defun org-table-copy-down (n)
8902 "Copy a field down in the current column.
8903 If the field at the cursor is empty, copy into it the content of the nearest
8904 non-empty field above. With argument N, use the Nth non-empty field.
8905 If the current field is not empty, it is copied down to the next row, and
8906 the cursor is moved with it. Therefore, repeating this command causes the
8907 column to be filled row-by-row.
8908 If the variable `org-table-copy-increment' is non-nil and the field is an
8909 integer or a timestamp, it will be incremented while copying. In the case of
8910 a timestamp, if the cursor is on the year, change the year. If it is on the
8911 month or the day, change that. Point will stay on the current date field
8912 in order to easily repeat the interval."
8914 (let* ((colpos (org-table-current-column))
8915 (col (current-column))
8916 (field (org-table-get-field))
8917 (non-empty (string-match "[^ \t]" field))
8918 (beg (org-table-begin))
8920 (org-table-check-inside-data-field)
8923 (setq txt (org-trim field))
8924 (org-table-next-row)
8925 (org-table-blank-field))
8929 (while (progn (beginning-of-line 1)
8930 (re-search-backward org-table-dataline-regexp
8932 (org-table-goto-column colpos t)
8933 (if (and (looking-at
8934 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
8935 (= (setq n (1- n)) 0))
8936 (throw 'exit (match-string 1))))))))
8939 (if (and org-table-copy-increment
8940 (string-match "^[0-9]+$" txt))
8941 (setq txt (format "%d" (+ (string-to-number txt) 1))))
8943 (move-to-column col)
8944 (if (and org-table-copy-increment (org-at-timestamp-p t))
8945 (org-timestamp-up 1)
8946 (org-table-maybe-recalculate-line))
8948 (move-to-column col))
8949 (error "No non-empty field found"))))
8951 (defun org-table-check-inside-data-field ()
8952 "Is point inside a table data field?
8953 I.e. not on a hline or before the first or after the last column?
8954 This actually throws an error, so it aborts the current command."
8955 (if (or (not (org-at-table-p))
8956 (= (org-table-current-column) 0)
8957 (org-at-table-hline-p)
8958 (looking-at "[ \t]*$"))
8959 (error "Not in table data field")))
8961 (defvar org-table-clip nil
8962 "Clipboard for table regions.")
8964 (defun org-table-blank-field ()
8965 "Blank the current table field or active region."
8967 (org-table-check-inside-data-field)
8968 (if (and (interactive-p) (org-region-active-p))
8969 (let (org-table-clip)
8970 (org-table-cut-region (region-beginning) (region-end)))
8971 (skip-chars-backward "^|")
8973 (if (looking-at "|[^|\n]+")
8974 (let* ((pos (match-beginning 0))
8975 (match (match-string 0))
8976 (len (org-string-width match)))
8977 (replace-match (concat "|" (make-string (1- len) ?\ )))
8978 (goto-char (+ 2 pos))
8979 (substring match 1)))))
8981 (defun org-table-get-field (&optional n replace)
8982 "Return the value of the field in column N of current row.
8983 N defaults to current field.
8984 If REPLACE is a string, replace field with this value. The return value
8985 is always the old value."
8986 (and n (org-table-goto-column n))
8987 (skip-chars-backward "^|\n")
8989 (if (looking-at "|[^|\r\n]*")
8990 (let* ((pos (match-beginning 0))
8991 (val (buffer-substring (1+ pos) (match-end 0))))
8993 (replace-match (concat "|" replace) t t))
8994 (goto-char (min (point-at-eol) (+ 2 pos)))
8996 (forward-char 1) ""))
8998 (defun org-table-field-info (arg)
8999 "Show info about the current field, and highlight any reference at point."
9001 (org-table-get-specials)
9003 (let* ((pos (point))
9004 (col (org-table-current-column))
9005 (cname (car (rassoc (int-to-string col) org-table-column-names)))
9006 (name (car (rassoc (list (org-current-line) col)
9007 org-table-named-field-locations)))
9008 (eql (org-table-get-stored-formulas))
9009 (dline (org-table-current-dline))
9010 (ref (format "@%d$%d" dline col))
9011 (ref1 (org-table-convert-refs-to-an ref))
9012 (fequation (or (assoc name eql) (assoc ref eql)))
9013 (cequation (assoc (int-to-string col) eql))
9014 (eqn (or fequation cequation)))
9017 (org-table-show-reference 'local)
9019 (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
9021 (if cname (concat " or $" cname) "")
9023 (if name (concat " or $" name) "")
9024 ;; FIXME: formula info not correct if special table line
9026 (concat ", formula: "
9027 (org-table-formula-to-user
9029 (if (string-match "^[$@]"(car eqn)) "" "$")
9030 (car eqn) "=" (cdr eqn))))
9033 (defun org-table-current-column ()
9034 "Find out which column we are in.
9035 When called interactively, column is also displayed in echo area."
9037 (if (interactive-p) (org-table-check-inside-data-field))
9039 (let ((cnt 0) (pos (point)))
9040 (beginning-of-line 1)
9041 (while (search-forward "|" pos t)
9042 (setq cnt (1+ cnt)))
9043 (if (interactive-p) (message "This is table column %d" cnt))
9046 (defun org-table-current-dline ()
9047 "Find out what table data line we are in.
9048 Only datalins count for this."
9050 (if (interactive-p) (org-table-check-inside-data-field))
9052 (let ((cnt 0) (pos (point)))
9053 (goto-char (org-table-begin))
9054 (while (<= (point) pos)
9055 (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
9056 (beginning-of-line 2))
9057 (if (interactive-p) (message "This is table line %d" cnt))
9060 (defun org-table-goto-column (n &optional on-delim force)
9061 "Move the cursor to the Nth column in the current table line.
9062 With optional argument ON-DELIM, stop with point before the left delimiter
9064 If there are less than N fields, just go to after the last delimiter.
9065 However, when FORCE is non-nil, create new columns if necessary."
9067 (let ((pos (point-at-eol)))
9068 (beginning-of-line 1)
9070 (while (and (> (setq n (1- n)) -1)
9071 (or (search-forward "|" pos t)
9073 (progn (end-of-line 1)
9074 (skip-chars-backward "^|")
9076 ; (backward-char 2) t)))))
9077 (when (and force (not (looking-at ".*|")))
9078 (save-excursion (end-of-line 1) (insert " | ")))
9081 (if (looking-at " ") (forward-char 1))))))
9083 (defun org-at-table-p (&optional table-type)
9084 "Return t if the cursor is inside an org-type table.
9085 If TABLE-TYPE is non-nil, also check for table.el-type tables."
9086 (if org-enable-table-editor
9088 (beginning-of-line 1)
9089 (looking-at (if table-type org-table-any-line-regexp
9090 org-table-line-regexp)))
9093 (defun org-at-table.el-p ()
9094 "Return t if and only if we are at a table.el table."
9095 (and (org-at-table-p 'any)
9097 (goto-char (org-table-begin 'any))
9098 (looking-at org-table1-hline-regexp))))
9100 (defun org-table-recognize-table.el ()
9101 "If there is a table.el table nearby, recognize it and move into it."
9102 (if org-table-tab-recognizes-table.el
9103 (if (org-at-table.el-p)
9105 (beginning-of-line 1)
9106 (if (looking-at org-table-dataline-regexp)
9108 (if (looking-at org-table1-hline-regexp)
9110 (beginning-of-line 2)
9111 (if (looking-at org-table-any-border-regexp)
9112 (beginning-of-line -1)))))
9113 (if (re-search-forward "|" (org-table-end t) t)
9116 (if (table--at-cell-p (point))
9118 (message "recognizing table.el table...")
9119 (table-recognize-table)
9120 (message "recognizing table.el table...done")))
9121 (error "This should not happen..."))
9126 (defun org-at-table-hline-p ()
9127 "Return t if the cursor is inside a hline in a table."
9128 (if org-enable-table-editor
9130 (beginning-of-line 1)
9131 (looking-at org-table-hline-regexp))
9134 (defun org-table-insert-column ()
9135 "Insert a new column into the table."
9137 (if (not (org-at-table-p))
9138 (error "Not at a table"))
9139 (org-table-find-dataline)
9140 (let* ((col (max 1 (org-table-current-column)))
9141 (beg (org-table-begin))
9142 (end (org-table-end))
9143 ;; Current cursor position
9144 (linepos (org-current-line))
9147 (while (< (point) end)
9148 (if (org-at-table-hline-p)
9150 (org-table-goto-column col t)
9152 (beginning-of-line 2))
9153 (move-marker end nil)
9155 (org-table-goto-column colpos)
9157 (org-table-fix-formulas "$" nil (1- col) 1)))
9159 (defun org-table-find-dataline ()
9160 "Find a dataline in the current table, which is needed for column commands."
9161 (if (and (org-at-table-p)
9162 (not (org-at-table-hline-p)))
9164 (let ((col (current-column))
9165 (end (org-table-end)))
9166 (move-to-column col)
9167 (while (and (< (point) end)
9168 (or (not (= (current-column) col))
9169 (org-at-table-hline-p)))
9170 (beginning-of-line 2)
9171 (move-to-column col))
9172 (if (and (org-at-table-p)
9173 (not (org-at-table-hline-p)))
9176 "Please position cursor in a data line for column operations")))))
9178 (defun org-table-delete-column ()
9179 "Delete a column from the table."
9181 (if (not (org-at-table-p))
9182 (error "Not at a table"))
9183 (org-table-find-dataline)
9184 (org-table-check-inside-data-field)
9185 (let* ((col (org-table-current-column))
9186 (beg (org-table-begin))
9187 (end (org-table-end))
9188 ;; Current cursor position
9189 (linepos (org-current-line))
9192 (while (< (point) end)
9193 (if (org-at-table-hline-p)
9195 (org-table-goto-column col t)
9196 (and (looking-at "|[^|\n]+|")
9197 (replace-match "|")))
9198 (beginning-of-line 2))
9199 (move-marker end nil)
9201 (org-table-goto-column colpos)
9203 (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
9206 (defun org-table-move-column-right ()
9207 "Move column to the right."
9209 (org-table-move-column nil))
9210 (defun org-table-move-column-left ()
9211 "Move column to the left."
9213 (org-table-move-column 'left))
9215 (defun org-table-move-column (&optional left)
9216 "Move the current column to the right. With arg LEFT, move to the left."
9218 (if (not (org-at-table-p))
9219 (error "Not at a table"))
9220 (org-table-find-dataline)
9221 (org-table-check-inside-data-field)
9222 (let* ((col (org-table-current-column))
9223 (col1 (if left (1- col) col))
9224 (beg (org-table-begin))
9225 (end (org-table-end))
9226 ;; Current cursor position
9227 (linepos (org-current-line))
9228 (colpos (if left (1- col) (1+ col))))
9229 (if (and left (= col 1))
9230 (error "Cannot move column further left"))
9231 (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
9232 (error "Cannot move column further right"))
9234 (while (< (point) end)
9235 (if (org-at-table-hline-p)
9237 (org-table-goto-column col1 t)
9238 (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
9239 (replace-match "|\\2|\\1|")))
9240 (beginning-of-line 2))
9241 (move-marker end nil)
9243 (org-table-goto-column colpos)
9245 (org-table-fix-formulas
9246 "$" (list (cons (number-to-string col) (number-to-string colpos))
9247 (cons (number-to-string colpos) (number-to-string col))))))
9249 (defun org-table-move-row-down ()
9250 "Move table row down."
9252 (org-table-move-row nil))
9253 (defun org-table-move-row-up ()
9254 "Move table row up."
9256 (org-table-move-row 'up))
9258 (defun org-table-move-row (&optional up)
9259 "Move the current table line down. With arg UP, move it up."
9261 (let* ((col (current-column))
9263 (hline1p (save-excursion (beginning-of-line 1)
9264 (looking-at org-table-hline-regexp)))
9265 (dline1 (org-table-current-dline))
9266 (dline2 (+ dline1 (if up -1 1)))
9269 (beginning-of-line tonew)
9270 (unless (org-at-table-p)
9272 (error "Cannot move row further"))
9273 (setq hline2p (looking-at org-table-hline-regexp))
9275 (beginning-of-line 1)
9277 (setq txt (buffer-substring (point) (1+ (point-at-eol))))
9278 (delete-region (point) (1+ (point-at-eol)))
9279 (beginning-of-line tonew)
9281 (beginning-of-line 0)
9282 (move-to-column col)
9283 (unless (or hline1p hline2p)
9284 (org-table-fix-formulas
9285 "@" (list (cons (number-to-string dline1) (number-to-string dline2))
9286 (cons (number-to-string dline2) (number-to-string dline1)))))))
9288 (defun org-table-insert-row (&optional arg)
9289 "Insert a new row above the current line into the table.
9290 With prefix ARG, insert below the current line."
9292 (if (not (org-at-table-p))
9293 (error "Not at a table"))
9294 (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
9295 (new (org-table-clean-line line)))
9296 ;; Fix the first field if necessary
9297 (if (string-match "^[ \t]*| *[#$] *|" line)
9298 (setq new (replace-match (match-string 0 line) t t new)))
9299 (beginning-of-line (if arg 2 1))
9300 (let (org-table-may-need-update) (insert-before-markers new "\n"))
9301 (beginning-of-line 0)
9302 (re-search-forward "| ?" (point-at-eol) t)
9303 (and (or org-table-may-need-update org-table-overlay-coordinates)
9305 (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))
9307 (defun org-table-insert-hline (&optional above)
9308 "Insert a horizontal-line below the current line into the table.
9309 With prefix ABOVE, insert above the current line."
9311 (if (not (org-at-table-p))
9312 (error "Not at a table"))
9313 (let ((line (org-table-clean-line
9314 (buffer-substring (point-at-bol) (point-at-eol))))
9315 (col (current-column)))
9316 (while (string-match "|\\( +\\)|" line)
9317 (setq line (replace-match
9318 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
9319 ?-) "|") t t line)))
9320 (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
9321 (beginning-of-line (if above 1 2))
9323 (beginning-of-line (if above 1 -1))
9324 (move-to-column col)
9325 (and org-table-overlay-coordinates (org-table-align))))
9327 (defun org-table-hline-and-move (&optional same-column)
9328 "Insert a hline and move to the row below that line."
9330 (let ((col (org-table-current-column)))
9331 (org-table-maybe-eval-formula)
9332 (org-table-maybe-recalculate-line)
9333 (org-table-insert-hline)
9335 (if (looking-at "\n[ \t]*|-")
9336 (progn (insert "\n|") (org-table-align))
9337 (org-table-next-field))
9338 (if same-column (org-table-goto-column col))))
9340 (defun org-table-clean-line (s)
9341 "Convert a table line S into a string with only \"|\" and space.
9342 In particular, this does handle wide and invisible characters."
9343 (if (string-match "^[ \t]*|-" s)
9344 ;; It's a hline, just map the characters
9345 (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
9346 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
9347 (setq s (replace-match
9348 (concat "|" (make-string (org-string-width (match-string 1 s))
9353 (defun org-table-kill-row ()
9354 "Delete the current row or horizontal line from the table."
9356 (if (not (org-at-table-p))
9357 (error "Not at a table"))
9358 (let ((col (current-column))
9359 (dline (org-table-current-dline)))
9360 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
9361 (if (not (org-at-table-p)) (beginning-of-line 0))
9362 (move-to-column col)
9363 (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
9366 (defun org-table-sort-lines (with-case &optional sorting-type)
9367 "Sort table lines according to the column at point.
9369 The position of point indicates the column to be used for
9370 sorting, and the range of lines is the range between the nearest
9371 horizontal separator lines, or the entire table of no such lines
9372 exist. If point is before the first column, you will be prompted
9373 for the sorting column. If there is an active region, the mark
9374 specifies the first line and the sorting column, while point
9375 should be in the last line to be included into the sorting.
9377 The command then prompts for the sorting type which can be
9378 alphabetically, numerically, or by time (as given in a time stamp
9379 in the field). Sorting in reverse order is also possible.
9381 With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
9383 If SORTING-TYPE is specified when this function is called from a Lisp
9384 program, no prompting will take place. SORTING-TYPE must be a character,
9385 any of (?a ?A ?n ?N ?t ?T) where the capital letter indicate that sorting
9386 should be done in reverse order."
9388 (let* ((thisline (org-current-line))
9389 (thiscol (org-table-current-column))
9390 beg end bcol ecol tend tbeg column lns pos)
9391 (when (equal thiscol 0)
9395 (read-string "Use column N for sorting: ")))
9397 (org-table-goto-column thiscol))
9398 (org-table-check-inside-data-field)
9399 (if (org-region-active-p)
9401 (setq beg (region-beginning) end (region-end))
9403 (setq column (org-table-current-column)
9406 (setq end (point-at-bol 2)))
9407 (setq column (org-table-current-column)
9409 tbeg (org-table-begin)
9410 tend (org-table-end))
9411 (if (re-search-backward org-table-hline-regexp tbeg t)
9412 (setq beg (point-at-bol 2))
9414 (setq beg (point-at-bol 1)))
9416 (if (re-search-forward org-table-hline-regexp tend t)
9417 (setq end (point-at-bol 1))
9419 (setq end (point-at-bol))))
9420 (setq beg (move-marker (make-marker) beg)
9421 end (move-marker (make-marker) end))
9424 (org-table-goto-column column)
9425 (skip-chars-backward "^|")
9426 (setq bcol (current-column))
9427 (org-table-goto-column (1+ column))
9428 (skip-chars-backward "^|")
9429 (setq ecol (1- (current-column)))
9430 (org-table-goto-column column)
9431 (setq lns (mapcar (lambda(x) (cons
9432 (org-sort-remove-invisible
9434 (org-split-string x "[ \t]*|[ \t]*")))
9436 (org-split-string (buffer-substring beg end) "\n")))
9437 (setq lns (org-do-sort lns "Table" with-case sorting-type))
9438 (delete-region beg end)
9439 (move-marker beg nil)
9440 (move-marker end nil)
9441 (insert (mapconcat 'cdr lns "\n") "\n")
9442 (goto-line thisline)
9443 (org-table-goto-column thiscol)
9444 (message "%d lines sorted, based on column %d" (length lns) column)))
9446 ;; FIXME: maybe we will not need this? Table sorting is broken....
9447 (defun org-sort-remove-invisible (s)
9448 (remove-text-properties 0 (length s) org-rm-props s)
9449 (while (string-match org-bracket-link-regexp s)
9450 (setq s (replace-match (if (match-end 2)
9452 (match-string 1 s)) t t s)))
9455 (defun org-table-cut-region (beg end)
9456 "Copy region in table to the clipboard and blank all relevant fields."
9458 (org-table-copy-region beg end 'cut))
9460 (defun org-table-copy-region (beg end &optional cut)
9461 "Copy rectangular region in table to clipboard.
9462 A special clipboard is used which can only be accessed
9463 with `org-table-paste-rectangle'."
9465 (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
9467 (rpl (if cut " " nil)))
9469 (org-table-check-inside-data-field)
9470 (setq l01 (org-current-line)
9471 c01 (org-table-current-column))
9473 (org-table-check-inside-data-field)
9474 (setq l02 (org-current-line)
9475 c02 (org-table-current-column))
9476 (setq l1 (min l01 l02) l2 (max l01 l02)
9477 c1 (min c01 c02) c2 (max c01 c02))
9481 (if (> l1 l2) (throw 'exit t))
9483 (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
9484 (setq cols nil ic1 c1 ic2 c2)
9485 (while (< ic1 (1+ ic2))
9486 (push (org-table-get-field ic1 rpl) cols)
9487 (setq ic1 (1+ ic1)))
9488 (push (nreverse cols) region)
9489 (setq l1 (1+ l1)))))
9490 (setq org-table-clip (nreverse region))
9491 (if cut (org-table-align))
9494 (defun org-table-paste-rectangle ()
9495 "Paste a rectangular region into a table.
9496 The upper right corner ends up in the current field. All involved fields
9497 will be overwritten. If the rectangle does not fit into the present table,
9498 the table is enlarged as needed. The process ignores horizontal separator
9501 (unless (and org-table-clip (listp org-table-clip))
9502 (error "First cut/copy a region to paste!"))
9503 (org-table-check-inside-data-field)
9504 (let* ((clip org-table-clip)
9505 (line (org-current-line))
9506 (col (org-table-current-column))
9507 (org-enable-table-editor t)
9508 (org-table-automatic-realign nil)
9510 (while (setq cols (pop clip))
9511 (while (org-at-table-hline-p) (beginning-of-line 2))
9512 (if (not (org-at-table-p))
9513 (progn (end-of-line 0) (org-table-next-field)))
9515 (while (setq field (pop cols))
9516 (org-table-goto-column c nil 'force)
9517 (org-table-get-field nil field)
9519 (beginning-of-line 2))
9521 (org-table-goto-column col)
9524 (defun org-table-convert ()
9525 "Convert from `org-mode' table to table.el and back.
9526 Obviously, this only works within limits. When an Org-mode table is
9527 converted to table.el, all horizontal separator lines get lost, because
9528 table.el uses these as cell boundaries and has no notion of horizontal lines.
9529 A table.el table can be converted to an Org-mode table only if it does not
9530 do row or column spanning. Multiline cells will become multiple cells.
9531 Beware, Org-mode does not test if the table can be successfully converted - it
9532 blindly applies a recipe that works for simple tables."
9535 (if (org-at-table.el-p)
9536 ;; convert to Org-mode table
9537 (let ((beg (move-marker (make-marker) (org-table-begin t)))
9538 (end (move-marker (make-marker) (org-table-end t))))
9539 (table-unrecognize-region beg end)
9541 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
9544 (if (org-at-table-p)
9545 ;; convert to table.el table
9546 (let ((beg (move-marker (make-marker) (org-table-begin)))
9547 (end (move-marker (make-marker) (org-table-end))))
9548 ;; first, get rid of all horizontal lines
9550 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
9552 ;; insert a hline before first
9554 (org-table-insert-hline 'above)
9555 (beginning-of-line -1)
9556 ;; insert a hline after each line
9557 (while (progn (beginning-of-line 3) (< (point) end))
9558 (org-table-insert-hline))
9560 (setq end (move-marker end (org-table-end)))
9561 ;; replace "+" at beginning and ending of hlines
9562 (while (re-search-forward "^\\([ \t]*\\)|-" end t)
9563 (replace-match "\\1+-"))
9565 (while (re-search-forward "-|[ \t]*$" end t)
9566 (replace-match "-+"))
9569 (defun org-table-wrap-region (arg)
9570 "Wrap several fields in a column like a paragraph.
9571 This is useful if you'd like to spread the contents of a field over several
9572 lines, in order to keep the table compact.
9574 If there is an active region, and both point and mark are in the same column,
9575 the text in the column is wrapped to minimum width for the given number of
9576 lines. Generally, this makes the table more compact. A prefix ARG may be
9577 used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
9578 formats the selected text to two lines. If the region was longer than two
9579 lines, the remaining lines remain empty. A negative prefix argument reduces
9580 the current number of lines by that amount. The wrapped text is pasted back
9581 into the table. If you formatted it to more lines than it was before, fields
9582 further down in the table get overwritten - so you might need to make space in
9585 If there is no region, the current field is split at the cursor position and
9586 the text fragment to the right of the cursor is prepended to the field one
9589 If there is no region, but you specify a prefix ARG, the current field gets
9590 blank, and the content is appended to the field above."
9592 (org-table-check-inside-data-field)
9593 (if (org-region-active-p)
9594 ;; There is a region: fill as a paragraph
9595 (let* ((beg (region-beginning))
9596 (cline (save-excursion (goto-char beg) (org-current-line)))
9597 (ccol (save-excursion (goto-char beg) (org-table-current-column)))
9599 (org-table-cut-region (region-beginning) (region-end))
9600 (if (> (length (car org-table-clip)) 1)
9601 (error "Region must be limited to single column"))
9602 (setq nlines (if arg
9604 (+ (length org-table-clip) arg)
9606 (length org-table-clip)))
9607 (setq org-table-clip
9608 (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
9611 (org-table-goto-column ccol)
9612 (org-table-paste-rectangle))
9613 ;; No region, split the current field at point
9614 (unless (org-get-alist-option org-M-RET-may-split-line 'table)
9615 (skip-chars-forward "^\r\n|"))
9617 ;; combine with field above
9618 (let ((s (org-table-blank-field))
9619 (col (org-table-current-column)))
9620 (beginning-of-line 0)
9621 (while (org-at-table-hline-p) (beginning-of-line 0))
9622 (org-table-goto-column col)
9623 (skip-chars-forward "^|")
9624 (skip-chars-backward " ")
9625 (insert " " (org-trim s))
9628 (if (looking-at "\\([^|]+\\)+|")
9629 (let ((s (match-string 1)))
9630 (replace-match " |")
9631 (goto-char (match-beginning 0))
9632 (org-table-next-row)
9633 (insert (org-trim s) " ")
9635 (org-table-next-row)))))
9637 (defvar org-field-marker nil)
9639 (defun org-table-edit-field (arg)
9640 "Edit table field in a different window.
9641 This is mainly useful for fields that contain hidden parts.
9642 When called with a \\[universal-argument] prefix, just make the full field visible so that
9643 it can be edited in place."
9646 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
9647 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
9648 (remove-text-properties b e '(org-cwidth t invisible t
9649 display t intangible t))
9650 (if (and (boundp 'font-lock-mode) font-lock-mode)
9651 (font-lock-fontify-block)))
9652 (let ((pos (move-marker (make-marker) (point)))
9653 (field (org-table-get-field))
9654 (cw (current-window-configuration))
9656 (org-switch-to-buffer-other-window "*Org tmp*")
9658 (insert "#\n# Edit field and finish with C-c C-c\n#\n")
9659 (let ((org-inhibit-startup t)) (org-mode))
9660 (goto-char (setq p (point-max)))
9661 (insert (org-trim field))
9662 (remove-text-properties p (point-max)
9663 '(invisible t org-cwidth t display t
9666 (org-set-local 'org-finish-function 'org-table-finish-edit-field)
9667 (org-set-local 'org-window-configuration cw)
9668 (org-set-local 'org-field-marker pos)
9669 (message "Edit and finish with C-c C-c"))))
9671 (defun org-table-finish-edit-field ()
9672 "Finish editing a table data field.
9673 Remove all newline characters, insert the result into the table, realign
9674 the table and kill the editing buffer."
9675 (let ((pos org-field-marker)
9676 (cw org-window-configuration)
9677 (cb (current-buffer))
9679 (goto-char (point-min))
9680 (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
9681 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
9682 (replace-match " "))
9683 (setq text (org-trim (buffer-string)))
9684 (set-window-configuration cw)
9686 (select-window (get-buffer-window (marker-buffer pos)))
9688 (move-marker pos nil)
9689 (org-table-check-inside-data-field)
9690 (org-table-get-field nil text)
9692 (message "New field value inserted")))
9695 "Remove whitespace at beginning and end of string."
9696 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
9697 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
9700 (defun org-wrap (string &optional width lines)
9701 "Wrap string to either a number of lines, or a width in characters.
9702 If WIDTH is non-nil, the string is wrapped to that width, however many lines
9703 that costs. If there is a word longer than WIDTH, the text is actually
9704 wrapped to the length of that word.
9705 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
9706 many lines, whatever width that takes.
9707 The return value is a list of lines, without newlines at the end."
9708 (let* ((words (org-split-string string "[ \t\n]+"))
9709 (maxword (apply 'max (mapcar 'org-string-width words)))
9712 (org-do-wrap words (max maxword width)))
9715 (setq ll (org-do-wrap words maxword))
9716 (if (<= (length ll) lines)
9719 (while (> (length ll) lines)
9721 (setq ll (org-do-wrap words w)))
9723 (t (error "Cannot wrap this")))))
9726 (defun org-do-wrap (words width)
9727 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
9730 (setq line (pop words))
9731 (while (and words (< (+ (length line) (length (car words))) width))
9732 (setq line (concat line " " (pop words))))
9733 (setq lines (push line lines)))
9736 (defun org-split-string (string &optional separators)
9737 "Splits STRING into substrings at SEPARATORS.
9738 No empty strings are returned if there are matches at the beginning
9740 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
9744 (while (and (string-match rexp string
9746 (= start (match-beginning 0))
9747 (< start (length string)))
9749 (< (match-beginning 0) (length string)))
9751 (or (eq (match-beginning 0) 0)
9752 (and (eq (match-beginning 0) (match-end 0))
9753 (eq (match-beginning 0) start))
9755 (cons (substring string start (match-beginning 0))
9757 (setq start (match-end 0)))
9758 (or (eq start (length string))
9760 (cons (substring string start)
9764 (defun org-table-map-tables (function)
9765 "Apply FUNCTION to the start of all tables in the buffer."
9769 (goto-char (point-min))
9770 (while (re-search-forward org-table-any-line-regexp nil t)
9771 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
9772 (beginning-of-line 1)
9773 (if (looking-at org-table-line-regexp)
9774 (save-excursion (funcall function)))
9775 (re-search-forward org-table-any-border-regexp nil 1))))
9776 (message "Mapping tables: done"))
9778 (defvar org-timecnt) ; dynamically scoped parameter
9780 (defun org-table-sum (&optional beg end nlast)
9781 "Sum numbers in region of current table column.
9782 The result will be displayed in the echo area, and will be available
9783 as kill to be inserted with \\[yank].
9785 If there is an active region, it is interpreted as a rectangle and all
9786 numbers in that rectangle will be summed. If there is no active
9787 region and point is located in a table column, sum all numbers in that
9790 If at least one number looks like a time HH:MM or HH:MM:SS, all other
9791 numbers are assumed to be times as well (in decimal hours) and the
9792 numbers are added as such.
9794 If NLAST is a number, only the NLAST fields will actually be summed."
9797 (let (col (org-timecnt 0) diff h m s org-table-clip)
9799 ((and beg end)) ; beg and end given explicitly
9800 ((org-region-active-p)
9801 (setq beg (region-beginning) end (region-end)))
9803 (setq col (org-table-current-column))
9804 (goto-char (org-table-begin))
9805 (unless (re-search-forward "^[ \t]*|[^-]" nil t)
9806 (error "No table data"))
9807 (org-table-goto-column col)
9809 (goto-char (org-table-end))
9810 (unless (re-search-backward "^[ \t]*|[^-]" nil t)
9811 (error "No table data"))
9812 (org-table-goto-column col)
9813 (setq end (point))))
9814 (let* ((items (apply 'append (org-table-copy-region beg end)))
9815 (items1 (cond ((not nlast) items)
9816 ((>= nlast (length items)) items)
9817 (t (setq items (reverse items))
9818 (setcdr (nthcdr (1- nlast) items) nil)
9820 (numbers (delq nil (mapcar 'org-table-get-number-for-summing
9822 (res (apply '+ numbers))
9823 (sres (if (= org-timecnt 0)
9825 (setq diff (* 3600 res)
9826 h (floor (/ diff 3600)) diff (mod diff 3600)
9827 m (floor (/ diff 60)) diff (mod diff 60)
9829 (format "%d:%02d:%02d" h m s))))
9833 (substitute-command-keys
9834 (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
9835 (length numbers) sres))))
9838 (defun org-table-get-number-for-summing (s)
9840 (if (string-match "^ *|? *" s)
9841 (setq s (replace-match "" nil nil s)))
9842 (if (string-match " *|? *$" s)
9843 (setq s (replace-match "" nil nil s)))
9844 (setq n (string-to-number s))
9846 ((and (string-match "0" s)
9847 (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
9848 ((string-match "\\`[ \t]+\\'" s) nil)
9849 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
9850 (let ((h (string-to-number (or (match-string 1 s) "0")))
9851 (m (string-to-number (or (match-string 2 s) "0")))
9852 (s (string-to-number (or (match-string 4 s) "0"))))
9853 (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
9854 (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
9858 (defun org-table-current-field-formula (&optional key noerror)
9859 "Return the formula active for the current field.
9860 Assumes that specials are in place.
9861 If KEY is given, return the key to this formula.
9862 Otherwise return the formula preceeded with \"=\" or \":=\"."
9863 (let* ((name (car (rassoc (list (org-current-line)
9864 (org-table-current-column))
9865 org-table-named-field-locations)))
9866 (col (org-table-current-column))
9867 (scol (int-to-string col))
9868 (ref (format "@%d$%d" (org-table-current-dline) col))
9869 (stored-list (org-table-get-stored-formulas noerror))
9870 (ass (or (assoc name stored-list)
9871 (assoc ref stored-list)
9872 (assoc scol stored-list))))
9875 (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
9878 (defun org-table-get-formula (&optional equation named)
9879 "Read a formula from the minibuffer, offer stored formula as default.
9880 When NAMED is non-nil, look for a named equation."
9881 (let* ((stored-list (org-table-get-stored-formulas))
9882 (name (car (rassoc (list (org-current-line)
9883 (org-table-current-column))
9884 org-table-named-field-locations)))
9885 (ref (format "@%d$%d" (org-table-current-dline)
9886 (org-table-current-column)))
9887 (refass (assoc ref stored-list))
9890 (int-to-string (org-table-current-column))))
9891 (dummy (and (or name refass) (not named)
9892 (not (y-or-n-p "Replace field formula with column formula? " ))
9894 (name (or name ref))
9895 (org-table-may-need-update nil)
9896 (stored (cdr (assoc scol stored-list)))
9898 ((and stored equation (string-match "^ *=? *$" equation))
9902 (t (org-table-formula-from-user
9904 (org-table-formula-to-user
9905 (format "%s formula %s%s="
9906 (if named "Field" "Column")
9907 (if (member (string-to-char scol) '(?$ ?@)) "" "$")
9909 (if stored (org-table-formula-to-user stored) "")
9910 'org-table-formula-history
9913 (when (not (string-match "\\S-" eq))
9915 (setq stored-list (delq (assoc scol stored-list) stored-list))
9916 (org-table-store-formulas stored-list)
9917 (error "Formula removed"))
9918 (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
9919 (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
9920 (if (and name (not named))
9921 ;; We set the column equation, delete the named one.
9922 (setq stored-list (delq (assoc name stored-list) stored-list)
9925 (setcdr (assoc scol stored-list) eq)
9926 (setq stored-list (cons (cons scol eq) stored-list)))
9927 (if (or mustsave (not (equal stored eq)))
9928 (org-table-store-formulas stored-list))
9931 (defun org-table-store-formulas (alist)
9932 "Store the list of formulas below the current table."
9933 (setq alist (sort alist 'org-table-formula-less-p))
9935 (goto-char (org-table-end))
9936 (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:\\(.*\n?\\)")
9938 ;; don't overwrite TBLFM, we might use text properties to store stuff
9939 (goto-char (match-beginning 2))
9940 (delete-region (match-beginning 2) (match-end 0)))
9941 (insert "#+TBLFM:"))
9943 (mapconcat (lambda (x)
9945 (if (equal (string-to-char (car x)) ?@) "" "$")
9946 (car x) "=" (cdr x)))
9950 (defsubst org-table-formula-make-cmp-string (a)
9951 (when (string-match "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?" a)
9953 (if (match-end 2) (format "@%05d" (string-to-number (match-string 2 a))) "")
9954 (if (match-end 4) (format "$%05d" (string-to-number (match-string 4 a))) "")
9955 (if (match-end 5) (concat "@@" (match-string 5 a))))))
9957 (defun org-table-formula-less-p (a b)
9958 "Compare two formulas for sorting."
9959 (let ((as (org-table-formula-make-cmp-string (car a)))
9960 (bs (org-table-formula-make-cmp-string (car b))))
9961 (and as bs (string< as bs))))
9963 (defun org-table-get-stored-formulas (&optional noerror)
9964 "Return an alist with the stored formulas directly after current table."
9966 (let (scol eq eq-alist strings string seen)
9968 (goto-char (org-table-end))
9969 (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
9970 (setq strings (org-split-string (match-string 2) " *:: *"))
9971 (while (setq string (pop strings))
9972 (when (string-match "\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*[^ \t]\\)" string)
9973 (setq scol (if (match-end 2)
9974 (match-string 2 string)
9975 (match-string 1 string))
9976 eq (match-string 3 string)
9977 eq-alist (cons (cons scol eq) eq-alist))
9978 (if (member scol seen)
9981 (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
9984 (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
9985 (push scol seen))))))
9986 (nreverse eq-alist)))
9988 (defun org-table-fix-formulas (key replace &optional limit delta remove)
9989 "Modify the equations after the table structure has been edited.
9990 KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
9991 For all numbers larger than LIMIT, shift them by DELTA."
9993 (goto-char (org-table-end))
9994 (when (looking-at "#\\+TBLFM:")
9995 (let ((re (concat key "\\([0-9]+\\)"))
9999 (format "\\(@[0-9]+\\)?\\$%d=.*?\\(::\\|$\\)" remove)
10000 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
10003 (while (re-search-forward re2 (point-at-eol) t)
10004 (replace-match "")))
10005 (while (re-search-forward re (point-at-eol) t)
10006 (setq s (match-string 1) n (string-to-number s))
10008 ((setq a (assoc s replace))
10009 (replace-match (concat key (cdr a)) t t))
10010 ((and limit (> n limit))
10011 (replace-match (concat key (int-to-string (+ n delta))) t t))))))))
10013 (defun org-table-get-specials ()
10014 "Get the column names and local parameters for this table."
10016 (let ((beg (org-table-begin)) (end (org-table-end))
10017 names name fields fields1 field cnt
10018 c v l line col types dlines hlines)
10019 (setq org-table-column-names nil
10020 org-table-local-parameters nil
10021 org-table-named-field-locations nil
10022 org-table-current-begin-line nil
10023 org-table-current-begin-pos nil
10024 org-table-current-line-types nil)
10026 (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
10027 (setq names (org-split-string (match-string 1) " *| *")
10029 (while (setq name (pop names))
10030 (setq cnt (1+ cnt))
10031 (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
10032 (push (cons name (int-to-string cnt)) org-table-column-names))))
10033 (setq org-table-column-names (nreverse org-table-column-names))
10034 (setq org-table-column-name-regexp
10035 (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
10037 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
10038 (setq fields (org-split-string (match-string 1) " *| *"))
10039 (while (setq field (pop fields))
10040 (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
10041 (push (cons (match-string 1 field) (match-string 2 field))
10042 org-table-local-parameters))))
10044 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
10045 (setq c (match-string 1)
10046 fields (org-split-string (match-string 2) " *| *"))
10048 (beginning-of-line (if (equal c "_") 2 0))
10049 (setq line (org-current-line) col 1)
10050 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
10051 (setq fields1 (org-split-string (match-string 1) " *| *"))))
10052 (while (and fields1 (setq field (pop fields)))
10053 (setq v (pop fields1) col (1+ col))
10054 (when (and (stringp field) (stringp v)
10055 (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
10056 (push (cons field v) org-table-local-parameters)
10057 (push (list field line col) org-table-named-field-locations))))
10058 ;; Analyse the line types
10060 (setq org-table-current-begin-line (org-current-line)
10061 org-table-current-begin-pos (point)
10062 l org-table-current-begin-line)
10063 (while (looking-at "[ \t]*|\\(-\\)?")
10064 (push (if (match-end 1) 'hline 'dline) types)
10065 (if (match-end 1) (push l hlines) (push l dlines))
10066 (beginning-of-line 2)
10068 (setq org-table-current-line-types (apply 'vector (nreverse types))
10069 org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
10070 org-table-hlines (apply 'vector (cons nil (nreverse hlines)))))))
10072 (defun org-table-maybe-eval-formula ()
10073 "Check if the current field starts with \"=\" or \":=\".
10074 If yes, store the formula and apply it."
10075 ;; We already know we are in a table. Get field will only return a formula
10076 ;; when appropriate. It might return a separator line, but no problem.
10077 (when org-table-formula-evaluate-inline
10078 (let* ((field (org-trim (or (org-table-get-field) "")))
10080 (when (string-match "^:?=\\(.*\\)" field)
10081 (setq named (equal (string-to-char field) ?:)
10082 eq (match-string 1 field))
10083 (if (or (fboundp 'calc-eval)
10084 (equal (substring eq 0 (min 2 (length eq))) "'("))
10085 (org-table-eval-formula (if named '(4) nil)
10086 (org-table-formula-from-user eq))
10087 (error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
10089 (defvar org-recalc-commands nil
10090 "List of commands triggering the recalculation of a line.
10091 Will be filled automatically during use.")
10093 (defvar org-recalc-marks
10094 '((" " . "Unmarked: no special line, no automatic recalculation")
10095 ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
10096 ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
10097 ("!" . "Column name definition line. Reference in formula as $name.")
10098 ("$" . "Parameter definition line name=value. Reference in formula as $name.")
10099 ("_" . "Names for values in row below this one.")
10100 ("^" . "Names for values in row above this one.")))
10102 (defun org-table-rotate-recalc-marks (&optional newchar)
10103 "Rotate the recalculation mark in the first column.
10104 If in any row, the first field is not consistent with a mark,
10105 insert a new column for the markers.
10106 When there is an active region, change all the lines in the region,
10107 after prompting for the marking character.
10108 After each change, a message will be displayed indicating the meaning
10111 (unless (org-at-table-p) (error "Not at a table"))
10112 (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
10113 (beg (org-table-begin))
10114 (end (org-table-end))
10115 (l (org-current-line))
10116 (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
10117 (l2 (if (org-region-active-p) (org-current-line (region-end))))
10121 (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
10122 (col (org-table-current-column))
10123 (forcenew (car (assoc newchar org-recalc-marks)))
10126 (message "Change region to what mark? Type # * ! $ or SPC: ")
10127 (setq newchar (char-to-string (read-char-exclusive))
10128 forcenew (car (assoc newchar org-recalc-marks))))
10129 (if (and newchar (not forcenew))
10130 (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
10132 (if l1 (goto-line l1))
10134 (beginning-of-line 1)
10135 (unless (looking-at org-table-dataline-regexp)
10136 (error "Not at a table data line")))
10138 (org-table-goto-column 1)
10139 (org-table-insert-column)
10140 (org-table-goto-column (1+ col)))
10141 (setq epos (point-at-eol))
10143 (beginning-of-line 1)
10144 (org-table-get-field
10145 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
10147 (setq new (or forcenew
10148 (cadr (member (match-string 1) marks))))
10154 (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
10155 (and (looking-at org-table-dataline-regexp)
10156 (org-table-get-field 1 (concat " " new " "))))
10158 (if (not (= epos (point-at-eol))) (org-table-align))
10160 (and (interactive-p) (message "%s" (cdr (assoc new org-recalc-marks))))))
10162 (defun org-table-maybe-recalculate-line ()
10163 "Recompute the current line if marked for it, and if we haven't just done it."
10165 (and org-table-allow-automatic-line-recalculation
10166 (not (and (memq last-command org-recalc-commands)
10167 (equal org-last-recalc-line (org-current-line))))
10168 (save-excursion (beginning-of-line 1)
10169 (looking-at org-table-auto-recalculate-regexp))
10170 (org-table-recalculate) t))
10172 (defvar org-table-formula-debug nil
10173 "Non-nil means, debug table formulas.
10174 When nil, simply write \"#ERROR\" in corrupted fields.")
10175 (make-variable-buffer-local 'org-table-formula-debug)
10178 (defsubst org-set-calc-mode (var &optional value)
10180 (setq var (assoc var '(("D" calc-angle-mode deg)
10181 ("R" calc-angle-mode rad)
10182 ("F" calc-prefer-frac t)
10183 ("S" calc-symbolic-mode t)))
10184 value (nth 2 var) var (nth 1 var)))
10185 (if (memq var modes)
10186 (setcar (cdr (memq var modes)) value)
10187 (cons var (cons value modes)))
10190 (defun org-table-eval-formula (&optional arg equation
10191 suppress-align suppress-const
10192 suppress-store suppress-analysis)
10193 "Replace the table field value at the cursor by the result of a calculation.
10195 This function makes use of Dave Gillespie's Calc package, in my view the
10196 most exciting program ever written for GNU Emacs. So you need to have Calc
10197 installed in order to use this function.
10199 In a table, this command replaces the value in the current field with the
10200 result of a formula. It also installs the formula as the \"current\" column
10201 formula, by storing it in a special line below the table. When called
10202 with a `C-u' prefix, the current field must ba a named field, and the
10203 formula is installed as valid in only this specific field.
10205 When called with two `C-u' prefixes, insert the active equation
10206 for the field back into the current field, so that it can be
10207 edited there. This is useful in order to use \\[org-table-show-reference]
10208 to check the referenced fields.
10210 When called, the command first prompts for a formula, which is read in
10211 the minibuffer. Previously entered formulas are available through the
10212 history list, and the last used formula is offered as a default.
10213 These stored formulas are adapted correctly when moving, inserting, or
10214 deleting columns with the corresponding commands.
10216 The formula can be any algebraic expression understood by the Calc package.
10217 For details, see the Org-mode manual.
10219 This function can also be called from Lisp programs and offers
10220 additional arguments: EQUATION can be the formula to apply. If this
10221 argument is given, the user will not be prompted. SUPPRESS-ALIGN is
10222 used to speed-up recursive calls by by-passing unnecessary aligns.
10223 SUPPRESS-CONST suppresses the interpretation of constants in the
10224 formula, assuming that this has been done already outside the function.
10225 SUPPRESS-STORE means the formula should not be stored, either because
10226 it is already stored, or because it is a modified equation that should
10227 not overwrite the stored one."
10229 (org-table-check-inside-data-field)
10230 (or suppress-analysis (org-table-get-specials))
10231 (if (equal arg '(16))
10232 (let ((eq (org-table-current-field-formula)))
10233 (or eq (error "No equation active for current field"))
10234 (org-table-get-field nil eq)
10236 (setq org-table-may-need-update t))
10238 (ndown (if (integerp arg) arg 1))
10239 (org-table-automatic-realign nil)
10240 (case-fold-search nil)
10242 (formula (if (and equation suppress-store)
10244 (org-table-get-formula equation (equal arg '(4)))))
10245 (n0 (org-table-current-column))
10246 (modes (copy-sequence org-calc-default-modes))
10247 (numbers nil) ; was a variable, now fixed default
10249 n form form0 bw fmt x ev orig c lispp literal)
10250 ;; Parse the format string. Since we have a lot of modes, this is
10251 ;; a lot of work. However, I think calc still uses most of the time.
10252 (if (string-match ";" formula)
10253 (let ((tmp (org-split-string formula ";")))
10254 (setq formula (car tmp)
10255 fmt (concat (cdr (assoc "%" org-table-local-parameters))
10257 (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
10258 (setq c (string-to-char (match-string 1 fmt))
10259 n (string-to-number (match-string 2 fmt)))
10261 (setq modes (org-set-calc-mode 'calc-internal-prec n))
10262 (setq modes (org-set-calc-mode
10264 (list (cdr (assoc c '((?n . float) (?f . fix)
10265 (?s . sci) (?e . eng))))
10267 (setq fmt (replace-match "" t t fmt)))
10268 (if (string-match "[NT]" fmt)
10269 (setq numbers (equal (match-string 0 fmt) "N")
10270 fmt (replace-match "" t t fmt)))
10271 (if (string-match "L" fmt)
10273 fmt (replace-match "" t t fmt)))
10274 (if (string-match "E" fmt)
10276 fmt (replace-match "" t t fmt)))
10277 (while (string-match "[DRFS]" fmt)
10278 (setq modes (org-set-calc-mode (match-string 0 fmt)))
10279 (setq fmt (replace-match "" t t fmt)))
10280 (unless (string-match "\\S-" fmt)
10282 (if (and (not suppress-const) org-table-formula-use-constants)
10283 (setq formula (org-table-formula-substitute-names formula)))
10284 (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
10286 (setq fields (org-split-string
10288 (buffer-substring (point-at-bol) (point-at-eol)))
10291 (setq fields (mapcar
10292 (lambda (x) (number-to-string (string-to-number x)))
10294 (setq ndown (1- ndown))
10295 (setq form (copy-sequence formula)
10296 lispp (and (> (length form) 2)(equal (substring form 0 2) "'(")))
10297 (if (and lispp literal) (setq lispp 'literal))
10298 ;; Check for old vertical references
10299 (setq form (org-rewrite-old-row-references form))
10300 ;; Insert complex ranges
10301 (while (string-match org-table-range-regexp form)
10305 (org-table-make-reference
10306 (org-table-get-range (match-string 0 form) nil n0)
10307 keep-empty numbers lispp))
10309 ;; Insert simple ranges
10310 (while (string-match "\\$\\([0-9]+\\)\\.\\.\\$\\([0-9]+\\)" form)
10314 (org-table-make-reference
10316 fields (string-to-number (match-string 1 form))
10317 (string-to-number (match-string 2 form)))
10318 keep-empty numbers lispp))
10321 ;; Insert the references to fields in same row
10322 (while (string-match "\\$\\([0-9]+\\)" form)
10323 (setq n (string-to-number (match-string 1 form))
10324 x (nth (1- (if (= n 0) n0 n)) fields))
10325 (unless x (error "Invalid field specifier \"%s\""
10326 (match-string 0 form)))
10327 (setq form (replace-match
10329 (org-table-make-reference x nil numbers lispp))
10333 (setq ev (condition-case nil
10334 (eval (eval (read form)))
10336 ev (if (numberp ev) (number-to-string ev) ev))
10337 (or (fboundp 'calc-eval)
10338 (error "Calc does not seem to be installed, and is needed to evaluate the formula"))
10339 (setq ev (calc-eval (cons form modes)
10340 (if numbers 'num))))
10342 (when org-table-formula-debug
10343 (with-output-to-temp-buffer "*Substitution History*"
10344 (princ (format "Substitution history of formula
10348 $1-> %s\n" orig formula form0 form))
10350 (princ (format " %s^\nError: %s"
10351 (make-string (car ev) ?\-) (nth 1 ev)))
10352 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
10354 (if fmt (format fmt (string-to-number ev)) ev)))))
10355 (setq bw (get-buffer-window "*Substitution History*"))
10356 (shrink-window-if-larger-than-buffer bw)
10357 (unless (and (interactive-p) (not ndown))
10358 (unless (let (inhibit-redisplay)
10359 (y-or-n-p "Debugging Formula. Continue to next? "))
10364 (if (listp ev) (setq fmt nil ev "#ERROR"))
10365 (org-table-justify-field-maybe
10366 (if fmt (format fmt (string-to-number ev)) ev))
10367 (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
10368 (call-interactively 'org-return)
10370 (and down (org-table-maybe-recalculate-line))
10371 (or suppress-align (and org-table-may-need-update
10372 (org-table-align))))))
10374 (defun org-table-put-field-property (prop value)
10376 (put-text-property (progn (skip-chars-backward "^|") (point))
10377 (progn (skip-chars-forward "^|") (point))
10380 (defun org-table-get-range (desc &optional tbeg col highlight)
10381 "Get a calc vector from a column, accorting to descriptor DESC.
10382 Optional arguments TBEG and COL can give the beginning of the table and
10383 the current column, to avoid unnecessary parsing.
10384 HIGHLIGHT means, just highlight the range."
10385 (if (not (equal (string-to-char desc) ?@))
10386 (setq desc (concat "@" desc)))
10388 (or tbeg (setq tbeg (org-table-begin)))
10389 (or col (setq col (org-table-current-column)))
10390 (let ((thisline (org-current-line))
10391 beg end c1 c2 r1 r2 rangep tmp)
10392 (unless (string-match org-table-range-regexp desc)
10393 (error "Invalid table range specifier `%s'" desc))
10394 (setq rangep (match-end 3)
10395 r1 (and (match-end 1) (match-string 1 desc))
10396 r2 (and (match-end 4) (match-string 4 desc))
10397 c1 (and (match-end 2) (substring (match-string 2 desc) 1))
10398 c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
10400 (and c1 (setq c1 (+ (string-to-number c1)
10401 (if (memq (string-to-char c1) '(?- ?+)) col 0))))
10402 (and c2 (setq c2 (+ (string-to-number c2)
10403 (if (memq (string-to-char c2) '(?- ?+)) col 0))))
10404 (if (equal r1 "") (setq r1 nil))
10405 (if (equal r2 "") (setq r2 nil))
10406 (if r1 (setq r1 (org-table-get-descriptor-line r1)))
10407 (if r2 (setq r2 (org-table-get-descriptor-line r2)))
10408 ; (setq r2 (or r2 r1) c2 (or c2 c1))
10409 (if (not r1) (setq r1 thisline))
10410 (if (not r2) (setq r2 thisline))
10411 (if (not c1) (setq c1 col))
10412 (if (not c2) (setq c2 col))
10413 (if (or (not rangep) (and (= r1 r2) (= c1 c2)))
10417 (while (not (looking-at org-table-dataline-regexp))
10418 (beginning-of-line 2))
10419 (prog1 (org-trim (org-table-get-field c1))
10420 (if highlight (org-table-highlight-rectangle (point) (point)))))
10421 ;; A range, return a vector
10422 ;; First sort the numbers to get a regular ractangle
10423 (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
10424 (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
10426 (while (not (looking-at org-table-dataline-regexp))
10427 (beginning-of-line 2))
10428 (org-table-goto-column c1)
10431 (while (not (looking-at org-table-dataline-regexp))
10432 (beginning-of-line 0))
10433 (org-table-goto-column c2)
10436 (org-table-highlight-rectangle
10437 beg (progn (skip-chars-forward "^|\n") (point))))
10438 ;; return string representation of calc vector
10440 (apply 'append (org-table-copy-region beg end)))))))
10442 (defun org-table-get-descriptor-line (desc &optional cline bline table)
10443 "Analyze descriptor DESC and retrieve the corresponding line number.
10444 The cursor is currently in line CLINE, the table begins in line BLINE,
10445 and TABLE is a vector with line types."
10446 (if (string-match "^[0-9]+$" desc)
10447 (aref org-table-dlines (string-to-number desc))
10448 (setq cline (or cline (org-current-line))
10449 bline (or bline org-table-current-begin-line)
10450 table (or table org-table-current-line-types))
10452 (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
10454 (and (not (match-end 3)) (not (match-end 6)))
10455 (and (match-end 3) (match-end 6) (not (match-end 5))))
10456 (error "invalid row descriptor `%s'" desc))
10457 (let* ((hdir (and (match-end 2) (match-string 2 desc)))
10458 (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
10459 (odir (and (match-end 5) (match-string 5 desc)))
10460 (on (if (match-end 6) (string-to-number (match-string 6 desc))))
10461 (i (- cline bline))
10462 (rel (and (match-end 6)
10463 (or (and (match-end 1) (not (match-end 3)))
10465 (if (and hn (not hdir))
10467 (setq i 0 hdir "+")
10468 (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
10469 (if (and (not hn) on (not odir))
10470 (error "should never happen");;(aref org-table-dlines on)
10471 (if (and hn (> hn 0))
10472 (setq i (org-find-row-type table i 'hline (equal hdir "-") nil hn)))
10474 (setq i (org-find-row-type table i 'dline (equal odir "-") rel on)))
10477 (defun org-find-row-type (table i type backwards relative n)
10478 (let ((l (length table)))
10480 (while (and (setq i (+ i (if backwards -1 1)))
10482 (not (eq (aref table i) type))
10483 (if (and relative (eq (aref table i) 'hline))
10484 (progn (setq i (- i (if backwards -1 1)) n 1) nil)
10487 (if (or (< i 0) (>= i l))
10488 (error "Row descriptior leads outside table")
10491 (defun org-rewrite-old-row-references (s)
10492 (if (string-match "&[-+0-9I]" s)
10493 (error "Formula contains old &row reference, please rewrite using @-syntax")
10496 (defun org-table-make-reference (elements keep-empty numbers lispp)
10497 "Convert list ELEMENTS to something appropriate to insert into formula.
10498 KEEP-EMPTY indicated to keep empty fields, default is to skip them.
10499 NUMBERS indicates that everything should be converted to numbers.
10500 LISPP means to return something appropriate for a Lisp list."
10501 (if (stringp elements) ; just a single val
10503 (if (eq lispp 'literal)
10505 (prin1-to-string (if numbers (string-to-number elements) elements)))
10506 (if (equal elements "") (setq elements "0"))
10507 (if numbers (number-to-string (string-to-number elements)) elements))
10511 (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
10513 (setq elements (or elements '("0")))
10517 (if (eq lispp 'literal)
10519 (prin1-to-string (if numbers (string-to-number x) x))))
10521 (concat "[" (mapconcat
10523 (if numbers (number-to-string (string-to-number x)) x))
10527 (defun org-table-recalculate (&optional all noalign)
10528 "Recalculate the current table line by applying all stored formulas.
10529 With prefix arg ALL, do this for all lines in the table."
10531 (or (memq this-command org-recalc-commands)
10532 (setq org-recalc-commands (cons this-command org-recalc-commands)))
10533 (unless (org-at-table-p) (error "Not at a table"))
10534 (if (equal all '(16))
10535 (org-table-iterate)
10536 (org-table-get-specials)
10537 (let* ((eqlist (sort (org-table-get-stored-formulas)
10538 (lambda (a b) (string< (car a) (car b)))))
10539 (inhibit-redisplay (not debug-on-error))
10540 (line-re org-table-dataline-regexp)
10541 (thisline (org-current-line))
10542 (thiscol (org-table-current-column))
10543 beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name)
10544 ;; Insert constants in all formulas
10546 (mapcar (lambda (x)
10547 (setcdr x (org-table-formula-substitute-names (cdr x)))
10550 ;; Split the equation list
10551 (while (setq eq (pop eqlist))
10552 (if (<= (string-to-char (car eq)) ?9)
10554 (push eq eqlname)))
10555 (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
10558 (setq end (move-marker (make-marker) (1+ (org-table-end))))
10559 (goto-char (setq beg (org-table-begin)))
10560 (if (re-search-forward org-table-calculate-mark-regexp end t)
10561 ;; This is a table with marked lines, compute selected lines
10562 (setq line-re org-table-recalculate-regexp)
10563 ;; Move forward to the first non-header line
10564 (if (and (re-search-forward org-table-dataline-regexp end t)
10565 (re-search-forward org-table-hline-regexp end t)
10566 (re-search-forward org-table-dataline-regexp end t))
10567 (setq beg (match-beginning 0))
10568 nil))) ;; just leave beg where it is
10569 (setq beg (point-at-bol)
10570 end (move-marker (make-marker) (1+ (point-at-eol)))))
10572 (and all (message "Re-applying formulas to full table..."))
10574 ;; First find the named fields, and mark them untouchanble
10575 (remove-text-properties beg end '(org-untouchable t))
10576 (while (setq eq (pop eqlname))
10577 (setq name (car eq)
10578 a (assoc name org-table-named-field-locations))
10580 (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
10582 (aref org-table-dlines
10583 (string-to-number (match-string 1 name)))
10584 (string-to-number (match-string 2 name)))))
10585 (when (and a (or all (equal (nth 1 a) thisline)))
10586 (message "Re-applying formula to field: %s" name)
10587 (goto-line (nth 1 a))
10588 (org-table-goto-column (nth 2 a))
10589 (push (append a (list (cdr eq))) eqlname1)
10590 (org-table-put-field-property :org-untouchable t)))
10592 ;; Now evauluate the column formulas, but skip fields covered by
10595 (while (re-search-forward line-re end t)
10596 (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
10597 ;; Unprotected line, recalculate
10598 (and all (message "Re-applying formulas to full table...(line %d)"
10599 (setq cnt (1+ cnt))))
10600 (setq org-last-recalc-line (org-current-line))
10602 (while (setq entry (pop eql))
10603 (goto-line org-last-recalc-line)
10604 (org-table-goto-column (string-to-number (car entry)) nil 'force)
10605 (unless (get-text-property (point) :org-untouchable)
10606 (org-table-eval-formula nil (cdr entry)
10607 'noalign 'nocst 'nostore 'noanalysis)))))
10609 ;; Now evaluate the field formulas
10610 (while (setq eq (pop eqlname1))
10611 (message "Re-applying formula to field: %s" (car eq))
10612 (goto-line (nth 1 eq))
10613 (org-table-goto-column (nth 2 eq))
10614 (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
10615 'nostore 'noanalysis))
10617 (goto-line thisline)
10618 (org-table-goto-column thiscol)
10619 (remove-text-properties (point-min) (point-max) '(org-untouchable t))
10620 (or noalign (and org-table-may-need-update (org-table-align))
10621 (and all (message "Re-applying formulas to %d lines...done" cnt)))
10623 ;; back to initial position
10624 (message "Re-applying formulas...done")
10625 (goto-line thisline)
10626 (org-table-goto-column thiscol)
10627 (or noalign (and org-table-may-need-update (org-table-align))
10628 (and all (message "Re-applying formulas...done"))))))
10630 (defun org-table-iterate (&optional arg)
10631 "Recalculate the table until it does not change anymore."
10633 (let ((imax (if arg (prefix-numeric-value arg) 10))
10635 (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
10640 (org-table-recalculate 'all)
10641 (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
10642 (if (not (string= lasttbl thistbl))
10643 (setq lasttbl thistbl)
10645 (message "Convergence after %d iterations" i)
10646 (message "Table was already stable"))
10648 (error "No convergence after %d iterations" i))))
10650 (defun org-table-formula-substitute-names (f)
10651 "Replace $const with values in string F."
10652 (let ((start 0) a (f1 f) (pp (/= (string-to-char f) ?')))
10653 ;; First, check for column names
10654 (while (setq start (string-match org-table-column-name-regexp f start))
10655 (setq start (1+ start))
10656 (setq a (assoc (match-string 1 f) org-table-column-names))
10657 (setq f (replace-match (concat "$" (cdr a)) t t f)))
10658 ;; Parameters and constants
10660 (while (setq start (string-match "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)" f start))
10661 (setq start (1+ start))
10662 (if (setq a (save-match-data
10663 (org-table-get-constant (match-string 1 f))))
10664 (setq f (replace-match
10665 (concat (if pp "(") a (if pp ")")) t t f))))
10666 (if org-table-formula-debug
10667 (put-text-property 0 (length f) :orig-formula f1 f))
10670 (defun org-table-get-constant (const)
10671 "Find the value for a parameter or constant in a formula.
10672 Parameters get priority."
10673 (or (cdr (assoc const org-table-local-parameters))
10674 (cdr (assoc const org-table-formula-constants-local))
10675 (cdr (assoc const org-table-formula-constants))
10676 (and (fboundp 'constants-get) (constants-get const))
10677 (and (string= (substring const 0 (min 5 (length const))) "PROP_")
10678 (org-entry-get nil (substring const 5) 'inherit))
10679 "#UNDEFINED_NAME"))
10681 (defvar org-table-fedit-map
10682 (let ((map (make-sparse-keymap)))
10683 (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
10684 (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
10685 (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
10686 (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
10687 (org-defkey map "\C-c?" 'org-table-show-reference)
10688 (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
10689 (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
10690 (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
10691 (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
10692 (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
10693 (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
10694 (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
10695 (org-defkey map [(meta down)] 'org-table-fedit-scroll)
10696 (org-defkey map [(meta tab)] 'lisp-complete-symbol)
10697 (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
10698 (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
10699 (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
10700 (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
10701 (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
10704 (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
10706 ["Finish and Install" org-table-fedit-finish t]
10707 ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
10708 ["Abort" org-table-fedit-abort t]
10710 ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
10711 ["Complete Lisp Symbol" lisp-complete-symbol t]
10713 "Shift Reference at Point"
10714 ["Up" org-table-fedit-ref-up t]
10715 ["Down" org-table-fedit-ref-down t]
10716 ["Left" org-table-fedit-ref-left t]
10717 ["Right" org-table-fedit-ref-right t]
10719 "Change Test Row for Column Formulas"
10720 ["Up" org-table-fedit-line-up t]
10721 ["Down" org-table-fedit-line-down t]
10723 ["Scroll Table Window" org-table-fedit-scroll t]
10724 ["Scroll Table Window down" org-table-fedit-scroll-down t]
10725 ["Show Table Grid" org-table-fedit-toggle-coordinates
10726 :style toggle :selected (with-current-buffer (marker-buffer org-pos)
10727 org-table-overlay-coordinates)]
10729 ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
10730 :style toggle :selected org-table-buffer-is-an]))
10734 (defun org-table-edit-formulas ()
10735 "Edit the formulas of the current table in a separate buffer."
10737 (when (save-excursion (beginning-of-line 1) (looking-at "#\\+TBLFM"))
10738 (beginning-of-line 0))
10739 (unless (org-at-table-p) (error "Not at a table"))
10740 (org-table-get-specials)
10741 (let ((key (org-table-current-field-formula 'key 'noerror))
10742 (eql (sort (org-table-get-stored-formulas 'noerror)
10743 'org-table-formula-less-p))
10744 (pos (move-marker (make-marker) (point)))
10746 (wc (current-window-configuration))
10747 (titles '((column . "# Column Formulas\n")
10748 (field . "# Field Formulas\n")
10749 (named . "# Named Field Formulas\n")))
10750 entry s type title)
10751 (org-switch-to-buffer-other-window "*Edit Formulas*")
10753 ;; Keep global-font-lock-mode from turning on font-lock-mode
10754 (let ((font-lock-global-modes '(not fundamental-mode)))
10755 (fundamental-mode))
10756 (org-set-local 'font-lock-global-modes (list 'not major-mode))
10757 (org-set-local 'org-pos pos)
10758 (org-set-local 'org-window-configuration wc)
10759 (use-local-map org-table-fedit-map)
10760 (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
10761 (easy-menu-add org-table-fedit-menu)
10762 (setq startline (org-current-line))
10763 (while (setq entry (pop eql))
10765 ((equal (string-to-char (car entry)) ?@) 'field)
10766 ((string-match "^[0-9]" (car entry)) 'column)
10768 (when (setq title (assq type titles))
10769 (or (bobp) (insert "\n"))
10770 (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
10771 (setq titles (delq title titles)))
10772 (if (equal key (car entry)) (setq startline (org-current-line)))
10773 (setq s (concat (if (equal (string-to-char (car entry)) ?@) "" "$")
10774 (car entry) " = " (cdr entry) "\n"))
10775 (remove-text-properties 0 (length s) '(face nil) s)
10777 (if (eq org-table-use-standard-references t)
10778 (org-table-fedit-toggle-ref-type))
10779 (goto-line startline)
10780 (message "Edit formulas and finish with `C-c C-c'. See menu for more commands.")))
10782 (defun org-table-fedit-post-command ()
10783 (when (not (memq this-command '(lisp-complete-symbol)))
10784 (let ((win (selected-window)))
10786 (condition-case nil
10787 (org-table-show-reference)
10789 (select-window win)))))
10791 (defun org-table-formula-to-user (s)
10792 "Convert a formula from internal to user representation."
10793 (if (eq org-table-use-standard-references t)
10794 (org-table-convert-refs-to-an s)
10797 (defun org-table-formula-from-user (s)
10798 "Convert a formula from user to internal representation."
10799 (if org-table-use-standard-references
10800 (org-table-convert-refs-to-rc s)
10803 (defun org-table-convert-refs-to-rc (s)
10804 "Convert spreadsheet references from AB7 to @7$28.
10805 Works for single references, but also for entire formulas and even the
10808 (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\)" s start)
10811 ;; format match, just advance
10812 (setq start (match-end 0)))
10813 ((and (> (match-beginning 0) 0)
10814 (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
10815 (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
10816 ;; 3.e5 or something like this.
10817 (setq start (match-end 0)))
10819 (setq start (match-beginning 0)
10821 (if (equal (match-string 2 s) "&")
10822 (format "$%d" (org-letters-to-number (match-string 1 s)))
10824 (string-to-number (match-string 2 s))
10825 (org-letters-to-number (match-string 1 s))))
10829 (defun org-table-convert-refs-to-an (s)
10830 "Convert spreadsheet references from to @7$28 to AB7.
10831 Works for single references, but also for entire formulas and even the
10833 (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
10834 (setq s (replace-match
10836 (org-number-to-letters
10837 (string-to-number (match-string 2 s)))
10838 (string-to-number (match-string 1 s)))
10840 (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
10841 (setq s (replace-match (concat "\\1"
10842 (org-number-to-letters
10843 (string-to-number (match-string 2 s))) "&")
10847 (defun org-letters-to-number (s)
10848 "Convert a base 26 number represented by letters into an integer.
10849 For example: AB -> 28."
10851 (setq s (upcase s))
10852 (while (> (length s) 0)
10853 (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
10854 s (substring s 1)))
10857 (defun org-number-to-letters (n)
10858 "Convert an integer into a base 26 number represented by letters.
10859 For example: 28 -> AB."
10862 (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
10866 (defun org-table-fedit-convert-buffer (function)
10867 "Convert all references in this buffer, using FUNTION."
10868 (let ((line (org-current-line)))
10869 (goto-char (point-min))
10870 (while (not (eobp))
10871 (insert (funcall function (buffer-substring (point) (point-at-eol))))
10872 (delete-region (point) (point-at-eol))
10873 (or (eobp) (forward-char 1)))
10876 (defun org-table-fedit-toggle-ref-type ()
10877 "Convert all references in the buffer from B3 to @3$2 and back."
10879 (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
10880 (org-table-fedit-convert-buffer
10881 (if org-table-buffer-is-an
10882 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
10883 (message "Reference type switched to %s"
10884 (if org-table-buffer-is-an "A1 etc" "@row$column")))
10886 (defun org-table-fedit-ref-up ()
10887 "Shift the reference at point one row/hline up."
10889 (org-table-fedit-shift-reference 'up))
10890 (defun org-table-fedit-ref-down ()
10891 "Shift the reference at point one row/hline down."
10893 (org-table-fedit-shift-reference 'down))
10894 (defun org-table-fedit-ref-left ()
10895 "Shift the reference at point one field to the left."
10897 (org-table-fedit-shift-reference 'left))
10898 (defun org-table-fedit-ref-right ()
10899 "Shift the reference at point one field to the right."
10901 (org-table-fedit-shift-reference 'right))
10903 (defun org-table-fedit-shift-reference (dir)
10905 ((org-at-regexp-p "\\(\\<[a-zA-Z]\\)&")
10906 (if (memq dir '(left right))
10907 (org-rematch-and-replace 1 (eq dir 'left))
10908 (error "Cannot shift reference in this direction")))
10909 ((org-at-regexp-p "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
10910 ;; A B3-like reference
10911 (if (memq dir '(up down))
10912 (org-rematch-and-replace 2 (eq dir 'up))
10913 (org-rematch-and-replace 1 (eq dir 'left))))
10915 "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
10916 ;; An internal reference
10917 (if (memq dir '(up down))
10918 (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
10919 (org-rematch-and-replace 5 (eq dir 'left))))))
10921 (defun org-rematch-and-replace (n &optional decr hline)
10922 "Re-match the group N, and replace it with the shifted refrence."
10923 (or (match-end n) (error "Cannot shift reference in this direction"))
10924 (goto-char (match-beginning n))
10925 (and (looking-at (regexp-quote (match-string n)))
10926 (replace-match (org-shift-refpart (match-string 0) decr hline)
10929 (defun org-shift-refpart (ref &optional decr hline)
10930 "Shift a refrence part REF.
10931 If DECR is set, decrease the references row/column, else increase.
10932 If HLINE is set, this may be a hline reference, it certainly is not
10933 a translation reference."
10935 (let* ((sign (string-match "^[-+]" ref)) n)
10937 (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
10939 ((and hline (string-match "^I+" ref))
10940 (setq n (string-to-number (concat sign (number-to-string (length ref)))))
10941 (setq n (+ n (if decr -1 1)))
10942 (if (= n 0) (setq n (+ n (if decr -1 1))))
10944 (setq sign (if (< n 0) "-" "+") n (abs n))
10945 (setq n (max 1 n)))
10946 (concat sign (make-string n ?I)))
10948 ((string-match "^[0-9]+" ref)
10949 (setq n (string-to-number (concat sign ref)))
10950 (setq n (+ n (if decr -1 1)))
10952 (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
10953 (number-to-string (max 1 n))))
10955 ((string-match "^[a-zA-Z]+" ref)
10956 (org-number-to-letters
10957 (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
10959 (t (error "Cannot shift reference"))))))
10961 (defun org-table-fedit-toggle-coordinates ()
10962 "Toggle the display of coordinates in the refrenced table."
10964 (let ((pos (marker-position org-pos)))
10965 (with-current-buffer (marker-buffer org-pos)
10968 (org-table-toggle-coordinate-overlays)))))
10970 (defun org-table-fedit-finish (&optional arg)
10971 "Parse the buffer for formula definitions and install them.
10972 With prefix ARG, apply the new formulas to the table."
10974 (org-table-remove-rectangle-highlight)
10975 (if org-table-use-standard-references
10977 (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
10978 (setq org-table-buffer-is-an nil)))
10979 (let ((pos org-pos) eql var form)
10980 (goto-char (point-min))
10981 (while (re-search-forward
10982 "^\\(@[0-9]+\\$[0-9]+\\|\\$\\([a-zA-Z0-9]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
10984 (setq var (if (match-end 2) (match-string 2) (match-string 1))
10985 form (match-string 3))
10986 (setq form (org-trim form))
10987 (when (not (equal form ""))
10988 (while (string-match "[ \t]*\n[ \t]*" form)
10989 (setq form (replace-match " " t t form)))
10990 (when (assoc var eql)
10991 (error "Double formulas for %s" var))
10992 (push (cons var form) eql)))
10994 (set-window-configuration org-window-configuration)
10995 (select-window (get-buffer-window (marker-buffer pos)))
10997 (unless (org-at-table-p)
10998 (error "Lost table position - cannot install formulae"))
10999 (org-table-store-formulas eql)
11000 (move-marker pos nil)
11001 (kill-buffer "*Edit Formulas*")
11003 (org-table-recalculate 'all)
11004 (message "New formulas installed - press C-u C-c C-c to apply."))))
11006 (defun org-table-fedit-abort ()
11007 "Abort editing formulas, without installing the changes."
11009 (org-table-remove-rectangle-highlight)
11010 (let ((pos org-pos))
11011 (set-window-configuration org-window-configuration)
11012 (select-window (get-buffer-window (marker-buffer pos)))
11014 (move-marker pos nil)
11015 (message "Formula editing aborted without installing changes")))
11017 (defun org-table-fedit-lisp-indent ()
11018 "Pretty-print and re-indent Lisp expressions in the Formula Editor."
11020 (let ((pos (point)) beg end ind)
11021 (beginning-of-line 1)
11023 ((looking-at "[ \t]")
11025 (call-interactively 'lisp-indent-line))
11026 ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
11027 ((not (fboundp 'pp-buffer))
11028 (error "Cannot pretty-print. Command `pp-buffer' is not available."))
11029 ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
11030 (goto-char (- (match-end 0) 2))
11032 (setq ind (make-string (current-column) ?\ ))
11033 (condition-case nil (forward-sexp 1)
11035 (error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
11038 (narrow-to-region beg end)
11039 (if (eq last-command this-command)
11041 (goto-char (point-min))
11042 (setq this-command nil)
11043 (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
11044 (replace-match " ")))
11046 (untabify (point-min) (point-max))
11047 (goto-char (1+ (point-min)))
11048 (while (re-search-forward "^." nil t)
11049 (beginning-of-line 1)
11051 (goto-char (point-max))
11052 (backward-delete-char 1)))
11056 (defvar org-show-positions nil)
11058 (defun org-table-show-reference (&optional local)
11059 "Show the location/value of the $ expression at point."
11061 (org-table-remove-rectangle-highlight)
11063 (let ((pos (if local (point) org-pos))
11065 (org-inhibit-highlight-removal t)
11066 (win (selected-window))
11067 (org-show-positions nil)
11068 var name e what match dest)
11069 (if local (org-table-get-specials))
11071 ((or (org-at-regexp-p org-table-range-regexp2)
11072 (org-at-regexp-p org-table-translate-regexp)
11073 (org-at-regexp-p org-table-range-regexp))
11076 (org-table-convert-refs-to-rc (match-string 0))))
11078 ((org-at-regexp-p "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
11079 ((org-at-regexp-p "\\$[0-9]+") 'column)
11081 (t (error "No reference at point")))
11082 match (and what (or match (match-string 0))))
11083 (when (and match (not (equal (match-beginning 0) (point-at-bol))))
11084 (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
11085 'secondary-selection))
11086 (org-add-hook 'before-change-functions
11087 'org-table-remove-rectangle-highlight)
11088 (if (eq what 'name) (setq var (substring match 1)))
11089 (when (eq what 'range)
11090 (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
11091 (setq match (org-table-formula-substitute-names match)))
11095 (re-search-backward "^\\S-" nil t)
11096 (beginning-of-line 1)
11097 (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
11100 (org-table-convert-refs-to-rc (match-string 1))))
11101 (org-table-add-rectangle-overlay
11102 (match-beginning 1) (match-end 1) face2))))
11103 (if (and (markerp pos) (marker-buffer pos))
11104 (if (get-buffer-window (marker-buffer pos))
11105 (select-window (get-buffer-window (marker-buffer pos)))
11106 (org-switch-to-buffer-other-window (get-buffer-window
11107 (marker-buffer pos)))))
11109 (org-table-force-dataline)
11111 (setq name (substring dest 1))
11113 ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
11114 (setq e (assoc name org-table-named-field-locations))
11115 (goto-line (nth 1 e))
11116 (org-table-goto-column (nth 2 e)))
11117 ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
11118 (let ((l (string-to-number (match-string 1 dest)))
11119 (c (string-to-number (match-string 2 dest))))
11120 (goto-line (aref org-table-dlines l))
11121 (org-table-goto-column c)))
11122 (t (org-table-goto-column (string-to-number name))))
11123 (move-marker pos (point))
11124 (org-table-highlight-rectangle nil nil face2))
11126 ((equal dest match))
11129 (condition-case nil
11131 (org-table-get-range match nil nil 'highlight))
11133 ((setq e (assoc var org-table-named-field-locations))
11134 (goto-line (nth 1 e))
11135 (org-table-goto-column (nth 2 e))
11136 (org-table-highlight-rectangle (point) (point))
11137 (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
11138 ((setq e (assoc var org-table-column-names))
11139 (org-table-goto-column (string-to-number (cdr e)))
11140 (org-table-highlight-rectangle (point) (point))
11141 (goto-char (org-table-begin))
11142 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
11145 (goto-char (match-beginning 1))
11146 (org-table-highlight-rectangle)
11147 (message "Named column (column %s)" (cdr e)))
11148 (error "Column name not found")))
11151 (org-table-goto-column (string-to-number (substring match 1)))
11152 (org-table-highlight-rectangle (point) (point))
11153 (message "Column %s" (substring match 1)))
11154 ((setq e (assoc var org-table-local-parameters))
11155 (goto-char (org-table-begin))
11156 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
11158 (goto-char (match-beginning 1))
11159 (org-table-highlight-rectangle)
11160 (message "Local parameter."))
11161 (error "Parameter not found")))
11164 ((not var) (error "No reference at point"))
11165 ((setq e (assoc var org-table-formula-constants-local))
11166 (message "Local Constant: $%s=%s in #+CONSTANTS line."
11168 ((setq e (assoc var org-table-formula-constants))
11169 (message "Constant: $%s=%s in `org-table-formula-constants'."
11171 ((setq e (and (fboundp 'constants-get) (constants-get var)))
11172 (message "Constant: $%s=%s, from `constants.el'%s."
11173 var e (format " (%s units)" constants-unit-system)))
11174 (t (error "Undefined name $%s" var)))))
11176 (when (and org-show-positions
11177 (not (memq this-command '(org-table-fedit-scroll
11178 org-table-fedit-scroll-down))))
11179 (push pos org-show-positions)
11180 (push org-table-current-begin-pos org-show-positions)
11181 (let ((min (apply 'min org-show-positions))
11182 (max (apply 'max org-show-positions)))
11183 (goto-char min) (recenter 0)
11185 (or (pos-visible-in-window-p max) (recenter -1))))
11186 (select-window win))))
11188 (defun org-table-force-dataline ()
11189 "Make sure the cursor is in a dataline in a table."
11190 (unless (save-excursion
11191 (beginning-of-line 1)
11192 (looking-at org-table-dataline-regexp))
11193 (let* ((re org-table-dataline-regexp)
11194 (p1 (save-excursion (re-search-forward re nil 'move)))
11195 (p2 (save-excursion (re-search-backward re nil 'move))))
11197 (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
11199 ((or p1 p2) (goto-char (or p1 p2)))
11200 (t (error "No table dataline around here"))))))
11202 (defun org-table-fedit-line-up ()
11203 "Move cursor one line up in the window showing the table."
11205 (org-table-fedit-move 'previous-line))
11207 (defun org-table-fedit-line-down ()
11208 "Move cursor one line down in the window showing the table."
11210 (org-table-fedit-move 'next-line))
11212 (defun org-table-fedit-move (command)
11213 "Move the cursor in the window shoinw the table.
11214 Use COMMAND to do the motion, repeat if necessary to end up in a data line."
11215 (let ((org-table-allow-automatic-line-recalculation nil)
11216 (pos org-pos) (win (selected-window)) p)
11217 (select-window (get-buffer-window (marker-buffer org-pos)))
11219 (call-interactively command)
11220 (while (and (org-at-table-p)
11221 (org-at-table-hline-p))
11222 (call-interactively command))
11223 (or (org-at-table-p) (goto-char p))
11224 (move-marker pos (point))
11225 (select-window win)))
11227 (defun org-table-fedit-scroll (N)
11229 (let ((other-window-scroll-buffer (marker-buffer org-pos)))
11230 (scroll-other-window N)))
11232 (defun org-table-fedit-scroll-down (N)
11234 (org-table-fedit-scroll (- N)))
11236 (defvar org-table-rectangle-overlays nil)
11238 (defun org-table-add-rectangle-overlay (beg end &optional face)
11239 "Add a new overlay."
11240 (let ((ov (org-make-overlay beg end)))
11241 (org-overlay-put ov 'face (or face 'secondary-selection))
11242 (push ov org-table-rectangle-overlays)))
11244 (defun org-table-highlight-rectangle (&optional beg end face)
11245 "Highlight rectangular region in a table."
11246 (setq beg (or beg (point)) end (or end (point)))
11247 (let ((b (min beg end))
11250 (and (boundp 'org-show-positions)
11251 (setq org-show-positions (cons b (cons e org-show-positions))))
11252 (goto-char (min beg end))
11253 (setq l1 (org-current-line)
11254 c1 (org-table-current-column))
11255 (goto-char (max beg end))
11256 (setq l2 (org-current-line)
11257 c2 (org-table-current-column))
11258 (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
11260 (beginning-of-line 1)
11261 (loop for line from l1 to l2 do
11262 (when (looking-at org-table-dataline-regexp)
11263 (org-table-goto-column c1)
11264 (skip-chars-backward "^|\n") (setq beg (point))
11265 (org-table-goto-column c2)
11266 (skip-chars-forward "^|\n") (setq end (point))
11267 (org-table-add-rectangle-overlay beg end face))
11268 (beginning-of-line 2))
11270 (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
11272 (defun org-table-remove-rectangle-highlight (&rest ignore)
11273 "Remove the rectangle overlays."
11274 (unless org-inhibit-highlight-removal
11275 (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
11276 (mapc 'org-delete-overlay org-table-rectangle-overlays)
11277 (setq org-table-rectangle-overlays nil)))
11279 (defvar org-table-coordinate-overlays nil
11280 "Collects the cooordinate grid overlays, so that they can be removed.")
11281 (make-variable-buffer-local 'org-table-coordinate-overlays)
11283 (defun org-table-overlay-coordinates ()
11284 "Add overlays to the table at point, to show row/column coordinates."
11286 (mapc 'org-delete-overlay org-table-coordinate-overlays)
11287 (setq org-table-coordinate-overlays nil)
11289 (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
11290 (goto-char (org-table-begin))
11291 (while (org-at-table-p)
11292 (setq eol (point-at-eol))
11293 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-bol))))
11294 (push ov org-table-coordinate-overlays)
11295 (setq hline (looking-at org-table-hline-regexp))
11296 (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
11297 (format "%4d" (setq id (1+ id)))))
11298 (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
11301 (while (re-search-forward "[+|]\\(-+\\)" eol t)
11302 (setq beg (1+ (match-beginning 0))
11304 s1 (concat "$" (int-to-string ic))
11305 s2 (org-number-to-letters ic)
11306 str (if (eq org-table-use-standard-references t) s2 s1))
11307 (setq ov (org-make-overlay beg (+ beg (length str))))
11308 (push ov org-table-coordinate-overlays)
11309 (org-overlay-display ov str 'org-special-keyword 'evaporate)))
11310 (beginning-of-line 2)))))
11312 (defun org-table-toggle-coordinate-overlays ()
11313 "Toggle the display of Row/Column numbers in tables."
11315 (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
11316 (message "Row/Column number display turned %s"
11317 (if org-table-overlay-coordinates "on" "off"))
11318 (if (and (org-at-table-p) org-table-overlay-coordinates)
11320 (unless org-table-overlay-coordinates
11321 (mapc 'org-delete-overlay org-table-coordinate-overlays)
11322 (setq org-table-coordinate-overlays nil)))
11324 (defun org-table-toggle-formula-debugger ()
11325 "Toggle the formula debugger in tables."
11327 (setq org-table-formula-debug (not org-table-formula-debug))
11328 (message "Formula debugging has been turned %s"
11329 (if org-table-formula-debug "on" "off")))
11331 ;;; The orgtbl minor mode
11333 ;; Define a minor mode which can be used in other modes in order to
11334 ;; integrate the org-mode table editor.
11336 ;; This is really a hack, because the org-mode table editor uses several
11337 ;; keys which normally belong to the major mode, for example the TAB and
11338 ;; RET keys. Here is how it works: The minor mode defines all the keys
11339 ;; necessary to operate the table editor, but wraps the commands into a
11340 ;; function which tests if the cursor is currently inside a table. If that
11341 ;; is the case, the table editor command is executed. However, when any of
11342 ;; those keys is used outside a table, the function uses `key-binding' to
11343 ;; look up if the key has an associated command in another currently active
11344 ;; keymap (minor modes, major mode, global), and executes that command.
11345 ;; There might be problems if any of the keys used by the table editor is
11346 ;; otherwise used as a prefix key.
11348 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
11349 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
11350 ;; addresses this by checking explicitly for both bindings.
11352 ;; The optimized version (see variable `orgtbl-optimized') takes over
11353 ;; all keys which are bound to `self-insert-command' in the *global map*.
11354 ;; Some modes bind other commands to simple characters, for example
11355 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
11356 ;; active, this binding is ignored inside tables and replaced with a
11357 ;; modified self-insert.
11359 (defvar orgtbl-mode nil
11360 "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
11361 table editor in arbitrary modes.")
11362 (make-variable-buffer-local 'orgtbl-mode)
11364 (defvar orgtbl-mode-map (make-keymap)
11365 "Keymap for `orgtbl-mode'.")
11368 (defun turn-on-orgtbl ()
11369 "Unconditionally turn on `orgtbl-mode'."
11372 (defvar org-old-auto-fill-inhibit-regexp nil
11373 "Local variable used by `orgtbl-mode'")
11375 (defconst orgtbl-line-start-regexp "[ \t]*\\(|\\|#\\+\\(TBLFM\\|ORGTBL\\):\\)"
11376 "Matches a line belonging to an orgtbl.")
11378 (defconst orgtbl-extra-font-lock-keywords
11379 (list (list (concat "^" orgtbl-line-start-regexp ".*")
11380 0 (quote 'org-table) 'prepend))
11381 "Extra font-lock-keywords to be added when orgtbl-mode is active.")
11384 (defun orgtbl-mode (&optional arg)
11385 "The `org-mode' table editor as a minor mode for use in other modes."
11388 ;; Exit without error, in case some hook functions calls this
11389 ;; by accident in org-mode.
11390 (message "Orgtbl-mode is not useful in org-mode, command ignored")
11392 (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
11395 (and (orgtbl-setup) (defun orgtbl-setup () nil))
11396 ;; Make sure we are first in minor-mode-map-alist
11397 (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
11398 (and c (setq minor-mode-map-alist
11399 (cons c (delq c minor-mode-map-alist)))))
11400 (org-set-local (quote org-table-may-need-update) t)
11401 (org-add-hook 'before-change-functions 'org-before-change-function
11403 (org-set-local 'org-old-auto-fill-inhibit-regexp
11404 auto-fill-inhibit-regexp)
11405 (org-set-local 'auto-fill-inhibit-regexp
11406 (if auto-fill-inhibit-regexp
11407 (concat orgtbl-line-start-regexp "\\|"
11408 auto-fill-inhibit-regexp)
11409 orgtbl-line-start-regexp))
11410 (org-add-to-invisibility-spec '(org-cwidth))
11411 (when (fboundp 'font-lock-add-keywords)
11412 (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
11413 (org-restart-font-lock))
11414 (easy-menu-add orgtbl-mode-menu)
11415 (run-hooks 'orgtbl-mode-hook))
11416 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
11417 (org-cleanup-narrow-column-properties)
11418 (org-remove-from-invisibility-spec '(org-cwidth))
11419 (remove-hook 'before-change-functions 'org-before-change-function t)
11420 (when (fboundp 'font-lock-remove-keywords)
11421 (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
11422 (org-restart-font-lock))
11423 (easy-menu-remove orgtbl-mode-menu)
11424 (force-mode-line-update 'all))))
11426 (defun org-cleanup-narrow-column-properties ()
11427 "Remove all properties related to narrow-column invisibility."
11429 (while (setq s (text-property-any s (point-max)
11430 'display org-narrow-column-arrow))
11431 (remove-text-properties s (1+ s) '(display t)))
11433 (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
11434 (remove-text-properties s (1+ s) '(org-cwidth t)))
11436 (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
11437 (remove-text-properties s (1+ s) '(invisible t)))))
11439 ;; Install it as a minor mode.
11440 (put 'orgtbl-mode :included t)
11441 (put 'orgtbl-mode :menu-tag "Org Table Mode")
11442 (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
11444 (defun orgtbl-make-binding (fun n &rest keys)
11445 "Create a function for binding in the table minor mode.
11446 FUN is the command to call inside a table. N is used to create a unique
11447 command name. KEYS are keys that should be checked in for a command
11448 to execute outside of tables."
11451 (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
11453 (concat "In tables, run `" (symbol-name fun) "'.\n"
11454 "Outside of tables, run the binding of `"
11455 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
11460 (list 'call-interactively (list 'quote fun))
11461 (list 'let '(orgtbl-mode)
11462 (list 'call-interactively
11464 (mapcar (lambda (k)
11465 (list 'key-binding k))
11467 '('orgtbl-error))))))))
11469 (defun orgtbl-error ()
11470 "Error when there is no default binding for a table key."
11472 (error "This key has no function outside tables"))
11474 (defun orgtbl-setup ()
11475 "Setup orgtbl keymaps."
11479 '([(meta shift left)] org-table-delete-column)
11480 '([(meta left)] org-table-move-column-left)
11481 '([(meta right)] org-table-move-column-right)
11482 '([(meta shift right)] org-table-insert-column)
11483 '([(meta shift up)] org-table-kill-row)
11484 '([(meta shift down)] org-table-insert-row)
11485 '([(meta up)] org-table-move-row-up)
11486 '([(meta down)] org-table-move-row-down)
11487 '("\C-c\C-w" org-table-cut-region)
11488 '("\C-c\M-w" org-table-copy-region)
11489 '("\C-c\C-y" org-table-paste-rectangle)
11490 '("\C-c-" org-table-insert-hline)
11491 '("\C-c}" org-table-toggle-coordinate-overlays)
11492 '("\C-c{" org-table-toggle-formula-debugger)
11493 '("\C-m" org-table-next-row)
11494 '([(shift return)] org-table-copy-down)
11495 '("\C-c\C-q" org-table-wrap-region)
11496 '("\C-c?" org-table-field-info)
11497 '("\C-c " org-table-blank-field)
11498 '("\C-c+" org-table-sum)
11499 '("\C-c=" org-table-eval-formula)
11500 '("\C-c'" org-table-edit-formulas)
11501 '("\C-c`" org-table-edit-field)
11502 '("\C-c*" org-table-recalculate)
11503 '("\C-c|" org-table-create-or-convert-from-region)
11504 '("\C-c^" org-table-sort-lines)
11505 '([(control ?#)] org-table-rotate-recalc-marks)))
11507 (while (setq elt (pop bindings))
11508 (setq nfunc (1+ nfunc))
11509 (setq key (org-key (car elt))
11511 cmd (orgtbl-make-binding fun nfunc key))
11512 (org-defkey orgtbl-mode-map key cmd))
11514 ;; Special treatment needed for TAB and RET
11515 (org-defkey orgtbl-mode-map [(return)]
11516 (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
11517 (org-defkey orgtbl-mode-map "\C-m"
11518 (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
11520 (org-defkey orgtbl-mode-map [(tab)]
11521 (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
11522 (org-defkey orgtbl-mode-map "\C-i"
11523 (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
11525 (org-defkey orgtbl-mode-map [(shift tab)]
11526 (orgtbl-make-binding 'org-table-previous-field 104
11527 [(shift tab)] [(tab)] "\C-i"))
11529 (org-defkey orgtbl-mode-map "\M-\C-m"
11530 (orgtbl-make-binding 'org-table-wrap-region 105
11531 "\M-\C-m" [(meta return)]))
11532 (org-defkey orgtbl-mode-map [(meta return)]
11533 (orgtbl-make-binding 'org-table-wrap-region 106
11534 [(meta return)] "\M-\C-m"))
11536 (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
11537 (when orgtbl-optimized
11538 ;; If the user wants maximum table support, we need to hijack
11539 ;; some standard editing functions
11540 (org-remap orgtbl-mode-map
11541 'self-insert-command 'orgtbl-self-insert-command
11542 'delete-char 'org-delete-char
11543 'delete-backward-char 'org-delete-backward-char)
11544 (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
11545 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
11547 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
11548 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
11549 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
11550 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
11552 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
11553 ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
11554 ["Copy Field from Above"
11555 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
11558 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
11559 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
11560 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
11561 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
11563 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
11564 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
11565 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
11566 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
11567 ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
11569 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
11571 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
11572 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
11573 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
11574 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
11577 ["Insert table template" orgtbl-insert-radio-table
11578 (assq major-mode orgtbl-radio-table-templates)]
11579 ["Comment/uncomment table" orgtbl-toggle-comment t])
11581 ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
11582 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
11583 ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
11584 ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
11585 ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
11586 ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
11587 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
11588 ["Sum Column/Rectangle" org-table-sum
11589 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
11590 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
11592 org-table-toggle-formula-debugger :active (org-at-table-p)
11594 :style toggle :selected org-table-formula-debug]
11595 ["Show Col/Row Numbers"
11596 org-table-toggle-coordinate-overlays :active (org-at-table-p)
11598 :style toggle :selected org-table-overlay-coordinates]
11602 (defun orgtbl-ctrl-c-ctrl-c (arg)
11603 "If the cursor is inside a table, realign the table.
11604 It it is a table to be sent away to a receiver, do it.
11605 With prefix arg, also recompute table."
11607 (let ((pos (point)) action)
11609 (beginning-of-line 1)
11610 (setq action (cond ((looking-at "#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
11611 ((looking-at "[ \t]*|") pos)
11612 ((looking-at "#\\+TBLFM:") 'recalc))))
11616 (org-table-maybe-eval-formula)
11618 (call-interactively 'org-table-recalculate)
11619 (org-table-maybe-recalculate-line))
11620 (call-interactively 'org-table-align)
11621 (orgtbl-send-table 'maybe))
11622 ((eq action 'recalc)
11624 (beginning-of-line 1)
11625 (skip-chars-backward " \r\n\t")
11626 (if (org-at-table-p)
11627 (org-call-with-arg 'org-table-recalculate t))))
11628 (t (let (orgtbl-mode)
11629 (call-interactively (key-binding "\C-c\C-c")))))))
11631 (defun orgtbl-tab (arg)
11632 "Justification and field motion for `orgtbl-mode'."
11634 (if arg (org-table-edit-field t)
11635 (org-table-justify-field-maybe)
11636 (org-table-next-field)))
11638 (defun orgtbl-ret ()
11639 "Justification and field motion for `orgtbl-mode'."
11641 (org-table-justify-field-maybe)
11642 (org-table-next-row))
11644 (defun orgtbl-self-insert-command (N)
11645 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
11646 If the cursor is in a table looking at whitespace, the whitespace is
11647 overwritten, and the table is not marked as requiring realignment."
11649 (if (and (org-at-table-p)
11651 (and org-table-auto-blank-field
11652 (member last-command
11653 '(orgtbl-hijacker-command-100
11654 orgtbl-hijacker-command-101
11655 orgtbl-hijacker-command-102
11656 orgtbl-hijacker-command-103
11657 orgtbl-hijacker-command-104
11658 orgtbl-hijacker-command-105))
11659 (org-table-blank-field))
11662 (looking-at "[^|\n]* +|"))
11663 (let (org-table-may-need-update)
11664 (goto-char (1- (match-end 0)))
11665 (delete-backward-char 1)
11666 (goto-char (match-beginning 0))
11667 (self-insert-command N))
11668 (setq org-table-may-need-update t)
11670 (call-interactively (key-binding (vector last-input-event))))))
11672 (defun org-force-self-insert (N)
11673 "Needed to enforce self-insert under remapping."
11675 (self-insert-command N))
11677 (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
11678 "Regula expression matching exponentials as produced by calc.")
11680 (defvar org-table-clean-did-remove-column nil)
11682 (defun orgtbl-export (table target)
11683 (let ((func (intern (concat "orgtbl-to-" (symbol-name target))))
11684 (lines (org-split-string table "[ \t]*\n[ \t]*"))
11685 org-table-last-alignment org-table-last-column-widths
11687 (if (not (fboundp func))
11688 (error "Cannot export orgtbl table to %s" target))
11689 (setq lines (org-table-clean-before-export lines))
11693 (if (string-match org-table-hline-regexp x)
11695 (org-split-string (org-trim x) "\\s-*|\\s-*")))
11697 (setq maxcol (apply 'max (mapcar (lambda (x) (if (listp x) (length x) 0))
11699 (loop for i from (1- maxcol) downto 0 do
11700 (setq column (mapcar (lambda (x) (if (listp x) (nth i x) nil)) table))
11701 (setq column (delq nil column))
11702 (push (apply 'max (mapcar 'string-width column)) org-table-last-column-widths)
11703 (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))
11704 (funcall func table nil)))
11706 (defun orgtbl-send-table (&optional maybe)
11707 "Send a tranformed version of this table to the receiver position.
11708 With argument MAYBE, fail quietly if no transformation is defined for
11712 (unless (org-at-table-p) (error "Not at a table"))
11713 ;; when non-interactive, we assume align has just happened.
11714 (when (interactive-p) (org-table-align))
11716 (goto-char (org-table-begin))
11717 (beginning-of-line 0)
11718 (unless (looking-at "#\\+ORGTBL: *SEND +\\([a-zA-Z0-9_]+\\) +\\([^ \t\r\n]+\\)\\( +.*\\)?")
11721 (error "Don't know how to transform this table."))))
11722 (let* ((name (match-string 1))
11724 (transform (intern (match-string 2)))
11725 (params (if (match-end 3) (read (concat "(" (match-string 3) ")"))))
11726 (skip (plist-get params :skip))
11727 (skipcols (plist-get params :skipcols))
11728 (txt (buffer-substring-no-properties
11729 (org-table-begin) (org-table-end)))
11730 (lines (nthcdr (or skip 0) (org-split-string txt "[ \t]*\n[ \t]*")))
11731 (lines (org-table-clean-before-export lines))
11732 (i0 (if org-table-clean-did-remove-column 2 1))
11735 (if (string-match org-table-hline-regexp x)
11737 (org-remove-by-index
11738 (org-split-string (org-trim x) "\\s-*|\\s-*")
11741 (fun (if (= i0 2) 'cdr 'identity))
11742 (org-table-last-alignment
11743 (org-remove-by-index (funcall fun org-table-last-alignment)
11745 (org-table-last-column-widths
11746 (org-remove-by-index (funcall fun org-table-last-column-widths)
11749 (unless (fboundp transform)
11750 (error "No such transformation function %s" transform))
11751 (setq txt (funcall transform table params))
11752 ;; Find the insertion place
11754 (goto-char (point-min))
11755 (unless (re-search-forward
11756 (concat "BEGIN RECEIVE ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
11757 (error "Don't know where to insert translated table"))
11758 (goto-char (match-beginning 0))
11759 (beginning-of-line 2)
11761 (unless (re-search-forward (concat "END RECEIVE ORGTBL +" name) nil t)
11762 (error "Cannot find end of insertion region"))
11763 (beginning-of-line 1)
11764 (delete-region beg (point))
11767 (message "Table converted and installed at receiver location"))))
11769 (defun org-remove-by-index (list indices &optional i0)
11770 "Remove the elements in LIST with indices in INDICES.
11771 First element has index 0, or I0 if given."
11774 (if (integerp indices) (setq indices (list indices)))
11775 (setq i0 (1- (or i0 0)))
11776 (delq :rm (mapcar (lambda (x)
11778 (if (memq i0 indices) :rm x))
11781 (defun orgtbl-toggle-comment ()
11782 "Comment or uncomment the orgtbl at point."
11784 (let* ((re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
11785 (re2 (concat "^" orgtbl-line-start-regexp))
11786 (commented (save-excursion (beginning-of-line 1)
11787 (cond ((looking-at re1) t)
11788 ((looking-at re2) nil)
11789 (t (error "Not at an org table")))))
11790 (re (if commented re1 re2))
11793 (beginning-of-line 1)
11794 (while (looking-at re) (beginning-of-line 0))
11795 (beginning-of-line 2)
11797 (while (looking-at re) (beginning-of-line 2))
11798 (setq end (point)))
11799 (comment-region beg end (if commented '(4) nil))))
11801 (defun orgtbl-insert-radio-table ()
11802 "Insert a radio table template appropriate for this major mode."
11804 (let* ((e (assq major-mode orgtbl-radio-table-templates))
11807 (unless e (error "No radio table setup defined for %s" major-mode))
11808 (setq name (read-string "Table name: "))
11809 (while (string-match "%n" txt)
11810 (setq txt (replace-match name t t txt)))
11811 (or (bolp) (insert "\n"))
11816 (defun org-get-param (params header i sym &optional hsym)
11817 "Get parameter value for symbol SYM.
11818 If this is a header line, actually get the value for the symbol with an
11819 additional \"h\" inserted after the colon.
11820 If the value is a protperty list, get the element for the current column.
11821 Assumes variables VAL, PARAMS, HEAD and I to be scoped into the function."
11822 (let ((val (plist-get params sym)))
11823 (and hsym header (setq val (or (plist-get params hsym) val)))
11824 (if (consp val) (plist-get val i) val)))
11826 (defun orgtbl-to-generic (table params)
11827 "Convert the orgtbl-mode TABLE to some other format.
11828 This generic routine can be used for many standard cases.
11829 TABLE is a list, each entry either the symbol `hline' for a horizontal
11830 separator line, or a list of fields for that line.
11831 PARAMS is a property list of parameters that can influence the conversion.
11832 For the generic converter, some parameters are obligatory: You need to
11833 specify either :lfmt, or all of (:lstart :lend :sep). If you do not use
11834 :splice, you must have :tstart and :tend.
11836 Valid parameters are
11838 :tstart String to start the table. Ignored when :splice is t.
11839 :tend String to end the table. Ignored when :splice is t.
11841 :splice When set to t, return only table body lines, don't wrap
11842 them into :tstart and :tend. Default is nil.
11844 :hline String to be inserted on horizontal separation lines.
11845 May be nil to ignore hlines.
11847 :lstart String to start a new table line.
11848 :lend String to end a table line
11849 :sep Separator between two fields
11850 :lfmt Format for entire line, with enough %s to capture all fields.
11851 If this is present, :lstart, :lend, and :sep are ignored.
11852 :fmt A format to be used to wrap the field, should contain
11853 %s for the original field value. For example, to wrap
11854 everything in dollars, you could use :fmt \"$%s$\".
11855 This may also be a property list with column numbers and
11856 formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
11858 :hlstart :hlend :hlsep :hlfmt :hfmt
11859 Same as above, specific for the header lines in the table.
11860 All lines before the first hline are treated as header.
11861 If any of these is not present, the data line value is used.
11863 :efmt Use this format to print numbers with exponentials.
11864 The format should have %s twice for inserting mantissa
11865 and exponent, for example \"%s\\\\times10^{%s}\". This
11866 may also be a property list with column numbers and
11867 formats. :fmt will still be applied after :efmt.
11869 In addition to this, the parameters :skip and :skipcols are always handled
11870 directly by `orgtbl-send-table'. See manual."
11873 (splicep (plist-get p :splice))
11874 (hline (plist-get p :hline))
11875 rtn line i fm efm lfmt h)
11877 ;; Do we have a header?
11878 (if (and (not splicep) (listp (car table)) (memq 'hline table))
11883 (push (or (plist-get p :tstart) "ERROR: no :tstart") rtn))
11885 ;; Now loop over all lines
11886 (while (setq line (pop table))
11887 (if (eq line 'hline)
11888 ;; A horizontal separator line
11889 (progn (if hline (push hline rtn))
11890 (setq h nil)) ; no longer in header
11891 ;; A normal line. Convert the fields, push line onto the result list
11897 fm (org-get-param p h i :fmt :hfmt)
11898 efm (org-get-param p h i :efmt))
11899 (if (and efm (string-match orgtbl-exp-regexp f))
11901 efm (match-string 1 f) (match-string 2 f))))
11902 (if fm (setq f (format fm f)))
11905 (if (setq lfmt (org-get-param p h i :lfmt :hlfmt))
11906 (push (apply 'format lfmt line) rtn)
11908 (org-get-param p h i :lstart :hlstart)
11909 (mapconcat 'identity line (org-get-param p h i :sep :hsep))
11910 (org-get-param p h i :lend :hlend))
11914 (push (or (plist-get p :tend) "ERROR: no :tend") rtn))
11916 (mapconcat 'identity (nreverse rtn) "\n")))
11918 (defun orgtbl-to-latex (table params)
11919 "Convert the orgtbl-mode TABLE to LaTeX.
11920 TABLE is a list, each entry either the symbol `hline' for a horizontal
11921 separator line, or a list of fields for that line.
11922 PARAMS is a property list of parameters that can influence the conversion.
11923 Supports all parameters from `orgtbl-to-generic'. Most important for
11926 :splice When set to t, return only table body lines, don't wrap
11927 them into a tabular environment. Default is nil.
11929 :fmt A format to be used to wrap the field, should contain %s for the
11930 original field value. For example, to wrap everything in dollars,
11931 use :fmt \"$%s$\". This may also be a property list with column
11932 numbers and formats. For example :fmt (2 \"$%s$\" 4 \"%s%%\")
11934 :efmt Format for transforming numbers with exponentials. The format
11935 should have %s twice for inserting mantissa and exponent, for
11936 example \"%s\\\\times10^{%s}\". LaTeX default is \"%s\\\\,(%s)\".
11937 This may also be a property list with column numbers and formats.
11939 The general parameters :skip and :skipcols have already been applied when
11940 this function is called."
11941 (let* ((alignment (mapconcat (lambda (x) (if x "r" "l"))
11942 org-table-last-alignment ""))
11945 :tstart (concat "\\begin{tabular}{" alignment "}")
11946 :tend "\\end{tabular}"
11947 :lstart "" :lend " \\\\" :sep " & "
11948 :efmt "%s\\,(%s)" :hline "\\hline")))
11949 (orgtbl-to-generic table (org-combine-plists params2 params))))
11951 (defun orgtbl-to-html (table params)
11952 "Convert the orgtbl-mode TABLE to LaTeX.
11953 TABLE is a list, each entry either the symbol `hline' for a horizontal
11954 separator line, or a list of fields for that line.
11955 PARAMS is a property list of parameters that can influence the conversion.
11956 Currently this function recognizes the following parameters:
11958 :splice When set to t, return only table body lines, don't wrap
11959 them into a <table> environment. Default is nil.
11961 The general parameters :skip and :skipcols have already been applied when
11962 this function is called. The function does *not* use `orgtbl-to-generic',
11963 so you cannot specify parameters for it."
11964 (let* ((splicep (plist-get params :splice))
11966 ;; Just call the formatter we already have
11967 ;; We need to make text lines for it, so put the fields back together.
11968 (setq html (org-format-org-table-html
11973 (concat "| " (mapconcat 'identity x " | ") " |")))
11976 (if (string-match "\n+\\'" html)
11977 (setq html (replace-match "" t t html)))
11980 (defun orgtbl-to-texinfo (table params)
11981 "Convert the orgtbl-mode TABLE to TeXInfo.
11982 TABLE is a list, each entry either the symbol `hline' for a horizontal
11983 separator line, or a list of fields for that line.
11984 PARAMS is a property list of parameters that can influence the conversion.
11985 Supports all parameters from `orgtbl-to-generic'. Most important for
11988 :splice nil/t When set to t, return only table body lines, don't wrap
11989 them into a multitable environment. Default is nil.
11991 :fmt fmt A format to be used to wrap the field, should contain
11992 %s for the original field value. For example, to wrap
11993 everything in @kbd{}, you could use :fmt \"@kbd{%s}\".
11994 This may also be a property list with column numbers and
11995 formats. For example :fmt (2 \"@kbd{%s}\" 4 \"@code{%s}\").
11997 :cf \"f1 f2..\" The column fractions for the table. By default these
11998 are computed automatically from the width of the columns
12001 The general parameters :skip and :skipcols have already been applied when
12002 this function is called."
12003 (let* ((total (float (apply '+ org-table-last-column-widths)))
12004 (colfrac (or (plist-get params :cf)
12006 (lambda (x) (format "%.3f" (/ (float x) total)))
12007 org-table-last-column-widths " ")))
12010 :tstart (concat "@multitable @columnfractions " colfrac)
12011 :tend "@end multitable"
12012 :lstart "@item " :lend "" :sep " @tab "
12013 :hlstart "@headitem ")))
12014 (orgtbl-to-generic table (org-combine-plists params2 params))))
12018 ;;; Link abbreviations
12020 (defun org-link-expand-abbrev (link)
12021 "Apply replacements as defined in `org-link-abbrev-alist."
12022 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
12023 (let* ((key (match-string 1 link))
12024 (as (or (assoc key org-link-abbrev-alist-local)
12025 (assoc key org-link-abbrev-alist)))
12026 (tag (and (match-end 2) (match-string 3 link)))
12030 (setq rpl (cdr as))
12032 ((symbolp rpl) (funcall rpl tag))
12033 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
12034 (t (concat rpl tag)))))
12037 ;;; Storing and inserting links
12039 (defvar org-insert-link-history nil
12040 "Minibuffer history for links inserted with `org-insert-link'.")
12042 (defvar org-stored-links nil
12043 "Contains the links stored with `org-store-link'.")
12045 (defvar org-store-link-plist nil
12046 "Plist with info about the most recently link created with `org-store-link'.")
12048 (defvar org-link-protocols nil
12049 "Link protocols added to Org-mode using `org-add-link-type'.")
12051 (defvar org-store-link-functions nil
12052 "List of functions that are called to create and store a link.
12053 Each function will be called in turn until one returns a non-nil
12054 value. Each function should check if it is responsible for creating
12055 this link (for example by looking at the major mode).
12056 If not, it must exit and return nil.
12057 If yes, it should return a non-nil value after a calling
12058 `org-store-link-props' with a list of properties and values.
12059 Special properties are:
12061 :type The link prefix. like \"http\". This must be given.
12062 :link The link, like \"http://www.astro.uva.nl/~dominik\".
12063 This is obligatory as well.
12064 :description Optional default description for the second pair
12065 of brackets in an Org-mode link. The user can still change
12066 this when inserting this link into an Org-mode buffer.
12068 In addition to these, any additional properties can be specified
12069 and then used in remember templates.")
12071 (defun org-add-link-type (type &optional follow publish)
12072 "Add TYPE to the list of `org-link-types'.
12073 Re-compute all regular expressions depending on `org-link-types'
12074 FOLLOW and PUBLISH are two functions. Both take the link path as
12076 FOLLOW should do whatever is necessary to follow the link, for example
12077 to find a file or display a mail message.
12079 PUBLISH takes the path and retuns the string that should be used when
12080 this document is published. FIMXE: This is actually not yet implemented."
12081 (add-to-list 'org-link-types type t)
12082 (org-make-link-regexps)
12083 (add-to-list 'org-link-protocols
12084 (list type follow publish)))
12086 (defun org-add-agenda-custom-command (entry)
12087 "Replace or add a command in `org-agenda-custom-commands'.
12088 This is mostly for hacking and trying a new command - once the command
12089 works you probably want to add it to `org-agenda-custom-commands' for good."
12090 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
12092 (setcdr ass (cdr entry))
12093 (push entry org-agenda-custom-commands))))
12096 (defun org-store-link (arg)
12097 "\\<org-mode-map>Store an org-link to the current location.
12098 This link is added to `org-stored-links' and can later be inserted
12099 into an org-buffer with \\[org-insert-link].
12101 For some link types, a prefix arg is interpreted:
12102 For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
12103 For file links, arg negates `org-context-in-file-links'."
12105 (setq org-store-link-plist nil) ; reset
12106 (let (link cpltxt desc description search txt)
12109 ((run-hook-with-args-until-success 'org-store-link-functions)
12110 (setq link (plist-get org-store-link-plist :link)
12111 desc (or (plist-get org-store-link-plist :description) link)))
12113 ((eq major-mode 'bbdb-mode)
12114 (let ((name (bbdb-record-name (bbdb-current-record)))
12115 (company (bbdb-record-getprop (bbdb-current-record) 'company)))
12116 (setq cpltxt (concat "bbdb:" (or name company))
12117 link (org-make-link cpltxt))
12118 (org-store-link-props :type "bbdb" :name name :company company)))
12120 ((eq major-mode 'Info-mode)
12121 (setq link (org-make-link "info:"
12122 (file-name-nondirectory Info-current-file)
12123 ":" Info-current-node))
12124 (setq cpltxt (concat (file-name-nondirectory Info-current-file)
12125 ":" Info-current-node))
12126 (org-store-link-props :type "info" :file Info-current-file
12127 :node Info-current-node))
12129 ((eq major-mode 'calendar-mode)
12130 (let ((cd (calendar-cursor-to-date)))
12132 (format-time-string
12133 (car org-time-stamp-formats)
12134 (apply 'encode-time
12135 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
12137 (org-store-link-props :type "calendar" :date cd)))
12139 ((or (eq major-mode 'vm-summary-mode)
12140 (eq major-mode 'vm-presentation-mode))
12141 (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
12142 (vm-follow-summary-cursor)
12144 (vm-select-folder-buffer)
12145 (let* ((message (car vm-message-pointer))
12146 (folder buffer-file-name)
12147 (subject (vm-su-subject message))
12148 (to (vm-get-header-contents message "To"))
12149 (from (vm-get-header-contents message "From"))
12150 (message-id (vm-su-message-id message)))
12151 (org-store-link-props :type "vm" :from from :to to :subject subject
12152 :message-id message-id)
12153 (setq message-id (org-remove-angle-brackets message-id))
12154 (setq folder (abbreviate-file-name folder))
12155 (if (string-match (concat "^" (regexp-quote vm-folder-directory))
12157 (setq folder (replace-match "" t t folder)))
12158 (setq cpltxt (org-email-link-description))
12159 (setq link (org-make-link "vm:" folder "#" message-id)))))
12161 ((eq major-mode 'wl-summary-mode)
12162 (let* ((msgnum (wl-summary-message-number))
12163 (message-id (elmo-message-field wl-summary-buffer-elmo-folder
12164 msgnum 'message-id))
12166 (if (fboundp 'elmo-message-entity)
12167 (elmo-message-entity
12168 wl-summary-buffer-elmo-folder msgnum)
12169 (elmo-msgdb-overview-get-entity
12170 msgnum (wl-summary-buffer-msgdb))))
12171 (from (wl-summary-line-from))
12172 (to (car (elmo-message-entity-field wl-message-entity 'to)))
12173 (subject (let (wl-thr-indent-string wl-parent-message-entity)
12174 (wl-summary-line-subject))))
12175 (org-store-link-props :type "wl" :from from :to to
12176 :subject subject :message-id message-id)
12177 (setq message-id (org-remove-angle-brackets message-id))
12178 (setq cpltxt (org-email-link-description))
12179 (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
12182 ((or (equal major-mode 'mh-folder-mode)
12183 (equal major-mode 'mh-show-mode))
12184 (let ((from (org-mhe-get-header "From:"))
12185 (to (org-mhe-get-header "To:"))
12186 (message-id (org-mhe-get-header "Message-Id:"))
12187 (subject (org-mhe-get-header "Subject:")))
12188 (org-store-link-props :type "mh" :from from :to to
12189 :subject subject :message-id message-id)
12190 (setq cpltxt (org-email-link-description))
12191 (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
12192 (org-remove-angle-brackets message-id)))))
12194 ((or (eq major-mode 'rmail-mode)
12195 (eq major-mode 'rmail-summary-mode))
12196 (save-window-excursion
12198 (when (eq major-mode 'rmail-summary-mode)
12199 (rmail-show-message rmail-current-message))
12200 (rmail-narrow-to-non-pruned-header)
12201 (let ((folder buffer-file-name)
12202 (message-id (mail-fetch-field "message-id"))
12203 (from (mail-fetch-field "from"))
12204 (to (mail-fetch-field "to"))
12205 (subject (mail-fetch-field "subject")))
12206 (org-store-link-props
12207 :type "rmail" :from from :to to
12208 :subject subject :message-id message-id)
12209 (setq message-id (org-remove-angle-brackets message-id))
12210 (setq cpltxt (org-email-link-description))
12211 (setq link (org-make-link "rmail:" folder "#" message-id)))
12212 (rmail-show-message rmail-current-message))))
12214 ((eq major-mode 'gnus-group-mode)
12215 (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
12216 (gnus-group-group-name)) ; version
12217 ((fboundp 'gnus-group-name)
12220 (unless group (error "Not on a group"))
12221 (org-store-link-props :type "gnus" :group group)
12222 (setq cpltxt (concat
12223 (if (org-xor arg org-usenet-links-prefer-google)
12224 "http://groups.google.com/groups?group="
12227 link (org-make-link cpltxt))))
12229 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
12230 (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
12231 (let* ((group gnus-newsgroup-name)
12232 (article (gnus-summary-article-number))
12233 (header (gnus-summary-article-header article))
12234 (from (mail-header-from header))
12235 (message-id (mail-header-id header))
12236 (date (mail-header-date header))
12237 (subject (gnus-summary-subject-string)))
12238 (org-store-link-props :type "gnus" :from from :subject subject
12239 :message-id message-id :group group)
12240 (setq cpltxt (org-email-link-description))
12241 (if (org-xor arg org-usenet-links-prefer-google)
12245 (format "http://groups.google.com/groups?as_umsgid=%s"
12246 (org-fixup-message-id-for-http message-id))))
12247 (setq link (org-make-link "gnus:" group
12248 "#" (number-to-string article))))))
12250 ((eq major-mode 'w3-mode)
12251 (setq cpltxt (url-view-url t)
12252 link (org-make-link cpltxt))
12253 (org-store-link-props :type "w3" :url (url-view-url t)))
12255 ((eq major-mode 'w3m-mode)
12256 (setq cpltxt (or w3m-current-title w3m-current-url)
12257 link (org-make-link w3m-current-url))
12258 (org-store-link-props :type "w3m" :url (url-view-url t)))
12260 ((setq search (run-hook-with-args-until-success
12261 'org-create-file-search-functions))
12262 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
12264 (setq cpltxt (or description link)))
12266 ((eq major-mode 'image-mode)
12267 (setq cpltxt (concat "file:"
12268 (abbreviate-file-name buffer-file-name))
12269 link (org-make-link cpltxt))
12270 (org-store-link-props :type "image" :file buffer-file-name))
12272 ((eq major-mode 'dired-mode)
12273 ;; link to the file in the current line
12274 (setq cpltxt (concat "file:"
12275 (abbreviate-file-name
12277 (dired-get-filename nil t))))
12278 link (org-make-link cpltxt)))
12280 ((and buffer-file-name (org-mode-p))
12281 ;; Just link to current headline
12282 (setq cpltxt (concat "file:"
12283 (abbreviate-file-name buffer-file-name)))
12284 ;; Add a context search string
12285 (when (org-xor org-context-in-file-links arg)
12286 ;; Check if we are on a target
12287 (if (org-in-regexp "<<\\(.*?\\)>>")
12288 (setq cpltxt (concat cpltxt "::" (match-string 1)))
12290 ((org-on-heading-p) nil)
12291 ((org-region-active-p)
12292 (buffer-substring (region-beginning) (region-end)))
12293 (t (buffer-substring (point-at-bol) (point-at-eol)))))
12294 (when (or (null txt) (string-match "\\S-" txt))
12296 (concat cpltxt "::" (org-make-org-heading-search-string txt))
12298 (if (string-match "::\\'" cpltxt)
12299 (setq cpltxt (substring cpltxt 0 -2)))
12300 (setq link (org-make-link cpltxt)))
12302 ((buffer-file-name (buffer-base-buffer))
12303 ;; Just link to this file here.
12304 (setq cpltxt (concat "file:"
12305 (abbreviate-file-name
12306 (buffer-file-name (buffer-base-buffer)))))
12307 ;; Add a context string
12308 (when (org-xor org-context-in-file-links arg)
12309 (setq txt (if (org-region-active-p)
12310 (buffer-substring (region-beginning) (region-end))
12311 (buffer-substring (point-at-bol) (point-at-eol))))
12312 ;; Only use search option if there is some text.
12313 (when (string-match "\\S-" txt)
12315 (concat cpltxt "::" (org-make-org-heading-search-string txt))
12317 (setq link (org-make-link cpltxt)))
12320 (error "Cannot link to a buffer which is not visiting a file"))
12322 (t (setq link nil)))
12324 (if (consp link) (setq cpltxt (car link) link (cdr link)))
12325 (setq link (or link cpltxt)
12326 desc (or desc cpltxt))
12327 (if (equal desc "NONE") (setq desc nil))
12329 (if (and (interactive-p) link)
12331 (setq org-stored-links
12332 (cons (list link desc) org-stored-links))
12333 (message "Stored: %s" (or desc link)))
12334 (and link (org-make-link-string link desc)))))
12336 (defun org-store-link-props (&rest plist)
12337 "Store link properties, extract names and addresses."
12339 (when (setq x (plist-get plist :from))
12340 (setq adr (mail-extract-address-components x))
12341 (plist-put plist :fromname (car adr))
12342 (plist-put plist :fromaddress (nth 1 adr)))
12343 (when (setq x (plist-get plist :to))
12344 (setq adr (mail-extract-address-components x))
12345 (plist-put plist :toname (car adr))
12346 (plist-put plist :toaddress (nth 1 adr))))
12347 (let ((from (plist-get plist :from))
12348 (to (plist-get plist :to)))
12349 (when (and from to org-from-is-user-regexp)
12350 (plist-put plist :fromto
12351 (if (string-match org-from-is-user-regexp from)
12353 (concat "from %f")))))
12354 (setq org-store-link-plist plist))
12356 (defun org-email-link-description (&optional fmt)
12357 "Return the description part of an email link.
12358 This takes information from `org-store-link-plist' and formats it
12359 according to FMT (default from `org-email-link-description-format')."
12360 (setq fmt (or fmt org-email-link-description-format))
12361 (let* ((p org-store-link-plist)
12362 (to (plist-get p :toaddress))
12363 (from (plist-get p :fromaddress))
12366 (cons "%c" (plist-get p :fromto))
12367 (cons "%F" (plist-get p :from))
12368 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
12369 (cons "%T" (plist-get p :to))
12370 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
12371 (cons "%s" (plist-get p :subject))
12372 (cons "%m" (plist-get p :message-id)))))
12373 (when (string-match "%c" fmt)
12374 ;; Check if the user wrote this message
12375 (if (and org-from-is-user-regexp from to
12376 (save-match-data (string-match org-from-is-user-regexp from)))
12377 (setq fmt (replace-match "to %t" t t fmt))
12378 (setq fmt (replace-match "from %f" t t fmt))))
12379 (org-replace-escapes fmt table)))
12381 (defun org-make-org-heading-search-string (&optional string heading)
12382 "Make search string for STRING or current headline."
12384 (let ((s (or string (org-get-heading))))
12385 (unless (and string (not heading))
12386 ;; We are using a headline, clean up garbage in there.
12387 (if (string-match org-todo-regexp s)
12388 (setq s (replace-match "" t t s)))
12389 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
12390 (setq s (replace-match "" t t s)))
12391 (setq s (org-trim s))
12392 (if (string-match (concat "^\\(" org-quote-string "\\|"
12393 org-comment-string "\\)") s)
12394 (setq s (replace-match "" t t s)))
12395 (while (string-match org-ts-regexp s)
12396 (setq s (replace-match "" t t s))))
12397 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
12398 (setq s (replace-match " " t t s)))
12399 (or string (setq s (concat "*" s))) ; Add * for headlines
12400 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
12402 (defun org-make-link (&rest strings)
12403 "Concatenate STRINGS."
12404 (apply 'concat strings))
12406 (defun org-make-link-string (link &optional description)
12407 "Make a link with brackets, consisting of LINK and DESCRIPTION."
12408 (unless (string-match "\\S-" link)
12409 (error "Empty link"))
12410 (when (stringp description)
12411 ;; Remove brackets from the description, they are fatal.
12412 (while (string-match "\\[" description)
12413 (setq description (replace-match "{" t t description)))
12414 (while (string-match "\\]" description)
12415 (setq description (replace-match "}" t t description))))
12416 (when (equal (org-link-escape link) description)
12417 ;; No description needed, it is identical
12418 (setq description nil))
12419 (when (and (not description)
12420 (not (equal link (org-link-escape link))))
12421 (setq description link))
12422 (concat "[[" (org-link-escape link) "]"
12423 (if description (concat "[" description "]") "")
12426 (defconst org-link-escape-chars
12430 (?\340 . "%E0") ; `a
12431 (?\342 . "%E2") ; ^a
12432 (?\347 . "%E7") ; ,c
12433 (?\350 . "%E8") ; `e
12434 (?\351 . "%E9") ; 'e
12435 (?\352 . "%EA") ; ^e
12436 (?\356 . "%EE") ; ^i
12437 (?\364 . "%F4") ; ^o
12438 (?\371 . "%F9") ; `u
12439 (?\373 . "%FB") ; ^u
12445 "Association list of escapes for some characters problematic in links.
12446 This is the list that is used for internal purposes.")
12448 (defconst org-link-escape-chars-browser
12449 '((?\ . "%20")) ; 32 for the SPC char
12450 "Association list of escapes for some characters problematic in links.
12451 This is the list that is used before handing over to the browser.")
12453 (defun org-link-escape (text &optional table)
12454 "Escape charaters in TEXT that are problematic for links."
12455 (setq table (or table org-link-escape-chars))
12457 (let ((re (mapconcat (lambda (x) (regexp-quote
12458 (char-to-string (car x))))
12460 (while (string-match re text)
12463 (cdr (assoc (string-to-char (match-string 0 text))
12468 (defun org-link-unescape (text &optional table)
12469 "Reverse the action of `org-link-escape'."
12470 (setq table (or table org-link-escape-chars))
12472 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
12474 (while (string-match re text)
12477 (char-to-string (car (rassoc (match-string 0 text) table)))
12481 (defun org-xor (a b)
12485 (defun org-get-header (header)
12486 "Find a header field in the current buffer."
12488 (goto-char (point-min))
12489 (let ((case-fold-search t) s)
12492 (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
12493 (setq s (match-string 1)))
12494 (while (string-match "\"" s)
12495 (setq s (replace-match "" t t s)))
12496 (if (string-match "[<(].*" s)
12497 (setq s (replace-match "" t t s))))
12498 ((eq header 'message-id)
12499 (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
12500 (setq s (match-string 1))))
12501 ((eq header 'subject)
12502 (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
12503 (setq s (match-string 1)))))
12504 (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
12505 (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
12509 (defun org-fixup-message-id-for-http (s)
12510 "Replace special characters in a message id, so it can be used in an http query."
12511 (while (string-match "<" s)
12512 (setq s (replace-match "%3C" t t s)))
12513 (while (string-match ">" s)
12514 (setq s (replace-match "%3E" t t s)))
12515 (while (string-match "@" s)
12516 (setq s (replace-match "%40" t t s)))
12520 (defun org-insert-link-global ()
12521 "Insert a link like Org-mode does.
12522 This command can be called in any mode to insert a link in Org-mode syntax."
12524 (org-run-like-in-org-mode 'org-insert-link))
12526 (defun org-insert-link (&optional complete-file)
12527 "Insert a link. At the prompt, enter the link.
12529 Completion can be used to select a link previously stored with
12530 `org-store-link'. When the empty string is entered (i.e. if you just
12531 press RET at the prompt), the link defaults to the most recently
12532 stored link. As SPC triggers completion in the minibuffer, you need to
12533 use M-SPC or C-q SPC to force the insertion of a space character.
12535 You will also be prompted for a description, and if one is given, it will
12536 be displayed in the buffer instead of the link.
12538 If there is already a link at point, this command will allow you to edit link
12539 and description parts.
12541 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
12542 selected using completion. The path to the file will be relative to
12543 the current directory if the file is in the current directory or a
12544 subdirectory. Otherwise, the link will be the absolute path as
12545 completed in the minibuffer (i.e. normally ~/path/to/file).
12547 With two \\[universal-argument] prefixes, enforce an absolute path even if the file
12548 is in the current directory or below.
12549 With three \\[universal-argument] prefixes, negate the meaning of
12550 `org-keep-stored-link-after-insertion'."
12552 (let* ((wcf (current-window-configuration))
12553 (region (if (org-region-active-p)
12554 (buffer-substring (region-beginning) (region-end))))
12555 (remove (and region (list (region-beginning) (region-end))))
12557 tmphist ; byte-compile incorrectly complains about this
12560 ((org-in-regexp org-bracket-link-regexp 1)
12561 ;; We do have a link at point, and we are going to edit it.
12562 (setq remove (list (match-beginning 0) (match-end 0)))
12563 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
12564 (setq link (read-string "Link: "
12566 (org-match-string-no-properties 1)))))
12567 ((or (org-in-regexp org-angle-link-re)
12568 (org-in-regexp org-plain-link-re))
12569 ;; Convert to bracket link
12570 (setq remove (list (match-beginning 0) (match-end 0))
12571 link (read-string "Link: "
12572 (org-remove-angle-brackets (match-string 0)))))
12573 ((equal complete-file '(4))
12574 ;; Completing read for file names.
12575 (setq file (read-file-name "File: "))
12576 (let ((pwd (file-name-as-directory (expand-file-name ".")))
12577 (pwd1 (file-name-as-directory (abbreviate-file-name
12578 (expand-file-name ".")))))
12580 ((equal complete-file '(16))
12581 (setq link (org-make-link
12583 (abbreviate-file-name (expand-file-name file)))))
12584 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
12585 (setq link (org-make-link "file:" (match-string 1 file))))
12586 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
12587 (expand-file-name file))
12588 (setq link (org-make-link
12589 "file:" (match-string 1 (expand-file-name file)))))
12590 (t (setq link (org-make-link "file:" file))))))
12592 ;; Read link, with completion for stored links.
12593 (with-output-to-temp-buffer "*Org Links*"
12594 (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
12595 (when org-stored-links
12596 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
12599 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
12600 (reverse org-stored-links) "\n"))))
12601 (let ((cw (selected-window)))
12602 (select-window (get-buffer-window "*Org Links*"))
12603 (shrink-window-if-larger-than-buffer)
12604 (setq truncate-lines t)
12605 (select-window cw))
12606 ;; Fake a link history, containing the stored links.
12607 (setq tmphist (append (mapcar 'car org-stored-links)
12608 org-insert-link-history))
12610 (setq link (org-completing-read
12613 (mapcar (lambda (x) (list (concat (car x) ":")))
12614 (append org-link-abbrev-alist-local org-link-abbrev-alist))
12615 (mapcar (lambda (x) (list (concat x ":")))
12619 (or (car (car org-stored-links)))))
12620 (set-window-configuration wcf)
12621 (kill-buffer "*Org Links*"))
12622 (setq entry (assoc link org-stored-links))
12623 (or entry (push link org-insert-link-history))
12624 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
12625 (not org-keep-stored-link-after-insertion))
12626 (setq org-stored-links (delq (assoc link org-stored-links)
12627 org-stored-links)))
12628 (setq desc (or desc (nth 1 entry)))))
12630 (if (string-match org-plain-link-re link)
12631 ;; URL-like link, normalize the use of angular brackets.
12632 (setq link (org-make-link (org-remove-angle-brackets link))))
12634 ;; Check if we are linking to the current file with a search option
12635 ;; If yes, simplify the link by using only the search option.
12636 (when (and buffer-file-name
12637 (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
12638 (let* ((path (match-string 1 link))
12639 (case-fold-search nil)
12640 (search (match-string 2 link)))
12642 (if (equal (file-truename buffer-file-name) (file-truename path))
12643 ;; We are linking to this same file, with a search option
12644 (setq link search)))))
12646 ;; Check if we can/should use a relative path. If yes, simplify the link
12647 (when (string-match "\\<file:\\(.*\\)" link)
12648 (let* ((path (match-string 1 link))
12650 (case-fold-search nil))
12652 ((eq org-link-file-path-type 'absolute)
12653 (setq path (abbreviate-file-name (expand-file-name path))))
12654 ((eq org-link-file-path-type 'noabbrev)
12655 (setq path (expand-file-name path)))
12656 ((eq org-link-file-path-type 'relative)
12657 (setq path (file-relative-name path)))
12660 (if (string-match (concat "^" (regexp-quote
12661 (file-name-as-directory
12662 (expand-file-name "."))))
12663 (expand-file-name path))
12664 ;; We are linking a file with relative path name.
12665 (setq path (substring (expand-file-name path)
12666 (match-end 0)))))))
12667 (setq link (concat "file:" path))
12668 (if (equal desc origpath)
12669 (setq desc path))))
12671 (setq desc (read-string "Description: " desc))
12672 (unless (string-match "\\S-" desc) (setq desc nil))
12673 (if remove (apply 'delete-region remove))
12674 (insert (org-make-link-string link desc))))
12676 (defun org-completing-read (&rest args)
12677 (let ((minibuffer-local-completion-map
12678 (copy-keymap minibuffer-local-completion-map)))
12679 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
12680 (apply 'completing-read args)))
12682 ;;; Opening/following a link
12683 (defvar org-link-search-failed nil)
12685 (defun org-next-link ()
12686 "Move forward to the next link.
12687 If the link is in hidden text, expose it."
12689 (when (and org-link-search-failed (eq this-command last-command))
12690 (goto-char (point-min))
12691 (message "Link search wrapped back to beginning of buffer"))
12692 (setq org-link-search-failed nil)
12693 (let* ((pos (point))
12695 (a (assoc :link ct)))
12696 (if a (goto-char (nth 2 a)))
12697 (if (re-search-forward org-any-link-re nil t)
12699 (goto-char (match-beginning 0))
12700 (if (org-invisible-p) (org-show-context)))
12702 (setq org-link-search-failed t)
12703 (error "No further link found"))))
12705 (defun org-previous-link ()
12706 "Move backward to the previous link.
12707 If the link is in hidden text, expose it."
12709 (when (and org-link-search-failed (eq this-command last-command))
12710 (goto-char (point-max))
12711 (message "Link search wrapped back to end of buffer"))
12712 (setq org-link-search-failed nil)
12713 (let* ((pos (point))
12715 (a (assoc :link ct)))
12716 (if a (goto-char (nth 1 a)))
12717 (if (re-search-backward org-any-link-re nil t)
12719 (goto-char (match-beginning 0))
12720 (if (org-invisible-p) (org-show-context)))
12722 (setq org-link-search-failed t)
12723 (error "No further link found"))))
12725 (defun org-find-file-at-mouse (ev)
12726 "Open file link or URL at mouse."
12728 (mouse-set-point ev)
12729 (org-open-at-point 'in-emacs))
12731 (defun org-open-at-mouse (ev)
12732 "Open file link or URL at mouse."
12734 (mouse-set-point ev)
12735 (org-open-at-point))
12737 (defvar org-window-config-before-follow-link nil
12738 "The window configuration before following a link.
12739 This is saved in case the need arises to restore it.")
12741 (defvar org-open-link-marker (make-marker)
12742 "Marker pointing to the location where `org-open-at-point; was called.")
12745 (defun org-open-at-point-global ()
12746 "Follow a link like Org-mode does.
12747 This command can be called in any mode to follow a link that has
12750 (org-run-like-in-org-mode 'org-open-at-point))
12752 (defun org-open-at-point (&optional in-emacs)
12753 "Open link at or after point.
12754 If there is no link at point, this function will search forward up to
12755 the end of the current subtree.
12756 Normally, files will be opened by an appropriate application. If the
12757 optional argument IN-EMACS is non-nil, Emacs will visit the file."
12759 (move-marker org-open-link-marker (point))
12760 (setq org-window-config-before-follow-link (current-window-configuration))
12761 (org-remove-occur-highlights nil nil t)
12762 (if (org-at-timestamp-p t)
12763 (org-follow-timestamp-link)
12764 (let (type path link line search (pos (point)))
12767 (skip-chars-forward "^]\n\r")
12768 (when (org-in-regexp org-bracket-link-regexp)
12769 (setq link (org-link-unescape (org-match-string-no-properties 1)))
12770 (while (string-match " *\n *" link)
12771 (setq link (replace-match " " t t link)))
12772 (setq link (org-link-expand-abbrev link))
12773 (if (string-match org-link-re-with-space2 link)
12774 (setq type (match-string 1 link) path (match-string 2 link))
12775 (setq type "thisfile" path link))
12778 (when (get-text-property (point) 'org-linked-text)
12779 (setq type "thisfile"
12780 pos (if (get-text-property (1+ (point)) 'org-linked-text)
12781 (1+ (point)) (point))
12782 path (buffer-substring
12783 (previous-single-property-change pos 'org-linked-text)
12784 (next-single-property-change pos 'org-linked-text)))
12788 (when (or (org-in-regexp org-angle-link-re)
12789 (org-in-regexp org-plain-link-re))
12790 (setq type (match-string 1) path (match-string 2))
12792 (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
12793 (setq type "tree-match"
12794 path (match-string 1))
12797 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
12799 path (match-string 1))
12800 (while (string-match ":" path)
12801 (setq path (replace-match "+" t t path)))
12802 (throw 'match t))))
12804 (error "No link found"))
12805 ;; Remove any trailing spaces in path
12806 (if (string-match " +\\'" path)
12807 (setq path (replace-match "" t t path)))
12811 ((assoc type org-link-protocols)
12812 (funcall (nth 1 (assoc type org-link-protocols)) path))
12814 ((equal type "mailto")
12815 (let ((cmd (car org-link-mailto-program))
12816 (args (cdr org-link-mailto-program)) args1
12817 (address path) (subject "") a)
12818 (if (string-match "\\(.*\\)::\\(.*\\)" path)
12819 (setq address (match-string 1 path)
12820 subject (org-link-escape (match-string 2 path))))
12823 ((not (stringp (car args))) (push (pop args) args1))
12824 (t (setq a (pop args))
12825 (if (string-match "%a" a)
12826 (setq a (replace-match address t t a)))
12827 (if (string-match "%s" a)
12828 (setq a (replace-match subject t t a)))
12830 (apply cmd (nreverse args1))))
12832 ((member type '("http" "https" "ftp" "news"))
12833 (browse-url (concat type ":" (org-link-escape
12834 path org-link-escape-chars-browser))))
12836 ((member type '("message"))
12837 (browse-url (concat type ":" path)))
12839 ((string= type "tags")
12840 (org-tags-view in-emacs path))
12841 ((string= type "thisfile")
12843 (switch-to-buffer-other-window
12844 (org-get-buffer-for-internal-link (current-buffer)))
12845 (org-mark-ring-push))
12846 (let ((cmd `(org-link-search
12848 ,(cond ((equal in-emacs '(4)) 'occur)
12849 ((equal in-emacs '(16)) 'org-occur)
12852 (condition-case nil (eval cmd)
12853 (error (progn (widen) (eval cmd))))))
12855 ((string= type "tree-match")
12856 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
12858 ((string= type "file")
12859 (if (string-match "::\\([0-9]+\\)\\'" path)
12860 (setq line (string-to-number (match-string 1 path))
12861 path (substring path 0 (match-beginning 0)))
12862 (if (string-match "::\\(.+\\)\\'" path)
12863 (setq search (match-string 1 path)
12864 path (substring path 0 (match-beginning 0)))))
12865 (if (string-match "[*?{]" (file-name-nondirectory path))
12867 (org-open-file path in-emacs line search)))
12869 ((string= type "news")
12870 (org-follow-gnus-link path))
12872 ((string= type "bbdb")
12873 (org-follow-bbdb-link path))
12875 ((string= type "info")
12876 (org-follow-info-link path))
12878 ((string= type "gnus")
12879 (let (group article)
12880 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12881 (error "Error in Gnus link"))
12882 (setq group (match-string 1 path)
12883 article (match-string 3 path))
12884 (org-follow-gnus-link group article)))
12886 ((string= type "vm")
12887 (let (folder article)
12888 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12889 (error "Error in VM link"))
12890 (setq folder (match-string 1 path)
12891 article (match-string 3 path))
12892 ;; in-emacs is the prefix arg, will be interpreted as read-only
12893 (org-follow-vm-link folder article in-emacs)))
12895 ((string= type "wl")
12896 (let (folder article)
12897 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12898 (error "Error in Wanderlust link"))
12899 (setq folder (match-string 1 path)
12900 article (match-string 3 path))
12901 (org-follow-wl-link folder article)))
12903 ((string= type "mhe")
12904 (let (folder article)
12905 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12906 (error "Error in MHE link"))
12907 (setq folder (match-string 1 path)
12908 article (match-string 3 path))
12909 (org-follow-mhe-link folder article)))
12911 ((string= type "rmail")
12912 (let (folder article)
12913 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
12914 (error "Error in RMAIL link"))
12915 (setq folder (match-string 1 path)
12916 article (match-string 3 path))
12917 (org-follow-rmail-link folder article)))
12919 ((string= type "shell")
12921 (if (or (not org-confirm-shell-link-function)
12922 (funcall org-confirm-shell-link-function
12923 (format "Execute \"%s\" in shell? "
12924 (org-add-props cmd nil
12925 'face 'org-warning))))
12927 (message "Executing %s" cmd)
12928 (shell-command cmd))
12931 ((string= type "elisp")
12933 (if (or (not org-confirm-elisp-link-function)
12934 (funcall org-confirm-elisp-link-function
12935 (format "Execute \"%s\" as elisp? "
12936 (org-add-props cmd nil
12937 'face 'org-warning))))
12938 (message "%s => %s" cmd (eval (read cmd)))
12942 (browse-url-at-point)))))
12943 (move-marker org-open-link-marker nil)
12944 (run-hook-with-args 'org-follow-link-hook))
12948 (defvar org-create-file-search-functions nil
12949 "List of functions to construct the right search string for a file link.
12950 These functions are called in turn with point at the location to
12951 which the link should point.
12953 A function in the hook should first test if it would like to
12954 handle this file type, for example by checking the major-mode or
12955 the file extension. If it decides not to handle this file, it
12956 should just return nil to give other functions a chance. If it
12957 does handle the file, it must return the search string to be used
12958 when following the link. The search string will be part of the
12959 file link, given after a double colon, and `org-open-at-point'
12960 will automatically search for it. If special measures must be
12961 taken to make the search successful, another function should be
12962 added to the companion hook `org-execute-file-search-functions',
12965 A function in this hook may also use `setq' to set the variable
12966 `description' to provide a suggestion for the descriptive text to
12967 be used for this link when it gets inserted into an Org-mode
12968 buffer with \\[org-insert-link].")
12970 (defvar org-execute-file-search-functions nil
12971 "List of functions to execute a file search triggered by a link.
12973 Functions added to this hook must accept a single argument, the
12974 search string that was part of the file link, the part after the
12975 double colon. The function must first check if it would like to
12976 handle this search, for example by checking the major-mode or the
12977 file extension. If it decides not to handle this search, it
12978 should just return nil to give other functions a chance. If it
12979 does handle the search, it must return a non-nil value to keep
12980 other functions from trying.
12982 Each function can access the current prefix argument through the
12983 variable `current-prefix-argument'. Note that a single prefix is
12984 used to force opening a link in Emacs, so it may be good to only
12985 use a numeric or double prefix to guide the search function.
12987 In case this is needed, a function in this hook can also restore
12988 the window configuration before `org-open-at-point' was called using:
12990 (set-window-configuration org-window-config-before-follow-link)")
12992 (defun org-link-search (s &optional type avoid-pos)
12993 "Search for a link search option.
12994 If S is surrounded by forward slashes, it is interpreted as a
12995 regular expression. In org-mode files, this will create an `org-occur'
12996 sparse tree. In ordinary files, `occur' will be used to list matches.
12997 If the current buffer is in `dired-mode', grep will be used to search
12998 in all files. If AVOID-POS is given, ignore matches near that position."
12999 (let ((case-fold-search t)
13000 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
13001 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
13002 (append '(("") (" ") ("\t") ("\n"))
13003 org-emphasis-alist)
13007 words re0 re1 re2 re3 re4 re5 re2a reall)
13009 ;; First check if there are any special
13010 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
13011 ;; Now try the builtin stuff
13013 (goto-char (point-min))
13016 (concat "<<" (regexp-quote s0) ">>") nil t)
13017 (setq pos (match-beginning 0))))
13018 ;; There is an exact target for this
13020 ((string-match "^/\\(.*\\)/$" s)
13021 ;; A regular expression
13024 (org-occur (match-string 1 s)))
13025 ;;((eq major-mode 'dired-mode)
13026 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
13027 (t (org-do-occur (match-string 1 s)))))
13029 ;; A normal search strings
13030 (when (equal (string-to-char s) ?*)
13031 ;; Anchor on headlines, post may include tags.
13032 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
13033 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
13034 s (substring s 1)))
13035 (remove-text-properties
13037 '(face nil mouse-face nil keymap nil fontified nil) s)
13038 ;; Make a series of regular expressions to find a match
13039 (setq words (org-split-string s "[ \n\r\t]+")
13040 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
13041 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
13043 re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
13044 re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
13045 re1 (concat pre re2 post)
13046 re3 (concat pre re4 post)
13047 re5 (concat pre ".*" re4)
13048 re2 (concat pre re2)
13049 re2a (concat pre re2a)
13050 re4 (concat pre re4)
13051 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
13052 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
13056 ((eq type 'org-occur) (org-occur reall))
13057 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
13058 (t (goto-char (point-min))
13059 (if (or (org-search-not-self 1 re0 nil t)
13060 (org-search-not-self 1 re1 nil t)
13061 (org-search-not-self 1 re2 nil t)
13062 (org-search-not-self 1 re2a nil t)
13063 (org-search-not-self 1 re3 nil t)
13064 (org-search-not-self 1 re4 nil t)
13065 (org-search-not-self 1 re5 nil t)
13067 (goto-char (match-beginning 1))
13069 (error "No match")))))
13071 ;; Normal string-search
13072 (goto-char (point-min))
13073 (if (search-forward s nil t)
13074 (goto-char (match-beginning 0))
13075 (error "No match"))))
13076 (and (org-mode-p) (org-show-context 'link-search))))
13078 (defun org-search-not-self (group &rest args)
13079 "Execute `re-search-forward', but only accept matches that do not
13080 enclose the position of `org-open-link-marker'."
13081 (let ((m org-open-link-marker))
13083 (while (apply 're-search-forward args)
13084 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
13085 (goto-char (match-end group))
13086 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
13087 (> (match-beginning 0) (marker-position m))
13088 (< (match-end 0) (marker-position m)))
13090 (or (not (org-in-regexp
13091 org-bracket-link-analytic-regexp 1))
13092 (not (match-end 4)) ; no description
13093 (and (<= (match-beginning 4) (point))
13094 (>= (match-end 4) (point))))))
13095 (throw 'exit (point))))))))
13097 (defun org-get-buffer-for-internal-link (buffer)
13098 "Return a buffer to be used for displaying the link target of internal links."
13100 ((not org-display-internal-link-with-indirect-buffer)
13102 ((string-match "(Clone)$" (buffer-name buffer))
13103 (message "Buffer is already a clone, not making another one")
13104 ;; we also do not modify visibility in this case
13106 (t ; make a new indirect buffer for displaying the link
13107 (let* ((bn (buffer-name buffer))
13108 (ibn (concat bn "(Clone)"))
13109 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
13110 (with-current-buffer ib (org-overview))
13113 (defun org-do-occur (regexp &optional cleanup)
13114 "Call the Emacs command `occur'.
13115 If CLEANUP is non-nil, remove the printout of the regular expression
13116 in the *Occur* buffer. This is useful if the regex is long and not useful
13120 (let ((cwin (selected-window)) win beg end)
13121 (when (setq win (get-buffer-window "*Occur*"))
13122 (select-window win))
13123 (goto-char (point-min))
13124 (when (re-search-forward "match[a-z]+" nil t)
13125 (setq beg (match-end 0))
13126 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
13127 (setq end (1- (match-beginning 0)))))
13128 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
13129 (goto-char (point-min))
13130 (select-window cwin))))
13132 ;;; The mark ring for links jumps
13134 (defvar org-mark-ring nil
13135 "Mark ring for positions before jumps in Org-mode.")
13136 (defvar org-mark-ring-last-goto nil
13137 "Last position in the mark ring used to go back.")
13138 ;; Fill and close the ring
13139 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
13140 (loop for i from 1 to org-mark-ring-length do
13141 (push (make-marker) org-mark-ring))
13142 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
13145 (defun org-mark-ring-push (&optional pos buffer)
13146 "Put the current position or POS into the mark ring and rotate it."
13148 (setq pos (or pos (point)))
13149 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
13150 (move-marker (car org-mark-ring)
13152 (or buffer (current-buffer)))
13154 (substitute-command-keys
13155 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
13157 (defun org-mark-ring-goto (&optional n)
13158 "Jump to the previous position in the mark ring.
13159 With prefix arg N, jump back that many stored positions. When
13160 called several times in succession, walk through the entire ring.
13161 Org-mode commands jumping to a different position in the current file,
13162 or to another Org-mode file, automatically push the old position
13166 (if (eq last-command this-command)
13167 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
13168 (setq p org-mark-ring))
13169 (setq org-mark-ring-last-goto p)
13171 (switch-to-buffer (marker-buffer m))
13173 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
13175 (defun org-remove-angle-brackets (s)
13176 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
13177 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
13179 (defun org-add-angle-brackets (s)
13180 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
13181 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
13184 ;;; Following specific links
13186 (defun org-follow-timestamp-link ()
13188 ((org-at-date-range-p t)
13189 (let ((org-agenda-start-on-weekday)
13190 (t1 (match-string 1))
13191 (t2 (match-string 2)))
13192 (setq t1 (time-to-days (org-time-string-to-time t1))
13193 t2 (time-to-days (org-time-string-to-time t2)))
13194 (org-agenda-list nil t1 (1+ (- t2 t1)))))
13195 ((org-at-timestamp-p t)
13196 (org-agenda-list nil (time-to-days (org-time-string-to-time
13197 (substring (match-string 1) 0 10)))
13199 (t (error "This should not happen"))))
13202 (defun org-follow-bbdb-link (name)
13203 "Follow a BBDB link to NAME."
13205 (let ((inhibit-redisplay (not debug-on-error))
13206 (bbdb-electric-p nil))
13208 ;; Exact match on name
13209 (bbdb-name (concat "\\`" name "\\'") nil)
13210 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13211 ;; Exact match on name
13212 (bbdb-company (concat "\\`" name "\\'") nil)
13213 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13214 ;; Partial match on name
13215 (bbdb-name name nil)
13216 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13217 ;; Partial match on company
13218 (bbdb-company name nil)
13219 (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
13220 ;; General match including network address and notes
13222 (when (= 0 (buffer-size (get-buffer "*BBDB*")))
13223 (delete-window (get-buffer-window "*BBDB*"))
13224 (error "No matching BBDB record")))))
13226 (defun org-follow-info-link (name)
13227 "Follow an info file & node link to NAME."
13228 (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
13229 (string-match "\\(.*\\)" name))
13232 (if (match-string 2 name) ; If there isn't a node, choose "Top"
13233 (Info-find-node (match-string 1 name) (match-string 2 name))
13234 (Info-find-node (match-string 1 name) "Top")))
13235 (message "Could not open: %s" name)))
13237 (defun org-follow-gnus-link (&optional group article)
13238 "Follow a Gnus link to GROUP and ARTICLE."
13240 (funcall (cdr (assq 'gnus org-link-frame-setup)))
13241 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
13242 (cond ((and group article)
13243 (gnus-group-read-group 1 nil group)
13244 (gnus-summary-goto-article (string-to-number article) nil t))
13245 (group (gnus-group-jump-to-group group))))
13247 (defun org-follow-vm-link (&optional folder article readonly)
13248 "Follow a VM link to FOLDER and ARTICLE."
13250 (setq article (org-add-angle-brackets article))
13251 (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
13252 ;; ange-ftp or efs or tramp access
13253 (let ((user (or (match-string 1 folder) (user-login-name)))
13254 (host (match-string 2 folder))
13255 (file (match-string 3 folder)))
13258 ;; use tramp to access the file
13259 (if (featurep 'xemacs)
13260 (setq folder (format "[%s@%s]%s" user host file))
13261 (setq folder (format "/%s@%s:%s" user host file))))
13263 ;; use ange-ftp or efs
13264 (require (if (featurep 'xemacs) 'efs 'ange-ftp))
13265 (setq folder (format "/%s@%s:%s" user host file))))))
13267 (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
13270 (vm-select-folder-buffer)
13272 (let ((case-fold-search t))
13273 (goto-char (point-min))
13274 (if (not (re-search-forward
13275 (concat "^" "message-id: *" (regexp-quote article))))
13276 (error "Could not find the specified message in this folder"))
13277 (vm-isearch-update)
13278 (vm-isearch-narrow)
13279 (vm-beginning-of-message)
13282 (defun org-follow-wl-link (folder article)
13283 "Follow a Wanderlust link to FOLDER and ARTICLE."
13284 (if (and (string= folder "%")
13286 (string-match "^\\([^#]+\\)\\(#\\(.*\\)\\)?" article))
13287 ;; XXX: imap-uw supports folders starting with '#' such as "#mh/inbox".
13288 ;; Thus, we recompose folder and article ids.
13289 (setq folder (format "%s#%s" folder (match-string 1 article))
13290 article (match-string 3 article)))
13291 (if (not (elmo-folder-exists-p (wl-folder-get-elmo-folder folder)))
13292 (error "No such folder: %s" folder))
13293 (wl-summary-goto-folder-subr folder 'no-sync t nil t nil nil)
13295 (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets article))
13296 (wl-summary-redisplay)))
13298 (defun org-follow-rmail-link (folder article)
13299 "Follow an RMAIL link to FOLDER and ARTICLE."
13300 (setq article (org-add-angle-brackets article))
13301 (let (message-number)
13303 (save-window-excursion
13304 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
13305 (setq message-number
13308 (goto-char (point-max))
13309 (if (re-search-backward
13310 (concat "^Message-ID:\\s-+" (regexp-quote
13313 (rmail-what-message))))))
13316 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
13317 (rmail-show-message message-number)
13319 (error "Message not found"))))
13321 ;;; mh-e integration based on planner-mode
13322 (defun org-mhe-get-message-real-folder ()
13323 "Return the name of the current message real folder, so if you use
13324 sequences, it will now work."
13327 (if (equal major-mode 'mh-folder-mode)
13329 ;; Refer to the show buffer
13330 mh-show-folder-buffer))
13332 (if (boundp 'mh-index-folder)
13333 (min (length mh-index-folder) (length folder))))
13335 ;; a simple test on mh-index-data does not work, because
13336 ;; mh-index-data is always nil in a show buffer.
13337 (if (and (boundp 'mh-index-folder)
13338 (string= mh-index-folder (substring folder 0 end-index)))
13339 (if (equal major-mode 'mh-show-mode)
13340 (save-window-excursion
13341 (let (pop-up-frames)
13342 (when (buffer-live-p (get-buffer folder))
13344 (pop-to-buffer folder)
13345 (org-mhe-get-message-folder-from-index)
13348 (org-mhe-get-message-folder-from-index)
13354 (defun org-mhe-get-message-folder-from-index ()
13355 "Returns the name of the message folder in a index folder buffer."
13357 (mh-index-previous-folder)
13358 (re-search-forward "^\\(+.*\\)$" nil t)
13359 (message "%s" (match-string 1))))
13361 (defun org-mhe-get-message-folder ()
13362 "Return the name of the current message folder. Be careful if you
13365 (if (equal major-mode 'mh-folder-mode)
13367 ;; Refer to the show buffer
13368 mh-show-folder-buffer)))
13370 (defun org-mhe-get-message-num ()
13371 "Return the number of the current message. Be careful if you
13374 (if (equal major-mode 'mh-folder-mode)
13375 (mh-get-msg-num nil)
13376 ;; Refer to the show buffer
13377 (mh-show-buffer-message-number))))
13379 (defun org-mhe-get-header (header)
13380 "Return a header of the message in folder mode. This will create a
13381 show buffer for the corresponding message. If you have a more clever
13383 (let* ((folder (org-mhe-get-message-folder))
13384 (num (org-mhe-get-message-num))
13385 (buffer (get-buffer-create (concat "show-" folder)))
13387 (with-current-buffer buffer
13388 (mh-display-msg num folder)
13389 (if (equal major-mode 'mh-folder-mode)
13390 (mh-header-display)
13391 (mh-show-header-display))
13392 (set-buffer buffer)
13393 (setq header-field (mh-get-header-field header))
13394 (if (equal major-mode 'mh-folder-mode)
13399 (defun org-follow-mhe-link (folder article)
13400 "Follow an MHE link to FOLDER and ARTICLE.
13401 If ARTICLE is nil FOLDER is shown. If the configuration variable
13402 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
13403 ARTICLE is searched in all folders. Indexed searches (swish++,
13404 namazu, and others supported by MH-E) will always search in all
13407 (require 'mh-search)
13408 (require 'mh-utils)
13411 (mh-visit-folder (mh-normalize-folder-name folder))
13412 (setq article (org-add-angle-brackets article))
13414 (if (equal mh-searcher 'pick)
13416 (mh-search folder (list "--message-id" article))
13417 (when (and org-mhe-search-all-folders
13418 (not (org-mhe-get-message-real-folder)))
13420 (mh-search "+" (list "--message-id" article))))
13421 (mh-search "+" article))
13422 (if (org-mhe-get-message-real-folder)
13425 (error "Message not found"))))
13429 ;; Use the custom search meachnism to construct and use search strings for
13430 ;; file links to BibTeX database entries.
13432 (defun org-create-file-search-in-bibtex ()
13433 "Create the search string and description for a BibTeX database entry."
13434 (when (eq major-mode 'bibtex-mode)
13435 ;; yes, we want to construct this search string.
13436 ;; Make a good description for this entry, using names, year and the title
13437 ;; Put it into the `description' variable which is dynamically scoped.
13438 (let ((bibtex-autokey-names 1)
13439 (bibtex-autokey-names-stretch 1)
13440 (bibtex-autokey-name-case-convert-function 'identity)
13441 (bibtex-autokey-name-separator " & ")
13442 (bibtex-autokey-additional-names " et al.")
13443 (bibtex-autokey-year-length 4)
13444 (bibtex-autokey-name-year-separator " ")
13445 (bibtex-autokey-titlewords 3)
13446 (bibtex-autokey-titleword-separator " ")
13447 (bibtex-autokey-titleword-case-convert-function 'identity)
13448 (bibtex-autokey-titleword-length 'infty)
13449 (bibtex-autokey-year-title-separator ": "))
13450 (setq description (bibtex-generate-autokey)))
13451 ;; Now parse the entry, get the key and return it.
13453 (bibtex-beginning-of-entry)
13454 (cdr (assoc "=key=" (bibtex-parse-entry))))))
13456 (defun org-execute-file-search-in-bibtex (s)
13457 "Find the link search string S as a key for a database entry."
13458 (when (eq major-mode 'bibtex-mode)
13459 ;; Yes, we want to do the search in this file.
13460 ;; We construct a regexp that searches for "@entrytype{" followed by the key
13461 (goto-char (point-min))
13462 (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
13463 (regexp-quote s) "[ \t\n]*,") nil t)
13464 (goto-char (match-beginning 0)))
13465 (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
13466 ;; Use double prefix to indicate that any web link should be browsed
13467 (let ((b (current-buffer)) (p (point)))
13468 ;; Restore the window configuration because we just use the web link
13469 (set-window-configuration org-window-config-before-follow-link)
13470 (save-excursion (set-buffer b) (goto-char p)
13472 (recenter 0)) ; Move entry start to beginning of window
13473 ;; return t to indicate that the search is done.
13476 ;; Finally add the functions to the right hooks.
13477 (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
13478 (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
13480 ;; end of Bibtex link setup
13482 ;;; Following file links
13484 (defun org-open-file (path &optional in-emacs line search)
13485 "Open the file at PATH.
13486 First, this expands any special file name abbreviations. Then the
13487 configuration variable `org-file-apps' is checked if it contains an
13488 entry for this file type, and if yes, the corresponding command is launched.
13489 If no application is found, Emacs simply visits the file.
13490 With optional argument IN-EMACS, Emacs will visit the file.
13491 Optional LINE specifies a line to go to, optional SEARCH a string to
13492 search for. If LINE or SEARCH is given, the file will always be
13494 If the file does not exist, an error is thrown."
13495 (setq in-emacs (or in-emacs line search))
13496 (let* ((file (if (equal path "")
13498 (substitute-in-file-name (expand-file-name path))))
13499 (apps (append org-file-apps (org-default-apps)))
13500 (remp (and (assq 'remote apps) (org-file-remote-p file)))
13501 (dirp (if remp nil (file-directory-p file)))
13502 (dfile (downcase file))
13503 (old-buffer (current-buffer))
13505 (old-mode major-mode)
13507 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
13508 (setq ext (match-string 1 dfile))
13509 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
13510 (setq ext (match-string 1 dfile))))
13513 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
13514 (and dirp (cdr (assoc 'directory apps)))
13515 (cdr (assoc ext apps))
13516 (cdr (assoc t apps)))))
13517 (when (eq cmd 'mailcap)
13519 (mailcap-parse-mailcaps)
13520 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
13521 (command (mailcap-mime-info mime-type)))
13522 (if (stringp command)
13524 (setq cmd 'emacs))))
13525 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
13526 (not (file-exists-p file))
13527 (not org-open-non-existing-files))
13528 (error "No such file: %s" file))
13530 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
13531 ;; Remove quotes around the file name - we'll use shell-quote-argument.
13532 (while (string-match "['\"]%s['\"]" cmd)
13533 (setq cmd (replace-match "%s" t t cmd)))
13534 (while (string-match "%s" cmd)
13535 (setq cmd (replace-match
13536 (save-match-data (shell-quote-argument file))
13538 (save-window-excursion
13539 (start-process-shell-command cmd nil cmd)))
13542 (funcall (cdr (assq 'file org-link-frame-setup)) file)
13544 (if line (goto-line line)
13545 (if search (org-link-search search))))
13548 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
13549 (and (org-mode-p) (eq old-mode 'org-mode)
13550 (or (not (equal old-buffer (current-buffer)))
13551 (not (equal old-pos (point))))
13552 (org-mark-ring-push old-pos old-buffer))))
13554 (defun org-default-apps ()
13555 "Return the default applications for this operating system."
13557 ((eq system-type 'darwin)
13558 org-file-apps-defaults-macosx)
13559 ((eq system-type 'windows-nt)
13560 org-file-apps-defaults-windowsnt)
13561 (t org-file-apps-defaults-gnu)))
13563 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
13564 (defun org-file-remote-p (file)
13565 "Test whether FILE specifies a location on a remote system.
13566 Return non-nil if the location is indeed remote.
13568 For example, the filename \"/user@host:/foo\" specifies a location
13569 on the system \"/user@host:\"."
13570 (cond ((fboundp 'file-remote-p)
13571 (file-remote-p file))
13572 ((fboundp 'tramp-handle-file-remote-p)
13573 (tramp-handle-file-remote-p file))
13574 ((and (boundp 'ange-ftp-name-format)
13575 (string-match (car ange-ftp-name-format) file))
13580 ;;;; Hooks for remember.el, and refiling
13582 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
13583 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
13586 (defun org-remember-insinuate ()
13587 "Setup remember.el for use wiht Org-mode."
13588 (require 'remember)
13589 (setq remember-annotation-functions '(org-remember-annotation))
13590 (setq remember-handler-functions '(org-remember-handler))
13591 (add-hook 'remember-mode-hook 'org-remember-apply-template))
13594 (defun org-remember-annotation ()
13595 "Return a link to the current location as an annotation for remember.el.
13596 If you are using Org-mode files as target for data storage with
13597 remember.el, then the annotations should include a link compatible with the
13598 conventions in Org-mode. This function returns such a link."
13599 (org-store-link nil))
13601 (defconst org-remember-help
13602 "Select a destination location for the note.
13603 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
13604 RET on headline -> Store as sublevel entry to current headline
13605 RET at beg-of-buf -> Append to file as level 2 headline
13606 <left>/<right> -> before/after current headline, same headings level")
13608 (defvar org-remember-previous-location nil)
13609 (defvar org-force-remember-template-char) ;; dynamically scoped
13611 ;; Save the major mode of the buffer we called remember from
13612 (defvar org-select-template-temp-major-mode nil)
13614 ;; Temporary store the buffer where remember was called from
13615 (defvar org-select-template-original-buffer nil)
13617 (defun org-select-remember-template (&optional use-char)
13618 (when org-remember-templates
13619 (let* ((pre-selected-templates
13622 (let ((ctxt (nth 5 tpl))
13623 (mode org-select-template-temp-major-mode)
13624 (buf org-select-template-original-buffer))
13625 (if (or (and (functionp ctxt)
13628 ;; Protect the user-defined function from error
13629 (condition-case nil (funcall ctxt) (error nil))))
13630 (and ctxt (listp ctxt)
13631 (delq nil (mapcar (lambda(x) (eq mode x)) ctxt))))
13633 org-remember-templates))
13634 ;; If no template at this point, add the default templates:
13635 (pre-selected-templates1
13636 (if (not (delq nil pre-selected-templates))
13637 (mapcar (lambda(x) (if (not (nth 5 x)) x))
13638 org-remember-templates)
13639 pre-selected-templates))
13640 ;; Then unconditionnally add template for any contexts
13641 (pre-selected-templates2
13642 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
13643 org-remember-templates)
13644 (delq nil pre-selected-templates1)))
13645 (templates (mapcar (lambda (x)
13646 (if (stringp (car x))
13647 (append (list (nth 1 x) (car x)) (cddr x))
13648 (append (list (car x) "") (cdr x))))
13649 (delq nil pre-selected-templates2)))
13652 ((= (length templates) 1)
13654 ((and (boundp 'org-force-remember-template-char)
13655 org-force-remember-template-char)
13656 (if (stringp org-force-remember-template-char)
13657 (string-to-char org-force-remember-template-char)
13658 org-force-remember-template-char))
13660 (message "Select template: %s"
13664 ((not (string-match "\\S-" (nth 1 x)))
13665 (format "[%c]" (car x)))
13666 ((equal (downcase (car x))
13667 (downcase (aref (nth 1 x) 0)))
13668 (format "[%c]%s" (car x)
13669 (substring (nth 1 x) 1)))
13670 (t (format "[%c]%s" (car x) (nth 1 x)))))
13672 (let ((inhibit-quit t) (char0 (read-char-exclusive)))
13673 (when (equal char0 ?\C-g)
13674 (jump-to-register remember-register)
13675 (kill-buffer remember-buffer))
13677 (cddr (assoc char templates)))))
13679 (defvar x-last-selected-text)
13680 (defvar x-last-selected-text-primary)
13683 (defun org-remember-apply-template (&optional use-char skip-interactive)
13684 "Initialize *remember* buffer with template, invoke `org-mode'.
13685 This function should be placed into `remember-mode-hook' and in fact requires
13686 to be run from that hook to function properly."
13687 (if org-remember-templates
13688 (let* ((entry (org-select-remember-template use-char))
13690 (plist-p (if org-store-link-plist t nil))
13691 (file (if (and (nth 1 entry) (stringp (nth 1 entry))
13692 (string-match "\\S-" (nth 1 entry)))
13694 org-default-notes-file))
13695 (headline (nth 2 entry))
13696 (v-c (or (and (eq window-system 'x)
13697 (fboundp 'x-cut-buffer-or-selection-value)
13698 (x-cut-buffer-or-selection-value))
13699 (org-bound-and-true-p x-last-selected-text)
13700 (org-bound-and-true-p x-last-selected-text-primary)
13701 (and (> (length kill-ring) 0) (current-kill 0))))
13702 (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
13703 (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
13704 (v-u (concat "[" (substring v-t 1 -1) "]"))
13705 (v-U (concat "[" (substring v-T 1 -1) "]"))
13706 ;; `initial' and `annotation' are bound in `remember'
13707 (v-i (if (boundp 'initial) initial))
13708 (v-a (if (and (boundp 'annotation) annotation)
13709 (if (equal annotation "[[]]") "" annotation)
13712 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
13713 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
13715 (v-n user-full-name)
13716 (org-startup-folded nil)
13717 org-time-was-given org-end-time-was-given x
13718 prompt completions char time pos default histvar)
13719 (setq org-store-link-plist
13720 (append (list :annotation v-a :initial v-i)
13721 org-store-link-plist))
13722 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
13724 (insert (substitute-command-keys
13726 "## Filing location: Select interactively, default, or last used:
13727 ## %s to select file and header location interactively.
13728 ## %s \"%s\" -> \"* %s\"
13729 ## C-u C-u C-c C-c \"%s\" -> \"* %s\"
13730 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
13731 (if org-remember-store-without-prompt " C-u C-c C-c" " C-c C-c")
13732 (if org-remember-store-without-prompt " C-c C-c" " C-u C-c C-c")
13733 (abbreviate-file-name (or file org-default-notes-file))
13735 (or (car org-remember-previous-location) "???")
13736 (or (cdr org-remember-previous-location) "???"))))
13737 (insert tpl) (goto-char (point-min))
13738 ;; Simple %-escapes
13739 (while (re-search-forward "%\\([tTuUaiAc]\\)" nil t)
13740 (when (and initial (equal (match-string 0) "%i"))
13742 (let* ((lead (buffer-substring
13743 (point-at-bol) (match-beginning 0))))
13744 (setq v-i (mapconcat 'identity
13745 (org-split-string initial "\n")
13746 (concat "\n" lead))))))
13748 (or (eval (intern (concat "v-" (match-string 1)))) "")
13751 ;; %[] Insert contents of a file.
13752 (goto-char (point-min))
13753 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
13754 (let ((start (match-beginning 0))
13755 (end (match-end 0))
13756 (filename (expand-file-name (match-string 1))))
13758 (delete-region start end)
13759 (condition-case error
13760 (insert-file-contents filename)
13761 (error (insert (format "%%![Couldn't insert %s: %s]"
13762 filename error))))))
13763 ;; %() embedded elisp
13764 (goto-char (point-min))
13765 (while (re-search-forward "%\\((.+)\\)" nil t)
13766 (goto-char (match-beginning 0))
13767 (let ((template-start (point)))
13770 (condition-case error
13771 (eval (read (current-buffer)))
13772 (error (format "%%![Error: %s]" error)))))
13773 (delete-region template-start (point))
13776 ;; From the property list
13778 (goto-char (point-min))
13779 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
13780 (and (setq x (or (plist-get org-store-link-plist
13781 (intern (match-string 1))) ""))
13782 (replace-match x t t))))
13784 ;; Turn on org-mode in the remember buffer, set local variables
13786 (org-set-local 'org-finish-function 'org-remember-finalize)
13787 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
13788 (org-set-local 'org-default-notes-file file))
13789 (if (and headline (stringp headline) (string-match "\\S-" headline))
13790 (org-set-local 'org-remember-default-headline headline))
13791 ;; Interactive template entries
13792 (goto-char (point-min))
13793 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGuUtT]\\)?" nil t)
13794 (setq char (if (match-end 3) (match-string 3))
13795 prompt (if (match-end 2) (match-string 2)))
13796 (goto-char (match-beginning 0))
13798 (setq completions nil default nil)
13800 (setq completions (org-split-string prompt "|")
13801 prompt (pop completions)
13802 default (car completions)
13803 histvar (intern (concat
13804 "org-remember-template-prompt-history::"
13806 completions (mapcar 'list completions)))
13808 ((member char '("G" "g"))
13809 (let* ((org-last-tags-completion-table
13810 (org-global-tags-completion-table
13811 (if (equal char "G") (org-agenda-files) (and file (list file)))))
13812 (org-add-colon-after-tag-completion t)
13813 (ins (completing-read
13814 (if prompt (concat prompt ": ") "Tags: ")
13815 'org-tags-completion-function nil nil nil
13816 'org-tags-history)))
13817 (setq ins (mapconcat 'identity
13818 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
13820 (when (string-match "\\S-" ins)
13821 (or (equal (char-before) ?:) (insert ":"))
13823 (or (equal (char-after) ?:) (insert ":")))))
13825 (setq org-time-was-given (equal (upcase char) char))
13826 (setq time (org-read-date (equal (upcase char) "U") t nil
13828 (org-insert-time-stamp time org-time-was-given
13829 (member char '("u" "U"))
13830 nil nil (list org-end-time-was-given)))
13832 (insert (org-completing-read
13833 (concat (if prompt prompt "Enter string")
13834 (if default (concat " [" default "]"))
13836 completions nil nil nil histvar default)))))
13837 (goto-char (point-min))
13838 (if (re-search-forward "%\\?" nil t)
13840 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
13842 (org-set-local 'org-finish-function 'org-remember-finalize))
13843 (when (save-excursion
13844 (goto-char (point-min))
13845 (re-search-forward "%!" nil t))
13847 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
13849 (defun org-remember-finish-immediately ()
13850 "File remember note immediately.
13851 This should be run in `post-command-hook' and will remove itself
13853 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
13854 (when org-finish-function
13855 (funcall org-finish-function)))
13857 (defvar org-clock-marker) ; Defined below
13858 (defun org-remember-finalize ()
13859 "Finalize the remember process."
13860 (unless (fboundp 'remember-finalize)
13861 (defalias 'remember-finalize 'remember-buffer))
13862 (when (and org-clock-marker
13863 (equal (marker-buffer org-clock-marker) (current-buffer)))
13864 ;; FIXME: test this, this is w/o notetaking!
13865 (let (org-log-note-clock-out) (org-clock-out)))
13866 (when buffer-file-name
13868 (setq buffer-file-name nil))
13869 (remember-finalize))
13872 (defun org-remember (&optional goto org-force-remember-template-char)
13873 "Call `remember'. If this is already a remember buffer, re-apply template.
13874 If there is an active region, make sure remember uses it as initial content
13875 of the remember buffer.
13877 When called interactively with a `C-u' prefix argument GOTO, don't remember
13878 anything, just go to the file/headline where the selected template usually
13879 stores its notes. With a double prefix arg `C-u C-u', go to the last
13880 note stored by remember.
13882 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
13883 associated with a template in `org-remember-templates'."
13886 ((equal goto '(4)) (org-go-to-remember-target))
13887 ((equal goto '(16)) (org-remember-goto-last-stored))
13889 ;; set temporary variables that will be needed in
13890 ;; `org-select-remember-template'
13891 (setq org-select-template-temp-major-mode major-mode)
13892 (setq org-select-template-original-buffer (current-buffer))
13893 (if (memq org-finish-function '(remember-buffer remember-finalize))
13895 (when (< (length org-remember-templates) 2)
13896 (error "No other template available"))
13898 (let ((annotation (plist-get org-store-link-plist :annotation))
13899 (initial (plist-get org-store-link-plist :initial)))
13900 (org-remember-apply-template))
13901 (message "Press C-c C-c to remember data"))
13902 (if (org-region-active-p)
13903 (remember (buffer-substring (point) (mark)))
13904 (call-interactively 'remember))))))
13906 (defun org-remember-goto-last-stored ()
13907 "Go to the location where the last remember note was stored."
13909 (bookmark-jump "org-remember-last-stored")
13910 (message "This is the last note stored by remember"))
13912 (defun org-go-to-remember-target (&optional template-key)
13913 "Go to the target location of a remember template.
13914 The user is queried for the template."
13916 (let* (org-select-template-temp-major-mode
13917 (entry (org-select-remember-template template-key))
13918 (file (nth 1 entry))
13919 (heading (nth 2 entry))
13921 (unless (and file (stringp file) (string-match "\\S-" file))
13922 (setq file org-default-notes-file))
13923 (unless (and heading (stringp heading) (string-match "\\S-" heading))
13924 (setq heading org-remember-default-headline))
13925 (setq visiting (org-find-base-buffer-visiting file))
13926 (if (not visiting) (find-file-noselect file))
13927 (switch-to-buffer (or visiting (get-file-buffer file)))
13929 (goto-char (point-min))
13930 (if (re-search-forward
13931 (concat "^\\*+[ \t]+" (regexp-quote heading)
13932 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
13934 (goto-char (match-beginning 0))
13935 (error "Target headline not found: %s" heading))))
13937 (defvar org-note-abort nil) ; dynamically scoped
13940 (defun org-remember-handler ()
13941 "Store stuff from remember.el into an org file.
13942 First prompts for an org file. If the user just presses return, the value
13943 of `org-default-notes-file' is used.
13944 Then the command offers the headings tree of the selected file in order to
13945 file the text at a specific location.
13946 You can either immediately press RET to get the note appended to the
13947 file, or you can use vertical cursor motion and visibility cycling (TAB) to
13948 find a better place. Then press RET or <left> or <right> in insert the note.
13950 Key Cursor position Note gets inserted
13951 -----------------------------------------------------------------------------
13952 RET buffer-start as level 1 heading at end of file
13953 RET on headline as sublevel of the heading at cursor
13954 RET no heading at cursor position, level taken from context.
13955 Or use prefix arg to specify level manually.
13956 <left> on headline as same level, before current heading
13957 <right> on headline as same level, after current heading
13959 So the fastest way to store the note is to press RET RET to append it to
13960 the default file. This way your current train of thought is not
13961 interrupted, in accordance with the principles of remember.el.
13962 You can also get the fast execution without prompting by using
13963 C-u C-c C-c to exit the remember buffer. See also the variable
13964 `org-remember-store-without-prompt'.
13966 Before being stored away, the function ensures that the text has a
13967 headline, i.e. a first line that starts with a \"*\". If not, a headline
13968 is constructed from the current date and some additional data.
13970 If the variable `org-adapt-indentation' is non-nil, the entire text is
13971 also indented so that it starts in the same column as the headline
13972 \(i.e. after the stars).
13974 See also the variable `org-reverse-note-order'."
13975 (goto-char (point-min))
13976 (while (looking-at "^[ \t]*\n\\|^##.*\n")
13977 (replace-match ""))
13978 (goto-char (point-max))
13979 (beginning-of-line 1)
13980 (while (looking-at "[ \t]*$\\|##.*")
13981 (delete-region (1- (point)) (point-max))
13982 (beginning-of-line 1))
13984 (if org-note-abort (throw 'quit nil))
13985 (let* ((txt (buffer-substring (point-min) (point-max)))
13986 (fastp (org-xor (equal current-prefix-arg '(4))
13987 org-remember-store-without-prompt))
13989 (fastp org-default-notes-file)
13990 ((and (eq org-remember-interactive-interface 'refile)
13991 org-refile-targets)
13992 org-default-notes-file)
13993 ((not (and (equal current-prefix-arg '(16))
13994 org-remember-previous-location))
13995 (org-get-org-file))))
13996 (heading org-remember-default-headline)
13997 (visiting (and file (org-find-base-buffer-visiting file)))
13998 (org-startup-folded nil)
13999 (org-startup-align-all-tables nil)
14000 (org-goto-start-pos 1)
14001 spos exitcmd level indent reversed)
14002 (if (and (equal current-prefix-arg '(16)) org-remember-previous-location)
14003 (setq file (car org-remember-previous-location)
14004 heading (cdr org-remember-previous-location)
14006 (setq current-prefix-arg nil)
14007 (if (string-match "[ \t\n]+\\'" txt)
14008 (setq txt (replace-match "" t t txt)))
14009 ;; Modify text so that it becomes a nice subtree which can be inserted
14010 ;; into an org tree.
14011 (let* ((lines (split-string txt "\n"))
14013 (setq first (car lines) lines (cdr lines))
14014 (if (string-match "^\\*+ " first)
14015 ;; Is already a headline
14017 ;; We need to add a headline: Use time and first buffer line
14018 (setq lines (cons first lines)
14019 first (concat "* " (current-time-string)
14020 " (" (remember-buffer-desc) ")")
14022 (if (and org-adapt-indentation indent)
14023 (setq lines (mapcar
14025 (if (string-match "\\S-" x)
14026 (concat indent x) x))
14028 (setq txt (concat first "\n"
14029 (mapconcat 'identity lines "\n"))))
14030 (if (string-match "\n[ \t]*\n[ \t\n]*\\'" txt)
14031 (setq txt (replace-match "\n\n" t t txt))
14032 (if (string-match "[ \t\n]*\\'" txt)
14033 (setq txt (replace-match "\n" t t txt))))
14034 ;; Put the modified text back into the remember buffer, for refile.
14037 (goto-char (point-min))
14038 (when (and (eq org-remember-interactive-interface 'refile)
14040 (org-refile nil (or visiting (find-file-noselect file)))
14043 (if (not visiting) (find-file-noselect file))
14044 (with-current-buffer (or visiting (get-file-buffer file))
14045 (unless (org-mode-p)
14046 (error "Target files for remember notes must be in Org-mode"))
14050 (and (goto-char (point-min))
14051 (not (re-search-forward "^\\* " nil t))
14052 (insert "\n* " (or heading "Notes") "\n"))
14053 (setq reversed (org-notes-order-reversed-p))
14055 ;; Find the default location
14056 (when (and heading (stringp heading) (string-match "\\S-" heading))
14057 (goto-char (point-min))
14058 (if (re-search-forward
14059 (concat "^\\*+[ \t]+" (regexp-quote heading)
14060 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
14062 (setq org-goto-start-pos (match-beginning 0))
14064 (goto-char (point-max))
14065 (unless (bolp) (newline))
14066 (insert "* " heading "\n")
14067 (setq org-goto-start-pos (point-at-bol 0)))))
14069 ;; Ask the User for a location, using the appropriate interface
14071 (fastp (setq spos org-goto-start-pos
14073 ((eq org-remember-interactive-interface 'outline)
14074 (setq spos (org-get-location (current-buffer)
14078 ((eq org-remember-interactive-interface 'outline-path-completion)
14079 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
14080 (org-refile-use-outline-path t))
14081 (setq spos (org-refile-get-location "Heading: ")
14083 spos (nth 3 spos))))
14084 (t (error "this should not hapen")))
14085 (if (not spos) (throw 'quit nil)) ; return nil to show we did
14086 ; not handle this note
14088 (cond ((org-on-heading-p t)
14089 (org-back-to-heading t)
14090 (setq level (funcall outline-level))
14092 ((eq exitcmd 'return)
14093 ;; sublevel of current
14094 (setq org-remember-previous-location
14095 (cons (abbreviate-file-name file)
14096 (org-get-heading 'notags)))
14098 (outline-next-heading)
14099 (org-end-of-subtree t)
14101 (if (looking-at "[ \t]*\n")
14102 (beginning-of-line 2)
14105 (bookmark-set "org-remember-last-stored")
14106 (org-paste-subtree (org-get-legal-level level 1) txt))
14107 ((eq exitcmd 'left)
14109 (bookmark-set "org-remember-last-stored")
14110 (org-paste-subtree level txt))
14111 ((eq exitcmd 'right)
14113 (org-end-of-subtree t)
14114 (bookmark-set "org-remember-last-stored")
14115 (org-paste-subtree level txt))
14116 (t (error "This should not happen"))))
14118 ((and (bobp) (not reversed))
14119 ;; Put it at the end, one level below level 1
14122 (goto-char (point-max))
14123 (if (not (bolp)) (newline))
14124 (bookmark-set "org-remember-last-stored")
14125 (org-paste-subtree (org-get-legal-level 1 1) txt)))
14127 ((and (bobp) reversed)
14128 ;; Put it at the start, as level 1
14131 (goto-char (point-min))
14132 (re-search-forward "^\\*+ " nil t)
14133 (beginning-of-line 1)
14134 (bookmark-set "org-remember-last-stored")
14135 (org-paste-subtree 1 txt)))
14137 ;; Put it right there, with automatic level determined by
14138 ;; org-paste-subtree or from prefix arg
14139 (bookmark-set "org-remember-last-stored")
14141 (if (numberp current-prefix-arg) current-prefix-arg)
14143 (when remember-save-after-remembering
14145 (if (not visiting) (kill-buffer (current-buffer)))))))))
14147 t) ;; return t to indicate that we took care of this note.
14149 (defun org-get-org-file ()
14150 "Read a filename, with default directory `org-directory'."
14151 (let ((default (or org-default-notes-file remember-data-file)))
14152 (read-file-name (format "File name [%s]: " default)
14153 (file-name-as-directory org-directory)
14156 (defun org-notes-order-reversed-p ()
14157 "Check if the current file should receive notes in reversed order."
14159 ((not org-reverse-note-order) nil)
14160 ((eq t org-reverse-note-order) t)
14161 ((not (listp org-reverse-note-order)) nil)
14163 (let ((all org-reverse-note-order)
14165 (while (setq entry (pop all))
14166 (if (string-match (car entry) buffer-file-name)
14167 (throw 'exit (cdr entry))))
14172 (defvar org-refile-target-table nil
14173 "The list of refile targets, created by `org-refile'.")
14175 (defvar org-agenda-new-buffers nil
14176 "Buffers created to visit agenda files.")
14178 (defun org-get-refile-targets (&optional default-buffer)
14179 "Produce a table with refile targets."
14180 (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
14181 targets txt re files f desc descre)
14182 (with-current-buffer (or default-buffer (current-buffer))
14183 (while (setq entry (pop entries))
14184 (setq files (car entry) desc (cdr entry))
14186 ((null files) (setq files (list (current-buffer))))
14187 ((eq files 'org-agenda-files)
14188 (setq files (org-agenda-files 'unrestricted)))
14189 ((and (symbolp files) (fboundp files))
14190 (setq files (funcall files)))
14191 ((and (symbolp files) (boundp files))
14192 (setq files (symbol-value files))))
14193 (if (stringp files) (setq files (list files)))
14195 ((eq (car desc) :tag)
14196 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
14197 ((eq (car desc) :todo)
14198 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
14199 ((eq (car desc) :regexp)
14200 (setq descre (cdr desc)))
14201 ((eq (car desc) :level)
14202 (setq descre (concat "^\\*\\{" (number-to-string
14203 (if org-odd-levels-only
14204 (1- (* 2 (cdr desc)))
14207 ((eq (car desc) :maxlevel)
14208 (setq descre (concat "^\\*\\{1," (number-to-string
14209 (if org-odd-levels-only
14210 (1- (* 2 (cdr desc)))
14213 (t (error "Bad refiling target description %s" desc)))
14214 (while (setq f (pop files))
14216 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
14217 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
14221 (goto-char (point-min))
14222 (while (re-search-forward descre nil t)
14223 (goto-char (point-at-bol))
14224 (when (looking-at org-complex-heading-regexp)
14225 (setq txt (match-string 4)
14226 re (concat "^" (regexp-quote
14227 (buffer-substring (match-beginning 1)
14229 (if (match-end 5) (setq re (concat re "[ \t]+"
14231 (match-string 5)))))
14232 (setq re (concat re "[ \t]*$"))
14233 (when org-refile-use-outline-path
14234 (setq txt (mapconcat 'identity
14236 (if (eq org-refile-use-outline-path 'file)
14237 (list (file-name-nondirectory
14238 (buffer-file-name (buffer-base-buffer))))
14239 (if (eq org-refile-use-outline-path 'full-file-path)
14240 (list (buffer-file-name (buffer-base-buffer)))))
14241 (org-get-outline-path)
14244 (push (list txt f re (point)) targets))
14245 (goto-char (point-at-eol))))))))
14246 (nreverse targets))))
14248 (defun org-get-outline-path ()
14249 "Return the outline path to the current entry, as a list."
14252 (while (org-up-heading-safe)
14253 (when (looking-at org-complex-heading-regexp)
14254 (push (org-match-string-no-properties 4) rtn)))
14257 (defvar org-refile-history nil
14258 "History for refiling operations.")
14260 (defun org-refile (&optional goto default-buffer)
14261 "Move the entry at point to another heading.
14262 The list of target headings is compiled using the information in
14263 `org-refile-targets', which see. This list is created upon first use, and
14264 you can update it by calling this command with a double prefix (`C-u C-u').
14265 FIXME: Can we find a better way of updating?
14267 At the target location, the entry is filed as a subitem of the target heading.
14268 Depending on `org-reverse-note-order', the new subitem will either be the
14269 first of the last subitem.
14271 With prefix arg GOTO, the command will only visit the target location,
14272 not actually move anything.
14273 With a double prefix `C-c C-c', go to the location where the last refiling
14274 operation has put the subtree.
14276 With a double prefix argument, the command can be used to jump to any
14277 heading in the current buffer."
14279 (let* ((cbuf (current-buffer))
14280 (filename (buffer-file-name (buffer-base-buffer cbuf)))
14281 pos it nbuf file re level reversed)
14282 (if (equal goto '(16))
14283 (org-refile-goto-last-stored)
14284 (when (setq it (org-refile-get-location
14285 (if goto "Goto: " "Refile to: ") default-buffer))
14286 (setq file (nth 1 it)
14289 (setq nbuf (or (find-buffer-visiting file)
14290 (find-file-noselect file)))
14293 (switch-to-buffer nbuf)
14295 (org-show-context 'org-goto))
14298 (set-buffer (setq nbuf (or (find-buffer-visiting file)
14299 (find-file-noselect file))))
14300 (setq reversed (org-notes-order-reversed-p))
14305 (looking-at outline-regexp)
14306 (setq level (org-get-legal-level (funcall outline-level) 1))
14309 (outline-next-heading)
14310 (or (save-excursion (outline-get-next-sibling))
14311 (org-end-of-subtree t t)
14313 (bookmark-set "org-refile-last-stored")
14314 (org-paste-subtree level))))
14316 (message "Entry refiled to \"%s\"" (car it)))))))
14318 (defun org-refile-goto-last-stored ()
14319 "Go to the location where the last refile was stored."
14321 (bookmark-jump "org-refile-last-stored")
14322 (message "This is the location of the last refile"))
14324 (defun org-refile-get-location (&optional prompt default-buffer)
14325 "Prompt the user for a refile location, using PROMPT."
14326 (let ((org-refile-targets org-refile-targets)
14327 (org-refile-use-outline-path org-refile-use-outline-path))
14328 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
14329 (unless org-refile-target-table
14330 (error "No refile targets"))
14331 (let* ((cbuf (current-buffer))
14332 (filename (buffer-file-name (buffer-base-buffer cbuf)))
14333 (fname (and filename (file-truename filename)))
14336 (if (not (equal fname (file-truename (nth 1 x))))
14337 (cons (concat (car x) " (" (file-name-nondirectory
14341 org-refile-target-table))
14342 (completion-ignore-case t))
14343 (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
14346 ;;;; Dynamic blocks
14348 (defun org-find-dblock (name)
14349 "Find the first dynamic block with name NAME in the buffer.
14350 If not found, stay at current position and return nil."
14353 (goto-char (point-min))
14354 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
14356 (match-beginning 0))))
14357 (if pos (goto-char pos))
14360 (defconst org-dblock-start-re
14361 "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
14362 "Matches the startline of a dynamic block, with parameters.")
14364 (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
14365 "Matches the end of a dyhamic block.")
14367 (defun org-create-dblock (plist)
14368 "Create a dynamic block section, with parameters taken from PLIST.
14369 PLIST must containe a :name entry which is used as name of the block."
14370 (unless (bolp) (newline))
14371 (let ((name (plist-get plist :name)))
14372 (insert "#+BEGIN: " name)
14374 (if (eq (car plist) :name)
14375 (setq plist (cddr plist))
14376 (insert " " (prin1-to-string (pop plist)))))
14377 (insert "\n\n#+END:\n")
14378 (beginning-of-line -2)))
14380 (defun org-prepare-dblock ()
14381 "Prepare dynamic block for refresh.
14382 This empties the block, puts the cursor at the insert position and returns
14383 the property list including an extra property :name with the block name."
14384 (unless (looking-at org-dblock-start-re)
14385 (error "Not at a dynamic block"))
14386 (let* ((begdel (1+ (match-end 0)))
14387 (name (org-no-properties (match-string 1)))
14388 (params (append (list :name name)
14389 (read (concat "(" (match-string 3) ")")))))
14390 (unless (re-search-forward org-dblock-end-re nil t)
14391 (error "Dynamic block not terminated"))
14392 (delete-region begdel (match-beginning 0))
14397 (defun org-map-dblocks (&optional command)
14398 "Apply COMMAND to all dynamic blocks in the current buffer.
14399 If COMMAND is not given, use `org-update-dblock'."
14400 (let ((cmd (or command 'org-update-dblock))
14403 (goto-char (point-min))
14404 (while (re-search-forward org-dblock-start-re nil t)
14405 (goto-char (setq pos (match-beginning 0)))
14406 (condition-case nil
14408 (error (message "Error during update of dynamic block")))
14410 (unless (re-search-forward org-dblock-end-re nil t)
14411 (error "Dynamic block not terminated"))))))
14413 (defun org-dblock-update (&optional arg)
14414 "User command for updating dynamic blocks.
14415 Update the dynamic block at point. With prefix ARG, update all dynamic
14416 blocks in the buffer."
14419 (org-update-all-dblocks)
14420 (or (looking-at org-dblock-start-re)
14421 (org-beginning-of-dblock))
14422 (org-update-dblock)))
14424 (defun org-update-dblock ()
14425 "Update the dynamic block at point
14426 This means to empty the block, parse for parameters and then call
14427 the correct writing function."
14428 (save-window-excursion
14429 (let* ((pos (point))
14430 (line (org-current-line))
14431 (params (org-prepare-dblock))
14432 (name (plist-get params :name))
14433 (cmd (intern (concat "org-dblock-write:" name))))
14434 (message "Updating dynamic block `%s' at line %d..." name line)
14435 (funcall cmd params)
14436 (message "Updating dynamic block `%s' at line %d...done" name line)
14439 (defun org-beginning-of-dblock ()
14440 "Find the beginning of the dynamic block at point.
14441 Error if there is no scuh block at point."
14442 (let ((pos (point))
14445 (if (and (re-search-backward org-dblock-start-re nil t)
14446 (setq beg (match-beginning 0))
14447 (re-search-forward org-dblock-end-re nil t)
14448 (> (match-end 0) pos))
14451 (error "Not in a dynamic block"))))
14453 (defun org-update-all-dblocks ()
14454 "Update all dynamic blocks in the buffer.
14455 This function can be used in a hook."
14457 (org-map-dblocks 'org-update-dblock)))
14462 (defconst org-additional-option-like-keywords
14463 '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
14464 "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "DATE:" "TBLFM"
14465 "BEGIN_EXAMPLE" "END_EXAMPLE"))
14467 (defun org-complete (&optional arg)
14468 "Perform completion on word at point.
14469 At the beginning of a headline, this completes TODO keywords as given in
14470 `org-todo-keywords'.
14471 If the current word is preceded by a backslash, completes the TeX symbols
14472 that are supported for HTML support.
14473 If the current word is preceded by \"#+\", completes special words for
14474 setting file options.
14475 In the line after \"#+STARTUP:, complete valid keywords.\"
14476 At all other locations, this simply calls the value of
14477 `org-completion-fallback-command'."
14479 (org-without-partial-completion
14481 (let* ((end (point))
14482 (beg1 (save-excursion
14483 (skip-chars-backward (org-re "[:alnum:]_@"))
14485 (beg (save-excursion
14486 (skip-chars-backward "a-zA-Z0-9_:$")
14488 (confirm (lambda (x) (stringp (car x))))
14489 (searchhead (equal (char-before beg) ?*))
14490 (tag (and (equal (char-before beg1) ?:)
14491 (equal (char-after (point-at-bol)) ?*)))
14492 (prop (and (equal (char-before beg1) ?:)
14493 (not (equal (char-after (point-at-bol)) ?*))))
14494 (texp (equal (char-before beg) ?\\))
14495 (link (equal (char-before beg) ?\[))
14496 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
14499 (startup (string-match "^#\\+STARTUP:.*"
14500 (buffer-substring (point-at-bol) (point))))
14501 (completion-ignore-case opt)
14510 (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
14511 (cons (match-string 2 x) (match-string 1 x)))
14512 (org-split-string (org-get-current-options) "\n"))
14513 (mapcar 'list org-additional-option-like-keywords)))
14515 (setq type :startup)
14516 org-startup-options)
14517 (link (append org-link-abbrev-alist-local
14518 org-link-abbrev-alist))
14522 ((string-match "\\`\\*+[ \t]+\\'"
14523 (buffer-substring (point-at-bol) beg))
14525 (mapcar 'list org-todo-keywords-1))
14527 (setq type :searchhead)
14529 (goto-char (point-min))
14530 (while (re-search-forward org-todo-line-regexp nil t)
14532 (org-make-org-heading-search-string
14533 (match-string 3) t))
14536 (tag (setq type :tag beg beg1)
14537 (or org-tag-alist (org-get-buffer-tags)))
14538 (prop (setq type :prop beg beg1)
14539 (mapcar 'list (org-buffer-property-keys nil t t)))
14541 (call-interactively org-completion-fallback-command)
14542 (throw 'exit nil)))))
14543 (pattern (buffer-substring-no-properties beg end))
14544 (completion (try-completion pattern table confirm)))
14545 (cond ((eq completion t)
14546 (if (not (assoc (upcase pattern) table))
14547 (message "Already complete")
14548 (if (equal type :opt)
14549 (insert (substring (cdr (assoc (upcase pattern) table))
14551 (if (memq type '(:tag :prop)) (insert ":")))))
14553 (message "Can't find completion for \"%s\"" pattern)
14555 ((not (string= pattern completion))
14556 (delete-region beg end)
14557 (if (string-match " +$" completion)
14558 (setq completion (replace-match "" t t completion)))
14559 (insert completion)
14560 (if (get-buffer-window "*Completions*")
14561 (delete-window (get-buffer-window "*Completions*")))
14562 (if (assoc completion table)
14563 (if (eq type :todo) (insert " ")
14564 (if (memq type '(:tag :prop)) (insert ":"))))
14565 (if (and (equal type :opt) (assoc completion table))
14566 (message "%s" (substitute-command-keys
14567 "Press \\[org-complete] again to insert example settings"))))
14569 (message "Making completion list...")
14570 (let ((list (sort (all-completions pattern table confirm)
14572 (with-output-to-temp-buffer "*Completions*"
14573 (condition-case nil
14574 ;; Protection needed for XEmacs and emacs 21
14575 (display-completion-list list pattern)
14576 (error (display-completion-list list)))))
14577 (message "Making completion list...%s" "done")))))))
14579 ;;;; TODO, DEADLINE, Comments
14581 (defun org-toggle-comment ()
14582 "Change the COMMENT state of an entry."
14585 (org-back-to-heading)
14586 (let (case-fold-search)
14587 (if (looking-at (concat outline-regexp
14588 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
14589 (replace-match "" t t nil 1)
14590 (if (looking-at outline-regexp)
14592 (goto-char (match-end 0))
14593 (insert org-comment-string " ")))))))
14595 (defvar org-last-todo-state-is-todo nil
14596 "This is non-nil when the last TODO state change led to a TODO state.
14597 If the last change removed the TODO tag or switched to DONE, then
14600 (defvar org-setting-tags nil) ; dynamically skiped
14602 ;; FIXME: better place
14603 (defun org-property-or-variable-value (var &optional inherit)
14604 "Check if there is a property fixing the value of VAR.
14605 If yes, return this value. If not, return the current value of the variable."
14606 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14607 (if (and prop (stringp prop) (string-match "\\S-" prop))
14609 (symbol-value var))))
14611 (defun org-parse-local-options (string var)
14612 "Parse STRING for startup setting relevant for variable VAR."
14613 (let ((rtn (symbol-value var))
14616 (if (or (not string) (not (string-match "\\S-" string)))
14618 (setq opts (delq nil (mapcar (lambda (x)
14619 (setq e (assoc x org-startup-options))
14620 (if (eq (nth 1 e) var) e nil))
14621 (org-split-string string "[ \t]+"))))
14625 (while (setq e (pop opts))
14626 (if (not (nth 3 e))
14627 (setq rtn (nth 2 e))
14628 (if (not (listp rtn)) (setq rtn nil))
14629 (push (nth 2 e) rtn)))
14632 (defvar org-blocker-hook nil
14633 "Hook for functions that are allowed to block a state change.
14635 Each function gets as its single argument a property list, see
14636 `org-trigger-hook' for more information about this list.
14638 If any of the functions in this hook returns nil, the state change
14641 (defvar org-trigger-hook nil
14642 "Hook for functions that are triggered by a state change.
14644 Each function gets as its single argument a property list with at least
14645 the following elements:
14647 (:type type-of-change :position pos-at-entry-start
14648 :from old-state :to new-state)
14650 Depending on the type, more properties may be present.
14652 This mechanism is currently implemented for:
14656 :type todo-state-change
14657 :from previous state (keyword as a string), or nil
14658 :to new state (keyword as a string), or nil")
14661 (defun org-todo (&optional arg)
14662 "Change the TODO state of an item.
14663 The state of an item is given by a keyword at the start of the heading,
14665 *** TODO Write paper
14668 The different keywords are specified in the variable `org-todo-keywords'.
14669 By default the available states are \"TODO\" and \"DONE\".
14670 So for this example: when the item starts with TODO, it is changed to DONE.
14671 When it starts with DONE, the DONE is removed. And when neither TODO nor
14672 DONE are present, add TODO at the beginning of the heading.
14674 With C-u prefix arg, use completion to determine the new state.
14675 With numeric prefix arg, switch to that state.
14677 For calling through lisp, arg is also interpreted in the following way:
14678 'none -> empty state
14679 \"\"(empty string) -> switch to empty state
14680 'done -> switch to DONE
14681 'nextset -> switch to the next set of keywords
14682 'previousset -> switch to the previous set of keywords
14683 \"WAITING\" -> switch to the specified keyword, but only if it
14684 really is a member of `org-todo-keywords'."
14688 (org-back-to-heading)
14689 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
14690 (or (looking-at (concat " +" org-todo-regexp " *"))
14692 (let* ((match-data (match-data))
14693 (startpos (point-at-bol))
14694 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
14695 (org-log-done org-log-done)
14696 (org-log-repeat org-log-repeat)
14697 (org-todo-log-states org-todo-log-states)
14698 (this (match-string 1))
14699 (hl-pos (match-beginning 0))
14700 (head (org-get-todo-sequence-head this))
14701 (ass (assoc head org-todo-kwd-alist))
14702 (interpret (nth 1 ass))
14703 (done-word (nth 3 ass))
14704 (final-done-word (nth 4 ass))
14705 (last-state (or this ""))
14706 (completion-ignore-case t)
14707 (member (member this org-todo-keywords-1))
14708 (tail (cdr member))
14710 ((and org-todo-key-trigger
14711 (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
14712 (and (not arg) org-use-fast-todo-selection
14713 (not (eq org-use-fast-todo-selection 'prefix)))))
14714 ;; Use fast selection
14715 (org-fast-todo-selection))
14716 ((and (equal arg '(4))
14717 (or (not org-use-fast-todo-selection)
14718 (not org-todo-key-trigger)))
14719 ;; Read a state with completion
14720 (completing-read "State: " (mapcar (lambda(x) (list x))
14721 org-todo-keywords-1)
14725 (if tail (car tail) nil)
14726 (car org-todo-keywords-1)))
14728 (if (equal member org-todo-keywords-1)
14731 (nth (- (length org-todo-keywords-1) (length tail) 2)
14732 org-todo-keywords-1)
14733 (org-last org-todo-keywords-1))))
14734 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
14735 (setq arg nil))) ; hack to fall back to cycling
14737 ;; user or caller requests a specific state
14739 ((equal arg "") nil)
14740 ((eq arg 'none) nil)
14741 ((eq arg 'done) (or done-word (car org-done-keywords)))
14743 (or (car (cdr (member head org-todo-heads)))
14744 (car org-todo-heads)))
14745 ((eq arg 'previousset)
14746 (let ((org-todo-heads (reverse org-todo-heads)))
14747 (or (car (cdr (member head org-todo-heads)))
14748 (car org-todo-heads))))
14749 ((car (member arg org-todo-keywords-1)))
14750 ((nth (1- (prefix-numeric-value arg))
14751 org-todo-keywords-1))))
14752 ((null member) (or head (car org-todo-keywords-1)))
14753 ((equal this final-done-word) nil) ;; -> make empty
14754 ((null tail) nil) ;; -> first entry
14755 ((eq interpret 'sequence)
14757 ((memq interpret '(type priority))
14758 (if (eq this-command last-command)
14760 (if (> (length tail) 0)
14761 (or done-word (car org-done-keywords))
14764 (next (if state (concat " " state " ") " "))
14765 (change-plist (list :type 'todo-state-change :from this :to state
14766 :position startpos))
14768 (when org-blocker-hook
14769 (unless (save-excursion
14771 (run-hook-with-args-until-failure
14772 'org-blocker-hook change-plist)))
14773 (if (interactive-p)
14774 (error "TODO state change from %s to %s blocked" this state)
14776 (message "TODO state change from %s to %s blocked" this state)
14777 (throw 'exit nil))))
14778 (store-match-data match-data)
14779 (replace-match next t t)
14780 (unless (pos-visible-in-window-p hl-pos)
14781 (message "TODO state changed to %s" (org-trim next)))
14783 (setq head (org-get-todo-sequence-head state)
14784 ass (assoc head org-todo-kwd-alist)
14785 interpret (nth 1 ass)
14786 done-word (nth 3 ass)
14787 final-done-word (nth 4 ass)))
14788 (when (memq arg '(nextset previousset))
14789 (message "Keyword-Set %d/%d: %s"
14790 (- (length org-todo-sets) -1
14791 (length (memq (assoc state org-todo-sets) org-todo-sets)))
14792 (length org-todo-sets)
14793 (mapconcat 'identity (assoc state org-todo-sets) " ")))
14794 (setq org-last-todo-state-is-todo
14795 (not (member state org-done-keywords)))
14796 (setq now-done-p (and (member state org-done-keywords)
14797 (not (member this org-done-keywords))))
14798 (and logging (org-local-logging logging))
14799 (when (and (or org-todo-log-states org-log-done)
14800 (not (memq arg '(nextset previousset))))
14801 ;; we need to look at recording a time and note
14802 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
14803 (nth 2 (assoc this org-todo-log-states))))
14805 (member state org-not-done-keywords)
14806 (not (member this org-not-done-keywords)))
14807 ;; This is now a todo state and was not one before
14808 ;; If there was a CLOSED time stamp, get rid of it.
14809 (org-add-planning-info nil nil 'closed))
14810 (when (and now-done-p org-log-done)
14811 ;; It is now done, and it was not done before
14812 (org-add-planning-info 'closed (org-current-time))
14813 (if (and (not dolog) (eq 'note org-log-done))
14814 (org-add-log-maybe 'done state 'findpos 'note)))
14815 (when (and state dolog)
14816 ;; This is a non-nil state, and we need to log it
14817 (org-add-log-maybe 'state state 'findpos dolog)))
14818 ;; Fixup tag positioning
14819 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
14820 (run-hooks 'org-after-todo-state-change-hook)
14821 (if (and arg (not (member state org-done-keywords)))
14822 (setq head (org-get-todo-sequence-head state)))
14823 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
14824 ;; Do we need to trigger a repeat?
14825 (when now-done-p (org-auto-repeat-maybe state))
14826 ;; Fixup cursor location if close to the keyword
14827 (if (and (outline-on-heading-p)
14829 (save-excursion (beginning-of-line 1)
14830 (looking-at org-todo-line-regexp))
14831 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
14833 (goto-char (or (match-end 2) (match-end 1)))
14835 (when org-trigger-hook
14837 (run-hook-with-args 'org-trigger-hook change-plist)))))))
14839 (defun org-local-logging (value)
14840 "Get logging settings from a property VALUE."
14842 ;; directly set the variables, they are already local.
14843 (setq org-log-done nil
14845 org-todo-log-states nil)
14846 (setq words (org-split-string value))
14847 (while (setq w (pop words))
14849 ((setq a (assoc w org-startup-options))
14850 (and (member (nth 1 a) '(org-log-done org-log-repeat))
14851 (set (nth 1 a) (nth 2 a))))
14852 ((setq a (org-extract-log-state-settings w))
14853 (and (member (car a) org-todo-keywords-1)
14854 (push a org-todo-log-states)))))))
14856 (defun org-get-todo-sequence-head (kwd)
14857 "Return the head of the TODO sequence to which KWD belongs.
14858 If KWD is not set, check if there is a text property remembering the
14863 (or (get-text-property (point-at-bol) 'org-todo-head)
14865 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
14866 nil (point-at-eol)))
14867 (get-text-property p 'org-todo-head))))
14868 ((not (member kwd org-todo-keywords-1))
14869 (car org-todo-keywords-1))
14870 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
14872 (defun org-fast-todo-selection ()
14873 "Fast TODO keyword selection with single keys.
14874 Returns the new TODO keyword, or nil if no state change should occur."
14875 (let* ((fulltable org-todo-key-alist)
14876 (done-keywords org-done-keywords) ;; needed for the faces.
14877 (maxlen (apply 'max (mapcar
14879 (if (stringp (car x)) (string-width (car x)) 0))
14882 (fwidth (+ maxlen 3 1 3))
14883 (ncol (/ (- (window-width) 4) fwidth))
14886 (save-window-excursion
14888 (set-buffer (get-buffer-create " *Org todo*"))
14889 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
14891 (org-set-local 'org-done-keywords done-keywords)
14892 (setq tbl fulltable cnt 0)
14893 (while (setq e (pop tbl))
14895 ((equal e '(:startgroup))
14896 (push '() groups) (setq ingroup t)
14897 (when (not (= cnt 0))
14901 ((equal e '(:endgroup))
14902 (setq ingroup nil cnt 0)
14905 (setq tg (car e) c (cdr e))
14906 (if ingroup (push tg (car groups)))
14907 (setq tg (org-add-props tg nil 'face
14908 (org-get-todo-face tg)))
14909 (if (and (= cnt 0) (not ingroup)) (insert " "))
14910 (insert "[" c "] " tg (make-string
14911 (- fwidth 4 (length tg)) ?\ ))
14912 (when (= (setq cnt (1+ cnt)) ncol)
14914 (if ingroup (insert " "))
14917 (goto-char (point-min))
14918 (if (and (not expert) (fboundp 'fit-window-to-buffer))
14919 (fit-window-to-buffer))
14920 (message "[a-z..]:Set [SPC]:clear")
14921 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
14924 (and (= c ?q) (not (rassoc c fulltable))))
14925 (setq quit-flag t))
14927 ((setq e (rassoc c fulltable) tg (car e))
14929 (t (setq quit-flag t))))))
14931 (defun org-get-repeat ()
14932 "Check if tere is a deadline/schedule with repeater in this entry."
14935 (org-back-to-heading t)
14936 (if (re-search-forward
14937 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
14938 (match-string 1)))))
14940 (defvar org-last-changed-timestamp)
14941 (defvar org-log-post-message)
14942 (defvar org-log-note-purpose)
14943 (defun org-auto-repeat-maybe (done-word)
14944 "Check if the current headline contains a repeated deadline/schedule.
14945 If yes, set TODO state back to what it was and change the base date
14946 of repeating deadline/scheduled time stamps to new date.
14947 This function is run automatically after each state change to a DONE state."
14948 ;; last-state is dynamically scoped into this function
14949 (let* ((repeat (org-get-repeat))
14950 (aa (assoc last-state org-todo-kwd-alist))
14951 (interpret (nth 1 aa))
14953 (whata '(("d" . day) ("m" . month) ("y" . year)))
14954 (msg "Entry repeats: ")
14956 (org-todo-log-states nil)
14959 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
14960 (org-todo (if (eq interpret 'type) last-state head))
14961 (when (and org-log-repeat
14962 (or (not (memq 'org-add-log-note
14963 (default-value 'post-command-hook)))
14964 (eq org-log-note-purpose 'done)))
14965 ;; Make sure a note is taken;
14966 (org-add-log-maybe 'state (or done-word (car org-done-keywords))
14967 'findpos org-log-repeat))
14968 (org-back-to-heading t)
14969 (org-add-planning-info nil nil 'closed)
14970 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
14971 org-deadline-time-regexp "\\)\\|\\("
14972 org-ts-regexp "\\)"))
14973 (while (re-search-forward
14974 re (save-excursion (outline-next-heading) (point)) t)
14975 (setq type (if (match-end 1) org-scheduled-string
14976 (if (match-end 3) org-deadline-string "Plain:"))
14977 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
14978 (when (string-match "\\([-+]?[0-9]+\\)\\([dwmy]\\)" ts)
14979 (setq n (string-to-number (match-string 1 ts))
14980 what (match-string 2 ts))
14981 (if (equal what "w") (setq n (* n 7) what "d"))
14982 (org-timestamp-change n (cdr (assoc what whata)))
14983 (setq msg (concat msg type org-last-changed-timestamp " "))))
14984 (setq org-log-post-message msg)
14985 (message "%s" msg))))
14987 (defun org-show-todo-tree (arg)
14988 "Make a compact tree which shows all headlines marked with TODO.
14989 The tree will show the lines where the regexp matches, and all higher
14990 headlines above the match.
14991 With a \\[universal-argument] prefix, also show the DONE entries.
14992 With a numeric prefix N, construct a sparse tree for the Nth element
14993 of `org-todo-keywords-1'."
14995 (let ((case-fold-search nil)
14997 (cond ((null arg) org-not-done-regexp)
14999 (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
15000 (mapcar 'list org-todo-keywords-1))))
15002 (mapconcat 'identity (org-split-string kwd "|") "\\|")
15004 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
15005 (regexp-quote (nth (1- (prefix-numeric-value arg))
15006 org-todo-keywords-1)))
15007 (t (error "Invalid prefix argument: %s" arg)))))
15008 (message "%d TODO entries found"
15009 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
15011 (defun org-deadline (&optional remove)
15012 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
15013 With argument REMOVE, remove any deadline from the item."
15017 (org-remove-timestamp-with-keyword org-deadline-string)
15018 (message "Item no longer has a deadline."))
15019 (org-add-planning-info 'deadline nil 'closed)))
15021 (defun org-schedule (&optional remove)
15022 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
15023 With argument REMOVE, remove any scheduling date from the item."
15027 (org-remove-timestamp-with-keyword org-scheduled-string)
15028 (message "Item is no longer scheduled."))
15029 (org-add-planning-info 'scheduled nil 'closed)))
15031 (defun org-remove-timestamp-with-keyword (keyword)
15032 "Remove all time stamps with KEYWORD in the current entry."
15033 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
15036 (org-back-to-heading t)
15038 (org-end-of-subtree t t)
15039 (while (re-search-backward re beg t)
15041 (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
15042 (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
15044 (defun org-add-planning-info (what &optional time &rest remove)
15045 "Insert new timestamp with keyword in the line directly after the headline.
15046 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
15047 If non is given, the user is prompted for a date.
15048 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
15051 (let (org-time-was-given org-end-time-was-given)
15052 (when what (setq time (or time (org-read-date nil 'to-time))))
15053 (when (and org-insert-labeled-timestamps-at-point
15054 (member what '(scheduled deadline)))
15056 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
15057 (org-insert-time-stamp time org-time-was-given
15058 nil nil nil (list org-end-time-was-given))
15062 (let (col list elt ts buffer-invisibility-spec)
15063 (org-back-to-heading t)
15064 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
15065 (goto-char (match-end 1))
15066 (setq col (current-column))
15067 (goto-char (match-end 0))
15068 (if (eobp) (insert "\n") (forward-char 1))
15069 (if (and (not (looking-at outline-regexp))
15070 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
15072 (not (equal (match-string 1) org-clock-string)))
15073 (narrow-to-region (match-beginning 0) (match-end 0))
15074 (insert-before-markers "\n")
15076 (narrow-to-region (point) (point))
15077 (indent-to-column col))
15078 ;; Check if we have to remove something.
15079 (setq list (cons what remove))
15081 (setq elt (pop list))
15082 (goto-char (point-min))
15083 (when (or (and (eq elt 'scheduled)
15084 (re-search-forward org-scheduled-time-regexp nil t))
15085 (and (eq elt 'deadline)
15086 (re-search-forward org-deadline-time-regexp nil t))
15087 (and (eq elt 'closed)
15088 (re-search-forward org-closed-time-regexp nil t)))
15090 (if (looking-at "--+<[^>]+>") (replace-match ""))
15091 (if (looking-at " +") (replace-match ""))))
15092 (goto-char (point-max))
15095 (if (not (equal (char-before) ?\ )) " " "")
15096 (cond ((eq what 'scheduled) org-scheduled-string)
15097 ((eq what 'deadline) org-deadline-string)
15098 ((eq what 'closed) org-closed-string))
15100 (setq ts (org-insert-time-stamp
15102 (or org-time-was-given
15103 (and (eq what 'closed) org-log-done-with-time))
15105 nil nil (list org-end-time-was-given)))
15107 (goto-char (point-min))
15109 (if (looking-at "[ \t]+\r?\n")
15110 (replace-match ""))
15113 (defvar org-log-note-marker (make-marker))
15114 (defvar org-log-note-purpose nil)
15115 (defvar org-log-note-state nil)
15116 (defvar org-log-note-how nil)
15117 (defvar org-log-note-window-configuration nil)
15118 (defvar org-log-note-return-to (make-marker))
15119 (defvar org-log-post-message nil
15120 "Message to be displayed after a log note has been stored.
15121 The auto-repeater uses this.")
15123 (defun org-add-log-maybe (&optional purpose state findpos how)
15124 "Set up the post command hook to take a note.
15125 If this is about to TODO state change, the new state is expected in STATE.
15126 When FINDPOS is non-nil, find the correct position for the note in
15127 the current entry. If not, assume that it can be inserted at point."
15130 (org-back-to-heading t)
15131 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
15132 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
15134 (goto-char (match-end 0))
15135 (unless org-log-states-order-reversed
15136 (and (= (char-after) ?\n) (forward-char 1))
15137 (org-skip-over-state-notes)
15138 (skip-chars-backward " \t\n\r")))
15139 (move-marker org-log-note-marker (point))
15140 (setq org-log-note-purpose purpose
15141 org-log-note-state state
15142 org-log-note-how how)
15143 (add-hook 'post-command-hook 'org-add-log-note 'append)))
15145 (defun org-skip-over-state-notes ()
15146 "Skip past the list of State notes in an entry."
15147 (if (looking-at "\n[ \t]*- State") (forward-char 1))
15148 (while (looking-at "[ \t]*- State")
15149 (condition-case nil
15151 (error (org-end-of-item)))))
15153 (defun org-add-log-note (&optional purpose)
15154 "Pop up a window for taking a note, and add this note later at point."
15155 (remove-hook 'post-command-hook 'org-add-log-note)
15156 (setq org-log-note-window-configuration (current-window-configuration))
15157 (delete-other-windows)
15158 (move-marker org-log-note-return-to (point))
15159 (switch-to-buffer (marker-buffer org-log-note-marker))
15160 (goto-char org-log-note-marker)
15161 (org-switch-to-buffer-other-window "*Org Note*")
15163 (if (memq org-log-note-how '(time state)) ; FIXME: time or state????????????
15164 (org-store-log-note)
15165 (let ((org-inhibit-startup t)) (org-mode))
15166 (insert (format "# Insert note for %s.
15167 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
15169 ((eq org-log-note-purpose 'clock-out) "stopped clock")
15170 ((eq org-log-note-purpose 'done) "closed todo item")
15171 ((eq org-log-note-purpose 'state)
15172 (format "state change to \"%s\"" org-log-note-state))
15173 (t (error "This should not happen")))))
15174 (org-set-local 'org-finish-function 'org-store-log-note)))
15176 (defun org-store-log-note ()
15177 "Finish taking a log note, and insert it to where it belongs."
15178 (let ((txt (buffer-string))
15179 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
15181 (kill-buffer (current-buffer))
15182 (while (string-match "\\`#.*\n[ \t\n]*" txt)
15183 (setq txt (replace-match "" t t txt)))
15184 (if (string-match "\\s-+\\'" txt)
15185 (setq txt (replace-match "" t t txt)))
15186 (setq lines (org-split-string txt "\n"))
15187 (when (and note (string-match "\\S-" note))
15189 (org-replace-escapes
15191 (list (cons "%u" (user-login-name))
15192 (cons "%U" user-full-name)
15193 (cons "%t" (format-time-string
15194 (org-time-stamp-format 'long 'inactive)
15196 (cons "%s" (if org-log-note-state
15197 (concat "\"" org-log-note-state "\"")
15199 (if lines (setq note (concat note " \\\\")))
15201 (when (or current-prefix-arg org-note-abort) (setq lines nil))
15204 (set-buffer (marker-buffer org-log-note-marker))
15206 (goto-char org-log-note-marker)
15207 (move-marker org-log-note-marker nil)
15209 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
15210 (indent-relative nil)
15211 (insert "- " (pop lines))
15212 (org-indent-line-function)
15213 (beginning-of-line 1)
15214 (looking-at "[ \t]*")
15215 (setq ind (concat (match-string 0) " "))
15217 (while lines (insert "\n" ind (pop lines)))))))
15218 (set-window-configuration org-log-note-window-configuration)
15219 (with-current-buffer (marker-buffer org-log-note-return-to)
15220 (goto-char org-log-note-return-to))
15221 (move-marker org-log-note-return-to nil)
15222 (and org-log-post-message (message "%s" org-log-post-message)))
15224 ;; FIXME: what else would be useful?
15228 (defun org-sparse-tree (&optional arg)
15229 "Create a sparse tree, prompt for the details.
15230 This command can create sparse trees. You first need to select the type
15231 of match used to create the tree:
15233 t Show entries with a specific TODO keyword.
15234 T Show entries selected by a tags match.
15235 p Enter a property name and its value (both with completion on existing
15236 names/values) and show entries with that property.
15237 r Show entries matching a regular expression
15238 d Show deadlines due within `org-deadline-warning-days'."
15240 (let (ans kwd value)
15241 (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
15242 (setq ans (read-char-exclusive))
15245 (call-interactively 'org-check-deadlines))
15247 (call-interactively 'org-check-before-date))
15249 (org-show-todo-tree '(4)))
15251 (call-interactively 'org-tags-sparse-tree))
15252 ((member ans '(?p ?P))
15253 (setq kwd (completing-read "Property: "
15254 (mapcar 'list (org-buffer-property-keys))))
15255 (setq value (completing-read "Value: "
15256 (mapcar 'list (org-property-values kwd))))
15257 (unless (string-match "\\`{.*}\\'" value)
15258 (setq value (concat "\"" value "\"")))
15259 (org-tags-sparse-tree arg (concat kwd "=" value)))
15260 ((member ans '(?r ?R ?/))
15261 (call-interactively 'org-occur))
15262 (t (error "No such sparse tree command \"%c\"" ans)))))
15264 (defvar org-occur-highlights nil
15265 "List of overlays used for occur matches.")
15266 (make-variable-buffer-local 'org-occur-highlights)
15267 (defvar org-occur-parameters nil
15268 "Parameters of the active org-occur calls.
15269 This is a list, each call to org-occur pushes as cons cell,
15270 containing the regular expression and the callback, onto the list.
15271 The list can contain several entries if `org-occur' has been called
15272 several time with the KEEP-PREVIOUS argument. Otherwise, this list
15273 will only contain one set of parameters. When the highlights are
15274 removed (for example with `C-c C-c', or with the next edit (depending
15275 on `org-remove-highlights-with-change'), this variable is emptied
15277 (make-variable-buffer-local 'org-occur-parameters)
15279 (defun org-occur (regexp &optional keep-previous callback)
15280 "Make a compact tree which shows all matches of REGEXP.
15281 The tree will show the lines where the regexp matches, and all higher
15282 headlines above the match. It will also show the heading after the match,
15283 to make sure editing the matching entry is easy.
15284 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
15285 call to `org-occur' will be kept, to allow stacking of calls to this
15287 If CALLBACK is non-nil, it is a function which is called to confirm
15288 that the match should indeed be shown."
15289 (interactive "sRegexp: \nP")
15290 (unless keep-previous
15291 (org-remove-occur-highlights nil nil t))
15292 (push (cons regexp callback) org-occur-parameters)
15295 (goto-char (point-min))
15296 (if (or (not keep-previous) ; do not want to keep
15297 (not org-occur-highlights)) ; no previous matches
15300 (while (re-search-forward regexp nil t)
15301 (when (or (not callback)
15302 (save-match-data (funcall callback)))
15303 (setq cnt (1+ cnt))
15304 (when org-highlight-sparse-tree-matches
15305 (org-highlight-new-match (match-beginning 0) (match-end 0)))
15306 (org-show-context 'occur-tree))))
15307 (when org-remove-highlights-with-change
15308 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
15310 (unless org-sparse-tree-open-archived-trees
15311 (org-hide-archived-subtrees (point-min) (point-max)))
15312 (run-hooks 'org-occur-hook)
15313 (if (interactive-p)
15314 (message "%d match(es) for regexp %s" cnt regexp))
15317 (defun org-show-context (&optional key)
15318 "Make sure point and context and visible.
15319 How much context is shown depends upon the variables
15320 `org-show-hierarchy-above', `org-show-following-heading'. and
15321 `org-show-siblings'."
15322 (let ((heading-p (org-on-heading-p t))
15323 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
15324 (following-p (org-get-alist-option org-show-following-heading key))
15325 (entry-p (org-get-alist-option org-show-entry-below key))
15326 (siblings-p (org-get-alist-option org-show-siblings key)))
15328 ;; Show heading or entry text
15329 (if (and heading-p (not entry-p))
15330 (org-flag-heading nil) ; only show the heading
15331 (and (or entry-p (org-invisible-p) (org-invisible-p2))
15332 (org-show-hidden-entry))) ; show entire entry
15334 ;; Show next sibling, or heading below text
15336 (and (if heading-p (org-goto-sibling) (outline-next-heading))
15337 (org-flag-heading nil))))
15338 (when siblings-p (org-show-siblings))
15340 ;; show all higher headings, possibly with siblings
15342 (while (and (condition-case nil
15343 (progn (org-up-heading-all 1) t)
15346 (org-flag-heading nil)
15347 (when siblings-p (org-show-siblings))))))))
15349 (defun org-reveal (&optional siblings)
15350 "Show current entry, hierarchy above it, and the following headline.
15351 This can be used to show a consistent set of context around locations
15352 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
15353 not t for the search context.
15355 With optional argument SIBLINGS, on each level of the hierarchy all
15356 siblings are shown. This repairs the tree structure to what it would
15357 look like when opened with hierarchical calls to `org-cycle'."
15359 (let ((org-show-hierarchy-above t)
15360 (org-show-following-heading t)
15361 (org-show-siblings (if siblings t org-show-siblings)))
15362 (org-show-context nil)))
15364 (defun org-highlight-new-match (beg end)
15365 "Highlight from BEG to END and mark the highlight is an occur headline."
15366 (let ((ov (org-make-overlay beg end)))
15367 (org-overlay-put ov 'face 'secondary-selection)
15368 (push ov org-occur-highlights)))
15370 (defun org-remove-occur-highlights (&optional beg end noremove)
15371 "Remove the occur highlights from the buffer.
15372 BEG and END are ignored. If NOREMOVE is nil, remove this function
15373 from the `before-change-functions' in the current buffer."
15375 (unless org-inhibit-highlight-removal
15376 (mapc 'org-delete-overlay org-occur-highlights)
15377 (setq org-occur-highlights nil)
15378 (setq org-occur-parameters nil)
15380 (remove-hook 'before-change-functions
15381 'org-remove-occur-highlights 'local))))
15385 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
15386 "Regular expression matching the priority indicator.")
15388 (defvar org-remove-priority-next-time nil)
15390 (defun org-priority-up ()
15391 "Increase the priority of the current item."
15393 (org-priority 'up))
15395 (defun org-priority-down ()
15396 "Decrease the priority of the current item."
15398 (org-priority 'down))
15400 (defun org-priority (&optional action)
15401 "Change the priority of an item by ARG.
15402 ACTION can be `set', `up', `down', or a character."
15404 (setq action (or action 'set))
15405 (let (current new news have remove)
15407 (org-back-to-heading)
15408 (if (looking-at org-priority-regexp)
15409 (setq current (string-to-char (match-string 2))
15411 (setq current org-default-priority))
15413 ((or (eq action 'set) (integerp action))
15414 (if (integerp action)
15416 (message "Priority %c-%c, SPC to remove: " org-highest-priority org-lowest-priority)
15417 (setq new (read-char-exclusive)))
15418 (if (and (= (upcase org-highest-priority) org-highest-priority)
15419 (= (upcase org-lowest-priority) org-lowest-priority))
15420 (setq new (upcase new)))
15421 (cond ((equal new ?\ ) (setq remove t))
15422 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
15423 (error "Priority must be between `%c' and `%c'"
15424 org-highest-priority org-lowest-priority))))
15426 (if (and (not have) (eq last-command this-command))
15427 (setq new org-lowest-priority)
15428 (setq new (if (and org-priority-start-cycle-with-default (not have))
15429 org-default-priority (1- current)))))
15431 (if (and (not have) (eq last-command this-command))
15432 (setq new org-highest-priority)
15433 (setq new (if (and org-priority-start-cycle-with-default (not have))
15434 org-default-priority (1+ current)))))
15435 (t (error "Invalid action")))
15436 (if (or (< (upcase new) org-highest-priority)
15437 (> (upcase new) org-lowest-priority))
15439 (setq news (format "%c" new))
15442 (replace-match "" t t nil 1)
15443 (replace-match news t t nil 2))
15445 (error "No priority cookie found in line")
15446 (looking-at org-todo-line-regexp)
15449 (goto-char (match-end 2))
15450 (insert " [#" news "]"))
15451 (goto-char (match-beginning 3))
15452 (insert "[#" news "] ")))))
15453 (org-preserve-lc (org-set-tags nil 'align))
15455 (message "Priority removed")
15456 (message "Priority of current item set to %s" news))))
15459 (defun org-get-priority (s)
15460 "Find priority cookie and return priority."
15462 (if (not (string-match org-priority-regexp s))
15463 (* 1000 (- org-lowest-priority org-default-priority))
15464 (* 1000 (- org-lowest-priority
15465 (string-to-char (match-string 2 s)))))))
15469 (defun org-scan-tags (action matcher &optional todo-only)
15470 "Scan headline tags with inheritance and produce output ACTION.
15471 ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
15472 evaluated, testing if a given set of tags qualifies a headline for
15473 inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
15474 are included in the output."
15475 (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
15476 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
15478 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
15479 (props (list 'face nil
15480 'done-face 'org-done
15482 'mouse-face 'highlight
15483 'org-not-done-regexp org-not-done-regexp
15484 'org-todo-regexp org-todo-regexp
15485 'keymap org-agenda-keymap
15487 (format "mouse-2 or RET jump to org file %s"
15488 (abbreviate-file-name
15489 (or (buffer-file-name (buffer-base-buffer))
15490 (buffer-name (buffer-base-buffer)))))))
15491 (case-fold-search nil)
15493 tags tags-list tags-alist (llast 0) rtn level category i txt
15494 todo marker entry priority)
15496 (goto-char (point-min))
15497 (when (eq action 'sparse-tree)
15499 (org-remove-occur-highlights))
15500 (while (re-search-forward re nil t)
15502 (setq todo (if (match-end 1) (match-string 2))
15503 tags (if (match-end 4) (match-string 4)))
15504 (goto-char (setq lspos (1+ (match-beginning 0))))
15505 (setq level (org-reduced-level (funcall outline-level))
15506 category (org-get-category))
15507 (setq i llast llast level)
15508 ;; remove tag lists from same and sublevels
15509 (while (>= i level)
15510 (when (setq entry (assoc i tags-alist))
15511 (setq tags-alist (delete entry tags-alist)))
15513 ;; add the nex tags
15515 (setq tags (mapcar 'downcase (org-split-string tags ":"))
15517 (cons (cons level tags) tags-alist)))
15518 ;; compile tags for current headline
15520 (if org-use-tag-inheritance
15521 (apply 'append (mapcar 'cdr tags-alist))
15523 (when (and (or (not todo-only) (member todo org-not-done-keywords))
15525 (or (not org-agenda-skip-archived-trees)
15526 (not (member org-archive-tag tags-list))))
15527 (and (eq action 'agenda) (org-agenda-skip))
15528 ;; list this headline
15530 (if (eq action 'sparse-tree)
15532 (and org-highlight-sparse-tree-matches
15533 (org-get-heading) (match-end 0)
15534 (org-highlight-new-match
15535 (match-beginning 0) (match-beginning 1)))
15536 (org-show-context 'tags-tree))
15537 (setq txt (org-format-agenda-item
15540 (if org-tags-match-list-sublevels
15541 (make-string (1- level) ?.) "")
15543 category tags-list)
15544 priority (org-get-priority txt))
15546 (setq marker (org-agenda-new-marker))
15547 (org-add-props txt props
15548 'org-marker marker 'org-hd-marker marker 'org-category category
15549 'priority priority 'type "tagsmatch")
15551 ;; if we are to skip sublevels, jump to end of subtree
15552 (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
15553 (when (and (eq action 'sparse-tree)
15554 (not org-sparse-tree-open-archived-trees))
15555 (org-hide-archived-subtrees (point-min) (point-max)))
15558 (defvar todo-only) ;; dynamically scoped
15560 (defun org-tags-sparse-tree (&optional todo-only match)
15561 "Create a sparse tree according to tags string MATCH.
15562 MATCH can contain positive and negative selection of tags, like
15563 \"+WORK+URGENT-WITHBOSS\".
15564 If optional argument TODO_ONLY is non-nil, only select lines that are
15567 (org-prepare-agenda-buffers (list (current-buffer)))
15568 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
15570 (defvar org-cached-props nil)
15571 (defun org-cached-entry-get (pom property)
15572 (if (or (eq t org-use-property-inheritance)
15573 (member property org-use-property-inheritance))
15574 ;; Caching is not possible, check it directly
15575 (org-entry-get pom property 'inherit)
15576 ;; Get all properties, so that we can do complicated checks easily
15577 (cdr (assoc property (or org-cached-props
15578 (setq org-cached-props
15579 (org-entry-properties pom)))))))
15581 (defun org-global-tags-completion-table (&optional files)
15582 "Return the list of all tags in all agenda buffer/files."
15589 (set-buffer (find-file-noselect file))
15590 (append (org-get-buffer-tags)
15591 (mapcar (lambda (x) (if (stringp (car-safe x))
15592 (list (car-safe x)) nil))
15594 (if (and files (car files))
15596 (org-agenda-files))))))))
15598 (defun org-make-tags-matcher (match)
15599 "Create the TAGS//TODO matcher form for the selection string MATCH."
15600 ;; todo-only is scoped dynamically into this function, and the function
15601 ;; may change it it the matcher asksk for it.
15603 ;; Get a new match request, with completion
15604 (let ((org-last-tags-completion-table
15605 (org-global-tags-completion-table)))
15606 (setq match (completing-read
15607 "Match: " 'org-tags-completion-function nil nil nil
15608 'org-tags-history))))
15610 ;; Parse the string and create a lisp form
15611 (let ((match0 match)
15612 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
15614 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
15615 orterms term orlist re-p level-p prop-p pn pv cat-p gv)
15616 (if (string-match "/+" match)
15617 ;; match contains also a todo-matching request
15619 (setq tagsmatch (substring match 0 (match-beginning 0))
15620 todomatch (substring match (match-end 0)))
15621 (if (string-match "^!" todomatch)
15622 (setq todo-only t todomatch (substring todomatch 1)))
15623 (if (string-match "^\\s-*$" todomatch)
15624 (setq todomatch nil)))
15625 ;; only matching tags
15626 (setq tagsmatch match todomatch nil))
15628 ;; Make the tags matcher
15629 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
15630 (setq tagsmatcher t)
15631 (setq orterms (org-split-string tagsmatch "|") orlist nil)
15632 (while (setq term (pop orterms))
15633 (while (and (equal (substring term -1) "\\") orterms)
15634 (setq term (concat term "|" (pop orterms)))) ; repair bad split
15635 (while (string-match re term)
15636 (setq minus (and (match-end 1)
15637 (equal (match-string 1 term) "-"))
15638 tag (match-string 2 term)
15639 re-p (equal (string-to-char tag) ?{)
15640 level-p (match-end 3)
15641 prop-p (match-end 4)
15643 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
15644 (level-p `(= level ,(string-to-number
15645 (match-string 3 term))))
15647 (setq pn (match-string 4 term)
15648 pv (match-string 5 term)
15649 cat-p (equal pn "CATEGORY")
15650 re-p (equal (string-to-char pv) ?{)
15651 pv (substring pv 1 -1))
15652 (if (equal pn "CATEGORY")
15653 (setq gv '(get-text-property (point) 'org-category))
15654 (setq gv `(org-cached-entry-get nil ,pn)))
15656 `(string-match ,pv (or ,gv ""))
15657 `(equal ,pv (or ,gv ""))))
15658 (t `(member ,(downcase tag) tags-list)))
15659 mm (if minus (list 'not mm) mm)
15660 term (substring term (match-end 0)))
15661 (push mm tagsmatcher))
15662 (push (if (> (length tagsmatcher) 1)
15663 (cons 'and tagsmatcher)
15666 (setq tagsmatcher nil))
15667 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
15669 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
15671 ;; Make the todo matcher
15672 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
15673 (setq todomatcher t)
15674 (setq orterms (org-split-string todomatch "|") orlist nil)
15675 (while (setq term (pop orterms))
15676 (while (string-match re term)
15677 (setq minus (and (match-end 1)
15678 (equal (match-string 1 term) "-"))
15679 kwd (match-string 2 term)
15680 re-p (equal (string-to-char kwd) ?{)
15681 term (substring term (match-end 0))
15683 `(string-match ,(substring kwd 1 -1) todo)
15684 (list 'equal 'todo kwd))
15685 mm (if minus (list 'not mm) mm))
15686 (push mm todomatcher))
15687 (push (if (> (length todomatcher) 1)
15688 (cons 'and todomatcher)
15691 (setq todomatcher nil))
15692 (setq todomatcher (if (> (length orlist) 1)
15693 (cons 'or orlist) (car orlist))))
15695 ;; Return the string and lisp forms of the matcher
15696 (setq matcher (if todomatcher
15697 (list 'and tagsmatcher todomatcher)
15699 (cons match0 matcher)))
15701 (defun org-match-any-p (re list)
15702 "Does re match any element of list?"
15703 (setq list (mapcar (lambda (x) (string-match re x)) list))
15706 (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
15707 (defvar org-tags-overlay (org-make-overlay 1 1))
15708 (org-detach-overlay org-tags-overlay)
15710 (defun org-align-tags-here (to-col)
15711 ;; Assumes that this is a headline
15712 (let ((pos (point)) (col (current-column)) tags)
15713 (beginning-of-line 1)
15714 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
15715 (< pos (match-beginning 2)))
15717 (setq tags (match-string 2))
15718 (goto-char (match-beginning 1))
15720 (delete-region (point) (1+ (match-end 0)))
15723 (max (1+ (current-column))
15727 (- (abs to-col) (length tags))))
15730 (move-to-column (min (current-column) col) t))
15733 (defun org-set-tags (&optional arg just-align)
15734 "Set the tags for the current headline.
15735 With prefix ARG, realign all tags in headings in the current buffer."
15737 (let* ((re (concat "^" outline-regexp))
15738 (current (org-get-tags-string))
15739 (col (current-column))
15740 (org-setting-tags t)
15741 table current-tags inherited-tags ; computed below when needed
15745 (goto-char (point-min))
15746 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
15747 (while (re-search-forward re nil t)
15748 (org-set-tags nil t)
15750 (message "All tags realigned to column %d" org-tags-column))
15752 (setq tags current)
15753 ;; Get a new set of tags from the user
15755 (setq table (or org-tag-alist (org-get-buffer-tags))
15756 org-last-tags-completion-table table
15757 current-tags (org-split-string current ":")
15758 inherited-tags (nreverse
15759 (nthcdr (length current-tags)
15760 (nreverse (org-get-tags-at))))
15762 (if (or (eq t org-use-fast-tag-selection)
15763 (and org-use-fast-tag-selection
15764 (delq nil (mapcar 'cdr table))))
15765 (org-fast-tag-selection
15766 current-tags inherited-tags table
15767 (if org-fast-tag-selection-include-todo org-todo-key-alist))
15768 (let ((org-add-colon-after-tag-completion t))
15770 (org-without-partial-completion
15771 (completing-read "Tags: " 'org-tags-completion-function
15772 nil nil current 'org-tags-history)))))))
15773 (while (string-match "[-+&]+" tags)
15774 ;; No boolean logic, just a list
15775 (setq tags (replace-match ":" t t tags))))
15777 (if (string-match "\\`[\t ]*\\'" tags)
15779 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
15780 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
15782 ;; Insert new tags at the correct column
15783 (beginning-of-line 1)
15785 ((and (equal current "") (equal tags "")))
15786 ((re-search-forward
15787 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
15789 (if (equal tags "")
15791 (goto-char (match-beginning 0))
15792 (setq c0 (current-column) p0 (point)
15793 c1 (max (1+ c0) (if (> org-tags-column 0)
15795 (- (- org-tags-column) (length tags))))
15796 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
15797 (replace-match rpl t t)
15798 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
15800 (t (error "Tags alignment failed")))
15801 (move-to-column col)
15803 (run-hooks 'org-after-tags-change-hook)))))
15805 (defun org-change-tag-in-region (beg end tag off)
15806 "Add or remove TAG for each entry in the region.
15807 This works in the agenda, and also in an org-mode buffer."
15809 (list (region-beginning) (region-end)
15810 (let ((org-last-tags-completion-table
15812 (org-get-buffer-tags)
15813 (org-global-tags-completion-table))))
15815 "Tag: " 'org-tags-completion-function nil nil nil
15816 'org-tags-history))
15818 (message "[s]et or [r]emove? ")
15819 (equal (read-char-exclusive) ?r))))
15820 (if (fboundp 'deactivate-mark) (deactivate-mark))
15821 (let ((agendap (equal major-mode 'org-agenda-mode))
15822 l1 l2 m buf pos newhead (cnt 0))
15824 (setq l2 (1- (org-current-line)))
15826 (setq l1 (org-current-line))
15827 (loop for l from l1 to l2 do
15829 (setq m (get-text-property (point) 'org-hd-marker))
15830 (when (or (and (org-mode-p) (org-on-heading-p))
15832 (setq buf (if agendap (marker-buffer m) (current-buffer))
15833 pos (if agendap m (point)))
15834 (with-current-buffer buf
15838 (setq cnt (1+ cnt))
15839 (org-toggle-tag tag (if off 'off 'on))
15840 (setq newhead (org-get-heading)))))
15841 (and agendap (org-agenda-change-all-lines newhead m))))
15842 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
15844 (defun org-tags-completion-function (string predicate &optional flag)
15845 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
15846 (confirm (lambda (x) (stringp (car x)))))
15847 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
15848 (setq s1 (match-string 1 string)
15849 s2 (match-string 2 string))
15850 (setq s1 "" s2 string))
15854 (setq rtn (try-completion s2 ctable confirm))
15857 (concat s1 s2 (substring rtn (length s2))
15858 (if (and org-add-colon-after-tag-completion
15859 (assoc rtn ctable))
15864 (all-completions s2 ctable confirm)
15868 (assoc s2 ctable)))
15871 (defun org-fast-tag-insert (kwd tags face &optional end)
15872 "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
15873 (insert (format "%-12s" (concat kwd ":"))
15874 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
15877 (defun org-fast-tag-show-exit (flag)
15880 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
15881 (replace-match ""))
15884 (move-to-column (- (window-width) 19) t)
15885 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
15887 (defun org-set-current-tags-overlay (current prefix)
15888 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
15889 (if (featurep 'xemacs)
15890 (org-overlay-display org-tags-overlay (concat prefix s)
15891 'secondary-selection)
15892 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
15893 (org-overlay-display org-tags-overlay (concat prefix s)))))
15895 (defun org-fast-tag-selection (current inherited table &optional todo-table)
15896 "Fast tag selection with single keys.
15897 CURRENT is the current list of tags in the headline, INHERITED is the
15898 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
15899 possibly with grouping information. TODO-TABLE is a similar table with
15900 TODO keywords, should these have keys assigned to them.
15901 If the keys are nil, a-z are automatically assigned.
15902 Returns the new tags string, or nil to not change the current settings."
15903 (let* ((fulltable (append table todo-table))
15904 (maxlen (apply 'max (mapcar
15906 (if (stringp (car x)) (string-width (car x)) 0))
15908 (buf (current-buffer))
15909 (expert (eq org-fast-tag-selection-single-key 'expert))
15911 (fwidth (+ maxlen 3 1 3))
15912 (ncol (/ (- (window-width) 4) fwidth))
15915 tg cnt e c char c1 c2 ntable tbl rtn
15916 ov-start ov-end ov-prefix
15917 (exit-after-next org-fast-tag-selection-single-key)
15918 (done-keywords org-done-keywords)
15921 (beginning-of-line 1)
15923 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
15924 (setq ov-start (match-beginning 1)
15925 ov-end (match-end 1)
15927 (setq ov-start (1- (point-at-eol))
15928 ov-end (1+ ov-start))
15929 (skip-chars-forward "^\n\r")
15932 (buffer-substring (1- (point)) (point))
15933 (if (> (current-column) org-tags-column)
15935 (make-string (- org-tags-column (current-column)) ?\ ))))))
15936 (org-move-overlay org-tags-overlay ov-start ov-end)
15937 (save-window-excursion
15939 (set-buffer (get-buffer-create " *Org tags*"))
15940 (delete-other-windows)
15941 (split-window-vertically)
15942 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
15944 (org-set-local 'org-done-keywords done-keywords)
15945 (org-fast-tag-insert "Inherited" inherited i-face "\n")
15946 (org-fast-tag-insert "Current" current c-face "\n\n")
15947 (org-fast-tag-show-exit exit-after-next)
15948 (org-set-current-tags-overlay current ov-prefix)
15949 (setq tbl fulltable char ?a cnt 0)
15950 (while (setq e (pop tbl))
15952 ((equal e '(:startgroup))
15953 (push '() groups) (setq ingroup t)
15954 (when (not (= cnt 0))
15958 ((equal e '(:endgroup))
15959 (setq ingroup nil cnt 0)
15962 (setq tg (car e) c2 nil)
15965 ;; automatically assign a character.
15966 (setq c1 (string-to-char
15967 (downcase (substring
15968 tg (if (= (string-to-char tg) ?@) 1 0)))))
15969 (if (or (rassoc c1 ntable) (rassoc c1 table))
15970 (while (or (rassoc char ntable) (rassoc char table))
15971 (setq char (1+ char)))
15973 (setq c (or c2 char)))
15974 (if ingroup (push tg (car groups)))
15975 (setq tg (org-add-props tg nil 'face
15977 ((not (assoc tg table))
15978 (org-get-todo-face tg))
15979 ((member tg current) c-face)
15980 ((member tg inherited) i-face)
15982 (if (and (= cnt 0) (not ingroup)) (insert " "))
15983 (insert "[" c "] " tg (make-string
15984 (- fwidth 4 (length tg)) ?\ ))
15985 (push (cons tg c) ntable)
15986 (when (= (setq cnt (1+ cnt)) ncol)
15988 (if ingroup (insert " "))
15990 (setq ntable (nreverse ntable))
15992 (goto-char (point-min))
15993 (if (and (not expert) (fboundp 'fit-window-to-buffer))
15994 (fit-window-to-buffer))
15998 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
15999 (if groups " [!] no groups" " [!]groups")
16000 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
16001 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
16003 ((= c ?\r) (throw 'exit t))
16005 (setq groups (not groups))
16006 (goto-char (point-min))
16007 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
16010 (org-fast-tag-show-exit
16011 (setq exit-after-next (not exit-after-next)))
16013 (delete-other-windows)
16014 (split-window-vertically)
16015 (org-switch-to-buffer-other-window " *Org tags*")
16016 (and (fboundp 'fit-window-to-buffer)
16017 (fit-window-to-buffer))))
16019 (and (= c ?q) (not (rassoc c ntable))))
16020 (org-detach-overlay org-tags-overlay)
16021 (setq quit-flag t))
16024 (if exit-after-next (setq exit-after-next 'now)))
16026 (condition-case nil
16027 (setq tg (completing-read
16030 (with-current-buffer buf
16031 (org-get-buffer-tags)))))
16032 (quit (setq tg "")))
16033 (when (string-match "\\S-" tg)
16034 (add-to-list 'buffer-tags (list tg))
16035 (if (member tg current)
16036 (setq current (delete tg current))
16037 (push tg current)))
16038 (if exit-after-next (setq exit-after-next 'now)))
16039 ((setq e (rassoc c todo-table) tg (car e))
16040 (with-current-buffer buf
16041 (save-excursion (org-todo tg)))
16042 (if exit-after-next (setq exit-after-next 'now)))
16043 ((setq e (rassoc c ntable) tg (car e))
16044 (if (member tg current)
16045 (setq current (delete tg current))
16046 (loop for g in groups do
16049 (setq current (delete x current)))
16052 (if exit-after-next (setq exit-after-next 'now))))
16054 ;; Create a sorted list
16058 (assoc b (cdr (memq (assoc a ntable) ntable))))))
16059 (if (eq exit-after-next 'now) (throw 'exit t))
16060 (goto-char (point-min))
16061 (beginning-of-line 2)
16062 (delete-region (point) (point-at-eol))
16063 (org-fast-tag-insert "Current" current c-face)
16064 (org-set-current-tags-overlay current ov-prefix)
16065 (while (re-search-forward
16066 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
16067 (setq tg (match-string 1))
16068 (add-text-properties
16069 (match-beginning 1) (match-end 1)
16072 ((member tg current) c-face)
16073 ((member tg inherited) i-face)
16074 (t (get-text-property (match-beginning 1) 'face))))))
16075 (goto-char (point-min)))))
16076 (org-detach-overlay org-tags-overlay)
16078 (mapconcat 'identity current ":")
16081 (defun org-get-tags-string ()
16082 "Get the TAGS string in the current headline."
16083 (unless (org-on-heading-p t)
16084 (error "Not on a heading"))
16086 (beginning-of-line 1)
16087 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
16088 (org-match-string-no-properties 1)
16091 (defun org-get-tags ()
16092 "Get the list of tags specified in the current headline."
16093 (org-split-string (org-get-tags-string) ":"))
16095 (defun org-get-buffer-tags ()
16096 "Get a table of all tags used in the buffer, for completion."
16099 (goto-char (point-min))
16100 (while (re-search-forward
16101 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
16102 (when (equal (char-after (point-at-bol 0)) ?*)
16103 (mapc (lambda (x) (add-to-list 'tags x))
16104 (org-split-string (org-match-string-no-properties 1) ":")))))
16105 (mapcar 'list tags)))
16110 ;;; Setting and retrieving properties
16112 (defconst org-special-properties
16113 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
16114 "TIMESTAMP" "TIMESTAMP_IA")
16115 "The special properties valid in Org-mode.
16117 These are properties that are not defined in the property drawer,
16118 but in some other way.")
16120 (defconst org-default-properties
16121 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
16122 "LOCATION" "LOGGING" "COLUMNS")
16123 "Some properties that are used by Org-mode for various purposes.
16124 Being in this list makes sure that they are offered for completion.")
16126 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
16127 "Regular expression matching the first line of a property drawer.")
16129 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
16130 "Regular expression matching the first line of a property drawer.")
16132 (defun org-property-action ()
16133 "Do an action on properties."
16136 (org-at-property-p)
16137 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
16138 (setq c (read-char-exclusive))
16141 (call-interactively 'org-set-property))
16143 (call-interactively 'org-delete-property))
16145 (call-interactively 'org-delete-property-globally))
16147 (call-interactively 'org-compute-property-at-point))
16148 (t (error "No such property action %c" c)))))
16150 (defun org-at-property-p ()
16151 "Is the cursor in a property line?"
16152 ;; FIXME: Does not check if we are actually in the drawer.
16153 ;; FIXME: also returns true on any drawers.....
16154 ;; This is used by C-c C-c for property action.
16156 (beginning-of-line 1)
16157 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
16159 (defmacro org-with-point-at (pom &rest body)
16160 "Move to buffer and point of point-or-marker POM for the duration of BODY."
16161 (declare (indent 1) (debug t))
16163 (if (markerp pom) (set-buffer (marker-buffer pom)))
16165 (goto-char (or pom (point)))
16168 (defun org-get-property-block (&optional beg end force)
16169 "Return the (beg . end) range of the body of the property drawer.
16170 BEG and END can be beginning and end of subtree, if not given
16171 they will be found.
16172 If the drawer does not exist and FORCE is non-nil, create the drawer."
16175 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
16176 (end (or end (progn (outline-next-heading) (point)))))
16178 (if (re-search-forward org-property-start-re end t)
16179 (setq beg (1+ (match-end 0)))
16182 (org-insert-property-drawer)
16183 (setq end (progn (outline-next-heading) (point))))
16186 (if (re-search-forward org-property-start-re end t)
16187 (setq beg (1+ (match-end 0)))))
16188 (if (re-search-forward org-property-end-re end t)
16189 (setq end (match-beginning 0))
16190 (or force (throw 'exit nil))
16193 (org-indent-line-function)
16194 (insert ":END:\n"))
16197 (defun org-entry-properties (&optional pom which)
16198 "Get all properties of the entry at point-or-marker POM.
16199 This includes the TODO keyword, the tags, time strings for deadline,
16200 scheduled, and clocking, and any additional properties defined in the
16201 entry. The return value is an alist, keys may occur multiple times
16202 if the property key was used several times.
16203 POM may also be nil, in which case the current entry is used.
16204 If WHICH is nil or `all', get all properties. If WHICH is
16205 `special' or `standard', only get that subclass."
16206 (setq which (or which 'all))
16207 (org-with-point-at pom
16208 (let ((clockstr (substring org-clock-string 0 -1))
16209 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
16210 beg end range props sum-props key value string clocksum)
16212 (when (condition-case nil (org-back-to-heading t) (error nil))
16214 (setq sum-props (get-text-property (point) 'org-summaries))
16215 (setq clocksum (get-text-property (point) :org-clock-minutes))
16216 (outline-next-heading)
16218 (when (memq which '(all special))
16219 ;; Get the special properties, like TODO and tags
16221 (when (and (looking-at org-todo-line-regexp) (match-end 2))
16222 (push (cons "TODO" (org-match-string-no-properties 2)) props))
16223 (when (looking-at org-priority-regexp)
16224 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
16225 (when (and (setq value (org-get-tags-string))
16226 (string-match "\\S-" value))
16227 (push (cons "TAGS" value) props))
16228 (when (setq value (org-get-tags-at))
16229 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
16231 (while (re-search-forward org-maybe-keyword-time-regexp end t)
16232 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
16233 string (if (equal key clockstr)
16237 (match-beginning 3) (goto-char (point-at-eol)))))
16238 (substring (org-match-string-no-properties 3) 1 -1)))
16240 (if (= (char-after (match-beginning 3)) ?\[)
16241 (setq key "TIMESTAMP_IA")
16242 (setq key "TIMESTAMP")))
16243 (when (or (equal key clockstr) (not (assoc key props)))
16244 (push (cons key string) props)))
16248 (when (memq which '(all standard))
16249 ;; Get the standard properties, like :PORP: ...
16250 (setq range (org-get-property-block beg end))
16252 (goto-char (car range))
16253 (while (re-search-forward
16254 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
16256 (setq key (org-match-string-no-properties 1)
16257 value (org-trim (or (org-match-string-no-properties 2) "")))
16258 (unless (member key excluded)
16259 (push (cons key (or value "")) props)))))
16261 (push (cons "CLOCKSUM"
16262 (org-column-number-to-string (/ (float clocksum) 60.)
16265 (append sum-props (nreverse props)))))))
16267 (defun org-entry-get (pom property &optional inherit)
16268 "Get value of PROPERTY for entry at point-or-marker POM.
16269 If INHERIT is non-nil and the entry does not have the property,
16270 then also check higher levels of the hierarchy.
16271 If the property is present but empty, the return value is the empty string.
16272 If the property is not present at all, nil is returned."
16273 (org-with-point-at pom
16275 (org-entry-get-with-inheritance property)
16276 (if (member property org-special-properties)
16277 ;; We need a special property. Use brute force, get all properties.
16278 (cdr (assoc property (org-entry-properties nil 'special)))
16279 (let ((range (org-get-property-block)))
16281 (goto-char (car range))
16283 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
16285 ;; Found the property, return it.
16287 (org-match-string-no-properties 1)
16290 (defun org-entry-delete (pom property)
16291 "Delete the property PROPERTY from entry at point-or-marker POM."
16292 (org-with-point-at pom
16293 (if (member property org-special-properties)
16294 nil ; cannot delete these properties.
16295 (let ((range (org-get-property-block)))
16297 (goto-char (car range))
16299 (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
16302 (delete-region (match-beginning 0) (1+ (point-at-eol)))
16306 ;; Multi-values properties are properties that contain multiple values
16307 ;; These values are assumed to be single words, separated by whitespace.
16308 (defun org-entry-add-to-multivalued-property (pom property value)
16309 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
16310 (let* ((old (org-entry-get pom property))
16311 (values (and old (org-split-string old "[ \t]"))))
16312 (unless (member value values)
16313 (setq values (cons value values))
16314 (org-entry-put pom property
16315 (mapconcat 'identity values " ")))))
16317 (defun org-entry-remove-from-multivalued-property (pom property value)
16318 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
16319 (let* ((old (org-entry-get pom property))
16320 (values (and old (org-split-string old "[ \t]"))))
16321 (when (member value values)
16322 (setq values (delete value values))
16323 (org-entry-put pom property
16324 (mapconcat 'identity values " ")))))
16326 (defun org-entry-member-in-multivalued-property (pom property value)
16327 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
16328 (let* ((old (org-entry-get pom property))
16329 (values (and old (org-split-string old "[ \t]"))))
16330 (member value values)))
16332 (defvar org-entry-property-inherited-from (make-marker))
16334 (defun org-entry-get-with-inheritance (property)
16335 "Get entry property, and search higher levels if not present."
16342 (when (setq tmp (org-entry-get nil property))
16343 (org-back-to-heading t)
16344 (move-marker org-entry-property-inherited-from (point))
16346 (or (org-up-heading-safe) (throw 'ex nil)))))
16347 (or tmp (cdr (assoc property org-local-properties))
16348 (cdr (assoc property org-global-properties))))))
16350 (defun org-entry-put (pom property value)
16351 "Set PROPERTY to VALUE for entry at point-or-marker POM."
16352 (org-with-point-at pom
16353 (org-back-to-heading t)
16354 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
16357 ((equal property "TODO")
16358 (when (and (stringp value) (string-match "\\S-" value)
16359 (not (member value org-todo-keywords-1)))
16360 (error "\"%s\" is not a valid TODO state" value))
16361 (if (or (not value)
16362 (not (string-match "\\S-" value)))
16363 (setq value 'none))
16365 (org-set-tags nil 'align))
16366 ((equal property "PRIORITY")
16367 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
16368 (string-to-char value) ?\ ))
16369 (org-set-tags nil 'align))
16370 ((equal property "SCHEDULED")
16371 (if (re-search-forward org-scheduled-time-regexp end t)
16373 ((eq value 'earlier) (org-timestamp-change -1 'day))
16374 ((eq value 'later) (org-timestamp-change 1 'day))
16375 (t (call-interactively 'org-schedule)))
16376 (call-interactively 'org-schedule)))
16377 ((equal property "DEADLINE")
16378 (if (re-search-forward org-deadline-time-regexp end t)
16380 ((eq value 'earlier) (org-timestamp-change -1 'day))
16381 ((eq value 'later) (org-timestamp-change 1 'day))
16382 (t (call-interactively 'org-deadline)))
16383 (call-interactively 'org-deadline)))
16384 ((member property org-special-properties)
16385 (error "The %s property can not yet be set with `org-entry-put'"
16387 (t ; a non-special property
16388 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
16389 (setq range (org-get-property-block beg end 'force))
16390 (goto-char (car range))
16391 (if (re-search-forward
16392 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
16394 (delete-region (match-beginning 1) (match-end 1))
16395 (goto-char (match-beginning 1)))
16396 (goto-char (cdr range))
16399 (org-indent-line-function)
16400 (insert ":" property ":"))
16401 (and value (insert " " value))
16402 (org-indent-line-function)))))))
16404 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
16405 "Get all property keys in the current buffer.
16406 With INCLUDE-SPECIALS, also list the special properties that relect things
16407 like tags and TODO state.
16408 With INCLUDE-DEFAULTS, also include properties that has special meaning
16409 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
16410 With INCLUDE-COLUMNS, also include property names given in COLUMN
16411 formats in the current buffer."
16412 (let (rtn range cfmt cols s p)
16416 (goto-char (point-min))
16417 (while (re-search-forward org-property-start-re nil t)
16418 (setq range (org-get-property-block))
16419 (goto-char (car range))
16420 (while (re-search-forward
16421 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
16423 (add-to-list 'rtn (org-match-string-no-properties 1)))
16424 (outline-next-heading))))
16426 (when include-specials
16427 (setq rtn (append org-special-properties rtn)))
16429 (when include-defaults
16430 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
16432 (when include-columns
16436 (goto-char (point-min))
16437 (while (re-search-forward
16438 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
16440 (setq cfmt (match-string 2) s 0)
16441 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
16443 (setq s (match-end 0)
16444 p (match-string 1 cfmt))
16445 (unless (or (equal p "ITEM")
16446 (member p org-special-properties))
16447 (add-to-list 'rtn (match-string 1 cfmt))))))))
16449 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
16451 (defun org-property-values (key)
16452 "Return a list of all values of property KEY."
16456 (goto-char (point-min))
16457 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
16459 (while (re-search-forward re nil t)
16460 (add-to-list 'values (org-trim (match-string 1))))
16461 (delete "" values)))))
16463 (defun org-insert-property-drawer ()
16464 "Insert a property drawer into the current entry."
16466 (org-back-to-heading t)
16467 (looking-at outline-regexp)
16468 (let ((indent (- (match-end 0)(match-beginning 0)))
16470 (re (concat "^[ \t]*" org-keyword-time-regexp))
16472 (outline-next-heading)
16475 (while (re-search-forward re end t))
16476 (setq hiddenp (org-invisible-p))
16478 (and (equal (char-after) ?\n) (forward-char 1))
16479 (org-skip-over-state-notes)
16480 (skip-chars-backward " \t\n\r")
16481 (if (eq (char-before) ?*) (forward-char 1))
16482 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
16483 (beginning-of-line 0)
16484 (indent-to-column indent)
16485 (beginning-of-line 2)
16486 (indent-to-column indent)
16487 (beginning-of-line 0)
16490 (org-back-to-heading t)
16492 (org-flag-drawer t))))
16494 (defun org-set-property (property value)
16495 "In the current entry, set PROPERTY to VALUE.
16496 When called interactively, this will prompt for a property name, offering
16497 completion on existing and default properties. And then it will prompt
16498 for a value, offering competion either on allowed values (via an inherited
16499 xxx_ALL property) or on existing values in other instances of this property
16500 in the current file."
16502 (let* ((prop (completing-read
16503 "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
16504 (cur (org-entry-get nil prop))
16505 (allowed (org-property-get-allowed-values nil prop 'table))
16506 (existing (mapcar 'list (org-property-values prop)))
16508 (completing-read "Value: " allowed nil 'req-match)
16510 (concat "Value" (if (and cur (string-match "\\S-" cur))
16511 (concat "[" cur "]") "")
16513 existing nil nil "" nil cur))))
16514 (list prop (if (equal val "") cur val))))
16515 (unless (equal (org-entry-get nil property) value)
16516 (org-entry-put nil property value)))
16518 (defun org-delete-property (property)
16519 "In the current entry, delete PROPERTY."
16521 (let* ((prop (completing-read
16522 "Property: " (org-entry-properties nil 'standard))))
16524 (message "Property %s %s" property
16525 (if (org-entry-delete nil property)
16527 "was not present in the entry")))
16529 (defun org-delete-property-globally (property)
16530 "Remove PROPERTY globally, from all entries."
16532 (let* ((prop (completing-read
16533 "Globally remove property: "
16534 (mapcar 'list (org-buffer-property-keys)))))
16539 (goto-char (point-min))
16541 (while (re-search-forward
16542 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
16544 (setq cnt (1+ cnt))
16545 (replace-match ""))
16546 (message "Property \"%s\" removed from %d entries" property cnt)))))
16548 (defvar org-columns-current-fmt-compiled) ; defined below
16550 (defun org-compute-property-at-point ()
16551 "Compute the property at point.
16552 This looks for an enclosing column format, extracts the operator and
16553 then applies it to the proerty in the column format's scope."
16555 (unless (org-at-property-p)
16556 (error "Not at a property"))
16557 (let ((prop (org-match-string-no-properties 2)))
16558 (org-columns-get-format-and-top-level)
16559 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
16560 (error "No operator defined for property %s" prop))
16561 (org-columns-compute prop)))
16563 (defun org-property-get-allowed-values (pom property &optional table)
16564 "Get allowed values for the property PROPERTY.
16565 When TABLE is non-nil, return an alist that can directly be used for
16569 ((equal property "TODO")
16570 (setq vals (org-with-point-at pom
16571 (append org-todo-keywords-1 '("")))))
16572 ((equal property "PRIORITY")
16573 (let ((n org-lowest-priority))
16574 (while (>= n org-highest-priority)
16575 (push (char-to-string n) vals)
16577 ((member property org-special-properties))
16579 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
16581 (when (and vals (string-match "\\S-" vals))
16582 (setq vals (car (read-from-string (concat "(" vals ")"))))
16583 (setq vals (mapcar (lambda (x)
16584 (cond ((stringp x) x)
16585 ((numberp x) (number-to-string x))
16586 ((symbolp x) (symbol-name x))
16589 (if table (mapcar 'list vals) vals)))
16591 (defun org-property-previous-allowed-value (&optional previous)
16592 "Switch to the next allowed value for this property."
16594 (org-property-next-allowed-value t))
16596 (defun org-property-next-allowed-value (&optional previous)
16597 "Switch to the next allowed value for this property."
16599 (unless (org-at-property-p)
16600 (error "Not at a property"))
16601 (let* ((key (match-string 2))
16602 (value (match-string 3))
16603 (allowed (or (org-property-get-allowed-values (point) key)
16604 (and (member value '("[ ]" "[-]" "[X]"))
16608 (error "Allowed values for this property have not been defined"))
16609 (if previous (setq allowed (reverse allowed)))
16610 (if (member value allowed)
16611 (setq nval (car (cdr (member value allowed)))))
16612 (setq nval (or nval (car allowed)))
16613 (if (equal nval value)
16614 (error "Only one allowed value for this property"))
16615 (org-at-property-p)
16616 (replace-match (concat " :" key ": " nval) t t)
16617 (org-indent-line-function)
16618 (beginning-of-line 1)
16619 (skip-chars-forward " \t")))
16621 (defun org-find-entry-with-id (ident)
16622 "Locate the entry that contains the ID property with exact value IDENT.
16623 IDENT can be a string, a symbol or a number, this function will search for
16624 the string representation of it.
16625 Return the position where this entry starts, or nil if there is no such entry."
16627 ((stringp ident) ident)
16628 ((symbol-name ident) (symbol-name ident))
16629 ((numberp ident) (number-to-string ident))
16630 (t (error "IDENT %s must be a string, symbol or number" ident))))
16631 (case-fold-search nil))
16635 (goto-char (point-min))
16636 (when (re-search-forward
16637 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
16639 (org-back-to-heading)
16644 (defvar org-columns-overlays nil
16645 "Holds the list of current column overlays.")
16647 (defvar org-columns-current-fmt nil
16648 "Local variable, holds the currently active column format.")
16649 (defvar org-columns-current-fmt-compiled nil
16650 "Local variable, holds the currently active column format.
16651 This is the compiled version of the format.")
16652 (defvar org-columns-current-widths nil
16653 "Loval variable, holds the currently widths of fields.")
16654 (defvar org-columns-current-maxwidths nil
16655 "Loval variable, holds the currently active maximum column widths.")
16656 (defvar org-columns-begin-marker (make-marker)
16657 "Points to the position where last a column creation command was called.")
16658 (defvar org-columns-top-level-marker (make-marker)
16659 "Points to the position where current columns region starts.")
16661 (defvar org-columns-map (make-sparse-keymap)
16662 "The keymap valid in column display.")
16664 (defun org-columns-content ()
16665 "Switch to contents view while in columns view."
16670 (org-defkey org-columns-map "c" 'org-columns-content)
16671 (org-defkey org-columns-map "o" 'org-overview)
16672 (org-defkey org-columns-map "e" 'org-columns-edit-value)
16673 (org-defkey org-columns-map "\C-c\C-t" 'org-columns-todo)
16674 (org-defkey org-columns-map "\C-c\C-c" 'org-columns-set-tags-or-toggle)
16675 (org-defkey org-columns-map "\C-c\C-o" 'org-columns-open-link)
16676 (org-defkey org-columns-map "v" 'org-columns-show-value)
16677 (org-defkey org-columns-map "q" 'org-columns-quit)
16678 (org-defkey org-columns-map "r" 'org-columns-redo)
16679 (org-defkey org-columns-map "g" 'org-columns-redo)
16680 (org-defkey org-columns-map [left] 'backward-char)
16681 (org-defkey org-columns-map "\M-b" 'backward-char)
16682 (org-defkey org-columns-map "a" 'org-columns-edit-allowed)
16683 (org-defkey org-columns-map "s" 'org-columns-edit-attributes)
16684 (org-defkey org-columns-map "\M-f" (lambda () (interactive) (goto-char (1+ (point)))))
16685 (org-defkey org-columns-map [right] (lambda () (interactive) (goto-char (1+ (point)))))
16686 (org-defkey org-columns-map [(shift right)] 'org-columns-next-allowed-value)
16687 (org-defkey org-columns-map "n" 'org-columns-next-allowed-value)
16688 (org-defkey org-columns-map [(shift left)] 'org-columns-previous-allowed-value)
16689 (org-defkey org-columns-map "p" 'org-columns-previous-allowed-value)
16690 (org-defkey org-columns-map "<" 'org-columns-narrow)
16691 (org-defkey org-columns-map ">" 'org-columns-widen)
16692 (org-defkey org-columns-map [(meta right)] 'org-columns-move-right)
16693 (org-defkey org-columns-map [(meta left)] 'org-columns-move-left)
16694 (org-defkey org-columns-map [(shift meta right)] 'org-columns-new)
16695 (org-defkey org-columns-map [(shift meta left)] 'org-columns-delete)
16697 (easy-menu-define org-columns-menu org-columns-map "Org Column Menu"
16699 ["Edit property" org-columns-edit-value t]
16700 ["Next allowed value" org-columns-next-allowed-value t]
16701 ["Previous allowed value" org-columns-previous-allowed-value t]
16702 ["Show full value" org-columns-show-value t]
16703 ["Edit allowed values" org-columns-edit-allowed t]
16705 ["Edit column attributes" org-columns-edit-attributes t]
16706 ["Increase column width" org-columns-widen t]
16707 ["Decrease column width" org-columns-narrow t]
16709 ["Move column right" org-columns-move-right t]
16710 ["Move column left" org-columns-move-left t]
16711 ["Add column" org-columns-new t]
16712 ["Delete column" org-columns-delete t]
16714 ["CONTENTS" org-columns-content t]
16715 ["OVERVIEW" org-overview t]
16716 ["Refresh columns display" org-columns-redo t]
16718 ["Open link" org-columns-open-link t]
16720 ["Quit" org-columns-quit t]))
16722 (defun org-columns-new-overlay (beg end &optional string face)
16723 "Create a new column overlay and add it to the list."
16724 (let ((ov (org-make-overlay beg end)))
16725 (org-overlay-put ov 'face (or face 'secondary-selection))
16726 (org-overlay-display ov string face)
16727 (push ov org-columns-overlays)
16730 (defun org-columns-display-here (&optional props)
16731 "Overlay the current line with column display."
16733 (let* ((fmt org-columns-current-fmt-compiled)
16734 (beg (point-at-bol))
16735 (level-face (save-excursion
16736 (beginning-of-line 1)
16737 (and (looking-at "\\(\\**\\)\\(\\* \\)")
16738 (org-get-level-face 2))))
16739 (color (list :foreground
16740 (face-attribute (or level-face 'default) :foreground)))
16741 props pom property ass width f string ov column val modval)
16742 ;; Check if the entry is in another buffer.
16744 (if (eq major-mode 'org-agenda-mode)
16745 (setq pom (or (get-text-property (point) 'org-hd-marker)
16746 (get-text-property (point) 'org-marker))
16747 props (if pom (org-entry-properties pom) nil))
16748 (setq props (org-entry-properties nil))))
16750 (while (setq column (pop fmt))
16751 (setq property (car column)
16752 ass (if (equal property "ITEM")
16757 (buffer-substring-no-properties
16758 (point-at-bol) (point-at-eol))))))
16759 (assoc property props))
16760 width (or (cdr (assoc property org-columns-current-maxwidths))
16763 f (format "%%-%d.%ds | " width width)
16764 val (or (cdr ass) "")
16765 modval (if (equal property "ITEM")
16766 (org-columns-cleanup-item val org-columns-current-fmt-compiled))
16767 string (format f (or modval val)))
16768 ;; Create the overlay
16770 (setq ov (org-columns-new-overlay
16771 beg (setq beg (1+ beg)) string
16772 (list color 'org-column)))
16773 ;;; (list (get-text-property (point-at-bol) 'face) 'org-column)))
16774 (org-overlay-put ov 'keymap org-columns-map)
16775 (org-overlay-put ov 'org-columns-key property)
16776 (org-overlay-put ov 'org-columns-value (cdr ass))
16777 (org-overlay-put ov 'org-columns-value-modified modval)
16778 (org-overlay-put ov 'org-columns-pom pom)
16779 (org-overlay-put ov 'org-columns-format f))
16780 (if (or (not (char-after beg))
16781 (equal (char-after beg) ?\n))
16782 (let ((inhibit-read-only t))
16785 (org-unmodified (insert " ")))))) ;; FIXME: add props and remove later?
16786 ;; Make the rest of the line disappear.
16788 (setq ov (org-columns-new-overlay beg (point-at-eol)))
16789 (org-overlay-put ov 'invisible t)
16790 (org-overlay-put ov 'keymap org-columns-map)
16791 (org-overlay-put ov 'intangible t)
16792 (push ov org-columns-overlays)
16793 (setq ov (org-make-overlay (1- (point-at-eol)) (1+ (point-at-eol))))
16794 (org-overlay-put ov 'keymap org-columns-map)
16795 (push ov org-columns-overlays)
16796 (let ((inhibit-read-only t))
16797 (put-text-property (max (point-min) (1- (point-at-bol)))
16798 (min (point-max) (1+ (point-at-eol)))
16799 'read-only "Type `e' to edit property")))))
16801 (defvar org-previous-header-line-format nil
16802 "The header line format before column view was turned on.")
16803 (defvar org-columns-inhibit-recalculation nil
16804 "Inhibit recomputing of columns on column view startup.")
16807 (defvar header-line-format)
16808 (defun org-columns-display-here-title ()
16809 "Overlay the newline before the current line with the table title."
16811 (let ((fmt org-columns-current-fmt-compiled)
16813 property width f column str widths)
16814 (while (setq column (pop fmt))
16815 (setq property (car column)
16816 str (or (nth 1 column) property)
16817 width (or (cdr (assoc property org-columns-current-maxwidths))
16820 widths (push width widths)
16821 f (format "%%-%d.%ds | " width width)
16822 string (format f str)
16823 title (concat title string)))
16824 (setq title (concat
16825 (org-add-props " " nil 'display '(space :align-to 0))
16826 (org-add-props title nil 'face '(:weight bold :underline t))))
16827 (org-set-local 'org-previous-header-line-format header-line-format)
16828 (org-set-local 'org-columns-current-widths (nreverse widths))
16829 (setq header-line-format title)))
16831 (defun org-columns-remove-overlays ()
16832 "Remove all currently active column overlays."
16834 (when (marker-buffer org-columns-begin-marker)
16835 (with-current-buffer (marker-buffer org-columns-begin-marker)
16836 (when (local-variable-p 'org-previous-header-line-format)
16837 (setq header-line-format org-previous-header-line-format)
16838 (kill-local-variable 'org-previous-header-line-format))
16839 (move-marker org-columns-begin-marker nil)
16840 (move-marker org-columns-top-level-marker nil)
16842 (mapc 'org-delete-overlay org-columns-overlays)
16843 (setq org-columns-overlays nil)
16844 (let ((inhibit-read-only t))
16845 (remove-text-properties (point-min) (point-max) '(read-only t)))))))
16847 (defun org-columns-cleanup-item (item fmt)
16848 "Remove from ITEM what is a column in the format FMT."
16849 (if (not org-complex-heading-regexp)
16851 (when (string-match org-complex-heading-regexp item)
16853 (org-add-props (concat (match-string 1 item) " ") nil
16854 'org-whitespace (* 2 (1- (org-reduced-level (- (match-end 1) (match-beginning 1))))))
16855 (and (match-end 2) (not (assoc "TODO" fmt)) (concat " " (match-string 2 item)))
16856 (and (match-end 3) (not (assoc "PRIORITY" fmt)) (concat " " (match-string 3 item)))
16857 " " (match-string 4 item)
16858 (and (match-end 5) (not (assoc "TAGS" fmt)) (concat " " (match-string 5 item)))))))
16860 (defun org-columns-show-value ()
16861 "Show the full value of the property."
16863 (let ((value (get-char-property (point) 'org-columns-value)))
16864 (message "Value is: %s" (or value ""))))
16866 (defun org-columns-quit ()
16867 "Remove the column overlays and in this way exit column editing."
16870 (org-columns-remove-overlays)
16871 (let ((inhibit-read-only t))
16872 (remove-text-properties (point-min) (point-max) '(read-only t))))
16873 (when (eq major-mode 'org-agenda-mode)
16875 "Modification not yet reflected in Agenda buffer, use `r' to refresh")))
16877 (defun org-columns-check-computed ()
16878 "Check if this column value is computed.
16879 If yes, throw an error indicating that changing it does not make sense."
16880 (let ((val (get-char-property (point) 'org-columns-value)))
16881 (when (and (stringp val)
16882 (get-char-property 0 'org-computed val))
16883 (error "This value is computed from the entry's children"))))
16885 (defun org-columns-todo (&optional arg)
16886 "Change the TODO state during column view."
16888 (org-columns-edit-value "TODO"))
16890 (defun org-columns-set-tags-or-toggle (&optional arg)
16891 "Toggle checkbox at point, or set tags for current headline."
16893 (if (string-match "\\`\\[[ xX-]\\]\\'"
16894 (get-char-property (point) 'org-columns-value))
16895 (org-columns-next-allowed-value)
16896 (org-columns-edit-value "TAGS")))
16898 (defun org-columns-edit-value (&optional key)
16899 "Edit the value of the property at point in column view.
16900 Where possible, use the standard interface for changing this line."
16902 (org-columns-check-computed)
16903 (let* ((external-key key)
16904 (col (current-column))
16905 (key (or key (get-char-property (point) 'org-columns-key)))
16906 (value (get-char-property (point) 'org-columns-value))
16907 (bol (point-at-bol)) (eol (point-at-eol))
16908 (pom (or (get-text-property bol 'org-hd-marker)
16909 (point))) ; keep despite of compiler waring
16911 (delq nil (mapcar (lambda (x)
16912 (and (eq (overlay-buffer x) (current-buffer))
16913 (>= (overlay-start x) bol)
16914 (<= (overlay-start x) eol)
16916 org-columns-overlays)))
16919 ((equal key "CLOCKSUM")
16920 (error "This special column cannot be edited"))
16921 ((equal key "ITEM")
16922 (setq eval '(org-with-point-at pom
16923 (org-edit-headline))))
16924 ((equal key "TODO")
16925 (setq eval '(org-with-point-at pom
16926 (let ((current-prefix-arg
16927 (if external-key current-prefix-arg '(4))))
16928 (call-interactively 'org-todo)))))
16929 ((equal key "PRIORITY")
16930 (setq eval '(org-with-point-at pom
16931 (call-interactively 'org-priority))))
16932 ((equal key "TAGS")
16933 (setq eval '(org-with-point-at pom
16934 (let ((org-fast-tag-selection-single-key
16935 (if (eq org-fast-tag-selection-single-key 'expert)
16936 t org-fast-tag-selection-single-key)))
16937 (call-interactively 'org-set-tags)))))
16938 ((equal key "DEADLINE")
16939 (setq eval '(org-with-point-at pom
16940 (call-interactively 'org-deadline))))
16941 ((equal key "SCHEDULED")
16942 (setq eval '(org-with-point-at pom
16943 (call-interactively 'org-schedule))))
16945 (setq allowed (org-property-get-allowed-values pom key 'table))
16947 (setq nval (completing-read "Value: " allowed nil t))
16948 (setq nval (read-string "Edit: " value)))
16949 (setq nval (org-trim nval))
16950 (when (not (equal nval value))
16951 (setq eval '(org-entry-put pom key nval)))))
16953 (let ((inhibit-read-only t))
16954 (remove-text-properties (max (point-min) (1- bol)) eol '(read-only t))
16957 (setq org-columns-overlays
16958 (org-delete-all line-overlays org-columns-overlays))
16959 (mapc 'org-delete-overlay line-overlays)
16960 (org-columns-eval eval))
16961 (org-columns-display-here))))
16962 (move-to-column col)
16963 (if (and (org-mode-p)
16964 (nth 3 (assoc key org-columns-current-fmt-compiled)))
16965 (org-columns-update key))))
16967 (defun org-edit-headline () ; FIXME: this is not columns specific
16968 "Edit the current headline, the part without TODO keyword, TAGS."
16969 (org-back-to-heading)
16970 (when (looking-at org-todo-line-regexp)
16971 (let ((pre (buffer-substring (match-beginning 0) (match-beginning 3)))
16972 (txt (match-string 3))
16975 (if (string-match (org-re "[ \t]+:[[:alnum:]:_@]+:[ \t]*$") txt)
16976 (setq post (match-string 0 txt)
16977 txt (substring txt 0 (match-beginning 0))))
16978 (setq txt2 (read-string "Edit: " txt))
16979 (when (not (equal txt txt2))
16980 (beginning-of-line 1)
16981 (insert pre txt2 post)
16982 (delete-region (point) (point-at-eol))
16983 (org-set-tags nil t)))))
16985 (defun org-columns-edit-allowed ()
16986 "Edit the list of allowed values for the current property."
16988 (let* ((key (get-char-property (point) 'org-columns-key))
16989 (key1 (concat key "_ALL"))
16990 (allowed (org-entry-get (point) key1 t))
16992 ;; FIXME: Cover editing TODO, TAGS etc in-buffer settings.????
16993 (setq nval (read-string "Allowed: " allowed))
16995 (cond ((marker-position org-entry-property-inherited-from)
16996 org-entry-property-inherited-from)
16997 ((marker-position org-columns-top-level-marker)
16998 org-columns-top-level-marker))
17001 (defmacro org-no-warnings (&rest body)
17002 (cons (if (fboundp 'with-no-warnings) 'with-no-warnings 'progn) body))
17004 (defun org-columns-eval (form)
17007 (beginning-of-line 1)
17008 ;; `next-line' is needed here, because it skips invisible line.
17009 (condition-case nil (org-no-warnings (next-line 1)) (error nil))
17010 (setq hidep (org-on-heading-p 1)))
17012 (and hidep (hide-entry))))
17014 (defun org-columns-previous-allowed-value ()
17015 "Switch to the previous allowed value for this column."
17017 (org-columns-next-allowed-value t))
17019 (defun org-columns-next-allowed-value (&optional previous)
17020 "Switch to the next allowed value for this column."
17022 (org-columns-check-computed)
17023 (let* ((col (current-column))
17024 (key (get-char-property (point) 'org-columns-key))
17025 (value (get-char-property (point) 'org-columns-value))
17026 (bol (point-at-bol)) (eol (point-at-eol))
17027 (pom (or (get-text-property bol 'org-hd-marker)
17028 (point))) ; keep despite of compiler waring
17030 (delq nil (mapcar (lambda (x)
17031 (and (eq (overlay-buffer x) (current-buffer))
17032 (>= (overlay-start x) bol)
17033 (<= (overlay-start x) eol)
17035 org-columns-overlays)))
17036 (allowed (or (org-property-get-allowed-values pom key)
17038 (nth 4 (assoc key org-columns-current-fmt-compiled))
17039 '(checkbox checkbox-n-of-m checkbox-percent))
17042 (when (equal key "ITEM")
17043 (error "Cannot edit item headline from here"))
17044 (unless (or allowed (member key '("SCHEDULED" "DEADLINE")))
17045 (error "Allowed values for this property have not been defined"))
17046 (if (member key '("SCHEDULED" "DEADLINE"))
17047 (setq nval (if previous 'earlier 'later))
17048 (if previous (setq allowed (reverse allowed)))
17049 (if (member value allowed)
17050 (setq nval (car (cdr (member value allowed)))))
17051 (setq nval (or nval (car allowed)))
17052 (if (equal nval value)
17053 (error "Only one allowed value for this property")))
17054 (let ((inhibit-read-only t))
17055 (remove-text-properties (1- bol) eol '(read-only t))
17058 (setq org-columns-overlays
17059 (org-delete-all line-overlays org-columns-overlays))
17060 (mapc 'org-delete-overlay line-overlays)
17061 (org-columns-eval '(org-entry-put pom key nval)))
17062 (org-columns-display-here)))
17063 (move-to-column col)
17064 (if (and (org-mode-p)
17065 (nth 3 (assoc key org-columns-current-fmt-compiled)))
17066 (org-columns-update key))))
17068 (defun org-verify-version (task)
17070 ((eq task 'columns)
17071 (if (or (featurep 'xemacs)
17072 (< emacs-major-version 22))
17073 (error "Emacs 22 is required for the columns feature")))))
17075 (defun org-columns-open-link (&optional arg)
17077 (let ((value (get-char-property (point) 'org-columns-value)))
17078 (org-open-link-from-string value arg)))
17080 (defun org-open-link-from-string (s &optional arg)
17081 "Open a link in the string S, as if it was in Org-mode."
17084 (let ((org-inhibit-startup t))
17087 (goto-char (point-min))
17088 (org-open-at-point arg))))
17090 (defun org-columns-get-format-and-top-level ()
17092 (when (condition-case nil (org-back-to-heading) (error nil))
17093 (move-marker org-entry-property-inherited-from nil)
17094 (setq fmt (org-entry-get nil "COLUMNS" t)))
17095 (setq fmt (or fmt org-columns-default-format))
17096 (org-set-local 'org-columns-current-fmt fmt)
17097 (org-columns-compile-format fmt)
17098 (if (marker-position org-entry-property-inherited-from)
17099 (move-marker org-columns-top-level-marker
17100 org-entry-property-inherited-from)
17101 (move-marker org-columns-top-level-marker (point)))
17104 (defun org-columns ()
17105 "Turn on column view on an org-mode file."
17107 (org-verify-version 'columns)
17108 (org-columns-remove-overlays)
17109 (move-marker org-columns-begin-marker (point))
17110 (let (beg end fmt cache maxwidths)
17111 (setq fmt (org-columns-get-format-and-top-level))
17113 (goto-char org-columns-top-level-marker)
17115 (unless org-columns-inhibit-recalculation
17116 (org-columns-compute-all))
17117 (setq end (or (condition-case nil (org-end-of-subtree t t) (error nil))
17119 ;; Get and cache the properties
17121 (when (assoc "CLOCKSUM" org-columns-current-fmt-compiled)
17124 (narrow-to-region beg end)
17126 (while (re-search-forward (concat "^" outline-regexp) end t)
17127 (push (cons (org-current-line) (org-entry-properties)) cache))
17129 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
17130 (org-set-local 'org-columns-current-maxwidths maxwidths)
17131 (org-columns-display-here-title)
17133 (goto-line (car x))
17134 (org-columns-display-here (cdr x)))
17137 (defun org-columns-new (&optional prop title width op fmt &rest rest)
17138 "Insert a new column, to the leeft o the current column."
17140 (let ((editp (and prop (assoc prop org-columns-current-fmt-compiled)))
17142 (setq prop (completing-read
17143 "Property: " (mapcar 'list (org-buffer-property-keys t nil t))
17145 (setq title (read-string (concat "Column title [" prop "]: ") (or title prop)))
17146 (setq width (read-string "Column width: " (if width (number-to-string width))))
17147 (if (string-match "\\S-" width)
17148 (setq width (string-to-number width))
17150 (setq fmt (completing-read "Summary [none]: "
17151 '(("none") ("add_numbers") ("currency") ("add_times") ("checkbox") ("checkbox-n-of-m") ("checkbox-percent"))
17153 (if (string-match "\\S-" fmt)
17154 (setq fmt (intern fmt))
17156 (if (eq fmt 'none) (setq fmt nil))
17159 (setcar editp prop)
17160 (setcdr editp (list title width nil fmt)))
17161 (setq cell (nthcdr (1- (current-column))
17162 org-columns-current-fmt-compiled))
17163 (setcdr cell (cons (list prop title width nil fmt)
17165 (org-columns-store-format)
17166 (org-columns-redo)))
17168 (defun org-columns-delete ()
17169 "Delete the column at point from columns view."
17171 (let* ((n (current-column))
17172 (title (nth 1 (nth n org-columns-current-fmt-compiled))))
17174 (format "Are you sure you want to remove column \"%s\"? " title))
17175 (setq org-columns-current-fmt-compiled
17176 (delq (nth n org-columns-current-fmt-compiled)
17177 org-columns-current-fmt-compiled))
17178 (org-columns-store-format)
17180 (if (>= (current-column) (length org-columns-current-fmt-compiled))
17181 (backward-char 1)))))
17183 (defun org-columns-edit-attributes ()
17184 "Edit the attributes of the current column."
17186 (let* ((n (current-column))
17187 (info (nth n org-columns-current-fmt-compiled)))
17188 (apply 'org-columns-new info)))
17190 (defun org-columns-widen (arg)
17191 "Make the column wider by ARG characters."
17193 (let* ((n (current-column))
17194 (entry (nth n org-columns-current-fmt-compiled))
17195 (width (or (nth 2 entry)
17196 (cdr (assoc (car entry) org-columns-current-maxwidths)))))
17197 (setq width (max 1 (+ width arg)))
17198 (setcar (nthcdr 2 entry) width)
17199 (org-columns-store-format)
17200 (org-columns-redo)))
17202 (defun org-columns-narrow (arg)
17203 "Make the column nrrower by ARG characters."
17205 (org-columns-widen (- arg)))
17207 (defun org-columns-move-right ()
17208 "Swap this column with the one to the right."
17210 (let* ((n (current-column))
17211 (cell (nthcdr n org-columns-current-fmt-compiled))
17213 (when (>= n (1- (length org-columns-current-fmt-compiled)))
17214 (error "Cannot shift this column further to the right"))
17215 (setq e (car cell))
17216 (setcar cell (car (cdr cell)))
17217 (setcdr cell (cons e (cdr (cdr cell))))
17218 (org-columns-store-format)
17222 (defun org-columns-move-left ()
17223 "Swap this column with the one to the left."
17225 (let* ((n (current-column)))
17227 (error "Cannot shift this column further to the left"))
17229 (org-columns-move-right)
17230 (backward-char 1)))
17232 (defun org-columns-store-format ()
17233 "Store the text version of the current columns format in appropriate place.
17234 This is either in the COLUMNS property of the node starting the current column
17235 display, or in the #+COLUMNS line of the current buffer."
17237 (setq fmt (org-columns-uncompile-format org-columns-current-fmt-compiled))
17238 (org-set-local 'org-columns-current-fmt fmt)
17239 (if (marker-position org-columns-top-level-marker)
17241 (goto-char org-columns-top-level-marker)
17242 (if (and (org-at-heading-p)
17243 (org-entry-get nil "COLUMNS"))
17244 (org-entry-put nil "COLUMNS" fmt)
17245 (goto-char (point-min))
17246 ;; Overwrite all #+COLUMNS lines....
17247 (while (re-search-forward "^#\\+COLUMNS:.*" nil t)
17248 (setq cnt (1+ cnt))
17249 (replace-match (concat "#+COLUMNS: " fmt) t t))
17251 (goto-char (point-min))
17252 (or (org-on-heading-p t) (outline-next-heading))
17253 (let ((inhibit-read-only t))
17254 (insert-before-markers "#+COLUMNS: " fmt "\n")))
17255 (org-set-local 'org-columns-default-format fmt))))))
17257 (defvar org-overriding-columns-format nil
17258 "When set, overrides any other definition.")
17259 (defvar org-agenda-view-columns-initially nil
17260 "When set, switch to columns view immediately after creating the agenda.")
17262 (defun org-agenda-columns ()
17263 "Turn on column view in the agenda."
17265 (org-verify-version 'columns)
17266 (org-columns-remove-overlays)
17267 (move-marker org-columns-begin-marker (point))
17268 (let (fmt cache maxwidths m)
17270 ((and (local-variable-p 'org-overriding-columns-format)
17271 org-overriding-columns-format)
17272 (setq fmt org-overriding-columns-format))
17273 ((setq m (get-text-property (point-at-bol) 'org-hd-marker))
17274 (setq fmt (org-entry-get m "COLUMNS" t)))
17275 ((and (boundp 'org-columns-current-fmt)
17276 (local-variable-p 'org-columns-current-fmt)
17277 org-columns-current-fmt)
17278 (setq fmt org-columns-current-fmt))
17279 ((setq m (next-single-property-change (point-min) 'org-hd-marker))
17280 (setq m (get-text-property m 'org-hd-marker))
17281 (setq fmt (org-entry-get m "COLUMNS" t))))
17282 (setq fmt (or fmt org-columns-default-format))
17283 (org-set-local 'org-columns-current-fmt fmt)
17284 (org-columns-compile-format fmt)
17286 ;; Get and cache the properties
17287 (goto-char (point-min))
17288 (while (not (eobp))
17289 (when (setq m (or (get-text-property (point) 'org-hd-marker)
17290 (get-text-property (point) 'org-marker)))
17291 (push (cons (org-current-line) (org-entry-properties m)) cache))
17292 (beginning-of-line 2))
17294 (setq maxwidths (org-columns-get-autowidth-alist fmt cache))
17295 (org-set-local 'org-columns-current-maxwidths maxwidths)
17296 (org-columns-display-here-title)
17298 (goto-line (car x))
17299 (org-columns-display-here (cdr x)))
17302 (defun org-columns-get-autowidth-alist (s cache)
17303 "Derive the maximum column widths from the format and the cache."
17304 (let ((start 0) rtn)
17305 (while (string-match (org-re "%\\([[:alpha:]][[:alnum:]_-]*\\)") s start)
17306 (push (cons (match-string 1 s) 1) rtn)
17307 (setq start (match-end 0)))
17309 (setcdr x (apply 'max
17312 (length (or (cdr (assoc (car x) (cdr y))) " ")))
17317 (defun org-columns-compute-all ()
17318 "Compute all columns that have operators defined."
17320 (remove-text-properties (point-min) (point-max) '(org-summaries t)))
17321 (let ((columns org-columns-current-fmt-compiled) col)
17322 (while (setq col (pop columns))
17325 (org-columns-compute (car col)))))))
17327 (defun org-columns-update (property)
17328 "Recompute PROPERTY, and update the columns display for it."
17329 (org-columns-compute property)
17333 (when (equal (org-overlay-get ov 'org-columns-key) property)
17334 (setq pos (org-overlay-start ov))
17336 (when (setq val (cdr (assoc property
17338 (point-at-bol) 'org-summaries))))
17339 (setq fmt (org-overlay-get ov 'org-columns-format))
17340 (org-overlay-put ov 'org-columns-value val)
17341 (org-overlay-put ov 'display (format fmt val)))))
17342 org-columns-overlays))))
17344 (defun org-columns-compute (property)
17345 "Sum the values of property PROPERTY hierarchically, for the entire buffer."
17347 (let* ((re (concat "^" outline-regexp))
17348 (lmax 30) ; Does anyone use deeper levels???
17349 (lsum (make-vector lmax 0))
17350 (lflag (make-vector lmax nil))
17352 (ass (assoc property org-columns-current-fmt-compiled))
17353 (format (nth 4 ass))
17354 (printf (nth 5 ass))
17355 (beg org-columns-top-level-marker)
17356 last-level val valflag flag end sumpos sum-alist sum str str1 useval)
17358 ;; Find the region to compute
17360 (setq end (condition-case nil (org-end-of-subtree t) (error (point-max))))
17362 ;; Walk the tree from the back and do the computations
17363 (while (re-search-backward re beg t)
17364 (setq sumpos (match-beginning 0)
17366 level (org-outline-level)
17367 val (org-entry-get nil property)
17368 valflag (and val (string-match "\\S-" val)))
17370 ((< level last-level)
17371 ;; put the sum of lower levels here as a property
17372 (setq sum (aref lsum last-level) ; current sum
17373 flag (aref lflag last-level) ; any valid entries from children?
17374 str (org-column-number-to-string sum format printf)
17375 str1 (org-add-props (copy-sequence str) nil 'org-computed t 'face 'bold)
17376 useval (if flag str1 (if valflag val ""))
17377 sum-alist (get-text-property sumpos 'org-summaries))
17378 (if (assoc property sum-alist)
17379 (setcdr (assoc property sum-alist) useval)
17380 (push (cons property useval) sum-alist)
17382 (add-text-properties sumpos (1+ sumpos)
17383 (list 'org-summaries sum-alist))))
17385 (org-entry-put nil property (if flag str val)))
17386 ;; add current to current level accumulator
17387 (when (or flag valflag)
17388 (aset lsum level (+ (aref lsum level)
17389 (if flag sum (org-column-string-to-number
17390 (if flag str val) format))))
17391 (aset lflag level t))
17392 ;; clear accumulators for deeper levels
17393 (loop for l from (1+ level) to (1- lmax) do
17395 (aset lflag l nil)))
17396 ((>= level last-level)
17397 ;; add what we have here to the accumulator for this level
17398 (aset lsum level (+ (aref lsum level)
17399 (org-column-string-to-number (or val "0") format)))
17400 (and valflag (aset lflag level t)))
17401 (t (error "This should not happen")))))))
17403 (defun org-columns-redo ()
17404 "Construct the column display again."
17406 (message "Recomputing columns...")
17408 (if (marker-position org-columns-begin-marker)
17409 (goto-char org-columns-begin-marker))
17410 (org-columns-remove-overlays)
17412 (call-interactively 'org-columns)
17413 (call-interactively 'org-agenda-columns)))
17414 (message "Recomputing columns...done"))
17416 (defun org-columns-not-in-agenda ()
17417 (if (eq major-mode 'org-agenda-mode)
17418 (error "This command is only allowed in Org-mode buffers")))
17421 (defun org-string-to-number (s)
17422 "Convert string to number, and interpret hh:mm:ss."
17423 (if (not (string-match ":" s))
17424 (string-to-number s)
17425 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
17427 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
17430 (defun org-column-number-to-string (n fmt &optional printf)
17431 "Convert a computed column number to a string value, according to FMT."
17433 ((eq fmt 'add_times)
17434 (let* ((h (floor n)) (m (floor (+ 0.5 (* 60 (- n h))))))
17435 (format "%d:%02d" h m)))
17436 ((eq fmt 'checkbox)
17437 (cond ((= n (floor n)) "[X]")
17440 ((memq fmt '(checkbox-n-of-m checkbox-percent))
17441 (let* ((n1 (floor n)) (n2 (floor (+ .5 (* 1000000 (- n n1))))))
17442 (org-nofm-to-completion n1 (+ n2 n1) (eq fmt 'checkbox-percent))))
17443 (printf (format printf n))
17444 ((eq fmt 'currency)
17446 (t (number-to-string n))))
17448 (defun org-nofm-to-completion (n m &optional percent)
17450 (format "[%d/%d]" n m)
17451 (format "[%d%%]"(floor (+ 0.5 (* 100. (/ (* 1.0 n) m)))))))
17453 (defun org-column-string-to-number (s fmt)
17454 "Convert a column value to a number that can be used for column computing."
17456 ((string-match ":" s)
17457 (let ((l (nreverse (org-split-string s ":"))) (sum 0.0))
17459 (setq sum (+ (string-to-number (pop l)) (/ sum 60))))
17461 ((memq fmt '(checkbox checkbox-n-of-m checkbox-percent))
17462 (if (equal s "[X]") 1. 0.000001))
17463 (t (string-to-number s))))
17465 (defun org-columns-uncompile-format (cfmt)
17466 "Turn the compiled columns format back into a string representation."
17467 (let ((rtn "") e s prop title op width fmt printf)
17468 (while (setq e (pop cfmt))
17476 ((eq fmt 'add_times) (setq op ":"))
17477 ((eq fmt 'checkbox) (setq op "X"))
17478 ((eq fmt 'checkbox-n-of-m) (setq op "X/"))
17479 ((eq fmt 'checkbox-percent) (setq op "X%"))
17480 ((eq fmt 'add_numbers) (setq op "+"))
17481 ((eq fmt 'currency) (setq op "$")))
17482 (if (and op printf) (setq op (concat op ";" printf)))
17483 (if (equal title prop) (setq title nil))
17484 (setq s (concat "%" (if width (number-to-string width))
17486 (if title (concat "(" title ")"))
17487 (if op (concat "{" op "}"))))
17488 (setq rtn (concat rtn " " s)))
17491 (defun org-columns-compile-format (fmt)
17492 "Turn a column format string into an alist of specifications.
17493 The alist has one entry for each column in the format. The elements of
17495 property the property
17496 title the title field for the columns
17497 width the column width in characters, can be nil for automatic
17498 operator the operator if any
17499 format the output format for computed results, derived from operator
17500 printf a printf format for computed values"
17501 (let ((start 0) width prop title op f printf)
17502 (setq org-columns-current-fmt-compiled nil)
17503 (while (string-match
17504 (org-re "%\\([0-9]+\\)?\\([[:alnum:]_-]+\\)\\(?:(\\([^)]+\\))\\)?\\(?:{\\([^}]+\\)}\\)?\\s-*")
17506 (setq start (match-end 0)
17507 width (match-string 1 fmt)
17508 prop (match-string 2 fmt)
17509 title (or (match-string 3 fmt) prop)
17510 op (match-string 4 fmt)
17513 (if width (setq width (string-to-number width)))
17514 (when (and op (string-match ";" op))
17515 (setq printf (substring op (match-end 0))
17516 op (substring op 0 (match-beginning 0))))
17518 ((equal op "+") (setq f 'add_numbers))
17519 ((equal op "$") (setq f 'currency))
17520 ((equal op ":") (setq f 'add_times))
17521 ((equal op "X") (setq f 'checkbox))
17522 ((equal op "X/") (setq f 'checkbox-n-of-m))
17523 ((equal op "X%") (setq f 'checkbox-percent))
17525 (push (list prop title width op f printf) org-columns-current-fmt-compiled))
17526 (setq org-columns-current-fmt-compiled
17527 (nreverse org-columns-current-fmt-compiled))))
17530 ;;; Dynamic block for Column view
17532 (defun org-columns-capture-view (&optional maxlevel skip-empty-rows)
17533 "Get the column view of the current buffer or subtree.
17534 The first optional argument MAXLEVEL sets the level limit. A
17535 second optional argument SKIP-EMPTY-ROWS tells whether to skip
17536 empty rows, an empty row being one where all the column view
17537 specifiers except ITEM are empty. This function returns a list
17538 containing the title row and all other rows. Each row is a list
17541 (let* ((title (mapcar 'cadr org-columns-current-fmt-compiled))
17542 (n (length title)) row tbl)
17543 (goto-char (point-min))
17544 (while (and (re-search-forward "^\\(\\*+\\) " nil t)
17545 (or (null maxlevel)
17547 (if org-odd-levels-only
17548 (/ (1+ (length (match-string 1))) 2)
17549 (length (match-string 1))))))
17550 (when (get-char-property (match-beginning 0) 'org-columns-key)
17552 (loop for i from 0 to (1- n) do
17553 (push (or (get-char-property (+ (match-beginning 0) i) 'org-columns-value-modified)
17554 (get-char-property (+ (match-beginning 0) i) 'org-columns-value)
17557 (setq row (nreverse row))
17558 (unless (and skip-empty-rows
17559 (eq 1 (length (delete "" (delete-dups row)))))
17561 (append (list title 'hline) (nreverse tbl)))))
17563 (defun org-dblock-write:columnview (params)
17564 "Write the column view table.
17565 PARAMS is a property list of parameters:
17567 :width enforce same column widths with <N> specifiers.
17568 :id the :ID: property of the entry where the columns view
17569 should be built, as a string. When `local', call locally.
17570 When `global' call column view with the cursor at the beginning
17571 of the buffer (usually this means that the whole buffer switches
17573 :hlines When t, insert a hline before each item. When a number, insert
17574 a hline before each level <= that number.
17575 :vlines When t, make each column a colgroup to enforce vertical lines.
17576 :maxlevel When set to a number, don't capture headlines below this level.
17578 When t, skip rows where all specifiers other than ITEM are empty."
17579 (let ((pos (move-marker (make-marker) (point)))
17580 (hlines (plist-get params :hlines))
17581 (vlines (plist-get params :vlines))
17582 (maxlevel (plist-get params :maxlevel))
17583 (skip-empty-rows (plist-get params :skip-empty-rows))
17584 tbl id idpos nfields tmp)
17587 (when (setq id (plist-get params :id))
17588 (cond ((not id) nil)
17589 ((eq id 'global) (goto-char (point-min)))
17590 ((eq id 'local) nil)
17591 ((setq idpos (org-find-entry-with-id id))
17593 (t (error "Cannot find entry with :ID: %s" id))))
17595 (setq tbl (org-columns-capture-view maxlevel skip-empty-rows))
17596 (setq nfields (length (car tbl)))
17597 (org-columns-quit)))
17599 (move-marker pos nil)
17601 (when (plist-get params :hlines)
17604 (if (eq (car tbl) 'hline)
17605 (push (pop tbl) tmp)
17606 (if (string-match "\\` *\\(\\*+\\)" (caar tbl))
17607 (if (and (not (eq (car tmp) 'hline))
17609 (and (numberp hlines) (<= (- (match-end 1) (match-beginning 1)) hlines))))
17610 (push 'hline tmp)))
17611 (push (pop tbl) tmp)))
17612 (setq tbl (nreverse tmp)))
17614 (setq tbl (mapcar (lambda (x)
17615 (if (eq 'hline x) x (cons "" x)))
17617 (setq tbl (append tbl (list (cons "/" (make-list nfields "<>"))))))
17619 (insert (org-listtable-to-string tbl))
17620 (when (plist-get params :width)
17621 (insert "\n|" (mapconcat (lambda (x) (format "<%d>" (max 3 x)))
17622 org-columns-current-widths "|")))
17624 (org-table-align))))
17626 (defun org-listtable-to-string (tbl)
17627 "Convert a listtable TBL to a string that contains the Org-mode table.
17628 The table still need to be alligned. The resulting string has no leading
17629 and tailing newline characters."
17634 (concat "|" (mapconcat 'identity x "|") "|"))
17635 ((eq x 'hline) "|-|")
17636 (t (error "Garbage in listtable: %s" x))))
17639 (defun org-insert-columns-dblock ()
17640 "Create a dynamic block capturing a column view table."
17642 (let ((defaults '(:name "columnview" :hlines 1))
17643 (id (completing-read
17644 "Capture columns (local, global, entry with :ID: property) [local]: "
17645 (append '(("global") ("local"))
17646 (mapcar 'list (org-property-values "ID"))))))
17647 (if (equal id "") (setq id 'local))
17648 (if (equal id "global") (setq id 'global))
17649 (setq defaults (append defaults (list :id id)))
17650 (org-create-dblock defaults)
17651 (org-update-dblock)))
17655 (defvar org-last-changed-timestamp nil)
17656 (defvar org-time-was-given) ; dynamically scoped parameter
17657 (defvar org-end-time-was-given) ; dynamically scoped parameter
17658 (defvar org-ts-what) ; dynamically scoped parameter
17660 (defun org-time-stamp (arg)
17661 "Prompt for a date/time and insert a time stamp.
17662 If the user specifies a time like HH:MM, or if this command is called
17663 with a prefix argument, the time stamp will contain date and time.
17664 Otherwise, only the date will be included. All parts of a date not
17665 specified by the user will be filled in from the current date/time.
17666 So if you press just return without typing anything, the time stamp
17667 will represent the current date/time. If there is already a timestamp
17668 at the cursor, it will be modified."
17672 ;; Default time is either today, or, when entering a range,
17673 ;; the range start.
17674 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
17676 (re-search-backward
17677 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
17678 (- (point) 20) t)))
17679 (apply 'encode-time (org-parse-time-string (match-string 1)))
17681 (default-input (and ts (org-get-compact-tod ts)))
17682 org-time-was-given org-end-time-was-given time)
17684 ((and (org-at-timestamp-p)
17685 (eq last-command 'org-time-stamp)
17686 (eq this-command 'org-time-stamp))
17688 (setq time (let ((this-command this-command))
17689 (org-read-date arg 'totime nil nil default-time default-input)))
17690 (org-insert-time-stamp time (or org-time-was-given arg)))
17691 ((org-at-timestamp-p)
17692 (setq time (let ((this-command this-command))
17693 (org-read-date arg 'totime nil nil default-time default-input)))
17694 (when (org-at-timestamp-p) ; just to get the match data
17696 (setq org-last-changed-timestamp
17697 (org-insert-time-stamp
17698 time (or org-time-was-given arg)
17699 nil nil nil (list org-end-time-was-given))))
17700 (message "Timestamp updated"))
17702 (setq time (let ((this-command this-command))
17703 (org-read-date arg 'totime nil nil default-time default-input)))
17704 (org-insert-time-stamp time (or org-time-was-given arg)
17705 nil nil nil (list org-end-time-was-given))))))
17707 ;; FIXME: can we use this for something else????
17708 ;; like computing time differences?????
17709 (defun org-get-compact-tod (s)
17710 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
17711 (let* ((t1 (match-string 1 s))
17712 (h1 (string-to-number (match-string 2 s)))
17713 (m1 (string-to-number (match-string 3 s)))
17714 (t2 (and (match-end 4) (match-string 5 s)))
17715 (h2 (and t2 (string-to-number (match-string 6 s))))
17716 (m2 (and t2 (string-to-number (match-string 7 s))))
17720 (setq dh (- h2 h1) dm (- m2 m1))
17721 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
17722 (concat t1 "+" (number-to-string dh)
17723 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
17725 (defun org-time-stamp-inactive (&optional arg)
17726 "Insert an inactive time stamp.
17727 An inactive time stamp is enclosed in square brackets instead of angle
17728 brackets. It is inactive in the sense that it does not trigger agenda entries,
17729 does not link to the calendar and cannot be changed with the S-cursor keys.
17730 So these are more for recording a certain time/date."
17732 (let (org-time-was-given org-end-time-was-given time)
17733 (setq time (org-read-date arg 'totime))
17734 (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
17735 nil nil (list org-end-time-was-given))))
17737 (defvar org-date-ovl (org-make-overlay 1 1))
17738 (org-overlay-put org-date-ovl 'face 'org-warning)
17739 (org-detach-overlay org-date-ovl)
17741 (defvar org-ans1) ; dynamically scoped parameter
17742 (defvar org-ans2) ; dynamically scoped parameter
17744 (defvar org-plain-time-of-day-regexp) ; defined below
17746 (defvar org-read-date-overlay nil)
17747 (defvar org-dcst nil) ; dynamically scoped
17749 (defun org-read-date (&optional with-time to-time from-string prompt
17750 default-time default-input)
17751 "Read a date, possibly a time, and make things smooth for the user.
17752 The prompt will suggest to enter an ISO date, but you can also enter anything
17753 which will at least partially be understood by `parse-time-string'.
17754 Unrecognized parts of the date will default to the current day, month, year,
17755 hour and minute. If this command is called to replace a timestamp at point,
17756 of to enter the second timestamp of a range, the default time is taken from the
17757 existing stamp. For example,
17758 3-2-5 --> 2003-02-05
17759 feb 15 --> currentyear-02-15
17760 sep 12 9 --> 2009-09-12
17761 12:45 --> today 12:45
17762 22 sept 0:34 --> currentyear-09-22 0:34
17763 12 --> currentyear-currentmonth-12
17764 Fri --> nearest Friday (today or later)
17767 Furthermore you can specify a relative date by giving, as the *first* thing
17768 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
17769 change in days weeks, months, years.
17770 With a single plus or minus, the date is relative to today. With a double
17771 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
17772 +4d --> four days from today
17773 +4 --> same as above
17774 +2w --> two weeks from today
17775 ++5 --> five days from default date
17777 The function understands only English month and weekday abbreviations,
17778 but this can be configured with the variables `parse-time-months' and
17779 `parse-time-weekdays'.
17781 While prompting, a calendar is popped up - you can also select the
17782 date with the mouse (button 1). The calendar shows a period of three
17783 months. To scroll it to other months, use the keys `>' and `<'.
17784 If you don't like the calendar, turn it off with
17785 \(setq org-read-date-popup-calendar nil)
17787 With optional argument TO-TIME, the date will immediately be converted
17788 to an internal time.
17789 With an optional argument WITH-TIME, the prompt will suggest to also
17790 insert a time. Note that when WITH-TIME is not set, you can still
17791 enter a time, and this function will inform the calling routine about
17792 this change. The calling routine may then choose to change the format
17793 used to insert the time stamp into the buffer to include the time.
17794 With optional argument FROM-STRING, read from this string instead from
17795 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
17796 the time/date that is used for everything that is not specified by the
17798 (require 'parse-time)
17799 (let* ((org-time-stamp-rounding-minutes
17800 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
17801 (org-dcst org-display-custom-times)
17802 (ct (org-current-time))
17803 (def (or default-time ct))
17804 (defdecode (decode-time def))
17806 (when (< (nth 2 defdecode) org-extend-today-until)
17807 (setcar (nthcdr 2 defdecode) -1)
17808 (setcar (nthcdr 1 defdecode) 59)
17809 (setq def (apply 'encode-time defdecode)
17810 defdecode (decode-time def)))))
17811 (calendar-move-hook nil)
17812 (view-diary-entries-initially nil)
17813 (view-calendar-holidays-initially nil)
17814 (timestr (format-time-string
17815 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
17816 (prompt (concat (if prompt (concat prompt " ") "")
17817 (format "Date+time [%s]: " timestr)))
17818 ans (org-ans0 "") org-ans1 org-ans2 final)
17821 (from-string (setq ans from-string))
17822 (org-read-date-popup-calendar
17824 (save-window-excursion
17826 (calendar-forward-day (- (time-to-days def)
17827 (calendar-absolute-from-gregorian
17828 (calendar-current-date))))
17829 (org-eval-in-calendar nil t)
17830 (let* ((old-map (current-local-map))
17831 (map (copy-keymap calendar-mode-map))
17832 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
17833 (org-defkey map (kbd "RET") 'org-calendar-select)
17834 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
17835 'org-calendar-select-mouse)
17836 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
17837 'org-calendar-select-mouse
)
17838 (org-defkey minibuffer-local-map
[(meta shift left
)]
17839 (lambda () (interactive)
17840 (org-eval-in-calendar '(calendar-backward-month 1))))
17841 (org-defkey minibuffer-local-map
[(meta shift right
)]
17842 (lambda () (interactive)
17843 (org-eval-in-calendar '(calendar-forward-month 1))))
17844 (org-defkey minibuffer-local-map
[(meta shift up
)]
17845 (lambda () (interactive)
17846 (org-eval-in-calendar '(calendar-backward-year 1))))
17847 (org-defkey minibuffer-local-map
[(meta shift down
)]
17848 (lambda () (interactive)
17849 (org-eval-in-calendar '(calendar-forward-year 1))))
17850 (org-defkey minibuffer-local-map
[(shift up
)]
17851 (lambda () (interactive)
17852 (org-eval-in-calendar '(calendar-backward-week 1))))
17853 (org-defkey minibuffer-local-map
[(shift down
)]
17854 (lambda () (interactive)
17855 (org-eval-in-calendar '(calendar-forward-week 1))))
17856 (org-defkey minibuffer-local-map
[(shift left
)]
17857 (lambda () (interactive)
17858 (org-eval-in-calendar '(calendar-backward-day 1))))
17859 (org-defkey minibuffer-local-map
[(shift right
)]
17860 (lambda () (interactive)
17861 (org-eval-in-calendar '(calendar-forward-day 1))))
17862 (org-defkey minibuffer-local-map
">"
17863 (lambda () (interactive)
17864 (org-eval-in-calendar '(scroll-calendar-left 1))))
17865 (org-defkey minibuffer-local-map
"<"
17866 (lambda () (interactive)
17867 (org-eval-in-calendar '(scroll-calendar-right 1))))
17870 (use-local-map map
)
17871 (add-hook 'post-command-hook
'org-read-date-display
)
17872 (setq org-ans0
(read-string prompt default-input nil nil
))
17873 ;; org-ans0: from prompt
17874 ;; org-ans1: from mouse click
17875 ;; org-ans2: from calendar motion
17876 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
17877 (remove-hook 'post-command-hook
'org-read-date-display
)
17878 (use-local-map old-map
)
17879 (when org-read-date-overlay
17880 (org-delete-overlay org-read-date-overlay
)
17881 (setq org-read-date-overlay nil
)))))))
17883 (t ; Naked prompt only
17885 (setq ans
(read-string prompt default-input nil timestr
))
17886 (when org-read-date-overlay
17887 (org-delete-overlay org-read-date-overlay
)
17888 (setq org-read-date-overlay nil
)))))
17890 (setq final
(org-read-date-analyze ans def defdecode
))
17893 (apply 'encode-time final
)
17894 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
17895 (format "%04d-%02d-%02d %02d:%02d"
17896 (nth 5 final
) (nth 4 final
) (nth 3 final
)
17897 (nth 2 final
) (nth 1 final
))
17898 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
17902 (defun org-read-date-display ()
17903 "Display the currrent date prompt interpretation in the minibuffer."
17904 (when org-read-date-display-live
17905 (when org-read-date-overlay
17906 (org-delete-overlay org-read-date-overlay
))
17909 (while (not (equal (buffer-substring
17910 (max (point-min) (- (point) 4)) (point))
17914 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
17915 " " (or org-ans1 org-ans2
)))
17916 (org-end-time-was-given nil
)
17917 (f (org-read-date-analyze ans def defdecode
))
17919 org-time-stamp-custom-formats
17920 org-time-stamp-formats
))
17921 (fmt (if (or with-time
17922 (and (boundp 'org-time-was-given
) org-time-was-given
))
17925 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
17926 (when (and org-end-time-was-given
17927 (string-match org-plain-time-of-day-regexp txt
))
17928 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
17929 org-end-time-was-given
17930 (substring txt
(match-end 0)))))
17931 (setq org-read-date-overlay
17932 (make-overlay (1- (point-at-eol)) (point-at-eol)))
17933 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
17935 (defun org-read-date-analyze (ans def defdecode
)
17936 "Analyze the combined answer of the date prompt."
17937 ;; FIXME: cleanup and comment
17938 (let (delta deltan deltaw deltadef year month day
17939 hour minute second wday pm h2 m2 tl wday1
)
17941 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
17942 (setq ans
(replace-match "" t t ans
)
17944 deltaw
(nth 1 delta
)
17945 deltadef
(nth 2 delta
)))
17947 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
17948 (when (string-match
17949 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
17950 (setq year
(if (match-end 2)
17951 (string-to-number (match-string 2 ans
))
17952 (string-to-number (format-time-string "%Y")))
17953 month
(string-to-number (match-string 3 ans
))
17954 day
(string-to-number (match-string 4 ans
)))
17955 (if (< year
100) (setq year
(+ 2000 year
)))
17956 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
17958 ;; Help matching am/pm times, because `parse-time-string' does not do that.
17959 ;; If there is a time with am/pm, and *no* time without it, we convert
17960 ;; so that matching will be successful.
17961 (loop for i from
1 to
2 do
; twice, for end time as well
17962 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
17963 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
17964 (setq hour
(string-to-number (match-string 1 ans
))
17965 minute
(if (match-end 3)
17966 (string-to-number (match-string 3 ans
))
17969 (string-to-char (downcase (match-string 4 ans
)))))
17970 (if (and (= hour
12) (not pm
))
17972 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
17973 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
17976 ;; Check if a time range is given as a duration
17977 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
17978 (setq hour
(string-to-number (match-string 1 ans
))
17979 h2
(+ hour
(string-to-number (match-string 3 ans
)))
17980 minute
(string-to-number (match-string 2 ans
))
17981 m2
(+ minute
(if (match-end 5) (string-to-number (match-string 5 ans
))0)))
17982 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
17983 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
) t t ans
)))
17985 ;; Check if there is a time range
17986 (when (boundp 'org-end-time-was-given
)
17987 (setq org-time-was-given nil
)
17988 (when (and (string-match org-plain-time-of-day-regexp ans
)
17990 (setq org-end-time-was-given
(match-string 8 ans
))
17991 (setq ans
(concat (substring ans
0 (match-beginning 7))
17992 (substring ans
(match-end 7))))))
17994 (setq tl
(parse-time-string ans
)
17995 day
(or (nth 3 tl
) (nth 3 defdecode
))
17996 month
(or (nth 4 tl
)
17997 (if (and org-read-date-prefer-future
17998 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
17999 (1+ (nth 4 defdecode
))
18000 (nth 4 defdecode
)))
18001 year
(or (nth 5 tl
)
18002 (if (and org-read-date-prefer-future
18003 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
18004 (1+ (nth 5 defdecode
))
18005 (nth 5 defdecode
)))
18006 hour
(or (nth 2 tl
) (nth 2 defdecode
))
18007 minute
(or (nth 1 tl
) (nth 1 defdecode
))
18008 second
(or (nth 0 tl
) 0)
18012 (let ((now (decode-time (current-time))))
18013 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
18014 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
18015 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
18016 ((equal deltaw
"m") (setq month
(+ month deltan
)))
18017 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
18018 (when (and wday
(not (nth 3 tl
)))
18019 ;; Weekday was given, but no day, so pick that day in the week
18020 ;; on or after the derived date.
18021 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
18022 (unless (equal wday wday1
)
18023 (setq day
(+ day
(%
(- wday wday1 -
7) 7)))))
18024 (if (and (boundp 'org-time-was-given
)
18026 (setq org-time-was-given t
))
18027 (if (< year
100) (setq year
(+ 2000 year
)))
18028 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
18029 (list second minute hour day month year
)))
18031 (defvar parse-time-weekdays
)
18033 (defun org-read-date-get-relative (s today default
)
18034 "Check string S for special relative date string.
18035 TODAY and DEFAULT are internal times, for today and for a default.
18036 Return shift list (N what def-flag)
18037 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
18038 N is the number of WHATs to shift.
18039 DEF-FLAG is t when a double ++ or -- indicates shift relative to
18040 the DEFAULT date rather than TODAY."
18041 (when (string-match
18043 "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
18045 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
18046 "\\([ \t]\\|$\\)") s
)
18047 (let* ((dir (if (match-end 1)
18048 (string-to-char (substring (match-string 1 s
) -
1))
18050 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
18051 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
18052 (what (if (match-end 3) (match-string 3 s
) "d"))
18053 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
18054 (date (if rel default today
))
18055 (wday (nth 6 (decode-time date
)))
18059 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
18060 (if (= dir ?-
) (setq delta
(- delta
7)))
18061 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
18062 (list delta
"d" rel
))
18063 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
18065 (defun org-eval-in-calendar (form &optional keepdate
)
18066 "Eval FORM in the calendar window and return to current window.
18067 Also, store the cursor date in variable org-ans2."
18068 (let ((sw (selected-window)))
18069 (select-window (get-buffer-window "*Calendar*"))
18071 (when (and (not keepdate
) (calendar-cursor-to-date))
18072 (let* ((date (calendar-cursor-to-date))
18073 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
18074 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
18075 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
18076 (select-window sw
)))
18078 ; ;; Update the prompt to show new default date
18080 ; (goto-char (point-min))
18081 ; (when (and org-ans2
18082 ; (re-search-forward "\\[[-0-9]+\\]" nil t)
18083 ; (get-text-property (match-end 0) 'field))
18084 ; (let ((inhibit-read-only t))
18085 ; (replace-match (concat "[" org-ans2 "]") t t)
18086 ; (add-text-properties (point-min) (1+ (match-end 0))
18087 ; (text-properties-at (1+ (point-min)))))))))
18089 (defun org-calendar-select ()
18090 "Return to `org-read-date' with the date currently selected.
18091 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
18093 (when (calendar-cursor-to-date)
18094 (let* ((date (calendar-cursor-to-date))
18095 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
18096 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
18097 (if (active-minibuffer-window) (exit-minibuffer))))
18099 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
18100 "Insert a date stamp for the date given by the internal TIME.
18101 WITH-HM means, use the stamp format that includes the time of the day.
18102 INACTIVE means use square brackets instead of angular ones, so that the
18103 stamp will not contribute to the agenda.
18104 PRE and POST are optional strings to be inserted before and after the
18106 The command returns the inserted time stamp."
18107 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
18109 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
18110 (insert-before-markers (or pre
""))
18111 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
18112 (when (listp extra
)
18113 (setq extra
(car extra
))
18114 (if (and (stringp extra
)
18115 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
18116 (setq extra
(format "-%02d:%02d"
18117 (string-to-number (match-string 1 extra
))
18118 (string-to-number (match-string 2 extra
))))
18122 (insert-before-markers extra
)
18124 (insert-before-markers (or post
""))
18127 (defun org-toggle-time-stamp-overlays ()
18128 "Toggle the use of custom time stamp formats."
18130 (setq org-display-custom-times
(not org-display-custom-times
))
18131 (unless org-display-custom-times
18132 (let ((p (point-min)) (bmp (buffer-modified-p)))
18133 (while (setq p
(next-single-property-change p
'display
))
18134 (if (and (get-text-property p
'display
)
18135 (eq (get-text-property p
'face
) 'org-date
))
18136 (remove-text-properties
18137 p
(setq p
(next-single-property-change p
'display
))
18139 (set-buffer-modified-p bmp
)))
18140 (if (featurep 'xemacs
)
18141 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
18142 (org-restart-font-lock)
18143 (setq org-table-may-need-update t
)
18144 (if org-display-custom-times
18145 (message "Time stamps are overlayed with custom format")
18146 (message "Time stamp overlays removed")))
18148 (defun org-display-custom-time (beg end
)
18149 "Overlay modified time stamp format over timestamp between BED and END."
18150 (let* ((ts (buffer-substring beg end
))
18151 t1 w1 with-hm tf time str w2
(off 0))
18153 (setq t1
(org-parse-time-string ts t
))
18154 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( \\+[0-9]+[dwmy]\\)?\\'" ts
)
18155 (setq off
(- (match-end 0) (match-beginning 0)))))
18156 (setq end
(- end off
))
18157 (setq w1
(- end beg
)
18158 with-hm
(and (nth 1 t1
) (nth 2 t1
))
18159 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
18160 time
(org-fix-decoded-time t1
)
18162 (format-time-string
18163 (substring tf
1 -
1) (apply 'encode-time time
))
18164 nil
'mouse-face
'highlight
)
18166 (if (not (= w2 w1
))
18167 (add-text-properties (1+ beg
) (+ 2 beg
)
18168 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
18169 (if (featurep 'xemacs
)
18171 (put-text-property beg end
'invisible t
)
18172 (put-text-property beg end
'end-glyph
(make-glyph str
)))
18173 (put-text-property beg end
'display str
))))
18175 (defun org-translate-time (string)
18176 "Translate all timestamps in STRING to custom format.
18177 But do this only if the variable `org-display-custom-times' is set."
18178 (when org-display-custom-times
18181 (re org-ts-regexp-both
)
18182 t1 with-hm inactive tf time str beg end
)
18183 (while (setq start
(string-match re string start
))
18184 (setq beg
(match-beginning 0)
18186 t1
(save-match-data
18187 (org-parse-time-string (substring string beg end
) t
))
18188 with-hm
(and (nth 1 t1
) (nth 2 t1
))
18189 inactive
(equal (substring string beg
(1+ beg
)) "[")
18190 tf
(funcall (if with-hm
'cdr
'car
)
18191 org-time-stamp-custom-formats
)
18192 time
(org-fix-decoded-time t1
)
18193 str
(format-time-string
18195 (if inactive
"[" "<") (substring tf
1 -
1)
18196 (if inactive
"]" ">"))
18197 (apply 'encode-time time
))
18198 string
(replace-match str t t string
)
18199 start
(+ start
(length str
)))))))
18202 (defun org-fix-decoded-time (time)
18203 "Set 0 instead of nil for the first 6 elements of time.
18204 Don't touch the rest."
18206 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
18208 (defun org-days-to-time (timestamp-string)
18209 "Difference between TIMESTAMP-STRING and now in days."
18210 (- (time-to-days (org-time-string-to-time timestamp-string
))
18211 (time-to-days (current-time))))
18213 (defun org-deadline-close (timestamp-string &optional ndays
)
18214 "Is the time in TIMESTAMP-STRING close to the current date?"
18215 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
18216 (and (< (org-days-to-time timestamp-string
) ndays
)
18217 (not (org-entry-is-done-p))))
18219 (defun org-get-wdays (ts)
18220 "Get the deadline lead time appropriate for timestring TS."
18222 ((<= org-deadline-warning-days
0)
18223 ;; 0 or negative, enforce this value no matter what
18224 (- org-deadline-warning-days
))
18225 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts
)
18226 ;; lead time is specified.
18227 (floor (* (string-to-number (match-string 1 ts
))
18228 (cdr (assoc (match-string 2 ts
)
18229 '(("d" .
1) ("w" .
7)
18230 ("m" .
30.4) ("y" .
365.25)))))))
18231 ;; go for the default.
18232 (t org-deadline-warning-days
)))
18234 (defun org-calendar-select-mouse (ev)
18235 "Return to `org-read-date' with the date currently selected.
18236 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
18238 (mouse-set-point ev
)
18239 (when (calendar-cursor-to-date)
18240 (let* ((date (calendar-cursor-to-date))
18241 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
18242 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
18243 (if (active-minibuffer-window) (exit-minibuffer))))
18245 (defun org-check-deadlines (ndays)
18246 "Check if there are any deadlines due or past due.
18247 A deadline is considered due if it happens within `org-deadline-warning-days'
18248 days from today's date. If the deadline appears in an entry marked DONE,
18249 it is not shown. The prefix arg NDAYS can be used to test that many
18250 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
18252 (let* ((org-warn-days
18254 ((equal ndays
'(4)) 100000)
18255 (ndays (prefix-numeric-value ndays
))
18256 (t (abs org-deadline-warning-days
))))
18257 (case-fold-search nil
)
18258 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
18260 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
18262 (message "%d deadlines past-due or due within %d days"
18263 (org-occur regexp nil callback
)
18266 (defun org-check-before-date (date)
18267 "Check if there are deadlines or scheduled entries before DATE."
18268 (interactive (list (org-read-date)))
18269 (let ((case-fold-search nil
)
18270 (regexp (concat "\\<\\(" org-deadline-string
18271 "\\|" org-scheduled-string
18272 "\\) *<\\([^>]+\\)>"))
18274 (lambda () (time-less-p
18275 (org-time-string-to-time (match-string 2))
18276 (org-time-string-to-time date
)))))
18277 (message "%d entries before %s"
18278 (org-occur regexp nil callback
) date
)))
18280 (defun org-evaluate-time-range (&optional to-buffer
)
18281 "Evaluate a time range by computing the difference between start and end.
18282 Normally the result is just printed in the echo area, but with prefix arg
18283 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
18284 If the time range is actually in a table, the result is inserted into the
18286 For time difference computation, a year is assumed to be exactly 365
18287 days in order to avoid rounding problems."
18290 (org-clock-update-time-maybe)
18292 (unless (org-at-date-range-p t
)
18293 (goto-char (point-at-bol))
18294 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
18295 (if (not (org-at-date-range-p t
))
18296 (error "Not at a time-stamp range, and none found in current line")))
18297 (let* ((ts1 (match-string 1))
18298 (ts2 (match-string 2))
18299 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
18300 (match-end (match-end 0))
18301 (time1 (org-time-string-to-time ts1
))
18302 (time2 (org-time-string-to-time ts2
))
18303 (t1 (time-to-seconds time1
))
18304 (t2 (time-to-seconds time2
))
18305 (diff (abs (- t2 t1
)))
18306 (negative (< (- t2 t1
) 0))
18307 ;; (ys (floor (* 365 24 60 60)))
18310 (fy "%dy %dd %02d:%02d")
18312 (fd "%dd %02d:%02d")
18317 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
18319 d
(floor (/ diff ds
)) diff
(mod diff ds
)
18320 h
(floor (/ diff hs
)) diff
(mod diff hs
)
18321 m
(floor (/ diff
60)))
18322 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
18324 d
(floor (+ (/ diff ds
) 0.5))
18326 (if (not to-buffer
)
18327 (message "%s" (org-make-tdiff-string y d h m
))
18328 (if (org-at-table-p)
18330 (goto-char match-end
)
18332 (and (looking-at " *|") (goto-char (match-end 0))))
18333 (goto-char match-end
))
18335 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
18336 (replace-match ""))
18337 (if negative
(insert " -"))
18338 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
18339 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
18340 (insert " " (format fh h m
))))
18341 (if align
(org-table-align))
18342 (message "Time difference inserted")))))
18344 (defun org-make-tdiff-string (y d h m
)
18347 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
18349 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
18351 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
18353 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
18355 (apply 'format fmt
(nreverse l
))))
18357 (defun org-time-string-to-time (s)
18358 (apply 'encode-time
(org-parse-time-string s
)))
18360 (defun org-time-string-to-absolute (s &optional daynr prefer
)
18361 "Convert a time stamp to an absolute day number.
18362 If there is a specifyer for a cyclic time stamp, get the closest date to
18365 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
18366 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
18369 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
18370 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
18371 (time-to-days (current-time))) (match-string 0 s
)
18373 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
18375 (defun org-time-from-absolute (d)
18376 "Return the time corresponding to date D.
18377 D may be an absolute day number, or a calendar-type list (month day year)."
18378 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
18379 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
18381 (defun org-calendar-holiday ()
18382 "List of holidays, for Diary display in Org-mode."
18383 (require 'holidays
)
18385 (if (fboundp 'calendar-check-holidays
)
18386 'calendar-check-holidays
'check-calendar-holidays
) date
)))
18387 (if hl
(mapconcat 'identity hl
"; "))))
18389 (defun org-diary-sexp-entry (sexp entry date
)
18390 "Process a SEXP diary ENTRY for DATE."
18391 (require 'diary-lib
)
18392 (let ((result (if calendar-debug-sexp
18393 (let ((stack-trace-on-error t
))
18394 (eval (car (read-from-string sexp
))))
18395 (condition-case nil
18396 (eval (car (read-from-string sexp
)))
18399 (message "Bad sexp at line %d in %s: %s"
18401 (buffer-file-name) sexp
)
18403 (cond ((stringp result
) result
)
18404 ((and (consp result
)
18405 (stringp (cdr result
))) (cdr result
))
18409 (defun org-diary-to-ical-string (frombuf)
18410 "Get iCalendar entries from diary entries in buffer FROMBUF.
18411 This uses the icalendar.el library."
18412 (let* ((tmpdir (if (featurep 'xemacs
)
18414 temporary-file-directory
))
18415 (tmpfile (make-temp-name
18416 (expand-file-name "orgics" tmpdir
)))
18419 (set-buffer frombuf
)
18420 (icalendar-export-region (point-min) (point-max) tmpfile
)
18421 (setq buf
(find-buffer-visiting tmpfile
))
18423 (goto-char (point-min))
18424 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
18425 (setq b
(match-beginning 0)))
18426 (goto-char (point-max))
18427 (if (re-search-backward "^END:VEVENT" nil t
)
18428 (setq e
(match-end 0)))
18429 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
18431 (kill-buffer frombuf
)
18432 (delete-file tmpfile
)
18435 (defun org-closest-date (start current change prefer
)
18436 "Find the date closest to CURRENT that is consistent with START and CHANGE.
18437 When PREFER is `past' return a date that is either CURRENT or past.
18438 When PREFER is `future', return a date that is either CURRENT or future."
18439 ;; Make the proper lists from the dates
18441 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
18442 dn dw sday cday n1 n2
18443 d m y y1 y2 date1 date2 nmonths nm ny m2
)
18445 (setq start
(org-date-to-gregorian start
)
18446 current
(org-date-to-gregorian
18447 (if org-agenda-repeating-timestamp-show-all
18449 (time-to-days (current-time))))
18450 sday
(calendar-absolute-from-gregorian start
)
18451 cday
(calendar-absolute-from-gregorian current
))
18453 (if (<= cday sday
) (throw 'exit sday
))
18455 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
18456 (setq dn
(string-to-number (match-string 1 change
))
18457 dw
(cdr (assoc (match-string 2 change
) a1
)))
18458 (error "Invalid change specifyer: %s" change
))
18459 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
18462 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
18465 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
18466 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
18467 (setq date1
(list m d y1
)
18468 n1
(calendar-absolute-from-gregorian date1
)
18469 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
18470 n2
(calendar-absolute-from-gregorian date2
)))
18472 ;; approx number of month between the tow dates
18473 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
18474 ;; How often does dn fit in there?
18475 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
18476 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
18478 ny
(floor (/ m
12))
18481 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
18482 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
18483 (setq m2
(+ m dn
) y2 y
)
18484 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
18485 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
18487 (setq n1 n2 m m2 y y2
)
18488 (setq m2
(+ m dn
) y2 y
)
18489 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
18490 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
18492 (if org-agenda-repeating-timestamp-show-all
18494 ((eq prefer
'past
) n1
)
18495 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
18496 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
18498 ((eq prefer
'past
) n1
)
18499 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
18500 (t (if (= cday n1
) n1 n2
)))))))
18502 (defun org-date-to-gregorian (date)
18503 "Turn any specification of DATE into a gregorian date for the calendar."
18504 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
18505 ((and (listp date
) (= (length date
) 3)) date
)
18507 (setq date
(org-parse-time-string date
))
18508 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
18510 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
18512 (defun org-parse-time-string (s &optional nodefault
)
18513 "Parse the standard Org-mode time string.
18514 This should be a lot faster than the normal `parse-time-string'.
18515 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
18516 hour and minute fields will be nil if not given."
18517 (if (string-match org-ts-regexp0 s
)
18519 (if (or (match-beginning 8) (not nodefault
))
18520 (string-to-number (or (match-string 8 s
) "0")))
18521 (if (or (match-beginning 7) (not nodefault
))
18522 (string-to-number (or (match-string 7 s
) "0")))
18523 (string-to-number (match-string 4 s
))
18524 (string-to-number (match-string 3 s
))
18525 (string-to-number (match-string 2 s
))
18529 (defun org-timestamp-up (&optional arg
)
18530 "Increase the date item at the cursor by one.
18531 If the cursor is on the year, change the year. If it is on the month or
18532 the day, change that.
18533 With prefix ARG, change by that many units."
18535 (org-timestamp-change (prefix-numeric-value arg
)))
18537 (defun org-timestamp-down (&optional arg
)
18538 "Decrease the date item at the cursor by one.
18539 If the cursor is on the year, change the year. If it is on the month or
18540 the day, change that.
18541 With prefix ARG, change by that many units."
18543 (org-timestamp-change (- (prefix-numeric-value arg
))))
18545 (defun org-timestamp-up-day (&optional arg
)
18546 "Increase the date in the time stamp by one day.
18547 With prefix ARG, change that many days."
18549 (if (and (not (org-at-timestamp-p t
))
18550 (org-on-heading-p))
18552 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
18554 (defun org-timestamp-down-day (&optional arg
)
18555 "Decrease the date in the time stamp by one day.
18556 With prefix ARG, change that many days."
18558 (if (and (not (org-at-timestamp-p t
))
18559 (org-on-heading-p))
18561 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
18563 (defsubst org-pos-in-match-range
(pos n
)
18564 (and (match-beginning n
)
18565 (<= (match-beginning n
) pos
)
18566 (>= (match-end n
) pos
)))
18568 (defun org-at-timestamp-p (&optional inactive-ok
)
18569 "Determine if the cursor is in or at a timestamp."
18571 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
18573 (ans (or (looking-at tsr
)
18575 (skip-chars-backward "^[<\n\r\t")
18576 (if (> (point) (point-min)) (backward-char 1))
18577 (and (looking-at tsr
)
18578 (> (- (match-end 0) pos
) -
1))))))
18580 (boundp 'org-ts-what
)
18583 ((= pos
(match-beginning 0)) 'bracket
)
18584 ((= pos
(1- (match-end 0))) 'bracket
)
18585 ((org-pos-in-match-range pos
2) 'year
)
18586 ((org-pos-in-match-range pos
3) 'month
)
18587 ((org-pos-in-match-range pos
7) 'hour
)
18588 ((org-pos-in-match-range pos
8) 'minute
)
18589 ((or (org-pos-in-match-range pos
4)
18590 (org-pos-in-match-range pos
5)) 'day
)
18591 ((and (> pos
(or (match-end 8) (match-end 5)))
18592 (< pos
(match-end 0)))
18593 (- pos
(or (match-end 8) (match-end 5))))
18597 (defun org-toggle-timestamp-type ()
18598 "Toggle the type (<active> or [inactive]) of a time stamp."
18600 (when (org-at-timestamp-p t
)
18602 (goto-char (match-beginning 0))
18603 (insert (if (equal (char-after) ?
<) "[" "<")) (delete-char 1)
18604 (goto-char (1- (match-end 0)))
18605 (insert (if (equal (char-after) ?
>) "]" ">")) (delete-char 1))
18606 (message "Timestamp is now %sactive"
18607 (if (equal (char-before) ?
>) "in" ""))))
18609 (defun org-timestamp-change (n &optional what
)
18610 "Change the date in the time stamp at point.
18611 The date will be changed by N times WHAT. WHAT can be `day', `month',
18612 `year', `minute', `second'. If WHAT is not given, the cursor position
18613 in the timestamp determines what will be changed."
18614 (let ((pos (point))
18616 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
18620 (if (not (org-at-timestamp-p t
))
18621 (error "Not at a timestamp"))
18622 (if (and (not what
) (eq org-ts-what
'bracket
))
18623 (org-toggle-timestamp-type)
18624 (if (and (not what
) (not (eq org-ts-what
'day
))
18625 org-display-custom-times
18626 (get-text-property (point) 'display
)
18627 (not (get-text-property (1- (point)) 'display
)))
18628 (setq org-ts-what
'day
))
18629 (setq org-ts-what
(or what org-ts-what
)
18630 inactive
(= (char-after (match-beginning 0)) ?\
[)
18631 ts
(match-string 0))
18634 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[-+][0-9]+[dwmy]\\)*\\)[]>]"
18636 (setq extra
(match-string 1 ts
)))
18637 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
18639 (setq time0
(org-parse-time-string ts
))
18640 (when (and (eq org-ts-what
'minute
)
18641 (eq current-prefix-arg nil
))
18642 (setq n
(* dm
(org-no-warnings (signum n
))))
18643 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
18644 (setcar (cdr time0
) (+ (nth 1 time0
)
18645 (if (> n
0) (- rem
) (- dm rem
))))))
18647 (encode-time (or (car time0
) 0)
18648 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
18649 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
18650 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
18651 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
18652 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
18654 (when (integerp org-ts-what
)
18655 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
18656 (if (eq what
'calendar
)
18657 (let ((cal-date (org-get-date-from-calendar)))
18658 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
18659 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
18660 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
18661 (setcar time0
(or (car time0
) 0))
18662 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
18663 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
18664 (setq time
(apply 'encode-time time0
))))
18665 (setq org-last-changed-timestamp
18666 (org-insert-time-stamp time with-hm inactive nil nil extra
))
18667 (org-clock-update-time-maybe)
18669 ;; Try to recenter the calendar window, if any
18670 (if (and org-calendar-follow-timestamp-change
18671 (get-buffer-window "*Calendar*" t
)
18672 (memq org-ts-what
'(day month year
)))
18673 (org-recenter-calendar (time-to-days time
))))))
18675 ;; FIXME: does not yet work for lead times
18676 (defun org-modify-ts-extra (s pos n dm
)
18677 "Change the different parts of the lead-time and repeat fields in timestamp."
18678 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
18680 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
18682 ((or (org-pos-in-match-range pos
2)
18683 (org-pos-in-match-range pos
3))
18684 (setq m
(string-to-number (match-string 3 s
))
18685 h
(string-to-number (match-string 2 s
)))
18686 (if (org-pos-in-match-range pos
2)
18688 (setq n
(* dm
(org-no-warnings (signum n
))))
18689 (when (not (= 0 (setq rem
(% m dm
))))
18690 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
18692 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
18693 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
18694 (setq h
(min 24 (max 0 h
)))
18695 (setq ng
1 new
(format "-%02d:%02d" h m
)))
18696 ((org-pos-in-match-range pos
6)
18697 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
18698 ((org-pos-in-match-range pos
5)
18699 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
18701 ((org-pos-in-match-range pos
9)
18702 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
18703 ((org-pos-in-match-range pos
8)
18704 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
18708 (substring s
0 (match-beginning ng
))
18710 (substring s
(match-end ng
))))))
18713 (defun org-recenter-calendar (date)
18714 "If the calendar is visible, recenter it to DATE."
18715 (let* ((win (selected-window))
18716 (cwin (get-buffer-window "*Calendar*" t
))
18717 (calendar-move-hook nil
))
18719 (select-window cwin
)
18720 (calendar-goto-date (if (listp date
) date
18721 (calendar-gregorian-from-absolute date
)))
18722 (select-window win
))))
18724 (defun org-goto-calendar (&optional arg
)
18725 "Go to the Emacs calendar at the current date.
18726 If there is a time stamp in the current line, go to that date.
18727 A prefix ARG can be used to force the current date."
18729 (let ((tsr org-ts-regexp
) diff
18730 (calendar-move-hook nil
)
18731 (view-calendar-holidays-initially nil
)
18732 (view-diary-entries-initially nil
))
18733 (if (or (org-at-timestamp-p)
18735 (beginning-of-line 1)
18736 (looking-at (concat ".*" tsr
))))
18737 (let ((d1 (time-to-days (current-time)))
18739 (org-time-string-to-time (match-string 1)))))
18740 (setq diff
(- d2 d1
))))
18742 (calendar-goto-today)
18743 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
18745 (defun org-get-date-from-calendar ()
18746 "Return a list (month day year) of date at point in calendar."
18747 (with-current-buffer "*Calendar*"
18749 (calendar-cursor-to-date))))
18751 (defun org-date-from-calendar ()
18752 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
18753 If there is already a time stamp at the cursor position, update it."
18755 (if (org-at-timestamp-p t
)
18756 (org-timestamp-change 0 'calendar
)
18757 (let ((cal-date (org-get-date-from-calendar)))
18758 (org-insert-time-stamp
18759 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
18761 (defvar appt-time-msg-list
)
18764 (defun org-agenda-to-appt (&optional refresh filter
)
18765 "Activate appointments found in `org-agenda-files'.
18766 With a \\[universal-argument] prefix, refresh the list of
18769 If FILTER is t, interactively prompt the user for a regular
18770 expression, and filter out entries that don't match it.
18772 If FILTER is a string, use this string as a regular expression
18773 for filtering entries out.
18775 FILTER can also be an alist with the car of each cell being
18776 either 'headline or 'category. For example:
18778 '((headline \"IMPORTANT\")
18779 (category \"Work\"))
18781 will only add headlines containing IMPORTANT or headlines
18782 belonging to the \"Work\" category."
18784 (require 'calendar
)
18785 (if refresh
(setq appt-time-msg-list nil
))
18787 (setq filter
(read-from-minibuffer "Regexp filter: ")))
18788 (let* ((cnt 0) ; count added events
18789 (org-agenda-new-buffers nil
)
18790 (org-deadline-warning-days 0)
18791 (today (org-date-to-gregorian
18792 (time-to-days (current-time))))
18793 (files (org-agenda-files)) entries file
)
18794 ;; Get all entries which may contain an appt
18795 (while (setq file
(pop files
))
18798 (org-agenda-get-day-entries
18799 file today
:timestamp
:scheduled
:deadline
))))
18800 (setq entries
(delq nil entries
))
18801 ;; Map thru entries and find if we should filter them out
18804 (let* ((evt (org-trim (get-text-property 1 'txt x
)))
18805 (cat (get-text-property 1 'org-category x
))
18806 (tod (get-text-property 1 'time-of-day x
))
18807 (ok (or (null filter
)
18808 (and (stringp filter
) (string-match filter evt
))
18809 (and (listp filter
)
18811 (cadr (assoc 'category filter
)) cat
)
18813 (cadr (assoc 'headline filter
)) evt
))))))
18814 ;; FIXME: Shall we remove text-properties for the appt text?
18815 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
18817 (setq tod
(number-to-string tod
)
18818 tod
(when (string-match
18819 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)" tod
)
18820 (concat (match-string 1 tod
) ":"
18821 (match-string 2 tod
))))
18823 (setq cnt
(1+ cnt
))))) entries
)
18824 (org-release-buffers org-agenda-new-buffers
)
18826 (message "No event to add")
18827 (message "Added %d event%s for today" cnt
(if (> cnt
1) "s" "")))))
18829 ;;; The clock for measuring work time.
18831 (defvar org-mode-line-string
"")
18832 (put 'org-mode-line-string
'risky-local-variable t
)
18834 (defvar org-mode-line-timer nil
)
18835 (defvar org-clock-heading
"")
18836 (defvar org-clock-start-time
"")
18838 (defun org-update-mode-line ()
18839 (let* ((delta (- (time-to-seconds (current-time))
18840 (time-to-seconds org-clock-start-time
)))
18841 (h (floor delta
3600))
18842 (m (floor (- delta
(* 3600 h
)) 60)))
18843 (setq org-mode-line-string
18844 (propertize (format "-[%d:%02d (%s)]" h m org-clock-heading
)
18845 'help-echo
"Org-mode clock is running"))
18846 (force-mode-line-update)))
18848 (defvar org-clock-marker
(make-marker)
18849 "Marker recording the last clock-in.")
18850 (defvar org-clock-mode-line-entry nil
18851 "Information for the modeline about the running clock.")
18853 (defun org-clock-in ()
18854 "Start the clock on the current item.
18855 If necessary, clock-out of the currently active clock."
18860 (org-back-to-heading t
)
18861 (when (and org-clock-in-switch-to-state
18862 (not (looking-at (concat outline-regexp
"[ \t]*"
18863 org-clock-in-switch-to-state
18865 (org-todo org-clock-in-switch-to-state
))
18866 (if (and org-clock-heading-function
18867 (functionp org-clock-heading-function
))
18868 (setq org-clock-heading
(funcall org-clock-heading-function
))
18869 (if (looking-at org-complex-heading-regexp
)
18870 (setq org-clock-heading
(match-string 4))
18871 (setq org-clock-heading
"???")))
18872 (setq org-clock-heading
(propertize org-clock-heading
'face nil
))
18873 (org-clock-find-position)
18875 (insert "\n") (backward-char 1)
18877 (insert org-clock-string
" ")
18878 (setq org-clock-start-time
(current-time))
18879 (setq ts
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
18880 (move-marker org-clock-marker
(point) (buffer-base-buffer))
18881 (or global-mode-string
(setq global-mode-string
'("")))
18882 (or (memq 'org-mode-line-string global-mode-string
)
18883 (setq global-mode-string
18884 (append global-mode-string
'(org-mode-line-string))))
18885 (org-update-mode-line)
18886 (setq org-mode-line-timer
(run-with-timer 60 60 'org-update-mode-line
))
18887 (message "Clock started at %s" ts
))))
18889 (defun org-clock-find-position ()
18890 "Find the location where the next clock line should be inserted."
18891 (org-back-to-heading t
)
18893 (let ((beg (point-at-bol 2)) (end (progn (outline-next-heading) (point)))
18894 (re (concat "^[ \t]*" org-clock-string
))
18898 (when (eobp) (newline) (setq end
(max (point) end
)))
18899 (when (re-search-forward "^[ \t]*:CLOCK:" end t
)
18900 ;; we seem to have a CLOCK drawer, so go there.
18901 (beginning-of-line 2)
18903 ;; Lets count the CLOCK lines
18905 (while (re-search-forward re end t
)
18906 (setq first
(or first
(match-beginning 0))
18907 last
(match-beginning 0)
18909 (when (and (integerp org-clock-into-drawer
)
18910 (>= (1+ cnt
) org-clock-into-drawer
))
18911 ;; Wrap current entries into a new drawer
18913 (beginning-of-line 2)
18914 (if (org-at-item-p) (org-end-of-item))
18916 (beginning-of-line 0)
18917 (org-indent-line-function)
18919 (insert ":CLOCK:\n")
18920 (beginning-of-line 0)
18921 (org-indent-line-function)
18922 (org-flag-drawer t
)
18923 (beginning-of-line 2)
18927 (while (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
18928 (not (equal (match-string 1) org-clock-string
)))
18929 ;; Planning info, skip to after it
18930 (beginning-of-line 2)
18931 (or (bolp) (newline)))
18932 (when (eq t org-clock-into-drawer
)
18933 (insert ":CLOCK:\n:END:\n")
18934 (beginning-of-line -
1)
18935 (org-indent-line-function)
18936 (org-flag-drawer t
)
18937 (beginning-of-line 2)
18938 (org-indent-line-function)))))
18940 (defun org-clock-out (&optional fail-quietly
)
18941 "Stop the currently running clock.
18942 If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
18945 (if (not (marker-buffer org-clock-marker
))
18946 (if fail-quietly
(throw 'exit t
) (error "No active clock")))
18949 (set-buffer (marker-buffer org-clock-marker
))
18950 (goto-char org-clock-marker
)
18951 (beginning-of-line 1)
18952 (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp
))
18953 (equal (match-string 1) org-clock-string
))
18954 (setq ts
(match-string 2))
18955 (if fail-quietly
(throw 'exit nil
) (error "Clock start time is gone")))
18956 (goto-char (match-end 0))
18957 (delete-region (point) (point-at-eol))
18959 (setq te
(org-insert-time-stamp (current-time) 'with-hm
'inactive
))
18960 (setq s
(- (time-to-seconds (apply 'encode-time
(org-parse-time-string te
)))
18961 (time-to-seconds (apply 'encode-time
(org-parse-time-string ts
))))
18962 h
(floor (/ s
3600))
18966 (insert " => " (format "%2d:%02d" h m
))
18967 (move-marker org-clock-marker nil
)
18968 (when org-log-note-clock-out
18969 (org-add-log-maybe 'clock-out
))
18970 (when org-mode-line-timer
18971 (cancel-timer org-mode-line-timer
)
18972 (setq org-mode-line-timer nil
))
18973 (setq global-mode-string
18974 (delq 'org-mode-line-string global-mode-string
))
18975 (force-mode-line-update)
18976 (message "Clock stopped at %s after HH:MM = %d:%02d" te h m
)))))
18978 (defun org-clock-cancel ()
18979 "Cancel the running clock be removing the start timestamp."
18981 (if (not (marker-buffer org-clock-marker
))
18982 (error "No active clock"))
18984 (set-buffer (marker-buffer org-clock-marker
))
18985 (goto-char org-clock-marker
)
18986 (delete-region (1- (point-at-bol)) (point-at-eol)))
18987 (setq global-mode-string
18988 (delq 'org-mode-line-string global-mode-string
))
18989 (force-mode-line-update)
18990 (message "Clock canceled"))
18992 (defun org-clock-goto (&optional delete-windows
)
18993 "Go to the currently clocked-in entry."
18995 (if (not (marker-buffer org-clock-marker
))
18996 (error "No active clock"))
18997 (switch-to-buffer-other-window
18998 (marker-buffer org-clock-marker
))
18999 (if delete-windows
(delete-other-windows))
19000 (goto-char org-clock-marker
)
19002 (org-back-to-heading)
19005 (defvar org-clock-file-total-minutes nil
19006 "Holds the file total time in minutes, after a call to `org-clock-sum'.")
19007 (make-variable-buffer-local 'org-clock-file-total-minutes
)
19009 (defun org-clock-sum (&optional tstart tend
)
19010 "Sum the times for each subtree.
19011 Puts the resulting times in minutes as a text property on each headline."
19013 (let* ((bmp (buffer-modified-p))
19014 (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
19016 "[ \t]*\\(?:\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)\\|=>[ \t]+\\([0-9]+\\):\\([0-9]+\\)\\)"))
19018 (ltimes (make-vector lmax
0))
19023 (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t
))
19025 (goto-char (point-max))
19026 (while (re-search-backward re nil t
)
19030 (setq ts
(match-string 2)
19031 te
(match-string 3)
19032 ts
(time-to-seconds
19033 (apply 'encode-time
(org-parse-time-string ts
)))
19034 te
(time-to-seconds
19035 (apply 'encode-time
(org-parse-time-string te
)))
19036 ts
(if tstart
(max ts tstart
) ts
)
19037 te
(if tend
(min te tend
) te
)
19039 t1
(if (> dt
0) (+ t1
(floor (/ dt
60))) t1
)))
19042 (setq t1
(+ t1
(string-to-number (match-string 5))
19043 (* 60 (string-to-number (match-string 4))))))
19045 (setq level
(- (match-end 1) (match-beginning 1)))
19046 (when (or (> t1
0) (> (aref ltimes level
) 0))
19047 (loop for l from
0 to level do
19048 (aset ltimes l
(+ (aref ltimes l
) t1
)))
19049 (setq t1
0 time
(aref ltimes level
))
19050 (loop for l from level to
(1- lmax
) do
19052 (goto-char (match-beginning 0))
19053 (put-text-property (point) (point-at-eol) :org-clock-minutes time
)))))
19054 (setq org-clock-file-total-minutes
(aref ltimes
0)))
19055 (set-buffer-modified-p bmp
)))
19057 (defun org-clock-display (&optional total-only
)
19058 "Show subtree times in the entire buffer.
19059 If TOTAL-ONLY is non-nil, only show the total time for the entire file
19062 (org-remove-clock-overlays)
19067 (goto-char (point-min))
19068 (while (or (and (equal (setq p
(point)) (point-min))
19069 (get-text-property p
:org-clock-minutes
))
19070 (setq p
(next-single-property-change
19071 (point) :org-clock-minutes
)))
19073 (when (setq time
(get-text-property p
:org-clock-minutes
))
19074 (org-put-clock-overlay time
(funcall outline-level
))))
19075 (setq h
(/ org-clock-file-total-minutes
60)
19076 m
(- org-clock-file-total-minutes
(* 60 h
)))
19077 ;; Arrange to remove the overlays upon next change.
19078 (when org-remove-highlights-with-change
19079 (org-add-hook 'before-change-functions
'org-remove-clock-overlays
19081 (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m
)))
19083 (defvar org-clock-overlays nil
)
19084 (make-variable-buffer-local 'org-clock-overlays
)
19086 (defun org-put-clock-overlay (time &optional level
)
19087 "Put an overlays on the current line, displaying TIME.
19088 If LEVEL is given, prefix time with a corresponding number of stars.
19089 This creates a new overlay and stores it in `org-clock-overlays', so that it
19090 will be easy to remove."
19091 (let* ((c 60) (h (floor (/ time
60))) (m (- time
(* 60 h
)))
19092 (l (if level
(org-get-legal-level level
0) 0))
19096 (unless (eolp) (skip-chars-backward "^ \t"))
19097 (skip-chars-backward " \t")
19098 (setq ov
(org-make-overlay (1- (point)) (point-at-eol))
19099 tx
(concat (buffer-substring (1- (point)) (point))
19100 (make-string (+ off
(max 0 (- c
(current-column)))) ?.
)
19101 (org-add-props (format "%s %2d:%02d%s"
19102 (make-string l ?
*) h m
19103 (make-string (- 16 l
) ?\
))
19104 '(face secondary-selection
))
19106 (if (not (featurep 'xemacs
))
19107 (org-overlay-put ov
'display tx
)
19108 (org-overlay-put ov
'invisible t
)
19109 (org-overlay-put ov
'end-glyph
(make-glyph tx
)))
19110 (push ov org-clock-overlays
)))
19112 (defun org-remove-clock-overlays (&optional beg end noremove
)
19113 "Remove the occur highlights from the buffer.
19114 BEG and END are ignored. If NOREMOVE is nil, remove this function
19115 from the `before-change-functions' in the current buffer."
19117 (unless org-inhibit-highlight-removal
19118 (mapc 'org-delete-overlay org-clock-overlays
)
19119 (setq org-clock-overlays nil
)
19121 (remove-hook 'before-change-functions
19122 'org-remove-clock-overlays
'local
))))
19124 (defun org-clock-out-if-current ()
19125 "Clock out if the current entry contains the running clock.
19126 This is used to stop the clock after a TODO entry is marked DONE,
19127 and is only done if the variable `org-clock-out-when-done' is not nil."
19128 (when (and org-clock-out-when-done
19129 (member state org-done-keywords
)
19130 (equal (marker-buffer org-clock-marker
) (current-buffer))
19131 (< (point) org-clock-marker
)
19132 (> (save-excursion (outline-next-heading) (point))
19134 ;; Clock out, but don't accept a logging message for this.
19135 (let ((org-log-note-clock-out nil
))
19138 (add-hook 'org-after-todo-state-change-hook
19139 'org-clock-out-if-current
)
19141 (defun org-check-running-clock ()
19142 "Check if the current buffer contains the running clock.
19143 If yes, offer to stop it and to save the buffer with the changes."
19144 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
19145 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
19148 (when (y-or-n-p "Save changed buffer?")
19151 (defun org-clock-report (&optional arg
)
19152 "Create a table containing a report about clocked time.
19153 If the cursor is inside an existing clocktable block, then the table
19154 will be updated. If not, a new clocktable will be inserted.
19155 When called with a prefix argument, move to the first clock table in the
19156 buffer and update it."
19158 (org-remove-clock-overlays)
19160 (org-find-dblock "clocktable")
19162 (if (org-in-clocktable-p)
19163 (goto-char (org-in-clocktable-p))
19164 (org-create-dblock (list :name
"clocktable"
19165 :maxlevel
2 :scope
'file
)))
19166 (org-update-dblock))
19168 (defun org-in-clocktable-p ()
19169 "Check if the cursor is in a clocktable."
19170 (let ((pos (point)) start
)
19173 (and (re-search-backward "^#\\+BEGIN:[ \t]+clocktable" nil t
)
19174 (setq start
(match-beginning 0))
19175 (re-search-forward "^#\\+END:.*" nil t
)
19176 (>= (match-end 0) pos
)
19179 (defun org-clock-update-time-maybe ()
19180 "If this is a CLOCK line, update it and return t.
19181 Otherwise, return nil."
19184 (beginning-of-line 1)
19185 (skip-chars-forward " \t")
19186 (when (looking-at org-clock-string
)
19187 (let ((re (concat "[ \t]*" org-clock-string
19188 " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
19189 "\\([ \t]*=>.*\\)?"))
19191 (if (not (looking-at re
))
19193 (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
19195 (setq ts
(match-string 1)
19196 te
(match-string 2))
19197 (setq s
(- (time-to-seconds
19198 (apply 'encode-time
(org-parse-time-string te
)))
19200 (apply 'encode-time
(org-parse-time-string ts
))))
19201 h
(floor (/ s
3600))
19205 (insert " => " (format "%2d:%02d" h m
))
19208 (defun org-clock-special-range (key &optional time as-strings
)
19209 "Return two times bordering a special time range.
19210 Key is a symbol specifying the range and can be one of `today', `yesterday',
19211 `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
19212 A week starts Monday 0:00 and ends Sunday 24:00.
19213 The range is determined relative to TIME. TIME defaults to the current time.
19214 The return value is a cons cell with two internal times like the ones
19215 returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
19216 the returned times will be formatted strings."
19217 (let* ((tm (decode-time (or time
(current-time))))
19218 (s 0) (m (nth 1 tm
)) (h (nth 2 tm
))
19219 (d (nth 3 tm
)) (month (nth 4 tm
)) (y (nth 5 tm
))
19221 s1 m1 h1 d1 month1 y1 diff ts te fm
)
19224 (setq h
0 m
0 h1
24 m1
0))
19225 ((eq key
'yesterday
)
19226 (setq d
(1- d
) h
0 m
0 h1
24 m1
0))
19227 ((eq key
'thisweek
)
19228 (setq diff
(if (= dow
0) 6 (1- dow
))
19229 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
19230 ((eq key
'lastweek
)
19231 (setq diff
(+ 7 (if (= dow
0) 6 (1- dow
)))
19232 m
0 h
0 d
(- d diff
) d1
(+ 7 d
)))
19233 ((eq key
'thismonth
)
19234 (setq d
1 h
0 m
0 d1
1 month1
(1+ month
) h1
0 m1
0))
19235 ((eq key
'lastmonth
)
19236 (setq d
1 h
0 m
0 d1
1 month
(1- month
) month1
(1+ month
) h1
0 m1
0))
19237 ((eq key
'thisyear
)
19238 (setq m
0 h
0 d
1 month
1 y1
(1+ y
)))
19239 ((eq key
'lastyear
)
19240 (setq m
0 h
0 d
1 month
1 y
(1- y
) y1
(1+ y
)))
19241 (t (error "No such time block %s" key
)))
19242 (setq ts
(encode-time s m h d month y
)
19243 te
(encode-time (or s1 s
) (or m1 m
) (or h1 h
)
19244 (or d1 d
) (or month1 month
) (or y1 y
)))
19245 (setq fm
(cdr org-time-stamp-formats
))
19247 (cons (format-time-string fm ts
) (format-time-string fm te
))
19250 (defun org-dblock-write:clocktable
(params)
19251 "Write the standard clocktable."
19253 (let* ((hlchars '((1 .
"*") (2 .
"/")))
19254 (ins (make-marker))
19256 (scope (plist-get params
:scope
))
19257 (tostring (plist-get params
:tostring
))
19258 (multifile (plist-get params
:multifile
))
19259 (header (plist-get params
:header
))
19260 (maxlevel (or (plist-get params
:maxlevel
) 3))
19261 (step (plist-get params
:step
))
19262 (emph (plist-get params
:emphasize
))
19263 (ts (plist-get params
:tstart
))
19264 (te (plist-get params
:tend
))
19265 (block (plist-get params
:block
))
19266 ipos time h m p level hlc hdl
19267 cc beg end pos tbl
)
19269 (org-clocktable-steps params
)
19272 (setq cc
(org-clock-special-range block nil t
)
19273 ts
(car cc
) te
(cdr cc
)))
19274 (if ts
(setq ts
(time-to-seconds
19275 (apply 'encode-time
(org-parse-time-string ts
)))))
19276 (if te
(setq te
(time-to-seconds
19277 (apply 'encode-time
(org-parse-time-string te
)))))
19278 (move-marker ins
(point))
19279 (setq ipos
(point))
19281 ;; Get the right scope
19286 ((eq scope
'file
) (widen))
19287 ((eq scope
'subtree
) (org-narrow-to-subtree))
19289 (while (org-up-heading-safe))
19290 (org-narrow-to-subtree))
19291 ((and (symbolp scope
) (string-match "^tree\\([0-9]+\\)$"
19292 (symbol-name scope
)))
19293 (setq level
(string-to-number (match-string 1 (symbol-name scope
))))
19295 (while (org-up-heading-safe)
19296 (looking-at outline-regexp
)
19297 (if (<= (org-reduced-level (funcall outline-level
)) level
)
19298 (throw 'exit nil
))))
19299 (org-narrow-to-subtree))
19300 ((or (listp scope
) (eq scope
'agenda
))
19301 (let* ((files (if (listp scope
) scope
(org-agenda-files)))
19303 (p1 (copy-sequence params
))
19305 (plist-put p1
:tostring t
)
19306 (plist-put p1
:multifile t
)
19307 (plist-put p1
:scope
'file
)
19308 (org-prepare-agenda-buffers files
)
19309 (while (setq file
(pop files
))
19310 (with-current-buffer (find-buffer-visiting file
)
19311 (push (org-clocktable-add-file
19312 file
(org-dblock-write:clocktable p1
)) tbl
)
19313 (setq total-time
(+ (or total-time
0)
19314 org-clock-file-total-minutes
)))))))
19317 (unless (eq scope
'agenda
)
19318 (org-clock-sum ts te
)
19319 (goto-char (point-min))
19320 (while (setq p
(next-single-property-change (point) :org-clock-minutes
))
19322 (when (setq time
(get-text-property p
:org-clock-minutes
))
19324 (beginning-of-line 1)
19325 (when (and (looking-at (org-re "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[[:alnum:]_@:]+:\\)?[ \t]*$"))
19326 (setq level
(org-reduced-level
19327 (- (match-end 1) (match-beginning 1))))
19328 (<= level maxlevel
))
19329 (setq hlc
(if emph
(or (cdr (assoc level hlchars
)) "") "")
19330 hdl
(match-string 2)
19332 m
(- time
(* 60 h
)))
19333 (if (and (not multifile
) (= level
1)) (push "|-" tbl
))
19335 "| " (int-to-string level
) "|" hlc hdl hlc
" |"
19336 (make-string (1- level
) ?|
)
19337 hlc
(format "%d:%02d" h m
) hlc
19339 (setq tbl
(nreverse tbl
))
19341 (if tbl
(mapconcat 'identity tbl
"\n") nil
)
19343 (insert-before-markers
19346 "Clock summary at ["
19348 (format-time-string (cdr org-time-stamp-formats
))
19352 (format " Considered range is /%s/." block
)
19355 (if (eq scope
'agenda
) "|File" "")
19356 "|L|Headline|Time|\n")
19357 (setq total-time
(or total-time org-clock-file-total-minutes
)
19358 h
(/ total-time
60)
19359 m
(- total-time
(* 60 h
)))
19360 (insert-before-markers
19362 (if (eq scope
'agenda
) "|" "")
19365 (format "*%d:%02d*" h m
)
19367 (setq tbl
(delq nil tbl
))
19368 (if (and (stringp (car tbl
)) (> (length (car tbl
)) 1)
19369 (equal (substring (car tbl
) 0 2) "|-"))
19371 (insert-before-markers (mapconcat
19372 'identity
(delq nil tbl
)
19373 (if (eq scope
'agenda
) "\n|-\n" "\n")))
19374 (backward-delete-char 1)
19376 (skip-chars-forward "^|")
19377 (org-table-align))))))
19379 (defun org-clocktable-steps (params)
19380 (let* ((p1 (copy-sequence params
))
19381 (ts (plist-get p1
:tstart
))
19382 (te (plist-get p1
:tend
))
19383 (step0 (plist-get p1
:step
))
19384 (step (cdr (assoc step0
'((day .
86400) (week .
604800)))))
19385 (block (plist-get p1
:block
))
19388 (setq cc
(org-clock-special-range block nil t
)
19389 ts
(car cc
) te
(cdr cc
)))
19390 (if ts
(setq ts
(time-to-seconds
19391 (apply 'encode-time
(org-parse-time-string ts
)))))
19392 (if te
(setq te
(time-to-seconds
19393 (apply 'encode-time
(org-parse-time-string te
)))))
19394 (plist-put p1
:header
"")
19395 (plist-put p1
:step nil
)
19396 (plist-put p1
:block nil
)
19398 (or (bolp) (insert "\n"))
19399 (plist-put p1
:tstart
(format-time-string
19400 (car org-time-stamp-formats
)
19401 (seconds-to-time ts
)))
19402 (plist-put p1
:tend
(format-time-string
19403 (car org-time-stamp-formats
)
19404 (seconds-to-time (setq ts
(+ ts step
)))))
19405 (insert "\n" (if (eq step0
'day
) "Daily report: " "Weekly report starting on: ")
19406 (plist-get p1
:tstart
) "\n")
19407 (org-dblock-write:clocktable p1
)
19408 (re-search-forward "#\\+END:")
19412 (defun org-clocktable-add-file (file table
)
19414 (let ((lines (org-split-string table
"\n"))
19415 (ff (file-name-nondirectory file
)))
19416 (mapconcat 'identity
19417 (mapcar (lambda (x)
19418 (if (string-match org-table-dataline-regexp x
)
19424 ;; FIXME: I don't think anybody uses this, ask David
19425 (defun org-collect-clock-time-entries ()
19426 "Return an internal list with clocking information.
19427 This list has one entry for each CLOCK interval.
19428 FIXME: describe the elements."
19430 (let ((re (concat "^[ \t]*" org-clock-string
19431 " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
19432 rtn beg end next cont level title total closedp leafp
19433 clockpos titlepos h m donep
)
19436 (goto-char (point-min))
19437 (while (re-search-forward re nil t
)
19438 (setq clockpos
(match-beginning 0)
19439 beg
(match-string 1) end
(match-string 2)
19440 cont
(match-end 0))
19441 (setq beg
(apply 'encode-time
(org-parse-time-string beg
))
19442 end
(apply 'encode-time
(org-parse-time-string end
)))
19443 (org-back-to-heading t
)
19444 (setq donep
(org-entry-is-done-p))
19445 (setq titlepos
(point)
19446 total
(or (get-text-property (1+ (point)) :org-clock-minutes
) 0)
19447 h
(/ total
60) m
(- total
(* 60 h
))
19449 (looking-at "\\(\\*+\\) +\\(.*\\)")
19450 (setq level
(- (match-end 1) (match-beginning 1))
19451 title
(org-match-string-no-properties 2))
19452 (save-excursion (outline-next-heading) (setq next
(point)))
19453 (setq closedp
(re-search-forward org-closed-time-regexp next t
))
19455 (setq leafp
(and (looking-at "^\\*+ ")
19456 (<= (- (match-end 0) (point)) level
)))
19457 (push (list beg end clockpos closedp donep
19458 total title titlepos level leafp
)
19463 ;;;; Agenda, and Diary Integration
19465 ;;; Define the Org-agenda-mode
19467 (defvar org-agenda-mode-map
(make-sparse-keymap)
19468 "Keymap for `org-agenda-mode'.")
19470 (defvar org-agenda-menu
) ; defined later in this file.
19471 (defvar org-agenda-follow-mode nil
)
19472 (defvar org-agenda-show-log nil
)
19473 (defvar org-agenda-redo-command nil
)
19474 (defvar org-agenda-query-string nil
)
19475 (defvar org-agenda-mode-hook nil
)
19476 (defvar org-agenda-type nil
)
19477 (defvar org-agenda-force-single-file nil
)
19479 (defun org-agenda-mode ()
19480 "Mode for time-sorted view on action items in Org-mode files.
19482 The following commands are available:
19484 \\{org-agenda-mode-map}"
19486 (kill-all-local-variables)
19487 (setq org-agenda-undo-list nil
19488 org-agenda-pending-undo-list nil
)
19489 (setq major-mode
'org-agenda-mode
)
19490 ;; Keep global-font-lock-mode from turning on font-lock-mode
19491 (org-set-local 'font-lock-global-modes
(list 'not major-mode
))
19492 (setq mode-name
"Org-Agenda")
19493 (use-local-map org-agenda-mode-map
)
19494 (easy-menu-add org-agenda-menu
)
19495 (if org-startup-truncated
(setq truncate-lines t
))
19496 (org-add-hook 'post-command-hook
'org-agenda-post-command-hook nil
'local
)
19497 (org-add-hook 'pre-command-hook
'org-unhighlight nil
'local
)
19498 ;; Make sure properties are removed when copying text
19499 (when (boundp 'buffer-substring-filters
)
19500 (org-set-local 'buffer-substring-filters
19502 (set-text-properties 0 (length x
) nil x
) x
)
19503 buffer-substring-filters
)))
19504 (unless org-agenda-keep-modes
19505 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
19506 org-agenda-show-log nil
))
19508 '("Agenda") "Agenda Files"
19512 (if (get 'org-agenda-files
'org-restrict
)
19513 "Restricted to single file"
19515 '(org-edit-agenda-file-list)
19516 (not (get 'org-agenda-files
'org-restrict
)))
19518 (mapcar 'org-file-menu-entry
(org-agenda-files))))
19519 (org-agenda-set-mode-name)
19521 (if (fboundp 'run-mode-hooks
) 'run-mode-hooks
'run-hooks
)
19522 (list 'org-agenda-mode-hook
)))
19524 (substitute-key-definition 'undo
'org-agenda-undo
19525 org-agenda-mode-map global-map
)
19526 (org-defkey org-agenda-mode-map
"\C-i" 'org-agenda-goto
)
19527 (org-defkey org-agenda-mode-map
[(tab)] 'org-agenda-goto
)
19528 (org-defkey org-agenda-mode-map
"\C-m" 'org-agenda-switch-to
)
19529 (org-defkey org-agenda-mode-map
"\C-k" 'org-agenda-kill
)
19530 (org-defkey org-agenda-mode-map
"\C-c$" 'org-agenda-archive
)
19531 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-s" 'org-agenda-archive
)
19532 (org-defkey org-agenda-mode-map
"$" 'org-agenda-archive
)
19533 (org-defkey org-agenda-mode-map
"\C-c\C-o" 'org-agenda-open-link
)
19534 (org-defkey org-agenda-mode-map
" " 'org-agenda-show
)
19535 (org-defkey org-agenda-mode-map
"\C-c\C-t" 'org-agenda-todo
)
19536 (org-defkey org-agenda-mode-map
[(control shift right
)] 'org-agenda-todo-nextset
)
19537 (org-defkey org-agenda-mode-map
[(control shift left
)] 'org-agenda-todo-previousset
)
19538 (org-defkey org-agenda-mode-map
"\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer
)
19539 (org-defkey org-agenda-mode-map
"b" 'org-agenda-tree-to-indirect-buffer
)
19540 (org-defkey org-agenda-mode-map
"o" 'delete-other-windows
)
19541 (org-defkey org-agenda-mode-map
"L" 'org-agenda-recenter
)
19542 (org-defkey org-agenda-mode-map
"t" 'org-agenda-todo
)
19543 (org-defkey org-agenda-mode-map
"a" 'org-agenda-toggle-archive-tag
)
19544 (org-defkey org-agenda-mode-map
":" 'org-agenda-set-tags
)
19545 (org-defkey org-agenda-mode-map
"." 'org-agenda-goto-today
)
19546 (org-defkey org-agenda-mode-map
"j" 'org-agenda-goto-date
)
19547 (org-defkey org-agenda-mode-map
"d" 'org-agenda-day-view
)
19548 (org-defkey org-agenda-mode-map
"w" 'org-agenda-week-view
)
19549 (org-defkey org-agenda-mode-map
"m" 'org-agenda-month-view
)
19550 (org-defkey org-agenda-mode-map
"y" 'org-agenda-year-view
)
19551 (org-defkey org-agenda-mode-map
[(shift right
)] 'org-agenda-date-later
)
19552 (org-defkey org-agenda-mode-map
[(shift left
)] 'org-agenda-date-earlier
)
19553 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(right)] 'org-agenda-date-later
)
19554 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(left)] 'org-agenda-date-earlier
)
19556 (org-defkey org-agenda-mode-map
">" 'org-agenda-date-prompt
)
19557 (org-defkey org-agenda-mode-map
"\C-c\C-s" 'org-agenda-schedule
)
19558 (org-defkey org-agenda-mode-map
"\C-c\C-d" 'org-agenda-deadline
)
19559 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
19560 (while l
(org-defkey org-agenda-mode-map
19561 (int-to-string (pop l
)) 'digit-argument
)))
19563 (org-defkey org-agenda-mode-map
"f" 'org-agenda-follow-mode
)
19564 (org-defkey org-agenda-mode-map
"l" 'org-agenda-log-mode
)
19565 (org-defkey org-agenda-mode-map
"D" 'org-agenda-toggle-diary
)
19566 (org-defkey org-agenda-mode-map
"G" 'org-agenda-toggle-time-grid
)
19567 (org-defkey org-agenda-mode-map
"r" 'org-agenda-redo
)
19568 (org-defkey org-agenda-mode-map
"g" 'org-agenda-redo
)
19569 (org-defkey org-agenda-mode-map
"e" 'org-agenda-execute
)
19570 (org-defkey org-agenda-mode-map
"q" 'org-agenda-quit
)
19571 (org-defkey org-agenda-mode-map
"x" 'org-agenda-exit
)
19572 (org-defkey org-agenda-mode-map
"\C-x\C-w" 'org-write-agenda
)
19573 (org-defkey org-agenda-mode-map
"s" 'org-save-all-org-buffers
)
19574 (org-defkey org-agenda-mode-map
"\C-x\C-s" 'org-save-all-org-buffers
)
19575 (org-defkey org-agenda-mode-map
"P" 'org-agenda-show-priority
)
19576 (org-defkey org-agenda-mode-map
"T" 'org-agenda-show-tags
)
19577 (org-defkey org-agenda-mode-map
"n" 'next-line
)
19578 (org-defkey org-agenda-mode-map
"p" 'previous-line
)
19579 (org-defkey org-agenda-mode-map
"\C-c\C-n" 'org-agenda-next-date-line
)
19580 (org-defkey org-agenda-mode-map
"\C-c\C-p" 'org-agenda-previous-date-line
)
19581 (org-defkey org-agenda-mode-map
"," 'org-agenda-priority
)
19582 (org-defkey org-agenda-mode-map
"\C-c," 'org-agenda-priority
)
19583 (org-defkey org-agenda-mode-map
"i" 'org-agenda-diary-entry
)
19584 (org-defkey org-agenda-mode-map
"c" 'org-agenda-goto-calendar
)
19585 (eval-after-load "calendar"
19586 '(org-defkey calendar-mode-map org-calendar-to-agenda-key
19587 'org-calendar-goto-agenda
))
19588 (org-defkey org-agenda-mode-map
"C" 'org-agenda-convert-date
)
19589 (org-defkey org-agenda-mode-map
"M" 'org-agenda-phases-of-moon
)
19590 (org-defkey org-agenda-mode-map
"S" 'org-agenda-sunrise-sunset
)
19591 (org-defkey org-agenda-mode-map
"h" 'org-agenda-holidays
)
19592 (org-defkey org-agenda-mode-map
"H" 'org-agenda-holidays
)
19593 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-i" 'org-agenda-clock-in
)
19594 (org-defkey org-agenda-mode-map
"I" 'org-agenda-clock-in
)
19595 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-o" 'org-agenda-clock-out
)
19596 (org-defkey org-agenda-mode-map
"O" 'org-agenda-clock-out
)
19597 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-x" 'org-agenda-clock-cancel
)
19598 (org-defkey org-agenda-mode-map
"X" 'org-agenda-clock-cancel
)
19599 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-j" 'org-clock-goto
)
19600 (org-defkey org-agenda-mode-map
"J" 'org-clock-goto
)
19601 (org-defkey org-agenda-mode-map
"+" 'org-agenda-priority-up
)
19602 (org-defkey org-agenda-mode-map
"-" 'org-agenda-priority-down
)
19603 (org-defkey org-agenda-mode-map
[(shift up
)] 'org-agenda-priority-up
)
19604 (org-defkey org-agenda-mode-map
[(shift down
)] 'org-agenda-priority-down
)
19605 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(up)] 'org-agenda-priority-up
)
19606 (org-defkey org-agenda-mode-map
[?\C-c ?\C-x
(down)] 'org-agenda-priority-down
)
19607 (org-defkey org-agenda-mode-map
[(right)] 'org-agenda-later
)
19608 (org-defkey org-agenda-mode-map
[(left)] 'org-agenda-earlier
)
19609 (org-defkey org-agenda-mode-map
"\C-c\C-x\C-c" 'org-agenda-columns
)
19611 (org-defkey org-agenda-mode-map
"[" 'org-agenda-manipulate-query-add
)
19612 (org-defkey org-agenda-mode-map
"]" 'org-agenda-manipulate-query-subtract
)
19613 (org-defkey org-agenda-mode-map
"{" 'org-agenda-manipulate-query-add-re
)
19614 (org-defkey org-agenda-mode-map
"}" 'org-agenda-manipulate-query-subtract-re
)
19616 (defvar org-agenda-keymap
(copy-keymap org-agenda-mode-map
)
19617 "Local keymap for agenda entries from Org-mode.")
19619 (org-defkey org-agenda-keymap
19620 (if (featurep 'xemacs
) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse
)
19621 (org-defkey org-agenda-keymap
19622 (if (featurep 'xemacs
) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse
)
19623 (when org-agenda-mouse-1-follows-link
19624 (org-defkey org-agenda-keymap
[follow-link
] 'mouse-face
))
19625 (easy-menu-define org-agenda-menu org-agenda-mode-map
"Agenda menu"
19629 ["Show" org-agenda-show t
]
19630 ["Go To (other window)" org-agenda-goto t
]
19631 ["Go To (this window)" org-agenda-switch-to t
]
19632 ["Follow Mode" org-agenda-follow-mode
19633 :style toggle
:selected org-agenda-follow-mode
:active t
]
19634 ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t
]
19636 ["Cycle TODO" org-agenda-todo t
]
19637 ["Archive subtree" org-agenda-archive t
]
19638 ["Delete subtree" org-agenda-kill t
]
19640 ["Goto Today" org-agenda-goto-today
(org-agenda-check-type nil
'agenda
'timeline
)]
19641 ["Next Dates" org-agenda-later
(org-agenda-check-type nil
'agenda
)]
19642 ["Previous Dates" org-agenda-earlier
(org-agenda-check-type nil
'agenda
)]
19643 ["Jump to date" org-agenda-goto-date
(org-agenda-check-type nil
'agenda
)]
19645 ("Tags and Properties"
19646 ["Show all Tags" org-agenda-show-tags t
]
19647 ["Set Tags current line" org-agenda-set-tags
(not (org-region-active-p))]
19648 ["Change tag in region" org-agenda-set-tags
(org-region-active-p)]
19650 ["Column View" org-columns t
])
19652 ["Schedule" org-agenda-schedule t
]
19653 ["Set Deadline" org-agenda-deadline t
]
19655 ["Change Date +1 day" org-agenda-date-later
(org-agenda-check-type nil
'agenda
'timeline
)]
19656 ["Change Date -1 day" org-agenda-date-earlier
(org-agenda-check-type nil
'agenda
'timeline
)]
19657 ["Change Date to ..." org-agenda-date-prompt
(org-agenda-check-type nil
'agenda
'timeline
)])
19659 ["Clock in" org-agenda-clock-in t
]
19660 ["Clock out" org-agenda-clock-out t
]
19661 ["Clock cancel" org-agenda-clock-cancel t
]
19662 ["Goto running clock" org-clock-goto t
])
19664 ["Set Priority" org-agenda-priority t
]
19665 ["Increase Priority" org-agenda-priority-up t
]
19666 ["Decrease Priority" org-agenda-priority-down t
]
19667 ["Show Priority" org-agenda-show-priority t
])
19669 ["New Diary Entry" org-agenda-diary-entry
(org-agenda-check-type nil
'agenda
'timeline
)]
19670 ["Goto Calendar" org-agenda-goto-calendar
(org-agenda-check-type nil
'agenda
'timeline
)]
19671 ["Phases of the Moon" org-agenda-phases-of-moon
(org-agenda-check-type nil
'agenda
'timeline
)]
19672 ["Sunrise/Sunset" org-agenda-sunrise-sunset
(org-agenda-check-type nil
'agenda
'timeline
)]
19673 ["Holidays" org-agenda-holidays
(org-agenda-check-type nil
'agenda
'timeline
)]
19674 ["Convert" org-agenda-convert-date
(org-agenda-check-type nil
'agenda
'timeline
)]
19676 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t
])
19679 ["Day View" org-agenda-day-view
:active
(org-agenda-check-type nil
'agenda
)
19680 :style radio
:selected
(equal org-agenda-ndays
1)]
19681 ["Week View" org-agenda-week-view
:active
(org-agenda-check-type nil
'agenda
)
19682 :style radio
:selected
(equal org-agenda-ndays
7)]
19683 ["Month View" org-agenda-month-view
:active
(org-agenda-check-type nil
'agenda
)
19684 :style radio
:selected
(member org-agenda-ndays
'(28 29 30 31))]
19685 ["Year View" org-agenda-year-view
:active
(org-agenda-check-type nil
'agenda
)
19686 :style radio
:selected
(member org-agenda-ndays
'(365 366))]
19688 ["Show Logbook entries" org-agenda-log-mode
19689 :style toggle
:selected org-agenda-show-log
:active
(org-agenda-check-type nil
'agenda
'timeline
)]
19690 ["Include Diary" org-agenda-toggle-diary
19691 :style toggle
:selected org-agenda-include-diary
:active
(org-agenda-check-type nil
'agenda
)]
19692 ["Use Time Grid" org-agenda-toggle-time-grid
19693 :style toggle
:selected org-agenda-use-time-grid
:active
(org-agenda-check-type nil
'agenda
)])
19694 ["Write view to file" org-write-agenda t
]
19695 ["Rebuild buffer" org-agenda-redo t
]
19696 ["Save all Org-mode Buffers" org-save-all-org-buffers t
]
19698 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list
]
19700 ["Quit" org-agenda-quit t
]
19701 ["Exit and Release Buffers" org-agenda-exit t
]
19706 (defvar org-agenda-allow-remote-undo t
19707 "Non-nil means, allow remote undo from the agenda buffer.")
19708 (defvar org-agenda-undo-list nil
19709 "List of undoable operations in the agenda since last refresh.")
19710 (defvar org-agenda-undo-has-started-in nil
19711 "Buffers that have already seen `undo-start' in the current undo sequence.")
19712 (defvar org-agenda-pending-undo-list nil
19713 "In a series of undo commands, this is the list of remaning undo items.")
19715 (defmacro org-if-unprotected
(&rest body
)
19716 "Execute BODY if there is no `org-protected' text property at point."
19717 (declare (debug t
))
19718 `(unless (get-text-property (point) 'org-protected
)
19721 (defmacro org-with-remote-undo
(_buffer &rest _body
)
19722 "Execute BODY while recording undo information in two buffers."
19723 (declare (indent 1) (debug t
))
19724 `(let ((_cline (org-current-line))
19725 (_cmd this-command
)
19726 (_buf1 (current-buffer))
19728 (_undo1 buffer-undo-list
)
19729 (_undo2 (with-current-buffer ,_buffer buffer-undo-list
))
19732 (when org-agenda-allow-remote-undo
19733 (setq _c1
(org-verify-change-for-undo
19734 _undo1
(with-current-buffer _buf1 buffer-undo-list
))
19735 _c2
(org-verify-change-for-undo
19736 _undo2
(with-current-buffer _buf2 buffer-undo-list
)))
19738 ;; make sure there are undo boundaries
19739 (and _c1
(with-current-buffer _buf1
(undo-boundary)))
19740 (and _c2
(with-current-buffer _buf2
(undo-boundary)))
19741 ;; remember which buffer to undo
19742 (push (list _cmd _cline _buf1 _c1 _buf2 _c2
)
19743 org-agenda-undo-list
)))))
19745 (defun org-agenda-undo ()
19746 "Undo a remote editing step in the agenda.
19747 This undoes changes both in the agenda buffer and in the remote buffer
19748 that have been changed along."
19750 (or org-agenda-allow-remote-undo
19751 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
19752 (if (not (eq this-command last-command
))
19753 (setq org-agenda-undo-has-started-in nil
19754 org-agenda-pending-undo-list org-agenda-undo-list
))
19755 (if (not org-agenda-pending-undo-list
)
19756 (error "No further undo information"))
19757 (let* ((entry (pop org-agenda-pending-undo-list
))
19758 buf line cmd rembuf
)
19759 (setq cmd
(pop entry
) line
(pop entry
))
19760 (setq rembuf
(nth 2 entry
))
19761 (org-with-remote-undo rembuf
19762 (while (bufferp (setq buf
(pop entry
)))
19764 (with-current-buffer buf
19765 (let ((last-undo-buffer buf
)
19766 (inhibit-read-only t
))
19767 (unless (memq buf org-agenda-undo-has-started-in
)
19768 (push buf org-agenda-undo-has-started-in
)
19769 (make-local-variable 'pending-undo-list
)
19771 (while (and pending-undo-list
19772 (listp pending-undo-list
)
19773 (not (car pending-undo-list
)))
19774 (pop pending-undo-list
))
19777 (message "`%s' undone (buffer %s)" cmd
(buffer-name rembuf
))))
19779 (defun org-verify-change-for-undo (l1 l2
)
19780 "Verify that a real change occurred between the undo lists L1 and L2."
19781 (while (and l1
(listp l1
) (null (car l1
))) (pop l1
))
19782 (while (and l2
(listp l2
) (null (car l2
))) (pop l2
))
19785 ;;; Agenda dispatch
19787 (defvar org-agenda-restrict nil
)
19788 (defvar org-agenda-restrict-begin
(make-marker))
19789 (defvar org-agenda-restrict-end
(make-marker))
19790 (defvar org-agenda-last-dispatch-buffer nil
)
19791 (defvar org-agenda-overriding-restriction nil
)
19794 (defun org-agenda (arg &optional keys restriction
)
19795 "Dispatch agenda commands to collect entries to the agenda buffer.
19796 Prompts for a command to execute. Any prefix arg will be passed
19797 on to the selected command. The default selections are:
19799 a Call `org-agenda-list' to display the agenda for current day or week.
19800 t Call `org-todo-list' to display the global todo list.
19801 T Call `org-todo-list' to display the global todo list, select only
19802 entries with a specific TODO keyword (the user gets a prompt).
19803 m Call `org-tags-view' to display headlines with tags matching
19804 a condition (the user is prompted for the condition).
19805 M Like `m', but select only TODO entries, no ordinary headlines.
19806 L Create a timeline for the current buffer.
19807 e Export views to associated files.
19809 More commands can be added by configuring the variable
19810 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
19811 searches can be pre-defined in this way.
19813 If the current buffer is in Org-mode and visiting a file, you can also
19814 first press `<' once to indicate that the agenda should be temporarily
19815 \(until the next use of \\[org-agenda]) restricted to the current file.
19816 Pressing `<' twice means to restrict to the current subtree or region
19820 (let* ((prefix-descriptions nil
)
19821 (org-agenda-custom-commands-orig org-agenda-custom-commands
)
19822 (org-agenda-custom-commands
19823 ;; normalize different versions
19827 (cond ((stringp (cdr x
))
19828 (push x prefix-descriptions
)
19830 ((stringp (nth 1 x
)) x
)
19831 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
19832 (t (cons (car x
) (cons "" (cdr x
))))))
19833 org-agenda-custom-commands
)))
19834 (buf (current-buffer))
19835 (bfn (buffer-file-name (buffer-base-buffer)))
19836 entry key type match lprops ans
)
19837 ;; Turn off restriction unless there is an overriding one
19838 (unless org-agenda-overriding-restriction
19839 (put 'org-agenda-files
'org-restrict nil
)
19840 (setq org-agenda-restrict nil
)
19841 (move-marker org-agenda-restrict-begin nil
)
19842 (move-marker org-agenda-restrict-end nil
))
19843 ;; Delete old local properties
19844 (put 'org-agenda-redo-command
'org-lprops nil
)
19845 ;; Remember where this call originated
19846 (setq org-agenda-last-dispatch-buffer
(current-buffer))
19848 (setq ans
(org-agenda-get-restriction-and-command prefix-descriptions
)
19850 restriction
(cdr ans
)))
19851 ;; Estabish the restriction, if any
19852 (when (and (not org-agenda-overriding-restriction
) restriction
)
19853 (put 'org-agenda-files
'org-restrict
(list bfn
))
19855 ((eq restriction
'region
)
19856 (setq org-agenda-restrict t
)
19857 (move-marker org-agenda-restrict-begin
(region-beginning))
19858 (move-marker org-agenda-restrict-end
(region-end)))
19859 ((eq restriction
'subtree
)
19861 (setq org-agenda-restrict t
)
19862 (org-back-to-heading t
)
19863 (move-marker org-agenda-restrict-begin
(point))
19864 (move-marker org-agenda-restrict-end
19865 (progn (org-end-of-subtree t
)))))))
19867 (require 'calendar
) ; FIXME: can we avoid this for some commands?
19868 ;; For example the todo list should not need it (but does...)
19870 ((setq entry
(assoc keys org-agenda-custom-commands
))
19871 (if (or (symbolp (nth 2 entry
)) (functionp (nth 2 entry
)))
19873 (setq type
(nth 2 entry
) match
(nth 3 entry
) lprops
(nth 4 entry
))
19874 (put 'org-agenda-redo-command
'org-lprops lprops
)
19877 (org-let lprops
'(org-agenda-list current-prefix-arg
)))
19878 ((eq type
'alltodo
)
19879 (org-let lprops
'(org-todo-list current-prefix-arg
)))
19881 (org-let lprops
'(org-search-view current-prefix-arg match
)))
19883 (org-let lprops
'(org-agenda-list-stuck-projects
19884 current-prefix-arg
)))
19886 (org-let lprops
'(org-tags-view current-prefix-arg match
)))
19887 ((eq type
'tags-todo
)
19888 (org-let lprops
'(org-tags-view '(4) match
)))
19890 (org-let lprops
'(org-todo-list match
)))
19891 ((eq type
'tags-tree
)
19892 (org-check-for-org-mode)
19893 (org-let lprops
'(org-tags-sparse-tree current-prefix-arg match
)))
19894 ((eq type
'todo-tree
)
19895 (org-check-for-org-mode)
19897 '(org-occur (concat "^" outline-regexp
"[ \t]*"
19898 (regexp-quote match
) "\\>"))))
19899 ((eq type
'occur-tree
)
19900 (org-check-for-org-mode)
19901 (org-let lprops
'(org-occur match
)))
19903 (org-let lprops
'(funcall type match
)))
19905 (org-let lprops
'(funcall type match
)))
19906 (t (error "Invalid custom agenda command type %s" type
))))
19907 (org-run-agenda-series (nth 1 entry
) (cddr entry
))))
19909 (setq org-agenda-custom-commands org-agenda-custom-commands-orig
)
19910 (customize-variable 'org-agenda-custom-commands
))
19911 ((equal keys
"a") (call-interactively 'org-agenda-list
))
19912 ((equal keys
"s") (call-interactively 'org-search-view
))
19913 ((equal keys
"t") (call-interactively 'org-todo-list
))
19914 ((equal keys
"T") (org-call-with-arg 'org-todo-list
(or arg
'(4))))
19915 ((equal keys
"m") (call-interactively 'org-tags-view
))
19916 ((equal keys
"M") (org-call-with-arg 'org-tags-view
(or arg
'(4))))
19917 ((equal keys
"e") (call-interactively 'org-store-agenda-views
))
19919 (unless (org-mode-p)
19920 (error "This is not an Org-mode file"))
19921 (unless restriction
19922 (put 'org-agenda-files
'org-restrict
(list bfn
))
19923 (org-call-with-arg 'org-timeline arg
)))
19924 ((equal keys
"#") (call-interactively 'org-agenda-list-stuck-projects
))
19925 ((equal keys
"/") (call-interactively 'org-occur-in-agenda-files
))
19926 ((equal keys
"!") (customize-variable 'org-stuck-projects
))
19927 (t (error "Invalid agenda key"))))))
19929 (defun org-agenda-normalize-custom-commands (cmds)
19933 (cond ((stringp (cdr x
)) nil
)
19934 ((stringp (nth 1 x
)) x
)
19935 ((not (nth 1 x
)) (cons (car x
) (cons "" (cddr x
))))
19936 (t (cons (car x
) (cons "" (cdr x
))))))
19939 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
19940 "The user interface for selecting an agenda command."
19942 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
19943 (restrict-ok (and bfn
(org-mode-p)))
19944 (region-p (org-region-active-p))
19945 (custom org-agenda-custom-commands
)
19947 restriction second-time
19948 c entry key type match prefixes rmheader header-end custom1 desc
)
19949 (save-window-excursion
19950 (delete-other-windows)
19951 (org-switch-to-buffer-other-window " *Agenda Commands*")
19953 (insert (eval-when-compile
19956 Press key for an agenda command: < Buffer,subtree/region restriction
19957 -------------------------------- > Remove restriction
19958 a Agenda for current week or day e Export agenda views
19959 t List of all TODO entries T Entries with special TODO kwd
19960 m Match a TAGS query M Like m, but only TODO entries
19961 L Timeline for current buffer # List stuck projects (!=configure)
19962 s Search for keywords C Configure custom agenda commands
19966 (while (string-match
19967 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
19969 (setq start
(match-end 0))
19970 (add-text-properties (match-beginning 2) (match-end 2)
19971 '(face bold
) header
))
19973 (setq header-end
(move-marker (make-marker) (point)))
19975 (setq custom1 custom
)
19976 (when (eq rmheader t
)
19978 (re-search-forward ":" nil t
)
19979 (delete-region (match-end 0) (point-at-eol))
19982 (delete-region (match-end 0) (point-at-eol))
19983 (move-marker header-end
(match-end 0)))
19984 (goto-char header-end
)
19985 (delete-region (point) (point-max))
19986 (while (setq entry
(pop custom1
))
19987 (setq key
(car entry
) desc
(nth 1 entry
)
19988 type
(nth 2 entry
) match
(nth 3 entry
))
19989 (if (> (length key
) 1)
19990 (add-to-list 'prefixes
(string-to-char key
))
19994 (org-add-props (copy-sequence key
)
19997 ((string-match "\\S-" desc
) desc
)
19998 ((eq type
'agenda
) "Agenda for current week or day")
19999 ((eq type
'alltodo
) "List of all TODO entries")
20000 ((eq type
'search
) "Word search")
20001 ((eq type
'stuck
) "List of stuck projects")
20002 ((eq type
'todo
) "TODO keyword")
20003 ((eq type
'tags
) "Tags query")
20004 ((eq type
'tags-todo
) "Tags (TODO)")
20005 ((eq type
'tags-tree
) "Tags tree")
20006 ((eq type
'todo-tree
) "TODO kwd tree")
20007 ((eq type
'occur-tree
) "Occur tree")
20008 ((functionp type
) (if (symbolp type
)
20010 "Lambda expression"))
20014 (org-add-props match nil
'face
'org-warning
))
20016 (format "set of %d commands" (length match
)))
20022 (org-add-props (char-to-string x
)
20024 (or (cdr (assoc (concat selstring
(char-to-string x
))
20025 prefix-descriptions
))
20028 (goto-char (point-min))
20029 (when (fboundp 'fit-window-to-buffer
)
20031 (if (not (pos-visible-in-window-p (point-max)))
20032 (fit-window-to-buffer))
20033 (setq second-time t
)
20034 (fit-window-to-buffer)))
20035 (message "Press key for agenda command%s:"
20036 (if (or restrict-ok org-agenda-overriding-restriction
)
20037 (if org-agenda-overriding-restriction
20038 " (restriction lock active)"
20040 (format " (restricted to %s)" restriction
)
20041 " (unrestricted)"))
20043 (setq c
(read-char-exclusive))
20046 ((assoc (char-to-string c
) custom
)
20047 (setq selstring
(concat selstring
(char-to-string c
)))
20048 (throw 'exit
(cons selstring restriction
)))
20050 (setq selstring
(concat selstring
(char-to-string c
))
20052 rmheader
(or rmheader t
)
20053 custom
(delq nil
(mapcar
20055 (if (or (= (length (car x
)) 1)
20056 (/= (string-to-char (car x
)) c
))
20058 (cons (substring (car x
) 1) (cdr x
))))
20060 ((and (not restrict-ok
) (memq c
'(?
1 ?
0 ?
<)))
20061 (message "Restriction is only possible in Org-mode buffers")
20062 (ding) (sit-for 1))
20064 (org-agenda-remove-restriction-lock 'noupdate
)
20065 (setq restriction
'buffer
))
20067 (org-agenda-remove-restriction-lock 'noupdate
)
20068 (setq restriction
(if region-p
'region
'subtree
)))
20070 (org-agenda-remove-restriction-lock 'noupdate
)
20073 ((eq restriction
'buffer
)
20074 (if region-p
'region
'subtree
))
20075 ((memq restriction
'(subtree region
))
20079 (org-agenda-remove-restriction-lock 'noupdate
)
20080 (setq restriction nil
))
20081 ((and (equal selstring
"") (memq c
'(?s ?a ?t ?m ?L ?C ?e ?T ?M ?
# ?
! ?
/)))
20082 (throw 'exit
(cons (setq selstring
(char-to-string c
)) restriction
)))
20083 ((and (> (length selstring
) 0) (eq c ?\d
))
20085 (org-agenda-get-restriction-and-command prefix-descriptions
))
20087 ((equal c ?q
) (error "Abort"))
20088 (t (error "Invalid key %c" c
))))))))
20090 (defun org-run-agenda-series (name series
)
20091 (org-prepare-agenda name
)
20092 (let* ((org-agenda-multi t
)
20093 (redo (list 'org-run-agenda-series name
(list 'quote series
)))
20094 (cmds (car series
))
20095 (gprops (nth 1 series
))
20096 match
;; The byte compiler incorrectly complains about this. Keep it!
20098 (while (setq cmd
(pop cmds
))
20099 (setq type
(car cmd
) match
(nth 1 cmd
) lprops
(nth 2 cmd
))
20102 (org-let2 gprops lprops
20103 '(call-interactively 'org-agenda-list
)))
20104 ((eq type
'alltodo
)
20105 (org-let2 gprops lprops
20106 '(call-interactively 'org-todo-list
)))
20108 (org-let2 gprops lprops
20109 '(org-search-view current-prefix-arg match
)))
20111 (org-let2 gprops lprops
20112 '(call-interactively 'org-agenda-list-stuck-projects
)))
20114 (org-let2 gprops lprops
20115 '(org-tags-view current-prefix-arg match
)))
20116 ((eq type
'tags-todo
)
20117 (org-let2 gprops lprops
20118 '(org-tags-view '(4) match
)))
20120 (org-let2 gprops lprops
20121 '(org-todo-list match
)))
20123 (org-let2 gprops lprops
20124 '(funcall type match
)))
20125 (t (error "Invalid type in command series"))))
20127 (setq org-agenda-redo-command redo
)
20128 (goto-char (point-min)))
20129 (org-finalize-agenda))
20132 (defmacro org-batch-agenda
(cmd-key &rest parameters
)
20133 "Run an agenda command in batch mode and send the result to STDOUT.
20134 If CMD-KEY is a string of length 1, it is used as a key in
20135 `org-agenda-custom-commands' and triggers this command. If it is a
20136 longer string it is used as a tags/todo match string.
20137 Paramters are alternating variable names and values that will be bound
20138 before running the agenda command."
20141 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
20142 (if (> (length cmd-key
) 2)
20143 (eval (list 'let
(nreverse pars
)
20144 (list 'org-tags-view nil cmd-key
)))
20145 (eval (list 'let
(nreverse pars
) (list 'org-agenda nil cmd-key
))))
20146 (set-buffer org-agenda-buffer-name
)
20147 (princ (org-encode-for-stdout (buffer-string)))))
20149 (defun org-encode-for-stdout (string)
20150 (if (fboundp 'encode-coding-string
)
20151 (encode-coding-string string buffer-file-coding-system
)
20154 (defvar org-agenda-info nil
)
20157 (defmacro org-batch-agenda-csv
(cmd-key &rest parameters
)
20158 "Run an agenda command in batch mode and send the result to STDOUT.
20159 If CMD-KEY is a string of length 1, it is used as a key in
20160 `org-agenda-custom-commands' and triggers this command. If it is a
20161 longer string it is used as a tags/todo match string.
20162 Paramters are alternating variable names and values that will be bound
20163 before running the agenda command.
20165 The output gives a line for each selected agenda item. Each
20166 item is a list of comma-separated values, like this:
20168 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
20170 category The category of the item
20171 head The headline, without TODO kwd, TAGS and PRIORITY
20172 type The type of the agenda entry, can be
20173 todo selected in TODO match
20174 tagsmatch selected in tags match
20175 diary imported from diary
20176 deadline a deadline on given date
20177 scheduled scheduled on given date
20178 timestamp entry has timestamp on given date
20179 closed entry was closed on given date
20180 upcoming-deadline warning about deadline
20181 past-scheduled forwarded scheduled item
20182 block entry has date block including g. date
20183 todo The todo keyword, if any
20184 tags All tags including inherited ones, separated by colons
20185 date The relevant date, like 2007-2-14
20186 time The time, like 15:00-16:50
20187 extra Sting with extra planning info
20188 priority-l The priority letter if any was given
20189 priority-n The computed numerical priority
20190 agenda-day The day in the agenda where this is listed"
20194 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
20195 (push (list 'org-agenda-remove-tags t
) pars
)
20196 (if (> (length cmd-key
) 2)
20197 (eval (list 'let
(nreverse pars
)
20198 (list 'org-tags-view nil cmd-key
)))
20199 (eval (list 'let
(nreverse pars
) (list 'org-agenda nil cmd-key
))))
20200 (set-buffer org-agenda-buffer-name
)
20201 (let* ((lines (org-split-string (buffer-string) "\n"))
20203 (while (setq line
(pop lines
))
20205 (if (not (get-text-property 0 'org-category line
)) (throw 'next nil
))
20206 (setq org-agenda-info
20207 (org-fix-agenda-info (text-properties-at 0 line
)))
20209 (org-encode-for-stdout
20210 (mapconcat 'org-agenda-export-csv-mapper
20211 '(org-category txt type todo tags date time-of-day extra
20212 priority-letter priority agenda-day
)
20216 (defun org-fix-agenda-info (props)
20217 "Make sure all properties on an agenda item have a canonical form,
20218 so the export commands can easily use it."
20220 (when (setq tmp
(plist-get props
'tags
))
20221 (setq props
(plist-put props
'tags
(mapconcat 'identity tmp
":"))))
20222 (when (setq tmp
(plist-get props
'date
))
20223 (if (integerp tmp
) (setq tmp
(calendar-gregorian-from-absolute tmp
)))
20224 (let ((calendar-date-display-form '(year "-" month
"-" day
)))
20225 '((format "%4d, %9s %2s, %4s" dayname monthname day year
))
20227 (setq tmp
(calendar-date-string tmp
)))
20228 (setq props
(plist-put props
'date tmp
)))
20229 (when (setq tmp
(plist-get props
'day
))
20230 (if (integerp tmp
) (setq tmp
(calendar-gregorian-from-absolute tmp
)))
20231 (let ((calendar-date-display-form '(year "-" month
"-" day
)))
20232 (setq tmp
(calendar-date-string tmp
)))
20233 (setq props
(plist-put props
'day tmp
))
20234 (setq props
(plist-put props
'agenda-day tmp
)))
20235 (when (setq tmp
(plist-get props
'txt
))
20236 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp
)
20237 (plist-put props
'priority-letter
(match-string 1 tmp
))
20238 (setq tmp
(replace-match "" t t tmp
)))
20239 (when (and (setq re
(plist-get props
'org-todo-regexp
))
20240 (setq re
(concat "\\`\\.*" re
" ?"))
20241 (string-match re tmp
))
20242 (plist-put props
'todo
(match-string 1 tmp
))
20243 (setq tmp
(replace-match "" t t tmp
)))
20244 (plist-put props
'txt tmp
)))
20247 (defun org-agenda-export-csv-mapper (prop)
20248 (let ((res (plist-get org-agenda-info prop
)))
20252 ((stringp res
) res
)
20253 (t (prin1-to-string res
))))
20254 (while (string-match "," res
)
20255 (setq res
(replace-match ";" t t res
)))
20260 (defun org-store-agenda-views (&rest parameters
)
20262 (eval (list 'org-batch-store-agenda-views
)))
20264 ;; FIXME, why is this a macro?????
20266 (defmacro org-batch-store-agenda-views
(&rest parameters
)
20267 "Run all custom agenda commands that have a file argument."
20268 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands
))
20269 (pop-up-frames nil
)
20270 (dir default-directory
)
20271 pars cmd thiscmdkey files opts
)
20273 (push (list (pop parameters
) (if parameters
(pop parameters
))) pars
))
20274 (setq pars
(reverse pars
))
20275 (save-window-excursion
20277 (setq cmd
(pop cmds
)
20278 thiscmdkey
(car cmd
)
20281 (if (stringp files
) (setq files
(list files
)))
20283 (eval (list 'let
(append org-agenda-exporter-settings opts pars
)
20284 (list 'org-agenda nil thiscmdkey
)))
20285 (set-buffer org-agenda-buffer-name
)
20287 (eval (list 'let
(append org-agenda-exporter-settings opts pars
)
20288 (list 'org-write-agenda
20289 (expand-file-name (pop files
) dir
) t
))))
20290 (and (get-buffer org-agenda-buffer-name
)
20291 (kill-buffer org-agenda-buffer-name
)))))))
20293 (defun org-write-agenda (file &optional nosettings
)
20294 "Write the current buffer (an agenda view) as a file.
20295 Depending on the extension of the file name, plain text (.txt),
20296 HTML (.html or .htm) or Postscript (.ps) is produced.
20297 If NOSETTINGS is given, do not scope the settings of
20298 `org-agenda-exporter-settings' into the export commands. This is used when
20299 the settings have already been scoped and we do not wish to overrule other,
20300 higher priority settings."
20301 (interactive "FWrite agenda to file: ")
20302 (if (not (file-writable-p file
))
20303 (error "Cannot write agenda to file %s" file
))
20305 ((string-match "\\.html?\\'" file
) (require 'htmlize
))
20306 ((string-match "\\.ps\\'" file
) (require 'ps-print
)))
20307 (org-let (if nosettings nil org-agenda-exporter-settings
)
20309 (save-window-excursion
20311 ((string-match "\\.html?\\'" file
)
20312 (set-buffer (htmlize-buffer (current-buffer)))
20314 (when (and org-agenda-export-html-style
20315 (string-match "<style>" org-agenda-export-html-style
))
20316 ;; replace <style> section with org-agenda-export-html-style
20317 (goto-char (point-min))
20318 (kill-region (- (search-forward "<style") 6)
20319 (search-forward "</style>"))
20320 (insert org-agenda-export-html-style
))
20322 (kill-buffer (current-buffer))
20323 (message "HTML written to %s" file
))
20324 ((string-match "\\.ps\\'" file
)
20325 (ps-print-buffer-with-faces file
)
20326 (message "Postscript written to %s" file
))
20328 (let ((bs (buffer-string)))
20332 (kill-buffer (current-buffer))
20333 (message "Plain text written to %s" file
))))))
20334 (set-buffer org-agenda-buffer-name
)))
20336 (defmacro org-no-read-only
(&rest body
)
20337 "Inhibit read-only for BODY."
20338 `(let ((inhibit-read-only t
)) ,@body
))
20340 (defun org-check-for-org-mode ()
20341 "Make sure current buffer is in org-mode. Error if not."
20343 (error "Cannot execute org-mode agenda command on buffer in %s."
20346 (defun org-fit-agenda-window ()
20347 "Fit the window to the buffer size."
20348 (and (memq org-agenda-window-setup
'(reorganize-frame))
20349 (fboundp 'fit-window-to-buffer
)
20350 (fit-window-to-buffer
20352 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions
)))
20353 (floor (* (frame-height) (car org-agenda-window-frame-fractions
))))))
20355 ;;; Agenda file list
20357 (defun org-agenda-files (&optional unrestricted
)
20358 "Get the list of agenda files.
20359 Optional UNRESTRICTED means return the full list even if a restriction
20360 is currently in place."
20363 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
20364 ((stringp org-agenda-files
) (org-read-agenda-file-list))
20365 ((listp org-agenda-files
) org-agenda-files
)
20366 (t (error "Invalid value of `org-agenda-files'")))))
20367 (setq files
(apply 'append
20368 (mapcar (lambda (f)
20369 (if (file-directory-p f
)
20370 (directory-files f t
20371 org-agenda-file-regexp
)
20374 (if org-agenda-skip-unavailable-files
20378 (and (file-readable-p file
) file
)))
20380 files
))) ; `org-check-agenda-file' will remove them from the list
20382 (defun org-edit-agenda-file-list ()
20383 "Edit the list of agenda files.
20384 Depending on setup, this either uses customize to edit the variable
20385 `org-agenda-files', or it visits the file that is holding the list. In the
20386 latter case, the buffer is set up in a way that saving it automatically kills
20387 the buffer and restores the previous window configuration."
20389 (if (stringp org-agenda-files
)
20390 (let ((cw (current-window-configuration)))
20391 (find-file org-agenda-files
)
20392 (org-set-local 'org-window-configuration cw
)
20393 (org-add-hook 'after-save-hook
20395 (set-window-configuration
20396 (prog1 org-window-configuration
20397 (kill-buffer (current-buffer))))
20398 (org-install-agenda-files-menu)
20399 (message "New agenda file list installed"))
20401 (message "%s" (substitute-command-keys
20402 "Edit list and finish with \\[save-buffer]")))
20403 (customize-variable 'org-agenda-files
)))
20405 (defun org-store-new-agenda-file-list (list)
20406 "Set new value for the agenda file list and save it correcly."
20407 (if (stringp org-agenda-files
)
20408 (let ((f org-agenda-files
) b
)
20409 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
20411 (insert (mapconcat 'identity list
"\n") "\n")))
20412 (let ((org-mode-hook nil
) (default-major-mode 'fundamental-mode
))
20413 (setq org-agenda-files list
)
20414 (customize-save-variable 'org-agenda-files org-agenda-files
))))
20416 (defun org-read-agenda-file-list ()
20417 "Read the list of agenda files from a file."
20418 (when (stringp org-agenda-files
)
20420 (insert-file-contents org-agenda-files
)
20421 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
20425 (defun org-cycle-agenda-files ()
20426 "Cycle through the files in `org-agenda-files'.
20427 If the current buffer visits an agenda file, find the next one in the list.
20428 If the current buffer does not, find the first agenda file."
20430 (let* ((fs (org-agenda-files t
))
20431 (files (append fs
(list (car fs
))))
20432 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
20434 (unless files
(error "No agenda files"))
20436 (while (setq file
(pop files
))
20437 (if (equal (file-truename file
) tcf
)
20439 (find-file (car files
))
20441 (find-file (car fs
)))
20442 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
20444 (defun org-agenda-file-to-front (&optional to-end
)
20445 "Move/add the current file to the top of the agenda file list.
20446 If the file is not present in the list, it is added to the front. If it is
20447 present, it is moved there. With optional argument TO-END, add/move to the
20450 (let ((org-agenda-skip-unavailable-files nil
)
20451 (file-alist (mapcar (lambda (x)
20452 (cons (file-truename x
) x
))
20453 (org-agenda-files t
)))
20454 (ctf (file-truename buffer-file-name
))
20456 (setq x
(assoc ctf file-alist
) had x
)
20458 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
20460 (setq file-alist
(append (delq x file-alist
) (list x
)))
20461 (setq file-alist
(cons x
(delq x file-alist
))))
20462 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
20463 (org-install-agenda-files-menu)
20464 (message "File %s to %s of agenda file list"
20465 (if had
"moved" "added") (if to-end
"end" "front"))))
20467 (defun org-remove-file (&optional file
)
20468 "Remove current file from the list of files in variable `org-agenda-files'.
20469 These are the files which are being checked for agenda entries.
20470 Optional argument FILE means, use this file instead of the current."
20472 (let* ((org-agenda-skip-unavailable-files nil
)
20473 (file (or file buffer-file-name
))
20474 (true-file (file-truename file
))
20475 (afile (abbreviate-file-name file
))
20476 (files (delq nil
(mapcar
20478 (if (equal true-file
20481 (org-agenda-files t
)))))
20482 (if (not (= (length files
) (length (org-agenda-files t
))))
20484 (org-store-new-agenda-file-list files
)
20485 (org-install-agenda-files-menu)
20486 (message "Removed file: %s" afile
))
20487 (message "File was not in list: %s (not removed)" afile
))))
20489 (defun org-file-menu-entry (file)
20490 (vector file
(list 'find-file file
) t
))
20492 (defun org-check-agenda-file (file)
20493 "Make sure FILE exists. If not, ask user what to do."
20494 (when (not (file-exists-p file
))
20495 (message "non-existent file %s. [R]emove from list or [A]bort?"
20496 (abbreviate-file-name file
))
20497 (let ((r (downcase (read-char-exclusive))))
20500 (org-remove-file file
)
20501 (throw 'nextfile t
))
20502 (t (error "Abort"))))))
20504 ;;; Agenda prepare and finalize
20506 (defvar org-agenda-multi nil
) ; dynammically scoped
20507 (defvar org-agenda-buffer-name
"*Org Agenda*")
20508 (defvar org-pre-agenda-window-conf nil
)
20509 (defvar org-agenda-name nil
)
20510 (defun org-prepare-agenda (&optional name
)
20511 (setq org-todo-keywords-for-agenda nil
)
20512 (setq org-done-keywords-for-agenda nil
)
20513 (if org-agenda-multi
20515 (setq buffer-read-only nil
)
20516 (goto-char (point-max))
20517 (unless (or (bobp) org-agenda-compact-blocks
)
20518 (insert "\n" (make-string (window-width) ?
=) "\n"))
20519 (narrow-to-region (point) (point-max)))
20520 (org-agenda-reset-markers)
20521 (org-prepare-agenda-buffers (org-agenda-files))
20522 (setq org-todo-keywords-for-agenda
20523 (org-uniquify org-todo-keywords-for-agenda
))
20524 (setq org-done-keywords-for-agenda
20525 (org-uniquify org-done-keywords-for-agenda
))
20526 (let* ((abuf (get-buffer-create org-agenda-buffer-name
))
20527 (awin (get-buffer-window abuf
)))
20529 ((equal (current-buffer) abuf
) nil
)
20530 (awin (select-window awin
))
20531 ((not (setq org-pre-agenda-window-conf
(current-window-configuration))))
20532 ((equal org-agenda-window-setup
'current-window
)
20533 (switch-to-buffer abuf
))
20534 ((equal org-agenda-window-setup
'other-window
)
20535 (org-switch-to-buffer-other-window abuf
))
20536 ((equal org-agenda-window-setup
'other-frame
)
20537 (switch-to-buffer-other-frame abuf
))
20538 ((equal org-agenda-window-setup
'reorganize-frame
)
20539 (delete-other-windows)
20540 (org-switch-to-buffer-other-window abuf
))))
20541 (setq buffer-read-only nil
)
20544 (and name
(not org-agenda-name
)
20545 (org-set-local 'org-agenda-name name
)))
20546 (setq buffer-read-only nil
))
20548 (defun org-finalize-agenda ()
20549 "Finishing touch for the agenda buffer, called just before displaying it."
20550 (unless org-agenda-multi
20552 (let ((inhibit-read-only t
))
20553 (goto-char (point-min))
20554 (while (org-activate-bracket-links (point-max))
20555 (add-text-properties (match-beginning 0) (match-end 0)
20557 (org-agenda-align-tags)
20558 (unless org-agenda-with-colors
20559 (remove-text-properties (point-min) (point-max) '(face nil
))))
20560 (if (and (boundp 'org-overriding-columns-format
)
20561 org-overriding-columns-format
)
20562 (org-set-local 'org-overriding-columns-format
20563 org-overriding-columns-format
))
20564 (if (and (boundp 'org-agenda-view-columns-initially
)
20565 org-agenda-view-columns-initially
)
20566 (org-agenda-columns))
20567 (when org-agenda-fontify-priorities
20568 (org-fontify-priorities))
20569 (run-hooks 'org-finalize-agenda-hook
)
20570 (setq org-agenda-type
(get-text-property (point) 'org-agenda-type
))
20573 (defun org-fontify-priorities ()
20574 "Make highest priority lines bold, and lowest italic."
20576 (mapc (lambda (o) (if (eq (org-overlay-get o
'org-type
) 'org-priority
)
20577 (org-delete-overlay o
)))
20578 (org-overlays-in (point-min) (point-max)))
20580 (let ((inhibit-read-only t
)
20582 (goto-char (point-min))
20583 (while (re-search-forward "\\[#\\(.\\)\\]" nil t
)
20584 (setq h
(or (get-char-property (point) 'org-highest-priority
)
20585 org-highest-priority
)
20586 l
(or (get-char-property (point) 'org-lowest-priority
)
20587 org-lowest-priority
)
20588 p
(string-to-char (match-string 1))
20589 b
(match-beginning 0) e
(point-at-eol)
20590 ov
(org-make-overlay b e
))
20593 (cond ((listp org-agenda-fontify-priorities
)
20594 (cdr (assoc p org-agenda-fontify-priorities
)))
20595 ((equal p l
) 'italic
)
20596 ((equal p h
) 'bold
)))
20597 (org-overlay-put ov
'org-type
'org-priority
)))))
20599 (defun org-prepare-agenda-buffers (files)
20600 "Create buffers for all agenda files, protect archived trees and comments."
20602 (let ((pa '(:org-archived t
))
20603 (pc '(:org-comment t
))
20604 (pall '(:org-archived t
:org-comment t
))
20605 (inhibit-read-only t
)
20606 (rea (concat ":" org-archive-tag
":"))
20610 (while (setq file
(pop files
))
20613 (org-check-agenda-file file
)
20614 (set-buffer (org-get-agenda-file-buffer file
)))
20616 (setq bmp
(buffer-modified-p))
20617 (org-refresh-category-properties)
20618 (setq org-todo-keywords-for-agenda
20619 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
20620 (setq org-done-keywords-for-agenda
20621 (append org-done-keywords-for-agenda org-done-keywords
))
20623 (remove-text-properties (point-min) (point-max) pall
)
20624 (when org-agenda-skip-archived-trees
20625 (goto-char (point-min))
20626 (while (re-search-forward rea nil t
)
20627 (if (org-on-heading-p t
)
20628 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
20629 (goto-char (point-min))
20630 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
20631 (while (re-search-forward re nil t
)
20632 (add-text-properties
20633 (match-beginning 0) (org-end-of-subtree t
) pc
)))
20634 (set-buffer-modified-p bmp
))))))
20636 (defvar org-agenda-skip-function nil
20637 "Function to be called at each match during agenda construction.
20638 If this function returns nil, the current match should not be skipped.
20639 Otherwise, the function must return a position from where the search
20640 should be continued.
20641 This may also be a Lisp form, it will be evaluated.
20642 Never set this variable using `setq' or so, because then it will apply
20643 to all future agenda commands. Instead, bind it with `let' to scope
20644 it dynamically into the agenda-constructing command. A good way to set
20645 it is through options in org-agenda-custom-commands.")
20647 (defun org-agenda-skip ()
20648 "Throw to `:skip' in places that should be skipped.
20649 Also moves point to the end of the skipped region, so that search can
20650 continue from there."
20651 (let ((p (point-at-bol)) to fp
)
20652 (and org-agenda-skip-archived-trees
20653 (get-text-property p
:org-archived
)
20654 (org-end-of-subtree t
)
20656 (and (get-text-property p
:org-comment
)
20657 (org-end-of-subtree t
)
20659 (if (equal (char-after p
) ?
#) (throw :skip t
))
20660 (when (and (or (setq fp
(functionp org-agenda-skip-function
))
20661 (consp org-agenda-skip-function
))
20662 (setq to
(save-excursion
20665 (funcall org-agenda-skip-function
)
20666 (eval org-agenda-skip-function
))))))
20670 (defvar org-agenda-markers nil
20671 "List of all currently active markers created by `org-agenda'.")
20672 (defvar org-agenda-last-marker-time
(time-to-seconds (current-time))
20673 "Creation time of the last agenda marker.")
20675 (defun org-agenda-new-marker (&optional pos
)
20676 "Return a new agenda marker.
20677 Org-mode keeps a list of these markers and resets them when they are
20679 (let ((m (copy-marker (or pos
(point)))))
20680 (setq org-agenda-last-marker-time
(time-to-seconds (current-time)))
20681 (push m org-agenda-markers
)
20684 (defun org-agenda-reset-markers ()
20685 "Reset markers created by `org-agenda'."
20686 (while org-agenda-markers
20687 (move-marker (pop org-agenda-markers
) nil
)))
20689 (defun org-get-agenda-file-buffer (file)
20690 "Get a buffer visiting FILE. If the buffer needs to be created, add
20691 it to the list of buffers which might be released later."
20692 (let ((buf (org-find-base-buffer-visiting file
)))
20694 buf
; just return it
20695 ;; Make a new buffer and remember it
20696 (setq buf
(find-file-noselect file
))
20697 (if buf
(push buf org-agenda-new-buffers
))
20700 (defun org-release-buffers (blist)
20701 "Release all buffers in list, asking the user for confirmation when needed.
20702 When a buffer is unmodified, it is just killed. When modified, it is saved
20703 \(if the user agrees) and then killed."
20705 (while (setq buf
(pop blist
))
20706 (setq file
(buffer-file-name buf
))
20707 (when (and (buffer-modified-p buf
)
20709 (y-or-n-p (format "Save file %s? " file
)))
20710 (with-current-buffer buf
(save-buffer)))
20711 (kill-buffer buf
))))
20713 (defun org-get-category (&optional pos
)
20714 "Get the category applying to position POS."
20715 (get-text-property (or pos
(point)) 'org-category
))
20717 ;;; Agenda timeline
20719 (defvar org-agenda-only-exact-dates nil
) ; dynamically scoped
20721 (defun org-timeline (&optional include-all
)
20722 "Show a time-sorted view of the entries in the current org file.
20723 Only entries with a time stamp of today or later will be listed. With
20724 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
20725 under the current date.
20726 If the buffer contains an active region, only check the region for
20729 (require 'calendar
)
20730 (org-compile-prefix-format 'timeline
)
20731 (org-set-sorting-strategy 'timeline
)
20733 (dotodo include-all
)
20734 (doclosed org-agenda-show-log
)
20735 (entry buffer-file-name
)
20736 (date (calendar-current-date))
20737 (beg (if (org-region-active-p) (region-beginning) (point-min)))
20738 (end (if (org-region-active-p) (region-end) (point-max)))
20739 (day-numbers (org-get-all-dates beg end
'no-ranges
20740 t doclosed
; always include today
20741 org-timeline-show-empty-dates
))
20742 (org-deadline-warning-days 0)
20743 (org-agenda-only-exact-dates t
)
20744 (today (time-to-days (current-time)))
20748 (setq org-agenda-redo-command
20750 (list 'org-switch-to-buffer-other-window
(current-buffer))
20751 (list 'org-timeline
(list 'quote include-all
))))
20753 ;; Remove past dates from the list of dates.
20754 (setq day-numbers
(delq nil
(mapcar (lambda(x)
20755 (if (>= x today
) x nil
))
20757 (org-prepare-agenda (concat "Timeline "
20758 (file-name-nondirectory buffer-file-name
)))
20759 (if doclosed
(push :closed args
))
20760 (push :timestamp args
)
20761 (push :deadline args
)
20762 (push :scheduled args
)
20764 (if dotodo
(push :todo args
))
20765 (while (setq d
(pop day-numbers
))
20766 (if (and (listp d
) (eq (car d
) :omitted
))
20769 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d
)))
20770 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
))
20771 (if (listp d
) (setq d
(car d
) emptyp t
) (setq emptyp nil
))
20772 (if (and (>= d today
)
20777 (insert (make-string 79 ?-
) "\n")))
20778 (setq date
(calendar-gregorian-from-absolute d
))
20780 (setq rtn
(and (not emptyp
)
20781 (apply 'org-agenda-get-day-entries entry
20783 (if (or rtn
(equal d today
) org-timeline-show-empty-dates
)
20786 (if (stringp org-agenda-format-date
)
20787 (format-time-string org-agenda-format-date
20788 (org-time-from-absolute date
))
20789 (funcall org-agenda-format-date date
))
20791 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
)
20792 (put-text-property s
(1- (point)) 'org-date-line t
)
20793 (if (equal d today
)
20794 (put-text-property s
(1- (point)) 'org-today t
))
20795 (and rtn
(insert (org-finalize-agenda-entries rtn
) "\n"))
20796 (put-text-property s
(1- (point)) 'day d
)))))
20797 (goto-char (point-min))
20798 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t
)
20800 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline
))
20801 (org-finalize-agenda)
20802 (setq buffer-read-only t
)))
20804 (defun org-get-all-dates (beg end
&optional no-ranges force-today inactive empty pre-re
)
20805 "Return a list of all relevant day numbers from BEG to END buffer positions.
20806 If NO-RANGES is non-nil, include only the start and end dates of a range,
20807 not every single day in the range. If FORCE-TODAY is non-nil, make
20808 sure that TODAY is included in the list. If INACTIVE is non-nil, also
20809 inactive time stamps (those in square brackets) are included.
20810 When EMPTY is non-nil, also include days without any entries."
20812 (if pre-re pre-re
"")
20813 (if inactive org-ts-regexp-both org-ts-regexp
)))
20814 dates dates1 date day day1 day2 ts1 ts2
)
20816 (setq dates
(list (time-to-days (current-time)))))
20819 (while (re-search-forward re end t
)
20820 (setq day
(time-to-days (org-time-string-to-time
20821 (substring (match-string 1) 0 10))))
20822 (or (memq day dates
) (push day dates
)))
20825 (while (re-search-forward org-tr-regexp end t
)
20826 (setq ts1
(substring (match-string 1) 0 10)
20827 ts2
(substring (match-string 2) 0 10)
20828 day1
(time-to-days (org-time-string-to-time ts1
))
20829 day2
(time-to-days (org-time-string-to-time ts2
)))
20830 (while (< (setq day1
(1+ day1
)) day2
)
20831 (or (memq day1 dates
) (push day1 dates
)))))
20832 (setq dates
(sort dates
'<))
20834 (while (setq day
(pop dates
))
20835 (setq day2
(car dates
))
20837 (when (and day2 empty
)
20838 (if (or (eq empty t
)
20839 (and (numberp empty
) (<= (- day2 day
) empty
)))
20840 (while (< (setq day
(1+ day
)) day2
)
20841 (push (list day
) dates1
))
20842 (push (cons :omitted
(- day2 day
)) dates1
))))
20843 (setq dates
(nreverse dates1
)))
20846 ;;; Agenda Daily/Weekly
20848 (defvar org-agenda-overriding-arguments nil
) ; dynamically scoped parameter
20849 (defvar org-agenda-start-day nil
) ; dynamically scoped parameter
20850 (defvar org-agenda-last-arguments nil
20851 "The arguments of the previous call to org-agenda")
20852 (defvar org-starting-day nil
) ; local variable in the agenda buffer
20853 (defvar org-agenda-span nil
) ; local variable in the agenda buffer
20854 (defvar org-include-all-loc nil
) ; local variable
20855 (defvar org-agenda-remove-date nil
) ; dynamically scoped
20858 (defun org-agenda-list (&optional include-all start-day ndays
)
20859 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
20860 The view will be for the current day or week, but from the overview buffer
20861 you will be able to go to other days/weeks.
20863 With one \\[universal-argument] prefix argument INCLUDE-ALL,
20864 all unfinished TODO items will also be shown, before the agenda.
20865 This feature is considered obsolete, please use the TODO list or a block
20868 With a numeric prefix argument in an interactive call, the agenda will
20869 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
20870 the number of days. NDAYS defaults to `org-agenda-ndays'.
20872 START-DAY defaults to TODAY, or to the most recent match for the weekday
20873 given in `org-agenda-start-on-weekday'."
20875 (if (and (integerp include-all
) (> include-all
0))
20876 (setq ndays include-all include-all nil
))
20877 (setq ndays
(or ndays org-agenda-ndays
)
20878 start-day
(or start-day org-agenda-start-day
))
20879 (if org-agenda-overriding-arguments
20880 (setq include-all
(car org-agenda-overriding-arguments
)
20881 start-day
(nth 1 org-agenda-overriding-arguments
)
20882 ndays
(nth 2 org-agenda-overriding-arguments
)))
20883 (if (stringp start-day
)
20884 ;; Convert to an absolute day number
20885 (setq start-day
(time-to-days (org-read-date nil t start-day
))))
20886 (setq org-agenda-last-arguments
(list include-all start-day ndays
))
20887 (org-compile-prefix-format 'agenda
)
20888 (org-set-sorting-strategy 'agenda
)
20889 (require 'calendar
)
20890 (let* ((org-agenda-start-on-weekday
20891 (if (or (equal ndays
7) (and (null ndays
) (equal 7 org-agenda-ndays
)))
20892 org-agenda-start-on-weekday nil
))
20893 (thefiles (org-agenda-files))
20895 (today (time-to-days
20896 (time-subtract (current-time)
20897 (list 0 (* 3600 org-extend-today-until
) 0))))
20898 (sd (or start-day today
))
20899 (start (if (or (null org-agenda-start-on-weekday
)
20900 (< org-agenda-ndays
7))
20902 (let* ((nt (calendar-day-of-week
20903 (calendar-gregorian-from-absolute sd
)))
20904 (n1 org-agenda-start-on-weekday
)
20906 (- sd
(+ (if (< d
0) 7 0) d
)))))
20907 (day-numbers (list start
))
20909 (inhibit-redisplay (not debug-on-error
))
20910 s e rtn rtnall file date d start-pos end-pos todayp nd
)
20911 (setq org-agenda-redo-command
20912 (list 'org-agenda-list
(list 'quote include-all
) start-day ndays
))
20913 ;; Make the list of days
20914 (setq ndays
(or ndays org-agenda-ndays
)
20917 (push (1+ (car day-numbers
)) day-numbers
)
20918 (setq ndays
(1- ndays
)))
20919 (setq day-numbers
(nreverse day-numbers
))
20920 (org-prepare-agenda "Day/Week")
20921 (org-set-local 'org-starting-day
(car day-numbers
))
20922 (org-set-local 'org-include-all-loc include-all
)
20923 (org-set-local 'org-agenda-span
20924 (org-agenda-ndays-to-span nd
))
20925 (when (and (or include-all org-agenda-include-all-todo
)
20926 (member today day-numbers
))
20927 (setq files thefiles
20929 (while (setq file
(pop files
))
20931 (org-check-agenda-file file
)
20932 (setq date
(calendar-gregorian-from-absolute today
)
20933 rtn
(org-agenda-get-day-entries
20935 (setq rtnall
(append rtnall rtn
))))
20937 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
20938 (add-text-properties (point-min) (1- (point))
20939 (list 'face
'org-agenda-structure
))
20940 (insert (org-finalize-agenda-entries rtnall
) "\n")))
20941 (unless org-agenda-compact-blocks
20943 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd
)))
20945 (add-text-properties s
(1- (point)) (list 'face
'org-agenda-structure
20946 'org-date-line t
)))
20947 (while (setq d
(pop day-numbers
))
20948 (setq date
(calendar-gregorian-from-absolute d
)
20950 (if (or (setq todayp
(= d today
))
20951 (and (not start-pos
) (= d sd
)))
20952 (setq start-pos
(point))
20953 (if (and start-pos
(not end-pos
))
20954 (setq end-pos
(point))))
20955 (setq files thefiles
20957 (while (setq file
(pop files
))
20959 (org-check-agenda-file file
)
20960 (if org-agenda-show-log
20961 (setq rtn
(org-agenda-get-day-entries
20963 :deadline
:scheduled
:timestamp
:sexp
:closed
))
20964 (setq rtn
(org-agenda-get-day-entries
20966 :deadline
:scheduled
:sexp
:timestamp
)))
20967 (setq rtnall
(append rtnall rtn
))))
20968 (if org-agenda-include-diary
20970 (require 'diary-lib
)
20971 (setq rtn
(org-get-entries-from-diary date
))
20972 (setq rtnall
(append rtnall rtn
))))
20973 (if (or rtnall org-agenda-show-all-dates
)
20975 (setq day-cnt
(1+ day-cnt
))
20977 (if (stringp org-agenda-format-date
)
20978 (format-time-string org-agenda-format-date
20979 (org-time-from-absolute date
))
20980 (funcall org-agenda-format-date date
))
20982 (put-text-property s
(1- (point)) 'face
'org-agenda-structure
)
20983 (put-text-property s
(1- (point)) 'org-date-line t
)
20984 (put-text-property s
(1- (point)) 'org-day-cnt day-cnt
)
20985 (if todayp
(put-text-property s
(1- (point)) 'org-today t
))
20987 (org-finalize-agenda-entries
20988 (org-agenda-add-time-grid-maybe
20991 (put-text-property s
(1- (point)) 'day d
)
20992 (put-text-property s
(1- (point)) 'org-day-cnt day-cnt
))))
20993 (goto-char (point-min))
20994 (org-fit-agenda-window)
20995 (unless (and (pos-visible-in-window-p (point-min))
20996 (pos-visible-in-window-p (point-max)))
20997 (goto-char (1- (point-max)))
20999 (if (not (pos-visible-in-window-p (or start-pos
1)))
21001 (goto-char (or start-pos
1))
21003 (goto-char (or start-pos
1))
21004 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda
))
21005 (org-finalize-agenda)
21006 (setq buffer-read-only t
)
21009 (defun org-agenda-ndays-to-span (n)
21010 (cond ((< n
7) 'day
) ((= n
7) 'week
) ((< n
32) 'month
) (t 'year
)))
21012 ;;; Agenda word search
21014 (defvar org-agenda-search-history nil
)
21017 (defun org-search-view (&optional arg string
)
21018 "Show all entries that contain words or regular expressions.
21019 If the first character of the search string is an asterisks,
21020 search only the headlines.
21022 The search string is broken into \"words\" by splitting at whitespace.
21023 The individual words are then interpreted as a boolean expression with
21024 logical AND. Words prefixed with a minus must not occur in the entry.
21025 Words without a prefix or prefixed with a plus must occur in the entry.
21026 Matching is case-insensitive and the words are enclosed by word delimiters.
21028 Words enclosed by curly braces are interpreted as regular expressions
21029 that must or must not match in the entry.
21031 This command searches the agenda files, and in addition the files listed
21032 in `org-agenda-text-search-extra-files'."
21034 (org-compile-prefix-format 'search
)
21035 (org-set-sorting-strategy 'search
)
21036 (org-prepare-agenda "SEARCH")
21037 (let* ((props (list 'face nil
21038 'done-face
'org-done
21039 'org-not-done-regexp org-not-done-regexp
21040 'org-todo-regexp org-todo-regexp
21041 'mouse-face
'highlight
21042 'keymap org-agenda-keymap
21043 'help-echo
(format "mouse-2 or RET jump to location")))
21044 regexp rtn rtnall files file pos
21045 marker priority category tags c neg re
21046 ee txt beg end words regexps
+ regexps- hdl-only buffer beg1 str
)
21047 (unless (and (not arg
)
21049 (string-match "\\S-" string
))
21050 (setq string
(read-string "[+-]Word/{Regexp} ...: "
21052 ((integerp arg
) (cons string arg
))
21054 'org-agenda-search-history
)))
21055 (setq org-agenda-redo-command
21056 (list 'org-search-view
'current-prefix-arg string
))
21057 (setq org-agenda-query-string string
)
21059 (if (equal (string-to-char string
) ?
*)
21061 words
(substring string
1))
21062 (setq words string
))
21063 (setq words
(org-split-string words
))
21065 (setq c
(string-to-char w
))
21067 (setq neg t w
(substring w
1))
21069 (setq neg nil w
(substring w
1))
21071 (if (string-match "\\`{.*}\\'" w
)
21072 (setq re
(substring w
1 -
1))
21073 (setq re
(concat "\\<" (regexp-quote (downcase w
)) "\\>")))
21074 (if neg
(push re regexps-
) (push re regexps
+)))
21076 (setq regexps
+ (sort regexps
+ (lambda (a b
) (> (length a
) (length b
)))))
21078 (setq regexp
(concat "^" org-outline-regexp
))
21079 (setq regexp
(pop regexps
+))
21080 (if hdl-only
(setq regexp
(concat "^" org-outline-regexp
".*?"
21082 (setq files
(append (org-agenda-files) org-agenda-text-search-extra-files
)
21084 (while (setq file
(pop files
))
21087 (org-check-agenda-file file
)
21088 (setq buffer
(if (file-exists-p file
)
21089 (org-get-agenda-file-buffer file
)
21090 (error "No such file %s" file
)))
21092 ;; If file does not exist, make sure an error message is sent
21093 (setq rtn
(list (format "ORG-AGENDA-ERROR: No such org-file %s"
21095 (with-current-buffer buffer
21096 (unless (org-mode-p)
21097 (error "Agenda file %s is not in `org-mode'" file
))
21098 (let ((case-fold-search t
))
21101 (if org-agenda-restrict
21102 (narrow-to-region org-agenda-restrict-begin
21103 org-agenda-restrict-end
)
21105 (goto-char (point-min))
21106 (unless (or (org-on-heading-p)
21107 (outline-next-heading))
21108 (throw 'nextfile t
))
21109 (goto-char (max (point-min) (1- (point))))
21110 (while (re-search-forward regexp nil t
)
21111 (org-back-to-heading t
)
21112 (skip-chars-forward "* ")
21113 (setq beg
(point-at-bol)
21115 end
(progn (outline-next-heading) (point)))
21119 (setq str
(buffer-substring-no-properties
21121 (if hdl-only
(point-at-eol) end
)))
21122 (mapc (lambda (wr) (when (string-match wr str
)
21123 (goto-char (1- end
))
21126 (mapc (lambda (wr) (unless (string-match wr str
)
21127 (goto-char (1- end
))
21131 (setq marker
(org-agenda-new-marker (point))
21132 category
(org-get-category)
21133 tags
(org-get-tags-at (point))
21134 txt
(org-format-agenda-item
21136 (buffer-substring-no-properties
21137 beg1
(point-at-eol))
21139 (org-add-props txt props
21140 'org-marker marker
'org-hd-marker marker
21141 'priority
1000 'org-category category
21144 (goto-char (1- end
)))))))))
21145 (setq rtn
(nreverse ee
))
21146 (setq rtnall
(append rtnall rtn
)))
21147 (if org-agenda-overriding-header
21148 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
21149 nil
'face
'org-agenda-structure
) "\n")
21150 (insert "Search words: ")
21151 (add-text-properties (point-min) (1- (point))
21152 (list 'face
'org-agenda-structure
))
21154 (insert string
"\n")
21155 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
21157 (unless org-agenda-multi
21158 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
21159 (add-text-properties pos
(1- (point))
21160 (list 'face
'org-agenda-structure
))))
21162 (insert (org-finalize-agenda-entries rtnall
) "\n"))
21163 (goto-char (point-min))
21164 (org-fit-agenda-window)
21165 (add-text-properties (point-min) (point-max) '(org-agenda-type search
))
21166 (org-finalize-agenda)
21167 (setq buffer-read-only t
)))
21169 ;;; Agenda TODO list
21171 (defvar org-select-this-todo-keyword nil
)
21172 (defvar org-last-arg nil
)
21175 (defun org-todo-list (arg)
21176 "Show all TODO entries from all agenda file in a single list.
21177 The prefix arg can be used to select a specific TODO keyword and limit
21178 the list to these. When using \\[universal-argument], you will be prompted
21179 for a keyword. A numeric prefix directly selects the Nth keyword in
21180 `org-todo-keywords-1'."
21182 (require 'calendar
)
21183 (org-compile-prefix-format 'todo
)
21184 (org-set-sorting-strategy 'todo
)
21185 (org-prepare-agenda "TODO")
21186 (let* ((today (time-to-days (current-time)))
21187 (date (calendar-gregorian-from-absolute today
))
21188 (kwds org-todo-keywords-for-agenda
)
21189 (completion-ignore-case t
)
21190 (org-select-this-todo-keyword
21191 (if (stringp arg
) arg
21192 (and arg
(integerp arg
) (> arg
0)
21193 (nth (1- arg
) kwds
))))
21194 rtn rtnall files file pos
)
21195 (when (equal arg
'(4))
21196 (setq org-select-this-todo-keyword
21197 (completing-read "Keyword (or KWD1|K2D2|...): "
21198 (mapcar 'list kwds
) nil nil
)))
21199 (and (equal 0 arg
) (setq org-select-this-todo-keyword nil
))
21200 (org-set-local 'org-last-arg arg
)
21201 (setq org-agenda-redo-command
21202 '(org-todo-list (or current-prefix-arg org-last-arg
)))
21203 (setq files
(org-agenda-files)
21205 (while (setq file
(pop files
))
21207 (org-check-agenda-file file
)
21208 (setq rtn
(org-agenda-get-day-entries file date
:todo
))
21209 (setq rtnall
(append rtnall rtn
))))
21210 (if org-agenda-overriding-header
21211 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
21212 nil
'face
'org-agenda-structure
) "\n")
21213 (insert "Global list of TODO items of type: ")
21214 (add-text-properties (point-min) (1- (point))
21215 (list 'face
'org-agenda-structure
))
21217 (insert (or org-select-this-todo-keyword
"ALL") "\n")
21218 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
21220 (unless org-agenda-multi
21221 (insert "Available with `N r': (0)ALL")
21224 (setq s
(format "(%d)%s" (setq n
(1+ n
)) x
))
21225 (if (> (+ (current-column) (string-width s
) 1) (frame-width))
21230 (add-text-properties pos
(1- (point)) (list 'face
'org-agenda-structure
)))
21232 (insert (org-finalize-agenda-entries rtnall
) "\n"))
21233 (goto-char (point-min))
21234 (org-fit-agenda-window)
21235 (add-text-properties (point-min) (point-max) '(org-agenda-type todo
))
21236 (org-finalize-agenda)
21237 (setq buffer-read-only t
)))
21239 ;;; Agenda tags match
21242 (defun org-tags-view (&optional todo-only match
)
21243 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
21244 The prefix arg TODO-ONLY limits the search to TODO entries."
21246 (org-compile-prefix-format 'tags
)
21247 (org-set-sorting-strategy 'tags
)
21248 (let* ((org-tags-match-list-sublevels
21249 (if todo-only t org-tags-match-list-sublevels
))
21250 (completion-ignore-case t
)
21251 rtn rtnall files file pos matcher
21253 (setq matcher
(org-make-tags-matcher match
)
21254 match
(car matcher
) matcher
(cdr matcher
))
21255 (org-prepare-agenda (concat "TAGS " match
))
21256 (setq org-agenda-query-string match
)
21257 (setq org-agenda-redo-command
21258 (list 'org-tags-view
(list 'quote todo-only
)
21259 (list 'if
'current-prefix-arg nil
'org-agenda-query-string
)))
21260 (setq files
(org-agenda-files)
21262 (while (setq file
(pop files
))
21264 (org-check-agenda-file file
)
21265 (setq buffer
(if (file-exists-p file
)
21266 (org-get-agenda-file-buffer file
)
21267 (error "No such file %s" file
)))
21269 ;; If file does not exist, merror message to agenda
21271 (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
21272 rtnall
(append rtnall rtn
))
21273 (with-current-buffer buffer
21274 (unless (org-mode-p)
21275 (error "Agenda file %s is not in `org-mode'" file
))
21278 (if org-agenda-restrict
21279 (narrow-to-region org-agenda-restrict-begin
21280 org-agenda-restrict-end
)
21282 (setq rtn
(org-scan-tags 'agenda matcher todo-only
))
21283 (setq rtnall
(append rtnall rtn
))))))))
21284 (if org-agenda-overriding-header
21285 (insert (org-add-props (copy-sequence org-agenda-overriding-header
)
21286 nil
'face
'org-agenda-structure
) "\n")
21287 (insert "Headlines with TAGS match: ")
21288 (add-text-properties (point-min) (1- (point))
21289 (list 'face
'org-agenda-structure
))
21291 (insert match
"\n")
21292 (add-text-properties pos
(1- (point)) (list 'face
'org-warning
))
21294 (unless org-agenda-multi
21295 (insert "Press `C-u r' to search again with new search string\n"))
21296 (add-text-properties pos
(1- (point)) (list 'face
'org-agenda-structure
)))
21298 (insert (org-finalize-agenda-entries rtnall
) "\n"))
21299 (goto-char (point-min))
21300 (org-fit-agenda-window)
21301 (add-text-properties (point-min) (point-max) '(org-agenda-type tags
))
21302 (org-finalize-agenda)
21303 (setq buffer-read-only t
)))
21305 ;;; Agenda Finding stuck projects
21307 (defvar org-agenda-skip-regexp nil
21308 "Regular expression used in skipping subtrees for the agenda.
21309 This is basically a temporary global variable that can be set and then
21310 used by user-defined selections using `org-agenda-skip-function'.")
21312 (defvar org-agenda-overriding-header nil
21313 "When this is set during todo and tags searches, will replace header.")
21315 (defun org-agenda-skip-subtree-when-regexp-matches ()
21316 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
21317 If yes, it returns the end position of this tree, causing agenda commands
21318 to skip this subtree. This is a function that can be put into
21319 `org-agenda-skip-function' for the duration of a command."
21320 (let ((end (save-excursion (org-end-of-subtree t
)))
21323 (setq skip
(re-search-forward org-agenda-skip-regexp end t
)))
21326 (defun org-agenda-skip-entry-if (&rest conditions
)
21327 "Skip entry if any of CONDITIONS is true.
21328 See `org-agenda-skip-if' for details."
21329 (org-agenda-skip-if nil conditions
))
21331 (defun org-agenda-skip-subtree-if (&rest conditions
)
21332 "Skip entry if any of CONDITIONS is true.
21333 See `org-agenda-skip-if' for details."
21334 (org-agenda-skip-if t conditions
))
21336 (defun org-agenda-skip-if (subtree conditions
)
21337 "Checks current entity for CONDITIONS.
21338 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
21339 the entry, i.e. the text before the next heading is checked.
21341 CONDITIONS is a list of symbols, boolean OR is used to combine the results
21342 from different tests. Valid conditions are:
21344 scheduled Check if there is a scheduled cookie
21345 notscheduled Check if there is no scheduled cookie
21346 deadline Check if there is a deadline
21347 notdeadline Check if there is no deadline
21348 regexp Check if regexp matches
21349 notregexp Check if regexp does not match.
21351 The regexp is taken from the conditions list, it must come right after
21352 the `regexp' or `notregexp' element.
21354 If any of these conditions is met, this function returns the end point of
21355 the entity, causing the search to continue from there. This is a function
21356 that can be put into `org-agenda-skip-function' for the duration of a command."
21358 (org-back-to-heading t
)
21361 (progn (org-end-of-subtree t
) (point))
21362 (progn (outline-next-heading) (1- (point)))))
21366 (and (memq 'scheduled conditions
)
21367 (re-search-forward org-scheduled-time-regexp end t
))
21368 (and (memq 'notscheduled conditions
)
21369 (not (re-search-forward org-scheduled-time-regexp end t
)))
21370 (and (memq 'deadline conditions
)
21371 (re-search-forward org-deadline-time-regexp end t
))
21372 (and (memq 'notdeadline conditions
)
21373 (not (re-search-forward org-deadline-time-regexp end t
)))
21374 (and (setq m
(memq 'regexp conditions
))
21375 (stringp (nth 1 m
))
21376 (re-search-forward (nth 1 m
) end t
))
21377 (and (setq m
(memq 'notregexp conditions
))
21378 (stringp (nth 1 m
))
21379 (not (re-search-forward (nth 1 m
) end t
))))
21383 (defun org-agenda-list-stuck-projects (&rest ignore
)
21384 "Create agenda view for projects that are stuck.
21385 Stuck projects are project that have no next actions. For the definitions
21386 of what a project is and how to check if it stuck, customize the variable
21387 `org-stuck-projects'.
21388 MATCH is being ignored."
21390 (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches
)
21391 ;; FIXME: we could have used org-agenda-skip-if here.
21392 (org-agenda-overriding-header "List of stuck projects: ")
21393 (matcher (nth 0 org-stuck-projects
))
21394 (todo (nth 1 org-stuck-projects
))
21395 (todo-wds (if (member "*" todo
)
21397 (org-prepare-agenda-buffers (org-agenda-files))
21399 org-done-keywords-for-agenda
21400 (copy-sequence org-todo-keywords-for-agenda
)))
21402 (todo-re (concat "^\\*+[ \t]+\\("
21403 (mapconcat 'identity todo-wds
"\\|")
21405 (tags (nth 2 org-stuck-projects
))
21406 (tags-re (if (member "*" tags
)
21407 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
21408 (concat "^\\*+ .*:\\("
21409 (mapconcat 'identity tags
"\\|")
21410 (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
21411 (gen-re (nth 3 org-stuck-projects
))
21417 (and gen-re
(stringp gen-re
) (string-match "\\S-" gen-re
)
21419 (setq org-agenda-skip-regexp
21421 (mapconcat 'identity re-list
"\\|")
21422 (error "No information how to identify unstuck projects")))
21423 (org-tags-view nil matcher
)
21424 (with-current-buffer org-agenda-buffer-name
21425 (setq org-agenda-redo-command
21426 '(org-agenda-list-stuck-projects
21427 (or current-prefix-arg org-last-arg
))))))
21429 ;;; Diary integration
21431 (defvar org-disable-agenda-to-diary nil
) ;Dynamically-scoped param.
21433 (defun org-get-entries-from-diary (date)
21434 "Get the (Emacs Calendar) diary entries for DATE."
21435 (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
21436 (diary-display-hook '(fancy-diary-display))
21437 (pop-up-frames nil
)
21438 (list-diary-entries-hook
21439 (cons 'org-diary-default-entry list-diary-entries-hook
))
21440 (diary-file-name-prefix-function nil
) ; turn this feature off
21441 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string
)
21443 (org-disable-agenda-to-diary t
))
21445 (save-window-excursion
21446 (funcall (if (fboundp 'diary-list-entries
)
21447 'diary-list-entries
'list-diary-entries
)
21449 (if (not (get-buffer fancy-diary-buffer
))
21451 (with-current-buffer fancy-diary-buffer
21452 (setq buffer-read-only nil
)
21453 (if (zerop (buffer-size))
21456 ;; Omit the date and other unnecessary stuff
21457 (org-agenda-cleanup-fancy-diary)
21458 ;; Add prefix to each line and extend the text properties
21459 (if (zerop (buffer-size))
21461 (setq entries
(buffer-substring (point-min) (- (point-max) 1)))))
21462 (set-buffer-modified-p nil
)
21463 (kill-buffer fancy-diary-buffer
)))
21465 (setq entries
(org-split-string entries
"\n"))
21469 (setq x
(org-format-agenda-item "" x
"Diary" nil
'time
))
21470 ;; Extend the text properties to the beginning of the line
21471 (org-add-props x
(text-properties-at (1- (length x
)) x
)
21472 'type
"diary" 'date date
))
21475 (defun org-agenda-cleanup-fancy-diary ()
21476 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
21477 This gets rid of the date, the underline under the date, and
21478 the dummy entry installed by `org-mode' to ensure non-empty diary for each
21479 date. It also removes lines that contain only whitespace."
21480 (goto-char (point-min))
21481 (if (looking-at ".*?:[ \t]*")
21484 (re-search-forward "\n=+$" nil t
)
21486 (while (re-search-backward "^ +\n?" nil t
) (replace-match "")))
21487 (re-search-forward "\n=+$" nil t
)
21488 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
21489 (goto-char (point-min))
21490 (while (re-search-forward "^ +\n" nil t
)
21491 (replace-match ""))
21492 (goto-char (point-min))
21493 (if (re-search-forward "^Org-mode dummy\n?" nil t
)
21494 (replace-match "")))
21496 ;; Make sure entries from the diary have the right text properties.
21497 (eval-after-load "diary-lib"
21498 '(if (boundp 'diary-modify-entry-list-string-function
)
21499 ;; We can rely on the hook, nothing to do
21501 ;; Hook not avaiable, must use advice to make this work
21502 (defadvice add-to-diary-list
(before org-mark-diary-entry activate
)
21503 "Make the position visible."
21504 (if (and org-disable-agenda-to-diary
;; called from org-agenda
21507 (setq string
(org-modify-diary-entry-string string
))))))
21509 (defun org-modify-diary-entry-string (string)
21510 "Add text properties to string, allowing org-mode to act on it."
21511 (org-add-props string nil
21512 'mouse-face
'highlight
21513 'keymap org-agenda-keymap
21514 'help-echo
(if buffer-file-name
21515 (format "mouse-2 or RET jump to diary file %s"
21516 (abbreviate-file-name buffer-file-name
))
21518 'org-agenda-diary-link t
21519 'org-marker
(org-agenda-new-marker (point-at-bol))))
21521 (defun org-diary-default-entry ()
21522 "Add a dummy entry to the diary.
21523 Needed to avoid empty dates which mess up holiday display."
21524 ;; Catch the error if dealing with the new add-to-diary-alist
21525 (when org-disable-agenda-to-diary
21526 (condition-case nil
21527 (add-to-diary-list original-date
"Org-mode dummy" "")
21529 (add-to-diary-list original-date
"Org-mode dummy" "" nil
)))))
21532 (defun org-diary (&rest args
)
21533 "Return diary information from org-files.
21534 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
21535 It accesses org files and extracts information from those files to be
21536 listed in the diary. The function accepts arguments specifying what
21537 items should be listed. The following arguments are allowed:
21539 :timestamp List the headlines of items containing a date stamp or
21540 date range matching the selected date. Deadlines will
21541 also be listed, on the expiration day.
21543 :sexp List entries resulting from diary-like sexps.
21545 :deadline List any deadlines past due, or due within
21546 `org-deadline-warning-days'. The listing occurs only
21547 in the diary for *today*, not at any other date. If
21548 an entry is marked DONE, it is no longer listed.
21550 :scheduled List all items which are scheduled for the given date.
21551 The diary for *today* also contains items which were
21552 scheduled earlier and are not yet marked DONE.
21554 :todo List all TODO items from the org-file. This may be a
21555 long list - so this is not turned on by default.
21556 Like deadlines, these entries only show up in the
21557 diary for *today*, not at any other date.
21559 The call in the diary file should look like this:
21561 &%%(org-diary) ~/path/to/some/orgfile.org
21563 Use a separate line for each org file to check. Or, if you omit the file name,
21564 all files listed in `org-agenda-files' will be checked automatically:
21568 If you don't give any arguments (as in the example above), the default
21569 arguments (:deadline :scheduled :timestamp :sexp) are used.
21570 So the example above may also be written as
21572 &%%(org-diary :deadline :timestamp :sexp :scheduled)
21574 The function expects the lisp variables `entry' and `date' to be provided
21575 by the caller, because this is how the calendar works. Don't use this
21576 function from a program - use `org-agenda-get-day-entries' instead."
21577 (when (> (- (time-to-seconds (current-time))
21578 org-agenda-last-marker-time
)
21580 (org-agenda-reset-markers))
21581 (org-compile-prefix-format 'agenda
)
21582 (org-set-sorting-strategy 'agenda
)
21583 (setq args
(or args
'(:deadline
:scheduled
:timestamp
:sexp
)))
21584 (let* ((files (if (and entry
(stringp entry
) (string-match "\\S-" entry
))
21586 (org-agenda-files t
)))
21588 (org-prepare-agenda-buffers files
)
21589 ;; If this is called during org-agenda, don't return any entries to
21590 ;; the calendar. Org Agenda will list these entries itself.
21591 (if org-disable-agenda-to-diary
(setq files nil
))
21592 (while (setq file
(pop files
))
21593 (setq rtn
(apply 'org-agenda-get-day-entries file date args
))
21594 (setq results
(append results rtn
)))
21596 (concat (org-finalize-agenda-entries results
) "\n"))))
21598 ;;; Agenda entry finders
21600 (defun org-agenda-get-day-entries (file date
&rest args
)
21601 "Does the work for `org-diary' and `org-agenda'.
21602 FILE is the path to a file to be checked for entries. DATE is date like
21603 the one returned by `calendar-current-date'. ARGS are symbols indicating
21604 which kind of entries should be extracted. For details about these, see
21605 the documentation of `org-diary'."
21606 (setq args
(or args
'(:deadline
:scheduled
:timestamp
:sexp
)))
21607 (let* ((org-startup-folded nil
)
21608 (org-startup-align-all-tables nil
)
21609 (buffer (if (file-exists-p file
)
21610 (org-get-agenda-file-buffer file
)
21611 (error "No such file %s" file
)))
21614 ;; If file does not exist, make sure an error message ends up in diary
21615 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file
))
21616 (with-current-buffer buffer
21617 (unless (org-mode-p)
21618 (error "Agenda file %s is not in `org-mode'" file
))
21619 (let ((case-fold-search nil
))
21622 (if org-agenda-restrict
21623 (narrow-to-region org-agenda-restrict-begin
21624 org-agenda-restrict-end
)
21626 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
21627 (while (setq arg
(pop args
))
21629 ((and (eq arg
:todo
)
21630 (equal date
(calendar-current-date)))
21631 (setq rtn
(org-agenda-get-todos))
21632 (setq results
(append results rtn
)))
21633 ((eq arg
:timestamp
)
21634 (setq rtn
(org-agenda-get-blocks))
21635 (setq results
(append results rtn
))
21636 (setq rtn
(org-agenda-get-timestamps))
21637 (setq results
(append results rtn
)))
21639 (setq rtn
(org-agenda-get-sexps))
21640 (setq results
(append results rtn
)))
21641 ((eq arg
:scheduled
)
21642 (setq rtn
(org-agenda-get-scheduled))
21643 (setq results
(append results rtn
)))
21645 (setq rtn
(org-agenda-get-closed))
21646 (setq results
(append results rtn
)))
21647 ((eq arg
:deadline
)
21648 (setq rtn
(org-agenda-get-deadlines))
21649 (setq results
(append results rtn
))))))))
21652 (defun org-entry-is-todo-p ()
21653 (member (org-get-todo-state) org-not-done-keywords
))
21655 (defun org-entry-is-done-p ()
21656 (member (org-get-todo-state) org-done-keywords
))
21658 (defun org-get-todo-state ()
21660 (org-back-to-heading t
)
21661 (and (looking-at org-todo-line-regexp
)
21663 (match-string 2))))
21665 (defun org-at-date-range-p (&optional inactive-ok
)
21666 "Is the cursor inside a date range?"
21670 (let ((pos (point)))
21671 (skip-chars-backward "^[<\r\n")
21672 (skip-chars-backward "<[")
21673 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
21674 (>= (match-end 0) pos
)
21676 (skip-chars-backward "^<[\r\n")
21677 (skip-chars-backward "<[")
21678 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp
))
21679 (>= (match-end 0) pos
)
21683 (defun org-agenda-get-todos ()
21684 "Return the TODO information for agenda display."
21685 (let* ((props (list 'face nil
21686 'done-face
'org-done
21687 'org-not-done-regexp org-not-done-regexp
21688 'org-todo-regexp org-todo-regexp
21689 'mouse-face
'highlight
21690 'keymap org-agenda-keymap
21692 (format "mouse-2 or RET jump to org file %s"
21693 (abbreviate-file-name buffer-file-name
))))
21694 ;; FIXME: get rid of the \n at some point but watch out
21695 (regexp (concat "^\\*+[ \t]+\\("
21696 (if org-select-this-todo-keyword
21697 (if (equal org-select-this-todo-keyword
"*")
21700 (mapconcat 'identity
(org-split-string org-select-this-todo-keyword
"|") "\\|")
21702 org-not-done-regexp
)
21704 marker priority category tags
21706 (goto-char (point-min))
21707 (while (re-search-forward regexp nil t
)
21710 (beginning-of-line)
21711 (setq beg
(point) end
(progn (outline-next-heading) (point)))
21712 (when (or (and org-agenda-todo-ignore-with-date
(goto-char beg
)
21713 (re-search-forward org-ts-regexp end t
))
21714 (and org-agenda-todo-ignore-scheduled
(goto-char beg
)
21715 (re-search-forward org-scheduled-time-regexp end t
))
21716 (and org-agenda-todo-ignore-deadlines
(goto-char beg
)
21717 (re-search-forward org-deadline-time-regexp end t
)
21718 (org-deadline-close (match-string 1))))
21719 (goto-char (1+ beg
))
21720 (or org-agenda-todo-list-sublevels
(org-end-of-subtree 'invisible
))
21721 (throw :skip nil
)))
21724 (goto-char (match-beginning 1))
21725 (setq marker
(org-agenda-new-marker (match-beginning 0))
21726 category
(org-get-category)
21727 tags
(org-get-tags-at (point))
21728 txt
(org-format-agenda-item "" (match-string 1) category tags
)
21729 priority
(1+ (org-get-priority txt
)))
21730 (org-add-props txt props
21731 'org-marker marker
'org-hd-marker marker
21732 'priority priority
'org-category category
21735 (if org-agenda-todo-list-sublevels
21736 (goto-char (match-end 1))
21737 (org-end-of-subtree 'invisible
))))
21740 (defconst org-agenda-no-heading-message
21741 "No heading for this item in buffer or region.")
21743 (defun org-agenda-get-timestamps ()
21744 "Return the date stamp information for agenda display."
21745 (let* ((props (list 'face nil
21746 'org-not-done-regexp org-not-done-regexp
21747 'org-todo-regexp org-todo-regexp
21748 'mouse-face
'highlight
21749 'keymap org-agenda-keymap
21751 (format "mouse-2 or RET jump to org file %s"
21752 (abbreviate-file-name buffer-file-name
))))
21753 (d1 (calendar-absolute-from-gregorian date
))
21757 (format-time-string
21759 (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
21765 (format-time-string
21766 (car org-time-stamp-formats
)
21767 (apply 'encode-time
; DATE bound by calendar
21768 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
21770 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
21771 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
21772 marker hdmarker deadlinep scheduledp donep tmp priority category
21773 ee txt timestr tags b0 b3 e3 head
)
21774 (goto-char (point-min))
21775 (while (re-search-forward regexp nil t
)
21776 (setq b0
(match-beginning 0)
21777 b3
(match-beginning 3) e3
(match-end 3))
21779 (and (org-at-date-range-p) (throw :skip nil
))
21781 (if (and (match-end 1)
21782 (not (= d1
(org-time-string-to-absolute (match-string 1) d1
))))
21785 (not (org-diary-sexp-entry (buffer-substring b3 e3
) "" date
)))
21787 (setq marker
(org-agenda-new-marker b0
)
21788 category
(org-get-category b0
)
21789 tmp
(buffer-substring (max (point-min)
21790 (- b0 org-ds-keyword-length
))
21792 timestr
(if b3
"" (buffer-substring b0
(point-at-eol)))
21793 deadlinep
(string-match org-deadline-regexp tmp
)
21794 scheduledp
(string-match org-scheduled-regexp tmp
)
21795 donep
(org-entry-is-done-p))
21796 (if (or scheduledp deadlinep
) (throw :skip t
))
21797 (if (string-match ">" timestr
)
21798 ;; substring should only run to end of time stamp
21799 (setq timestr
(substring timestr
0 (match-end 0))))
21801 (if (re-search-backward "^\\*+ " nil t
)
21803 (goto-char (match-beginning 0))
21804 (setq hdmarker
(org-agenda-new-marker)
21805 tags
(org-get-tags-at))
21806 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
21807 (setq head
(match-string 1))
21808 (and org-agenda-skip-timestamp-if-done donep
(throw :skip t
))
21809 (setq txt
(org-format-agenda-item
21810 nil head category tags timestr nil
21812 (setq txt org-agenda-no-heading-message
))
21813 (setq priority
(org-get-priority txt
))
21814 (org-add-props txt props
21815 'org-marker marker
'org-hd-marker hdmarker
)
21816 (org-add-props txt nil
'priority priority
21817 'org-category category
'date date
21820 (outline-next-heading)))
21823 (defun org-agenda-get-sexps ()
21824 "Return the sexp information for agenda display."
21825 (require 'diary-lib
)
21826 (let* ((props (list 'face nil
21827 'mouse-face
'highlight
21828 'keymap org-agenda-keymap
21830 (format "mouse-2 or RET jump to org file %s"
21831 (abbreviate-file-name buffer-file-name
))))
21833 marker category ee txt tags entry result beg b sexp sexp-entry
)
21834 (goto-char (point-min))
21835 (while (re-search-forward regexp nil t
)
21838 (setq beg
(match-beginning 0))
21839 (goto-char (1- (match-end 0)))
21842 (setq sexp
(buffer-substring b
(point)))
21843 (setq sexp-entry
(if (looking-at "[ \t]*\\(\\S-.*\\)")
21844 (org-trim (match-string 1))
21846 (setq result
(org-diary-sexp-entry sexp sexp-entry date
))
21848 (setq marker
(org-agenda-new-marker beg
)
21849 category
(org-get-category beg
))
21851 (if (string-match "\\S-" result
)
21853 (setq txt
"SEXP entry returned empty string"))
21855 (setq txt
(org-format-agenda-item
21856 "" txt category tags
'time
))
21857 (org-add-props txt props
'org-marker marker
)
21858 (org-add-props txt nil
21859 'org-category category
'date date
21864 (defun org-agenda-get-closed ()
21865 "Return the logged TODO entries for agenda display."
21866 (let* ((props (list 'mouse-face
'highlight
21867 'org-not-done-regexp org-not-done-regexp
21868 'org-todo-regexp org-todo-regexp
21869 'keymap org-agenda-keymap
21871 (format "mouse-2 or RET jump to org file %s"
21872 (abbreviate-file-name buffer-file-name
))))
21874 "\\<\\(" org-closed-string
"\\|" org-clock-string
"\\) *\\["
21877 (format-time-string
21878 (car org-time-stamp-formats
)
21879 (apply 'encode-time
; DATE bound by calendar
21880 (list 0 0 0 (nth 1 date
) (car date
) (nth 2 date
))))
21882 marker hdmarker priority category tags closedp
21884 (goto-char (point-min))
21885 (while (re-search-forward regexp nil t
)
21888 (setq marker
(org-agenda-new-marker (match-beginning 0))
21889 closedp
(equal (match-string 1) org-closed-string
)
21890 category
(org-get-category (match-beginning 0))
21891 timestr
(buffer-substring (match-beginning 0) (point-at-eol))
21892 ;; donep (org-entry-is-done-p)
21894 (if (string-match "\\]" timestr
)
21895 ;; substring should only run to end of time stamp
21896 (setq timestr
(substring timestr
0 (match-end 0))))
21898 (if (re-search-backward "^\\*+ " nil t
)
21900 (goto-char (match-beginning 0))
21901 (setq hdmarker
(org-agenda-new-marker)
21902 tags
(org-get-tags-at))
21903 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
21904 (setq txt
(org-format-agenda-item
21905 (if closedp
"Closed: " "Clocked: ")
21906 (match-string 1) category tags timestr
)))
21907 (setq txt org-agenda-no-heading-message
))
21908 (setq priority
100000)
21909 (org-add-props txt props
21910 'org-marker marker
'org-hd-marker hdmarker
'face
'org-done
21911 'priority priority
'org-category category
21912 'type
"closed" 'date date
21913 'undone-face
'org-warning
'done-face
'org-done
)
21915 (goto-char (point-at-eol))))
21918 (defun org-agenda-get-deadlines ()
21919 "Return the deadline information for agenda display."
21920 (let* ((props (list 'mouse-face
'highlight
21921 'org-not-done-regexp org-not-done-regexp
21922 'org-todo-regexp org-todo-regexp
21923 'keymap org-agenda-keymap
21925 (format "mouse-2 or RET jump to org file %s"
21926 (abbreviate-file-name buffer-file-name
))))
21927 (regexp org-deadline-time-regexp
)
21928 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
21929 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
21930 d2 diff dfrac wdays pos pos1 category tags
21931 ee txt head face s upcomingp donep timestr
)
21932 (goto-char (point-min))
21933 (while (re-search-forward regexp nil t
)
21936 (setq s
(match-string 1)
21937 pos
(1- (match-beginning 1))
21938 d2
(org-time-string-to-absolute (match-string 1) d1
'past
)
21940 wdays
(org-get-wdays s
)
21941 dfrac
(/ (* 1.0 (- wdays diff
)) (max wdays
1))
21942 upcomingp
(and todayp
(> diff
0)))
21943 ;; When to show a deadline in the calendar:
21944 ;; If the expiration is within wdays warning time.
21945 ;; Past-due deadlines are only shown on the current date
21946 (if (or (and (<= diff wdays
)
21947 (and todayp
(not org-agenda-only-exact-dates
)))
21950 (setq category
(org-get-category))
21951 (if (re-search-backward "^\\*+[ \t]+" nil t
)
21953 (goto-char (match-end 0))
21954 (setq pos1
(match-beginning 0))
21955 (setq tags
(org-get-tags-at pos1
))
21956 (setq head
(buffer-substring-no-properties
21958 (progn (skip-chars-forward "^\r\n")
21960 (setq donep
(string-match org-looking-at-done-regexp head
))
21961 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s
)
21963 (concat (substring s
(match-beginning 1)) " "))
21964 (setq timestr
'time
))
21966 (or org-agenda-skip-deadline-if-done
21969 (setq txt
(org-format-agenda-item
21971 (car org-agenda-deadline-leaders
)
21972 (format (nth 1 org-agenda-deadline-leaders
)
21974 head category tags timestr
))))
21975 (setq txt org-agenda-no-heading-message
))
21977 (setq face
(org-agenda-deadline-face dfrac wdays
))
21978 (org-add-props txt props
21979 'org-marker
(org-agenda-new-marker pos
)
21980 'org-hd-marker
(org-agenda-new-marker pos1
)
21981 'priority
(+ (- diff
)
21982 (org-get-priority txt
))
21983 'org-category category
21984 'type
(if upcomingp
"upcoming-deadline" "deadline")
21985 'date
(if upcomingp date d2
)
21986 'face
(if donep
'org-done face
)
21987 'undone-face face
'done-face
'org-done
)
21991 (defun org-agenda-deadline-face (fraction &optional wdays
)
21992 "Return the face to displaying a deadline item.
21993 FRACTION is what fraction of the head-warning time has passed."
21994 (if (equal wdays
0) (setq fraction
1.
))
21995 (let ((faces org-agenda-deadline-faces
) f
)
21997 (while (setq f
(pop faces
))
21998 (if (>= fraction
(car f
)) (throw 'exit
(cdr f
)))))))
22000 (defun org-agenda-get-scheduled ()
22001 "Return the scheduled information for agenda display."
22002 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
22003 'org-todo-regexp org-todo-regexp
22004 'done-face
'org-done
22005 'mouse-face
'highlight
22006 'keymap org-agenda-keymap
22008 (format "mouse-2 or RET jump to org file %s"
22009 (abbreviate-file-name buffer-file-name
))))
22010 (regexp org-scheduled-time-regexp
)
22011 (todayp (equal date
(calendar-current-date))) ; DATE bound by calendar
22012 (d1 (calendar-absolute-from-gregorian date
)) ; DATE bound by calendar
22013 d2 diff pos pos1 category tags
22014 ee txt head pastschedp donep face timestr s
)
22015 (goto-char (point-min))
22016 (while (re-search-forward regexp nil t
)
22019 (setq s
(match-string 1)
22020 pos
(1- (match-beginning 1))
22021 d2
(org-time-string-to-absolute (match-string 1) d1
'past
)
22023 ;;; do we need to do this for deadleine too????
22024 ;;; d2 (org-time-string-to-absolute (match-string 1) (if todayp nil d1))
22026 (setq pastschedp
(and todayp
(< diff
0)))
22027 ;; When to show a scheduled item in the calendar:
22028 ;; If it is on or past the date.
22029 (if (or (and (< diff
0)
22030 (and todayp
(not org-agenda-only-exact-dates
)))
22033 (setq category
(org-get-category))
22034 (if (re-search-backward "^\\*+[ \t]+" nil t
)
22036 (goto-char (match-end 0))
22037 (setq pos1
(match-beginning 0))
22038 (setq tags
(org-get-tags-at))
22039 (setq head
(buffer-substring-no-properties
22041 (progn (skip-chars-forward "^\r\n") (point))))
22042 (setq donep
(string-match org-looking-at-done-regexp head
))
22043 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s
)
22045 (concat (substring s
(match-beginning 1)) " "))
22046 (setq timestr
'time
))
22048 (or org-agenda-skip-scheduled-if-done
22051 (setq txt
(org-format-agenda-item
22053 (car org-agenda-scheduled-leaders
)
22054 (format (nth 1 org-agenda-scheduled-leaders
)
22056 head category tags timestr
))))
22057 (setq txt org-agenda-no-heading-message
))
22059 (setq face
(if pastschedp
22060 'org-scheduled-previously
22061 'org-scheduled-today
))
22062 (org-add-props txt props
22064 'face
(if donep
'org-done face
)
22065 'org-marker
(org-agenda-new-marker pos
)
22066 'org-hd-marker
(org-agenda-new-marker pos1
)
22067 'type
(if pastschedp
"past-scheduled" "scheduled")
22068 'date
(if pastschedp d2 date
)
22069 'priority
(+ 94 (- 5 diff
) (org-get-priority txt
))
22070 'org-category category
)
22074 (defun org-agenda-get-blocks ()
22075 "Return the date-range information for agenda display."
22076 (let* ((props (list 'face nil
22077 'org-not-done-regexp org-not-done-regexp
22078 'org-todo-regexp org-todo-regexp
22079 'mouse-face
'highlight
22080 'keymap org-agenda-keymap
22082 (format "mouse-2 or RET jump to org file %s"
22083 (abbreviate-file-name buffer-file-name
))))
22084 (regexp org-tr-regexp
)
22085 (d0 (calendar-absolute-from-gregorian date
))
22086 marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos
22088 (goto-char (point-min))
22089 (while (re-search-forward regexp nil t
)
22093 (setq timestr
(match-string 0)
22094 s1
(match-string 1)
22095 s2
(match-string 2)
22096 d1
(time-to-days (org-time-string-to-time s1
))
22097 d2
(time-to-days (org-time-string-to-time s2
)))
22098 (if (and (> (- d0 d1
) -
1) (> (- d2 d0
) -
1))
22099 ;; Only allow days between the limits, because the normal
22100 ;; date stamps will catch the limits.
22102 (setq marker
(org-agenda-new-marker (point)))
22103 (setq category
(org-get-category))
22104 (if (re-search-backward "^\\*+ " nil t
)
22106 (goto-char (match-beginning 0))
22107 (setq hdmarker
(org-agenda-new-marker (point)))
22108 (setq tags
(org-get-tags-at))
22109 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
22110 (setq head
(match-string 1))
22111 (and org-agenda-skip-timestamp-if-done
22112 (org-entry-is-done-p)
22114 (setq txt
(org-format-agenda-item
22115 (format (if (= d1 d2
) "" "(%d/%d): ")
22116 (1+ (- d0 d1
)) (1+ (- d2 d1
)))
22118 (if (= d0 d1
) timestr
))))
22119 (setq txt org-agenda-no-heading-message
))
22120 (org-add-props txt props
22121 'org-marker marker
'org-hd-marker hdmarker
22122 'type
"block" 'date date
22123 'priority
(org-get-priority txt
) 'org-category category
)
22126 ;; Sort the entries by expiration date.
22129 ;;; Agenda presentation and sorting
22131 (defconst org-plain-time-of-day-regexp
22133 "\\(\\<[012]?[0-9]"
22134 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
22136 "\\(\\<[012]?[0-9]"
22137 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
22139 "Regular expression to match a plain time or time range.
22140 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
22141 groups carry important information:
22143 1 the first time, range or not
22144 8 the second time, if it is a range.")
22146 (defconst org-plain-time-extension-regexp
22148 "\\(\\<[012]?[0-9]"
22149 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
22150 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
22151 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
22152 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
22153 groups carry important information:
22155 7 hours of duration
22156 9 minutes of duration")
22158 (defconst org-stamp-time-of-day-regexp
22160 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
22161 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
22163 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
22164 "Regular expression to match a timestamp time or time range.
22165 After a match, the following groups carry important information:
22167 1 date plus weekday, for backreferencing to make sure both times on same day
22168 2 the first time, range or not
22169 4 the second time, if it is a range.")
22171 (defvar org-prefix-has-time nil
22172 "A flag, set by `org-compile-prefix-format'.
22173 The flag is set if the currently compiled format contains a `%t'.")
22174 (defvar org-prefix-has-tag nil
22175 "A flag, set by `org-compile-prefix-format'.
22176 The flag is set if the currently compiled format contains a `%T'.")
22178 (defun org-format-agenda-item (extra txt
&optional category tags dotime
22179 noprefix remove-re
)
22180 "Format TXT to be inserted into the agenda buffer.
22181 In particular, it adds the prefix and corresponding text properties. EXTRA
22182 must be a string and replaces the `%s' specifier in the prefix format.
22183 CATEGORY (string, symbol or nil) may be used to overrule the default
22184 category taken from local variable or file name. It will replace the `%c'
22185 specifier in the format. DOTIME, when non-nil, indicates that a
22186 time-of-day should be extracted from TXT for sorting of this entry, and for
22187 the `%t' specifier in the format. When DOTIME is a string, this string is
22188 searched for a time before TXT is. NOPREFIX is a flag and indicates that
22189 only the correctly processes TXT should be returned - this is used by
22190 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
22191 Any match of REMOVE-RE will be removed from TXT."
22193 ;; Diary entries sometimes have extra whitespace at the beginning
22194 (if (string-match "^ +" txt
) (setq txt
(replace-match "" nil nil txt
)))
22195 (let* ((category (or category
22197 (if buffer-file-name
22198 (file-name-sans-extension
22199 (file-name-nondirectory buffer-file-name
))
22201 (tag (if tags
(nth (1- (length tags
)) tags
) ""))
22202 time
; time and tag are needed for the eval of the prefix format
22203 (ts (if dotime
(concat (if (stringp dotime
) dotime
"") txt
)))
22204 (time-of-day (and dotime
(org-get-time-of-day ts
)))
22205 stamp plain s0 s1 s2 rtn srp
)
22206 (when (and dotime time-of-day org-prefix-has-time
)
22207 ;; Extract starting and ending time and move them to prefix
22208 (when (or (setq stamp
(string-match org-stamp-time-of-day-regexp ts
))
22209 (setq plain
(string-match org-plain-time-of-day-regexp ts
)))
22210 (setq s0
(match-string 0 ts
)
22211 srp
(and stamp
(match-end 3))
22212 s1
(match-string (if plain
1 2) ts
)
22213 s2
(match-string (if plain
8 (if srp
4 6)) ts
))
22215 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
22216 ;; them, we might want to remove them there to avoid duplication.
22217 ;; The user can turn this off with a variable.
22218 (if (and org-agenda-remove-times-when-in-prefix
(or stamp plain
)
22219 (string-match (concat (regexp-quote s0
) " *") txt
)
22220 (not (equal ?\
] (string-to-char (substring txt
(match-end 0)))))
22221 (if (eq org-agenda-remove-times-when-in-prefix
'beg
)
22222 (= (match-beginning 0) 0)
22224 (setq txt
(replace-match "" nil nil txt
))))
22225 ;; Normalize the time(s) to 24 hour
22226 (if s1
(setq s1
(org-get-time-of-day s1
'string t
)))
22227 (if s2
(setq s2
(org-get-time-of-day s2
'string t
))))
22229 (when (and s1
(not s2
) org-agenda-default-appointment-duration
22230 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1
))
22231 (let ((m (+ (string-to-number (match-string 2 s1
))
22232 (* 60 (string-to-number (match-string 1 s1
)))
22233 org-agenda-default-appointment-duration
))
22235 (setq h
(/ m
60) m
(- m
(* h
60)))
22236 (setq s2
(format "%02d:%02d" h m
))))
22238 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
22240 ;; Tags are in the string
22241 (if (or (eq org-agenda-remove-tags t
)
22242 (and org-agenda-remove-tags
22243 org-prefix-has-tag
))
22244 (setq txt
(replace-match "" t t txt
))
22245 (setq txt
(replace-match
22246 (concat (make-string (max (- 50 (length txt
)) 1) ?\
)
22247 (match-string 2 txt
))
22251 (while (string-match remove-re txt
)
22252 (setq txt
(replace-match "" t t txt
))))
22254 ;; Create the final string
22257 ;; Prepare the variables needed in the eval of the compiled format
22258 (setq time
(cond (s2 (concat s1
"-" s2
))
22259 (s1 (concat s1
"......"))
22261 extra
(or extra
"")
22262 category
(if (symbolp category
) (symbol-name category
) category
))
22263 ;; Evaluate the compiled format
22264 (setq rtn
(concat (eval org-prefix-format-compiled
) txt
)))
22266 ;; And finally add the text properties
22267 (org-add-props rtn nil
22268 'org-category
(downcase category
) 'tags tags
22269 'org-highest-priority org-highest-priority
22270 'org-lowest-priority org-lowest-priority
22271 'prefix-length
(- (length rtn
) (length txt
))
22272 'time-of-day time-of-day
22278 (defvar org-agenda-sorting-strategy
) ;; because the def is in a let form
22279 (defvar org-agenda-sorting-strategy-selected nil
)
22281 (defun org-agenda-add-time-grid-maybe (list ndays todayp
)
22283 (cond ((not org-agenda-use-time-grid
) (throw 'exit list
))
22284 ((and todayp
(member 'today
(car org-agenda-time-grid
))))
22285 ((and (= ndays
1) (member 'daily
(car org-agenda-time-grid
))))
22286 ((member 'weekly
(car org-agenda-time-grid
)))
22287 (t (throw 'exit list
)))
22288 (let* ((have (delq nil
(mapcar
22289 (lambda (x) (get-text-property 1 'time-of-day x
))
22291 (string (nth 1 org-agenda-time-grid
))
22292 (gridtimes (nth 2 org-agenda-time-grid
))
22293 (req (car org-agenda-time-grid
))
22294 (remove (member 'remove-match req
))
22296 (if (and (member 'require-timed req
) (not have
))
22297 ;; don't show empty grid
22298 (throw 'exit list
))
22299 (while (setq time
(pop gridtimes
))
22300 (unless (and remove
(member time have
))
22301 (setq time
(int-to-string time
))
22302 (push (org-format-agenda-item
22304 (concat (substring time
0 -
2) ":" (substring time -
2)))
22307 1 (length (car new
)) 'face
'org-time-grid
(car new
))))
22308 (if (member 'time-up org-agenda-sorting-strategy-selected
)
22310 (append list new
)))))
22312 (defun org-compile-prefix-format (key)
22313 "Compile the prefix format into a Lisp form that can be evaluated.
22314 The resulting form is returned and stored in the variable
22315 `org-prefix-format-compiled'."
22316 (setq org-prefix-has-time nil org-prefix-has-tag nil
)
22318 ((stringp org-agenda-prefix-format
)
22319 org-agenda-prefix-format
)
22320 ((assq key org-agenda-prefix-format
)
22321 (cdr (assq key org-agenda-prefix-format
)))
22322 (t " %-12:c%?-12t% s")))
22324 varform vars var e c f opt
)
22325 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
22327 (setq var
(cdr (assoc (match-string 4 s
)
22328 '(("c" . category
) ("t" . time
) ("s" . extra
)
22330 c
(or (match-string 3 s
) "")
22331 opt
(match-beginning 1)
22332 start
(1+ (match-beginning 0)))
22333 (if (equal var
'time
) (setq org-prefix-has-time t
))
22334 (if (equal var
'tag
) (setq org-prefix-has-tag t
))
22335 (setq f
(concat "%" (match-string 2 s
) "s"))
22338 `(if (equal "" ,var
)
22340 (format ,f
(if (equal "" ,var
) "" (concat ,var
,c
)))))
22341 (setq varform
`(format ,f
(if (equal ,var
"") "" (concat ,var
,c
)))))
22342 (setq s
(replace-match "%s" t nil s
))
22343 (push varform vars
))
22344 (setq vars
(nreverse vars
))
22345 (setq org-prefix-format-compiled
`(format ,s
,@vars
))))
22347 (defun org-set-sorting-strategy (key)
22348 (if (symbolp (car org-agenda-sorting-strategy
))
22350 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy
)
22351 (setq org-agenda-sorting-strategy-selected
22352 (or (cdr (assq key org-agenda-sorting-strategy
))
22353 (cdr (assq 'agenda org-agenda-sorting-strategy
))
22354 '(time-up category-keep priority-down
)))))
22356 (defun org-get-time-of-day (s &optional string mod24
)
22357 "Check string S for a time of day.
22358 If found, return it as a military time number between 0 and 2400.
22359 If not found, return nil.
22360 The optional STRING argument forces conversion into a 5 character wide string
22364 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s
)
22365 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s
))
22366 (let* ((h (string-to-number (match-string 1 s
)))
22367 (m (if (match-end 3) (string-to-number (match-string 3 s
)) 0))
22368 (ampm (if (match-end 4) (downcase (match-string 4 s
))))
22369 (am-p (equal ampm
"am"))
22370 (h1 (cond ((not ampm
) h
)
22371 ((= h
12) (if am-p
0 12))
22372 (t (+ h
(if am-p
0 12)))))
22373 (h2 (if (and string mod24
(not (and (= m
0) (= h1
24))))
22375 (t0 (+ (* 100 h2
) m
))
22376 (t1 (concat (if (>= h1
24) "+" " ")
22377 (if (< t0
100) "0" "")
22378 (if (< t0
10) "0" "")
22379 (int-to-string t0
))))
22380 (if string
(concat (substring t1 -
4 -
2) ":" (substring t1 -
2)) t0
)))))
22382 (defun org-finalize-agenda-entries (list &optional nosort
)
22383 "Sort and concatenate the agenda items."
22384 (setq list
(mapcar 'org-agenda-highlight-todo list
))
22387 (mapconcat 'identity
(sort list
'org-entries-lessp
) "\n")))
22389 (defun org-agenda-highlight-todo (x)
22393 (beginning-of-line 1)
22394 (setq re
(get-text-property (point) 'org-todo-regexp
))
22395 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length
) 0)))
22396 (when (looking-at (concat "[ \t]*\\.*" re
" +"))
22397 (add-text-properties (match-beginning 0) (match-end 0)
22398 (list 'face
(org-get-todo-face 0)))
22399 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
22400 (delete-region (match-beginning 1) (1- (match-end 0)))
22401 (goto-char (match-beginning 1))
22402 (insert (format org-agenda-todo-keyword-format s
)))))
22403 (setq re
(concat (get-text-property 0 'org-todo-regexp x
))
22404 pl
(get-text-property 0 'prefix-length x
))
22406 (equal (string-match (concat "\\(\\.*\\)" re
"\\( +\\)")
22408 (add-text-properties
22409 (or (match-end 1) (match-end 0)) (match-end 0)
22410 (list 'face
(org-get-todo-face (match-string 2 x
)))
22412 (setq x
(concat (substring x
0 (match-end 1))
22413 (format org-agenda-todo-keyword-format
22414 (match-string 2 x
))
22416 (substring x
(match-end 3)))))
22419 (defsubst org-cmp-priority
(a b
)
22420 "Compare the priorities of string A and B."
22421 (let ((pa (or (get-text-property 1 'priority a
) 0))
22422 (pb (or (get-text-property 1 'priority b
) 0)))
22423 (cond ((> pa pb
) +1)
22427 (defsubst org-cmp-category
(a b
)
22428 "Compare the string values of categories of strings A and B."
22429 (let ((ca (or (get-text-property 1 'org-category a
) ""))
22430 (cb (or (get-text-property 1 'org-category b
) "")))
22431 (cond ((string-lessp ca cb
) -
1)
22432 ((string-lessp cb ca
) +1)
22435 (defsubst org-cmp-tag
(a b
)
22436 "Compare the string values of categories of strings A and B."
22437 (let ((ta (car (last (get-text-property 1 'tags a
))))
22438 (tb (car (last (get-text-property 1 'tags b
)))))
22439 (cond ((not ta
) +1)
22441 ((string-lessp ta tb
) -
1)
22442 ((string-lessp tb ta
) +1)
22445 (defsubst org-cmp-time
(a b
)
22446 "Compare the time-of-day values of strings A and B."
22447 (let* ((def (if org-sort-agenda-notime-is-late
9901 -
1))
22448 (ta (or (get-text-property 1 'time-of-day a
) def
))
22449 (tb (or (get-text-property 1 'time-of-day b
) def
)))
22450 (cond ((< ta tb
) -
1)
22454 (defun org-entries-lessp (a b
)
22455 "Predicate for sorting agenda entries."
22456 ;; The following variables will be used when the form is evaluated.
22457 ;; So even though the compiler complains, keep them.
22458 (let* ((time-up (org-cmp-time a b
))
22459 (time-down (if time-up
(- time-up
) nil
))
22460 (priority-up (org-cmp-priority a b
))
22461 (priority-down (if priority-up
(- priority-up
) nil
))
22462 (category-up (org-cmp-category a b
))
22463 (category-down (if category-up
(- category-up
) nil
))
22464 (category-keep (if category-up
+1 nil
))
22465 (tag-up (org-cmp-tag a b
))
22466 (tag-down (if tag-up
(- tag-up
) nil
)))
22468 (eval (cons 'or org-agenda-sorting-strategy-selected
))
22469 '((-1 . t
) (1 . nil
) (nil . nil
))))))
22471 ;;; Agenda restriction lock
22473 (defvar org-agenda-restriction-lock-overlay
(org-make-overlay 1 1)
22474 "Overlay to mark the headline to which arenda commands are restricted.")
22475 (org-overlay-put org-agenda-restriction-lock-overlay
22476 'face
'org-agenda-restriction-lock
)
22477 (org-overlay-put org-agenda-restriction-lock-overlay
22478 'help-echo
"Agendas are currently limited to this subtree.")
22479 (org-detach-overlay org-agenda-restriction-lock-overlay
)
22480 (defvar org-speedbar-restriction-lock-overlay
(org-make-overlay 1 1)
22481 "Overlay marking the agenda restriction line in speedbar.")
22482 (org-overlay-put org-speedbar-restriction-lock-overlay
22483 'face
'org-agenda-restriction-lock
)
22484 (org-overlay-put org-speedbar-restriction-lock-overlay
22485 'help-echo
"Agendas are currently limited to this item.")
22486 (org-detach-overlay org-speedbar-restriction-lock-overlay
)
22488 (defun org-agenda-set-restriction-lock (&optional type
)
22489 "Set restriction lock for agenda, to current subtree or file.
22490 Restriction will be the file if TYPE is `file', or if type is the
22491 universal prefix '(4), or if the cursor is before the first headline
22492 in the file. Otherwise, restriction will be to the current subtree."
22494 (and (equal type
'(4)) (setq type
'file
))
22497 ((org-at-heading-p) 'subtree
)
22498 ((condition-case nil
(org-back-to-heading t
) (error nil
))
22501 (if (eq type
'subtree
)
22503 (setq org-agenda-restrict t
)
22504 (setq org-agenda-overriding-restriction
'subtree
)
22505 (put 'org-agenda-files
'org-restrict
22506 (list (buffer-file-name (buffer-base-buffer))))
22507 (org-back-to-heading t
)
22508 (org-move-overlay org-agenda-restriction-lock-overlay
(point) (point-at-eol))
22509 (move-marker org-agenda-restrict-begin
(point))
22510 (move-marker org-agenda-restrict-end
22511 (save-excursion (org-end-of-subtree t
)))
22512 (message "Locking agenda restriction to subtree"))
22513 (put 'org-agenda-files
'org-restrict
22514 (list (buffer-file-name (buffer-base-buffer))))
22515 (setq org-agenda-restrict nil
)
22516 (setq org-agenda-overriding-restriction
'file
)
22517 (move-marker org-agenda-restrict-begin nil
)
22518 (move-marker org-agenda-restrict-end nil
)
22519 (message "Locking agenda restriction to file"))
22520 (setq current-prefix-arg nil
)
22521 (org-agenda-maybe-redo))
22523 (defun org-agenda-remove-restriction-lock (&optional noupdate
)
22524 "Remove the agenda restriction lock."
22526 (org-detach-overlay org-agenda-restriction-lock-overlay
)
22527 (org-detach-overlay org-speedbar-restriction-lock-overlay
)
22528 (setq org-agenda-overriding-restriction nil
)
22529 (setq org-agenda-restrict nil
)
22530 (put 'org-agenda-files
'org-restrict nil
)
22531 (move-marker org-agenda-restrict-begin nil
)
22532 (move-marker org-agenda-restrict-end nil
)
22533 (setq current-prefix-arg nil
)
22534 (message "Agenda restriction lock removed")
22535 (or noupdate
(org-agenda-maybe-redo)))
22537 (defun org-agenda-maybe-redo ()
22538 "If there is any window showing the agenda view, update it."
22539 (let ((w (get-buffer-window org-agenda-buffer-name t
))
22540 (w0 (selected-window)))
22545 (if org-agenda-overriding-restriction
22546 (message "Agenda view shifted to new %s restriction"
22547 org-agenda-overriding-restriction
)
22548 (message "Agenda restriction lock removed")))))
22550 ;;; Agenda commands
22552 (defun org-agenda-check-type (error &rest types
)
22553 "Check if agenda buffer is of allowed type.
22554 If ERROR is non-nil, throw an error, otherwise just return nil."
22555 (if (memq org-agenda-type types
)
22558 (error "Not allowed in %s-type agenda buffers" org-agenda-type
)
22561 (defun org-agenda-quit ()
22562 "Exit agenda by removing the window or the buffer."
22564 (let ((buf (current-buffer)))
22565 (if (not (one-window-p)) (delete-window))
22567 (org-agenda-reset-markers)
22568 (org-columns-remove-overlays))
22569 ;; Maybe restore the pre-agenda window configuration.
22570 (and org-agenda-restore-windows-after-quit
22571 (not (eq org-agenda-window-setup
'other-frame
))
22572 org-pre-agenda-window-conf
22573 (set-window-configuration org-pre-agenda-window-conf
)))
22575 (defun org-agenda-exit ()
22576 "Exit agenda by removing the window or the buffer.
22577 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
22578 Org-mode buffers visited directly by the user will not be touched."
22580 (org-release-buffers org-agenda-new-buffers
)
22581 (setq org-agenda-new-buffers nil
)
22584 (defun org-agenda-execute (arg)
22585 "Execute another agenda command, keeping same window.\\<global-map>
22586 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
22588 (let ((org-agenda-window-setup 'current-window
))
22591 (defun org-save-all-org-buffers ()
22592 "Save all Org-mode buffers without user confirmation."
22594 (message "Saving all Org-mode buffers...")
22595 (save-some-buffers t
'org-mode-p
)
22596 (message "Saving all Org-mode buffers... done"))
22598 (defun org-agenda-redo ()
22600 When this is the global TODO list, a prefix argument will be interpreted."
22602 (let* ((org-agenda-keep-modes t
)
22603 (line (org-current-line))
22604 (window-line (- line
(org-current-line (window-start))))
22605 (lprops (get 'org-agenda-redo-command
'org-lprops
)))
22606 (message "Rebuilding agenda buffer...")
22607 (org-let lprops
'(eval org-agenda-redo-command
))
22608 (setq org-agenda-undo-list nil
22609 org-agenda-pending-undo-list nil
)
22610 (message "Rebuilding agenda buffer...done")
22612 (recenter window-line
)))
22614 (defun org-agenda-manipulate-query-add ()
22615 "Manipulate the query by adding a search term with positive selection.
22616 Positive selection means, the term must be matched for selection of an entry."
22618 (org-agenda-manipulate-query ?\
[))
22619 (defun org-agenda-manipulate-query-subtract ()
22620 "Manipulate the query by adding a search term with negative selection.
22621 Negative selection means, term must not be matched for selection of an entry."
22623 (org-agenda-manipulate-query ?\
]))
22624 (defun org-agenda-manipulate-query-add-re ()
22625 "Manipulate the query by adding a search regexp with positive selection.
22626 Positive selection means, the regexp must match for selection of an entry."
22628 (org-agenda-manipulate-query ?\
{))
22629 (defun org-agenda-manipulate-query-subtract-re ()
22630 "Manipulate the query by adding a search regexp with negative selection.
22631 Negative selection means, regexp must not match for selection of an entry."
22633 (org-agenda-manipulate-query ?\
}))
22634 (defun org-agenda-manipulate-query (char)
22636 ((eq org-agenda-type
'search
)
22638 'org-agenda-query-string
22639 (cdr (assoc char
'((?\
[ .
" +") (?\
] .
" -")
22640 (?\
{ .
" +{}") (?\
} .
" -{}")))))
22641 (setq org-agenda-redo-command
22642 (list 'org-search-view
22643 (+ (length org-agenda-query-string
)
22644 (if (member char
'(?\
{ ?\
})) 0 1))
22645 org-agenda-query-string
))
22646 (set-register org-agenda-query-register org-agenda-query-string
)
22648 (t (error "Canot manipulate query for %s-type agenda buffers"
22649 org-agenda-type
))))
22651 (defun org-add-to-string (var string
)
22652 (set var
(concat (symbol-value var
) string
)))
22654 (defun org-agenda-goto-date (date)
22655 "Jump to DATE in agenda."
22656 (interactive (list (org-read-date)))
22657 (org-agenda-list nil date
))
22659 (defun org-agenda-goto-today ()
22662 (org-agenda-check-type t
'timeline
'agenda
)
22663 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t
)))
22665 (tdpos (goto-char tdpos
))
22666 ((eq org-agenda-type
'agenda
)
22667 (let* ((sd (time-to-days
22668 (time-subtract (current-time)
22669 (list 0 (* 3600 org-extend-today-until
) 0))))
22670 (comp (org-agenda-compute-time-span sd org-agenda-span
))
22671 (org-agenda-overriding-arguments org-agenda-last-arguments
))
22672 (setf (nth 1 org-agenda-overriding-arguments
) (car comp
))
22673 (setf (nth 2 org-agenda-overriding-arguments
) (cdr comp
))
22675 (org-agenda-find-same-or-today-or-agenda)))
22676 (t (error "Cannot find today")))))
22678 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt
)
22680 (or (and cnt
(text-property-any (point-min) (point-max) 'org-day-cnt cnt
))
22681 (text-property-any (point-min) (point-max) 'org-today t
)
22682 (text-property-any (point-min) (point-max) 'org-agenda-type
'agenda
)
22685 (defun org-agenda-later (arg)
22686 "Go forward in time by thee current span.
22687 With prefix ARG, go forward that many times the current span."
22689 (org-agenda-check-type t
'agenda
)
22690 (let* ((span org-agenda-span
)
22691 (sd org-starting-day
)
22692 (greg (calendar-gregorian-from-absolute sd
))
22693 (cnt (get-text-property (point) 'org-day-cnt
))
22697 (setq sd
(+ arg sd
) nd
1))
22699 (setq sd
(+ (* 7 arg
) sd
) nd
7))
22701 (setq greg2
(list (+ (car greg
) arg
) (nth 1 greg
) (nth 2 greg
))
22702 sd
(calendar-absolute-from-gregorian greg2
))
22703 (setcar greg2
(1+ (car greg2
)))
22704 (setq nd
(- (calendar-absolute-from-gregorian greg2
) sd
)))
22706 (setq greg2
(list (car greg
) (nth 1 greg
) (+ arg
(nth 2 greg
)))
22707 sd
(calendar-absolute-from-gregorian greg2
))
22708 (setcar (nthcdr 2 greg2
) (1+ (nth 2 greg2
)))
22709 (setq nd
(- (calendar-absolute-from-gregorian greg2
) sd
))))
22710 (let ((org-agenda-overriding-arguments
22711 (list (car org-agenda-last-arguments
) sd nd t
)))
22713 (org-agenda-find-same-or-today-or-agenda cnt
))))
22715 (defun org-agenda-earlier (arg)
22716 "Go backward in time by the current span.
22717 With prefix ARG, go backward that many times the current span."
22719 (org-agenda-later (- arg
)))
22721 (defun org-agenda-day-view ()
22722 "Switch to daily view for agenda."
22724 (setq org-agenda-ndays
1)
22725 (org-agenda-change-time-span 'day
))
22726 (defun org-agenda-week-view ()
22727 "Switch to daily view for agenda."
22729 (setq org-agenda-ndays
7)
22730 (org-agenda-change-time-span 'week
))
22731 (defun org-agenda-month-view ()
22732 "Switch to daily view for agenda."
22734 (org-agenda-change-time-span 'month
))
22735 (defun org-agenda-year-view ()
22736 "Switch to daily view for agenda."
22738 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
22739 (org-agenda-change-time-span 'year
)
22742 (defun org-agenda-change-time-span (span)
22743 "Change the agenda view to SPAN.
22744 SPAN may be `day', `week', `month', `year'."
22745 (org-agenda-check-type t
'agenda
)
22746 (if (equal org-agenda-span span
)
22747 (error "Viewing span is already \"%s\"" span
))
22748 (let* ((sd (or (get-text-property (point) 'day
)
22750 (computed (org-agenda-compute-time-span sd span
))
22751 (org-agenda-overriding-arguments
22752 (list (car org-agenda-last-arguments
)
22753 (car computed
) (cdr computed
) t
)))
22755 (org-agenda-find-same-or-today-or-agenda))
22756 (org-agenda-set-mode-name)
22757 (message "Switched to %s view" span
))
22759 (defun org-agenda-compute-time-span (sd span
)
22760 "Compute starting date and number of days for agenda.
22761 SPAN may be `day', `week', `month', `year'. The return value
22762 is a cons cell with the starting date and the number of days,
22763 so that the date SD will be in that range."
22764 (let* ((greg (calendar-gregorian-from-absolute sd
))
22770 (let* ((nt (calendar-day-of-week
22771 (calendar-gregorian-from-absolute sd
)))
22772 (d (if org-agenda-start-on-weekday
22773 (- nt org-agenda-start-on-weekday
)
22775 (setq sd
(- sd
(+ (if (< d
0) 7 0) d
)))
22778 (setq sd
(calendar-absolute-from-gregorian
22779 (list (car greg
) 1 (nth 2 greg
)))
22780 nd
(- (calendar-absolute-from-gregorian
22781 (list (1+ (car greg
)) 1 (nth 2 greg
)))
22784 (setq sd
(calendar-absolute-from-gregorian
22785 (list 1 1 (nth 2 greg
)))
22786 nd
(- (calendar-absolute-from-gregorian
22787 (list 1 1 (1+ (nth 2 greg
))))
22791 ;; FIXME: does not work if user makes date format that starts with a blank
22792 (defun org-agenda-next-date-line (&optional arg
)
22793 "Jump to the next line indicating a date in agenda buffer."
22795 (org-agenda-check-type t
'agenda
'timeline
)
22796 (beginning-of-line 1)
22797 (if (looking-at "^\\S-") (forward-char 1))
22798 (if (not (re-search-forward "^\\S-" nil t arg
))
22801 (error "No next date after this line in this buffer")))
22802 (goto-char (match-beginning 0)))
22804 (defun org-agenda-previous-date-line (&optional arg
)
22805 "Jump to the previous line indicating a date in agenda buffer."
22807 (org-agenda-check-type t
'agenda
'timeline
)
22808 (beginning-of-line 1)
22809 (if (not (re-search-backward "^\\S-" nil t arg
))
22810 (error "No previous date before this line in this buffer")))
22812 ;; Initialize the highlight
22813 (defvar org-hl
(org-make-overlay 1 1))
22814 (org-overlay-put org-hl
'face
'highlight
)
22816 (defun org-highlight (begin end
&optional buffer
)
22817 "Highlight a region with overlay."
22818 (funcall (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
)
22819 org-hl begin end
(or buffer
(current-buffer))))
22821 (defun org-unhighlight ()
22822 "Detach overlay INDEX."
22823 (funcall (if (featurep 'xemacs
) 'detach-extent
'delete-overlay
) org-hl
))
22825 ;; FIXME this is currently not used.
22826 (defun org-highlight-until-next-command (beg end
&optional buffer
)
22827 (org-highlight beg end buffer
)
22828 (add-hook 'pre-command-hook
'org-unhighlight-once
))
22829 (defun org-unhighlight-once ()
22830 (remove-hook 'pre-command-hook
'org-unhighlight-once
)
22833 (defun org-agenda-follow-mode ()
22834 "Toggle follow mode in an agenda buffer."
22836 (setq org-agenda-follow-mode
(not org-agenda-follow-mode
))
22837 (org-agenda-set-mode-name)
22838 (message "Follow mode is %s"
22839 (if org-agenda-follow-mode
"on" "off")))
22841 (defun org-agenda-log-mode ()
22842 "Toggle log mode in an agenda buffer."
22844 (org-agenda-check-type t
'agenda
'timeline
)
22845 (setq org-agenda-show-log
(not org-agenda-show-log
))
22846 (org-agenda-set-mode-name)
22848 (message "Log mode is %s"
22849 (if org-agenda-show-log
"on" "off")))
22851 (defun org-agenda-toggle-diary ()
22852 "Toggle diary inclusion in an agenda buffer."
22854 (org-agenda-check-type t
'agenda
)
22855 (setq org-agenda-include-diary
(not org-agenda-include-diary
))
22857 (org-agenda-set-mode-name)
22858 (message "Diary inclusion turned %s"
22859 (if org-agenda-include-diary
"on" "off")))
22861 (defun org-agenda-toggle-time-grid ()
22862 "Toggle time grid in an agenda buffer."
22864 (org-agenda-check-type t
'agenda
)
22865 (setq org-agenda-use-time-grid
(not org-agenda-use-time-grid
))
22867 (org-agenda-set-mode-name)
22868 (message "Time-grid turned %s"
22869 (if org-agenda-use-time-grid
"on" "off")))
22871 (defun org-agenda-set-mode-name ()
22872 "Set the mode name to indicate all the small mode settings."
22874 (concat "Org-Agenda"
22875 (if (equal org-agenda-ndays
1) " Day" "")
22876 (if (equal org-agenda-ndays
7) " Week" "")
22877 (if org-agenda-follow-mode
" Follow" "")
22878 (if org-agenda-include-diary
" Diary" "")
22879 (if org-agenda-use-time-grid
" Grid" "")
22880 (if org-agenda-show-log
" Log" "")))
22881 (force-mode-line-update))
22883 (defun org-agenda-post-command-hook ()
22884 (and (eolp) (not (bolp)) (backward-char 1))
22885 (setq org-agenda-type
(get-text-property (point) 'org-agenda-type
))
22886 (if (and org-agenda-follow-mode
22887 (get-text-property (point) 'org-marker
))
22888 (org-agenda-show)))
22890 (defun org-agenda-show-priority ()
22891 "Show the priority of the current item.
22892 This priority is composed of the main priority given with the [#A] cookies,
22893 and by additional input from the age of a schedules or deadline entry."
22895 (let* ((pri (get-text-property (point-at-bol) 'priority
)))
22896 (message "Priority is %d" (if pri pri -
1000))))
22898 (defun org-agenda-show-tags ()
22899 "Show the tags applicable to the current item."
22901 (let* ((tags (get-text-property (point-at-bol) 'tags
)))
22903 (message "Tags are :%s:"
22904 (org-no-properties (mapconcat 'identity tags
":")))
22905 (message "No tags associated with this line"))))
22907 (defun org-agenda-goto (&optional highlight
)
22908 "Go to the Org-mode file which contains the item at point."
22910 (let* ((marker (or (get-text-property (point) 'org-marker
)
22911 (org-agenda-error)))
22912 (buffer (marker-buffer marker
))
22913 (pos (marker-position marker
)))
22914 (switch-to-buffer-other-window buffer
)
22918 (org-show-context 'agenda
)
22920 (and (outline-next-heading)
22921 (org-flag-heading nil
)))) ; show the next heading
22922 (recenter (/ (window-height) 2))
22923 (run-hooks 'org-agenda-after-show-hook
)
22924 (and highlight
(org-highlight (point-at-bol) (point-at-eol)))))
22926 (defvar org-agenda-after-show-hook nil
22927 "Normal hook run after an item has been shown from the agenda.
22928 Point is in the buffer where the item originated.")
22930 (defun org-agenda-kill ()
22931 "Kill the entry or subtree belonging to the current agenda entry."
22933 (or (eq major-mode
'org-agenda-mode
) (error "Not in agenda"))
22934 (let* ((marker (or (get-text-property (point) 'org-marker
)
22935 (org-agenda-error)))
22936 (buffer (marker-buffer marker
))
22937 (pos (marker-position marker
))
22938 (type (get-text-property (point) 'type
))
22939 dbeg dend
(n 0) conf
)
22940 (org-with-remote-undo buffer
22941 (with-current-buffer buffer
22944 (if (and (org-mode-p) (not (member type
'("sexp"))))
22945 (setq dbeg
(progn (org-back-to-heading t
) (point))
22946 dend
(org-end-of-subtree t t
))
22947 (setq dbeg
(point-at-bol)
22948 dend
(min (point-max) (1+ (point-at-eol)))))
22950 (while (re-search-forward "^[ \t]*\\S-" dend t
) (setq n
(1+ n
)))))
22951 (setq conf
(or (eq t org-agenda-confirm-kill
)
22952 (and (numberp org-agenda-confirm-kill
)
22953 (> n org-agenda-confirm-kill
))))
22956 (format "Delete entry with %d lines in buffer \"%s\"? "
22957 n
(buffer-name buffer
))))
22959 (org-remove-subtree-entries-from-agenda buffer dbeg dend
)
22960 (with-current-buffer buffer
(delete-region dbeg dend
))
22961 (message "Agenda item and source killed"))))
22963 (defun org-agenda-archive ()
22964 "Kill the entry or subtree belonging to the current agenda entry."
22966 (or (eq major-mode
'org-agenda-mode
) (error "Not in agenda"))
22967 (let* ((marker (or (get-text-property (point) 'org-marker
)
22968 (org-agenda-error)))
22969 (buffer (marker-buffer marker
))
22970 (pos (marker-position marker
)))
22971 (org-with-remote-undo buffer
22972 (with-current-buffer buffer
22976 (org-remove-subtree-entries-from-agenda)
22977 (org-back-to-heading t
)
22978 (org-archive-subtree))
22979 (error "Archiving works only in Org-mode files"))))))
22981 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end
)
22982 "Remove all lines in the agenda that correspond to a given subtree.
22983 The subtree is the one in buffer BUF, starting at BEG and ending at END.
22984 If this information is not given, the function uses the tree at point."
22985 (let ((buf (or buf
(current-buffer))) m p
)
22987 (unless (and beg end
)
22988 (org-back-to-heading t
)
22990 (org-end-of-subtree t
)
22991 (setq end
(point)))
22992 (set-buffer (get-buffer org-agenda-buffer-name
))
22994 (goto-char (point-max))
22995 (beginning-of-line 1)
22996 (while (not (bobp))
22997 (when (and (setq m
(get-text-property (point) 'org-marker
))
22998 (equal buf
(marker-buffer m
))
22999 (setq p
(marker-position m
))
23002 (let ((inhibit-read-only t
))
23003 (delete-region (point-at-bol) (1+ (point-at-eol)))))
23004 (beginning-of-line 0))))))
23006 (defun org-agenda-open-link ()
23007 "Follow the link in the current line, if any."
23009 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local
)
23012 (narrow-to-region (point-at-bol) (point-at-eol))
23013 (org-open-at-point))))
23015 (defun org-agenda-copy-local-variable (var)
23016 "Get a variable from a referenced buffer and install it here."
23017 (let ((m (get-text-property (point) 'org-marker
)))
23018 (when (and m
(buffer-live-p (marker-buffer m
)))
23019 (org-set-local var
(with-current-buffer (marker-buffer m
)
23020 (symbol-value var
))))))
23022 (defun org-agenda-switch-to (&optional delete-other-windows
)
23023 "Go to the Org-mode file which contains the item at point."
23025 (let* ((marker (or (get-text-property (point) 'org-marker
)
23026 (org-agenda-error)))
23027 (buffer (marker-buffer marker
))
23028 (pos (marker-position marker
)))
23029 (switch-to-buffer buffer
)
23030 (and delete-other-windows
(delete-other-windows))
23034 (org-show-context 'agenda
)
23036 (and (outline-next-heading)
23037 (org-flag-heading nil
)))))) ; show the next heading
23039 (defun org-agenda-goto-mouse (ev)
23040 "Go to the Org-mode file which contains the item at the mouse click."
23042 (mouse-set-point ev
)
23045 (defun org-agenda-show ()
23046 "Display the Org-mode file which contains the item at point."
23048 (let ((win (selected-window)))
23049 (org-agenda-goto t
)
23050 (select-window win
)))
23052 (defun org-agenda-recenter (arg)
23053 "Display the Org-mode file which contains the item at point and recenter."
23055 (let ((win (selected-window)))
23056 (org-agenda-goto t
)
23058 (select-window win
)))
23060 (defun org-agenda-show-mouse (ev)
23061 "Display the Org-mode file which contains the item at the mouse click."
23063 (mouse-set-point ev
)
23066 (defun org-agenda-check-no-diary ()
23067 "Check if the entry is a diary link and abort if yes."
23068 (if (get-text-property (point) 'org-agenda-diary-link
)
23069 (org-agenda-error)))
23071 (defun org-agenda-error ()
23072 (error "Command not allowed in this line"))
23074 (defun org-agenda-tree-to-indirect-buffer ()
23075 "Show the subtree corresponding to the current entry in an indirect buffer.
23076 This calls the command `org-tree-to-indirect-buffer' from the original
23078 With numerical prefix arg ARG, go up to this level and then take that tree.
23079 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
23082 (org-agenda-check-no-diary)
23083 (let* ((marker (or (get-text-property (point) 'org-marker
)
23084 (org-agenda-error)))
23085 (buffer (marker-buffer marker
))
23086 (pos (marker-position marker
)))
23087 (with-current-buffer buffer
23090 (call-interactively 'org-tree-to-indirect-buffer
)))))
23092 (defvar org-last-heading-marker
(make-marker)
23093 "Marker pointing to the headline that last changed its TODO state
23094 by a remote command from the agenda.")
23096 (defun org-agenda-todo-nextset ()
23097 "Switch TODO entry to next sequence."
23099 (org-agenda-todo 'nextset
))
23101 (defun org-agenda-todo-previousset ()
23102 "Switch TODO entry to previous sequence."
23104 (org-agenda-todo 'previousset
))
23106 (defun org-agenda-todo (&optional arg
)
23107 "Cycle TODO state of line at point, also in Org-mode file.
23108 This changes the line at point, all other lines in the agenda referring to
23109 the same tree node, and the headline of the tree node in the Org-mode file."
23111 (org-agenda-check-no-diary)
23112 (let* ((col (current-column))
23113 (marker (or (get-text-property (point) 'org-marker
)
23114 (org-agenda-error)))
23115 (buffer (marker-buffer marker
))
23116 (pos (marker-position marker
))
23117 (hdmarker (get-text-property (point) 'org-hd-marker
))
23118 (inhibit-read-only t
)
23120 (org-with-remote-undo buffer
23121 (with-current-buffer buffer
23124 (org-show-context 'agenda
)
23126 (and (outline-next-heading)
23127 (org-flag-heading nil
))) ; show the next heading
23129 (and (bolp) (forward-char 1))
23130 (setq newhead
(org-get-heading))
23132 (org-back-to-heading)
23133 (move-marker org-last-heading-marker
(point))))
23134 (beginning-of-line 1)
23136 (org-agenda-change-all-lines newhead hdmarker
'fixface
))
23137 (move-to-column col
))))
23139 (defun org-agenda-change-all-lines (newhead hdmarker
&optional fixface
)
23140 "Change all lines in the agenda buffer which match HDMARKER.
23141 The new content of the line will be NEWHEAD (as modified by
23142 `org-format-agenda-item'). HDMARKER is checked with
23143 `equal' against all `org-hd-marker' text properties in the file.
23144 If FIXFACE is non-nil, the face of each item is modified acording to
23145 the new TODO state."
23146 (let* ((inhibit-read-only t
)
23147 props m pl undone-face done-face finish new dotime cat tags
)
23149 (goto-char (point-max))
23150 (beginning-of-line 1)
23151 (while (not finish
)
23152 (setq finish
(bobp))
23153 (when (and (setq m
(get-text-property (point) 'org-hd-marker
))
23154 (equal m hdmarker
))
23155 (setq props
(text-properties-at (point))
23156 dotime
(get-text-property (point) 'dotime
)
23157 cat
(get-text-property (point) 'org-category
)
23158 tags
(get-text-property (point) 'tags
)
23159 new
(org-format-agenda-item "x" newhead cat tags dotime
'noprefix
)
23160 pl
(get-text-property (point) 'prefix-length
)
23161 undone-face
(get-text-property (point) 'undone-face
)
23162 done-face
(get-text-property (point) 'done-face
))
23163 (move-to-column pl
)
23166 (beginning-of-line 1)
23167 (and (looking-at ".*\n?") (replace-match "")))
23169 (replace-match new t t
)
23170 (beginning-of-line 1)
23171 (add-text-properties (point-at-bol) (point-at-eol) props
)
23173 (add-text-properties
23174 (point-at-bol) (point-at-eol)
23176 (if org-last-todo-state-is-todo
23177 undone-face done-face
))))
23178 (org-agenda-highlight-todo 'line
)
23179 (beginning-of-line 1))
23180 (t (error "Line update did not work"))))
23181 (beginning-of-line 0)))
23182 (org-finalize-agenda)))
23184 (defun org-agenda-align-tags (&optional line
)
23185 "Align all tags in agenda items to `org-agenda-tags-column'."
23186 (let ((inhibit-read-only t
) l c
)
23188 (goto-char (if line
(point-at-bol) (point-min)))
23189 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
23190 (if line
(point-at-eol) nil
) t
)
23191 (add-text-properties
23192 (match-beginning 2) (match-end 2)
23193 (list 'face
(delq nil
(list 'org-tag
(get-text-property
23194 (match-beginning 2) 'face
)))))
23195 (setq l
(- (match-end 2) (match-beginning 2))
23196 c
(if (< org-agenda-tags-column
0)
23197 (- (abs org-agenda-tags-column
) l
)
23198 org-agenda-tags-column
))
23199 (delete-region (match-beginning 1) (match-end 1))
23200 (goto-char (match-beginning 1))
23201 (insert (org-add-props
23202 (make-string (max 1 (- c
(current-column))) ?\
)
23203 (text-properties-at (point))))))))
23205 (defun org-agenda-priority-up ()
23206 "Increase the priority of line at point, also in Org-mode file."
23208 (org-agenda-priority 'up
))
23210 (defun org-agenda-priority-down ()
23211 "Decrease the priority of line at point, also in Org-mode file."
23213 (org-agenda-priority 'down
))
23215 (defun org-agenda-priority (&optional force-direction
)
23216 "Set the priority of line at point, also in Org-mode file.
23217 This changes the line at point, all other lines in the agenda referring to
23218 the same tree node, and the headline of the tree node in the Org-mode file."
23220 (org-agenda-check-no-diary)
23221 (let* ((marker (or (get-text-property (point) 'org-marker
)
23222 (org-agenda-error)))
23223 (hdmarker (get-text-property (point) 'org-hd-marker
))
23224 (buffer (marker-buffer hdmarker
))
23225 (pos (marker-position hdmarker
))
23226 (inhibit-read-only t
)
23228 (org-with-remote-undo buffer
23229 (with-current-buffer buffer
23232 (org-show-context 'agenda
)
23234 (and (outline-next-heading)
23235 (org-flag-heading nil
))) ; show the next heading
23236 (funcall 'org-priority force-direction
)
23238 (setq newhead
(org-get-heading)))
23239 (org-agenda-change-all-lines newhead hdmarker
)
23240 (beginning-of-line 1))))
23242 (defun org-get-tags-at (&optional pos
)
23243 "Get a list of all headline tags applicable at POS.
23244 POS defaults to point. If tags are inherited, the list contains
23245 the targets in the same sequence as the headlines appear, i.e.
23246 the tags of the current headline come last."
23248 (let (tags lastpos
)
23252 (goto-char (or pos
(point)))
23254 (org-back-to-heading t
)
23255 (condition-case nil
23256 (while (not (equal lastpos
(point)))
23257 (setq lastpos
(point))
23258 (if (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
23259 (setq tags
(append (org-split-string
23260 (org-match-string-no-properties 1) ":")
23262 (or org-use-tag-inheritance
(error ""))
23263 (org-up-heading-all 1))
23267 ;; FIXME: should fix the tags property of the agenda line.
23268 (defun org-agenda-set-tags ()
23269 "Set tags for the current headline."
23271 (org-agenda-check-no-diary)
23272 (if (and (org-region-active-p) (interactive-p))
23273 (call-interactively 'org-change-tag-in-region
)
23274 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
23275 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
23276 (org-agenda-error)))
23277 (buffer (marker-buffer hdmarker
))
23278 (pos (marker-position hdmarker
))
23279 (inhibit-read-only t
)
23281 (org-with-remote-undo buffer
23282 (with-current-buffer buffer
23286 (org-show-context 'agenda
))
23288 (and (outline-next-heading)
23289 (org-flag-heading nil
))) ; show the next heading
23291 (call-interactively 'org-set-tags
)
23293 (setq newhead
(org-get-heading)))
23294 (org-agenda-change-all-lines newhead hdmarker
)
23295 (beginning-of-line 1)))))
23297 (defun org-agenda-toggle-archive-tag ()
23298 "Toggle the archive tag for the current entry."
23300 (org-agenda-check-no-diary)
23301 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
23302 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker
)
23303 (org-agenda-error)))
23304 (buffer (marker-buffer hdmarker
))
23305 (pos (marker-position hdmarker
))
23306 (inhibit-read-only t
)
23308 (org-with-remote-undo buffer
23309 (with-current-buffer buffer
23312 (org-show-context 'agenda
)
23314 (and (outline-next-heading)
23315 (org-flag-heading nil
))) ; show the next heading
23316 (call-interactively 'org-toggle-archive-tag
)
23318 (setq newhead
(org-get-heading)))
23319 (org-agenda-change-all-lines newhead hdmarker
)
23320 (beginning-of-line 1))))
23322 (defun org-agenda-date-later (arg &optional what
)
23323 "Change the date of this item to one day later."
23325 (org-agenda-check-type t
'agenda
'timeline
)
23326 (org-agenda-check-no-diary)
23327 (let* ((marker (or (get-text-property (point) 'org-marker
)
23328 (org-agenda-error)))
23329 (buffer (marker-buffer marker
))
23330 (pos (marker-position marker
)))
23331 (org-with-remote-undo buffer
23332 (with-current-buffer buffer
23335 (if (not (org-at-timestamp-p))
23336 (error "Cannot find time stamp"))
23337 (org-timestamp-change arg
(or what
'day
)))
23338 (org-agenda-show-new-time marker org-last-changed-timestamp
))
23339 (message "Time stamp changed to %s" org-last-changed-timestamp
)))
23341 (defun org-agenda-date-earlier (arg &optional what
)
23342 "Change the date of this item to one day earlier."
23344 (org-agenda-date-later (- arg
) what
))
23346 (defun org-agenda-show-new-time (marker stamp
&optional prefix
)
23347 "Show new date stamp via text properties."
23348 ;; We use text properties to make this undoable
23349 (let ((inhibit-read-only t
))
23350 (setq stamp
(concat " " prefix
" => " stamp
))
23352 (goto-char (point-max))
23353 (while (not (bobp))
23354 (when (equal marker
(get-text-property (point) 'org-marker
))
23355 (move-to-column (- (window-width) (length stamp
)) t
)
23356 (if (featurep 'xemacs
)
23357 ;; Use `duplicable' property to trigger undo recording
23358 (let ((ex (make-extent nil nil
))
23359 (gl (make-glyph stamp
)))
23360 (set-glyph-face gl
'secondary-selection
)
23361 (set-extent-properties
23362 ex
(list 'invisible t
'end-glyph gl
'duplicable t
))
23363 (insert-extent ex
(1- (point)) (point-at-eol)))
23364 (add-text-properties
23365 (1- (point)) (point-at-eol)
23366 (list 'display
(org-add-props stamp nil
23367 'face
'secondary-selection
))))
23368 (beginning-of-line 1))
23369 (beginning-of-line 0)))))
23371 (defun org-agenda-date-prompt (arg)
23372 "Change the date of this item. Date is prompted for, with default today.
23373 The prefix ARG is passed to the `org-time-stamp' command and can therefore
23374 be used to request time specification in the time stamp."
23376 (org-agenda-check-type t
'agenda
'timeline
)
23377 (org-agenda-check-no-diary)
23378 (let* ((marker (or (get-text-property (point) 'org-marker
)
23379 (org-agenda-error)))
23380 (buffer (marker-buffer marker
))
23381 (pos (marker-position marker
)))
23382 (org-with-remote-undo buffer
23383 (with-current-buffer buffer
23386 (if (not (org-at-timestamp-p))
23387 (error "Cannot find time stamp"))
23388 (org-time-stamp arg
)
23389 (message "Time stamp changed to %s" org-last-changed-timestamp
)))))
23391 (defun org-agenda-schedule (arg)
23392 "Schedule the item at point."
23394 (org-agenda-check-type t
'agenda
'timeline
'todo
'tags
)
23395 (org-agenda-check-no-diary)
23396 (let* ((marker (or (get-text-property (point) 'org-marker
)
23397 (org-agenda-error)))
23398 (type (marker-insertion-type marker
))
23399 (buffer (marker-buffer marker
))
23400 (pos (marker-position marker
))
23401 (org-insert-labeled-timestamps-at-point nil
)
23403 (when type
(message "%s" type
) (sit-for 3))
23404 (set-marker-insertion-type marker t
)
23405 (org-with-remote-undo buffer
23406 (with-current-buffer buffer
23409 (setq ts
(org-schedule arg
)))
23410 (org-agenda-show-new-time marker ts
"S"))
23411 (message "Item scheduled for %s" ts
)))
23413 (defun org-agenda-deadline (arg)
23414 "Schedule the item at point."
23416 (org-agenda-check-type t
'agenda
'timeline
'todo
'tags
)
23417 (org-agenda-check-no-diary)
23418 (let* ((marker (or (get-text-property (point) 'org-marker
)
23419 (org-agenda-error)))
23420 (buffer (marker-buffer marker
))
23421 (pos (marker-position marker
))
23422 (org-insert-labeled-timestamps-at-point nil
)
23424 (org-with-remote-undo buffer
23425 (with-current-buffer buffer
23428 (setq ts
(org-deadline arg
)))
23429 (org-agenda-show-new-time marker ts
"S"))
23430 (message "Deadline for this item set to %s" ts
)))
23432 (defun org-get-heading (&optional no-tags
)
23433 "Return the heading of the current entry, without the stars."
23435 (org-back-to-heading t
)
23438 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
23439 "\\*+[ \t]+\\([^\r\n]*\\)"))
23440 (match-string 1) "")))
23442 (defun org-agenda-clock-in (&optional arg
)
23443 "Start the clock on the currently selected item."
23445 (org-agenda-check-no-diary)
23446 (let* ((marker (or (get-text-property (point) 'org-marker
)
23447 (org-agenda-error)))
23448 (pos (marker-position marker
)))
23449 (org-with-remote-undo (marker-buffer marker
)
23450 (with-current-buffer (marker-buffer marker
)
23455 (defun org-agenda-clock-out (&optional arg
)
23456 "Stop the currently running clock."
23458 (unless (marker-buffer org-clock-marker
)
23459 (error "No running clock"))
23460 (org-with-remote-undo (marker-buffer org-clock-marker
)
23463 (defun org-agenda-clock-cancel (&optional arg
)
23464 "Cancel the currently running clock."
23466 (unless (marker-buffer org-clock-marker
)
23467 (error "No running clock"))
23468 (org-with-remote-undo (marker-buffer org-clock-marker
)
23469 (org-clock-cancel)))
23471 (defun org-agenda-diary-entry ()
23472 "Make a diary entry, like the `i' command from the calendar.
23473 All the standard commands work: block, weekly etc."
23475 (org-agenda-check-type t
'agenda
'timeline
)
23476 (require 'diary-lib
)
23477 (let* ((char (progn
23478 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
23479 (read-char-exclusive)))
23480 (cmd (cdr (assoc char
23481 '((?d . insert-diary-entry
)
23482 (?w . insert-weekly-diary-entry
)
23483 (?m . insert-monthly-diary-entry
)
23484 (?y . insert-yearly-diary-entry
)
23485 (?a . insert-anniversary-diary-entry
)
23486 (?b . insert-block-diary-entry
)
23487 (?c . insert-cyclic-diary-entry
)))))
23488 (oldf (symbol-function 'calendar-cursor-to-date
))
23489 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
23491 (mark (or (mark t
) (point))))
23493 (error "No command associated with <%c>" char
))
23494 (unless (and (get-text-property point
'day
)
23495 (or (not (equal ?b char
))
23496 (get-text-property mark
'day
)))
23497 (error "Don't know which date to use for diary entry"))
23498 ;; We implement this by hacking the `calendar-cursor-to-date' function
23499 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
23500 (let ((calendar-mark-ring
23501 (list (calendar-gregorian-from-absolute
23502 (or (get-text-property mark
'day
)
23503 (get-text-property point
'day
))))))
23506 (fset 'calendar-cursor-to-date
23507 (lambda (&optional error
)
23508 (calendar-gregorian-from-absolute
23509 (get-text-property point
'day
))))
23510 (call-interactively cmd
))
23511 (fset 'calendar-cursor-to-date oldf
)))))
23514 (defun org-agenda-execute-calendar-command (cmd)
23515 "Execute a calendar command from the agenda, with the date associated to
23516 the cursor position."
23517 (org-agenda-check-type t
'agenda
'timeline
)
23518 (require 'diary-lib
)
23519 (unless (get-text-property (point) 'day
)
23520 (error "Don't know which date to use for calendar command"))
23521 (let* ((oldf (symbol-function 'calendar-cursor-to-date
))
23523 (date (calendar-gregorian-from-absolute
23524 (get-text-property point
'day
)))
23525 ;; the following 3 vars are needed in the calendar
23526 (displayed-day (extract-calendar-day date
))
23527 (displayed-month (extract-calendar-month date
))
23528 (displayed-year (extract-calendar-year date
)))
23531 (fset 'calendar-cursor-to-date
23532 (lambda (&optional error
)
23533 (calendar-gregorian-from-absolute
23534 (get-text-property point
'day
))))
23535 (call-interactively cmd
))
23536 (fset 'calendar-cursor-to-date oldf
))))
23538 (defun org-agenda-phases-of-moon ()
23539 "Display the phases of the moon for the 3 months around the cursor date."
23541 (org-agenda-execute-calendar-command 'calendar-phases-of-moon
))
23543 (defun org-agenda-holidays ()
23544 "Display the holidays for the 3 months around the cursor date."
23546 (org-agenda-execute-calendar-command 'list-calendar-holidays
))
23548 (defun org-agenda-sunrise-sunset (arg)
23549 "Display sunrise and sunset for the cursor date.
23550 Latitude and longitude can be specified with the variables
23551 `calendar-latitude' and `calendar-longitude'. When called with prefix
23552 argument, latitude and longitude will be prompted for."
23554 (let ((calendar-longitude (if arg nil calendar-longitude
))
23555 (calendar-latitude (if arg nil calendar-latitude
))
23556 (calendar-location-name
23557 (if arg
"the given coordinates" calendar-location-name
)))
23558 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset
)))
23560 (defun org-agenda-goto-calendar ()
23561 "Open the Emacs calendar with the date at the cursor."
23563 (org-agenda-check-type t
'agenda
'timeline
)
23564 (let* ((day (or (get-text-property (point) 'day
)
23565 (error "Don't know which date to open in calendar")))
23566 (date (calendar-gregorian-from-absolute day
))
23567 (calendar-move-hook nil
)
23568 (view-calendar-holidays-initially nil
)
23569 (view-diary-entries-initially nil
))
23571 (calendar-goto-date date
)))
23573 (defun org-calendar-goto-agenda ()
23574 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
23575 This is a command that has to be installed in `calendar-mode-map'."
23577 (org-agenda-list nil
(calendar-absolute-from-gregorian
23578 (calendar-cursor-to-date))
23581 (defun org-agenda-convert-date ()
23583 (org-agenda-check-type t
'agenda
'timeline
)
23584 (let ((day (get-text-property (point) 'day
))
23587 (error "Don't know which date to convert"))
23588 (setq date
(calendar-gregorian-from-absolute day
))
23590 "Gregorian: " (calendar-date-string date
) "\n"
23591 "ISO: " (calendar-iso-date-string date
) "\n"
23592 "Day of Yr: " (calendar-day-of-year-string date
) "\n"
23593 "Julian: " (calendar-julian-date-string date
) "\n"
23594 "Astron. JD: " (calendar-astro-date-string date
)
23595 " (Julian date number at noon UTC)\n"
23596 "Hebrew: " (calendar-hebrew-date-string date
) " (until sunset)\n"
23597 "Islamic: " (calendar-islamic-date-string date
) " (until sunset)\n"
23598 "French: " (calendar-french-date-string date
) "\n"
23599 "Baha'i: " (calendar-bahai-date-string date
) " (until sunset)\n"
23600 "Mayan: " (calendar-mayan-date-string date
) "\n"
23601 "Coptic: " (calendar-coptic-date-string date
) "\n"
23602 "Ethiopic: " (calendar-ethiopic-date-string date
) "\n"
23603 "Persian: " (calendar-persian-date-string date
) "\n"
23604 "Chinese: " (calendar-chinese-date-string date
) "\n"))
23605 (with-output-to-temp-buffer "*Dates*"
23607 (if (fboundp 'fit-window-to-buffer
)
23608 (fit-window-to-buffer (get-buffer-window "*Dates*")))))
23611 ;;;; Embedded LaTeX
23613 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
23614 "Keymap for the minor `org-cdlatex-mode'.")
23616 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
23617 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
23618 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
23619 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
23620 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
23622 (defvar org-cdlatex-texmathp-advice-is-done nil
23623 "Flag remembering if we have applied the advice to texmathp already.")
23625 (define-minor-mode org-cdlatex-mode
23626 "Toggle the minor `org-cdlatex-mode'.
23627 This mode supports entering LaTeX environment and math in LaTeX fragments
23629 \\{org-cdlatex-mode-map}"
23631 (when org-cdlatex-mode
(require 'cdlatex
))
23632 (unless org-cdlatex-texmathp-advice-is-done
23633 (setq org-cdlatex-texmathp-advice-is-done t
)
23634 (defadvice texmathp
(around org-math-always-on activate
)
23635 "Always return t in org-mode buffers.
23636 This is because we want to insert math symbols without dollars even outside
23637 the LaTeX math segments. If Orgmode thinks that point is actually inside
23638 en embedded LaTeX fragement, let texmathp do its job.
23639 \\[org-cdlatex-mode-map]"
23643 ((not (org-mode-p)) ad-do-it
)
23644 ((eq this-command
'cdlatex-math-symbol
)
23645 (setq ad-return-value t
23646 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
23648 (let ((p (org-inside-LaTeX-fragment-p)))
23649 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
23650 (setq ad-return-value t
23651 texmathp-why
'("Org-mode embedded math" .
0))
23652 (if p ad-do-it
)))))))))
23654 (defun turn-on-org-cdlatex ()
23655 "Unconditionally turn on `org-cdlatex-mode'."
23656 (org-cdlatex-mode 1))
23658 (defun org-inside-LaTeX-fragment-p ()
23659 "Test if point is inside a LaTeX fragment.
23660 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
23661 sequence appearing also before point.
23662 Even though the matchers for math are configurable, this function assumes
23663 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
23664 delimiters are skipped when they have been removed by customization.
23665 The return value is nil, or a cons cell with the delimiter and
23666 and the position of this delimiter.
23668 This function does a reasonably good job, but can locally be fooled by
23669 for example currency specifications. For example it will assume being in
23670 inline math after \"$22.34\". The LaTeX fragment formatter will only format
23671 fragments that are properly closed, but during editing, we have to live
23672 with the uncertainty caused by missing closing delimiters. This function
23673 looks only before point, not after."
23675 (let ((pos (point))
23676 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
23678 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
23680 dd-on str
(start 0) m re
)
23683 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
23684 re
(nth 1 (assoc "$" org-latex-regexps
)))
23685 (while (string-match re str start
)
23687 ((= (match-end 0) (length str
))
23688 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
23689 ((= (match-end 0) (- (length str
) 5))
23691 (t (setq start
(match-end 0))))))
23692 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
23694 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
23695 (and (match-beginning 2) (throw 'exit nil
))
23697 (while (re-search-backward "\\$\\$" lim t
)
23698 (setq dd-on
(not dd-on
)))
23700 (if dd-on
(cons "$$" m
))))))
23703 (defun org-try-cdlatex-tab ()
23704 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
23705 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
23706 - inside a LaTeX fragment, or
23707 - after the first word in a line, where an abbreviation expansion could
23708 insert a LaTeX environment."
23709 (when org-cdlatex-mode
23712 (skip-chars-backward "a-zA-Z0-9*")
23713 (skip-chars-backward " \t")
23716 ((org-inside-LaTeX-fragment-p)
23720 (defun org-cdlatex-underscore-caret (&optional arg
)
23721 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
23722 Revert to the normal definition outside of these fragments."
23724 (if (org-inside-LaTeX-fragment-p)
23725 (call-interactively 'cdlatex-sub-superscript
)
23726 (let (org-cdlatex-mode)
23727 (call-interactively (key-binding (vector last-input-event
))))))
23729 (defun org-cdlatex-math-modify (&optional arg
)
23730 "Execute `cdlatex-math-modify' in LaTeX fragments.
23731 Revert to the normal definition outside of these fragments."
23733 (if (org-inside-LaTeX-fragment-p)
23734 (call-interactively 'cdlatex-math-modify
)
23735 (let (org-cdlatex-mode)
23736 (call-interactively (key-binding (vector last-input-event
))))))
23738 (defvar org-latex-fragment-image-overlays nil
23739 "List of overlays carrying the images of latex fragments.")
23740 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
23742 (defun org-remove-latex-fragment-image-overlays ()
23743 "Remove all overlays with LaTeX fragment images in current buffer."
23744 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
23745 (setq org-latex-fragment-image-overlays nil
))
23747 (defun org-preview-latex-fragment (&optional subtree
)
23748 "Preview the LaTeX fragment at point, or all locally or globally.
23749 If the cursor is in a LaTeX fragment, create the image and overlay
23750 it over the source code. If there is no fragment at point, display
23751 all fragments in the current text, from one headline to the next. With
23752 prefix SUBTREE, display all fragments in the current subtree. With a
23753 double prefix `C-u C-u', or when the cursor is before the first headline,
23754 display all fragments in the buffer.
23755 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
23757 (org-remove-latex-fragment-image-overlays)
23760 (let (beg end at msg
)
23762 ((or (equal subtree
'(16))
23763 (not (save-excursion
23764 (re-search-backward (concat "^" outline-regexp
) nil t
))))
23765 (setq beg
(point-min) end
(point-max)
23766 msg
"Creating images for buffer...%s"))
23767 ((equal subtree
'(4))
23768 (org-back-to-heading)
23769 (setq beg
(point) end
(org-end-of-subtree t
)
23770 msg
"Creating images for subtree...%s"))
23772 (if (setq at
(org-inside-LaTeX-fragment-p))
23773 (goto-char (max (point-min) (- (cdr at
) 2)))
23774 (org-back-to-heading))
23775 (setq beg
(point) end
(progn (outline-next-heading) (point))
23776 msg
(if at
"Creating image...%s"
23777 "Creating images for entry...%s"))))
23779 (narrow-to-region beg end
)
23782 (concat "ltxpng/" (file-name-sans-extension
23783 (file-name-nondirectory
23784 buffer-file-name
)))
23785 default-directory
'overlays msg at
'forbuffer
)
23786 (message msg
"done. Use `C-c C-c' to remove images.")))))
23788 (defvar org-latex-regexps
23789 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
23790 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
23791 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
23792 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil
)
23793 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
23794 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t
)
23795 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t
))
23796 "Regular expressions for matching embedded LaTeX.")
23798 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
23799 "Replace LaTeX fragments with links to an image, and produce images."
23800 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
23801 (let* ((prefixnodir (file-name-nondirectory prefix
))
23802 (absprefix (expand-file-name prefix dir
))
23803 (todir (file-name-directory absprefix
))
23804 (opt org-format-latex-options
)
23805 (matchers (plist-get opt
:matchers
))
23806 (re-list org-latex-regexps
)
23807 (cnt 0) txt link beg end re e checkdir
23808 m n block linkfile movefile ov
)
23809 ;; Check if there are old images files with this prefix, and remove them
23810 (when (file-directory-p todir
)
23814 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
23815 ;; Check the different regular expressions
23816 (while (setq e
(pop re-list
))
23817 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
23818 block
(if (nth 3 e
) "\n\n" ""))
23819 (when (member m matchers
)
23820 (goto-char (point-min))
23821 (while (re-search-forward re nil t
)
23822 (when (or (not at
) (equal (cdr at
) (match-beginning n
)))
23823 (setq txt
(match-string n
)
23824 beg
(match-beginning n
) end
(match-end n
)
23826 linkfile
(format "%s_%04d.png" prefix cnt
)
23827 movefile
(format "%s_%04d.png" absprefix cnt
)
23828 link
(concat block
"[[file:" linkfile
"]]" block
))
23829 (if msg
(message msg cnt
))
23831 (unless checkdir
; make sure the directory exists
23833 (or (file-directory-p todir
) (make-directory todir
)))
23834 (org-create-formula-image
23835 txt movefile opt forbuffer
)
23838 (setq ov
(org-make-overlay beg end
))
23839 (if (featurep 'xemacs
)
23841 (org-overlay-put ov
'invisible t
)
23844 (make-glyph (vector 'png
:file movefile
))))
23847 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
23848 (push ov org-latex-fragment-image-overlays
)
23850 (delete-region beg end
)
23851 (insert link
))))))))
23853 ;; This function borrows from Ganesh Swami's latex2png.el
23854 (defun org-create-formula-image (string tofile options buffer
)
23855 (let* ((tmpdir (if (featurep 'xemacs
)
23857 temporary-file-directory
))
23858 (texfilebase (make-temp-name
23859 (expand-file-name "orgtex" tmpdir
)))
23860 (texfile (concat texfilebase
".tex"))
23861 (dvifile (concat texfilebase
".dvi"))
23862 (pngfile (concat texfilebase
".png"))
23863 (fnh (face-attribute 'default
:height nil
))
23864 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
23865 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
23866 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
23868 (bg (or (plist-get options
(if buffer
:background
:html-background
))
23870 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
23871 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
23872 (with-temp-file texfile
23873 (insert org-format-latex-header
23874 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
23875 (let ((dir default-directory
))
23876 (condition-case nil
23879 (call-process "latex" nil nil nil texfile
))
23882 (if (not (file-exists-p dvifile
))
23883 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
23884 (call-process "dvipng" nil nil nil
23885 "-E" "-fg" fg
"-bg" bg
23887 ;;"-x" scale "-y" scale
23891 (if (not (file-exists-p pngfile
))
23892 (progn (message "Failed to create png file from %s" texfile
) nil
)
23893 ;; Use the requested file name and clean up
23894 (copy-file pngfile tofile
'replace
)
23895 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
23896 (delete-file (concat texfilebase e
)))
23899 (defun org-dvipng-color (attr)
23900 "Return an rgb color specification for dvipng."
23901 (apply 'format
"rgb %s %s %s"
23902 (mapcar 'org-normalize-color
23903 (color-values (face-attribute 'default attr nil
)))))
23905 (defun org-normalize-color (value)
23906 "Return string to be used as color value for an RGB component."
23907 (format "%g" (/ value
65535.0)))
23911 ;;; Variables, constants, and parameter plists
23913 (defconst org-level-max
20)
23915 (defvar org-export-html-preamble nil
23916 "Preamble, to be inserted just after <body>. Set by publishing functions.")
23917 (defvar org-export-html-postamble nil
23918 "Preamble, to be inserted just before </body>. Set by publishing functions.")
23919 (defvar org-export-html-auto-preamble t
23920 "Should default preamble be inserted? Set by publishing functions.")
23921 (defvar org-export-html-auto-postamble t
23922 "Should default postamble be inserted? Set by publishing functions.")
23923 (defvar org-current-export-file nil
) ; dynamically scoped parameter
23924 (defvar org-current-export-dir nil
) ; dynamically scoped parameter
23927 (defconst org-export-plist-vars
23928 '((:language . org-export-default-language
)
23929 (:customtime . org-display-custom-times
)
23930 (:headline-levels . org-export-headline-levels
)
23931 (:section-numbers . org-export-with-section-numbers
)
23932 (:table-of-contents . org-export-with-toc
)
23933 (:preserve-breaks . org-export-preserve-breaks
)
23934 (:archived-trees . org-export-with-archived-trees
)
23935 (:emphasize . org-export-with-emphasize
)
23936 (:sub-superscript . org-export-with-sub-superscripts
)
23937 (:special-strings . org-export-with-special-strings
)
23938 (:footnotes . org-export-with-footnotes
)
23939 (:drawers . org-export-with-drawers
)
23940 (:tags . org-export-with-tags
)
23941 (:TeX-macros . org-export-with-TeX-macros
)
23942 (:LaTeX-fragments . org-export-with-LaTeX-fragments
)
23943 (:skip-before-1st-heading . org-export-skip-text-before-1st-heading
)
23944 (:fixed-width . org-export-with-fixed-width
)
23945 (:timestamps . org-export-with-timestamps
)
23946 (:author-info . org-export-author-info
)
23947 (:time-stamp-file . org-export-time-stamp-file
)
23948 (:tables . org-export-with-tables
)
23949 (:table-auto-headline . org-export-highlight-first-table-line
)
23950 (:style . org-export-html-style
)
23951 (:agenda-style . org-agenda-export-html-style
)
23952 (:convert-org-links . org-export-html-link-org-files-as-html
)
23953 (:inline-images . org-export-html-inline-images
)
23954 (:html-extension . org-export-html-extension
)
23955 (:html-table-tag . org-export-html-table-tag
)
23956 (:expand-quoted-html . org-export-html-expand
)
23957 (:timestamp . org-export-html-with-timestamp
)
23958 (:publishing-directory . org-export-publishing-directory
)
23959 (:preamble . org-export-html-preamble
)
23960 (:postamble . org-export-html-postamble
)
23961 (:auto-preamble . org-export-html-auto-preamble
)
23962 (:auto-postamble . org-export-html-auto-postamble
)
23963 (:author . user-full-name
)
23964 (:email . user-mail-address
)))
23966 (defun org-default-export-plist ()
23967 "Return the property list with default settings for the export variables."
23968 (let ((l org-export-plist-vars
) rtn e
)
23969 (while (setq e
(pop l
))
23970 (setq rtn
(cons (car e
) (cons (symbol-value (cdr e
)) rtn
))))
23973 (defun org-infile-export-plist ()
23974 "Return the property list with file-local settings for export."
23979 (let ((re (org-make-options-regexp
23980 '("TITLE" "AUTHOR" "DATE" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
23981 p key val text options
)
23982 (while (re-search-forward re nil t
)
23983 (setq key
(org-match-string-no-properties 1)
23984 val
(org-match-string-no-properties 2))
23986 ((string-equal key
"TITLE") (setq p
(plist-put p
:title val
)))
23987 ((string-equal key
"AUTHOR")(setq p
(plist-put p
:author val
)))
23988 ((string-equal key
"EMAIL") (setq p
(plist-put p
:email val
)))
23989 ((string-equal key
"DATE") (setq p
(plist-put p
:date val
)))
23990 ((string-equal key
"LANGUAGE") (setq p
(plist-put p
:language val
)))
23991 ((string-equal key
"TEXT")
23992 (setq text
(if text
(concat text
"\n" val
) val
)))
23993 ((string-equal key
"OPTIONS") (setq options val
))))
23994 (setq p
(plist-put p
:text text
))
23996 (let ((op '(("H" .
:headline-levels
)
23997 ("num" .
:section-numbers
)
23998 ("toc" .
:table-of-contents
)
23999 ("\\n" .
:preserve-breaks
)
24000 ("@" .
:expand-quoted-html
)
24001 (":" .
:fixed-width
)
24003 ("^" .
:sub-superscript
)
24004 ("-" .
:special-strings
)
24009 ("TeX" .
:TeX-macros
)
24010 ("LaTeX" .
:LaTeX-fragments
)
24011 ("skip" .
:skip-before-1st-heading
)
24012 ("author" .
:author-info
)
24013 ("timestamp" .
:time-stamp-file
)))
24015 (while (setq o
(pop op
))
24016 (if (string-match (concat (regexp-quote (car o
))
24017 ":\\([^ \t\n\r;,.]*\\)")
24019 (setq p
(plist-put p
(cdr o
)
24020 (car (read-from-string
24021 (match-string 1 options
)))))))))
24024 (defun org-export-directory (type plist
)
24025 (let* ((val (plist-get plist
:publishing-directory
))
24026 (dir (if (listp val
)
24027 (or (cdr (assoc type val
)) ".")
24031 (defun org-skip-comments (lines)
24032 "Skip lines starting with \"#\" and subtrees starting with COMMENT."
24033 (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string
))
24034 (re2 "^\\(\\*+\\)[ \t\n\r]")
24035 (case-fold-search nil
)
24037 (while (setq line
(pop lines
))
24039 ((and (string-match re1 line
)
24040 (setq level
(- (match-end 1) (match-beginning 1))))
24041 ;; Beginning of a COMMENT subtree. Skip it.
24042 (while (and (setq line
(pop lines
))
24043 (or (not (string-match re2 line
))
24044 (> (- (match-end 1) (match-beginning 1)) level
))))
24045 (setq lines
(cons line lines
)))
24046 ((string-match "^#" line
)
24047 ;; an ordinary comment line
24049 ((and org-export-table-remove-special-lines
24050 (string-match "^[ \t]*|" line
)
24051 (or (string-match "^[ \t]*| *[!_^] *|" line
)
24052 (and (string-match "| *<[0-9]+> *|" line
)
24053 (not (string-match "| *[^ <|]" line
)))))
24054 ;; a special table line that should be removed
24056 (t (setq rtn
(cons line rtn
)))))
24059 (defun org-export (&optional arg
)
24061 (let ((help "[t] insert the export option template
24062 \[v] limit export to visible part of outline tree
24064 \[a] export as ASCII
24066 \[h] export as HTML
24067 \[H] export as HTML to temporary buffer
24068 \[R] export region as HTML
24069 \[b] export as HTML and browse immediately
24070 \[x] export as XOXO
24072 \[l] export as LaTeX
24073 \[L] export as LaTeX to temporary buffer
24075 \[i] export current file as iCalendar file
24076 \[I] export all agenda files as iCalendar files
24077 \[c] export agenda files into combined iCalendar file
24079 \[F] publish current file
24080 \[P] publish current project
24081 \[X] publish... (project will be prompted for)
24082 \[A] publish all projects")
24084 '((?t . org-insert-export-options-template
)
24085 (?v . org-export-visible
)
24086 (?a . org-export-as-ascii
)
24087 (?h . org-export-as-html
)
24088 (?b . org-export-as-html-and-open
)
24089 (?H . org-export-as-html-to-buffer
)
24090 (?R . org-export-region-as-html
)
24091 (?x . org-export-as-xoxo
)
24092 (?l . org-export-as-latex
)
24093 (?L . org-export-as-latex-to-buffer
)
24094 (?i . org-export-icalendar-this-file
)
24095 (?I . org-export-icalendar-all-agenda-files
)
24096 (?c . org-export-icalendar-combine-agenda-files
)
24097 (?F . org-publish-current-file
)
24098 (?P . org-publish-current-project
)
24100 (?A . org-publish-all
)))
24102 (save-window-excursion
24103 (delete-other-windows)
24104 (with-output-to-temp-buffer "*Org Export/Publishing Help*"
24106 (message "Select command: ")
24107 (setq r1
(read-char-exclusive)))
24108 (setq r2
(if (< r1
27) (+ r1
96) r1
))
24109 (if (setq ass
(assq r2 cmds
))
24110 (call-interactively (cdr ass
))
24111 (error "No command associated with key %c" r1
))))
24113 (defconst org-html-entities
24121 ("vert" .
"|")
24154 ("Aring") ("AA".
"Å")
24243 ("varepsilon".
"ε")
24256 ("sigmaf") ("varsigma".
"ς")
24264 ("thetasym") ("vartheta".
"ϑ")
24267 ("bull") ("bullet".
"•")
24268 ("hellip") ("dots".
"…")
24278 ("larr") ("leftarrow".
"←") ("gets".
"←")
24279 ("uarr") ("uparrow".
"↑")
24280 ("rarr") ("to".
"→") ("rightarrow".
"→")
24281 ("darr")("downarrow".
"↓")
24282 ("harr") ("leftrightarrow".
"↔")
24283 ("crarr") ("hookleftarrow".
"↵") ; has round hook, not quite CR
24284 ("lArr") ("Leftarrow".
"⇐")
24285 ("uArr") ("Uparrow".
"⇑")
24286 ("rArr") ("Rightarrow".
"⇒")
24287 ("dArr") ("Downarrow".
"⇓")
24288 ("hArr") ("Leftrightarrow".
"⇔")
24290 ("part") ("partial".
"∂")
24291 ("exist") ("exists".
"∃")
24292 ("empty") ("emptyset".
"∅")
24294 ("isin") ("in".
"∈")
24300 ("lowast") ("ast".
"∗")
24302 ("prop") ("proptp".
"∝")
24303 ("infin") ("infty".
"∞")
24304 ("ang") ("angle".
"∠")
24305 ("and") ("wedge".
"∧")
24306 ("or") ("vee".
"∨")
24312 ("cong") ("simeq".
"≅")
24313 ("asymp")("approx".
"≈")
24314 ("ne") ("neq".
"≠")
24318 ("sub") ("subset".
"⊂")
24319 ("sup") ("supset".
"⊃")
24326 ("sdot") ("cdot".
"⋅")
24333 ("loz") ("Diamond".
"◊")
24334 ("spades") ("spadesuit".
"♠")
24335 ("clubs") ("clubsuit".
"♣")
24336 ("hearts") ("diamondsuit".
"♥")
24337 ("diams") ("diamondsuit".
"♦")
24338 ("smile".
"☺") ("blacksmile".
"☻") ("sad".
"☹")
24372 ("arccos".
"arccos")
24373 ("arcsin".
"arcsin")
24374 ("arctan".
"arctan")
24391 ("liminf".
"liminf")
24392 ("limsup".
"limsup")
24405 "Entities for TeX->HTML translation.
24406 Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
24407 \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
24408 In that case, \"\\ent\" will be translated to \"&other;\".
24409 The list contains HTML entities for Latin-1, Greek and other symbols.
24410 It is supplemented by a number of commonly used TeX macros with appropriate
24411 translations. There is currently no way for users to extend this.")
24413 ;;; General functions for all backends
24415 (defun org-cleaned-string-for-export (string &rest parameters
)
24416 "Cleanup a buffer STRING so that links can be created safely."
24418 (let* ((re-radio (and org-target-link-regexp
24419 (concat "\\([^<]\\)\\(" org-target-link-regexp
"\\)")))
24420 (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re
))
24421 (re-angle-link (concat "\\([^[]\\)" org-angle-link-re
))
24422 (re-archive (concat ":" org-archive-tag
":"))
24423 (re-quote (concat "^\\*+[ \t]+" org-quote-string
"\\>"))
24424 (re-commented (concat "^\\*+[ \t]+" org-comment-string
"\\>"))
24425 (htmlp (plist-get parameters
:for-html
))
24426 (asciip (plist-get parameters
:for-ascii
))
24427 (latexp (plist-get parameters
:for-LaTeX
))
24428 (commentsp (plist-get parameters
:comments
))
24429 (archived-trees (plist-get parameters
:archived-trees
))
24430 (inhibit-read-only t
)
24431 (drawers org-drawers
)
24432 (exp-drawers (plist-get parameters
:drawers
))
24433 (outline-regexp "\\*+ ")
24436 (with-current-buffer (get-buffer-create " org-mode-tmp")
24439 ;; Remove license-to-kill stuff
24440 (while (setq p
(text-property-any (point-min) (point-max)
24441 :org-license-to-kill t
))
24442 (delete-region p
(next-single-property-change p
:org-license-to-kill
)))
24444 (let ((org-inhibit-startup t
)) (org-mode))
24445 (untabify (point-min) (point-max))
24447 ;; Get rid of drawers
24448 (unless (eq t exp-drawers
)
24449 (goto-char (point-min))
24450 (let ((re (concat "^[ \t]*:\\("
24453 (org-delete-all exp-drawers
24454 (copy-sequence drawers
))
24456 "\\):[ \t]*\n\\([^@]*?\n\\)?[ \t]*:END:[ \t]*\n")))
24457 (while (re-search-forward re nil t
)
24458 (replace-match ""))))
24460 ;; Get the correct stuff before the first headline
24461 (when (plist-get parameters
:skip-before-1st-heading
)
24462 (goto-char (point-min))
24463 (when (re-search-forward "^\\*+[ \t]" nil t
)
24464 (delete-region (point-min) (match-beginning 0))
24465 (goto-char (point-min))
24467 (when (plist-get parameters
:add-text
)
24468 (goto-char (point-min))
24469 (insert (plist-get parameters
:add-text
) "\n"))
24471 ;; Get rid of archived trees
24472 (when (not (eq archived-trees t
))
24473 (goto-char (point-min))
24474 (while (re-search-forward re-archive nil t
)
24475 (if (not (org-on-heading-p t
))
24476 (org-end-of-subtree t
)
24477 (beginning-of-line 1)
24478 (setq a
(if archived-trees
24479 (1+ (point-at-eol)) (point))
24480 b
(org-end-of-subtree t
))
24481 (if (> b a
) (delete-region a b
)))))
24483 ;; Find targets in comments and move them out of comments,
24484 ;; but mark them as targets that should be invisible
24485 (goto-char (point-min))
24486 (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t
)
24487 (replace-match "\\1(INVISIBLE)"))
24489 ;; Protect backend specific stuff, throw away the others.
24491 `((,htmlp
"HTML" "BEGIN_HTML" "END_HTML")
24492 (,asciip
"ASCII" "BEGIN_ASCII" "END_ASCII")
24493 (,latexp
"LaTeX" "BEGIN_LaTeX" "END_LaTeX")))
24495 (goto-char (point-min))
24496 (while (re-search-forward "^#\\+BEGIN_EXAMPLE[ \t]*\n" nil t
)
24497 (goto-char (match-end 0))
24498 (while (not (looking-at "#\\+END_EXAMPLE"))
24500 (beginning-of-line 2)))
24501 (goto-char (point-min))
24502 (while (re-search-forward "^[ \t]*:.*\\(\n[ \t]*:.*\\)*" nil t
)
24503 (add-text-properties (match-beginning 0) (match-end 0)
24504 '(org-protected t
)))
24506 (setq fmt
(pop formatters
))
24508 (goto-char (point-min))
24509 (while (re-search-forward (concat "^#\\+" (cadr fmt
)
24510 ":[ \t]*\\(.*\\)") nil t
)
24511 (replace-match "\\1" t
)
24512 (add-text-properties
24513 (point-at-bol) (min (1+ (point-at-eol)) (point-max))
24514 '(org-protected t
))))
24515 (goto-char (point-min))
24516 (while (re-search-forward
24518 (caddr fmt
) "\\>.*\\(\\(\n.*\\)*?\n\\)#\\+"
24519 (cadddr fmt
) "\\>.*\n?") nil t
)
24521 (add-text-properties (match-beginning 1) (1+ (match-end 1))
24522 '(org-protected t
))
24523 (delete-region (match-beginning 0) (match-end 0))))))
24525 ;; Protect quoted subtrees
24526 (goto-char (point-min))
24527 (while (re-search-forward re-quote nil t
)
24528 (goto-char (match-beginning 0))
24530 (add-text-properties (point) (org-end-of-subtree t
)
24531 '(org-protected t
)))
24533 ;; Protect verbatim elements
24534 (goto-char (point-min))
24535 (while (re-search-forward org-verbatim-re nil t
)
24536 (add-text-properties (match-beginning 4) (match-end 4)
24537 '(org-protected t
))
24538 (goto-char (1+ (match-end 4))))
24540 ;; Remove subtrees that are commented
24541 (goto-char (point-min))
24542 (while (re-search-forward re-commented nil t
)
24543 (goto-char (match-beginning 0))
24544 (delete-region (point) (org-end-of-subtree t
)))
24546 ;; Remove special table lines
24547 (when org-export-table-remove-special-lines
24548 (goto-char (point-min))
24549 (while (re-search-forward "^[ \t]*|" nil t
)
24550 (beginning-of-line 1)
24551 (if (or (looking-at "[ \t]*| *[!_^] *|")
24552 (and (looking-at ".*?| *<[0-9]+> *|")
24553 (not (looking-at ".*?| *[^ <|]"))))
24554 (delete-region (max (point-min) (1- (point-at-bol)))
24558 ;; Specific LaTeX stuff
24560 (require 'org-export-latex nil
)
24561 (org-export-latex-cleaned-string))
24564 (org-export-ascii-clean-string))
24566 ;; Specific HTML stuff
24568 ;; Convert LaTeX fragments to images
24569 (when (plist-get parameters
:LaTeX-fragments
)
24571 (concat "ltxpng/" (file-name-sans-extension
24572 (file-name-nondirectory
24573 org-current-export-file
)))
24574 org-current-export-dir nil
"Creating LaTeX image %s"))
24575 (message "Exporting..."))
24577 ;; Remove or replace comments
24578 (goto-char (point-min))
24579 (while (re-search-forward "^#\\(.*\n?\\)" nil t
)
24581 (progn (add-text-properties
24582 (match-beginning 0) (match-end 0) '(org-protected t
))
24583 (replace-match (format commentsp
(match-string 1)) t t
))
24584 (replace-match "")))
24586 ;; Find matches for radio targets and turn them into internal links
24587 (goto-char (point-min))
24589 (while (re-search-forward re-radio nil t
)
24590 (org-if-unprotected
24591 (replace-match "\\1[[\\2]]"))))
24593 ;; Find all links that contain a newline and put them into a single line
24594 (goto-char (point-min))
24595 (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t
)
24596 (org-if-unprotected
24597 (replace-match "\\1 \\3")
24598 (goto-char (match-beginning 0))))
24601 ;; Normalize links: Convert angle and plain links into bracket links
24602 ;; Expand link abbreviations
24603 (goto-char (point-min))
24604 (while (re-search-forward re-plain-link nil t
)
24605 (goto-char (1- (match-end 0)))
24606 (org-if-unprotected
24607 (let* ((s (concat (match-string 1) "[[" (match-string 2)
24608 ":" (match-string 3) "]]")))
24609 ;; added 'org-link face to links
24610 (put-text-property 0 (length s
) 'face
'org-link s
)
24611 (replace-match s t t
))))
24612 (goto-char (point-min))
24613 (while (re-search-forward re-angle-link nil t
)
24614 (goto-char (1- (match-end 0)))
24615 (org-if-unprotected
24616 (let* ((s (concat (match-string 1) "[[" (match-string 2)
24617 ":" (match-string 3) "]]")))
24618 (put-text-property 0 (length s
) 'face
'org-link s
)
24619 (replace-match s t t
))))
24620 (goto-char (point-min))
24621 (while (re-search-forward org-bracket-link-regexp nil t
)
24622 (org-if-unprotected
24623 (let* ((s (concat "[[" (setq xx
(save-match-data
24624 (org-link-expand-abbrev (match-string 1))))
24628 (concat "[" xx
"]"))
24630 (put-text-property 0 (length s
) 'face
'org-link s
)
24631 (replace-match s t t
))))
24633 ;; Find multiline emphasis and put them into single line
24634 (when (plist-get parameters
:emph-multiline
)
24635 (goto-char (point-min))
24636 (while (re-search-forward org-emph-re nil t
)
24637 (if (not (= (char-after (match-beginning 3))
24638 (char-after (match-beginning 4))))
24639 (org-if-unprotected
24640 (subst-char-in-region (match-beginning 0) (match-end 0)
24642 (goto-char (1- (match-end 0))))
24643 (goto-char (1+ (match-beginning 0))))))
24645 (setq rtn
(buffer-string)))
24646 (kill-buffer " org-mode-tmp")
24649 (defun org-export-grab-title-from-buffer ()
24650 "Get a title for the current document, from looking at the buffer."
24651 (let ((inhibit-read-only t
))
24653 (goto-char (point-min))
24654 (let ((end (save-excursion (outline-next-heading) (point))))
24655 (when (re-search-forward "^[ \t]*[^|# \t\r\n].*\n" end t
)
24656 ;; Mark the line so that it will not be exported as normal text.
24658 (add-text-properties (match-beginning 0) (match-end 0)
24659 (list :org-license-to-kill t
)))
24660 ;; Return the title string
24661 (org-trim (match-string 0)))))))
24663 (defun org-export-get-title-from-subtree ()
24664 "Return subtree title and exclude it from export."
24665 (let (title (m (mark)))
24667 (goto-char (region-beginning))
24668 (when (and (org-at-heading-p)
24669 (>= (org-end-of-subtree t t
) (region-end)))
24670 ;; This is a subtree, we take the title from the first heading
24671 (goto-char (region-beginning))
24672 (looking-at org-todo-line-regexp
)
24673 (setq title
(match-string 3))
24675 (add-text-properties (point) (1+ (point-at-eol))
24676 (list :org-license-to-kill t
)))))
24679 (defun org-solidify-link-text (s &optional alist
)
24680 "Take link text and make a safe target out of it."
24685 (org-split-string s
"[ \t\r\n]+") "--"))
24686 (a (assoc rtn alist
)))
24687 (or (cdr a
) rtn
))))
24689 (defun org-get-min-level (lines)
24690 "Get the minimum level in LINES."
24691 (let ((re "^\\(\\*+\\) ") l min
)
24693 (while (setq l
(pop lines
))
24694 (if (string-match re l
)
24695 (throw 'exit
(org-tr-level (length (match-string 1 l
))))))
24698 ;; Variable holding the vector with section numbers
24699 (defvar org-section-numbers
(make-vector org-level-max
0))
24701 (defun org-init-section-numbers ()
24702 "Initialize the vector for the section numbers."
24704 (numbers (nreverse (org-split-string "" "\\.")))
24705 (depth (1- (length org-section-numbers
)))
24706 (i depth
) number-string
)
24709 (aset org-section-numbers i
0)
24710 (setq number-string
(or (car numbers
) "0"))
24711 (if (string-match "\\`[A-Z]\\'" number-string
)
24712 (aset org-section-numbers i
24713 (- (string-to-char number-string
) ?A -
1))
24714 (aset org-section-numbers i
(string-to-number number-string
)))
24718 (defun org-section-number (&optional level
)
24719 "Return a string with the current section number.
24720 When LEVEL is non-nil, increase section numbers on that level."
24721 (let* ((depth (1- (length org-section-numbers
))) idx n
(string ""))
24724 (aset org-section-numbers
24725 level
(1+ (aref org-section-numbers level
))))
24726 (setq idx
(1+ level
))
24727 (while (<= idx depth
)
24728 (if (not (= idx
1))
24729 (aset org-section-numbers idx
0))
24730 (setq idx
(1+ idx
))))
24732 (while (<= idx depth
)
24733 (setq n
(aref org-section-numbers idx
))
24734 (setq string
(concat string
(if (not (string= string
"")) "." "")
24735 (int-to-string n
)))
24736 (setq idx
(1+ idx
)))
24738 (if (string-match "\\`\\([@0]\\.\\)+" string
)
24739 (setq string
(replace-match "" t nil string
)))
24740 (if (string-match "\\(\\.0\\)+\\'" string
)
24741 (setq string
(replace-match "" t nil string
))))
24746 (defvar org-last-level nil
) ; dynamically scoped variable
24747 (defvar org-min-level nil
) ; dynamically scoped variable
24748 (defvar org-levels-open nil
) ; dynamically scoped parameter
24749 (defvar org-ascii-current-indentation nil
) ; For communication
24751 (defun org-export-as-ascii (arg)
24752 "Export the outline as a pretty ASCII file.
24753 If there is an active region, export only the region.
24754 The prefix ARG specifies how many levels of the outline should become
24755 underlined headlines. The default is 3."
24757 (setq-default org-todo-line-regexp org-todo-line-regexp
)
24758 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
24759 (org-infile-export-plist)))
24760 (region-p (org-region-active-p))
24764 (goto-char (region-beginning))
24765 (and (org-at-heading-p)
24766 (>= (org-end-of-subtree t t
) (region-end))))))
24767 (custom-times org-display-custom-times
)
24768 (org-ascii-current-indentation '(0 .
0))
24772 (case-fold-search nil
)
24773 (filename (concat (file-name-as-directory
24774 (org-export-directory :ascii opt-plist
))
24775 (file-name-sans-extension
24777 (org-entry-get (region-beginning)
24778 "EXPORT_FILE_NAME" t
))
24779 (file-name-nondirectory buffer-file-name
)))
24781 (filename (if (equal (file-truename filename
)
24782 (file-truename buffer-file-name
))
24783 (concat filename
".txt")
24785 (buffer (find-file-noselect filename
))
24786 (org-levels-open (make-vector org-level-max nil
))
24787 (odd org-odd-levels-only
)
24788 (date (plist-get opt-plist
:date
))
24789 (author (plist-get opt-plist
:author
))
24790 (title (or (and subtree-p
(org-export-get-title-from-subtree))
24791 (plist-get opt-plist
:title
)
24793 (plist-get opt-plist
:skip-before-1st-heading
))
24794 (org-export-grab-title-from-buffer))
24795 (file-name-sans-extension
24796 (file-name-nondirectory buffer-file-name
))))
24797 (email (plist-get opt-plist
:email
))
24798 (language (plist-get opt-plist
:language
))
24799 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
24800 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
24805 (if (org-region-active-p) (region-beginning) (point-min))
24806 (if (org-region-active-p) (region-end) (point-max))))
24807 (lines (org-split-string
24808 (org-cleaned-string-for-export
24811 :skip-before-1st-heading
24812 (plist-get opt-plist
:skip-before-1st-heading
)
24813 :drawers
(plist-get opt-plist
:drawers
)
24814 :verbatim-multiline t
24816 (plist-get opt-plist
:archived-trees
)
24817 :add-text
(plist-get opt-plist
:text
))
24819 thetoc have-headings first-heading-pos
24820 table-open table-buffer
)
24822 (let ((inhibit-read-only t
))
24824 (remove-text-properties (point-min) (point-max)
24825 '(:org-license-to-kill t
))))
24827 (setq org-min-level
(org-get-min-level lines
))
24828 (setq org-last-level org-min-level
)
24829 (org-init-section-numbers)
24831 (find-file-noselect filename
)
24833 (setq lang-words
(or (assoc language org-export-language-setup
)
24834 (assoc "en" org-export-language-setup
)))
24835 (switch-to-buffer-other-window buffer
)
24838 ;; create local variables for all options, to make sure all called
24839 ;; functions get the correct information
24841 (set (make-local-variable (cdr x
))
24842 (plist-get opt-plist
(car x
))))
24843 org-export-plist-vars
)
24844 (org-set-local 'org-odd-levels-only odd
)
24845 (setq umax
(if arg
(prefix-numeric-value arg
)
24846 org-export-headline-levels
))
24847 (setq umax-toc
(if (integerp org-export-with-toc
)
24848 (min org-export-with-toc umax
)
24852 (if title
(org-insert-centered title ?
=))
24854 (if (and (or author email
)
24855 org-export-author-info
)
24856 (insert (concat (nth 1 lang-words
) ": " (or author
"")
24857 (if email
(concat " <" email
">") "")
24861 ((and date
(string-match "%" date
))
24862 (setq date
(format-time-string date
(current-time))))
24864 (t (setq date
(format-time-string "%Y/%m/%d %X" (current-time)))))
24866 (if (and date org-export-time-stamp-file
)
24867 (insert (concat (nth 2 lang-words
) ": " date
"\n")))
24871 (if org-export-with-toc
24873 (push (concat (nth 3 lang-words
) "\n") thetoc
)
24874 (push (concat (make-string (length (nth 3 lang-words
)) ?
=) "\n") thetoc
)
24875 (mapc '(lambda (line)
24876 (if (string-match org-todo-line-regexp
24878 ;; This is a headline
24880 (setq have-headings t
)
24881 (setq level
(- (match-end 1) (match-beginning 1))
24882 level
(org-tr-level level
)
24883 txt
(match-string 3 line
)
24885 (or (and org-export-mark-todo-in-toc
24886 (match-beginning 2)
24887 (not (member (match-string 2 line
)
24888 org-done-keywords
)))
24890 (and org-export-mark-todo-in-toc
24892 (org-search-todo-below
24893 line lines level
))))
24894 (setq txt
(org-html-expand-for-ascii txt
))
24896 (while (string-match org-bracket-link-regexp txt
)
24899 (match-string (if (match-end 2) 3 1) txt
)
24902 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
24904 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
24906 (setq txt
(replace-match "" t t txt
)))
24907 (if (string-match quote-re0 txt
)
24908 (setq txt
(replace-match "" t t txt
)))
24910 (if org-export-with-section-numbers
24911 (setq txt
(concat (org-section-number level
)
24913 (if (<= level umax-toc
)
24918 (* (max 0 (- level org-min-level
)) 4) ?\
)
24919 (format (if todo
"%s (*)\n" "%s\n") txt
))
24921 (setq org-last-level level
))
24924 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
24926 (org-init-section-numbers)
24927 (while (setq line
(pop lines
))
24928 ;; Remove the quoted HTML tags.
24929 (setq line
(org-html-expand-for-ascii line
))
24931 (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line
)
24932 (setq line
(replace-match "" t t line
)))
24933 ;; Replace internal links
24934 (while (string-match org-bracket-link-regexp line
)
24935 (setq line
(replace-match
24936 (if (match-end 3) "[\\3]" "[\\1]")
24939 (setq line
(org-translate-time line
)))
24941 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
24943 (setq first-heading-pos
(or first-heading-pos
(point)))
24944 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
24945 txt
(match-string 2 line
))
24946 (org-ascii-level-start level txt umax lines
))
24948 ((and org-export-with-tables
24949 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
24950 (if (not table-open
)
24951 ;; New table starts
24952 (setq table-open t table-buffer nil
))
24953 ;; Accumulate lines
24954 (setq table-buffer
(cons line table-buffer
))
24955 (when (or (not lines
)
24956 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
24958 (setq table-open nil
24959 table-buffer
(nreverse table-buffer
))
24962 (org-fix-indentation x org-ascii-current-indentation
))
24963 (org-format-table-ascii table-buffer
)
24966 (setq line
(org-fix-indentation line org-ascii-current-indentation
))
24967 (if (and org-export-with-fixed-width
24968 (string-match "^\\([ \t]*\\)\\(:\\)" line
))
24969 (setq line
(replace-match "\\1" nil nil line
)))
24970 (insert line
"\n"))))
24974 ;; insert the table of contents
24976 (goto-char (point-min))
24977 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
24979 (goto-char (match-beginning 0))
24980 (replace-match ""))
24981 (goto-char first-heading-pos
))
24982 (mapc 'insert thetoc
)
24983 (or (looking-at "[ \t]*\n[ \t]*\n")
24986 ;; Convert whitespace place holders
24987 (goto-char (point-min))
24989 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
24990 (setq end
(next-single-property-change beg
'org-whitespace
))
24992 (delete-region beg end
)
24993 (insert (make-string (- end beg
) ?\
))))
24996 ;; remove display and invisible chars
24998 (goto-char (point-min))
24999 (while (setq beg
(next-single-property-change (point) 'display
))
25000 (setq end
(next-single-property-change beg
'display
))
25001 (delete-region beg end
)
25004 (goto-char (point-min))
25005 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
25006 (setq end
(next-single-property-change beg
'org-cwidth
))
25007 (delete-region beg end
)
25009 (goto-char (point-min))))
25011 (defun org-export-ascii-clean-string ()
25012 "Do extra work for ASCII export"
25013 (goto-char (point-min))
25014 (while (re-search-forward org-verbatim-re nil t
)
25015 (goto-char (match-end 2))
25016 (backward-delete-char 1) (insert "'")
25017 (goto-char (match-beginning 2))
25018 (delete-char 1) (insert "`")
25019 (goto-char (match-end 2))))
25021 (defun org-search-todo-below (line lines level
)
25022 "Search the subtree below LINE for any TODO entries."
25023 (let ((rest (cdr (memq line lines
)))
25024 (re org-todo-line-regexp
)
25027 (while (setq line
(pop rest
))
25028 (if (string-match re line
)
25030 (setq lv
(- (match-end 1) (match-beginning 1))
25031 todo
(and (match-beginning 2)
25032 (not (member (match-string 2 line
)
25033 org-done-keywords
))))
25035 (if (<= lv level
) (throw 'exit nil
))
25036 (if todo
(throw 'exit t
))))))))
25038 (defun org-html-expand-for-ascii (line)
25039 "Handle quoted HTML for ASCII export."
25040 (if org-export-html-expand
25041 (while (string-match "@<[^<>\n]*>" line
)
25042 ;; We just remove the tags for now.
25043 (setq line
(replace-match "" nil nil line
))))
25046 (defun org-insert-centered (s &optional underline
)
25047 "Insert the string S centered and underline it with character UNDERLINE."
25048 (let ((ind (max (/ (- 80 (string-width s
)) 2) 0)))
25049 (insert (make-string ind ?\
) s
"\n")
25051 (insert (make-string ind ?\
)
25052 (make-string (string-width s
) underline
)
25055 (defun org-ascii-level-start (level title umax
&optional lines
)
25056 "Insert a new level in ASCII export."
25057 (let (char (n (- level umax
1)) (ind 0))
25060 (insert (make-string (* 2 n
) ?\
)
25061 (char-to-string (nth (% n
(length org-export-ascii-bullets
))
25062 org-export-ascii-bullets
))
25064 ;; find the indentation of the next non-empty line
25067 (if (string-match "^\\* " (car lines
)) (throw 'stop nil
))
25068 (if (string-match "^\\([ \t]*\\)\\S-" (car lines
))
25069 (throw 'stop
(setq ind
(org-get-indentation (car lines
)))))
25071 (setq org-ascii-current-indentation
(cons (* 2 (1+ n
)) ind
)))
25072 (if (or (not (equal (char-before) ?
\n))
25073 (not (equal (char-before (1- (point))) ?
\n)))
25075 (setq char
(nth (- umax level
) (reverse org-export-ascii-underline
)))
25076 (unless org-export-with-tags
25077 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
25078 (setq title
(replace-match "" t t title
))))
25079 (if org-export-with-section-numbers
25080 (setq title
(concat (org-section-number level
) " " title
)))
25081 (insert title
"\n" (make-string (string-width title
) char
) "\n")
25082 (setq org-ascii-current-indentation
'(0 .
0)))))
25084 (defun org-export-visible (type arg
)
25085 "Create a copy of the visible part of the current buffer, and export it.
25086 The copy is created in a temporary buffer and removed after use.
25087 TYPE is the final key (as a string) that also select the export command in
25088 the `C-c C-e' export dispatcher.
25089 As a special case, if the you type SPC at the prompt, the temporary
25090 org-mode file will not be removed but presented to you so that you can
25091 continue to use it. The prefix arg ARG is passed through to the exporting
25095 (message "Export visible: [a]SCII [h]tml [b]rowse HTML [H/R]uffer with HTML [x]OXO [ ]keep buffer")
25096 (read-char-exclusive))
25097 current-prefix-arg
))
25098 (if (not (member type
'(?a ?\C-a ?b ?\C-b ?h ?x ?\
)))
25099 (error "Invalid export key"))
25100 (let* ((binding (cdr (assoc type
25101 '((?a . org-export-as-ascii
)
25102 (?\C-a . org-export-as-ascii
)
25103 (?b . org-export-as-html-and-open
)
25104 (?\C-b . org-export-as-html-and-open
)
25105 (?h . org-export-as-html
)
25106 (?H . org-export-as-html-to-buffer
)
25107 (?R . org-export-region-as-html
)
25108 (?x . org-export-as-xoxo
)))))
25109 (keepp (equal type ?\
))
25110 (file buffer-file-name
)
25111 (buffer (get-buffer-create "*Org Export Visible*"))
25113 ;; Need to hack the drawers here.
25115 (goto-char (point-min))
25116 (while (re-search-forward org-drawer-regexp nil t
)
25117 (goto-char (match-beginning 1))
25118 (or (org-invisible-p) (org-flag-drawer nil
))))
25119 (with-current-buffer buffer
(erase-buffer))
25121 (setq s
(goto-char (point-min)))
25122 (while (not (= (point) (point-max)))
25123 (goto-char (org-find-invisible))
25124 (append-to-buffer buffer s
(point))
25125 (setq s
(goto-char (org-find-visible))))
25126 (org-cycle-hide-drawers 'all
)
25127 (goto-char (point-min))
25129 ;; Copy all comment lines to the end, to make sure #+ settings are
25130 ;; still available for the second export step. Kind of a hack, but
25131 ;; does do the trick.
25132 (if (looking-at "#[^\r\n]*")
25133 (append-to-buffer buffer
(match-beginning 0) (1+ (match-end 0))))
25134 (while (re-search-forward "[\n\r]#[^\n\r]*" nil t
)
25135 (append-to-buffer buffer
(1+ (match-beginning 0))
25136 (min (point-max) (1+ (match-end 0))))))
25137 (set-buffer buffer
)
25138 (let ((buffer-file-name file
)
25139 (org-inhibit-startup t
))
25142 (unless keepp
(funcall binding arg
))))
25144 (kill-buffer buffer
)
25145 (switch-to-buffer-other-window buffer
)
25146 (goto-char (point-min)))))
25148 (defun org-find-visible ()
25150 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
25151 (get-char-property s
'invisible
)))
25153 (defun org-find-invisible ()
25155 (while (and (not (= (point-max) (setq s
(next-overlay-change s
))))
25156 (not (get-char-property s
'invisible
))))
25161 (defun org-get-current-options ()
25162 "Return a string with current options as keyword options.
25163 Does include HTML export options as well as TODO and CATEGORY stuff."
25169 #+TEXT: Some descriptive text to be emitted. Several lines OK.
25170 #+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
25174 #+PRIORITIES: %c %c %c
25176 #+STARTUP: %s %s %s %s %s
25181 (buffer-name) (user-full-name) user-mail-address org-export-default-language
25182 org-export-headline-levels
25183 org-export-with-section-numbers
25184 org-export-with-toc
25185 org-export-preserve-breaks
25186 org-export-html-expand
25187 org-export-with-fixed-width
25188 org-export-with-tables
25189 org-export-with-sub-superscripts
25190 org-export-with-special-strings
25191 org-export-with-footnotes
25192 org-export-with-emphasize
25193 org-export-with-TeX-macros
25194 org-export-with-LaTeX-fragments
25195 org-export-skip-text-before-1st-heading
25196 org-export-with-drawers
25197 org-export-with-tags
25198 (file-name-nondirectory buffer-file-name
)
25199 "TODO FEEDBACK VERIFY DONE"
25200 "Me Jason Marie DONE"
25201 org-highest-priority org-lowest-priority org-default-priority
25202 (mapconcat 'identity org-drawers
" ")
25203 (cdr (assoc org-startup-folded
25204 '((nil .
"showall") (t .
"overview") (content .
"content"))))
25205 (if org-odd-levels-only
"odd" "oddeven")
25206 (if org-hide-leading-stars
"hidestars" "showstars")
25207 (if org-startup-align-all-tables
"align" "noalign")
25208 (cond ((eq org-log-done t
) "logdone")
25209 ((equal org-log-done
'note
) "lognotedone")
25210 ((not org-log-done
) "nologdone"))
25211 (or (mapconcat (lambda (x)
25213 ((equal '(:startgroup
) x
) "{")
25214 ((equal '(:endgroup
) x
) "}")
25215 ((cdr x
) (format "%s(%c)" (car x
) (cdr x
)))
25217 (or org-tag-alist
(org-get-buffer-tags)) " ") "")
25218 org-archive-location
25219 "org file:~/org/%s.org"
25222 (defun org-insert-export-options-template ()
25223 "Insert into the buffer a template with information for exporting."
25225 (if (not (bolp)) (newline))
25226 (let ((s (org-get-current-options)))
25227 (and (string-match "#\\+CATEGORY" s
)
25228 (setq s
(substring s
0 (match-beginning 0))))
25231 (defun org-toggle-fixed-width-section (arg)
25232 "Toggle the fixed-width export.
25233 If there is no active region, the QUOTE keyword at the current headline is
25234 inserted or removed. When present, it causes the text between this headline
25235 and the next to be exported as fixed-width text, and unmodified.
25236 If there is an active region, this command adds or removes a colon as the
25237 first character of this line. If the first character of a line is a colon,
25238 this line is also exported in fixed-width font."
25241 (regionp (org-region-active-p))
25242 (beg (if regionp
(region-beginning) (point)))
25243 (end (if regionp
(region-end)))
25244 (nlines (or arg
(if (and beg end
) (count-lines beg end
) 1)))
25245 (case-fold-search nil
)
25246 (re "[ \t]*\\(:\\)")
25251 (setq cc
(current-column))
25252 (beginning-of-line 1)
25253 (setq off
(looking-at re
))
25254 (while (> nlines
0)
25255 (setq nlines
(1- nlines
))
25256 (beginning-of-line 1)
25259 (move-to-column cc t
)
25262 ((and off
(looking-at re
))
25263 (replace-match "" t t nil
1))
25264 ((not off
) (move-to-column cc t
) (insert ":")))
25267 (org-back-to-heading)
25268 (if (looking-at (concat outline-regexp
25269 "\\( *\\<" org-quote-string
"\\>[ \t]*\\)"))
25270 (replace-match "" t t nil
1)
25271 (if (looking-at outline-regexp
)
25273 (goto-char (match-end 0))
25274 (insert org-quote-string
" "))))))))
25276 (defun org-export-as-html-and-open (arg)
25277 "Export the outline as HTML and immediately open it with a browser.
25278 If there is an active region, export only the region.
25279 The prefix ARG specifies how many levels of the outline should become
25280 headlines. The default is 3. Lower levels will become bulleted lists."
25282 (org-export-as-html arg
'hidden
)
25283 (org-open-file buffer-file-name
))
25285 (defun org-export-as-html-batch ()
25286 "Call `org-export-as-html', may be used in batch processing as
25288 --load=$HOME/lib/emacs/org.el
25289 --eval \"(setq org-export-headline-levels 2)\"
25290 --visit=MyFile --funcall org-export-as-html-batch"
25291 (org-export-as-html org-export-headline-levels
'hidden
))
25293 (defun org-export-as-html-to-buffer (arg)
25294 "Call `org-exort-as-html` with output to a temporary buffer.
25295 No file is created. The prefix ARG is passed through to `org-export-as-html'."
25297 (org-export-as-html arg nil nil
"*Org HTML Export*")
25298 (switch-to-buffer-other-window "*Org HTML Export*"))
25300 (defun org-replace-region-by-html (beg end
)
25301 "Assume the current region has org-mode syntax, and convert it to HTML.
25302 This can be used in any buffer. For example, you could write an
25303 itemized list in org-mode syntax in an HTML buffer and then use this
25304 command to convert it."
25306 (let (reg html buf pop-up-frames
)
25307 (save-window-excursion
25309 (setq html
(org-export-region-as-html
25310 beg end t
'string
))
25311 (setq reg
(buffer-substring beg end
)
25312 buf
(get-buffer-create "*Org tmp*"))
25313 (with-current-buffer buf
25317 (setq html
(org-export-region-as-html
25318 (point-min) (point-max) t
'string
)))
25319 (kill-buffer buf
)))
25320 (delete-region beg end
)
25323 (defun org-export-region-as-html (beg end
&optional body-only buffer
)
25324 "Convert region from BEG to END in org-mode buffer to HTML.
25325 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
25326 contents, and only produce the region of converted text, useful for
25327 cut-and-paste operations.
25328 If BUFFER is a buffer or a string, use/create that buffer as a target
25329 of the converted HTML. If BUFFER is the symbol `string', return the
25330 produced HTML as a string and leave not buffer behind. For example,
25331 a Lisp program could call this function in the following way:
25333 (setq html (org-export-region-as-html beg end t 'string))
25335 When called interactively, the output buffer is selected, and shown
25336 in a window. A non-interactive call will only retunr the buffer."
25337 (interactive "r\nP")
25338 (when (interactive-p)
25339 (setq buffer
"*Org HTML Export*"))
25340 (let ((transient-mark-mode t
) (zmacs-regions t
)
25343 (set-mark (point)) ;; to activate the region
25345 (setq rtn
(org-export-as-html
25348 (if (fboundp 'deactivate-mark
) (deactivate-mark))
25349 (if (and (interactive-p) (bufferp rtn
))
25350 (switch-to-buffer-other-window rtn
)
25353 (defvar html-table-tag nil
) ; dynamically scoped into this.
25354 (defun org-export-as-html (arg &optional hidden ext-plist
25355 to-buffer body-only pub-dir
)
25356 "Export the outline as a pretty HTML file.
25357 If there is an active region, export only the region. The prefix
25358 ARG specifies how many levels of the outline should become
25359 headlines. The default is 3. Lower levels will become bulleted
25360 lists. When HIDDEN is non-nil, don't display the HTML buffer.
25361 EXT-PLIST is a property list with external parameters overriding
25362 org-mode's default settings, but still inferior to file-local
25363 settings. When TO-BUFFER is non-nil, create a buffer with that
25364 name and export to that buffer. If TO-BUFFER is the symbol
25365 `string', don't leave any buffer behind but just return the
25366 resulting HTML as a string. When BODY-ONLY is set, don't produce
25367 the file header and footer, simply return the content of
25368 <body>...</body>, without even the body tags themselves. When
25369 PUB-DIR is set, use this as the publishing directory."
25372 ;; Make sure we have a file name when we need it.
25373 (when (and (not (or to-buffer body-only
))
25374 (not buffer-file-name
))
25375 (if (buffer-base-buffer)
25376 (org-set-local 'buffer-file-name
25377 (with-current-buffer (buffer-base-buffer)
25379 (error "Need a file name to be able to export.")))
25381 (message "Exporting...")
25382 (setq-default org-todo-line-regexp org-todo-line-regexp
)
25383 (setq-default org-deadline-line-regexp org-deadline-line-regexp
)
25384 (setq-default org-done-keywords org-done-keywords
)
25385 (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp
)
25386 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
25388 (org-infile-export-plist)))
25390 (style (plist-get opt-plist
:style
))
25391 (html-extension (plist-get opt-plist
:html-extension
))
25392 (link-validate (plist-get opt-plist
:link-validation-function
))
25393 valid thetoc have-headings first-heading-pos
25394 (odd org-odd-levels-only
)
25395 (region-p (org-region-active-p))
25399 (goto-char (region-beginning))
25400 (and (org-at-heading-p)
25401 (>= (org-end-of-subtree t t
) (region-end))))))
25402 ;; The following two are dynamically scoped into other
25404 (org-current-export-dir
25405 (or pub-dir
(org-export-directory :html opt-plist
)))
25406 (org-current-export-file buffer-file-name
)
25407 (level 0) (line "") (origline "") txt todo
25410 (filename (if to-buffer nil
25413 (file-name-sans-extension
25415 (org-entry-get (region-beginning)
25416 "EXPORT_FILE_NAME" t
))
25417 (file-name-nondirectory buffer-file-name
)))
25418 "." html-extension
)
25419 (file-name-as-directory
25420 (or pub-dir
(org-export-directory :html opt-plist
))))))
25421 (current-dir (if buffer-file-name
25422 (file-name-directory buffer-file-name
)
25423 default-directory
))
25424 (buffer (if to-buffer
25426 ((eq to-buffer
'string
) (get-buffer-create "*Org HTML Export*"))
25427 (t (get-buffer-create to-buffer
)))
25428 (find-file-noselect filename
)))
25429 (org-levels-open (make-vector org-level-max nil
))
25430 (date (plist-get opt-plist
:date
))
25431 (author (plist-get opt-plist
:author
))
25432 (title (or (and subtree-p
(org-export-get-title-from-subtree))
25433 (plist-get opt-plist
:title
)
25435 (plist-get opt-plist
:skip-before-1st-heading
))
25436 (org-export-grab-title-from-buffer))
25437 (and buffer-file-name
25438 (file-name-sans-extension
25439 (file-name-nondirectory buffer-file-name
)))
25441 (html-table-tag (plist-get opt-plist
:html-table-tag
))
25442 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
25443 (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string
"\\>\\)"))
25446 (in-local-list nil
)
25447 (local-list-num nil
)
25448 (local-list-indent nil
)
25449 (llt org-plain-list-ordered-item-terminator
)
25450 (email (plist-get opt-plist
:email
))
25451 (language (plist-get opt-plist
:language
))
25453 (target-alist nil
) tg
25456 (coding-system (and (boundp 'buffer-file-coding-system
)
25457 buffer-file-coding-system
))
25458 (coding-system-for-write (or org-export-html-coding-system
25460 (save-buffer-coding-system (or org-export-html-coding-system
25462 (charset (and coding-system-for-write
25463 (fboundp 'coding-system-get
)
25464 (coding-system-get coding-system-for-write
25468 (if region-p
(region-beginning) (point-min))
25469 (if region-p
(region-end) (point-max))))
25472 (org-cleaned-string-for-export
25476 :skip-before-1st-heading
25477 (plist-get opt-plist
:skip-before-1st-heading
)
25478 :drawers
(plist-get opt-plist
:drawers
)
25480 (plist-get opt-plist
:archived-trees
)
25482 (plist-get opt-plist
:text
)
25484 (plist-get opt-plist
:LaTeX-fragments
))
25487 table-buffer table-orig-buffer
25488 ind start-is-num starter didclose
25489 rpl path desc descp desc1 desc2 link
25492 (let ((inhibit-read-only t
))
25494 (remove-text-properties (point-min) (point-max)
25495 '(:org-license-to-kill t
))))
25497 (message "Exporting...")
25499 (setq org-min-level
(org-get-min-level lines
))
25500 (setq org-last-level org-min-level
)
25501 (org-init-section-numbers)
25504 ((and date
(string-match "%" date
))
25505 (setq date
(format-time-string date
(current-time))))
25507 (t (setq date
(format-time-string "%Y/%m/%d %X" (current-time)))))
25509 ;; Get the language-dependent settings
25510 (setq lang-words
(or (assoc language org-export-language-setup
)
25511 (assoc "en" org-export-language-setup
)))
25513 ;; Switch to the output buffer
25514 (set-buffer buffer
)
25515 (let ((inhibit-read-only t
)) (erase-buffer))
25518 (and (fboundp 'set-buffer-file-coding-system
)
25519 (set-buffer-file-coding-system coding-system-for-write
))
25521 (let ((case-fold-search nil
)
25522 (org-odd-levels-only odd
))
25523 ;; create local variables for all options, to make sure all called
25524 ;; functions get the correct information
25526 (set (make-local-variable (cdr x
))
25527 (plist-get opt-plist
(car x
))))
25528 org-export-plist-vars
)
25529 (setq umax
(if arg
(prefix-numeric-value arg
)
25530 org-export-headline-levels
))
25531 (setq umax-toc
(if (integerp org-export-with-toc
)
25532 (min org-export-with-toc umax
)
25537 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
25538 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
25539 <html xmlns=\"http://www.w3.org/1999/xhtml\"
25540 lang=\"%s\" xml:lang=\"%s\">
25543 <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
25544 <meta name=\"generator\" content=\"Org-mode\"/>
25545 <meta name=\"generated\" content=\"%s\"/>
25546 <meta name=\"author\" content=\"%s\"/>
25550 language language
(org-html-expand title
)
25551 (or charset
"iso-8859-1") date author style
))
25553 (insert (or (plist-get opt-plist
:preamble
) ""))
25555 (when (plist-get opt-plist
:auto-preamble
)
25556 (if title
(insert (format org-export-html-title-format
25557 (org-html-expand title
))))))
25559 (if (and org-export-with-toc
(not body-only
))
25561 (push (format "<h%d>%s</h%d>\n"
25562 org-export-html-toplevel-hlevel
25564 org-export-html-toplevel-hlevel
)
25566 (push "<ul>\n<li>" thetoc
)
25568 (mapcar '(lambda (line)
25569 (if (string-match org-todo-line-regexp line
)
25570 ;; This is a headline
25572 (setq have-headings t
)
25573 (setq level
(- (match-end 1) (match-beginning 1))
25574 level
(org-tr-level level
)
25575 txt
(save-match-data
25577 (org-export-cleanup-toc-line
25578 (match-string 3 line
))))
25580 (or (and org-export-mark-todo-in-toc
25581 (match-beginning 2)
25582 (not (member (match-string 2 line
)
25583 org-done-keywords
)))
25585 (and org-export-mark-todo-in-toc
25587 (org-search-todo-below
25588 line lines level
))))
25590 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt
)
25591 (setq txt
(replace-match " <span class=\"tag\"> \\1</span>" t nil txt
)))
25592 (if (string-match quote-re0 txt
)
25593 (setq txt
(replace-match "" t t txt
)))
25594 (if org-export-with-section-numbers
25595 (setq txt
(concat (org-section-number level
)
25597 (if (<= level
(max umax umax-toc
))
25598 (setq head-count
(+ head-count
1)))
25599 (if (<= level umax-toc
)
25601 (if (> level org-last-level
)
25603 (setq cnt
(- level org-last-level
))
25604 (while (>= (setq cnt
(1- cnt
)) 0)
25605 (push "\n<ul>\n<li>" thetoc
))
25606 (push "\n" thetoc
)))
25607 (if (< level org-last-level
)
25609 (setq cnt
(- org-last-level level
))
25610 (while (>= (setq cnt
(1- cnt
)) 0)
25611 (push "</li>\n</ul>" thetoc
))
25612 (push "\n" thetoc
)))
25613 ;; Check for targets
25614 (while (string-match org-target-regexp line
)
25615 (setq tg
(match-string 1 line
)
25616 line
(replace-match
25617 (concat "@<span class=\"target\">" tg
"@</span> ")
25619 (push (cons (org-solidify-link-text tg
)
25620 (format "sec-%d" head-count
))
25622 (while (string-match "<\\(<\\)+\\|>\\(>\\)+" txt
)
25623 (setq txt
(replace-match "" t t txt
)))
25627 "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
25628 "</li>\n<li><a href=\"#sec-%d\">%s</a>")
25629 head-count txt
) thetoc
)
25631 (setq org-last-level level
))
25635 (while (> org-last-level
(1- org-min-level
))
25636 (setq org-last-level
(1- org-last-level
))
25637 (push "</li>\n</ul>\n" thetoc
))
25638 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
25640 (setq head-count
0)
25641 (org-init-section-numbers)
25643 (while (setq line
(pop lines
) origline line
)
25646 ;; end of quote section?
25647 (when (and inquote
(string-match "^\\*+ " line
))
25648 (insert "</pre>\n")
25649 (setq inquote nil
))
25650 ;; inside a quote section?
25652 (insert (org-html-protect line
) "\n")
25653 (throw 'nextline nil
))
25656 (when (and org-export-with-fixed-width
25657 (string-match "^[ \t]*:\\(.*\\)" line
))
25658 (when (not infixed
)
25660 (insert "<pre>\n"))
25661 (insert (org-html-protect (match-string 1 line
)) "\n")
25663 (not (string-match "^[ \t]*\\(:.*\\)"
25666 (insert "</pre>\n"))
25667 (throw 'nextline nil
))
25670 (when (get-text-property 0 'org-protected line
)
25672 (when (re-search-backward
25673 "\\(<p>\\)\\([ \t\r\n]*\\)\\=" (- (point) 100) t
)
25674 (setq par
(match-string 1))
25675 (replace-match "\\2\n"))
25678 (or (= (length (car lines
)) 0)
25679 (get-text-property 0 'org-protected
(car lines
))))
25680 (insert (pop lines
) "\n"))
25681 (and par
(insert "<p>\n")))
25682 (throw 'nextline nil
))
25685 (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line
)
25686 (insert "\n<hr/>\n")
25687 (throw 'nextline nil
))
25689 ;; make targets to anchors
25690 (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line
)
25693 (setq line
(replace-match
25694 (concat "@<a name=\""
25695 (org-solidify-link-text (match-string 1 line
))
25698 ((and org-export-with-toc
(equal (string-to-char line
) ?
*))
25699 (setq line
(replace-match
25700 (concat "@<span class=\"target\">" (match-string 1 line
) "@</span> ")
25701 ; (concat "@<i>" (match-string 1 line) "@</i> ")
25704 (setq line
(replace-match
25705 (concat "@<a name=\""
25706 (org-solidify-link-text (match-string 1 line
))
25707 "\" class=\"target\">" (match-string 1 line
) "@</a> ")
25710 (setq line
(org-html-handle-time-stamps line
))
25712 ;; replace "&" by "&", "<" and ">" by "<" and ">"
25713 ;; handle @<..> HTML tags (replace "@>..<" by "<..>")
25714 ;; Also handle sub_superscripts and checkboxes
25715 (or (string-match org-table-hline-regexp line
)
25716 (setq line
(org-html-expand line
)))
25718 ;; Format the links
25720 (while (string-match org-bracket-link-analytic-regexp line start
)
25721 (setq start
(match-beginning 0))
25722 (setq type
(if (match-end 2) (match-string 2 line
) "internal"))
25723 (setq path
(match-string 3 line
))
25724 (setq desc1
(if (match-end 5) (match-string 5 line
))
25725 desc2
(if (match-end 2) (concat type
":" path
) path
)
25726 descp
(and desc1
(not (equal desc1 desc2
)))
25727 desc
(or desc1 desc2
))
25728 ;; Make an image out of the description if that is so wanted
25729 (when (and descp
(org-file-image-p desc
))
25731 (if (string-match "^file:" desc
)
25732 (setq desc
(substring desc
(match-end 0)))))
25733 (setq desc
(concat "<img src=\"" desc
"\"/>")))
25734 ;; FIXME: do we need to unescape here somewhere?
25736 ((equal type
"internal")
25740 (org-solidify-link-text
25741 (save-match-data (org-link-unescape path
)) target-alist
)
25742 "\">" desc
"</a>")))
25743 ((member type
'("http" "https"))
25744 ;; standard URL, just check if we need to inline an image
25745 (if (and (or (eq t org-export-html-inline-images
)
25746 (and org-export-html-inline-images
(not descp
)))
25747 (org-file-image-p path
))
25748 (setq rpl
(concat "<img src=\"" type
":" path
"\"/>"))
25749 (setq link
(concat type
":" path
))
25750 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>"))))
25751 ((member type
'("ftp" "mailto" "news"))
25753 (setq link
(concat type
":" path
))
25754 (setq rpl
(concat "<a href=\"" link
"\">" desc
"</a>")))
25755 ((string= type
"file")
25757 (let* ((filename path
)
25758 (abs-p (file-name-absolute-p filename
))
25759 thefile file-is-image-p search
)
25761 (if (string-match "::\\(.*\\)" filename
)
25762 (setq search
(match-string 1 filename
)
25763 filename
(replace-match "" t nil filename
)))
25765 (if (functionp link-validate
)
25766 (funcall link-validate filename current-dir
)
25768 (setq file-is-image-p
(org-file-image-p filename
))
25769 (setq thefile
(if abs-p
(expand-file-name filename
) filename
))
25770 (when (and org-export-html-link-org-files-as-html
25771 (string-match "\\.org$" thefile
))
25772 (setq thefile
(concat (substring thefile
0
25773 (match-beginning 0))
25774 "." html-extension
))
25776 ;; make sure this is can be used as target search
25777 (not (string-match "^[0-9]*$" search
))
25778 (not (string-match "^\\*" search
))
25779 (not (string-match "^/.*/$" search
)))
25780 (setq thefile
(concat thefile
"#"
25781 (org-solidify-link-text
25782 (org-link-unescape search
)))))
25783 (when (string-match "^file:" desc
)
25784 (setq desc
(replace-match "" t t desc
))
25785 (if (string-match "\\.org$" desc
)
25786 (setq desc
(replace-match "" t t desc
))))))
25787 (setq rpl
(if (and file-is-image-p
25788 (or (eq t org-export-html-inline-images
)
25789 (and org-export-html-inline-images
25791 (concat "<img src=\"" thefile
"\"/>")
25792 (concat "<a href=\"" thefile
"\">" desc
"</a>")))
25793 (if (not valid
) (setq rpl desc
))))
25794 ((member type
'("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
25795 (setq rpl
(concat "<i><" type
":"
25796 (save-match-data (org-link-unescape path
))
25798 (setq line
(replace-match rpl t t line
)
25799 start
(+ start
(length rpl
))))
25802 (if (and (string-match org-todo-line-regexp line
)
25803 (match-beginning 2))
25806 (concat (substring line
0 (match-beginning 2))
25808 (if (member (match-string 2 line
)
25811 "\">" (match-string 2 line
)
25812 "</span>" (substring line
(match-end 2)))))
25814 ;; Does this contain a reference to a footnote?
25815 (when org-export-with-footnotes
25817 (while (string-match "\\([^* \t].*?\\)\\[\\([0-9]+\\)\\]" line start
)
25818 (if (get-text-property (match-beginning 2) 'org-protected line
)
25819 (setq start
(match-end 2))
25820 (let ((n (match-string 2 line
)))
25824 "%s<sup><a class=\"footref\" name=\"fnr.%s\" href=\"#fn.%s\">%s</a></sup>"
25825 (match-string 1 line
) n n n
)
25829 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
25830 ;; This is a headline
25831 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)))
25832 txt
(match-string 2 line
))
25833 (if (string-match quote-re0 txt
)
25834 (setq txt
(replace-match "" t t txt
)))
25835 (if (<= level
(max umax umax-toc
))
25836 (setq head-count
(+ head-count
1)))
25837 (when in-local-list
25838 ;; Close any local lists before inserting a new header line
25839 (while local-list-num
25841 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
25842 (pop local-list-num
))
25843 (setq local-list-indent nil
25844 in-local-list nil
))
25845 (setq first-heading-pos
(or first-heading-pos
(point)))
25846 (org-html-level-start level txt umax
25847 (and org-export-with-toc
(<= level umax
))
25850 (when (string-match quote-re line
)
25854 ((and org-export-with-tables
25855 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
25856 (if (not table-open
)
25857 ;; New table starts
25858 (setq table-open t table-buffer nil table-orig-buffer nil
))
25859 ;; Accumulate lines
25860 (setq table-buffer
(cons line table-buffer
)
25861 table-orig-buffer
(cons origline table-orig-buffer
))
25862 (when (or (not lines
)
25863 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
25865 (setq table-open nil
25866 table-buffer
(nreverse table-buffer
)
25867 table-orig-buffer
(nreverse table-orig-buffer
))
25868 (org-close-par-maybe)
25869 (insert (org-format-table-html table-buffer table-orig-buffer
))))
25872 (when (string-match
25874 ((eq llt t
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
25875 ((= llt ?.
) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
25876 ((= llt ?\
)) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
25877 (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
25879 (setq ind
(org-get-string-indentation line
)
25880 start-is-num
(match-beginning 4)
25881 starter
(if (match-beginning 2)
25882 (substring (match-string 2 line
) 0 -
1))
25883 line
(substring line
(match-beginning 5)))
25884 (unless (string-match "[^ \t]" line
)
25885 ;; empty line. Pretend indentation is large.
25886 (setq ind
(if org-empty-line-terminates-plain-lists
25888 (1+ (or (car local-list-indent
) 1)))))
25889 (setq didclose nil
)
25890 (while (and in-local-list
25891 (or (and (= ind
(car local-list-indent
))
25893 (< ind
(car local-list-indent
))))
25896 (insert (if (car local-list-num
) "</ol>\n" "</ul>"))
25897 (pop local-list-num
) (pop local-list-indent
)
25898 (setq in-local-list local-list-indent
))
25901 (or (not in-local-list
)
25902 (> ind
(car local-list-indent
))))
25903 ;; Start new (level of) list
25904 (org-close-par-maybe)
25905 (insert (if start-is-num
"<ol>\n<li>\n" "<ul>\n<li>\n"))
25906 (push start-is-num local-list-num
)
25907 (push ind local-list-indent
)
25908 (setq in-local-list t
))
25910 ;; continue current list
25914 ;; we did close a list, normal text follows: need <p>
25916 (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line
)
25919 (if (equal (match-string 1 line
) "X")
25921 "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
25924 ;; Empty lines start a new paragraph. If hand-formatted lists
25925 ;; are not fully interpreted, lines starting with "-", "+", "*"
25926 ;; also start a new paragraph.
25927 (if (string-match "^ [-+*]-\\|^[ \t]*$" line
) (org-open-par))
25929 ;; Is this the start of a footnote?
25930 (when org-export-with-footnotes
25931 (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line
)
25932 (org-close-par-maybe)
25933 (let ((n (match-string 1 line
)))
25934 (setq line
(replace-match
25935 (format "<p class=\"footnote\"><sup><a class=\"footnum\" name=\"fn.%s\" href=\"#fnr.%s\">%s</a></sup>" n n n
) t t line
)))))
25937 ;; Check if the line break needs to be conserved
25939 ((string-match "\\\\\\\\[ \t]*$" line
)
25940 (setq line
(replace-match "<br/>" t t line
)))
25941 (org-export-preserve-breaks
25942 (setq line
(concat line
"<br/>"))))
25944 (insert line
"\n")))))
25946 ;; Properly close all local lists and other lists
25947 (when inquote
(insert "</pre>\n"))
25948 (when in-local-list
25949 ;; Close any local lists before inserting a new header line
25950 (while local-list-num
25952 (insert (if (car local-list-num
) "</ol>\n" "</ul>\n"))
25953 (pop local-list-num
))
25954 (setq local-list-indent nil
25955 in-local-list nil
))
25956 (org-html-level-start 1 nil umax
25957 (and org-export-with-toc
(<= level umax
))
25961 (when (plist-get opt-plist
:auto-postamble
)
25962 (insert "<div id=\"postamble\">")
25963 (when (and org-export-author-info author
)
25964 (insert "<p class=\"author\"> "
25965 (nth 1 lang-words
) ": " author
"\n")
25967 (if (listp (split-string email
",+ *"))
25969 (insert "<a href=\"mailto:" e
"\"><"
25971 (split-string email
",+ *"))
25972 (insert "<a href=\"mailto:" email
"\"><"
25973 email
"></a>\n")))
25975 (when (and date org-export-time-stamp-file
)
25976 (insert "<p class=\"date\"> "
25977 (nth 2 lang-words
) ": "
25981 (if org-export-html-with-timestamp
25982 (insert org-export-html-html-helper-timestamp
))
25983 (insert (or (plist-get opt-plist
:postamble
) ""))
25984 (insert "</body>\n</html>\n"))
25987 (if (eq major-mode default-major-mode
) (html-mode))
25989 ;; insert the table of contents
25990 (goto-char (point-min))
25992 (if (or (re-search-forward
25993 "<p>\\s-*\\[TABLE-OF-CONTENTS\\]\\s-*</p>" nil t
)
25995 "\\[TABLE-OF-CONTENTS\\]" nil t
))
25997 (goto-char (match-beginning 0))
25998 (replace-match ""))
25999 (goto-char first-heading-pos
)
26000 (when (looking-at "\\s-*</p>")
26001 (goto-char (match-end 0))
26003 (insert "<div id=\"table-of-contents\">\n")
26004 (mapc 'insert thetoc
)
26005 (insert "</div>\n"))
26006 ;; remove empty paragraphs and lists
26007 (goto-char (point-min))
26008 (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t
)
26009 (replace-match ""))
26010 (goto-char (point-min))
26011 (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t
)
26012 (replace-match ""))
26013 (goto-char (point-min))
26014 (while (re-search-forward "</ul>\\s-*<ul>\n?" nil t
)
26015 (replace-match ""))
26016 ;; Convert whitespace place holders
26017 (goto-char (point-min))
26019 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
26020 (setq n
(get-text-property beg
'org-whitespace
)
26021 end
(next-single-property-change beg
'org-whitespace
))
26023 (delete-region beg end
)
26024 (insert (format "<span style=\"visibility:hidden;\">%s</span>"
26025 (make-string n ?x
)))))
26027 (or to-buffer
(progn (save-buffer) (kill-buffer (current-buffer))))
26028 (goto-char (point-min))
26029 (message "Exporting... done")
26030 (if (eq to-buffer
'string
)
26031 (prog1 (buffer-substring (point-min) (point-max))
26032 (kill-buffer (current-buffer)))
26033 (current-buffer)))))
26035 (defvar org-table-colgroup-info nil
)
26036 (defun org-format-table-ascii (lines)
26037 "Format a table for ascii export."
26038 (if (stringp lines
)
26039 (setq lines
(org-split-string lines
"\n")))
26040 (if (not (string-match "^[ \t]*|" (car lines
)))
26041 ;; Table made by table.el - test for spanning
26044 ;; A normal org table
26045 ;; Get rid of hlines at beginning and end
26046 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
26047 (setq lines
(nreverse lines
))
26048 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
26049 (setq lines
(nreverse lines
))
26050 (when org-export-table-remove-special-lines
26051 ;; Check if the table has a marking column. If yes remove the
26052 ;; column and the special lines
26053 (setq lines
(org-table-clean-before-export lines
)))
26054 ;; Get rid of the vertical lines except for grouping
26055 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
26056 rtn line vl1 start
)
26057 (while (setq line
(pop lines
))
26058 (if (string-match org-table-hline-regexp line
)
26059 (and (string-match "|\\(.*\\)|" line
)
26060 (setq line
(replace-match " \\1" t nil line
)))
26061 (setq start
0 vl1 vl
)
26062 (while (string-match "|" line start
)
26063 (setq start
(match-end 0))
26064 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
26068 (defun org-colgroup-info-to-vline-list (info)
26071 (setq last new new
(pop info
))
26072 (if (or (memq last
'(:end
:startend
))
26073 (memq new
'(:start
:startend
)))
26076 (setq vl
(nreverse vl
))
26077 (and vl
(setcar vl nil
))
26080 (defun org-format-table-html (lines olines
)
26081 "Find out which HTML converter to use and return the HTML code."
26082 (if (stringp lines
)
26083 (setq lines
(org-split-string lines
"\n")))
26084 (if (string-match "^[ \t]*|" (car lines
))
26085 ;; A normal org table
26086 (org-format-org-table-html lines
)
26087 ;; Table made by table.el - test for spanning
26088 (let* ((hlines (delq nil
(mapcar
26090 (if (string-match "^[ \t]*\\+-" x
) x
26093 (first (car hlines
))
26094 (ll (and (string-match "\\S-+" first
)
26095 (match-string 0 first
)))
26096 (re (concat "^[ \t]*" (regexp-quote ll
)))
26097 (spanning (delq nil
(mapcar (lambda (x) (not (string-match re x
)))
26099 (if (and (not spanning
)
26100 (not org-export-prefer-native-exporter-for-tables
))
26101 ;; We can use my own converter with HTML conversions
26102 (org-format-table-table-html lines
)
26103 ;; Need to use the code generator in table.el, with the original text.
26104 (org-format-table-table-html-using-table-generate-source olines
)))))
26106 (defun org-format-org-table-html (lines &optional splice
)
26107 "Format a table into HTML."
26108 ;; Get rid of hlines at beginning and end
26109 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
26110 (setq lines
(nreverse lines
))
26111 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
26112 (setq lines
(nreverse lines
))
26113 (when org-export-table-remove-special-lines
26114 ;; Check if the table has a marking column. If yes remove the
26115 ;; column and the special lines
26116 (setq lines
(org-table-clean-before-export lines
)))
26118 (let ((head (and org-export-highlight-first-table-line
26120 (lambda (x) (string-match "^[ \t]*|-" x
))
26123 tbopen line fields html gr colgropen
)
26124 (if splice
(setq head nil
))
26125 (unless splice
(push (if head
"<thead>" "<tbody>") html
))
26127 (while (setq line
(pop lines
))
26129 (if (string-match "^[ \t]*|-" line
)
26132 (push (if head
"</thead>" "</tbody>") html
)
26133 (if lines
(push "<tbody>" html
) (setq tbopen nil
)))
26134 (setq head nil
) ;; head ends here, first time around
26135 ;; ignore this line
26136 (throw 'next-line t
)))
26137 ;; Break the line into fields
26138 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
26139 (unless fnum
(setq fnum
(make-vector (length fields
) 0)))
26140 (setq nlines
(1+ nlines
) i -
1)
26141 (push (concat "<tr>"
26145 (if (and (< i nlines
)
26146 (string-match org-table-number-regexp x
))
26147 (incf (aref fnum i
)))
26149 (concat (car org-export-table-header-tags
) x
26150 (cdr org-export-table-header-tags
))
26151 (concat (car org-export-table-data-tags
) x
26152 (cdr org-export-table-data-tags
))))
26156 (unless splice
(if tbopen
(push "</tbody>" html
)))
26157 (unless splice
(push "</table>\n" html
))
26158 (setq html
(nreverse html
))
26160 ;; Put in col tags with the alignment (unfortuntely often ignored...)
26163 (setq gr
(pop org-table-colgroup-info
))
26164 (format "%s<col align=\"%s\"></col>%s"
26165 (if (memq gr
'(:start
:startend
))
26167 (if colgropen
"</colgroup>\n<colgroup>" "<colgroup>")
26168 (setq colgropen t
))
26170 (if (> (/ (float x
) nlines
) org-table-number-fraction
)
26172 (if (memq gr
'(:end
:startend
))
26173 (progn (setq colgropen nil
) "</colgroup>")
26177 (if colgropen
(setq html
(cons (car html
) (cons "</colgroup>" (cdr html
)))))
26178 (push html-table-tag html
))
26179 (concat (mapconcat 'identity html
"\n") "\n")))
26181 (defun org-table-clean-before-export (lines)
26182 "Check if the table has a marking column.
26183 If yes remove the column and the special lines."
26184 (setq org-table-colgroup-info nil
)
26187 (lambda (x) (or (string-match "^[ \t]*|-" x
)
26188 (string-match "^[ \t]*| *\\([#!$*_^ /]\\) *|" x
)))
26191 (setq org-table-clean-did-remove-column nil
)
26196 ((string-match "^[ \t]*| */ *|" x
)
26197 (setq org-table-colgroup-info
26198 (mapcar (lambda (x)
26199 (cond ((member x
'("<" "<")) :start
)
26200 ((member x
'(">" ">")) :end
)
26201 ((member x
'("<>" "<>")) :startend
)
26203 (org-split-string x
"[ \t]*|[ \t]*")))
26207 (setq org-table-clean-did-remove-column t
)
26212 ((string-match "^[ \t]*| */ *|" x
)
26213 (setq org-table-colgroup-info
26214 (mapcar (lambda (x)
26215 (cond ((member x
'("<" "<")) :start
)
26216 ((member x
'(">" ">")) :end
)
26217 ((member x
'("<>" "<>")) :startend
)
26219 (cdr (org-split-string x
"[ \t]*|[ \t]*"))))
26221 ((string-match "^[ \t]*| *[!_^/] *|" x
)
26222 nil
) ; ignore this line
26223 ((or (string-match "^\\([ \t]*\\)|-+\\+" x
)
26224 (string-match "^\\([ \t]*\\)|[^|]*|" x
))
26225 ;; remove the first column
26226 (replace-match "\\1|" t nil x
))))
26229 (defun org-format-table-table-html (lines)
26230 "Format a table generated by table.el into HTML.
26231 This conversion does *not* use `table-generate-source' from table.el.
26232 This has the advantage that Org-mode's HTML conversions can be used.
26233 But it has the disadvantage, that no cell- or row-spanning is allowed."
26234 (let (line field-buffer
26235 (head org-export-highlight-first-table-line
)
26237 (setq html
(concat html-table-tag
"\n"))
26238 (while (setq line
(pop lines
))
26239 (setq empty
" ")
26241 (if (string-match "^[ \t]*\\+-" line
)
26252 (if (equal x
"") (setq x empty
))
26254 (concat (car org-export-table-header-tags
) x
26255 (cdr org-export-table-header-tags
))
26256 (concat (car org-export-table-data-tags
) x
26257 (cdr org-export-table-data-tags
))))
26261 (setq field-buffer nil
)))
26262 ;; Ignore this line
26263 (throw 'next-line t
)))
26264 ;; Break the line into fields and store the fields
26265 (setq fields
(org-split-string line
"[ \t]*|[ \t]*"))
26267 (setq field-buffer
(mapcar
26269 (concat x
"<br/>" (pop fields
)))
26271 (setq field-buffer fields
))))
26272 (setq html
(concat html
"</table>\n"))
26275 (defun org-format-table-table-html-using-table-generate-source (lines)
26276 "Format a table into html, using `table-generate-source' from table.el.
26277 This has the advantage that cell- or row-spanning is allowed.
26278 But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
26280 (with-current-buffer (get-buffer-create " org-tmp1 ")
26282 (insert (mapconcat 'identity lines
"\n"))
26283 (goto-char (point-min))
26284 (if (not (re-search-forward "|[^+]" nil t
))
26285 (error "Error processing table"))
26286 (table-recognize-table)
26287 (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
26288 (table-generate-source 'html
" org-tmp2 ")
26289 (set-buffer " org-tmp2 ")
26290 (buffer-substring (point-min) (point-max))))
26292 (defun org-html-handle-time-stamps (s)
26293 "Format time stamps in string S, or remove them."
26296 (while (string-match org-maybe-keyword-time-regexp s
)
26297 (if (and (match-end 1) (equal (match-string 1 s
) org-clock-string
))
26298 ;; never export CLOCK
26300 (or b
(setq b
(substring s
0 (match-beginning 0))))
26301 (if (not org-export-with-timestamps
)
26302 (setq r
(concat r
(substring s
0 (match-beginning 0)))
26303 s
(substring s
(match-end 0)))
26305 r
(substring s
0 (match-beginning 0))
26307 (format "@<span class=\"timestamp-kwd\">%s @</span>"
26308 (match-string 1 s
)))
26309 (format " @<span class=\"timestamp\">%s@</span>"
26311 (org-translate-time (match-string 3 s
)) 1 -
1)))
26312 s
(substring s
(match-end 0)))))
26313 ;; Line break if line started and ended with time stamp stuff
26316 (setq r
(concat r s
))
26317 (unless (string-match "\\S-" (concat b s
))
26318 (setq r
(concat r
"@<br/>")))
26321 (defun org-html-protect (s)
26322 ;; convert & to &, < to < and > to >
26324 (while (string-match "&" s start
)
26325 (setq s
(replace-match "&" t t s
)
26326 start
(1+ (match-beginning 0))))
26327 (while (string-match "<" s
)
26328 (setq s
(replace-match "<" t t s
)))
26329 (while (string-match ">" s
)
26330 (setq s
(replace-match ">" t t s
))))
26333 (defun org-export-cleanup-toc-line (s)
26334 "Remove tags and time staps from lines going into the toc."
26335 (when (memq org-export-with-tags
'(not-in-toc nil
))
26336 (if (string-match (org-re " +:[[:alnum:]_@:]+: *$") s
)
26337 (setq s
(replace-match "" t t s
))))
26338 (when org-export-remove-timestamps-from-toc
26339 (while (string-match org-maybe-keyword-time-regexp s
)
26340 (setq s
(replace-match "" t t s
))))
26341 (while (string-match org-bracket-link-regexp s
)
26342 (setq s
(replace-match (match-string (if (match-end 3) 3 1) s
)
26346 (defun org-html-expand (string)
26347 "Prepare STRING for HTML export. Applies all active conversions.
26348 If there are links in the string, don't modify these."
26349 (let* ((re (concat org-bracket-link-regexp
"\\|"
26350 (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$")))
26352 (while (setq m
(string-match re string
))
26353 (setq s
(substring string
0 m
)
26354 l
(match-string 0 string
)
26355 string
(substring string
(match-end 0)))
26356 (push (org-html-do-expand s
) res
)
26358 (push (org-html-do-expand string
) res
)
26359 (apply 'concat
(nreverse res
))))
26361 (defun org-html-do-expand (s)
26362 "Apply all active conversions to translate special ASCII to HTML."
26363 (setq s
(org-html-protect s
))
26364 (if org-export-html-expand
26366 (while (string-match "@<\\([^&]*\\)>" s
)
26367 (setq s
(replace-match "<\\1>" t nil s
)))))
26368 (if org-export-with-emphasize
26369 (setq s
(org-export-html-convert-emphasize s
)))
26370 (if org-export-with-special-strings
26371 (setq s
(org-export-html-convert-special-strings s
)))
26372 (if org-export-with-sub-superscripts
26373 (setq s
(org-export-html-convert-sub-super s
)))
26374 (if org-export-with-TeX-macros
26375 (let ((start 0) wd ass
)
26376 (while (setq start
(string-match "\\\\\\([a-zA-Z]+\\)" s start
))
26377 (if (get-text-property (match-beginning 0) 'org-protected s
)
26378 (setq start
(match-end 0))
26379 (setq wd
(match-string 1 s
))
26380 (if (setq ass
(assoc wd org-html-entities
))
26381 (setq s
(replace-match (or (cdr ass
)
26382 (concat "&" (car ass
) ";"))
26384 (setq start
(+ start
(length wd
))))))))
26387 (defun org-create-multibrace-regexp (left right n
)
26388 "Create a regular expression which will match a balanced sexp.
26389 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
26390 as single character strings.
26391 The regexp returned will match the entire expression including the
26392 delimiters. It will also define a single group which contains the
26393 match except for the outermost delimiters. The maximum depth of
26394 stacked delimiters is N. Escaping delimiters is not possible."
26395 (let* ((nothing (concat "[^" "\\" left
"\\" right
"]*?"))
26398 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
26401 re
(concat re or next
)
26402 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
26403 (concat left
"\\(" re
"\\)" right
)))
26405 (defvar org-match-substring-regexp
26407 "\\([^\\]\\)\\([_^]\\)\\("
26408 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
26410 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
26412 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
26413 "The regular expression matching a sub- or superscript.")
26415 (defvar org-match-substring-with-braces-regexp
26417 "\\([^\\]\\)\\([_^]\\)\\("
26418 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
26420 "The regular expression matching a sub- or superscript, forcing braces.")
26422 (defconst org-export-html-special-string-regexps
26423 '(("\\\\-" .
"­")
26424 ("---\\([^-]\\)" .
"—\\1")
26425 ("--\\([^-]\\)" .
"–\\1")
26426 ("\\.\\.\\." .
"…"))
26427 "Regular expressions for special string conversion.")
26429 (defun org-export-html-convert-special-strings (string)
26430 "Convert special characters in STRING to HTML."
26431 (let ((all org-export-html-special-string-regexps
)
26433 (while (setq a
(pop all
))
26434 (setq re
(car a
) rpl
(cdr a
) start
0)
26435 (while (string-match re string start
)
26436 (if (get-text-property (match-beginning 0) 'org-protected string
)
26437 (setq start
(match-end 0))
26438 (setq string
(replace-match rpl t nil string
)))))
26441 (defun org-export-html-convert-sub-super (string)
26442 "Convert sub- and superscripts in STRING to HTML."
26443 (let (key c
(s 0) (requireb (eq org-export-with-sub-superscripts
'{})))
26444 (while (string-match org-match-substring-regexp string s
)
26446 ((and requireb
(match-end 8)) (setq s
(match-end 2)))
26447 ((get-text-property (match-beginning 2) 'org-protected string
)
26448 (setq s
(match-end 2)))
26450 (setq s
(match-end 1)
26451 key
(if (string= (match-string 2 string
) "_") "sub" "sup")
26452 c
(or (match-string 8 string
)
26453 (match-string 6 string
)
26454 (match-string 5 string
))
26455 string
(replace-match
26456 (concat (match-string 1 string
)
26457 "<" key
">" c
"</" key
">")
26459 (while (string-match "\\\\\\([_^]\\)" string
)
26460 (setq string
(replace-match (match-string 1 string
) t t string
)))
26463 (defun org-export-html-convert-emphasize (string)
26466 (while (string-match org-emph-re string s
)
26468 (substring string
(match-beginning 3) (1+ (match-beginning 3)))
26469 (substring string
(match-beginning 4) (1+ (match-beginning 4)))))
26470 (setq s
(match-beginning 0)
26473 (match-string 1 string
)
26474 (nth 2 (assoc (match-string 3 string
) org-emphasis-alist
))
26475 (match-string 4 string
)
26476 (nth 3 (assoc (match-string 3 string
)
26477 org-emphasis-alist
))
26478 (match-string 5 string
))
26479 string
(replace-match rpl t t string
)
26480 s
(+ s
(- (length rpl
) 2)))
26484 (defvar org-par-open nil
)
26485 (defun org-open-par ()
26486 "Insert <p>, but first close previous paragraph if any."
26487 (org-close-par-maybe)
26489 (setq org-par-open t
))
26490 (defun org-close-par-maybe ()
26491 "Close paragraph if there is one open."
26494 (setq org-par-open nil
)))
26495 (defun org-close-li ()
26496 "Close <li> if necessary."
26497 (org-close-par-maybe)
26498 (insert "</li>\n"))
26500 (defvar body-only
) ; dynamically scoped into this.
26501 (defun org-html-level-start (level title umax with-toc head-count
)
26502 "Insert a new level in HTML export.
26503 When TITLE is nil, just close all open levels."
26504 (org-close-par-maybe)
26505 (let ((l org-level-max
))
26506 (while (>= l level
)
26507 (if (aref org-levels-open
(1- l
))
26509 (org-html-level-close l umax
)
26510 (aset org-levels-open
(1- l
) nil
)))
26513 ;; If title is nil, this means this function is called to close
26514 ;; all levels, so the rest is done only if title is given
26515 (when (string-match (org-re "\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
26516 (setq title
(replace-match
26517 (if org-export-with-tags
26520 " <span class=\"tag\">"
26521 (mapconcat 'identity
(org-split-string
26522 (match-string 1 title
) ":")
26529 (if (aref org-levels-open
(1- level
))
26532 (insert "<li>" title
"<br/>\n"))
26533 (aset org-levels-open
(1- level
) t
)
26534 (org-close-par-maybe)
26535 (insert "<ul>\n<li>" title
"<br/>\n")))
26536 (aset org-levels-open
(1- level
) t
)
26537 (if (and org-export-with-section-numbers
(not body-only
))
26538 (setq title
(concat (org-section-number level
) " " title
)))
26539 (setq level
(+ level org-export-html-toplevel-hlevel -
1))
26541 (insert (format "\n<div class=\"outline-%d\">\n<h%d id=\"sec-%d\">%s</h%d>\n"
26542 level level head-count title level
))
26543 (insert (format "\n<div class=\"outline-%d\">\n<h%d>%s</h%d>\n" level level title level
)))
26546 (defun org-html-level-close (level max-outline-level
)
26547 "Terminate one level in HTML export."
26548 (if (<= level max-outline-level
)
26549 (insert "</div>\n")
26551 (insert "</ul>\n")))
26553 ;;; iCalendar export
26556 (defun org-export-icalendar-this-file ()
26557 "Export current file as an iCalendar file.
26558 The iCalendar file will be located in the same directory as the Org-mode
26559 file, but with extension `.ics'."
26561 (org-export-icalendar nil buffer-file-name
))
26564 (defun org-export-icalendar-all-agenda-files ()
26565 "Export all files in `org-agenda-files' to iCalendar .ics files.
26566 Each iCalendar file will be located in the same directory as the Org-mode
26567 file, but with extension `.ics'."
26569 (apply 'org-export-icalendar nil
(org-agenda-files t
)))
26572 (defun org-export-icalendar-combine-agenda-files ()
26573 "Export all files in `org-agenda-files' to a single combined iCalendar file.
26574 The file is stored under the name `org-combined-agenda-icalendar-file'."
26576 (apply 'org-export-icalendar t
(org-agenda-files t
)))
26578 (defun org-export-icalendar (combine &rest files
)
26579 "Create iCalendar files for all elements of FILES.
26580 If COMBINE is non-nil, combine all calendar entries into a single large
26581 file and store it under the name `org-combined-agenda-icalendar-file'."
26583 (org-prepare-agenda-buffers files
)
26584 (let* ((dir (org-export-directory
26585 :ical
(list :publishing-directory
26586 org-export-publishing-directory
)))
26587 file ical-file ical-buffer category started org-agenda-new-buffers
)
26589 (and (get-buffer "*ical-tmp*") (kill-buffer "*ical-tmp*"))
26592 (if (file-name-absolute-p org-combined-agenda-icalendar-file
)
26593 org-combined-agenda-icalendar-file
26594 (expand-file-name org-combined-agenda-icalendar-file dir
))
26595 ical-buffer
(org-get-agenda-file-buffer ical-file
))
26596 (set-buffer ical-buffer
) (erase-buffer))
26597 (while (setq file
(pop files
))
26599 (org-check-agenda-file file
)
26600 (set-buffer (org-get-agenda-file-buffer file
))
26602 (setq ical-file
(concat (file-name-as-directory dir
)
26603 (file-name-sans-extension
26604 (file-name-nondirectory buffer-file-name
))
26606 (setq ical-buffer
(org-get-agenda-file-buffer ical-file
))
26607 (with-current-buffer ical-buffer
(erase-buffer)))
26608 (setq category
(or org-category
26609 (file-name-sans-extension
26610 (file-name-nondirectory buffer-file-name
))))
26611 (if (symbolp category
) (setq category
(symbol-name category
)))
26612 (let ((standard-output ical-buffer
))
26614 (and (not started
) (setq started t
)
26615 (org-start-icalendar-file org-icalendar-combined-name
))
26616 (org-start-icalendar-file category
))
26617 (org-print-icalendar-entries combine
)
26618 (when (or (and combine
(not files
)) (not combine
))
26619 (org-finish-icalendar-file)
26620 (set-buffer ical-buffer
)
26622 (run-hooks 'org-after-save-iCalendar-file-hook
)))))
26623 (org-release-buffers org-agenda-new-buffers
))))
26625 (defvar org-after-save-iCalendar-file-hook nil
26626 "Hook run after an iCalendar file has been saved.
26627 The iCalendar buffer is still current when this hook is run.
26628 A good way to use this is to tell a desktop calenndar application to re-read
26629 the iCalendar file.")
26631 (defun org-print-icalendar-entries (&optional combine
)
26632 "Print iCalendar entries for the current Org-mode file to `standard-output'.
26633 When COMBINE is non nil, add the category to each line."
26634 (let ((re1 (concat org-ts-regexp
"\\|<%%([^>\n]+>"))
26635 (re2 (concat "--?-?\\(" org-ts-regexp
"\\)"))
26636 (dts (org-ical-ts-to-string
26637 (format-time-string (cdr org-time-stamp-formats
) (current-time))
26639 hd ts ts2 state status
(inc t
) pos b sexp rrule
26640 scheduledp deadlinep tmp pri category entry location summary desc
26641 (sexp-buffer (get-buffer-create "*ical-tmp*")))
26642 (org-refresh-category-properties)
26644 (goto-char (point-min))
26645 (while (re-search-forward re1 nil t
)
26648 (setq pos
(match-beginning 0)
26649 ts
(match-string 0)
26651 hd
(org-get-heading)
26652 summary
(org-icalendar-cleanup-string
26653 (org-entry-get nil
"SUMMARY"))
26654 desc
(org-icalendar-cleanup-string
26655 (or (org-entry-get nil
"DESCRIPTION")
26656 (and org-icalendar-include-body
(org-get-entry)))
26657 t org-icalendar-include-body
)
26658 location
(org-icalendar-cleanup-string
26659 (org-entry-get nil
"LOCATION"))
26660 category
(org-get-category))
26661 (if (looking-at re2
)
26663 (goto-char (match-end 0))
26664 (setq ts2
(match-string 1) inc nil
))
26665 (setq tmp
(buffer-substring (max (point-min)
26666 (- pos org-ds-keyword-length
))
26668 ts2
(if (string-match "[0-9]\\{1,2\\}:[0-9][0-9]-\\([0-9]\\{1,2\\}:[0-9][0-9]\\)" ts
)
26671 (replace-match "\\1" t nil ts
))
26673 deadlinep
(string-match org-deadline-regexp tmp
)
26674 scheduledp
(string-match org-scheduled-regexp tmp
)
26675 ;; donep (org-entry-is-done-p)
26677 (if (or (string-match org-tr-regexp hd
)
26678 (string-match org-ts-regexp hd
))
26679 (setq hd
(replace-match "" t t hd
)))
26680 (if (string-match "\\+\\([0-9]+\\)\\([dwmy]\\)>" ts
)
26682 (concat "\nRRULE:FREQ="
26684 (match-string 2 ts
)
26685 '(("d" .
"DAILY")("w" .
"WEEKLY")
26686 ("m" .
"MONTHLY")("y" .
"YEARLY"))))
26687 ";INTERVAL=" (match-string 1 ts
)))
26689 (setq summary
(or summary hd
))
26690 (if (string-match org-bracket-link-regexp summary
)
26692 (replace-match (if (match-end 3)
26693 (match-string 3 summary
)
26694 (match-string 1 summary
))
26696 (if deadlinep
(setq summary
(concat "DL: " summary
)))
26697 (if scheduledp
(setq summary
(concat "S: " summary
)))
26698 (if (string-match "\\`<%%" ts
)
26699 (with-current-buffer sexp-buffer
26700 (insert (substring ts
1 -
1) " " summary
"\n"))
26701 (princ (format "BEGIN:VEVENT
26707 (org-ical-ts-to-string ts
"DTSTART")
26708 (org-ical-ts-to-string ts2
"DTEND" inc
)
26710 (if (and desc
(string-match "\\S-" desc
))
26711 (concat "\nDESCRIPTION: " desc
) "")
26712 (if (and location
(string-match "\\S-" location
))
26713 (concat "\nLOCATION: " location
) "")
26716 (when (and org-icalendar-include-sexps
26717 (condition-case nil
(require 'icalendar
) (error nil
))
26718 (fboundp 'icalendar-export-region
))
26719 ;; Get all the literal sexps
26720 (goto-char (point-min))
26721 (while (re-search-forward "^&?%%(" nil t
)
26724 (setq b
(match-beginning 0))
26725 (goto-char (1- (match-end 0)))
26728 (setq sexp
(buffer-substring b
(point)))
26729 (with-current-buffer sexp-buffer
26730 (insert sexp
"\n"))
26731 (princ (org-diary-to-ical-string sexp-buffer
)))))
26733 (when org-icalendar-include-todo
26734 (goto-char (point-min))
26735 (while (re-search-forward org-todo-line-regexp nil t
)
26738 (setq state
(match-string 2))
26739 (setq status
(if (member state org-done-keywords
)
26740 "COMPLETED" "NEEDS-ACTION"))
26742 (or (not (member state org-done-keywords
))
26743 (eq org-icalendar-include-todo
'all
))
26744 (not (member org-archive-tag
(org-get-tags-at)))
26746 (setq hd
(match-string 3)
26747 summary
(org-icalendar-cleanup-string
26748 (org-entry-get nil
"SUMMARY"))
26749 desc
(org-icalendar-cleanup-string
26750 (or (org-entry-get nil
"DESCRIPTION")
26751 (and org-icalendar-include-body
(org-get-entry)))
26752 t org-icalendar-include-body
)
26753 location
(org-icalendar-cleanup-string
26754 (org-entry-get nil
"LOCATION")))
26755 (if (string-match org-bracket-link-regexp hd
)
26756 (setq hd
(replace-match (if (match-end 3) (match-string 3 hd
)
26757 (match-string 1 hd
))
26759 (if (string-match org-priority-regexp hd
)
26760 (setq pri
(string-to-char (match-string 2 hd
))
26761 hd
(concat (substring hd
0 (match-beginning 1))
26762 (substring hd
(match-end 1))))
26763 (setq pri org-default-priority
))
26764 (setq pri
(floor (1+ (* 8.
(/ (float (- org-lowest-priority pri
))
26765 (- org-lowest-priority org-highest-priority
))))))
26767 (princ (format "BEGIN:VTODO
26777 (if (and location
(string-match "\\S-" location
))
26778 (concat "\nLOCATION: " location
) "")
26779 (if (and desc
(string-match "\\S-" desc
))
26780 (concat "\nDESCRIPTION: " desc
) "")
26781 category pri status
)))))))))
26783 (defun org-icalendar-cleanup-string (s &optional is-body maxlength
)
26784 "Take out stuff and quote what needs to be quoted.
26785 When IS-BODY is non-nil, assume that this is the body of an item, clean up
26786 whitespace, newlines, drawers, and timestamps, and cut it down to MAXLENGTH
26791 (let ((re (concat "\\(" org-drawer-regexp
"\\)[^\000]*?:END:.*\n?"))
26792 (re2 (concat "^[ \t]*" org-keyword-time-regexp
".*\n?")))
26793 (while (string-match re s
) (setq s
(replace-match "" t t s
)))
26794 (while (string-match re2 s
) (setq s
(replace-match "" t t s
)))))
26796 (while (string-match "\\([,;\\]\\)" s start
)
26797 (setq start
(+ (match-beginning 0) 2)
26798 s
(replace-match "\\\\\\1" nil nil s
))))
26800 (while (string-match "[ \t]*\n[ \t]*" s
)
26801 (setq s
(replace-match "\\n" t t s
))))
26802 (setq s
(org-trim s
))
26805 (if (and (numberp maxlength
)
26806 (> (length s
) maxlength
))
26807 (setq s
(substring s
0 maxlength
)))))
26810 (defun org-get-entry ()
26811 "Clean-up description string."
26813 (org-back-to-heading t
)
26814 (buffer-substring (point-at-bol 2) (org-end-of-subtree t
))))
26816 (defun org-start-icalendar-file (name)
26817 "Start an iCalendar file by inserting the header."
26818 (let ((user user-full-name
)
26819 (name (or name
"unknown"))
26820 (timezone (cadr (current-time-zone))))
26822 (format "BEGIN:VCALENDAR
26825 PRODID:-//%s//Emacs with Org-mode//EN
26827 CALSCALE:GREGORIAN\n" name user timezone
))))
26829 (defun org-finish-icalendar-file ()
26830 "Finish an iCalendar file by inserting the END statement."
26831 (princ "END:VCALENDAR\n"))
26833 (defun org-ical-ts-to-string (s keyword
&optional inc
)
26834 "Take a time string S and convert it to iCalendar format.
26835 KEYWORD is added in front, to make a complete line like DTSTART....
26836 When INC is non-nil, increase the hour by two (if time string contains
26837 a time), or the day by one (if it does not contain a time)."
26838 (let ((t1 (org-parse-time-string s
'nodefault
))
26839 t2 fmt have-time time
)
26840 (if (and (car t1
) (nth 1 t1
) (nth 2 t1
))
26841 (setq t2 t1 have-time t
)
26842 (setq t2
(org-parse-time-string s
)))
26843 (let ((s (car t2
)) (mi (nth 1 t2
)) (h (nth 2 t2
))
26844 (d (nth 3 t2
)) (m (nth 4 t2
)) (y (nth 5 t2
)))
26847 (if org-agenda-default-appointment-duration
26848 (setq mi
(+ org-agenda-default-appointment-duration mi
))
26851 (setq time
(encode-time s mi h d m y
)))
26852 (setq fmt
(if have-time
":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
26853 (concat keyword
(format-time-string fmt time
))))
26857 (defun org-export-as-xoxo-insert-into (buffer &rest output
)
26858 (with-current-buffer buffer
26859 (apply 'insert output
)))
26860 (put 'org-export-as-xoxo-insert-into
'lisp-indent-function
1)
26862 (defun org-export-as-xoxo (&optional buffer
)
26863 "Export the org buffer as XOXO.
26864 The XOXO buffer is named *xoxo-<source buffer name>*"
26865 (interactive (list (current-buffer)))
26866 ;; A quickie abstraction
26868 ;; Output everything as XOXO
26869 (with-current-buffer (get-buffer buffer
)
26870 (let* ((pos (point))
26871 (opt-plist (org-combine-plists (org-default-export-plist)
26872 (org-infile-export-plist)))
26873 (filename (concat (file-name-as-directory
26874 (org-export-directory :xoxo opt-plist
))
26875 (file-name-sans-extension
26876 (file-name-nondirectory buffer-file-name
))
26878 (out (find-file-noselect filename
))
26881 (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
26882 ;; Check the output buffer is empty.
26883 (with-current-buffer out
(erase-buffer))
26884 ;; Kick off the output
26885 (org-export-as-xoxo-insert-into out
"<ol class='xoxo'>\n")
26886 (while (re-search-forward "^\\(\\*+\\)[ \t]+\\(.+\\)" (point-max) 't
)
26887 (let* ((hd (match-string-no-properties 1))
26888 (level (length hd
))
26890 (match-string-no-properties 2)
26892 (goto-char (match-end 0))
26897 (if (looking-at "^[ \t]\\(.*\\)")
26898 (setq str
(concat str
(match-string-no-properties 1)))
26899 (throw 'loop str
)))))))))
26901 ;; Handle level rendering
26903 ((> level last-level
)
26904 (org-export-as-xoxo-insert-into out
"\n<ol>\n"))
26906 ((< level last-level
)
26907 (dotimes (- (- last-level level
) 1)
26909 (org-export-as-xoxo-insert-into out
"</li>\n"))
26910 (org-export-as-xoxo-insert-into out
"</ol>\n"))
26912 (org-export-as-xoxo-insert-into out
"</li>\n")
26913 (setq hanging-li nil
)))
26915 ((equal level last-level
)
26917 (org-export-as-xoxo-insert-into out
"</li>\n")))
26920 (setq last-level level
)
26922 ;; And output the new li
26923 (setq hanging-li
't
)
26924 (if (equal ?
+ (elt text
0))
26925 (org-export-as-xoxo-insert-into out
"<li class='" (substring text
1) "'>")
26926 (org-export-as-xoxo-insert-into out
"<li>" text
))))
26928 ;; Finally finish off the ol
26929 (dotimes (- last-level
1)
26931 (org-export-as-xoxo-insert-into out
"</li>\n"))
26932 (org-export-as-xoxo-insert-into out
"</ol>\n"))
26935 ;; Finish the buffer off and clean it up.
26936 (switch-to-buffer-other-window out
)
26937 (indent-region (point-min) (point-max) nil
)
26939 (goto-char (point-min))
26945 ;; Make `C-c C-x' a prefix key
26946 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
26948 ;; TAB key with modifiers
26949 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
26950 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
26951 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
26952 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
26953 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
26954 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
26955 ;; The following line is necessary under Suse GNU/Linux
26956 (unless (featurep 'xemacs
)
26957 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
26958 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
26959 (define-key org-mode-map
[backtab] 'org-shifttab)
26961 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
26962 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
26963 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
26965 ;; Cursor keys with modifiers
26966 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
26967 (org-defkey org-mode-map [(meta right)] 'org-metaright)
26968 (org-defkey org-mode-map [(meta up)] 'org-metaup)
26969 (org-defkey org-mode-map [(meta down)] 'org-metadown)
26971 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
26972 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
26973 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
26974 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
26976 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
26977 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
26978 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
26979 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
26981 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
26982 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
26984 ;;; Extra keys for tty access.
26985 ;; We only set them when really needed because otherwise the
26986 ;; menus don't show the simple keys
26988 (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
26989 (not window-system))
26990 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
26991 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
26992 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
26993 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
26994 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
26995 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
26996 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
26997 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
26998 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
26999 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
27000 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
27001 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
27002 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
27003 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
27004 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
27005 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
27006 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
27007 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
27008 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
27009 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
27010 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
27011 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
27013 ;; All the other keys
27015 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
27016 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
27017 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
27018 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
27019 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
27020 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
27021 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
27022 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
27023 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
27024 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
27025 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
27026 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
27027 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
27028 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
27029 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
27030 (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
27031 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
27032 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
27033 (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
27034 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
27035 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
27036 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
27037 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
27038 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
27039 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
27040 (org-defkey org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
27041 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
27042 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
27043 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
27044 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
27045 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
27046 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
27047 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
27048 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
27049 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
27050 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
27051 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
27052 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
27053 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
27054 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
27055 (org-defkey org-mode-map "\C-c^" 'org-sort)
27056 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
27057 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
27058 (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
27059 (org-defkey org-mode-map "\C-m" 'org-return)
27060 (org-defkey org-mode-map "\C-j" 'org-return-indent)
27061 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
27062 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
27063 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
27064 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
27065 (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
27066 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
27067 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
27068 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
27069 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
27070 (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
27071 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
27072 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
27073 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
27074 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
27075 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
27077 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
27078 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
27079 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
27080 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
27082 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
27083 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
27084 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
27085 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
27086 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
27087 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
27088 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
27089 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
27090 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
27091 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
27092 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
27093 (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
27095 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
27097 (when (featurep 'xemacs)
27098 (org-defkey org-mode-map 'button3 'popup-mode-menu))
27100 (defsubst org-table-p () (org-at-table-p))
27102 (defun org-self-insert-command (N)
27103 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
27104 If the cursor is in a table looking at whitespace, the whitespace is
27105 overwritten, and the table is not marked as requiring realignment."
27107 (if (and (org-table-p)
27109 ;; check if we blank the field, and if that triggers align
27110 (and org-table-auto-blank-field
27111 (member last-command
27112 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
27113 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
27114 ;; got extra space, this field does not determine column width
27115 (let (org-table-may-need-update) (org-table-blank-field))
27116 ;; no extra space, this field may determine column width
27117 (org-table-blank-field)))
27120 (looking-at "[^|\n]* |"))
27121 (let (org-table-may-need-update)
27122 (goto-char (1- (match-end 0)))
27123 (delete-backward-char 1)
27124 (goto-char (match-beginning 0))
27125 (self-insert-command N))
27126 (setq org-table-may-need-update t)
27127 (self-insert-command N)
27128 (org-fix-tags-on-the-fly)))
27130 (defun org-fix-tags-on-the-fly ()
27131 (when (and (equal (char-after (point-at-bol)) ?*)
27132 (org-on-heading-p))
27133 (org-align-tags-here org-tags-column)))
27135 (defun org-delete-backward-char (N)
27136 "Like `delete-backward-char', insert whitespace at field end in tables.
27137 When deleting backwards, in tables this function will insert whitespace in
27138 front of the next \"|\" separator, to keep the table aligned. The table will
27139 still be marked for re-alignment if the field did fill the entire column,
27140 because, in this case the deletion might narrow the column."
27142 (if (and (org-table-p)
27144 (string-match "|" (buffer-substring (point-at-bol) (point)))
27145 (looking-at ".*?|"))
27146 (let ((pos (point))
27147 (noalign (looking-at "[^|\n\r]* |"))
27148 (c org-table-may-need-update))
27149 (backward-delete-char N)
27150 (skip-chars-forward "^|")
27152 (goto-char (1- pos))
27153 ;; noalign: if there were two spaces at the end, this field
27154 ;; does not determine the width of the column.
27155 (if noalign (setq org-table-may-need-update c)))
27156 (backward-delete-char N)
27157 (org-fix-tags-on-the-fly)))
27159 (defun org-delete-char (N)
27160 "Like `delete-char', but insert whitespace at field end in tables.
27161 When deleting characters, in tables this function will insert whitespace in
27162 front of the next \"|\" separator, to keep the table aligned. The table will
27163 still be marked for re-alignment if the field did fill the entire column,
27164 because, in this case the deletion might narrow the column."
27166 (if (and (org-table-p)
27168 (not (= (char-after) ?|))
27170 (if (looking-at ".*?|")
27171 (let ((pos (point))
27172 (noalign (looking-at "[^|\n\r]* |"))
27173 (c org-table-may-need-update))
27174 (replace-match (concat
27175 (substring (match-string 0) 1 -1)
27178 ;; noalign: if there were two spaces at the end, this field
27179 ;; does not determine the width of the column.
27180 (if noalign (setq org-table-may-need-update c)))
27183 (org-fix-tags-on-the-fly)))
27185 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
27186 (put 'org-self-insert-command 'delete-selection t)
27187 (put 'orgtbl-self-insert-command 'delete-selection t)
27188 (put 'org-delete-char 'delete-selection 'supersede)
27189 (put 'org-delete-backward-char 'delete-selection 'supersede)
27191 ;; Make `flyspell-mode' delay after some commands
27192 (put 'org-self-insert-command 'flyspell-delayed t)
27193 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
27194 (put 'org-delete-char 'flyspell-delayed t)
27195 (put 'org-delete-backward-char 'flyspell-delayed t)
27197 ;; Make pabbrev-mode expand after org-mode commands
27198 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
27199 (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
27201 ;; How to do this: Measure non-white length of current string
27202 ;; If equal to column width, we should realign.
27204 (defun org-remap (map &rest commands)
27205 "In MAP, remap the functions given in COMMANDS.
27206 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
27209 (setq old (pop commands) new (pop commands))
27210 (if (fboundp 'command-remapping)
27211 (org-defkey map (vector 'remap old) new)
27212 (substitute-key-definition old new map global-map)))))
27214 (when (eq org-enable-table-editor 'optimized)
27215 ;; If the user wants maximum table support, we need to hijack
27216 ;; some standard editing functions
27217 (org-remap org-mode-map
27218 'self-insert-command 'org-self-insert-command
27219 'delete-char 'org-delete-char
27220 'delete-backward-char 'org-delete-backward-char)
27221 (org-defkey org-mode-map "|" 'org-force-self-insert))
27223 (defun org-shiftcursor-error ()
27224 "Throw an error because Shift-Cursor command was applied in wrong context."
27225 (error "This command is active in special context like tables, headlines or timestamps"))
27227 (defun org-shifttab (&optional arg)
27228 "Global visibility cycling or move to previous table field.
27229 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
27231 See the individual commands for more information."
27234 ((org-at-table-p) (call-interactively 'org-table-previous-field))
27235 (arg (message "Content view to level: ")
27236 (org-content (prefix-numeric-value arg))
27237 (setq org-cycle-global-status 'overview))
27238 (t (call-interactively 'org-global-cycle))))
27240 (defun org-shiftmetaleft ()
27241 "Promote subtree or delete table column.
27242 Calls `org-promote-subtree', `org-outdent-item',
27243 or `org-table-delete-column', depending on context.
27244 See the individual commands for more information."
27247 ((org-at-table-p) (call-interactively 'org-table-delete-column))
27248 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
27249 ((org-at-item-p) (call-interactively 'org-outdent-item))
27250 (t (org-shiftcursor-error))))
27252 (defun org-shiftmetaright ()
27253 "Demote subtree or insert table column.
27254 Calls `org-demote-subtree', `org-indent-item',
27255 or `org-table-insert-column', depending on context.
27256 See the individual commands for more information."
27259 ((org-at-table-p) (call-interactively 'org-table-insert-column))
27260 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
27261 ((org-at-item-p) (call-interactively 'org-indent-item))
27262 (t (org-shiftcursor-error))))
27264 (defun org-shiftmetaup (&optional arg)
27265 "Move subtree up or kill table row.
27266 Calls `org-move-subtree-up' or `org-table-kill-row' or
27267 `org-move-item-up' depending on context. See the individual commands
27268 for more information."
27271 ((org-at-table-p) (call-interactively 'org-table-kill-row))
27272 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
27273 ((org-at-item-p) (call-interactively 'org-move-item-up))
27274 (t (org-shiftcursor-error))))
27275 (defun org-shiftmetadown (&optional arg)
27276 "Move subtree down or insert table row.
27277 Calls `org-move-subtree-down' or `org-table-insert-row' or
27278 `org-move-item-down', depending on context. See the individual
27279 commands for more information."
27282 ((org-at-table-p) (call-interactively 'org-table-insert-row))
27283 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
27284 ((org-at-item-p) (call-interactively 'org-move-item-down))
27285 (t (org-shiftcursor-error))))
27287 (defun org-metaleft (&optional arg)
27288 "Promote heading or move table column to left.
27289 Calls `org-do-promote' or `org-table-move-column', depending on context.
27290 With no specific context, calls the Emacs default `backward-word'.
27291 See the individual commands for more information."
27294 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
27295 ((or (org-on-heading-p) (org-region-active-p))
27296 (call-interactively 'org-do-promote))
27297 ((org-at-item-p) (call-interactively 'org-outdent-item))
27298 (t (call-interactively 'backward-word))))
27300 (defun org-metaright (&optional arg)
27301 "Demote subtree or move table column to right.
27302 Calls `org-do-demote' or `org-table-move-column', depending on context.
27303 With no specific context, calls the Emacs default `forward-word'.
27304 See the individual commands for more information."
27307 ((org-at-table-p) (call-interactively 'org-table-move-column))
27308 ((or (org-on-heading-p) (org-region-active-p))
27309 (call-interactively 'org-do-demote))
27310 ((org-at-item-p) (call-interactively 'org-indent-item))
27311 (t (call-interactively 'forward-word))))
27313 (defun org-metaup (&optional arg)
27314 "Move subtree up or move table row up.
27315 Calls `org-move-subtree-up' or `org-table-move-row' or
27316 `org-move-item-up', depending on context. See the individual commands
27317 for more information."
27320 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
27321 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
27322 ((org-at-item-p) (call-interactively 'org-move-item-up))
27323 (t (transpose-lines 1) (beginning-of-line -1))))
27325 (defun org-metadown (&optional arg)
27326 "Move subtree down or move table row down.
27327 Calls `org-move-subtree-down' or `org-table-move-row' or
27328 `org-move-item-down', depending on context. See the individual
27329 commands for more information."
27332 ((org-at-table-p) (call-interactively 'org-table-move-row))
27333 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
27334 ((org-at-item-p) (call-interactively 'org-move-item-down))
27335 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
27337 (defun org-shiftup (&optional arg)
27338 "Increase item in timestamp or increase priority of current headline.
27339 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
27340 depending on context. See the individual commands for more information."
27343 ((org-at-timestamp-p t)
27344 (call-interactively (if org-edit-timestamp-down-means-later
27345 'org-timestamp-down 'org-timestamp-up)))
27346 ((org-on-heading-p) (call-interactively 'org-priority-up))
27347 ((org-at-item-p) (call-interactively 'org-previous-item))
27348 (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
27350 (defun org-shiftdown (&optional arg)
27351 "Decrease item in timestamp or decrease priority of current headline.
27352 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
27353 depending on context. See the individual commands for more information."
27356 ((org-at-timestamp-p t)
27357 (call-interactively (if org-edit-timestamp-down-means-later
27358 'org-timestamp-up 'org-timestamp-down)))
27359 ((org-on-heading-p) (call-interactively 'org-priority-down))
27360 (t (call-interactively 'org-next-item))))
27362 (defun org-shiftright ()
27363 "Next TODO keyword or timestamp one day later, depending on context."
27366 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
27367 ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
27368 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
27369 ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
27370 (t (org-shiftcursor-error))))
27372 (defun org-shiftleft ()
27373 "Previous TODO keyword or timestamp one day earlier, depending on context."
27376 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
27377 ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
27378 ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
27379 ((org-at-property-p)
27380 (call-interactively 'org-property-previous-allowed-value))
27381 (t (org-shiftcursor-error))))
27383 (defun org-shiftcontrolright ()
27384 "Switch to next TODO set."
27387 ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
27388 (t (org-shiftcursor-error))))
27390 (defun org-shiftcontrolleft ()
27391 "Switch to previous TODO set."
27394 ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
27395 (t (org-shiftcursor-error))))
27397 (defun org-ctrl-c-ret ()
27398 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
27401 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
27402 (t (call-interactively 'org-insert-heading))))
27404 (defun org-copy-special ()
27405 "Copy region in table or copy current subtree.
27406 Calls `org-table-copy' or `org-copy-subtree', depending on context.
27407 See the individual commands for more information."
27409 (call-interactively
27410 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
27412 (defun org-cut-special ()
27413 "Cut region in table or cut current subtree.
27414 Calls `org-table-copy' or `org-cut-subtree', depending on context.
27415 See the individual commands for more information."
27417 (call-interactively
27418 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
27420 (defun org-paste-special (arg)
27421 "Paste rectangular region into table, or past subtree relative to level.
27422 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
27423 See the individual commands for more information."
27425 (if (org-at-table-p)
27426 (org-table-paste-rectangle)
27427 (org-paste-subtree arg)))
27429 (defun org-ctrl-c-ctrl-c (&optional arg)
27430 "Set tags in headline, or update according to changed information at point.
27432 This command does many different things, depending on context:
27434 - If the cursor is in a headline, prompt for tags and insert them
27435 into the current line, aligned to `org-tags-column'. When called
27436 with prefix arg, realign all tags in the current buffer.
27438 - If the cursor is in one of the special #+KEYWORD lines, this
27439 triggers scanning the buffer for these lines and updating the
27442 - If the cursor is inside a table, realign the table. This command
27443 works even if the automatic table editor has been turned off.
27445 - If the cursor is on a #+TBLFM line, re-apply the formulas to
27448 - If the cursor is a the beginning of a dynamic block, update it.
27450 - If the cursor is inside a table created by the table.el package,
27451 activate that table.
27453 - If the current buffer is a remember buffer, close note and file it.
27454 with a prefix argument, file it without further interaction to the default
27457 - If the cursor is on a <<<target>>>, update radio targets and corresponding
27458 links in this buffer.
27460 - If the cursor is on a numbered item in a plain list, renumber the
27463 - If the cursor is on a checkbox, toggle it."
27465 (let ((org-enable-table-editor t))
27467 ((or org-clock-overlays
27468 org-occur-highlights
27469 org-latex-fragment-image-overlays)
27470 (org-remove-clock-overlays)
27471 (org-remove-occur-highlights)
27472 (org-remove-latex-fragment-image-overlays)
27473 (message "Temporary highlights/overlays removed from current buffer"))
27474 ((and (local-variable-p 'org-finish-function (current-buffer))
27475 (fboundp org-finish-function))
27476 (funcall org-finish-function))
27477 ((org-at-property-p)
27478 (call-interactively 'org-property-action))
27479 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
27480 ((org-on-heading-p) (call-interactively 'org-set-tags))
27481 ((org-at-table.el-p)
27483 (beginning-of-line 1)
27484 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
27485 (call-interactively 'table-recognize-table))
27487 (org-table-maybe-eval-formula)
27489 (call-interactively 'org-table-recalculate)
27490 (org-table-maybe-recalculate-line))
27491 (call-interactively 'org-table-align))
27492 ((org-at-item-checkbox-p)
27493 (call-interactively 'org-toggle-checkbox))
27495 (call-interactively 'org-maybe-renumber-ordered-list))
27496 ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
27498 (beginning-of-line 1)
27499 (org-update-dblock))
27500 ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
27502 ((equal (match-string 1) "TBLFM")
27503 ;; Recalculate the table before this line
27505 (beginning-of-line 1)
27506 (skip-chars-backward " \r\n\t")
27507 (if (org-at-table-p)
27508 (org-call-with-arg 'org-table-recalculate t))))
27510 (call-interactively 'org-mode-restart))))
27511 (t (error "C-c C-c can do nothing useful at this location.")))))
27513 (defun org-mode-restart ()
27514 "Restart Org-mode, to scan again for special lines.
27515 Also updates the keyword regular expressions."
27517 (let ((org-inhibit-startup t)) (org-mode))
27518 (message "Org-mode restarted to refresh keyword and special line setup"))
27520 (defun org-kill-note-or-show-branches ()
27521 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
27523 (if (not org-finish-function)
27524 (call-interactively 'show-branches)
27525 (let ((org-note-abort t))
27526 (funcall org-finish-function))))
27528 (defun org-return (&optional indent)
27529 "Goto next table row or insert a newline.
27530 Calls `org-table-next-row' or `newline', depending on context.
27531 See the individual commands for more information."
27534 ((bobp) (if indent (newline-and-indent) (newline)))
27535 ((and (org-at-heading-p)
27537 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
27542 (org-table-justify-field-maybe)
27543 (call-interactively 'org-table-next-row))
27544 (t (if indent (newline-and-indent) (newline)))))
27546 (defun org-return-indent ()
27547 "Goto next table row or insert a newline and indent.
27548 Calls `org-table-next-row' or `newline-and-indent', depending on
27549 context. See the individual commands for more information."
27553 (defun org-ctrl-c-star ()
27554 "Compute table, or change heading status of lines.
27555 Calls `org-table-recalculate' or `org-toggle-region-headlines',
27556 depending on context. This will also turn a plain list item or a normal
27557 line into a subheading."
27561 (call-interactively 'org-table-recalculate))
27562 ((org-region-active-p)
27563 ;; Convert all lines in region to list items
27564 (call-interactively 'org-toggle-region-headings))
27565 ((org-on-heading-p)
27566 (org-toggle-region-headings (point-at-bol)
27567 (min (1+ (point-at-eol)) (point-max))))
27569 ;; Convert to heading
27570 ;; FIXME: not yet implemented
27572 (t (org-toggle-region-headings (point-at-bol)
27573 (min (1+ (point-at-eol)) (point-max))))))
27575 (defun org-ctrl-c-minus ()
27576 "Insert separator line in table or modify bullet status of line.
27577 Also turns a plain line or a region of lines into list items.
27578 Calls `org-table-insert-hline', `org-toggle-region-items', or
27579 `org-cycle-list-bullet', depending on context."
27583 (call-interactively 'org-table-insert-hline))
27584 ((org-on-heading-p)
27587 (beginning-of-line 1)
27588 (if (looking-at "\\*+ ")
27589 (replace-match (concat (make-string (- (match-end 0) (point)) ?\ ) "- ")))))
27590 ((org-region-active-p)
27591 ;; Convert all lines in region to list items
27592 (call-interactively 'org-toggle-region-items))
27594 (call-interactively 'org-cycle-list-bullet))
27595 (t (org-toggle-region-items (point-at-bol)
27596 (min (1+ (point-at-eol)) (point-max))))))
27598 (defun org-toggle-region-items (beg end)
27599 "Convert all lines in region to list items.
27600 If the first line is already an item, convert all list items in the region
27606 (setq l2 (org-current-line))
27608 (beginning-of-line 1)
27609 (setq l (1- (org-current-line)))
27610 (if (org-at-item-p)
27611 ;; We already have items, de-itemize
27612 (while (< (setq l (1+ l)) l2)
27613 (when (org-at-item-p)
27614 (goto-char (match-beginning 2))
27615 (delete-region (match-beginning 2) (match-end 2))
27616 (and (looking-at "[ \t]+") (replace-match "")))
27617 (beginning-of-line 2))
27618 (while (< (setq l (1+ l)) l2)
27619 (unless (org-at-item-p)
27620 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
27621 (replace-match "\\1- \\2")))
27622 (beginning-of-line 2))))))
27624 (defun org-toggle-region-headings (beg end)
27625 "Convert all lines in region to list items.
27626 If the first line is already an item, convert all list items in the region
27632 (setq l2 (org-current-line))
27634 (beginning-of-line 1)
27635 (setq l (1- (org-current-line)))
27636 (if (org-on-heading-p)
27637 ;; We already have headlines, de-star them
27638 (while (< (setq l (1+ l)) l2)
27639 (when (org-on-heading-p t)
27640 (and (looking-at outline-regexp) (replace-match "")))
27641 (beginning-of-line 2))
27642 (let* ((stars (save-excursion
27643 (re-search-backward org-complex-heading-regexp nil t)
27644 (or (match-string 1) "*")))
27645 (add-stars (if org-odd-levels-only "**" "*"))
27646 (rpl (concat stars add-stars " \\2")))
27647 (while (< (setq l (1+ l)) l2)
27648 (unless (org-on-heading-p)
27649 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
27650 (replace-match rpl)))
27651 (beginning-of-line 2)))))))
27653 (defun org-meta-return (&optional arg)
27654 "Insert a new heading or wrap a region in a table.
27655 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
27656 See the individual commands for more information."
27660 (call-interactively 'org-table-wrap-region))
27661 (t (call-interactively 'org-insert-heading))))
27665 ;; Define the Org-mode menus
27666 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
27668 ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
27669 ["Next Field" org-cycle (org-at-table-p)]
27670 ["Previous Field" org-shifttab (org-at-table-p)]
27671 ["Next Row" org-return (org-at-table-p)]
27673 ["Blank Field" org-table-blank-field (org-at-table-p)]
27674 ["Edit Field" org-table-edit-field (org-at-table-p)]
27675 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
27678 ["Move Column Left" org-metaleft (org-at-table-p)]
27679 ["Move Column Right" org-metaright (org-at-table-p)]
27680 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
27681 ["Insert Column" org-shiftmetaright (org-at-table-p)])
27683 ["Move Row Up" org-metaup (org-at-table-p)]
27684 ["Move Row Down" org-metadown (org-at-table-p)]
27685 ["Delete Row" org-shiftmetaup (org-at-table-p)]
27686 ["Insert Row" org-shiftmetadown (org-at-table-p)]
27687 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
27689 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
27691 ["Copy Rectangle" org-copy-special (org-at-table-p)]
27692 ["Cut Rectangle" org-cut-special (org-at-table-p)]
27693 ["Paste Rectangle" org-paste-special (org-at-table-p)]
27694 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
27697 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
27698 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
27699 ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
27701 ["Recalculate line" org-table-recalculate (org-at-table-p)]
27702 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
27703 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
27705 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
27707 ["Sum Column/Rectangle" org-table-sum
27708 (or (org-at-table-p) (org-region-active-p))]
27709 ["Which Column?" org-table-current-column (org-at-table-p)])
27711 org-table-toggle-formula-debugger
27712 :style toggle :selected org-table-formula-debug]
27713 ["Show Col/Row Numbers"
27714 org-table-toggle-coordinate-overlays
27715 :style toggle :selected org-table-overlay-coordinates]
27717 ["Create" org-table-create (and (not (org-at-table-p))
27718 org-enable-table-editor)]
27719 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
27720 ["Import from File" org-table-import (not (org-at-table-p))]
27721 ["Export to File" org-table-export (org-at-table-p)]
27723 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
27725 (easy-menu-define org-org-menu org-mode-map "Org menu"
27728 ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
27729 ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
27730 ["Sparse Tree" org-occur t]
27731 ["Reveal Context" org-reveal t]
27732 ["Show All" show-all t]
27734 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
27736 ["New Heading" org-insert-heading t]
27737 ("Navigate Headings"
27738 ["Up" outline-up-heading t]
27739 ["Next" outline-next-visible-heading t]
27740 ["Previous" outline-previous-visible-heading t]
27741 ["Next Same Level" outline-forward-same-level t]
27742 ["Previous Same Level" outline-backward-same-level t]
27744 ["Jump" org-goto t])
27746 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
27747 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
27749 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
27750 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
27751 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
27753 ["Promote Heading" org-metaleft (not (org-at-table-p))]
27754 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
27755 ["Demote Heading" org-metaright (not (org-at-table-p))]
27756 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
27758 ["Sort Region/Children" org-sort (not (org-at-table-p))]
27760 ["Convert to odd levels" org-convert-to-odd-levels t]
27761 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
27763 ["Emphasis..." org-emphasize t])
27765 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
27766 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
27767 ; :active t :keys "C-u C-c C-x C-a"]
27768 ["Sparse trees open ARCHIVE trees"
27769 (setq org-sparse-tree-open-archived-trees
27770 (not org-sparse-tree-open-archived-trees))
27771 :style toggle :selected org-sparse-tree-open-archived-trees]
27772 ["Cycling opens ARCHIVE trees"
27773 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
27774 :style toggle :selected org-cycle-open-archived-trees]
27775 ["Agenda includes ARCHIVE trees"
27776 (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
27777 :style toggle :selected (not org-agenda-skip-archived-trees)]
27779 ["Move Subtree to Archive" org-advertized-archive-subtree t]
27780 ; ["Check and Move Children" (org-archive-subtree '(4))
27781 ; :active t :keys "C-u C-c C-x C-s"]
27785 ["TODO/DONE/-" org-todo t]
27787 ["Next keyword" org-shiftright (org-on-heading-p)]
27788 ["Previous keyword" org-shiftleft (org-on-heading-p)]
27789 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
27790 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
27791 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
27792 ["Show TODO Tree" org-show-todo-tree t]
27793 ["Global TODO list" org-todo-list t]
27795 ["Set Priority" org-priority t]
27796 ["Priority Up" org-shiftup t]
27797 ["Priority Down" org-shiftdown t])
27798 ("TAGS and Properties"
27799 ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
27800 ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
27802 ["Set property" 'org-set-property t]
27803 ["Column view of properties" org-columns t]
27804 ["Insert Column View DBlock" org-insert-columns-dblock t])
27805 ("Dates and Scheduling"
27806 ["Timestamp" org-time-stamp t]
27807 ["Timestamp (inactive)" org-time-stamp-inactive t]
27809 ["1 Day Later" org-shiftright t]
27810 ["1 Day Earlier" org-shiftleft t]
27811 ["1 ... Later" org-shiftup t]
27812 ["1 ... Earlier" org-shiftdown t])
27813 ["Compute Time Range" org-evaluate-time-range t]
27814 ["Schedule Item" org-schedule t]
27815 ["Deadline" org-deadline t]
27817 ["Custom time format" org-toggle-time-stamp-overlays
27818 :style radio :selected org-display-custom-times]
27820 ["Goto Calendar" org-goto-calendar t]
27821 ["Date from Calendar" org-date-from-calendar t])
27823 ["Clock in" org-clock-in t]
27824 ["Clock out" org-clock-out t]
27825 ["Clock cancel" org-clock-cancel t]
27826 ["Goto running clock" org-clock-goto t]
27827 ["Display times" org-clock-display t]
27828 ["Create clock table" org-clock-report t]
27830 ["Record DONE time"
27831 (progn (setq org-log-done (not org-log-done))
27832 (message "Switching to %s will %s record a timestamp"
27833 (car org-done-keywords)
27834 (if org-log-done "automatically" "not")))
27835 :style toggle :selected org-log-done])
27837 ["Agenda Command..." org-agenda t]
27838 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
27839 ("File List for Agenda")
27840 ("Special views current file"
27841 ["TODO Tree" org-show-todo-tree t]
27842 ["Check Deadlines" org-check-deadlines t]
27843 ["Timeline" org-timeline t]
27844 ["Tags Tree" org-tags-sparse-tree t])
27847 ["Store Link (Global)" org-store-link t]
27848 ["Insert Link" org-insert-link t]
27849 ["Follow Link" org-open-at-point t]
27851 ["Next link" org-next-link t]
27852 ["Previous link" org-previous-link t]
27854 ["Descriptive Links"
27855 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
27856 :style radio :selected (member '(org-link) buffer-invisibility-spec)]
27859 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
27860 :style radio :selected (not (member '(org-link) buffer-invisibility-spec))])
27862 ["Export/Publish..." org-export t]
27864 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
27865 :selected org-cdlatex-mode]
27866 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
27867 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
27868 ["Modify math symbol" org-cdlatex-math-modify
27869 (org-inside-LaTeX-fragment-p)]
27870 ["Export LaTeX fragments as images"
27871 (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
27872 :style toggle :selected org-export-with-LaTeX-fragments])
27875 ["Show Version" org-version t]
27876 ["Info Documentation" org-info t])
27878 ["Browse Org Group" org-customize t]
27880 ["Expand This Menu" org-create-customize-menu
27881 (fboundp 'customize-menu-create)])
27883 ["Refresh setup" org-mode-restart t]
27886 (defun org-info (&optional node)
27887 "Read documentation for Org-mode in the info system.
27888 With optional NODE, go directly to that node."
27890 (info (format "(org)%s" (or node ""))))
27892 (defun org-install-agenda-files-menu ()
27893 (let ((bl (buffer-list)))
27896 (set-buffer (pop bl))
27897 (if (org-mode-p) (setq bl nil)))
27900 '("Org") "File List for Agenda"
27903 ["Edit File List" (org-edit-agenda-file-list) t]
27904 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
27905 ["Remove Current File from List" org-remove-file t]
27906 ["Cycle through agenda files" org-cycle-agenda-files t]
27907 ["Occur in all agenda files" org-occur-in-agenda-files t]
27909 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
27913 (defun org-customize ()
27914 "Call the customize function with org as argument."
27916 (customize-browse 'org))
27918 (defun org-create-customize-menu ()
27919 "Create a full customization menu for Org-mode, insert it into the menu."
27921 (if (fboundp 'customize-menu-create)
27924 '("Org") "Customize"
27925 `(["Browse Org group" org-customize t]
27927 ,(customize-menu-create 'org)
27928 ["Set" Custom-set t]
27929 ["Save" Custom-save t]
27930 ["Reset to Current" Custom-reset-current t]
27931 ["Reset to Saved" Custom-reset-saved t]
27932 ["Reset to Standard Settings" Custom-reset-standard t]))
27933 (message "\"Org\"-menu now contains full customization menu"))
27934 (error "Cannot expand menu (outdated version of cus-edit.el)")))
27936 ;;;; Miscellaneous stuff
27939 ;;; Generally useful functions
27941 (defun org-context ()
27942 "Return a list of contexts of the current cursor position.
27943 If several contexts apply, all are returned.
27944 Each context entry is a list with a symbol naming the context, and
27945 two positions indicating start and end of the context. Possible
27948 :headline anywhere in a headline
27949 :headline-stars on the leading stars in a headline
27950 :todo-keyword on a TODO keyword (including DONE) in a headline
27951 :tags on the TAGS in a headline
27952 :priority on the priority cookie in a headline
27953 :item on the first line of a plain list item
27954 :item-bullet on the bullet/number of a plain list item
27955 :checkbox on the checkbox in a plain list item
27956 :table in an org-mode table
27957 :table-special on a special filed in a table
27958 :table-table in a table.el table
27959 :link on a hyperlink
27960 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
27961 :target on a <<target>>
27962 :radio-target on a <<<radio-target>>>
27963 :latex-fragment on a LaTeX fragment
27964 :latex-preview on a LaTeX fragment with overlayed preview image
27966 This function expects the position to be visible because it uses font-lock
27967 faces as a help to recognize the following contexts: :table-special, :link,
27969 (let* ((f (get-text-property (point) 'face))
27970 (faces (if (listp f) f (list f)))
27971 (p (point)) clist o)
27972 ;; First the large context
27974 ((org-on-heading-p t)
27975 (push (list :headline (point-at-bol) (point-at-eol)) clist)
27977 (beginning-of-line 1)
27978 (looking-at org-todo-line-tags-regexp))
27979 (push (org-point-in-group p 1 :headline-stars) clist)
27980 (push (org-point-in-group p 2 :todo-keyword) clist)
27981 (push (org-point-in-group p 4 :tags) clist))
27983 (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
27984 (if (looking-at "\\[#[A-Z0-9]\\]")
27985 (push (org-point-in-group p 0 :priority) clist)))
27988 (push (org-point-in-group p 2 :item-bullet) clist)
27989 (push (list :item (point-at-bol)
27990 (save-excursion (org-end-of-item) (point)))
27992 (and (org-at-item-checkbox-p)
27993 (push (org-point-in-group p 0 :checkbox) clist)))
27996 (push (list :table (org-table-begin) (org-table-end)) clist)
27997 (if (memq 'org-formula faces)
27998 (push (list :table-special
27999 (previous-single-property-change p 'face)
28000 (next-single-property-change p 'face)) clist)))
28001 ((org-at-table-p 'any)
28002 (push (list :table-table) clist)))
28005 ;; Now the small context
28007 ((org-at-timestamp-p)
28008 (push (org-point-in-group p 0 :timestamp) clist))
28009 ((memq 'org-link faces)
28011 (previous-single-property-change p 'face)
28012 (next-single-property-change p 'face)) clist))
28013 ((memq 'org-special-keyword faces)
28014 (push (list :keyword
28015 (previous-single-property-change p 'face)
28016 (next-single-property-change p 'face)) clist))
28018 (push (org-point-in-group p 0 :target) clist)
28019 (goto-char (1- (match-beginning 0)))
28020 (if (looking-at org-radio-target-regexp)
28021 (push (org-point-in-group p 0 :radio-target) clist))
28023 ((setq o (car (delq nil
28026 (if (memq x org-latex-fragment-image-overlays) x))
28027 (org-overlays-at (point))))))
28028 (push (list :latex-fragment
28029 (org-overlay-start o) (org-overlay-end o)) clist)
28030 (push (list :latex-preview
28031 (org-overlay-start o) (org-overlay-end o)) clist))
28032 ((org-inside-LaTeX-fragment-p)
28033 ;; FIXME: positions wrong.
28034 (push (list :latex-fragment (point) (point)) clist)))
28036 (setq clist (nreverse (delq nil clist)))
28039 ;; FIXME: Compare with at-regexp-p Do we need both?
28040 (defun org-in-regexp (re &optional nlines visually)
28041 "Check if point is inside a match of regexp.
28042 Normally only the current line is checked, but you can include NLINES extra
28043 lines both before and after point into the search.
28044 If VISUALLY is set, require that the cursor is not after the match but
28045 really on, so that the block visually is on the match."
28047 (let ((pos (point))
28048 (eol (point-at-eol (+ 1 (or nlines 0))))
28049 (inc (if visually 1 0)))
28051 (beginning-of-line (- 1 (or nlines 0)))
28052 (while (re-search-forward re eol t)
28053 (if (and (<= (match-beginning 0) pos)
28054 (>= (+ inc (match-end 0)) pos))
28055 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
28057 (defun org-at-regexp-p (regexp)
28058 "Is point inside a match of REGEXP in the current line?"
28061 (let ((pos (point)) (end (point-at-eol)))
28062 (beginning-of-line 1)
28063 (while (re-search-forward regexp end t)
28064 (if (and (<= (match-beginning 0) pos)
28065 (>= (match-end 0) pos))
28069 (defun org-occur-in-agenda-files (regexp &optional nlines)
28070 "Call `multi-occur' with buffers for all agenda files."
28071 (interactive "sOrg-files matching: \np")
28072 (let* ((files (org-agenda-files))
28073 (tnames (mapcar 'file-truename files))
28074 (extra org-agenda-text-search-extra-files)
28076 (while (setq f (pop extra))
28077 (unless (member (file-truename f) tnames)
28078 (add-to-list 'files f 'append)
28079 (add-to-list 'tnames (file-truename f) 'append)))
28081 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
28084 (if (boundp 'occur-mode-find-occurrence-hook)
28086 (add-hook 'occur-mode-find-occurrence-hook
28091 (defadvice occur-mode-goto-occurrence
28092 (after org-occur-reveal activate)
28093 (and (org-mode-p) (org-reveal)))
28094 (defadvice occur-mode-goto-occurrence-other-window
28095 (after org-occur-reveal activate)
28096 (and (org-mode-p) (org-reveal)))
28097 (defadvice occur-mode-display-occurrence
28098 (after org-occur-reveal activate)
28100 (let ((pos (occur-mode-find-occurrence)))
28101 (with-current-buffer (marker-buffer pos)
28106 (defun org-uniquify (list)
28107 "Remove duplicate elements from LIST."
28109 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
28112 (defun org-delete-all (elts list)
28113 "Remove all elements in ELTS from LIST."
28115 (setq list (delete (pop elts) list)))
28118 (defun org-back-over-empty-lines ()
28119 "Move backwards over witespace, to the beginning of the first empty line.
28120 Returns the number o empty lines passed."
28121 (let ((pos (point)))
28122 (skip-chars-backward " \t\n\r")
28123 (beginning-of-line 2)
28124 (goto-char (min (point) pos))
28125 (count-lines (point) pos)))
28127 (defun org-skip-whitespace ()
28128 (skip-chars-forward " \t\n\r"))
28130 (defun org-point-in-group (point group &optional context)
28131 "Check if POINT is in match-group GROUP.
28132 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
28133 match. If the match group does ot exist or point is not inside it,
28135 (and (match-beginning group)
28136 (>= point (match-beginning group))
28137 (<= point (match-end group))
28139 (list context (match-beginning group) (match-end group))
28142 (defun org-switch-to-buffer-other-window (&rest args)
28143 "Switch to buffer in a second window on the current frame.
28144 In particular, do not allow pop-up frames."
28145 (let (pop-up-frames special-display-buffer-names special-display-regexps
28146 special-display-function)
28147 (apply 'switch-to-buffer-other-window args)))
28149 (defun org-combine-plists (&rest plists)
28150 "Create a single property list from all plists in PLISTS.
28151 The process starts by copying the first list, and then setting properties
28152 from the other lists. Settings in the last list are the most significant
28153 ones and overrule settings in the other lists."
28154 (let ((rtn (copy-sequence (pop plists)))
28157 (setq ls (pop plists))
28159 (setq p (pop ls) v (pop ls))
28160 (setq rtn (plist-put rtn p v))))
28163 (defun org-move-line-down (arg)
28164 "Move the current line down. With prefix argument, move it past ARG lines."
28166 (let ((col (current-column))
28168 (beginning-of-line 1) (setq beg (point))
28169 (beginning-of-line 2) (setq end (point))
28170 (beginning-of-line (+ 1 arg))
28171 (setq pos (move-marker (make-marker) (point)))
28172 (insert (delete-and-extract-region beg end))
28174 (move-to-column col)))
28176 (defun org-move-line-up (arg)
28177 "Move the current line up. With prefix argument, move it past ARG lines."
28179 (let ((col (current-column))
28181 (beginning-of-line 1) (setq beg (point))
28182 (beginning-of-line 2) (setq end (point))
28183 (beginning-of-line (- arg))
28184 (setq pos (move-marker (make-marker) (point)))
28185 (insert (delete-and-extract-region beg end))
28187 (move-to-column col)))
28189 (defun org-replace-escapes (string table)
28190 "Replace %-escapes in STRING with values in TABLE.
28191 TABLE is an association list with keys like \"%a\" and string values.
28192 The sequences in STRING may contain normal field width and padding information,
28193 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
28194 so values can contain further %-escapes if they are define later in TABLE."
28195 (let ((case-fold-search nil)
28197 (while (setq e (pop table))
28198 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
28199 (while (string-match re string)
28200 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
28202 (setq string (replace-match rpl t t string))))
28206 (defun org-sublist (list start end)
28207 "Return a section of LIST, from START to END.
28208 Counting starts at 1."
28209 (let (rtn (c start))
28210 (setq list (nthcdr (1- start) list))
28211 (while (and list (<= c end))
28212 (push (pop list) rtn)
28216 (defun org-find-base-buffer-visiting (file)
28217 "Like `find-buffer-visiting' but alway return the base buffer and
28218 not an indirect buffer"
28219 (let ((buf (find-buffer-visiting file)))
28221 (or (buffer-base-buffer buf) buf)
28224 (defun org-image-file-name-regexp ()
28225 "Return regexp matching the file names of images."
28226 (if (fboundp 'image-file-name-regexp)
28227 (image-file-name-regexp)
28228 (let ((image-file-name-extensions
28229 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
28230 "xbm" "xpm" "pbm" "pgm" "ppm")))
28232 (regexp-opt (nconc (mapcar 'upcase
28233 image-file-name-extensions)
28234 image-file-name-extensions)
28238 (defun org-file-image-p (file)
28239 "Return non-nil if FILE is an image."
28241 (string-match (org-image-file-name-regexp) file)))
28243 ;;; Paragraph filling stuff.
28244 ;; We want this to be just right, so use the full arsenal.
28246 (defun org-indent-line-function ()
28247 "Indent line like previous, but further if previous was headline or item."
28249 (let* ((pos (point))
28250 (itemp (org-at-item-p))
28251 column bpos bcol tpos tcol bullet btype bullet-type)
28252 ;; Find the previous relevant line
28253 (beginning-of-line 1)
28255 ((looking-at "#") (setq column 0))
28256 ((looking-at "\\*+ ") (setq column 0))
28258 (beginning-of-line 0)
28259 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
28260 (beginning-of-line 0))
28262 ((looking-at "\\*+[ \t]+")
28263 (goto-char (match-end 0))
28264 (setq column (current-column)))
28266 (org-beginning-of-item)
28267 ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
28268 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
28269 (setq bpos (match-beginning 1) tpos (match-end 0)
28270 bcol (progn (goto-char bpos) (current-column))
28271 tcol (progn (goto-char tpos) (current-column))
28272 bullet (match-string 1)
28273 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
28277 (beginning-of-line 1)
28278 (if (looking-at "\\S-")
28280 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
28281 (setq bullet (match-string 1)
28282 btype (if (string-match "[0-9]" bullet) "n" bullet))
28283 (setq column (if (equal btype bullet-type) bcol tcol)))
28284 (setq column (org-get-indentation)))))
28285 (t (setq column (org-get-indentation))))))
28287 (if (<= (current-column) (current-indentation))
28288 (indent-line-to column)
28289 (save-excursion (indent-line-to column)))
28290 (setq column (current-column))
28291 (beginning-of-line 1)
28293 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
28294 (replace-match (concat "\\1" (format org-property-format
28295 (match-string 2) (match-string 3)))
28297 (move-to-column column)))
28299 (defun org-set-autofill-regexps ()
28301 ;; In the paragraph separator we include headlines, because filling
28302 ;; text in a line directly attached to a headline would otherwise
28303 ;; fill the headline as well.
28304 (org-set-local 'comment-start-skip "^#+[ \t]*")
28305 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
28306 ;; The paragraph starter includes hand-formatted lists.
28307 (org-set-local 'paragraph-start
28308 "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
28309 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
28310 ;; But only if the user has not turned off tables or fixed-width regions
28312 'auto-fill-inhibit-regexp
28313 (concat "\\*+ \\|#\\+"
28314 "\\|[ \t]*" org-keyword-time-regexp
28315 (if (or org-enable-table-editor org-enable-fixed-width-editor)
28318 (if org-enable-table-editor "|" "")
28319 (if org-enable-fixed-width-editor ":" "")
28321 ;; We use our own fill-paragraph function, to make sure that tables
28322 ;; and fixed-width regions are not wrapped. That function will pass
28323 ;; through to `fill-paragraph' when appropriate.
28324 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
28325 ; Adaptive filling: To get full control, first make sure that
28326 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
28327 (org-set-local 'adaptive-fill-regexp "\000")
28328 (org-set-local 'adaptive-fill-function
28329 'org-adaptive-fill-function)
28331 'align-mode-rules-list
28332 '((org-in-buffer-settings
28333 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
28334 (modes . '(org-mode))))))
28336 (defun org-fill-paragraph (&optional justify)
28337 "Re-align a table, pass through to fill-paragraph if no table."
28338 (let ((table-p (org-at-table-p))
28339 (table.el-p (org-at-table.el-p)))
28340 (cond ((and (equal (char-after (point-at-bol)) ?*)
28341 (save-excursion (goto-char (point-at-bol))
28342 (looking-at outline-regexp)))
28343 t) ; skip headlines
28344 (table.el-p t) ; skip table.el tables
28345 (table-p (org-table-align) t) ; align org-mode tables
28346 (t nil)))) ; call paragraph-fill
28348 ;; For reference, this is the default value of adaptive-fill-regexp
28349 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
28351 (defun org-adaptive-fill-function ()
28352 "Return a fill prefix for org-mode files.
28353 In particular, this makes sure hanging paragraphs for hand-formatted lists
28355 (cond ((looking-at "#[ \t]+")
28357 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
28359 (goto-char (match-end 0))
28360 (make-string (current-column) ?\ )))
28363 ;;;; Functions extending outline functionality
28366 (defun org-beginning-of-line (&optional arg)
28367 "Go to the beginning of the current line. If that is invisible, continue
28368 to a visible line beginning. This makes the function of C-a more intuitive.
28369 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
28370 first attempt, and only move to after the tags when the cursor is already
28371 beyond the end of the headline."
28373 (let ((pos (point)))
28374 (beginning-of-line 1)
28378 (if (org-invisible-p)
28379 (while (and (not (bobp)) (org-invisible-p))
28381 (beginning-of-line 1))
28383 (when org-special-ctrl-a/e
28385 ((and (looking-at org-todo-line-regexp)
28386 (= (char-after (match-end 1)) ?\ ))
28388 (if (eq org-special-ctrl-a/e t)
28389 (cond ((> pos (match-beginning 3)) (match-beginning 3))
28390 ((= pos (point)) (match-beginning 3))
28392 (cond ((> pos (point)) (point))
28393 ((not (eq last-command this-command)) (point))
28394 (t (match-beginning 3))))))
28397 (if (eq org-special-ctrl-a/e t)
28398 (cond ((> pos (match-end 4)) (match-end 4))
28399 ((= pos (point)) (match-end 4))
28401 (cond ((> pos (point)) (point))
28402 ((not (eq last-command this-command)) (point))
28403 (t (match-end 4))))))))))
28405 (defun org-end-of-line (&optional arg)
28406 "Go to the end of the line.
28407 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
28408 first attempt, and only move to after the tags when the cursor is already
28409 beyond the end of the headline."
28411 (if (or (not org-special-ctrl-a/e)
28412 (not (org-on-heading-p)))
28414 (let ((pos (point)))
28415 (beginning-of-line 1)
28416 (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
28417 (if (eq org-special-ctrl-a/e t)
28418 (if (or (< pos (match-beginning 1))
28419 (= pos (match-end 0)))
28420 (goto-char (match-beginning 1))
28421 (goto-char (match-end 0)))
28422 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
28423 (goto-char (match-end 0))
28424 (goto-char (match-beginning 1))))
28425 (end-of-line arg)))))
28427 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
28428 (define-key org-mode-map "\C-e" 'org-end-of-line)
28430 (defun org-kill-line (&optional arg)
28431 "Kill line, to tags or end of line."
28434 ((or (not org-special-ctrl-k)
28436 (not (org-on-heading-p)))
28437 (call-interactively 'kill-line))
28438 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
28439 (kill-region (point) (match-beginning 1))
28440 (org-set-tags nil t))
28441 (t (kill-region (point) (point-at-eol)))))
28443 (define-key org-mode-map "\C-k" 'org-kill-line)
28445 (defun org-invisible-p ()
28446 "Check if point is at a character currently not visible."
28447 ;; Early versions of noutline don't have `outline-invisible-p'.
28448 (if (fboundp 'outline-invisible-p)
28449 (outline-invisible-p)
28450 (get-char-property (point) 'invisible)))
28452 (defun org-invisible-p2 ()
28453 "Check if point is at a character currently not visible."
28455 (if (and (eolp) (not (bobp))) (backward-char 1))
28456 ;; Early versions of noutline don't have `outline-invisible-p'.
28457 (if (fboundp 'outline-invisible-p)
28458 (outline-invisible-p)
28459 (get-char-property (point) 'invisible))))
28461 (defalias 'org-back-to-heading 'outline-back-to-heading)
28462 (defalias 'org-on-heading-p 'outline-on-heading-p)
28463 (defalias 'org-at-heading-p 'outline-on-heading-p)
28464 (defun org-at-heading-or-item-p ()
28465 (or (org-on-heading-p) (org-at-item-p)))
28467 (defun org-on-target-p ()
28468 (or (org-in-regexp org-radio-target-regexp)
28469 (org-in-regexp org-target-regexp)))
28471 (defun org-up-heading-all (arg)
28472 "Move to the heading line of which the present line is a subheading.
28473 This function considers both visible and invisible heading lines.
28474 With argument, move up ARG levels."
28475 (if (fboundp 'outline-up-heading-all)
28476 (outline-up-heading-all arg) ; emacs 21 version of outline.el
28477 (outline-up-heading arg t))) ; emacs 22 version of outline.el
28479 (defun org-up-heading-safe ()
28480 "Move to the heading line of which the present line is a subheading.
28481 This version will not throw an error. It will return the level of the
28482 headline found, or nil if no higher level is found."
28483 (let ((pos (point)) start-level level
28484 (re (concat "^" outline-regexp)))
28486 (outline-back-to-heading t)
28487 (setq start-level (funcall outline-level))
28488 (if (equal start-level 1) (throw 'exit nil))
28489 (while (re-search-backward re nil t)
28490 (setq level (funcall outline-level))
28491 (if (< level start-level) (throw 'exit level)))
28494 (defun org-first-sibling-p ()
28495 "Is this heading the first child of its parents?"
28497 (let ((re (concat "^" outline-regexp))
28499 (unless (org-at-heading-p t)
28500 (error "Not at a heading"))
28501 (setq level (funcall outline-level))
28503 (if (not (re-search-backward re nil t))
28505 (setq l (funcall outline-level))
28508 (defun org-goto-sibling (&optional previous)
28509 "Goto the next sibling, even if it is invisible.
28510 When PREVIOUS is set, go to the previous sibling instead. Returns t
28511 when a sibling was found. When none is found, return nil and don't
28513 (let ((fun (if previous 're-search-backward 're-search-forward))
28515 (re (concat "^" outline-regexp))
28517 (when (condition-case nil (org-back-to-heading t) (error nil))
28518 (setq level (funcall outline-level))
28520 (or previous (forward-char 1))
28521 (while (funcall fun re nil t)
28522 (setq l (funcall outline-level))
28523 (when (< l level) (goto-char pos) (throw 'exit nil))
28524 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
28528 (defun org-show-siblings ()
28529 "Show all siblings of the current headline."
28531 (while (org-goto-sibling) (org-flag-heading nil)))
28533 (while (org-goto-sibling 'previous)
28534 (org-flag-heading nil))))
28536 (defun org-show-hidden-entry ()
28537 "Show an entry where even the heading is hidden."
28541 (defun org-flag-heading (flag &optional entry)
28542 "Flag the current heading. FLAG non-nil means make invisible.
28543 When ENTRY is non-nil, show the entire entry."
28545 (org-back-to-heading t)
28546 ;; Check if we should show the entire entry
28551 (and (outline-next-heading)
28552 (org-flag-heading nil))))
28553 (outline-flag-region (max (point-min) (1- (point)))
28554 (save-excursion (outline-end-of-heading) (point))
28557 (defun org-end-of-subtree (&optional invisible-OK to-heading)
28558 ;; This is an exact copy of the original function, but it uses
28559 ;; `org-back-to-heading', to make it work also in invisible
28560 ;; trees. And is uses an invisible-OK argument.
28561 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
28562 (org-back-to-heading invisible-OK)
28564 (level (funcall outline-level)))
28565 (while (and (not (eobp))
28566 (or first (> (funcall outline-level) level)))
28568 (outline-next-heading))
28570 (if (memq (preceding-char) '(?\n ?\^M))
28572 ;; Go to end of line before heading
28574 (if (memq (preceding-char) '(?\n ?\^M))
28575 ;; leave blank line before heading
28576 (forward-char -1))))))
28579 (defun org-show-subtree ()
28580 "Show everything after this heading at deeper levels."
28581 (outline-flag-region
28584 (outline-end-of-subtree) (outline-next-heading) (point))
28587 (defun org-show-entry ()
28588 "Show the body directly following this heading.
28589 Show the heading too, if it is currently invisible."
28592 (condition-case nil
28594 (org-back-to-heading t)
28595 (outline-flag-region
28596 (max (point-min) (1- (point)))
28599 (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
28600 (or (match-beginning 1) (point-max)))
28604 (defun org-make-options-regexp (kwds)
28605 "Make a regular expression for keyword lines."
28609 (mapconcat 'regexp-quote kwds "\\|")
28613 ;; Make isearch reveal the necessary context
28614 (defun org-isearch-end ()
28615 "Reveal context after isearch exits."
28616 (when isearch-success ; only if search was successful
28617 (if (featurep 'xemacs)
28618 ;; Under XEmacs, the hook is run in the correct place,
28619 ;; we directly show the context.
28620 (org-show-context 'isearch)
28621 ;; In Emacs the hook runs *before* restoring the overlays.
28622 ;; So we have to use a one-time post-command-hook to do this.
28623 ;; (Emacs 22 has a special variable, see function `org-mode')
28624 (unless (and (boundp 'isearch-mode-end-hook-quit)
28625 isearch-mode-end-hook-quit)
28626 ;; Only when the isearch was not quitted.
28627 (org-add-hook 'post-command-hook 'org-isearch-post-command
28628 'append 'local)))))
28630 (defun org-isearch-post-command ()
28631 "Remove self from hook, and show context."
28632 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
28633 (org-show-context 'isearch))
28636 ;;;; Integration with and fixes for other packages
28640 (defvar org-imenu-markers nil
28641 "All markers currently used by Imenu.")
28642 (make-variable-buffer-local 'org-imenu-markers)
28644 (defun org-imenu-new-marker (&optional pos)
28645 "Return a new marker for use by Imenu, and remember the marker."
28646 (let ((m (make-marker)))
28647 (move-marker m (or pos (point)))
28648 (push m org-imenu-markers)
28651 (defun org-imenu-get-tree ()
28652 "Produce the index for Imenu."
28653 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
28654 (setq org-imenu-markers nil)
28655 (let* ((n org-imenu-depth)
28656 (re (concat "^" outline-regexp))
28657 (subs (make-vector (1+ n) nil))
28663 (goto-char (point-max))
28664 (while (re-search-backward re nil t)
28665 (setq level (org-reduced-level (funcall outline-level)))
28667 (looking-at org-complex-heading-regexp)
28668 (setq head (org-match-string-no-properties 4)
28669 m (org-imenu-new-marker))
28670 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
28671 (if (>= level last-level)
28672 (push (cons head m) (aref subs level))
28673 (push (cons head (aref subs (1+ level))) (aref subs level))
28674 (loop for i from (1+ level) to n do (aset subs i nil)))
28675 (setq last-level level)))))
28678 (eval-after-load "imenu"
28680 (add-hook 'imenu-after-jump-hook
28681 (lambda () (org-show-context 'org-goto)))))
28683 ;; Speedbar support
28685 (defun org-speedbar-set-agenda-restriction ()
28686 "Restrict future agenda commands to the location at point in speedbar.
28687 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
28689 (let (p m tp np dir txt w)
28691 ((setq p (text-property-any (point-at-bol) (point-at-eol)
28693 (setq m (get-text-property p 'org-imenu-marker))
28696 (set-buffer (marker-buffer m))
28698 (org-agenda-set-restriction-lock 'subtree))))
28699 ((setq p (text-property-any (point-at-bol) (point-at-eol)
28700 'speedbar-function 'speedbar-find-file))
28701 (setq tp (previous-single-property-change
28702 (1+ p) 'speedbar-function)
28703 np (next-single-property-change
28704 tp 'speedbar-function)
28705 dir (speedbar-line-directory)
28706 txt (buffer-substring-no-properties (or tp (point-min))
28707 (or np (point-max))))
28710 (set-buffer (find-file-noselect
28711 (let ((default-directory dir))
28712 (expand-file-name txt))))
28713 (unless (org-mode-p)
28714 (error "Cannot restrict to non-Org-mode file"))
28715 (org-agenda-set-restriction-lock 'file))))
28716 (t (error "Don't know how to restrict Org-mode's agenda")))
28717 (org-move-overlay org-speedbar-restriction-lock-overlay
28718 (point-at-bol) (point-at-eol))
28719 (setq current-prefix-arg nil)
28720 (org-agenda-maybe-redo)))
28722 (eval-after-load "speedbar"
28724 (speedbar-add-supported-extension ".org")
28725 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
28726 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
28727 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
28728 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
28729 (add-hook 'speedbar-visiting-tag-hook
28730 (lambda () (org-show-context 'org-goto)))))
28733 ;;; Fixes and Hacks
28735 ;; Make flyspell not check words in links, to not mess up our keymap
28736 (defun org-mode-flyspell-verify ()
28737 "Don't let flyspell put overlays at active buttons."
28738 (not (get-text-property (point) 'keymap)))
28740 ;; Make `bookmark-jump' show the jump location if it was hidden.
28741 (eval-after-load "bookmark"
28742 '(if (boundp 'bookmark-after-jump-hook)
28743 ;; We can use the hook
28744 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
28745 ;; Hook not available, use advice
28746 (defadvice bookmark-jump (after org-make-visible activate)
28747 "Make the position visible."
28748 (org-bookmark-jump-unhide))))
28750 (defun org-bookmark-jump-unhide ()
28751 "Unhide the current position, to show the bookmark location."
28753 (or (org-invisible-p)
28754 (save-excursion (goto-char (max (point-min) (1- (point))))
28755 (org-invisible-p)))
28756 (org-show-context 'bookmark-jump)))
28758 ;; Fix a bug in htmlize where there are text properties (face nil)
28759 (eval-after-load "htmlize"
28761 (defadvice htmlize-faces-in-buffer (after org-no-nil-faces activate)
28762 "Make sure there are no nil faces"
28763 (setq ad-return-value (delq nil ad-return-value)))))
28765 ;; Make session.el ignore our circular variable
28766 (eval-after-load "session"
28767 '(add-to-list 'session-globals-exclude 'org-mark-ring))
28769 ;;;; Experimental code
28771 (defun org-closed-in-range ()
28772 "Sparse tree of items closed in a certain time range.
28773 Still experimental, may disappear in the future."
28775 ;; Get the time interval from the user.
28776 (let* ((time1 (time-to-seconds
28777 (org-read-date nil 'to-time nil "Starting date: ")))
28778 (time2 (time-to-seconds
28779 (org-read-date nil 'to-time nil "End date:")))
28780 ;; callback function
28781 (callback (lambda ()
28784 (apply 'encode-time
28785 (org-parse-time-string
28786 (match-string 1))))))
28787 ;; check if time in interval
28788 (and (>= time time1) (<= time time2))))))
28789 ;; make tree, check each match with the callback
28790 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
28796 (run-hooks 'org-load-hook)
28798 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
28799 ;;; org.el ends here