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, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org-mode develops organizational tasks around NOTES files that contain
33 ;; information about projects as plain text. Org-mode is implemented on
34 ;; top of outline-mode, which makes it possible to keep the content of
35 ;; large files well structured. Visibility cycling and structure editing
36 ;; help to work with the tree. Tables are easily created with a built-in
37 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
38 ;; and scheduling. It dynamically compiles entries into an agenda that
39 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
40 ;; Plain text URL-like links connect to websites, emails, Usenet
41 ;; messages, BBDB entries, and any files related to the projects. For
42 ;; printing and sharing of notes, an Org-mode file can be exported as a
43 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
44 ;; iCalendar file. It can also serve as a publishing tool for a set of
47 ;; Installation and Activation
48 ;; ---------------------------
49 ;; See the corresponding sections in the manual at
51 ;; http://orgmode.org/org.html#Installation
55 ;; The documentation of Org-mode can be found in the TeXInfo file. The
56 ;; distribution also contains a PDF version of it. At the homepage of
57 ;; Org-mode, you can read the same text online as HTML. There is also an
58 ;; excellent reference card made by Philip Rooke. This card can be found
59 ;; in the etc/ directory of Emacs 22.
61 ;; A list of recent changes can be found at
62 ;; http://orgmode.org/Changes.html
66 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
67 (defvar org-table-formula-constants-local nil
68 "Local version of `org-table-formula-constants'.")
69 (make-variable-buffer-local 'org-table-formula-constants-local
)
71 ;;;; Require other packages
77 ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
78 ;; the file noutline.el being loaded.
79 (if (featurep 'xemacs
) (condition-case nil
(require 'noutline
)))
80 ;; We require noutline, which might be provided in outline.el
81 (require 'outline
) (require 'noutline
)
82 ;; Other stuff we need.
84 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
92 (require 'org-footnote
)
94 ;;;; Customization variables
98 (defconst org-version
"6.31trans"
99 "The version number of the file org.el.")
101 (defun org-version (&optional here
)
102 "Show the org-mode version in the echo area.
103 With prefix arg HERE, insert it at point."
105 (let* ((version org-version
)
107 (dir (concat (file-name-directory (locate-library "org")) "../" )))
108 (if (and (file-exists-p (expand-file-name ".git" dir
))
109 (executable-find "git"))
110 (let ((pwd (substring (pwd) 10)))
112 (if (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
114 (set-buffer "*Shell Command Output*")
115 (goto-char (point-min))
116 (re-search-forward "[^\n]+")
117 (setq git-version
(match-string 0))
118 (subst-char-in-string ?- ?. git-version t
)
119 (shell-command "git diff-index --name-only HEAD --")
120 (unless (eql 1 (point-max))
121 (setq git-version
(concat git-version
".dirty")))
122 (setq version
(concat version
" (" git-version
")")))
124 (setq version
(format "Org-mode version %s" version
))
125 (if here
(insert version
))
129 ;;; Compatibility constants
131 ;;; The custom variables
134 "Outline-based notes management and organizer."
140 (defcustom org-load-hook nil
141 "Hook that is run after org.el has been loaded."
145 (defvar org-modules
) ; defined below
146 (defvar org-modules-loaded nil
147 "Have the modules been loaded already?")
149 (defun org-load-modules-maybe (&optional force
)
150 "Load all extensions listed in `org-modules'."
151 (when (or force
(not org-modules-loaded
))
153 (condition-case nil
(require ext
)
154 (error (message "Problems while trying to load feature `%s'" ext
))))
156 (setq org-modules-loaded t
)))
158 (defun org-set-modules (var value
)
159 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
161 (when (featurep 'org
)
162 (org-load-modules-maybe 'force
)))
164 (when (org-bound-and-true-p org-modules
)
165 (let ((a (member 'org-infojs org-modules
)))
166 (and a
(setcar a
'org-jsinfo
))))
168 (defcustom org-modules
'(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl
)
169 "Modules that should always be loaded together with org.el.
170 If a description starts with <C>, the file is not part of Emacs
171 and loading it will require that you have downloaded and properly installed
172 the org-mode distribution.
174 You can also use this system to load external packages (i.e. neither Org
175 core modules, nor modules from the CONTRIB directory). Just add symbols
176 to the end of the list. If the package is called org-xyz.el, then you need
177 to add the symbol `xyz', and the package must have a call to
181 :set
'org-set-modules
184 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
185 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
186 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
187 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
188 (const :tag
" id: Global IDs for identifying entries" org-id
)
189 (const :tag
" info: Links to Info nodes" org-info
)
190 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
191 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
192 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
193 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
194 (const :tag
" mew Links to Mew folders/messages" org-mew
)
195 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
196 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
197 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
198 (const :tag
" vm: Links to VM folders/messages" org-vm
)
199 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
200 (const :tag
" w3m: Special cut/paste from w3m to Org." org-w3m
)
201 (const :tag
" mouse: Additional mouse support" org-mouse
)
203 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
204 (const :tag
"C annotation-helper: Call Remember directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-annotation-helper
)
205 (const :tag
"C bookmark: Org links to bookmarks" org-bookmark
)
206 (const :tag
"C browser-url: Store link, directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-browser-url
)
207 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
208 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
209 (const :tag
"C collector: Collect properties into tables" org-collector
)
210 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
211 (const :tag
"C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol
)
212 (const :tag
"C eval: Include command output as text" org-eval
)
213 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
214 (const :tag
"C expiry: Expiry mechanism for Org entries" org-expiry
)
215 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
216 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
218 (const :tag
"C invoice Help manage client invoices in OrgMode" org-invoice
)
220 (const :tag
"C jira Add a jira:ticket protocol to Org" org-jira
)
221 (const :tag
"C mairix: Hook mairix search into Org for different MUAs" org-mairix
)
222 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
223 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
224 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
225 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
226 (const :tag
"C R: Computation using the R language" org-R
)
227 (const :tag
"C registry: A registry for Org links" org-registry
)
228 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
229 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
230 (const :tag
"C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
231 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
232 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
233 (const :tag
"C track: Keep up with Org development" org-track
)
234 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
236 (defcustom org-support-shift-select nil
237 "Non-nil means, make shift-cursor commands select text when possible.
239 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
240 selecting a region, or enlarge thusly regions started in this way.
241 In Org-mode, in special contexts, these same keys are used for other
242 purposes, important enough to compete with shift selection. Org tries
243 to balance these needs by supporting `shift-select-mode' outside these
244 special contexts, under control of this variable.
246 The default of this variable is nil, to avoid confusing behavior. Shifted
247 cursor keys will then execute Org commands in the following contexts:
248 - on a headline, changing TODO state (left/right) and priority (up/down)
249 - on a time stamp, changing the time
250 - in a plain list item, changing the bullet type
251 - in a property definition line, switching between allowed values
252 - in the BEGIN line of a clock table (changing the time block).
253 Outside these contexts, the commands will throw an error.
255 When this variable is t and the cursor is not in a special context,
256 Org-mode will support shift-selection for making and enlarging regions.
257 To make this more effective, the bullet cycling will no longer happen
258 anywhere in an item line, but only if the cursor is exactly on the bullet.
260 If you set this variable to the symbol `always', then the keys
261 will not be special in headlines, property lines, and item lines, to make
262 shift selection work there as well. If this is what you want, you can
263 use the following alternative commands: `C-c C-t' and `C-c ,' to
264 change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
265 TODO sets, `C-c -' to cycle item bullet types, and properties can be
266 edited by hand or in column view.
268 However, when the cursor is on a timestamp, shift-cursor commands
269 will still edit the time stamp - this is just too good to give up.
271 XEmacs user should have this variable set to nil, because shift-select-mode
275 (const :tag
"Never" nil
)
276 (const :tag
"When outside special context" t
)
277 (const :tag
"Everywhere except timestamps" always
)))
279 (defgroup org-startup nil
280 "Options concerning startup of Org-mode."
284 (defcustom org-startup-folded t
285 "Non-nil means, entering Org-mode will switch to OVERVIEW.
286 This can also be configured on a per-file basis by adding one of
287 the following lines anywhere in the buffer:
289 #+STARTUP: fold (or `overview', this is equivalent)
290 #+STARTUP: nofold (or `showall', this is equivalent)
292 #+STARTUP: showeverything"
295 (const :tag
"nofold: show all" nil
)
296 (const :tag
"fold: overview" t
)
297 (const :tag
"content: all headlines" content
)
298 (const :tag
"show everything, even drawers" showeverything
)))
300 (defcustom org-startup-truncated t
301 "Non-nil means, entering Org-mode will set `truncate-lines'.
302 This is useful since some lines containing links can be very long and
303 uninteresting. Also tables look terrible when wrapped."
307 (defcustom org-startup-indented nil
308 "Non-nil means, turn on `org-indent-mode' on startup.
309 This can also be configured on a per-file basis by adding one of
310 the following lines anywhere in the buffer:
314 :group
'org-structure
316 (const :tag
"Not" nil
)
317 (const :tag
"Globally (slow on startup in large files)" t
)))
319 (defcustom org-startup-align-all-tables nil
320 "Non-nil means, align all tables when visiting a file.
321 This is useful when the column width in tables is forced with <N> cookies
322 in table fields. Such tables will look correct only after the first re-align.
323 This can also be configured on a per-file basis by adding one of
324 the following lines anywhere in the buffer:
330 (defcustom org-insert-mode-line-in-empty-file nil
331 "Non-nil means insert the first line setting Org-mode in empty files.
332 When the function `org-mode' is called interactively in an empty file, this
333 normally means that the file name does not automatically trigger Org-mode.
334 To ensure that the file will always be in Org-mode in the future, a
335 line enforcing Org-mode will be inserted into the buffer, if this option
340 (defcustom org-replace-disputed-keys nil
341 "Non-nil means use alternative key bindings for some keys.
342 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
343 These keys are also used by other packages like shift-selection-mode'
344 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
345 If you want to use Org-mode together with one of these other modes,
346 or more generally if you would like to move some Org-mode commands to
347 other keys, set this variable and configure the keys with the variable
350 This option is only relevant at load-time of Org-mode, and must be set
351 *before* org.el is loaded. Changing it requires a restart of Emacs to
356 (defcustom org-use-extra-keys nil
357 "Non-nil means use extra key sequence definitions for certain
358 commands. This happens automatically if you run XEmacs or if
359 window-system is nil. This variable lets you do the same
360 manually. You must set it before loading org.
362 Example: on Carbon Emacs 22 running graphically, with an external
363 keyboard on a Powerbook, the default way of setting M-left might
364 not work for either Alt or ESC. Setting this variable will make
369 (if (fboundp 'defvaralias
)
370 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
372 (defcustom org-disputed-keys
373 '(([(shift up
)] .
[(meta p
)])
374 ([(shift down
)] .
[(meta n
)])
375 ([(shift left
)] .
[(meta -
)])
376 ([(shift right
)] .
[(meta +)])
377 ([(control shift right
)] .
[(meta shift
+)])
378 ([(control shift left
)] .
[(meta shift -
)]))
379 "Keys for which Org-mode and other modes compete.
380 This is an alist, cars are the default keys, second element specifies
381 the alternative to use when `org-replace-disputed-keys' is t.
383 Keys can be specified in any syntax supported by `define-key'.
384 The value of this option takes effect only at Org-mode's startup,
385 therefore you'll have to restart Emacs to apply it after changing."
390 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
391 Or return the original if not disputed."
392 (if org-replace-disputed-keys
393 (let* ((nkey (key-description key
))
394 (x (org-find-if (lambda (x)
395 (equal (key-description (car x
)) nkey
))
400 (defun org-find-if (predicate seq
)
403 (if (funcall predicate
(car seq
))
404 (throw 'exit
(car seq
))
407 (defun org-defkey (keymap key def
)
408 "Define a key, possibly translated, as returned by `org-key'."
409 (define-key keymap
(org-key key
) def
))
411 (defcustom org-ellipsis nil
412 "The ellipsis to use in the Org-mode outline.
413 When nil, just use the standard three dots. When a string, use that instead,
414 When a face, use the standard 3 dots, but with the specified face.
415 The change affects only Org-mode (which will then use its own display table).
416 Changing this requires executing `M-x org-mode' in a buffer to become
419 :type
'(choice (const :tag
"Default" nil
)
420 (face :tag
"Face" :value org-warning
)
421 (string :tag
"String" :value
"...#")))
423 (defvar org-display-table nil
424 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
426 (defgroup org-keywords nil
427 "Keywords in Org-mode."
431 (defcustom org-deadline-string
"DEADLINE:"
432 "String to mark deadline entries.
433 A deadline is this string, followed by a time stamp. Should be a word,
434 terminated by a colon. You can insert a schedule keyword and
435 a timestamp with \\[org-deadline].
436 Changes become only effective after restarting Emacs."
440 (defcustom org-scheduled-string
"SCHEDULED:"
441 "String to mark scheduled TODO entries.
442 A schedule is this string, followed by a time stamp. Should be a word,
443 terminated by a colon. You can insert a schedule keyword and
444 a timestamp with \\[org-schedule].
445 Changes become only effective after restarting Emacs."
449 (defcustom org-closed-string
"CLOSED:"
450 "String used as the prefix for timestamps logging closing a TODO entry."
454 (defcustom org-clock-string
"CLOCK:"
455 "String used as prefix for timestamps clocking work hours on an item."
459 (defcustom org-comment-string
"COMMENT"
460 "Entries starting with this keyword will never be exported.
461 An entry can be toggled between COMMENT and normal with
462 \\[org-toggle-comment].
463 Changes become only effective after restarting Emacs."
467 (defcustom org-quote-string
"QUOTE"
468 "Entries starting with this keyword will be exported in fixed-width font.
469 Quoting applies only to the text in the entry following the headline, and does
470 not extend beyond the next headline, even if that is lower level.
471 An entry can be toggled between QUOTE and normal with
472 \\[org-toggle-fixed-width-section]."
476 (defconst org-repeat-re
477 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
478 "Regular expression for specifying repeated events.
479 After a match, group 1 contains the repeat expression.")
481 (defgroup org-structure nil
482 "Options concerning the general structure of Org-mode files."
486 (defgroup org-reveal-location nil
487 "Options about how to make context of a location visible."
488 :tag
"Org Reveal Location"
489 :group
'org-structure
)
491 (defconst org-context-choice
493 (const :tag
"Always" t
)
494 (const :tag
"Never" nil
)
495 (repeat :greedy t
:tag
"Individual contexts"
497 (choice :tag
"Context"
504 (const bookmark-jump
)
508 "Contexts for the reveal options.")
510 (defcustom org-show-hierarchy-above
'((default . t
))
511 "Non-nil means, show full hierarchy when revealing a location.
512 Org-mode often shows locations in an org-mode file which might have
513 been invisible before. When this is set, the hierarchy of headings
514 above the exposed location is shown.
515 Turning this off for example for sparse trees makes them very compact.
516 Instead of t, this can also be an alist specifying this option for different
517 contexts. Valid contexts are
518 agenda when exposing an entry from the agenda
519 org-goto when using the command `org-goto' on key C-c C-j
520 occur-tree when using the command `org-occur' on key C-c /
521 tags-tree when constructing a sparse tree based on tags matches
522 link-search when exposing search matches associated with a link
523 mark-goto when exposing the jump goal of a mark
524 bookmark-jump when exposing a bookmark location
525 isearch when exiting from an incremental search
526 default default for all contexts not set explicitly"
527 :group
'org-reveal-location
528 :type org-context-choice
)
530 (defcustom org-show-following-heading
'((default . nil
))
531 "Non-nil means, show following heading when revealing a location.
532 Org-mode often shows locations in an org-mode file which might have
533 been invisible before. When this is set, the heading following the
535 Turning this off for example for sparse trees makes them very compact,
536 but makes it harder to edit the location of the match. In such a case,
537 use the command \\[org-reveal] to show more context.
538 Instead of t, this can also be an alist specifying this option for different
539 contexts. See `org-show-hierarchy-above' for valid contexts."
540 :group
'org-reveal-location
541 :type org-context-choice
)
543 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
544 "Non-nil means, show all sibling heading when revealing a location.
545 Org-mode often shows locations in an org-mode file which might have
546 been invisible before. When this is set, the sibling of the current entry
547 heading are all made visible. If `org-show-hierarchy-above' is t,
548 the same happens on each level of the hierarchy above the current entry.
550 By default this is on for the isearch context, off for all other contexts.
551 Turning this off for example for sparse trees makes them very compact,
552 but makes it harder to edit the location of the match. In such a case,
553 use the command \\[org-reveal] to show more context.
554 Instead of t, this can also be an alist specifying this option for different
555 contexts. See `org-show-hierarchy-above' for valid contexts."
556 :group
'org-reveal-location
557 :type org-context-choice
)
559 (defcustom org-show-entry-below
'((default . nil
))
560 "Non-nil means, show the entry below a headline when revealing a location.
561 Org-mode often shows locations in an org-mode file which might have
562 been invisible before. When this is set, the text below the headline that is
563 exposed is also shown.
565 By default this is off for all contexts.
566 Instead of t, this can also be an alist specifying this option for different
567 contexts. See `org-show-hierarchy-above' for valid contexts."
568 :group
'org-reveal-location
569 :type org-context-choice
)
571 (defcustom org-indirect-buffer-display
'other-window
572 "How should indirect tree buffers be displayed?
573 This applies to indirect buffers created with the commands
574 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
576 current-window Display in the current window
577 other-window Just display in another window.
578 dedicated-frame Create one new frame, and re-use it each time.
579 new-frame Make a new frame each time. Note that in this case
580 previously-made indirect buffers are kept, and you need to
581 kill these buffers yourself."
582 :group
'org-structure
583 :group
'org-agenda-windows
585 (const :tag
"In current window" current-window
)
586 (const :tag
"In current frame, other window" other-window
)
587 (const :tag
"Each time a new frame" new-frame
)
588 (const :tag
"One dedicated frame" dedicated-frame
)))
590 (defgroup org-cycle nil
591 "Options concerning visibility cycling in Org-mode."
593 :group
'org-structure
)
595 (defcustom org-cycle-skip-children-state-if-no-children t
596 "Non-nil means, skip CHILDREN state in entries that don't have any."
600 (defcustom org-cycle-max-level nil
601 "Maximum level which should still be subject to visibility cycling.
602 Levels higher than this will, for cycling, be treated as text, not a headline.
603 When `org-odd-levels-only' is set, a value of N in this variable actually
604 means 2N-1 stars as the limiting headline.
605 When nil, cycle all levels.
606 Note that the limiting level of cycling is also influenced by
607 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
608 `org-inlinetask-min-level' is, cycling will be limited to levels one less
612 (const :tag
"No limit" nil
)
613 (integer :tag
"Maximum level")))
615 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK")
616 "Names of drawers. Drawers are not opened by cycling on the headline above.
617 Drawers only open with a TAB on the drawer line itself. A drawer looks like
622 The drawer \"PROPERTIES\" is special for capturing properties through
625 Drawers can be defined on the per-file basis with a line like:
627 #+DRAWERS: HIDDEN STATE PROPERTIES"
628 :group
'org-structure
630 :type
'(repeat (string :tag
"Drawer Name")))
632 (defcustom org-hide-block-startup nil
633 "Non-nil means, , entering Org-mode will fold all blocks.
634 This can also be set in on a per-file basis with
636 #+STARTUP: hideblocks
637 #+STARTUP: showblocks"
642 (defcustom org-cycle-global-at-bob nil
643 "Cycle globally if cursor is at beginning of buffer and not at a headline.
644 This makes it possible to do global cycling without having to use S-TAB or
645 C-u TAB. For this special case to work, the first line of the buffer
646 must not be a headline - it may be empty or some other text. When used in
647 this way, `org-cycle-hook' is disables temporarily, to make sure the
648 cursor stays at the beginning of the buffer.
649 When this option is nil, don't do anything special at the beginning
654 (defcustom org-cycle-emulate-tab t
655 "Where should `org-cycle' emulate TAB.
657 white Only in completely white lines
658 whitestart Only at the beginning of lines, before the first non-white char
659 t Everywhere except in headlines
660 exc-hl-bol Everywhere except at the start of a headline
661 If TAB is used in a place where it does not emulate TAB, the current subtree
662 visibility is cycled."
664 :type
'(choice (const :tag
"Never" nil
)
665 (const :tag
"Only in completely white lines" white
)
666 (const :tag
"Before first char in a line" whitestart
)
667 (const :tag
"Everywhere except in headlines" t
)
668 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
671 (defcustom org-cycle-separator-lines
2
672 "Number of empty lines needed to keep an empty line between collapsed trees.
673 If you leave an empty line between the end of a subtree and the following
674 headline, this empty line is hidden when the subtree is folded.
675 Org-mode will leave (exactly) one empty line visible if the number of
676 empty lines is equal or larger to the number given in this variable.
677 So the default 2 means, at least 2 empty lines after the end of a subtree
678 are needed to produce free space between a collapsed subtree and the
681 If the number is negative, and the number of empty lines is at least -N,
682 all empty lines are shown.
684 Special case: when 0, never leave empty lines in collapsed view."
687 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
689 (defcustom org-pre-cycle-hook nil
690 "Hook that is run before visibility cycling is happening.
691 The function(s) in this hook must accept a single argument which indicates
692 the new state that will be set right after running this hook. The
693 argument is a symbol. Before a global state change, it can have the values
694 `overview', `content', or `all'. Before a local state change, it can have
695 the values `folded', `children', or `subtree'."
699 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
700 org-cycle-hide-drawers
701 org-cycle-show-empty-lines
702 org-optimize-window-after-visibility-change
)
703 "Hook that is run after `org-cycle' has changed the buffer visibility.
704 The function(s) in this hook must accept a single argument which indicates
705 the new state that was set by the most recent `org-cycle' command. The
706 argument is a symbol. After a global state change, it can have the values
707 `overview', `content', or `all'. After a local state change, it can have
708 the values `folded', `children', or `subtree'."
712 (defgroup org-edit-structure nil
713 "Options concerning structure editing in Org-mode."
714 :tag
"Org Edit Structure"
715 :group
'org-structure
)
717 (defcustom org-odd-levels-only nil
718 "Non-nil means, skip even levels and only use odd levels for the outline.
719 This has the effect that two stars are being added/taken away in
720 promotion/demotion commands. It also influences how levels are
721 handled by the exporters.
722 Changing it requires restart of `font-lock-mode' to become effective
723 for fontification also in regions already fontified.
724 You may also set this on a per-file basis by adding one of the following
729 :group
'org-edit-structure
730 :group
'org-font-lock
733 (defcustom org-adapt-indentation t
734 "Non-nil means, adapt indentation to outline node level.
736 When this variable is set, Org assumes that you write outlines by
737 indenting text in each node to align with the headline (after the stars).
738 The following issues are influenced by this variable:
740 - When this is set and the *entire* text in an entry is indented, the
741 indentation is increased by one space in a demotion command, and
742 decreased by one in a promotion command. If any line in the entry
743 body starts with text at column 0, indentation is not changed at all.
745 - Property drawers and planning information is inserted indented when
746 this variable s set. When nil, they will not be indented.
748 - TAB indents a line relative to context. The lines below a headline
749 will be indented when this variable is set.
751 Note that this is all about true indentation, by adding and removing
752 space characters. See also `org-indent.el' which does level-dependent
753 indentation in a virtual way, i.e. at display time in Emacs."
754 :group
'org-edit-structure
757 (defcustom org-special-ctrl-a
/e nil
758 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
760 When t, `C-a' will bring back the cursor to the beginning of the
761 headline text, i.e. after the stars and after a possible TODO keyword.
762 In an item, this will be the position after the bullet.
763 When the cursor is already at that position, another `C-a' will bring
764 it to the beginning of the line.
766 `C-e' will jump to the end of the headline, ignoring the presence of tags
767 in the headline. A second `C-e' will then jump to the true end of the
768 line, after any tags. This also means that, when this variable is
769 non-nil, `C-e' also will never jump beyond the end of the heading of a
770 folded section, i.e. not after the ellipses.
772 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
773 going to the true line boundary first. Only a directly following, identical
774 keypress will bring the cursor to the special positions.
776 This may also be a cons cell where the behavior for `C-a' and `C-e' is
778 :group
'org-edit-structure
780 (const :tag
"off" nil
)
781 (const :tag
"on: after stars/bullet and before tags first" t
)
782 (const :tag
"reversed: true line boundary first" reversed
)
783 (cons :tag
"Set C-a and C-e separately"
784 (choice :tag
"Special C-a"
785 (const :tag
"off" nil
)
786 (const :tag
"on: after stars/bullet first" t
)
787 (const :tag
"reversed: before stars/bullet first" reversed
))
788 (choice :tag
"Special C-e"
789 (const :tag
"off" nil
)
790 (const :tag
"on: before tags first" t
)
791 (const :tag
"reversed: after tags first" reversed
)))))
792 (if (fboundp 'defvaralias
)
793 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
795 (defcustom org-special-ctrl-k nil
796 "Non-nil means `C-k' will behave specially in headlines.
797 When nil, `C-k' will call the default `kill-line' command.
798 When t, the following will happen while the cursor is in the headline:
800 - When the cursor is at the beginning of a headline, kill the entire
801 line and possible the folded subtree below the line.
802 - When in the middle of the headline text, kill the headline up to the tags.
803 - When after the headline text, kill the tags."
804 :group
'org-edit-structure
807 (defcustom org-yank-folded-subtrees t
808 "Non-nil means, when yanking subtrees, fold them.
809 If the kill is a single subtree, or a sequence of subtrees, i.e. if
810 it starts with a heading and all other headings in it are either children
811 or siblings, then fold all the subtrees. However, do this only if no
812 text after the yank would be swallowed into a folded tree by this action."
813 :group
'org-edit-structure
816 (defcustom org-yank-adjusted-subtrees nil
817 "Non-nil means, when yanking subtrees, adjust the level.
818 With this setting, `org-paste-subtree' is used to insert the subtree, see
819 this function for details."
820 :group
'org-edit-structure
823 (defcustom org-M-RET-may-split-line
'((default . t
))
824 "Non-nil means, M-RET will split the line at the cursor position.
825 When nil, it will go to the end of the line before making a
827 You may also set this option in a different way for different
828 contexts. Valid contexts are:
830 headline when creating a new headline
831 item when creating a new item
832 table in a table field
833 default the value to be used for all contexts not explicitly
835 :group
'org-structure
838 (const :tag
"Always" t
)
839 (const :tag
"Never" nil
)
840 (repeat :greedy t
:tag
"Individual contexts"
842 (choice :tag
"Context"
850 (defcustom org-insert-heading-respect-content nil
851 "Non-nil means, insert new headings after the current subtree.
852 When nil, the new heading is created directly after the current line.
853 The commands \\[org-insert-heading-respect-content] and
854 \\[org-insert-todo-heading-respect-content] turn this variable on
855 for the duration of the command."
856 :group
'org-structure
859 (defcustom org-blank-before-new-entry
'((heading . auto
)
860 (plain-list-item . auto
))
861 "Should `org-insert-heading' leave a blank line before new heading/item?
862 The value is an alist, with `heading' and `plain-list-item' as car,
863 and a boolean flag as cdr. For plain lists, if the variable
864 `org-empty-line-terminates-plain-lists' is set, the setting here
865 is ignored and no empty line is inserted, to keep the list in tact."
866 :group
'org-edit-structure
868 (cons (const heading
)
869 (choice (const :tag
"Never" nil
)
870 (const :tag
"Always" t
)
871 (const :tag
"Auto" auto
)))
872 (cons (const plain-list-item
)
873 (choice (const :tag
"Never" nil
)
874 (const :tag
"Always" t
)
875 (const :tag
"Auto" auto
)))))
877 (defcustom org-insert-heading-hook nil
878 "Hook being run after inserting a new heading."
879 :group
'org-edit-structure
882 (defcustom org-enable-fixed-width-editor t
883 "Non-nil means, lines starting with \":\" are treated as fixed-width.
884 This currently only means, they are never auto-wrapped.
885 When nil, such lines will be treated like ordinary lines.
886 See also the QUOTE keyword."
887 :group
'org-edit-structure
891 (defcustom org-goto-auto-isearch t
892 "Non-nil means, typing characters in org-goto starts incremental search."
893 :group
'org-edit-structure
896 (defgroup org-sparse-trees nil
897 "Options concerning sparse trees in Org-mode."
898 :tag
"Org Sparse Trees"
899 :group
'org-structure
)
901 (defcustom org-highlight-sparse-tree-matches t
902 "Non-nil means, highlight all matches that define a sparse tree.
903 The highlights will automatically disappear the next time the buffer is
904 changed by an edit command."
905 :group
'org-sparse-trees
908 (defcustom org-remove-highlights-with-change t
909 "Non-nil means, any change to the buffer will remove temporary highlights.
910 Such highlights are created by `org-occur' and `org-clock-display'.
911 When nil, `C-c C-c needs to be used to get rid of the highlights.
912 The highlights created by `org-preview-latex-fragment' always need
913 `C-c C-c' to be removed."
914 :group
'org-sparse-trees
919 (defcustom org-occur-hook
'(org-first-headline-recenter)
920 "Hook that is run after `org-occur' has constructed a sparse tree.
921 This can be used to recenter the window to show as much of the structure
923 :group
'org-sparse-trees
926 (defgroup org-imenu-and-speedbar nil
927 "Options concerning imenu and speedbar in Org-mode."
928 :tag
"Org Imenu and Speedbar"
929 :group
'org-structure
)
931 (defcustom org-imenu-depth
2
932 "The maximum level for Imenu access to Org-mode headlines.
933 This also applied for speedbar access."
934 :group
'org-imenu-and-speedbar
937 (defgroup org-table nil
938 "Options concerning tables in Org-mode."
942 (defcustom org-enable-table-editor
'optimized
943 "Non-nil means, lines starting with \"|\" are handled by the table editor.
944 When nil, such lines will be treated like ordinary lines.
946 When equal to the symbol `optimized', the table editor will be optimized to
948 - Automatic overwrite mode in front of whitespace in table fields.
949 This makes the structure of the table stay in tact as long as the edited
950 field does not exceed the column width.
951 - Minimize the number of realigns. Normally, the table is aligned each time
952 TAB or RET are pressed to move to another field. With optimization this
953 happens only if changes to a field might have changed the column width.
954 Optimization requires replacing the functions `self-insert-command',
955 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
956 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
957 very good at guessing when a re-align will be necessary, but you can always
958 force one with \\[org-ctrl-c-ctrl-c].
960 If you would like to use the optimized version in Org-mode, but the
961 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
963 This variable can be used to turn on and off the table editor during a session,
964 but in order to toggle optimization, a restart is required.
966 See also the variable `org-table-auto-blank-field'."
969 (const :tag
"off" nil
)
971 (const :tag
"on, optimized" optimized
)))
973 (defcustom org-self-insert-cluster-for-undo t
974 "Non-nil means cluster self-insert commands for undo when possible.
975 If this is set, then, like in the Emacs command loop, 20 consequtive
976 characters will be undone together.
977 This is configurable, because there is some impact on typing performance."
981 (defcustom org-table-tab-recognizes-table.el t
982 "Non-nil means, TAB will automatically notice a table.el table.
983 When it sees such a table, it moves point into it and - if necessary -
984 calls `table-recognize-table'."
985 :group
'org-table-editing
988 (defgroup org-link nil
989 "Options concerning links in Org-mode."
993 (defvar org-link-abbrev-alist-local nil
994 "Buffer-local version of `org-link-abbrev-alist', which see.
995 The value of this is taken from the #+LINK lines.")
996 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
998 (defcustom org-link-abbrev-alist nil
999 "Alist of link abbreviations.
1000 The car of each element is a string, to be replaced at the start of a link.
1001 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1002 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1004 [[linkkey:tag][description]]
1006 The 'linkkey' must be a word word, starting with a letter, followed
1007 by letters, numbers, '-' or '_'.
1009 If REPLACE is a string, the tag will simply be appended to create the link.
1010 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1011 the placeholder \"%h\" will cause a url-encoded version of the tag to
1012 be inserted at that point (see the function `url-hexify-string').
1014 REPLACE may also be a function that will be called with the tag as the
1015 only argument to create the link, which should be returned as a string.
1017 See the manual for examples."
1021 (string :tag
"Protocol")
1023 (string :tag
"Format")
1026 (defcustom org-descriptive-links t
1027 "Non-nil means, hide link part and only show description of bracket links.
1028 Bracket links are like [[link][description]]. This variable sets the initial
1029 state in new org-mode buffers. The setting can then be toggled on a
1030 per-buffer basis from the Org->Hyperlinks menu."
1034 (defcustom org-link-file-path-type
'adaptive
1035 "How the path name in file links should be stored.
1038 relative Relative to the current directory, i.e. the directory of the file
1039 into which the link is being inserted.
1040 absolute Absolute path, if possible with ~ for home directory.
1041 noabbrev Absolute path, no abbreviation of home directory.
1042 adaptive Use relative path for files in the current directory and sub-
1043 directories of it. For other files, use an absolute path."
1051 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1052 "Types of links that should be activated in Org-mode files.
1053 This is a list of symbols, each leading to the activation of a certain link
1054 type. In principle, it does not hurt to turn on most link types - there may
1055 be a small gain when turning off unused link types. The types are:
1057 bracket The recommended [[link][description]] or [[link]] links with hiding.
1058 angular Links in angular brackets that may contain whitespace like
1059 <bbdb:Carsten Dominik>.
1060 plain Plain links in normal text, no whitespace, like http://google.com.
1061 radio Text that is matched by a radio target, see manual for details.
1062 tag Tag settings in a headline (link to tag search).
1063 date Time stamps (link to calendar).
1064 footnote Footnote labels.
1066 Changing this variable requires a restart of Emacs to become effective."
1068 :type
'(set :greedy t
1069 (const :tag
"Double bracket links (new style)" bracket
)
1070 (const :tag
"Angular bracket links (old style)" angular
)
1071 (const :tag
"Plain text links" plain
)
1072 (const :tag
"Radio target matches" radio
)
1073 (const :tag
"Tags" tag
)
1074 (const :tag
"Timestamps" date
)
1075 (const :tag
"Footnotes" footnote
)))
1077 (defcustom org-make-link-description-function nil
1078 "Function to use to generate link descriptions from links. If
1079 nil the link location will be used. This function must take two
1080 parameters; the first is the link and the second the description
1081 org-insert-link has generated, and should return the description
1086 (defgroup org-link-store nil
1087 "Options concerning storing links in Org-mode."
1088 :tag
"Org Store Link"
1091 (defcustom org-email-link-description-format
"Email %c: %.30s"
1092 "Format of the description part of a link to an email or usenet message.
1093 The following %-escapes will be replaced by corresponding information:
1095 %F full \"From\" field
1096 %f name, taken from \"From\" field, address if no name
1097 %T full \"To\" field
1098 %t first name in \"To\" field, address if no name
1099 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1100 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1104 You may use normal field width specification between the % and the letter.
1105 This is for example useful to limit the length of the subject.
1107 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1108 :group
'org-link-store
1111 (defcustom org-from-is-user-regexp
1113 (when (and user-mail-address
(not (string= user-mail-address
"")))
1114 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1115 (when (and user-full-name
(not (string= user-full-name
"")))
1116 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1117 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1118 "Regexp matched against the \"From:\" header of an email or usenet message.
1119 It should match if the message is from the user him/herself."
1120 :group
'org-link-store
1123 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1124 "Non-nil means, storing a link to an Org file will use entry IDs.
1126 Note that before this variable is even considered, org-id must be loaded,
1127 so please customize `org-modules' and turn it on.
1129 The variable can have the following values:
1131 t Create an ID if needed to make a link to the current entry.
1133 create-if-interactive
1134 If `org-store-link' is called directly (interactively, as a user
1135 command), do create an ID to support the link. But when doing the
1136 job for remember, only use the ID if it already exists. The
1137 purpose of this setting is to avoid proliferation of unwanted
1138 IDs, just because you happen to be in an Org file when you
1139 call `org-remember' that automatically and preemptively
1140 creates a link. If you do want to get an ID link in a remember
1141 template to an entry not having an ID, create it first by
1142 explicitly creating a link to it, using `C-c C-l' first.
1144 create-if-interactive-and-no-custom-id
1145 Like create-if-interactive, but do not create an ID if there is
1146 a CUSTOM_ID property defined in the entry. This is the default.
1149 Use existing ID, do not create one.
1151 nil Never use an ID to make a link, instead link using a text search for
1153 :group
'org-link-store
1155 (const :tag
"Create ID to make link" t
)
1156 (const :tag
"Create if storing link interactively"
1157 create-if-interactive
)
1158 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1159 create-if-interactive-and-no-custom-id
)
1160 (const :tag
"Only use existing" use-existing
)
1161 (const :tag
"Do not use ID to create link" nil
)))
1163 (defcustom org-context-in-file-links t
1164 "Non-nil means, file links from `org-store-link' contain context.
1165 A search string will be added to the file name with :: as separator and
1166 used to find the context when the link is activated by the command
1167 `org-open-at-point'.
1168 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1169 negates this setting for the duration of the command."
1170 :group
'org-link-store
1173 (defcustom org-keep-stored-link-after-insertion nil
1174 "Non-nil means, keep link in list for entire session.
1176 The command `org-store-link' adds a link pointing to the current
1177 location to an internal list. These links accumulate during a session.
1178 The command `org-insert-link' can be used to insert links into any
1179 Org-mode file (offering completion for all stored links). When this
1180 option is nil, every link which has been inserted once using \\[org-insert-link]
1181 will be removed from the list, to make completing the unused links
1183 :group
'org-link-store
1186 (defgroup org-link-follow nil
1187 "Options concerning following links in Org-mode."
1188 :tag
"Org Follow Link"
1191 (defcustom org-link-translation-function nil
1192 "Function to translate links with different syntax to Org syntax.
1193 This can be used to translate links created for example by the Planner
1194 or emacs-wiki packages to Org syntax.
1195 The function must accept two parameters, a TYPE containing the link
1196 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1197 which is everything after the link protocol. It should return a cons
1198 with possibly modified values of type and path.
1199 Org contains a function for this, so if you set this variable to
1200 `org-translate-link-from-planner', you should be able follow many
1201 links created by planner."
1202 :group
'org-link-follow
1205 (defcustom org-follow-link-hook nil
1206 "Hook that is run after a link has been followed."
1207 :group
'org-link-follow
1210 (defcustom org-tab-follows-link nil
1211 "Non-nil means, on links TAB will follow the link.
1212 Needs to be set before org.el is loaded.
1213 This really should not be used, it does not make sense, and the
1214 implementation is bad."
1215 :group
'org-link-follow
1218 (defcustom org-return-follows-link nil
1219 "Non-nil means, on links RET will follow the link.
1220 Needs to be set before org.el is loaded."
1221 :group
'org-link-follow
1224 (defcustom org-mouse-1-follows-link
1225 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1226 "Non-nil means, mouse-1 on a link will follow the link.
1227 A longer mouse click will still set point. Does not work on XEmacs.
1228 Needs to be set before org.el is loaded."
1229 :group
'org-link-follow
1232 (defcustom org-mark-ring-length
4
1233 "Number of different positions to be recorded in the ring
1234 Changing this requires a restart of Emacs to work correctly."
1235 :group
'org-link-follow
1238 (defcustom org-link-frame-setup
1239 '((vm . vm-visit-folder-other-frame
)
1240 (gnus . gnus-other-frame
)
1241 (file . find-file-other-window
))
1242 "Setup the frame configuration for following links.
1243 When following a link with Emacs, it may often be useful to display
1244 this link in another window or frame. This variable can be used to
1245 set this up for the different types of links.
1248 `vm-visit-folder-other-frame'
1249 For Gnus, use any of
1252 `org-gnus-no-new-news'
1253 For FILE, use any of
1255 `find-file-other-window'
1256 `find-file-other-frame'
1257 For the calendar, use the variable `calendar-setup'.
1258 For BBDB, it is currently only possible to display the matches in
1260 :group
'org-link-follow
1264 (const vm-visit-folder
)
1265 (const vm-visit-folder-other-window
)
1266 (const vm-visit-folder-other-frame
)))
1270 (const gnus-other-frame
)
1271 (const org-gnus-no-new-news
)))
1275 (const find-file-other-window
)
1276 (const find-file-other-frame
)))))
1278 (defcustom org-display-internal-link-with-indirect-buffer nil
1279 "Non-nil means, use indirect buffer to display infile links.
1280 Activating internal links (from one location in a file to another location
1281 in the same file) normally just jumps to the location. When the link is
1282 activated with a C-u prefix (or with mouse-3), the link is displayed in
1283 another window. When this option is set, the other window actually displays
1284 an indirect buffer clone of the current buffer, to avoid any visibility
1285 changes to the current buffer."
1286 :group
'org-link-follow
1289 (defcustom org-open-non-existing-files nil
1290 "Non-nil means, `org-open-file' will open non-existing files.
1291 When nil, an error will be generated.
1292 This variable applies only to external applications because they
1293 might choke on non-existing files. If the link is to a file that
1294 will be openend in Emacs, the variable is ignored."
1295 :group
'org-link-follow
1298 (defcustom org-open-directory-means-index-dot-org nil
1299 "Non-nil means, a link to a directory really means to index.org.
1300 When nil, following a directory link will run dired or open a finder/explorer
1301 window on that directory."
1302 :group
'org-link-follow
1305 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1306 "Function and arguments to call for following mailto links.
1307 This is a list with the first element being a lisp function, and the
1308 remaining elements being arguments to the function. In string arguments,
1309 %a will be replaced by the address, and %s will be replaced by the subject
1310 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1311 :group
'org-link-follow
1313 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1314 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1315 (const :tag
"message-mail" (message-mail "%a" "%s"))
1316 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1318 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1319 "Non-nil means, ask for confirmation before executing shell links.
1320 Shell links can be dangerous: just think about a link
1322 [[shell:rm -rf ~/*][Google Search]]
1324 This link would show up in your Org-mode document as \"Google Search\",
1325 but really it would remove your entire home directory.
1326 Therefore we advise against setting this variable to nil.
1327 Just change it to `y-or-n-p' if you want to confirm with a
1328 single keystroke rather than having to type \"yes\"."
1329 :group
'org-link-follow
1331 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1332 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1333 (const :tag
"no confirmation (dangerous)" nil
)))
1335 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1336 "Non-nil means, ask for confirmation before executing Emacs Lisp links.
1337 Elisp links can be dangerous: just think about a link
1339 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1341 This link would show up in your Org-mode document as \"Google Search\",
1342 but really it would remove your entire home directory.
1343 Therefore we advise against setting this variable to nil.
1344 Just change it to `y-or-n-p' if you want to confirm with a
1345 single keystroke rather than having to type \"yes\"."
1346 :group
'org-link-follow
1348 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1349 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1350 (const :tag
"no confirmation (dangerous)" nil
)))
1352 (defconst org-file-apps-defaults-gnu
1356 "Default file applications on a UNIX or GNU/Linux system.
1357 See `org-file-apps'.")
1359 (defconst org-file-apps-defaults-macosx
1362 (system .
"open %s")
1364 ("eps.gz" .
"gv %s")
1366 ("fig" .
"xfig %s"))
1367 "Default file applications on a MacOS X system.
1368 The system \"open\" is known as a default, but we use X11 applications
1369 for some files for which the OS does not have a good default.
1370 See `org-file-apps'.")
1372 (defconst org-file-apps-defaults-windowsnt
1376 (list (if (featurep 'xemacs
)
1377 'mswindows-shell-execute
1381 (list (if (featurep 'xemacs
)
1382 'mswindows-shell-execute
1385 "Default file applications on a Windows NT system.
1386 The system \"open\" is used for most files.
1387 See `org-file-apps'.")
1389 (defcustom org-file-apps
1392 ("\\.x?html?\\'" . default
)
1393 ("\\.pdf\\'" . default
)
1395 "External applications for opening `file:path' items in a document.
1396 Org-mode uses system defaults for different file types, but
1397 you can use this variable to set the application for a given file
1398 extension. The entries in this list are cons cells where the car identifies
1399 files and the cdr the corresponding command. Possible values for the
1401 \"regex\" Regular expression matched against the file name. For backward
1402 compatibility, this can also be a string with only alphanumeric
1403 characters, which is then interpreted as an extension.
1404 `directory' Matches a directory
1405 `remote' Matches a remote file, accessible through tramp or efs.
1406 Remote files most likely should be visited through Emacs
1407 because external applications cannot handle such paths.
1408 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1409 so all files Emacs knows how to handle. Using this with
1410 command `emacs' will open most files in Emacs. Beware that this
1411 will also open html files inside Emacs, unless you add
1412 (\"html\" . default) to the list as well.
1413 t Default for files not matched by any of the other options.
1414 `system' The system command to open files, like `open' on Windows
1415 and Mac OS X, and mailcap under GNU/Linux. This is the command
1416 that will be selected if you call `C-c C-o' with a double
1419 Possible values for the command are:
1420 `emacs' The file will be visited by the current Emacs process.
1421 `default' Use the default application for this file type, which is the
1422 association for t in the list, most likely in the system-specific
1424 This can be used to overrule an unwanted setting in the
1425 system-specific variable.
1426 `system' Use the system command for opening files, like \"open\".
1427 This command is specified by the entry whose car is `system'.
1428 Most likely, the system-specific version of this variable
1429 does define this command, but you can overrule/replace it
1431 string A command to be executed by a shell; %s will be replaced
1432 by the path to the file.
1433 sexp A Lisp form which will be evaluated. The file path will
1434 be available in the Lisp variable `file'.
1435 For more examples, see the system specific constants
1436 `org-file-apps-defaults-macosx'
1437 `org-file-apps-defaults-windowsnt'
1438 `org-file-apps-defaults-gnu'."
1439 :group
'org-link-follow
1441 (cons (choice :value
""
1442 (string :tag
"Extension")
1443 (const :tag
"System command to open files" system
)
1444 (const :tag
"Default for unrecognized files" t
)
1445 (const :tag
"Remote file" remote
)
1446 (const :tag
"Links to a directory" directory
)
1447 (const :tag
"Any files that have Emacs modes"
1450 (const :tag
"Visit with Emacs" emacs
)
1451 (const :tag
"Use default" default
)
1452 (const :tag
"Use the system command" system
)
1453 (string :tag
"Command")
1454 (sexp :tag
"Lisp form")))))
1456 (defgroup org-refile nil
1457 "Options concerning refiling entries in Org-mode."
1461 (defcustom org-directory
"~/org"
1462 "Directory with org files.
1463 This is just a default location to look for Org files. There is no need
1464 at all to put your files into this directory. It is only used in the
1465 following situations:
1467 1. When a remember template specifies a target file that is not an
1468 absolute path. The path will then be interpreted relative to
1470 2. When a remember note is filed away in an interactive way (when exiting the
1471 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1472 with `org-directory' as the default path."
1474 :group
'org-remember
1477 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1478 "Default target for storing notes.
1479 Used by the hooks for remember.el. This can be a string, or nil to mean
1480 the value of `remember-data-file'.
1481 You can set this on a per-template basis with the variable
1482 `org-remember-templates'."
1484 :group
'org-remember
1486 (const :tag
"Default from remember-data-file" nil
)
1489 (defcustom org-goto-interface
'outline
1490 "The default interface to be used for `org-goto'.
1492 outline The interface shows an outline of the relevant file
1493 and the correct heading is found by moving through
1494 the outline or by searching with incremental search.
1495 outline-path-completion Headlines in the current buffer are offered via
1496 completion. This is the interface also used by
1497 the refile command."
1500 (const :tag
"Outline" outline
)
1501 (const :tag
"Outline-path-completion" outline-path-completion
)))
1503 (defcustom org-goto-max-level
5
1504 "Maximum level to be considered when running org-goto with refile interface."
1508 (defcustom org-reverse-note-order nil
1509 "Non-nil means, store new notes at the beginning of a file or entry.
1510 When nil, new notes will be filed to the end of a file or entry.
1511 This can also be a list with cons cells of regular expressions that
1512 are matched against file names, and values."
1513 :group
'org-remember
1516 (const :tag
"Reverse always" t
)
1517 (const :tag
"Reverse never" nil
)
1518 (repeat :tag
"By file name regexp"
1519 (cons regexp boolean
))))
1521 (defcustom org-refile-targets nil
1522 "Targets for refiling entries with \\[org-refile].
1523 This is list of cons cells. Each cell contains:
1524 - a specification of the files to be considered, either a list of files,
1525 or a symbol whose function or variable value will be used to retrieve
1526 a file name or a list of file names. If you use `org-agenda-files' for
1527 that, all agenda files will be scanned for targets. Nil means, consider
1528 headings in the current buffer.
1529 - A specification of how to find candidate refile targets. This may be
1531 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1532 This tag has to be present in all target headlines, inheritance will
1534 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1536 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1537 headlines that are refiling targets.
1538 - a cons cell (:level . N). Any headline of level N is considered a target.
1539 Note that, when `org-odd-levels-only' is set, level corresponds to
1540 order in hierarchy, not to the number of stars.
1541 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1542 Note that, when `org-odd-levels-only' is set, level corresponds to
1543 order in hierarchy, not to the number of stars.
1545 You can set the variable `org-refile-target-verify-function' to a function
1546 to verify each headline found by the simple critery above.
1548 When this variable is nil, all top-level headlines in the current buffer
1549 are used, equivalent to the value `((nil . (:level . 1))'."
1553 (choice :value org-agenda-files
1554 (const :tag
"All agenda files" org-agenda-files
)
1555 (const :tag
"Current buffer" nil
)
1556 (function) (variable) (file))
1557 (choice :tag
"Identify target headline by"
1558 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1559 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1560 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1561 (cons :tag
"Level number" (const :value
:level
) (integer))
1562 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1564 (defcustom org-refile-target-verify-function nil
1565 "Function to verify if the headline at point should be a refile target.
1566 The function will be called without arguments, with point at the
1567 beginning of the headline. It should return t and leave point
1568 where it is if the headline is a valid target for refiling.
1570 If the target should not be selected, the function must return nil.
1571 In addition to this, it may move point to a place from where the search
1572 should be continued. For example, the function may decide that the entire
1573 subtree of the current entry should be excluded and move point to the end
1578 (defcustom org-refile-use-outline-path nil
1579 "Non-nil means, provide refile targets as paths.
1580 So a level 3 headline will be available as level1/level2/level3.
1582 When the value is `file', also include the file name (without directory)
1583 into the path. In this case, you can also stop the completion after
1584 the file name, to get entries inserted as top level in the file.
1586 When `full-file-path', include the full file path."
1589 (const :tag
"Not" nil
)
1590 (const :tag
"Yes" t
)
1591 (const :tag
"Start with file name" file
)
1592 (const :tag
"Start with full file path" full-file-path
)))
1594 (defcustom org-outline-path-complete-in-steps t
1595 "Non-nil means, complete the outline path in hierarchical steps.
1596 When Org-mode uses the refile interface to select an outline path
1597 \(see variable `org-refile-use-outline-path'), the completion of
1598 the path can be done is a single go, or if can be done in steps down
1599 the headline hierarchy. Going in steps is probably the best if you
1600 do not use a special completion package like `ido' or `icicles'.
1601 However, when using these packages, going in one step can be very
1602 fast, while still showing the whole path to the entry."
1606 (defcustom org-refile-allow-creating-parent-nodes nil
1607 "Non-nil means, allow to create new nodes as refile targets.
1608 New nodes are then created by adding \"/new node name\" to the completion
1609 of an existing node. When the value of this variable is `confirm',
1610 new node creation must be confirmed by the user (recommended)
1611 When nil, the completion must match an existing entry.
1613 Note that, if the new heading is not seen by the criteria
1614 listed in `org-refile-targets', multiple instances of the same
1615 heading would be created by trying again to file under the new
1619 (const :tag
"Never" nil
)
1620 (const :tag
"Always" t
)
1621 (const :tag
"Prompt for confirmation" confirm
)))
1623 (defgroup org-todo nil
1624 "Options concerning TODO items in Org-mode."
1628 (defgroup org-progress nil
1629 "Options concerning Progress logging in Org-mode."
1633 (defvar org-todo-interpretation-widgets
1635 (:tag
"Sequence (cycling hits every state)" sequence
)
1636 (:tag
"Type (cycling directly to DONE)" type
))
1637 "The available interpretation symbols for customizing
1638 `org-todo-keywords'.
1639 Interested libraries should add to this list.")
1641 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
1642 "List of TODO entry keyword sequences and their interpretation.
1643 \\<org-mode-map>This is a list of sequences.
1645 Each sequence starts with a symbol, either `sequence' or `type',
1646 indicating if the keywords should be interpreted as a sequence of
1647 action steps, or as different types of TODO items. The first
1648 keywords are states requiring action - these states will select a headline
1649 for inclusion into the global TODO list Org-mode produces. If one of
1650 the \"keywords\" is the vertical bat \"|\" the remaining keywords
1651 signify that no further action is necessary. If \"|\" is not found,
1652 the last keyword is treated as the only DONE state of the sequence.
1654 The command \\[org-todo] cycles an entry through these states, and one
1655 additional state where no keyword is present. For details about this
1656 cycling, see the manual.
1658 TODO keywords and interpretation can also be set on a per-file basis with
1659 the special #+SEQ_TODO and #+TYP_TODO lines.
1661 Each keyword can optionally specify a character for fast state selection
1662 \(in combination with the variable `org-use-fast-todo-selection')
1663 and specifiers for state change logging, using the same syntax
1664 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
1665 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
1666 indicates to record a time stamp each time this state is selected.
1668 Each keyword may also specify if a timestamp or a note should be
1669 recorded when entering or leaving the state, by adding additional
1670 characters in the parenthesis after the keyword. This looks like this:
1671 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
1672 record only the time of the state change. With X and Y being either
1673 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
1674 Y when leaving the state if and only if the *target* state does not
1675 define X. You may omit any of the fast-selection key or X or /Y,
1676 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1678 For backward compatibility, this variable may also be just a list
1679 of keywords - in this case the interpretation (sequence or type) will be
1680 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1682 :group
'org-keywords
1684 (repeat :tag
"Old syntax, just keywords"
1685 (string :tag
"Keyword"))
1686 (repeat :tag
"New syntax"
1689 :tag
"Interpretation"
1690 ;;Quick and dirty way to see
1691 ;;`org-todo-interpretations'. This takes the
1692 ;;place of item arguments
1700 org-todo-interpretation-widgets
))
1703 (string :tag
"Keyword"))))))
1705 (defvar org-todo-keywords-1 nil
1706 "All TODO and DONE keywords active in a buffer.")
1707 (make-variable-buffer-local 'org-todo-keywords-1
)
1708 (defvar org-todo-keywords-for-agenda nil
)
1709 (defvar org-done-keywords-for-agenda nil
)
1710 (defvar org-drawers-for-agenda nil
)
1711 (defvar org-todo-keyword-alist-for-agenda nil
)
1712 (defvar org-tag-alist-for-agenda nil
)
1713 (defvar org-agenda-contributing-files nil
)
1714 (defvar org-not-done-keywords nil
)
1715 (make-variable-buffer-local 'org-not-done-keywords
)
1716 (defvar org-done-keywords nil
)
1717 (make-variable-buffer-local 'org-done-keywords
)
1718 (defvar org-todo-heads nil
)
1719 (make-variable-buffer-local 'org-todo-heads
)
1720 (defvar org-todo-sets nil
)
1721 (make-variable-buffer-local 'org-todo-sets
)
1722 (defvar org-todo-log-states nil
)
1723 (make-variable-buffer-local 'org-todo-log-states
)
1724 (defvar org-todo-kwd-alist nil
)
1725 (make-variable-buffer-local 'org-todo-kwd-alist
)
1726 (defvar org-todo-key-alist nil
)
1727 (make-variable-buffer-local 'org-todo-key-alist
)
1728 (defvar org-todo-key-trigger nil
)
1729 (make-variable-buffer-local 'org-todo-key-trigger
)
1731 (defcustom org-todo-interpretation
'sequence
1732 "Controls how TODO keywords are interpreted.
1733 This variable is in principle obsolete and is only used for
1734 backward compatibility, if the interpretation of todo keywords is
1735 not given already in `org-todo-keywords'. See that variable for
1738 :group
'org-keywords
1739 :type
'(choice (const sequence
)
1742 (defcustom org-use-fast-todo-selection t
1743 "Non-nil means, use the fast todo selection scheme with C-c C-t.
1744 This variable describes if and under what circumstances the cycling
1745 mechanism for TODO keywords will be replaced by a single-key, direct
1748 When nil, fast selection is never used.
1750 When the symbol `prefix', it will be used when `org-todo' is called with
1751 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
1752 in an agenda buffer.
1754 When t, fast selection is used by default. In this case, the prefix
1755 argument forces cycling instead.
1757 In all cases, the special interface is only used if access keys have actually
1758 been assigned by the user, i.e. if keywords in the configuration are followed
1759 by a letter in parenthesis, like TODO(t)."
1762 (const :tag
"Never" nil
)
1763 (const :tag
"By default" t
)
1764 (const :tag
"Only with C-u C-c C-t" prefix
)))
1766 (defcustom org-provide-todo-statistics t
1767 "Non-nil means, update todo statistics after insert and toggle.
1768 ALL-HEADLINES means update todo statistics by including headlines
1769 with no TODO keyword as well, counting them as not done.
1770 A list of TODO keywords means the same, but skip keywords that are
1773 When this is set, todo statistics is updated in the parent of the
1774 current entry each time a todo state is changed."
1777 (const :tag
"Yes, only for TODO entries" t
)
1778 (const :tag
"Yes, including all entries" 'all-headlines
)
1779 (repeat :tag
"Yes, for TODOs in this list"
1780 (string :tag
"TODO keyword"))
1781 (other :tag
"No TODO statistics" nil
)))
1783 (defcustom org-hierarchical-todo-statistics t
1784 "Non-nil means, TODO statistics covers just direct children.
1785 When nil, all entries in the subtree are considered.
1786 This has only an effect if `org-provide-todo-statistics' is set.
1787 To set this to nil for only a single subtree, use a COOKIE_DATA
1788 property and include the word \"recursive\" into the value."
1792 (defcustom org-after-todo-state-change-hook nil
1793 "Hook which is run after the state of a TODO item was changed.
1794 The new state (a string with a TODO keyword, or nil) is available in the
1795 Lisp variable `state'."
1799 (defvar org-blocker-hook nil
1800 "Hook for functions that are allowed to block a state change.
1802 Each function gets as its single argument a property list, see
1803 `org-trigger-hook' for more information about this list.
1805 If any of the functions in this hook returns nil, the state change
1808 (defvar org-trigger-hook nil
1809 "Hook for functions that are triggered by a state change.
1811 Each function gets as its single argument a property list with at least
1812 the following elements:
1814 (:type type-of-change :position pos-at-entry-start
1815 :from old-state :to new-state)
1817 Depending on the type, more properties may be present.
1819 This mechanism is currently implemented for:
1823 :type todo-state-change
1824 :from previous state (keyword as a string), or nil, or a symbol
1825 'todo' or 'done', to indicate the general type of state.
1826 :to new state, like in :from")
1828 (defcustom org-enforce-todo-dependencies nil
1829 "Non-nil means, undone TODO entries will block switching the parent to DONE.
1830 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
1831 be blocked if any prior sibling is not yet done.
1832 Finally, if the parent is blocked because of ordered siblings of its own,
1833 the child will also be blocked.
1834 This variable needs to be set before org.el is loaded, and you need to
1835 restart Emacs after a change to make the change effective. The only way
1836 to change is while Emacs is running is through the customize interface."
1837 :set
(lambda (var val
)
1840 (add-hook 'org-blocker-hook
1841 'org-block-todo-from-children-or-siblings-or-parent
)
1842 (remove-hook 'org-blocker-hook
1843 'org-block-todo-from-children-or-siblings-or-parent
)))
1847 (defcustom org-enforce-todo-checkbox-dependencies nil
1848 "Non-nil means, unchecked boxes will block switching the parent to DONE.
1849 When this is nil, checkboxes have no influence on switching TODO states.
1850 When non-nil, you first need to check off all check boxes before the TODO
1851 entry can be switched to DONE.
1852 This variable needs to be set before org.el is loaded, and you need to
1853 restart Emacs after a change to make the change effective. The only way
1854 to change is while Emacs is running is through the customize interface."
1855 :set
(lambda (var val
)
1858 (add-hook 'org-blocker-hook
1859 'org-block-todo-from-checkboxes
)
1860 (remove-hook 'org-blocker-hook
1861 'org-block-todo-from-checkboxes
)))
1865 (defcustom org-treat-insert-todo-heading-as-state-change nil
1866 "Non-nil means, inserting a TODO heading is treated as state change.
1867 So when the command \\[org-insert-todo-heading] is used, state change
1868 logging will apply if appropriate. When nil, the new TODO item will
1869 be inserted directly, and no logging will take place."
1873 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
1874 "Non-nil means, switching TODO states with S-cursor counts as state change.
1875 This is the default behavior. However, setting this to nil allows a
1876 convenient way to select a TODO state and bypass any logging associated
1881 (defcustom org-todo-state-tags-triggers nil
1882 "Tag changes that should be triggered by TODO state changes.
1883 This is a list. Each entry is
1885 (state-change (tag . flag) .......)
1887 State-change can be a string with a state, and empty string to indicate the
1888 state that has no TODO keyword, or it can be one of the symbols `todo'
1889 or `done', meaning any not-done or done state, respectively."
1893 (cons (choice :tag
"When changing to"
1894 (const :tag
"Not-done state" todo
)
1895 (const :tag
"Done state" done
)
1896 (string :tag
"State"))
1898 (cons :tag
"Tag action"
1900 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
1902 (defcustom org-log-done nil
1903 "Information to record when a task moves to the DONE state.
1905 Possible values are:
1907 nil Don't add anything, just change the keyword
1908 time Add a time stamp to the task
1909 note Prompt a closing note and add it with template `org-log-note-headings'
1911 This option can also be set with on a per-file-basis with
1913 #+STARTUP: nologdone
1915 #+STARTUP: lognotedone
1917 You can have local logging settings for a subtree by setting the LOGGING
1918 property to one or more of these keywords."
1920 :group
'org-progress
1922 (const :tag
"No logging" nil
)
1923 (const :tag
"Record CLOSED timestamp" time
)
1924 (const :tag
"Record CLOSED timestamp with closing note." note
)))
1926 ;; Normalize old uses of org-log-done.
1928 ((eq org-log-done t
) (setq org-log-done
'time
))
1929 ((and (listp org-log-done
) (memq 'done org-log-done
))
1930 (setq org-log-done
'note
)))
1932 (defcustom org-log-note-clock-out nil
1933 "Non-nil means, record a note when clocking out of an item.
1934 This can also be configured on a per-file basis by adding one of
1935 the following lines anywhere in the buffer:
1937 #+STARTUP: lognoteclock-out
1938 #+STARTUP: nolognoteclock-out"
1940 :group
'org-progress
1943 (defcustom org-log-done-with-time t
1944 "Non-nil means, the CLOSED time stamp will contain date and time.
1945 When nil, only the date will be recorded."
1946 :group
'org-progress
1949 (defcustom org-log-note-headings
1950 '((done .
"CLOSING NOTE %t")
1951 (state .
"State %-12s from %-12S %t")
1952 (note .
"Note taken on %t")
1954 "Headings for notes added to entries.
1955 The value is an alist, with the car being a symbol indicating the note
1956 context, and the cdr is the heading to be used. The heading may also be the
1958 %t in the heading will be replaced by a time stamp.
1959 %s will be replaced by the new TODO state, in double quotes.
1960 %S will be replaced by the old TODO state, in double quotes.
1961 %u will be replaced by the user name.
1962 %U will be replaced by the full user name."
1964 :group
'org-progress
1965 :type
'(list :greedy t
1966 (cons (const :tag
"Heading when closing an item" done
) string
)
1968 "Heading when changing todo state (todo sequence only)"
1970 (cons (const :tag
"Heading when just taking a note" note
) string
)
1971 (cons (const :tag
"Heading when clocking out" clock-out
) string
)))
1973 (unless (assq 'note org-log-note-headings
)
1974 (push '(note .
"%t") org-log-note-headings
))
1976 (defcustom org-log-into-drawer nil
1977 "Non-nil means, insert state change notes and time stamps into a drawer.
1978 When nil, state changes notes will be inserted after the headline and
1979 any scheduling and clock lines, but not inside a drawer.
1981 The value of this variable should be the name of the drawer to use.
1982 LOGBOOK is proposed at the default drawer for this purpose, you can
1983 also set this to a string to define the drawer of your choice.
1985 A value of t is also allowed, representing \"LOGBOOK\".
1987 If this variable is set, `org-log-state-notes-insert-after-drawers'
1990 You can set the property LOG_INTO_DRAWER to overrule this setting for
1993 :group
'org-progress
1995 (const :tag
"Not into a drawer" nil
)
1996 (const :tag
"LOGBOOK" t
)
1997 (string :tag
"Other")))
1999 (if (fboundp 'defvaralias
)
2000 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2002 (defun org-log-into-drawer ()
2003 "Return the value of `org-log-into-drawer', but let properties overrule.
2004 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2005 used instead of the default value."
2006 (let ((p (ignore-errors (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
))))
2008 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2009 ((equal p
"t") "LOGBOOK")
2012 (defcustom org-log-state-notes-insert-after-drawers nil
2013 "Non-nil means, insert state change notes after any drawers in entry.
2014 Only the drawers that *immediately* follow the headline and the
2015 deadline/scheduled line are skipped.
2016 When nil, insert notes right after the heading and perhaps the line
2017 with deadline/scheduling if present.
2019 This variable will have no effect if `org-log-into-drawer' is
2022 :group
'org-progress
2025 (defcustom org-log-states-order-reversed t
2026 "Non-nil means, the latest state change note will be directly after heading.
2027 When nil, the notes will be orderer according to time."
2029 :group
'org-progress
2032 (defcustom org-log-repeat
'time
2033 "Non-nil means, record moving through the DONE state when triggering repeat.
2034 An auto-repeating task is immediately switched back to TODO when
2035 marked DONE. If you are not logging state changes (by adding \"@\"
2036 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2037 record a closing note, there will be no record of the task moving
2038 through DONE. This variable forces taking a note anyway.
2040 nil Don't force a record
2041 time Record a time stamp
2044 This option can also be set with on a per-file-basis with
2046 #+STARTUP: logrepeat
2047 #+STARTUP: lognoterepeat
2048 #+STARTUP: nologrepeat
2050 You can have local logging settings for a subtree by setting the LOGGING
2051 property to one or more of these keywords."
2053 :group
'org-progress
2055 (const :tag
"Don't force a record" nil
)
2056 (const :tag
"Force recording the DONE state" time
)
2057 (const :tag
"Force recording a note with the DONE state" note
)))
2060 (defgroup org-priorities nil
2061 "Priorities in Org-mode."
2062 :tag
"Org Priorities"
2065 (defcustom org-enable-priority-commands t
2066 "Non-nil means, priority commands are active.
2067 When nil, these commands will be disabled, so that you never accidentally
2069 :group
'org-priorities
2072 (defcustom org-highest-priority ?A
2073 "The highest priority of TODO items. A character like ?A, ?B etc.
2074 Must have a smaller ASCII number than `org-lowest-priority'."
2075 :group
'org-priorities
2078 (defcustom org-lowest-priority ?C
2079 "The lowest priority of TODO items. A character like ?A, ?B etc.
2080 Must have a larger ASCII number than `org-highest-priority'."
2081 :group
'org-priorities
2084 (defcustom org-default-priority ?B
2085 "The default priority of TODO items.
2086 This is the priority an item get if no explicit priority is given."
2087 :group
'org-priorities
2090 (defcustom org-priority-start-cycle-with-default t
2091 "Non-nil means, start with default priority when starting to cycle.
2092 When this is nil, the first step in the cycle will be (depending on the
2093 command used) one higher or lower that the default priority."
2094 :group
'org-priorities
2097 (defgroup org-time nil
2098 "Options concerning time stamps and deadlines in Org-mode."
2102 (defcustom org-insert-labeled-timestamps-at-point nil
2103 "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
2104 When nil, these labeled time stamps are forces into the second line of an
2105 entry, just after the headline. When scheduling from the global TODO list,
2106 the time stamp will always be forced into the second line."
2110 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2111 "Formats for `format-time-string' which are used for time stamps.
2112 It is not recommended to change this constant.")
2114 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2115 "Number of minutes to round time stamps to.
2116 These are two values, the first applies when first creating a time stamp.
2117 The second applies when changing it with the commands `S-up' and `S-down'.
2118 When changing the time stamp, this means that it will change in steps
2119 of N minutes, as given by the second value.
2121 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2122 numbers should be factors of 60, so for example 5, 10, 15.
2124 When this is larger than 1, you can still force an exact time-stamp by using
2125 a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
2126 and by using a prefix arg to `S-up/down' to specify the exact number
2127 of minutes to shift."
2129 :get
'(lambda (var) ; Make sure all entries have 5 elements
2130 (if (integerp (default-value var
))
2131 (list (default-value var
) 5)
2132 (default-value var
)))
2134 (integer :tag
"when inserting times")
2135 (integer :tag
"when modifying times")))
2137 ;; Normalize old customizations of this variable.
2138 (when (integerp org-time-stamp-rounding-minutes
)
2139 (setq org-time-stamp-rounding-minutes
2140 (list org-time-stamp-rounding-minutes
2141 org-time-stamp-rounding-minutes
)))
2143 (defcustom org-display-custom-times nil
2144 "Non-nil means, overlay custom formats over all time stamps.
2145 The formats are defined through the variable `org-time-stamp-custom-formats'.
2146 To turn this on on a per-file basis, insert anywhere in the file:
2147 #+STARTUP: customtime"
2151 (make-variable-buffer-local 'org-display-custom-times
)
2153 (defcustom org-time-stamp-custom-formats
2154 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2155 "Custom formats for time stamps. See `format-time-string' for the syntax.
2156 These are overlayed over the default ISO format if the variable
2157 `org-display-custom-times' is set. Time like %H:%M should be at the
2158 end of the second format. The custom formats are also honored by export
2159 commands, if custom time display is turned on at the time of export."
2163 (defun org-time-stamp-format (&optional long inactive
)
2164 "Get the right format for a time string."
2165 (let ((f (if long
(cdr org-time-stamp-formats
)
2166 (car org-time-stamp-formats
))))
2168 (concat "[" (substring f
1 -
1) "]")
2171 (defcustom org-time-clocksum-format
"%d:%02d"
2172 "The format string used when creating CLOCKSUM lines, or when
2173 org-mode generates a time duration."
2177 (defcustom org-deadline-warning-days
14
2178 "No. of days before expiration during which a deadline becomes active.
2179 This variable governs the display in sparse trees and in the agenda.
2180 When 0 or negative, it means use this number (the absolute value of it)
2181 even if a deadline has a different individual lead time specified.
2183 Custom commands can set this variable in the options section."
2185 :group
'org-agenda-daily
/weekly
2188 (defcustom org-read-date-prefer-future t
2189 "Non-nil means, assume future for incomplete date input from user.
2190 This affects the following situations:
2191 1. The user gives a month but not a year.
2192 For example, if it is april and you enter \"feb 2\", this will be read
2193 as feb 2, *next* year. \"May 5\", however, will be this year.
2194 2. The user gives a day, but no month.
2195 For example, if today is the 15th, and you enter \"3\", Org-mode will
2196 read this as the third of *next* month. However, if you enter \"17\",
2197 it will be considered as *this* month.
2199 If you set this variable to the symbol `time', then also the following
2202 3. If the user gives a time, but no day. If the time is before now,
2203 to will be interpreted as tomorrow.
2205 Currently none of this works for ISO week specifications.
2207 When this option is nil, the current day, month and year will always be
2211 (const :tag
"Never" nil
)
2212 (const :tag
"Check month and day" t
)
2213 (const :tag
"Check month, day, and time" time
)))
2215 (defcustom org-read-date-display-live t
2216 "Non-nil means, display current interpretation of date prompt live.
2217 This display will be in an overlay, in the minibuffer."
2221 (defcustom org-read-date-popup-calendar t
2222 "Non-nil means, pop up a calendar when prompting for a date.
2223 In the calendar, the date can be selected with mouse-1. However, the
2224 minibuffer will also be active, and you can simply enter the date as well.
2225 When nil, only the minibuffer will be available."
2228 (if (fboundp 'defvaralias
)
2229 (defvaralias 'org-popup-calendar-for-date-prompt
2230 'org-read-date-popup-calendar
))
2232 (defcustom org-read-date-minibuffer-setup-hook nil
2233 "Hook to be used to set up keys for the date/time interface.
2234 Add key definitions to `minibuffer-local-map', which will be a temporary
2239 (defcustom org-extend-today-until
0
2240 "The hour when your day really ends. Must be an integer.
2241 This has influence for the following applications:
2242 - When switching the agenda to \"today\". It it is still earlier than
2243 the time given here, the day recognized as TODAY is actually yesterday.
2244 - When a date is read from the user and it is still before the time given
2245 here, the current date and time will be assumed to be yesterday, 23:59.
2246 Also, timestamps inserted in remember templates follow this rule.
2248 IMPORTANT: This is a feature whose implementation is and likely will
2249 remain incomplete. Really, it is only here because past midnight seems to
2250 be the favorite working time of John Wiegley :-)"
2254 (defcustom org-edit-timestamp-down-means-later nil
2255 "Non-nil means, S-down will increase the time in a time stamp.
2256 When nil, S-up will increase."
2260 (defcustom org-calendar-follow-timestamp-change t
2261 "Non-nil means, make the calendar window follow timestamp changes.
2262 When a timestamp is modified and the calendar window is visible, it will be
2263 moved to the new date."
2267 (defgroup org-tags nil
2268 "Options concerning tags in Org-mode."
2272 (defcustom org-tag-alist nil
2273 "List of tags allowed in Org-mode files.
2274 When this list is nil, Org-mode will base TAG input on what is already in the
2276 The value of this variable is an alist, the car of each entry must be a
2277 keyword as a string, the cdr may be a character that is used to select
2278 that tag through the fast-tag-selection interface.
2279 See the manual for details."
2283 (cons (string :tag
"Tag name")
2284 (character :tag
"Access char"))
2285 (const :tag
"Start radio group" (:startgroup
))
2286 (const :tag
"End radio group" (:endgroup
))
2287 (const :tag
"New line" (:newline
)))))
2289 (defcustom org-tag-persistent-alist nil
2290 "List of tags that will always appear in all Org-mode files.
2291 This is in addition to any in buffer settings or customizations
2293 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2294 The value of this variable is an alist, the car of each entry must be a
2295 keyword as a string, the cdr may be a character that is used to select
2296 that tag through the fast-tag-selection interface.
2297 See the manual for details.
2298 To disable these tags on a per-file basis, insert anywhere in the file:
2303 (cons (string :tag
"Tag name")
2304 (character :tag
"Access char"))
2305 (const :tag
"Start radio group" (:startgroup
))
2306 (const :tag
"End radio group" (:endgroup
))
2307 (const :tag
"New line" (:newline
)))))
2309 (defvar org-file-tags nil
2310 "List of tags that can be inherited by all entries in the file.
2311 The tags will be inherited if the variable `org-use-tag-inheritance'
2312 says they should be.
2313 This variable is populated from #+TAG lines.")
2315 (defcustom org-use-fast-tag-selection
'auto
2316 "Non-nil means, use fast tag selection scheme.
2317 This is a special interface to select and deselect tags with single keys.
2318 When nil, fast selection is never used.
2319 When the symbol `auto', fast selection is used if and only if selection
2320 characters for tags have been configured, either through the variable
2321 `org-tag-alist' or through a #+TAGS line in the buffer.
2322 When t, fast selection is always used and selection keys are assigned
2323 automatically if necessary."
2326 (const :tag
"Always" t
)
2327 (const :tag
"Never" nil
)
2328 (const :tag
"When selection characters are configured" 'auto
)))
2330 (defcustom org-fast-tag-selection-single-key nil
2331 "Non-nil means, fast tag selection exits after first change.
2332 When nil, you have to press RET to exit it.
2333 During fast tag selection, you can toggle this flag with `C-c'.
2334 This variable can also have the value `expert'. In this case, the window
2335 displaying the tags menu is not even shown, until you press C-c again."
2338 (const :tag
"No" nil
)
2339 (const :tag
"Yes" t
)
2340 (const :tag
"Expert" expert
)))
2342 (defvar org-fast-tag-selection-include-todo nil
2343 "Non-nil means, fast tags selection interface will also offer TODO states.
2344 This is an undocumented feature, you should not rely on it.")
2346 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2347 "The column to which tags should be indented in a headline.
2348 If this number is positive, it specifies the column. If it is negative,
2349 it means that the tags should be flushright to that column. For example,
2350 -80 works well for a normal 80 character screen."
2354 (defcustom org-auto-align-tags t
2355 "Non-nil means, realign tags after pro/demotion of TODO state change.
2356 These operations change the length of a headline and therefore shift
2357 the tags around. With this options turned on, after each such operation
2358 the tags are again aligned to `org-tags-column'."
2362 (defcustom org-use-tag-inheritance t
2363 "Non-nil means, tags in levels apply also for sublevels.
2364 When nil, only the tags directly given in a specific line apply there.
2365 This may also be a list of tags that should be inherited, or a regexp that
2366 matches tags that should be inherited. Additional control is possible
2367 with the variable `org-tags-exclude-from-inheritance' which gives an
2368 explicit list of tags to be excluded from inheritance., even if the value of
2369 `org-use-tag-inheritance' would select it for inheritance.
2371 If this option is t, a match early-on in a tree can lead to a large
2372 number of matches in the subtree when constructing the agenda or creating
2373 a sparse tree. If you only want to see the first match in a tree during
2374 a search, check out the variable `org-tags-match-list-sublevels'."
2377 (const :tag
"Not" nil
)
2378 (const :tag
"Always" t
)
2379 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2380 (regexp :tag
"Tags matched by regexp")))
2382 (defcustom org-tags-exclude-from-inheritance nil
2383 "List of tags that should never be inherited.
2384 This is a way to exclude a few tags from inheritance. For way to do
2385 the opposite, to actively allow inheritance for selected tags,
2386 see the variable `org-use-tag-inheritance'."
2388 :type
'(repeat (string :tag
"Tag")))
2390 (defun org-tag-inherit-p (tag)
2391 "Check if TAG is one that should be inherited."
2393 ((member tag org-tags-exclude-from-inheritance
) nil
)
2394 ((eq org-use-tag-inheritance t
) t
)
2395 ((not org-use-tag-inheritance
) nil
)
2396 ((stringp org-use-tag-inheritance
)
2397 (string-match org-use-tag-inheritance tag
))
2398 ((listp org-use-tag-inheritance
)
2399 (member tag org-use-tag-inheritance
))
2400 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2402 (defcustom org-tags-match-list-sublevels t
2403 "Non-nil means list also sublevels of headlines matching a search.
2404 This variable applies to tags/property searches, and also to stuck
2405 projects because this search is based on a tags match as well.
2407 When set to the symbol `indented', sublevels are indented with
2410 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2411 the sublevels of a headline matching a tag search often also match
2412 the same search. Listing all of them can create very long lists.
2413 Setting this variable to nil causes subtrees of a match to be skipped.
2415 This variable is semi-obsolete and probably should always be true. It
2416 is better to limit inheritance to certain tags using the variables
2417 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
2420 (const :tag
"No, don't list them" nil
)
2421 (const :tag
"Yes, do list them" t
)
2422 (const :tag
"List them, indented with leading dots" indented
)))
2424 (defcustom org-tags-sort-function nil
2425 "When set, tags are sorted using this function as a comparator"
2428 (const :tag
"No sorting" nil
)
2429 (const :tag
"Alphabetical" string
<)
2430 (const :tag
"Reverse alphabetical" string
>)
2431 (function :tag
"Custom function" nil
)))
2433 (defvar org-tags-history nil
2434 "History of minibuffer reads for tags.")
2435 (defvar org-last-tags-completion-table nil
2436 "The last used completion table for tags.")
2437 (defvar org-after-tags-change-hook nil
2438 "Hook that is run after the tags in a line have changed.")
2440 (defgroup org-properties nil
2441 "Options concerning properties in Org-mode."
2442 :tag
"Org Properties"
2445 (defcustom org-property-format
"%-10s %s"
2446 "How property key/value pairs should be formatted by `indent-line'.
2447 When `indent-line' hits a property definition, it will format the line
2448 according to this format, mainly to make sure that the values are
2449 lined-up with respect to each other."
2450 :group
'org-properties
2453 (defcustom org-use-property-inheritance nil
2454 "Non-nil means, properties apply also for sublevels.
2456 This setting is chiefly used during property searches. Turning it on can
2457 cause significant overhead when doing a search, which is why it is not
2460 When nil, only the properties directly given in the current entry count.
2461 When t, every property is inherited. The value may also be a list of
2462 properties that should have inheritance, or a regular expression matching
2463 properties that should be inherited.
2465 However, note that some special properties use inheritance under special
2466 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
2467 and the properties ending in \"_ALL\" when they are used as descriptor
2468 for valid values of a property.
2470 Note for programmers:
2471 When querying an entry with `org-entry-get', you can control if inheritance
2472 should be used. By default, `org-entry-get' looks only at the local
2473 properties. You can request inheritance by setting the inherit argument
2474 to t (to force inheritance) or to `selective' (to respect the setting
2476 :group
'org-properties
2478 (const :tag
"Not" nil
)
2479 (const :tag
"Always" t
)
2480 (repeat :tag
"Specific properties" (string :tag
"Property"))
2481 (regexp :tag
"Properties matched by regexp")))
2483 (defun org-property-inherit-p (property)
2484 "Check if PROPERTY is one that should be inherited."
2486 ((eq org-use-property-inheritance t
) t
)
2487 ((not org-use-property-inheritance
) nil
)
2488 ((stringp org-use-property-inheritance
)
2489 (string-match org-use-property-inheritance property
))
2490 ((listp org-use-property-inheritance
)
2491 (member property org-use-property-inheritance
))
2492 (t (error "Invalid setting of `org-use-property-inheritance'"))))
2494 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
2495 "The default column format, if no other format has been defined.
2496 This variable can be set on the per-file basis by inserting a line
2498 #+COLUMNS: %25ITEM ....."
2499 :group
'org-properties
2502 (defcustom org-columns-ellipses
".."
2503 "The ellipses to be used when a field in column view is truncated.
2504 When this is the empty string, as many characters as possible are shown,
2505 but then there will be no visual indication that the field has been truncated.
2506 When this is a string of length N, the last N characters of a truncated
2507 field are replaced by this string. If the column is narrower than the
2508 ellipses string, only part of the ellipses string will be shown."
2509 :group
'org-properties
2512 (defcustom org-columns-modify-value-for-display-function nil
2513 "Function that modifies values for display in column view.
2514 For example, it can be used to cut out a certain part from a time stamp.
2515 The function must take 2 arguments:
2517 column-title The title of the column (*not* the property name)
2518 value The value that should be modified.
2520 The function should return the value that should be displayed,
2521 or nil if the normal value should be used."
2522 :group
'org-properties
2525 (defcustom org-effort-property
"Effort"
2526 "The property that is being used to keep track of effort estimates.
2527 Effort estimates given in this property need to have the format H:MM."
2528 :group
'org-properties
2529 :group
'org-progress
2530 :type
'(string :tag
"Property"))
2532 (defconst org-global-properties-fixed
2533 '(("VISIBILITY_ALL" .
"folded children content all")
2534 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
2535 "List of property/value pairs that can be inherited by any entry.
2537 These are fixed values, for the preset properties. The user variable
2538 that can be used to add to this list is `org-global-properties'.
2540 The entries in this list are cons cells where the car is a property
2541 name and cdr is a string with the value. If the value represents
2542 multiple items like an \"_ALL\" property, separate the items by
2545 (defcustom org-global-properties nil
2546 "List of property/value pairs that can be inherited by any entry.
2548 This list will be combined with the constant `org-global-properties-fixed'.
2550 The entries in this list are cons cells where the car is a property
2551 name and cdr is a string with the value.
2553 You can set buffer-local values for the same purpose in the variable
2554 `org-file-properties' this by adding lines like
2556 #+PROPERTY: NAME VALUE"
2557 :group
'org-properties
2559 (cons (string :tag
"Property")
2560 (string :tag
"Value"))))
2562 (defvar org-file-properties nil
2563 "List of property/value pairs that can be inherited by any entry.
2564 Valid for the current buffer.
2565 This variable is populated from #+PROPERTY lines.")
2566 (make-variable-buffer-local 'org-file-properties
)
2568 (defgroup org-agenda nil
2569 "Options concerning agenda views in Org-mode."
2573 (defvar org-category nil
2574 "Variable used by org files to set a category for agenda display.
2575 Such files should use a file variable to set it, for example
2577 # -*- mode: org; org-category: \"ELisp\"
2579 or contain a special line
2583 If the file does not specify a category, then file's base name
2585 (make-variable-buffer-local 'org-category
)
2586 (put 'org-category
'safe-local-variable
'(lambda (x) (or (symbolp x
) (stringp x
))))
2588 (defcustom org-agenda-files nil
2589 "The files to be used for agenda display.
2590 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
2591 \\[org-remove-file]. You can also use customize to edit the list.
2593 If an entry is a directory, all files in that directory that are matched by
2594 `org-agenda-file-regexp' will be part of the file list.
2596 If the value of the variable is not a list but a single file name, then
2597 the list of agenda files is actually stored and maintained in that file, one
2598 agenda file per line."
2601 (repeat :tag
"List of files and directories" file
)
2602 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
2604 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
2605 "Regular expression to match files for `org-agenda-files'.
2606 If any element in the list in that variable contains a directory instead
2607 of a normal file, all files in that directory that are matched by this
2608 regular expression will be included."
2612 (defcustom org-agenda-text-search-extra-files nil
2613 "List of extra files to be searched by text search commands.
2614 These files will be search in addition to the agenda files by the
2615 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
2616 Note that these files will only be searched for text search commands,
2617 not for the other agenda views like todo lists, tag searches or the weekly
2618 agenda. This variable is intended to list notes and possibly archive files
2619 that should also be searched by these two commands.
2620 In fact, if the first element in the list is the symbol `agenda-archives',
2621 than all archive files of all agenda files will be added to the search
2624 :type
'(set :greedy t
2625 (const :tag
"Agenda Archives" agenda-archives
)
2626 (repeat :inline t
(file))))
2628 (if (fboundp 'defvaralias
)
2629 (defvaralias 'org-agenda-multi-occur-extra-files
2630 'org-agenda-text-search-extra-files
))
2632 (defcustom org-agenda-skip-unavailable-files nil
2633 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
2634 A nil value means to remove them, after a query, from the list."
2638 (defcustom org-calendar-to-agenda-key
[?c
]
2639 "The key to be installed in `calendar-mode-map' for switching to the agenda.
2640 The command `org-calendar-goto-agenda' will be bound to this key. The
2641 default is the character `c' because then `c' can be used to switch back and
2642 forth between agenda and calendar."
2646 (defcustom org-calendar-agenda-action-key
[?k
]
2647 "The key to be installed in `calendar-mode-map' for agenda-action.
2648 The command `org-agenda-action' will be bound to this key. The
2649 default is the character `k' because we use the same key in the agenda."
2653 (eval-after-load "calendar"
2655 (org-defkey calendar-mode-map org-calendar-to-agenda-key
2656 'org-calendar-goto-agenda
)
2657 (org-defkey calendar-mode-map org-calendar-agenda-action-key
2658 'org-agenda-action
)))
2660 (defgroup org-latex nil
2661 "Options for embedding LaTeX code into Org-mode."
2665 (defcustom org-format-latex-options
2666 '(:foreground default
:background default
:scale
1.0
2667 :html-foreground
"Black" :html-background
"Transparent" :html-scale
1.0
2668 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
2669 "Options for creating images from LaTeX fragments.
2670 This is a property list with the following properties:
2671 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
2672 `default' means use the foreground of the default face.
2673 :background the background color, or \"Transparent\".
2674 `default' means use the background of the default face.
2675 :scale a scaling factor for the size of the images.
2676 :html-foreground, :html-background, :html-scale
2677 the same numbers for HTML export.
2678 :matchers a list indicating which matchers should be used to
2679 find LaTeX fragments. Valid members of this list are:
2680 \"begin\" find environments
2681 \"$1\" find single characters surrounded by $.$
2682 \"$\" find math expressions surrounded by $...$
2683 \"$$\" find math expressions surrounded by $$....$$
2684 \"\\(\" find math expressions surrounded by \\(...\\)
2685 \"\\ [\" find math expressions surrounded by \\ [...\\]"
2689 (defcustom org-format-latex-header
"\\documentclass{article}
2690 \\usepackage{amssymb}
2691 \\usepackage[usenames]{color}
2692 \\usepackage{amsmath}
2693 \\usepackage{latexsym}
2694 \\usepackage[mathscr]{eucal}
2695 \\pagestyle{empty} % do not remove
2696 % The settings below are copied from fullpage.sty
2697 \\setlength{\\textwidth}{\\paperwidth}
2698 \\addtolength{\\textwidth}{-3cm}
2699 \\setlength{\\oddsidemargin}{1.5cm}
2700 \\addtolength{\\oddsidemargin}{-2.54cm}
2701 \\setlength{\\evensidemargin}{\\oddsidemargin}
2702 \\setlength{\\textheight}{\\paperheight}
2703 \\addtolength{\\textheight}{-\\headheight}
2704 \\addtolength{\\textheight}{-\\headsep}
2705 \\addtolength{\\textheight}{-\\footskip}
2706 \\addtolength{\\textheight}{-3cm}
2707 \\setlength{\\topmargin}{1.5cm}
2708 \\addtolength{\\topmargin}{-2.54cm}"
2709 "The document header used for processing LaTeX fragments.
2710 It is imperative that this header make sure that no page number
2711 appears on the page."
2716 (defgroup org-font-lock nil
2717 "Font-lock settings for highlighting in Org-mode."
2718 :tag
"Org Font Lock"
2721 (defcustom org-level-color-stars-only nil
2722 "Non-nil means fontify only the stars in each headline.
2723 When nil, the entire headline is fontified.
2724 Changing it requires restart of `font-lock-mode' to become effective
2725 also in regions already fontified."
2726 :group
'org-font-lock
2729 (defcustom org-hide-leading-stars nil
2730 "Non-nil means, hide the first N-1 stars in a headline.
2731 This works by using the face `org-hide' for these stars. This
2732 face is white for a light background, and black for a dark
2733 background. You may have to customize the face `org-hide' to
2735 Changing it requires restart of `font-lock-mode' to become effective
2736 also in regions already fontified.
2737 You may also set this on a per-file basis by adding one of the following
2738 lines to the buffer:
2740 #+STARTUP: hidestars
2741 #+STARTUP: showstars"
2742 :group
'org-font-lock
2745 (defcustom org-fontify-done-headline nil
2746 "Non-nil means, change the face of a headline if it is marked DONE.
2747 Normally, only the TODO/DONE keyword indicates the state of a headline.
2748 When this is non-nil, the headline after the keyword is set to the
2749 `org-headline-done' as an additional indication."
2750 :group
'org-font-lock
2753 (defcustom org-fontify-emphasized-text t
2754 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
2755 Changing this variable requires a restart of Emacs to take effect."
2756 :group
'org-font-lock
2759 (defcustom org-fontify-whole-heading-line nil
2760 "Non-nil means fontify the whole line for headings.
2761 This is useful when setting a background color for the
2763 :group
'org-font-lock
2766 (defcustom org-highlight-latex-fragments-and-specials nil
2767 "Non-nil means, fontify what is treated specially by the exporters."
2768 :group
'org-font-lock
2771 (defcustom org-hide-emphasis-markers nil
2772 "Non-nil mean font-lock should hide the emphasis marker characters."
2773 :group
'org-font-lock
2776 (defvar org-emph-re nil
2777 "Regular expression for matching emphasis.")
2778 (defvar org-verbatim-re nil
2779 "Regular expression for matching verbatim text.")
2780 (defvar org-emphasis-regexp-components
) ; defined just below
2781 (defvar org-emphasis-alist
) ; defined just below
2782 (defun org-set-emph-re (var val
)
2783 "Set variable and compute the emphasis regular expression."
2785 (when (and (boundp 'org-emphasis-alist
)
2786 (boundp 'org-emphasis-regexp-components
)
2787 org-emphasis-alist org-emphasis-regexp-components
)
2788 (let* ((e org-emphasis-regexp-components
)
2794 (body1 (concat body
"*?"))
2795 (markers (mapconcat 'car org-emphasis-alist
""))
2796 (vmarkers (mapconcat
2797 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
2798 org-emphasis-alist
"")))
2799 ;; make sure special characters appear at the right position in the class
2800 (if (string-match "\\^" markers
)
2801 (setq markers
(concat (replace-match "" t t markers
) "^")))
2802 (if (string-match "-" markers
)
2803 (setq markers
(concat (replace-match "" t t markers
) "-")))
2804 (if (string-match "\\^" vmarkers
)
2805 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
2806 (if (string-match "-" vmarkers
)
2807 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
2809 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
2810 (int-to-string nl
) "\\}")))
2813 (concat "\\([" pre
"]\\|^\\)"
2815 "\\([" markers
"]\\)"
2823 "\\([" post
"]\\|$\\)"))
2824 (setq org-verbatim-re
2825 (concat "\\([" pre
"]\\|^\\)"
2827 "\\([" vmarkers
"]\\)"
2835 "\\([" post
"]\\|$\\)")))))
2837 (defcustom org-emphasis-regexp-components
2838 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
2839 "Components used to build the regular expression for emphasis.
2840 This is a list with 6 entries. Terminology: In an emphasis string
2841 like \" *strong word* \", we call the initial space PREMATCH, the final
2842 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
2843 and \"trong wor\" is the body. The different components in this variable
2844 specify what is allowed/forbidden in each part:
2846 pre Chars allowed as prematch. Beginning of line will be allowed too.
2847 post Chars allowed as postmatch. End of line will be allowed too.
2848 border The chars *forbidden* as border characters.
2849 body-regexp A regexp like \".\" to match a body character. Don't use
2850 non-shy groups here, and don't allow newline here.
2851 newline The maximum number of newlines allowed in an emphasis exp.
2853 Use customize to modify this, or restart Emacs after changing it."
2854 :group
'org-font-lock
2855 :set
'org-set-emph-re
2857 (sexp :tag
"Allowed chars in pre ")
2858 (sexp :tag
"Allowed chars in post ")
2859 (sexp :tag
"Forbidden chars in border ")
2860 (sexp :tag
"Regexp for body ")
2861 (integer :tag
"number of newlines allowed")
2862 (option (boolean :tag
"Please ignore this button"))))
2864 (defcustom org-emphasis-alist
2865 `(("*" bold
"<b>" "</b>")
2866 ("/" italic
"<i>" "</i>")
2867 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
2868 ("=" org-code
"<code>" "</code>" verbatim
)
2869 ("~" org-verbatim
"<code>" "</code>" verbatim
)
2870 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
2873 "Special syntax for emphasized text.
2874 Text starting and ending with a special character will be emphasized, for
2875 example *bold*, _underlined_ and /italic/. This variable sets the marker
2876 characters, the face to be used by font-lock for highlighting in Org-mode
2877 Emacs buffers, and the HTML tags to be used for this.
2878 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
2879 Use customize to modify this, or restart Emacs after changing it."
2880 :group
'org-font-lock
2881 :set
'org-set-emph-re
2884 (string :tag
"Marker character")
2886 (face :tag
"Font-lock-face")
2887 (plist :tag
"Face property list"))
2888 (string :tag
"HTML start tag")
2889 (string :tag
"HTML end tag")
2890 (option (const verbatim
)))))
2892 (defvar org-protecting-blocks
2893 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
2894 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
2895 This is needed for font-lock setup.")
2897 ;;; Miscellaneous options
2899 (defgroup org-completion nil
2900 "Completion in Org-mode."
2901 :tag
"Org Completion"
2904 (defcustom org-completion-use-ido nil
2905 "Non-nil means, use ido completion wherever possible.
2906 Note that `ido-mode' must be active for this variable to be relevant.
2907 If you decide to turn this variable on, you might well want to turn off
2908 `org-outline-path-complete-in-steps'.
2909 See also `org-completion-use-iswitchb'."
2910 :group
'org-completion
2913 (defcustom org-completion-use-iswitchb nil
2914 "Non-nil means, use iswitchb completion wherever possible.
2915 Note that `iswitchb-mode' must be active for this variable to be relevant.
2916 If you decide to turn this variable on, you might well want to turn off
2917 `org-outline-path-complete-in-steps'.
2918 Note that thi variable has only an effect if `org-completion-use-ido' is nil."
2919 :group
'org-completion
2922 (defcustom org-completion-fallback-command
'hippie-expand
2923 "The expansion command called by \\[org-complete] in normal context.
2924 Normal means, no org-mode-specific context."
2925 :group
'org-completion
2928 ;;; Functions and variables from ther packages
2929 ;; Declared here to avoid compiler warnings
2932 (defvar outline-mode-menu-heading
)
2933 (defvar outline-mode-menu-show
)
2934 (defvar outline-mode-menu-hide
)
2935 (defvar zmacs-regions
) ; XEmacs regions
2938 (defvar mark-active
)
2941 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
2942 (declare-function calendar-forward-day
"cal-move" (arg))
2943 (declare-function calendar-goto-date
"cal-move" (date))
2944 (declare-function calendar-goto-today
"cal-move" ())
2945 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
2946 (defvar calc-embedded-close-formula
)
2947 (defvar calc-embedded-open-formula
)
2948 (declare-function cdlatex-tab
"ext:cdlatex" ())
2949 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
2950 (defvar font-lock-unfontify-region-function
)
2951 (declare-function iswitchb-read-buffer
"iswitchb"
2952 (prompt &optional default require-match start matches-set
))
2953 (defvar iswitchb-temp-buflist
)
2954 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
2955 (defvar org-agenda-tags-todo-honor-ignore-options
)
2956 (declare-function org-agenda-skip
"org-agenda" ())
2957 (declare-function org-format-agenda-item
"org-agenda"
2958 (extra txt
&optional category tags dotime noprefix remove-re
))
2959 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
2960 (declare-function org-agenda-change-all-lines
"org-agenda"
2961 (newhead hdmarker
&optional fixface just-this
))
2962 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
2963 (declare-function org-agenda-maybe-redo
"org-agenda" ())
2964 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
2966 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
2967 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
2968 "org-agenda" (&optional end
))
2969 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
2970 (declare-function org-indent-mode
"org-indent" (&optional arg
))
2971 (declare-function parse-time-string
"parse-time" (string))
2972 (defvar remember-data-file
)
2973 (defvar texmathp-why
)
2974 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
2975 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
2977 (defvar w3m-current-url
)
2978 (defvar w3m-current-title
)
2980 (defvar org-latex-regexps
)
2982 ;;; Autoload and prepare some org modules
2984 ;; Some table stuff that needs to be defined here, because it is used
2985 ;; by the functions setting up org-mode or checking for table context.
2987 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
2988 "Detects an org-type or table-type table.")
2989 (defconst org-table-line-regexp
"^[ \t]*|"
2990 "Detects an org-type table line.")
2991 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
2992 "Detects an org-type table line.")
2993 (defconst org-table-hline-regexp
"^[ \t]*|-"
2994 "Detects an org-type table hline.")
2995 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
2996 "Detects a table-type table hline.")
2997 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
2998 "Searching from within a table (any type) this finds the first line
2999 outside the table.")
3001 ;; Autoload the functions in org-table.el that are needed by functions here.
3004 (org-autoload "org-table"
3005 '(org-table-align org-table-begin org-table-blank-field
3006 org-table-convert org-table-convert-region org-table-copy-down
3007 org-table-copy-region org-table-create
3008 org-table-create-or-convert-from-region
3009 org-table-create-with-table.el org-table-current-dline
3010 org-table-cut-region org-table-delete-column org-table-edit-field
3011 org-table-edit-formulas org-table-end org-table-eval-formula
3012 org-table-export org-table-field-info
3013 org-table-get-stored-formulas org-table-goto-column
3014 org-table-hline-and-move org-table-import org-table-insert-column
3015 org-table-insert-hline org-table-insert-row org-table-iterate
3016 org-table-justify-field-maybe org-table-kill-row
3017 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3018 org-table-move-column org-table-move-column-left
3019 org-table-move-column-right org-table-move-row
3020 org-table-move-row-down org-table-move-row-up
3021 org-table-next-field org-table-next-row org-table-paste-rectangle
3022 org-table-previous-field org-table-recalculate
3023 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3024 org-table-toggle-coordinate-overlays
3025 org-table-toggle-formula-debugger org-table-wrap-region
3026 orgtbl-mode turn-on-orgtbl org-table-to-lisp
)))
3028 (defun org-at-table-p (&optional table-type
)
3029 "Return t if the cursor is inside an org-type table.
3030 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3031 (if org-enable-table-editor
3033 (beginning-of-line 1)
3034 (looking-at (if table-type org-table-any-line-regexp
3035 org-table-line-regexp
)))
3037 (defsubst org-table-p
() (org-at-table-p))
3039 (defun org-at-table.el-p
()
3040 "Return t if and only if we are at a table.el table."
3041 (and (org-at-table-p 'any
)
3043 (goto-char (org-table-begin 'any
))
3044 (looking-at org-table1-hline-regexp
))))
3045 (defun org-table-recognize-table.el
()
3046 "If there is a table.el table nearby, recognize it and move into it."
3047 (if org-table-tab-recognizes-table.el
3048 (if (org-at-table.el-p
)
3050 (beginning-of-line 1)
3051 (if (looking-at org-table-dataline-regexp
)
3053 (if (looking-at org-table1-hline-regexp
)
3055 (beginning-of-line 2)
3056 (if (looking-at org-table-any-border-regexp
)
3057 (beginning-of-line -
1)))))
3058 (if (re-search-forward "|" (org-table-end t
) t
)
3061 (if (table--at-cell-p (point))
3063 (message "recognizing table.el table...")
3064 (table-recognize-table)
3065 (message "recognizing table.el table...done")))
3066 (error "This should not happen..."))
3071 (defun org-at-table-hline-p ()
3072 "Return t if the cursor is inside a hline in a table."
3073 (if org-enable-table-editor
3075 (beginning-of-line 1)
3076 (looking-at org-table-hline-regexp
))
3079 (defvar org-table-clean-did-remove-column nil
)
3081 (defun org-table-map-tables (function)
3082 "Apply FUNCTION to the start of all tables in the buffer."
3086 (goto-char (point-min))
3087 (while (re-search-forward org-table-any-line-regexp nil t
)
3088 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
3089 (beginning-of-line 1)
3090 (when (looking-at org-table-line-regexp
)
3091 (save-excursion (funcall function
))
3092 (or (looking-at org-table-line-regexp
)
3094 (re-search-forward org-table-any-border-regexp nil
1))))
3095 (message "Mapping tables: done"))
3097 ;; Declare and autoload functions from org-exp.el & Co
3099 (declare-function org-default-export-plist
"org-exp")
3100 (declare-function org-infile-export-plist
"org-exp")
3101 (declare-function org-get-current-options
"org-exp")
3103 (org-autoload "org-exp"
3104 '(org-export org-export-visible
3105 org-insert-export-options-template
3106 org-table-clean-before-export
))
3107 (org-autoload "org-ascii"
3108 '(org-export-as-ascii org-export-ascii-preprocess
3109 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3110 org-export-region-as-ascii
))
3111 (org-autoload "org-html"
3112 '(org-export-as-html-and-open
3113 org-export-as-html-batch org-export-as-html-to-buffer
3114 org-replace-region-by-html org-export-region-as-html
3115 org-export-as-html
))
3116 (org-autoload "org-icalendar"
3117 '(org-export-icalendar-this-file
3118 org-export-icalendar-all-agenda-files
3119 org-export-icalendar-combine-agenda-files
))
3120 (org-autoload "org-xoxo" '(org-export-as-xoxo)))
3122 ;; Declare and autoload functions from org-agenda.el
3125 (org-autoload "org-agenda"
3126 '(org-agenda org-agenda-list org-search-view
3127 org-todo-list org-tags-view org-agenda-list-stuck-projects
3128 org-diary org-agenda-to-appt
3129 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
3131 ;; Autoload org-remember
3134 (org-autoload "org-remember"
3135 '(org-remember-insinuate org-remember-annotation
3136 org-remember-apply-template org-remember org-remember-handler
)))
3138 ;; Autoload org-clock.el
3141 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
3143 (declare-function org-clock-update-mode-line
"org-clock" ())
3144 (declare-function org-resolve-clocks
"org-clock"
3145 (&optional also-non-dangling-p prompt last-valid
))
3146 (defvar org-clock-start-time
)
3147 (defvar org-clock-marker
(make-marker)
3148 "Marker recording the last clock-in.")
3149 (defvar org-clock-hd-marker
(make-marker)
3150 "Marker recording the last clock-in, but the headline position.")
3151 (defun org-clock-is-active ()
3152 "Return non-nil if clock is currently running.
3153 The return value is actually the clock marker."
3154 (marker-buffer org-clock-marker
))
3159 '(org-clock-in org-clock-out org-clock-cancel
3160 org-clock-goto org-clock-sum org-clock-display
3161 org-clock-remove-overlays org-clock-report
3162 org-clocktable-shift org-dblock-write
:clocktable
3163 org-get-clocktable org-resolve-clocks
)))
3165 (defun org-clock-update-time-maybe ()
3166 "If this is a CLOCK line, update it and return t.
3167 Otherwise, return nil."
3170 (beginning-of-line 1)
3171 (skip-chars-forward " \t")
3172 (when (looking-at org-clock-string
)
3173 (let ((re (concat "[ \t]*" org-clock-string
3174 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
3175 "\\([ \t]*=>.*\\)?\\)?"))
3178 ((not (looking-at re
))
3180 ((not (match-end 2))
3181 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3182 (> org-clock-marker
(point))
3183 (<= org-clock-marker
(point-at-eol)))
3184 ;; The clock is running here
3185 (setq org-clock-start-time
3187 (org-parse-time-string (match-string 1))))
3188 (org-clock-update-mode-line)))
3190 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
3192 (setq ts
(match-string 1)
3193 te
(match-string 3))
3194 (setq s
(- (org-float-time
3195 (apply 'encode-time
(org-parse-time-string te
)))
3197 (apply 'encode-time
(org-parse-time-string ts
))))
3200 h
(floor (/ s
3600))
3204 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
3207 (defun org-check-running-clock ()
3208 "Check if the current buffer contains the running clock.
3209 If yes, offer to stop it and to save the buffer with the changes."
3210 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3211 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3214 (when (y-or-n-p "Save changed buffer?")
3217 (defun org-clocktable-try-shift (dir n
)
3218 "Check if this line starts a clock table, if yes, shift the time block."
3219 (when (org-match-line "#\\+BEGIN: clocktable\\>")
3220 (org-clocktable-shift dir n
)))
3222 ;; Autoload org-timer.el
3227 '(org-timer-start org-timer org-timer-item
3228 org-timer-change-times-in-region
3230 org-timer-reset-timers
3231 org-timer-show-remaining-time
)))
3233 ;; Autoload org-feed.el
3238 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
3241 ;; Autoload org-indent.el
3246 '(org-indent-mode)))
3248 ;; Autoload org-mobile.el
3253 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
3255 ;; Autoload archiving code
3256 ;; The stuff that is needed for cycling and tags has to be defined here.
3258 (defgroup org-archive nil
3259 "Options concerning archiving in Org-mode."
3261 :group
'org-structure
)
3263 (defcustom org-archive-location
"%s_archive::"
3264 "The location where subtrees should be archived.
3266 The value of this variable is a string, consisting of two parts,
3267 separated by a double-colon. The first part is a filename and
3268 the second part is a headline.
3270 When the filename is omitted, archiving happens in the same file.
3271 %s in the filename will be replaced by the current file
3272 name (without the directory part). Archiving to a different file
3273 is useful to keep archived entries from contributing to the
3276 The archived entries will be filed as subtrees of the specified
3277 headline. When the headline is omitted, the subtrees are simply
3278 filed away at the end of the file, as top-level entries. Also in
3279 the heading you can use %s to represent the file name, this can be
3280 useful when using the same archive for a number of different files.
3282 Here are a few examples:
3284 If the current file is Projects.org, archive in file
3285 Projects.org_archive, as top-level trees. This is the default.
3287 \"::* Archived Tasks\"
3288 Archive in the current file, under the top-level headline
3289 \"* Archived Tasks\".
3291 \"~/org/archive.org::\"
3292 Archive in file ~/org/archive.org (absolute path), as top-level trees.
3294 \"~/org/archive.org::From %s\"
3295 Archive in file ~/org/archive.org (absolute path), und headlines
3296 \"From FILENAME\" where file name is the current file name.
3298 \"basement::** Finished Tasks\"
3299 Archive in file ./basement (relative path), as level 3 trees
3300 below the level 2 heading \"** Finished Tasks\".
3302 You may set this option on a per-file basis by adding to the buffer a
3305 #+ARCHIVE: basement::** Finished Tasks
3307 You may also define it locally for a subtree by setting an ARCHIVE property
3308 in the entry. If such a property is found in an entry, or anywhere up
3309 the hierarchy, it will be used."
3313 (defcustom org-archive-tag
"ARCHIVE"
3314 "The tag that marks a subtree as archived.
3315 An archived subtree does not open during visibility cycling, and does
3316 not contribute to the agenda listings.
3317 After changing this, font-lock must be restarted in the relevant buffers to
3318 get the proper fontification."
3320 :group
'org-keywords
3323 (defcustom org-agenda-skip-archived-trees t
3324 "Non-nil means, the agenda will skip any items located in archived trees.
3325 An archived tree is a tree marked with the tag ARCHIVE. The use of this
3326 variable is no longer recommended, you should leave it at the value t.
3327 Instead, use the key `v' to cycle the archives-mode in the agenda."
3329 :group
'org-agenda-skip
3332 (defcustom org-columns-skip-arrchived-trees t
3333 "Non-nil means, irgnore archived trees when creating column view."
3335 :group
'org-properties
3338 (defcustom org-cycle-open-archived-trees nil
3339 "Non-nil means, `org-cycle' will open archived trees.
3340 An archived tree is a tree marked with the tag ARCHIVE.
3341 When nil, archived trees will stay folded. You can still open them with
3342 normal outline commands like `show-all', but not with the cycling commands."
3347 (defcustom org-sparse-tree-open-archived-trees nil
3348 "Non-nil means sparse tree construction shows matches in archived trees.
3349 When nil, matches in these trees are highlighted, but the trees are kept in
3352 :group
'org-sparse-trees
3355 (defun org-cycle-hide-archived-subtrees (state)
3356 "Re-hide all archived subtrees after a visibility state change."
3357 (when (and (not org-cycle-open-archived-trees
)
3358 (not (memq state
'(overview folded
))))
3360 (let* ((globalp (memq state
'(contents all
)))
3361 (beg (if globalp
(point-min) (point)))
3362 (end (if globalp
(point-max) (org-end-of-subtree t
))))
3363 (org-hide-archived-subtrees beg end
)
3365 (if (looking-at (concat ".*:" org-archive-tag
":"))
3366 (message "%s" (substitute-command-keys
3367 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
3369 (defun org-force-cycle-archived ()
3370 "Cycle subtree even if it is archived."
3372 (setq this-command
'org-cycle
)
3373 (let ((org-cycle-open-archived-trees t
))
3374 (call-interactively 'org-cycle
)))
3376 (defun org-hide-archived-subtrees (beg end
)
3377 "Re-hide all archived subtrees after a visibility state change."
3379 (let* ((re (concat ":" org-archive-tag
":")))
3381 (while (re-search-forward re end t
)
3382 (and (org-on-heading-p) (hide-subtree))
3383 (org-end-of-subtree t
)))))
3385 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
3388 (org-autoload "org-archive"
3389 '(org-add-archive-files org-archive-subtree
3390 org-archive-to-archive-sibling org-toggle-archive-tag
)))
3392 ;; Autoload Column View Code
3394 (declare-function org-columns-number-to-string
"org-colview")
3395 (declare-function org-columns-get-format-and-top-level
"org-colview")
3396 (declare-function org-columns-compute
"org-colview")
3398 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
3399 '(org-columns-number-to-string org-columns-get-format-and-top-level
3400 org-columns-compute org-agenda-columns org-columns-remove-overlays
3401 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
3405 (declare-function org-id-store-link
"org-id")
3406 (declare-function org-id-locations-load
"org-id")
3407 (declare-function org-id-locations-save
"org-id")
3408 (defvar org-id-track-globally
)
3409 (org-autoload "org-id"
3410 '(org-id-get-create org-id-new org-id-copy org-id-get
3411 org-id-get-with-outline-path-completion
3412 org-id-get-with-outline-drilling
3413 org-id-goto org-id-find org-id-store-link
))
3415 ;; Autoload Plotting Code
3417 (org-autoload "org-plot"
3418 '(org-plot/gnuplot
))
3420 ;;; Variables for pre-computed regular expressions, all buffer local
3422 (defvar org-drawer-regexp nil
3423 "Matches first line of a hidden block.")
3424 (make-variable-buffer-local 'org-drawer-regexp
)
3425 (defvar org-todo-regexp nil
3426 "Matches any of the TODO state keywords.")
3427 (make-variable-buffer-local 'org-todo-regexp
)
3428 (defvar org-not-done-regexp nil
3429 "Matches any of the TODO state keywords except the last one.")
3430 (make-variable-buffer-local 'org-not-done-regexp
)
3431 (defvar org-not-done-heading-regexp nil
3432 "Matches a TODO headline that is not done.")
3433 (make-variable-buffer-local 'org-not-done-regexp
)
3434 (defvar org-todo-line-regexp nil
3435 "Matches a headline and puts TODO state into group 2 if present.")
3436 (make-variable-buffer-local 'org-todo-line-regexp
)
3437 (defvar org-complex-heading-regexp nil
3438 "Matches a headline and puts everything into groups:
3440 group 2: The todo keyword, maybe
3441 group 3: Priority cookie
3442 group 4: True headline
3444 (make-variable-buffer-local 'org-complex-heading-regexp
)
3445 (defvar org-complex-heading-regexp-format nil
)
3446 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
3447 (defvar org-todo-line-tags-regexp nil
3448 "Matches a headline and puts TODO state into group 2 if present.
3449 Also put tags into group 4 if tags are present.")
3450 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
3451 (defvar org-nl-done-regexp nil
3452 "Matches newline followed by a headline with the DONE keyword.")
3453 (make-variable-buffer-local 'org-nl-done-regexp
)
3454 (defvar org-looking-at-done-regexp nil
3455 "Matches the DONE keyword a point.")
3456 (make-variable-buffer-local 'org-looking-at-done-regexp
)
3457 (defvar org-ds-keyword-length
12
3458 "Maximum length of the Deadline and SCHEDULED keywords.")
3459 (make-variable-buffer-local 'org-ds-keyword-length
)
3460 (defvar org-deadline-regexp nil
3461 "Matches the DEADLINE keyword.")
3462 (make-variable-buffer-local 'org-deadline-regexp
)
3463 (defvar org-deadline-time-regexp nil
3464 "Matches the DEADLINE keyword together with a time stamp.")
3465 (make-variable-buffer-local 'org-deadline-time-regexp
)
3466 (defvar org-deadline-line-regexp nil
3467 "Matches the DEADLINE keyword and the rest of the line.")
3468 (make-variable-buffer-local 'org-deadline-line-regexp
)
3469 (defvar org-scheduled-regexp nil
3470 "Matches the SCHEDULED keyword.")
3471 (make-variable-buffer-local 'org-scheduled-regexp
)
3472 (defvar org-scheduled-time-regexp nil
3473 "Matches the SCHEDULED keyword together with a time stamp.")
3474 (make-variable-buffer-local 'org-scheduled-time-regexp
)
3475 (defvar org-closed-time-regexp nil
3476 "Matches the CLOSED keyword together with a time stamp.")
3477 (make-variable-buffer-local 'org-closed-time-regexp
)
3479 (defvar org-keyword-time-regexp nil
3480 "Matches any of the 4 keywords, together with the time stamp.")
3481 (make-variable-buffer-local 'org-keyword-time-regexp
)
3482 (defvar org-keyword-time-not-clock-regexp nil
3483 "Matches any of the 3 keywords, together with the time stamp.")
3484 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
3485 (defvar org-maybe-keyword-time-regexp nil
3486 "Matches a timestamp, possibly preceeded by a keyword.")
3487 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
3488 (defvar org-planning-or-clock-line-re nil
3489 "Matches a line with planning or clock info.")
3490 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
3492 (defconst org-plain-time-of-day-regexp
3495 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3498 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3500 "Regular expression to match a plain time or time range.
3501 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3502 groups carry important information:
3504 1 the first time, range or not
3505 8 the second time, if it is a range.")
3507 (defconst org-plain-time-extension-regexp
3510 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
3511 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
3512 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
3513 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
3514 groups carry important information:
3517 9 minutes of duration")
3519 (defconst org-stamp-time-of-day-regexp
3521 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
3522 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
3524 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
3525 "Regular expression to match a timestamp time or time range.
3526 After a match, the following groups carry important information:
3528 1 date plus weekday, for backreferencing to make sure both times on same day
3529 2 the first time, range or not
3530 4 the second time, if it is a range.")
3532 (defconst org-startup-options
3533 '(("fold" org-startup-folded t
)
3534 ("overview" org-startup-folded t
)
3535 ("nofold" org-startup-folded nil
)
3536 ("showall" org-startup-folded nil
)
3537 ("showeverything" org-startup-folded showeverything
)
3538 ("content" org-startup-folded content
)
3539 ("indent" org-startup-indented t
)
3540 ("noindent" org-startup-indented nil
)
3541 ("hidestars" org-hide-leading-stars t
)
3542 ("showstars" org-hide-leading-stars nil
)
3543 ("odd" org-odd-levels-only t
)
3544 ("oddeven" org-odd-levels-only nil
)
3545 ("align" org-startup-align-all-tables t
)
3546 ("noalign" org-startup-align-all-tables nil
)
3547 ("customtime" org-display-custom-times t
)
3548 ("logdone" org-log-done time
)
3549 ("lognotedone" org-log-done note
)
3550 ("nologdone" org-log-done nil
)
3551 ("lognoteclock-out" org-log-note-clock-out t
)
3552 ("nolognoteclock-out" org-log-note-clock-out nil
)
3553 ("logrepeat" org-log-repeat state
)
3554 ("lognoterepeat" org-log-repeat note
)
3555 ("nologrepeat" org-log-repeat nil
)
3556 ("fninline" org-footnote-define-inline t
)
3557 ("nofninline" org-footnote-define-inline nil
)
3558 ("fnlocal" org-footnote-section nil
)
3559 ("fnauto" org-footnote-auto-label t
)
3560 ("fnprompt" org-footnote-auto-label nil
)
3561 ("fnconfirm" org-footnote-auto-label confirm
)
3562 ("fnplain" org-footnote-auto-label plain
)
3563 ("fnadjust" org-footnote-auto-adjust t
)
3564 ("nofnadjust" org-footnote-auto-adjust nil
)
3565 ("constcgs" constants-unit-system cgs
)
3566 ("constSI" constants-unit-system SI
)
3567 ("noptag" org-tag-persistent-alist nil
)
3568 ("hideblocks" org-hide-block-startup t
)
3569 ("nohideblocks" org-hide-block-startup nil
))
3570 "Variable associated with STARTUP options for org-mode.
3571 Each element is a list of three items: The startup options as written
3572 in the #+STARTUP line, the corresponding variable, and the value to
3573 set this variable to if the option is found. An optional forth element PUSH
3574 means to push this value onto the list in the variable.")
3576 (defun org-set-regexps-and-options ()
3577 "Precompute regular expressions for current buffer."
3579 (org-set-local 'org-todo-kwd-alist nil
)
3580 (org-set-local 'org-todo-key-alist nil
)
3581 (org-set-local 'org-todo-key-trigger nil
)
3582 (org-set-local 'org-todo-keywords-1 nil
)
3583 (org-set-local 'org-done-keywords nil
)
3584 (org-set-local 'org-todo-heads nil
)
3585 (org-set-local 'org-todo-sets nil
)
3586 (org-set-local 'org-todo-log-states nil
)
3587 (org-set-local 'org-file-properties nil
)
3588 (org-set-local 'org-file-tags nil
)
3589 (let ((re (org-make-options-regexp
3590 '("CATEGORY" "TODO" "COLUMNS"
3591 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
3592 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
3593 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
3595 kwds kws0 kwsa key log value cat arch tags const links hw dws
3596 tail sep kws1 prio props ftags drawers
3597 ext-setup-or-nil setup-contents
(start 0))
3601 (goto-char (point-min))
3602 (while (or (and ext-setup-or-nil
3603 (string-match re ext-setup-or-nil start
)
3604 (setq start
(match-end 0)))
3605 (and (setq ext-setup-or-nil nil start
0)
3606 (re-search-forward re nil t
)))
3607 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
3608 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
3610 ((equal key
"CATEGORY")
3611 (if (string-match "[ \t]+$" value
)
3612 (setq value
(replace-match "" t t value
)))
3614 ((member key
'("SEQ_TODO" "TODO"))
3615 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
3616 ((equal key
"TYP_TODO")
3617 (push (cons 'type
(org-split-string value splitre
)) kwds
))
3618 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
3619 ;; general TODO-like setup
3620 (push (cons (intern (downcase (match-string 1 key
)))
3621 (org-split-string value splitre
)) kwds
))
3623 (setq tags
(append tags
(if tags
'("\\n") nil
)
3624 (org-split-string value splitre
))))
3625 ((equal key
"COLUMNS")
3626 (org-set-local 'org-columns-default-format value
))
3628 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
3629 (push (cons (match-string 1 value
)
3630 (org-trim (match-string 2 value
)))
3632 ((equal key
"PRIORITIES")
3633 (setq prio
(org-split-string value
" +")))
3634 ((equal key
"PROPERTY")
3635 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
3636 (push (cons (match-string 1 value
) (match-string 2 value
))
3638 ((equal key
"FILETAGS")
3639 (when (string-match "\\S-" value
)
3644 (mapcar (lambda (x) (org-split-string x
":"))
3645 (org-split-string value
)))))))
3646 ((equal key
"DRAWERS")
3647 (setq drawers
(org-split-string value splitre
)))
3648 ((equal key
"CONSTANTS")
3649 (setq const
(append const
(org-split-string value splitre
))))
3650 ((equal key
"STARTUP")
3651 (let ((opts (org-split-string value splitre
))
3653 (while (setq l
(pop opts
))
3654 (when (setq l
(assoc l org-startup-options
))
3655 (setq var
(nth 1 l
) val
(nth 2 l
))
3657 (set (make-local-variable var
) val
)
3658 (if (not (listp (symbol-value var
)))
3659 (set (make-local-variable var
) nil
))
3660 (set (make-local-variable var
) (symbol-value var
))
3661 (add-to-list var val
))))))
3662 ((equal key
"ARCHIVE")
3663 (string-match " *$" value
)
3664 (setq arch
(replace-match "" t t value
))
3665 (remove-text-properties 0 (length arch
)
3666 '(face t fontified t
) arch
))
3667 ((equal key
"SETUPFILE")
3668 (setq setup-contents
(org-file-contents
3670 (org-remove-double-quotes value
))
3672 (if (not ext-setup-or-nil
)
3673 (setq ext-setup-or-nil setup-contents start
0)
3674 (setq ext-setup-or-nil
3675 (concat (substring ext-setup-or-nil
0 start
)
3676 "\n" setup-contents
"\n"
3677 (substring ext-setup-or-nil start
)))))
3680 (org-set-local 'org-category
(intern cat
))
3681 (push (cons "CATEGORY" cat
) props
))
3683 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
3684 (setq prio
(mapcar 'string-to-char prio
))
3685 (org-set-local 'org-highest-priority
(nth 0 prio
))
3686 (org-set-local 'org-lowest-priority
(nth 1 prio
))
3687 (org-set-local 'org-default-priority
(nth 2 prio
)))
3688 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
3689 (and ftags
(org-set-local 'org-file-tags
3690 (mapcar 'org-add-prop-inherited ftags
)))
3691 (and drawers
(org-set-local 'org-drawers drawers
))
3692 (and arch
(org-set-local 'org-archive-location arch
))
3693 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
3694 ;; Process the TODO keywords
3696 ;; Use the global values as if they had been given locally.
3697 (setq kwds
(default-value 'org-todo-keywords
))
3698 (if (stringp (car kwds
))
3699 (setq kwds
(list (cons org-todo-interpretation
3700 (default-value 'org-todo-keywords
)))))
3701 (setq kwds
(reverse kwds
)))
3702 (setq kwds
(nreverse kwds
))
3704 (while (setq kws
(pop kwds
))
3706 (run-hook-with-args-until-success
3707 'org-todo-setup-filter-hook kws
)
3709 (setq inter
(pop kws
) sep
(member "|" kws
)
3710 kws0
(delete "|" (copy-sequence kws
))
3715 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
3717 (setq kw
(match-string 1 x
)
3718 key
(and (match-end 2) (match-string 2 x
))
3719 log
(org-extract-log-state-settings x
))
3720 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
3721 (and log
(push log org-todo-log-states
))
3723 (error "Invalid TODO keyword %s" x
)))
3725 kwsa
(if kwsa
(append '((:startgroup
))
3729 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
3730 tail
(list inter hw
(car dws
) (org-last dws
))))
3731 (add-to-list 'org-todo-heads hw
'append
)
3732 (push kws1 org-todo-sets
)
3733 (setq org-done-keywords
(append org-done-keywords dws nil
))
3734 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
3735 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
3736 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
3737 (setq org-todo-sets
(nreverse org-todo-sets
)
3738 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
3739 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
3740 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
3741 ;; Process the constants
3744 (while (setq e
(pop const
))
3745 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
3746 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
3747 (setq org-table-formula-constants-local cst
)))
3749 ;; Process the tags.
3752 (while (setq e
(pop tags
))
3754 ((equal e
"{") (push '(:startgroup
) tgs
))
3755 ((equal e
"}") (push '(:endgroup
) tgs
))
3756 ((equal e
"\\n") (push '(:newline
) tgs
))
3757 ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e
)
3758 (push (cons (match-string 1 e
)
3759 (string-to-char (match-string 2 e
)))
3761 (t (push (list e
) tgs
))))
3762 (org-set-local 'org-tag-alist nil
)
3763 (while (setq e
(pop tgs
))
3764 (or (and (stringp (car e
))
3765 (assoc (car e
) org-tag-alist
))
3766 (push e org-tag-alist
)))))
3768 ;; Compute the regular expressions and other local variables
3769 (if (not org-done-keywords
)
3770 (setq org-done-keywords
(and org-todo-keywords-1
3771 (list (org-last org-todo-keywords-1
)))))
3772 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
3773 (length org-scheduled-string
)
3774 (length org-clock-string
)
3775 (length org-closed-string
)))
3777 (concat "^[ \t]*:\\("
3778 (mapconcat 'regexp-quote org-drawers
"\\|")
3780 org-not-done-keywords
3781 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
3783 (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
3787 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3789 org-not-done-heading-regexp
3790 (concat "^\\(\\*+\\)[ \t]+\\("
3791 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
3793 org-todo-line-regexp
3794 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3795 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3796 "\\)\\>\\)?[ \t]*\\(.*\\)")
3797 org-complex-heading-regexp
3798 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3799 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3800 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3801 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3802 org-complex-heading-regexp-format
3803 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3804 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3805 "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
3806 "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3808 (concat "\n\\*+[ \t]+"
3809 "\\(?:" (mapconcat 'regexp-quote org-done-keywords
"\\|")
3811 org-todo-line-tags-regexp
3812 (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3813 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
3815 "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
3816 org-looking-at-done-regexp
3818 (mapconcat 'regexp-quote org-done-keywords
"\\|") "\\)"
3820 org-deadline-regexp
(concat "\\<" org-deadline-string
)
3821 org-deadline-time-regexp
3822 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
3823 org-deadline-line-regexp
3824 (concat "\\<\\(" org-deadline-string
"\\).*")
3825 org-scheduled-regexp
3826 (concat "\\<" org-scheduled-string
)
3827 org-scheduled-time-regexp
3828 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
3829 org-closed-time-regexp
3830 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
3831 org-keyword-time-regexp
3832 (concat "\\<\\(" org-scheduled-string
3833 "\\|" org-deadline-string
3834 "\\|" org-closed-string
3835 "\\|" org-clock-string
"\\)"
3836 " *[[<]\\([^]>]+\\)[]>]")
3837 org-keyword-time-not-clock-regexp
3838 (concat "\\<\\(" org-scheduled-string
3839 "\\|" org-deadline-string
3840 "\\|" org-closed-string
3842 " *[[<]\\([^]>]+\\)[]>]")
3843 org-maybe-keyword-time-regexp
3844 (concat "\\(\\<\\(" org-scheduled-string
3845 "\\|" org-deadline-string
3846 "\\|" org-closed-string
3847 "\\|" org-clock-string
"\\)\\)?"
3848 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
3849 org-planning-or-clock-line-re
3850 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
3851 "\\|" org-deadline-string
3852 "\\|" org-closed-string
"\\|" org-clock-string
3855 (org-compute-latex-and-specials-regexp)
3856 (org-set-font-lock-defaults))))
3858 (defun org-file-contents (file &optional noerror
)
3859 "Return the contents of FILE, as a string."
3861 (not (file-readable-p file
)))
3864 (message "Cannot read file %s" file
)
3867 (error "Cannot read file %s" file
))
3869 (insert-file-contents file
)
3872 (defun org-extract-log-state-settings (x)
3873 "Extract the log state setting from a TODO keyword string.
3874 This will extract info from a string like \"WAIT(w@/!)\"."
3875 (let (kw key log1 log2
)
3876 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
3877 (setq kw
(match-string 1 x
)
3878 key
(and (match-end 2) (match-string 2 x
))
3879 log1
(and (match-end 3) (match-string 3 x
))
3880 log2
(and (match-end 4) (match-string 4 x
)))
3883 (and log1
(if (equal log1
"!") 'time
'note
))
3884 (and log2
(if (equal log2
"!") 'time
'note
)))))))
3886 (defun org-remove-keyword-keys (list)
3887 "Remove a pair of parenthesis at the end of each string in LIST."
3889 (if (string-match "(.*)$" x
)
3890 (substring x
0 (match-beginning 0))
3894 ;; FIXME: this could be done much better, using second characters etc.
3895 (defun org-assign-fast-keys (alist)
3896 "Assign fast keys to a keyword-key alist.
3897 Respect keys that are already there."
3898 (let (new e k c c1 c2
(char ?a
))
3899 (while (setq e
(pop alist
))
3901 ((equal e
'(:startgroup
)) (push e new
))
3902 ((equal e
'(:endgroup
)) (push e new
))
3903 ((equal e
'(:newline
)) (push e new
))
3905 (setq k
(car e
) c2 nil
)
3908 ;; automatically assign a character.
3909 (setq c1
(string-to-char
3910 (downcase (substring
3911 k
(if (= (string-to-char k
) ?
@) 1 0)))))
3912 (if (or (rassoc c1 new
) (rassoc c1 alist
))
3913 (while (or (rassoc char new
) (rassoc char alist
))
3914 (setq char
(1+ char
)))
3916 (setq c
(or c2 char
)))
3917 (push (cons k c
) new
))))
3920 ;;; Some variables used in various places
3922 (defvar org-window-configuration nil
3923 "Used in various places to store a window configuration.")
3924 (defvar org-selected-window nil
3925 "Used in various places to store a window configuration.")
3926 (defvar org-finish-function nil
3927 "Function to be called when `C-c C-c' is used.
3928 This is for getting out of special buffers like remember.")
3931 ;; FIXME: Occasionally check by commenting these, to make sure
3932 ;; no other functions uses these, forgetting to let-bind them.
3937 ;; Defined somewhere in this file, but used before definition.
3938 (defvar org-html-entities
)
3939 (defvar org-struct-menu
)
3940 (defvar org-org-menu
)
3941 (defvar org-tbl-menu
)
3942 (defvar org-agenda-keymap
)
3944 ;;;; Define the Org-mode
3946 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
3947 (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
3950 ;; We use a before-change function to check if a table might need
3952 (defvar org-table-may-need-update t
3953 "Indicates that a table might need an update.
3954 This variable is set by `org-before-change-function'.
3955 `org-table-align' sets it back to nil.")
3956 (defun org-before-change-function (beg end
)
3957 "Every change indicates that a table might need an update."
3958 (setq org-table-may-need-update t
))
3959 (defvar org-mode-map
)
3960 (defvar org-mode-hook nil
3961 "Mode hook for Org-mode, run after the mode was turned on.")
3962 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
3963 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
3964 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
3965 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
3966 (defvar org-table-buffer-is-an nil
)
3967 (defconst org-outline-regexp
"\\*+ ")
3970 (define-derived-mode org-mode outline-mode
"Org"
3971 "Outline-based notes management and organizer, alias
3972 \"Carsten's outline-mode for keeping track of everything.\"
3974 Org-mode develops organizational tasks around a NOTES file which
3975 contains information about projects as plain text. Org-mode is
3976 implemented on top of outline-mode, which is ideal to keep the content
3977 of large files well structured. It supports ToDo items, deadlines and
3978 time stamps, which magically appear in the diary listing of the Emacs
3979 calendar. Tables are easily created with a built-in table editor.
3980 Plain text URL-like links connect to websites, emails (VM), Usenet
3981 messages (Gnus), BBDB entries, and any files related to the project.
3982 For printing and sharing of notes, an Org-mode file (or a part of it)
3983 can be exported as a structured ASCII or HTML file.
3985 The following commands are available:
3989 ;; Get rid of Outline menus, they are not needed
3990 ;; Need to do this here because define-derived-mode sets up
3991 ;; the keymap so late. Still, it is a waste to call this each time
3992 ;; we switch another buffer into org-mode.
3993 (if (featurep 'xemacs
)
3994 (when (boundp 'outline-mode-menu-heading
)
3995 ;; Assume this is Greg's port, it used easymenu
3996 (easy-menu-remove outline-mode-menu-heading
)
3997 (easy-menu-remove outline-mode-menu-show
)
3998 (easy-menu-remove outline-mode-menu-hide
))
3999 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4000 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4001 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4003 (org-load-modules-maybe)
4004 (easy-menu-add org-org-menu
)
4005 (easy-menu-add org-tbl-menu
)
4006 (org-install-agenda-files-menu)
4007 (if org-descriptive-links
(org-add-to-invisibility-spec '(org-link)))
4008 (org-add-to-invisibility-spec '(org-cwidth))
4009 (org-add-to-invisibility-spec '(org-hide-block . t
))
4010 (when (featurep 'xemacs
)
4011 (org-set-local 'line-move-ignore-invisible t
))
4012 (org-set-local 'outline-regexp org-outline-regexp
)
4013 (org-set-local 'outline-level
'org-outline-level
)
4014 (when (and org-ellipsis
4015 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4016 (fboundp 'make-glyph-code
))
4017 (unless org-display-table
4018 (setq org-display-table
(make-display-table)))
4019 (set-display-table-slot
4022 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4024 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4025 (setq buffer-display-table org-display-table
))
4026 (org-set-regexps-and-options)
4027 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4028 ;; tag faces set outside customize.... force initialization.
4029 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4031 (org-set-local 'calc-embedded-open-mode
"# ")
4032 (modify-syntax-entry ?
# "<")
4033 (modify-syntax-entry ?
@ "w")
4034 (if org-startup-truncated
(setq truncate-lines t
))
4035 (org-set-local 'font-lock-unfontify-region-function
4036 'org-unfontify-region
)
4037 ;; Activate before-change-function
4038 (org-set-local 'org-table-may-need-update t
)
4039 (org-add-hook 'before-change-functions
'org-before-change-function nil
4041 ;; Check for running clock before killing a buffer
4042 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4043 ;; Paragraphs and auto-filling
4044 (org-set-autofill-regexps)
4045 (setq indent-line-function
'org-indent-line-function
)
4046 (org-update-radio-target-regexp)
4047 ;; Make sure dependence stuff works reliably, even for users who set it
4049 (if org-enforce-todo-dependencies
4050 (add-hook 'org-blocker-hook
4051 'org-block-todo-from-children-or-siblings-or-parent
)
4052 (remove-hook 'org-blocker-hook
4053 'org-block-todo-from-children-or-siblings-or-parent
))
4054 (if org-enforce-todo-checkbox-dependencies
4055 (add-hook 'org-blocker-hook
4056 'org-block-todo-from-checkboxes
)
4057 (remove-hook 'org-blocker-hook
4058 'org-block-todo-from-checkboxes
))
4060 ;; Comment characters
4061 ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
4062 (org-set-local 'comment-padding
" ")
4064 ;; Align options lines
4066 'align-mode-rules-list
4067 '((org-in-buffer-settings
4068 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4069 (modes .
'(org-mode)))))
4072 (org-set-local 'imenu-create-index-function
4073 'org-imenu-get-tree
)
4075 ;; Make isearch reveal context
4076 (if (or (featurep 'xemacs
)
4077 (not (boundp 'outline-isearch-open-invisible-function
)))
4078 ;; Emacs 21 and XEmacs make use of the hook
4079 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4080 ;; Emacs 22 deals with this through a special variable
4081 (org-set-local 'outline-isearch-open-invisible-function
4082 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4084 ;; If empty file that did not turn on org-mode automatically, make it to.
4085 (if (and org-insert-mode-line-in-empty-file
4087 (= (point-min) (point-max)))
4088 (insert "# -*- mode: org -*-\n\n"))
4090 (unless org-inhibit-startup
4091 (when org-startup-align-all-tables
4092 (let ((bmp (buffer-modified-p)))
4093 (org-table-map-tables 'org-table-align
)
4094 (set-buffer-modified-p bmp
)))
4095 (when org-startup-indented
4096 (require 'org-indent
)
4097 (org-indent-mode 1))
4098 (org-set-startup-visibility)))
4100 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
4102 (defun org-current-time ()
4103 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
4104 (if (> (car org-time-stamp-rounding-minutes
) 1)
4105 (let ((r (car org-time-stamp-rounding-minutes
))
4106 (time (decode-time)))
4108 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
4112 ;;;; Font-Lock stuff, including the activators
4114 (defvar org-mouse-map
(make-sparse-keymap))
4115 (org-defkey org-mouse-map
4116 (if (featurep 'xemacs
) [button2] [mouse-2]) 'org-open-at-mouse)
4117 (org-defkey org-mouse-map
4118 (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
4119 (when org-mouse-1-follows-link
4120 (org-defkey org-mouse-map [follow-link] 'mouse-face))
4121 (when org-tab-follows-link
4122 (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
4123 (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
4125 (require 'font-lock)
4127 (defconst org-non-link-chars "]\t\n\r<>")
4128 (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
4130 (defvar org-link-types-re nil
4131 "Matches a link that has a url-like prefix like \"http:\"")
4132 (defvar org-link-re-with-space nil
4133 "Matches a link with spaces, optional angular brackets around it.")
4134 (defvar org-link-re-with-space2 nil
4135 "Matches a link with spaces, optional angular brackets around it.")
4136 (defvar org-link-re-with-space3 nil
4137 "Matches a link with spaces, only for internal part in bracket links.")
4138 (defvar org-angle-link-re nil
4139 "Matches link with angular brackets, spaces are allowed.")
4140 (defvar org-plain-link-re nil
4141 "Matches plain link, without spaces.")
4142 (defvar org-bracket-link-regexp nil
4143 "Matches a link in double brackets.")
4144 (defvar org-bracket-link-analytic-regexp nil
4145 "Regular expression used to analyze links.
4146 Here is what the match groups contain after a match:
4152 (defvar org-bracket-link-analytic-regexp++ nil
4153 "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
4154 (defvar org-any-link-re nil
4155 "Regular expression matching any link.")
4157 (defun org-make-link-regexps ()
4158 "Update the link regular expressions.
4159 This should be called after the variable `org-link-types' has changed."
4160 (setq org-link-types-re
4162 "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
4163 org-link-re-with-space
4165 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4166 "\\([^" org-non-link-chars " ]"
4167 "[^" org-non-link-chars "]*"
4168 "[^" org-non-link-chars " ]\\)>?")
4169 org-link-re-with-space2
4171 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4172 "\\([^" org-non-link-chars " ]"
4174 "[^" org-non-link-chars " ]\\)>?")
4175 org-link-re-with-space3
4177 "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4178 "\\([^" org-non-link-chars " ]"
4182 "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4183 "\\([^" org-non-link-chars " ]"
4184 "[^" org-non-link-chars "]*"
4188 "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
4189 "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
4190 org-bracket-link-regexp
4191 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
4192 org-bracket-link-analytic-regexp
4195 "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
4198 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4200 org-bracket-link-analytic-regexp++
4203 "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
4206 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
4209 (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
4210 org-angle-link-re "\\)\\|\\("
4211 org-plain-link-re "\\)")))
4213 (org-make-link-regexps)
4215 (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
4216 "Regular expression for fast time stamp matching.")
4217 (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
4218 "Regular expression for fast time stamp matching.")
4219 (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4220 "Regular expression matching time strings for analysis.
4221 This one does not require the space after the date, so it can be used
4222 on a string that terminates immediately after the date.")
4223 (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
4224 "Regular expression matching time strings for analysis.")
4225 (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
4226 "Regular expression matching time stamps, with groups.")
4227 (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
4228 "Regular expression matching time stamps (also [..]), with groups.")
4229 (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
4230 "Regular expression matching a time stamp range.")
4231 (defconst org-tr-regexp-both
4232 (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
4233 "Regular expression matching a time stamp range.")
4234 (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
4235 org-ts-regexp "\\)?")
4236 "Regular expression matching a time stamp or time stamp range.")
4237 (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
4238 org-ts-regexp-both "\\)?")
4239 "Regular expression matching a time stamp or time stamp range.
4240 The time stamps may be either active or inactive.")
4242 (defvar org-emph-face nil)
4244 (defun org-do-emphasis-faces (limit)
4245 "Run through the buffer and add overlays to links."
4247 (while (and (not rtn) (re-search-forward org-emph-re limit t))
4248 (if (not (= (char-after (match-beginning 3))
4249 (char-after (match-beginning 4))))
4252 (setq a (assoc (match-string 3) org-emphasis-alist))
4253 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
4257 (org-remove-flyspell-overlays-in
4258 (match-beginning 0) (match-end 0)))
4259 (add-text-properties (match-beginning 2) (match-end 2)
4260 '(font-lock-multiline t))
4261 (when org-hide-emphasis-markers
4262 (add-text-properties (match-end 4) (match-beginning 5)
4263 '(invisible org-link))
4264 (add-text-properties (match-beginning 3) (match-end 3)
4265 '(invisible org-link)))))
4269 (defun org-emphasize (&optional char)
4270 "Insert or change an emphasis, i.e. a font like bold or italic.
4271 If there is an active region, change that region to a new emphasis.
4272 If there is no region, just insert the marker characters and position
4273 the cursor between them.
4274 CHAR should be either the marker character, or the first character of the
4275 HTML tag associated with that emphasis. If CHAR is a space, the means
4276 to remove the emphasis of the selected region.
4277 If char is not given (for example in an interactive call) it
4278 will be prompted for."
4280 (let ((eal org-emphasis-alist) e det
4281 (erc org-emphasis-regexp-components)
4283 (string "") beg end move tag c s)
4284 (if (org-region-active-p)
4285 (setq beg (region-beginning) end (region-end)
4286 string (buffer-substring beg end))
4289 (while (setq e (pop eal))
4290 (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
4292 (push (cons c (string-to-char (car e))) det)
4293 (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
4294 (substring tag 1)))))
4295 (setq det (nreverse det))
4297 (message "%s" (concat "Emphasis marker or tag:" prompt))
4298 (setq char (read-char-exclusive)))
4299 (setq char (or (cdr (assoc char det)) char))
4300 (if (equal char ?\ )
4301 (setq s "" move nil)
4302 (unless (assoc (char-to-string char) org-emphasis-alist)
4303 (error "No such emphasis marker: \"%c\"" char))
4304 (setq s (char-to-string char)))
4305 (while (and (> (length string) 1)
4306 (equal (substring string 0 1) (substring string -1))
4307 (assoc (substring string 0 1) org-emphasis-alist))
4308 (setq string (substring string 1 -1)))
4309 (setq string (concat s string s))
4310 (if beg (delete-region beg end))
4312 (string-match (concat "[" (nth 0 erc) "\n]")
4313 (char-to-string (char-before (point)))))
4315 (unless (string-match (concat "[" (nth 1 erc) "\n]")
4316 (char-to-string (char-after (point))))
4317 (insert " ") (backward-char 1))
4319 (and move (backward-char 1))))
4321 (defconst org-nonsticky-props
4322 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
4324 (defsubst org-rear-nonsticky-at (pos)
4325 (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
4327 (defun org-activate-plain-links (limit)
4328 "Run through the buffer and add overlays to links."
4331 (if (re-search-forward org-plain-link-re limit t)
4333 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4334 (setq f (get-text-property (match-beginning 0) 'face))
4335 (if (or (eq f 'org-tag)
4336 (and (listp f) (memq 'org-tag f)))
4338 (add-text-properties (match-beginning 0) (match-end 0)
4339 (list 'mouse-face 'highlight
4340 'keymap org-mouse-map))
4341 (org-rear-nonsticky-at (match-end 0)))
4344 (defun org-activate-code (limit)
4345 (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
4347 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4348 (remove-text-properties (match-beginning 0) (match-end 0)
4349 '(display t invisible t intangible t))
4352 (defun org-fontify-meta-lines-and-blocks (limit)
4353 "Fontify #+ lines and blocks, in the correct ways."
4354 (let ((case-fold-search t))
4355 (if (re-search-forward
4356 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
4358 (let ((beg (match-beginning 0))
4359 (beg1 (line-beginning-position 2))
4360 (dc1 (downcase (match-string 2)))
4361 (dc3 (downcase (match-string 3)))
4364 ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
4365 ;; a single line of backend-specific content
4366 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4367 (remove-text-properties (match-beginning 0) (match-end 0)
4368 '(display t invisible t intangible t))
4369 (add-text-properties (match-beginning 1) (match-end 3)
4370 '(font-lock-fontified t face org-meta-line))
4371 (add-text-properties (match-beginning 6) (match-end 6)
4372 '(font-lock-fontified t face org-block))
4374 ((and (match-end 4) (equal dc3 "begin"))
4376 (setq quoting (member (downcase (match-string 5))
4377 org-protecting-blocks))
4378 (when (re-search-forward
4379 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
4380 nil t) ;; on purpose, we look further than LIMIT
4381 (setq end (match-end 0) end1 (1- (match-beginning 0)))
4383 (remove-text-properties beg end
4384 '(display t invisible t intangible t)))
4385 (add-text-properties
4387 '(font-lock-fontified t font-lock-multiline t))
4388 (add-text-properties beg beg1 '(face org-meta-line))
4389 (add-text-properties end1 end '(face org-meta-line))
4391 (add-text-properties beg1 end1 '(face org-block)))
4393 ((not (member (char-after beg) '(?\ ?\t)))
4394 ;; just any other in-buffer setting, but not indented
4395 (add-text-properties
4397 '(font-lock-fontified t face org-meta-line))
4399 ((or (member dc1 '("begin:" "end:" "caption:" "label:"
4400 "orgtbl:" "tblfm:" "tblname:"))
4401 (and (match-end 4) (equal dc3 "attr")))
4402 (add-text-properties
4404 '(font-lock-fontified t face org-meta-line))
4406 ((member dc3 '(" " ""))
4407 (add-text-properties
4409 '(font-lock-fontified t face font-lock-comment-face)))
4412 (defun org-activate-angle-links (limit)
4413 "Run through the buffer and add overlays to links."
4414 (if (re-search-forward org-angle-link-re limit t)
4416 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4417 (add-text-properties (match-beginning 0) (match-end 0)
4418 (list 'mouse-face 'highlight
4419 'keymap org-mouse-map))
4420 (org-rear-nonsticky-at (match-end 0))
4423 (defun org-activate-footnote-links (limit)
4424 "Run through the buffer and add overlays to links."
4425 (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
4428 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4429 (add-text-properties (match-beginning 2) (match-end 2)
4430 (list 'mouse-face 'highlight
4431 'keymap org-mouse-map
4433 (if (= (point-at-bol) (match-beginning 2))
4434 "Footnote definition"
4435 "Footnote reference")
4437 (org-rear-nonsticky-at (match-end 2))
4440 (defun org-activate-bracket-links (limit)
4441 "Run through the buffer and add overlays to bracketed links."
4442 (if (re-search-forward org-bracket-link-regexp limit t)
4443 (let* ((help (concat "LINK: "
4444 (org-match-string-no-properties 1)))
4445 ;; FIXME: above we should remove the escapes.
4446 ;; but that requires another match, protecting match data,
4447 ;; a lot of overhead for font-lock.
4448 (ip (org-maybe-intangible
4449 (list 'invisible 'org-link
4450 'keymap org-mouse-map 'mouse-face 'highlight
4451 'font-lock-multiline t 'help-echo help)))
4452 (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
4453 'font-lock-multiline t 'help-echo help)))
4454 ;; We need to remove the invisible property here. Table narrowing
4455 ;; may have made some of this invisible.
4456 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4457 (remove-text-properties (match-beginning 0) (match-end 0)
4461 (add-text-properties (match-beginning 0) (match-beginning 3) ip)
4462 (org-rear-nonsticky-at (match-beginning 3))
4463 (add-text-properties (match-beginning 3) (match-end 3) vp)
4464 (org-rear-nonsticky-at (match-end 3))
4465 (add-text-properties (match-end 3) (match-end 0) ip)
4466 (org-rear-nonsticky-at (match-end 0)))
4467 (add-text-properties (match-beginning 0) (match-beginning 1) ip)
4468 (org-rear-nonsticky-at (match-beginning 1))
4469 (add-text-properties (match-beginning 1) (match-end 1) vp)
4470 (org-rear-nonsticky-at (match-end 1))
4471 (add-text-properties (match-end 1) (match-end 0) ip)
4472 (org-rear-nonsticky-at (match-end 0)))
4475 (defun org-activate-dates (limit)
4476 "Run through the buffer and add overlays to dates."
4477 (if (re-search-forward org-tsr-regexp-both limit t)
4479 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4480 (add-text-properties (match-beginning 0) (match-end 0)
4481 (list 'mouse-face 'highlight
4482 'keymap org-mouse-map))
4483 (org-rear-nonsticky-at (match-end 0))
4484 (when org-display-custom-times
4486 (org-display-custom-time (match-beginning 3) (match-end 3)))
4487 (org-display-custom-time (match-beginning 1) (match-end 1)))
4490 (defvar org-target-link-regexp nil
4491 "Regular expression matching radio targets in plain text.")
4492 (make-variable-buffer-local 'org-target-link-regexp)
4493 (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
4494 "Regular expression matching a link target.")
4495 (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
4496 "Regular expression matching a radio target.")
4497 (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
4498 "Regular expression matching any target.")
4500 (defun org-activate-target-links (limit)
4501 "Run through the buffer and add overlays to target matches."
4502 (when org-target-link-regexp
4503 (let ((case-fold-search t))
4504 (if (re-search-forward org-target-link-regexp limit t)
4506 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4507 (add-text-properties (match-beginning 0) (match-end 0)
4508 (list 'mouse-face 'highlight
4509 'keymap org-mouse-map
4510 'help-echo "Radio target link"
4511 'org-linked-text t))
4512 (org-rear-nonsticky-at (match-end 0))
4515 (defun org-update-radio-target-regexp ()
4516 "Find all radio targets in this file and update the regular expression."
4518 (when (memq 'radio org-activate-links)
4519 (setq org-target-link-regexp
4520 (org-make-target-link-regexp (org-all-targets 'radio)))
4521 (org-restart-font-lock)))
4523 (defun org-hide-wide-columns (limit)
4525 (setq s (text-property-any (point) (or limit (point-max))
4528 (setq e (next-single-property-change s 'org-cwidth))
4529 (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
4533 (defvar org-latex-and-specials-regexp nil
4534 "Regular expression for highlighting export special stuff.")
4535 (defvar org-match-substring-regexp)
4536 (defvar org-match-substring-with-braces-regexp)
4538 ;; This should be with the exporter code, but we also use if for font-locking
4539 (defconst org-export-html-special-string-regexps
4540 '(("\\\\-" . "­")
4541 ("---\\([^-]\\)" . "—\\1")
4542 ("--\\([^-]\\)" . "–\\1")
4543 ("\\.\\.\\." . "…"))
4544 "Regular expressions for special string conversion.")
4547 (defun org-compute-latex-and-specials-regexp ()
4548 "Compute regular expression for stuff treated specially by exporters."
4549 (if (not org-highlight-latex-fragments-and-specials)
4550 (org-set-local 'org-latex-and-specials-regexp nil)
4553 ((matchers (plist-get org-format-latex-options :matchers))
4554 (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
4555 org-latex-regexps)))
4556 (options (org-combine-plists (org-default-export-plist)
4557 (org-infile-export-plist)))
4558 (org-export-with-sub-superscripts (plist-get options :sub-superscript))
4559 (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
4560 (org-export-with-TeX-macros (plist-get options :TeX-macros))
4561 (org-export-html-expand (plist-get options :expand-quoted-html))
4562 (org-export-with-special-strings (plist-get options :special-strings))
4565 ((equal org-export-with-sub-superscripts '{})
4566 (list org-match-substring-with-braces-regexp))
4567 (org-export-with-sub-superscripts
4568 (list org-match-substring-regexp))
4571 (if org-export-with-LaTeX-fragments
4572 (mapcar (lambda (x) (nth 1 x)) latexs)))
4574 (if org-export-with-TeX-macros
4575 (list (concat "\\\\"
4577 (append (mapcar 'car org-html-entities)
4578 (if (boundp 'org-latex-entities)
4580 (or (car-safe x) x))
4585 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
4586 (re-special (if org-export-with-special-strings
4587 (mapcar (lambda (x) (car x))
4588 org-export-html-special-string-regexps)))
4592 (if org-export-html-expand "@<[^>\n]+>")
4595 'org-latex-and-specials-regexp
4596 (mapconcat 'identity (append re-latex re-sub re-macros re-special
4599 (defun org-do-latex-and-special-faces (limit)
4600 "Run through the buffer and add overlays to links."
4601 (when org-latex-and-specials-regexp
4603 (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
4605 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
4607 '(org-code org-verbatim underline)))
4610 d (cond ((member (char-after (1+ (match-beginning 0)))
4613 (font-lock-prepend-text-property
4614 (+ d (match-beginning 0)) (match-end 0)
4615 'face 'org-latex-and-export-specials)
4616 (add-text-properties (+ d (match-beginning 0)) (match-end 0)
4617 '(font-lock-multiline t)))))
4620 (defun org-restart-font-lock ()
4621 "Restart font-lock-mode, to force refontification."
4622 (when (and (boundp 'font-lock-mode) font-lock-mode)
4624 (font-lock-mode 1)))
4626 (defun org-all-targets (&optional radio)
4627 "Return a list of all targets in this file.
4628 With optional argument RADIO, only find radio targets."
4629 (let ((re (if radio org-radio-target-regexp org-target-regexp))
4632 (goto-char (point-min))
4633 (while (re-search-forward re nil t)
4634 (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
4637 (defun org-make-target-link-regexp (targets)
4638 "Make regular expression matching all strings in TARGETS.
4639 The regular expression finds the targets also if there is a line break
4646 (while (string-match " +" x)
4647 (setq x (replace-match "\\s-+" t t x)))
4653 (defun org-activate-tags (limit)
4654 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
4656 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
4657 (add-text-properties (match-beginning 1) (match-end 1)
4658 (list 'mouse-face 'highlight
4659 'keymap org-mouse-map))
4660 (org-rear-nonsticky-at (match-end 1))
4663 (defun org-outline-level ()
4664 "Compute the outline level of the heading at point.
4665 This function assumes that the cursor is at the beginning of a line matched
4666 by outline-regexp. Otherwise it returns garbage.
4667 If this is called at a normal headline, the level is the number of stars.
4668 Use `org-reduced-level' to remove the effect of `org-odd-levels'.
4669 For plain list items, if they are matched by `outline-regexp', this returns
4670 1000 plus the line indentation."
4672 (looking-at outline-regexp)
4673 (if (match-beginning 1)
4674 (+ (org-get-string-indentation (match-string 1)) 1000)
4675 (1- (- (match-end 0) (match-beginning 0))))))
4677 (defvar org-font-lock-keywords nil)
4679 (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
4680 "Regular expression matching a property line.")
4682 (defvar org-font-lock-hook nil
4683 "Functions to be called for special font lock stuff.")
4685 (defun org-font-lock-hook (limit)
4686 (run-hook-with-args 'org-font-lock-hook limit))
4688 (defun org-set-font-lock-defaults ()
4689 (let* ((em org-fontify-emphasized-text)
4690 (lk org-activate-links)
4691 (org-font-lock-extra-keywords
4694 '(org-font-lock-hook)
4696 `(,(if org-fontify-whole-heading-line
4697 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
4698 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
4699 (1 (org-get-level-face 1))
4700 (2 (org-get-level-face 2))
4701 (3 (org-get-level-face 3)))
4703 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
4706 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
4707 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
4708 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
4709 '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
4711 (list org-drawer-regexp '(0 'org-special-keyword t))
4712 (list "^[ \t]*:END:" '(0 'org-special-keyword t))
4714 (list org-property-re
4715 '(1 'org-special-keyword t)
4716 '(3 'org-property-value t))
4718 (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
4719 (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
4720 (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
4721 (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
4722 (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
4723 (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
4724 (if (memq 'footnote lk) '(org-activate-footnote-links
4725 (2 'org-footnote t)))
4726 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
4727 '(org-hide-wide-columns (0 nil append))
4729 (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
4730 '(1 (org-get-todo-face 1) t))
4732 (if org-fontify-done-headline
4733 (list (concat "^[*]+ +\\<\\("
4734 (mapconcat 'regexp-quote org-done-keywords "\\|")
4736 '(2 'org-headline-done t))
4739 '(org-font-lock-add-priority-faces)
4741 '(org-font-lock-add-tag-faces)
4743 (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
4744 (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
4745 (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
4746 (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
4749 (if (featurep 'xemacs)
4750 '(org-do-emphasis-faces (0 nil append))
4751 '(org-do-emphasis-faces)))
4753 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
4754 2 'org-checkbox prepend)
4755 (if org-provide-checkbox-statistics
4756 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
4757 (0 (org-get-checkbox-statistics-face) t)))
4758 ;; Description list items
4759 '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
4761 ;; ARCHIVEd headings
4762 (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
4763 '(1 'org-archived prepend))
4765 '(org-do-latex-and-special-faces)
4767 '(org-activate-code (1 'org-code t))
4769 (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
4770 "\\|" org-quote-string "\\)\\>")
4771 '(1 'org-special-keyword t))
4772 '("^#.*" (0 'font-lock-comment-face t))
4773 ;; Blocks and meta lines
4774 '(org-fontify-meta-lines-and-blocks)
4776 (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
4777 ;; Now set the full font-lock-keywords
4778 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
4779 (org-set-local 'font-lock-defaults
4780 '(org-font-lock-keywords t nil nil backward-paragraph))
4781 (kill-local-variable 'font-lock-keywords) nil))
4783 (defun org-fontify-like-in-org-mode (s &optional odd-levels)
4784 "Fontify string S like in Org-mode"
4787 (let ((org-odd-levels-only odd-levels))
4789 (font-lock-fontify-buffer)
4795 (defun org-get-level-face (n)
4796 "Get the right face for match N in font-lock matching of headlines."
4797 (setq org-l (- (match-end 2) (match-beginning 1) 1))
4798 (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
4799 (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
4801 ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
4803 (t (if org-level-color-stars-only nil org-f))))
4805 (defun org-get-todo-face (kwd)
4806 "Get the right face for a TODO keyword KWD.
4807 If KWD is a number, get the corresponding match group."
4808 (if (numberp kwd) (setq kwd (match-string kwd)))
4809 (or (cdr (assoc kwd org-todo-keyword-faces))
4810 (and (member kwd org-done-keywords) 'org-done)
4813 (defun org-font-lock-add-tag-faces (limit)
4814 "Add the special tag faces."
4815 (when (and org-tag-faces org-tags-special-faces-re)
4816 (while (re-search-forward org-tags-special-faces-re limit t)
4817 (add-text-properties (match-beginning 1) (match-end 1)
4818 (list 'face (org-get-tag-face 1)
4819 'font-lock-fontified t))
4820 (backward-char 1))))
4822 (defun org-font-lock-add-priority-faces (limit)
4823 "Add the special priority faces."
4824 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
4825 (add-text-properties
4826 (match-beginning 0) (match-end 0)
4827 (list 'face (or (cdr (assoc (char-after (match-beginning 1))
4828 org-priority-faces))
4829 'org-special-keyword)
4830 'font-lock-fontified t))))
4832 (defun org-get-tag-face (kwd)
4833 "Get the right face for a TODO keyword KWD.
4834 If KWD is a number, get the corresponding match group."
4835 (if (numberp kwd) (setq kwd (match-string kwd)))
4836 (or (cdr (assoc kwd org-tag-faces))
4839 (defun org-unfontify-region (beg end &optional maybe_loudly)
4840 "Remove fontification and activation overlays from links."
4841 (font-lock-default-unfontify-region beg end)
4842 (let* ((buffer-undo-list t)
4843 (inhibit-read-only t) (inhibit-point-motion-hooks t)
4844 (inhibit-modification-hooks t)
4845 deactivate-mark buffer-file-name buffer-file-truename)
4846 (remove-text-properties beg end
4847 '(mouse-face t keymap t org-linked-text t
4848 invisible t intangible t
4849 line-prefix t wrap-prefix t
4850 org-no-flyspell t))))
4852 ;;;; Visibility cycling, including org-goto and indirect buffer
4856 (defvar org-cycle-global-status nil)
4857 (make-variable-buffer-local 'org-cycle-global-status)
4858 (defvar org-cycle-subtree-status nil)
4859 (make-variable-buffer-local 'org-cycle-subtree-status)
4863 (defvar org-inlinetask-min-level)
4865 (defun org-cycle (&optional arg)
4866 "TAB-action and visibility cycling for Org-mode.
4868 This is the command invoked in Org-mode by the TAB key. Its main purpose
4869 is outine visibility cycling, but it also invokes other actions
4870 in special contexts.
4872 - When this function is called with a prefix argument, rotate the entire
4873 buffer through 3 states (global cycling)
4874 1. OVERVIEW: Show only top-level headlines.
4875 2. CONTENTS: Show all headlines of all levels, but no body text.
4876 3. SHOW ALL: Show everything.
4877 When called with two `C-u C-u' prefixes, switch to the startup visibility,
4878 determined by the variable `org-startup-folded', and by any VISIBILITY
4879 properties in the buffer.
4880 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
4881 including any drawers.
4883 - When inside a table, re-align the table and move to the next field.
4885 - When point is at the beginning of a headline, rotate the subtree started
4886 by this line through 3 different states (local cycling)
4887 1. FOLDED: Only the main headline is shown.
4888 2. CHILDREN: The main headline and the direct children are shown.
4889 From this state, you can move to one of the children
4890 and zoom in further.
4891 3. SUBTREE: Show the entire subtree, including body text.
4892 If there is no subtree, switch directly from CHILDREN to FOLDED.
4894 - When there is a numeric prefix, go up to a heading with level ARG, do
4895 a `show-subtree' and return to the previous cursor position. If ARG
4896 is negative, go up that many levels.
4898 - When point is not at the beginning of a headline, execute the global
4899 binding for TAB, which is re-indenting the line. See the option
4900 `org-cycle-emulate-tab' for details.
4902 - Special case: if point is at the beginning of the buffer and there is
4903 no headline in line 1, this function will act as if called with prefix arg.
4904 But only if also the variable `org-cycle-global-at-bob' is t."
4906 (org-load-modules-maybe)
4907 (unless (run-hook-with-args-until-success 'org-tab-first-hook)
4909 (or org-cycle-max-level
4910 (and (boundp 'org-inlinetask-min-level)
4911 org-inlinetask-min-level
4912 (1- org-inlinetask-min-level))))
4913 (nstars (and limit-level
4914 (if org-odd-levels-only
4915 (and limit-level (1- (* limit-level 2)))
4919 ((not (org-mode-p)) outline-regexp)
4920 ((or (eq org-cycle-include-plain-lists 'integrate)
4921 (and org-cycle-include-plain-lists (org-at-item-p)))
4923 (if nstars (format "\\{1,%d\\}" nstars) "+")
4924 " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
4925 (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
4926 (bob-special (and org-cycle-global-at-bob (bobp)
4927 (not (looking-at outline-regexp))))
4930 (delq 'org-optimize-window-after-visibility-change
4931 (copy-sequence org-cycle-hook))
4935 (if (or bob-special (equal arg '(4)))
4936 ;; special case: use global cycling
4942 (org-set-startup-visibility)
4943 (message "Startup visibility, plus VISIBILITY properties"))
4947 (message "Entire buffer visible, including drawers"))
4949 ((org-at-table-p 'any)
4950 ;; Enter the table or move to the next field in the table
4951 (or (org-table-recognize-table.el)
4953 (if arg (org-table-edit-field t)
4954 (org-table-justify-field-maybe)
4955 (call-interactively 'org-table-next-field)))))
4957 ((run-hook-with-args-until-success
4958 'org-tab-after-check-for-table-hook))
4960 ((eq arg t) ;; Global cycling
4961 (org-cycle-internal-global))
4963 ((and org-drawers org-drawer-regexp
4965 (beginning-of-line 1)
4966 (looking-at org-drawer-regexp)))
4967 ;; Toggle block visibility
4969 (not (get-char-property (match-end 0) 'invisible))))
4972 ;; Show-subtree, ARG levels up from here.
4974 (org-back-to-heading)
4975 (outline-up-heading (if (< arg 0) (- arg)
4976 (- (funcall outline-level) arg)))
4977 (org-show-subtree)))
4979 ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
4980 (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
4982 (org-cycle-internal-local))
4984 ;; TAB emulation and template completion
4985 (buffer-read-only (org-back-to-heading))
4987 ((run-hook-with-args-until-success
4988 'org-tab-after-check-for-cycling-hook))
4990 ((org-try-structure-completion))
4992 ((org-try-cdlatex-tab))
4994 ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
4996 (not (looking-at outline-regexp))))
4997 (call-interactively (global-key-binding "\t")))
4999 ((if (and (memq org-cycle-emulate-tab '(white whitestart))
5000 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
5001 (or (and (eq org-cycle-emulate-tab 'white)
5002 (= (match-end 0) (point-at-eol)))
5003 (and (eq org-cycle-emulate-tab 'whitestart)
5004 (>= (match-end 0) pos))))
5006 (eq org-cycle-emulate-tab t))
5007 (call-interactively (global-key-binding "\t")))
5010 (org-back-to-heading)
5013 (defun org-cycle-internal-global ()
5014 "Do the global cycling action."
5016 ((and (eq last-command this-command)
5017 (eq org-cycle-global-status 'overview))
5018 ;; We just created the overview - now do table of contents
5019 ;; This can be slow in very large buffers, so indicate action
5020 (run-hook-with-args 'org-pre-cycle-hook 'contents)
5021 (message "CONTENTS...")
5023 (message "CONTENTS...done")
5024 (setq org-cycle-global-status 'contents)
5025 (run-hook-with-args 'org-cycle-hook 'contents))
5027 ((and (eq last-command this-command)
5028 (eq org-cycle-global-status 'contents))
5029 ;; We just showed the table of contents - now show everything
5030 (run-hook-with-args 'org-pre-cycle-hook 'all)
5032 (message "SHOW ALL")
5033 (setq org-cycle-global-status 'all)
5034 (run-hook-with-args 'org-cycle-hook 'all))
5037 ;; Default action: go to overview
5038 (run-hook-with-args 'org-pre-cycle-hook 'overview)
5040 (message "OVERVIEW")
5041 (setq org-cycle-global-status 'overview)
5042 (run-hook-with-args 'org-cycle-hook 'overview))))
5044 (defun org-cycle-internal-local ()
5045 "Do the local cycling action."
5046 (org-back-to-heading)
5047 (let ((goal-column 0) eoh eol eos level has-children children-skipped)
5048 ;; First, some boundaries
5050 (org-back-to-heading)
5051 (setq level (funcall outline-level))
5053 (beginning-of-line 2)
5054 (if (or (featurep 'xemacs) (<= emacs-major-version 21))
5055 ; XEmacs does not have `next-single-char-property-change'
5056 ; I'm not sure about Emacs 21.
5057 (while (and (not (eobp)) ;; this is like `next-line'
5058 (get-char-property (1- (point)) 'invisible))
5059 (beginning-of-line 2))
5060 (while (and (not (eobp)) ;; this is like `next-line'
5061 (get-char-property (1- (point)) 'invisible))
5062 (goto-char (next-single-char-property-change (point) 'invisible))
5063 ;;;??? (or (bolp) (beginning-of-line 2))))
5064 (and (eolp) (beginning-of-line 2))))
5066 (outline-end-of-heading) (setq eoh (point))
5068 (outline-next-heading)
5069 (setq has-children (and (org-at-heading-p t)
5070 (> (funcall outline-level) level))))
5071 (org-end-of-subtree t)
5073 (skip-chars-forward " \t\n")
5074 (beginning-of-line 1) ; in case this is an item
5076 (setq eos (if (eobp) (point) (1- (point)))))
5077 ;; Find out what to do next and set `this-command'
5080 ;; Nothing is hidden behind this heading
5081 (run-hook-with-args 'org-pre-cycle-hook 'empty)
5082 (message "EMPTY ENTRY")
5083 (setq org-cycle-subtree-status nil)
5086 (outline-next-heading)
5087 (if (org-invisible-p) (org-flag-heading nil))))
5088 ((and (or (>= eol eos)
5089 (not (string-match "\\S-" (buffer-substring eol eos))))
5091 (not (setq children-skipped
5092 org-cycle-skip-children-state-if-no-children))))
5093 ;; Entire subtree is hidden in one line: children view
5094 (run-hook-with-args 'org-pre-cycle-hook 'children)
5097 (message "CHILDREN")
5100 (outline-next-heading)
5101 (if (org-invisible-p) (org-flag-heading nil)))
5102 (setq org-cycle-subtree-status 'children)
5103 (run-hook-with-args 'org-cycle-hook 'children))
5104 ((or children-skipped
5105 (and (eq last-command this-command)
5106 (eq org-cycle-subtree-status 'children)))
5107 ;; We just showed the children, or no children are there,
5108 ;; now show everything.
5109 (run-hook-with-args 'org-pre-cycle-hook 'subtree)
5111 (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
5112 (setq org-cycle-subtree-status 'subtree)
5113 (run-hook-with-args 'org-cycle-hook 'subtree))
5115 ;; Default action: hide the subtree.
5116 (run-hook-with-args 'org-pre-cycle-hook 'folded)
5119 (setq org-cycle-subtree-status 'folded)
5120 (run-hook-with-args 'org-cycle-hook 'folded)))))
5123 (defun org-global-cycle (&optional arg)
5124 "Cycle the global visibility. For details see `org-cycle'.
5125 With C-u prefix arg, switch to startup visibility.
5126 With a numeric prefix, show all headlines up to that level."
5128 (let ((org-cycle-include-plain-lists
5129 (if (org-mode-p) org-cycle-include-plain-lists nil)))
5133 (hide-sublevels arg)
5134 (setq org-cycle-global-status 'contents))
5136 (org-set-startup-visibility)
5137 (message "Startup visibility, plus VISIBILITY properties."))
5139 (org-cycle '(4))))))
5141 (defun org-set-startup-visibility ()
5142 "Set the visibility required by startup options and properties."
5144 ((eq org-startup-folded t)
5146 ((eq org-startup-folded 'content)
5147 (let ((this-command 'org-cycle) (last-command 'org-cycle))
5148 (org-cycle '(4)) (org-cycle '(4)))))
5149 (unless (eq org-startup-folded 'showeverything)
5150 (if org-hide-block-startup (org-hide-block-all))
5151 (org-set-visibility-according-to-property 'no-cleanup)
5152 (org-cycle-hide-archived-subtrees 'all)
5153 (org-cycle-hide-drawers 'all)
5154 (org-cycle-show-empty-lines 'all)))
5156 (defun org-set-visibility-according-to-property (&optional no-cleanup)
5157 "Switch subtree visibilities according to :VISIBILITY: property."
5159 (let (org-show-entry-below state)
5161 (goto-char (point-min))
5162 (while (re-search-forward
5163 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
5165 (setq state (match-string 1))
5167 (org-back-to-heading t)
5171 ((equal state '("fold" "folded"))
5173 ((equal state "children")
5174 (org-show-hidden-entry)
5176 ((equal state "content")
5179 (org-narrow-to-subtree)
5181 ((member state '("all" "showall"))
5184 (org-cycle-hide-archived-subtrees 'all)
5185 (org-cycle-hide-drawers 'all)
5186 (org-cycle-show-empty-lines 'all)))))
5188 (defun org-overview ()
5189 "Switch to overview mode, showing only top-level headlines.
5190 Really, this shows all headlines with level equal or greater than the level
5191 of the first headline in the buffer. This is important, because if the
5192 first headline is not level one, then (hide-sublevels 1) gives confusing
5195 (let ((level (save-excursion
5196 (goto-char (point-min))
5197 (if (re-search-forward (concat "^" outline-regexp) nil t)
5199 (goto-char (match-beginning 0))
5200 (funcall outline-level))))))
5201 (and level (hide-sublevels level))))
5203 (defun org-content (&optional arg)
5204 "Show all headlines in the buffer, like a table of contents.
5205 With numerical argument N, show content up to level N."
5208 ;; Visit all headings and show their offspring
5209 (and (integerp arg) (org-overview))
5210 (goto-char (point-max))
5212 (while (and (progn (condition-case nil
5213 (outline-previous-visible-heading 1)
5214 (error (goto-char (point-min))))
5216 (looking-at outline-regexp))
5218 (show-children (1- arg))
5220 (if (bobp) (throw 'exit nil))))))
5223 (defun org-optimize-window-after-visibility-change (state)
5224 "Adjust the window after a change in outline visibility.
5225 This function is the default value of the hook `org-cycle-hook'."
5226 (when (get-buffer-window (current-buffer))
5228 ((eq state 'content) nil)
5229 ((eq state 'all) nil)
5230 ((eq state 'folded) nil)
5231 ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
5232 ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
5234 ;; FIXME: no longer in use
5235 (defun org-compact-display-after-subtree-move ()
5236 "Show a compacter version of the tree of the entry's parent."
5238 (if (org-up-heading-safe)
5243 (org-cycle-show-empty-lines 'children)
5244 (org-cycle-hide-drawers 'children))
5247 (defun org-remove-empty-overlays-at (pos)
5248 "Remove outline overlays that do not contain non-white stuff."
5251 (and (eq 'outline (org-overlay-get o 'invisible))
5252 (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
5253 (org-overlay-end o))))
5254 (org-delete-overlay o)))
5255 (org-overlays-at pos)))
5257 (defun org-clean-visibility-after-subtree-move ()
5258 "Fix visibility issues after moving a subtree."
5259 ;; First, find a reasonable region to look at:
5260 ;; Start two siblings above, end three below
5261 (let* ((beg (save-excursion
5262 (and (org-get-last-sibling)
5263 (org-get-last-sibling))
5265 (end (save-excursion
5266 (and (org-get-next-sibling)
5267 (org-get-next-sibling)
5268 (org-get-next-sibling))
5269 (if (org-at-heading-p)
5272 (level (looking-at "\\*+"))
5273 (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
5276 (narrow-to-region beg end)
5278 ;; Properly fold already folded siblings
5279 (goto-char (point-min))
5280 (while (re-search-forward re nil t)
5281 (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
5283 (org-cycle-show-empty-lines 'overview)
5284 (org-cycle-hide-drawers 'overview)))))
5286 (defun org-cycle-show-empty-lines (state)
5287 "Show empty lines above all visible headlines.
5288 The region to be covered depends on STATE when called through
5289 `org-cycle-hook'. Lisp program can use t for STATE to get the
5290 entire buffer covered. Note that an empty line is only shown if there
5291 are at least `org-cycle-separator-lines' empty lines before the headline."
5292 (when (not (= org-cycle-separator-lines 0))
5294 (let* ((n (abs org-cycle-separator-lines))
5296 ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
5297 ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
5298 (t (let ((ns (number-to-string (- n 2))))
5299 (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
5300 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
5303 ((memq state '(overview contents t))
5304 (setq beg (point-min) end (point-max)))
5305 ((memq state '(children folded))
5306 (setq beg (point) end (progn (org-end-of-subtree t t)
5307 (beginning-of-line 2)
5311 (while (re-search-forward re end t)
5312 (unless (get-char-property (match-end 1) 'invisible)
5313 (setq e (match-end 1))
5314 (if (< org-cycle-separator-lines 0)
5315 (setq b (save-excursion
5316 (goto-char (match-beginning 0))
5317 (org-back-over-empty-lines)
5319 (goto-char (max (point-min) (1- (point))))
5323 (setq b (match-beginning 1)))
5324 (outline-flag-region b e nil)))))))
5325 ;; Never hide empty lines at the end of the file.
5327 (goto-char (point-max))
5328 (outline-previous-heading)
5329 (outline-end-of-heading)
5330 (if (and (looking-at "[ \t\n]+")
5331 (= (match-end 0) (point-max)))
5332 (outline-flag-region (point) (match-end 0) nil))))
5334 (defun org-show-empty-lines-in-parent ()
5335 "Move to the parent and re-show empty lines before visible headlines."
5337 (let ((context (if (org-up-heading-safe) 'children 'overview)))
5338 (org-cycle-show-empty-lines context))))
5340 (defun org-files-list ()
5341 "Return `org-agenda-files' list, plus all open org-mode files.
5342 This is useful for operations that need to scan all of a user's
5343 open and agenda-wise Org files."
5344 (let ((files (mapcar 'expand-file-name org-agenda-files)))
5345 (dolist (buf (buffer-list))
5346 (with-current-buffer buf
5347 (if (and (eq major-mode 'org-mode) (buffer-file-name))
5348 (let ((file (expand-file-name (buffer-file-name))))
5349 (unless (member file files)
5350 (push file files))))))
5353 (defsubst org-entry-beginning-position ()
5354 "Return the beginning position of the current entry."
5355 (save-excursion (outline-back-to-heading t) (point)))
5357 (defsubst org-entry-end-position ()
5358 "Return the end position of the current entry."
5359 (save-excursion (outline-next-heading) (point)))
5361 (defun org-cycle-hide-drawers (state)
5362 "Re-hide all drawers after a visibility state change."
5363 (when (and (org-mode-p)
5364 (not (memq state '(overview folded contents))))
5366 (let* ((globalp (memq state '(contents all)))
5367 (beg (if globalp (point-min) (point)))
5368 (end (if globalp (point-max)
5369 (if (eq state 'children)
5370 (save-excursion (outline-next-heading) (point))
5371 (org-end-of-subtree t)))))
5373 (while (re-search-forward org-drawer-regexp end t)
5374 (org-flag-drawer t))))))
5376 (defun org-flag-drawer (flag)
5378 (beginning-of-line 1)
5379 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
5380 (let ((b (match-end 0))
5381 (outline-regexp org-outline-regexp))
5382 (if (re-search-forward
5384 (save-excursion (outline-next-heading) (point)) t)
5385 (outline-flag-region b (point-at-eol) flag)
5386 (error ":END: line missing at position %s" b))))))
5388 (defun org-subtree-end-visible-p ()
5389 "Is the end of the current subtree visible?"
5390 (pos-visible-in-window-p
5391 (save-excursion (org-end-of-subtree t) (point))))
5393 (defun org-first-headline-recenter (&optional N)
5394 "Move cursor to the first headline and recenter the headline.
5395 Optional argument N means, put the headline into the Nth line of the window."
5396 (goto-char (point-min))
5397 (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
5399 (recenter (prefix-numeric-value N))))
5401 ;;; Folding of blocks
5403 (defconst org-block-regexp
5405 "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
5406 "Regular expression for hiding blocks.")
5408 (defvar org-hide-block-overlays nil
5409 "Overays hiding blocks.")
5410 (make-variable-buffer-local 'org-hide-block-overlays)
5412 (defun org-block-map (function &optional start end)
5413 "Call func at the head of all source blocks in the current
5414 buffer. Optional arguments START and END can be used to limit
5416 (let ((start (or start (point-min)))
5417 (end (or end (point-max))))
5420 (while (and (< (point) end) (re-search-forward org-block-regexp end t))
5423 (goto-char (match-beginning 0))
5424 (funcall function)))))))
5426 (defun org-hide-block-toggle-all ()
5427 "Toggle the visibility of all blocks in the current buffer."
5428 (org-block-map #'org-hide-block-toggle))
5430 (defun org-hide-block-all ()
5431 "Fold all blocks in the current buffer."
5433 (org-show-block-all)
5434 (org-block-map #'org-hide-block-toggle-maybe))
5436 (defun org-show-block-all ()
5437 "Unfold all blocks in the current buffer."
5438 (mapc 'org-delete-overlay org-hide-block-overlays)
5439 (setq org-hide-block-overlays nil))
5441 (defun org-hide-block-toggle-maybe ()
5442 "Toggle visibility of block at point."
5444 (let ((case-fold-search t))
5446 (beginning-of-line 1)
5447 (looking-at org-block-regexp))
5448 (progn (org-hide-block-toggle)
5449 t) ;; to signal that we took action
5450 nil))) ;; to signal that we did not
5452 (defun org-hide-block-toggle (&optional force)
5453 "Toggle the visibility of the current block."
5457 (if (re-search-forward org-block-regexp nil t)
5458 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
5459 (end (match-end 0)) ;; end of entire body
5461 (if (memq t (mapcar (lambda (overlay)
5462 (eq (org-overlay-get overlay 'invisible)
5464 (org-overlays-at start)))
5465 (if (or (not force) (eq force 'off))
5467 (when (member ov org-hide-block-overlays)
5468 (setq org-hide-block-overlays
5469 (delq ov org-hide-block-overlays)))
5470 (when (eq (org-overlay-get ov 'invisible)
5472 (org-delete-overlay ov)))
5473 (org-overlays-at start)))
5474 (setq ov (org-make-overlay start end))
5475 (org-overlay-put ov 'invisible 'org-hide-block)
5476 ;; make the block accessible to isearch
5478 ov 'isearch-open-invisible
5480 (when (member ov org-hide-block-overlays)
5481 (setq org-hide-block-overlays
5482 (delq ov org-hide-block-overlays)))
5483 (when (eq (org-overlay-get ov 'invisible)
5485 (org-delete-overlay ov))))
5486 (push ov org-hide-block-overlays)))
5487 (error "Not looking at a source block"))))
5489 ;; org-tab-after-check-for-cycling-hook
5490 (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
5491 ;; Remove overlays when changing major mode
5492 (add-hook 'org-mode-hook
5493 (lambda () (org-add-hook 'change-major-mode-hook
5494 'org-show-block-all 'append 'local)))
5498 (defvar org-goto-window-configuration nil)
5499 (defvar org-goto-marker nil)
5500 (defvar org-goto-map
5501 (let ((map (make-sparse-keymap)))
5502 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
5503 (while (setq cmd (pop cmds))
5504 (substitute-key-definition cmd cmd map global-map)))
5505 (suppress-keymap map)
5506 (org-defkey map "\C-m" 'org-goto-ret)
5507 (org-defkey map [(return)] 'org-goto-ret)
5508 (org-defkey map [(left)] 'org-goto-left)
5509 (org-defkey map [(right)] 'org-goto-right)
5510 (org-defkey map [(control ?g)] 'org-goto-quit)
5511 (org-defkey map "\C-i" 'org-cycle)
5512 (org-defkey map [(tab)] 'org-cycle)
5513 (org-defkey map [(down)] 'outline-next-visible-heading)
5514 (org-defkey map [(up)] 'outline-previous-visible-heading)
5515 (if org-goto-auto-isearch
5516 (if (fboundp 'define-key-after)
5517 (define-key-after map [t] 'org-goto-local-auto-isearch)
5519 (org-defkey map "q" 'org-goto-quit)
5520 (org-defkey map "n" 'outline-next-visible-heading)
5521 (org-defkey map "p" 'outline-previous-visible-heading)
5522 (org-defkey map "f" 'outline-forward-same-level)
5523 (org-defkey map "b" 'outline-backward-same-level)
5524 (org-defkey map "u" 'outline-up-heading))
5525 (org-defkey map "/" 'org-occur)
5526 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
5527 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
5528 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
5529 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
5530 (org-defkey map "\C-c\C-u" 'outline-up-heading)
5533 (defconst org-goto-help
5534 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
5535 RET=jump to location [Q]uit and return to previous location
5536 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
5538 (defvar org-goto-start-pos) ; dynamically scoped parameter
5540 ;; FIXME: Docstring doe not mention both interfaces
5541 (defun org-goto (&optional alternative-interface)
5542 "Look up a different location in the current file, keeping current visibility.
5544 When you want look-up or go to a different location in a document, the
5545 fastest way is often to fold the entire buffer and then dive into the tree.
5546 This method has the disadvantage, that the previous location will be folded,
5547 which may not be what you want.
5549 This command works around this by showing a copy of the current buffer
5550 in an indirect buffer, in overview mode. You can dive into the tree in
5551 that copy, use org-occur and incremental search to find a location.
5552 When pressing RET or `Q', the command returns to the original buffer in
5553 which the visibility is still unchanged. After RET is will also jump to
5554 the location selected in the indirect buffer and expose the
5555 the headline hierarchy above."
5557 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
5558 (org-refile-use-outline-path t)
5559 (org-refile-target-verify-function nil)
5561 (if (not alternative-interface)
5563 (if (eq org-goto-interface 'outline)
5564 'outline-path-completion
5566 (org-goto-start-pos (point))
5568 (if (eq interface 'outline)
5569 (car (org-get-location (current-buffer) org-goto-help))
5570 (nth 3 (org-refile-get-location "Goto: ")))))
5573 (org-mark-ring-push org-goto-start-pos)
5574 (goto-char selected-point)
5575 (if (or (org-invisible-p) (org-invisible-p2))
5576 (org-show-context 'org-goto)))
5579 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
5580 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
5581 (defvar org-goto-local-auto-isearch-map) ; defined below
5583 (defun org-get-location (buf help)
5584 "Let the user select a location in the Org-mode buffer BUF.
5585 This function uses a recursive edit. It returns the selected position
5587 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
5588 (isearch-hide-immediately nil)
5589 (isearch-search-fun-function
5590 (lambda () 'org-goto-local-search-headings))
5591 (org-goto-selected-point org-goto-exit-command))
5593 (save-window-excursion
5594 (delete-other-windows)
5595 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
5598 (make-indirect-buffer (current-buffer) "*org-goto*")
5599 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
5600 (with-output-to-temp-buffer "*Help*"
5602 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
5603 (setq buffer-read-only nil)
5604 (let ((org-startup-truncated t)
5605 (org-startup-folded nil)
5606 (org-startup-align-all-tables nil))
5609 (setq buffer-read-only t)
5610 (if (and (boundp 'org-goto-start-pos)
5611 (integer-or-marker-p org-goto-start-pos))
5612 (let ((org-show-hierarchy-above t)
5613 (org-show-siblings t)
5614 (org-show-following-heading t))
5615 (goto-char org-goto-start-pos)
5616 (and (org-invisible-p) (org-show-context)))
5617 (goto-char (point-min)))
5618 (let (org-special-ctrl-a/e) (org-beginning-of-line))
5619 (message "Select location and press RET")
5620 (use-local-map org-goto-map)
5623 (kill-buffer "*org-goto*")
5624 (cons org-goto-selected-point org-goto-exit-command)))
5626 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
5627 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
5628 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
5629 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
5631 (defun org-goto-local-search-headings (string bound noerror)
5632 "Search and make sure that any matches are in headlines."
5634 (while (if isearch-forward
5635 (search-forward string bound noerror)
5636 (search-backward string bound noerror))
5637 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
5638 (and (member :headline context)
5639 (not (member :tags context))))
5640 (throw 'return (point))))))
5642 (defun org-goto-local-auto-isearch ()
5645 (goto-char (point-min))
5646 (let ((keys (this-command-keys)))
5647 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
5649 (isearch-process-search-char (string-to-char keys)))))
5651 (defun org-goto-ret (&optional arg)
5652 "Finish `org-goto' by going to the new location."
5654 (setq org-goto-selected-point (point)
5655 org-goto-exit-command 'return)
5658 (defun org-goto-left ()
5659 "Finish `org-goto' by going to the new location."
5661 (if (org-on-heading-p)
5663 (beginning-of-line 1)
5664 (setq org-goto-selected-point (point)
5665 org-goto-exit-command 'left)
5667 (error "Not on a heading")))
5669 (defun org-goto-right ()
5670 "Finish `org-goto' by going to the new location."
5672 (if (org-on-heading-p)
5674 (setq org-goto-selected-point (point)
5675 org-goto-exit-command 'right)
5677 (error "Not on a heading")))
5679 (defun org-goto-quit ()
5680 "Finish `org-goto' without cursor motion."
5682 (setq org-goto-selected-point nil)
5683 (setq org-goto-exit-command 'quit)
5686 ;;; Indirect buffer display of subtrees
5688 (defvar org-indirect-dedicated-frame nil
5689 "This is the frame being used for indirect tree display.")
5690 (defvar org-last-indirect-buffer nil)
5692 (defun org-tree-to-indirect-buffer (&optional arg)
5693 "Create indirect buffer and narrow it to current subtree.
5694 With numerical prefix ARG, go up to this level and then take that tree.
5695 If ARG is negative, go up that many levels.
5696 If `org-indirect-buffer-display' is not `new-frame', the command removes the
5697 indirect buffer previously made with this command, to avoid proliferation of
5698 indirect buffers. However, when you call the command with a `C-u' prefix, or
5699 when `org-indirect-buffer-display' is `new-frame', the last buffer
5700 is kept so that you can work with several indirect buffers at the same time.
5701 If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
5702 requests that a new frame be made for the new buffer, so that the dedicated
5703 frame is not changed."
5705 (let ((cbuf (current-buffer))
5706 (cwin (selected-window))
5708 beg end level heading ibuf)
5710 (org-back-to-heading t)
5712 (setq level (org-outline-level))
5713 (if (< arg 0) (setq arg (+ level arg)))
5714 (while (> (setq level (org-outline-level)) arg)
5715 (outline-up-heading 1 t)))
5717 heading (org-get-heading))
5718 (org-end-of-subtree t t) (setq end (point)))
5719 (if (and (buffer-live-p org-last-indirect-buffer)
5720 (not (eq org-indirect-buffer-display 'new-frame))
5722 (kill-buffer org-last-indirect-buffer))
5723 (setq ibuf (org-get-indirect-buffer cbuf)
5724 org-last-indirect-buffer ibuf)
5726 ((or (eq org-indirect-buffer-display 'new-frame)
5727 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
5728 (select-frame (make-frame))
5729 (delete-other-windows)
5730 (switch-to-buffer ibuf)
5731 (org-set-frame-title heading))
5732 ((eq org-indirect-buffer-display 'dedicated-frame)
5734 (select-frame (or (and org-indirect-dedicated-frame
5735 (frame-live-p org-indirect-dedicated-frame)
5736 org-indirect-dedicated-frame)
5737 (setq org-indirect-dedicated-frame (make-frame)))))
5738 (delete-other-windows)
5739 (switch-to-buffer ibuf)
5740 (org-set-frame-title (concat "Indirect: " heading)))
5741 ((eq org-indirect-buffer-display 'current-window)
5742 (switch-to-buffer ibuf))
5743 ((eq org-indirect-buffer-display 'other-window)
5744 (pop-to-buffer ibuf))
5745 (t (error "Invalid value")))
5746 (if (featurep 'xemacs)
5747 (save-excursion (org-mode) (turn-on-font-lock)))
5748 (narrow-to-region beg end)
5751 (and (window-live-p cwin) (select-window cwin))))
5753 (defun org-get-indirect-buffer (&optional buffer)
5754 (setq buffer (or buffer (current-buffer)))
5755 (let ((n 1) (base (buffer-name buffer)) bname)
5756 (while (buffer-live-p
5757 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
5760 (make-indirect-buffer buffer bname 'clone)
5761 (error (make-indirect-buffer buffer bname)))))
5763 (defun org-set-frame-title (title)
5764 "Set the title of the current frame to the string TITLE."
5765 ;; FIXME: how to name a single frame in XEmacs???
5766 (unless (featurep 'xemacs)
5767 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
5769 ;;;; Structure editing
5771 ;;; Inserting headlines
5773 (defun org-previous-line-empty-p ()
5776 (or (beginning-of-line 0) t)
5778 (looking-at "[ \t]*$")))))
5780 (defun org-insert-heading (&optional force-heading)
5781 "Insert a new heading or item with same depth at point.
5782 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
5783 If point is at the beginning of a headline, insert a sibling before the
5784 current headline. If point is not at the beginning, do not split the line,
5785 but create the new headline after the current line."
5787 (if (= (buffer-size) 0)
5789 (when (or force-heading (not (org-insert-item)))
5790 (let* ((empty-line-p nil)
5791 (head (save-excursion
5794 (org-back-to-heading)
5795 (setq empty-line-p (org-previous-line-empty-p))
5798 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
5799 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
5800 pos hide-previous previous-pos)
5802 ((and (org-on-heading-p) (bolp)
5804 (save-excursion (backward-char 1) (not (org-invisible-p)))))
5805 ;; insert before the current line
5806 (open-line (if blank 2 1)))
5808 (not org-insert-heading-respect-content)
5811 (backward-char 1) (not (org-invisible-p)))))
5812 ;; insert right here
5815 ;; somewhere in the line
5817 (setq previous-pos (point-at-bol))
5819 (setq hide-previous (org-invisible-p)))
5820 (and org-insert-heading-respect-content (org-show-subtree))
5822 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
5825 (goto-char (point-at-bol))
5826 (and (looking-at org-complex-heading-regexp)
5827 (> p (match-beginning 4)))))))
5830 (org-insert-heading-respect-content
5831 (org-end-of-subtree nil t)
5832 (or (bolp) (newline))
5833 (or (org-previous-line-empty-p)
5834 (and blank (newline)))
5840 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
5841 (setq tags (and (match-end 2) (match-string 2)))
5843 (delete-region (match-beginning 1) (match-end 1)))
5844 (setq pos (point-at-bol))
5845 (or split (end-of-line 1))
5846 (delete-horizontal-space)
5847 (newline (if blank 2 1))
5853 (org-set-tags nil 'align))))
5855 (or split (end-of-line 1))
5856 (newline (if blank 2 1)))))))
5857 (insert head) (just-one-space)
5860 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
5861 (when (and org-insert-heading-respect-content hide-previous)
5863 (goto-char previous-pos)
5865 (run-hooks 'org-insert-heading-hook)))))
5867 (defun org-get-heading (&optional no-tags)
5868 "Return the heading of the current entry, without the stars."
5870 (org-back-to-heading t)
5873 (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
5874 "\\*+[ \t]+\\([^\r\n]*\\)"))
5875 (match-string 1) "")))
5877 (defun org-heading-components ()
5878 "Return the components of the current heading.
5879 This is a list with the following elements:
5880 - the level as an integer
5881 - the reduced level, different if `org-odd-levels-only' is set.
5882 - the TODO keyword, or nil
5883 - the priority character, like ?A, or nil if no priority is given
5884 - the headline text itself, or the tags string if no headline text
5885 - the tags string, or nil."
5887 (org-back-to-heading t)
5888 (if (looking-at org-complex-heading-regexp)
5889 (list (length (match-string 1))
5890 (org-reduced-level (length (match-string 1)))
5891 (org-match-string-no-properties 2)
5892 (and (match-end 3) (aref (match-string 3) 2))
5893 (org-match-string-no-properties 4)
5894 (org-match-string-no-properties 5)))))
5896 (defun org-get-entry ()
5897 "Get the entry text, after heading, entire subtree."
5899 (org-back-to-heading t)
5900 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
5902 (defun org-insert-heading-after-current ()
5903 "Insert a new heading with same level as current, after current subtree."
5905 (org-back-to-heading)
5906 (org-insert-heading)
5907 (org-move-subtree-down)
5910 (defun org-insert-heading-respect-content ()
5912 (let ((org-insert-heading-respect-content t))
5913 (org-insert-heading t)))
5915 (defun org-insert-todo-heading-respect-content (&optional force-state)
5917 (let ((org-insert-heading-respect-content t))
5918 (org-insert-todo-heading force-state t)))
5920 (defun org-insert-todo-heading (arg &optional force-heading)
5921 "Insert a new heading with the same level and TODO state as current heading.
5922 If the heading has no TODO state, or if the state is DONE, use the first
5923 state (TODO by default). Also with prefix arg, force first state."
5925 (when (or force-heading (not (org-insert-item 'checkbox)))
5926 (org-insert-heading force-heading)
5928 (org-back-to-heading)
5929 (outline-previous-heading)
5930 (looking-at org-todo-line-regexp))
5934 (not (match-beginning 2))
5935 (member (match-string 2) org-done-keywords))
5936 (car org-todo-keywords-1)
5940 (run-hook-with-args-until-success
5941 'org-todo-get-default-hook new-mark-x nil)
5943 (beginning-of-line 1)
5944 (and (looking-at "\\*+ ") (goto-char (match-end 0))
5945 (if org-treat-insert-todo-heading-as-state-change
5947 (insert new-mark " "))))
5948 (when org-provide-todo-statistics
5949 (org-update-parent-todo-statistics))))
5951 (defun org-insert-subheading (arg)
5952 "Insert a new subheading and demote it.
5953 Works for outline headings and for plain lists alike."
5955 (org-insert-heading arg)
5957 ((org-on-heading-p) (org-do-demote))
5958 ((org-at-item-p) (org-indent-item 1))))
5960 (defun org-insert-todo-subheading (arg)
5961 "Insert a new subheading with TODO keyword or checkbox and demote it.
5962 Works for outline headings and for plain lists alike."
5964 (org-insert-todo-heading arg)
5966 ((org-on-heading-p) (org-do-demote))
5967 ((org-at-item-p) (org-indent-item 1))))
5969 ;;; Promotion and Demotion
5971 (defvar org-after-demote-entry-hook nil
5972 "Hook run after an entry has been demoted.
5973 The cursor will be at the beginning of the entry.
5974 When a subtree is being demoted, the hook will be called for each node.")
5976 (defvar org-after-promote-entry-hook nil
5977 "Hook run after an entry has been promoted.
5978 The cursor will be at the beginning of the entry.
5979 When a subtree is being promoted, the hook will be called for each node.")
5981 (defun org-promote-subtree ()
5982 "Promote the entire subtree.
5983 See also `org-promote'."
5986 (org-map-tree 'org-promote))
5987 (org-fix-position-after-promote))
5989 (defun org-demote-subtree ()
5990 "Demote the entire subtree. See `org-demote'.
5991 See also `org-promote'."
5994 (org-map-tree 'org-demote))
5995 (org-fix-position-after-promote))
5998 (defun org-do-promote ()
5999 "Promote the current heading higher up the tree.
6000 If the region is active in `transient-mark-mode', promote all headings
6004 (if (org-region-active-p)
6005 (org-map-region 'org-promote (region-beginning) (region-end))
6007 (org-fix-position-after-promote))
6009 (defun org-do-demote ()
6010 "Demote the current heading lower down the tree.
6011 If the region is active in `transient-mark-mode', demote all headings
6015 (if (org-region-active-p)
6016 (org-map-region 'org-demote (region-beginning) (region-end))
6018 (org-fix-position-after-promote))
6020 (defun org-fix-position-after-promote ()
6021 "Make sure that after pro/demotion cursor position is right."
6022 (let ((pos (point)))
6023 (when (save-excursion
6024 (beginning-of-line 1)
6025 (looking-at org-todo-line-regexp)
6026 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
6027 (cond ((eobp) (insert " "))
6028 ((eolp) (insert " "))
6029 ((equal (char-after) ?\ ) (forward-char 1))))))
6031 (defun org-reduced-level (l)
6032 "Compute the effective level of a heading.
6033 This takes into account the setting of `org-odd-levels-only'."
6034 (if org-odd-levels-only (1+ (floor (/ l 2))) l))
6036 (defun org-get-valid-level (level &optional change)
6037 "Rectify a level change under the influence of `org-odd-levels-only'
6038 LEVEL is a current level, CHANGE is by how much the level should be
6039 modified. Even if CHANGE is nil, LEVEL may be returned modified because
6040 even level numbers will become the next higher odd number."
6041 (if org-odd-levels-only
6042 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
6043 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
6044 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
6045 (max 1 (+ level (or change 0)))))
6047 (if (boundp 'define-obsolete-function-alias)
6048 (if (or (featurep 'xemacs) (< emacs-major-version 23))
6049 (define-obsolete-function-alias 'org-get-legal-level
6050 'org-get-valid-level)
6051 (define-obsolete-function-alias 'org-get-legal-level
6052 'org-get-valid-level "23.1")))
6054 (defun org-promote ()
6055 "Promote the current heading higher up the tree.
6056 If the region is active in `transient-mark-mode', promote all headings
6058 (org-back-to-heading t)
6059 (let* ((level (save-match-data (funcall outline-level)))
6060 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
6061 (diff (abs (- level (length up-head) -1))))
6062 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
6063 (replace-match up-head nil t)
6064 ;; Fixup tag positioning
6065 (and org-auto-align-tags (org-set-tags nil t))
6066 (if org-adapt-indentation (org-fixup-indentation (- diff)))
6067 (run-hooks 'org-after-promote-entry-hook)))
6069 (defun org-demote ()
6070 "Demote the current heading lower down the tree.
6071 If the region is active in `transient-mark-mode', demote all headings
6073 (org-back-to-heading t)
6074 (let* ((level (save-match-data (funcall outline-level)))
6075 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
6076 (diff (abs (- level (length down-head) -1))))
6077 (replace-match down-head nil t)
6078 ;; Fixup tag positioning
6079 (and org-auto-align-tags (org-set-tags nil t))
6080 (if org-adapt-indentation (org-fixup-indentation diff))
6081 (run-hooks 'org-after-demote-entry-hook)))
6083 (defun org-map-tree (fun)
6084 "Call FUN for every heading underneath the current one."
6085 (org-back-to-heading)
6086 (let ((level (funcall outline-level)))
6090 (outline-next-heading)
6091 (> (funcall outline-level) level))
6095 (defun org-map-region (fun beg end)
6096 "Call FUN for every heading between BEG and END."
6097 (let ((org-ignore-region t))
6099 (setq end (copy-marker end))
6101 (if (and (re-search-forward (concat "^" outline-regexp) nil t)
6105 (outline-next-heading)
6110 (defun org-fixup-indentation (diff)
6111 "Change the indentation in the current entry by DIFF
6112 However, if any line in the current entry has no indentation, or if it
6113 would end up with no indentation after the change, nothing at all is done."
6115 (let ((end (save-excursion (outline-next-heading)
6117 (prohibit (if (> diff 0)
6119 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
6121 (unless (save-excursion (end-of-line 1)
6122 (re-search-forward prohibit end t))
6123 (while (and (< (point) end)
6124 (re-search-forward "^[ \t]+" end t))
6125 (goto-char (match-end 0))
6126 (setq col (current-column))
6127 (if (< diff 0) (replace-match ""))
6128 (org-indent-to-column (+ diff col))))
6129 (move-marker end nil))))
6131 (defun org-convert-to-odd-levels ()
6132 "Convert an org-mode file with all levels allowed to one with odd levels.
6133 This will leave level 1 alone, convert level 2 to level 3, level 3 to
6136 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
6137 (let ((outline-regexp org-outline-regexp)
6138 (outline-level 'org-outline-level)
6139 (org-odd-levels-only nil) n)
6141 (goto-char (point-min))
6142 (while (re-search-forward "^\\*\\*+ " nil t)
6143 (setq n (- (length (match-string 0)) 2))
6144 (while (>= (setq n (1- n)) 0)
6146 (end-of-line 1))))))
6148 (defun org-convert-to-oddeven-levels ()
6149 "Convert an org-mode file with only odd levels to one with odd and even levels.
6150 This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
6151 section with an even level, conversion would destroy the structure of the file. An error
6152 is signaled in this case."
6154 (goto-char (point-min))
6155 ;; First check if there are no even levels
6156 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
6157 (org-show-context t)
6158 (error "Not all levels are odd in this file. Conversion not possible"))
6159 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
6160 (let ((outline-regexp org-outline-regexp)
6161 (outline-level 'org-outline-level)
6162 (org-odd-levels-only nil) n)
6164 (goto-char (point-min))
6165 (while (re-search-forward "^\\*\\*+ " nil t)
6166 (setq n (/ (1- (length (match-string 0))) 2))
6167 (while (>= (setq n (1- n)) 0)
6169 (end-of-line 1))))))
6171 (defun org-tr-level (n)
6172 "Make N odd if required."
6173 (if org-odd-levels-only (1+ (/ n 2)) n))
6175 ;;; Vertical tree motion, cutting and pasting of subtrees
6177 (defun org-move-subtree-up (&optional arg)
6178 "Move the current subtree up past ARG headlines of the same level."
6180 (org-move-subtree-down (- (prefix-numeric-value arg))))
6182 (defun org-move-subtree-down (&optional arg)
6183 "Move the current subtree down past ARG headlines of the same level."
6185 (setq arg (prefix-numeric-value arg))
6186 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
6187 'org-get-last-sibling))
6188 (ins-point (make-marker))
6190 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
6192 (org-back-to-heading)
6195 (setq ne-beg (org-back-over-empty-lines))
6198 (save-excursion (outline-end-of-heading)
6199 (setq folded (org-invisible-p)))
6200 (outline-end-of-subtree))
6201 (outline-next-heading)
6202 (setq ne-end (org-back-over-empty-lines))
6205 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
6206 ;; include less whitespace
6209 (forward-line (- ne-beg ne-end))
6210 (setq beg (point))))
6211 ;; Find insertion point, with error handling
6213 (or (and (funcall movfunc) (looking-at outline-regexp))
6214 (progn (goto-char beg0)
6215 (error "Cannot move past superior level or buffer limit")))
6216 (setq cnt (1- cnt)))
6218 ;; Moving forward - still need to move over subtree
6219 (progn (org-end-of-subtree t t)
6221 (org-back-over-empty-lines)
6222 (or (bolp) (newline)))))
6223 (setq ne-ins (org-back-over-empty-lines))
6224 (move-marker ins-point (point))
6225 (setq txt (buffer-substring beg end))
6226 (org-save-markers-in-region beg end)
6227 (delete-region beg end)
6228 (org-remove-empty-overlays-at beg)
6229 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
6230 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
6231 (and (not (bolp)) (looking-at "\n") (forward-char 1))
6232 (let ((bbb (point)))
6233 (insert-before-markers txt)
6234 (org-reinstall-markers-in-region bbb)
6235 (move-marker ins-point bbb))
6236 (or (bolp) (insert "\n"))
6237 (setq ins-end (point))
6238 (goto-char ins-point)
6239 (org-skip-whitespace)
6240 (when (and (< arg 0)
6241 (org-first-sibling-p)
6243 ;; Move whitespace back to beginning
6246 (let ((kill-whole-line t))
6247 (kill-line (- ne-ins ne-beg)) (point)))
6248 (insert (make-string (- ne-ins ne-beg) ?\n)))
6249 (move-marker ins-point nil)
6254 (org-cycle-hide-drawers 'children))
6255 (org-clean-visibility-after-subtree-move)))
6257 (defvar org-subtree-clip ""
6258 "Clipboard for cut and paste of subtrees.
6259 This is actually only a copy of the kill, because we use the normal kill
6260 ring. We need it to check if the kill was created by `org-copy-subtree'.")
6262 (defvar org-subtree-clip-folded nil
6263 "Was the last copied subtree folded?
6264 This is used to fold the tree back after pasting.")
6266 (defun org-cut-subtree (&optional n)
6267 "Cut the current subtree into the clipboard.
6268 With prefix arg N, cut this many sequential subtrees.
6269 This is a short-hand for marking the subtree and then cutting it."
6271 (org-copy-subtree n 'cut))
6273 (defun org-copy-subtree (&optional n cut force-store-markers)
6274 "Cut the current subtree into the clipboard.
6275 With prefix arg N, cut this many sequential subtrees.
6276 This is a short-hand for marking the subtree and then copying it.
6277 If CUT is non-nil, actually cut the subtree.
6278 If FORCE-STORE-MARKERS is non-nil, store the relative locations
6279 of some markers in the region, even if CUT is non-nil. This is
6280 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
6282 (let (beg end folded (beg0 (point)))
6284 (org-back-to-heading nil) ; take what looks like a subtree
6285 (org-back-to-heading t)) ; take what is really there
6286 (org-back-over-empty-lines)
6288 (skip-chars-forward " \t\r\n")
6290 (save-excursion (outline-end-of-heading)
6291 (setq folded (org-invisible-p)))
6293 (org-forward-same-level (1- n) t)
6295 (org-end-of-subtree t t))
6296 (org-back-over-empty-lines)
6300 (setq org-subtree-clip-folded folded)
6301 (when (or cut force-store-markers)
6302 (org-save-markers-in-region beg end))
6303 (if cut (kill-region beg end) (copy-region-as-kill beg end))
6304 (setq org-subtree-clip (current-kill 0))
6305 (message "%s: Subtree(s) with %d characters"
6306 (if cut "Cut" "Copied")
6307 (length org-subtree-clip)))))
6309 (defun org-paste-subtree (&optional level tree for-yank)
6310 "Paste the clipboard as a subtree, with modification of headline level.
6311 The entire subtree is promoted or demoted in order to match a new headline
6314 If the cursor is at the beginning of a headline, the same level as
6315 that headline is used to paste the tree
6317 If not, the new level is derived from the *visible* headings
6318 before and after the insertion point, and taken to be the inferior headline
6319 level of the two. So if the previous visible heading is level 3 and the
6320 next is level 4 (or vice versa), level 4 will be used for insertion.
6321 This makes sure that the subtree remains an independent subtree and does
6322 not swallow low level entries.
6324 You can also force a different level, either by using a numeric prefix
6325 argument, or by inserting the heading marker by hand. For example, if the
6326 cursor is after \"*****\", then the tree will be shifted to level 5.
6328 If optional TREE is given, use this text instead of the kill ring.
6330 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
6331 move back over whitespace before inserting, and move point to the end of
6332 the inserted text when done."
6334 (setq tree (or tree (and kill-ring (current-kill 0))))
6335 (unless (org-kill-is-subtree-p tree)
6337 (substitute-command-keys
6338 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
6339 (let* ((visp (not (org-invisible-p)))
6341 (^re (concat "^\\(" outline-regexp "\\)"))
6342 (re (concat "\\(" outline-regexp "\\)"))
6343 (^re_ (concat "\\(\\*+\\)[ \t]*"))
6345 (old-level (if (string-match ^re txt)
6346 (- (match-end 0) (match-beginning 0) 1)
6348 (force-level (cond (level (prefix-numeric-value level))
6349 ((and (looking-at "[ \t]*$")
6351 ^re_ (buffer-substring
6352 (point-at-bol) (point))))
6353 (- (match-end 1) (match-beginning 1)))
6355 (looking-at org-outline-regexp))
6356 (- (match-end 0) (point) 1))
6358 (previous-level (save-excursion
6361 (outline-previous-visible-heading 1)
6363 (- (match-end 0) (match-beginning 0) 1)
6366 (next-level (save-excursion
6369 (or (looking-at outline-regexp)
6370 (outline-next-visible-heading 1))
6372 (- (match-end 0) (match-beginning 0) 1)
6375 (new-level (or force-level (max previous-level next-level)))
6376 (shift (if (or (= old-level -1)
6378 (= old-level new-level))
6380 (- new-level old-level)))
6381 (delta (if (> shift 0) -1 1))
6382 (func (if (> shift 0) 'org-demote 'org-promote))
6383 (org-odd-levels-only nil)
6385 ;; Remove the forced level indicator
6387 (delete-region (point-at-bol) (point)))
6389 (beginning-of-line 1)
6390 (unless for-yank (org-back-over-empty-lines))
6392 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
6393 (insert-before-markers txt)
6394 (unless (string-match "\n\\'" txt) (insert "\n"))
6395 (setq newend (point))
6396 (org-reinstall-markers-in-region beg)
6399 (skip-chars-forward " \t\n\r")
6401 (if (and (org-invisible-p) visp)
6402 (save-excursion (outline-show-heading)))
6403 ;; Shift if necessary
6406 (narrow-to-region beg end)
6407 (while (not (= shift 0))
6408 (org-map-region func (point-min) (point-max))
6409 (setq shift (+ delta shift)))
6410 (goto-char (point-min))
6411 (setq newend (point-max))))
6412 (when (or (interactive-p) for-yank)
6413 (message "Clipboard pasted as level %d subtree" new-level))
6414 (if (and (not for-yank) ; in this case, org-yank will decide about folding
6416 (eq org-subtree-clip (current-kill 0))
6417 org-subtree-clip-folded)
6418 ;; The tree was folded before it was killed/copied
6420 (and for-yank (goto-char newend))))
6422 (defun org-kill-is-subtree-p (&optional txt)
6423 "Check if the current kill is an outline subtree, or a set of trees.
6424 Returns nil if kill does not start with a headline, or if the first
6425 headline level is not the largest headline level in the tree.
6426 So this will actually accept several entries of equal levels as well,
6427 which is OK for `org-paste-subtree'.
6428 If optional TXT is given, check this string instead of the current kill."
6429 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
6430 (start-level (and kill
6431 (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
6432 org-outline-regexp "\\)")
6434 (- (match-end 2) (match-beginning 2) 1)))
6435 (re (concat "^" org-outline-regexp))
6436 (start (1+ (or (match-beginning 2) -1))))
6437 (if (not start-level)
6439 nil) ;; does not even start with a heading
6441 (while (setq start (string-match re kill (1+ start)))
6442 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
6446 (defvar org-markers-to-move nil
6447 "Markers that should be moved with a cut-and-paste operation.
6448 Those markers are stored together with their positions relative to
6449 the start of the region.")
6451 (defun org-save-markers-in-region (beg end)
6452 "Check markers in region.
6453 If these markers are between BEG and END, record their position relative
6454 to BEG, so that after moving the block of text, we can put the markers back
6456 This function gets called just before an entry or tree gets cut from the
6457 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
6458 called immediately, to move the markers with the entries."
6459 (setq org-markers-to-move nil)
6460 (when (featurep 'org-clock)
6461 (org-clock-save-markers-for-cut-and-paste beg end))
6462 (when (featurep 'org-agenda)
6463 (org-agenda-save-markers-for-cut-and-paste beg end)))
6465 (defun org-check-and-save-marker (marker beg end)
6466 "Check if MARKER is between BEG and END.
6467 If yes, remember the marker and the distance to BEG."
6468 (when (and (marker-buffer marker)
6469 (equal (marker-buffer marker) (current-buffer)))
6470 (if (and (>= marker beg) (< marker end))
6471 (push (cons marker (- marker beg)) org-markers-to-move))))
6473 (defun org-reinstall-markers-in-region (beg)
6474 "Move all remembered markers to their position relative to BEG."
6476 (move-marker (car x) (+ beg (cdr x))))
6477 org-markers-to-move)
6478 (setq org-markers-to-move nil))
6480 (defun org-narrow-to-subtree ()
6481 "Narrow buffer to the current subtree."
6486 (progn (org-back-to-heading t) (point))
6487 (progn (org-end-of-subtree t t) (point))))))
6489 (defun org-clone-subtree-with-time-shift (n &optional shift)
6490 "Clone the task (subtree) at point N times.
6491 The clones will be inserted as siblings.
6493 In interactive use, the user will be prompted for the number of clones
6494 to be produced, and for a time SHIFT, which may be a repeater as used
6495 in time stamps, for example `+3d'.
6497 When a valid repeater is given and the entry contains any time stamps,
6498 the clones will become a sequence in time, with time stamps in the
6499 subtree shifted for each clone produced. If SHIFT is nil or the
6500 empty string, time stamps will be left alone.
6502 If the original subtree did contain time stamps with a repeater,
6503 the following will happen:
6504 - the repeater will be removed in each clone
6505 - an additional clone will be produced, with the current, unshifted
6506 date(s) in the entry.
6507 - the original entry will be placed *after* all the clones, with
6509 - the start days in the repeater in the original entry will be shifted
6510 to past the last clone.
6511 I this way you can spell out a number of instances of a repeating task,
6512 and still retain the repeater to cover future instances of the task."
6513 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
6514 (let (beg end template task
6515 shift-n shift-what doshift nmin nmax (n-no-remove -1))
6516 (if (not (and (integerp n) (> n 0)))
6517 (error "Invalid number of replications %s" n))
6518 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
6519 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
6521 (error "Invalid shift specification %s" shift))
6523 (setq shift-n (string-to-number (match-string 1 shift))
6524 shift-what (cdr (assoc (match-string 2 shift)
6525 '(("d" . day) ("w" . week)
6526 ("m" . month) ("y" . year))))))
6527 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
6528 (setq nmin 1 nmax n)
6529 (org-back-to-heading t)
6531 (org-end-of-subtree t t)
6533 (setq template (buffer-substring beg end))
6535 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
6536 (delete-region beg end)
6538 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
6540 (loop for n from nmin to nmax do
6542 (setq task template)
6546 (goto-char (point-min))
6547 (while (re-search-forward org-ts-regexp-both nil t)
6548 (org-timestamp-change (* n shift-n) shift-what))
6549 (unless (= n n-no-remove)
6550 (goto-char (point-min))
6551 (while (re-search-forward org-ts-regexp nil t)
6553 (goto-char (match-beginning 0))
6554 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
6555 (delete-region (match-beginning 1) (match-end 1))))))
6556 (setq task (buffer-string))))
6562 (defun org-sort (with-case)
6563 "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
6564 Optional argument WITH-CASE means sort case-sensitively.
6565 With a double prefix argument, also remove duplicate entries."
6567 (if (org-at-table-p)
6568 (org-call-with-arg 'org-table-sort-lines with-case)
6569 (org-call-with-arg 'org-sort-entries-or-items with-case)))
6571 (defun org-sort-remove-invisible (s)
6572 (remove-text-properties 0 (length s) org-rm-props s)
6573 (while (string-match org-bracket-link-regexp s)
6574 (setq s (replace-match (if (match-end 2)
6576 (match-string 1 s)) t t s)))
6579 (defvar org-priority-regexp) ; defined later in the file
6581 (defvar org-after-sorting-entries-or-items-hook nil
6582 "Hook that is run after a bunch of entries or items have been sorted.
6583 When children are sorted, the cursor is in the parent line when this
6584 hook gets called. When a region or a plain list is sorted, the cursor
6585 will be in the first entry of the sorted region/list.")
6587 (defun org-sort-entries-or-items
6588 (&optional with-case sorting-type getkey-func compare-func property)
6589 "Sort entries on a certain level of an outline tree, or plain list items.
6590 If there is an active region, the entries in the region are sorted.
6591 Else, if the cursor is before the first entry, sort the top-level items.
6592 Else, the children of the entry at point are sorted.
6593 If the cursor is at the first item in a plain list, the list items will be
6596 Sorting can be alphabetically, numerically, by date/time as given by
6597 a time stamp, by a property or by priority.
6599 The command prompts for the sorting type unless it has been given to the
6600 function through the SORTING-TYPE argument, which needs to a character,
6601 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
6602 precise meaning of each character:
6604 n Numerically, by converting the beginning of the entry/item to a number.
6605 a Alphabetically, ignoring the TODO keyword and the priority, if any.
6606 t By date/time, either the first active time stamp in the entry, or, if
6607 none exist, by the first inactive one.
6608 In items, only the first line will be chekced.
6609 s By the scheduled date/time.
6610 d By deadline date/time.
6611 c By creation time, which is assumed to be the first inactive time stamp
6612 at the beginning of a line.
6613 p By priority according to the cookie.
6614 r By the value of a property.
6616 Capital letters will reverse the sort order.
6618 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
6619 called with point at the beginning of the record. It must return either
6620 a string or a number that should serve as the sorting key for that record.
6622 Comparing entries ignores case by default. However, with an optional argument
6623 WITH-CASE, the sorting considers case as well."
6625 (let ((case-func (if with-case 'identity 'downcase))
6626 start beg end stars re re2
6627 txt what tmp plain-list-p)
6628 ;; Find beginning and end of region to sort
6630 ((org-region-active-p)
6631 ;; we will sort the region
6632 (setq end (region-end)
6634 (goto-char (region-beginning))
6635 (if (not (org-on-heading-p)) (outline-next-heading))
6636 (setq start (point)))
6638 ;; we will sort this plain list
6639 (org-beginning-of-item-list) (setq start (point))
6640 (org-end-of-item-list) (setq end (point))
6642 (setq plain-list-p t
6644 ((or (org-on-heading-p)
6645 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
6646 ;; we will sort the children of the current headline
6647 (org-back-to-heading)
6649 end (progn (org-end-of-subtree t t)
6650 (org-back-over-empty-lines)
6655 (outline-next-heading))
6657 ;; we will sort the top-level entries in this file
6658 (goto-char (point-min))
6659 (or (org-on-heading-p) (outline-next-heading))
6660 (setq start (point) end (point-max) what "top-level")
6665 (if (>= beg end) (error "Nothing to sort"))
6667 (unless plain-list-p
6668 (looking-at "\\(\\*+\\)")
6669 (setq stars (match-string 1)
6670 re (concat "^" (regexp-quote stars) " +")
6671 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
6672 txt (buffer-substring beg end))
6673 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
6674 (if (and (not (equal stars "*")) (string-match re2 txt))
6675 (error "Region to sort contains a level above the first entry")))
6677 (unless sorting-type
6680 "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
6681 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
6682 [t]ime [s]cheduled [d]eadline [c]reated
6683 A/N/T/S/D/C/P/O/F means reversed:")
6685 (setq sorting-type (read-char-exclusive))
6687 (and (= (downcase sorting-type) ?f)
6689 (org-icompleting-read "Sort using function: "
6690 obarray 'fboundp t nil nil))
6691 (setq getkey-func (intern getkey-func)))
6693 (and (= (downcase sorting-type) ?r)
6695 (org-icompleting-read "Property: "
6696 (mapcar 'list (org-buffer-property-keys t))
6699 (message "Sorting entries...")
6702 (narrow-to-region start end)
6704 (let ((dcst (downcase sorting-type))
6705 (case-fold-search nil)
6706 (now (current-time)))
6708 (/= dcst sorting-type)
6709 ;; This function moves to the beginning character of the "record" to
6713 (if (org-at-item-p) t (goto-char (point-max))))
6715 (if (re-search-forward re nil t)
6716 (goto-char (match-beginning 0))
6717 (goto-char (point-max)))))
6718 ;; This function moves to the last character of the "record" being
6725 (outline-forward-same-level 1)
6727 (goto-char (point-max)))))))
6729 ;; This function returns the value that gets sorted against.
6732 (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
6735 (string-to-number (buffer-substring (match-end 0)
6738 (buffer-substring (match-end 0) (point-at-eol)))
6740 (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
6741 (re-search-forward org-ts-regexp-both
6743 (org-time-string-to-seconds (match-string 0))
6744 (org-float-time now)))
6748 (setq tmp (funcall getkey-func))
6749 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6751 (error "Invalid key function `%s'" getkey-func)))
6752 (t (error "Invalid sorting type `%c'" sorting-type)))))
6756 (if (looking-at org-complex-heading-regexp)
6757 (string-to-number (match-string 4))
6760 (if (looking-at org-complex-heading-regexp)
6761 (funcall case-func (match-string 4))
6764 (let ((end (save-excursion (outline-next-heading) (point))))
6765 (if (or (re-search-forward org-ts-regexp end t)
6766 (re-search-forward org-ts-regexp-both end t))
6767 (org-time-string-to-seconds (match-string 0))
6768 (org-float-time now))))
6770 (let ((end (save-excursion (outline-next-heading) (point))))
6771 (if (re-search-forward
6772 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
6774 (org-time-string-to-seconds (match-string 0))
6775 (org-float-time now))))
6777 (let ((end (save-excursion (outline-next-heading) (point))))
6778 (if (re-search-forward org-scheduled-time-regexp end t)
6779 (org-time-string-to-seconds (match-string 1))
6780 (org-float-time now))))
6782 (let ((end (save-excursion (outline-next-heading) (point))))
6783 (if (re-search-forward org-deadline-time-regexp end t)
6784 (org-time-string-to-seconds (match-string 1))
6785 (org-float-time now))))
6787 (if (re-search-forward org-priority-regexp (point-at-eol) t)
6788 (string-to-char (match-string 2))
6789 org-default-priority))
6791 (or (org-entry-get nil property) ""))
6793 (if (looking-at org-complex-heading-regexp)
6794 (- 9999 (length (member (match-string 2)
6795 org-todo-keywords-1)))))
6799 (setq tmp (funcall getkey-func))
6800 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
6802 (error "Invalid key function `%s'" getkey-func)))
6803 (t (error "Invalid sorting type `%c'" sorting-type)))))
6806 ((= dcst ?a) 'string<)
6807 ((= dcst ?f) compare-func)
6808 ((member dcst '(?p ?t ?s ?d ?c)) '<)
6810 (run-hooks 'org-after-sorting-entries-or-items-hook)
6811 (message "Sorting entries...done")))
6813 (defun org-do-sort (table what &optional with-case sorting-type)
6814 "Sort TABLE of WHAT according to SORTING-TYPE.
6815 The user will be prompted for the SORTING-TYPE if the call to this
6816 function does not specify it. WHAT is only for the prompt, to indicate
6817 what is being sorted. The sorting key will be extracted from
6818 the car of the elements of the table.
6819 If WITH-CASE is non-nil, the sorting will be case-sensitive."
6820 (unless sorting-type
6822 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
6824 (setq sorting-type (read-char-exclusive)))
6825 (let ((dcst (downcase sorting-type))
6826 extractfun comparefun)
6827 ;; Define the appropriate functions
6830 (setq extractfun 'string-to-number
6831 comparefun (if (= dcst sorting-type) '< '>)))
6833 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
6834 (lambda(x) (downcase (org-sort-remove-invisible x))))
6835 comparefun (if (= dcst sorting-type)
6837 (lambda (a b) (and (not (string< a b))
6838 (not (string= a b)))))))
6842 (if (or (string-match org-ts-regexp x)
6843 (string-match org-ts-regexp-both x))
6845 (org-time-string-to-time (match-string 0 x)))
6847 comparefun (if (= dcst sorting-type) '< '>)))
6848 (t (error "Invalid sorting type `%c'" sorting-type)))
6850 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
6852 (lambda (a b) (funcall comparefun (car a) (car b))))))
6855 ;;; The orgstruct minor mode
6857 ;; Define a minor mode which can be used in other modes in order to
6858 ;; integrate the org-mode structure editing commands.
6860 ;; This is really a hack, because the org-mode structure commands use
6861 ;; keys which normally belong to the major mode. Here is how it
6862 ;; works: The minor mode defines all the keys necessary to operate the
6863 ;; structure commands, but wraps the commands into a function which
6864 ;; tests if the cursor is currently at a headline or a plain list
6865 ;; item. If that is the case, the structure command is used,
6866 ;; temporarily setting many Org-mode variables like regular
6867 ;; expressions for filling etc. However, when any of those keys is
6868 ;; used at a different location, function uses `key-binding' to look
6869 ;; up if the key has an associated command in another currently active
6870 ;; keymap (minor modes, major mode, global), and executes that
6871 ;; command. There might be problems if any of the keys is otherwise
6872 ;; used as a prefix key.
6874 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
6875 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
6876 ;; addresses this by checking explicitly for both bindings.
6878 (defvar orgstruct-mode-map (make-sparse-keymap)
6879 "Keymap for the minor `orgstruct-mode'.")
6881 (defvar org-local-vars nil
6882 "List of local variables, for use by `orgstruct-mode'")
6885 (define-minor-mode orgstruct-mode
6886 "Toggle the minor more `orgstruct-mode'.
6887 This mode is for using Org-mode structure commands in other modes.
6888 The following key behave as if Org-mode was active, if the cursor
6889 is on a headline, or on a plain list item (both in the definition
6892 M-up Move entry/item up
6893 M-down Move entry/item down
6896 M-S-up Move entry/item up
6897 M-S-down Move entry/item down
6898 M-S-left Promote subtree
6899 M-S-right Demote subtree
6900 M-q Fill paragraph and items like in Org-mode
6902 C-c - Cycle list bullet
6903 TAB Cycle item visibility
6904 M-RET Insert new heading/item
6905 S-M-RET Insert new TODO heading / Checkbox item
6906 C-c C-c Set tags / toggle checkbox"
6907 nil " OrgStruct" nil
6908 (org-load-modules-maybe)
6909 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
6912 (defun turn-on-orgstruct ()
6913 "Unconditionally turn on `orgstruct-mode'."
6916 (defun orgstruct++-mode (&optional arg)
6917 "Toggle `orgstruct-mode', the enhanced version of it.
6918 In addition to setting orgstruct-mode, this also exports all indentation
6919 and autofilling variables from org-mode into the buffer. It will also
6920 recognize item context in multiline items.
6921 Note that turning off orgstruct-mode will *not* remove the
6922 indentation/paragraph settings. This can only be done by refreshing the
6923 major mode, for example with \\[normal-mode]."
6925 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
6933 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
6934 (symbol-name (car x)))
6935 (setq var (car x) val (nth 1 x))
6936 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
6938 (org-set-local 'orgstruct-is-++ t))))
6940 (defvar orgstruct-is-++ nil
6941 "Is orgstruct-mode in ++ version in the current-buffer?")
6942 (make-variable-buffer-local 'orgstruct-is-++)
6945 (defun turn-on-orgstruct++ ()
6946 "Unconditionally turn on `orgstruct++-mode'."
6947 (orgstruct++-mode 1))
6949 (defun orgstruct-error ()
6950 "Error when there is no default binding for a structure key."
6952 (error "This key has no function outside structure elements"))
6954 (defun orgstruct-setup ()
6955 "Setup orgstruct keymaps."
6959 '([(meta up)] org-metaup)
6960 '([(meta down)] org-metadown)
6961 '([(meta left)] org-metaleft)
6962 '([(meta right)] org-metaright)
6963 '([(meta shift up)] org-shiftmetaup)
6964 '([(meta shift down)] org-shiftmetadown)
6965 '([(meta shift left)] org-shiftmetaleft)
6966 '([(meta shift right)] org-shiftmetaright)
6967 '([?\e (up)] org-metaup)
6968 '([?\e (down)] org-metadown)
6969 '([?\e (left)] org-metaleft)
6970 '([?\e (right)] org-metaright)
6971 '([?\e (shift up)] org-shiftmetaup)
6972 '([?\e (shift down)] org-shiftmetadown)
6973 '([?\e (shift left)] org-shiftmetaleft)
6974 '([?\e (shift right)] org-shiftmetaright)
6975 '([(shift up)] org-shiftup)
6976 '([(shift down)] org-shiftdown)
6977 '([(shift left)] org-shiftleft)
6978 '([(shift right)] org-shiftright)
6979 '("\C-c\C-c" org-ctrl-c-ctrl-c)
6980 '("\M-q" fill-paragraph)
6982 '("\C-c-" org-cycle-list-bullet)))
6984 (while (setq elt (pop bindings))
6985 (setq nfunc (1+ nfunc))
6986 (setq key (org-key (car elt))
6988 cmd (orgstruct-make-binding fun nfunc key))
6989 (org-defkey orgstruct-mode-map key cmd))
6991 ;; Special treatment needed for TAB and RET
6992 (org-defkey orgstruct-mode-map [(tab)]
6993 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
6994 (org-defkey orgstruct-mode-map "\C-i"
6995 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
6997 (org-defkey orgstruct-mode-map "\M-\C-m"
6998 (orgstruct-make-binding 'org-insert-heading 105
6999 "\M-\C-m" [(meta return)]))
7000 (org-defkey orgstruct-mode-map [(meta return)]
7001 (orgstruct-make-binding 'org-insert-heading 106
7002 [(meta return)] "\M-\C-m"))
7004 (org-defkey orgstruct-mode-map [(shift meta return)]
7005 (orgstruct-make-binding 'org-insert-todo-heading 107
7006 [(meta return)] "\M-\C-m"))
7008 (org-defkey orgstruct-mode-map "\e\C-m"
7009 (orgstruct-make-binding 'org-insert-heading 108
7010 "\e\C-m" [?\e (return)]))
7011 (org-defkey orgstruct-mode-map [?\e (return)]
7012 (orgstruct-make-binding 'org-insert-heading 109
7013 [?\e (return)] "\e\C-m"))
7014 (org-defkey orgstruct-mode-map [?\e (shift return)]
7015 (orgstruct-make-binding 'org-insert-todo-heading 110
7016 [?\e (return)] "\e\C-m"))
7018 (unless org-local-vars
7019 (setq org-local-vars (org-get-local-variables)))
7023 (defun orgstruct-make-binding (fun n &rest keys)
7024 "Create a function for binding in the structure minor mode.
7025 FUN is the command to call inside a table. N is used to create a unique
7026 command name. KEYS are keys that should be checked in for a command
7027 to execute outside of tables."
7030 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
7032 (concat "In Structure, run `" (symbol-name fun) "'.\n"
7033 "Outside of structure, run the binding of `"
7034 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
7038 `(org-context-p 'headline 'item
7039 (and orgstruct-is-++
7040 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
7042 (list 'org-run-like-in-org-mode (list 'quote fun))
7043 (list 'let '(orgstruct-mode)
7044 (list 'call-interactively
7047 (list 'key-binding k))
7049 '('orgstruct-error))))))))
7051 (defun org-context-p (&rest contexts)
7052 "Check if local context is any of CONTEXTS.
7053 Possible values in the list of contexts are `table', `headline', and `item'."
7054 (let ((pos (point)))
7055 (goto-char (point-at-bol))
7056 (prog1 (or (and (memq 'table contexts)
7057 (looking-at "[ \t]*|"))
7058 (and (memq 'headline contexts)
7059 ;;????????? (looking-at "\\*+"))
7060 (looking-at outline-regexp))
7061 (and (memq 'item contexts)
7062 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
7063 (and (memq 'item-body contexts)
7067 (defun org-get-local-variables ()
7068 "Return a list of all local variables in an org-mode buffer."
7070 (with-current-buffer (get-buffer-create "*Org tmp*")
7073 (setq varlist (buffer-local-variables)))
7074 (kill-buffer "*Org tmp*")
7081 (list (car x) (list 'quote (cdr x)))))
7083 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
7084 (symbol-name (car x)))
7089 (defun org-run-like-in-org-mode (cmd)
7090 "Run a command, pretending that the current buffer is in Org-mode.
7091 This will temporarily bind local variables that are typically bound in
7092 Org-mode to the values they have in Org-mode, and then interactively
7094 (org-load-modules-maybe)
7095 (unless org-local-vars
7096 (setq org-local-vars (org-get-local-variables)))
7097 (eval (list 'let org-local-vars
7098 (list 'call-interactively (list 'quote cmd)))))
7102 (defun org-get-category (&optional pos)
7103 "Get the category applying to position POS."
7104 (get-text-property (or pos (point)) 'org-category))
7106 (defun org-refresh-category-properties ()
7107 "Refresh category text properties in the buffer."
7108 (let ((def-cat (cond
7109 ((null org-category)
7110 (if buffer-file-name
7111 (file-name-sans-extension
7112 (file-name-nondirectory buffer-file-name))
7114 ((symbolp org-category) (symbol-name org-category))
7116 beg end cat pos optionp)
7121 (goto-char (point-min))
7122 (put-text-property (point) (point-max) 'org-category def-cat)
7123 (while (re-search-forward
7124 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
7125 (setq pos (match-end 0)
7126 optionp (equal (char-after (match-beginning 0)) ?#)
7127 cat (org-trim (match-string 2)))
7129 (setq beg (point-at-bol) end (point-max))
7130 (org-back-to-heading t)
7131 (setq beg (point) end (org-end-of-subtree t t)))
7132 (put-text-property beg end 'org-category cat)
7133 (goto-char pos)))))))
7138 ;;; Link abbreviations
7140 (defun org-link-expand-abbrev (link)
7141 "Apply replacements as defined in `org-link-abbrev-alist."
7142 (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
7143 (let* ((key (match-string 1 link))
7144 (as (or (assoc key org-link-abbrev-alist-local)
7145 (assoc key org-link-abbrev-alist)))
7146 (tag (and (match-end 2) (match-string 3 link)))
7152 ((symbolp rpl) (funcall rpl tag))
7153 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
7154 ((string-match "%h" rpl)
7155 (replace-match (url-hexify-string (or tag "")) t t rpl))
7156 (t (concat rpl tag)))))
7159 ;;; Storing and inserting links
7161 (defvar org-insert-link-history nil
7162 "Minibuffer history for links inserted with `org-insert-link'.")
7164 (defvar org-stored-links nil
7165 "Contains the links stored with `org-store-link'.")
7167 (defvar org-store-link-plist nil
7168 "Plist with info about the most recently link created with `org-store-link'.")
7170 (defvar org-link-protocols nil
7171 "Link protocols added to Org-mode using `org-add-link-type'.")
7173 (defvar org-store-link-functions nil
7174 "List of functions that are called to create and store a link.
7175 Each function will be called in turn until one returns a non-nil
7176 value. Each function should check if it is responsible for creating
7177 this link (for example by looking at the major mode).
7178 If not, it must exit and return nil.
7179 If yes, it should return a non-nil value after a calling
7180 `org-store-link-props' with a list of properties and values.
7181 Special properties are:
7183 :type The link prefix. like \"http\". This must be given.
7184 :link The link, like \"http://www.astro.uva.nl/~dominik\".
7185 This is obligatory as well.
7186 :description Optional default description for the second pair
7187 of brackets in an Org-mode link. The user can still change
7188 this when inserting this link into an Org-mode buffer.
7190 In addition to these, any additional properties can be specified
7191 and then used in remember templates.")
7193 (defun org-add-link-type (type &optional follow export)
7194 "Add TYPE to the list of `org-link-types'.
7195 Re-compute all regular expressions depending on `org-link-types'
7197 FOLLOW and EXPORT are two functions.
7199 FOLLOW should take the link path as the single argument and do whatever
7200 is necessary to follow the link, for example find a file or display
7203 EXPORT should format the link path for export to one of the export formats.
7204 It should be a function accepting three arguments:
7206 path the path of the link, the text after the prefix (like \"http:\")
7207 desc the description of the link, if any, nil if there was no description
7208 format the export format, a symbol like `html' or `latex'.
7210 The function may use the FORMAT information to return different values
7211 depending on the format. The return value will be put literally into
7213 Org-mode has a built-in default for exporting links. If you are happy with
7214 this default, there is no need to define an export function for the link
7215 type. For a simple example of an export function, see `org-bbdb.el'."
7216 (add-to-list 'org-link-types type t)
7217 (org-make-link-regexps)
7218 (if (assoc type org-link-protocols)
7219 (setcdr (assoc type org-link-protocols) (list follow export))
7220 (push (list type follow export) org-link-protocols)))
7222 (defvar org-agenda-buffer-name)
7225 (defun org-store-link (arg)
7226 "\\<org-mode-map>Store an org-link to the current location.
7227 This link is added to `org-stored-links' and can later be inserted
7228 into an org-buffer with \\[org-insert-link].
7230 For some link types, a prefix arg is interpreted:
7231 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
7232 For file links, arg negates `org-context-in-file-links'."
7234 (org-load-modules-maybe)
7235 (setq org-store-link-plist nil) ; reset
7236 (let ((outline-regexp (org-get-limited-outline-regexp))
7237 link cpltxt desc description search txt custom-id)
7240 ((run-hook-with-args-until-success 'org-store-link-functions)
7241 (setq link (plist-get org-store-link-plist :link)
7242 desc (or (plist-get org-store-link-plist :description) link)))
7244 ((equal (buffer-name) "*Org Edit Src Example*")
7246 (while (or (not label)
7250 (goto-char (point-min))
7252 (regexp-quote (format org-coderef-label-format label))
7254 (when label (message "Label exists already") (sit-for 2))
7255 (setq label (read-string "Code line label: " label)))
7257 (setq link (format org-coderef-label-format label))
7258 (setq gc (- 79 (length link)))
7259 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
7261 (setq link (concat "(" label ")") desc nil)))
7263 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
7264 ;; We are in the agenda, link to referenced location
7265 (let ((m (or (get-text-property (point) 'org-hd-marker)
7266 (get-text-property (point) 'org-marker))))
7268 (org-with-point-at m
7269 (call-interactively 'org-store-link)))))
7271 ((eq major-mode 'calendar-mode)
7272 (let ((cd (calendar-cursor-to-date)))
7275 (car org-time-stamp-formats)
7277 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
7279 (org-store-link-props :type "calendar" :date cd)))
7281 ((eq major-mode 'w3-mode)
7282 (setq cpltxt (if (and (buffer-name)
7283 (not (string-match "Untitled" (buffer-name))))
7286 link (org-make-link (url-view-url t)))
7287 (org-store-link-props :type "w3" :url (url-view-url t)))
7289 ((eq major-mode 'w3m-mode)
7290 (setq cpltxt (or w3m-current-title w3m-current-url)
7291 link (org-make-link w3m-current-url))
7292 (org-store-link-props :type "w3m" :url (url-view-url t)))
7294 ((setq search (run-hook-with-args-until-success
7295 'org-create-file-search-functions))
7296 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
7298 (setq cpltxt (or description link)))
7300 ((eq major-mode 'image-mode)
7301 (setq cpltxt (concat "file:"
7302 (abbreviate-file-name buffer-file-name))
7303 link (org-make-link cpltxt))
7304 (org-store-link-props :type "image" :file buffer-file-name))
7306 ((eq major-mode 'dired-mode)
7307 ;; link to the file in the current line
7308 (setq cpltxt (concat "file:"
7309 (abbreviate-file-name
7311 (dired-get-filename nil t))))
7312 link (org-make-link cpltxt)))
7314 ((and buffer-file-name (org-mode-p))
7315 (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
7317 ((org-in-regexp "<<\\(.*?\\)>>")
7320 (abbreviate-file-name buffer-file-name)
7321 "::" (match-string 1))
7322 link (org-make-link cpltxt)))
7323 ((and (featurep 'org-id)
7324 (or (eq org-link-to-org-use-id t)
7325 (and (eq org-link-to-org-use-id 'create-if-interactive)
7327 (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
7330 (and org-link-to-org-use-id
7332 (org-entry-get nil "ID")
7334 ;; We can make a link using the ID.
7335 (setq link (condition-case nil
7336 (prog1 (org-id-store-link)
7337 (setq desc (plist-get org-store-link-plist
7340 ;; probably before first headline, link to file only
7342 (abbreviate-file-name buffer-file-name))))))
7344 ;; Just link to current headline
7345 (setq cpltxt (concat "file:"
7346 (abbreviate-file-name buffer-file-name)))
7347 ;; Add a context search string
7348 (when (org-xor org-context-in-file-links arg)
7350 ((org-on-heading-p) nil)
7351 ((org-region-active-p)
7352 (buffer-substring (region-beginning) (region-end)))
7354 (when (or (null txt) (string-match "\\S-" txt))
7358 (org-make-org-heading-search-string txt)
7360 desc (or (nth 4 (ignore-errors
7361 (org-heading-components))) "NONE"))))
7362 (if (string-match "::\\'" cpltxt)
7363 (setq cpltxt (substring cpltxt 0 -2)))
7364 (setq link (org-make-link cpltxt)))))
7366 ((buffer-file-name (buffer-base-buffer))
7367 ;; Just link to this file here.
7368 (setq cpltxt (concat "file:"
7369 (abbreviate-file-name
7370 (buffer-file-name (buffer-base-buffer)))))
7371 ;; Add a context string
7372 (when (org-xor org-context-in-file-links arg)
7373 (setq txt (if (org-region-active-p)
7374 (buffer-substring (region-beginning) (region-end))
7375 (buffer-substring (point-at-bol) (point-at-eol))))
7376 ;; Only use search option if there is some text.
7377 (when (string-match "\\S-" txt)
7379 (concat cpltxt "::" (org-make-org-heading-search-string txt))
7381 (setq link (org-make-link cpltxt)))
7384 (error "Cannot link to a buffer which is not visiting a file"))
7386 (t (setq link nil)))
7388 (if (consp link) (setq cpltxt (car link) link (cdr link)))
7389 (setq link (or link cpltxt)
7390 desc (or desc cpltxt))
7391 (if (equal desc "NONE") (setq desc nil))
7393 (if (and (or (interactive-p) executing-kbd-macro) link)
7395 (setq org-stored-links
7396 (cons (list link desc) org-stored-links))
7397 (message "Stored: %s" (or desc link))
7399 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
7401 (setq org-stored-links
7402 (cons (list link desc) org-stored-links))))
7403 (and link (org-make-link-string link desc)))))
7405 (defun org-store-link-props (&rest plist)
7406 "Store link properties, extract names and addresses."
7408 (when (setq x (plist-get plist :from))
7409 (setq adr (mail-extract-address-components x))
7410 (setq plist (plist-put plist :fromname (car adr)))
7411 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
7412 (when (setq x (plist-get plist :to))
7413 (setq adr (mail-extract-address-components x))
7414 (setq plist (plist-put plist :toname (car adr)))
7415 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
7416 (let ((from (plist-get plist :from))
7417 (to (plist-get plist :to)))
7418 (when (and from to org-from-is-user-regexp)
7420 (plist-put plist :fromto
7421 (if (string-match org-from-is-user-regexp from)
7423 (concat "from %f"))))))
7424 (setq org-store-link-plist plist))
7426 (defun org-add-link-props (&rest plist)
7427 "Add these properties to the link property list."
7430 (setq key (pop plist) value (pop plist))
7431 (setq org-store-link-plist
7432 (plist-put org-store-link-plist key value)))))
7434 (defun org-email-link-description (&optional fmt)
7435 "Return the description part of an email link.
7436 This takes information from `org-store-link-plist' and formats it
7437 according to FMT (default from `org-email-link-description-format')."
7438 (setq fmt (or fmt org-email-link-description-format))
7439 (let* ((p org-store-link-plist)
7440 (to (plist-get p :toaddress))
7441 (from (plist-get p :fromaddress))
7444 (cons "%c" (plist-get p :fromto))
7445 (cons "%F" (plist-get p :from))
7446 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
7447 (cons "%T" (plist-get p :to))
7448 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
7449 (cons "%s" (plist-get p :subject))
7450 (cons "%m" (plist-get p :message-id)))))
7451 (when (string-match "%c" fmt)
7452 ;; Check if the user wrote this message
7453 (if (and org-from-is-user-regexp from to
7454 (save-match-data (string-match org-from-is-user-regexp from)))
7455 (setq fmt (replace-match "to %t" t t fmt))
7456 (setq fmt (replace-match "from %f" t t fmt))))
7457 (org-replace-escapes fmt table)))
7459 (defun org-make-org-heading-search-string (&optional string heading)
7460 "Make search string for STRING or current headline."
7462 (let ((s (or string (org-get-heading))))
7463 (unless (and string (not heading))
7464 ;; We are using a headline, clean up garbage in there.
7465 (if (string-match org-todo-regexp s)
7466 (setq s (replace-match "" t t s)))
7467 (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
7468 (setq s (replace-match "" t t s)))
7469 (setq s (org-trim s))
7470 (if (string-match (concat "^\\(" org-quote-string "\\|"
7471 org-comment-string "\\)") s)
7472 (setq s (replace-match "" t t s)))
7473 (while (string-match org-ts-regexp s)
7474 (setq s (replace-match "" t t s))))
7475 (while (string-match "[^a-zA-Z_0-9 \t]+" s)
7476 (setq s (replace-match " " t t s)))
7477 (or string (setq s (concat "*" s))) ; Add * for headlines
7478 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
7480 (defun org-make-link (&rest strings)
7481 "Concatenate STRINGS."
7482 (apply 'concat strings))
7484 (defun org-make-link-string (link &optional description)
7485 "Make a link with brackets, consisting of LINK and DESCRIPTION."
7486 (unless (string-match "\\S-" link)
7487 (error "Empty link"))
7488 (when (stringp description)
7489 ;; Remove brackets from the description, they are fatal.
7490 (while (string-match "\\[" description)
7491 (setq description (replace-match "{" t t description)))
7492 (while (string-match "\\]" description)
7493 (setq description (replace-match "}" t t description))))
7494 (when (equal (org-link-escape link) description)
7495 ;; No description needed, it is identical
7496 (setq description nil))
7497 (when (and (not description)
7498 (not (equal link (org-link-escape link))))
7499 (setq description (org-extract-attributes link)))
7500 (concat "[[" (org-link-escape link) "]"
7501 (if description (concat "[" description "]") "")
7504 (defconst org-link-escape-chars
7508 (?\340 . "%E0") ; `a
7509 (?\342 . "%E2") ; ^a
7510 (?\347 . "%E7") ; ,c
7511 (?\350 . "%E8") ; `e
7512 (?\351 . "%E9") ; 'e
7513 (?\352 . "%EA") ; ^e
7514 (?\356 . "%EE") ; ^i
7515 (?\364 . "%F4") ; ^o
7516 (?\371 . "%F9") ; `u
7517 (?\373 . "%FB") ; ^u
7523 "Association list of escapes for some characters problematic in links.
7524 This is the list that is used for internal purposes.")
7526 (defvar org-url-encoding-use-url-hexify nil)
7528 (defconst org-link-escape-chars-browser
7529 '((?\ . "%20")) ; 32 for the SPC char
7530 "Association list of escapes for some characters problematic in links.
7531 This is the list that is used before handing over to the browser.")
7533 (defun org-link-escape (text &optional table)
7534 "Escape characters in TEXT that are problematic for links."
7535 (if org-url-encoding-use-url-hexify
7536 (url-hexify-string text)
7537 (setq table (or table org-link-escape-chars))
7539 (let ((re (mapconcat (lambda (x) (regexp-quote
7540 (char-to-string (car x))))
7542 (while (string-match re text)
7545 (cdr (assoc (string-to-char (match-string 0 text))
7550 (defun org-link-unescape (text &optional table)
7551 "Reverse the action of `org-link-escape'."
7552 (if org-url-encoding-use-url-hexify
7553 (url-unhex-string text)
7554 (setq table (or table org-link-escape-chars))
7556 (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
7558 (while (string-match re text)
7561 (char-to-string (car (rassoc (match-string 0 text) table)))
7565 (defun org-xor (a b)
7569 (defun org-fixup-message-id-for-http (s)
7570 "Replace special characters in a message id, so it can be used in an http query."
7571 (while (string-match "<" s)
7572 (setq s (replace-match "%3C" t t s)))
7573 (while (string-match ">" s)
7574 (setq s (replace-match "%3E" t t s)))
7575 (while (string-match "@" s)
7576 (setq s (replace-match "%40" t t s)))
7580 (defun org-insert-link-global ()
7581 "Insert a link like Org-mode does.
7582 This command can be called in any mode to insert a link in Org-mode syntax."
7584 (org-load-modules-maybe)
7585 (org-run-like-in-org-mode 'org-insert-link))
7587 (defun org-insert-link (&optional complete-file link-location)
7588 "Insert a link. At the prompt, enter the link.
7590 Completion can be used to insert any of the link protocol prefixes like
7593 The history can be used to select a link previously stored with
7594 `org-store-link'. When the empty string is entered (i.e. if you just
7595 press RET at the prompt), the link defaults to the most recently
7596 stored link. As SPC triggers completion in the minibuffer, you need to
7597 use M-SPC or C-q SPC to force the insertion of a space character.
7599 You will also be prompted for a description, and if one is given, it will
7600 be displayed in the buffer instead of the link.
7602 If there is already a link at point, this command will allow you to edit link
7603 and description parts.
7605 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
7606 be selected using completion. The path to the file will be relative to the
7607 current directory if the file is in the current directory or a subdirectory.
7608 Otherwise, the link will be the absolute path as completed in the minibuffer
7609 \(i.e. normally ~/path/to/file). You can configure this behavior using the
7610 option `org-link-file-path-type'.
7612 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
7613 the current directory or below.
7615 With three \\[universal-argument] prefixes, negate the meaning of
7616 `org-keep-stored-link-after-insertion'.
7618 If `org-make-link-description-function' is non-nil, this function will be
7619 called with the link target, and the result will be the default
7622 If the LINK-LOCATION parameter is non-nil, this value will be
7623 used as the link location instead of reading one interactively."
7625 (let* ((wcf (current-window-configuration))
7626 (region (if (org-region-active-p)
7627 (buffer-substring (region-beginning) (region-end))))
7628 (remove (and region (list (region-beginning) (region-end))))
7630 tmphist ; byte-compile incorrectly complains about this
7631 (link link-location)
7632 entry file all-prefixes)
7634 (link-location) ; specified by arg, just use it.
7635 ((org-in-regexp org-bracket-link-regexp 1)
7636 ;; We do have a link at point, and we are going to edit it.
7637 (setq remove (list (match-beginning 0) (match-end 0)))
7638 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
7639 (setq link (read-string "Link: "
7641 (org-match-string-no-properties 1)))))
7642 ((or (org-in-regexp org-angle-link-re)
7643 (org-in-regexp org-plain-link-re))
7644 ;; Convert to bracket link
7645 (setq remove (list (match-beginning 0) (match-end 0))
7646 link (read-string "Link: "
7647 (org-remove-angle-brackets (match-string 0)))))
7648 ((member complete-file '((4) (16)))
7649 ;; Completing read for file names.
7650 (setq link (org-file-complete-link complete-file)))
7652 ;; Read link, with completion for stored links.
7653 (with-output-to-temp-buffer "*Org Links*"
7654 (princ "Insert a link.
7655 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
7656 (when org-stored-links
7657 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
7660 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
7661 (reverse org-stored-links) "\n"))))
7662 (let ((cw (selected-window)))
7663 (select-window (get-buffer-window "*Org Links*"))
7664 (setq truncate-lines t)
7665 (unless (pos-visible-in-window-p (point-max))
7666 (org-fit-window-to-buffer))
7667 (and (window-live-p cw) (select-window cw)))
7668 ;; Fake a link history, containing the stored links.
7669 (setq tmphist (append (mapcar 'car org-stored-links)
7670 org-insert-link-history))
7671 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
7672 (mapcar 'car org-link-abbrev-alist)
7677 (let ((org-completion-use-ido nil)
7678 (org-completion-use-iswitchb nil))
7679 (org-completing-read
7682 (mapcar (lambda (x) (list (concat x ":")))
7684 (mapcar 'car org-stored-links))
7687 (car (car org-stored-links)))))
7688 (if (or (member link all-prefixes)
7689 (and (equal ":" (substring link -1))
7690 (member (substring link 0 -1) all-prefixes)
7691 (setq link (substring link 0 -1))))
7692 (setq link (org-link-try-special-completion link))))
7693 (set-window-configuration wcf)
7694 (kill-buffer "*Org Links*"))
7695 (setq entry (assoc link org-stored-links))
7696 (or entry (push link org-insert-link-history))
7697 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
7698 (not org-keep-stored-link-after-insertion))
7699 (setq org-stored-links (delq (assoc link org-stored-links)
7701 (setq desc (or desc (nth 1 entry)))))
7703 (if (string-match org-plain-link-re link)
7704 ;; URL-like link, normalize the use of angular brackets.
7705 (setq link (org-make-link (org-remove-angle-brackets link))))
7707 ;; Check if we are linking to the current file with a search option
7708 ;; If yes, simplify the link by using only the search option.
7709 (when (and buffer-file-name
7710 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
7711 (let* ((path (match-string 1 link))
7712 (case-fold-search nil)
7713 (search (match-string 2 link)))
7715 (if (equal (file-truename buffer-file-name) (file-truename path))
7716 ;; We are linking to this same file, with a search option
7717 (setq link search)))))
7719 ;; Check if we can/should use a relative path. If yes, simplify the link
7720 (when (string-match "^file:\\(.*\\)" link)
7721 (let* ((path (match-string 1 link))
7723 (case-fold-search nil))
7725 ((or (eq org-link-file-path-type 'absolute)
7726 (equal complete-file '(16)))
7727 (setq path (abbreviate-file-name (expand-file-name path))))
7728 ((eq org-link-file-path-type 'noabbrev)
7729 (setq path (expand-file-name path)))
7730 ((eq org-link-file-path-type 'relative)
7731 (setq path (file-relative-name path)))
7734 (if (string-match (concat "^" (regexp-quote
7735 (file-name-as-directory
7736 (expand-file-name "."))))
7737 (expand-file-name path))
7738 ;; We are linking a file with relative path name.
7739 (setq path (substring (expand-file-name path)
7741 (setq path (abbreviate-file-name (expand-file-name path)))))))
7742 (setq link (concat "file:" path))
7743 (if (equal desc origpath)
7746 (if org-make-link-description-function
7747 (setq desc (funcall org-make-link-description-function link desc)))
7749 (setq desc (read-string "Description: " desc))
7750 (unless (string-match "\\S-" desc) (setq desc nil))
7751 (if remove (apply 'delete-region remove))
7752 (insert (org-make-link-string link desc))))
7754 (defun org-link-try-special-completion (type)
7755 "If there is completion support for link type TYPE, offer it."
7756 (let ((fun (intern (concat "org-" type "-complete-link"))))
7759 (read-string "Link (no completion support): " (concat type ":")))))
7761 (defun org-file-complete-link (&optional arg)
7762 "Create a file link using completion."
7764 (setq file (read-file-name "File: "))
7765 (let ((pwd (file-name-as-directory (expand-file-name ".")))
7766 (pwd1 (file-name-as-directory (abbreviate-file-name
7767 (expand-file-name ".")))))
7770 (setq link (org-make-link
7772 (abbreviate-file-name (expand-file-name file)))))
7773 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
7774 (setq link (org-make-link "file:" (match-string 1 file))))
7775 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
7776 (expand-file-name file))
7777 (setq link (org-make-link
7778 "file:" (match-string 1 (expand-file-name file)))))
7779 (t (setq link (org-make-link "file:" file)))))
7782 (defun org-completing-read (&rest args)
7783 "Completing-read with SPACE being a normal character."
7784 (let ((minibuffer-local-completion-map
7785 (copy-keymap minibuffer-local-completion-map)))
7786 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
7787 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
7788 (apply 'org-icompleting-read args)))
7790 (defun org-completing-read-no-i (&rest args)
7791 (let (org-completion-use-ido org-completion-use-iswitchb)
7792 (apply 'org-completing-read args)))
7794 (defun org-iswitchb-completing-read (prompt choices &rest args)
7795 "Use iswitch as a completing-read replacement to choose from choices.
7796 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
7798 (let* ((iswitchb-use-virtual-buffers nil)
7799 (iswitchb-make-buflist-hook
7801 (setq iswitchb-temp-buflist choices))))
7802 (iswitchb-read-buffer prompt)))
7804 (defun org-icompleting-read (&rest args)
7805 "Completing-read using `ido-mode' or `iswitchb' speedups if available"
7806 (if (and org-completion-use-ido
7807 (fboundp 'ido-completing-read)
7808 (boundp 'ido-mode) ido-mode
7809 (listp (second args)))
7810 (let ((ido-enter-matching-directory nil))
7811 (apply 'ido-completing-read (concat (car args))
7812 (if (consp (car (nth 1 args)))
7813 (mapcar (lambda (x) (car x)) (nth 1 args))
7816 (if (and org-completion-use-iswitchb
7817 (boundp 'iswitchb-mode) iswitchb-mode
7818 (listp (second args)))
7819 (apply 'org-iswitchb-completing-read (concat (car args))
7820 (mapcar (lambda (x) (car x)) (nth 1 args))
7822 (apply 'completing-read args))))
7824 (defun org-extract-attributes (s)
7825 "Extract the attributes cookie from a string and set as text property."
7826 (let (a attr (start 0) key value)
7828 (when (string-match "{{\\([^}]+\\)}}$" s)
7829 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
7830 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
7831 (setq key (match-string 1 a) value (match-string 2 a)
7833 attr (plist-put attr (intern key) value))))
7834 (org-add-props s nil 'org-attr attr))
7837 (defun org-extract-attributes-from-string (tag)
7838 (let (key value attr)
7839 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
7840 (setq key (match-string 1 tag) value (match-string 2 tag)
7841 tag (replace-match "" t t tag)
7842 attr (plist-put attr (intern key) value)))
7845 (defun org-attributes-to-string (plist)
7846 "Format a property list into an HTML attribute list."
7847 (let ((s "") key value)
7849 (setq key (pop plist) value (pop plist))
7851 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
7854 ;;; Opening/following a link
7856 (defvar org-link-search-failed nil)
7858 (defun org-next-link ()
7859 "Move forward to the next link.
7860 If the link is in hidden text, expose it."
7862 (when (and org-link-search-failed (eq this-command last-command))
7863 (goto-char (point-min))
7864 (message "Link search wrapped back to beginning of buffer"))
7865 (setq org-link-search-failed nil)
7866 (let* ((pos (point))
7868 (a (assoc :link ct)))
7869 (if a (goto-char (nth 2 a)))
7870 (if (re-search-forward org-any-link-re nil t)
7872 (goto-char (match-beginning 0))
7873 (if (org-invisible-p) (org-show-context)))
7875 (setq org-link-search-failed t)
7876 (error "No further link found"))))
7878 (defun org-previous-link ()
7879 "Move backward to the previous link.
7880 If the link is in hidden text, expose it."
7882 (when (and org-link-search-failed (eq this-command last-command))
7883 (goto-char (point-max))
7884 (message "Link search wrapped back to end of buffer"))
7885 (setq org-link-search-failed nil)
7886 (let* ((pos (point))
7888 (a (assoc :link ct)))
7889 (if a (goto-char (nth 1 a)))
7890 (if (re-search-backward org-any-link-re nil t)
7892 (goto-char (match-beginning 0))
7893 (if (org-invisible-p) (org-show-context)))
7895 (setq org-link-search-failed t)
7896 (error "No further link found"))))
7898 (defun org-translate-link (s)
7899 "Translate a link string if a translation function has been defined."
7900 (if (and org-link-translation-function
7901 (fboundp org-link-translation-function)
7902 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
7904 (setq s (funcall org-link-translation-function
7905 (match-string 1) (match-string 2)))
7906 (concat (car s) ":" (cdr s)))
7909 (defun org-translate-link-from-planner (type path)
7910 "Translate a link from Emacs Planner syntax so that Org can follow it.
7911 This is still an experimental function, your mileage may vary."
7913 ((member type '("http" "https" "news" "ftp"))
7914 ;; standard Internet links are the same.
7916 ((and (equal type "irc") (string-match "^//" path))
7917 ;; Planner has two / at the beginning of an irc link, we have 1.
7918 ;; We should have zero, actually....
7919 (setq path (substring path 1)))
7920 ((and (equal type "lisp") (string-match "^/" path))
7921 ;; Planner has a slash, we do not.
7922 (setq type "elisp" path (substring path 1)))
7923 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
7924 ;; A typical message link. Planner has the id after the fina slash,
7925 ;; we separate it with a hash mark
7926 (setq path (concat (match-string 1 path) "#"
7927 (org-remove-angle-brackets (match-string 2 path)))))
7931 (defun org-find-file-at-mouse (ev)
7932 "Open file link or URL at mouse."
7934 (mouse-set-point ev)
7935 (org-open-at-point 'in-emacs))
7937 (defun org-open-at-mouse (ev)
7938 "Open file link or URL at mouse."
7940 (mouse-set-point ev)
7941 (if (eq major-mode 'org-agenda-mode)
7942 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
7943 (org-open-at-point))
7945 (defvar org-window-config-before-follow-link nil
7946 "The window configuration before following a link.
7947 This is saved in case the need arises to restore it.")
7949 (defvar org-open-link-marker (make-marker)
7950 "Marker pointing to the location where `org-open-at-point; was called.")
7953 (defun org-open-at-point-global ()
7954 "Follow a link like Org-mode does.
7955 This command can be called in any mode to follow a link that has
7958 (org-run-like-in-org-mode 'org-open-at-point))
7961 (defun org-open-link-from-string (s &optional arg reference-buffer)
7962 "Open a link in the string S, as if it was in Org-mode."
7963 (interactive "sLink: \nP")
7964 (let ((reference-buffer (or reference-buffer (current-buffer))))
7966 (let ((org-inhibit-startup t))
7969 (goto-char (point-min))
7970 (org-open-at-point arg reference-buffer)))))
7972 (defun org-open-at-point (&optional in-emacs reference-buffer)
7973 "Open link at or after point.
7974 If there is no link at point, this function will search forward up to
7975 the end of the current line.
7976 Normally, files will be opened by an appropriate application. If the
7977 optional argument IN-EMACS is non-nil, Emacs will visit the file.
7978 With a double prefix argument, try to open outside of Emacs, in the
7979 application the system uses for this file type."
7981 (org-load-modules-maybe)
7982 (move-marker org-open-link-marker (point))
7983 (setq org-window-config-before-follow-link (current-window-configuration))
7984 (org-remove-occur-highlights nil nil t)
7986 ((and (org-on-heading-p)
7988 (concat org-plain-link-re "\\|"
7989 org-bracket-link-regexp "\\|"
7990 org-angle-link-re "\\|"
7991 "[ \t]:[^ \t\n]+:[ \t]*$"))))
7992 (org-offer-links-in-entry in-emacs))
7993 ((org-at-timestamp-p t) (org-follow-timestamp-link))
7994 ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
7995 (org-footnote-action))
7997 (let (type path link line search (pos (point)))
8000 (skip-chars-forward "^]\n\r")
8001 (when (org-in-regexp org-bracket-link-regexp)
8002 (setq link (org-extract-attributes
8003 (org-link-unescape (org-match-string-no-properties 1))))
8004 (while (string-match " *\n *" link)
8005 (setq link (replace-match " " t t link)))
8006 (setq link (org-link-expand-abbrev link))
8008 ((or (file-name-absolute-p link)
8009 (string-match "^\\.\\.?/" link))
8010 (setq type "file" path link))
8011 ((string-match org-link-re-with-space3 link)
8012 (setq type (match-string 1 link) path (match-string 2 link)))
8013 (t (setq type "thisfile" path link)))
8016 (when (get-text-property (point) 'org-linked-text)
8017 (setq type "thisfile"
8018 pos (if (get-text-property (1+ (point)) 'org-linked-text)
8019 (1+ (point)) (point))
8020 path (buffer-substring
8021 (previous-single-property-change pos 'org-linked-text)
8022 (next-single-property-change pos 'org-linked-text)))
8026 (when (or (org-in-regexp org-angle-link-re)
8027 (org-in-regexp org-plain-link-re))
8028 (setq type (match-string 1) path (match-string 2))
8031 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
8033 path (match-string 1))
8034 (while (string-match ":" path)
8035 (setq path (replace-match "+" t t path)))
8037 (when (org-in-regexp "<\\([^><\n]+\\)>")
8038 (setq type "tree-match"
8039 path (match-string 1))
8042 (error "No link found"))
8044 ;; switch back to reference buffer
8045 ;; needed when if called in a temporary buffer through
8046 ;; org-open-link-from-string
8047 (with-current-buffer (or reference-buffer (current-buffer))
8049 ;; Remove any trailing spaces in path
8050 (if (string-match " +\\'" path)
8051 (setq path (replace-match "" t t path)))
8052 (if (and org-link-translation-function
8053 (fboundp org-link-translation-function))
8054 ;; Check if we need to translate the link
8055 (let ((tmp (funcall org-link-translation-function type path)))
8056 (setq type (car tmp) path (cdr tmp))))
8060 ((assoc type org-link-protocols)
8061 (funcall (nth 1 (assoc type org-link-protocols)) path))
8063 ((equal type "mailto")
8064 (let ((cmd (car org-link-mailto-program))
8065 (args (cdr org-link-mailto-program)) args1
8066 (address path) (subject "") a)
8067 (if (string-match "\\(.*\\)::\\(.*\\)" path)
8068 (setq address (match-string 1 path)
8069 subject (org-link-escape (match-string 2 path))))
8072 ((not (stringp (car args))) (push (pop args) args1))
8073 (t (setq a (pop args))
8074 (if (string-match "%a" a)
8075 (setq a (replace-match address t t a)))
8076 (if (string-match "%s" a)
8077 (setq a (replace-match subject t t a)))
8079 (apply cmd (nreverse args1))))
8081 ((member type '("http" "https" "ftp" "news"))
8082 (browse-url (concat type ":" (org-link-escape
8083 path org-link-escape-chars-browser))))
8085 ((member type '("message"))
8086 (browse-url (concat type ":" path)))
8088 ((string= type "tags")
8089 (org-tags-view in-emacs path))
8090 ((string= type "thisfile")
8092 (switch-to-buffer-other-window
8093 (org-get-buffer-for-internal-link (current-buffer)))
8094 (org-mark-ring-push))
8095 (let ((cmd `(org-link-search
8097 ,(cond ((equal in-emacs '(4)) 'occur)
8098 ((equal in-emacs '(16)) 'org-occur)
8101 (condition-case nil (eval cmd)
8102 (error (progn (widen) (eval cmd))))))
8104 ((string= type "tree-match")
8105 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
8107 ((string= type "file")
8108 (if (string-match "::\\([0-9]+\\)\\'" path)
8109 (setq line (string-to-number (match-string 1 path))
8110 path (substring path 0 (match-beginning 0)))
8111 (if (string-match "::\\(.+\\)\\'" path)
8112 (setq search (match-string 1 path)
8113 path (substring path 0 (match-beginning 0)))))
8114 (if (string-match "[*?{]" (file-name-nondirectory path))
8116 (org-open-file path in-emacs line search)))
8118 ((string= type "news")
8120 (org-gnus-follow-link path))
8122 ((string= type "shell")
8124 (if (or (not org-confirm-shell-link-function)
8125 (funcall org-confirm-shell-link-function
8126 (format "Execute \"%s\" in shell? "
8127 (org-add-props cmd nil
8128 'face 'org-warning))))
8130 (message "Executing %s" cmd)
8131 (shell-command cmd))
8134 ((string= type "elisp")
8136 (if (or (not org-confirm-elisp-link-function)
8137 (funcall org-confirm-elisp-link-function
8138 (format "Execute \"%s\" as elisp? "
8139 (org-add-props cmd nil
8140 'face 'org-warning))))
8141 (message "%s => %s" cmd
8142 (if (equal (string-to-char cmd) ?\()
8144 (call-interactively (read cmd))))
8148 (browse-url-at-point)))))))
8149 (move-marker org-open-link-marker nil)
8150 (run-hook-with-args 'org-follow-link-hook))
8152 (defun org-offer-links-in-entry (&optional nth zero)
8153 "Offer links in the curren entry and follow the selected link.
8154 If there is only one link, follow it immediately as well.
8155 If NTH is an integer, immediately pick the NTH link found.
8156 If ZERO is a string, check also this string for a link, and if
8157 there is one, offer it as link number zero."
8158 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
8159 "\\(" org-angle-link-re "\\)\\|"
8160 "\\(" org-plain-link-re "\\)"))
8162 (in-emacs (if (integerp nth) nil nth))
8163 have-zero end links link c)
8164 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
8165 (push (match-string 0 zero) links)
8166 (setq cnt (1- cnt) have-zero t))
8168 (org-back-to-heading t)
8169 (setq end (save-excursion (outline-next-heading) (point)))
8170 (while (re-search-forward re end t)
8171 (push (match-string 0) links))
8172 (setq links (org-uniquify (reverse links))))
8175 ((null links) (error "No links"))
8176 ((equal (length links) 1)
8177 (setq link (car links)))
8178 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
8179 (setq link (nth (if have-zero nth (1- nth)) links)))
8180 (t ; we have to select a link
8182 (save-window-excursion
8183 (delete-other-windows)
8184 (with-output-to-temp-buffer "*Select Link*"
8186 (if (not (string-match org-bracket-link-regexp l))
8187 (princ (format "[%c] %s\n" (incf cnt)
8188 (org-remove-angle-brackets l)))
8190 (princ (format "[%c] %s (%s)\n" (incf cnt)
8191 (match-string 3 l) (match-string 1 l)))
8192 (princ (format "[%c] %s\n" (incf cnt)
8193 (match-string 1 l))))))
8195 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
8196 (message "Select link to open:")
8197 (setq c (read-char-exclusive))
8198 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
8199 (when (equal c ?q) (error "Abort"))
8201 (if have-zero (setq nth (1+ nth)))
8202 (unless (and (integerp nth) (>= (length links) nth))
8203 (error "Invalid link selection"))
8204 (setq link (nth (1- nth) links))))
8205 (org-open-link-from-string link in-emacs (current-buffer))))
8209 (defun org-get-effort (&optional pom)
8210 "Get the effort estimate for the current entry."
8211 (org-entry-get pom org-effort-property))
8215 (defvar org-create-file-search-functions nil
8216 "List of functions to construct the right search string for a file link.
8217 These functions are called in turn with point at the location to
8218 which the link should point.
8220 A function in the hook should first test if it would like to
8221 handle this file type, for example by checking the major-mode or
8222 the file extension. If it decides not to handle this file, it
8223 should just return nil to give other functions a chance. If it
8224 does handle the file, it must return the search string to be used
8225 when following the link. The search string will be part of the
8226 file link, given after a double colon, and `org-open-at-point'
8227 will automatically search for it. If special measures must be
8228 taken to make the search successful, another function should be
8229 added to the companion hook `org-execute-file-search-functions',
8232 A function in this hook may also use `setq' to set the variable
8233 `description' to provide a suggestion for the descriptive text to
8234 be used for this link when it gets inserted into an Org-mode
8235 buffer with \\[org-insert-link].")
8237 (defvar org-execute-file-search-functions nil
8238 "List of functions to execute a file search triggered by a link.
8240 Functions added to this hook must accept a single argument, the
8241 search string that was part of the file link, the part after the
8242 double colon. The function must first check if it would like to
8243 handle this search, for example by checking the major-mode or the
8244 file extension. If it decides not to handle this search, it
8245 should just return nil to give other functions a chance. If it
8246 does handle the search, it must return a non-nil value to keep
8247 other functions from trying.
8249 Each function can access the current prefix argument through the
8250 variable `current-prefix-argument'. Note that a single prefix is
8251 used to force opening a link in Emacs, so it may be good to only
8252 use a numeric or double prefix to guide the search function.
8254 In case this is needed, a function in this hook can also restore
8255 the window configuration before `org-open-at-point' was called using:
8257 (set-window-configuration org-window-config-before-follow-link)")
8259 (defun org-link-search (s &optional type avoid-pos)
8260 "Search for a link search option.
8261 If S is surrounded by forward slashes, it is interpreted as a
8262 regular expression. In org-mode files, this will create an `org-occur'
8263 sparse tree. In ordinary files, `occur' will be used to list matches.
8264 If the current buffer is in `dired-mode', grep will be used to search
8265 in all files. If AVOID-POS is given, ignore matches near that position."
8266 (let ((case-fold-search t)
8267 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
8268 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
8269 (append '(("") (" ") ("\t") ("\n"))
8273 (pre nil) (post nil)
8274 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
8276 ;; First check if there are any special
8277 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
8278 ;; Now try the builtin stuff
8279 ((and (equal (string-to-char s0) ?#)
8282 (goto-char (point-min))
8285 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
8286 (setq type 'dedicated
8287 pos (match-beginning 0))))
8288 ;; There is an exact target for this
8290 (org-back-to-heading t)))
8292 (goto-char (point-min))
8295 (concat "<<" (regexp-quote s0) ">>") nil t)
8296 (setq type 'dedicated
8297 pos (match-beginning 0))))
8298 ;; There is an exact target for this
8300 ((and (string-match "^(\\(.*\\))$" s0)
8302 (goto-char (point-min))
8305 (concat "[^[]" (regexp-quote
8306 (format org-coderef-label-format
8307 (match-string 1 s0))))
8309 (setq type 'dedicated
8310 pos (1+ (match-beginning 0))))))
8311 ;; There is a coderef target for this
8313 ((string-match "^/\\(.*\\)/$" s)
8314 ;; A regular expression
8317 (org-occur (match-string 1 s)))
8318 ;;((eq major-mode 'dired-mode)
8319 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
8320 (t (org-do-occur (match-string 1 s)))))
8322 ;; A normal search strings
8323 (when (equal (string-to-char s) ?*)
8324 ;; Anchor on headlines, post may include tags.
8325 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
8326 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
8328 (remove-text-properties
8330 '(face nil mouse-face nil keymap nil fontified nil) s)
8331 ;; Make a series of regular expressions to find a match
8332 (setq words (org-split-string s "[ \n\r\t]+")
8334 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
8335 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
8337 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
8338 re2a (concat "[ \t\r\n]" re2a_)
8339 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
8340 re4 (concat "[^a-zA-Z_]" re4_)
8342 re1 (concat pre re2 post)
8343 re3 (concat pre (if pre re4_ re4) post)
8344 re5 (concat pre ".*" re4)
8345 re2 (concat pre re2)
8346 re2a (concat pre (if pre re2a_ re2a))
8347 re4 (concat pre (if pre re4_ re4))
8348 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
8349 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
8353 ((eq type 'org-occur) (org-occur reall))
8354 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
8355 (t (goto-char (point-min))
8357 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
8358 (org-search-not-self 1 re1 nil t)
8359 (org-search-not-self 1 re2 nil t)
8360 (org-search-not-self 1 re2a nil t)
8361 (org-search-not-self 1 re3 nil t)
8362 (org-search-not-self 1 re4 nil t)
8363 (org-search-not-self 1 re5 nil t)
8365 (goto-char (match-beginning 1))
8367 (error "No match")))))
8369 ;; Normal string-search
8370 (goto-char (point-min))
8371 (if (search-forward s nil t)
8372 (goto-char (match-beginning 0))
8373 (error "No match"))))
8374 (and (org-mode-p) (org-show-context 'link-search))
8377 (defun org-search-not-self (group &rest args)
8378 "Execute `re-search-forward', but only accept matches that do not
8379 enclose the position of `org-open-link-marker'."
8380 (let ((m org-open-link-marker))
8382 (while (apply 're-search-forward args)
8383 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
8384 (goto-char (match-end group))
8385 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
8386 (> (match-beginning 0) (marker-position m))
8387 (< (match-end 0) (marker-position m)))
8389 (or (not (org-in-regexp
8390 org-bracket-link-analytic-regexp 1))
8391 (not (match-end 4)) ; no description
8392 (and (<= (match-beginning 4) (point))
8393 (>= (match-end 4) (point))))))
8394 (throw 'exit (point))))))))
8396 (defun org-get-buffer-for-internal-link (buffer)
8397 "Return a buffer to be used for displaying the link target of internal links."
8399 ((not org-display-internal-link-with-indirect-buffer)
8401 ((string-match "(Clone)$" (buffer-name buffer))
8402 (message "Buffer is already a clone, not making another one")
8403 ;; we also do not modify visibility in this case
8405 (t ; make a new indirect buffer for displaying the link
8406 (let* ((bn (buffer-name buffer))
8407 (ibn (concat bn "(Clone)"))
8408 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
8409 (with-current-buffer ib (org-overview))
8412 (defun org-do-occur (regexp &optional cleanup)
8413 "Call the Emacs command `occur'.
8414 If CLEANUP is non-nil, remove the printout of the regular expression
8415 in the *Occur* buffer. This is useful if the regex is long and not useful
8419 (let ((cwin (selected-window)) win beg end)
8420 (when (setq win (get-buffer-window "*Occur*"))
8421 (select-window win))
8422 (goto-char (point-min))
8423 (when (re-search-forward "match[a-z]+" nil t)
8424 (setq beg (match-end 0))
8425 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
8426 (setq end (1- (match-beginning 0)))))
8427 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
8428 (goto-char (point-min))
8429 (select-window cwin))))
8431 ;;; The mark ring for links jumps
8433 (defvar org-mark-ring nil
8434 "Mark ring for positions before jumps in Org-mode.")
8435 (defvar org-mark-ring-last-goto nil
8436 "Last position in the mark ring used to go back.")
8437 ;; Fill and close the ring
8438 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
8439 (loop for i from 1 to org-mark-ring-length do
8440 (push (make-marker) org-mark-ring))
8441 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
8444 (defun org-mark-ring-push (&optional pos buffer)
8445 "Put the current position or POS into the mark ring and rotate it."
8447 (setq pos (or pos (point)))
8448 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
8449 (move-marker (car org-mark-ring)
8451 (or buffer (current-buffer)))
8453 (substitute-command-keys
8454 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
8456 (defun org-mark-ring-goto (&optional n)
8457 "Jump to the previous position in the mark ring.
8458 With prefix arg N, jump back that many stored positions. When
8459 called several times in succession, walk through the entire ring.
8460 Org-mode commands jumping to a different position in the current file,
8461 or to another Org-mode file, automatically push the old position
8465 (if (eq last-command this-command)
8466 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
8467 (setq p org-mark-ring))
8468 (setq org-mark-ring-last-goto p)
8470 (switch-to-buffer (marker-buffer m))
8472 (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
8474 (defun org-remove-angle-brackets (s)
8475 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
8476 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
8478 (defun org-add-angle-brackets (s)
8479 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
8480 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
8482 (defun org-remove-double-quotes (s)
8483 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
8484 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
8487 ;;; Following specific links
8489 (defun org-follow-timestamp-link ()
8491 ((org-at-date-range-p t)
8492 (let ((org-agenda-start-on-weekday)
8493 (t1 (match-string 1))
8494 (t2 (match-string 2)))
8495 (setq t1 (time-to-days (org-time-string-to-time t1))
8496 t2 (time-to-days (org-time-string-to-time t2)))
8497 (org-agenda-list nil t1 (1+ (- t2 t1)))))
8498 ((org-at-timestamp-p t)
8499 (org-agenda-list nil (time-to-days (org-time-string-to-time
8500 (substring (match-string 1) 0 10)))
8502 (t (error "This should not happen"))))
8505 ;;; Following file links
8506 (defvar org-wait nil)
8507 (defun org-open-file (path &optional in-emacs line search)
8508 "Open the file at PATH.
8509 First, this expands any special file name abbreviations. Then the
8510 configuration variable `org-file-apps' is checked if it contains an
8511 entry for this file type, and if yes, the corresponding command is launched.
8513 If no application is found, Emacs simply visits the file.
8515 With optional prefix argument IN-EMACS, Emacs will visit the file.
8516 With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
8517 and o use an external application to visit the file.
8519 Optional LINE specifies a line to go to, optional SEARCH a string to
8520 search for. If LINE or SEARCH is given, the file will always be
8522 If the file does not exist, an error is thrown."
8523 (setq in-emacs (or in-emacs line search))
8524 (let* ((file (if (equal path "")
8526 (substitute-in-file-name (expand-file-name path))))
8527 (apps (append org-file-apps (org-default-apps)))
8528 (remp (and (assq 'remote apps) (org-file-remote-p file)))
8529 (dirp (if remp nil (file-directory-p file)))
8530 (file (if (and dirp org-open-directory-means-index-dot-org)
8531 (concat (file-name-as-directory file) "index.org")
8533 (a-m-a-p (assq 'auto-mode apps))
8534 (dfile (downcase file))
8535 (old-buffer (current-buffer))
8537 (old-mode major-mode)
8539 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
8540 (setq ext (match-string 1 dfile))
8541 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
8542 (setq ext (match-string 1 dfile))))
8544 ((equal in-emacs '(16))
8545 (setq cmd (cdr (assoc 'system apps))))
8546 (in-emacs (setq cmd 'emacs))
8548 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
8549 (and dirp (cdr (assoc 'directory apps)))
8550 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
8552 (cdr (assoc ext apps))
8553 (cdr (assoc t apps))))))
8554 (when (eq cmd 'system)
8555 (setq cmd (cdr (assoc 'system apps))))
8556 (when (eq cmd 'default)
8557 (setq cmd (cdr (assoc t apps))))
8558 (when (eq cmd 'mailcap)
8560 (mailcap-parse-mailcaps)
8561 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
8562 (command (mailcap-mime-info mime-type)))
8563 (if (stringp command)
8565 (setq cmd 'emacs))))
8566 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
8567 (not (file-exists-p file))
8568 (not org-open-non-existing-files))
8569 (error "No such file: %s" file))
8571 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
8572 ;; Remove quotes around the file name - we'll use shell-quote-argument.
8573 (while (string-match "['\"]%s['\"]" cmd)
8574 (setq cmd (replace-match "%s" t t cmd)))
8575 (while (string-match "%s" cmd)
8576 (setq cmd (replace-match
8578 (shell-quote-argument
8579 (convert-standard-filename file)))
8581 (save-window-excursion
8582 (start-process-shell-command cmd nil cmd)
8583 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
8587 (funcall (cdr (assq 'file org-link-frame-setup)) file)
8589 (if line (org-goto-line line)
8590 (if search (org-link-search search))))
8592 (let ((file (convert-standard-filename file)))
8594 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
8595 (and (org-mode-p) (eq old-mode 'org-mode)
8596 (or (not (equal old-buffer (current-buffer)))
8597 (not (equal old-pos (point))))
8598 (org-mark-ring-push old-pos old-buffer))))
8600 (defun org-default-apps ()
8601 "Return the default applications for this operating system."
8603 ((eq system-type 'darwin)
8604 org-file-apps-defaults-macosx)
8605 ((eq system-type 'windows-nt)
8606 org-file-apps-defaults-windowsnt)
8607 (t org-file-apps-defaults-gnu)))
8609 (defun org-apps-regexp-alist (list &optional add-auto-mode)
8610 "Convert extensions to regular expressions in the cars of LIST.
8611 Also, weed out any non-string entries, because the return value is used
8612 only for regexp matching.
8613 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
8614 point to the symbol `emacs', indicating that the file should
8615 be opened in Emacs."
8619 (if (not (stringp (car x)))
8621 (if (string-match "\\W" (car x))
8623 (cons (concat "\\." (car x) "\\'") (cdr x)))))
8626 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
8628 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
8629 (defun org-file-remote-p (file)
8630 "Test whether FILE specifies a location on a remote system.
8631 Return non-nil if the location is indeed remote.
8633 For example, the filename \"/user@host:/foo\" specifies a location
8634 on the system \"/user@host:\"."
8635 (cond ((fboundp 'file-remote-p)
8636 (file-remote-p file))
8637 ((fboundp 'tramp-handle-file-remote-p)
8638 (tramp-handle-file-remote-p file))
8639 ((and (boundp 'ange-ftp-name-format)
8640 (string-match (car ange-ftp-name-format) file))
8647 (defun org-get-org-file ()
8648 "Read a filename, with default directory `org-directory'."
8649 (let ((default (or org-default-notes-file remember-data-file)))
8650 (read-file-name (format "File name [%s]: " default)
8651 (file-name-as-directory org-directory)
8654 (defun org-notes-order-reversed-p ()
8655 "Check if the current file should receive notes in reversed order."
8657 ((not org-reverse-note-order) nil)
8658 ((eq t org-reverse-note-order) t)
8659 ((not (listp org-reverse-note-order)) nil)
8661 (let ((all org-reverse-note-order)
8663 (while (setq entry (pop all))
8664 (if (string-match (car entry) buffer-file-name)
8665 (throw 'exit (cdr entry))))
8668 (defvar org-refile-target-table nil
8669 "The list of refile targets, created by `org-refile'.")
8671 (defvar org-agenda-new-buffers nil
8672 "Buffers created to visit agenda files.")
8674 (defun org-get-refile-targets (&optional default-buffer)
8675 "Produce a table with refile targets."
8676 (let ((case-fold-search nil)
8677 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
8678 (entries (or org-refile-targets '((nil . (:level . 1)))))
8679 targets txt re files f desc descre fast-path-p level pos0)
8680 (message "Getting targets...")
8681 (with-current-buffer (or default-buffer (current-buffer))
8682 (while (setq entry (pop entries))
8683 (setq files (car entry) desc (cdr entry))
8684 (setq fast-path-p nil)
8686 ((null files) (setq files (list (current-buffer))))
8687 ((eq files 'org-agenda-files)
8688 (setq files (org-agenda-files 'unrestricted)))
8689 ((and (symbolp files) (fboundp files))
8690 (setq files (funcall files)))
8691 ((and (symbolp files) (boundp files))
8692 (setq files (symbol-value files))))
8693 (if (stringp files) (setq files (list files)))
8695 ((eq (car desc) :tag)
8696 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
8697 ((eq (car desc) :todo)
8698 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
8699 ((eq (car desc) :regexp)
8700 (setq descre (cdr desc)))
8701 ((eq (car desc) :level)
8702 (setq descre (concat "^\\*\\{" (number-to-string
8703 (if org-odd-levels-only
8704 (1- (* 2 (cdr desc)))
8707 ((eq (car desc) :maxlevel)
8708 (setq fast-path-p t)
8709 (setq descre (concat "^\\*\\{1," (number-to-string
8710 (if org-odd-levels-only
8711 (1- (* 2 (cdr desc)))
8714 (t (error "Bad refiling target description %s" desc)))
8715 (while (setq f (pop files))
8717 (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
8718 (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
8719 (setq f (expand-file-name f))
8720 (if (eq org-refile-use-outline-path 'file)
8721 (push (list (file-name-nondirectory f) f nil nil) targets))
8725 (goto-char (point-min))
8726 (while (re-search-forward descre nil t)
8727 (goto-char (setq pos0 (point-at-bol)))
8729 (when org-refile-target-verify-function
8731 (or (funcall org-refile-target-verify-function)
8733 (when (looking-at org-complex-heading-regexp)
8734 (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
8735 txt (org-link-display-format (match-string 4))
8736 re (concat "^" (regexp-quote
8737 (buffer-substring (match-beginning 1)
8739 (if (match-end 5) (setq re (concat re "[ \t]+"
8741 (match-string 5)))))
8742 (setq re (concat re "[ \t]*$"))
8743 (when org-refile-use-outline-path
8744 (setq txt (mapconcat 'org-protect-slash
8746 (if (eq org-refile-use-outline-path 'file)
8747 (list (file-name-nondirectory
8748 (buffer-file-name (buffer-base-buffer))))
8749 (if (eq org-refile-use-outline-path 'full-file-path)
8750 (list (buffer-file-name (buffer-base-buffer)))))
8751 (org-get-outline-path fast-path-p level txt)
8754 (push (list txt f re (point)) targets)))
8755 (when (= (point) pos0)
8756 ;; verification function has not moved point
8757 (goto-char (point-at-eol))))))))))
8758 (message "Getting targets...done")
8759 (nreverse targets)))
8761 (defun org-protect-slash (s)
8762 (while (string-match "/" s)
8763 (setq s (replace-match "\\" t t s)))
8766 (defvar org-olpa (make-vector 20 nil))
8768 (defun org-get-outline-path (&optional fastp level heading)
8769 "Return the outline path to the current entry, as a list."
8773 (error "Outline path failure, more than 19 levels."))
8774 (loop for i from level upto 19 do
8775 (aset org-olpa i nil))
8777 (delq nil (append org-olpa nil))
8778 (aset org-olpa level heading)))
8781 (while (org-up-heading-safe)
8782 (when (looking-at org-complex-heading-regexp)
8783 (push (org-match-string-no-properties 4) rtn)))
8786 (defvar org-refile-history nil
8787 "History for refiling operations.")
8789 (defvar org-after-refile-insert-hook nil
8790 "Hook run after `org-refile' has inserted its stuff at the new location.
8791 Note that this is still *before* the stuff will be removed from
8792 the *old* location.")
8794 (defun org-refile (&optional goto default-buffer rfloc)
8795 "Move the entry at point to another heading.
8796 The list of target headings is compiled using the information in
8797 `org-refile-targets', which see. This list is created before each use
8798 and will therefore always be up-to-date.
8800 At the target location, the entry is filed as a subitem of the target heading.
8801 Depending on `org-reverse-note-order', the new subitem will either be the
8802 first or the last subitem.
8804 If there is an active region, all entries in that region will be moved.
8805 However, the region must fulfil the requirement that the first heading
8806 is the first one sets the top-level of the moved text - at most siblings
8807 below it are allowed.
8809 With prefix arg GOTO, the command will only visit the target location,
8810 not actually move anything.
8811 With a double prefix `C-u C-u', go to the location where the last refiling
8812 operation has put the subtree.
8814 RFLOC can be a refile location obtained in a different way.
8816 See also `org-refile-use-outline-path' and `org-completion-use-ido'"
8818 (let* ((cbuf (current-buffer))
8819 (regionp (org-region-active-p))
8820 (region-start (and regionp (region-beginning)))
8821 (region-end (and regionp (region-end)))
8822 (region-length (and regionp (- region-end region-start)))
8823 (filename (buffer-file-name (buffer-base-buffer cbuf)))
8824 pos it nbuf file re level reversed)
8826 (goto-char region-start)
8827 (or (bolp) (goto-char (point-at-bol)))
8828 (setq region-start (point))
8829 (unless (org-kill-is-subtree-p
8830 (buffer-substring region-start region-end))
8831 (error "The region is not a (sequence of) subtree(s)")))
8832 (if (equal goto '(16))
8833 (org-refile-goto-last-stored)
8834 (when (setq it (or rfloc
8836 (org-refile-get-location
8837 (if goto "Goto: " "Refile to: ") default-buffer
8838 org-refile-allow-creating-parent-nodes))))
8839 (setq file (nth 1 it)
8844 (equal (buffer-file-name) file)
8846 (and (>= pos region-start)
8847 (<= pos region-end))
8848 (and (>= pos (point))
8849 (< pos (save-excursion
8850 (org-end-of-subtree t t))))))
8851 (error "Cannot refile to position inside the tree or region"))
8853 (setq nbuf (or (find-buffer-visiting file)
8854 (find-file-noselect file)))
8857 (switch-to-buffer nbuf)
8859 (org-show-context 'org-goto))
8862 (org-kill-new (buffer-substring region-start region-end))
8863 (org-save-markers-in-region region-start region-end))
8864 (org-copy-subtree 1 nil t))
8866 (set-buffer (setq nbuf (or (find-buffer-visiting file)
8867 (find-file-noselect file))))
8868 (setq reversed (org-notes-order-reversed-p))
8875 (looking-at outline-regexp)
8876 (setq level (org-get-valid-level (funcall outline-level) 1))
8879 (or (outline-next-heading) (point-max))
8880 (or (save-excursion (org-get-next-sibling))
8881 (org-end-of-subtree t t)
8885 (goto-char (point-max))
8886 (goto-char (point-min))
8887 (or (outline-next-heading) (goto-char (point-max)))))
8888 (if (not (bolp)) (newline))
8889 (bookmark-set "org-refile-last-stored")
8890 (org-paste-subtree level)
8891 (if (fboundp 'deactivate-mark) (deactivate-mark))
8892 (run-hooks 'org-after-refile-insert-hook))))
8894 (delete-region (point) (+ (point) region-length))
8896 (when (featurep 'org-inlinetask)
8897 (org-inlinetask-remove-END-maybe))
8898 (setq org-markers-to-move nil)
8899 (message "Refiled to \"%s\"" (car it))))))
8902 (defun org-refile-goto-last-stored ()
8903 "Go to the location where the last refile was stored."
8905 (bookmark-jump "org-refile-last-stored")
8906 (message "This is the location of the last refile"))
8908 (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
8909 "Prompt the user for a refile location, using PROMPT."
8910 (let ((org-refile-targets org-refile-targets)
8911 (org-refile-use-outline-path org-refile-use-outline-path))
8912 (setq org-refile-target-table (org-get-refile-targets default-buffer)))
8913 (unless org-refile-target-table
8914 (error "No refile targets"))
8915 (let* ((cbuf (current-buffer))
8916 (partial-completion-mode nil)
8917 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
8918 (cfunc (if (and org-refile-use-outline-path
8919 org-outline-path-complete-in-steps)
8920 'org-olpath-completing-read
8921 'org-icompleting-read))
8922 (extra (if org-refile-use-outline-path "/" ""))
8923 (filename (and cfn (expand-file-name cfn)))
8926 (if (and (not (member org-refile-use-outline-path
8927 '(file full-file-path)))
8928 (not (equal filename (nth 1 x))))
8929 (cons (concat (car x) extra " ("
8930 (file-name-nondirectory (nth 1 x)) ")")
8932 (cons (concat (car x) extra) (cdr x))))
8933 org-refile-target-table))
8934 (completion-ignore-case t)
8935 pa answ parent-target child parent old-hist)
8936 (setq old-hist org-refile-history)
8937 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
8938 nil 'org-refile-history))
8939 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
8942 (when (or (not org-refile-history)
8943 (not (eq old-hist org-refile-history))
8944 (not (equal (car pa) (car org-refile-history))))
8945 (setq org-refile-history
8946 (cons (car pa) (if (assoc (car org-refile-history) tbl)
8948 (cdr org-refile-history))))
8949 (if (equal (car org-refile-history) (nth 1 org-refile-history))
8950 (pop org-refile-history)))
8952 (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
8953 (setq parent (match-string 1 answ)
8954 child (match-string 2 answ))
8955 (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
8956 (when (and parent-target
8957 (or (eq new-nodes t)
8958 (and (eq new-nodes 'confirm)
8959 (y-or-n-p (format "Create new node \"%s\"? " child)))))
8960 (org-refile-new-child parent-target child))))))
8962 (defun org-refile-new-child (parent-target child)
8963 "Use refile target PARENT-TARGET to add new CHILD below it."
8964 (unless parent-target
8965 (error "Cannot find parent for new node"))
8966 (let ((file (nth 1 parent-target))
8967 (pos (nth 3 parent-target))
8969 (with-current-buffer (or (find-buffer-visiting file)
8970 (find-file-noselect file))
8976 (goto-char (point-max))
8977 (if (not (bolp)) (newline)))
8978 (when (looking-at outline-regexp)
8979 (setq level (funcall outline-level))
8980 (org-end-of-subtree t t))
8981 (org-back-over-empty-lines)
8982 (insert "\n" (make-string
8983 (if pos (org-get-valid-level level 1) 1) ?*)
8985 (beginning-of-line 0)
8986 (list (concat (car parent-target) "/" child) file "" (point)))))))
8988 (defun org-olpath-completing-read (prompt collection &rest args)
8989 "Read an outline path like a file name."
8990 (let ((thetable collection)
8991 (org-completion-use-ido nil) ; does not work with ido.
8992 (org-completion-use-iswitchb nil)) ; or iswitchb
8994 'org-icompleting-read prompt
8995 (lambda (string predicate &optional flag)
8996 (let (rtn r f (l (length string)))
9000 (try-completion string thetable))
9003 (setq rtn (all-completions string thetable predicate))
9006 (setq r (substring x l))
9007 (if (string-match " ([^)]*)$" x)
9008 (setq f (match-string 0 x))
9010 (if (string-match "/" r)
9011 (concat string (substring r 0 (match-end 0)) f)
9016 (assoc string thetable)))
9022 (defun org-find-dblock (name)
9023 "Find the first dynamic block with name NAME in the buffer.
9024 If not found, stay at current position and return nil."
9027 (goto-char (point-min))
9028 (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
9030 (match-beginning 0))))
9031 (if pos (goto-char pos))
9034 (defconst org-dblock-start-re
9035 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
9036 "Matches the startline of a dynamic block, with parameters.")
9038 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
9039 "Matches the end of a dynamic block.")
9041 (defun org-create-dblock (plist)
9042 "Create a dynamic block section, with parameters taken from PLIST.
9043 PLIST must contain a :name entry which is used as name of the block."
9044 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
9047 (let ((col (current-column))
9048 (name (plist-get plist :name)))
9049 (insert "#+BEGIN: " name)
9051 (if (eq (car plist) :name)
9052 (setq plist (cddr plist))
9053 (insert " " (prin1-to-string (pop plist)))))
9054 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
9055 (beginning-of-line -2)))
9057 (defun org-prepare-dblock ()
9058 "Prepare dynamic block for refresh.
9059 This empties the block, puts the cursor at the insert position and returns
9060 the property list including an extra property :name with the block name."
9061 (unless (looking-at org-dblock-start-re)
9062 (error "Not at a dynamic block"))
9063 (let* ((begdel (1+ (match-end 0)))
9064 (name (org-no-properties (match-string 1)))
9065 (params (append (list :name name)
9066 (read (concat "(" (match-string 3) ")")))))
9068 (beginning-of-line 1)
9069 (skip-chars-forward " \t")
9070 (setq params (plist-put params :indentation-column (current-column))))
9071 (unless (re-search-forward org-dblock-end-re nil t)
9072 (error "Dynamic block not terminated"))
9075 (list :content (buffer-substring
9076 begdel (match-beginning 0)))))
9077 (delete-region begdel (match-beginning 0))
9082 (defun org-map-dblocks (&optional command)
9083 "Apply COMMAND to all dynamic blocks in the current buffer.
9084 If COMMAND is not given, use `org-update-dblock'."
9085 (let ((cmd (or command 'org-update-dblock))
9088 (goto-char (point-min))
9089 (while (re-search-forward org-dblock-start-re nil t)
9090 (goto-char (setq pos (match-beginning 0)))
9093 (error (message "Error during update of dynamic block")))
9095 (unless (re-search-forward org-dblock-end-re nil t)
9096 (error "Dynamic block not terminated"))))))
9098 (defun org-dblock-update (&optional arg)
9099 "User command for updating dynamic blocks.
9100 Update the dynamic block at point. With prefix ARG, update all dynamic
9101 blocks in the buffer."
9104 (org-update-all-dblocks)
9105 (or (looking-at org-dblock-start-re)
9106 (org-beginning-of-dblock))
9107 (org-update-dblock)))
9109 (defun org-update-dblock ()
9110 "Update the dynamic block at point
9111 This means to empty the block, parse for parameters and then call
9112 the correct writing function."
9113 (save-window-excursion
9114 (let* ((pos (point))
9115 (line (org-current-line))
9116 (params (org-prepare-dblock))
9117 (name (plist-get params :name))
9118 (indent (plist-get params :indentation-column))
9119 (cmd (intern (concat "org-dblock-write:" name))))
9120 (message "Updating dynamic block `%s' at line %d..." name line)
9121 (funcall cmd params)
9122 (message "Updating dynamic block `%s' at line %d...done" name line)
9124 (when (and indent (> indent 0))
9125 (setq indent (make-string indent ?\ ))
9127 (org-beginning-of-dblock)
9129 (while (not (looking-at org-dblock-end-re))
9131 (beginning-of-line 2))
9132 (when (looking-at org-dblock-end-re)
9133 (and (looking-at "[ \t]+")
9135 (insert indent)))))))
9137 (defun org-beginning-of-dblock ()
9138 "Find the beginning of the dynamic block at point.
9139 Error if there is no such block at point."
9143 (if (and (re-search-backward org-dblock-start-re nil t)
9144 (setq beg (match-beginning 0))
9145 (re-search-forward org-dblock-end-re nil t)
9146 (> (match-end 0) pos))
9149 (error "Not in a dynamic block"))))
9151 (defun org-update-all-dblocks ()
9152 "Update all dynamic blocks in the buffer.
9153 This function can be used in a hook."
9155 (org-map-dblocks 'org-update-dblock)))
9160 (defconst org-additional-option-like-keywords
9161 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
9162 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
9163 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "LATEX_CLASS:" "ATTR_LaTeX"
9165 "ORGTBL" "TBLFM:" "TBLNAME:"
9166 "BEGIN_EXAMPLE" "END_EXAMPLE"
9167 "BEGIN_QUOTE" "END_QUOTE"
9168 "BEGIN_VERSE" "END_VERSE"
9169 "BEGIN_CENTER" "END_CENTER"
9170 "BEGIN_SRC" "END_SRC"
9171 "CATEGORY" "COLUMNS"
9176 (defcustom org-structure-template-alist
9178 ("s" "#+begin_src ?\n\n#+end_src"
9179 "<src lang=\"?\">\n\n</src>")
9180 ("e" "#+begin_example\n?\n#+end_example"
9181 "<example>\n?\n</example>")
9182 ("q" "#+begin_quote\n?\n#+end_quote"
9183 "<quote>\n?\n</quote>")
9184 ("v" "#+begin_verse\n?\n#+end_verse"
9185 "<verse>\n?\n/verse>")
9186 ("c" "#+begin_center\n?\n#+end_center"
9187 "<center>\n?\n/center>")
9188 ("l" "#+begin_latex\n?\n#+end_latex"
9189 "<literal style=\"latex\">\n?\n</literal>")
9191 "<literal style=\"latex\">?</literal>")
9192 ("h" "#+begin_html\n?\n#+end_html"
9193 "<literal style=\"html\">\n?\n</literal>")
9195 "<literal style=\"html\">?</literal>")
9196 ("a" "#+begin_ascii\n?\n#+end_ascii")
9198 ("i" "#+include %file ?"
9199 "<include file=%file markup=\"?\">")
9201 "Structure completion elements.
9202 This is a list of abbreviation keys and values. The value gets inserted
9203 it you type @samp{.} followed by the key and then the completion key,
9204 usually `M-TAB'. %file will be replaced by a file name after prompting
9205 for the file using completion.
9206 There are two templates for each key, the first uses the original Org syntax,
9207 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
9208 the default when the /org-mtags.el/ module has been loaded. See also the
9209 variable `org-mtags-prefer-muse-templates'.
9210 This is an experimental feature, it is undecided if it is going to stay in."
9211 :group 'org-completion
9214 (string :tag "Template")
9215 (string :tag "Muse Template")))
9217 (defun org-try-structure-completion ()
9218 "Try to complete a structure template before point.
9219 This looks for strings like \"<e\" on an otherwise empty line and
9221 (let ((l (buffer-substring (point-at-bol) (point)))
9223 (when (and (looking-at "[ \t]*$")
9224 (string-match "^[ \t]*<\\([a-z]+\\)$"l)
9225 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
9226 (org-complete-expand-structure-template (+ -1 (point-at-bol)
9227 (match-beginning 1)) a)
9230 (defun org-complete-expand-structure-template (start cell)
9231 "Expand a structure template."
9232 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
9233 (rpl (nth (if musep 2 1) cell))
9235 (delete-region start (point))
9236 (when (string-match "\\`#\\+" rpl)
9239 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
9240 (setq ind (buffer-substring (point-at-bol) (point))))
9242 (setq start (point))
9243 (if (string-match "%file" rpl)
9244 (setq rpl (replace-match
9248 (abbreviate-file-name (read-file-name "Include file: ")))
9251 (setq rpl (mapconcat 'identity (split-string rpl "\n")
9254 (if (re-search-backward "\\?" start t) (delete-char 1))))
9257 (defun org-complete (&optional arg)
9258 "Perform completion on word at point.
9259 At the beginning of a headline, this completes TODO keywords as given in
9260 `org-todo-keywords'.
9261 If the current word is preceded by a backslash, completes the TeX symbols
9262 that are supported for HTML support.
9263 If the current word is preceded by \"#+\", completes special words for
9264 setting file options.
9265 In the line after \"#+STARTUP:, complete valid keywords.\"
9266 At all other locations, this simply calls the value of
9267 `org-completion-fallback-command'."
9269 (org-without-partial-completion
9273 (beg1 (save-excursion
9274 (skip-chars-backward (org-re "[:alnum:]_@"))
9276 (beg (save-excursion
9277 (skip-chars-backward "a-zA-Z0-9_:$")
9279 (confirm (lambda (x) (stringp (car x))))
9280 (searchhead (equal (char-before beg) ?*))
9282 (when (and (member (char-before beg1) '(?. ?<))
9283 (setq a (assoc (buffer-substring beg1 (point))
9284 org-structure-template-alist)))
9285 (org-complete-expand-structure-template (1- beg1) a)
9287 (tag (and (equal (char-before beg1) ?:)
9288 (equal (char-after (point-at-bol)) ?*)))
9289 (prop (and (equal (char-before beg1) ?:)
9290 (not (equal (char-after (point-at-bol)) ?*))))
9291 (texp (equal (char-before beg) ?\\))
9292 (link (equal (char-before beg) ?\[))
9293 (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
9296 (startup (string-match "^#\\+STARTUP:.*"
9297 (buffer-substring (point-at-bol) (point))))
9298 (completion-ignore-case opt)
9310 "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
9311 (cons (match-string 2 x)
9312 (match-string 1 x))))
9313 (org-split-string (org-get-current-options) "\n")))
9314 (mapcar 'list org-additional-option-like-keywords)))
9316 (setq type :startup)
9317 org-startup-options)
9318 (link (append org-link-abbrev-alist-local
9319 org-link-abbrev-alist))
9323 ((string-match "\\`\\*+[ \t]+\\'"
9324 (buffer-substring (point-at-bol) beg))
9326 (mapcar 'list org-todo-keywords-1))
9328 (setq type :searchhead)
9330 (goto-char (point-min))
9331 (while (re-search-forward org-todo-line-regexp nil t)
9333 (org-make-org-heading-search-string
9334 (match-string 3) t))
9337 (tag (setq type :tag beg beg1)
9338 (or org-tag-alist (org-get-buffer-tags)))
9339 (prop (setq type :prop beg beg1)
9340 (mapcar 'list (org-buffer-property-keys nil t t)))
9342 (call-interactively org-completion-fallback-command)
9343 (throw 'exit nil)))))
9344 (pattern (buffer-substring-no-properties beg end))
9345 (completion (try-completion pattern table confirm)))
9346 (cond ((eq completion t)
9347 (if (not (assoc (upcase pattern) table))
9348 (message "Already complete")
9349 (if (and (equal type :opt)
9350 (not (member (car (assoc (upcase pattern) table))
9351 org-additional-option-like-keywords)))
9352 (insert (substring (cdr (assoc (upcase pattern) table))
9354 (if (memq type '(:tag :prop)) (insert ":")))))
9356 (message "Can't find completion for \"%s\"" pattern)
9358 ((not (string= pattern completion))
9359 (delete-region beg end)
9360 (if (string-match " +$" completion)
9361 (setq completion (replace-match "" t t completion)))
9363 (if (get-buffer-window "*Completions*")
9364 (delete-window (get-buffer-window "*Completions*")))
9365 (if (assoc completion table)
9366 (if (eq type :todo) (insert " ")
9367 (if (memq type '(:tag :prop)) (insert ":"))))
9368 (if (and (equal type :opt) (assoc completion table))
9369 (message "%s" (substitute-command-keys
9370 "Press \\[org-complete] again to insert example settings"))))
9372 (message "Making completion list...")
9373 (let ((list (sort (all-completions pattern table confirm)
9375 (with-output-to-temp-buffer "*Completions*"
9377 ;; Protection needed for XEmacs and emacs 21
9378 (display-completion-list list pattern)
9379 (error (display-completion-list list)))))
9380 (message "Making completion list...%s" "done")))))))
9382 ;;;; TODO, DEADLINE, Comments
9384 (defun org-toggle-comment ()
9385 "Change the COMMENT state of an entry."
9388 (org-back-to-heading)
9389 (let (case-fold-search)
9390 (if (looking-at (concat outline-regexp
9391 "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
9392 (replace-match "" t t nil 1)
9393 (if (looking-at outline-regexp)
9395 (goto-char (match-end 0))
9396 (insert org-comment-string " ")))))))
9398 (defvar org-last-todo-state-is-todo nil
9399 "This is non-nil when the last TODO state change led to a TODO state.
9400 If the last change removed the TODO tag or switched to DONE, then
9403 (defvar org-setting-tags nil) ; dynamically skipped
9405 (defun org-parse-local-options (string var)
9406 "Parse STRING for startup setting relevant for variable VAR."
9407 (let ((rtn (symbol-value var))
9410 (if (or (not string) (not (string-match "\\S-" string)))
9412 (setq opts (delq nil (mapcar (lambda (x)
9413 (setq e (assoc x org-startup-options))
9414 (if (eq (nth 1 e) var) e nil))
9415 (org-split-string string "[ \t]+"))))
9419 (while (setq e (pop opts))
9421 (setq rtn (nth 2 e))
9422 (if (not (listp rtn)) (setq rtn nil))
9423 (push (nth 2 e) rtn)))
9426 (defvar org-todo-setup-filter-hook nil
9427 "Hook for functions that pre-filter todo specs.
9429 Each function takes a todo spec and returns either `nil' or the spec
9430 transformed into canonical form." )
9432 (defvar org-todo-get-default-hook nil
9433 "Hook for functions that get a default item for todo.
9435 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
9436 `nil' or a string to be used for the todo mark." )
9438 (defvar org-agenda-headline-snapshot-before-repeat)
9440 (defun org-todo (&optional arg)
9441 "Change the TODO state of an item.
9442 The state of an item is given by a keyword at the start of the heading,
9444 *** TODO Write paper
9447 The different keywords are specified in the variable `org-todo-keywords'.
9448 By default the available states are \"TODO\" and \"DONE\".
9449 So for this example: when the item starts with TODO, it is changed to DONE.
9450 When it starts with DONE, the DONE is removed. And when neither TODO nor
9451 DONE are present, add TODO at the beginning of the heading.
9453 With C-u prefix arg, use completion to determine the new state.
9454 With numeric prefix arg, switch to that state.
9455 With a double C-u prefix, switch to the next set of TODO keywords (nextset).
9456 With a tripple C-u prefix, circumvent any state blocking.
9458 For calling through lisp, arg is also interpreted in the following way:
9459 'none -> empty state
9460 \"\"(empty string) -> switch to empty state
9461 'done -> switch to DONE
9462 'nextset -> switch to the next set of keywords
9463 'previousset -> switch to the previous set of keywords
9464 \"WAITING\" -> switch to the specified keyword, but only if it
9465 really is a member of `org-todo-keywords'."
9467 (if (equal arg '(16)) (setq arg 'nextset))
9468 (let ((org-blocker-hook org-blocker-hook)
9469 (case-fold-search nil))
9470 (when (equal arg '(64))
9471 (setq arg nil org-blocker-hook nil))
9472 (when (and org-blocker-hook
9473 (or org-inhibit-blocking
9474 (org-entry-get nil "NOBLOCKING")))
9475 (setq org-blocker-hook nil))
9478 (org-back-to-heading)
9479 (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
9480 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
9482 (let* ((match-data (match-data))
9483 (startpos (point-at-bol))
9484 (logging (save-match-data (org-entry-get nil "LOGGING" t)))
9485 (org-log-done org-log-done)
9486 (org-log-repeat org-log-repeat)
9487 (org-todo-log-states org-todo-log-states)
9488 (this (match-string 1))
9489 (hl-pos (match-beginning 0))
9490 (head (org-get-todo-sequence-head this))
9491 (ass (assoc head org-todo-kwd-alist))
9492 (interpret (nth 1 ass))
9493 (done-word (nth 3 ass))
9494 (final-done-word (nth 4 ass))
9495 (last-state (or this ""))
9496 (completion-ignore-case t)
9497 (member (member this org-todo-keywords-1))
9500 ((and org-todo-key-trigger
9501 (or (and (equal arg '(4))
9502 (eq org-use-fast-todo-selection 'prefix))
9503 (and (not arg) org-use-fast-todo-selection
9504 (not (eq org-use-fast-todo-selection
9506 ;; Use fast selection
9507 (org-fast-todo-selection))
9508 ((and (equal arg '(4))
9509 (or (not org-use-fast-todo-selection)
9510 (not org-todo-key-trigger)))
9511 ;; Read a state with completion
9512 (org-icompleting-read
9513 "State: " (mapcar (lambda(x) (list x))
9514 org-todo-keywords-1)
9518 (if tail (car tail) nil)
9519 (car org-todo-keywords-1)))
9521 (if (equal member org-todo-keywords-1)
9524 (nth (- (length org-todo-keywords-1)
9526 org-todo-keywords-1)
9527 (org-last org-todo-keywords-1))))
9528 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
9529 (setq arg nil))) ; hack to fall back to cycling
9531 ;; user or caller requests a specific state
9533 ((equal arg "") nil)
9534 ((eq arg 'none) nil)
9535 ((eq arg 'done) (or done-word (car org-done-keywords)))
9537 (or (car (cdr (member head org-todo-heads)))
9538 (car org-todo-heads)))
9539 ((eq arg 'previousset)
9540 (let ((org-todo-heads (reverse org-todo-heads)))
9541 (or (car (cdr (member head org-todo-heads)))
9542 (car org-todo-heads))))
9543 ((car (member arg org-todo-keywords-1)))
9545 (error "State `%s' not valid in this file" arg))
9546 ((nth (1- (prefix-numeric-value arg))
9547 org-todo-keywords-1))))
9548 ((null member) (or head (car org-todo-keywords-1)))
9549 ((equal this final-done-word) nil) ;; -> make empty
9550 ((null tail) nil) ;; -> first entry
9551 ((memq interpret '(type priority))
9552 (if (eq this-command last-command)
9554 (if (> (length tail) 0)
9555 (or done-word (car org-done-keywords))
9560 (run-hook-with-args-until-success
9561 'org-todo-get-default-hook state last-state)
9563 (next (if state (concat " " state " ") " "))
9564 (change-plist (list :type 'todo-state-change :from this :to state
9565 :position startpos))
9567 (when org-blocker-hook
9568 (setq org-last-todo-state-is-todo
9569 (not (member this org-done-keywords)))
9570 (unless (save-excursion
9572 (run-hook-with-args-until-failure
9573 'org-blocker-hook change-plist)))
9575 (error "TODO state change from %s to %s blocked" this state)
9577 (message "TODO state change from %s to %s blocked" this state)
9578 (throw 'exit nil))))
9579 (store-match-data match-data)
9580 (replace-match next t t)
9581 (unless (pos-visible-in-window-p hl-pos)
9582 (message "TODO state changed to %s" (org-trim next)))
9584 (setq head (org-get-todo-sequence-head state)
9585 ass (assoc head org-todo-kwd-alist)
9586 interpret (nth 1 ass)
9587 done-word (nth 3 ass)
9588 final-done-word (nth 4 ass)))
9589 (when (memq arg '(nextset previousset))
9590 (message "Keyword-Set %d/%d: %s"
9591 (- (length org-todo-sets) -1
9592 (length (memq (assoc state org-todo-sets) org-todo-sets)))
9593 (length org-todo-sets)
9594 (mapconcat 'identity (assoc state org-todo-sets) " ")))
9595 (setq org-last-todo-state-is-todo
9596 (not (member state org-done-keywords)))
9597 (setq now-done-p (and (member state org-done-keywords)
9598 (not (member this org-done-keywords))))
9599 (and logging (org-local-logging logging))
9600 (when (and (or org-todo-log-states org-log-done)
9601 (not (eq org-inhibit-logging t))
9602 (not (memq arg '(nextset previousset))))
9603 ;; we need to look at recording a time and note
9604 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
9605 (nth 2 (assoc this org-todo-log-states))))
9606 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
9609 (member state org-not-done-keywords)
9610 (not (member this org-not-done-keywords)))
9611 ;; This is now a todo state and was not one before
9612 ;; If there was a CLOSED time stamp, get rid of it.
9613 (org-add-planning-info nil nil 'closed))
9614 (when (and now-done-p org-log-done)
9615 ;; It is now done, and it was not done before
9616 (org-add-planning-info 'closed (org-current-time))
9617 (if (and (not dolog) (eq 'note org-log-done))
9618 (org-add-log-setup 'done state this 'findpos 'note)))
9619 (when (and state dolog)
9620 ;; This is a non-nil state, and we need to log it
9621 (org-add-log-setup 'state state this 'findpos dolog)))
9622 ;; Fixup tag positioning
9623 (org-todo-trigger-tag-changes state)
9624 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
9625 (when org-provide-todo-statistics
9626 (org-update-parent-todo-statistics))
9627 (run-hooks 'org-after-todo-state-change-hook)
9628 (if (and arg (not (member state org-done-keywords)))
9629 (setq head (org-get-todo-sequence-head state)))
9630 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
9631 ;; Do we need to trigger a repeat?
9633 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
9634 ;; This is for the agenda, take a snapshot of the headline.
9636 (setq org-agenda-headline-snapshot-before-repeat
9637 (org-get-heading))))
9638 (org-auto-repeat-maybe state))
9639 ;; Fixup cursor location if close to the keyword
9640 (if (and (outline-on-heading-p)
9642 (save-excursion (beginning-of-line 1)
9643 (looking-at org-todo-line-regexp))
9644 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
9646 (goto-char (or (match-end 2) (match-end 1)))
9647 (and (looking-at " ") (just-one-space))))
9648 (when org-trigger-hook
9650 (run-hook-with-args 'org-trigger-hook change-plist))))))))
9652 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
9653 "Block turning an entry into a TODO, using the hierarchy.
9654 This checks whether the current task should be blocked from state
9655 changes. Such blocking occurs when:
9657 1. The task has children which are not all in a completed state.
9659 2. A task has a parent with the property :ORDERED:, and there
9660 are siblings prior to the current task with incomplete
9663 3. The parent of the task is blocked because it has siblings that should
9664 be done first, or is child of a block grandparent TODO entry."
9667 ;; If this is not a todo state change, or if this entry is already DONE,
9669 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9670 (member (plist-get change-plist :from)
9671 (cons 'done org-done-keywords))
9672 (member (plist-get change-plist :to)
9673 (cons 'todo org-not-done-keywords)))
9674 (throw 'dont-block t))
9675 ;; If this task has children, and any are undone, it's blocked
9677 (org-back-to-heading t)
9678 (let ((this-level (funcall outline-level)))
9679 (outline-next-heading)
9680 (let ((child-level (funcall outline-level)))
9681 (while (and (not (eobp))
9682 (> child-level this-level))
9683 ;; this todo has children, check whether they are all
9685 (if (and (not (org-entry-is-done-p))
9686 (org-entry-is-todo-p))
9687 (throw 'dont-block nil))
9688 (outline-next-heading)
9689 (setq child-level (funcall outline-level))))))
9690 ;; Otherwise, if the task's parent has the :ORDERED: property, and
9691 ;; any previous siblings are undone, it's blocked
9693 (org-back-to-heading t)
9694 (let* ((pos (point))
9695 (parent-pos (and (org-up-heading-safe) (point))))
9696 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9697 (when (and (org-entry-get (point) "ORDERED")
9699 (re-search-forward org-not-done-heading-regexp pos t))
9700 (throw 'dont-block nil)) ; block, there is an older sibling not done.
9701 ;; Search further up the hierarchy, to see if an anchestor is blocked
9703 (goto-char parent-pos)
9704 (if (not (looking-at org-not-done-heading-regexp))
9705 (throw 'dont-block t)) ; do not block, parent is not a TODO
9707 (setq parent-pos (and (org-up-heading-safe) (point)))
9708 (if (not parent-pos) (throw 'dont-block t)) ; no parent
9709 (when (and (org-entry-get (point) "ORDERED")
9711 (re-search-forward org-not-done-heading-regexp pos t))
9712 (throw 'dont-block nil))))))) ; block, older sibling not done.
9714 (defcustom org-track-ordered-property-with-tag nil
9715 "Should the ORDERED property also be shown as a tag?
9716 The ORDERED property decides if an entry should require subtasks to be
9717 completed in sequence. Since a property is not very visible, setting
9718 this option means that toggling the ORDERED property with the command
9719 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
9720 not relevant for the behavior, but it makes things more visible.
9722 Note that toggling the tag with tags commands will not change the property
9723 and therefore not influence behavior!
9725 This can be t, meaning the tag ORDERED should be used, It can also be a
9726 string to select a different tag for this task."
9729 (const :tag "No tracking" nil)
9730 (const :tag "Track with ORDERED tag" t)
9731 (string :tag "Use other tag")))
9733 (defun org-toggle-ordered-property ()
9734 "Toggle the ORDERED property of the current entry.
9735 For better visibility, you can track the value of this property with a tag.
9736 See variable `org-track-ordered-property-with-tag'."
9738 (let* ((t1 org-track-ordered-property-with-tag)
9739 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
9741 (org-back-to-heading)
9742 (if (org-entry-get nil "ORDERED")
9744 (org-delete-property "ORDERED")
9745 (and tag (org-toggle-tag tag 'off))
9746 (message "Subtasks can be completed in arbitrary order"))
9747 (org-entry-put nil "ORDERED" "t")
9748 (and tag (org-toggle-tag tag 'on))
9749 (message "Subtasks must be completed in sequence")))))
9751 (defvar org-blocked-by-checkboxes) ; dynamically scoped
9752 (defun org-block-todo-from-checkboxes (change-plist)
9753 "Block turning an entry into a TODO, using checkboxes.
9754 This checks whether the current task should be blocked from state
9755 changes because there are uncheckd boxes in this entry."
9757 ;; If this is not a todo state change, or if this entry is already DONE,
9759 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
9760 (member (plist-get change-plist :from)
9761 (cons 'done org-done-keywords))
9762 (member (plist-get change-plist :to)
9763 (cons 'todo org-not-done-keywords)))
9764 (throw 'dont-block t))
9765 ;; If this task has checkboxes that are not checked, it's blocked
9767 (org-back-to-heading t)
9768 (let ((beg (point)) end)
9769 (outline-next-heading)
9772 (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
9775 (if (boundp 'org-blocked-by-checkboxes)
9776 (setq org-blocked-by-checkboxes t))
9777 (throw 'dont-block nil)))))
9780 (defun org-update-statistics-cookies (all)
9781 "Update the statistics cookie, either from TODO or from checkboxes.
9782 This should be called with the cursor in a line with a statistics cookie."
9786 (org-update-checkbox-count 'all)
9787 (org-map-entries 'org-update-parent-todo-statistics))
9788 (if (not (org-on-heading-p))
9789 (org-update-checkbox-count)
9790 (let ((pos (move-marker (make-marker) (point)))
9792 (ignore-errors (org-back-to-heading t))
9793 (if (not (org-on-heading-p))
9794 (org-update-checkbox-count)
9795 (setq l1 (org-outline-level))
9796 (setq end (save-excursion
9797 (outline-next-heading)
9798 (if (org-on-heading-p) (setq l2 (org-outline-level)))
9800 (if (and (save-excursion (re-search-forward
9801 "^[ \t]*[-+*] \\[[- X]\\]" end t))
9802 (not (save-excursion (re-search-forward
9803 ":COOKIE_DATA:.*\\<todo\\>" end t))))
9804 (org-update-checkbox-count)
9805 (if (and l2 (> l2 l1))
9808 (org-update-parent-todo-statistics))
9809 (error "No data for statistics cookie"))))
9811 (move-marker pos nil)))))
9813 (defvar org-entry-property-inherited-from) ;; defined below
9814 (defun org-update-parent-todo-statistics ()
9815 "Update any statistics cookie in the parent of the current headline.
9816 When `org-hierarchical-todo-statistics' is nil, statistics will cover
9817 the entire subtree and this will travel up the hierarchy and update
9818 statistics everywhere."
9821 (recursive (or (not org-hierarchical-todo-statistics)
9824 (or (setq prop (org-entry-get
9825 nil "COOKIE_DATA" 'inherit)) ""))))
9826 (lim (or (and prop (marker-position
9827 org-entry-property-inherited-from))
9830 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
9831 level ltoggle l1 new ndel
9832 (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
9835 (beginning-of-line 1)
9836 (if (org-at-heading-p)
9837 (setq ltoggle (funcall outline-level))
9838 (error "This should not happen"))
9839 (while (and (setq level (org-up-heading-safe))
9840 (or recursive first)
9851 (while (re-search-forward box-re (point-at-eol) t)
9852 (setq cnt-all 0 cnt-done 0 cookie-present t)
9853 (setq is-percent (match-end 2))
9855 (unless (outline-next-heading) (throw 'exit nil))
9856 (while (and (looking-at org-complex-heading-regexp)
9857 (> (setq l1 (length (match-string 1))) level))
9858 (setq kwd (and (or recursive (= l1 ltoggle))
9860 (if (or (eq org-provide-todo-statistics 'all-headlines)
9861 (and (listp org-provide-todo-statistics)
9862 (or (member kwd org-provide-todo-statistics)
9863 (member kwd org-done-keywords))))
9864 (setq cnt-all (1+ cnt-all))
9865 (if (eq org-provide-todo-statistics t)
9866 (and kwd (setq cnt-all (1+ cnt-all)))))
9867 (and (member kwd org-done-keywords)
9868 (setq cnt-done (1+ cnt-done)))
9869 (outline-next-heading)))
9872 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
9873 (format "[%d/%d]" cnt-done cnt-all))
9874 ndel (- (match-end 0) (match-beginning 0)))
9875 (goto-char (match-beginning 0))
9877 (delete-region (point) (+ (point) ndel))))
9878 (when cookie-present
9879 (run-hook-with-args 'org-after-todo-statistics-hook
9880 cnt-done (- cnt-all cnt-done)))))
9881 (run-hooks 'org-todo-statistics-hook)))
9883 (defvar org-after-todo-statistics-hook nil
9884 "Hook that is called after a TODO statistics cookie has been updated.
9885 Each function is called with two arguments: the number of not-done entries
9886 and the number of done entries.
9888 For example, the following function, when added to this hook, will switch
9889 an entry to DONE when all children are done, and back to TODO when new
9890 entries are set to a TODO status. Note that this hook is only called
9891 when there is a statistics cookie in the headline!
9893 (defun org-summary-todo (n-done n-not-done)
9894 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
9895 (let (org-log-done org-log-states) ; turn off logging
9896 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
9899 (defvar org-todo-statistics-hook nil
9900 "Hook that is run whenever Org thinks TODO statistics should be updated.
9901 This hook runs even if there is no statisics cookie present, in which case
9902 `org-after-todo-statistics-hook' would not run.")
9904 (defun org-todo-trigger-tag-changes (state)
9905 "Apply the changes defined in `org-todo-state-tags-triggers'."
9906 (let ((l org-todo-state-tags-triggers)
9908 (when (or (not state) (equal state ""))
9909 (setq changes (append changes (cdr (assoc "" l)))))
9910 (when (and (stringp state) (> (length state) 0))
9911 (setq changes (append changes (cdr (assoc state l)))))
9912 (when (member state org-not-done-keywords)
9913 (setq changes (append changes (cdr (assoc 'todo l)))))
9914 (when (member state org-done-keywords)
9915 (setq changes (append changes (cdr (assoc 'done l)))))
9917 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
9919 (defun org-local-logging (value)
9920 "Get logging settings from a property VALUE."
9922 ;; directly set the variables, they are already local.
9923 (setq org-log-done nil
9925 org-todo-log-states nil)
9926 (setq words (org-split-string value))
9927 (while (setq w (pop words))
9929 ((setq a (assoc w org-startup-options))
9930 (and (member (nth 1 a) '(org-log-done org-log-repeat))
9931 (set (nth 1 a) (nth 2 a))))
9932 ((setq a (org-extract-log-state-settings w))
9933 (and (member (car a) org-todo-keywords-1)
9934 (push a org-todo-log-states)))))))
9936 (defun org-get-todo-sequence-head (kwd)
9937 "Return the head of the TODO sequence to which KWD belongs.
9938 If KWD is not set, check if there is a text property remembering the
9943 (or (get-text-property (point-at-bol) 'org-todo-head)
9945 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
9946 nil (point-at-eol)))
9947 (get-text-property p 'org-todo-head))))
9948 ((not (member kwd org-todo-keywords-1))
9949 (car org-todo-keywords-1))
9950 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
9952 (defun org-fast-todo-selection ()
9953 "Fast TODO keyword selection with single keys.
9954 Returns the new TODO keyword, or nil if no state change should occur."
9955 (let* ((fulltable org-todo-key-alist)
9956 (done-keywords org-done-keywords) ;; needed for the faces.
9957 (maxlen (apply 'max (mapcar
9959 (if (stringp (car x)) (string-width (car x)) 0))
9962 (fwidth (+ maxlen 3 1 3))
9963 (ncol (/ (- (window-width) 4) fwidth))
9967 (save-window-excursion
9969 (set-buffer (get-buffer-create " *Org todo*"))
9970 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
9972 (org-set-local 'org-done-keywords done-keywords)
9973 (setq tbl fulltable cnt 0)
9974 (while (setq e (pop tbl))
9976 ((equal e '(:startgroup))
9977 (push '() groups) (setq ingroup t)
9978 (when (not (= cnt 0))
9982 ((equal e '(:endgroup))
9983 (setq ingroup nil cnt 0)
9985 ((equal e '(:newline))
9986 (when (not (= cnt 0))
9990 (while (equal (car tbl) '(:newline))
9992 (setq tbl (cdr tbl)))))
9994 (setq tg (car e) c (cdr e))
9995 (if ingroup (push tg (car groups)))
9996 (setq tg (org-add-props tg nil 'face
9997 (org-get-todo-face tg)))
9998 (if (and (= cnt 0) (not ingroup)) (insert " "))
9999 (insert "[" c "] " tg (make-string
10000 (- fwidth 4 (length tg)) ?\ ))
10001 (when (= (setq cnt (1+ cnt)) ncol)
10003 (if ingroup (insert " "))
10006 (goto-char (point-min))
10007 (if (not expert) (org-fit-window-to-buffer))
10008 (message "[a-z..]:Set [SPC]:clear")
10009 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
10012 (and (= c ?q) (not (rassoc c fulltable))))
10013 (setq quit-flag t))
10015 ((setq e (rassoc c fulltable) tg (car e))
10017 (t (setq quit-flag t)))))))
10019 (defun org-entry-is-todo-p ()
10020 (member (org-get-todo-state) org-not-done-keywords))
10022 (defun org-entry-is-done-p ()
10023 (member (org-get-todo-state) org-done-keywords))
10025 (defun org-get-todo-state ()
10027 (org-back-to-heading t)
10028 (and (looking-at org-todo-line-regexp)
10030 (match-string 2))))
10032 (defun org-at-date-range-p (&optional inactive-ok)
10033 "Is the cursor inside a date range?"
10037 (let ((pos (point)))
10038 (skip-chars-backward "^[<\r\n")
10039 (skip-chars-backward "<[")
10040 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10041 (>= (match-end 0) pos)
10043 (skip-chars-backward "^<[\r\n")
10044 (skip-chars-backward "<[")
10045 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
10046 (>= (match-end 0) pos)
10050 (defun org-get-repeat ()
10051 "Check if there is a deadline/schedule with repeater in this entry."
10054 (org-back-to-heading t)
10055 (if (re-search-forward
10056 org-repeat-re (save-excursion (outline-next-heading) (point)) t)
10057 (match-string 1)))))
10059 (defvar org-last-changed-timestamp)
10060 (defvar org-last-inserted-timestamp)
10061 (defvar org-log-post-message)
10062 (defvar org-log-note-purpose)
10063 (defvar org-log-note-how)
10064 (defvar org-log-note-extra)
10065 (defun org-auto-repeat-maybe (done-word)
10066 "Check if the current headline contains a repeated deadline/schedule.
10067 If yes, set TODO state back to what it was and change the base date
10068 of repeating deadline/scheduled time stamps to new date.
10069 This function is run automatically after each state change to a DONE state."
10070 ;; last-state is dynamically scoped into this function
10071 (let* ((repeat (org-get-repeat))
10072 (aa (assoc last-state org-todo-kwd-alist))
10073 (interpret (nth 1 aa))
10075 (whata '(("d" . day) ("m" . month) ("y" . year)))
10076 (msg "Entry repeats: ")
10078 (org-todo-log-states nil)
10079 (nshiftmax 10) (nshift 0)
10080 re type n what ts time)
10082 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
10083 (org-todo (if (eq interpret 'type) last-state head))
10084 (org-entry-put nil "LAST_REPEAT" (format-time-string
10085 (org-time-stamp-format t t)))
10086 (when org-log-repeat
10087 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
10088 (memq 'org-add-log-note post-command-hook))
10089 ;; OK, we are already setup for some record
10090 (if (eq org-log-repeat 'note)
10091 ;; make sure we take a note, not only a time stamp
10092 (setq org-log-note-how 'note))
10093 ;; Set up for taking a record
10094 (org-add-log-setup 'state (or done-word (car org-done-keywords))
10096 'findpos org-log-repeat)))
10097 (org-back-to-heading t)
10098 (org-add-planning-info nil nil 'closed)
10099 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
10100 org-deadline-time-regexp "\\)\\|\\("
10101 org-ts-regexp "\\)"))
10102 (while (re-search-forward
10103 re (save-excursion (outline-next-heading) (point)) t)
10104 (setq type (if (match-end 1) org-scheduled-string
10105 (if (match-end 3) org-deadline-string "Plain:"))
10106 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
10107 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
10108 (setq n (string-to-number (match-string 2 ts))
10109 what (match-string 3 ts))
10110 (if (equal what "w") (setq n (* n 7) what "d"))
10111 ;; Preparation, see if we need to modify the start date for the change
10112 (when (match-end 1)
10113 (setq time (save-match-data (org-time-string-to-time ts)))
10115 ((equal (match-string 1 ts) ".")
10116 ;; Shift starting date to today
10117 (org-timestamp-change
10118 (- (time-to-days (current-time)) (time-to-days time))
10120 ((equal (match-string 1 ts) "+")
10121 (while (or (= nshift 0)
10122 (<= (time-to-days time) (time-to-days (current-time))))
10123 (when (= (incf nshift) nshiftmax)
10124 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
10126 (org-timestamp-change n (cdr (assoc what whata)))
10127 (org-at-timestamp-p t)
10128 (setq ts (match-string 1))
10129 (setq time (save-match-data (org-time-string-to-time ts))))
10130 (org-timestamp-change (- n) (cdr (assoc what whata)))
10131 ;; rematch, so that we have everything in place for the real shift
10132 (org-at-timestamp-p t)
10133 (setq ts (match-string 1))
10134 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
10135 (org-timestamp-change n (cdr (assoc what whata)))
10136 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
10137 (setq org-log-post-message msg)
10138 (message "%s" msg))))
10140 (defun org-show-todo-tree (arg)
10141 "Make a compact tree which shows all headlines marked with TODO.
10142 The tree will show the lines where the regexp matches, and all higher
10143 headlines above the match.
10144 With a \\[universal-argument] prefix, prompt for a regexp to match.
10145 With a numeric prefix N, construct a sparse tree for the Nth element
10146 of `org-todo-keywords-1'."
10148 (let ((case-fold-search nil)
10150 (cond ((null arg) org-not-done-regexp)
10152 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
10153 (mapcar 'list org-todo-keywords-1))))
10155 (mapconcat 'identity (org-split-string kwd "|") "\\|")
10157 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
10158 (regexp-quote (nth (1- (prefix-numeric-value arg))
10159 org-todo-keywords-1)))
10160 (t (error "Invalid prefix argument: %s" arg)))))
10161 (message "%d TODO entries found"
10162 (org-occur (concat "^" outline-regexp " *" kwd-re )))))
10164 (defun org-deadline (&optional remove time)
10165 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
10166 With argument REMOVE, remove any deadline from the item.
10167 When TIME is set, it should be an internal time specification, and the
10168 scheduling will use the corresponding date."
10172 (org-remove-timestamp-with-keyword org-deadline-string)
10173 (message "Item no longer has a deadline."))
10174 (if (org-get-repeat)
10175 (error "Cannot change deadline on task with repeater, please do that by hand")
10176 (org-add-planning-info 'deadline time 'closed)
10177 (message "Deadline on %s" org-last-inserted-timestamp))))
10179 (defun org-schedule (&optional remove time)
10180 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
10181 With argument REMOVE, remove any scheduling date from the item.
10182 When TIME is set, it should be an internal time specification, and the
10183 scheduling will use the corresponding date."
10187 (org-remove-timestamp-with-keyword org-scheduled-string)
10188 (message "Item is no longer scheduled."))
10189 (if (org-get-repeat)
10190 (error "Cannot reschedule task with repeater, please do that by hand")
10191 (org-add-planning-info 'scheduled time 'closed)
10192 (message "Scheduled to %s" org-last-inserted-timestamp))))
10194 (defun org-get-scheduled-time (pom &optional inherit)
10195 "Get the scheduled time as a time tuple, of a format suitable
10196 for calling org-schedule with, or if there is no scheduling,
10198 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
10200 (apply 'encode-time (org-parse-time-string time)))))
10202 (defun org-get-deadline-time (pom &optional inherit)
10203 "Get the deadine as a time tuple, of a format suitable for
10204 calling org-deadlin with, or if there is no scheduling, returns
10206 (let ((time (org-entry-get pom "DEADLINE" inherit)))
10208 (apply 'encode-time (org-parse-time-string time)))))
10210 (defun org-remove-timestamp-with-keyword (keyword)
10211 "Remove all time stamps with KEYWORD in the current entry."
10212 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
10215 (org-back-to-heading t)
10217 (outline-next-heading)
10218 (while (re-search-backward re beg t)
10220 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
10221 (equal (char-before) ?\ ))
10222 (backward-delete-char 1)
10223 (if (string-match "^[ \t]*$" (buffer-substring
10224 (point-at-bol) (point-at-eol)))
10225 (delete-region (point-at-bol)
10226 (min (point-max) (1+ (point-at-eol))))))))))
10228 (defun org-add-planning-info (what &optional time &rest remove)
10229 "Insert new timestamp with keyword in the line directly after the headline.
10230 WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
10231 If non is given, the user is prompted for a date.
10232 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
10235 (let (org-time-was-given org-end-time-was-given ts
10236 end default-time default-input)
10239 (when (and (not time) (memq what '(scheduled deadline)))
10240 ;; Try to get a default date/time from existing timestamp
10242 (org-back-to-heading t)
10243 (setq end (save-excursion (outline-next-heading) (point)))
10244 (when (re-search-forward (if (eq what 'scheduled)
10245 org-scheduled-time-regexp
10246 org-deadline-time-regexp)
10248 (setq ts (match-string 1)
10250 (apply 'encode-time (org-parse-time-string ts))
10251 default-input (and ts (org-get-compact-tod ts))))))
10253 ;; If necessary, get the time from the user
10254 (setq time (or time (org-read-date nil 'to-time nil nil
10255 default-time default-input))))
10257 (when (and org-insert-labeled-timestamps-at-point
10258 (member what '(scheduled deadline)))
10260 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
10261 (org-insert-time-stamp time org-time-was-given
10262 nil nil nil (list org-end-time-was-given))
10266 (let (col list elt ts buffer-invisibility-spec)
10267 (org-back-to-heading t)
10268 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
10269 (goto-char (match-end 1))
10270 (setq col (current-column))
10271 (goto-char (match-end 0))
10272 (if (eobp) (insert "\n") (forward-char 1))
10273 (when (and (not what)
10276 org-keyword-time-not-clock-regexp))))
10277 ;; Nothing to add, nothing to remove...... :-)
10279 (if (and (not (looking-at outline-regexp))
10280 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
10282 (not (equal (match-string 1) org-clock-string)))
10283 (narrow-to-region (match-beginning 0) (match-end 0))
10284 (insert-before-markers "\n")
10286 (narrow-to-region (point) (point))
10287 (and org-adapt-indentation (org-indent-to-column col)))
10288 ;; Check if we have to remove something.
10289 (setq list (cons what remove))
10291 (setq elt (pop list))
10292 (goto-char (point-min))
10293 (when (or (and (eq elt 'scheduled)
10294 (re-search-forward org-scheduled-time-regexp nil t))
10295 (and (eq elt 'deadline)
10296 (re-search-forward org-deadline-time-regexp nil t))
10297 (and (eq elt 'closed)
10298 (re-search-forward org-closed-time-regexp nil t)))
10300 (if (looking-at "--+<[^>]+>") (replace-match ""))
10301 (skip-chars-backward " ")
10302 (if (looking-at " +") (replace-match ""))))
10303 (goto-char (point-max))
10304 (and org-adapt-indentation (bolp) (org-indent-to-column col))
10307 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
10308 (cond ((eq what 'scheduled) org-scheduled-string)
10309 ((eq what 'deadline) org-deadline-string)
10310 ((eq what 'closed) org-closed-string))
10312 (setq ts (org-insert-time-stamp
10314 (or org-time-was-given
10315 (and (eq what 'closed) org-log-done-with-time))
10317 nil nil (list org-end-time-was-given)))
10319 (goto-char (point-min))
10321 (if (and (looking-at "[ \t]+\n")
10322 (equal (char-before) ?\n))
10323 (delete-region (1- (point)) (point-at-eol)))
10326 (defvar org-log-note-marker (make-marker))
10327 (defvar org-log-note-purpose nil)
10328 (defvar org-log-note-state nil)
10329 (defvar org-log-note-previous-state nil)
10330 (defvar org-log-note-how nil)
10331 (defvar org-log-note-extra nil)
10332 (defvar org-log-note-window-configuration nil)
10333 (defvar org-log-note-return-to (make-marker))
10334 (defvar org-log-post-message nil
10335 "Message to be displayed after a log note has been stored.
10336 The auto-repeater uses this.")
10338 (defun org-add-note ()
10339 "Add a note to the current entry.
10340 This is done in the same way as adding a state change note."
10342 (org-add-log-setup 'note nil nil 'findpos nil))
10344 (defvar org-property-end-re)
10345 (defun org-add-log-setup (&optional purpose state prev-state
10346 findpos how &optional extra)
10347 "Set up the post command hook to take a note.
10348 If this is about to TODO state change, the new state is expected in STATE.
10349 When FINDPOS is non-nil, find the correct position for the note in
10350 the current entry. If not, assume that it can be inserted at point.
10351 HOW is an indicator what kind of note should be created.
10352 EXTRA is additional text that will be inserted into the notes buffer."
10353 (let* ((org-log-into-drawer (org-log-into-drawer))
10354 (drawer (cond ((stringp org-log-into-drawer)
10355 org-log-into-drawer)
10356 (org-log-into-drawer "LOGBOOK")
10361 (org-back-to-heading t)
10362 (narrow-to-region (point) (save-excursion
10363 (outline-next-heading) (point)))
10364 (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
10365 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
10367 (goto-char (match-end 0))
10370 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
10373 (goto-char (match-end 0))
10374 (or org-log-states-order-reversed
10375 (and (re-search-forward org-property-end-re nil t)
10376 (goto-char (1- (match-beginning 0))))))
10377 (insert "\n:" drawer ":\n:END:")
10378 (beginning-of-line 0)
10379 (org-indent-line-function)
10380 (beginning-of-line 2)
10381 (org-indent-line-function)
10383 ((and org-log-state-notes-insert-after-drawers
10385 (forward-line) (looking-at org-drawer-regexp)))
10387 (while (looking-at org-drawer-regexp)
10388 (goto-char (match-end 0))
10389 (re-search-forward org-property-end-re (point-max) t)
10391 (forward-line -1)))
10392 (unless org-log-states-order-reversed
10393 (and (= (char-after) ?\n) (forward-char 1))
10394 (org-skip-over-state-notes)
10395 (skip-chars-backward " \t\n\r")))
10396 (move-marker org-log-note-marker (point))
10397 (setq org-log-note-purpose purpose
10398 org-log-note-state state
10399 org-log-note-previous-state prev-state
10400 org-log-note-how how
10401 org-log-note-extra extra)
10402 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
10404 (defun org-skip-over-state-notes ()
10405 "Skip past the list of State notes in an entry."
10406 (if (looking-at "\n[ \t]*- State") (forward-char 1))
10407 (while (looking-at "[ \t]*- State")
10408 (condition-case nil
10410 (error (org-end-of-item)))))
10412 (defun org-add-log-note (&optional purpose)
10413 "Pop up a window for taking a note, and add this note later at point."
10414 (remove-hook 'post-command-hook 'org-add-log-note)
10415 (setq org-log-note-window-configuration (current-window-configuration))
10416 (delete-other-windows)
10417 (move-marker org-log-note-return-to (point))
10418 (switch-to-buffer (marker-buffer org-log-note-marker))
10419 (goto-char org-log-note-marker)
10420 (org-switch-to-buffer-other-window "*Org Note*")
10422 (if (memq org-log-note-how '(time state))
10423 (let (current-prefix-arg) (org-store-log-note))
10424 (let ((org-inhibit-startup t)) (org-mode))
10425 (insert (format "# Insert note for %s.
10426 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
10428 ((eq org-log-note-purpose 'clock-out) "stopped clock")
10429 ((eq org-log-note-purpose 'done) "closed todo item")
10430 ((eq org-log-note-purpose 'state)
10431 (format "state change from \"%s\" to \"%s\""
10432 (or org-log-note-previous-state "")
10433 (or org-log-note-state "")))
10434 ((eq org-log-note-purpose 'note)
10436 (t (error "This should not happen")))))
10437 (if org-log-note-extra (insert org-log-note-extra))
10438 (org-set-local 'org-finish-function 'org-store-log-note)))
10440 (defvar org-note-abort nil) ; dynamically scoped
10441 (defun org-store-log-note ()
10442 "Finish taking a log note, and insert it to where it belongs."
10443 (let ((txt (buffer-string))
10444 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
10446 (kill-buffer (current-buffer))
10447 (while (string-match "\\`#.*\n[ \t\n]*" txt)
10448 (setq txt (replace-match "" t t txt)))
10449 (if (string-match "\\s-+\\'" txt)
10450 (setq txt (replace-match "" t t txt)))
10451 (setq lines (org-split-string txt "\n"))
10452 (when (and note (string-match "\\S-" note))
10454 (org-replace-escapes
10456 (list (cons "%u" (user-login-name))
10457 (cons "%U" user-full-name)
10458 (cons "%t" (format-time-string
10459 (org-time-stamp-format 'long 'inactive)
10461 (cons "%s" (if org-log-note-state
10462 (concat "\"" org-log-note-state "\"")
10464 (cons "%S" (if org-log-note-previous-state
10465 (concat "\"" org-log-note-previous-state "\"")
10467 (if lines (setq note (concat note " \\\\")))
10469 (when (or current-prefix-arg org-note-abort)
10470 (when org-log-into-drawer
10471 (org-remove-empty-drawer-at
10472 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
10473 org-log-note-marker))
10477 (set-buffer (marker-buffer org-log-note-marker))
10479 (goto-char org-log-note-marker)
10480 (move-marker org-log-note-marker nil)
10482 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
10483 (insert "- " (pop lines))
10484 (org-indent-line-function)
10485 (beginning-of-line 1)
10486 (looking-at "[ \t]*")
10487 (setq ind (concat (match-string 0) " "))
10489 (while lines (insert "\n" ind (pop lines)))
10490 (message "Note stored")
10491 (org-back-to-heading t)
10492 (org-cycle-hide-drawers 'children)))))
10493 (set-window-configuration org-log-note-window-configuration)
10494 (with-current-buffer (marker-buffer org-log-note-return-to)
10495 (goto-char org-log-note-return-to))
10496 (move-marker org-log-note-return-to nil)
10497 (and org-log-post-message (message "%s" org-log-post-message)))
10499 (defun org-remove-empty-drawer-at (drawer pos)
10500 "Remove an emptyr DARWER drawer at position POS.
10501 POS may also be a marker."
10502 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
10508 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
10509 (replace-match ""))))))
10511 (defun org-sparse-tree (&optional arg)
10512 "Create a sparse tree, prompt for the details.
10513 This command can create sparse trees. You first need to select the type
10514 of match used to create the tree:
10516 t Show entries with a specific TODO keyword.
10517 m Show entries selected by a tags/property match.
10518 p Enter a property name and its value (both with completion on existing
10519 names/values) and show entries with that property.
10520 r Show entries matching a regular expression.
10521 d Show deadlines due within `org-deadline-warning-days'.
10522 b Show deadlines and scheduled items before a date.
10523 a Show deadlines and scheduled items after a date."
10525 (let (ans kwd value)
10526 (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
10527 (setq ans (read-char-exclusive))
10530 (call-interactively 'org-check-deadlines))
10532 (call-interactively 'org-check-before-date))
10534 (call-interactively 'org-check-after-date))
10536 (org-show-todo-tree '(4)))
10537 ((member ans '(?T ?m))
10538 (call-interactively 'org-match-sparse-tree))
10539 ((member ans '(?p ?P))
10540 (setq kwd (org-icompleting-read "Property: "
10541 (mapcar 'list (org-buffer-property-keys))))
10542 (setq value (org-icompleting-read "Value: "
10543 (mapcar 'list (org-property-values kwd))))
10544 (unless (string-match "\\`{.*}\\'" value)
10545 (setq value (concat "\"" value "\"")))
10546 (org-match-sparse-tree arg (concat kwd "=" value)))
10547 ((member ans '(?r ?R ?/))
10548 (call-interactively 'org-occur))
10549 (t (error "No such sparse tree command \"%c\"" ans)))))
10551 (defvar org-occur-highlights nil
10552 "List of overlays used for occur matches.")
10553 (make-variable-buffer-local 'org-occur-highlights)
10554 (defvar org-occur-parameters nil
10555 "Parameters of the active org-occur calls.
10556 This is a list, each call to org-occur pushes as cons cell,
10557 containing the regular expression and the callback, onto the list.
10558 The list can contain several entries if `org-occur' has been called
10559 several time with the KEEP-PREVIOUS argument. Otherwise, this list
10560 will only contain one set of parameters. When the highlights are
10561 removed (for example with `C-c C-c', or with the next edit (depending
10562 on `org-remove-highlights-with-change'), this variable is emptied
10564 (make-variable-buffer-local 'org-occur-parameters)
10566 (defun org-occur (regexp &optional keep-previous callback)
10567 "Make a compact tree which shows all matches of REGEXP.
10568 The tree will show the lines where the regexp matches, and all higher
10569 headlines above the match. It will also show the heading after the match,
10570 to make sure editing the matching entry is easy.
10571 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
10572 call to `org-occur' will be kept, to allow stacking of calls to this
10574 If CALLBACK is non-nil, it is a function which is called to confirm
10575 that the match should indeed be shown."
10576 (interactive "sRegexp: \nP")
10577 (when (equal regexp "")
10578 (error "Regexp cannot be empty"))
10579 (unless keep-previous
10580 (org-remove-occur-highlights nil nil t))
10581 (push (cons regexp callback) org-occur-parameters)
10584 (goto-char (point-min))
10585 (if (or (not keep-previous) ; do not want to keep
10586 (not org-occur-highlights)) ; no previous matches
10589 (while (re-search-forward regexp nil t)
10590 (when (or (not callback)
10591 (save-match-data (funcall callback)))
10592 (setq cnt (1+ cnt))
10593 (when org-highlight-sparse-tree-matches
10594 (org-highlight-new-match (match-beginning 0) (match-end 0)))
10595 (org-show-context 'occur-tree))))
10596 (when org-remove-highlights-with-change
10597 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
10599 (unless org-sparse-tree-open-archived-trees
10600 (org-hide-archived-subtrees (point-min) (point-max)))
10601 (run-hooks 'org-occur-hook)
10602 (if (interactive-p)
10603 (message "%d match(es) for regexp %s" cnt regexp))
10606 (defun org-show-context (&optional key)
10607 "Make sure point and context and visible.
10608 How much context is shown depends upon the variables
10609 `org-show-hierarchy-above', `org-show-following-heading'. and
10610 `org-show-siblings'."
10611 (let ((heading-p (org-on-heading-p t))
10612 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
10613 (following-p (org-get-alist-option org-show-following-heading key))
10614 (entry-p (org-get-alist-option org-show-entry-below key))
10615 (siblings-p (org-get-alist-option org-show-siblings key)))
10617 ;; Show heading or entry text
10618 (if (and heading-p (not entry-p))
10619 (org-flag-heading nil) ; only show the heading
10620 (and (or entry-p (org-invisible-p) (org-invisible-p2))
10621 (org-show-hidden-entry))) ; show entire entry
10623 ;; Show next sibling, or heading below text
10625 (and (if heading-p (org-goto-sibling) (outline-next-heading))
10626 (org-flag-heading nil))))
10627 (when siblings-p (org-show-siblings))
10629 ;; show all higher headings, possibly with siblings
10631 (while (and (condition-case nil
10632 (progn (org-up-heading-all 1) t)
10635 (org-flag-heading nil)
10636 (when siblings-p (org-show-siblings))))))))
10638 (defun org-reveal (&optional siblings)
10639 "Show current entry, hierarchy above it, and the following headline.
10640 This can be used to show a consistent set of context around locations
10641 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
10642 not t for the search context.
10644 With optional argument SIBLINGS, on each level of the hierarchy all
10645 siblings are shown. This repairs the tree structure to what it would
10646 look like when opened with hierarchical calls to `org-cycle'."
10648 (let ((org-show-hierarchy-above t)
10649 (org-show-following-heading t)
10650 (org-show-siblings (if siblings t org-show-siblings)))
10651 (org-show-context nil)))
10653 (defun org-highlight-new-match (beg end)
10654 "Highlight from BEG to END and mark the highlight is an occur headline."
10655 (let ((ov (org-make-overlay beg end)))
10656 (org-overlay-put ov 'face 'secondary-selection)
10657 (push ov org-occur-highlights)))
10659 (defun org-remove-occur-highlights (&optional beg end noremove)
10660 "Remove the occur highlights from the buffer.
10661 BEG and END are ignored. If NOREMOVE is nil, remove this function
10662 from the `before-change-functions' in the current buffer."
10664 (unless org-inhibit-highlight-removal
10665 (mapc 'org-delete-overlay org-occur-highlights)
10666 (setq org-occur-highlights nil)
10667 (setq org-occur-parameters nil)
10669 (remove-hook 'before-change-functions
10670 'org-remove-occur-highlights 'local))))
10674 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
10675 "Regular expression matching the priority indicator.")
10677 (defvar org-remove-priority-next-time nil)
10679 (defun org-priority-up ()
10680 "Increase the priority of the current item."
10682 (org-priority 'up))
10684 (defun org-priority-down ()
10685 "Decrease the priority of the current item."
10687 (org-priority 'down))
10689 (defun org-priority (&optional action)
10690 "Change the priority of an item by ARG.
10691 ACTION can be `set', `up', `down', or a character."
10693 (unless org-enable-priority-commands
10694 (error "Priority commands are disabled"))
10695 (setq action (or action 'set))
10696 (let (current new news have remove)
10698 (org-back-to-heading t)
10699 (if (looking-at org-priority-regexp)
10700 (setq current (string-to-char (match-string 2))
10702 (setq current org-default-priority))
10704 ((or (eq action 'set)
10705 (if (featurep 'xemacs) (characterp action) (integerp action)))
10706 (if (not (eq action 'set))
10708 (message "Priority %c-%c, SPC to remove: "
10709 org-highest-priority org-lowest-priority)
10710 (setq new (read-char-exclusive)))
10711 (if (and (= (upcase org-highest-priority) org-highest-priority)
10712 (= (upcase org-lowest-priority) org-lowest-priority))
10713 (setq new (upcase new)))
10714 (cond ((equal new ?\ ) (setq remove t))
10715 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
10716 (error "Priority must be between `%c' and `%c'"
10717 org-highest-priority org-lowest-priority))))
10719 (if (and (not have) (eq last-command this-command))
10720 (setq new org-lowest-priority)
10721 (setq new (if (and org-priority-start-cycle-with-default (not have))
10722 org-default-priority (1- current)))))
10724 (if (and (not have) (eq last-command this-command))
10725 (setq new org-highest-priority)
10726 (setq new (if (and org-priority-start-cycle-with-default (not have))
10727 org-default-priority (1+ current)))))
10728 (t (error "Invalid action")))
10729 (if (or (< (upcase new) org-highest-priority)
10730 (> (upcase new) org-lowest-priority))
10732 (setq news (format "%c" new))
10735 (replace-match "" t t nil 1)
10736 (replace-match news t t nil 2))
10738 (error "No priority cookie found in line")
10739 (let ((case-fold-search nil))
10740 (looking-at org-todo-line-regexp))
10743 (goto-char (match-end 2))
10744 (insert " [#" news "]"))
10745 (goto-char (match-beginning 3))
10746 (insert "[#" news "] "))))
10747 (org-preserve-lc (org-set-tags nil 'align)))
10749 (message "Priority removed")
10750 (message "Priority of current item set to %s" news))))
10752 (defun org-get-priority (s)
10753 "Find priority cookie and return priority."
10755 (if (not (string-match org-priority-regexp s))
10756 (* 1000 (- org-lowest-priority org-default-priority))
10757 (* 1000 (- org-lowest-priority
10758 (string-to-char (match-string 2 s)))))))
10762 (defvar org-agenda-archives-mode)
10763 (defvar org-map-continue-from nil
10764 "Position from where mapping should continue.
10765 Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
10767 (defvar org-scanner-tags nil
10768 "The current tag list while the tags scanner is running.")
10769 (defvar org-trust-scanner-tags nil
10770 "Should `org-get-tags-at' use the tags fro the scanner.
10771 This is for internal dynamical scoping only.
10772 When this is non-nil, the function `org-get-tags-at' will return the value
10773 of `org-scanner-tags' instead of building the list by itself. This
10774 can lead to large speed-ups when the tags scanner is used in a file with
10775 many entries, and when the list of tags is retrieved, for example to
10776 obtain a list of properties. Building the tags list for each entry in such
10777 a file becomes an N^2 operation - but with this variable set, it scales
10780 (defun org-scan-tags (action matcher &optional todo-only)
10781 "Scan headline tags with inheritance and produce output ACTION.
10783 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
10784 or `agenda' to produce an entry list for an agenda view. It can also be
10785 a Lisp form or a function that should be called at each matched headline, in
10786 this case the return value is a list of all return values from these calls.
10788 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
10789 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
10790 only lines with a TODO keyword are included in the output."
10791 (require 'org-agenda)
10792 (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
10793 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
10795 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
10796 (props (list 'face 'default
10797 'done-face 'org-agenda-done
10798 'undone-face 'default
10799 'mouse-face 'highlight
10800 'org-not-done-regexp org-not-done-regexp
10801 'org-todo-regexp org-todo-regexp
10802 'keymap org-agenda-keymap
10804 (format "mouse-2 or RET jump to org file %s"
10805 (abbreviate-file-name
10806 (or (buffer-file-name (buffer-base-buffer))
10807 (buffer-name (buffer-base-buffer)))))))
10808 (case-fold-search nil)
10809 (org-map-continue-from nil)
10810 lspos tags tags-list
10811 (tags-alist (list (cons 0 org-file-tags)))
10812 (llast 0) rtn rtn1 level category i txt
10813 todo marker entry priority)
10814 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
10815 (setq action (list 'lambda nil action)))
10817 (goto-char (point-min))
10818 (when (eq action 'sparse-tree)
10820 (org-remove-occur-highlights))
10821 (while (re-search-forward re nil t)
10823 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
10824 tags (if (match-end 4) (org-match-string-no-properties 4)))
10825 (goto-char (setq lspos (match-beginning 0)))
10826 (setq level (org-reduced-level (funcall outline-level))
10827 category (org-get-category))
10828 (setq i llast llast level)
10829 ;; remove tag lists from same and sublevels
10830 (while (>= i level)
10831 (when (setq entry (assoc i tags-alist))
10832 (setq tags-alist (delete entry tags-alist)))
10834 ;; add the next tags
10836 (setq tags (org-split-string tags ":")
10838 (cons (cons level tags) tags-alist)))
10839 ;; compile tags for current headline
10841 (if org-use-tag-inheritance
10842 (apply 'append (mapcar 'cdr (reverse tags-alist)))
10844 org-scanner-tags tags-list)
10845 (when org-use-tag-inheritance
10846 (setcdr (car tags-alist)
10847 (mapcar (lambda (x)
10848 (setq x (copy-sequence x))
10849 (org-add-prop-inherited x))
10850 (cdar tags-alist))))
10851 (when (and tags org-use-tag-inheritance
10852 (or (not (eq t org-use-tag-inheritance))
10853 org-tags-exclude-from-inheritance))
10854 ;; selective inheritance, remove uninherited ones
10855 (setcdr (car tags-alist)
10856 (org-remove-uniherited-tags (cdar tags-alist))))
10857 (when (and (or (not todo-only)
10858 (and (member todo org-not-done-keywords)
10859 (or (not org-agenda-tags-todo-honor-ignore-options)
10860 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
10861 (let ((case-fold-search t)) (eval matcher))
10863 (not (member org-archive-tag tags-list))
10864 ;; we have an archive tag, should we use this anyway?
10865 (or (not org-agenda-skip-archived-trees)
10866 (and (eq action 'agenda) org-agenda-archives-mode))))
10867 (unless (eq action 'sparse-tree) (org-agenda-skip))
10869 ;; select this headline
10872 ((eq action 'sparse-tree)
10873 (and org-highlight-sparse-tree-matches
10874 (org-get-heading) (match-end 0)
10875 (org-highlight-new-match
10876 (match-beginning 0) (match-beginning 1)))
10877 (org-show-context 'tags-tree))
10878 ((eq action 'agenda)
10879 (setq txt (org-format-agenda-item
10882 (if (eq org-tags-match-list-sublevels 'indented)
10883 (make-string (1- level) ?.) "")
10888 priority (org-get-priority txt))
10890 (setq marker (org-agenda-new-marker))
10891 (org-add-props txt props
10892 'org-marker marker 'org-hd-marker marker 'org-category category
10894 'priority priority 'type "tagsmatch")
10896 ((functionp action)
10897 (setq org-map-continue-from nil)
10899 (setq rtn1 (funcall action))
10901 (t (error "Invalid action")))
10903 ;; if we are to skip sublevels, jump to end of subtree
10904 (unless org-tags-match-list-sublevels
10905 (org-end-of-subtree t)
10906 (backward-char 1))))
10907 ;; Get the correct position from where to continue
10908 (if org-map-continue-from
10909 (goto-char org-map-continue-from)
10910 (and (= (point) lspos) (end-of-line 1)))))
10911 (when (and (eq action 'sparse-tree)
10912 (not org-sparse-tree-open-archived-trees))
10913 (org-hide-archived-subtrees (point-min) (point-max)))
10916 (defun org-remove-uniherited-tags (tags)
10917 "Remove all tags that are not inherited from the list TAGS."
10919 ((eq org-use-tag-inheritance t)
10920 (if org-tags-exclude-from-inheritance
10921 (org-delete-all org-tags-exclude-from-inheritance tags)
10923 ((not org-use-tag-inheritance) nil)
10924 ((stringp org-use-tag-inheritance)
10927 (if (and (string-match org-use-tag-inheritance x)
10928 (not (member x org-tags-exclude-from-inheritance)))
10931 ((listp org-use-tag-inheritance)
10934 (if (member x org-use-tag-inheritance) x nil))
10937 (defvar todo-only) ;; dynamically scoped
10939 (defun org-match-sparse-tree (&optional todo-only match)
10940 "Create a sparse tree according to tags string MATCH.
10941 MATCH can contain positive and negative selection of tags, like
10942 \"+WORK+URGENT-WITHBOSS\".
10943 If optional argument TODO-ONLY is non-nil, only select lines that are
10946 (org-prepare-agenda-buffers (list (current-buffer)))
10947 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
10949 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
10951 (defvar org-cached-props nil)
10952 (defun org-cached-entry-get (pom property)
10953 (if (or (eq t org-use-property-inheritance)
10954 (and (stringp org-use-property-inheritance)
10955 (string-match org-use-property-inheritance property))
10956 (and (listp org-use-property-inheritance)
10957 (member property org-use-property-inheritance)))
10958 ;; Caching is not possible, check it directly
10959 (org-entry-get pom property 'inherit)
10960 ;; Get all properties, so that we can do complicated checks easily
10961 (cdr (assoc property (or org-cached-props
10962 (setq org-cached-props
10963 (org-entry-properties pom)))))))
10965 (defun org-global-tags-completion-table (&optional files)
10966 "Return the list of all tags in all agenda buffer/files."
10973 (set-buffer (find-file-noselect file))
10974 (append (org-get-buffer-tags)
10975 (mapcar (lambda (x) (if (stringp (car-safe x))
10976 (list (car-safe x)) nil))
10978 (if (and files (car files))
10980 (org-agenda-files))))))))
10982 (defun org-make-tags-matcher (match)
10983 "Create the TAGS//TODO matcher form for the selection string MATCH."
10984 ;; todo-only is scoped dynamically into this function, and the function
10985 ;; may change it if the matcher asks for it.
10987 ;; Get a new match request, with completion
10988 (let ((org-last-tags-completion-table
10989 (org-global-tags-completion-table)))
10990 (setq match (org-completing-read-no-i
10991 "Match: " 'org-tags-completion-function nil nil nil
10992 'org-tags-history))))
10994 ;; Parse the string and create a lisp form
10995 (let ((match0 match)
10996 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
10998 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
10999 orterms term orlist re-p str-p level-p level-op time-p
11000 prop-p pn pv po cat-p gv rest)
11001 (if (string-match "/+" match)
11002 ;; match contains also a todo-matching request
11004 (setq tagsmatch (substring match 0 (match-beginning 0))
11005 todomatch (substring match (match-end 0)))
11006 (if (string-match "^!" todomatch)
11007 (setq todo-only t todomatch (substring todomatch 1)))
11008 (if (string-match "^\\s-*$" todomatch)
11009 (setq todomatch nil)))
11010 ;; only matching tags
11011 (setq tagsmatch match todomatch nil))
11013 ;; Make the tags matcher
11014 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
11015 (setq tagsmatcher t)
11016 (setq orterms (org-split-string tagsmatch "|") orlist nil)
11017 (while (setq term (pop orterms))
11018 (while (and (equal (substring term -1) "\\") orterms)
11019 (setq term (concat term "|" (pop orterms)))) ; repair bad split
11020 (while (string-match re term)
11021 (setq rest (substring term (match-end 0))
11022 minus (and (match-end 1)
11023 (equal (match-string 1 term) "-"))
11024 tag (match-string 2 term)
11025 re-p (equal (string-to-char tag) ?{)
11026 level-p (match-end 4)
11027 prop-p (match-end 5)
11029 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
11031 (setq level-op (org-op-to-function (match-string 3 term)))
11032 `(,level-op level ,(string-to-number
11033 (match-string 4 term))))
11035 (setq pn (match-string 5 term)
11036 po (match-string 6 term)
11037 pv (match-string 7 term)
11038 cat-p (equal pn "CATEGORY")
11039 re-p (equal (string-to-char pv) ?{)
11040 str-p (equal (string-to-char pv) ?\")
11041 time-p (save-match-data
11042 (string-match "^\"[[<].*[]>]\"$" pv))
11043 pv (if (or re-p str-p) (substring pv 1 -1) pv))
11044 (if time-p (setq pv (org-matcher-time pv)))
11045 (setq po (org-op-to-function po (if time-p 'time str-p)))
11047 ((equal pn "CATEGORY")
11048 (setq gv '(get-text-property (point) 'org-category)))
11052 (setq gv `(org-cached-entry-get nil ,pn))))
11055 `(not (string-match ,pv (or ,gv "")))
11056 `(string-match ,pv (or ,gv "")))
11058 `(,po (or ,gv "") ,pv)
11059 `(,po (string-to-number (or ,gv ""))
11060 ,(string-to-number pv) ))))
11061 (t `(member ,tag tags-list)))
11062 mm (if minus (list 'not mm) mm)
11064 (push mm tagsmatcher))
11065 (push (if (> (length tagsmatcher) 1)
11066 (cons 'and tagsmatcher)
11069 (setq tagsmatcher nil))
11070 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
11072 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
11073 ;; Make the todo matcher
11074 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
11075 (setq todomatcher t)
11076 (setq orterms (org-split-string todomatch "|") orlist nil)
11077 (while (setq term (pop orterms))
11078 (while (string-match re term)
11079 (setq minus (and (match-end 1)
11080 (equal (match-string 1 term) "-"))
11081 kwd (match-string 2 term)
11082 re-p (equal (string-to-char kwd) ?{)
11083 term (substring term (match-end 0))
11085 `(string-match ,(substring kwd 1 -1) todo)
11086 (list 'equal 'todo kwd))
11087 mm (if minus (list 'not mm) mm))
11088 (push mm todomatcher))
11089 (push (if (> (length todomatcher) 1)
11090 (cons 'and todomatcher)
11093 (setq todomatcher nil))
11094 (setq todomatcher (if (> (length orlist) 1)
11095 (cons 'or orlist) (car orlist))))
11097 ;; Return the string and lisp forms of the matcher
11098 (setq matcher (if todomatcher
11099 (list 'and tagsmatcher todomatcher)
11101 (cons match0 matcher)))
11103 (defun org-op-to-function (op &optional stringp)
11104 "Turn an operator into the appropriate function."
11107 ((equal op "<" ) '(< string< org-time<))
11108 ((equal op ">" ) '(> org-string> org-time>))
11109 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
11110 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
11111 ((member op '("=" "==")) '(= string= org-time=))
11112 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
11113 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
11115 (defun org<> (a b) (not (= a b)))
11116 (defun org-string<= (a b) (or (string= a b) (string< a b)))
11117 (defun org-string>= (a b) (not (string< a b)))
11118 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
11119 (defun org-string<> (a b) (not (string= a b)))
11120 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
11121 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
11122 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
11123 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
11124 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
11125 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
11127 "Convert S to a floating point time.
11128 If S is already a number, just return it. If it is a string, parse
11129 it as a time string and apply `float-time' to it. If S is nil, just return 0."
11133 (condition-case nil
11134 (float-time (apply 'encode-time (org-parse-time-string s)))
11138 (defun org-time-today ()
11139 "Time in seconds today at 0:00.
11140 Returns the float number of seconds since the beginning of the
11141 epoch to the beginning of today (00:00)."
11142 (float-time (apply 'encode-time
11143 (append '(0 0 0) (nthcdr 3 (decode-time))))))
11145 (defun org-matcher-time (s)
11146 "Interpret a time comparison value."
11149 ((string= s "<now>") (float-time))
11150 ((string= s "<today>") (org-time-today))
11151 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
11152 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
11153 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
11154 (+ (org-time-today)
11155 (* (string-to-number (match-string 1 s))
11156 (cdr (assoc (match-string 2 s)
11157 '(("d" . 86400.0) ("w" . 604800.0)
11158 ("m" . 2678400.0) ("y" . 31557600.0)))))))
11161 (defun org-match-any-p (re list)
11162 "Does re match any element of list?"
11163 (setq list (mapcar (lambda (x) (string-match re x)) list))
11166 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
11167 (defvar org-tags-overlay (org-make-overlay 1 1))
11168 (org-detach-overlay org-tags-overlay)
11170 (defun org-get-local-tags-at (&optional pos)
11171 "Get a list of tags defined in the current headline."
11172 (org-get-tags-at pos 'local))
11174 (defun org-get-local-tags ()
11175 "Get a list of tags defined in the current headline."
11176 (org-get-tags-at nil 'local))
11178 (defun org-get-tags-at (&optional pos local)
11179 "Get a list of all headline tags applicable at POS.
11180 POS defaults to point. If tags are inherited, the list contains
11181 the targets in the same sequence as the headlines appear, i.e.
11182 the tags of the current headline come last.
11183 When LOCAL is non-nil, only return tags from the current headline,
11184 ignore inherited ones."
11186 (if (and org-trust-scanner-tags
11187 (or (not pos) (equal pos (point)))
11190 (let (tags ltags lastpos parent)
11194 (goto-char (or pos (point)))
11197 (condition-case nil
11199 (org-back-to-heading t)
11200 (while (not (equal lastpos (point)))
11201 (setq lastpos (point))
11203 (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
11204 (setq ltags (org-split-string
11205 (org-match-string-no-properties 1) ":"))
11207 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
11210 (org-remove-uniherited-tags ltags)
11213 (or org-use-tag-inheritance (throw 'done t))
11214 (if local (throw 'done t))
11215 (or (org-up-heading-safe) (error nil))
11218 (append (org-remove-uniherited-tags org-file-tags) tags)))))
11220 (defun org-add-prop-inherited (s)
11221 (add-text-properties 0 (length s) '(inherited t) s)
11224 (defun org-toggle-tag (tag &optional onoff)
11225 "Toggle the tag TAG for the current line.
11226 If ONOFF is `on' or `off', don't toggle but set to this state."
11229 (org-back-to-heading t)
11230 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
11233 (setq current (match-string 1))
11234 (replace-match ""))
11236 (setq current (nreverse (org-split-string current ":")))
11240 (or (member tag current) (push tag current)))
11242 (or (not (member tag current)) (setq current (delete tag current))))
11243 (t (if (member tag current)
11244 (setq current (delete tag current))
11246 (push tag current))))
11250 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
11251 (org-set-tags nil t))
11252 (delete-horizontal-space))
11253 (run-hooks 'org-after-tags-change-hook))
11256 (defun org-align-tags-here (to-col)
11257 ;; Assumes that this is a headline
11258 (let ((pos (point)) (col (current-column)) ncol tags-l p)
11259 (beginning-of-line 1)
11260 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11261 (< pos (match-beginning 2)))
11263 (setq tags-l (- (match-end 2) (match-beginning 2)))
11264 (goto-char (match-beginning 1))
11266 (delete-region (point) (1+ (match-beginning 2)))
11267 (setq ncol (max (1+ (current-column))
11271 (- (abs to-col) tags-l))))
11273 (insert (make-string (- ncol (current-column)) ?\ ))
11274 (setq ncol (current-column))
11275 (when indent-tabs-mode (tabify p (point-at-eol)))
11276 (org-move-to-column (min ncol col) t))
11279 (defun org-set-tags-command (&optional arg just-align)
11280 "Call the set-tags command for the current entry."
11282 (if (org-on-heading-p)
11283 (org-set-tags arg just-align)
11285 (org-back-to-heading t)
11286 (org-set-tags arg just-align))))
11288 (defun org-set-tags-to (data)
11289 "Set the tags of the current entry to DATA, replacing the current tags.
11290 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
11291 If DATA is nil or the empty string, any tags will be removed."
11292 (interactive "sTags: ")
11296 ((equal data "") "")
11298 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
11301 (concat ":" (mapconcat 'identity data ":") ":"))
11305 (org-back-to-heading t)
11306 (when (looking-at org-complex-heading-regexp)
11309 (goto-char (match-beginning 5))
11311 (delete-region (point) (point-at-eol))
11312 (org-set-tags nil 'align))
11313 (goto-char (point-at-eol))
11315 (org-set-tags nil 'align)))
11316 (beginning-of-line 1)
11317 (if (looking-at ".*?\\([ \t]+\\)$")
11318 (delete-region (match-beginning 1) (match-end 1))))))
11320 (defun org-set-tags (&optional arg just-align)
11321 "Set the tags for the current headline.
11322 With prefix ARG, realign all tags in headings in the current buffer."
11324 (let* ((re (concat "^" outline-regexp))
11325 (current (org-get-tags-string))
11326 (col (current-column))
11327 (org-setting-tags t)
11328 table current-tags inherited-tags ; computed below when needed
11332 (goto-char (point-min))
11333 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
11334 (while (re-search-forward re nil t)
11335 (org-set-tags nil t)
11337 (message "All tags realigned to column %d" org-tags-column))
11339 (setq tags current)
11340 ;; Get a new set of tags from the user
11342 (setq table (append org-tag-persistent-alist
11343 (or org-tag-alist (org-get-buffer-tags)))
11344 org-last-tags-completion-table table
11345 current-tags (org-split-string current ":")
11346 inherited-tags (nreverse
11347 (nthcdr (length current-tags)
11348 (nreverse (org-get-tags-at))))
11350 (if (or (eq t org-use-fast-tag-selection)
11351 (and org-use-fast-tag-selection
11352 (delq nil (mapcar 'cdr table))))
11353 (org-fast-tag-selection
11354 current-tags inherited-tags table
11355 (if org-fast-tag-selection-include-todo org-todo-key-alist))
11356 (let ((org-add-colon-after-tag-completion t))
11358 (org-without-partial-completion
11359 (org-icompleting-read "Tags: " 'org-tags-completion-function
11360 nil nil current 'org-tags-history)))))))
11361 (while (string-match "[-+&]+" tags)
11362 ;; No boolean logic, just a list
11363 (setq tags (replace-match ":" t t tags))))
11365 (if org-tags-sort-function
11366 (setq tags (mapconcat 'identity
11367 (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
11368 org-tags-sort-function) ":")))
11370 (if (string-match "\\`[\t ]*\\'" tags)
11372 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
11373 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
11375 ;; Insert new tags at the correct column
11376 (beginning-of-line 1)
11378 ((and (equal current "") (equal tags "")))
11379 ((re-search-forward
11380 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
11382 (if (equal tags "")
11384 (goto-char (match-beginning 0))
11385 (setq c0 (current-column) p0 (point)
11386 c1 (max (1+ c0) (if (> org-tags-column 0)
11388 (- (- org-tags-column) (length tags))))
11389 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
11390 (replace-match rpl t t)
11391 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
11393 (t (error "Tags alignment failed")))
11394 (org-move-to-column col)
11396 (run-hooks 'org-after-tags-change-hook)))))
11398 (defun org-change-tag-in-region (beg end tag off)
11399 "Add or remove TAG for each entry in the region.
11400 This works in the agenda, and also in an org-mode buffer."
11402 (list (region-beginning) (region-end)
11403 (let ((org-last-tags-completion-table
11405 (org-get-buffer-tags)
11406 (org-global-tags-completion-table))))
11407 (org-icompleting-read
11408 "Tag: " 'org-tags-completion-function nil nil nil
11409 'org-tags-history))
11411 (message "[s]et or [r]emove? ")
11412 (equal (read-char-exclusive) ?r))))
11413 (if (fboundp 'deactivate-mark) (deactivate-mark))
11414 (let ((agendap (equal major-mode 'org-agenda-mode))
11415 l1 l2 m buf pos newhead (cnt 0))
11417 (setq l2 (1- (org-current-line)))
11419 (setq l1 (org-current-line))
11420 (loop for l from l1 to l2 do
11422 (setq m (get-text-property (point) 'org-hd-marker))
11423 (when (or (and (org-mode-p) (org-on-heading-p))
11425 (setq buf (if agendap (marker-buffer m) (current-buffer))
11426 pos (if agendap m (point)))
11427 (with-current-buffer buf
11431 (setq cnt (1+ cnt))
11432 (org-toggle-tag tag (if off 'off 'on))
11433 (setq newhead (org-get-heading)))))
11434 (and agendap (org-agenda-change-all-lines newhead m))))
11435 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
11437 (defun org-tags-completion-function (string predicate &optional flag)
11438 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
11439 (confirm (lambda (x) (stringp (car x)))))
11440 (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
11441 (setq s1 (match-string 1 string)
11442 s2 (match-string 2 string))
11443 (setq s1 "" s2 string))
11447 (setq rtn (try-completion s2 ctable confirm))
11450 (concat s1 s2 (substring rtn (length s2))
11451 (if (and org-add-colon-after-tag-completion
11452 (assoc rtn ctable))
11457 (all-completions s2 ctable confirm)
11461 (assoc s2 ctable)))
11464 (defun org-fast-tag-insert (kwd tags face &optional end)
11465 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
11466 (insert (format "%-12s" (concat kwd ":"))
11467 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
11470 (defun org-fast-tag-show-exit (flag)
11473 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
11474 (replace-match ""))
11477 (org-move-to-column (- (window-width) 19) t)
11478 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
11480 (defun org-set-current-tags-overlay (current prefix)
11481 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
11482 (if (featurep 'xemacs)
11483 (org-overlay-display org-tags-overlay (concat prefix s)
11484 'secondary-selection)
11485 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
11486 (org-overlay-display org-tags-overlay (concat prefix s)))))
11488 (defun org-fast-tag-selection (current inherited table &optional todo-table)
11489 "Fast tag selection with single keys.
11490 CURRENT is the current list of tags in the headline, INHERITED is the
11491 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
11492 possibly with grouping information. TODO-TABLE is a similar table with
11493 TODO keywords, should these have keys assigned to them.
11494 If the keys are nil, a-z are automatically assigned.
11495 Returns the new tags string, or nil to not change the current settings."
11496 (let* ((fulltable (append table todo-table))
11497 (maxlen (apply 'max (mapcar
11499 (if (stringp (car x)) (string-width (car x)) 0))
11501 (buf (current-buffer))
11502 (expert (eq org-fast-tag-selection-single-key 'expert))
11504 (fwidth (+ maxlen 3 1 3))
11505 (ncol (/ (- (window-width) 4) fwidth))
11508 tg cnt e c char c1 c2 ntable tbl rtn
11509 ov-start ov-end ov-prefix
11510 (exit-after-next org-fast-tag-selection-single-key)
11511 (done-keywords org-done-keywords)
11514 (beginning-of-line 1)
11516 (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11517 (setq ov-start (match-beginning 1)
11518 ov-end (match-end 1)
11520 (setq ov-start (1- (point-at-eol))
11521 ov-end (1+ ov-start))
11522 (skip-chars-forward "^\n\r")
11525 (buffer-substring (1- (point)) (point))
11526 (if (> (current-column) org-tags-column)
11528 (make-string (- org-tags-column (current-column)) ?\ ))))))
11529 (org-move-overlay org-tags-overlay ov-start ov-end)
11530 (save-window-excursion
11532 (set-buffer (get-buffer-create " *Org tags*"))
11533 (delete-other-windows)
11534 (split-window-vertically)
11535 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
11537 (org-set-local 'org-done-keywords done-keywords)
11538 (org-fast-tag-insert "Inherited" inherited i-face "\n")
11539 (org-fast-tag-insert "Current" current c-face "\n\n")
11540 (org-fast-tag-show-exit exit-after-next)
11541 (org-set-current-tags-overlay current ov-prefix)
11542 (setq tbl fulltable char ?a cnt 0)
11543 (while (setq e (pop tbl))
11545 ((equal e '(:startgroup))
11546 (push '() groups) (setq ingroup t)
11547 (when (not (= cnt 0))
11551 ((equal e '(:endgroup))
11552 (setq ingroup nil cnt 0)
11554 ((equal e '(:newline))
11555 (when (not (= cnt 0))
11559 (while (equal (car tbl) '(:newline))
11561 (setq tbl (cdr tbl)))))
11563 (setq tg (copy-sequence (car e)) c2 nil)
11566 ;; automatically assign a character.
11567 (setq c1 (string-to-char
11568 (downcase (substring
11569 tg (if (= (string-to-char tg) ?@) 1 0)))))
11570 (if (or (rassoc c1 ntable) (rassoc c1 table))
11571 (while (or (rassoc char ntable) (rassoc char table))
11572 (setq char (1+ char)))
11574 (setq c (or c2 char)))
11575 (if ingroup (push tg (car groups)))
11576 (setq tg (org-add-props tg nil 'face
11578 ((not (assoc tg table))
11579 (org-get-todo-face tg))
11580 ((member tg current) c-face)
11581 ((member tg inherited) i-face)
11583 (if (and (= cnt 0) (not ingroup)) (insert " "))
11584 (insert "[" c "] " tg (make-string
11585 (- fwidth 4 (length tg)) ?\ ))
11586 (push (cons tg c) ntable)
11587 (when (= (setq cnt (1+ cnt)) ncol)
11589 (if ingroup (insert " "))
11591 (setq ntable (nreverse ntable))
11593 (goto-char (point-min))
11594 (if (not expert) (org-fit-window-to-buffer))
11598 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
11599 (if groups " [!] no groups" " [!]groups")
11600 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
11601 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11603 ((= c ?\r) (throw 'exit t))
11605 (setq groups (not groups))
11606 (goto-char (point-min))
11607 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
11610 (org-fast-tag-show-exit
11611 (setq exit-after-next (not exit-after-next)))
11613 (delete-other-windows)
11614 (split-window-vertically)
11615 (org-switch-to-buffer-other-window " *Org tags*")
11616 (org-fit-window-to-buffer)))
11618 (and (= c ?q) (not (rassoc c ntable))))
11619 (org-detach-overlay org-tags-overlay)
11620 (setq quit-flag t))
11623 (if exit-after-next (setq exit-after-next 'now)))
11625 (condition-case nil
11626 (setq tg (org-icompleting-read
11629 (with-current-buffer buf
11630 (org-get-buffer-tags)))))
11631 (quit (setq tg "")))
11632 (when (string-match "\\S-" tg)
11633 (add-to-list 'buffer-tags (list tg))
11634 (if (member tg current)
11635 (setq current (delete tg current))
11636 (push tg current)))
11637 (if exit-after-next (setq exit-after-next 'now)))
11638 ((setq e (rassoc c todo-table) tg (car e))
11639 (with-current-buffer buf
11640 (save-excursion (org-todo tg)))
11641 (if exit-after-next (setq exit-after-next 'now)))
11642 ((setq e (rassoc c ntable) tg (car e))
11643 (if (member tg current)
11644 (setq current (delete tg current))
11645 (loop for g in groups do
11648 (setq current (delete x current)))
11651 (if exit-after-next (setq exit-after-next 'now))))
11653 ;; Create a sorted list
11657 (assoc b (cdr (memq (assoc a ntable) ntable))))))
11658 (if (eq exit-after-next 'now) (throw 'exit t))
11659 (goto-char (point-min))
11660 (beginning-of-line 2)
11661 (delete-region (point) (point-at-eol))
11662 (org-fast-tag-insert "Current" current c-face)
11663 (org-set-current-tags-overlay current ov-prefix)
11664 (while (re-search-forward
11665 (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
11666 (setq tg (match-string 1))
11667 (add-text-properties
11668 (match-beginning 1) (match-end 1)
11671 ((member tg current) c-face)
11672 ((member tg inherited) i-face)
11673 (t (get-text-property (match-beginning 1) 'face))))))
11674 (goto-char (point-min)))))
11675 (org-detach-overlay org-tags-overlay)
11677 (mapconcat 'identity current ":")
11680 (defun org-get-tags-string ()
11681 "Get the TAGS string in the current headline."
11682 (unless (org-on-heading-p t)
11683 (error "Not on a heading"))
11685 (beginning-of-line 1)
11686 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
11687 (org-match-string-no-properties 1)
11690 (defun org-get-tags ()
11691 "Get the list of tags specified in the current headline."
11692 (org-split-string (org-get-tags-string) ":"))
11694 (defun org-get-buffer-tags ()
11695 "Get a table of all tags used in the buffer, for completion."
11698 (goto-char (point-min))
11699 (while (re-search-forward
11700 (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
11701 (when (equal (char-after (point-at-bol 0)) ?*)
11702 (mapc (lambda (x) (add-to-list 'tags x))
11703 (org-split-string (org-match-string-no-properties 1) ":")))))
11704 (mapcar 'list tags)))
11706 ;;;; The mapping API
11709 (defun org-map-entries (func &optional match scope &rest skip)
11710 "Call FUNC at each headline selected by MATCH in SCOPE.
11712 FUNC is a function or a lisp form. The function will be called without
11713 arguments, with the cursor positioned at the beginning of the headline.
11714 The return values of all calls to the function will be collected and
11715 returned as a list.
11717 The call to FUNC will be wrapped into a save-excursion form, so FUNC
11718 does not need to preserve point. After evaluation, the cursor will be
11719 moved to the end of the line (presumably of the headline of the
11720 processed entry) and search continues from there. Under some
11721 circumstances, this may not produce the wanted results. For example,
11722 if you have removed (e.g. archived) the current (sub)tree it could
11723 mean that the next entry will be skipped entirely. In such cases, you
11724 can specify the position from where search should continue by making
11725 FUNC set the variable `org-map-continue-from' to the desired buffer
11728 MATCH is a tags/property/todo match as it is used in the agenda tags view.
11729 Only headlines that are matched by this query will be considered during
11730 the iteration. When MATCH is nil or t, all headlines will be
11731 visited by the iteration.
11733 SCOPE determines the scope of this command. It can be any of:
11735 nil The current buffer, respecting the restriction if any
11736 tree The subtree started with the entry at point
11737 file The current buffer, without restriction
11739 The current buffer, and any archives associated with it
11740 agenda All agenda files
11741 agenda-with-archives
11742 All agenda files with any archive files associated with them
11744 If this is a list, all files in the list will be scanned
11746 The remaining args are treated as settings for the skipping facilities of
11747 the scanner. The following items can be given here:
11749 archive skip trees with the archive tag.
11750 comment skip trees with the COMMENT keyword
11751 function or Emacs Lisp form:
11752 will be used as value for `org-agenda-skip-function', so whenever
11753 the function returns t, FUNC will not be called for that
11754 entry and search will continue from the point where the
11755 function leaves it.
11757 If your function needs to retrieve the tags including inherited tags
11758 at the *current* entry, you can use the value of the variable
11759 `org-scanner-tags' which will be much faster than getting the value
11760 with `org-get-tags-at'. If your function gets properties with
11761 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
11762 to t around the call to `org-entry-properties' to get the same speedup.
11763 Note that if your function moves around to retrieve tags and properties at
11764 a *different* entry, you cannot use these techniques."
11765 (let* ((org-agenda-archives-mode nil) ; just to make sure
11766 (org-agenda-skip-archived-trees (memq 'archive skip))
11767 (org-agenda-skip-comment-trees (memq 'comment skip))
11768 (org-agenda-skip-function
11769 (car (org-delete-all '(comment archive) skip)))
11770 (org-tags-match-list-sublevels t)
11772 org-todo-keywords-for-agenda
11773 org-done-keywords-for-agenda
11774 org-todo-keyword-alist-for-agenda
11775 org-drawers-for-agenda
11776 org-tag-alist-for-agenda)
11779 ((eq match t) (setq matcher t))
11780 ((eq match nil) (setq matcher t))
11781 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
11785 (when (eq scope 'tree)
11786 (org-back-to-heading t)
11787 (org-narrow-to-subtree)
11792 (org-prepare-agenda-buffers
11793 (list (buffer-file-name (current-buffer))))
11794 (setq res (org-scan-tags func matcher)))
11795 ;; Get the right scope
11797 ((and scope (listp scope) (symbolp (car scope)))
11798 (setq scope (eval scope)))
11799 ((eq scope 'agenda)
11800 (setq scope (org-agenda-files t)))
11801 ((eq scope 'agenda-with-archives)
11802 (setq scope (org-agenda-files t))
11803 (setq scope (org-add-archive-files scope)))
11805 (setq scope (list (buffer-file-name))))
11806 ((eq scope 'file-with-archives)
11807 (setq scope (org-add-archive-files (list (buffer-file-name))))))
11808 (org-prepare-agenda-buffers scope)
11809 (while (setq file (pop scope))
11810 (with-current-buffer (org-find-base-buffer-visiting file)
11814 (goto-char (point-min))
11815 (setq res (append res (org-scan-tags func matcher))))))))))
11820 ;;; Setting and retrieving properties
11822 (defconst org-special-properties
11823 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
11824 "TIMESTAMP" "TIMESTAMP_IA")
11825 "The special properties valid in Org-mode.
11827 These are properties that are not defined in the property drawer,
11828 but in some other way.")
11830 (defconst org-default-properties
11831 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
11832 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
11833 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
11834 "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
11835 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
11836 "CLOCK_MODELINE_TOTAL")
11837 "Some properties that are used by Org-mode for various purposes.
11838 Being in this list makes sure that they are offered for completion.")
11840 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
11841 "Regular expression matching the first line of a property drawer.")
11843 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
11844 "Regular expression matching the first line of a property drawer.")
11846 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
11847 "Regular expression matching the first line of a property drawer.")
11849 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
11850 "Regular expression matching the first line of a property drawer.")
11852 (defconst org-property-drawer-re
11853 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
11854 org-property-end-re "\\)\n?")
11855 "Matches an entire property drawer.")
11857 (defconst org-clock-drawer-re
11858 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
11859 org-property-end-re "\\)\n?")
11860 "Matches an entire clock drawer.")
11862 (defun org-property-action ()
11863 "Do an action on properties."
11866 (org-at-property-p)
11867 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
11868 (setq c (read-char-exclusive))
11871 (call-interactively 'org-set-property))
11873 (call-interactively 'org-delete-property))
11875 (call-interactively 'org-delete-property-globally))
11877 (call-interactively 'org-compute-property-at-point))
11878 (t (error "No such property action %c" c)))))
11880 (defun org-set-effort (&optional value)
11881 "Set the effort property of the current entry.
11882 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
11885 (if (equal value 0) (setq value 10))
11886 (let* ((completion-ignore-case t)
11887 (prop org-effort-property)
11888 (cur (org-entry-get nil prop))
11889 (allowed (org-property-get-allowed-values nil prop 'table))
11890 (existing (mapcar 'list (org-property-values prop)))
11892 ((stringp value) value)
11893 ((and allowed (integerp value))
11894 (or (car (nth (1- value) allowed))
11895 (car (org-last allowed))))
11897 (org-completing-read "Value: " allowed nil 'req-match))
11899 (let (org-completion-use-ido org-completion-use-iswitchb)
11900 (org-completing-read
11901 (concat "Value " (if (and cur (string-match "\\S-" cur))
11902 (concat "[" cur "]") "")
11904 existing nil nil "" nil cur))))))
11905 (unless (equal (org-entry-get nil prop) val)
11906 (org-entry-put nil prop val))
11907 (message "%s is now %s" prop val)))
11909 (defun org-at-property-p ()
11910 "Is the cursor in a property line?"
11911 ;; FIXME: Does not check if we are actually in the drawer.
11912 ;; FIXME: also returns true on any drawers.....
11913 ;; This is used by C-c C-c for property action.
11915 (beginning-of-line 1)
11916 (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
11918 (defun org-get-property-block (&optional beg end force)
11919 "Return the (beg . end) range of the body of the property drawer.
11920 BEG and END can be beginning and end of subtree, if not given
11921 they will be found.
11922 If the drawer does not exist and FORCE is non-nil, create the drawer."
11925 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
11926 (end (or end (progn (outline-next-heading) (point)))))
11928 (if (re-search-forward org-property-start-re end t)
11929 (setq beg (1+ (match-end 0)))
11932 (org-insert-property-drawer)
11933 (setq end (progn (outline-next-heading) (point))))
11936 (if (re-search-forward org-property-start-re end t)
11937 (setq beg (1+ (match-end 0)))))
11938 (if (re-search-forward org-property-end-re end t)
11939 (setq end (match-beginning 0))
11940 (or force (throw 'exit nil))
11943 (org-indent-line-function)
11944 (insert ":END:\n"))
11947 (defun org-entry-properties (&optional pom which)
11948 "Get all properties of the entry at point-or-marker POM.
11949 This includes the TODO keyword, the tags, time strings for deadline,
11950 scheduled, and clocking, and any additional properties defined in the
11951 entry. The return value is an alist, keys may occur multiple times
11952 if the property key was used several times.
11953 POM may also be nil, in which case the current entry is used.
11954 If WHICH is nil or `all', get all properties. If WHICH is
11955 `special' or `standard', only get that subclass."
11956 (setq which (or which 'all))
11957 (org-with-point-at pom
11958 (let ((clockstr (substring org-clock-string 0 -1))
11959 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
11960 beg end range props sum-props key value string clocksum)
11962 (when (condition-case nil
11963 (and (org-mode-p) (org-back-to-heading t))
11966 (setq sum-props (get-text-property (point) 'org-summaries))
11967 (setq clocksum (get-text-property (point) :org-clock-minutes))
11968 (outline-next-heading)
11970 (when (memq which '(all special))
11971 ;; Get the special properties, like TODO and tags
11973 (when (and (looking-at org-todo-line-regexp) (match-end 2))
11974 (push (cons "TODO" (org-match-string-no-properties 2)) props))
11975 (when (looking-at org-priority-regexp)
11976 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
11977 (when (and (setq value (org-get-tags-string))
11978 (string-match "\\S-" value))
11979 (push (cons "TAGS" value) props))
11980 (when (setq value (org-get-tags-at))
11981 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
11983 (while (re-search-forward org-maybe-keyword-time-regexp end t)
11984 (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
11985 string (if (equal key clockstr)
11989 (match-beginning 3) (goto-char (point-at-eol)))))
11990 (substring (org-match-string-no-properties 3) 1 -1)))
11992 (if (= (char-after (match-beginning 3)) ?\[)
11993 (setq key "TIMESTAMP_IA")
11994 (setq key "TIMESTAMP")))
11995 (when (or (equal key clockstr) (not (assoc key props)))
11996 (push (cons key string) props)))
12000 (when (memq which '(all standard))
12001 ;; Get the standard properties, like :PROP: ...
12002 (setq range (org-get-property-block beg end))
12004 (goto-char (car range))
12005 (while (re-search-forward
12006 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
12008 (setq key (org-match-string-no-properties 1)
12009 value (org-trim (or (org-match-string-no-properties 2) "")))
12010 (unless (member key excluded)
12011 (push (cons key (or value "")) props)))))
12013 (push (cons "CLOCKSUM"
12014 (org-columns-number-to-string (/ (float clocksum) 60.)
12017 (unless (assoc "CATEGORY" props)
12018 (setq value (or (org-get-category)
12019 (progn (org-refresh-category-properties)
12020 (org-get-category))))
12021 (push (cons "CATEGORY" value) props))
12022 (append sum-props (nreverse props)))))))
12024 (defun org-entry-get (pom property &optional inherit)
12025 "Get value of PROPERTY for entry at point-or-marker POM.
12026 If INHERIT is non-nil and the entry does not have the property,
12027 then also check higher levels of the hierarchy.
12028 If INHERIT is the symbol `selective', use inheritance only if the setting
12029 in `org-use-property-inheritance' selects PROPERTY for inheritance.
12030 If the property is present but empty, the return value is the empty string.
12031 If the property is not present at all, nil is returned."
12032 (org-with-point-at pom
12033 (if (and inherit (if (eq inherit 'selective)
12034 (org-property-inherit-p property)
12036 (org-entry-get-with-inheritance property)
12037 (if (member property org-special-properties)
12038 ;; We need a special property. Use brute force, get all properties.
12039 (cdr (assoc property (org-entry-properties nil 'special)))
12040 (let ((range (org-get-property-block)))
12042 (goto-char (car range))
12044 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
12046 ;; Found the property, return it.
12048 (org-match-string-no-properties 1)
12051 (defun org-property-or-variable-value (var &optional inherit)
12052 "Check if there is a property fixing the value of VAR.
12053 If yes, return this value. If not, return the current value of the variable."
12054 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
12055 (if (and prop (stringp prop) (string-match "\\S-" prop))
12057 (symbol-value var))))
12059 (defun org-entry-delete (pom property)
12060 "Delete the property PROPERTY from entry at point-or-marker POM."
12061 (org-with-point-at pom
12062 (if (member property org-special-properties)
12063 nil ; cannot delete these properties.
12064 (let ((range (org-get-property-block)))
12066 (goto-char (car range))
12068 (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
12071 (delete-region (match-beginning 0) (1+ (point-at-eol)))
12075 ;; Multi-values properties are properties that contain multiple values
12076 ;; These values are assumed to be single words, separated by whitespace.
12077 (defun org-entry-add-to-multivalued-property (pom property value)
12078 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
12079 (let* ((old (org-entry-get pom property))
12080 (values (and old (org-split-string old "[ \t]"))))
12081 (setq value (org-entry-protect-space value))
12082 (unless (member value values)
12083 (setq values (cons value values))
12084 (org-entry-put pom property
12085 (mapconcat 'identity values " ")))))
12087 (defun org-entry-remove-from-multivalued-property (pom property value)
12088 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
12089 (let* ((old (org-entry-get pom property))
12090 (values (and old (org-split-string old "[ \t]"))))
12091 (setq value (org-entry-protect-space value))
12092 (when (member value values)
12093 (setq values (delete value values))
12094 (org-entry-put pom property
12095 (mapconcat 'identity values " ")))))
12097 (defun org-entry-member-in-multivalued-property (pom property value)
12098 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
12099 (let* ((old (org-entry-get pom property))
12100 (values (and old (org-split-string old "[ \t]"))))
12101 (setq value (org-entry-protect-space value))
12102 (member value values)))
12104 (defun org-entry-get-multivalued-property (pom property)
12105 "Return a list of values in a multivalued property."
12106 (let* ((value (org-entry-get pom property))
12107 (values (and value (org-split-string value "[ \t]"))))
12108 (mapcar 'org-entry-restore-space values)))
12110 (defun org-entry-put-multivalued-property (pom property &rest values)
12111 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
12112 VALUES should be a list of strings. Spaces will be protected."
12113 (org-entry-put pom property
12114 (mapconcat 'org-entry-protect-space values " "))
12115 (let* ((value (org-entry-get pom property))
12116 (values (and value (org-split-string value "[ \t]"))))
12117 (mapcar 'org-entry-restore-space values)))
12119 (defun org-entry-protect-space (s)
12120 "Protect spaces and newline in string S."
12121 (while (string-match " " s)
12122 (setq s (replace-match "%20" t t s)))
12123 (while (string-match "\n" s)
12124 (setq s (replace-match "%0A" t t s)))
12127 (defun org-entry-restore-space (s)
12128 "Restore spaces and newline in string S."
12129 (while (string-match "%20" s)
12130 (setq s (replace-match " " t t s)))
12131 (while (string-match "%0A" s)
12132 (setq s (replace-match "\n" t t s)))
12135 (defvar org-entry-property-inherited-from (make-marker)
12136 "Marker pointing to the entry from where a property was inherited.
12137 Each call to `org-entry-get-with-inheritance' will set this marker to the
12138 location of the entry where the inheritance search matched. If there was
12139 no match, the marker will point nowhere.
12140 Note that also `org-entry-get' calls this function, if the INHERIT flag
12143 (defun org-entry-get-with-inheritance (property)
12144 "Get entry property, and search higher levels if not present."
12145 (move-marker org-entry-property-inherited-from nil)
12152 (when (setq tmp (org-entry-get nil property))
12153 (org-back-to-heading t)
12154 (move-marker org-entry-property-inherited-from (point))
12156 (or (org-up-heading-safe) (throw 'ex nil)))))
12158 (cdr (assoc property org-file-properties))
12159 (cdr (assoc property org-global-properties))
12160 (cdr (assoc property org-global-properties-fixed))))))
12162 (defun org-entry-put (pom property value)
12163 "Set PROPERTY to VALUE for entry at point-or-marker POM."
12164 (org-with-point-at pom
12165 (org-back-to-heading t)
12166 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
12169 ((equal property "TODO")
12170 (when (and (stringp value) (string-match "\\S-" value)
12171 (not (member value org-todo-keywords-1)))
12172 (error "\"%s\" is not a valid TODO state" value))
12173 (if (or (not value)
12174 (not (string-match "\\S-" value)))
12175 (setq value 'none))
12177 (org-set-tags nil 'align))
12178 ((equal property "PRIORITY")
12179 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
12180 (string-to-char value) ?\ ))
12181 (org-set-tags nil 'align))
12182 ((equal property "SCHEDULED")
12183 (if (re-search-forward org-scheduled-time-regexp end t)
12185 ((eq value 'earlier) (org-timestamp-change -1 'day))
12186 ((eq value 'later) (org-timestamp-change 1 'day))
12187 (t (call-interactively 'org-schedule)))
12188 (call-interactively 'org-schedule)))
12189 ((equal property "DEADLINE")
12190 (if (re-search-forward org-deadline-time-regexp end t)
12192 ((eq value 'earlier) (org-timestamp-change -1 'day))
12193 ((eq value 'later) (org-timestamp-change 1 'day))
12194 (t (call-interactively 'org-deadline)))
12195 (call-interactively 'org-deadline)))
12196 ((member property org-special-properties)
12197 (error "The %s property can not yet be set with `org-entry-put'"
12199 (t ; a non-special property
12200 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
12201 (setq range (org-get-property-block beg end 'force))
12202 (goto-char (car range))
12203 (if (re-search-forward
12204 (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
12206 (delete-region (match-beginning 1) (match-end 1))
12207 (goto-char (match-beginning 1)))
12208 (goto-char (cdr range))
12211 (org-indent-line-function)
12212 (insert ":" property ":"))
12213 (and value (insert " " value))
12214 (org-indent-line-function)))))))
12216 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
12217 "Get all property keys in the current buffer.
12218 With INCLUDE-SPECIALS, also list the special properties that reflect things
12219 like tags and TODO state.
12220 With INCLUDE-DEFAULTS, also include properties that has special meaning
12221 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
12222 With INCLUDE-COLUMNS, also include property names given in COLUMN
12223 formats in the current buffer."
12224 (let (rtn range cfmt s p)
12228 (goto-char (point-min))
12229 (while (re-search-forward org-property-start-re nil t)
12230 (setq range (org-get-property-block))
12231 (goto-char (car range))
12232 (while (re-search-forward
12233 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
12235 (add-to-list 'rtn (org-match-string-no-properties 1)))
12236 (outline-next-heading))))
12238 (when include-specials
12239 (setq rtn (append org-special-properties rtn)))
12241 (when include-defaults
12242 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
12243 (add-to-list 'rtn org-effort-property))
12245 (when include-columns
12249 (goto-char (point-min))
12250 (while (re-search-forward
12251 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
12253 (setq cfmt (match-string 2) s 0)
12254 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
12256 (setq s (match-end 0)
12257 p (match-string 1 cfmt))
12258 (unless (or (equal p "ITEM")
12259 (member p org-special-properties))
12260 (add-to-list 'rtn (match-string 1 cfmt))))))))
12262 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
12264 (defun org-property-values (key)
12265 "Return a list of all values of property KEY."
12269 (goto-char (point-min))
12270 (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
12272 (while (re-search-forward re nil t)
12273 (add-to-list 'values (org-trim (match-string 1))))
12274 (delete "" values)))))
12276 (defun org-insert-property-drawer ()
12277 "Insert a property drawer into the current entry."
12279 (org-back-to-heading t)
12280 (looking-at outline-regexp)
12281 (let ((indent (if org-adapt-indentation
12282 (- (match-end 0)(match-beginning 0))
12285 (re (concat "^[ \t]*" org-keyword-time-regexp))
12287 (outline-next-heading)
12290 (while (re-search-forward re end t))
12291 (setq hiddenp (org-invisible-p))
12293 (and (equal (char-after) ?\n) (forward-char 1))
12294 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
12295 (if (member (match-string 1) '("CLOCK:" ":END:"))
12296 ;; just skip this line
12297 (beginning-of-line 2)
12298 ;; Drawer start, find the end
12299 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
12300 (beginning-of-line 1)))
12301 (org-skip-over-state-notes)
12302 (skip-chars-backward " \t\n\r")
12303 (if (eq (char-before) ?*) (forward-char 1))
12304 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
12305 (beginning-of-line 0)
12306 (org-indent-to-column indent)
12307 (beginning-of-line 2)
12308 (org-indent-to-column indent)
12309 (beginning-of-line 0)
12312 (org-back-to-heading t)
12314 (org-flag-drawer t))))
12316 (defun org-set-property (property value)
12317 "In the current entry, set PROPERTY to VALUE.
12318 When called interactively, this will prompt for a property name, offering
12319 completion on existing and default properties. And then it will prompt
12320 for a value, offering completion either on allowed values (via an inherited
12321 xxx_ALL property) or on existing values in other instances of this property
12322 in the current file."
12324 (let* ((completion-ignore-case t)
12325 (keys (org-buffer-property-keys nil t t))
12326 (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
12327 (prop (if (member prop0 keys)
12329 (or (cdr (assoc (downcase prop0)
12330 (mapcar (lambda (x) (cons (downcase x) x))
12333 (cur (org-entry-get nil prop))
12334 (allowed (org-property-get-allowed-values nil prop 'table))
12335 (existing (mapcar 'list (org-property-values prop)))
12337 (org-completing-read "Value: " allowed nil 'req-match)
12338 (let (org-completion-use-ido org-completion-use-iswitchb)
12339 (org-completing-read
12340 (concat "Value " (if (and cur (string-match "\\S-" cur))
12341 (concat "[" cur "]") "")
12343 existing nil nil "" nil cur)))))
12344 (list prop (if (equal val "") cur val))))
12345 (unless (equal (org-entry-get nil property) value)
12346 (org-entry-put nil property value)))
12348 (defun org-delete-property (property)
12349 "In the current entry, delete PROPERTY."
12351 (let* ((completion-ignore-case t)
12352 (prop (org-icompleting-read
12353 "Property: " (org-entry-properties nil 'standard))))
12355 (message "Property %s %s" property
12356 (if (org-entry-delete nil property)
12358 "was not present in the entry")))
12360 (defun org-delete-property-globally (property)
12361 "Remove PROPERTY globally, from all entries."
12363 (let* ((completion-ignore-case t)
12364 (prop (org-icompleting-read
12365 "Globally remove property: "
12366 (mapcar 'list (org-buffer-property-keys)))))
12371 (goto-char (point-min))
12373 (while (re-search-forward
12374 (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
12376 (setq cnt (1+ cnt))
12377 (replace-match ""))
12378 (message "Property \"%s\" removed from %d entries" property cnt)))))
12380 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
12382 (defun org-compute-property-at-point ()
12383 "Compute the property at point.
12384 This looks for an enclosing column format, extracts the operator and
12385 then applies it to the property in the column format's scope."
12387 (unless (org-at-property-p)
12388 (error "Not at a property"))
12389 (let ((prop (org-match-string-no-properties 2)))
12390 (org-columns-get-format-and-top-level)
12391 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
12392 (error "No operator defined for property %s" prop))
12393 (org-columns-compute prop)))
12395 (defun org-property-get-allowed-values (pom property &optional table)
12396 "Get allowed values for the property PROPERTY.
12397 When TABLE is non-nil, return an alist that can directly be used for
12401 ((equal property "TODO")
12402 (setq vals (org-with-point-at pom
12403 (append org-todo-keywords-1 '("")))))
12404 ((equal property "PRIORITY")
12405 (let ((n org-lowest-priority))
12406 (while (>= n org-highest-priority)
12407 (push (char-to-string n) vals)
12409 ((member property org-special-properties))
12411 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
12413 (when (and vals (string-match "\\S-" vals))
12414 (setq vals (car (read-from-string (concat "(" vals ")"))))
12415 (setq vals (mapcar (lambda (x)
12416 (cond ((stringp x) x)
12417 ((numberp x) (number-to-string x))
12418 ((symbolp x) (symbol-name x))
12421 (if table (mapcar 'list vals) vals)))
12423 (defun org-property-previous-allowed-value (&optional previous)
12424 "Switch to the next allowed value for this property."
12426 (org-property-next-allowed-value t))
12428 (defun org-property-next-allowed-value (&optional previous)
12429 "Switch to the next allowed value for this property."
12431 (unless (org-at-property-p)
12432 (error "Not at a property"))
12433 (let* ((key (match-string 2))
12434 (value (match-string 3))
12435 (allowed (or (org-property-get-allowed-values (point) key)
12436 (and (member value '("[ ]" "[-]" "[X]"))
12440 (error "Allowed values for this property have not been defined"))
12441 (if previous (setq allowed (reverse allowed)))
12442 (if (member value allowed)
12443 (setq nval (car (cdr (member value allowed)))))
12444 (setq nval (or nval (car allowed)))
12445 (if (equal nval value)
12446 (error "Only one allowed value for this property"))
12447 (org-at-property-p)
12448 (replace-match (concat " :" key ": " nval) t t)
12449 (org-indent-line-function)
12450 (beginning-of-line 1)
12451 (skip-chars-forward " \t")))
12453 (defun org-find-entry-with-id (ident)
12454 "Locate the entry that contains the ID property with exact value IDENT.
12455 IDENT can be a string, a symbol or a number, this function will search for
12456 the string representation of it.
12457 Return the position where this entry starts, or nil if there is no such entry."
12458 (interactive "sID: ")
12460 ((stringp ident) ident)
12461 ((symbol-name ident) (symbol-name ident))
12462 ((numberp ident) (number-to-string ident))
12463 (t (error "IDENT %s must be a string, symbol or number" ident))))
12464 (case-fold-search nil))
12468 (goto-char (point-min))
12469 (when (re-search-forward
12470 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
12472 (org-back-to-heading t)
12477 (defvar org-last-changed-timestamp nil)
12478 (defvar org-last-inserted-timestamp nil
12479 "The last time stamp inserted with `org-insert-time-stamp'.")
12480 (defvar org-time-was-given) ; dynamically scoped parameter
12481 (defvar org-end-time-was-given) ; dynamically scoped parameter
12482 (defvar org-ts-what) ; dynamically scoped parameter
12484 (defun org-time-stamp (arg &optional inactive)
12485 "Prompt for a date/time and insert a time stamp.
12486 If the user specifies a time like HH:MM, or if this command is called
12487 with a prefix argument, the time stamp will contain date and time.
12488 Otherwise, only the date will be included. All parts of a date not
12489 specified by the user will be filled in from the current date/time.
12490 So if you press just return without typing anything, the time stamp
12491 will represent the current date/time. If there is already a timestamp
12492 at the cursor, it will be modified."
12496 ;; Default time is either today, or, when entering a range,
12497 ;; the range start.
12498 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
12500 (re-search-backward
12501 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
12502 (- (point) 20) t)))
12503 (apply 'encode-time (org-parse-time-string (match-string 1)))
12505 (default-input (and ts (org-get-compact-tod ts)))
12506 org-time-was-given org-end-time-was-given time)
12508 ((and (org-at-timestamp-p t)
12509 (memq last-command '(org-time-stamp org-time-stamp-inactive))
12510 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
12512 (setq time (let ((this-command this-command))
12513 (org-read-date arg 'totime nil nil
12514 default-time default-input)))
12515 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
12516 ((org-at-timestamp-p t)
12517 (setq time (let ((this-command this-command))
12518 (org-read-date arg 'totime nil nil default-time default-input)))
12519 (when (org-at-timestamp-p t) ; just to get the match data
12520 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
12522 (setq org-last-changed-timestamp
12523 (org-insert-time-stamp
12524 time (or org-time-was-given arg)
12525 inactive nil nil (list org-end-time-was-given))))
12526 (message "Timestamp updated"))
12528 (setq time (let ((this-command this-command))
12529 (org-read-date arg 'totime nil nil default-time default-input)))
12530 (org-insert-time-stamp time (or org-time-was-given arg) inactive
12531 nil nil (list org-end-time-was-given))))))
12533 ;; FIXME: can we use this for something else, like computing time differences?
12534 (defun org-get-compact-tod (s)
12535 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
12536 (let* ((t1 (match-string 1 s))
12537 (h1 (string-to-number (match-string 2 s)))
12538 (m1 (string-to-number (match-string 3 s)))
12539 (t2 (and (match-end 4) (match-string 5 s)))
12540 (h2 (and t2 (string-to-number (match-string 6 s))))
12541 (m2 (and t2 (string-to-number (match-string 7 s))))
12545 (setq dh (- h2 h1) dm (- m2 m1))
12546 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
12547 (concat t1 "+" (number-to-string dh)
12548 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
12550 (defun org-time-stamp-inactive (&optional arg)
12551 "Insert an inactive time stamp.
12552 An inactive time stamp is enclosed in square brackets instead of angle
12553 brackets. It is inactive in the sense that it does not trigger agenda entries,
12554 does not link to the calendar and cannot be changed with the S-cursor keys.
12555 So these are more for recording a certain time/date."
12557 (org-time-stamp arg 'inactive))
12559 (defvar org-date-ovl (org-make-overlay 1 1))
12560 (org-overlay-put org-date-ovl 'face 'org-warning)
12561 (org-detach-overlay org-date-ovl)
12563 (defvar org-ans1) ; dynamically scoped parameter
12564 (defvar org-ans2) ; dynamically scoped parameter
12566 (defvar org-plain-time-of-day-regexp) ; defined below
12568 (defvar org-overriding-default-time nil) ; dynamically scoped
12569 (defvar org-read-date-overlay nil)
12570 (defvar org-dcst nil) ; dynamically scoped
12571 (defvar org-read-date-history nil)
12572 (defvar org-read-date-final-answer nil)
12574 (defun org-read-date (&optional with-time to-time from-string prompt
12575 default-time default-input)
12576 "Read a date, possibly a time, and make things smooth for the user.
12577 The prompt will suggest to enter an ISO date, but you can also enter anything
12578 which will at least partially be understood by `parse-time-string'.
12579 Unrecognized parts of the date will default to the current day, month, year,
12580 hour and minute. If this command is called to replace a timestamp at point,
12581 of to enter the second timestamp of a range, the default time is taken from the
12582 existing stamp. For example,
12583 3-2-5 --> 2003-02-05
12584 feb 15 --> currentyear-02-15
12585 sep 12 9 --> 2009-09-12
12586 12:45 --> today 12:45
12587 22 sept 0:34 --> currentyear-09-22 0:34
12588 12 --> currentyear-currentmonth-12
12589 Fri --> nearest Friday (today or later)
12592 Furthermore you can specify a relative date by giving, as the *first* thing
12593 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
12594 change in days weeks, months, years.
12595 With a single plus or minus, the date is relative to today. With a double
12596 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
12597 +4d --> four days from today
12598 +4 --> same as above
12599 +2w --> two weeks from today
12600 ++5 --> five days from default date
12602 The function understands only English month and weekday abbreviations,
12603 but this can be configured with the variables `parse-time-months' and
12604 `parse-time-weekdays'.
12606 While prompting, a calendar is popped up - you can also select the
12607 date with the mouse (button 1). The calendar shows a period of three
12608 months. To scroll it to other months, use the keys `>' and `<'.
12609 If you don't like the calendar, turn it off with
12610 \(setq org-read-date-popup-calendar nil)
12612 With optional argument TO-TIME, the date will immediately be converted
12613 to an internal time.
12614 With an optional argument WITH-TIME, the prompt will suggest to also
12615 insert a time. Note that when WITH-TIME is not set, you can still
12616 enter a time, and this function will inform the calling routine about
12617 this change. The calling routine may then choose to change the format
12618 used to insert the time stamp into the buffer to include the time.
12619 With optional argument FROM-STRING, read from this string instead from
12620 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
12621 the time/date that is used for everything that is not specified by the
12623 (require 'parse-time)
12624 (let* ((org-time-stamp-rounding-minutes
12625 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
12626 (org-dcst org-display-custom-times)
12627 (ct (org-current-time))
12628 (def (or org-overriding-default-time default-time ct))
12629 (defdecode (decode-time def))
12631 (when (< (nth 2 defdecode) org-extend-today-until)
12632 (setcar (nthcdr 2 defdecode) -1)
12633 (setcar (nthcdr 1 defdecode) 59)
12634 (setq def (apply 'encode-time defdecode)
12635 defdecode (decode-time def)))))
12636 (calendar-frame-setup nil)
12637 (calendar-move-hook nil)
12638 (calendar-view-diary-initially-flag nil)
12639 (view-diary-entries-initially nil)
12640 (calendar-view-holidays-initially-flag nil)
12641 (view-calendar-holidays-initially nil)
12642 (timestr (format-time-string
12643 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
12644 (prompt (concat (if prompt (concat prompt " ") "")
12645 (format "Date+time [%s]: " timestr)))
12646 ans (org-ans0 "") org-ans1 org-ans2 final)
12649 (from-string (setq ans from-string))
12650 (org-read-date-popup-calendar
12652 (save-window-excursion
12654 (calendar-forward-day (- (time-to-days def)
12655 (calendar-absolute-from-gregorian
12656 (calendar-current-date))))
12657 (org-eval-in-calendar nil t)
12658 (let* ((old-map (current-local-map))
12659 (map (copy-keymap calendar-mode-map))
12660 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
12661 (org-defkey map (kbd "RET") 'org-calendar-select)
12662 (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
12663 'org-calendar-select-mouse)
12664 (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2
])
12665 'org-calendar-select-mouse
)
12666 (org-defkey minibuffer-local-map
[(meta shift left
)]
12667 (lambda () (interactive)
12668 (org-eval-in-calendar '(calendar-backward-month 1))))
12669 (org-defkey minibuffer-local-map
[(meta shift right
)]
12670 (lambda () (interactive)
12671 (org-eval-in-calendar '(calendar-forward-month 1))))
12672 (org-defkey minibuffer-local-map
[(meta shift up
)]
12673 (lambda () (interactive)
12674 (org-eval-in-calendar '(calendar-backward-year 1))))
12675 (org-defkey minibuffer-local-map
[(meta shift down
)]
12676 (lambda () (interactive)
12677 (org-eval-in-calendar '(calendar-forward-year 1))))
12678 (org-defkey minibuffer-local-map
[?\e
(shift left
)]
12679 (lambda () (interactive)
12680 (org-eval-in-calendar '(calendar-backward-month 1))))
12681 (org-defkey minibuffer-local-map
[?\e
(shift right
)]
12682 (lambda () (interactive)
12683 (org-eval-in-calendar '(calendar-forward-month 1))))
12684 (org-defkey minibuffer-local-map
[?\e
(shift up
)]
12685 (lambda () (interactive)
12686 (org-eval-in-calendar '(calendar-backward-year 1))))
12687 (org-defkey minibuffer-local-map
[?\e
(shift down
)]
12688 (lambda () (interactive)
12689 (org-eval-in-calendar '(calendar-forward-year 1))))
12690 (org-defkey minibuffer-local-map
[(shift up
)]
12691 (lambda () (interactive)
12692 (org-eval-in-calendar '(calendar-backward-week 1))))
12693 (org-defkey minibuffer-local-map
[(shift down
)]
12694 (lambda () (interactive)
12695 (org-eval-in-calendar '(calendar-forward-week 1))))
12696 (org-defkey minibuffer-local-map
[(shift left
)]
12697 (lambda () (interactive)
12698 (org-eval-in-calendar '(calendar-backward-day 1))))
12699 (org-defkey minibuffer-local-map
[(shift right
)]
12700 (lambda () (interactive)
12701 (org-eval-in-calendar '(calendar-forward-day 1))))
12702 (org-defkey minibuffer-local-map
">"
12703 (lambda () (interactive)
12704 (org-eval-in-calendar '(scroll-calendar-left 1))))
12705 (org-defkey minibuffer-local-map
"<"
12706 (lambda () (interactive)
12707 (org-eval-in-calendar '(scroll-calendar-right 1))))
12708 (run-hooks 'org-read-date-minibuffer-setup-hook
)
12711 (use-local-map map
)
12712 (add-hook 'post-command-hook
'org-read-date-display
)
12713 (setq org-ans0
(read-string prompt default-input
12714 'org-read-date-history nil
))
12715 ;; org-ans0: from prompt
12716 ;; org-ans1: from mouse click
12717 ;; org-ans2: from calendar motion
12718 (setq ans
(concat org-ans0
" " (or org-ans1 org-ans2
))))
12719 (remove-hook 'post-command-hook
'org-read-date-display
)
12720 (use-local-map old-map
)
12721 (when org-read-date-overlay
12722 (org-delete-overlay org-read-date-overlay
)
12723 (setq org-read-date-overlay nil
)))))))
12725 (t ; Naked prompt only
12727 (setq ans
(read-string prompt default-input
12728 'org-read-date-history timestr
))
12729 (when org-read-date-overlay
12730 (org-delete-overlay org-read-date-overlay
)
12731 (setq org-read-date-overlay nil
)))))
12733 (setq final
(org-read-date-analyze ans def defdecode
))
12734 (setq org-read-date-final-answer ans
)
12737 (apply 'encode-time final
)
12738 (if (and (boundp 'org-time-was-given
) org-time-was-given
)
12739 (format "%04d-%02d-%02d %02d:%02d"
12740 (nth 5 final
) (nth 4 final
) (nth 3 final
)
12741 (nth 2 final
) (nth 1 final
))
12742 (format "%04d-%02d-%02d" (nth 5 final
) (nth 4 final
) (nth 3 final
))))))
12747 (defvar org-read-date-analyze-futurep nil
)
12748 (defun org-read-date-display ()
12749 "Display the current date prompt interpretation in the minibuffer."
12750 (when org-read-date-display-live
12751 (when org-read-date-overlay
12752 (org-delete-overlay org-read-date-overlay
))
12755 (while (not (equal (buffer-substring
12756 (max (point-min) (- (point) 4)) (point))
12760 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
12761 " " (or org-ans1 org-ans2
)))
12762 (org-end-time-was-given nil
)
12763 (f (org-read-date-analyze ans def defdecode
))
12765 org-time-stamp-custom-formats
12766 org-time-stamp-formats
))
12767 (fmt (if (or with-time
12768 (and (boundp 'org-time-was-given
) org-time-was-given
))
12771 (txt (concat "=> " (format-time-string fmt
(apply 'encode-time f
)))))
12772 (when (and org-end-time-was-given
12773 (string-match org-plain-time-of-day-regexp txt
))
12774 (setq txt
(concat (substring txt
0 (match-end 0)) "-"
12775 org-end-time-was-given
12776 (substring txt
(match-end 0)))))
12777 (when org-read-date-analyze-futurep
12778 (setq txt
(concat txt
" (=>F)")))
12779 (setq org-read-date-overlay
12780 (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
12781 (org-overlay-display org-read-date-overlay txt
'secondary-selection
))))
12783 (defun org-read-date-analyze (ans def defdecode
)
12784 "Analyse the combined answer of the date prompt."
12785 ;; FIXME: cleanup and comment
12786 (let (delta deltan deltaw deltadef year month day
12787 hour minute second wday pm h2 m2 tl wday1
12788 iso-year iso-weekday iso-week iso-year iso-date futurep
)
12789 (setq org-read-date-analyze-futurep nil
)
12790 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans
)
12793 (when (setq delta
(org-read-date-get-relative ans
(current-time) def
))
12794 (setq ans
(replace-match "" t t ans
)
12796 deltaw
(nth 1 delta
)
12797 deltadef
(nth 2 delta
)))
12799 ;; Check if there is an iso week date in there
12800 ;; If yes, sore the info and postpone interpreting it until the rest
12801 ;; of the parsing is done
12802 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans
)
12803 (setq iso-year
(if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans
))))
12804 iso-weekday
(if (match-end 3) (string-to-number (match-string 3 ans
)))
12805 iso-week
(string-to-number (match-string 2 ans
)))
12806 (setq ans
(replace-match "" t t ans
)))
12808 ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
12809 (when (string-match
12810 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans
)
12811 (setq year
(if (match-end 2)
12812 (string-to-number (match-string 2 ans
))
12813 (string-to-number (format-time-string "%Y")))
12814 month
(string-to-number (match-string 3 ans
))
12815 day
(string-to-number (match-string 4 ans
)))
12816 (if (< year
100) (setq year
(+ 2000 year
)))
12817 (setq ans
(replace-match (format "%04d-%02d-%02d\\5" year month day
)
12819 ;; Help matching am/pm times, because `parse-time-string' does not do that.
12820 ;; If there is a time with am/pm, and *no* time without it, we convert
12821 ;; so that matching will be successful.
12822 (loop for i from
1 to
2 do
; twice, for end time as well
12823 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans
))
12824 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans
))
12825 (setq hour
(string-to-number (match-string 1 ans
))
12826 minute
(if (match-end 3)
12827 (string-to-number (match-string 3 ans
))
12830 (string-to-char (downcase (match-string 4 ans
)))))
12831 (if (and (= hour
12) (not pm
))
12833 (if (and pm
(< hour
12)) (setq hour
(+ 12 hour
))))
12834 (setq ans
(replace-match (format "%02d:%02d" hour minute
)
12837 ;; Check if a time range is given as a duration
12838 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans
)
12839 (setq hour
(string-to-number (match-string 1 ans
))
12840 h2
(+ hour
(string-to-number (match-string 3 ans
)))
12841 minute
(string-to-number (match-string 2 ans
))
12842 m2
(+ minute
(if (match-end 5) (string-to-number
12843 (match-string 5 ans
))0)))
12844 (if (>= m2
60) (setq h2
(1+ h2
) m2
(- m2
60)))
12845 (setq ans
(replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2
)
12848 ;; Check if there is a time range
12849 (when (boundp 'org-end-time-was-given
)
12850 (setq org-time-was-given nil
)
12851 (when (and (string-match org-plain-time-of-day-regexp ans
)
12853 (setq org-end-time-was-given
(match-string 8 ans
))
12854 (setq ans
(concat (substring ans
0 (match-beginning 7))
12855 (substring ans
(match-end 7))))))
12857 (setq tl
(parse-time-string ans
)
12858 day
(or (nth 3 tl
) (nth 3 defdecode
))
12859 month
(or (nth 4 tl
)
12860 (if (and org-read-date-prefer-future
12861 (nth 3 tl
) (< (nth 3 tl
) (nth 3 defdecode
)))
12862 (prog1 (1+ (nth 4 defdecode
)) (setq futurep t
))
12863 (nth 4 defdecode
)))
12864 year
(or (nth 5 tl
)
12865 (if (and org-read-date-prefer-future
12866 (nth 4 tl
) (< (nth 4 tl
) (nth 4 defdecode
)))
12867 (prog1 (1+ (nth 5 defdecode
)) (setq futurep t
))
12868 (nth 5 defdecode
)))
12869 hour
(or (nth 2 tl
) (nth 2 defdecode
))
12870 minute
(or (nth 1 tl
) (nth 1 defdecode
))
12871 second
(or (nth 0 tl
) 0)
12874 (when (and (eq org-read-date-prefer-future
'time
)
12875 (not (nth 3 tl
)) (not (nth 4 tl
)) (not (nth 5 tl
))
12876 (equal day
(nth 3 defdecode
))
12877 (equal month
(nth 4 defdecode
))
12878 (equal year
(nth 5 defdecode
))
12880 (or (< (nth 2 tl
) (nth 2 defdecode
))
12881 (and (= (nth 2 tl
) (nth 2 defdecode
))
12883 (< (nth 1 tl
) (nth 1 defdecode
)))))
12887 ;; Special date definitions below
12890 ;; There was an iso week
12892 (setq year
(or iso-year year
)
12893 day
(or iso-weekday wday
1)
12894 wday nil
; to make sure that the trigger below does not match
12895 iso-date
(calendar-gregorian-from-absolute
12896 (calendar-absolute-from-iso
12897 (list iso-week day year
))))
12898 ; FIXME: Should we also push ISO weeks into the future?
12899 ; (when (and org-read-date-prefer-future
12901 ; (< (calendar-absolute-from-gregorian iso-date)
12902 ; (time-to-days (current-time))))
12903 ; (setq year (1+ year)
12904 ; iso-date (calendar-gregorian-from-absolute
12905 ; (calendar-absolute-from-iso
12906 ; (list iso-week day year)))))
12907 (setq month
(car iso-date
)
12908 year
(nth 2 iso-date
)
12909 day
(nth 1 iso-date
)))
12913 (let ((now (decode-time (current-time))))
12914 (setq day
(nth 3 now
) month
(nth 4 now
) year
(nth 5 now
))))
12915 (cond ((member deltaw
'("d" "")) (setq day
(+ day deltan
)))
12916 ((equal deltaw
"w") (setq day
(+ day
(* 7 deltan
))))
12917 ((equal deltaw
"m") (setq month
(+ month deltan
)))
12918 ((equal deltaw
"y") (setq year
(+ year deltan
)))))
12919 ((and wday
(not (nth 3 tl
)))
12921 ;; Weekday was given, but no day, so pick that day in the week
12922 ;; on or after the derived date.
12923 (setq wday1
(nth 6 (decode-time (encode-time 0 0 0 day month year
))))
12924 (unless (equal wday wday1
)
12925 (setq day
(+ day
(%
(- wday wday1 -
7) 7))))))
12926 (if (and (boundp 'org-time-was-given
)
12928 (setq org-time-was-given t
))
12929 (if (< year
100) (setq year
(+ 2000 year
)))
12930 (if (< year
1970) (setq year
(nth 5 defdecode
))) ; not representable
12931 (setq org-read-date-analyze-futurep futurep
)
12932 (list second minute hour day month year
)))
12934 (defvar parse-time-weekdays
)
12936 (defun org-read-date-get-relative (s today default
)
12937 "Check string S for special relative date string.
12938 TODAY and DEFAULT are internal times, for today and for a default.
12939 Return shift list (N what def-flag)
12940 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
12941 N is the number of WHATs to shift.
12942 DEF-FLAG is t when a double ++ or -- indicates shift relative to
12943 the DEFAULT date rather than TODAY."
12947 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
12949 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays
"\\|") "\\)\\)?"
12950 "\\([ \t]\\|$\\)") s
)
12951 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
12952 (let* ((dir (if (> (match-end 1) (match-beginning 1))
12953 (string-to-char (substring (match-string 1 s
) -
1))
12955 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
12956 (n (if (match-end 2) (string-to-number (match-string 2 s
)) 1))
12957 (what (if (match-end 3) (match-string 3 s
) "d"))
12958 (wday1 (cdr (assoc (downcase what
) parse-time-weekdays
)))
12959 (date (if rel default today
))
12960 (wday (nth 6 (decode-time date
)))
12964 (setq delta
(mod (+ 7 (- wday1 wday
)) 7))
12965 (if (= dir ?-
) (setq delta
(- delta
7)))
12966 (if (> n
1) (setq delta
(+ delta
(* (1- n
) (if (= dir ?-
) -
7 7)))))
12967 (list delta
"d" rel
))
12968 (list (* n
(if (= dir ?-
) -
1 1)) what rel
)))))
12970 (defun org-eval-in-calendar (form &optional keepdate
)
12971 "Eval FORM in the calendar window and return to current window.
12972 Also, store the cursor date in variable org-ans2."
12973 (let ((sf (selected-frame))
12974 (sw (selected-window)))
12975 (select-window (get-buffer-window "*Calendar*" t
))
12977 (when (and (not keepdate
) (calendar-cursor-to-date))
12978 (let* ((date (calendar-cursor-to-date))
12979 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12980 (setq org-ans2
(format-time-string "%Y-%m-%d" time
))))
12981 (org-move-overlay org-date-ovl
(1- (point)) (1+ (point)) (current-buffer))
12983 (org-select-frame-set-input-focus sf
)))
12985 (defun org-calendar-select ()
12986 "Return to `org-read-date' with the date currently selected.
12987 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
12989 (when (calendar-cursor-to-date)
12990 (let* ((date (calendar-cursor-to-date))
12991 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
12992 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
12993 (if (active-minibuffer-window) (exit-minibuffer))))
12995 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra
)
12996 "Insert a date stamp for the date given by the internal TIME.
12997 WITH-HM means, use the stamp format that includes the time of the day.
12998 INACTIVE means use square brackets instead of angular ones, so that the
12999 stamp will not contribute to the agenda.
13000 PRE and POST are optional strings to be inserted before and after the
13002 The command returns the inserted time stamp."
13003 (let ((fmt (funcall (if with-hm
'cdr
'car
) org-time-stamp-formats
))
13005 (if inactive
(setq fmt
(concat "[" (substring fmt
1 -
1) "]")))
13006 (insert-before-markers (or pre
""))
13007 (insert-before-markers (setq stamp
(format-time-string fmt time
)))
13008 (when (listp extra
)
13009 (setq extra
(car extra
))
13010 (if (and (stringp extra
)
13011 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra
))
13012 (setq extra
(format "-%02d:%02d"
13013 (string-to-number (match-string 1 extra
))
13014 (string-to-number (match-string 2 extra
))))
13018 (insert-before-markers extra
)
13020 (insert-before-markers (or post
""))
13021 (setq org-last-inserted-timestamp stamp
)))
13023 (defun org-toggle-time-stamp-overlays ()
13024 "Toggle the use of custom time stamp formats."
13026 (setq org-display-custom-times
(not org-display-custom-times
))
13027 (unless org-display-custom-times
13028 (let ((p (point-min)) (bmp (buffer-modified-p)))
13029 (while (setq p
(next-single-property-change p
'display
))
13030 (if (and (get-text-property p
'display
)
13031 (eq (get-text-property p
'face
) 'org-date
))
13032 (remove-text-properties
13033 p
(setq p
(next-single-property-change p
'display
))
13035 (set-buffer-modified-p bmp
)))
13036 (if (featurep 'xemacs
)
13037 (remove-text-properties (point-min) (point-max) '(end-glyph t
)))
13038 (org-restart-font-lock)
13039 (setq org-table-may-need-update t
)
13040 (if org-display-custom-times
13041 (message "Time stamps are overlayed with custom format")
13042 (message "Time stamp overlays removed")))
13044 (defun org-display-custom-time (beg end
)
13045 "Overlay modified time stamp format over timestamp between BEG and END."
13046 (let* ((ts (buffer-substring beg end
))
13047 t1 w1 with-hm tf time str w2
(off 0))
13049 (setq t1
(org-parse-time-string ts t
))
13050 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts
)
13051 (setq off
(- (match-end 0) (match-beginning 0)))))
13052 (setq end
(- end off
))
13053 (setq w1
(- end beg
)
13054 with-hm
(and (nth 1 t1
) (nth 2 t1
))
13055 tf
(funcall (if with-hm
'cdr
'car
) org-time-stamp-custom-formats
)
13056 time
(org-fix-decoded-time t1
)
13058 (format-time-string
13059 (substring tf
1 -
1) (apply 'encode-time time
))
13060 nil
'mouse-face
'highlight
)
13062 (if (not (= w2 w1
))
13063 (add-text-properties (1+ beg
) (+ 2 beg
)
13064 (list 'org-dwidth t
'org-dwidth-n
(- w1 w2
))))
13065 (if (featurep 'xemacs
)
13067 (put-text-property beg end
'invisible t
)
13068 (put-text-property beg end
'end-glyph
(make-glyph str
)))
13069 (put-text-property beg end
'display str
))))
13071 (defun org-translate-time (string)
13072 "Translate all timestamps in STRING to custom format.
13073 But do this only if the variable `org-display-custom-times' is set."
13074 (when org-display-custom-times
13077 (re org-ts-regexp-both
)
13078 t1 with-hm inactive tf time str beg end
)
13079 (while (setq start
(string-match re string start
))
13080 (setq beg
(match-beginning 0)
13082 t1
(save-match-data
13083 (org-parse-time-string (substring string beg end
) t
))
13084 with-hm
(and (nth 1 t1
) (nth 2 t1
))
13085 inactive
(equal (substring string beg
(1+ beg
)) "[")
13086 tf
(funcall (if with-hm
'cdr
'car
)
13087 org-time-stamp-custom-formats
)
13088 time
(org-fix-decoded-time t1
)
13089 str
(format-time-string
13091 (if inactive
"[" "<") (substring tf
1 -
1)
13092 (if inactive
"]" ">"))
13093 (apply 'encode-time time
))
13094 string
(replace-match str t t string
)
13095 start
(+ start
(length str
)))))))
13098 (defun org-fix-decoded-time (time)
13099 "Set 0 instead of nil for the first 6 elements of time.
13100 Don't touch the rest."
13102 (mapcar (lambda (x) (if (< (setq n
(1+ n
)) 7) (or x
0) x
)) time
)))
13104 (defun org-days-to-time (timestamp-string)
13105 "Difference between TIMESTAMP-STRING and now in days."
13106 (- (time-to-days (org-time-string-to-time timestamp-string
))
13107 (time-to-days (current-time))))
13109 (defun org-deadline-close (timestamp-string &optional ndays
)
13110 "Is the time in TIMESTAMP-STRING close to the current date?"
13111 (setq ndays
(or ndays
(org-get-wdays timestamp-string
)))
13112 (and (< (org-days-to-time timestamp-string
) ndays
)
13113 (not (org-entry-is-done-p))))
13115 (defun org-get-wdays (ts)
13116 "Get the deadline lead time appropriate for timestring TS."
13118 ((<= org-deadline-warning-days
0)
13119 ;; 0 or negative, enforce this value no matter what
13120 (- org-deadline-warning-days
))
13121 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts
)
13122 ;; lead time is specified.
13123 (floor (* (string-to-number (match-string 1 ts
))
13124 (cdr (assoc (match-string 2 ts
)
13125 '(("d" .
1) ("w" .
7)
13126 ("m" .
30.4) ("y" .
365.25)))))))
13127 ;; go for the default.
13128 (t org-deadline-warning-days
)))
13130 (defun org-calendar-select-mouse (ev)
13131 "Return to `org-read-date' with the date currently selected.
13132 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
13134 (mouse-set-point ev
)
13135 (when (calendar-cursor-to-date)
13136 (let* ((date (calendar-cursor-to-date))
13137 (time (encode-time 0 0 0 (nth 1 date
) (nth 0 date
) (nth 2 date
))))
13138 (setq org-ans1
(format-time-string "%Y-%m-%d" time
)))
13139 (if (active-minibuffer-window) (exit-minibuffer))))
13141 (defun org-check-deadlines (ndays)
13142 "Check if there are any deadlines due or past due.
13143 A deadline is considered due if it happens within `org-deadline-warning-days'
13144 days from today's date. If the deadline appears in an entry marked DONE,
13145 it is not shown. The prefix arg NDAYS can be used to test that many
13146 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
13148 (let* ((org-warn-days
13150 ((equal ndays
'(4)) 100000)
13151 (ndays (prefix-numeric-value ndays
))
13152 (t (abs org-deadline-warning-days
))))
13153 (case-fold-search nil
)
13154 (regexp (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>"))
13156 (lambda () (org-deadline-close (match-string 1) org-warn-days
))))
13158 (message "%d deadlines past-due or due within %d days"
13159 (org-occur regexp nil callback
)
13162 (defun org-check-before-date (date)
13163 "Check if there are deadlines or scheduled entries before DATE."
13164 (interactive (list (org-read-date)))
13165 (let ((case-fold-search nil
)
13166 (regexp (concat "\\<\\(" org-deadline-string
13167 "\\|" org-scheduled-string
13168 "\\) *<\\([^>]+\\)>"))
13170 (lambda () (time-less-p
13171 (org-time-string-to-time (match-string 2))
13172 (org-time-string-to-time date
)))))
13173 (message "%d entries before %s"
13174 (org-occur regexp nil callback
) date
)))
13176 (defun org-check-after-date (date)
13177 "Check if there are deadlines or scheduled entries after DATE."
13178 (interactive (list (org-read-date)))
13179 (let ((case-fold-search nil
)
13180 (regexp (concat "\\<\\(" org-deadline-string
13181 "\\|" org-scheduled-string
13182 "\\) *<\\([^>]+\\)>"))
13186 (org-time-string-to-time (match-string 2))
13187 (org-time-string-to-time date
))))))
13188 (message "%d entries after %s"
13189 (org-occur regexp nil callback
) date
)))
13191 (defun org-evaluate-time-range (&optional to-buffer
)
13192 "Evaluate a time range by computing the difference between start and end.
13193 Normally the result is just printed in the echo area, but with prefix arg
13194 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
13195 If the time range is actually in a table, the result is inserted into the
13197 For time difference computation, a year is assumed to be exactly 365
13198 days in order to avoid rounding problems."
13201 (org-clock-update-time-maybe)
13203 (unless (org-at-date-range-p t
)
13204 (goto-char (point-at-bol))
13205 (re-search-forward org-tr-regexp-both
(point-at-eol) t
))
13206 (if (not (org-at-date-range-p t
))
13207 (error "Not at a time-stamp range, and none found in current line")))
13208 (let* ((ts1 (match-string 1))
13209 (ts2 (match-string 2))
13210 (havetime (or (> (length ts1
) 15) (> (length ts2
) 15)))
13211 (match-end (match-end 0))
13212 (time1 (org-time-string-to-time ts1
))
13213 (time2 (org-time-string-to-time ts2
))
13214 (t1 (org-float-time time1
))
13215 (t2 (org-float-time time2
))
13216 (diff (abs (- t2 t1
)))
13217 (negative (< (- t2 t1
) 0))
13218 ;; (ys (floor (* 365 24 60 60)))
13221 (fy "%dy %dd %02d:%02d")
13223 (fd "%dd %02d:%02d")
13228 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
13230 d
(floor (/ diff ds
)) diff
(mod diff ds
)
13231 h
(floor (/ diff hs
)) diff
(mod diff hs
)
13232 m
(floor (/ diff
60)))
13233 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
13235 d
(floor (+ (/ diff ds
) 0.5))
13237 (if (not to-buffer
)
13238 (message "%s" (org-make-tdiff-string y d h m
))
13239 (if (org-at-table-p)
13241 (goto-char match-end
)
13243 (and (looking-at " *|") (goto-char (match-end 0))))
13244 (goto-char match-end
))
13246 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
13247 (replace-match ""))
13248 (if negative
(insert " -"))
13249 (if (> y
0) (insert " " (format (if havetime fy fy1
) y d h m
))
13250 (if (> d
0) (insert " " (format (if havetime fd fd1
) d h m
))
13251 (insert " " (format fh h m
))))
13252 (if align
(org-table-align))
13253 (message "Time difference inserted")))))
13255 (defun org-make-tdiff-string (y d h m
)
13258 (if (> y
0) (setq fmt
(concat fmt
"%d year" (if (> y
1) "s" "") " ")
13260 (if (> d
0) (setq fmt
(concat fmt
"%d day" (if (> d
1) "s" "") " ")
13262 (if (> h
0) (setq fmt
(concat fmt
"%d hour" (if (> h
1) "s" "") " ")
13264 (if (> m
0) (setq fmt
(concat fmt
"%d minute" (if (> m
1) "s" "") " ")
13266 (apply 'format fmt
(nreverse l
))))
13268 (defun org-time-string-to-time (s)
13269 (apply 'encode-time
(org-parse-time-string s
)))
13270 (defun org-time-string-to-seconds (s)
13271 (org-float-time (org-time-string-to-time s
)))
13273 (defun org-time-string-to-absolute (s &optional daynr prefer show-all
)
13274 "Convert a time stamp to an absolute day number.
13275 If there is a specifyer for a cyclic time stamp, get the closest date to
13277 PREFER and SHOW-ALL are passed through to `org-closest-date'.
13278 the variable date is bound by the calendar when this is called."
13280 ((and daynr
(string-match "\\`%%\\((.*)\\)" s
))
13281 (if (org-diary-sexp-entry (match-string 1 s
) "" date
)
13284 ((and daynr
(string-match "\\+[0-9]+[dwmy]" s
))
13285 (org-closest-date s
(if (and (boundp 'daynr
) (integerp daynr
)) daynr
13286 (time-to-days (current-time))) (match-string 0 s
)
13288 (t (time-to-days (apply 'encode-time
(org-parse-time-string s
))))))
13290 (defun org-days-to-iso-week (days)
13291 "Return the iso week number."
13293 (car (calendar-iso-from-absolute days
)))
13295 (defun org-small-year-to-year (year)
13296 "Convert 2-digit years into 4-digit years.
13297 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
13298 The year 2000 cannot be abbreviated. Any year larger than 99
13299 is returned unchanged."
13301 (setq year
(+ 2000 year
))
13303 (setq year
(+ 1900 year
))))
13306 (defun org-time-from-absolute (d)
13307 "Return the time corresponding to date D.
13308 D may be an absolute day number, or a calendar-type list (month day year)."
13309 (if (numberp d
) (setq d
(calendar-gregorian-from-absolute d
)))
13310 (encode-time 0 0 0 (nth 1 d
) (car d
) (nth 2 d
)))
13312 (defun org-calendar-holiday ()
13313 "List of holidays, for Diary display in Org-mode."
13314 (require 'holidays
)
13316 (if (fboundp 'calendar-check-holidays
)
13317 'calendar-check-holidays
'check-calendar-holidays
) date
)))
13318 (if hl
(mapconcat 'identity hl
"; "))))
13320 (defun org-diary-sexp-entry (sexp entry date
)
13321 "Process a SEXP diary ENTRY for DATE."
13322 (require 'diary-lib
)
13323 (let ((result (if calendar-debug-sexp
13324 (let ((stack-trace-on-error t
))
13325 (eval (car (read-from-string sexp
))))
13326 (condition-case nil
13327 (eval (car (read-from-string sexp
)))
13330 (message "Bad sexp at line %d in %s: %s"
13332 (buffer-file-name) sexp
)
13334 (cond ((stringp result
) result
)
13335 ((and (consp result
)
13336 (stringp (cdr result
))) (cdr result
))
13340 (defun org-diary-to-ical-string (frombuf)
13341 "Get iCalendar entries from diary entries in buffer FROMBUF.
13342 This uses the icalendar.el library."
13343 (let* ((tmpdir (if (featurep 'xemacs
)
13345 temporary-file-directory
))
13346 (tmpfile (make-temp-name
13347 (expand-file-name "orgics" tmpdir
)))
13350 (set-buffer frombuf
)
13351 (icalendar-export-region (point-min) (point-max) tmpfile
)
13352 (setq buf
(find-buffer-visiting tmpfile
))
13354 (goto-char (point-min))
13355 (if (re-search-forward "^BEGIN:VEVENT" nil t
)
13356 (setq b
(match-beginning 0)))
13357 (goto-char (point-max))
13358 (if (re-search-backward "^END:VEVENT" nil t
)
13359 (setq e
(match-end 0)))
13360 (setq rtn
(if (and b e
) (concat (buffer-substring b e
) "\n") "")))
13362 (delete-file tmpfile
)
13365 (defun org-closest-date (start current change prefer show-all
)
13366 "Find the date closest to CURRENT that is consistent with START and CHANGE.
13367 When PREFER is `past' return a date that is either CURRENT or past.
13368 When PREFER is `future', return a date that is either CURRENT or future.
13369 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
13370 ;; Make the proper lists from the dates
13372 (let ((a1 '(("d" . day
) ("w" . week
) ("m" . month
) ("y" . year
)))
13373 dn dw sday cday n1 n2 n0
13374 d m y y1 y2 date1 date2 nmonths nm ny m2
)
13376 (setq start
(org-date-to-gregorian start
)
13377 current
(org-date-to-gregorian
13380 (time-to-days (current-time))))
13381 sday
(calendar-absolute-from-gregorian start
)
13382 cday
(calendar-absolute-from-gregorian current
))
13384 (if (<= cday sday
) (throw 'exit sday
))
13386 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change
)
13387 (setq dn
(string-to-number (match-string 1 change
))
13388 dw
(cdr (assoc (match-string 2 change
) a1
)))
13389 (error "Invalid change specifyer: %s" change
))
13390 (if (eq dw
'week
) (setq dw
'day dn
(* 7 dn
)))
13393 (setq n1
(+ sday
(* dn
(floor (/ (- cday sday
) dn
))))
13396 (setq d
(nth 1 start
) m
(car start
) y1
(nth 2 start
) y2
(nth 2 current
))
13397 (setq y1
(+ (* (floor (/ (- y2 y1
) dn
)) dn
) y1
))
13398 (setq date1
(list m d y1
)
13399 n1
(calendar-absolute-from-gregorian date1
)
13400 date2
(list m d
(+ y1
(* (if (< n1 cday
) 1 -
1) dn
)))
13401 n2
(calendar-absolute-from-gregorian date2
)))
13403 ;; approx number of month between the two dates
13404 (setq nmonths
(floor (/ (- cday sday
) 30.436875)))
13405 ;; How often does dn fit in there?
13406 (setq d
(nth 1 start
) m
(car start
) y
(nth 2 start
)
13407 nm
(* dn
(max 0 (1- (floor (/ nmonths dn
)))))
13409 ny
(floor (/ m
12))
13412 (while (> m
12) (setq m
(- m
12) y
(1+ y
)))
13413 (setq n1
(calendar-absolute-from-gregorian (list m d y
)))
13414 (setq m2
(+ m dn
) y2 y
)
13415 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
13416 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
)))
13417 (while (<= n2 cday
)
13418 (setq n1 n2 m m2 y y2
)
13419 (setq m2
(+ m dn
) y2 y
)
13420 (if (> m2
12) (setq y2
(1+ y2
) m2
(- m2
12)))
13421 (setq n2
(calendar-absolute-from-gregorian (list m2 d y2
))))))
13422 ;; Make sure n1 is the earlier date
13423 (setq n0 n1 n1
(min n1 n2
) n2
(max n0 n2
))
13426 ((eq prefer
'past
) (if (= cday n2
) n2 n1
))
13427 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
13428 (t (if (> (abs (- cday n1
)) (abs (- cday n2
))) n2 n1
)))
13430 ((eq prefer
'past
) (if (= cday n2
) n2 n1
))
13431 ((eq prefer
'future
) (if (= cday n1
) n1 n2
))
13432 (t (if (= cday n1
) n1 n2
)))))))
13434 (defun org-date-to-gregorian (date)
13435 "Turn any specification of DATE into a gregorian date for the calendar."
13436 (cond ((integerp date
) (calendar-gregorian-from-absolute date
))
13437 ((and (listp date
) (= (length date
) 3)) date
)
13439 (setq date
(org-parse-time-string date
))
13440 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))
13442 (list (nth 4 date
) (nth 3 date
) (nth 5 date
)))))
13444 (defun org-parse-time-string (s &optional nodefault
)
13445 "Parse the standard Org-mode time string.
13446 This should be a lot faster than the normal `parse-time-string'.
13447 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
13448 hour and minute fields will be nil if not given."
13449 (if (string-match org-ts-regexp0 s
)
13451 (if (or (match-beginning 8) (not nodefault
))
13452 (string-to-number (or (match-string 8 s
) "0")))
13453 (if (or (match-beginning 7) (not nodefault
))
13454 (string-to-number (or (match-string 7 s
) "0")))
13455 (string-to-number (match-string 4 s
))
13456 (string-to-number (match-string 3 s
))
13457 (string-to-number (match-string 2 s
))
13459 (error "Not a standard Org-mode time string: %s" s
)))
13461 (defun org-timestamp-up (&optional arg
)
13462 "Increase the date item at the cursor by one.
13463 If the cursor is on the year, change the year. If it is on the month or
13464 the day, change that.
13465 With prefix ARG, change by that many units."
13467 (org-timestamp-change (prefix-numeric-value arg
)))
13469 (defun org-timestamp-down (&optional arg
)
13470 "Decrease the date item at the cursor by one.
13471 If the cursor is on the year, change the year. If it is on the month or
13472 the day, change that.
13473 With prefix ARG, change by that many units."
13475 (org-timestamp-change (- (prefix-numeric-value arg
))))
13477 (defun org-timestamp-up-day (&optional arg
)
13478 "Increase the date in the time stamp by one day.
13479 With prefix ARG, change that many days."
13481 (if (and (not (org-at-timestamp-p t
))
13482 (org-on-heading-p))
13484 (org-timestamp-change (prefix-numeric-value arg
) 'day
)))
13486 (defun org-timestamp-down-day (&optional arg
)
13487 "Decrease the date in the time stamp by one day.
13488 With prefix ARG, change that many days."
13490 (if (and (not (org-at-timestamp-p t
))
13491 (org-on-heading-p))
13493 (org-timestamp-change (- (prefix-numeric-value arg
)) 'day
)))
13495 (defun org-at-timestamp-p (&optional inactive-ok
)
13496 "Determine if the cursor is in or at a timestamp."
13498 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2
))
13500 (ans (or (looking-at tsr
)
13502 (skip-chars-backward "^[<\n\r\t")
13503 (if (> (point) (point-min)) (backward-char 1))
13504 (and (looking-at tsr
)
13505 (> (- (match-end 0) pos
) -
1))))))
13507 (boundp 'org-ts-what
)
13510 ((= pos
(match-beginning 0)) 'bracket
)
13511 ((= pos
(1- (match-end 0))) 'bracket
)
13512 ((org-pos-in-match-range pos
2) 'year
)
13513 ((org-pos-in-match-range pos
3) 'month
)
13514 ((org-pos-in-match-range pos
7) 'hour
)
13515 ((org-pos-in-match-range pos
8) 'minute
)
13516 ((or (org-pos-in-match-range pos
4)
13517 (org-pos-in-match-range pos
5)) 'day
)
13518 ((and (> pos
(or (match-end 8) (match-end 5)))
13519 (< pos
(match-end 0)))
13520 (- pos
(or (match-end 8) (match-end 5))))
13524 (defun org-toggle-timestamp-type ()
13525 "Toggle the type (<active> or [inactive]) of a time stamp."
13527 (when (org-at-timestamp-p t
)
13528 (let ((beg (match-beginning 0)) (end (match-end 0))
13529 (map '((?\
[ .
"<") (?\
] .
">") (?
< .
"[") (?
> .
"]"))))
13532 (while (re-search-forward "[][<>]" end t
)
13533 (replace-match (cdr (assoc (char-after (match-beginning 0)) map
))
13535 (message "Timestamp is now %sactive"
13536 (if (equal (char-after beg
) ?
<) "" "in")))))
13538 (defun org-timestamp-change (n &optional what
)
13539 "Change the date in the time stamp at point.
13540 The date will be changed by N times WHAT. WHAT can be `day', `month',
13541 `year', `minute', `second'. If WHAT is not given, the cursor position
13542 in the timestamp determines what will be changed."
13543 (let ((pos (point))
13545 (dm (max (nth 1 org-time-stamp-rounding-minutes
) 1))
13549 (if (not (org-at-timestamp-p t
))
13550 (error "Not at a timestamp"))
13551 (if (and (not what
) (eq org-ts-what
'bracket
))
13552 (org-toggle-timestamp-type)
13553 (if (and (not what
) (not (eq org-ts-what
'day
))
13554 org-display-custom-times
13555 (get-text-property (point) 'display
)
13556 (not (get-text-property (1- (point)) 'display
)))
13557 (setq org-ts-what
'day
))
13558 (setq org-ts-what
(or what org-ts-what
)
13559 inactive
(= (char-after (match-beginning 0)) ?\
[)
13560 ts
(match-string 0))
13563 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
13565 (setq extra
(match-string 1 ts
)))
13566 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts
)
13568 (setq time0
(org-parse-time-string ts
))
13569 (when (and (eq org-ts-what
'minute
)
13570 (eq current-prefix-arg nil
))
13571 (setq n
(* dm
(cond ((> n
0) 1) ((< n
0) -
1) (t 0))))
13572 (when (not (= 0 (setq rem
(%
(nth 1 time0
) dm
))))
13573 (setcar (cdr time0
) (+ (nth 1 time0
)
13574 (if (> n
0) (- rem
) (- dm rem
))))))
13576 (encode-time (or (car time0
) 0)
13577 (+ (if (eq org-ts-what
'minute
) n
0) (nth 1 time0
))
13578 (+ (if (eq org-ts-what
'hour
) n
0) (nth 2 time0
))
13579 (+ (if (eq org-ts-what
'day
) n
0) (nth 3 time0
))
13580 (+ (if (eq org-ts-what
'month
) n
0) (nth 4 time0
))
13581 (+ (if (eq org-ts-what
'year
) n
0) (nth 5 time0
))
13583 (when (and (member org-ts-what
'(hour minute
))
13585 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra
))
13586 (setq extra
(org-modify-ts-extra
13588 (if (eq org-ts-what
'hour
) 2 5)
13590 (when (integerp org-ts-what
)
13591 (setq extra
(org-modify-ts-extra extra org-ts-what n dm
)))
13592 (if (eq what
'calendar
)
13593 (let ((cal-date (org-get-date-from-calendar)))
13594 (setcar (nthcdr 4 time0
) (nth 0 cal-date
)) ; month
13595 (setcar (nthcdr 3 time0
) (nth 1 cal-date
)) ; day
13596 (setcar (nthcdr 5 time0
) (nth 2 cal-date
)) ; year
13597 (setcar time0
(or (car time0
) 0))
13598 (setcar (nthcdr 1 time0
) (or (nth 1 time0
) 0))
13599 (setcar (nthcdr 2 time0
) (or (nth 2 time0
) 0))
13600 (setq time
(apply 'encode-time time0
))))
13601 (setq org-last-changed-timestamp
13602 (org-insert-time-stamp time with-hm inactive nil nil extra
))
13603 (org-clock-update-time-maybe)
13605 ;; Try to recenter the calendar window, if any
13606 (if (and org-calendar-follow-timestamp-change
13607 (get-buffer-window "*Calendar*" t
)
13608 (memq org-ts-what
'(day month year
)))
13609 (org-recenter-calendar (time-to-days time
))))))
13611 (defun org-modify-ts-extra (s pos n dm
)
13612 "Change the different parts of the lead-time and repeat fields in timestamp."
13613 (let ((idx '(("d" .
0) ("w" .
1) ("m" .
2) ("y" .
3) ("d" . -
1) ("y" .
4)))
13615 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s
)
13617 ((or (org-pos-in-match-range pos
2)
13618 (org-pos-in-match-range pos
3))
13619 (setq m
(string-to-number (match-string 3 s
))
13620 h
(string-to-number (match-string 2 s
)))
13621 (if (org-pos-in-match-range pos
2)
13623 (setq n
(* dm
(org-no-warnings (signum n
))))
13624 (when (not (= 0 (setq rem
(% m dm
))))
13625 (setq m
(+ m
(if (> n
0) (- rem
) (- dm rem
)))))
13627 (if (< m
0) (setq m
(+ m
60) h
(1- h
)))
13628 (if (> m
59) (setq m
(- m
60) h
(1+ h
)))
13629 (setq h
(min 24 (max 0 h
)))
13630 (setq ng
1 new
(format "-%02d:%02d" h m
)))
13631 ((org-pos-in-match-range pos
6)
13632 (setq ng
6 new
(car (rassoc (+ n
(cdr (assoc (match-string 6 s
) idx
))) idx
))))
13633 ((org-pos-in-match-range pos
5)
13634 (setq ng
5 new
(format "%d" (max 1 (+ n
(string-to-number (match-string 5 s
)))))))
13636 ((org-pos-in-match-range pos
9)
13637 (setq ng
9 new
(car (rassoc (+ n
(cdr (assoc (match-string 9 s
) idx
))) idx
))))
13638 ((org-pos-in-match-range pos
8)
13639 (setq ng
8 new
(format "%d" (max 0 (+ n
(string-to-number (match-string 8 s
))))))))
13643 (substring s
0 (match-beginning ng
))
13645 (substring s
(match-end ng
))))))
13648 (defun org-recenter-calendar (date)
13649 "If the calendar is visible, recenter it to DATE."
13650 (let* ((win (selected-window))
13651 (cwin (get-buffer-window "*Calendar*" t
))
13652 (calendar-move-hook nil
))
13654 (select-window cwin
)
13655 (calendar-goto-date (if (listp date
) date
13656 (calendar-gregorian-from-absolute date
)))
13657 (select-window win
))))
13659 (defun org-goto-calendar (&optional arg
)
13660 "Go to the Emacs calendar at the current date.
13661 If there is a time stamp in the current line, go to that date.
13662 A prefix ARG can be used to force the current date."
13664 (let ((tsr org-ts-regexp
) diff
13665 (calendar-move-hook nil
)
13666 (calendar-view-holidays-initially-flag nil
)
13667 (view-calendar-holidays-initially nil
)
13668 (calendar-view-diary-initially-flag nil
)
13669 (view-diary-entries-initially nil
))
13670 (if (or (org-at-timestamp-p)
13672 (beginning-of-line 1)
13673 (looking-at (concat ".*" tsr
))))
13674 (let ((d1 (time-to-days (current-time)))
13676 (org-time-string-to-time (match-string 1)))))
13677 (setq diff
(- d2 d1
))))
13679 (calendar-goto-today)
13680 (if (and diff
(not arg
)) (calendar-forward-day diff
))))
13682 (defun org-get-date-from-calendar ()
13683 "Return a list (month day year) of date at point in calendar."
13684 (with-current-buffer "*Calendar*"
13686 (calendar-cursor-to-date))))
13688 (defun org-date-from-calendar ()
13689 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
13690 If there is already a time stamp at the cursor position, update it."
13692 (if (org-at-timestamp-p t
)
13693 (org-timestamp-change 0 'calendar
)
13694 (let ((cal-date (org-get-date-from-calendar)))
13695 (org-insert-time-stamp
13696 (encode-time 0 0 0 (nth 1 cal-date
) (car cal-date
) (nth 2 cal-date
))))))
13698 (defun org-minutes-to-hh:mm-string
(m)
13699 "Compute H:MM from a number of minutes."
13700 (let ((h (/ m
60)))
13701 (setq m
(- m
(* 60 h
)))
13702 (format org-time-clocksum-format h m
)))
13704 (defun org-hh:mm-string-to-minutes
(s)
13705 "Convert a string H:MM to a number of minutes.
13706 If the string is just a number, interprete it as minutes.
13707 In fact, the first hh:mm or number in the string will be taken,
13708 there can be extra stuff in the string.
13709 If no number is found, the return value is 0."
13711 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s
)
13712 (+ (* (string-to-number (match-string 1 s
)) 60)
13713 (string-to-number (match-string 2 s
))))
13714 ((string-match "\\([0-9]+\\)" s
)
13715 (string-to-number (match-string 1 s
)))
13720 (defun org-save-all-org-buffers ()
13721 "Save all Org-mode buffers without user confirmation."
13723 (message "Saving all Org-mode buffers...")
13724 (save-some-buffers t
'org-mode-p
)
13725 (when (featurep 'org-id
) (org-id-locations-save))
13726 (message "Saving all Org-mode buffers... done"))
13728 (defun org-revert-all-org-buffers ()
13729 "Revert all Org-mode buffers.
13730 Prompt for confirmation when there are unsaved changes.
13731 Be sure you know what you are doing before letting this function
13732 overwrite your changes.
13734 This function is useful in a setup where one tracks org files
13735 with a version control system, to revert on one machine after pulling
13736 changes from another. I believe the procedure must be like this:
13738 1. M-x org-save-all-org-buffers
13739 2. Pull changes from the other machine, resolve conflicts
13740 3. M-x org-revert-all-org-buffers"
13742 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
13745 (save-window-excursion
13748 (when (and (with-current-buffer b
(org-mode-p))
13749 (with-current-buffer b buffer-file-name
))
13750 (switch-to-buffer b
)
13751 (revert-buffer t
'no-confirm
)))
13753 (when (and (featurep 'org-id
) org-id-track-globally
)
13754 (org-id-locations-load)))))
13759 (defun org-iswitchb (&optional arg
)
13760 "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
13761 With a prefix argument, restrict available to files.
13762 With two prefix arguments, restrict available buffers to agenda files."
13764 (let ((blist (cond ((equal arg
'(4)) (org-buffer-list 'files
))
13765 ((equal arg
'(16)) (org-buffer-list 'agenda
))
13766 (t (org-buffer-list)))))
13768 (org-icompleting-read "Org buffer: "
13769 (mapcar 'list
(mapcar 'buffer-name blist
))
13773 (defalias 'org-ido-switchb
'org-iswitchb
)
13775 (defun org-buffer-list (&optional predicate exclude-tmp
)
13776 "Return a list of Org buffers.
13777 PREDICATE can be `export', `files' or `agenda'.
13779 export restrict the list to Export buffers.
13780 files restrict the list to buffers visiting Org files.
13781 agenda restrict the list to buffers visiting agenda files.
13783 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
13785 (agenda-files (and (eq predicate
'agenda
)
13786 (mapcar 'file-truename
(org-agenda-files t
))))
13789 ((eq predicate
'files
)
13790 (lambda (b) (with-current-buffer b
(eq major-mode
'org-mode
))))
13791 ((eq predicate
'export
)
13792 (lambda (b) (string-match "\*Org .*Export" (buffer-name b
))))
13793 ((eq predicate
'agenda
)
13795 (with-current-buffer b
13796 (and (eq major-mode
'org-mode
)
13797 (setq bfn
(buffer-file-name b
))
13798 (member (file-truename bfn
) agenda-files
)))))
13799 (t (lambda (b) (with-current-buffer b
13800 (or (eq major-mode
'org-mode
)
13801 (string-match "\*Org .*Export"
13802 (buffer-name b
)))))))))
13806 (if (and (funcall filter b
)
13807 (or (not exclude-tmp
)
13808 (not (string-match "tmp" (buffer-name b
)))))
13813 (defun org-agenda-files (&optional unrestricted archives
)
13814 "Get the list of agenda files.
13815 Optional UNRESTRICTED means return the full list even if a restriction
13816 is currently in place.
13817 When ARCHIVES is t, include all archive files hat are really being
13818 used by the agenda files. If ARCHIVE is `ifmode', do this only if
13819 `org-agenda-archives-mode' is t."
13822 ((and (not unrestricted
) (get 'org-agenda-files
'org-restrict
)))
13823 ((stringp org-agenda-files
) (org-read-agenda-file-list))
13824 ((listp org-agenda-files
) org-agenda-files
)
13825 (t (error "Invalid value of `org-agenda-files'")))))
13826 (setq files
(apply 'append
13827 (mapcar (lambda (f)
13828 (if (file-directory-p f
)
13830 f t org-agenda-file-regexp
)
13833 (when org-agenda-skip-unavailable-files
13834 (setq files
(delq nil
13837 (and (file-readable-p file
) file
)))
13839 (when (or (eq archives t
)
13840 (and (eq archives
'ifmode
) (eq org-agenda-archives-mode t
)))
13841 (setq files
(org-add-archive-files files
)))
13844 (defun org-edit-agenda-file-list ()
13845 "Edit the list of agenda files.
13846 Depending on setup, this either uses customize to edit the variable
13847 `org-agenda-files', or it visits the file that is holding the list. In the
13848 latter case, the buffer is set up in a way that saving it automatically kills
13849 the buffer and restores the previous window configuration."
13851 (if (stringp org-agenda-files
)
13852 (let ((cw (current-window-configuration)))
13853 (find-file org-agenda-files
)
13854 (org-set-local 'org-window-configuration cw
)
13855 (org-add-hook 'after-save-hook
13857 (set-window-configuration
13858 (prog1 org-window-configuration
13859 (kill-buffer (current-buffer))))
13860 (org-install-agenda-files-menu)
13861 (message "New agenda file list installed"))
13863 (message "%s" (substitute-command-keys
13864 "Edit list and finish with \\[save-buffer]")))
13865 (customize-variable 'org-agenda-files
)))
13867 (defun org-store-new-agenda-file-list (list)
13868 "Set new value for the agenda file list and save it correctly."
13869 (if (stringp org-agenda-files
)
13870 (let ((f org-agenda-files
) b
)
13871 (while (setq b
(find-buffer-visiting f
)) (kill-buffer b
))
13873 (insert (mapconcat 'identity list
"\n") "\n")))
13874 (let ((org-mode-hook nil
) (org-inhibit-startup t
)
13875 (org-insert-mode-line-in-empty-file nil
))
13876 (setq org-agenda-files list
)
13877 (customize-save-variable 'org-agenda-files org-agenda-files
))))
13879 (defun org-read-agenda-file-list ()
13880 "Read the list of agenda files from a file."
13881 (when (file-directory-p org-agenda-files
)
13882 (error "`org-agenda-files' cannot be a single directory"))
13883 (when (stringp org-agenda-files
)
13885 (insert-file-contents org-agenda-files
)
13886 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
13890 (defun org-cycle-agenda-files ()
13891 "Cycle through the files in `org-agenda-files'.
13892 If the current buffer visits an agenda file, find the next one in the list.
13893 If the current buffer does not, find the first agenda file."
13895 (let* ((fs (org-agenda-files t
))
13896 (files (append fs
(list (car fs
))))
13897 (tcf (if buffer-file-name
(file-truename buffer-file-name
)))
13899 (unless files
(error "No agenda files"))
13901 (while (setq file
(pop files
))
13902 (if (equal (file-truename file
) tcf
)
13904 (find-file (car files
))
13906 (find-file (car fs
)))
13907 (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
13909 (defun org-agenda-file-to-front (&optional to-end
)
13910 "Move/add the current file to the top of the agenda file list.
13911 If the file is not present in the list, it is added to the front. If it is
13912 present, it is moved there. With optional argument TO-END, add/move to the
13915 (let ((org-agenda-skip-unavailable-files nil
)
13916 (file-alist (mapcar (lambda (x)
13917 (cons (file-truename x
) x
))
13918 (org-agenda-files t
)))
13919 (ctf (file-truename buffer-file-name
))
13921 (setq x
(assoc ctf file-alist
) had x
)
13923 (if (not x
) (setq x
(cons ctf
(abbreviate-file-name buffer-file-name
))))
13925 (setq file-alist
(append (delq x file-alist
) (list x
)))
13926 (setq file-alist
(cons x
(delq x file-alist
))))
13927 (org-store-new-agenda-file-list (mapcar 'cdr file-alist
))
13928 (org-install-agenda-files-menu)
13929 (message "File %s to %s of agenda file list"
13930 (if had
"moved" "added") (if to-end
"end" "front"))))
13932 (defun org-remove-file (&optional file
)
13933 "Remove current file from the list of files in variable `org-agenda-files'.
13934 These are the files which are being checked for agenda entries.
13935 Optional argument FILE means, use this file instead of the current."
13937 (let* ((org-agenda-skip-unavailable-files nil
)
13938 (file (or file buffer-file-name
))
13939 (true-file (file-truename file
))
13940 (afile (abbreviate-file-name file
))
13941 (files (delq nil
(mapcar
13943 (if (equal true-file
13946 (org-agenda-files t
)))))
13947 (if (not (= (length files
) (length (org-agenda-files t
))))
13949 (org-store-new-agenda-file-list files
)
13950 (org-install-agenda-files-menu)
13951 (message "Removed file: %s" afile
))
13952 (message "File was not in list: %s (not removed)" afile
))))
13954 (defun org-file-menu-entry (file)
13955 (vector file
(list 'find-file file
) t
))
13957 (defun org-check-agenda-file (file)
13958 "Make sure FILE exists. If not, ask user what to do."
13959 (when (not (file-exists-p file
))
13960 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
13961 (abbreviate-file-name file
))
13962 (let ((r (downcase (read-char-exclusive))))
13965 (org-remove-file file
)
13966 (throw 'nextfile t
))
13967 (t (error "Abort"))))))
13969 (defun org-get-agenda-file-buffer (file)
13970 "Get a buffer visiting FILE. If the buffer needs to be created, add
13971 it to the list of buffers which might be released later."
13972 (let ((buf (org-find-base-buffer-visiting file
)))
13974 buf
; just return it
13975 ;; Make a new buffer and remember it
13976 (setq buf
(find-file-noselect file
))
13977 (if buf
(push buf org-agenda-new-buffers
))
13980 (defun org-release-buffers (blist)
13981 "Release all buffers in list, asking the user for confirmation when needed.
13982 When a buffer is unmodified, it is just killed. When modified, it is saved
13983 \(if the user agrees) and then killed."
13985 (while (setq buf
(pop blist
))
13986 (setq file
(buffer-file-name buf
))
13987 (when (and (buffer-modified-p buf
)
13989 (y-or-n-p (format "Save file %s? " file
)))
13990 (with-current-buffer buf
(save-buffer)))
13991 (kill-buffer buf
))))
13993 (defun org-prepare-agenda-buffers (files)
13994 "Create buffers for all agenda files, protect archived trees and comments."
13996 (let ((pa '(:org-archived t
))
13997 (pc '(:org-comment t
))
13998 (pall '(:org-archived t
:org-comment t
))
13999 (inhibit-read-only t
)
14000 (rea (concat ":" org-archive-tag
":"))
14004 (while (setq file
(pop files
))
14008 (org-check-agenda-file file
)
14009 (set-buffer (org-get-agenda-file-buffer file
)))
14011 (setq bmp
(buffer-modified-p))
14012 (org-refresh-category-properties)
14013 (setq org-todo-keywords-for-agenda
14014 (append org-todo-keywords-for-agenda org-todo-keywords-1
))
14015 (setq org-done-keywords-for-agenda
14016 (append org-done-keywords-for-agenda org-done-keywords
))
14017 (setq org-todo-keyword-alist-for-agenda
14018 (append org-todo-keyword-alist-for-agenda org-todo-key-alist
))
14019 (setq org-drawers-for-agenda
14020 (append org-drawers-for-agenda org-drawers
))
14021 (setq org-tag-alist-for-agenda
14022 (append org-tag-alist-for-agenda org-tag-alist
))
14025 (remove-text-properties (point-min) (point-max) pall
)
14026 (when org-agenda-skip-archived-trees
14027 (goto-char (point-min))
14028 (while (re-search-forward rea nil t
)
14029 (if (org-on-heading-p t
)
14030 (add-text-properties (point-at-bol) (org-end-of-subtree t
) pa
))))
14031 (goto-char (point-min))
14032 (setq re
(concat "^\\*+ +" org-comment-string
"\\>"))
14033 (while (re-search-forward re nil t
)
14034 (add-text-properties
14035 (match-beginning 0) (org-end-of-subtree t
) pc
)))
14036 (set-buffer-modified-p bmp
)))))
14037 (setq org-todo-keyword-alist-for-agenda
14038 (org-uniquify org-todo-keyword-alist-for-agenda
)
14039 org-tag-alist-for-agenda
(org-uniquify org-tag-alist-for-agenda
))))
14041 ;;;; Embedded LaTeX
14043 (defvar org-cdlatex-mode-map
(make-sparse-keymap)
14044 "Keymap for the minor `org-cdlatex-mode'.")
14046 (org-defkey org-cdlatex-mode-map
"_" 'org-cdlatex-underscore-caret
)
14047 (org-defkey org-cdlatex-mode-map
"^" 'org-cdlatex-underscore-caret
)
14048 (org-defkey org-cdlatex-mode-map
"`" 'cdlatex-math-symbol
)
14049 (org-defkey org-cdlatex-mode-map
"'" 'org-cdlatex-math-modify
)
14050 (org-defkey org-cdlatex-mode-map
"\C-c{" 'cdlatex-environment
)
14052 (defvar org-cdlatex-texmathp-advice-is-done nil
14053 "Flag remembering if we have applied the advice to texmathp already.")
14055 (define-minor-mode org-cdlatex-mode
14056 "Toggle the minor `org-cdlatex-mode'.
14057 This mode supports entering LaTeX environment and math in LaTeX fragments
14059 \\{org-cdlatex-mode-map}"
14061 (when org-cdlatex-mode
(require 'cdlatex
))
14062 (unless org-cdlatex-texmathp-advice-is-done
14063 (setq org-cdlatex-texmathp-advice-is-done t
)
14064 (defadvice texmathp
(around org-math-always-on activate
)
14065 "Always return t in org-mode buffers.
14066 This is because we want to insert math symbols without dollars even outside
14067 the LaTeX math segments. If Orgmode thinks that point is actually inside
14068 an embedded LaTeX fragment, let texmathp do its job.
14069 \\[org-cdlatex-mode-map]"
14073 ((not (org-mode-p)) ad-do-it
)
14074 ((eq this-command
'cdlatex-math-symbol
)
14075 (setq ad-return-value t
14076 texmathp-why
'("cdlatex-math-symbol in org-mode" .
0)))
14078 (let ((p (org-inside-LaTeX-fragment-p)))
14079 (if (and p
(member (car p
) (plist-get org-format-latex-options
:matchers
)))
14080 (setq ad-return-value t
14081 texmathp-why
'("Org-mode embedded math" .
0))
14082 (if p ad-do-it
)))))))))
14084 (defun turn-on-org-cdlatex ()
14085 "Unconditionally turn on `org-cdlatex-mode'."
14086 (org-cdlatex-mode 1))
14088 (defun org-inside-LaTeX-fragment-p ()
14089 "Test if point is inside a LaTeX fragment.
14090 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
14091 sequence appearing also before point.
14092 Even though the matchers for math are configurable, this function assumes
14093 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
14094 delimiters are skipped when they have been removed by customization.
14095 The return value is nil, or a cons cell with the delimiter and
14096 and the position of this delimiter.
14098 This function does a reasonably good job, but can locally be fooled by
14099 for example currency specifications. For example it will assume being in
14100 inline math after \"$22.34\". The LaTeX fragment formatter will only format
14101 fragments that are properly closed, but during editing, we have to live
14102 with the uncertainty caused by missing closing delimiters. This function
14103 looks only before point, not after."
14105 (let ((pos (point))
14106 (dodollar (member "$" (plist-get org-format-latex-options
:matchers
)))
14108 (re-search-backward (concat "^\\(" paragraph-start
"\\)") nil t
)
14110 dd-on str
(start 0) m re
)
14113 (setq str
(concat (buffer-substring lim
(point)) "\000 X$.")
14114 re
(nth 1 (assoc "$" org-latex-regexps
)))
14115 (while (string-match re str start
)
14117 ((= (match-end 0) (length str
))
14118 (throw 'exit
(cons "$" (+ lim
(match-beginning 0) 1))))
14119 ((= (match-end 0) (- (length str
) 5))
14121 (t (setq start
(match-end 0))))))
14122 (when (setq m
(re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t
))
14124 (and (match-beginning 1) (throw 'exit
(cons (match-string 1) m
)))
14125 (and (match-beginning 2) (throw 'exit nil
))
14127 (while (re-search-backward "\\$\\$" lim t
)
14128 (setq dd-on
(not dd-on
)))
14130 (if dd-on
(cons "$$" m
))))))
14133 (defun org-try-cdlatex-tab ()
14134 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
14135 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
14136 - inside a LaTeX fragment, or
14137 - after the first word in a line, where an abbreviation expansion could
14138 insert a LaTeX environment."
14139 (when org-cdlatex-mode
14142 (skip-chars-backward "a-zA-Z0-9*")
14143 (skip-chars-backward " \t")
14146 ((org-inside-LaTeX-fragment-p)
14150 (defun org-cdlatex-underscore-caret (&optional arg
)
14151 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
14152 Revert to the normal definition outside of these fragments."
14154 (if (org-inside-LaTeX-fragment-p)
14155 (call-interactively 'cdlatex-sub-superscript
)
14156 (let (org-cdlatex-mode)
14157 (call-interactively (key-binding (vector last-input-event
))))))
14159 (defun org-cdlatex-math-modify (&optional arg
)
14160 "Execute `cdlatex-math-modify' in LaTeX fragments.
14161 Revert to the normal definition outside of these fragments."
14163 (if (org-inside-LaTeX-fragment-p)
14164 (call-interactively 'cdlatex-math-modify
)
14165 (let (org-cdlatex-mode)
14166 (call-interactively (key-binding (vector last-input-event
))))))
14168 (defvar org-latex-fragment-image-overlays nil
14169 "List of overlays carrying the images of latex fragments.")
14170 (make-variable-buffer-local 'org-latex-fragment-image-overlays
)
14172 (defun org-remove-latex-fragment-image-overlays ()
14173 "Remove all overlays with LaTeX fragment images in current buffer."
14174 (mapc 'org-delete-overlay org-latex-fragment-image-overlays
)
14175 (setq org-latex-fragment-image-overlays nil
))
14177 (defun org-preview-latex-fragment (&optional subtree
)
14178 "Preview the LaTeX fragment at point, or all locally or globally.
14179 If the cursor is in a LaTeX fragment, create the image and overlay
14180 it over the source code. If there is no fragment at point, display
14181 all fragments in the current text, from one headline to the next. With
14182 prefix SUBTREE, display all fragments in the current subtree. With a
14183 double prefix `C-u C-u', or when the cursor is before the first headline,
14184 display all fragments in the buffer.
14185 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
14187 (org-remove-latex-fragment-image-overlays)
14190 (let (beg end at msg
)
14192 ((or (equal subtree
'(16))
14193 (not (save-excursion
14194 (re-search-backward (concat "^" outline-regexp
) nil t
))))
14195 (setq beg
(point-min) end
(point-max)
14196 msg
"Creating images for buffer...%s"))
14197 ((equal subtree
'(4))
14198 (org-back-to-heading)
14199 (setq beg
(point) end
(org-end-of-subtree t
)
14200 msg
"Creating images for subtree...%s"))
14202 (if (setq at
(org-inside-LaTeX-fragment-p))
14203 (goto-char (max (point-min) (- (cdr at
) 2)))
14204 (org-back-to-heading))
14205 (setq beg
(point) end
(progn (outline-next-heading) (point))
14206 msg
(if at
"Creating image...%s"
14207 "Creating images for entry...%s"))))
14209 (narrow-to-region beg end
)
14212 (concat "ltxpng/" (file-name-sans-extension
14213 (file-name-nondirectory
14214 buffer-file-name
)))
14215 default-directory
'overlays msg at
'forbuffer
)
14216 (message msg
"done. Use `C-c C-c' to remove images.")))))
14218 (defvar org-latex-regexps
14219 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t
)
14220 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
14221 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
14222 ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
14223 ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil
)
14224 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil
)
14225 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil
)
14226 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil
))
14227 "Regular expressions for matching embedded LaTeX.")
14229 (defun org-format-latex (prefix &optional dir overlays msg at forbuffer
)
14230 "Replace LaTeX fragments with links to an image, and produce images.
14231 Some of the options can be changed using the variable
14232 `org-format-latex-options'."
14233 (if (and overlays
(fboundp 'clear-image-cache
)) (clear-image-cache))
14234 (let* ((prefixnodir (file-name-nondirectory prefix
))
14235 (absprefix (expand-file-name prefix dir
))
14236 (todir (file-name-directory absprefix
))
14237 (opt org-format-latex-options
)
14238 (matchers (plist-get opt
:matchers
))
14239 (re-list org-latex-regexps
)
14240 (cnt 0) txt link beg end re e checkdir
14241 executables-checked
14242 m n block linkfile movefile ov
)
14243 ;; Check if there are old images files with this prefix, and remove them
14244 (when (file-directory-p todir
)
14248 (concat (regexp-quote prefixnodir
) "_[0-9]+\\.png$"))))
14249 ;; Check the different regular expressions
14250 (while (setq e
(pop re-list
))
14251 (setq m
(car e
) re
(nth 1 e
) n
(nth 2 e
)
14252 block
(if (nth 3 e
) "\n\n" ""))
14253 (when (member m matchers
)
14254 (goto-char (point-min))
14255 (while (re-search-forward re nil t
)
14256 (when (and (or (not at
) (equal (cdr at
) (match-beginning n
)))
14257 (not (get-text-property (match-beginning n
)
14260 (not (eq (get-char-property (match-beginning n
)
14262 'org-latex-overlay
))))
14263 (setq txt
(match-string n
)
14264 beg
(match-beginning n
) end
(match-end n
)
14266 linkfile
(format "%s_%04d.png" prefix cnt
)
14267 movefile
(format "%s_%04d.png" absprefix cnt
)
14268 link
(concat block
"[[file:" linkfile
"]]" block
))
14269 (if msg
(message msg cnt
))
14271 (unless checkdir
; make sure the directory exists
14273 (or (file-directory-p todir
) (make-directory todir
)))
14275 (unless executables-checked
14276 (org-check-external-command
14277 "latex" "needed to convert LaTeX fragments to images")
14278 (org-check-external-command
14279 "dvipng" "needed to convert LaTeX fragments to images")
14280 (setq executables-checked t
))
14282 (org-create-formula-image
14283 txt movefile opt forbuffer
)
14287 (if (eq (org-overlay-get o
'org-overlay-type
)
14288 'org-latex-overlay
)
14289 (org-delete-overlay o
)))
14290 (org-overlays-in beg end
))
14291 (setq ov
(org-make-overlay beg end
))
14292 (org-overlay-put ov
'org-overlay-type
'org-latex-overlay
)
14293 (if (featurep 'xemacs
)
14295 (org-overlay-put ov
'invisible t
)
14298 (make-glyph (vector 'png
:file movefile
))))
14301 (list 'image
:type
'png
:file movefile
:ascent
'center
)))
14302 (push ov org-latex-fragment-image-overlays
)
14304 (delete-region beg end
)
14305 (insert link
))))))))
14307 (defvar org-export-latex-packages-alist
) ;; defined in org-latex.el
14308 ;; This function borrows from Ganesh Swami's latex2png.el
14309 (defun org-create-formula-image (string tofile options buffer
)
14310 "This calls dvipng."
14311 (require 'org-latex
)
14312 (let* ((tmpdir (if (featurep 'xemacs
)
14314 temporary-file-directory
))
14315 (texfilebase (make-temp-name
14316 (expand-file-name "orgtex" tmpdir
)))
14317 (texfile (concat texfilebase
".tex"))
14318 (dvifile (concat texfilebase
".dvi"))
14319 (pngfile (concat texfilebase
".png"))
14320 (fnh (if (featurep 'xemacs
)
14321 (font-height (get-face-font 'default
))
14322 (face-attribute 'default
:height nil
)))
14323 (scale (or (plist-get options
(if buffer
:scale
:html-scale
)) 1.0))
14324 (dpi (number-to-string (* scale
(floor (* 0.9 (if buffer fnh
140.
))))))
14325 (fg (or (plist-get options
(if buffer
:foreground
:html-foreground
))
14327 (bg (or (plist-get options
(if buffer
:background
:html-background
))
14329 (if (eq fg
'default
) (setq fg
(org-dvipng-color :foreground
)))
14330 (if (eq bg
'default
) (setq bg
(org-dvipng-color :background
)))
14331 (with-temp-file texfile
14332 (insert org-format-latex-header
14333 (if org-export-latex-packages-alist
14335 (mapconcat (lambda(p)
14336 (if (equal "" (car p
))
14337 (format "\\usepackage{%s}" (cadr p
))
14338 (format "\\usepackage[%s]{%s}"
14339 (car p
) (cadr p
))))
14340 org-export-latex-packages-alist
"\n"))
14342 "\n\\begin{document}\n" string
"\n\\end{document}\n"))
14343 (let ((dir default-directory
))
14344 (condition-case nil
14347 (call-process "latex" nil nil nil texfile
))
14350 (if (not (file-exists-p dvifile
))
14351 (progn (message "Failed to create dvi file from %s" texfile
) nil
)
14352 (condition-case nil
14353 (call-process "dvipng" nil nil nil
14356 ;;"-x" scale "-y" scale
14361 (if (not (file-exists-p pngfile
))
14362 (progn (message "Failed to create png file from %s" texfile
) nil
)
14363 ;; Use the requested file name and clean up
14364 (copy-file pngfile tofile
'replace
)
14365 (loop for e in
'(".dvi" ".tex" ".aux" ".log" ".png") do
14366 (delete-file (concat texfilebase e
)))
14369 (defun org-dvipng-color (attr)
14370 "Return an rgb color specification for dvipng."
14371 (apply 'format
"rgb %s %s %s"
14372 (mapcar 'org-normalize-color
14373 (color-values (face-attribute 'default attr nil
)))))
14375 (defun org-normalize-color (value)
14376 "Return string to be used as color value for an RGB component."
14377 (format "%g" (/ value
65535.0)))
14381 ;; Make `C-c C-x' a prefix key
14382 (org-defkey org-mode-map
"\C-c\C-x" (make-sparse-keymap))
14384 ;; TAB key with modifiers
14385 (org-defkey org-mode-map
"\C-i" 'org-cycle
)
14386 (org-defkey org-mode-map
[(tab)] 'org-cycle
)
14387 (org-defkey org-mode-map
[(control tab
)] 'org-force-cycle-archived
)
14388 (org-defkey org-mode-map
[(meta tab
)] 'org-complete
)
14389 (org-defkey org-mode-map
"\M-\t" 'org-complete
)
14390 (org-defkey org-mode-map
"\M-\C-i" 'org-complete
)
14391 ;; The following line is necessary under Suse GNU/Linux
14392 (unless (featurep 'xemacs
)
14393 (org-defkey org-mode-map
[S-iso-lefttab
] 'org-shifttab
))
14394 (org-defkey org-mode-map
[(shift tab
)] 'org-shifttab
)
14395 (define-key org-mode-map
[backtab] 'org-shifttab)
14397 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
14398 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
14399 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
14401 ;; Cursor keys with modifiers
14402 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
14403 (org-defkey org-mode-map [(meta right)] 'org-metaright)
14404 (org-defkey org-mode-map [(meta up)] 'org-metaup)
14405 (org-defkey org-mode-map [(meta down)] 'org-metadown)
14407 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
14408 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
14409 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
14410 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
14412 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
14413 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
14414 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
14415 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
14417 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
14418 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
14420 ;;; Extra keys for tty access.
14421 ;; We only set them when really needed because otherwise the
14422 ;; menus don't show the simple keys
14424 (when (or org-use-extra-keys
14425 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
14426 (not window-system))
14427 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
14428 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
14429 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
14430 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
14431 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
14432 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
14433 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
14434 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
14435 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
14436 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
14437 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
14438 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
14439 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
14440 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
14441 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
14442 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
14443 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
14444 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
14445 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
14446 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
14447 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
14448 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
14449 (org-defkey org-mode-map [?\e (tab)] 'org-complete)
14450 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
14451 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
14452 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
14453 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
14454 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
14456 ;; All the other keys
14458 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
14459 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
14460 (if (boundp 'narrow-map)
14461 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
14462 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
14463 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
14464 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
14465 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
14466 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
14467 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
14468 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
14469 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
14470 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
14471 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
14472 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
14473 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
14474 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
14475 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
14476 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
14477 (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
14478 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
14479 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
14480 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
14481 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
14482 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
14483 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
14484 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
14485 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
14486 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
14487 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
14488 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
14489 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
14490 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
14491 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
14492 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
14493 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
14494 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
14495 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
14496 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
14497 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
14498 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
14499 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
14500 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
14501 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
14502 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
14503 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
14504 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14505 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
14506 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
14507 (org-defkey org-mode-map "\C-c^" 'org-sort)
14508 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
14509 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
14510 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
14511 (org-defkey org-mode-map "\C-m" 'org-return)
14512 (org-defkey org-mode-map "\C-j" 'org-return-indent)
14513 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
14514 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
14515 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
14516 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
14517 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
14518 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
14519 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
14520 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
14521 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
14522 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
14523 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
14524 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
14525 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
14526 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
14527 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
14528 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
14529 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
14530 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
14531 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
14532 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
14534 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
14535 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
14536 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
14537 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
14539 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
14540 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
14541 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
14542 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
14543 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
14544 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
14545 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
14546 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
14547 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
14548 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
14549 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
14550 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
14551 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
14552 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
14553 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
14555 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
14556 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
14557 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
14558 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
14560 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
14562 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
14564 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
14565 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
14567 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
14570 (when (featurep 'xemacs)
14571 (org-defkey org-mode-map 'button3 'popup-mode-menu))
14574 (defvar org-self-insert-command-undo-counter 0)
14576 (defvar org-table-auto-blank-field) ; defined in org-table.el
14577 (defun org-self-insert-command (N)
14578 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
14579 If the cursor is in a table looking at whitespace, the whitespace is
14580 overwritten, and the table is not marked as requiring realignment."
14585 ;; check if we blank the field, and if that triggers align
14586 (and (featurep 'org-table) org-table-auto-blank-field
14587 (member last-command
14588 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
14589 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
14590 ;; got extra space, this field does not determine column width
14591 (let (org-table-may-need-update) (org-table-blank-field))
14592 ;; no extra space, this field may determine column width
14593 (org-table-blank-field)))
14596 (looking-at "[^|\n]* |"))
14597 (let (org-table-may-need-update)
14598 (goto-char (1- (match-end 0)))
14599 (delete-backward-char 1)
14600 (goto-char (match-beginning 0))
14601 (self-insert-command N))
14602 (setq org-table-may-need-update t)
14603 (self-insert-command N)
14604 (org-fix-tags-on-the-fly)
14605 (if org-self-insert-cluster-for-undo
14606 (if (not (eq last-command 'org-self-insert-command))
14607 (setq org-self-insert-command-undo-counter 1)
14608 (if (>= org-self-insert-command-undo-counter 20)
14609 (setq org-self-insert-command-undo-counter 1)
14610 (and (> org-self-insert-command-undo-counter 0)
14612 (not (cadr buffer-undo-list)) ; remove nil entry
14613 (setcdr buffer-undo-list (cddr buffer-undo-list)))
14614 (setq org-self-insert-command-undo-counter
14615 (1+ org-self-insert-command-undo-counter)))))))
14617 (defun org-fix-tags-on-the-fly ()
14618 (when (and (equal (char-after (point-at-bol)) ?*)
14619 (org-on-heading-p))
14620 (org-align-tags-here org-tags-column)))
14622 (defun org-delete-backward-char (N)
14623 "Like `delete-backward-char', insert whitespace at field end in tables.
14624 When deleting backwards, in tables this function will insert whitespace in
14625 front of the next \"|\" separator, to keep the table aligned. The table will
14626 still be marked for re-alignment if the field did fill the entire column,
14627 because, in this case the deletion might narrow the column."
14629 (if (and (org-table-p)
14631 (string-match "|" (buffer-substring (point-at-bol) (point)))
14632 (looking-at ".*?|"))
14633 (let ((pos (point))
14634 (noalign (looking-at "[^|\n\r]* |"))
14635 (c org-table-may-need-update))
14636 (backward-delete-char N)
14637 (skip-chars-forward "^|")
14639 (goto-char (1- pos))
14640 ;; noalign: if there were two spaces at the end, this field
14641 ;; does not determine the width of the column.
14642 (if noalign (setq org-table-may-need-update c)))
14643 (backward-delete-char N)
14644 (org-fix-tags-on-the-fly)))
14646 (defun org-delete-char (N)
14647 "Like `delete-char', but insert whitespace at field end in tables.
14648 When deleting characters, in tables this function will insert whitespace in
14649 front of the next \"|\" separator, to keep the table aligned. The table will
14650 still be marked for re-alignment if the field did fill the entire column,
14651 because, in this case the deletion might narrow the column."
14653 (if (and (org-table-p)
14655 (not (= (char-after) ?|))
14657 (if (looking-at ".*?|")
14658 (let ((pos (point))
14659 (noalign (looking-at "[^|\n\r]* |"))
14660 (c org-table-may-need-update))
14661 (replace-match (concat
14662 (substring (match-string 0) 1 -1)
14665 ;; noalign: if there were two spaces at the end, this field
14666 ;; does not determine the width of the column.
14667 (if noalign (setq org-table-may-need-update c)))
14670 (org-fix-tags-on-the-fly)))
14672 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
14673 (put 'org-self-insert-command 'delete-selection t)
14674 (put 'orgtbl-self-insert-command 'delete-selection t)
14675 (put 'org-delete-char 'delete-selection 'supersede)
14676 (put 'org-delete-backward-char 'delete-selection 'supersede)
14677 (put 'org-yank 'delete-selection 'yank)
14679 ;; Make `flyspell-mode' delay after some commands
14680 (put 'org-self-insert-command 'flyspell-delayed t)
14681 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
14682 (put 'org-delete-char 'flyspell-delayed t)
14683 (put 'org-delete-backward-char 'flyspell-delayed t)
14685 ;; Make pabbrev-mode expand after org-mode commands
14686 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
14687 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
14689 ;; How to do this: Measure non-white length of current string
14690 ;; If equal to column width, we should realign.
14692 (defun org-remap (map &rest commands)
14693 "In MAP, remap the functions given in COMMANDS.
14694 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
14697 (setq old (pop commands) new (pop commands))
14698 (if (fboundp 'command-remapping)
14699 (org-defkey map (vector 'remap old) new)
14700 (substitute-key-definition old new map global-map)))))
14702 (when (eq org-enable-table-editor 'optimized)
14703 ;; If the user wants maximum table support, we need to hijack
14704 ;; some standard editing functions
14705 (org-remap org-mode-map
14706 'self-insert-command 'org-self-insert-command
14707 'delete-char 'org-delete-char
14708 'delete-backward-char 'org-delete-backward-char)
14709 (org-defkey org-mode-map "|" 'org-force-self-insert))
14711 (defvar org-ctrl-c-ctrl-c-hook nil
14712 "Hook for functions attaching themselves to `C-c C-c'.
14713 This can be used to add additional functionality to the C-c C-c key which
14714 executes context-dependent commands.
14715 Each function will be called with no arguments. The function must check
14716 if the context is appropriate for it to act. If yes, it should do its
14717 thing and then return a non-nil value. If the context is wrong,
14718 just do nothing and return nil.")
14720 (defvar org-tab-first-hook nil
14721 "Hook for functions to attach themselves to TAB.
14722 See `org-ctrl-c-ctrl-c-hook' for more information.
14723 This hook runs as the first action when TAB is pressed, even before
14724 `org-cycle' messes around with the `outline-regexp' to cater for
14725 inline tasks and plain list item folding.
14726 If any function in this hook returns t, not other actions like table
14727 field motion visibility cycling will be done.")
14729 (defvar org-tab-after-check-for-table-hook nil
14730 "Hook for functions to attach themselves to TAB.
14731 See `org-ctrl-c-ctrl-c-hook' for more information.
14732 This hook runs after it has been established that the cursor is not in a
14733 table, but before checking if the cursor is in a headline or if global cycling
14735 If any function in this hook returns t, not other actions like visibility
14736 cycling will be done.")
14738 (defvar org-tab-after-check-for-cycling-hook nil
14739 "Hook for functions to attach themselves to TAB.
14740 See `org-ctrl-c-ctrl-c-hook' for more information.
14741 This hook runs after it has been established that not table field motion and
14742 not visibility should be done because of current context. This is probably
14743 the place where a package like yasnippets can hook in.")
14745 (defvar org-metaleft-hook nil
14746 "Hook for functions attaching themselves to `M-left'.
14747 See `org-ctrl-c-ctrl-c-hook' for more information.")
14748 (defvar org-metaright-hook nil
14749 "Hook for functions attaching themselves to `M-right'.
14750 See `org-ctrl-c-ctrl-c-hook' for more information.")
14751 (defvar org-metaup-hook nil
14752 "Hook for functions attaching themselves to `M-up'.
14753 See `org-ctrl-c-ctrl-c-hook' for more information.")
14754 (defvar org-metadown-hook nil
14755 "Hook for functions attaching themselves to `M-down'.
14756 See `org-ctrl-c-ctrl-c-hook' for more information.")
14757 (defvar org-shiftmetaleft-hook nil
14758 "Hook for functions attaching themselves to `M-S-left'.
14759 See `org-ctrl-c-ctrl-c-hook' for more information.")
14760 (defvar org-shiftmetaright-hook nil
14761 "Hook for functions attaching themselves to `M-S-right'.
14762 See `org-ctrl-c-ctrl-c-hook' for more information.")
14763 (defvar org-shiftmetaup-hook nil
14764 "Hook for functions attaching themselves to `M-S-up'.
14765 See `org-ctrl-c-ctrl-c-hook' for more information.")
14766 (defvar org-shiftmetadown-hook nil
14767 "Hook for functions attaching themselves to `M-S-down'.
14768 See `org-ctrl-c-ctrl-c-hook' for more information.")
14769 (defvar org-metareturn-hook nil
14770 "Hook for functions attaching themselves to `M-RET'.
14771 See `org-ctrl-c-ctrl-c-hook' for more information.")
14773 (defun org-modifier-cursor-error ()
14774 "Throw an error, a modified cursor command was applied in wrong context."
14775 (error "This command is active in special context like tables, headlines or items"))
14777 (defun org-shiftselect-error ()
14778 "Throw an error because Shift-Cursor command was applied in wrong context."
14779 (if (and (boundp 'shift-select-mode) shift-select-mode)
14780 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
14781 (error "This command works only in special context like headlines or timestamps")))
14783 (defun org-call-for-shift-select (cmd)
14784 (let ((this-command-keys-shift-translated t))
14785 (call-interactively cmd)))
14787 (defun org-shifttab (&optional arg)
14788 "Global visibility cycling or move to previous table field.
14789 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
14791 See the individual commands for more information."
14794 ((org-at-table-p) (call-interactively 'org-table-previous-field))
14796 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
14797 (message "Content view to level: %d" arg)
14798 (org-content (prefix-numeric-value arg2))
14799 (setq org-cycle-global-status 'overview)))
14800 (t (call-interactively 'org-global-cycle))))
14802 (defun org-shiftmetaleft ()
14803 "Promote subtree or delete table column.
14804 Calls `org-promote-subtree', `org-outdent-item',
14805 or `org-table-delete-column', depending on context.
14806 See the individual commands for more information."
14809 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
14810 ((org-at-table-p) (call-interactively 'org-table-delete-column))
14811 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
14812 ((org-at-item-p) (call-interactively 'org-outdent-item))
14813 (t (org-modifier-cursor-error))))
14815 (defun org-shiftmetaright ()
14816 "Demote subtree or insert table column.
14817 Calls `org-demote-subtree', `org-indent-item',
14818 or `org-table-insert-column', depending on context.
14819 See the individual commands for more information."
14822 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
14823 ((org-at-table-p) (call-interactively 'org-table-insert-column))
14824 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
14825 ((org-at-item-p) (call-interactively 'org-indent-item))
14826 (t (org-modifier-cursor-error))))
14828 (defun org-shiftmetaup (&optional arg)
14829 "Move subtree up or kill table row.
14830 Calls `org-move-subtree-up' or `org-table-kill-row' or
14831 `org-move-item-up' depending on context. See the individual commands
14832 for more information."
14835 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
14836 ((org-at-table-p) (call-interactively 'org-table-kill-row))
14837 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
14838 ((org-at-item-p) (call-interactively 'org-move-item-up))
14839 (t (org-modifier-cursor-error))))
14841 (defun org-shiftmetadown (&optional arg)
14842 "Move subtree down or insert table row.
14843 Calls `org-move-subtree-down' or `org-table-insert-row' or
14844 `org-move-item-down', depending on context. See the individual
14845 commands for more information."
14848 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
14849 ((org-at-table-p) (call-interactively 'org-table-insert-row))
14850 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
14851 ((org-at-item-p) (call-interactively 'org-move-item-down))
14852 (t (org-modifier-cursor-error))))
14854 (defun org-metaleft (&optional arg)
14855 "Promote heading or move table column to left.
14856 Calls `org-do-promote' or `org-table-move-column', depending on context.
14857 With no specific context, calls the Emacs default `backward-word'.
14858 See the individual commands for more information."
14861 ((run-hook-with-args-until-success 'org-metaleft-hook))
14862 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
14863 ((or (org-on-heading-p)
14864 (and (org-region-active-p)
14866 (goto-char (region-beginning))
14867 (org-on-heading-p))))
14868 (call-interactively 'org-do-promote))
14869 ((or (org-at-item-p)
14870 (and (org-region-active-p)
14872 (goto-char (region-beginning))
14874 (call-interactively 'org-outdent-item))
14875 (t (call-interactively 'backward-word))))
14877 (defun org-metaright (&optional arg)
14878 "Demote subtree or move table column to right.
14879 Calls `org-do-demote' or `org-table-move-column', depending on context.
14880 With no specific context, calls the Emacs default `forward-word'.
14881 See the individual commands for more information."
14884 ((run-hook-with-args-until-success 'org-metaright-hook))
14885 ((org-at-table-p) (call-interactively 'org-table-move-column))
14886 ((or (org-on-heading-p)
14887 (and (org-region-active-p)
14889 (goto-char (region-beginning))
14890 (org-on-heading-p))))
14891 (call-interactively 'org-do-demote))
14892 ((or (org-at-item-p)
14893 (and (org-region-active-p)
14895 (goto-char (region-beginning))
14897 (call-interactively 'org-indent-item))
14898 (t (call-interactively 'forward-word))))
14900 (defun org-metaup (&optional arg)
14901 "Move subtree up or move table row up.
14902 Calls `org-move-subtree-up' or `org-table-move-row' or
14903 `org-move-item-up', depending on context. See the individual commands
14904 for more information."
14907 ((run-hook-with-args-until-success 'org-metaup-hook))
14908 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
14909 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
14910 ((org-at-item-p) (call-interactively 'org-move-item-up))
14911 (t (transpose-lines 1) (beginning-of-line -1))))
14913 (defun org-metadown (&optional arg)
14914 "Move subtree down or move table row down.
14915 Calls `org-move-subtree-down' or `org-table-move-row' or
14916 `org-move-item-down', depending on context. See the individual
14917 commands for more information."
14920 ((run-hook-with-args-until-success 'org-metadown-hook))
14921 ((org-at-table-p) (call-interactively 'org-table-move-row))
14922 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
14923 ((org-at-item-p) (call-interactively 'org-move-item-down))
14924 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
14926 (defun org-shiftup (&optional arg)
14927 "Increase item in timestamp or increase priority of current headline.
14928 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
14929 depending on context. See the individual commands for more information."
14932 ((and org-support-shift-select (org-region-active-p))
14933 (org-call-for-shift-select 'previous-line))
14934 ((org-at-timestamp-p t)
14935 (call-interactively (if org-edit-timestamp-down-means-later
14936 'org-timestamp-down 'org-timestamp-up)))
14937 ((and (not (eq org-support-shift-select 'always))
14938 org-enable-priority-commands
14939 (org-on-heading-p))
14940 (call-interactively 'org-priority-up))
14941 ((and (not org-support-shift-select) (org-at-item-p))
14942 (call-interactively 'org-previous-item))
14943 ((org-clocktable-try-shift 'up arg))
14944 (org-support-shift-select
14945 (org-call-for-shift-select 'previous-line))
14946 (t (org-shiftselect-error))))
14948 (defun org-shiftdown (&optional arg)
14949 "Decrease item in timestamp or decrease priority of current headline.
14950 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
14951 depending on context. See the individual commands for more information."
14954 ((and org-support-shift-select (org-region-active-p))
14955 (org-call-for-shift-select 'next-line))
14956 ((org-at-timestamp-p t)
14957 (call-interactively (if org-edit-timestamp-down-means-later
14958 'org-timestamp-up 'org-timestamp-down)))
14959 ((and (not (eq org-support-shift-select 'always))
14960 org-enable-priority-commands
14961 (org-on-heading-p))
14962 (call-interactively 'org-priority-down))
14963 ((and (not org-support-shift-select) (org-at-item-p))
14964 (call-interactively 'org-next-item))
14965 ((org-clocktable-try-shift 'down arg))
14966 (org-support-shift-select
14967 (org-call-for-shift-select 'next-line))
14968 (t (org-shiftselect-error))))
14970 (defun org-shiftright (&optional arg)
14971 "Cycle the thing at point or in the current line, depending on context.
14972 Depending on context, this does one of the following:
14974 - switch a timestamp at point one day into the future
14975 - on a headline, switch to the next TODO keyword.
14976 - on an item, switch entire list to the next bullet type
14977 - on a property line, switch to the next allowed value
14978 - on a clocktable definition line, move time block into the future"
14981 ((and org-support-shift-select (org-region-active-p))
14982 (org-call-for-shift-select 'forward-char))
14983 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
14984 ((and (not (eq org-support-shift-select 'always))
14985 (org-on-heading-p))
14986 (let ((org-inhibit-logging
14987 (not org-treat-S-cursor-todo-selection-as-state-change))
14988 (org-inhibit-blocking
14989 (not org-treat-S-cursor-todo-selection-as-state-change)))
14990 (org-call-with-arg 'org-todo 'right)))
14991 ((or (and org-support-shift-select
14992 (not (eq org-support-shift-select 'always))
14993 (org-at-item-bullet-p))
14994 (and (not org-support-shift-select) (org-at-item-p)))
14995 (org-call-with-arg 'org-cycle-list-bullet nil))
14996 ((and (not (eq org-support-shift-select 'always))
14997 (org-at-property-p))
14998 (call-interactively 'org-property-next-allowed-value))
14999 ((org-clocktable-try-shift 'right arg))
15000 (org-support-shift-select
15001 (org-call-for-shift-select 'forward-char))
15002 (t (org-shiftselect-error))))
15004 (defun org-shiftleft (&optional arg)
15005 "Cycle the thing at point or in the current line, depending on context.
15006 Depending on context, this does one of the following:
15008 - switch a timestamp at point one day into the past
15009 - on a headline, switch to the previous TODO keyword.
15010 - on an item, switch entire list to the previous bullet type
15011 - on a property line, switch to the previous allowed value
15012 - on a clocktable definition line, move time block into the past"
15015 ((and org-support-shift-select (org-region-active-p))
15016 (org-call-for-shift-select 'backward-char))
15017 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
15018 ((and (not (eq org-support-shift-select 'always))
15019 (org-on-heading-p))
15020 (let ((org-inhibit-logging
15021 (not org-treat-S-cursor-todo-selection-as-state-change))
15022 (org-inhibit-blocking
15023 (not org-treat-S-cursor-todo-selection-as-state-change)))
15024 (org-call-with-arg 'org-todo 'left)))
15025 ((or (and org-support-shift-select
15026 (not (eq org-support-shift-select 'always))
15027 (org-at-item-bullet-p))
15028 (and (not org-support-shift-select) (org-at-item-p)))
15029 (org-call-with-arg 'org-cycle-list-bullet 'previous))
15030 ((and (not (eq org-support-shift-select 'always))
15031 (org-at-property-p))
15032 (call-interactively 'org-property-previous-allowed-value))
15033 ((org-clocktable-try-shift 'left arg))
15034 (org-support-shift-select
15035 (org-call-for-shift-select 'backward-char))
15036 (t (org-shiftselect-error))))
15038 (defun org-shiftcontrolright ()
15039 "Switch to next TODO set."
15042 ((and org-support-shift-select (org-region-active-p))
15043 (org-call-for-shift-select 'forward-word))
15044 ((and (not (eq org-support-shift-select 'always))
15045 (org-on-heading-p))
15046 (org-call-with-arg 'org-todo 'nextset))
15047 (org-support-shift-select
15048 (org-call-for-shift-select 'forward-word))
15049 (t (org-shiftselect-error))))
15051 (defun org-shiftcontrolleft ()
15052 "Switch to previous TODO set."
15055 ((and org-support-shift-select (org-region-active-p))
15056 (org-call-for-shift-select 'backward-word))
15057 ((and (not (eq org-support-shift-select 'always))
15058 (org-on-heading-p))
15059 (org-call-with-arg 'org-todo 'previousset))
15060 (org-support-shift-select
15061 (org-call-for-shift-select 'backward-word))
15062 (t (org-shiftselect-error))))
15064 (defun org-ctrl-c-ret ()
15065 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
15068 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
15069 (t (call-interactively 'org-insert-heading))))
15071 (defun org-copy-special ()
15072 "Copy region in table or copy current subtree.
15073 Calls `org-table-copy' or `org-copy-subtree', depending on context.
15074 See the individual commands for more information."
15076 (call-interactively
15077 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
15079 (defun org-cut-special ()
15080 "Cut region in table or cut current subtree.
15081 Calls `org-table-copy' or `org-cut-subtree', depending on context.
15082 See the individual commands for more information."
15084 (call-interactively
15085 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
15087 (defun org-paste-special (arg)
15088 "Paste rectangular region into table, or past subtree relative to level.
15089 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
15090 See the individual commands for more information."
15092 (if (org-at-table-p)
15093 (org-table-paste-rectangle)
15094 (org-paste-subtree arg)))
15096 (defun org-edit-special ()
15097 "Call a special editor for the stuff at point.
15098 When at a table, call the formula editor with `org-table-edit-formulas'.
15099 When at the first line of an src example, call `org-edit-src-code'.
15100 When in an #+include line, visit the include file. Otherwise call
15101 `ffap' to visit the file at point."
15105 (call-interactively 'org-table-edit-formulas))
15107 (beginning-of-line 1)
15108 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
15109 (find-file (org-trim (match-string 1))))
15110 ((org-edit-src-code))
15111 ((org-edit-fixed-width-region))
15112 (t (call-interactively 'ffap))))
15115 (defun org-ctrl-c-ctrl-c (&optional arg)
15116 "Set tags in headline, or update according to changed information at point.
15118 This command does many different things, depending on context:
15120 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
15121 this is what we do.
15123 - If the cursor is on a statistics cookie, update it.
15125 - If the cursor is in a headline, prompt for tags and insert them
15126 into the current line, aligned to `org-tags-column'. When called
15127 with prefix arg, realign all tags in the current buffer.
15129 - If the cursor is in one of the special #+KEYWORD lines, this
15130 triggers scanning the buffer for these lines and updating the
15133 - If the cursor is inside a table, realign the table. This command
15134 works even if the automatic table editor has been turned off.
15136 - If the cursor is on a #+TBLFM line, re-apply the formulas to
15139 - If the cursor is at a footnote reference or definition, jump to
15140 the corresponding definition or references, respectively.
15142 - If the cursor is a the beginning of a dynamic block, update it.
15144 - If the cursor is inside a table created by the table.el package,
15145 activate that table.
15147 - If the current buffer is a remember buffer, close note and file
15148 it. A prefix argument of 1 files to the default location
15149 without further interaction. A prefix argument of 2 files to
15150 the currently clocking task.
15152 - If the cursor is on a <<<target>>>, update radio targets and corresponding
15153 links in this buffer.
15155 - If the cursor is on a numbered item in a plain list, renumber the
15158 - If the cursor is on a checkbox, toggle it."
15160 (let ((org-enable-table-editor t))
15162 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
15163 org-occur-highlights
15164 org-latex-fragment-image-overlays)
15165 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
15166 (org-remove-occur-highlights)
15167 (org-remove-latex-fragment-image-overlays)
15168 (message "Temporary highlights/overlays removed from current buffer"))
15169 ((and (local-variable-p 'org-finish-function (current-buffer))
15170 (fboundp org-finish-function))
15171 (funcall org-finish-function))
15172 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
15173 ((org-at-property-p)
15174 (call-interactively 'org-property-action))
15175 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
15176 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
15177 (or (org-on-heading-p) (org-at-item-p)))
15178 (call-interactively 'org-update-statistics-cookies))
15179 ((org-on-heading-p) (call-interactively 'org-set-tags))
15180 ((org-at-table.el-p)
15182 (beginning-of-line 1)
15183 (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
15184 (call-interactively 'table-recognize-table))
15186 (org-table-maybe-eval-formula)
15188 (call-interactively 'org-table-recalculate)
15189 (org-table-maybe-recalculate-line))
15190 (call-interactively 'org-table-align))
15191 ((or (org-footnote-at-reference-p)
15192 (org-footnote-at-definition-p))
15193 (call-interactively 'org-footnote-action))
15194 ((org-at-item-checkbox-p)
15195 (call-interactively 'org-toggle-checkbox))
15198 (call-interactively 'org-toggle-checkbox)
15199 (call-interactively 'org-maybe-renumber-ordered-list)))
15200 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
15202 (beginning-of-line 1)
15203 (save-excursion (org-update-dblock)))
15205 (beginning-of-line 1)
15206 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
15208 ((equal (match-string 1) "TBLFM")
15209 ;; Recalculate the table before this line
15211 (beginning-of-line 1)
15212 (skip-chars-backward " \r\n\t")
15213 (if (org-at-table-p)
15214 (org-call-with-arg 'org-table-recalculate (or arg t)))))
15216 ; (org-set-regexps-and-options)
15217 ; (org-restart-font-lock)
15218 (let ((org-inhibit-startup t)) (org-mode-restart))
15219 (message "Local setup has been refreshed"))))
15220 ((org-clock-update-time-maybe))
15221 (t (error "C-c C-c can do nothing useful at this location")))))
15223 (defun org-mode-restart ()
15224 "Restart Org-mode, to scan again for special lines.
15225 Also updates the keyword regular expressions."
15228 (message "Org-mode restarted"))
15230 (defun org-kill-note-or-show-branches ()
15231 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
15233 (if (not org-finish-function)
15234 (call-interactively 'show-branches)
15235 (let ((org-note-abort t))
15236 (funcall org-finish-function))))
15238 (defun org-return (&optional indent)
15239 "Goto next table row or insert a newline.
15240 Calls `org-table-next-row' or `newline', depending on context.
15241 See the individual commands for more information."
15244 ((bobp) (if indent (newline-and-indent) (newline)))
15246 (org-table-justify-field-maybe)
15247 (call-interactively 'org-table-next-row))
15248 ((and org-return-follows-link
15249 (eq (get-text-property (point) 'face) 'org-link))
15250 (call-interactively 'org-open-at-point))
15251 ((and (org-at-heading-p)
15253 (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
15257 (t (if indent (newline-and-indent) (newline)))))
15259 (defun org-return-indent ()
15260 "Goto next table row or insert a newline and indent.
15261 Calls `org-table-next-row' or `newline-and-indent', depending on
15262 context. See the individual commands for more information."
15266 (defun org-ctrl-c-star ()
15267 "Compute table, or change heading status of lines.
15268 Calls `org-table-recalculate' or `org-toggle-heading',
15269 depending on context."
15273 (call-interactively 'org-table-recalculate))
15275 ;; Convert all lines in region to list items
15276 (call-interactively 'org-toggle-heading))))
15278 (defun org-ctrl-c-minus ()
15279 "Insert separator line in table or modify bullet status of line.
15280 Also turns a plain line or a region of lines into list items.
15281 Calls `org-table-insert-hline', `org-toggle-item', or
15282 `org-cycle-list-bullet', depending on context."
15286 (call-interactively 'org-table-insert-hline))
15287 ((org-region-active-p)
15288 (call-interactively 'org-toggle-item))
15290 (call-interactively 'org-cycle-list-bullet))
15292 (call-interactively 'org-toggle-item))))
15294 (defun org-toggle-item ()
15295 "Convert headings or normal lines to items, items to normal lines.
15296 If there is no active region, only the current line is considered.
15298 If the first line in the region is a headline, convert all headlines to items.
15300 If the first line in the region is an item, convert all items to normal lines.
15302 If the first line is normal text, add an item bullet to each line."
15304 (let (l2 l beg end)
15305 (if (org-region-active-p)
15306 (setq beg (region-beginning) end (region-end))
15307 (setq beg (point-at-bol)
15308 end (min (1+ (point-at-eol)) (point-max))))
15311 (setq l2 (org-current-line))
15313 (beginning-of-line 1)
15314 (setq l (1- (org-current-line)))
15315 (if (org-at-item-p)
15316 ;; We already have items, de-itemize
15317 (while (< (setq l (1+ l)) l2)
15318 (when (org-at-item-p)
15319 (goto-char (match-beginning 2))
15320 (delete-region (match-beginning 2) (match-end 2))
15321 (and (looking-at "[ \t]+") (replace-match "")))
15322 (beginning-of-line 2))
15323 (if (org-on-heading-p)
15324 ;; Headings, convert to items
15325 (while (< (setq l (1+ l)) l2)
15326 (if (looking-at org-outline-regexp)
15327 (replace-match "- " t t))
15328 (beginning-of-line 2))
15329 ;; normal lines, turn them into items
15330 (while (< (setq l (1+ l)) l2)
15331 (unless (org-at-item-p)
15332 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
15333 (replace-match "\\1- \\2")))
15334 (beginning-of-line 2)))))))
15336 (defun org-toggle-heading (&optional nstars)
15337 "Convert headings to normal text, or items or text to headings.
15338 If there is no active region, only the current line is considered.
15340 If the first line is a heading, remove the stars from all headlines
15343 If the first line is a plain list item, turn all plain list items
15346 If the first line is a normal line, turn each and every line in the
15347 region into a heading.
15349 When converting a line into a heading, the number of stars is chosen
15350 such that the lines become children of the current entry. However,
15351 when a prefix argument is given, its value determines the number of
15354 (let (l2 l itemp beg end)
15355 (if (org-region-active-p)
15356 (setq beg (region-beginning) end (region-end))
15357 (setq beg (point-at-bol)
15358 end (min (1+ (point-at-eol)) (point-max))))
15361 (setq l2 (org-current-line))
15363 (beginning-of-line 1)
15364 (setq l (1- (org-current-line)))
15365 (if (org-on-heading-p)
15366 ;; We already have headlines, de-star them
15367 (while (< (setq l (1+ l)) l2)
15368 (when (org-on-heading-p t)
15369 (and (looking-at outline-regexp) (replace-match "")))
15370 (beginning-of-line 2))
15371 (setq itemp (org-at-item-p))
15374 (make-string (prefix-numeric-value current-prefix-arg)
15377 (if (re-search-backward org-complex-heading-regexp nil t)
15378 (match-string 1) ""))))
15379 (add-stars (cond (nstars "")
15380 ((equal stars "") "*")
15381 (org-odd-levels-only "**")
15383 (rpl (concat stars add-stars " ")))
15384 (while (< (setq l (1+ l)) l2)
15386 (and (org-at-item-p) (replace-match rpl t t))
15387 (unless (org-on-heading-p)
15388 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
15389 (replace-match (concat rpl (match-string 2))))))
15390 (beginning-of-line 2)))))))
15392 (defun org-meta-return (&optional arg)
15393 "Insert a new heading or wrap a region in a table.
15394 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
15395 See the individual commands for more information."
15398 ((run-hook-with-args-until-success 'org-metareturn-hook))
15400 (call-interactively 'org-table-wrap-region))
15401 (t (call-interactively 'org-insert-heading))))
15405 ;; Define the Org-mode menus
15406 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
15408 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
15409 ["Next Field" org-cycle (org-at-table-p)]
15410 ["Previous Field" org-shifttab (org-at-table-p)]
15411 ["Next Row" org-return (org-at-table-p)]
15413 ["Blank Field" org-table-blank-field (org-at-table-p)]
15414 ["Edit Field" org-table-edit-field (org-at-table-p)]
15415 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
15418 ["Move Column Left" org-metaleft (org-at-table-p)]
15419 ["Move Column Right" org-metaright (org-at-table-p)]
15420 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
15421 ["Insert Column" org-shiftmetaright (org-at-table-p)])
15423 ["Move Row Up" org-metaup (org-at-table-p)]
15424 ["Move Row Down" org-metadown (org-at-table-p)]
15425 ["Delete Row" org-shiftmetaup (org-at-table-p)]
15426 ["Insert Row" org-shiftmetadown (org-at-table-p)]
15427 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
15429 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
15431 ["Copy Rectangle" org-copy-special (org-at-table-p)]
15432 ["Cut Rectangle" org-cut-special (org-at-table-p)]
15433 ["Paste Rectangle" org-paste-special (org-at-table-p)]
15434 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
15437 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
15438 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
15439 ["Edit Formulas" org-edit-special (org-at-table-p)]
15441 ["Recalculate line" org-table-recalculate (org-at-table-p)]
15442 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
15443 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
15445 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
15447 ["Sum Column/Rectangle" org-table-sum
15448 (or (org-at-table-p) (org-region-active-p))]
15449 ["Which Column?" org-table-current-column (org-at-table-p)])
15451 org-table-toggle-formula-debugger
15452 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
15453 ["Show Col/Row Numbers"
15454 org-table-toggle-coordinate-overlays
15456 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
15458 ["Create" org-table-create (and (not (org-at-table-p))
15459 org-enable-table-editor)]
15460 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
15461 ["Import from File" org-table-import (not (org-at-table-p))]
15462 ["Export to File" org-table-export (org-at-table-p)]
15464 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
15466 (easy-menu-define org-org-menu org-mode-map "Org menu"
15469 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
15470 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
15471 ["Sparse Tree..." org-sparse-tree t]
15472 ["Reveal Context" org-reveal t]
15473 ["Show All" show-all t]
15475 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
15477 ["New Heading" org-insert-heading t]
15478 ("Navigate Headings"
15479 ["Up" outline-up-heading t]
15480 ["Next" outline-next-visible-heading t]
15481 ["Previous" outline-previous-visible-heading t]
15482 ["Next Same Level" outline-forward-same-level t]
15483 ["Previous Same Level" outline-backward-same-level t]
15485 ["Jump" org-goto t])
15487 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
15488 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
15490 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
15491 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
15492 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
15494 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
15496 ["Promote Heading" org-metaleft (not (org-at-table-p))]
15497 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
15498 ["Demote Heading" org-metaright (not (org-at-table-p))]
15499 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
15501 ["Sort Region/Children" org-sort (not (org-at-table-p))]
15503 ["Convert to odd levels" org-convert-to-odd-levels t]
15504 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
15506 ["Emphasis..." org-emphasize t]
15507 ["Edit Source Example" org-edit-special t]
15509 ["Footnote new/jump" org-footnote-action t]
15510 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
15512 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
15513 ; ["Check and Tag Children" (org-toggle-archive-tag (4))
15514 ; :active t :keys "C-u C-c C-x C-a"]
15515 ["Sparse trees open ARCHIVE trees"
15516 (setq org-sparse-tree-open-archived-trees
15517 (not org-sparse-tree-open-archived-trees))
15518 :style toggle :selected org-sparse-tree-open-archived-trees]
15519 ["Cycling opens ARCHIVE trees"
15520 (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
15521 :style toggle :selected org-cycle-open-archived-trees]
15523 ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
15524 ["Move Subtree to Archive" org-advertized-archive-subtree t]
15525 ; ["Check and Move Children" (org-archive-subtree '(4))
15526 ; :active t :keys "C-u C-c C-x C-s"]
15530 ["Store Link (Global)" org-store-link t]
15531 ["Find existing link to here" org-occur-link-in-agenda-files t]
15532 ["Insert Link" org-insert-link t]
15533 ["Follow Link" org-open-at-point t]
15535 ["Next link" org-next-link t]
15536 ["Previous link" org-previous-link t]
15538 ["Descriptive Links"
15539 (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
15541 :selected (member '(org-link) buffer-invisibility-spec)]
15544 (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
15546 :selected (not (member '(org-link) buffer-invisibility-spec))])
15549 ["TODO/DONE/-" org-todo t]
15551 ["Next keyword" org-shiftright (org-on-heading-p)]
15552 ["Previous keyword" org-shiftleft (org-on-heading-p)]
15553 ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
15554 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
15555 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
15556 ["Show TODO Tree" org-show-todo-tree t]
15557 ["Global TODO list" org-todo-list t]
15559 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
15560 :selected org-enforce-todo-dependencies :style toggle :active t]
15561 "Settings for tree at point"
15562 ["Do Children sequentially" org-toggle-ordered-property :style radio
15563 :selected (ignore-errors (org-entry-get nil "ORDERED"))
15564 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15565 ["Do Children parallel" org-toggle-ordered-property :style radio
15566 :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
15567 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
15569 ["Set Priority" org-priority t]
15570 ["Priority Up" org-shiftup t]
15571 ["Priority Down" org-shiftdown t]
15573 ["Get news from all feeds" org-feed-update-all t]
15574 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
15575 ["Customize feeds" (customize-variable 'org-feed-alist) t])
15576 ("TAGS and Properties"
15577 ["Set Tags" org-set-tags-command t]
15578 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
15580 ["Set property" org-set-property t]
15581 ["Column view of properties" org-columns t]
15582 ["Insert Column View DBlock" org-insert-columns-dblock t])
15583 ("Dates and Scheduling"
15584 ["Timestamp" org-time-stamp t]
15585 ["Timestamp (inactive)" org-time-stamp-inactive t]
15587 ["1 Day Later" org-shiftright t]
15588 ["1 Day Earlier" org-shiftleft t]
15589 ["1 ... Later" org-shiftup t]
15590 ["1 ... Earlier" org-shiftdown t])
15591 ["Compute Time Range" org-evaluate-time-range t]
15592 ["Schedule Item" org-schedule t]
15593 ["Deadline" org-deadline t]
15595 ["Custom time format" org-toggle-time-stamp-overlays
15596 :style radio :selected org-display-custom-times]
15598 ["Goto Calendar" org-goto-calendar t]
15599 ["Date from Calendar" org-date-from-calendar t]
15601 ["Start/Restart Timer" org-timer-start t]
15602 ["Pause/Continue Timer" org-timer-pause-or-continue t]
15603 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
15604 ["Insert Timer String" org-timer t]
15605 ["Insert Timer Item" org-timer-item t])
15607 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
15608 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
15609 ["Clock out" org-clock-out t]
15610 ["Clock cancel" org-clock-cancel t]
15612 ["Mark as default task" org-clock-mark-default-task t]
15613 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
15614 ["Goto running clock" org-clock-goto t]
15616 ["Display times" org-clock-display t]
15617 ["Create clock table" org-clock-report t]
15619 ["Record DONE time"
15620 (progn (setq org-log-done (not org-log-done))
15621 (message "Switching to %s will %s record a timestamp"
15622 (car org-done-keywords)
15623 (if org-log-done "automatically" "not")))
15624 :style toggle :selected org-log-done])
15626 ["Agenda Command..." org-agenda t]
15627 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
15628 ("File List for Agenda")
15629 ("Special views current file"
15630 ["TODO Tree" org-show-todo-tree t]
15631 ["Check Deadlines" org-check-deadlines t]
15632 ["Timeline" org-timeline t]
15633 ["Tags/Property tree" org-match-sparse-tree t])
15635 ["Export/Publish..." org-export t]
15637 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
15638 :selected org-cdlatex-mode]
15639 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
15640 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
15641 ["Modify math symbol" org-cdlatex-math-modify
15642 (org-inside-LaTeX-fragment-p)]
15643 ["Insert citation" org-reftex-citation t]
15645 ["Export LaTeX fragments as images"
15646 (if (featurep 'org-exp)
15647 (setq org-export-with-LaTeX-fragments
15648 (not org-export-with-LaTeX-fragments))
15649 (require 'org-exp))
15650 :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
15651 org-export-with-LaTeX-fragments)])
15654 ["Push Files and Views" org-mobile-push t]
15655 ["Get Captured and Flagged" org-mobile-pull t]
15656 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
15658 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
15661 ["Show Version" org-version t]
15662 ["Info Documentation" org-info t])
15664 ["Browse Org Group" org-customize t]
15666 ["Expand This Menu" org-create-customize-menu
15667 (fboundp 'customize-menu-create)])
15668 ["Send bug report" org-submit-bug-report t]
15671 ["Refresh setup current buffer" org-mode-restart t]
15672 ["Reload Org (after update)" org-reload t]
15673 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
15676 (defun org-info (&optional node)
15677 "Read documentation for Org-mode in the info system.
15678 With optional NODE, go directly to that node."
15680 (info (format "(org)%s" (or node ""))))
15683 (defun org-submit-bug-report ()
15684 "Submit a bug report on Org-mode via mail.
15686 Don't hesitate to report any problems or inaccurate documentation.
15688 If you don't have setup sending mail from (X)Emacs, please copy the
15689 output buffer into your mail program, as it gives us important
15690 information about your Org-mode version and configuration."
15692 (require 'reporter)
15693 (org-load-modules-maybe)
15694 (org-require-autoloaded-modules)
15695 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
15696 (reporter-submit-bug-report
15697 "emacs-orgmode@gnu.org"
15700 (save-window-excursion
15701 (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
15702 (delete-other-windows)
15704 (insert "You are about to submit a bug report to the Org-mode mailing list.
15706 We would like to add your full Org-mode and Outline configuration to the
15707 bug report. This greatly simplifies the work of the maintainer and
15708 other experts on the mailing list.
15710 HOWEVER, some variables you have customized may contain private
15711 information. The names of customers, colleagues, or friends, might
15712 appear in the form of file names, tags, todo states, or search strings.
15713 If you answer yes to the prompt, you might want to check and remove
15714 such private information before sending the email.")
15715 (add-text-properties (point-min) (point-max) '(face org-warning))
15716 (when (yes-or-no-p "Include your Org-mode configuration ")
15720 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
15721 (or (and (symbol-value v)
15722 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
15724 (get v 'custom-type) (get v 'standard-value)
15725 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
15727 (kill-buffer (get-buffer "*Warn about privacy*"))
15730 "Remember to cover the basics, that is, what you expected to happen and
15731 what in fact did happen. You don't know how to make a good report? See
15733 http://orgmode.org/manual/Feedback.html#Feedback
15735 Your bug report will be posted to the Org-mode mailing list.
15736 ------------------------------------------------------------------------")))
15738 (defun org-install-agenda-files-menu ()
15739 (let ((bl (buffer-list)))
15742 (set-buffer (pop bl))
15743 (if (org-mode-p) (setq bl nil)))
15746 '("Org") "File List for Agenda"
15749 ["Edit File List" (org-edit-agenda-file-list) t]
15750 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
15751 ["Remove Current File from List" org-remove-file t]
15752 ["Cycle through agenda files" org-cycle-agenda-files t]
15753 ["Occur in all agenda files" org-occur-in-agenda-files t]
15755 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
15760 (defun org-require-autoloaded-modules ()
15763 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
15764 org-docbook org-exp org-html org-icalendar
15766 org-publish org-remember org-table
15767 org-timer org-xoxo)))
15770 (defun org-reload (&optional uncompiled)
15771 "Reload all org lisp files.
15772 With prefix arg UNCOMPILED, load the uncompiled versions."
15774 (require 'find-func)
15775 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
15776 (dir-org (file-name-directory (org-find-library-name "org")))
15777 (dir-org-contrib (ignore-errors
15778 (file-name-directory
15779 (org-find-library-name "org-contribdir"))))
15781 (append (directory-files dir-org t file-re)
15782 (and dir-org-contrib
15783 (directory-files dir-org-contrib t file-re))))
15784 (remove-re (concat (if (featurep 'xemacs)
15785 "org-colview" "org-colview-xemacs")
15787 (setq files (mapcar 'file-name-sans-extension files))
15788 (setq files (mapcar
15789 (lambda (x) (if (string-match remove-re x) nil x))
15791 (setq files (delq nil files))
15794 (when (featurep (intern (file-name-nondirectory f)))
15795 (if (and (not uncompiled)
15796 (file-exists-p (concat f ".elc")))
15797 (load (concat f ".elc") nil nil t)
15798 (load (concat f ".el") nil nil t))))
15803 (defun org-customize ()
15804 "Call the customize function with org as argument."
15806 (org-load-modules-maybe)
15807 (org-require-autoloaded-modules)
15808 (customize-browse 'org))
15810 (defun org-create-customize-menu ()
15811 "Create a full customization menu for Org-mode, insert it into the menu."
15813 (org-load-modules-maybe)
15814 (org-require-autoloaded-modules)
15815 (if (fboundp 'customize-menu-create)
15818 '("Org") "Customize"
15819 `(["Browse Org group" org-customize t]
15821 ,(customize-menu-create 'org)
15822 ["Set" Custom-set t]
15823 ["Save" Custom-save t]
15824 ["Reset to Current" Custom-reset-current t]
15825 ["Reset to Saved" Custom-reset-saved t]
15826 ["Reset to Standard Settings" Custom-reset-standard t]))
15827 (message "\"Org\"-menu now contains full customization menu"))
15828 (error "Cannot expand menu (outdated version of cus-edit.el)")))
15830 ;;;; Miscellaneous stuff
15832 ;;; Generally useful functions
15834 (defun org-get-at-bol (property)
15835 "Get text property PROPERTY at beginning of line."
15836 (get-text-property (point-at-bol) property))
15838 (defun org-find-text-property-in-string (prop s)
15839 "Return the first non-nil value of property PROP in string S."
15840 (or (get-text-property 0 prop s)
15841 (get-text-property (or (next-single-property-change 0 prop s) 0)
15844 (defun org-display-warning (message) ;; Copied from Emacs-Muse
15845 "Display the given MESSAGE as a warning."
15846 (if (fboundp 'display-warning)
15847 (display-warning 'org message
15848 (if (featurep 'xemacs)
15851 (let ((buf (get-buffer-create "*Org warnings*")))
15852 (with-current-buffer buf
15853 (goto-char (point-max))
15854 (insert "Warning (Org): " message)
15857 (display-buffer buf)
15860 (defun org-in-commented-line ()
15861 "Is point in a line starting with `#'?"
15862 (equal (char-after (point-at-bol)) ?#))
15864 (defun org-goto-marker-or-bmk (marker &optional bookmark)
15865 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
15866 (if (and marker (marker-buffer marker)
15867 (buffer-live-p (marker-buffer marker)))
15869 (switch-to-buffer (marker-buffer marker))
15870 (if (or (> marker (point-max)) (< marker (point-min)))
15873 (org-show-context 'org-goto))
15875 (bookmark-jump bookmark)
15876 (error "Cannot find location"))))
15878 (defun org-quote-csv-field (s)
15879 "Quote field for inclusion in CSV material."
15880 (if (string-match "[\",]" s)
15881 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
15884 (defun org-plist-delete (plist property)
15885 "Delete PROPERTY from PLIST.
15886 This is in contrast to merely setting it to 0."
15889 (if (not (eq property (car plist)))
15890 (setq p (plist-put p (car plist) (nth 1 plist))))
15891 (setq plist (cddr plist)))
15894 (defun org-force-self-insert (N)
15895 "Needed to enforce self-insert under remapping."
15897 (self-insert-command N))
15899 (defun org-string-width (s)
15900 "Compute width of string, ignoring invisible characters.
15901 This ignores character with invisibility property `org-link', and also
15902 characters with property `org-cwidth', because these will become invisible
15903 upon the next fontification round."
15905 (when (or (eq t buffer-invisibility-spec)
15906 (assq 'org-link buffer-invisibility-spec))
15907 (while (setq b (text-property-any 0 (length s)
15908 'invisible 'org-link s))
15909 (setq s (concat (substring s 0 b)
15910 (substring s (or (next-single-property-change
15911 b 'invisible s) (length s)))))))
15912 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
15913 (setq s (concat (substring s 0 b)
15914 (substring s (or (next-single-property-change
15915 b 'org-cwidth s) (length s))))))
15916 (setq l (string-width s) b -1)
15917 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
15918 (setq l (- l (get-text-property b 'org-dwidth-n s))))
15921 (defun org-get-indentation (&optional line)
15922 "Get the indentation of the current line, interpreting tabs.
15923 When LINE is given, assume it represents a line and compute its indentation."
15925 (if (string-match "^ *" (org-remove-tabs line))
15928 (beginning-of-line 1)
15929 (skip-chars-forward " \t")
15930 (current-column))))
15932 (defun org-remove-tabs (s &optional width)
15933 "Replace tabulators in S with spaces.
15934 Assumes that s is a single line, starting in column 0."
15935 (setq width (or width tab-width))
15936 (while (string-match "\t" s)
15937 (setq s (replace-match
15939 (- (* width (/ (+ (match-beginning 0) width) width))
15940 (match-beginning 0)) ?\ )
15944 (defun org-fix-indentation (line ind)
15945 "Fix indentation in LINE.
15946 IND is a cons cell with target and minimum indentation.
15947 If the current indentation in LINE is smaller than the minimum,
15948 leave it alone. If it is larger than ind, set it to the target."
15949 (let* ((l (org-remove-tabs line))
15950 (i (org-get-indentation l))
15951 (i1 (car ind)) (i2 (cdr ind)))
15952 (if (>= i i2) (setq l (substring line i2)))
15954 (concat (make-string i1 ?\ ) l)
15957 (defun org-remove-indentation (code &optional n)
15958 "Remove the maximum common indentation from the lines in CODE.
15959 N may optionally be the number of spaces to remove."
15962 (org-do-remove-indentation n)
15965 (defun org-do-remove-indentation (&optional n)
15966 "Remove the maximum common indentation from the buffer."
15967 (untabify (point-min) (point-max))
15968 (let ((min 10000) re)
15971 (goto-char (point-min))
15972 (while (re-search-forward "^ *[^ \n]" nil t)
15973 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
15974 (unless (or (= min 0) (= min 10000))
15975 (setq re (format "^ \\{%d\\}" min))
15976 (goto-char (point-min))
15977 (while (re-search-forward re nil t)
15982 (defun org-fill-template (template alist)
15983 "Find each %key of ALIST in TEMPLATE and replace it."
15984 (let (entry key value)
15985 (setq alist (sort (copy-sequence alist)
15986 (lambda (a b) (< (length (car a)) (length (car b))))))
15987 (while (setq entry (pop alist))
15989 (replace-regexp-in-string
15990 (concat "%" (regexp-quote (car entry)))
15991 (cdr entry) template t t)))
15994 (defun org-base-buffer (buffer)
15995 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
15998 (or (buffer-base-buffer buffer)
16001 (defun org-trim (s)
16002 "Remove whitespace at beginning and end of string."
16003 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
16004 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
16007 (defun org-wrap (string &optional width lines)
16008 "Wrap string to either a number of lines, or a width in characters.
16009 If WIDTH is non-nil, the string is wrapped to that width, however many lines
16010 that costs. If there is a word longer than WIDTH, the text is actually
16011 wrapped to the length of that word.
16012 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
16013 many lines, whatever width that takes.
16014 The return value is a list of lines, without newlines at the end."
16015 (let* ((words (org-split-string string "[ \t\n]+"))
16016 (maxword (apply 'max (mapcar 'org-string-width words)))
16019 (org-do-wrap words (max maxword width)))
16022 (setq ll (org-do-wrap words maxword))
16023 (if (<= (length ll) lines)
16026 (while (> (length ll) lines)
16028 (setq ll (org-do-wrap words w)))
16030 (t (error "Cannot wrap this")))))
16032 (defun org-do-wrap (words width)
16033 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
16036 (setq line (pop words))
16037 (while (and words (< (+ (length line) (length (car words))) width))
16038 (setq line (concat line " " (pop words))))
16039 (setq lines (push line lines)))
16042 (defun org-split-string (string &optional separators)
16043 "Splits STRING into substrings at SEPARATORS.
16044 No empty strings are returned if there are matches at the beginning
16045 and end of string."
16046 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
16050 (while (and (string-match rexp string
16052 (= start (match-beginning 0))
16053 (< start (length string)))
16055 (< (match-beginning 0) (length string)))
16057 (or (eq (match-beginning 0) 0)
16058 (and (eq (match-beginning 0) (match-end 0))
16059 (eq (match-beginning 0) start))
16061 (cons (substring string start (match-beginning 0))
16063 (setq start (match-end 0)))
16064 (or (eq start (length string))
16066 (cons (substring string start)
16070 (defun org-quote-vert (s)
16071 "Replace \"|\" with \"\\vert\"."
16072 (while (string-match "|" s)
16073 (setq s (replace-match "\\vert" t t s)))
16076 (defun org-uuidgen-p (s)
16077 "Is S an ID created by UUIDGEN?"
16078 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
16080 (defun org-context ()
16081 "Return a list of contexts of the current cursor position.
16082 If several contexts apply, all are returned.
16083 Each context entry is a list with a symbol naming the context, and
16084 two positions indicating start and end of the context. Possible
16087 :headline anywhere in a headline
16088 :headline-stars on the leading stars in a headline
16089 :todo-keyword on a TODO keyword (including DONE) in a headline
16090 :tags on the TAGS in a headline
16091 :priority on the priority cookie in a headline
16092 :item on the first line of a plain list item
16093 :item-bullet on the bullet/number of a plain list item
16094 :checkbox on the checkbox in a plain list item
16095 :table in an org-mode table
16096 :table-special on a special filed in a table
16097 :table-table in a table.el table
16098 :link on a hyperlink
16099 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
16100 :target on a <<target>>
16101 :radio-target on a <<<radio-target>>>
16102 :latex-fragment on a LaTeX fragment
16103 :latex-preview on a LaTeX fragment with overlayed preview image
16105 This function expects the position to be visible because it uses font-lock
16106 faces as a help to recognize the following contexts: :table-special, :link,
16108 (let* ((f (get-text-property (point) 'face))
16109 (faces (if (listp f) f (list f)))
16110 (p (point)) clist o)
16111 ;; First the large context
16113 ((org-on-heading-p t)
16114 (push (list :headline (point-at-bol) (point-at-eol)) clist)
16116 (beginning-of-line 1)
16117 (looking-at org-todo-line-tags-regexp))
16118 (push (org-point-in-group p 1 :headline-stars) clist)
16119 (push (org-point-in-group p 2 :todo-keyword) clist)
16120 (push (org-point-in-group p 4 :tags) clist))
16122 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
16123 (if (looking-at "\\[#[A-Z0-9]\\]")
16124 (push (org-point-in-group p 0 :priority) clist)))
16127 (push (org-point-in-group p 2 :item-bullet) clist)
16128 (push (list :item (point-at-bol)
16129 (save-excursion (org-end-of-item) (point)))
16131 (and (org-at-item-checkbox-p)
16132 (push (org-point-in-group p 0 :checkbox) clist)))
16135 (push (list :table (org-table-begin) (org-table-end)) clist)
16136 (if (memq 'org-formula faces)
16137 (push (list :table-special
16138 (previous-single-property-change p 'face)
16139 (next-single-property-change p 'face)) clist)))
16140 ((org-at-table-p 'any)
16141 (push (list :table-table) clist)))
16144 ;; Now the small context
16146 ((org-at-timestamp-p)
16147 (push (org-point-in-group p 0 :timestamp) clist))
16148 ((memq 'org-link faces)
16150 (previous-single-property-change p 'face)
16151 (next-single-property-change p 'face)) clist))
16152 ((memq 'org-special-keyword faces)
16153 (push (list :keyword
16154 (previous-single-property-change p 'face)
16155 (next-single-property-change p 'face)) clist))
16157 (push (org-point-in-group p 0 :target) clist)
16158 (goto-char (1- (match-beginning 0)))
16159 (if (looking-at org-radio-target-regexp)
16160 (push (org-point-in-group p 0 :radio-target) clist))
16162 ((setq o (car (delq nil
16165 (if (memq x org-latex-fragment-image-overlays) x))
16166 (org-overlays-at (point))))))
16167 (push (list :latex-fragment
16168 (org-overlay-start o) (org-overlay-end o)) clist)
16169 (push (list :latex-preview
16170 (org-overlay-start o) (org-overlay-end o)) clist))
16171 ((org-inside-LaTeX-fragment-p)
16172 ;; FIXME: positions wrong.
16173 (push (list :latex-fragment (point) (point)) clist)))
16175 (setq clist (nreverse (delq nil clist)))
16178 ;; FIXME: Compare with at-regexp-p Do we need both?
16179 (defun org-in-regexp (re &optional nlines visually)
16180 "Check if point is inside a match of regexp.
16181 Normally only the current line is checked, but you can include NLINES extra
16182 lines both before and after point into the search.
16183 If VISUALLY is set, require that the cursor is not after the match but
16184 really on, so that the block visually is on the match."
16186 (let ((pos (point))
16187 (eol (point-at-eol (+ 1 (or nlines 0))))
16188 (inc (if visually 1 0)))
16190 (beginning-of-line (- 1 (or nlines 0)))
16191 (while (re-search-forward re eol t)
16192 (if (and (<= (match-beginning 0) pos)
16193 (>= (+ inc (match-end 0)) pos))
16194 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
16196 (defun org-at-regexp-p (regexp)
16197 "Is point inside a match of REGEXP in the current line?"
16200 (let ((pos (point)) (end (point-at-eol)))
16201 (beginning-of-line 1)
16202 (while (re-search-forward regexp end t)
16203 (if (and (<= (match-beginning 0) pos)
16204 (>= (match-end 0) pos))
16208 (defun org-occur-in-agenda-files (regexp &optional nlines)
16209 "Call `multi-occur' with buffers for all agenda files."
16210 (interactive "sOrg-files matching: \np")
16211 (let* ((files (org-agenda-files))
16212 (tnames (mapcar 'file-truename files))
16213 (extra org-agenda-text-search-extra-files)
16215 (when (eq (car extra) 'agenda-archives)
16216 (setq extra (cdr extra))
16217 (setq files (org-add-archive-files files)))
16218 (while (setq f (pop extra))
16219 (unless (member (file-truename f) tnames)
16220 (add-to-list 'files f 'append)
16221 (add-to-list 'tnames (file-truename f) 'append)))
16223 (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
16226 (if (boundp 'occur-mode-find-occurrence-hook)
16228 (add-hook 'occur-mode-find-occurrence-hook
16233 (defadvice occur-mode-goto-occurrence
16234 (after org-occur-reveal activate)
16235 (and (org-mode-p) (org-reveal)))
16236 (defadvice occur-mode-goto-occurrence-other-window
16237 (after org-occur-reveal activate)
16238 (and (org-mode-p) (org-reveal)))
16239 (defadvice occur-mode-display-occurrence
16240 (after org-occur-reveal activate)
16242 (let ((pos (occur-mode-find-occurrence)))
16243 (with-current-buffer (marker-buffer pos)
16248 (defun org-occur-link-in-agenda-files ()
16249 "Create a link and search for it in the agendas.
16250 The link is not stored in `org-stored-links', it is just created
16251 for the search purpose."
16253 (let ((link (condition-case nil
16254 (org-store-link nil)
16255 (error "Unable to create a link to here"))))
16256 (org-occur-in-agenda-files (regexp-quote link))))
16258 (defun org-uniquify (list)
16259 "Remove duplicate elements from LIST."
16261 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
16264 (defun org-delete-all (elts list)
16265 "Remove all elements in ELTS from LIST."
16267 (setq list (delete (pop elts) list)))
16270 (defun org-back-over-empty-lines ()
16271 "Move backwards over whitespace, to the beginning of the first empty line.
16272 Returns the number of empty lines passed."
16273 (let ((pos (point)))
16274 (skip-chars-backward " \t\n\r")
16275 (beginning-of-line 2)
16276 (goto-char (min (point) pos))
16277 (count-lines (point) pos)))
16279 (defun org-skip-whitespace ()
16280 (skip-chars-forward " \t\n\r"))
16282 (defun org-point-in-group (point group &optional context)
16283 "Check if POINT is in match-group GROUP.
16284 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
16285 match. If the match group does ot exist or point is not inside it,
16287 (and (match-beginning group)
16288 (>= point (match-beginning group))
16289 (<= point (match-end group))
16291 (list context (match-beginning group) (match-end group))
16294 (defun org-switch-to-buffer-other-window (&rest args)
16295 "Switch to buffer in a second window on the current frame.
16296 In particular, do not allow pop-up frames."
16297 (let (pop-up-frames special-display-buffer-names special-display-regexps
16298 special-display-function)
16299 (apply 'switch-to-buffer-other-window args)))
16301 (defun org-combine-plists (&rest plists)
16302 "Create a single property list from all plists in PLISTS.
16303 The process starts by copying the first list, and then setting properties
16304 from the other lists. Settings in the last list are the most significant
16305 ones and overrule settings in the other lists."
16306 (let ((rtn (copy-sequence (pop plists)))
16309 (setq ls (pop plists))
16311 (setq p (pop ls) v (pop ls))
16312 (setq rtn (plist-put rtn p v))))
16315 (defun org-move-line-down (arg)
16316 "Move the current line down. With prefix argument, move it past ARG lines."
16318 (let ((col (current-column))
16320 (beginning-of-line 1) (setq beg (point))
16321 (beginning-of-line 2) (setq end (point))
16322 (beginning-of-line (+ 1 arg))
16323 (setq pos (move-marker (make-marker) (point)))
16324 (insert (delete-and-extract-region beg end))
16326 (org-move-to-column col)))
16328 (defun org-move-line-up (arg)
16329 "Move the current line up. With prefix argument, move it past ARG lines."
16331 (let ((col (current-column))
16333 (beginning-of-line 1) (setq beg (point))
16334 (beginning-of-line 2) (setq end (point))
16335 (beginning-of-line (- arg))
16336 (setq pos (move-marker (make-marker) (point)))
16337 (insert (delete-and-extract-region beg end))
16339 (org-move-to-column col)))
16341 (defun org-replace-escapes (string table)
16342 "Replace %-escapes in STRING with values in TABLE.
16343 TABLE is an association list with keys like \"%a\" and string values.
16344 The sequences in STRING may contain normal field width and padding information,
16345 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
16346 so values can contain further %-escapes if they are define later in TABLE."
16347 (let ((case-fold-search nil)
16349 (while (setq e (pop table))
16350 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
16351 (while (string-match re string)
16352 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
16354 (setq string (replace-match rpl t t string))))
16358 (defun org-sublist (list start end)
16359 "Return a section of LIST, from START to END.
16360 Counting starts at 1."
16361 (let (rtn (c start))
16362 (setq list (nthcdr (1- start) list))
16363 (while (and list (<= c end))
16364 (push (pop list) rtn)
16368 (defun org-find-base-buffer-visiting (file)
16369 "Like `find-buffer-visiting' but always return the base buffer and
16370 not an indirect buffer."
16371 (let ((buf (or (get-file-buffer file)
16372 (find-buffer-visiting file))))
16374 (or (buffer-base-buffer buf) buf)
16377 (defun org-image-file-name-regexp (&optional extensions)
16378 "Return regexp matching the file names of images.
16379 If EXTENSIONS is given, only match these."
16380 (if (and (not extensions) (fboundp 'image-file-name-regexp))
16381 (image-file-name-regexp)
16382 (let ((image-file-name-extensions
16384 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
16385 "xbm" "xpm" "pbm" "pgm" "ppm"))))
16387 (regexp-opt (nconc (mapcar 'upcase
16388 image-file-name-extensions)
16389 image-file-name-extensions)
16393 (defun org-file-image-p (file &optional extensions)
16394 "Return non-nil if FILE is an image."
16396 (string-match (org-image-file-name-regexp extensions) file)))
16398 (defun org-get-cursor-date ()
16399 "Return the date at cursor in as a time.
16400 This works in the calendar and in the agenda, anywhere else it just
16401 returns the current time."
16402 (let (date day defd)
16404 ((eq major-mode 'calendar-mode)
16405 (setq date (calendar-cursor-to-date)
16406 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
16407 ((eq major-mode 'org-agenda-mode)
16408 (setq day (get-text-property (point) 'day))
16410 (setq date (calendar-gregorian-from-absolute day)
16411 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
16413 (or defd (current-time))))
16415 (defvar org-agenda-action-marker (make-marker)
16416 "Marker pointing to the entry for the next agenda action.")
16418 (defun org-mark-entry-for-agenda-action ()
16419 "Mark the current entry as target of an agenda action.
16420 Agenda actions are actions executed from the agenda with the key `k',
16421 which make use of the date at the cursor."
16423 (move-marker org-agenda-action-marker
16424 (save-excursion (org-back-to-heading t) (point))
16427 "Entry marked for action; press `k' at desired date in agenda or calendar"))
16429 ;;; Paragraph filling stuff.
16430 ;; We want this to be just right, so use the full arsenal.
16432 (defun org-indent-line-function ()
16433 "Indent line like previous, but further if previous was headline or item."
16435 (let* ((pos (point))
16436 (itemp (org-at-item-p))
16437 (case-fold-search t)
16438 (org-drawer-regexp (or org-drawer-regexp "\000"))
16439 column bpos bcol tpos tcol bullet btype bullet-type)
16440 ;; Find the previous relevant line
16441 (beginning-of-line 1)
16443 ((looking-at "#") (setq column 0))
16444 ((looking-at "\\*+ ") (setq column 0))
16445 ((and (looking-at "[ \t]*:END:")
16446 (save-excursion (re-search-backward org-drawer-regexp nil t)))
16448 (goto-char (1- (match-beginning 1)))
16449 (setq column (current-column))))
16450 ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
16452 (re-search-backward
16453 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
16454 (setq column (org-get-indentation (match-string 0))))
16456 (beginning-of-line 0)
16457 (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
16458 (not (looking-at "[ \t]*:END:"))
16459 (not (looking-at org-drawer-regexp)))
16460 (beginning-of-line 0))
16462 ((looking-at "\\*+[ \t]+")
16463 (if (not org-adapt-indentation)
16465 (goto-char (match-end 0))
16466 (setq column (current-column))))
16467 ((looking-at org-drawer-regexp)
16468 (goto-char (1- (match-beginning 1)))
16469 (setq column (current-column)))
16470 ((looking-at "\\([ \t]*\\):END:")
16471 (goto-char (match-end 1))
16472 (setq column (current-column)))
16474 (org-beginning-of-item)
16475 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
16476 (setq bpos (match-beginning 1) tpos (match-end 0)
16477 bcol (progn (goto-char bpos) (current-column))
16478 tcol (progn (goto-char tpos) (current-column))
16479 bullet (match-string 1)
16480 bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
16481 (if (> tcol (+ bcol org-description-max-indent))
16482 (setq tcol (+ bcol 5)))
16486 (beginning-of-line 1)
16487 (if (looking-at "\\S-")
16489 (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
16490 (setq bullet (match-string 1)
16491 btype (if (string-match "[0-9]" bullet) "n" bullet))
16492 (setq column (if (equal btype bullet-type) bcol tcol)))
16493 (setq column (org-get-indentation)))))
16494 (t (setq column (org-get-indentation))))))
16496 (if (<= (current-column) (current-indentation))
16497 (org-indent-line-to column)
16498 (save-excursion (org-indent-line-to column)))
16499 (setq column (current-column))
16500 (beginning-of-line 1)
16502 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
16503 (replace-match (concat (match-string 1)
16504 (format org-property-format
16505 (match-string 2) (match-string 3)))
16507 (org-move-to-column column)))
16509 (defun org-set-autofill-regexps ()
16511 ;; In the paragraph separator we include headlines, because filling
16512 ;; text in a line directly attached to a headline would otherwise
16513 ;; fill the headline as well.
16514 (org-set-local 'comment-start-skip "^#+[ \t]*")
16515 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
16516 ;; The paragraph starter includes hand-formatted lists.
16524 "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
16527 "\\\\\\(begin\\|end\\|[][]\\)"))
16528 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
16529 ;; But only if the user has not turned off tables or fixed-width regions
16531 'auto-fill-inhibit-regexp
16532 (concat "\\*+ \\|#\\+"
16533 "\\|[ \t]*" org-keyword-time-regexp
16534 (if (or org-enable-table-editor org-enable-fixed-width-editor)
16537 (if org-enable-table-editor "|" "")
16538 (if org-enable-fixed-width-editor ":" "")
16540 ;; We use our own fill-paragraph function, to make sure that tables
16541 ;; and fixed-width regions are not wrapped. That function will pass
16542 ;; through to `fill-paragraph' when appropriate.
16543 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
16544 ; Adaptive filling: To get full control, first make sure that
16545 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
16546 (org-set-local 'adaptive-fill-regexp "\000")
16547 (org-set-local 'adaptive-fill-function
16548 'org-adaptive-fill-function)
16550 'align-mode-rules-list
16551 '((org-in-buffer-settings
16552 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
16553 (modes . '(org-mode))))))
16555 (defun org-fill-paragraph (&optional justify)
16556 "Re-align a table, pass through to fill-paragraph if no table."
16557 (let ((table-p (org-at-table-p))
16558 (table.el-p (org-at-table.el-p)))
16559 (cond ((and (equal (char-after (point-at-bol)) ?*)
16560 (save-excursion (goto-char (point-at-bol))
16561 (looking-at outline-regexp)))
16562 t) ; skip headlines
16563 (table.el-p t) ; skip table.el tables
16564 (table-p (org-table-align) t) ; align org-mode tables
16565 (t nil)))) ; call paragraph-fill
16567 ;; For reference, this is the default value of adaptive-fill-regexp
16568 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
16570 (defun org-adaptive-fill-function ()
16571 "Return a fill prefix for org-mode files.
16572 In particular, this makes sure hanging paragraphs for hand-formatted lists
16574 (cond ((looking-at "#[ \t]+")
16576 ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
16578 (if (> (match-end 1) (+ (match-beginning 1)
16579 org-description-max-indent))
16580 (goto-char (+ (match-beginning 1) 5))
16581 (goto-char (match-end 0)))
16582 (make-string (current-column) ?\ )))
16583 ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
16585 (goto-char (match-end 0))
16586 (make-string (current-column) ?\ )))
16591 (defun org-toggle-fixed-width-section (arg)
16592 "Toggle the fixed-width export.
16593 If there is no active region, the QUOTE keyword at the current headline is
16594 inserted or removed. When present, it causes the text between this headline
16595 and the next to be exported as fixed-width text, and unmodified.
16596 If there is an active region, this command adds or removes a colon as the
16597 first character of this line. If the first character of a line is a colon,
16598 this line is also exported in fixed-width font."
16601 (regionp (org-region-active-p))
16602 (beg (if regionp (region-beginning) (point)))
16603 (end (if regionp (region-end)))
16604 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
16605 (case-fold-search nil)
16606 (re "[ \t]*\\(: \\)")
16611 (setq cc (current-column))
16612 (beginning-of-line 1)
16613 (setq off (looking-at re))
16614 (while (> nlines 0)
16615 (setq nlines (1- nlines))
16616 (beginning-of-line 1)
16619 (org-move-to-column cc t)
16622 ((and off (looking-at re))
16623 (replace-match "" t t nil 1))
16624 ((not off) (org-move-to-column cc t) (insert ": ")))
16627 (org-back-to-heading)
16628 (if (looking-at (concat outline-regexp
16629 "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
16630 (replace-match "" t t nil 1)
16631 (if (looking-at outline-regexp)
16633 (goto-char (match-end 0))
16634 (insert org-quote-string " "))))))))
16636 (defun org-reftex-citation ()
16637 "Use reftex-citation to insert a citation into the buffer.
16638 This looks for a line like
16640 #+BIBLIOGRAPHY: foo plain option:-d
16642 and derives from it that foo.bib is the bbliography file relevant
16643 for this document. It then installs the necessary environment for RefTeX
16644 to work in this buffer and calls `reftex-citation' to insert a citation
16647 Export of such citations to both LaTeX and HTML is handled by the contributed
16648 package org-exp-bibtex by Taru Karttunen."
16650 (let ((reftex-docstruct-symbol 'rds)
16651 (reftex-cite-format "\\cite{%l}")
16656 (let ((case-fold-search t)
16657 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
16658 (if (not (save-excursion
16659 (or (re-search-forward re nil t)
16660 (re-search-backward re nil t))))
16661 (error "No bibliography defined in file")
16662 (setq bib (concat (match-string 1) ".bib")
16663 rds (list (list 'bib bib)))))))
16664 (call-interactively 'reftex-citation)))
16666 ;;;; Functions extending outline functionality
16668 (defun org-beginning-of-line (&optional arg)
16669 "Go to the beginning of the current line. If that is invisible, continue
16670 to a visible line beginning. This makes the function of C-a more intuitive.
16671 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16672 first attempt, and only move to after the tags when the cursor is already
16673 beyond the end of the headline."
16675 (let ((pos (point))
16676 (special (if (consp org-special-ctrl-a/e)
16677 (car org-special-ctrl-a/e)
16678 org-special-ctrl-a/e))
16680 (if (org-bound-and-true-p line-move-visual)
16681 (beginning-of-visual-line 1)
16682 (beginning-of-line 1))
16683 (if (and arg (fboundp 'move-beginning-of-line))
16684 (call-interactively 'move-beginning-of-line)
16688 (if (org-invisible-p)
16689 (while (and (not (bobp)) (org-invisible-p))
16691 (beginning-of-line 1))
16692 (forward-char 1))))
16695 ((and (looking-at org-complex-heading-regexp)
16696 (= (char-after (match-end 1)) ?\ ))
16697 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
16701 (cond ((> pos refpos) refpos)
16702 ((= pos (point)) refpos)
16704 (cond ((> pos (point)) (point))
16705 ((not (eq last-command this-command)) (point))
16710 (cond ((> pos (match-end 4)) (match-end 4))
16711 ((= pos (point)) (match-end 4))
16713 (cond ((> pos (point)) (point))
16714 ((not (eq last-command this-command)) (point))
16715 (t (match-end 4))))))))
16717 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16719 (defun org-end-of-line (&optional arg)
16720 "Go to the end of the line.
16721 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
16722 first attempt, and only move to after the tags when the cursor is already
16723 beyond the end of the headline."
16725 (let ((special (if (consp org-special-ctrl-a/e)
16726 (cdr org-special-ctrl-a/e)
16727 org-special-ctrl-a/e)))
16728 (if (or (not special)
16729 (not (org-on-heading-p))
16731 (call-interactively
16732 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
16733 ((fboundp 'move-end-of-line) 'move-end-of-line)
16735 (let ((pos (point)))
16736 (beginning-of-line 1)
16737 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
16739 (if (or (< pos (match-beginning 1))
16740 (= pos (match-end 0)))
16741 (goto-char (match-beginning 1))
16742 (goto-char (match-end 0)))
16743 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
16744 (goto-char (match-end 0))
16745 (goto-char (match-beginning 1))))
16746 (call-interactively (if (fboundp 'move-end-of-line)
16750 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
16752 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
16753 (define-key org-mode-map "\C-e" 'org-end-of-line)
16754 (define-key org-mode-map [home] 'org-beginning-of-line)
16755 (define-key org-mode-map [end] 'org-end-of-line)
16757 (defun org-backward-sentence (&optional arg)
16758 "Go to beginning of sentence, or beginning of table field.
16759 This will call `backward-sentence' or `org-table-beginning-of-field',
16760 depending on context."
16763 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
16764 (t (call-interactively 'backward-sentence))))
16766 (defun org-forward-sentence (&optional arg)
16767 "Go to end of sentence, or end of table field.
16768 This will call `forward-sentence' or `org-table-end-of-field',
16769 depending on context."
16772 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
16773 (t (call-interactively 'forward-sentence))))
16775 (define-key org-mode-map "\M-a" 'org-backward-sentence)
16776 (define-key org-mode-map "\M-e" 'org-forward-sentence)
16778 (defun org-kill-line (&optional arg)
16779 "Kill line, to tags or end of line."
16782 ((or (not org-special-ctrl-k)
16784 (not (org-on-heading-p)))
16785 (call-interactively 'kill-line))
16786 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
16787 (kill-region (point) (match-beginning 1))
16788 (org-set-tags nil t))
16789 (t (kill-region (point) (point-at-eol)))))
16791 (define-key org-mode-map "\C-k" 'org-kill-line)
16793 (defun org-yank (&optional arg)
16794 "Yank. If the kill is a subtree, treat it specially.
16795 This command will look at the current kill and check if is a single
16796 subtree, or a series of subtrees[1]. If it passes the test, and if the
16797 cursor is at the beginning of a line or after the stars of a currently
16798 empty headline, then the yank is handled specially. How exactly depends
16799 on the value of the following variables, both set by default.
16801 org-yank-folded-subtrees
16802 When set, the subtree(s) will be folded after insertion, but only
16803 if doing so would now swallow text after the yanked text.
16805 org-yank-adjusted-subtrees
16806 When set, the subtree will be promoted or demoted in order to
16807 fit into the local outline tree structure, which means that the level
16808 will be adjusted so that it becomes the smaller one of the two
16809 *visible* surrounding headings.
16811 Any prefix to this command will cause `yank' to be called directly with
16812 no special treatment. In particular, a simple `C-u' prefix will just
16813 plainly yank the text as it is.
16815 \[1] The test checks if the first non-white line is a heading
16816 and if there are no other headings with fewer stars."
16818 (org-yank-generic 'yank arg))
16820 (defun org-yank-generic (command arg)
16821 "Perform some yank-like command.
16823 This function implements the behavior described in the `org-yank'
16824 documentation. However, it has been generalized to work for any
16825 interactive command with similar behavior."
16827 ;; pretend to be command COMMAND
16828 (setq this-command command)
16831 (call-interactively command)
16833 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
16834 (and (org-kill-is-subtree-p)
16836 (and (looking-at "[ \t]*$")
16839 (buffer-substring (point-at-bol) (point)))))))
16842 ((and subtreep org-yank-folded-subtrees)
16843 (let ((beg (point))
16845 (if (and subtreep org-yank-adjusted-subtrees)
16846 (org-paste-subtree nil nil 'for-yank)
16847 (call-interactively command))
16851 (when (and (bolp) subtreep
16852 (not (setq swallowp
16853 (org-yank-folding-would-swallow-text beg end))))
16854 (or (looking-at outline-regexp)
16855 (re-search-forward (concat "^" outline-regexp) end t))
16856 (while (and (< (point) end) (looking-at outline-regexp))
16858 (org-cycle-show-empty-lines 'folded)
16859 (condition-case nil
16860 (outline-forward-same-level 1)
16861 (error (goto-char end)))))
16864 "Inserted text not folded because that would swallow text"))
16867 (skip-chars-forward " \t\n\r")
16868 (beginning-of-line 1)
16869 (push-mark beg 'nomsg)))
16870 ((and subtreep org-yank-adjusted-subtrees)
16871 (let ((beg (point-at-bol)))
16872 (org-paste-subtree nil nil 'for-yank)
16873 (push-mark beg 'nomsg)))
16875 (call-interactively command))))))
16877 (defun org-yank-folding-would-swallow-text (beg end)
16878 "Would hide-subtree at BEG swallow any text after END?"
16882 (when (or (looking-at outline-regexp)
16883 (re-search-forward (concat "^" outline-regexp) end t))
16884 (setq level (org-outline-level)))
16886 (skip-chars-forward " \t\r\n\v\f")
16888 (and (bolp) (looking-at org-outline-regexp)
16889 (<= (org-outline-level) level)))
16890 nil ; Nothing would be swallowed
16891 t)))) ; something would swallow
16893 (define-key org-mode-map "\C-y" 'org-yank)
16895 (defun org-invisible-p ()
16896 "Check if point is at a character currently not visible."
16897 ;; Early versions of noutline don't have `outline-invisible-p'.
16898 (if (fboundp 'outline-invisible-p)
16899 (outline-invisible-p)
16900 (get-char-property (point) 'invisible)))
16902 (defun org-invisible-p2 ()
16903 "Check if point is at a character currently not visible."
16905 (if (and (eolp) (not (bobp))) (backward-char 1))
16906 ;; Early versions of noutline don't have `outline-invisible-p'.
16907 (if (fboundp 'outline-invisible-p)
16908 (outline-invisible-p)
16909 (get-char-property (point) 'invisible))))
16911 (defun org-back-to-heading (&optional invisible-ok)
16912 "Call `outline-back-to-heading', but provide a better error message."
16913 (condition-case nil
16914 (outline-back-to-heading invisible-ok)
16915 (error (error "Before first headline at position %d in buffer %s"
16916 (point) (current-buffer)))))
16918 (defun org-before-first-heading-p ()
16919 "Before first heading?"
16921 (null (re-search-backward "^\\*+ " nil t))))
16923 (defun org-on-heading-p (&optional ignored)
16924 (outline-on-heading-p t))
16925 (defun org-at-heading-p (&optional ignored)
16926 (outline-on-heading-p t))
16928 (defun org-at-heading-or-item-p ()
16929 (or (org-on-heading-p) (org-at-item-p)))
16931 (defun org-on-target-p ()
16932 (or (org-in-regexp org-radio-target-regexp)
16933 (org-in-regexp org-target-regexp)))
16935 (defun org-up-heading-all (arg)
16936 "Move to the heading line of which the present line is a subheading.
16937 This function considers both visible and invisible heading lines.
16938 With argument, move up ARG levels."
16939 (if (fboundp 'outline-up-heading-all)
16940 (outline-up-heading-all arg) ; emacs 21 version of outline.el
16941 (outline-up-heading arg t))) ; emacs 22 version of outline.el
16943 (defun org-up-heading-safe ()
16944 "Move to the heading line of which the present line is a subheading.
16945 This version will not throw an error. It will return the level of the
16946 headline found, or nil if no higher level is found.
16948 Also, this function will be a lot faster than `outline-up-heading',
16949 because it relies on stars being the outline starters. This can really
16950 make a significant difference in outlines with very many siblings."
16951 (let (start-level re)
16952 (org-back-to-heading t)
16953 (setq start-level (funcall outline-level))
16954 (if (equal start-level 1)
16956 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
16957 (if (re-search-backward re nil t)
16958 (funcall outline-level)))))
16960 (defun org-first-sibling-p ()
16961 "Is this heading the first child of its parents?"
16963 (let ((re (concat "^" outline-regexp))
16965 (unless (org-at-heading-p t)
16966 (error "Not at a heading"))
16967 (setq level (funcall outline-level))
16969 (if (not (re-search-backward re nil t))
16971 (setq l (funcall outline-level))
16974 (defun org-goto-sibling (&optional previous)
16975 "Goto the next sibling, even if it is invisible.
16976 When PREVIOUS is set, go to the previous sibling instead. Returns t
16977 when a sibling was found. When none is found, return nil and don't
16979 (let ((fun (if previous 're-search-backward 're-search-forward))
16981 (re (concat "^" outline-regexp))
16983 (when (condition-case nil (org-back-to-heading t) (error nil))
16984 (setq level (funcall outline-level))
16986 (or previous (forward-char 1))
16987 (while (funcall fun re nil t)
16988 (setq l (funcall outline-level))
16989 (when (< l level) (goto-char pos) (throw 'exit nil))
16990 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
16994 (defun org-show-siblings ()
16995 "Show all siblings of the current headline."
16997 (while (org-goto-sibling) (org-flag-heading nil)))
16999 (while (org-goto-sibling 'previous)
17000 (org-flag-heading nil))))
17002 (defun org-show-hidden-entry ()
17003 "Show an entry where even the heading is hidden."
17007 (defun org-flag-heading (flag &optional entry)
17008 "Flag the current heading. FLAG non-nil means make invisible.
17009 When ENTRY is non-nil, show the entire entry."
17011 (org-back-to-heading t)
17012 ;; Check if we should show the entire entry
17017 (and (outline-next-heading)
17018 (org-flag-heading nil))))
17019 (outline-flag-region (max (point-min) (1- (point)))
17020 (save-excursion (outline-end-of-heading) (point))
17023 (defun org-get-next-sibling ()
17024 "Move to next heading of the same level, and return point.
17025 If there is no such heading, return nil.
17026 This is like outline-next-sibling, but invisible headings are ok."
17027 (let ((level (funcall outline-level)))
17028 (outline-next-heading)
17029 (while (and (not (eobp)) (> (funcall outline-level) level))
17030 (outline-next-heading))
17031 (if (or (eobp) (< (funcall outline-level) level))
17035 (defun org-get-last-sibling ()
17036 "Move to previous heading of the same level, and return point.
17037 If there is no such heading, return nil."
17038 (let ((opoint (point))
17039 (level (funcall outline-level)))
17040 (outline-previous-heading)
17041 (when (and (/= (point) opoint) (outline-on-heading-p t))
17042 (while (and (> (funcall outline-level) level)
17044 (outline-previous-heading))
17045 (if (< (funcall outline-level) level)
17049 (defun org-end-of-subtree (&optional invisible-OK to-heading)
17050 ;; This contains an exact copy of the original function, but it uses
17051 ;; `org-back-to-heading', to make it work also in invisible
17052 ;; trees. And is uses an invisible-OK argument.
17053 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
17054 ;; Furthermore, when used inside Org, finding the end of a large subtree
17055 ;; with many children and grandchildren etc, this can be much faster
17056 ;; than the outline version.
17057 (org-back-to-heading invisible-OK)
17059 (level (funcall outline-level)))
17060 (if (and (org-mode-p) (< level 1000))
17061 ;; A true heading (not a plain list item), in Org-mode
17062 ;; This means we can easily find the end by looking
17063 ;; only for the right number of stars. Using a regexp to do
17064 ;; this is so much faster than using a Lisp loop.
17065 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
17067 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
17068 ;; something else, do it the slow way
17069 (while (and (not (eobp))
17070 (or first (> (funcall outline-level) level)))
17072 (outline-next-heading)))
17074 (if (memq (preceding-char) '(?\n ?\^M))
17076 ;; Go to end of line before heading
17078 (if (memq (preceding-char) '(?\n ?\^M))
17079 ;; leave blank line before heading
17080 (forward-char -1))))))
17083 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
17084 "Use Org version in org-mode, for dramatic speed-up."
17085 (if (eq major-mode 'org-mode)
17087 (org-end-of-subtree nil t)
17088 (unless (eobp) (backward-char 1)))
17091 (defun org-forward-same-level (arg &optional invisible-ok)
17092 "Move forward to the arg'th subheading at same level as this one.
17093 Stop at the first and last subheadings of a superior heading."
17095 (org-back-to-heading invisible-ok)
17097 (let* ((level (- (match-end 0) (match-beginning 0) 1))
17098 (re (format "^\\*\\{1,%d\\} " level))
17102 (while (and (re-search-forward re nil 'move)
17103 (setq l (- (match-end 0) (match-beginning 0) 1))
17107 (if (< l level) (setq arg 1)))
17108 (setq arg (1- arg)))
17109 (beginning-of-line 1)))
17111 (defun org-backward-same-level (arg &optional invisible-ok)
17112 "Move backward to the arg'th subheading at same level as this one.
17113 Stop at the first and last subheadings of a superior heading."
17115 (org-back-to-heading)
17117 (let* ((level (- (match-end 0) (match-beginning 0) 1))
17118 (re (format "^\\*\\{1,%d\\} " level))
17121 (while (and (re-search-backward re nil 'move)
17122 (setq l (- (match-end 0) (match-beginning 0) 1))
17126 (if (< l level) (setq arg 1)))
17127 (setq arg (1- arg)))))
17129 (defun org-show-subtree ()
17130 "Show everything after this heading at deeper levels."
17131 (outline-flag-region
17134 (org-end-of-subtree t t))
17137 (defun org-show-entry ()
17138 "Show the body directly following this heading.
17139 Show the heading too, if it is currently invisible."
17142 (condition-case nil
17144 (org-back-to-heading t)
17145 (outline-flag-region
17146 (max (point-min) (1- (point)))
17148 (if (re-search-forward
17149 (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
17150 (match-beginning 1)
17153 (org-cycle-hide-drawers 'children))
17156 (defun org-make-options-regexp (kwds &optional extra)
17157 "Make a regular expression for keyword lines."
17161 (mapconcat 'regexp-quote kwds "\\|")
17162 (if extra (concat "\\|" extra))
17166 ;; Make isearch reveal the necessary context
17167 (defun org-isearch-end ()
17168 "Reveal context after isearch exits."
17169 (when isearch-success ; only if search was successful
17170 (if (featurep 'xemacs)
17171 ;; Under XEmacs, the hook is run in the correct place,
17172 ;; we directly show the context.
17173 (org-show-context 'isearch)
17174 ;; In Emacs the hook runs *before* restoring the overlays.
17175 ;; So we have to use a one-time post-command-hook to do this.
17176 ;; (Emacs 22 has a special variable, see function `org-mode')
17177 (unless (and (boundp 'isearch-mode-end-hook-quit)
17178 isearch-mode-end-hook-quit)
17179 ;; Only when the isearch was not quitted.
17180 (org-add-hook 'post-command-hook 'org-isearch-post-command
17181 'append 'local)))))
17183 (defun org-isearch-post-command ()
17184 "Remove self from hook, and show context."
17185 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
17186 (org-show-context 'isearch))
17189 ;;;; Integration with and fixes for other packages
17193 (defvar org-imenu-markers nil
17194 "All markers currently used by Imenu.")
17195 (make-variable-buffer-local 'org-imenu-markers)
17197 (defun org-imenu-new-marker (&optional pos)
17198 "Return a new marker for use by Imenu, and remember the marker."
17199 (let ((m (make-marker)))
17200 (move-marker m (or pos (point)))
17201 (push m org-imenu-markers)
17204 (defun org-imenu-get-tree ()
17205 "Produce the index for Imenu."
17206 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
17207 (setq org-imenu-markers nil)
17208 (let* ((n org-imenu-depth)
17209 (re (concat "^" outline-regexp))
17210 (subs (make-vector (1+ n) nil))
17216 (goto-char (point-max))
17217 (while (re-search-backward re nil t)
17218 (setq level (org-reduced-level (funcall outline-level)))
17220 (looking-at org-complex-heading-regexp)
17221 (setq head (org-link-display-format
17222 (org-match-string-no-properties 4))
17223 m (org-imenu-new-marker))
17224 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
17225 (if (>= level last-level)
17226 (push (cons head m) (aref subs level))
17227 (push (cons head (aref subs (1+ level))) (aref subs level))
17228 (loop for i from (1+ level) to n do (aset subs i nil)))
17229 (setq last-level level)))))
17232 (eval-after-load "imenu"
17234 (add-hook 'imenu-after-jump-hook
17236 (if (eq major-mode 'org-mode)
17237 (org-show-context 'org-goto))))))
17239 (defun org-link-display-format (link)
17240 "Replace a link with either the description, or the link target
17241 if no description is present"
17243 (if (string-match org-bracket-link-analytic-regexp link)
17244 (replace-match (or (match-string 5 link)
17245 (concat (match-string 1 link)
17246 (match-string 3 link)))
17250 ;; Speedbar support
17252 (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
17253 "Overlay marking the agenda restriction line in speedbar.")
17254 (org-overlay-put org-speedbar-restriction-lock-overlay
17255 'face 'org-agenda-restriction-lock)
17256 (org-overlay-put org-speedbar-restriction-lock-overlay
17257 'help-echo "Agendas are currently limited to this item.")
17258 (org-detach-overlay org-speedbar-restriction-lock-overlay)
17260 (defun org-speedbar-set-agenda-restriction ()
17261 "Restrict future agenda commands to the location at point in speedbar.
17262 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
17264 (require 'org-agenda)
17265 (let (p m tp np dir txt)
17267 ((setq p (text-property-any (point-at-bol) (point-at-eol)
17269 (setq m (get-text-property p 'org-imenu-marker))
17272 (set-buffer (marker-buffer m))
17274 (org-agenda-set-restriction-lock 'subtree))))
17275 ((setq p (text-property-any (point-at-bol) (point-at-eol)
17276 'speedbar-function 'speedbar-find-file))
17277 (setq tp (previous-single-property-change
17278 (1+ p) 'speedbar-function)
17279 np (next-single-property-change
17280 tp 'speedbar-function)
17281 dir (speedbar-line-directory)
17282 txt (buffer-substring-no-properties (or tp (point-min))
17283 (or np (point-max))))
17286 (set-buffer (find-file-noselect
17287 (let ((default-directory dir))
17288 (expand-file-name txt))))
17289 (unless (org-mode-p)
17290 (error "Cannot restrict to non-Org-mode file"))
17291 (org-agenda-set-restriction-lock 'file))))
17292 (t (error "Don't know how to restrict Org-mode's agenda")))
17293 (org-move-overlay org-speedbar-restriction-lock-overlay
17294 (point-at-bol) (point-at-eol))
17295 (setq current-prefix-arg nil)
17296 (org-agenda-maybe-redo)))
17298 (eval-after-load "speedbar"
17300 (speedbar-add-supported-extension ".org")
17301 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
17302 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
17303 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
17304 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17305 (add-hook 'speedbar-visiting-tag-hook
17306 (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
17309 ;;; Fixes and Hacks for problems with other packages
17311 ;; Make flyspell not check words in links, to not mess up our keymap
17312 (defun org-mode-flyspell-verify ()
17313 "Don't let flyspell put overlays at active buttons."
17314 (and (not (get-text-property (point) 'keymap))
17315 (not (get-text-property (point) 'org-no-flyspell))))
17317 (defun org-remove-flyspell-overlays-in (beg end)
17318 "Remove flyspell overlays in region."
17319 (and (org-bound-and-true-p flyspell-mode)
17320 (fboundp 'flyspell-delete-region-overlays)
17321 (flyspell-delete-region-overlays beg end))
17322 (add-text-properties beg end '(org-no-flyspell t)))
17324 ;; Make `bookmark-jump' show the jump location if it was hidden.
17325 (eval-after-load "bookmark"
17326 '(if (boundp 'bookmark-after-jump-hook)
17327 ;; We can use the hook
17328 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
17329 ;; Hook not available, use advice
17330 (defadvice bookmark-jump (after org-make-visible activate)
17331 "Make the position visible."
17332 (org-bookmark-jump-unhide))))
17334 ;; Make sure saveplace show the location if it was hidden
17335 (eval-after-load "saveplace"
17336 '(defadvice save-place-find-file-hook (after org-make-visible activate)
17337 "Make the position visible."
17338 (org-bookmark-jump-unhide)))
17340 (defun org-bookmark-jump-unhide ()
17341 "Unhide the current position, to show the bookmark location."
17343 (or (org-invisible-p)
17344 (save-excursion (goto-char (max (point-min) (1- (point))))
17345 (org-invisible-p)))
17346 (org-show-context 'bookmark-jump)))
17348 ;; Make session.el ignore our circular variable
17349 (eval-after-load "session"
17350 '(add-to-list 'session-globals-exclude 'org-mark-ring))
17352 ;;;; Experimental code
17354 (defun org-closed-in-range ()
17355 "Sparse tree of items closed in a certain time range.
17356 Still experimental, may disappear in the future."
17358 ;; Get the time interval from the user.
17359 (let* ((time1 (org-float-time
17360 (org-read-date nil 'to-time nil "Starting date: ")))
17361 (time2 (org-float-time
17362 (org-read-date nil 'to-time nil "End date:")))
17363 ;; callback function
17364 (callback (lambda ()
17367 (apply 'encode-time
17368 (org-parse-time-string
17369 (match-string 1))))))
17370 ;; check if time in interval
17371 (and (>= time time1) (<= time time2))))))
17372 ;; make tree, check each match with the callback
17373 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
17379 (run-hooks 'org-load-hook)
17381 ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
17383 ;;; org.el ends here