1 ;;; org.el --- Outline-based notes management and organizer
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
31 ;; project planning with a fast and effective plain-text system.
33 ;; Org-mode develops organizational tasks around NOTES files that contain
34 ;; information about projects as plain text. Org-mode is implemented on
35 ;; top of outline-mode, which makes it possible to keep the content of
36 ;; large files well structured. Visibility cycling and structure editing
37 ;; help to work with the tree. Tables are easily created with a built-in
38 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
39 ;; and scheduling. It dynamically compiles entries into an agenda that
40 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
41 ;; Plain text URL-like links connect to websites, emails, Usenet
42 ;; messages, BBDB entries, and any files related to the projects. For
43 ;; printing and sharing of notes, an Org-mode file can be exported as a
44 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
45 ;; iCalendar file. It can also serve as a publishing tool for a set of
48 ;; Installation and Activation
49 ;; ---------------------------
50 ;; See the corresponding sections in the manual at
52 ;; http://orgmode.org/org.html#Installation
56 ;; The documentation of Org-mode can be found in the TeXInfo file. The
57 ;; distribution also contains a PDF version of it. At the homepage of
58 ;; Org-mode, you can read the same text online as HTML. There is also an
59 ;; excellent reference card made by Philip Rooke. This card can be found
60 ;; in the etc/ directory of Emacs 22.
62 ;; A list of recent changes can be found at
63 ;; http://orgmode.org/Changes.html
67 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
68 (defvar org-table-formula-constants-local nil
69 "Local version of `org-table-formula-constants'.")
70 (make-variable-buffer-local 'org-table-formula-constants-local
)
72 ;;;; Require other packages
79 (require 'format-spec
)
81 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
82 (when (fboundp 'defvaralias
)
83 (unless (boundp 'calendar-view-holidays-initially-flag
)
84 (defvaralias 'calendar-view-holidays-initially-flag
85 'view-calendar-holidays-initially
))
86 (unless (boundp 'calendar-view-diary-initially-flag
)
87 (defvaralias 'calendar-view-diary-initially-flag
88 'view-diary-entries-initially
))
89 (unless (boundp 'diary-fancy-buffer
)
90 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
92 (require 'outline
) (require 'noutline
)
93 ;; Other stuff we need.
95 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
100 (require 'org-entities
)
101 (require 'org-compat
)
104 (require 'org-pcomplete
)
106 (require 'org-footnote
)
108 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
109 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
110 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
111 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
112 (declare-function org-at-clock-log-p
"org-clock" ())
113 (declare-function org-clock-timestamps-up
"org-clock" ())
114 (declare-function org-clock-timestamps-down
"org-clock" ())
125 ;; load languages based on value of `org-babel-load-languages'
126 (defvar org-babel-load-languages
)
128 (defun org-babel-do-load-languages (sym value
)
129 "Load the languages defined in `org-babel-load-languages'."
130 (set-default sym value
)
132 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
135 (require (intern (concat "ob-" lang
))))
137 (funcall 'fmakunbound
138 (intern (concat "org-babel-execute:" lang
)))
139 (funcall 'fmakunbound
140 (intern (concat "org-babel-expand-body:" lang
)))))))
141 org-babel-load-languages
))
143 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
144 "Languages which can be evaluated in Org-mode buffers.
145 This list can be used to load support for any of the languages
146 below, note that each language will depend on a different set of
147 system executables and/or Emacs modes. When a language is
148 \"loaded\", then code blocks in that language can be evaluated
149 with `org-babel-execute-src-block' bound by default to C-c
150 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
151 be set to remove code block evaluation from the C-c C-c
152 keybinding. By default only Emacs Lisp (which has no
153 requirements) is loaded."
155 :set
'org-babel-do-load-languages
157 :type
'(alist :tag
"Babel Languages"
160 (const :tag
"Awk" awk
)
163 (const :tag
"Asymptote" asymptote
)
164 (const :tag
"Calc" calc
)
165 (const :tag
"Clojure" clojure
)
166 (const :tag
"CSS" css
)
167 (const :tag
"Ditaa" ditaa
)
168 (const :tag
"Dot" dot
)
169 (const :tag
"Emacs Lisp" emacs-lisp
)
170 (const :tag
"Fortran" fortran
)
171 (const :tag
"Gnuplot" gnuplot
)
172 (const :tag
"Haskell" haskell
)
174 (const :tag
"Java" java
)
175 (const :tag
"Javascript" js
)
176 (const :tag
"LaTeX" latex
)
177 (const :tag
"Ledger" ledger
)
178 (const :tag
"Lilypond" lilypond
)
179 (const :tag
"Maxima" maxima
)
180 (const :tag
"Matlab" matlab
)
181 (const :tag
"Mscgen" mscgen
)
182 (const :tag
"Ocaml" ocaml
)
183 (const :tag
"Octave" octave
)
184 (const :tag
"Org" org
)
185 (const :tag
"Perl" perl
)
186 (const :tag
"Pico Lisp" picolisp
)
187 (const :tag
"PlantUML" plantuml
)
188 (const :tag
"Python" python
)
189 (const :tag
"Ruby" ruby
)
190 (const :tag
"Sass" sass
)
191 (const :tag
"Scala" scala
)
192 (const :tag
"Scheme" scheme
)
193 (const :tag
"Screen" screen
)
194 (const :tag
"Shell Script" sh
)
195 (const :tag
"Shen" shen
)
196 (const :tag
"Sql" sql
)
197 (const :tag
"Sqlite" sqlite
))
198 :value-type
(boolean :tag
"Activate" :value t
)))
200 ;;;; Customization variables
201 (defcustom org-clone-delete-id nil
202 "Remove ID property of clones of a subtree.
203 When non-nil, clones of a subtree don't inherit the ID property.
204 Otherwise they inherit the ID property with a new unique
212 (defconst org-version
"7.8.06"
213 "The version number of the file org.el.")
216 (defun org-version (&optional here
)
217 "Show the org-mode version in the echo area.
218 With prefix arg HERE, insert it at point."
220 (let* ((origin default-directory
)
221 (version org-version
)
223 (dir (concat (file-name-directory (locate-library "org")) "../" )))
224 (when (and (file-exists-p (expand-file-name ".git" dir
))
225 (executable-find "git"))
229 (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
230 (with-current-buffer "*Shell Command Output*"
231 (goto-char (point-min))
232 (setq git-version
(buffer-substring (point) (point-at-eol))))
233 (subst-char-in-string ?- ?. git-version t
)
234 (when (string-match "\\S-"
235 (shell-command-to-string
236 "git diff-index --name-only HEAD --"))
237 (setq git-version
(concat git-version
".dirty")))
238 (setq version
(concat version
" (" git-version
")"))))
240 (setq version
(format "Org-mode version %s" version
))
241 (if here
(insert version
))
244 ;;; Compatibility constants
246 ;;; The custom variables
249 "Outline-based notes management and organizer."
254 (defcustom org-mode-hook nil
255 "Mode hook for Org-mode, run after the mode was turned on."
259 (defcustom org-load-hook nil
260 "Hook that is run after org.el has been loaded."
264 (defcustom org-log-buffer-setup-hook nil
265 "Hook that is run after an Org log buffer is created."
270 (defvar org-modules
) ; defined below
271 (defvar org-modules-loaded nil
272 "Have the modules been loaded already?")
274 (defun org-load-modules-maybe (&optional force
)
275 "Load all extensions listed in `org-modules'."
276 (when (or force
(not org-modules-loaded
))
278 (condition-case nil
(require ext
)
279 (error (message "Problems while trying to load feature `%s'" ext
))))
281 (setq org-modules-loaded t
)))
283 (defun org-set-modules (var value
)
284 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
286 (when (featurep 'org
)
287 (org-load-modules-maybe 'force
)))
289 (when (org-bound-and-true-p org-modules
)
290 (let ((a (member 'org-infojs org-modules
)))
291 (and a
(setcar a
'org-jsinfo
))))
293 (defcustom org-modules
'(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl
)
294 "Modules that should always be loaded together with org.el.
295 If a description starts with <C>, the file is not part of Emacs
296 and loading it will require that you have downloaded and properly installed
297 the org-mode distribution.
299 You can also use this system to load external packages (i.e. neither Org
300 core modules, nor modules from the CONTRIB directory). Just add symbols
301 to the end of the list. If the package is called org-xyz.el, then you need
302 to add the symbol `xyz', and the package must have a call to
306 :set
'org-set-modules
309 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
310 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
311 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
312 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
313 (const :tag
" docview: Links to doc-view buffers" org-docview
)
314 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
315 (const :tag
" id: Global IDs for identifying entries" org-id
)
316 (const :tag
" info: Links to Info nodes" org-info
)
317 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
318 (const :tag
" habit: Track your consistency with habits" org-habit
)
319 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
320 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
321 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
322 (const :tag
" mew Links to Mew folders/messages" org-mew
)
323 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
324 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
325 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
326 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
327 (const :tag
" vm: Links to VM folders/messages" org-vm
)
328 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
329 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
330 (const :tag
" mouse: Additional mouse support" org-mouse
)
331 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
333 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
334 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
335 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
336 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
337 (const :tag
"C collector: Collect properties into tables" org-collector
)
338 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
339 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
340 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
341 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
342 (const :tag
"C eval: Include command output as text" org-eval
)
343 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
344 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
345 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
346 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
347 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
349 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
351 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
352 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
353 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
354 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
355 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
356 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
357 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
358 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
359 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
360 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
361 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
362 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
363 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
364 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
365 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
366 (const :tag
"C track: Keep up with Org-mode development" org-track
)
367 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
368 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
369 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
371 (defcustom org-support-shift-select nil
372 "Non-nil means make shift-cursor commands select text when possible.
374 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
375 start selecting a region, or enlarge regions started in this way.
376 In Org-mode, in special contexts, these same keys are used for
377 other purposes, important enough to compete with shift selection.
378 Org tries to balance these needs by supporting `shift-select-mode'
379 outside these special contexts, under control of this variable.
381 The default of this variable is nil, to avoid confusing behavior. Shifted
382 cursor keys will then execute Org commands in the following contexts:
383 - on a headline, changing TODO state (left/right) and priority (up/down)
384 - on a time stamp, changing the time
385 - in a plain list item, changing the bullet type
386 - in a property definition line, switching between allowed values
387 - in the BEGIN line of a clock table (changing the time block).
388 Outside these contexts, the commands will throw an error.
390 When this variable is t and the cursor is not in a special
391 context, Org-mode will support shift-selection for making and
392 enlarging regions. To make this more effective, the bullet
393 cycling will no longer happen anywhere in an item line, but only
394 if the cursor is exactly on the bullet.
396 If you set this variable to the symbol `always', then the keys
397 will not be special in headlines, property lines, and item lines,
398 to make shift selection work there as well. If this is what you
399 want, you can use the following alternative commands: `C-c C-t'
400 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
401 can be used to switch TODO sets, `C-c -' to cycle item bullet
402 types, and properties can be edited by hand or in column view.
404 However, when the cursor is on a timestamp, shift-cursor commands
405 will still edit the time stamp - this is just too good to give up.
407 XEmacs user should have this variable set to nil, because
408 `shift-select-mode' is in Emacs 23 or later only."
411 (const :tag
"Never" nil
)
412 (const :tag
"When outside special context" t
)
413 (const :tag
"Everywhere except timestamps" always
)))
415 (defcustom org-loop-over-headlines-in-active-region nil
416 "Shall some commands act upon headlines in the active region?
418 When set to `t', some commands will be performed in all headlines
419 within the active region.
421 When set to `start-level', some commands will be performed in all
422 headlines within the active region, provided that these headlines
423 are of the same level than the first one.
425 When set to a string, those commands will be performed on the
426 matching headlines within the active region. Such string must be
427 a tags/property/todo match as it is used in the agenda tags view.
429 The list of commands is: `org-schedule', `org-deadline',
430 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
431 `org-archive-to-archive-sibling'. The archiving commands skip
432 already archived entries."
433 :type
'(choice (const :tag
"Don't loop" nil
)
434 (const :tag
"All headlines in active region" t
)
435 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
436 (string :tag
"Tags/Property/Todo matcher"))
441 (defgroup org-startup nil
442 "Options concerning startup of Org-mode."
446 (defcustom org-startup-folded t
447 "Non-nil means entering Org-mode will switch to OVERVIEW.
448 This can also be configured on a per-file basis by adding one of
449 the following lines anywhere in the buffer:
451 #+STARTUP: fold (or `overview', this is equivalent)
452 #+STARTUP: nofold (or `showall', this is equivalent)
454 #+STARTUP: showeverything"
457 (const :tag
"nofold: show all" nil
)
458 (const :tag
"fold: overview" t
)
459 (const :tag
"content: all headlines" content
)
460 (const :tag
"show everything, even drawers" showeverything
)))
462 (defcustom org-startup-truncated t
463 "Non-nil means entering Org-mode will set `truncate-lines'.
464 This is useful since some lines containing links can be very long and
465 uninteresting. Also tables look terrible when wrapped."
469 (defcustom org-startup-indented nil
470 "Non-nil means turn on `org-indent-mode' on startup.
471 This can also be configured on a per-file basis by adding one of
472 the following lines anywhere in the buffer:
476 :group
'org-structure
478 (const :tag
"Not" nil
)
479 (const :tag
"Globally (slow on startup in large files)" t
)))
481 (defcustom org-use-sub-superscripts t
482 "Non-nil means interpret \"_\" and \"^\" for export.
483 When this option is turned on, you can use TeX-like syntax for sub- and
484 superscripts. Several characters after \"_\" or \"^\" will be
485 considered as a single item - so grouping with {} is normally not
486 needed. For example, the following things will be parsed as single
487 sub- or superscripts.
489 10^24 or 10^tau several digits will be considered 1 item.
490 10^-12 or 10^-tau a leading sign with digits or a word
491 x^2-y^3 will be read as x^2 - y^3, because items are
492 terminated by almost any nonword/nondigit char.
493 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
495 Still, ambiguity is possible - so when in doubt use {} to enclose the
496 sub/superscript. If you set this variable to the symbol `{}',
497 the braces are *required* in order to trigger interpretations as
498 sub/superscript. This can be helpful in documents that need \"_\"
499 frequently in plain text.
501 Not all export backends support this, but HTML does.
503 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
505 :group
'org-export-translation
508 (const :tag
"Always interpret" t
)
509 (const :tag
"Only with braces" {})
510 (const :tag
"Never interpret" nil
)))
512 (if (fboundp 'defvaralias
)
513 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
516 (defcustom org-startup-with-beamer-mode nil
517 "Non-nil means turn on `org-beamer-mode' on startup.
518 This can also be configured on a per-file basis by adding one of
519 the following lines anywhere in the buffer:
526 (defcustom org-startup-align-all-tables nil
527 "Non-nil means align all tables when visiting a file.
528 This is useful when the column width in tables is forced with <N> cookies
529 in table fields. Such tables will look correct only after the first re-align.
530 This can also be configured on a per-file basis by adding one of
531 the following lines anywhere in the buffer:
537 (defcustom org-startup-with-inline-images nil
538 "Non-nil means show inline images when loading a new Org file.
539 This can also be configured on a per-file basis by adding one of
540 the following lines anywhere in the buffer:
541 #+STARTUP: inlineimages
542 #+STARTUP: noinlineimages"
547 (defcustom org-insert-mode-line-in-empty-file nil
548 "Non-nil means insert the first line setting Org-mode in empty files.
549 When the function `org-mode' is called interactively in an empty file, this
550 normally means that the file name does not automatically trigger Org-mode.
551 To ensure that the file will always be in Org-mode in the future, a
552 line enforcing Org-mode will be inserted into the buffer, if this option
557 (defcustom org-replace-disputed-keys nil
558 "Non-nil means use alternative key bindings for some keys.
559 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
560 These keys are also used by other packages like shift-selection-mode'
561 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
562 If you want to use Org-mode together with one of these other modes,
563 or more generally if you would like to move some Org-mode commands to
564 other keys, set this variable and configure the keys with the variable
567 This option is only relevant at load-time of Org-mode, and must be set
568 *before* org.el is loaded. Changing it requires a restart of Emacs to
573 (defcustom org-use-extra-keys nil
574 "Non-nil means use extra key sequence definitions for certain commands.
575 This happens automatically if you run XEmacs or if `window-system'
576 is nil. This variable lets you do the same manually. You must
577 set it before loading org.
579 Example: on Carbon Emacs 22 running graphically, with an external
580 keyboard on a Powerbook, the default way of setting M-left might
581 not work for either Alt or ESC. Setting this variable will make
586 (if (fboundp 'defvaralias
)
587 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
589 (defcustom org-disputed-keys
590 '(([(shift up
)] .
[(meta p
)])
591 ([(shift down
)] .
[(meta n
)])
592 ([(shift left
)] .
[(meta -
)])
593 ([(shift right
)] .
[(meta +)])
594 ([(control shift right
)] .
[(meta shift
+)])
595 ([(control shift left
)] .
[(meta shift -
)]))
596 "Keys for which Org-mode and other modes compete.
597 This is an alist, cars are the default keys, second element specifies
598 the alternative to use when `org-replace-disputed-keys' is t.
600 Keys can be specified in any syntax supported by `define-key'.
601 The value of this option takes effect only at Org-mode's startup,
602 therefore you'll have to restart Emacs to apply it after changing."
607 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
608 Or return the original if not disputed.
609 Also apply the translations defined in `org-xemacs-key-equivalents'."
610 (when org-replace-disputed-keys
611 (let* ((nkey (key-description key
))
612 (x (org-find-if (lambda (x)
613 (equal (key-description (car x
)) nkey
))
615 (setq key
(if x
(cdr x
) key
))))
616 (when (featurep 'xemacs
)
617 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
620 (defun org-find-if (predicate seq
)
623 (if (funcall predicate
(car seq
))
624 (throw 'exit
(car seq
))
627 (defun org-defkey (keymap key def
)
628 "Define a key, possibly translated, as returned by `org-key'."
629 (define-key keymap
(org-key key
) def
))
631 (defcustom org-ellipsis nil
632 "The ellipsis to use in the Org-mode outline.
633 When nil, just use the standard three dots. When a string, use that instead,
634 When a face, use the standard 3 dots, but with the specified face.
635 The change affects only Org-mode (which will then use its own display table).
636 Changing this requires executing `M-x org-mode' in a buffer to become
639 :type
'(choice (const :tag
"Default" nil
)
640 (face :tag
"Face" :value org-warning
)
641 (string :tag
"String" :value
"...#")))
643 (defvar org-display-table nil
644 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
646 (defgroup org-keywords nil
647 "Keywords in Org-mode."
651 (defcustom org-deadline-string
"DEADLINE:"
652 "String to mark deadline entries.
653 A deadline is this string, followed by a time stamp. Should be a word,
654 terminated by a colon. You can insert a schedule keyword and
655 a timestamp with \\[org-deadline].
656 Changes become only effective after restarting Emacs."
660 (defcustom org-scheduled-string
"SCHEDULED:"
661 "String to mark scheduled TODO entries.
662 A schedule is this string, followed by a time stamp. Should be a word,
663 terminated by a colon. You can insert a schedule keyword and
664 a timestamp with \\[org-schedule].
665 Changes become only effective after restarting Emacs."
669 (defcustom org-closed-string
"CLOSED:"
670 "String used as the prefix for timestamps logging closing a TODO entry."
674 (defcustom org-clock-string
"CLOCK:"
675 "String used as prefix for timestamps clocking work hours on an item."
679 (defcustom org-comment-string
"COMMENT"
680 "Entries starting with this keyword will never be exported.
681 An entry can be toggled between COMMENT and normal with
682 \\[org-toggle-comment].
683 Changes become only effective after restarting Emacs."
687 (defcustom org-quote-string
"QUOTE"
688 "Entries starting with this keyword will be exported in fixed-width font.
689 Quoting applies only to the text in the entry following the headline, and does
690 not extend beyond the next headline, even if that is lower level.
691 An entry can be toggled between QUOTE and normal with
692 \\[org-toggle-fixed-width-section]."
696 (defconst org-repeat-re
697 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
698 "Regular expression for specifying repeated events.
699 After a match, group 1 contains the repeat expression.")
701 (defgroup org-structure nil
702 "Options concerning the general structure of Org-mode files."
706 (defgroup org-reveal-location nil
707 "Options about how to make context of a location visible."
708 :tag
"Org Reveal Location"
709 :group
'org-structure
)
711 (defconst org-context-choice
713 (const :tag
"Always" t
)
714 (const :tag
"Never" nil
)
715 (repeat :greedy t
:tag
"Individual contexts"
717 (choice :tag
"Context"
724 (const bookmark-jump
)
728 "Contexts for the reveal options.")
730 (defcustom org-show-hierarchy-above
'((default . t
))
731 "Non-nil means show full hierarchy when revealing a location.
732 Org-mode often shows locations in an org-mode file which might have
733 been invisible before. When this is set, the hierarchy of headings
734 above the exposed location is shown.
735 Turning this off for example for sparse trees makes them very compact.
736 Instead of t, this can also be an alist specifying this option for different
737 contexts. Valid contexts are
738 agenda when exposing an entry from the agenda
739 org-goto when using the command `org-goto' on key C-c C-j
740 occur-tree when using the command `org-occur' on key C-c /
741 tags-tree when constructing a sparse tree based on tags matches
742 link-search when exposing search matches associated with a link
743 mark-goto when exposing the jump goal of a mark
744 bookmark-jump when exposing a bookmark location
745 isearch when exiting from an incremental search
746 default default for all contexts not set explicitly"
747 :group
'org-reveal-location
748 :type org-context-choice
)
750 (defcustom org-show-following-heading
'((default . nil
))
751 "Non-nil means show following heading when revealing a location.
752 Org-mode often shows locations in an org-mode file which might have
753 been invisible before. When this is set, the heading following the
755 Turning this off for example for sparse trees makes them very compact,
756 but makes it harder to edit the location of the match. In such a case,
757 use the command \\[org-reveal] to show more context.
758 Instead of t, this can also be an alist specifying this option for different
759 contexts. See `org-show-hierarchy-above' for valid contexts."
760 :group
'org-reveal-location
761 :type org-context-choice
)
763 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
764 "Non-nil means show all sibling heading when revealing a location.
765 Org-mode often shows locations in an org-mode file which might have
766 been invisible before. When this is set, the sibling of the current entry
767 heading are all made visible. If `org-show-hierarchy-above' is t,
768 the same happens on each level of the hierarchy above the current entry.
770 By default this is on for the isearch context, off for all other contexts.
771 Turning this off for example for sparse trees makes them very compact,
772 but makes it harder to edit the location of the match. In such a case,
773 use the command \\[org-reveal] to show more context.
774 Instead of t, this can also be an alist specifying this option for different
775 contexts. See `org-show-hierarchy-above' for valid contexts."
776 :group
'org-reveal-location
777 :type org-context-choice
)
779 (defcustom org-show-entry-below
'((default . nil
))
780 "Non-nil means show the entry below a headline when revealing a location.
781 Org-mode often shows locations in an org-mode file which might have
782 been invisible before. When this is set, the text below the headline that is
783 exposed is also shown.
785 By default this is off for all contexts.
786 Instead of t, this can also be an alist specifying this option for different
787 contexts. See `org-show-hierarchy-above' for valid contexts."
788 :group
'org-reveal-location
789 :type org-context-choice
)
791 (defcustom org-indirect-buffer-display
'other-window
792 "How should indirect tree buffers be displayed?
793 This applies to indirect buffers created with the commands
794 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
796 current-window Display in the current window
797 other-window Just display in another window.
798 dedicated-frame Create one new frame, and re-use it each time.
799 new-frame Make a new frame each time. Note that in this case
800 previously-made indirect buffers are kept, and you need to
801 kill these buffers yourself."
802 :group
'org-structure
803 :group
'org-agenda-windows
805 (const :tag
"In current window" current-window
)
806 (const :tag
"In current frame, other window" other-window
)
807 (const :tag
"Each time a new frame" new-frame
)
808 (const :tag
"One dedicated frame" dedicated-frame
)))
810 (defcustom org-use-speed-commands nil
811 "Non-nil means activate single letter commands at beginning of a headline.
812 This may also be a function to test for appropriate locations where speed
813 commands should be active."
814 :group
'org-structure
816 (const :tag
"Never" nil
)
817 (const :tag
"At beginning of headline stars" t
)
820 (defcustom org-speed-commands-user nil
821 "Alist of additional speed commands.
822 This list will be checked before `org-speed-commands-default'
823 when the variable `org-use-speed-commands' is non-nil
824 and when the cursor is at the beginning of a headline.
825 The car if each entry is a string with a single letter, which must
826 be assigned to `self-insert-command' in the global map.
827 The cdr is either a command to be called interactively, a function
828 to be called, or a form to be evaluated.
829 An entry that is just a list with a single string will be interpreted
830 as a descriptive headline that will be added when listing the speed
831 commands in the Help buffer using the `?' speed command."
832 :group
'org-structure
833 :type
'(repeat :value
("k" . ignore
)
834 (choice :value
("k" . ignore
)
835 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
836 (cons :tag
"Letter and Command"
837 (string :tag
"Command letter")
842 (defgroup org-cycle nil
843 "Options concerning visibility cycling in Org-mode."
845 :group
'org-structure
)
847 (defcustom org-cycle-skip-children-state-if-no-children t
848 "Non-nil means skip CHILDREN state in entries that don't have any."
852 (defcustom org-cycle-max-level nil
853 "Maximum level which should still be subject to visibility cycling.
854 Levels higher than this will, for cycling, be treated as text, not a headline.
855 When `org-odd-levels-only' is set, a value of N in this variable actually
856 means 2N-1 stars as the limiting headline.
857 When nil, cycle all levels.
858 Note that the limiting level of cycling is also influenced by
859 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
860 `org-inlinetask-min-level' is, cycling will be limited to levels one less
864 (const :tag
"No limit" nil
)
865 (integer :tag
"Maximum level")))
867 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
868 "Names of drawers. Drawers are not opened by cycling on the headline above.
869 Drawers only open with a TAB on the drawer line itself. A drawer looks like
874 The drawer \"PROPERTIES\" is special for capturing properties through
877 Drawers can be defined on the per-file basis with a line like:
879 #+DRAWERS: HIDDEN STATE PROPERTIES"
880 :group
'org-structure
882 :type
'(repeat (string :tag
"Drawer Name")))
884 (defcustom org-hide-block-startup nil
885 "Non-nil means entering Org-mode will fold all blocks.
886 This can also be set in on a per-file basis with
888 #+STARTUP: hideblocks
889 #+STARTUP: showblocks"
894 (defcustom org-cycle-global-at-bob nil
895 "Cycle globally if cursor is at beginning of buffer and not at a headline.
896 This makes it possible to do global cycling without having to use S-TAB or
897 \\[universal-argument] TAB. For this special case to work, the first line \
899 must not be a headline - it may be empty or some other text. When used in
900 this way, `org-cycle-hook' is disables temporarily, to make sure the
901 cursor stays at the beginning of the buffer.
902 When this option is nil, don't do anything special at the beginning
907 (defcustom org-cycle-level-after-item
/entry-creation t
908 "Non-nil means cycle entry level or item indentation in new empty entries.
910 When the cursor is at the end of an empty headline, i.e with only stars
911 and maybe a TODO keyword, TAB will then switch the entry to become a child,
912 and then all possible ancestor states, before returning to the original state.
913 This makes data entry extremely fast: M-RET to create a new headline,
914 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
916 When the cursor is at the end of an empty plain list item, one TAB will
917 make it a subitem, two or more tabs will back up to make this an item
918 higher up in the item hierarchy."
922 (defcustom org-cycle-emulate-tab t
923 "Where should `org-cycle' emulate TAB.
925 white Only in completely white lines
926 whitestart Only at the beginning of lines, before the first non-white char
927 t Everywhere except in headlines
928 exc-hl-bol Everywhere except at the start of a headline
929 If TAB is used in a place where it does not emulate TAB, the current subtree
930 visibility is cycled."
932 :type
'(choice (const :tag
"Never" nil
)
933 (const :tag
"Only in completely white lines" white
)
934 (const :tag
"Before first char in a line" whitestart
)
935 (const :tag
"Everywhere except in headlines" t
)
936 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
939 (defcustom org-cycle-separator-lines
2
940 "Number of empty lines needed to keep an empty line between collapsed trees.
941 If you leave an empty line between the end of a subtree and the following
942 headline, this empty line is hidden when the subtree is folded.
943 Org-mode will leave (exactly) one empty line visible if the number of
944 empty lines is equal or larger to the number given in this variable.
945 So the default 2 means at least 2 empty lines after the end of a subtree
946 are needed to produce free space between a collapsed subtree and the
949 If the number is negative, and the number of empty lines is at least -N,
950 all empty lines are shown.
952 Special case: when 0, never leave empty lines in collapsed view."
955 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
957 (defcustom org-pre-cycle-hook nil
958 "Hook that is run before visibility cycling is happening.
959 The function(s) in this hook must accept a single argument which indicates
960 the new state that will be set right after running this hook. The
961 argument is a symbol. Before a global state change, it can have the values
962 `overview', `content', or `all'. Before a local state change, it can have
963 the values `folded', `children', or `subtree'."
967 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
968 org-cycle-hide-drawers
969 org-cycle-show-empty-lines
970 org-optimize-window-after-visibility-change
)
971 "Hook that is run after `org-cycle' has changed the buffer visibility.
972 The function(s) in this hook must accept a single argument which indicates
973 the new state that was set by the most recent `org-cycle' command. The
974 argument is a symbol. After a global state change, it can have the values
975 `overview', `content', or `all'. After a local state change, it can have
976 the values `folded', `children', or `subtree'."
980 (defgroup org-edit-structure nil
981 "Options concerning structure editing in Org-mode."
982 :tag
"Org Edit Structure"
983 :group
'org-structure
)
985 (defcustom org-odd-levels-only nil
986 "Non-nil means skip even levels and only use odd levels for the outline.
987 This has the effect that two stars are being added/taken away in
988 promotion/demotion commands. It also influences how levels are
989 handled by the exporters.
990 Changing it requires restart of `font-lock-mode' to become effective
991 for fontification also in regions already fontified.
992 You may also set this on a per-file basis by adding one of the following
997 :group
'org-edit-structure
998 :group
'org-appearance
1001 (defcustom org-adapt-indentation t
1002 "Non-nil means adapt indentation to outline node level.
1004 When this variable is set, Org assumes that you write outlines by
1005 indenting text in each node to align with the headline (after the stars).
1006 The following issues are influenced by this variable:
1008 - When this is set and the *entire* text in an entry is indented, the
1009 indentation is increased by one space in a demotion command, and
1010 decreased by one in a promotion command. If any line in the entry
1011 body starts with text at column 0, indentation is not changed at all.
1013 - Property drawers and planning information is inserted indented when
1014 this variable s set. When nil, they will not be indented.
1016 - TAB indents a line relative to context. The lines below a headline
1017 will be indented when this variable is set.
1019 Note that this is all about true indentation, by adding and removing
1020 space characters. See also `org-indent.el' which does level-dependent
1021 indentation in a virtual way, i.e. at display time in Emacs."
1022 :group
'org-edit-structure
1025 (defcustom org-special-ctrl-a
/e nil
1026 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1028 When t, `C-a' will bring back the cursor to the beginning of the
1029 headline text, i.e. after the stars and after a possible TODO
1030 keyword. In an item, this will be the position after bullet and
1031 check-box, if any. When the cursor is already at that position,
1032 another `C-a' will bring it to the beginning of the line.
1034 `C-e' will jump to the end of the headline, ignoring the presence
1035 of tags in the headline. A second `C-e' will then jump to the
1036 true end of the line, after any tags. This also means that, when
1037 this variable is non-nil, `C-e' also will never jump beyond the
1038 end of the heading of a folded section, i.e. not after the
1041 When set to the symbol `reversed', the first `C-a' or `C-e' works
1042 normally, going to the true line boundary first. Only a directly
1043 following, identical keypress will bring the cursor to the
1046 This may also be a cons cell where the behavior for `C-a' and
1047 `C-e' is set separately."
1048 :group
'org-edit-structure
1050 (const :tag
"off" nil
)
1051 (const :tag
"on: after stars/bullet and before tags first" t
)
1052 (const :tag
"reversed: true line boundary first" reversed
)
1053 (cons :tag
"Set C-a and C-e separately"
1054 (choice :tag
"Special C-a"
1055 (const :tag
"off" nil
)
1056 (const :tag
"on: after stars/bullet first" t
)
1057 (const :tag
"reversed: before stars/bullet first" reversed
))
1058 (choice :tag
"Special C-e"
1059 (const :tag
"off" nil
)
1060 (const :tag
"on: before tags first" t
)
1061 (const :tag
"reversed: after tags first" reversed
)))))
1062 (if (fboundp 'defvaralias
)
1063 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1065 (defcustom org-special-ctrl-k nil
1066 "Non-nil means `C-k' will behave specially in headlines.
1067 When nil, `C-k' will call the default `kill-line' command.
1068 When t, the following will happen while the cursor is in the headline:
1070 - When the cursor is at the beginning of a headline, kill the entire
1071 line and possible the folded subtree below the line.
1072 - When in the middle of the headline text, kill the headline up to the tags.
1073 - When after the headline text, kill the tags."
1074 :group
'org-edit-structure
1077 (defcustom org-ctrl-k-protect-subtree nil
1078 "Non-nil means, do not delete a hidden subtree with C-k.
1079 When set to the symbol `error', simply throw an error when C-k is
1080 used to kill (part-of) a headline that has hidden text behind it.
1081 Any other non-nil value will result in a query to the user, if it is
1082 OK to kill that hidden subtree. When nil, kill without remorse."
1083 :group
'org-edit-structure
1086 (const :tag
"Do not protect hidden subtrees" nil
)
1087 (const :tag
"Protect hidden subtrees with a security query" t
)
1088 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1090 (defcustom org-catch-invisible-edits nil
1091 "Check if in invisible region before inserting or deleting a character.
1094 nil Do not check, so just do invisible edits.
1095 error Throw an error and do nothing.
1096 show Make point visible, and do the requested edit.
1097 show-and-error Make point visible, then throw an error and abort the edit.
1098 smart Make point visible, and do insertion/deletion if it is
1099 adjacent to visible text and the change feels predictable.
1100 Never delete a previously invisible character or add in the
1101 middle or right after an invisible region. Basically, this
1102 allows insertion and backward-delete right before ellipses.
1103 FIXME: maybe in this case we should not even show?"
1104 :group
'org-edit-structure
1107 (const :tag
"Do not check" nil
)
1108 (const :tag
"Throw error when trying to edit" error
)
1109 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1110 (const :tag
"Show invisible part and do the edit" show
)
1111 (const :tag
"Be smart and do the right thing" smart
)))
1113 (defcustom org-yank-folded-subtrees t
1114 "Non-nil means when yanking subtrees, fold them.
1115 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1116 it starts with a heading and all other headings in it are either children
1117 or siblings, then fold all the subtrees. However, do this only if no
1118 text after the yank would be swallowed into a folded tree by this action."
1119 :group
'org-edit-structure
1122 (defcustom org-yank-adjusted-subtrees nil
1123 "Non-nil means when yanking subtrees, adjust the level.
1124 With this setting, `org-paste-subtree' is used to insert the subtree, see
1125 this function for details."
1126 :group
'org-edit-structure
1129 (defcustom org-M-RET-may-split-line
'((default . t
))
1130 "Non-nil means M-RET will split the line at the cursor position.
1131 When nil, it will go to the end of the line before making a
1133 You may also set this option in a different way for different
1134 contexts. Valid contexts are:
1136 headline when creating a new headline
1137 item when creating a new item
1138 table in a table field
1139 default the value to be used for all contexts not explicitly
1141 :group
'org-structure
1144 (const :tag
"Always" t
)
1145 (const :tag
"Never" nil
)
1146 (repeat :greedy t
:tag
"Individual contexts"
1148 (choice :tag
"Context"
1156 (defcustom org-insert-heading-respect-content nil
1157 "Non-nil means insert new headings after the current subtree.
1158 When nil, the new heading is created directly after the current line.
1159 The commands \\[org-insert-heading-respect-content] and
1160 \\[org-insert-todo-heading-respect-content] turn this variable on
1161 for the duration of the command."
1162 :group
'org-structure
1165 (defcustom org-blank-before-new-entry
'((heading . auto
)
1166 (plain-list-item . auto
))
1167 "Should `org-insert-heading' leave a blank line before new heading/item?
1168 The value is an alist, with `heading' and `plain-list-item' as CAR,
1169 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1170 which case Org will look at the surrounding headings/items and try to
1171 make an intelligent decision whether to insert a blank line or not.
1173 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1174 set, the setting here is ignored and no empty line is inserted, to avoid
1175 breaking the list structure."
1176 :group
'org-edit-structure
1178 (cons (const heading
)
1179 (choice (const :tag
"Never" nil
)
1180 (const :tag
"Always" t
)
1181 (const :tag
"Auto" auto
)))
1182 (cons (const plain-list-item
)
1183 (choice (const :tag
"Never" nil
)
1184 (const :tag
"Always" t
)
1185 (const :tag
"Auto" auto
)))))
1187 (defcustom org-insert-heading-hook nil
1188 "Hook being run after inserting a new heading."
1189 :group
'org-edit-structure
1192 (defcustom org-enable-fixed-width-editor t
1193 "Non-nil means lines starting with \":\" are treated as fixed-width.
1194 This currently only means they are never auto-wrapped.
1195 When nil, such lines will be treated like ordinary lines.
1196 See also the QUOTE keyword."
1197 :group
'org-edit-structure
1200 (defcustom org-goto-auto-isearch t
1201 "Non-nil means typing characters in `org-goto' starts incremental search."
1202 :group
'org-edit-structure
1205 (defgroup org-sparse-trees nil
1206 "Options concerning sparse trees in Org-mode."
1207 :tag
"Org Sparse Trees"
1208 :group
'org-structure
)
1210 (defcustom org-highlight-sparse-tree-matches t
1211 "Non-nil means highlight all matches that define a sparse tree.
1212 The highlights will automatically disappear the next time the buffer is
1213 changed by an edit command."
1214 :group
'org-sparse-trees
1217 (defcustom org-remove-highlights-with-change t
1218 "Non-nil means any change to the buffer will remove temporary highlights.
1219 Such highlights are created by `org-occur' and `org-clock-display'.
1220 When nil, `C-c C-c needs to be used to get rid of the highlights.
1221 The highlights created by `org-preview-latex-fragment' always need
1222 `C-c C-c' to be removed."
1223 :group
'org-sparse-trees
1228 (defcustom org-occur-hook
'(org-first-headline-recenter)
1229 "Hook that is run after `org-occur' has constructed a sparse tree.
1230 This can be used to recenter the window to show as much of the structure
1232 :group
'org-sparse-trees
1235 (defgroup org-imenu-and-speedbar nil
1236 "Options concerning imenu and speedbar in Org-mode."
1237 :tag
"Org Imenu and Speedbar"
1238 :group
'org-structure
)
1240 (defcustom org-imenu-depth
2
1241 "The maximum level for Imenu access to Org-mode headlines.
1242 This also applied for speedbar access."
1243 :group
'org-imenu-and-speedbar
1246 (defgroup org-table nil
1247 "Options concerning tables in Org-mode."
1251 (defcustom org-enable-table-editor
'optimized
1252 "Non-nil means lines starting with \"|\" are handled by the table editor.
1253 When nil, such lines will be treated like ordinary lines.
1255 When equal to the symbol `optimized', the table editor will be optimized to
1257 - Automatic overwrite mode in front of whitespace in table fields.
1258 This makes the structure of the table stay in tact as long as the edited
1259 field does not exceed the column width.
1260 - Minimize the number of realigns. Normally, the table is aligned each time
1261 TAB or RET are pressed to move to another field. With optimization this
1262 happens only if changes to a field might have changed the column width.
1263 Optimization requires replacing the functions `self-insert-command',
1264 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1265 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1266 very good at guessing when a re-align will be necessary, but you can always
1267 force one with \\[org-ctrl-c-ctrl-c].
1269 If you would like to use the optimized version in Org-mode, but the
1270 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1272 This variable can be used to turn on and off the table editor during a session,
1273 but in order to toggle optimization, a restart is required.
1275 See also the variable `org-table-auto-blank-field'."
1278 (const :tag
"off" nil
)
1280 (const :tag
"on, optimized" optimized
)))
1282 (defcustom org-self-insert-cluster-for-undo t
1283 "Non-nil means cluster self-insert commands for undo when possible.
1284 If this is set, then, like in the Emacs command loop, 20 consecutive
1285 characters will be undone together.
1286 This is configurable, because there is some impact on typing performance."
1290 (defcustom org-table-tab-recognizes-table.el t
1291 "Non-nil means TAB will automatically notice a table.el table.
1292 When it sees such a table, it moves point into it and - if necessary -
1293 calls `table-recognize-table'."
1294 :group
'org-table-editing
1297 (defgroup org-link nil
1298 "Options concerning links in Org-mode."
1302 (defvar org-link-abbrev-alist-local nil
1303 "Buffer-local version of `org-link-abbrev-alist', which see.
1304 The value of this is taken from the #+LINK lines.")
1305 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1307 (defcustom org-link-abbrev-alist nil
1308 "Alist of link abbreviations.
1309 The car of each element is a string, to be replaced at the start of a link.
1310 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1311 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1313 [[linkkey:tag][description]]
1315 The 'linkkey' must be a word word, starting with a letter, followed
1316 by letters, numbers, '-' or '_'.
1318 If REPLACE is a string, the tag will simply be appended to create the link.
1319 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1320 the placeholder \"%h\" will cause a url-encoded version of the tag to
1321 be inserted at that point (see the function `url-hexify-string').
1323 REPLACE may also be a function that will be called with the tag as the
1324 only argument to create the link, which should be returned as a string.
1326 See the manual for examples."
1330 (string :tag
"Protocol")
1332 (string :tag
"Format")
1335 (defcustom org-descriptive-links t
1336 "Non-nil means Org will display descriptive links.
1337 E.g. [[http://orgmode.org][Org website]] will be displayed as
1338 \"Org Website\", hiding the link itself and just displaying its
1339 description. When set to `nil', Org will display the full links
1342 You can interactively set the value of this variable by calling
1343 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1347 (defcustom org-link-file-path-type
'adaptive
1348 "How the path name in file links should be stored.
1351 relative Relative to the current directory, i.e. the directory of the file
1352 into which the link is being inserted.
1353 absolute Absolute path, if possible with ~ for home directory.
1354 noabbrev Absolute path, no abbreviation of home directory.
1355 adaptive Use relative path for files in the current directory and sub-
1356 directories of it. For other files, use an absolute path."
1364 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1365 "Types of links that should be activated in Org-mode files.
1366 This is a list of symbols, each leading to the activation of a certain link
1367 type. In principle, it does not hurt to turn on most link types - there may
1368 be a small gain when turning off unused link types. The types are:
1370 bracket The recommended [[link][description]] or [[link]] links with hiding.
1371 angle Links in angular brackets that may contain whitespace like
1372 <bbdb:Carsten Dominik>.
1373 plain Plain links in normal text, no whitespace, like http://google.com.
1374 radio Text that is matched by a radio target, see manual for details.
1375 tag Tag settings in a headline (link to tag search).
1376 date Time stamps (link to calendar).
1377 footnote Footnote labels.
1379 Changing this variable requires a restart of Emacs to become effective."
1381 :type
'(set :greedy t
1382 (const :tag
"Double bracket links" bracket
)
1383 (const :tag
"Angular bracket links" angle
)
1384 (const :tag
"Plain text links" plain
)
1385 (const :tag
"Radio target matches" radio
)
1386 (const :tag
"Tags" tag
)
1387 (const :tag
"Timestamps" date
)
1388 (const :tag
"Footnotes" footnote
)))
1390 (defcustom org-make-link-description-function nil
1391 "Function to use to generate link descriptions from links.
1392 If nil the link location will be used. This function must take
1393 two parameters; the first is the link and the second the
1394 description `org-insert-link' has generated, and should return the
1395 description to use."
1399 (defgroup org-link-store nil
1400 "Options concerning storing links in Org-mode."
1401 :tag
"Org Store Link"
1404 (defcustom org-email-link-description-format
"Email %c: %.30s"
1405 "Format of the description part of a link to an email or usenet message.
1406 The following %-escapes will be replaced by corresponding information:
1408 %F full \"From\" field
1409 %f name, taken from \"From\" field, address if no name
1410 %T full \"To\" field
1411 %t first name in \"To\" field, address if no name
1412 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1413 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1418 You may use normal field width specification between the % and the letter.
1419 This is for example useful to limit the length of the subject.
1421 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1422 :group
'org-link-store
1425 (defcustom org-from-is-user-regexp
1427 (when (and user-mail-address
(not (string= user-mail-address
"")))
1428 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1429 (when (and user-full-name
(not (string= user-full-name
"")))
1430 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1431 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1432 "Regexp matched against the \"From:\" header of an email or usenet message.
1433 It should match if the message is from the user him/herself."
1434 :group
'org-link-store
1437 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1438 "Non-nil means storing a link to an Org file will use entry IDs.
1440 Note that before this variable is even considered, org-id must be loaded,
1441 so please customize `org-modules' and turn it on.
1443 The variable can have the following values:
1445 t Create an ID if needed to make a link to the current entry.
1447 create-if-interactive
1448 If `org-store-link' is called directly (interactively, as a user
1449 command), do create an ID to support the link. But when doing the
1450 job for remember, only use the ID if it already exists. The
1451 purpose of this setting is to avoid proliferation of unwanted
1452 IDs, just because you happen to be in an Org file when you
1453 call `org-remember' that automatically and preemptively
1454 creates a link. If you do want to get an ID link in a remember
1455 template to an entry not having an ID, create it first by
1456 explicitly creating a link to it, using `C-c C-l' first.
1458 create-if-interactive-and-no-custom-id
1459 Like create-if-interactive, but do not create an ID if there is
1460 a CUSTOM_ID property defined in the entry. This is the default.
1463 Use existing ID, do not create one.
1465 nil Never use an ID to make a link, instead link using a text search for
1467 :group
'org-link-store
1469 (const :tag
"Create ID to make link" t
)
1470 (const :tag
"Create if storing link interactively"
1471 create-if-interactive
)
1472 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1473 create-if-interactive-and-no-custom-id
)
1474 (const :tag
"Only use existing" use-existing
)
1475 (const :tag
"Do not use ID to create link" nil
)))
1477 (defcustom org-context-in-file-links t
1478 "Non-nil means file links from `org-store-link' contain context.
1479 A search string will be added to the file name with :: as separator and
1480 used to find the context when the link is activated by the command
1481 `org-open-at-point'. When this option is t, the entire active region
1482 will be placed in the search string of the file link. If set to a
1483 positive integer, only the first n lines of context will be stored.
1485 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1486 negates this setting for the duration of the command."
1487 :group
'org-link-store
1488 :type
'(choice boolean integer
))
1490 (defcustom org-keep-stored-link-after-insertion nil
1491 "Non-nil means keep link in list for entire session.
1493 The command `org-store-link' adds a link pointing to the current
1494 location to an internal list. These links accumulate during a session.
1495 The command `org-insert-link' can be used to insert links into any
1496 Org-mode file (offering completion for all stored links). When this
1497 option is nil, every link which has been inserted once using \\[org-insert-link]
1498 will be removed from the list, to make completing the unused links
1500 :group
'org-link-store
1503 (defgroup org-link-follow nil
1504 "Options concerning following links in Org-mode."
1505 :tag
"Org Follow Link"
1508 (defcustom org-link-translation-function nil
1509 "Function to translate links with different syntax to Org syntax.
1510 This can be used to translate links created for example by the Planner
1511 or emacs-wiki packages to Org syntax.
1512 The function must accept two parameters, a TYPE containing the link
1513 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1514 which is everything after the link protocol. It should return a cons
1515 with possibly modified values of type and path.
1516 Org contains a function for this, so if you set this variable to
1517 `org-translate-link-from-planner', you should be able follow many
1518 links created by planner."
1519 :group
'org-link-follow
1522 (defcustom org-follow-link-hook nil
1523 "Hook that is run after a link has been followed."
1524 :group
'org-link-follow
1527 (defcustom org-tab-follows-link nil
1528 "Non-nil means on links TAB will follow the link.
1529 Needs to be set before org.el is loaded.
1530 This really should not be used, it does not make sense, and the
1531 implementation is bad."
1532 :group
'org-link-follow
1535 (defcustom org-return-follows-link nil
1536 "Non-nil means on links RET will follow the link."
1537 :group
'org-link-follow
1540 (defcustom org-mouse-1-follows-link
1541 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1542 "Non-nil means mouse-1 on a link will follow the link.
1543 A longer mouse click will still set point. Does not work on XEmacs.
1544 Needs to be set before org.el is loaded."
1545 :group
'org-link-follow
1548 (defcustom org-mark-ring-length
4
1549 "Number of different positions to be recorded in the ring.
1550 Changing this requires a restart of Emacs to work correctly."
1551 :group
'org-link-follow
1554 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1555 "Non-nil means internal links in Org files must exactly match a headline.
1556 When nil, the link search tries to match a phrase with all words
1557 in the search text."
1558 :group
'org-link-follow
1561 (const :tag
"Use fuzzy text search" nil
)
1562 (const :tag
"Match only exact headline" t
)
1563 (const :tag
"Match exact headline or query to create it"
1566 (defcustom org-link-frame-setup
1567 '((vm . vm-visit-folder-other-frame
)
1568 (vm-imap . vm-visit-imap-folder-other-frame
)
1569 (gnus . org-gnus-no-new-news
)
1570 (file . find-file-other-window
)
1571 (wl . wl-other-frame
))
1572 "Setup the frame configuration for following links.
1573 When following a link with Emacs, it may often be useful to display
1574 this link in another window or frame. This variable can be used to
1575 set this up for the different types of links.
1578 `vm-visit-folder-other-window'
1579 `vm-visit-folder-other-frame'
1580 For Gnus, use any of
1583 `org-gnus-no-new-news'
1584 For FILE, use any of
1586 `find-file-other-window'
1587 `find-file-other-frame'
1588 For Wanderlust use any of
1591 For the calendar, use the variable `calendar-setup'.
1592 For BBDB, it is currently only possible to display the matches in
1594 :group
'org-link-follow
1598 (const vm-visit-folder
)
1599 (const vm-visit-folder-other-window
)
1600 (const vm-visit-folder-other-frame
)))
1604 (const gnus-other-frame
)
1605 (const org-gnus-no-new-news
)))
1609 (const find-file-other-window
)
1610 (const find-file-other-frame
)))
1614 (const wl-other-frame
)))))
1616 (defcustom org-display-internal-link-with-indirect-buffer nil
1617 "Non-nil means use indirect buffer to display infile links.
1618 Activating internal links (from one location in a file to another location
1619 in the same file) normally just jumps to the location. When the link is
1620 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1622 another window. When this option is set, the other window actually displays
1623 an indirect buffer clone of the current buffer, to avoid any visibility
1624 changes to the current buffer."
1625 :group
'org-link-follow
1628 (defcustom org-open-non-existing-files nil
1629 "Non-nil means `org-open-file' will open non-existing files.
1630 When nil, an error will be generated.
1631 This variable applies only to external applications because they
1632 might choke on non-existing files. If the link is to a file that
1633 will be opened in Emacs, the variable is ignored."
1634 :group
'org-link-follow
1637 (defcustom org-open-directory-means-index-dot-org nil
1638 "Non-nil means a link to a directory really means to index.org.
1639 When nil, following a directory link will run dired or open a finder/explorer
1640 window on that directory."
1641 :group
'org-link-follow
1644 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1645 "Function and arguments to call for following mailto links.
1646 This is a list with the first element being a Lisp function, and the
1647 remaining elements being arguments to the function. In string arguments,
1648 %a will be replaced by the address, and %s will be replaced by the subject
1649 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1650 :group
'org-link-follow
1652 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1653 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1654 (const :tag
"message-mail" (message-mail "%a" "%s"))
1655 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1657 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1658 "Non-nil means ask for confirmation before executing shell links.
1659 Shell links can be dangerous: just think about a link
1661 [[shell:rm -rf ~/*][Google Search]]
1663 This link would show up in your Org-mode document as \"Google Search\",
1664 but really it would remove your entire home directory.
1665 Therefore we advise against setting this variable to nil.
1666 Just change it to `y-or-n-p' if you want to confirm with a
1667 single keystroke rather than having to type \"yes\"."
1668 :group
'org-link-follow
1670 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1671 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1672 (const :tag
"no confirmation (dangerous)" nil
)))
1673 (put 'org-confirm-shell-link-function
1674 'safe-local-variable
1675 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1677 (defcustom org-confirm-shell-link-not-regexp
""
1678 "A regexp to skip confirmation for shell links."
1679 :group
'org-link-follow
1683 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1684 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1685 Elisp links can be dangerous: just think about a link
1687 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1689 This link would show up in your Org-mode document as \"Google Search\",
1690 but really it would remove your entire home directory.
1691 Therefore we advise against setting this variable to nil.
1692 Just change it to `y-or-n-p' if you want to confirm with a
1693 single keystroke rather than having to type \"yes\"."
1694 :group
'org-link-follow
1696 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1697 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1698 (const :tag
"no confirmation (dangerous)" nil
)))
1699 (put 'org-confirm-shell-link-function
1700 'safe-local-variable
1701 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1703 (defcustom org-confirm-elisp-link-not-regexp
""
1704 "A regexp to skip confirmation for Elisp links."
1705 :group
'org-link-follow
1709 (defconst org-file-apps-defaults-gnu
1713 "Default file applications on a UNIX or GNU/Linux system.
1714 See `org-file-apps'.")
1716 (defconst org-file-apps-defaults-macosx
1719 (system .
"open %s")
1721 ("eps.gz" .
"gv %s")
1723 ("fig" .
"xfig %s"))
1724 "Default file applications on a MacOS X system.
1725 The system \"open\" is known as a default, but we use X11 applications
1726 for some files for which the OS does not have a good default.
1727 See `org-file-apps'.")
1729 (defconst org-file-apps-defaults-windowsnt
1733 (list (if (featurep 'xemacs
)
1734 'mswindows-shell-execute
1738 (list (if (featurep 'xemacs
)
1739 'mswindows-shell-execute
1742 "Default file applications on a Windows NT system.
1743 The system \"open\" is used for most files.
1744 See `org-file-apps'.")
1746 (defcustom org-file-apps
1749 ("\\.mm\\'" . default
)
1750 ("\\.x?html?\\'" . default
)
1751 ("\\.pdf\\'" . default
)
1753 "External applications for opening `file:path' items in a document.
1754 Org-mode uses system defaults for different file types, but
1755 you can use this variable to set the application for a given file
1756 extension. The entries in this list are cons cells where the car identifies
1757 files and the cdr the corresponding command. Possible values for the
1759 \"string\" A string as a file identifier can be interpreted in different
1760 ways, depending on its contents:
1762 - Alphanumeric characters only:
1763 Match links with this file extension.
1764 Example: (\"pdf\" . \"evince %s\")
1765 to open PDFs with evince.
1767 - Regular expression: Match links where the
1768 filename matches the regexp. If you want to
1769 use groups here, use shy groups.
1771 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1772 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1773 to open *.html and *.xhtml with firefox.
1775 - Regular expression which contains (non-shy) groups:
1776 Match links where the whole link, including \"::\", and
1777 anything after that, matches the regexp.
1778 In a custom command string, %1, %2, etc. are replaced with
1779 the parts of the link that were matched by the groups.
1780 For backwards compatibility, if a command string is given
1781 that does not use any of the group matches, this case is
1782 handled identically to the second one (i.e. match against
1784 In a custom lisp form, you can access the group matches with
1785 (match-string n link).
1787 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1788 to open [[file:document.pdf::5]] with evince at page 5.
1790 `directory' Matches a directory
1791 `remote' Matches a remote file, accessible through tramp or efs.
1792 Remote files most likely should be visited through Emacs
1793 because external applications cannot handle such paths.
1794 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1795 so all files Emacs knows how to handle. Using this with
1796 command `emacs' will open most files in Emacs. Beware that this
1797 will also open html files inside Emacs, unless you add
1798 (\"html\" . default) to the list as well.
1799 t Default for files not matched by any of the other options.
1800 `system' The system command to open files, like `open' on Windows
1801 and Mac OS X, and mailcap under GNU/Linux. This is the command
1802 that will be selected if you call `C-c C-o' with a double
1803 \\[universal-argument] \\[universal-argument] prefix.
1805 Possible values for the command are:
1806 `emacs' The file will be visited by the current Emacs process.
1807 `default' Use the default application for this file type, which is the
1808 association for t in the list, most likely in the system-specific
1810 This can be used to overrule an unwanted setting in the
1811 system-specific variable.
1812 `system' Use the system command for opening files, like \"open\".
1813 This command is specified by the entry whose car is `system'.
1814 Most likely, the system-specific version of this variable
1815 does define this command, but you can overrule/replace it
1817 string A command to be executed by a shell; %s will be replaced
1818 by the path to the file.
1819 sexp A Lisp form which will be evaluated. The file path will
1820 be available in the Lisp variable `file'.
1821 For more examples, see the system specific constants
1822 `org-file-apps-defaults-macosx'
1823 `org-file-apps-defaults-windowsnt'
1824 `org-file-apps-defaults-gnu'."
1825 :group
'org-link-follow
1827 (cons (choice :value
""
1828 (string :tag
"Extension")
1829 (const :tag
"System command to open files" system
)
1830 (const :tag
"Default for unrecognized files" t
)
1831 (const :tag
"Remote file" remote
)
1832 (const :tag
"Links to a directory" directory
)
1833 (const :tag
"Any files that have Emacs modes"
1836 (const :tag
"Visit with Emacs" emacs
)
1837 (const :tag
"Use default" default
)
1838 (const :tag
"Use the system command" system
)
1839 (string :tag
"Command")
1840 (sexp :tag
"Lisp form")))))
1844 (defgroup org-refile nil
1845 "Options concerning refiling entries in Org-mode."
1849 (defcustom org-directory
"~/org"
1850 "Directory with org files.
1851 This is just a default location to look for Org files. There is no need
1852 at all to put your files into this directory. It is only used in the
1853 following situations:
1855 1. When a remember template specifies a target file that is not an
1856 absolute path. The path will then be interpreted relative to
1858 2. When a remember note is filed away in an interactive way (when exiting the
1859 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1860 with `org-directory' as the default path."
1862 :group
'org-remember
1865 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1866 "Default target for storing notes.
1867 Used as a fall back file for org-remember.el and org-capture.el, for
1868 templates that do not specify a target file."
1870 :group
'org-remember
1872 (const :tag
"Default from remember-data-file" nil
)
1875 (defcustom org-goto-interface
'outline
1876 "The default interface to be used for `org-goto'.
1878 outline The interface shows an outline of the relevant file
1879 and the correct heading is found by moving through
1880 the outline or by searching with incremental search.
1881 outline-path-completion Headlines in the current buffer are offered via
1882 completion. This is the interface also used by
1883 the refile command."
1886 (const :tag
"Outline" outline
)
1887 (const :tag
"Outline-path-completion" outline-path-completion
)))
1889 (defcustom org-goto-max-level
5
1890 "Maximum target level when running `org-goto' with refile interface."
1894 (defcustom org-reverse-note-order nil
1895 "Non-nil means store new notes at the beginning of a file or entry.
1896 When nil, new notes will be filed to the end of a file or entry.
1897 This can also be a list with cons cells of regular expressions that
1898 are matched against file names, and values."
1899 :group
'org-remember
1902 (const :tag
"Reverse always" t
)
1903 (const :tag
"Reverse never" nil
)
1904 (repeat :tag
"By file name regexp"
1905 (cons regexp boolean
))))
1907 (defcustom org-log-refile nil
1908 "Information to record when a task is refiled.
1910 Possible values are:
1912 nil Don't add anything
1913 time Add a time stamp to the task
1914 note Prompt for a note and add it with template `org-log-note-headings'
1916 This option can also be set with on a per-file-basis with
1918 #+STARTUP: nologrefile
1919 #+STARTUP: logrefile
1920 #+STARTUP: lognoterefile
1922 You can have local logging settings for a subtree by setting the LOGGING
1923 property to one or more of these keywords.
1925 When bulk-refiling from the agenda, the value `note' is forbidden and
1926 will temporarily be changed to `time'."
1928 :group
'org-progress
1931 (const :tag
"No logging" nil
)
1932 (const :tag
"Record timestamp" time
)
1933 (const :tag
"Record timestamp with note." note
)))
1935 (defcustom org-refile-targets nil
1936 "Targets for refiling entries with \\[org-refile].
1937 This is a list of cons cells. Each cell contains:
1938 - a specification of the files to be considered, either a list of files,
1939 or a symbol whose function or variable value will be used to retrieve
1940 a file name or a list of file names. If you use `org-agenda-files' for
1941 that, all agenda files will be scanned for targets. Nil means consider
1942 headings in the current buffer.
1943 - A specification of how to find candidate refile targets. This may be
1945 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1946 This tag has to be present in all target headlines, inheritance will
1948 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1950 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1951 headlines that are refiling targets.
1952 - a cons cell (:level . N). Any headline of level N is considered a target.
1953 Note that, when `org-odd-levels-only' is set, level corresponds to
1954 order in hierarchy, not to the number of stars.
1955 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1956 Note that, when `org-odd-levels-only' is set, level corresponds to
1957 order in hierarchy, not to the number of stars.
1959 Each element of this list generates a set of possible targets.
1960 The union of these sets is presented (with completion) to
1961 the user by `org-refile'.
1963 You can set the variable `org-refile-target-verify-function' to a function
1964 to verify each headline found by the simple criteria above.
1966 When this variable is nil, all top-level headlines in the current buffer
1967 are used, equivalent to the value `((nil . (:level . 1))'."
1971 (choice :value org-agenda-files
1972 (const :tag
"All agenda files" org-agenda-files
)
1973 (const :tag
"Current buffer" nil
)
1974 (function) (variable) (file))
1975 (choice :tag
"Identify target headline by"
1976 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1977 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1978 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1979 (cons :tag
"Level number" (const :value
:level
) (integer))
1980 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1982 (defcustom org-refile-target-verify-function nil
1983 "Function to verify if the headline at point should be a refile target.
1984 The function will be called without arguments, with point at the
1985 beginning of the headline. It should return t and leave point
1986 where it is if the headline is a valid target for refiling.
1988 If the target should not be selected, the function must return nil.
1989 In addition to this, it may move point to a place from where the search
1990 should be continued. For example, the function may decide that the entire
1991 subtree of the current entry should be excluded and move point to the end
1996 (defcustom org-refile-use-cache nil
1997 "Non-nil means cache refile targets to speed up the process.
1998 The cache for a particular file will be updated automatically when
1999 the buffer has been killed, or when any of the marker used for flagging
2000 refile targets no longer points at a live buffer.
2001 If you have added new entries to a buffer that might themselves be targets,
2002 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2003 find that easier, `C-u C-u C-u C-c C-w'."
2008 (defcustom org-refile-use-outline-path nil
2009 "Non-nil means provide refile targets as paths.
2010 So a level 3 headline will be available as level1/level2/level3.
2012 When the value is `file', also include the file name (without directory)
2013 into the path. In this case, you can also stop the completion after
2014 the file name, to get entries inserted as top level in the file.
2016 When `full-file-path', include the full file path."
2019 (const :tag
"Not" nil
)
2020 (const :tag
"Yes" t
)
2021 (const :tag
"Start with file name" file
)
2022 (const :tag
"Start with full file path" full-file-path
)))
2024 (defcustom org-outline-path-complete-in-steps t
2025 "Non-nil means complete the outline path in hierarchical steps.
2026 When Org-mode uses the refile interface to select an outline path
2027 \(see variable `org-refile-use-outline-path'), the completion of
2028 the path can be done is a single go, or if can be done in steps down
2029 the headline hierarchy. Going in steps is probably the best if you
2030 do not use a special completion package like `ido' or `icicles'.
2031 However, when using these packages, going in one step can be very
2032 fast, while still showing the whole path to the entry."
2036 (defcustom org-refile-allow-creating-parent-nodes nil
2037 "Non-nil means allow to create new nodes as refile targets.
2038 New nodes are then created by adding \"/new node name\" to the completion
2039 of an existing node. When the value of this variable is `confirm',
2040 new node creation must be confirmed by the user (recommended)
2041 When nil, the completion must match an existing entry.
2043 Note that, if the new heading is not seen by the criteria
2044 listed in `org-refile-targets', multiple instances of the same
2045 heading would be created by trying again to file under the new
2049 (const :tag
"Never" nil
)
2050 (const :tag
"Always" t
)
2051 (const :tag
"Prompt for confirmation" confirm
)))
2053 (defcustom org-refile-active-region-within-subtree nil
2054 "Non-nil means also refile active region within a subtree.
2056 By default `org-refile' doesn't allow refiling regions if they
2057 don't contain a set of subtrees, but it might be convenient to
2058 do so sometimes: in that case, the first line of the region is
2059 converted to a headline before refiling."
2064 (defgroup org-todo nil
2065 "Options concerning TODO items in Org-mode."
2069 (defgroup org-progress nil
2070 "Options concerning Progress logging in Org-mode."
2074 (defvar org-todo-interpretation-widgets
2075 '((:tag
"Sequence (cycling hits every state)" sequence
)
2076 (:tag
"Type (cycling directly to DONE)" type
))
2077 "The available interpretation symbols for customizing `org-todo-keywords'.
2078 Interested libraries should add to this list.")
2080 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2081 "List of TODO entry keyword sequences and their interpretation.
2082 \\<org-mode-map>This is a list of sequences.
2084 Each sequence starts with a symbol, either `sequence' or `type',
2085 indicating if the keywords should be interpreted as a sequence of
2086 action steps, or as different types of TODO items. The first
2087 keywords are states requiring action - these states will select a headline
2088 for inclusion into the global TODO list Org-mode produces. If one of
2089 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2090 signify that no further action is necessary. If \"|\" is not found,
2091 the last keyword is treated as the only DONE state of the sequence.
2093 The command \\[org-todo] cycles an entry through these states, and one
2094 additional state where no keyword is present. For details about this
2095 cycling, see the manual.
2097 TODO keywords and interpretation can also be set on a per-file basis with
2098 the special #+SEQ_TODO and #+TYP_TODO lines.
2100 Each keyword can optionally specify a character for fast state selection
2101 \(in combination with the variable `org-use-fast-todo-selection')
2102 and specifiers for state change logging, using the same syntax
2103 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
2104 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2105 indicates to record a time stamp each time this state is selected.
2107 Each keyword may also specify if a timestamp or a note should be
2108 recorded when entering or leaving the state, by adding additional
2109 characters in the parenthesis after the keyword. This looks like this:
2110 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2111 record only the time of the state change. With X and Y being either
2112 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2113 Y when leaving the state if and only if the *target* state does not
2114 define X. You may omit any of the fast-selection key or X or /Y,
2115 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2117 For backward compatibility, this variable may also be just a list
2118 of keywords - in this case the interpretation (sequence or type) will be
2119 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2121 :group
'org-keywords
2123 (repeat :tag
"Old syntax, just keywords"
2124 (string :tag
"Keyword"))
2125 (repeat :tag
"New syntax"
2128 :tag
"Interpretation"
2129 ;;Quick and dirty way to see
2130 ;;`org-todo-interpretations'. This takes the
2131 ;;place of item arguments
2139 org-todo-interpretation-widgets
))
2142 (string :tag
"Keyword"))))))
2144 (defvar org-todo-keywords-1 nil
2145 "All TODO and DONE keywords active in a buffer.")
2146 (make-variable-buffer-local 'org-todo-keywords-1
)
2147 (defvar org-todo-keywords-for-agenda nil
)
2148 (defvar org-done-keywords-for-agenda nil
)
2149 (defvar org-drawers-for-agenda nil
)
2150 (defvar org-todo-keyword-alist-for-agenda nil
)
2151 (defvar org-tag-alist-for-agenda nil
)
2152 (defvar org-agenda-contributing-files nil
)
2153 (defvar org-not-done-keywords nil
)
2154 (make-variable-buffer-local 'org-not-done-keywords
)
2155 (defvar org-done-keywords nil
)
2156 (make-variable-buffer-local 'org-done-keywords
)
2157 (defvar org-todo-heads nil
)
2158 (make-variable-buffer-local 'org-todo-heads
)
2159 (defvar org-todo-sets nil
)
2160 (make-variable-buffer-local 'org-todo-sets
)
2161 (defvar org-todo-log-states nil
)
2162 (make-variable-buffer-local 'org-todo-log-states
)
2163 (defvar org-todo-kwd-alist nil
)
2164 (make-variable-buffer-local 'org-todo-kwd-alist
)
2165 (defvar org-todo-key-alist nil
)
2166 (make-variable-buffer-local 'org-todo-key-alist
)
2167 (defvar org-todo-key-trigger nil
)
2168 (make-variable-buffer-local 'org-todo-key-trigger
)
2170 (defcustom org-todo-interpretation
'sequence
2171 "Controls how TODO keywords are interpreted.
2172 This variable is in principle obsolete and is only used for
2173 backward compatibility, if the interpretation of todo keywords is
2174 not given already in `org-todo-keywords'. See that variable for
2177 :group
'org-keywords
2178 :type
'(choice (const sequence
)
2181 (defcustom org-use-fast-todo-selection t
2182 "Non-nil means use the fast todo selection scheme with C-c C-t.
2183 This variable describes if and under what circumstances the cycling
2184 mechanism for TODO keywords will be replaced by a single-key, direct
2187 When nil, fast selection is never used.
2189 When the symbol `prefix', it will be used when `org-todo' is called with
2190 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
2191 in an agenda buffer.
2193 When t, fast selection is used by default. In this case, the prefix
2194 argument forces cycling instead.
2196 In all cases, the special interface is only used if access keys have actually
2197 been assigned by the user, i.e. if keywords in the configuration are followed
2198 by a letter in parenthesis, like TODO(t)."
2201 (const :tag
"Never" nil
)
2202 (const :tag
"By default" t
)
2203 (const :tag
"Only with C-u C-c C-t" prefix
)))
2205 (defcustom org-provide-todo-statistics t
2206 "Non-nil means update todo statistics after insert and toggle.
2207 ALL-HEADLINES means update todo statistics by including headlines
2208 with no TODO keyword as well, counting them as not done.
2209 A list of TODO keywords means the same, but skip keywords that are
2212 When this is set, todo statistics is updated in the parent of the
2213 current entry each time a todo state is changed."
2216 (const :tag
"Yes, only for TODO entries" t
)
2217 (const :tag
"Yes, including all entries" 'all-headlines
)
2218 (repeat :tag
"Yes, for TODOs in this list"
2219 (string :tag
"TODO keyword"))
2220 (other :tag
"No TODO statistics" nil
)))
2222 (defcustom org-hierarchical-todo-statistics t
2223 "Non-nil means TODO statistics covers just direct children.
2224 When nil, all entries in the subtree are considered.
2225 This has only an effect if `org-provide-todo-statistics' is set.
2226 To set this to nil for only a single subtree, use a COOKIE_DATA
2227 property and include the word \"recursive\" into the value."
2231 (defcustom org-after-todo-state-change-hook nil
2232 "Hook which is run after the state of a TODO item was changed.
2233 The new state (a string with a TODO keyword, or nil) is available in the
2234 Lisp variable `state'."
2238 (defvar org-blocker-hook nil
2239 "Hook for functions that are allowed to block a state change.
2241 Each function gets as its single argument a property list, see
2242 `org-trigger-hook' for more information about this list.
2244 If any of the functions in this hook returns nil, the state change
2247 (defvar org-trigger-hook nil
2248 "Hook for functions that are triggered by a state change.
2250 Each function gets as its single argument a property list with at least
2251 the following elements:
2253 (:type type-of-change :position pos-at-entry-start
2254 :from old-state :to new-state)
2256 Depending on the type, more properties may be present.
2258 This mechanism is currently implemented for:
2262 :type todo-state-change
2263 :from previous state (keyword as a string), or nil, or a symbol
2264 'todo' or 'done', to indicate the general type of state.
2265 :to new state, like in :from")
2267 (defcustom org-enforce-todo-dependencies nil
2268 "Non-nil means undone TODO entries will block switching the parent to DONE.
2269 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2270 be blocked if any prior sibling is not yet done.
2271 Finally, if the parent is blocked because of ordered siblings of its own,
2272 the child will also be blocked.
2273 This variable needs to be set before org.el is loaded, and you need to
2274 restart Emacs after a change to make the change effective. The only way
2275 to change is while Emacs is running is through the customize interface."
2276 :set
(lambda (var val
)
2279 (add-hook 'org-blocker-hook
2280 'org-block-todo-from-children-or-siblings-or-parent
)
2281 (remove-hook 'org-blocker-hook
2282 'org-block-todo-from-children-or-siblings-or-parent
)))
2286 (defcustom org-enforce-todo-checkbox-dependencies nil
2287 "Non-nil means unchecked boxes will block switching the parent to DONE.
2288 When this is nil, checkboxes have no influence on switching TODO states.
2289 When non-nil, you first need to check off all check boxes before the TODO
2290 entry can be switched to DONE.
2291 This variable needs to be set before org.el is loaded, and you need to
2292 restart Emacs after a change to make the change effective. The only way
2293 to change is while Emacs is running is through the customize interface."
2294 :set
(lambda (var val
)
2297 (add-hook 'org-blocker-hook
2298 'org-block-todo-from-checkboxes
)
2299 (remove-hook 'org-blocker-hook
2300 'org-block-todo-from-checkboxes
)))
2304 (defcustom org-treat-insert-todo-heading-as-state-change nil
2305 "Non-nil means inserting a TODO heading is treated as state change.
2306 So when the command \\[org-insert-todo-heading] is used, state change
2307 logging will apply if appropriate. When nil, the new TODO item will
2308 be inserted directly, and no logging will take place."
2312 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2313 "Non-nil means switching TODO states with S-cursor counts as state change.
2314 This is the default behavior. However, setting this to nil allows a
2315 convenient way to select a TODO state and bypass any logging associated
2320 (defcustom org-todo-state-tags-triggers nil
2321 "Tag changes that should be triggered by TODO state changes.
2322 This is a list. Each entry is
2324 (state-change (tag . flag) .......)
2326 State-change can be a string with a state, and empty string to indicate the
2327 state that has no TODO keyword, or it can be one of the symbols `todo'
2328 or `done', meaning any not-done or done state, respectively."
2332 (cons (choice :tag
"When changing to"
2333 (const :tag
"Not-done state" todo
)
2334 (const :tag
"Done state" done
)
2335 (string :tag
"State"))
2337 (cons :tag
"Tag action"
2339 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2341 (defcustom org-log-done nil
2342 "Information to record when a task moves to the DONE state.
2344 Possible values are:
2346 nil Don't add anything, just change the keyword
2347 time Add a time stamp to the task
2348 note Prompt for a note and add it with template `org-log-note-headings'
2350 This option can also be set with on a per-file-basis with
2352 #+STARTUP: nologdone
2354 #+STARTUP: lognotedone
2356 You can have local logging settings for a subtree by setting the LOGGING
2357 property to one or more of these keywords."
2359 :group
'org-progress
2361 (const :tag
"No logging" nil
)
2362 (const :tag
"Record CLOSED timestamp" time
)
2363 (const :tag
"Record CLOSED timestamp with note." note
)))
2365 ;; Normalize old uses of org-log-done.
2367 ((eq org-log-done t
) (setq org-log-done
'time
))
2368 ((and (listp org-log-done
) (memq 'done org-log-done
))
2369 (setq org-log-done
'note
)))
2371 (defcustom org-log-reschedule nil
2372 "Information to record when the scheduling date of a tasks is modified.
2374 Possible values are:
2376 nil Don't add anything, just change the date
2377 time Add a time stamp to the task
2378 note Prompt for a note and add it with template `org-log-note-headings'
2380 This option can also be set with on a per-file-basis with
2382 #+STARTUP: nologreschedule
2383 #+STARTUP: logreschedule
2384 #+STARTUP: lognotereschedule"
2386 :group
'org-progress
2388 (const :tag
"No logging" nil
)
2389 (const :tag
"Record timestamp" time
)
2390 (const :tag
"Record timestamp with note." note
)))
2392 (defcustom org-log-redeadline nil
2393 "Information to record when the deadline date of a tasks is modified.
2395 Possible values are:
2397 nil Don't add anything, just change the date
2398 time Add a time stamp to the task
2399 note Prompt for a note and add it with template `org-log-note-headings'
2401 This option can also be set with on a per-file-basis with
2403 #+STARTUP: nologredeadline
2404 #+STARTUP: logredeadline
2405 #+STARTUP: lognoteredeadline
2407 You can have local logging settings for a subtree by setting the LOGGING
2408 property to one or more of these keywords."
2410 :group
'org-progress
2412 (const :tag
"No logging" nil
)
2413 (const :tag
"Record timestamp" time
)
2414 (const :tag
"Record timestamp with note." note
)))
2416 (defcustom org-log-note-clock-out nil
2417 "Non-nil means record a note when clocking out of an item.
2418 This can also be configured on a per-file basis by adding one of
2419 the following lines anywhere in the buffer:
2421 #+STARTUP: lognoteclock-out
2422 #+STARTUP: nolognoteclock-out"
2424 :group
'org-progress
2427 (defcustom org-log-done-with-time t
2428 "Non-nil means the CLOSED time stamp will contain date and time.
2429 When nil, only the date will be recorded."
2430 :group
'org-progress
2433 (defcustom org-log-note-headings
2434 '((done .
"CLOSING NOTE %t")
2435 (state .
"State %-12s from %-12S %t")
2436 (note .
"Note taken on %t")
2437 (reschedule .
"Rescheduled from %S on %t")
2438 (delschedule .
"Not scheduled, was %S on %t")
2439 (redeadline .
"New deadline from %S on %t")
2440 (deldeadline .
"Removed deadline, was %S on %t")
2441 (refile .
"Refiled on %t")
2443 "Headings for notes added to entries.
2444 The value is an alist, with the car being a symbol indicating the note
2445 context, and the cdr is the heading to be used. The heading may also be the
2447 %t in the heading will be replaced by a time stamp.
2448 %T will be an active time stamp instead the default inactive one
2449 %d will be replaced by a short-format time stamp.
2450 %D will be replaced by an active short-format time stamp.
2451 %s will be replaced by the new TODO state, in double quotes.
2452 %S will be replaced by the old TODO state, in double quotes.
2453 %u will be replaced by the user name.
2454 %U will be replaced by the full user name.
2456 In fact, it is not a good idea to change the `state' entry, because
2457 agenda log mode depends on the format of these entries."
2459 :group
'org-progress
2460 :type
'(list :greedy t
2461 (cons (const :tag
"Heading when closing an item" done
) string
)
2463 "Heading when changing todo state (todo sequence only)"
2465 (cons (const :tag
"Heading when just taking a note" note
) string
)
2466 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2467 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2468 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2469 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2470 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2471 (cons (const :tag
"Heading when refiling" refile
) string
)))
2473 (unless (assq 'note org-log-note-headings
)
2474 (push '(note .
"%t") org-log-note-headings
))
2476 (defcustom org-log-into-drawer nil
2477 "Non-nil means insert state change notes and time stamps into a drawer.
2478 When nil, state changes notes will be inserted after the headline and
2479 any scheduling and clock lines, but not inside a drawer.
2481 The value of this variable should be the name of the drawer to use.
2482 LOGBOOK is proposed as the default drawer for this purpose, you can
2483 also set this to a string to define the drawer of your choice.
2485 A value of t is also allowed, representing \"LOGBOOK\".
2487 If this variable is set, `org-log-state-notes-insert-after-drawers'
2490 You can set the property LOG_INTO_DRAWER to overrule this setting for
2493 :group
'org-progress
2495 (const :tag
"Not into a drawer" nil
)
2496 (const :tag
"LOGBOOK" t
)
2497 (string :tag
"Other")))
2499 (if (fboundp 'defvaralias
)
2500 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2502 (defun org-log-into-drawer ()
2503 "Return the value of `org-log-into-drawer', but let properties overrule.
2504 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2505 used instead of the default value."
2506 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
)))
2508 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2509 ((equal p
"t") "LOGBOOK")
2512 (defcustom org-log-state-notes-insert-after-drawers nil
2513 "Non-nil means insert state change notes after any drawers in entry.
2514 Only the drawers that *immediately* follow the headline and the
2515 deadline/scheduled line are skipped.
2516 When nil, insert notes right after the heading and perhaps the line
2517 with deadline/scheduling if present.
2519 This variable will have no effect if `org-log-into-drawer' is
2522 :group
'org-progress
2525 (defcustom org-log-states-order-reversed t
2526 "Non-nil means the latest state note will be directly after heading.
2527 When nil, the state change notes will be ordered according to time."
2529 :group
'org-progress
2532 (defcustom org-todo-repeat-to-state nil
2533 "The TODO state to which a repeater should return the repeating task.
2534 By default this is the first task in a TODO sequence, or the previous state
2535 in a TODO_TYP set. But you can specify another task here.
2536 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2539 :type
'(choice (const :tag
"Head of sequence" nil
)
2540 (string :tag
"Specific state")))
2542 (defcustom org-log-repeat
'time
2543 "Non-nil means record moving through the DONE state when triggering repeat.
2544 An auto-repeating task is immediately switched back to TODO when
2545 marked DONE. If you are not logging state changes (by adding \"@\"
2546 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2547 record a closing note, there will be no record of the task moving
2548 through DONE. This variable forces taking a note anyway.
2550 nil Don't force a record
2551 time Record a time stamp
2554 This option can also be set with on a per-file-basis with
2556 #+STARTUP: logrepeat
2557 #+STARTUP: lognoterepeat
2558 #+STARTUP: nologrepeat
2560 You can have local logging settings for a subtree by setting the LOGGING
2561 property to one or more of these keywords."
2563 :group
'org-progress
2565 (const :tag
"Don't force a record" nil
)
2566 (const :tag
"Force recording the DONE state" time
)
2567 (const :tag
"Force recording a note with the DONE state" note
)))
2570 (defgroup org-priorities nil
2571 "Priorities in Org-mode."
2572 :tag
"Org Priorities"
2575 (defcustom org-enable-priority-commands t
2576 "Non-nil means priority commands are active.
2577 When nil, these commands will be disabled, so that you never accidentally
2579 :group
'org-priorities
2582 (defcustom org-highest-priority ?A
2583 "The highest priority of TODO items. A character like ?A, ?B etc.
2584 Must have a smaller ASCII number than `org-lowest-priority'."
2585 :group
'org-priorities
2588 (defcustom org-lowest-priority ?C
2589 "The lowest priority of TODO items. A character like ?A, ?B etc.
2590 Must have a larger ASCII number than `org-highest-priority'."
2591 :group
'org-priorities
2594 (defcustom org-default-priority ?B
2595 "The default priority of TODO items.
2596 This is the priority an item gets if no explicit priority is given.
2597 When starting to cycle on an empty priority the first step in the cycle
2598 depends on `org-priority-start-cycle-with-default'. The resulting first
2599 step priority must not exceed the range from `org-highest-priority' to
2600 `org-lowest-priority' which means that `org-default-priority' has to be
2601 in this range exclusive or inclusive the range boundaries. Else the
2602 first step refuses to set the default and the second will fall back
2603 to (depending on the command used) the highest or lowest priority."
2604 :group
'org-priorities
2607 (defcustom org-priority-start-cycle-with-default t
2608 "Non-nil means start with default priority when starting to cycle.
2609 When this is nil, the first step in the cycle will be (depending on the
2610 command used) one higher or lower than the default priority.
2611 See also `org-default-priority'."
2612 :group
'org-priorities
2615 (defcustom org-get-priority-function nil
2616 "Function to extract the priority from a string.
2617 The string is normally the headline. If this is nil Org computes the
2618 priority from the priority cookie like [#A] in the headline. It returns
2619 an integer, increasing by 1000 for each priority level.
2620 The user can set a different function here, which should take a string
2621 as an argument and return the numeric priority."
2622 :group
'org-priorities
2626 (defgroup org-time nil
2627 "Options concerning time stamps and deadlines in Org-mode."
2631 (defcustom org-insert-labeled-timestamps-at-point nil
2632 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2633 When nil, these labeled time stamps are forces into the second line of an
2634 entry, just after the headline. When scheduling from the global TODO list,
2635 the time stamp will always be forced into the second line."
2639 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2640 "Formats for `format-time-string' which are used for time stamps.
2641 It is not recommended to change this constant.")
2643 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2644 "Number of minutes to round time stamps to.
2645 These are two values, the first applies when first creating a time stamp.
2646 The second applies when changing it with the commands `S-up' and `S-down'.
2647 When changing the time stamp, this means that it will change in steps
2648 of N minutes, as given by the second value.
2650 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2651 numbers should be factors of 60, so for example 5, 10, 15.
2653 When this is larger than 1, you can still force an exact time stamp by using
2654 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2655 and by using a prefix arg to `S-up/down' to specify the exact number
2656 of minutes to shift."
2658 :get
#'(lambda (var) ; Make sure both elements are there
2659 (if (integerp (default-value var
))
2660 (list (default-value var
) 5)
2661 (default-value var
)))
2663 (integer :tag
"when inserting times")
2664 (integer :tag
"when modifying times")))
2666 ;; Normalize old customizations of this variable.
2667 (when (integerp org-time-stamp-rounding-minutes
)
2668 (setq org-time-stamp-rounding-minutes
2669 (list org-time-stamp-rounding-minutes
2670 org-time-stamp-rounding-minutes
)))
2672 (defcustom org-display-custom-times nil
2673 "Non-nil means overlay custom formats over all time stamps.
2674 The formats are defined through the variable `org-time-stamp-custom-formats'.
2675 To turn this on on a per-file basis, insert anywhere in the file:
2676 #+STARTUP: customtime"
2680 (make-variable-buffer-local 'org-display-custom-times
)
2682 (defcustom org-time-stamp-custom-formats
2683 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2684 "Custom formats for time stamps. See `format-time-string' for the syntax.
2685 These are overlaid over the default ISO format if the variable
2686 `org-display-custom-times' is set. Time like %H:%M should be at the
2687 end of the second format. The custom formats are also honored by export
2688 commands, if custom time display is turned on at the time of export."
2692 (defun org-time-stamp-format (&optional long inactive
)
2693 "Get the right format for a time string."
2694 (let ((f (if long
(cdr org-time-stamp-formats
)
2695 (car org-time-stamp-formats
))))
2697 (concat "[" (substring f
1 -
1) "]")
2700 (defcustom org-time-clocksum-format
"%d:%02d"
2701 "The format string used when creating CLOCKSUM lines.
2702 This is also used when org-mode generates a time duration."
2706 (defcustom org-time-clocksum-use-fractional nil
2707 "If non-nil, \\[org-clock-display] uses fractional times.
2708 org-mode generates a time duration."
2712 (defcustom org-time-clocksum-fractional-format
"%.2f"
2713 "The format string used when creating CLOCKSUM lines, or when
2714 org-mode generates a time duration."
2718 (defcustom org-deadline-warning-days
14
2719 "No. of days before expiration during which a deadline becomes active.
2720 This variable governs the display in sparse trees and in the agenda.
2721 When 0 or negative, it means use this number (the absolute value of it)
2722 even if a deadline has a different individual lead time specified.
2724 Custom commands can set this variable in the options section."
2726 :group
'org-agenda-daily
/weekly
2729 (defcustom org-read-date-prefer-future t
2730 "Non-nil means assume future for incomplete date input from user.
2731 This affects the following situations:
2732 1. The user gives a month but not a year.
2733 For example, if it is April and you enter \"feb 2\", this will be read
2734 as Feb 2, *next* year. \"May 5\", however, will be this year.
2735 2. The user gives a day, but no month.
2736 For example, if today is the 15th, and you enter \"3\", Org-mode will
2737 read this as the third of *next* month. However, if you enter \"17\",
2738 it will be considered as *this* month.
2740 If you set this variable to the symbol `time', then also the following
2743 3. If the user gives a time, but no day. If the time is before now,
2744 to will be interpreted as tomorrow.
2746 Currently none of this works for ISO week specifications.
2748 When this option is nil, the current day, month and year will always be
2751 See also `org-agenda-jump-prefer-future'."
2754 (const :tag
"Never" nil
)
2755 (const :tag
"Check month and day" t
)
2756 (const :tag
"Check month, day, and time" time
)))
2758 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2759 "Should the agenda jump command prefer the future for incomplete dates?
2760 The default is to do the same as configured in `org-read-date-prefer-future'.
2761 But you can also set a deviating value here.
2762 This may t or nil, or the symbol `org-read-date-prefer-future'."
2767 (const :tag
"Use org-read-date-prefer-future"
2768 org-read-date-prefer-future
)
2769 (const :tag
"Never" nil
)
2770 (const :tag
"Always" t
)))
2772 (defcustom org-read-date-force-compatible-dates t
2773 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2775 Depending on the system Emacs is running on, certain dates cannot
2776 be represented with the type used internally to represent time.
2777 Dates between 1970-1-1 and 2038-1-1 can always be represented
2778 correctly. Some systems allow for earlier dates, some for later,
2779 some for both. One way to find out it to insert any date into an
2780 Org buffer, putting the cursor on the year and hitting S-up and
2781 S-down to test the range.
2783 When this variable is set to t, the date/time prompt will not let
2784 you specify dates outside the 1970-2037 range, so it is certain that
2785 these dates will work in whatever version of Emacs you are
2786 running, and also that you can move a file from one Emacs implementation
2787 to another. WHenever Org is forcing the year for you, it will display
2790 When this variable is nil, Org will check if the date is
2791 representable in the specific Emacs implementation you are using.
2792 If not, it will force a year, usually the current year, and beep
2793 to remind you. Currently this setting is not recommended because
2794 the likelihood that you will open your Org files in an Emacs that
2795 has limited date range is not negligible.
2797 A workaround for this problem is to use diary sexp dates for time
2798 stamps outside of this range."
2803 (defcustom org-read-date-display-live t
2804 "Non-nil means display current interpretation of date prompt live.
2805 This display will be in an overlay, in the minibuffer."
2809 (defcustom org-read-date-popup-calendar t
2810 "Non-nil means pop up a calendar when prompting for a date.
2811 In the calendar, the date can be selected with mouse-1. However, the
2812 minibuffer will also be active, and you can simply enter the date as well.
2813 When nil, only the minibuffer will be available."
2816 (if (fboundp 'defvaralias
)
2817 (defvaralias 'org-popup-calendar-for-date-prompt
2818 'org-read-date-popup-calendar
))
2820 (defcustom org-read-date-minibuffer-setup-hook nil
2821 "Hook to be used to set up keys for the date/time interface.
2822 Add key definitions to `minibuffer-local-map', which will be a temporary
2827 (defcustom org-extend-today-until
0
2828 "The hour when your day really ends. Must be an integer.
2829 This has influence for the following applications:
2830 - When switching the agenda to \"today\". It it is still earlier than
2831 the time given here, the day recognized as TODAY is actually yesterday.
2832 - When a date is read from the user and it is still before the time given
2833 here, the current date and time will be assumed to be yesterday, 23:59.
2834 Also, timestamps inserted in remember templates follow this rule.
2836 IMPORTANT: This is a feature whose implementation is and likely will
2837 remain incomplete. Really, it is only here because past midnight seems to
2838 be the favorite working time of John Wiegley :-)"
2842 (defcustom org-use-effective-time nil
2843 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2844 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2845 \"effective time\" of any timestamps between midnight and 8am will be
2846 23:59 of the previous day."
2851 (defcustom org-edit-timestamp-down-means-later nil
2852 "Non-nil means S-down will increase the time in a time stamp.
2853 When nil, S-up will increase."
2857 (defcustom org-calendar-follow-timestamp-change t
2858 "Non-nil means make the calendar window follow timestamp changes.
2859 When a timestamp is modified and the calendar window is visible, it will be
2860 moved to the new date."
2864 (defgroup org-tags nil
2865 "Options concerning tags in Org-mode."
2869 (defcustom org-tag-alist nil
2870 "List of tags allowed in Org-mode files.
2871 When this list is nil, Org-mode will base TAG input on what is already in the
2873 The value of this variable is an alist, the car of each entry must be a
2874 keyword as a string, the cdr may be a character that is used to select
2875 that tag through the fast-tag-selection interface.
2876 See the manual for details."
2880 (cons (string :tag
"Tag name")
2881 (character :tag
"Access char"))
2882 (list :tag
"Start radio group"
2884 (option (string :tag
"Group description")))
2885 (list :tag
"End radio group"
2887 (option (string :tag
"Group description")))
2888 (const :tag
"New line" (:newline
)))))
2890 (defcustom org-tag-persistent-alist nil
2891 "List of tags that will always appear in all Org-mode files.
2892 This is in addition to any in buffer settings or customizations
2894 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2895 The value of this variable is an alist, the car of each entry must be a
2896 keyword as a string, the cdr may be a character that is used to select
2897 that tag through the fast-tag-selection interface.
2898 See the manual for details.
2899 To disable these tags on a per-file basis, insert anywhere in the file:
2904 (cons (string :tag
"Tag name")
2905 (character :tag
"Access char"))
2906 (const :tag
"Start radio group" (:startgroup
))
2907 (const :tag
"End radio group" (:endgroup
))
2908 (const :tag
"New line" (:newline
)))))
2910 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2911 "If non-nil, always offer completion for all tags of all agenda files.
2912 Instead of customizing this variable directly, you might want to
2913 set it locally for capture buffers, because there no list of
2914 tags in that file can be created dynamically (there are none).
2916 (add-hook 'org-capture-mode-hook
2918 (set (make-local-variable
2919 'org-complete-tags-always-offer-all-agenda-tags)
2925 (defvar org-file-tags nil
2926 "List of tags that can be inherited by all entries in the file.
2927 The tags will be inherited if the variable `org-use-tag-inheritance'
2928 says they should be.
2929 This variable is populated from #+FILETAGS lines.")
2931 (defcustom org-use-fast-tag-selection
'auto
2932 "Non-nil means use fast tag selection scheme.
2933 This is a special interface to select and deselect tags with single keys.
2934 When nil, fast selection is never used.
2935 When the symbol `auto', fast selection is used if and only if selection
2936 characters for tags have been configured, either through the variable
2937 `org-tag-alist' or through a #+TAGS line in the buffer.
2938 When t, fast selection is always used and selection keys are assigned
2939 automatically if necessary."
2942 (const :tag
"Always" t
)
2943 (const :tag
"Never" nil
)
2944 (const :tag
"When selection characters are configured" 'auto
)))
2946 (defcustom org-fast-tag-selection-single-key nil
2947 "Non-nil means fast tag selection exits after first change.
2948 When nil, you have to press RET to exit it.
2949 During fast tag selection, you can toggle this flag with `C-c'.
2950 This variable can also have the value `expert'. In this case, the window
2951 displaying the tags menu is not even shown, until you press C-c again."
2954 (const :tag
"No" nil
)
2955 (const :tag
"Yes" t
)
2956 (const :tag
"Expert" expert
)))
2958 (defvar org-fast-tag-selection-include-todo nil
2959 "Non-nil means fast tags selection interface will also offer TODO states.
2960 This is an undocumented feature, you should not rely on it.")
2962 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2963 "The column to which tags should be indented in a headline.
2964 If this number is positive, it specifies the column. If it is negative,
2965 it means that the tags should be flushright to that column. For example,
2966 -80 works well for a normal 80 character screen.
2967 When 0, place tags directly after headline text, with only one space in
2972 (defcustom org-auto-align-tags t
2973 "Non-nil keeps tags aligned when modifying headlines.
2974 Some operations (i.e. demoting) change the length of a headline and
2975 therefore shift the tags around. With this option turned on, after
2976 each such operation the tags are again aligned to `org-tags-column'."
2980 (defcustom org-use-tag-inheritance t
2981 "Non-nil means tags in levels apply also for sublevels.
2982 When nil, only the tags directly given in a specific line apply there.
2983 This may also be a list of tags that should be inherited, or a regexp that
2984 matches tags that should be inherited. Additional control is possible
2985 with the variable `org-tags-exclude-from-inheritance' which gives an
2986 explicit list of tags to be excluded from inheritance., even if the value of
2987 `org-use-tag-inheritance' would select it for inheritance.
2989 If this option is t, a match early-on in a tree can lead to a large
2990 number of matches in the subtree when constructing the agenda or creating
2991 a sparse tree. If you only want to see the first match in a tree during
2992 a search, check out the variable `org-tags-match-list-sublevels'."
2995 (const :tag
"Not" nil
)
2996 (const :tag
"Always" t
)
2997 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2998 (regexp :tag
"Tags matched by regexp")))
3000 (defcustom org-tags-exclude-from-inheritance nil
3001 "List of tags that should never be inherited.
3002 This is a way to exclude a few tags from inheritance. For way to do
3003 the opposite, to actively allow inheritance for selected tags,
3004 see the variable `org-use-tag-inheritance'."
3006 :type
'(repeat (string :tag
"Tag")))
3008 (defun org-tag-inherit-p (tag)
3009 "Check if TAG is one that should be inherited."
3011 ((member tag org-tags-exclude-from-inheritance
) nil
)
3012 ((eq org-use-tag-inheritance t
) t
)
3013 ((not org-use-tag-inheritance
) nil
)
3014 ((stringp org-use-tag-inheritance
)
3015 (string-match org-use-tag-inheritance tag
))
3016 ((listp org-use-tag-inheritance
)
3017 (member tag org-use-tag-inheritance
))
3018 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3020 (defcustom org-tags-match-list-sublevels t
3021 "Non-nil means list also sublevels of headlines matching a search.
3022 This variable applies to tags/property searches, and also to stuck
3023 projects because this search is based on a tags match as well.
3025 When set to the symbol `indented', sublevels are indented with
3028 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3029 the sublevels of a headline matching a tag search often also match
3030 the same search. Listing all of them can create very long lists.
3031 Setting this variable to nil causes subtrees of a match to be skipped.
3033 This variable is semi-obsolete and probably should always be true. It
3034 is better to limit inheritance to certain tags using the variables
3035 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3038 (const :tag
"No, don't list them" nil
)
3039 (const :tag
"Yes, do list them" t
)
3040 (const :tag
"List them, indented with leading dots" indented
)))
3042 (defcustom org-tags-sort-function nil
3043 "When set, tags are sorted using this function as a comparator."
3046 (const :tag
"No sorting" nil
)
3047 (const :tag
"Alphabetical" string
<)
3048 (const :tag
"Reverse alphabetical" string
>)
3049 (function :tag
"Custom function" nil
)))
3051 (defvar org-tags-history nil
3052 "History of minibuffer reads for tags.")
3053 (defvar org-last-tags-completion-table nil
3054 "The last used completion table for tags.")
3055 (defvar org-after-tags-change-hook nil
3056 "Hook that is run after the tags in a line have changed.")
3058 (defgroup org-properties nil
3059 "Options concerning properties in Org-mode."
3060 :tag
"Org Properties"
3063 (defcustom org-property-format
"%-10s %s"
3064 "How property key/value pairs should be formatted by `indent-line'.
3065 When `indent-line' hits a property definition, it will format the line
3066 according to this format, mainly to make sure that the values are
3067 lined-up with respect to each other."
3068 :group
'org-properties
3071 (defcustom org-properties-postprocess-alist nil
3072 "Alist of properties and functions to adjust inserted values.
3073 Elements of this alist must be of the form
3075 ([string] [function])
3077 where [string] must be a property name and [function] must be a
3078 lambda expression: this lambda expression must take one argument,
3079 the value to adjust, and return the new value as a string.
3081 For example, this element will allow the property \"Remaining\"
3082 to be updated wrt the relation between the \"Effort\" property
3083 and the clock summary:
3085 ((\"Remaining\" (lambda(value)
3086 (let ((clocksum (org-clock-sum-current-item))
3087 (effort (org-duration-string-to-minutes
3088 (org-entry-get (point) \"Effort\"))))
3089 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3090 :group
'org-properties
3094 (defcustom org-use-property-inheritance nil
3095 "Non-nil means properties apply also for sublevels.
3097 This setting is chiefly used during property searches. Turning it on can
3098 cause significant overhead when doing a search, which is why it is not
3101 When nil, only the properties directly given in the current entry count.
3102 When t, every property is inherited. The value may also be a list of
3103 properties that should have inheritance, or a regular expression matching
3104 properties that should be inherited.
3106 However, note that some special properties use inheritance under special
3107 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3108 and the properties ending in \"_ALL\" when they are used as descriptor
3109 for valid values of a property.
3111 Note for programmers:
3112 When querying an entry with `org-entry-get', you can control if inheritance
3113 should be used. By default, `org-entry-get' looks only at the local
3114 properties. You can request inheritance by setting the inherit argument
3115 to t (to force inheritance) or to `selective' (to respect the setting
3117 :group
'org-properties
3119 (const :tag
"Not" nil
)
3120 (const :tag
"Always" t
)
3121 (repeat :tag
"Specific properties" (string :tag
"Property"))
3122 (regexp :tag
"Properties matched by regexp")))
3124 (defun org-property-inherit-p (property)
3125 "Check if PROPERTY is one that should be inherited."
3127 ((eq org-use-property-inheritance t
) t
)
3128 ((not org-use-property-inheritance
) nil
)
3129 ((stringp org-use-property-inheritance
)
3130 (string-match org-use-property-inheritance property
))
3131 ((listp org-use-property-inheritance
)
3132 (member property org-use-property-inheritance
))
3133 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3135 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3136 "The default column format, if no other format has been defined.
3137 This variable can be set on the per-file basis by inserting a line
3139 #+COLUMNS: %25ITEM ....."
3140 :group
'org-properties
3143 (defcustom org-columns-ellipses
".."
3144 "The ellipses to be used when a field in column view is truncated.
3145 When this is the empty string, as many characters as possible are shown,
3146 but then there will be no visual indication that the field has been truncated.
3147 When this is a string of length N, the last N characters of a truncated
3148 field are replaced by this string. If the column is narrower than the
3149 ellipses string, only part of the ellipses string will be shown."
3150 :group
'org-properties
3153 (defcustom org-columns-modify-value-for-display-function nil
3154 "Function that modifies values for display in column view.
3155 For example, it can be used to cut out a certain part from a time stamp.
3156 The function must take 2 arguments:
3158 column-title The title of the column (*not* the property name)
3159 value The value that should be modified.
3161 The function should return the value that should be displayed,
3162 or nil if the normal value should be used."
3163 :group
'org-properties
3166 (defcustom org-effort-property
"Effort"
3167 "The property that is being used to keep track of effort estimates.
3168 Effort estimates given in this property need to have the format H:MM."
3169 :group
'org-properties
3170 :group
'org-progress
3171 :type
'(string :tag
"Property"))
3173 (defconst org-global-properties-fixed
3174 '(("VISIBILITY_ALL" .
"folded children content all")
3175 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3176 "List of property/value pairs that can be inherited by any entry.
3178 These are fixed values, for the preset properties. The user variable
3179 that can be used to add to this list is `org-global-properties'.
3181 The entries in this list are cons cells where the car is a property
3182 name and cdr is a string with the value. If the value represents
3183 multiple items like an \"_ALL\" property, separate the items by
3186 (defcustom org-global-properties nil
3187 "List of property/value pairs that can be inherited by any entry.
3189 This list will be combined with the constant `org-global-properties-fixed'.
3191 The entries in this list are cons cells where the car is a property
3192 name and cdr is a string with the value.
3194 You can set buffer-local values for the same purpose in the variable
3195 `org-file-properties' this by adding lines like
3197 #+PROPERTY: NAME VALUE"
3198 :group
'org-properties
3200 (cons (string :tag
"Property")
3201 (string :tag
"Value"))))
3203 (defvar org-file-properties nil
3204 "List of property/value pairs that can be inherited by any entry.
3205 Valid for the current buffer.
3206 This variable is populated from #+PROPERTY lines.")
3207 (make-variable-buffer-local 'org-file-properties
)
3209 (defgroup org-agenda nil
3210 "Options concerning agenda views in Org-mode."
3214 (defvar org-category nil
3215 "Variable used by org files to set a category for agenda display.
3216 Such files should use a file variable to set it, for example
3218 # -*- mode: org; org-category: \"ELisp\"
3220 or contain a special line
3224 If the file does not specify a category, then file's base name
3226 (make-variable-buffer-local 'org-category
)
3227 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3229 (defcustom org-agenda-files nil
3230 "The files to be used for agenda display.
3231 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3232 \\[org-remove-file]. You can also use customize to edit the list.
3234 If an entry is a directory, all files in that directory that are matched by
3235 `org-agenda-file-regexp' will be part of the file list.
3237 If the value of the variable is not a list but a single file name, then
3238 the list of agenda files is actually stored and maintained in that file, one
3239 agenda file per line. In this file paths can be given relative to
3240 `org-directory'. Tilde expansion and environment variable substitution
3244 (repeat :tag
"List of files and directories" file
)
3245 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3247 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3248 "Regular expression to match files for `org-agenda-files'.
3249 If any element in the list in that variable contains a directory instead
3250 of a normal file, all files in that directory that are matched by this
3251 regular expression will be included."
3255 (defcustom org-agenda-text-search-extra-files nil
3256 "List of extra files to be searched by text search commands.
3257 These files will be search in addition to the agenda files by the
3258 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3259 Note that these files will only be searched for text search commands,
3260 not for the other agenda views like todo lists, tag searches or the weekly
3261 agenda. This variable is intended to list notes and possibly archive files
3262 that should also be searched by these two commands.
3263 In fact, if the first element in the list is the symbol `agenda-archives',
3264 than all archive files of all agenda files will be added to the search
3267 :type
'(set :greedy t
3268 (const :tag
"Agenda Archives" agenda-archives
)
3269 (repeat :inline t
(file))))
3271 (if (fboundp 'defvaralias
)
3272 (defvaralias 'org-agenda-multi-occur-extra-files
3273 'org-agenda-text-search-extra-files
))
3275 (defcustom org-agenda-skip-unavailable-files nil
3276 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3277 A nil value means to remove them, after a query, from the list."
3281 (defcustom org-calendar-to-agenda-key
[?c
]
3282 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3283 The command `org-calendar-goto-agenda' will be bound to this key. The
3284 default is the character `c' because then `c' can be used to switch back and
3285 forth between agenda and calendar."
3289 (defcustom org-calendar-agenda-action-key
[?k
]
3290 "The key to be installed in `calendar-mode-map' for agenda-action.
3291 The command `org-agenda-action' will be bound to this key. The
3292 default is the character `k' because we use the same key in the agenda."
3296 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3297 "The key to be installed in `calendar-mode-map' for adding diary entries.
3298 This option is irrelevant until `org-agenda-diary-file' has been configured
3299 to point to an Org-mode file. When that is the case, the command
3300 `org-agenda-diary-entry' will be bound to the key given here, by default
3301 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3302 if you want to continue doing this, you need to change this to a different
3307 (defcustom org-agenda-diary-file
'diary-file
3308 "File to which to add new entries with the `i' key in agenda and calendar.
3309 When this is the symbol `diary-file', the functionality in the Emacs
3310 calendar will be used to add entries to the `diary-file'. But when this
3311 points to a file, `org-agenda-diary-entry' will be used instead."
3314 (const :tag
"The standard Emacs diary file" diary-file
)
3315 (file :tag
"Special Org file diary entries")))
3317 (eval-after-load "calendar"
3319 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3320 'org-calendar-goto-agenda
)
3321 (org-defkey calendar-mode-map org-calendar-agenda-action-key
3323 (add-hook 'calendar-mode-hook
3325 (unless (eq org-agenda-diary-file
'diary-file
)
3326 (define-key calendar-mode-map
3327 org-calendar-insert-diary-entry-key
3328 'org-agenda-diary-entry
))))))
3330 (defgroup org-latex nil
3331 "Options for embedding LaTeX code into Org-mode."
3335 (defcustom org-format-latex-options
3336 '(:foreground default
:background default
:scale
1.0
3337 :html-foreground
"Black" :html-background
"Transparent"
3338 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3339 "Options for creating images from LaTeX fragments.
3340 This is a property list with the following properties:
3341 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3342 `default' means use the foreground of the default face.
3343 :background the background color, or \"Transparent\".
3344 `default' means use the background of the default face.
3345 :scale a scaling factor for the size of the images, to get more pixels
3346 :html-foreground, :html-background, :html-scale
3347 the same numbers for HTML export.
3348 :matchers a list indicating which matchers should be used to
3349 find LaTeX fragments. Valid members of this list are:
3350 \"begin\" find environments
3351 \"$1\" find single characters surrounded by $.$
3352 \"$\" find math expressions surrounded by $...$
3353 \"$$\" find math expressions surrounded by $$....$$
3354 \"\\(\" find math expressions surrounded by \\(...\\)
3355 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3359 (defcustom org-format-latex-signal-error t
3360 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3361 When nil, just push out a message."
3366 (defcustom org-latex-to-mathml-jar-file nil
3367 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3368 Use this to specify additional executable file say a jar file.
3370 When using MathToWeb as the converter, specify the full-path to
3371 your mathtoweb.jar file."
3375 (const :tag
"None" nil
)
3376 (file :tag
"JAR file" :must-match t
)))
3378 (defcustom org-latex-to-mathml-convert-command nil
3379 "Command to convert LaTeX fragments to MathML.
3380 Replace format-specifiers in the command as noted below and use
3381 `shell-command' to convert LaTeX to MathML.
3382 %j: Executable file in fully expanded form as specified by
3383 `org-latex-to-mathml-jar-file'.
3384 %I: Input LaTeX file in fully expanded form
3385 %o: Output MathML file
3386 This command is used by `org-create-math-formula'.
3388 When using MathToWeb as the converter, set this to
3389 \"java -jar %j -unicode -force -df %o %I\"."
3393 (const :tag
"None" nil
)
3394 (string :tag
"\nShell command")))
3396 (defun org-format-latex-mathml-available-p ()
3397 "Return t if `org-latex-to-mathml-convert-command' is usable."
3399 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3400 org-latex-to-mathml-convert-command
)
3401 (let ((executable (car (split-string
3402 org-latex-to-mathml-convert-command
))))
3403 (when (executable-find executable
)
3405 "%j" org-latex-to-mathml-convert-command
)
3406 (file-readable-p org-latex-to-mathml-jar-file
)
3409 (defcustom org-format-latex-header
"\\documentclass{article}
3410 \\usepackage[usenames]{color}
3411 \\usepackage{amsmath}
3412 \\usepackage[mathscr]{eucal}
3413 \\pagestyle{empty} % do not remove
3416 % The settings below are copied from fullpage.sty
3417 \\setlength{\\textwidth}{\\paperwidth}
3418 \\addtolength{\\textwidth}{-3cm}
3419 \\setlength{\\oddsidemargin}{1.5cm}
3420 \\addtolength{\\oddsidemargin}{-2.54cm}
3421 \\setlength{\\evensidemargin}{\\oddsidemargin}
3422 \\setlength{\\textheight}{\\paperheight}
3423 \\addtolength{\\textheight}{-\\headheight}
3424 \\addtolength{\\textheight}{-\\headsep}
3425 \\addtolength{\\textheight}{-\\footskip}
3426 \\addtolength{\\textheight}{-3cm}
3427 \\setlength{\\topmargin}{1.5cm}
3428 \\addtolength{\\topmargin}{-2.54cm}"
3429 "The document header used for processing LaTeX fragments.
3430 It is imperative that this header make sure that no page number
3431 appears on the page. The package defined in the variables
3432 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3433 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3438 (defvar org-format-latex-header-extra nil
)
3440 (defun org-set-packages-alist (var val
)
3441 "Set the packages alist and make sure it has 3 elements per entry."
3442 (set var
(mapcar (lambda (x)
3443 (if (and (consp x
) (= (length x
) 2))
3444 (list (car x
) (nth 1 x
) t
)
3448 (defun org-get-packages-alist (var)
3450 "Get the packages alist and make sure it has 3 elements per entry."
3452 (if (and (consp x
) (= (length x
) 2))
3453 (list (car x
) (nth 1 x
) t
)
3455 (default-value var
)))
3457 ;; The following variables are defined here because is it also used
3458 ;; when formatting latex fragments. Originally it was part of the
3459 ;; LaTeX exporter, which is why the name includes "export".
3460 (defcustom org-export-latex-default-packages-alist
3461 '(("AUTO" "inputenc" t
)
3465 ("" "longtable" nil
)
3477 "Alist of default packages to be inserted in the header.
3478 Change this only if one of the packages here causes an incompatibility
3479 with another package you are using.
3480 The packages in this list are needed by one part or another of Org-mode
3481 to function properly.
3483 - inputenc, fontenc: for basic font and character selection
3484 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3485 for interpreting the entities in `org-entities'. You can skip some of these
3486 packages if you don't use any of the symbols in it.
3487 - graphicx: for including images
3488 - float, wrapfig: for figure placement
3489 - longtable: for long tables
3490 - hyperref: for cross references
3492 Therefore you should not modify this variable unless you know what you
3493 are doing. The one reason to change it anyway is that you might be loading
3494 some other package that conflicts with one of the default packages.
3495 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3496 If SNIPPET-FLAG is t, the package also needs to be included when
3497 compiling LaTeX snippets into images for inclusion into HTML."
3498 :group
'org-export-latex
3499 :set
'org-set-packages-alist
3500 :get
'org-get-packages-alist
3504 (list :tag
"options/package pair"
3505 (string :tag
"options")
3506 (string :tag
"package")
3507 (boolean :tag
"Snippet"))
3508 (string :tag
"A line of LaTeX"))))
3510 (defcustom org-export-latex-packages-alist nil
3511 "Alist of packages to be inserted in every LaTeX header.
3512 These will be inserted after `org-export-latex-default-packages-alist'.
3513 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3514 SNIPPET-FLAG, when t, indicates that this package is also needed when
3515 turning LaTeX snippets into images for inclusion into HTML.
3516 Make sure that you only list packages here which:
3517 - you want in every file
3518 - do not conflict with the default packages in
3519 `org-export-latex-default-packages-alist'
3520 - do not conflict with the setup in `org-format-latex-header'."
3521 :group
'org-export-latex
3522 :set
'org-set-packages-alist
3523 :get
'org-get-packages-alist
3526 (list :tag
"options/package pair"
3527 (string :tag
"options")
3528 (string :tag
"package")
3529 (boolean :tag
"Snippet"))
3530 (string :tag
"A line of LaTeX"))))
3533 (defgroup org-appearance nil
3534 "Settings for Org-mode appearance."
3535 :tag
"Org Appearance"
3538 (defcustom org-level-color-stars-only nil
3539 "Non-nil means fontify only the stars in each headline.
3540 When nil, the entire headline is fontified.
3541 Changing it requires restart of `font-lock-mode' to become effective
3542 also in regions already fontified."
3543 :group
'org-appearance
3546 (defcustom org-hide-leading-stars nil
3547 "Non-nil means hide the first N-1 stars in a headline.
3548 This works by using the face `org-hide' for these stars. This
3549 face is white for a light background, and black for a dark
3550 background. You may have to customize the face `org-hide' to
3552 Changing it requires restart of `font-lock-mode' to become effective
3553 also in regions already fontified.
3554 You may also set this on a per-file basis by adding one of the following
3555 lines to the buffer:
3557 #+STARTUP: hidestars
3558 #+STARTUP: showstars"
3559 :group
'org-appearance
3562 (defcustom org-hidden-keywords nil
3563 "List of symbols corresponding to keywords to be hidden the org buffer.
3564 For example, a value '(title) for this list will make the document's title
3565 appear in the buffer without the initial #+TITLE: keyword."
3566 :group
'org-appearance
3568 :type
'(set (const :tag
"#+AUTHOR" author
)
3569 (const :tag
"#+DATE" date
)
3570 (const :tag
"#+EMAIL" email
)
3571 (const :tag
"#+TITLE" title
)))
3573 (defcustom org-fontify-done-headline nil
3574 "Non-nil means change the face of a headline if it is marked DONE.
3575 Normally, only the TODO/DONE keyword indicates the state of a headline.
3576 When this is non-nil, the headline after the keyword is set to the
3577 `org-headline-done' as an additional indication."
3578 :group
'org-appearance
3581 (defcustom org-fontify-emphasized-text t
3582 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3583 Changing this variable requires a restart of Emacs to take effect."
3584 :group
'org-appearance
3587 (defcustom org-fontify-whole-heading-line nil
3588 "Non-nil means fontify the whole line for headings.
3589 This is useful when setting a background color for the
3591 :group
'org-appearance
3594 (defcustom org-highlight-latex-fragments-and-specials nil
3595 "Non-nil means fontify what is treated specially by the exporters."
3596 :group
'org-appearance
3599 (defcustom org-hide-emphasis-markers nil
3600 "Non-nil mean font-lock should hide the emphasis marker characters."
3601 :group
'org-appearance
3604 (defcustom org-pretty-entities nil
3605 "Non-nil means show entities as UTF8 characters.
3606 When nil, the \\name form remains in the buffer."
3607 :group
'org-appearance
3611 (defcustom org-pretty-entities-include-sub-superscripts t
3612 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3613 :group
'org-appearance
3617 (defvar org-emph-re nil
3618 "Regular expression for matching emphasis.
3619 After a match, the match groups contain these elements:
3620 0 The match of the full regular expression, including the characters
3621 before and after the proper match
3622 1 The character before the proper match, or empty at beginning of line
3623 2 The proper match, including the leading and trailing markers
3624 3 The leading marker like * or /, indicating the type of highlighting
3625 4 The text between the emphasis markers, not including the markers
3626 5 The character after the match, empty at the end of a line")
3627 (defvar org-verbatim-re nil
3628 "Regular expression for matching verbatim text.")
3629 (defvar org-emphasis-regexp-components
) ; defined just below
3630 (defvar org-emphasis-alist
) ; defined just below
3631 (defun org-set-emph-re (var val
)
3632 "Set variable and compute the emphasis regular expression."
3634 (when (and (boundp 'org-emphasis-alist
)
3635 (boundp 'org-emphasis-regexp-components
)
3636 org-emphasis-alist org-emphasis-regexp-components
)
3637 (let* ((e org-emphasis-regexp-components
)
3643 (body1 (concat body
"*?"))
3644 (markers (mapconcat 'car org-emphasis-alist
""))
3645 (vmarkers (mapconcat
3646 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3647 org-emphasis-alist
"")))
3648 ;; make sure special characters appear at the right position in the class
3649 (if (string-match "\\^" markers
)
3650 (setq markers
(concat (replace-match "" t t markers
) "^")))
3651 (if (string-match "-" markers
)
3652 (setq markers
(concat (replace-match "" t t markers
) "-")))
3653 (if (string-match "\\^" vmarkers
)
3654 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3655 (if (string-match "-" vmarkers
)
3656 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3658 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3659 (int-to-string nl
) "\\}")))
3662 (concat "\\([" pre
"]\\|^\\)"
3664 "\\([" markers
"]\\)"
3672 "\\([" post
"]\\|$\\)"))
3673 (setq org-verbatim-re
3674 (concat "\\([" pre
"]\\|^\\)"
3676 "\\([" vmarkers
"]\\)"
3684 "\\([" post
"]\\|$\\)")))))
3686 (defcustom org-emphasis-regexp-components
3687 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3688 "Components used to build the regular expression for emphasis.
3689 This is a list with five entries. Terminology: In an emphasis string
3690 like \" *strong word* \", we call the initial space PREMATCH, the final
3691 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3692 and \"trong wor\" is the body. The different components in this variable
3693 specify what is allowed/forbidden in each part:
3695 pre Chars allowed as prematch. Beginning of line will be allowed too.
3696 post Chars allowed as postmatch. End of line will be allowed too.
3697 border The chars *forbidden* as border characters.
3698 body-regexp A regexp like \".\" to match a body character. Don't use
3699 non-shy groups here, and don't allow newline here.
3700 newline The maximum number of newlines allowed in an emphasis exp.
3702 Use customize to modify this, or restart Emacs after changing it."
3703 :group
'org-appearance
3704 :set
'org-set-emph-re
3706 (sexp :tag
"Allowed chars in pre ")
3707 (sexp :tag
"Allowed chars in post ")
3708 (sexp :tag
"Forbidden chars in border ")
3709 (sexp :tag
"Regexp for body ")
3710 (integer :tag
"number of newlines allowed")
3711 (option (boolean :tag
"Please ignore this button"))))
3713 (defcustom org-emphasis-alist
3714 `(("*" bold
"<b>" "</b>")
3715 ("/" italic
"<i>" "</i>")
3716 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3717 ("=" org-code
"<code>" "</code>" verbatim
)
3718 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3719 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3722 "Special syntax for emphasized text.
3723 Text starting and ending with a special character will be emphasized, for
3724 example *bold*, _underlined_ and /italic/. This variable sets the marker
3725 characters, the face to be used by font-lock for highlighting in Org-mode
3726 Emacs buffers, and the HTML tags to be used for this.
3727 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3728 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3729 Use customize to modify this, or restart Emacs after changing it."
3730 :group
'org-appearance
3731 :set
'org-set-emph-re
3734 (string :tag
"Marker character")
3736 (face :tag
"Font-lock-face")
3737 (plist :tag
"Face property list"))
3738 (string :tag
"HTML start tag")
3739 (string :tag
"HTML end tag")
3740 (option (const verbatim
)))))
3742 (defvar org-protecting-blocks
3743 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3744 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3745 This is needed for font-lock setup.")
3747 ;;; Miscellaneous options
3749 (defgroup org-completion nil
3750 "Completion in Org-mode."
3751 :tag
"Org Completion"
3754 (defcustom org-completion-use-ido nil
3755 "Non-nil means use ido completion wherever possible.
3756 Note that `ido-mode' must be active for this variable to be relevant.
3757 If you decide to turn this variable on, you might well want to turn off
3758 `org-outline-path-complete-in-steps'.
3759 See also `org-completion-use-iswitchb'."
3760 :group
'org-completion
3763 (defcustom org-completion-use-iswitchb nil
3764 "Non-nil means use iswitchb completion wherever possible.
3765 Note that `iswitchb-mode' must be active for this variable to be relevant.
3766 If you decide to turn this variable on, you might well want to turn off
3767 `org-outline-path-complete-in-steps'.
3768 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3769 :group
'org-completion
3772 (defcustom org-completion-fallback-command
'hippie-expand
3773 "The expansion command called by \\[pcomplete] in normal context.
3774 Normal means, no org-mode-specific context."
3775 :group
'org-completion
3778 ;;; Functions and variables from their packages
3779 ;; Declared here to avoid compiler warnings
3782 (defvar outline-mode-menu-heading
)
3783 (defvar outline-mode-menu-show
)
3784 (defvar outline-mode-menu-hide
)
3785 (defvar zmacs-regions
) ; XEmacs regions
3788 (defvar mark-active
)
3791 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3792 (declare-function calendar-forward-day
"cal-move" (arg))
3793 (declare-function calendar-goto-date
"cal-move" (date))
3794 (declare-function calendar-goto-today
"cal-move" ())
3795 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3796 (defvar calc-embedded-close-formula
)
3797 (defvar calc-embedded-open-formula
)
3798 (declare-function cdlatex-tab
"ext:cdlatex" ())
3799 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3800 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3801 (defvar font-lock-unfontify-region-function
)
3802 (declare-function iswitchb-read-buffer
"iswitchb"
3803 (prompt &optional default require-match start matches-set
))
3804 (defvar iswitchb-temp-buflist
)
3805 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3806 (defvar org-agenda-tags-todo-honor-ignore-options
)
3807 (declare-function org-agenda-skip
"org-agenda" ())
3809 org-agenda-format-item
"org-agenda"
3810 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3811 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3812 (declare-function org-agenda-change-all-lines
"org-agenda"
3813 (newhead hdmarker
&optional fixface just-this
))
3814 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3815 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3816 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3818 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3819 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3820 "org-agenda" (&optional end
))
3821 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3822 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3823 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3824 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3825 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3826 (declare-function parse-time-string
"parse-time" (string))
3827 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3828 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3829 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3830 (defvar remember-data-file
)
3831 (defvar texmathp-why
)
3832 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3833 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3835 (defvar w3m-current-url
)
3836 (defvar w3m-current-title
)
3838 (defvar org-latex-regexps
)
3840 ;;; Autoload and prepare some org modules
3842 ;; Some table stuff that needs to be defined here, because it is used
3843 ;; by the functions setting up org-mode or checking for table context.
3845 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3846 "Detect an org-type or table-type table.")
3847 (defconst org-table-line-regexp
"^[ \t]*|"
3848 "Detect an org-type table line.")
3849 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3850 "Detect an org-type table line.")
3851 (defconst org-table-hline-regexp
"^[ \t]*|-"
3852 "Detect an org-type table hline.")
3853 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3854 "Detect a table-type table hline.")
3855 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3856 "Detect the first line outside a table when searching from within it.
3857 This works for both table types.")
3859 ;; Autoload the functions in org-table.el that are needed by functions here.
3862 (org-autoload "org-table"
3863 '(org-table-align org-table-begin org-table-blank-field
3864 org-table-convert org-table-convert-region org-table-copy-down
3865 org-table-copy-region org-table-create
3866 org-table-create-or-convert-from-region
3867 org-table-create-with-table.el org-table-current-dline
3868 org-table-cut-region org-table-delete-column org-table-edit-field
3869 org-table-edit-formulas org-table-end org-table-eval-formula
3870 org-table-export org-table-field-info
3871 org-table-get-stored-formulas org-table-goto-column
3872 org-table-hline-and-move org-table-import org-table-insert-column
3873 org-table-insert-hline org-table-insert-row org-table-iterate
3874 org-table-justify-field-maybe org-table-kill-row
3875 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3876 org-table-move-column org-table-move-column-left
3877 org-table-move-column-right org-table-move-row
3878 org-table-move-row-down org-table-move-row-up
3879 org-table-next-field org-table-next-row org-table-paste-rectangle
3880 org-table-previous-field org-table-recalculate
3881 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3882 org-table-toggle-coordinate-overlays
3883 org-table-toggle-formula-debugger org-table-wrap-region
3884 orgtbl-mode turn-on-orgtbl org-table-to-lisp
3885 orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
3886 orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo
)))
3888 (defun org-at-table-p (&optional table-type
)
3889 "Return t if the cursor is inside an org-type table.
3890 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3891 (if org-enable-table-editor
3893 (beginning-of-line 1)
3894 (looking-at (if table-type org-table-any-line-regexp
3895 org-table-line-regexp
)))
3897 (defsubst org-table-p
() (org-at-table-p))
3899 (defun org-at-table.el-p
()
3900 "Return t if and only if we are at a table.el table."
3901 (and (org-at-table-p 'any
)
3903 (goto-char (org-table-begin 'any
))
3904 (looking-at org-table1-hline-regexp
))))
3905 (defun org-table-recognize-table.el
()
3906 "If there is a table.el table nearby, recognize it and move into it."
3907 (if org-table-tab-recognizes-table.el
3908 (if (org-at-table.el-p
)
3910 (beginning-of-line 1)
3911 (if (looking-at org-table-dataline-regexp
)
3913 (if (looking-at org-table1-hline-regexp
)
3915 (beginning-of-line 2)
3916 (if (looking-at org-table-any-border-regexp
)
3917 (beginning-of-line -
1)))))
3918 (if (re-search-forward "|" (org-table-end t
) t
)
3921 (if (table--at-cell-p (point))
3923 (message "recognizing table.el table...")
3924 (table-recognize-table)
3925 (message "recognizing table.el table...done")))
3926 (error "This should not happen"))
3931 (defun org-at-table-hline-p ()
3932 "Return t if the cursor is inside a hline in a table."
3933 (if org-enable-table-editor
3935 (beginning-of-line 1)
3936 (looking-at org-table-hline-regexp
))
3939 (defvar org-table-clean-did-remove-column nil
)
3941 (defun org-table-map-tables (function &optional quietly
)
3942 "Apply FUNCTION to the start of all tables in the buffer."
3946 (goto-char (point-min))
3947 (while (re-search-forward org-table-any-line-regexp nil t
)
3949 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3950 (beginning-of-line 1)
3951 (when (looking-at org-table-line-regexp
)
3952 (save-excursion (funcall function
))
3953 (or (looking-at org-table-line-regexp
)
3955 (re-search-forward org-table-any-border-regexp nil
1))))
3956 (unless quietly
(message "Mapping tables: done")))
3958 ;; Declare and autoload functions from org-exp.el & Co
3960 (declare-function org-default-export-plist
"org-exp")
3961 (declare-function org-infile-export-plist
"org-exp")
3962 (declare-function org-get-current-options
"org-exp")
3964 (org-autoload "org-exp"
3965 '(org-export org-export-visible
3966 org-insert-export-options-template
3967 org-table-clean-before-export
))
3968 (org-autoload "org-ascii"
3969 '(org-export-as-ascii org-export-ascii-preprocess
3970 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3971 org-export-region-as-ascii
))
3972 (org-autoload "org-latex"
3973 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3974 org-replace-region-by-latex org-export-region-as-latex
3975 org-export-as-latex org-export-as-pdf
3976 org-export-as-pdf-and-open
))
3977 (org-autoload "org-html"
3978 '(org-export-as-html-and-open
3979 org-export-as-html-batch org-export-as-html-to-buffer
3980 org-replace-region-by-html org-export-region-as-html
3981 org-export-as-html
))
3982 (org-autoload "org-docbook"
3983 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
3984 org-replace-region-by-docbook org-export-region-as-docbook
3985 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
3986 org-export-as-docbook
))
3987 (org-autoload "org-icalendar"
3988 '(org-export-icalendar-this-file
3989 org-export-icalendar-all-agenda-files
3990 org-export-icalendar-combine-agenda-files
))
3991 (org-autoload "org-xoxo" '(org-export-as-xoxo))
3992 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning
)))
3994 ;; Declare and autoload functions from org-agenda.el
3997 (org-autoload "org-agenda"
3998 '(org-agenda org-agenda-list org-search-view
3999 org-todo-list org-tags-view org-agenda-list-stuck-projects
4000 org-diary org-agenda-to-appt
4001 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
4003 ;; Autoload org-remember
4006 (org-autoload "org-remember"
4007 '(org-remember-insinuate org-remember-annotation
4008 org-remember-apply-template org-remember org-remember-handler
)))
4011 (org-autoload "org-capture"
4012 '(org-capture org-capture-insert-template-here
4013 org-capture-import-remember-templates
)))
4015 ;; Autoload org-clock.el
4017 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
4019 (declare-function org-clock-update-mode-line
"org-clock" ())
4020 (declare-function org-resolve-clocks
"org-clock"
4021 (&optional also-non-dangling-p prompt last-valid
))
4022 (defvar org-clock-start-time
)
4023 (defvar org-clock-marker
(make-marker)
4024 "Marker recording the last clock-in.")
4025 (defvar org-clock-hd-marker
(make-marker)
4026 "Marker recording the last clock-in, but the headline position.")
4027 (defvar org-clock-heading
""
4028 "The heading of the current clock entry.")
4029 (defun org-clock-is-active ()
4030 "Return non-nil if clock is currently running.
4031 The return value is actually the clock marker."
4032 (marker-buffer org-clock-marker
))
4037 '(org-clock-in org-clock-out org-clock-cancel
4038 org-clock-goto org-clock-sum org-clock-display
4039 org-clock-remove-overlays org-clock-report
4040 org-clocktable-shift org-dblock-write
:clocktable
4041 org-get-clocktable org-resolve-clocks
)))
4043 (defun org-clock-update-time-maybe ()
4044 "If this is a CLOCK line, update it and return t.
4045 Otherwise, return nil."
4048 (beginning-of-line 1)
4049 (skip-chars-forward " \t")
4050 (when (looking-at org-clock-string
)
4051 (let ((re (concat "[ \t]*" org-clock-string
4052 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
4053 "\\([ \t]*=>.*\\)?\\)?"))
4056 ((not (looking-at re
))
4058 ((not (match-end 2))
4059 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4060 (> org-clock-marker
(point))
4061 (<= org-clock-marker
(point-at-eol)))
4062 ;; The clock is running here
4063 (setq org-clock-start-time
4065 (org-parse-time-string (match-string 1))))
4066 (org-clock-update-mode-line)))
4068 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
4070 (setq ts
(match-string 1)
4071 te
(match-string 3))
4072 (setq s
(- (org-float-time
4073 (apply 'encode-time
(org-parse-time-string te
)))
4075 (apply 'encode-time
(org-parse-time-string ts
))))
4078 h
(floor (/ s
3600))
4082 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
4085 (defun org-check-running-clock ()
4086 "Check if the current buffer contains the running clock.
4087 If yes, offer to stop it and to save the buffer with the changes."
4088 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4089 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4092 (when (y-or-n-p "Save changed buffer?")
4095 (defun org-clocktable-try-shift (dir n
)
4096 "Check if this line starts a clock table, if yes, shift the time block."
4097 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4098 (org-clocktable-shift dir n
)))
4100 ;; Autoload org-timer.el
4105 '(org-timer-start org-timer org-timer-item
4106 org-timer-change-times-in-region
4108 org-timer-reset-timers
4109 org-timer-show-remaining-time
)))
4111 ;; Autoload org-feed.el
4116 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
4119 ;; Autoload org-indent.el
4121 ;; Define the variable already here, to make sure we have it.
4122 (defvar org-indent-mode nil
4123 "Non-nil if Org-Indent mode is enabled.
4124 Use the command `org-indent-mode' to change this variable.")
4129 '(org-indent-mode)))
4131 ;; Autoload org-mobile.el
4136 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
4138 ;; Autoload archiving code
4139 ;; The stuff that is needed for cycling and tags has to be defined here.
4141 (defgroup org-archive nil
4142 "Options concerning archiving in Org-mode."
4144 :group
'org-structure
)
4146 (defcustom org-archive-location
"%s_archive::"
4147 "The location where subtrees should be archived.
4149 The value of this variable is a string, consisting of two parts,
4150 separated by a double-colon. The first part is a filename and
4151 the second part is a headline.
4153 When the filename is omitted, archiving happens in the same file.
4154 %s in the filename will be replaced by the current file
4155 name (without the directory part). Archiving to a different file
4156 is useful to keep archived entries from contributing to the
4159 The archived entries will be filed as subtrees of the specified
4160 headline. When the headline is omitted, the subtrees are simply
4161 filed away at the end of the file, as top-level entries. Also in
4162 the heading you can use %s to represent the file name, this can be
4163 useful when using the same archive for a number of different files.
4165 Here are a few examples:
4167 If the current file is Projects.org, archive in file
4168 Projects.org_archive, as top-level trees. This is the default.
4170 \"::* Archived Tasks\"
4171 Archive in the current file, under the top-level headline
4172 \"* Archived Tasks\".
4174 \"~/org/archive.org::\"
4175 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4177 \"~/org/archive.org::* From %s\"
4178 Archive in file ~/org/archive.org (absolute path), under headlines
4179 \"From FILENAME\" where file name is the current file name.
4181 \"basement::** Finished Tasks\"
4182 Archive in file ./basement (relative path), as level 3 trees
4183 below the level 2 heading \"** Finished Tasks\".
4185 You may set this option on a per-file basis by adding to the buffer a
4188 #+ARCHIVE: basement::** Finished Tasks
4190 You may also define it locally for a subtree by setting an ARCHIVE property
4191 in the entry. If such a property is found in an entry, or anywhere up
4192 the hierarchy, it will be used."
4196 (defcustom org-archive-tag
"ARCHIVE"
4197 "The tag that marks a subtree as archived.
4198 An archived subtree does not open during visibility cycling, and does
4199 not contribute to the agenda listings.
4200 After changing this, font-lock must be restarted in the relevant buffers to
4201 get the proper fontification."
4203 :group
'org-keywords
4206 (defcustom org-agenda-skip-archived-trees t
4207 "Non-nil means the agenda will skip any items located in archived trees.
4208 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4209 variable is no longer recommended, you should leave it at the value t.
4210 Instead, use the key `v' to cycle the archives-mode in the agenda."
4212 :group
'org-agenda-skip
4215 (defcustom org-columns-skip-archived-trees t
4216 "Non-nil means ignore archived trees when creating column view."
4218 :group
'org-properties
4221 (defcustom org-cycle-open-archived-trees nil
4222 "Non-nil means `org-cycle' will open archived trees.
4223 An archived tree is a tree marked with the tag ARCHIVE.
4224 When nil, archived trees will stay folded. You can still open them with
4225 normal outline commands like `show-all', but not with the cycling commands."
4230 (defcustom org-sparse-tree-open-archived-trees nil
4231 "Non-nil means sparse tree construction shows matches in archived trees.
4232 When nil, matches in these trees are highlighted, but the trees are kept in
4235 :group
'org-sparse-trees
4238 (defun org-cycle-hide-archived-subtrees (state)
4239 "Re-hide all archived subtrees after a visibility state change."
4240 (when (and (not org-cycle-open-archived-trees
)
4241 (not (memq state
'(overview folded
))))
4243 (let* ((globalp (memq state
'(contents all
)))
4244 (beg (if globalp
(point-min) (point)))
4245 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4246 (org-hide-archived-subtrees beg end
)
4248 (if (looking-at (concat ".*:" org-archive-tag
":"))
4249 (message "%s" (substitute-command-keys
4250 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4252 (defun org-force-cycle-archived ()
4253 "Cycle subtree even if it is archived."
4255 (setq this-command
'org-cycle
)
4256 (let ((org-cycle-open-archived-trees t
))
4257 (call-interactively 'org-cycle
)))
4259 (defun org-hide-archived-subtrees (beg end
)
4260 "Re-hide all archived subtrees after a visibility state change."
4262 (let* ((re (concat ":" org-archive-tag
":")))
4264 (while (re-search-forward re end t
)
4265 (when (org-at-heading-p)
4266 (org-flag-subtree t
)
4267 (org-end-of-subtree t
))))))
4269 (defun org-flag-subtree (flag)
4271 (org-back-to-heading t
)
4272 (outline-end-of-heading)
4273 (outline-flag-region (point)
4274 (progn (org-end-of-subtree t
) (point))
4277 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4280 (org-autoload "org-archive"
4281 '(org-add-archive-files org-archive-subtree
4282 org-archive-to-archive-sibling org-toggle-archive-tag
4283 org-archive-subtree-default
4284 org-archive-subtree-default-with-confirmation
)))
4286 ;; Autoload Column View Code
4288 (declare-function org-columns-number-to-string
"org-colview")
4289 (declare-function org-columns-get-format-and-top-level
"org-colview")
4290 (declare-function org-columns-compute
"org-colview")
4292 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4293 '(org-columns-number-to-string org-columns-get-format-and-top-level
4294 org-columns-compute org-agenda-columns org-columns-remove-overlays
4295 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
4299 (declare-function org-id-store-link
"org-id")
4300 (declare-function org-id-locations-load
"org-id")
4301 (declare-function org-id-locations-save
"org-id")
4302 (defvar org-id-track-globally
)
4303 (org-autoload "org-id"
4304 '(org-id-get-create org-id-new org-id-copy org-id-get
4305 org-id-get-with-outline-path-completion
4306 org-id-get-with-outline-drilling org-id-store-link
4307 org-id-goto org-id-find org-id-store-link
))
4309 ;; Autoload Plotting Code
4311 (org-autoload "org-plot"
4312 '(org-plot/gnuplot
))
4314 ;;; Variables for pre-computed regular expressions, all buffer local
4316 (defvar org-drawer-regexp nil
4317 "Matches first line of a hidden block.")
4318 (make-variable-buffer-local 'org-drawer-regexp
)
4319 (defvar org-todo-regexp nil
4320 "Matches any of the TODO state keywords.")
4321 (make-variable-buffer-local 'org-todo-regexp
)
4322 (defvar org-not-done-regexp nil
4323 "Matches any of the TODO state keywords except the last one.")
4324 (make-variable-buffer-local 'org-not-done-regexp
)
4325 (defvar org-not-done-heading-regexp nil
4326 "Matches a TODO headline that is not done.")
4327 (make-variable-buffer-local 'org-not-done-regexp
)
4328 (defvar org-todo-line-regexp nil
4329 "Matches a headline and puts TODO state into group 2 if present.")
4330 (make-variable-buffer-local 'org-todo-line-regexp
)
4331 (defvar org-complex-heading-regexp nil
4332 "Matches a headline and puts everything into groups:
4334 group 2: The todo keyword, maybe
4335 group 3: Priority cookie
4336 group 4: True headline
4338 (make-variable-buffer-local 'org-complex-heading-regexp
)
4339 (defvar org-complex-heading-regexp-format nil
4340 "Printf format to make regexp to match an exact headline.
4341 This regexp will match the headline of any node which has the
4342 exact headline text that is put into the format, but may have any
4343 TODO state, priority and tags.")
4344 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4345 (defvar org-todo-line-tags-regexp nil
4346 "Matches a headline and puts TODO state into group 2 if present.
4347 Also put tags into group 4 if tags are present.")
4348 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4349 (defvar org-ds-keyword-length
12
4350 "Maximum length of the Deadline and SCHEDULED keywords.")
4351 (make-variable-buffer-local 'org-ds-keyword-length
)
4352 (defvar org-deadline-regexp nil
4353 "Matches the DEADLINE keyword.")
4354 (make-variable-buffer-local 'org-deadline-regexp
)
4355 (defvar org-deadline-time-regexp nil
4356 "Matches the DEADLINE keyword together with a time stamp.")
4357 (make-variable-buffer-local 'org-deadline-time-regexp
)
4358 (defvar org-deadline-line-regexp nil
4359 "Matches the DEADLINE keyword and the rest of the line.")
4360 (make-variable-buffer-local 'org-deadline-line-regexp
)
4361 (defvar org-scheduled-regexp nil
4362 "Matches the SCHEDULED keyword.")
4363 (make-variable-buffer-local 'org-scheduled-regexp
)
4364 (defvar org-scheduled-time-regexp nil
4365 "Matches the SCHEDULED keyword together with a time stamp.")
4366 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4367 (defvar org-closed-time-regexp nil
4368 "Matches the CLOSED keyword together with a time stamp.")
4369 (make-variable-buffer-local 'org-closed-time-regexp
)
4371 (defvar org-keyword-time-regexp nil
4372 "Matches any of the 4 keywords, together with the time stamp.")
4373 (make-variable-buffer-local 'org-keyword-time-regexp
)
4374 (defvar org-keyword-time-not-clock-regexp nil
4375 "Matches any of the 3 keywords, together with the time stamp.")
4376 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4377 (defvar org-maybe-keyword-time-regexp nil
4378 "Matches a timestamp, possibly preceded by a keyword.")
4379 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4380 (defvar org-planning-or-clock-line-re nil
4381 "Matches a line with planning or clock info.")
4382 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
4383 (defvar org-all-time-keywords nil
4384 "List of time keywords.")
4385 (make-variable-buffer-local 'org-all-time-keywords
)
4387 (defconst org-plain-time-of-day-regexp
4390 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4393 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4395 "Regular expression to match a plain time or time range.
4396 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4397 groups carry important information:
4399 1 the first time, range or not
4400 8 the second time, if it is a range.")
4402 (defconst org-plain-time-extension-regexp
4405 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4406 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4407 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4408 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4409 groups carry important information:
4412 9 minutes of duration")
4414 (defconst org-stamp-time-of-day-regexp
4416 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4417 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4419 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4420 "Regular expression to match a timestamp time or time range.
4421 After a match, the following groups carry important information:
4423 1 date plus weekday, for back referencing to make sure both times are on the same day
4424 2 the first time, range or not
4425 4 the second time, if it is a range.")
4427 (defconst org-startup-options
4428 '(("fold" org-startup-folded t
)
4429 ("overview" org-startup-folded t
)
4430 ("nofold" org-startup-folded nil
)
4431 ("showall" org-startup-folded nil
)
4432 ("showeverything" org-startup-folded showeverything
)
4433 ("content" org-startup-folded content
)
4434 ("indent" org-startup-indented t
)
4435 ("noindent" org-startup-indented nil
)
4436 ("hidestars" org-hide-leading-stars t
)
4437 ("showstars" org-hide-leading-stars nil
)
4438 ("odd" org-odd-levels-only t
)
4439 ("oddeven" org-odd-levels-only nil
)
4440 ("align" org-startup-align-all-tables t
)
4441 ("noalign" org-startup-align-all-tables nil
)
4442 ("inlineimages" org-startup-with-inline-images t
)
4443 ("noinlineimages" org-startup-with-inline-images nil
)
4444 ("customtime" org-display-custom-times t
)
4445 ("logdone" org-log-done time
)
4446 ("lognotedone" org-log-done note
)
4447 ("nologdone" org-log-done nil
)
4448 ("lognoteclock-out" org-log-note-clock-out t
)
4449 ("nolognoteclock-out" org-log-note-clock-out nil
)
4450 ("logrepeat" org-log-repeat state
)
4451 ("lognoterepeat" org-log-repeat note
)
4452 ("nologrepeat" org-log-repeat nil
)
4453 ("logreschedule" org-log-reschedule time
)
4454 ("lognotereschedule" org-log-reschedule note
)
4455 ("nologreschedule" org-log-reschedule nil
)
4456 ("logredeadline" org-log-redeadline time
)
4457 ("lognoteredeadline" org-log-redeadline note
)
4458 ("nologredeadline" org-log-redeadline nil
)
4459 ("logrefile" org-log-refile time
)
4460 ("lognoterefile" org-log-refile note
)
4461 ("nologrefile" org-log-refile nil
)
4462 ("fninline" org-footnote-define-inline t
)
4463 ("nofninline" org-footnote-define-inline nil
)
4464 ("fnlocal" org-footnote-section nil
)
4465 ("fnauto" org-footnote-auto-label t
)
4466 ("fnprompt" org-footnote-auto-label nil
)
4467 ("fnconfirm" org-footnote-auto-label confirm
)
4468 ("fnplain" org-footnote-auto-label plain
)
4469 ("fnadjust" org-footnote-auto-adjust t
)
4470 ("nofnadjust" org-footnote-auto-adjust nil
)
4471 ("constcgs" constants-unit-system cgs
)
4472 ("constSI" constants-unit-system SI
)
4473 ("noptag" org-tag-persistent-alist nil
)
4474 ("hideblocks" org-hide-block-startup t
)
4475 ("nohideblocks" org-hide-block-startup nil
)
4476 ("beamer" org-startup-with-beamer-mode t
)
4477 ("entitiespretty" org-pretty-entities t
)
4478 ("entitiesplain" org-pretty-entities nil
))
4479 "Variable associated with STARTUP options for org-mode.
4480 Each element is a list of three items: The startup options as written
4481 in the #+STARTUP line, the corresponding variable, and the value to
4482 set this variable to if the option is found. An optional forth element PUSH
4483 means to push this value onto the list in the variable.")
4485 (defun org-update-property-plist (key val props
)
4486 "Update PROPS with KEY and VAL."
4487 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4488 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4489 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4490 (previous (cdr (assoc key props
))))
4492 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4493 (cons (cons key val
) remainder
))))
4495 (defconst org-block-regexp
4496 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4497 "Regular expression for hiding blocks.")
4498 (defconst org-heading-keyword-regexp-format
4499 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4500 "Printf format for a regexp matching an headline with some keyword.
4501 This regexp will match the headline of any node which has the
4502 exact keyword that is put into the format. The keyword isn't in
4503 any group by default, but the stars and the body are.")
4504 (defconst org-heading-keyword-maybe-regexp-format
4505 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4506 "Printf format for a regexp matching an headline, possibly with some keyword.
4507 This regexp can match any headline with the specified keyword, or
4508 without a keyword. The keyword isn't in any group by default,
4509 but the stars and the body are.")
4511 (defun org-set-regexps-and-options ()
4512 "Precompute regular expressions for current buffer."
4513 (when (eq major-mode
'org-mode
)
4514 (org-set-local 'org-todo-kwd-alist nil
)
4515 (org-set-local 'org-todo-key-alist nil
)
4516 (org-set-local 'org-todo-key-trigger nil
)
4517 (org-set-local 'org-todo-keywords-1 nil
)
4518 (org-set-local 'org-done-keywords nil
)
4519 (org-set-local 'org-todo-heads nil
)
4520 (org-set-local 'org-todo-sets nil
)
4521 (org-set-local 'org-todo-log-states nil
)
4522 (org-set-local 'org-file-properties nil
)
4523 (org-set-local 'org-file-tags nil
)
4524 (let ((re (org-make-options-regexp
4525 '("CATEGORY" "TODO" "COLUMNS"
4526 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4527 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4529 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4531 (scripts org-use-sub-superscripts
)
4532 kwds kws0 kwsa key log value cat arch tags const links hw dws
4533 tail sep kws1 prio props ftags drawers beamer-p
4534 ext-setup-or-nil setup-contents
(start 0))
4538 (goto-char (point-min))
4539 (while (or (and ext-setup-or-nil
4540 (string-match re ext-setup-or-nil start
)
4541 (setq start
(match-end 0)))
4542 (and (setq ext-setup-or-nil nil start
0)
4543 (re-search-forward re nil t
)))
4544 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4545 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4546 (if (stringp value
) (setq value
(org-trim value
)))
4548 ((equal key
"CATEGORY")
4550 ((member key
'("SEQ_TODO" "TODO"))
4551 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4552 ((equal key
"TYP_TODO")
4553 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4554 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4555 ;; general TODO-like setup
4556 (push (cons (intern (downcase (match-string 1 key
)))
4557 (org-split-string value splitre
)) kwds
))
4559 (setq tags
(append tags
(if tags
'("\\n") nil
)
4560 (org-split-string value splitre
))))
4561 ((equal key
"COLUMNS")
4562 (org-set-local 'org-columns-default-format value
))
4564 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4565 (push (cons (match-string 1 value
)
4566 (org-trim (match-string 2 value
)))
4568 ((equal key
"PRIORITIES")
4569 (setq prio
(org-split-string value
" +")))
4570 ((equal key
"PROPERTY")
4571 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4572 (setq props
(org-update-property-plist (match-string 1 value
)
4573 (match-string 2 value
)
4575 ((equal key
"FILETAGS")
4576 (when (string-match "\\S-" value
)
4581 (mapcar (lambda (x) (org-split-string x
":"))
4582 (org-split-string value
)))))))
4583 ((equal key
"DRAWERS")
4584 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4585 ((equal key
"CONSTANTS")
4586 (setq const
(append const
(org-split-string value splitre
))))
4587 ((equal key
"STARTUP")
4588 (let ((opts (org-split-string value splitre
))
4590 (while (setq l
(pop opts
))
4591 (when (setq l
(assoc l org-startup-options
))
4592 (setq var
(nth 1 l
) val
(nth 2 l
))
4594 (set (make-local-variable var
) val
)
4595 (if (not (listp (symbol-value var
)))
4596 (set (make-local-variable var
) nil
))
4597 (set (make-local-variable var
) (symbol-value var
))
4598 (add-to-list var val
))))))
4599 ((equal key
"ARCHIVE")
4601 (remove-text-properties 0 (length arch
)
4602 '(face t fontified t
) arch
))
4603 ((equal key
"LATEX_CLASS")
4604 (setq beamer-p
(equal value
"beamer")))
4605 ((equal key
"OPTIONS")
4606 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4607 (setq scripts
(read (match-string 2 value
)))))
4608 ((equal key
"SETUPFILE")
4609 (setq setup-contents
(org-file-contents
4611 (org-remove-double-quotes value
))
4613 (if (not ext-setup-or-nil
)
4614 (setq ext-setup-or-nil setup-contents start
0)
4615 (setq ext-setup-or-nil
4616 (concat (substring ext-setup-or-nil
0 start
)
4617 "\n" setup-contents
"\n"
4618 (substring ext-setup-or-nil start
)))))))
4619 ;; search for property blocks
4620 (goto-char (point-min))
4621 (while (re-search-forward org-block-regexp nil t
)
4622 (when (equal "PROPERTY" (upcase (match-string 1)))
4623 (setq value
(replace-regexp-in-string
4624 "[\n\r]" " " (match-string 4)))
4625 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4626 (setq props
(org-update-property-plist (match-string 1 value
)
4627 (match-string 2 value
)
4629 (org-set-local 'org-use-sub-superscripts scripts
)
4631 (org-set-local 'org-category
(intern cat
))
4632 (push (cons "CATEGORY" cat
) props
))
4634 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4635 (setq prio
(mapcar 'string-to-char prio
))
4636 (org-set-local 'org-highest-priority
(nth 0 prio
))
4637 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4638 (org-set-local 'org-default-priority
(nth 2 prio
)))
4639 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4640 (and ftags
(org-set-local 'org-file-tags
4641 (mapcar 'org-add-prop-inherited ftags
)))
4642 (and drawers
(org-set-local 'org-drawers drawers
))
4643 (and arch
(org-set-local 'org-archive-location arch
))
4644 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4645 ;; Process the TODO keywords
4647 ;; Use the global values as if they had been given locally.
4648 (setq kwds
(default-value 'org-todo-keywords
))
4649 (if (stringp (car kwds
))
4650 (setq kwds
(list (cons org-todo-interpretation
4651 (default-value 'org-todo-keywords
)))))
4652 (setq kwds
(reverse kwds
)))
4653 (setq kwds
(nreverse kwds
))
4655 (while (setq kws
(pop kwds
))
4657 (run-hook-with-args-until-success
4658 'org-todo-setup-filter-hook kws
)
4660 (setq inter
(pop kws
) sep
(member "|" kws
)
4661 kws0
(delete "|" (copy-sequence kws
))
4666 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4668 (setq kw
(match-string 1 x
)
4669 key
(and (match-end 2) (match-string 2 x
))
4670 log
(org-extract-log-state-settings x
))
4671 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4672 (and log
(push log org-todo-log-states
))
4674 (error "Invalid TODO keyword %s" x
)))
4676 kwsa
(if kwsa
(append '((:startgroup
))
4680 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4681 tail
(list inter hw
(car dws
) (org-last dws
))))
4682 (add-to-list 'org-todo-heads hw
'append
)
4683 (push kws1 org-todo-sets
)
4684 (setq org-done-keywords
(append org-done-keywords dws nil
))
4685 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4686 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4687 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4688 (setq org-todo-sets
(nreverse org-todo-sets
)
4689 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4690 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4691 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4692 ;; Process the constants
4695 (while (setq e
(pop const
))
4696 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4697 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4698 (setq org-table-formula-constants-local cst
)))
4700 ;; Process the tags.
4703 (while (setq e
(pop tags
))
4705 ((equal e
"{") (push '(:startgroup
) tgs
))
4706 ((equal e
"}") (push '(:endgroup
) tgs
))
4707 ((equal e
"\\n") (push '(:newline
) tgs
))
4708 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4709 (push (cons (match-string 1 e
)
4710 (string-to-char (match-string 2 e
)))
4712 (t (push (list e
) tgs
))))
4713 (org-set-local 'org-tag-alist nil
)
4714 (while (setq e
(pop tgs
))
4715 (or (and (stringp (car e
))
4716 (assoc (car e
) org-tag-alist
))
4717 (push e org-tag-alist
)))))
4719 ;; Compute the regular expressions and other local variables.
4720 ;; Using `org-outline-regexp-bol' would complicate them much,
4721 ;; because of the fixed white space at the end of that string.
4722 (if (not org-done-keywords
)
4723 (setq org-done-keywords
(and org-todo-keywords-1
4724 (list (org-last org-todo-keywords-1
)))))
4725 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4726 (length org-scheduled-string
)
4727 (length org-clock-string
)
4728 (length org-closed-string
)))
4730 (concat "^[ \t]*:\\("
4731 (mapconcat 'regexp-quote org-drawers
"\\|")
4733 org-not-done-keywords
4734 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4737 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4741 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4743 org-not-done-heading-regexp
4744 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4745 org-todo-line-regexp
4746 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4747 org-complex-heading-regexp
4748 (concat "^\\(\\*+\\)"
4749 "\\(?: +" org-todo-regexp
"\\)?"
4750 "\\(?: +\\(\\[#.\\]\\)\\)?"
4751 "\\(?: +\\(.*?\\)\\)?"
4752 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4754 org-complex-heading-regexp-format
4755 (concat "^\\(\\*+\\)"
4756 "\\(?: +" org-todo-regexp
"\\)?"
4757 "\\(?: +\\(\\[#.\\]\\)\\)?"
4759 ;; Stats cookies can be stuck to body.
4760 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4762 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4764 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4766 org-todo-line-tags-regexp
4767 (concat "^\\(\\*+\\)"
4768 "\\(?: +" org-todo-regexp
"\\)?"
4769 "\\(?: +\\(.*?\\)\\)?"
4770 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4772 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4773 org-deadline-time-regexp
4774 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4775 org-deadline-line-regexp
4776 (concat "\\<\\(" org-deadline-string
"\\).*")
4777 org-scheduled-regexp
4778 (concat "\\<" org-scheduled-string
)
4779 org-scheduled-time-regexp
4780 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4781 org-closed-time-regexp
4782 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4783 org-keyword-time-regexp
4784 (concat "\\<\\(" org-scheduled-string
4785 "\\|" org-deadline-string
4786 "\\|" org-closed-string
4787 "\\|" org-clock-string
"\\)"
4788 " *[[<]\\([^]>]+\\)[]>]")
4789 org-keyword-time-not-clock-regexp
4790 (concat "\\<\\(" org-scheduled-string
4791 "\\|" org-deadline-string
4792 "\\|" org-closed-string
4794 " *[[<]\\([^]>]+\\)[]>]")
4795 org-maybe-keyword-time-regexp
4796 (concat "\\(\\<\\(" org-scheduled-string
4797 "\\|" org-deadline-string
4798 "\\|" org-closed-string
4799 "\\|" org-clock-string
"\\)\\)?"
4800 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4801 org-planning-or-clock-line-re
4802 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4803 "\\|" org-deadline-string
4804 "\\|" org-closed-string
"\\|" org-clock-string
4806 org-all-time-keywords
4807 (mapcar (lambda (w) (substring w
0 -
1))
4808 (list org-scheduled-string org-deadline-string
4809 org-clock-string org-closed-string
))
4811 (org-compute-latex-and-specials-regexp)
4812 (org-set-font-lock-defaults))))
4814 (defun org-file-contents (file &optional noerror
)
4815 "Return the contents of FILE, as a string."
4817 (not (file-readable-p file
)))
4820 (message "Cannot read file \"%s\"" file
)
4823 (error "Cannot read file \"%s\"" file
))
4825 (insert-file-contents file
)
4828 (defun org-extract-log-state-settings (x)
4829 "Extract the log state setting from a TODO keyword string.
4830 This will extract info from a string like \"WAIT(w@/!)\"."
4831 (let (kw key log1 log2
)
4832 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4833 (setq kw
(match-string 1 x
)
4834 key
(and (match-end 2) (match-string 2 x
))
4835 log1
(and (match-end 3) (match-string 3 x
))
4836 log2
(and (match-end 4) (match-string 4 x
)))
4839 (and log1
(if (equal log1
"!") 'time
'note
))
4840 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4842 (defun org-remove-keyword-keys (list)
4843 "Remove a pair of parenthesis at the end of each string in LIST."
4845 (if (string-match "(.*)$" x
)
4846 (substring x
0 (match-beginning 0))
4850 (defun org-assign-fast-keys (alist)
4851 "Assign fast keys to a keyword-key alist.
4852 Respect keys that are already there."
4853 (let (new e
(alt ?
0))
4854 (while (setq e
(pop alist
))
4855 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4856 (cdr e
)) ;; Key already assigned.
4858 (let ((clist (string-to-list (downcase (car e
))))
4859 (used (append new alist
)))
4860 (when (= (car clist
) ?
@)
4862 (while (and clist
(rassoc (car clist
) used
))
4865 (while (rassoc alt used
)
4867 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4870 ;;; Some variables used in various places
4872 (defvar org-window-configuration nil
4873 "Used in various places to store a window configuration.")
4874 (defvar org-selected-window nil
4875 "Used in various places to store a window configuration.")
4876 (defvar org-finish-function nil
4877 "Function to be called when `C-c C-c' is used.
4878 This is for getting out of special buffers like remember.")
4881 ;; FIXME: Occasionally check by commenting these, to make sure
4882 ;; no other functions uses these, forgetting to let-bind them.
4887 ;; Defined somewhere in this file, but used before definition.
4888 (defvar org-entities
) ;; defined in org-entities.el
4889 (defvar org-struct-menu
)
4890 (defvar org-org-menu
)
4891 (defvar org-tbl-menu
)
4893 ;;;; Define the Org-mode
4895 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4896 (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"))
4899 ;; We use a before-change function to check if a table might need
4901 (defvar org-table-may-need-update t
4902 "Indicates that a table might need an update.
4903 This variable is set by `org-before-change-function'.
4904 `org-table-align' sets it back to nil.")
4905 (defun org-before-change-function (beg end
)
4906 "Every change indicates that a table might need an update."
4907 (setq org-table-may-need-update t
))
4908 (defvar org-mode-map
)
4909 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4910 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4911 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4912 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4913 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4914 (defvar org-table-buffer-is-an nil
)
4916 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
4917 ;; some places -- e.g. see the macro org-with-limited-levels.
4919 ;; In Org buffers, the value of `outline-regexp' is that of
4920 ;; `org-outline-regexp'. The only function still directly relying on
4921 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
4922 ;; job when `orgstruct-mode' is active.
4923 (defvar org-outline-regexp
"\\*+ "
4924 "Regexp to match Org headlines.")
4925 (defconst org-outline-regexp-bol
"^\\*+ "
4926 "Regexp to match Org headlines.
4927 This is similar to `org-outline-regexp' but additionally makes
4928 sure that we are at the beginning of the line.")
4930 (defconst org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4931 "Matches an headline, putting stars and text into groups.
4932 Stars are put in group 1 and the trimmed body in group 2.")
4934 (defvar bidi-paragraph-direction
)
4937 (define-derived-mode org-mode outline-mode
"Org"
4938 "Outline-based notes management and organizer, alias
4939 \"Carsten's outline-mode for keeping track of everything.\"
4941 Org-mode develops organizational tasks around a NOTES file which
4942 contains information about projects as plain text. Org-mode is
4943 implemented on top of outline-mode, which is ideal to keep the content
4944 of large files well structured. It supports ToDo items, deadlines and
4945 time stamps, which magically appear in the diary listing of the Emacs
4946 calendar. Tables are easily created with a built-in table editor.
4947 Plain text URL-like links connect to websites, emails (VM), Usenet
4948 messages (Gnus), BBDB entries, and any files related to the project.
4949 For printing and sharing of notes, an Org-mode file (or a part of it)
4950 can be exported as a structured ASCII or HTML file.
4952 The following commands are available:
4956 ;; Get rid of Outline menus, they are not needed
4957 ;; Need to do this here because define-derived-mode sets up
4958 ;; the keymap so late. Still, it is a waste to call this each time
4959 ;; we switch another buffer into org-mode.
4960 (if (featurep 'xemacs
)
4961 (when (boundp 'outline-mode-menu-heading
)
4962 ;; Assume this is Greg's port, it uses easymenu
4963 (easy-menu-remove outline-mode-menu-heading
)
4964 (easy-menu-remove outline-mode-menu-show
)
4965 (easy-menu-remove outline-mode-menu-hide
))
4966 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4967 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4968 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4970 (org-load-modules-maybe)
4971 (easy-menu-add org-org-menu
)
4972 (easy-menu-add org-tbl-menu
)
4973 (org-install-agenda-files-menu)
4974 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
4975 (add-to-invisibility-spec '(org-cwidth))
4976 (add-to-invisibility-spec '(org-hide-block . t
))
4977 (when (featurep 'xemacs
)
4978 (org-set-local 'line-move-ignore-invisible t
))
4979 (org-set-local 'outline-regexp org-outline-regexp
)
4980 (org-set-local 'outline-level
'org-outline-level
)
4981 (setq bidi-paragraph-direction
'left-to-right
)
4982 (when (and org-ellipsis
4983 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4984 (fboundp 'make-glyph-code
))
4985 (unless org-display-table
4986 (setq org-display-table
(make-display-table)))
4987 (set-display-table-slot
4990 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4992 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4993 (setq buffer-display-table org-display-table
))
4994 (org-set-regexps-and-options)
4995 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4996 ;; tag faces set outside customize.... force initialization.
4997 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4999 (org-set-local 'calc-embedded-open-mode
"# ")
5000 (modify-syntax-entry ?
@ "w")
5001 (if org-startup-truncated
(setq truncate-lines t
))
5002 (org-set-local 'font-lock-unfontify-region-function
5003 'org-unfontify-region
)
5004 ;; Activate before-change-function
5005 (org-set-local 'org-table-may-need-update t
)
5006 (org-add-hook 'before-change-functions
'org-before-change-function nil
5008 ;; Check for running clock before killing a buffer
5009 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5010 ;; Paragraphs and auto-filling
5011 (org-set-autofill-regexps)
5012 (setq indent-line-function
'org-indent-line-function
)
5013 (org-update-radio-target-regexp)
5014 ;; Beginning/end of defun
5015 (org-set-local 'beginning-of-defun-function
'org-beginning-of-defun
)
5016 (org-set-local 'end-of-defun-function
'org-end-of-defun
)
5017 ;; Next error for sparse trees
5018 (org-set-local 'next-error-function
'org-occur-next-match
)
5019 ;; Make sure dependence stuff works reliably, even for users who set it
5021 (if org-enforce-todo-dependencies
5022 (add-hook 'org-blocker-hook
5023 'org-block-todo-from-children-or-siblings-or-parent
)
5024 (remove-hook 'org-blocker-hook
5025 'org-block-todo-from-children-or-siblings-or-parent
))
5026 (if org-enforce-todo-checkbox-dependencies
5027 (add-hook 'org-blocker-hook
5028 'org-block-todo-from-checkboxes
)
5029 (remove-hook 'org-blocker-hook
5030 'org-block-todo-from-checkboxes
))
5032 ;; Comment characters
5033 (org-set-local 'comment-start
"#")
5034 (org-set-local 'comment-padding
" ")
5036 ;; Align options lines
5038 'align-mode-rules-list
5039 '((org-in-buffer-settings
5040 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5041 (modes .
'(org-mode)))))
5044 (org-set-local 'imenu-create-index-function
5045 'org-imenu-get-tree
)
5047 ;; Make isearch reveal context
5048 (if (or (featurep 'xemacs
)
5049 (not (boundp 'outline-isearch-open-invisible-function
)))
5050 ;; Emacs 21 and XEmacs make use of the hook
5051 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5052 ;; Emacs 22 deals with this through a special variable
5053 (org-set-local 'outline-isearch-open-invisible-function
5054 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5056 ;; Turn on org-beamer-mode?
5057 (and org-startup-with-beamer-mode
(org-beamer-mode 1))
5059 ;; Setup the pcomplete hooks
5060 (set (make-local-variable 'pcomplete-command-completion-function
)
5061 'org-pcomplete-initial
)
5062 (set (make-local-variable 'pcomplete-command-name-function
)
5063 'org-command-at-point
)
5064 (set (make-local-variable 'pcomplete-default-completion-function
)
5066 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5067 'org-parse-arguments
)
5068 (set (make-local-variable 'pcomplete-termination-string
) "")
5069 (set (make-local-variable 'face-remapping-alist
)
5070 '((default org-default
)))
5072 ;; If empty file that did not turn on org-mode automatically, make it to.
5073 (if (and org-insert-mode-line-in-empty-file
5074 (org-called-interactively-p 'any
)
5075 (= (point-min) (point-max)))
5076 (insert "# -*- mode: org -*-\n\n"))
5077 (unless org-inhibit-startup
5078 (when org-startup-align-all-tables
5079 (let ((bmp (buffer-modified-p)))
5080 (org-table-map-tables 'org-table-align
'quietly
)
5081 (set-buffer-modified-p bmp
)))
5082 (when org-startup-with-inline-images
5083 (org-display-inline-images))
5084 (when org-startup-indented
5085 (require 'org-indent
)
5086 (org-indent-mode 1))
5087 (unless org-inhibit-startup-visibility-stuff
5088 (org-set-startup-visibility))))
5090 (when (fboundp 'abbrev-table-put
)
5091 (abbrev-table-put org-mode-abbrev-table
5092 :parents
(list text-mode-abbrev-table
)))
5094 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5096 (defun org-current-time ()
5097 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5098 (if (> (car org-time-stamp-rounding-minutes
) 1)
5099 (let ((r (car org-time-stamp-rounding-minutes
))
5100 (time (decode-time)))
5102 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5107 "Return today date, considering `org-extend-today-until'."
5109 (time-subtract (current-time)
5110 (list 0 (* 3600 org-extend-today-until
) 0))))
5112 ;;;; Font-Lock stuff, including the activators
5114 (defvar org-mouse-map
(make-sparse-keymap))
5115 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5116 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5117 (when org-mouse-1-follows-link
5118 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5119 (when org-tab-follows-link
5120 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5121 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5123 (require 'font-lock
)
5125 (defconst org-non-link-chars
"]\t\n\r<>")
5126 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5127 "shell" "elisp" "doi" "message"))
5128 (defvar org-link-types-re nil
5129 "Matches a link that has a url-like prefix like \"http:\"")
5130 (defvar org-link-re-with-space nil
5131 "Matches a link with spaces, optional angular brackets around it.")
5132 (defvar org-link-re-with-space2 nil
5133 "Matches a link with spaces, optional angular brackets around it.")
5134 (defvar org-link-re-with-space3 nil
5135 "Matches a link with spaces, only for internal part in bracket links.")
5136 (defvar org-angle-link-re nil
5137 "Matches link with angular brackets, spaces are allowed.")
5138 (defvar org-plain-link-re nil
5139 "Matches plain link, without spaces.")
5140 (defvar org-bracket-link-regexp nil
5141 "Matches a link in double brackets.")
5142 (defvar org-bracket-link-analytic-regexp nil
5143 "Regular expression used to analyze links.
5144 Here is what the match groups contain after a match:
5150 (defvar org-bracket-link-analytic-regexp
++ nil
5151 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5152 (defvar org-any-link-re nil
5153 "Regular expression matching any link.")
5155 (defcustom org-match-sexp-depth
3
5156 "Number of stacked braces for sub/superscript matching.
5157 This has to be set before loading org.el to be effective."
5158 :group
'org-export-translation
; ??????????????????????????/
5161 (defun org-create-multibrace-regexp (left right n
)
5162 "Create a regular expression which will match a balanced sexp.
5163 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5164 as single character strings.
5165 The regexp returned will match the entire expression including the
5166 delimiters. It will also define a single group which contains the
5167 match except for the outermost delimiters. The maximum depth of
5168 stacked delimiters is N. Escaping delimiters is not possible."
5169 (let* ((nothing (concat "[^" left right
"]*?"))
5172 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5175 re
(concat re or next
)
5176 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5177 (concat left
"\\(" re
"\\)" right
)))
5179 (defvar org-match-substring-regexp
5181 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5182 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5184 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5186 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5187 "The regular expression matching a sub- or superscript.")
5189 (defvar org-match-substring-with-braces-regexp
5191 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5192 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5194 "The regular expression matching a sub- or superscript, forcing braces.")
5196 (defun org-make-link-regexps ()
5197 "Update the link regular expressions.
5198 This should be called after the variable `org-link-types' has changed."
5199 (setq org-link-types-re
5201 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5202 org-link-re-with-space
5204 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5205 "\\([^" org-non-link-chars
" ]"
5206 "[^" org-non-link-chars
"]*"
5207 "[^" org-non-link-chars
" ]\\)>?")
5208 org-link-re-with-space2
5210 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5211 "\\([^" org-non-link-chars
" ]"
5213 "[^" org-non-link-chars
" ]\\)>?")
5214 org-link-re-with-space3
5216 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5217 "\\([^" org-non-link-chars
" ]"
5221 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5222 "\\([^" org-non-link-chars
" ]"
5223 "[^" org-non-link-chars
"]*"
5227 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5228 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5229 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5230 org-bracket-link-regexp
5231 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5232 org-bracket-link-analytic-regexp
5235 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5238 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5240 org-bracket-link-analytic-regexp
++
5243 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5246 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5249 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5250 org-angle-link-re
"\\)\\|\\("
5251 org-plain-link-re
"\\)")))
5253 (org-make-link-regexps)
5255 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5256 "Regular expression for fast time stamp matching.")
5257 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5258 "Regular expression for fast time stamp matching.")
5259 (defconst org-ts-regexp0
5260 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5261 "Regular expression matching time strings for analysis.
5262 This one does not require the space after the date, so it can be used
5263 on a string that terminates immediately after the date.")
5264 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5265 "Regular expression matching time strings for analysis.")
5266 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5267 "Regular expression matching time stamps, with groups.")
5268 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5269 "Regular expression matching time stamps (also [..]), with groups.")
5270 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5271 "Regular expression matching a time stamp range.")
5272 (defconst org-tr-regexp-both
5273 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5274 "Regular expression matching a time stamp range.")
5275 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5276 org-ts-regexp
"\\)?")
5277 "Regular expression matching a time stamp or time stamp range.")
5278 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5279 org-ts-regexp-both
"\\)?")
5280 "Regular expression matching a time stamp or time stamp range.
5281 The time stamps may be either active or inactive.")
5283 (defvar org-emph-face nil
)
5285 (defun org-do-emphasis-faces (limit)
5286 "Run through the buffer and add overlays to emphasized strings."
5288 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5289 (if (not (= (char-after (match-beginning 3))
5290 (char-after (match-beginning 4))))
5293 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5294 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5298 (org-remove-flyspell-overlays-in
5299 (match-beginning 0) (match-end 0)))
5300 (add-text-properties (match-beginning 2) (match-end 2)
5301 '(font-lock-multiline t org-emphasis t
))
5302 (when org-hide-emphasis-markers
5303 (add-text-properties (match-end 4) (match-beginning 5)
5304 '(invisible org-link
))
5305 (add-text-properties (match-beginning 3) (match-end 3)
5306 '(invisible org-link
)))))
5310 (defun org-emphasize (&optional char
)
5311 "Insert or change an emphasis, i.e. a font like bold or italic.
5312 If there is an active region, change that region to a new emphasis.
5313 If there is no region, just insert the marker characters and position
5314 the cursor between them.
5315 CHAR should be either the marker character, or the first character of the
5316 HTML tag associated with that emphasis. If CHAR is a space, the means
5317 to remove the emphasis of the selected region.
5318 If char is not given (for example in an interactive call) it
5319 will be prompted for."
5321 (let ((eal org-emphasis-alist
) e det
5322 (erc org-emphasis-regexp-components
)
5324 (string "") beg end move tag c s
)
5325 (if (org-region-active-p)
5326 (setq beg
(region-beginning) end
(region-end)
5327 string
(buffer-substring beg end
))
5330 (while (setq e
(pop eal
))
5331 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5333 (push (cons c
(string-to-char (car e
))) det
)
5334 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5335 (substring tag
1)))))
5336 (setq det
(nreverse det
))
5338 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5339 (setq char
(read-char-exclusive)))
5340 (setq char
(or (cdr (assoc char det
)) char
))
5341 (if (equal char ?\
)
5342 (setq s
"" move nil
)
5343 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5344 (error "No such emphasis marker: \"%c\"" char
))
5345 (setq s
(char-to-string char
)))
5346 (while (and (> (length string
) 1)
5347 (equal (substring string
0 1) (substring string -
1))
5348 (assoc (substring string
0 1) org-emphasis-alist
))
5349 (setq string
(substring string
1 -
1)))
5350 (setq string
(concat s string s
))
5351 (if beg
(delete-region beg end
))
5353 (string-match (concat "[" (nth 0 erc
) "\n]")
5354 (char-to-string (char-before (point)))))
5357 (string-match (concat "[" (nth 1 erc
) "\n]")
5358 (char-to-string (char-after (point)))))
5359 (insert " ") (backward-char 1))
5361 (and move
(backward-char 1))))
5363 (defconst org-nonsticky-props
5364 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
5366 (defsubst org-rear-nonsticky-at
(pos)
5367 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5369 (defun org-activate-plain-links (limit)
5370 "Run through the buffer and add overlays to links."
5373 (if (re-search-forward org-plain-link-re limit t
)
5375 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5376 (setq f
(get-text-property (match-beginning 0) 'face
))
5377 (if (or (eq f
'org-tag
)
5378 (and (listp f
) (memq 'org-tag f
)))
5380 (add-text-properties (match-beginning 0) (match-end 0)
5381 (list 'mouse-face
'highlight
5383 'keymap org-mouse-map
))
5384 (org-rear-nonsticky-at (match-end 0)))
5387 (defun org-activate-code (limit)
5388 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5390 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5391 (remove-text-properties (match-beginning 0) (match-end 0)
5392 '(display t invisible t intangible t
))
5395 (defcustom org-src-fontify-natively nil
5396 "When non-nil, fontify code in code blocks."
5399 :group
'org-appearance
5402 (defun org-fontify-meta-lines-and-blocks (limit)
5404 (org-fontify-meta-lines-and-blocks-1 limit
)
5405 (error (message "org-mode fontification error"))))
5407 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5408 "Fontify #+ lines and blocks, in the correct ways."
5409 (let ((case-fold-search t
))
5410 (if (re-search-forward
5411 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5413 (let ((beg (match-beginning 0))
5414 (block-start (match-end 0))
5416 (lang (match-string 7))
5417 (beg1 (line-beginning-position 2))
5418 (dc1 (downcase (match-string 2)))
5419 (dc3 (downcase (match-string 3)))
5420 end end1 quoting block-type ovl
)
5422 ((member dc1
'("html:" "ascii:" "latex:" "docbook:"))
5423 ;; a single line of backend-specific content
5424 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5425 (remove-text-properties (match-beginning 0) (match-end 0)
5426 '(display t invisible t intangible t
))
5427 (add-text-properties (match-beginning 1) (match-end 3)
5428 '(font-lock-fontified t face org-meta-line
))
5429 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5430 '(font-lock-fontified t face org-block
))
5431 ; for backend-specific code
5433 ((and (match-end 4) (equal dc3
"begin"))
5435 (setq block-type
(downcase (match-string 5))
5436 quoting
(member block-type org-protecting-blocks
))
5437 (when (re-search-forward
5438 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5439 nil t
) ;; on purpose, we look further than LIMIT
5440 (setq end
(match-end 0) end1
(1- (match-beginning 0)))
5441 (setq block-end
(match-beginning 0))
5443 (remove-text-properties beg end
5444 '(display t invisible t intangible t
)))
5445 (add-text-properties
5447 '(font-lock-fontified t font-lock-multiline t
))
5448 (add-text-properties beg beg1
'(face org-meta-line
))
5449 (add-text-properties end1
(min (point-max) (1+ end
))
5450 '(face org-meta-line
)) ; for end_src
5452 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5453 (org-src-font-lock-fontify-block lang block-start block-end
)
5454 ;; remove old background overlays
5456 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5457 (delete-overlay ov
)))
5458 (overlays-at (/ (+ beg1 block-end
) 2)))
5459 ;; add a background overlay
5460 (setq ovl
(make-overlay beg1 block-end
))
5461 (overlay-put ovl
'face
'org-block-background
)
5462 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5464 (add-text-properties beg1
(min (point-max) (1+ end1
))
5465 '(face org-block
))) ; end of source block
5466 ((not org-fontify-quote-and-verse-blocks
))
5467 ((string= block-type
"quote")
5468 (add-text-properties beg1
(1+ end1
) '(face org-quote
)))
5469 ((string= block-type
"verse")
5470 (add-text-properties beg1
(1+ end1
) '(face org-verse
))))
5471 (add-text-properties beg beg1
'(face org-block-begin-line
))
5472 (add-text-properties (1+ end
) (1+ end1
) '(face org-block-end-line
))
5474 ((member dc1
'("title:" "author:" "email:" "date:"))
5475 (add-text-properties
5477 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5478 '(font-lock-fontified t invisible t
)
5479 '(font-lock-fontified t face org-document-info-keyword
)))
5480 (add-text-properties
5481 (match-beginning 6) (match-end 6)
5482 (if (string-equal dc1
"title:")
5483 '(font-lock-fontified t face org-document-title
)
5484 '(font-lock-fontified t face org-document-info
))))
5485 ((not (member (char-after beg
) '(?\ ?
\t)))
5486 ;; just any other in-buffer setting, but not indented
5487 (add-text-properties
5488 beg
(1+ (match-end 0))
5489 '(font-lock-fontified t face org-meta-line
))
5491 ((or (member dc1
'("begin:" "end:" "caption:" "label:"
5492 "orgtbl:" "tblfm:" "tblname:" "results:"
5493 "call:" "header:" "headers:" "name:"))
5494 (and (match-end 4) (equal dc3
"attr")))
5495 (add-text-properties
5497 '(font-lock-fontified t face org-meta-line
))
5499 ((member dc3
'(" " ""))
5500 (add-text-properties
5502 '(font-lock-fontified t face font-lock-comment-face
)))
5505 (defun org-strip-protective-commas (beg end
)
5506 "Strip protective commas between BEG and END in the current buffer."
5511 (let ((front-line (save-excursion
5513 "[^[:space:]]" end t
)
5514 (goto-char (match-beginning 0))
5516 (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t
)
5517 (goto-char (match-beginning 1))
5518 (when (= (current-column) front-line
)
5519 (replace-match "" nil nil nil
1)))))))
5521 (defun org-activate-angle-links (limit)
5522 "Run through the buffer and add overlays to links."
5523 (if (re-search-forward org-angle-link-re limit t
)
5525 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5526 (add-text-properties (match-beginning 0) (match-end 0)
5527 (list 'mouse-face
'highlight
5528 'keymap org-mouse-map
))
5529 (org-rear-nonsticky-at (match-end 0))
5532 (defun org-activate-footnote-links (limit)
5533 "Run through the buffer and add overlays to footnotes."
5534 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5536 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5537 (org-remove-flyspell-overlays-in beg end
)
5538 (add-text-properties beg end
5539 (list 'mouse-face
'highlight
5540 'keymap org-mouse-map
5542 (if (= (point-at-bol) beg
)
5543 "Footnote definition"
5544 "Footnote reference")
5545 'font-lock-fontified t
5546 'font-lock-multiline t
5547 'face
'org-footnote
))))))
5549 (defun org-activate-bracket-links (limit)
5550 "Run through the buffer and add overlays to bracketed links."
5551 (if (re-search-forward org-bracket-link-regexp limit t
)
5552 (let* ((help (concat "LINK: "
5553 (org-match-string-no-properties 1)))
5554 ;; FIXME: above we should remove the escapes.
5555 ;; but that requires another match, protecting match data,
5556 ;; a lot of overhead for font-lock.
5557 (ip (org-maybe-intangible
5558 (list 'invisible
'org-link
5559 'keymap org-mouse-map
'mouse-face
'highlight
5560 'font-lock-multiline t
'help-echo help
)))
5561 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5562 'font-lock-multiline t
'help-echo help
)))
5563 ;; We need to remove the invisible property here. Table narrowing
5564 ;; may have made some of this invisible.
5565 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5566 (remove-text-properties (match-beginning 0) (match-end 0)
5570 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5571 (org-rear-nonsticky-at (match-beginning 3))
5572 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5573 (org-rear-nonsticky-at (match-end 3))
5574 (add-text-properties (match-end 3) (match-end 0) ip
)
5575 (org-rear-nonsticky-at (match-end 0)))
5576 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5577 (org-rear-nonsticky-at (match-beginning 1))
5578 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5579 (org-rear-nonsticky-at (match-end 1))
5580 (add-text-properties (match-end 1) (match-end 0) ip
)
5581 (org-rear-nonsticky-at (match-end 0)))
5584 (defun org-activate-dates (limit)
5585 "Run through the buffer and add overlays to dates."
5586 (if (re-search-forward org-tsr-regexp-both limit t
)
5588 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5589 (add-text-properties (match-beginning 0) (match-end 0)
5590 (list 'mouse-face
'highlight
5591 'keymap org-mouse-map
))
5592 (org-rear-nonsticky-at (match-end 0))
5593 (when org-display-custom-times
5595 (org-display-custom-time (match-beginning 3) (match-end 3)))
5596 (org-display-custom-time (match-beginning 1) (match-end 1)))
5599 (defvar org-target-link-regexp nil
5600 "Regular expression matching radio targets in plain text.")
5601 (make-variable-buffer-local 'org-target-link-regexp
)
5602 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5603 "Regular expression matching a link target.")
5604 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5605 "Regular expression matching a radio target.")
5606 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5607 "Regular expression matching any target.")
5609 (defun org-activate-target-links (limit)
5610 "Run through the buffer and add overlays to target matches."
5611 (when org-target-link-regexp
5612 (let ((case-fold-search t
))
5613 (if (re-search-forward org-target-link-regexp limit t
)
5615 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5616 (add-text-properties (match-beginning 0) (match-end 0)
5617 (list 'mouse-face
'highlight
5618 'keymap org-mouse-map
5619 'help-echo
"Radio target link"
5620 'org-linked-text t
))
5621 (org-rear-nonsticky-at (match-end 0))
5624 (defun org-update-radio-target-regexp ()
5625 "Find all radio targets in this file and update the regular expression."
5627 (when (memq 'radio org-activate-links
)
5628 (setq org-target-link-regexp
5629 (org-make-target-link-regexp (org-all-targets 'radio
)))
5630 (org-restart-font-lock)))
5632 (defun org-hide-wide-columns (limit)
5634 (setq s
(text-property-any (point) (or limit
(point-max))
5637 (setq e
(next-single-property-change s
'org-cwidth
))
5638 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5642 (defvar org-latex-and-specials-regexp nil
5643 "Regular expression for highlighting export special stuff.")
5644 (defvar org-match-substring-regexp
)
5645 (defvar org-match-substring-with-braces-regexp
)
5647 ;; This should be with the exporter code, but we also use if for font-locking
5648 (defconst org-export-html-special-string-regexps
5649 '(("\\\\-" .
"­")
5650 ("---\\([^-]\\)" .
"—\\1")
5651 ("--\\([^-]\\)" .
"–\\1")
5652 ("\\.\\.\\." .
"…"))
5653 "Regular expressions for special string conversion.")
5656 (defun org-compute-latex-and-specials-regexp ()
5657 "Compute regular expression for stuff treated specially by exporters."
5658 (if (not org-highlight-latex-fragments-and-specials
)
5659 (org-set-local 'org-latex-and-specials-regexp nil
)
5662 ((matchers (plist-get org-format-latex-options
:matchers
))
5663 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5664 org-latex-regexps
)))
5665 (org-export-allow-BIND nil
)
5666 (options (org-combine-plists (org-default-export-plist)
5667 (org-infile-export-plist)))
5668 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5669 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5670 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5671 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5672 (org-export-with-special-strings (plist-get options
:special-strings
))
5675 ((equal org-export-with-sub-superscripts
'{})
5676 (list org-match-substring-with-braces-regexp
))
5677 (org-export-with-sub-superscripts
5678 (list org-match-substring-regexp
))
5681 (if org-export-with-LaTeX-fragments
5682 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5684 (if org-export-with-TeX-macros
5685 (list (concat "\\\\"
5691 (append org-entities-user
5693 (if (boundp 'org-latex-entities
)
5695 (or (car-safe x
) x
))
5700 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5701 (re-special (if org-export-with-special-strings
5702 (mapcar (lambda (x) (car x
))
5703 org-export-html-special-string-regexps
)))
5707 (if org-export-html-expand
"@<[^>\n]+>")
5710 'org-latex-and-specials-regexp
5711 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5714 (defun org-do-latex-and-special-faces (limit)
5715 "Run through the buffer and add overlays to links."
5716 (when org-latex-and-specials-regexp
5718 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5720 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5722 '(org-code org-verbatim underline
)))
5725 d
(cond ((member (char-after (1+ (match-beginning 0)))
5728 (font-lock-prepend-text-property
5729 (+ d
(match-beginning 0)) (match-end 0)
5730 'face
'org-latex-and-export-specials
)
5731 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5732 '(font-lock-multiline t
)))))
5735 (defun org-restart-font-lock ()
5736 "Restart `font-lock-mode', to force refontification."
5737 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5739 (font-lock-mode 1)))
5741 (defun org-all-targets (&optional radio
)
5742 "Return a list of all targets in this file.
5743 With optional argument RADIO, only find radio targets."
5744 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5747 (goto-char (point-min))
5748 (while (re-search-forward re nil t
)
5749 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5752 (defun org-make-target-link-regexp (targets)
5753 "Make regular expression matching all strings in TARGETS.
5754 The regular expression finds the targets also if there is a line break
5761 (setq x
(regexp-quote x
))
5762 (while (string-match " +" x
)
5763 (setq x
(replace-match "\\s-+" t t x
)))
5769 (defun org-activate-tags (limit)
5770 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5772 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5773 (add-text-properties (match-beginning 1) (match-end 1)
5774 (list 'mouse-face
'highlight
5775 'keymap org-mouse-map
))
5776 (org-rear-nonsticky-at (match-end 1))
5779 (defun org-outline-level ()
5780 "Compute the outline level of the heading at point.
5781 This function assumes that the cursor is at the beginning of a line matched
5782 by `outline-regexp'. Otherwise it returns garbage.
5783 If this is called at a normal headline, the level is the number of stars.
5784 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5786 (looking-at org-outline-regexp
)
5787 (1- (- (match-end 0) (match-beginning 0)))))
5789 (defvar org-font-lock-keywords nil
)
5791 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5792 "Regular expression matching a property line.")
5794 (defvar org-font-lock-hook nil
5795 "Functions to be called for special font lock stuff.")
5797 (defvar org-font-lock-set-keywords-hook nil
5798 "Functions that can manipulate `org-font-lock-extra-keywords'.
5799 This is called after `org-font-lock-extra-keywords' is defined, but before
5800 it is installed to be used by font lock. This can be useful if something
5801 needs to be inserted at a specific position in the font-lock sequence.")
5803 (defun org-font-lock-hook (limit)
5804 (run-hook-with-args 'org-font-lock-hook limit
))
5806 (defun org-set-font-lock-defaults ()
5807 (let* ((em org-fontify-emphasized-text
)
5808 (lk org-activate-links
)
5809 (org-font-lock-extra-keywords
5812 '(org-font-lock-hook)
5814 `(,(if org-fontify-whole-heading-line
5815 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5816 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5817 (1 (org-get-level-face 1))
5818 (2 (org-get-level-face 2))
5819 (3 (org-get-level-face 3)))
5821 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5824 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5825 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5826 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5827 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5829 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5830 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5832 (list org-property-re
5833 '(1 'org-special-keyword t
)
5834 '(3 'org-property-value t
))
5836 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5837 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5838 (if (memq 'plain lk
) '(org-activate-plain-links))
5839 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5840 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5841 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5842 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5843 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5844 '(org-hide-wide-columns (0 nil append
))
5846 (list (format org-heading-keyword-regexp-format
5848 '(2 (org-get-todo-face 2) t
))
5850 (if org-fontify-done-headline
5851 (list (format org-heading-keyword-regexp-format
5854 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5856 '(2 'org-headline-done t
))
5859 '(org-font-lock-add-priority-faces)
5861 '(org-font-lock-add-tag-faces)
5863 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5864 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5865 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5866 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5869 (if (featurep 'xemacs
)
5870 '(org-do-emphasis-faces (0 nil append
))
5871 '(org-do-emphasis-faces)))
5873 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5874 1 'org-checkbox prepend
)
5875 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5876 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5877 (0 (org-get-checkbox-statistics-face) t
)))
5878 ;; Description list items
5879 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5881 ;; ARCHIVEd headings
5883 org-outline-regexp-bol
5884 "\\(.*:" org-archive-tag
":.*\\)")
5885 '(1 'org-archived prepend
))
5887 '(org-do-latex-and-special-faces)
5888 '(org-fontify-entities)
5889 '(org-raise-scripts)
5891 '(org-activate-code (1 'org-code t
))
5893 (list (format org-heading-keyword-regexp-format
5895 org-comment-string
"\\|" org-quote-string
5897 '(2 'org-special-keyword t
))
5898 '("^#.*" (0 'font-lock-comment-face t
))
5899 ;; Blocks and meta lines
5900 '(org-fontify-meta-lines-and-blocks)
5902 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5903 (run-hooks 'org-font-lock-set-keywords-hook
)
5904 ;; Now set the full font-lock-keywords
5905 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5906 (org-set-local 'font-lock-defaults
5907 '(org-font-lock-keywords t nil nil backward-paragraph
))
5908 (kill-local-variable 'font-lock-keywords
) nil
))
5910 (defun org-toggle-pretty-entities ()
5911 "Toggle the composition display of entities as UTF8 characters."
5913 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5914 (org-restart-font-lock)
5915 (if org-pretty-entities
5916 (message "Entities are displayed as UTF8 characters")
5919 (org-decompose-region (point-min) (point-max))
5920 (message "Entities are displayed plain"))))
5922 (defun org-fontify-entities (limit)
5923 "Find an entity to fontify."
5925 (when org-pretty-entities
5927 (while (re-search-forward
5928 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
5930 (if (and (not (org-in-indented-comment-line))
5931 (setq ee
(org-entity-get (match-string 1)))
5932 (= (length (nth 6 ee
)) 1))
5934 (add-text-properties
5935 (match-beginning 0) (match-end 1)
5936 (list 'font-lock-fontified t
))
5937 (compose-region (match-beginning 0) (match-end 1)
5943 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5944 "Fontify string S like in Org-mode."
5947 (let ((org-odd-levels-only odd-levels
))
5949 (font-lock-fontify-buffer)
5955 (defun org-get-level-face (n)
5956 "Get the right face for match N in font-lock matching of headlines."
5957 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5958 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5959 (if org-cycle-level-faces
5960 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5961 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
5963 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5965 (t (if org-level-color-stars-only nil org-f
))))
5968 (defun org-get-todo-face (kwd)
5969 "Get the right face for a TODO keyword KWD.
5970 If KWD is a number, get the corresponding match group."
5971 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5972 (or (org-face-from-face-or-color
5973 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5974 (and (member kwd org-done-keywords
) 'org-done
)
5977 (defun org-face-from-face-or-color (context inherit face-or-color
)
5978 "Create a face list that inherits INHERIT, but sets the foreground color.
5979 When FACE-OR-COLOR is not a string, just return it."
5980 (if (stringp face-or-color
)
5981 (list :inherit inherit
5982 (cdr (assoc context org-faces-easy-properties
))
5986 (defun org-font-lock-add-tag-faces (limit)
5987 "Add the special tag faces."
5988 (when (and org-tag-faces org-tags-special-faces-re
)
5989 (while (re-search-forward org-tags-special-faces-re limit t
)
5990 (add-text-properties (match-beginning 1) (match-end 1)
5991 (list 'face
(org-get-tag-face 1)
5992 'font-lock-fontified t
))
5993 (backward-char 1))))
5995 (defun org-font-lock-add-priority-faces (limit)
5996 "Add the special priority faces."
5997 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
5998 (add-text-properties
5999 (match-beginning 0) (match-end 0)
6000 (list 'face
(or (org-face-from-face-or-color
6001 'priority
'org-special-keyword
6002 (cdr (assoc (char-after (match-beginning 1))
6003 org-priority-faces
)))
6004 'org-special-keyword
)
6005 'font-lock-fontified t
))))
6007 (defun org-get-tag-face (kwd)
6008 "Get the right face for a TODO keyword KWD.
6009 If KWD is a number, get the corresponding match group."
6010 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6011 (or (org-face-from-face-or-color
6012 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6015 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6016 "Remove fontification and activation overlays from links."
6017 (font-lock-default-unfontify-region beg end
)
6018 (let* ((buffer-undo-list t
)
6019 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6020 (inhibit-modification-hooks t
)
6021 deactivate-mark buffer-file-name buffer-file-truename
)
6022 (org-decompose-region beg end
)
6023 (remove-text-properties beg end
6024 '(mouse-face t keymap t org-linked-text t
6025 invisible t intangible t
6026 org-no-flyspell t org-emphasis t
))
6027 (org-remove-font-lock-display-properties beg end
)))
6029 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6030 ((raise 0.3) (height 0.7))
6033 "Display properties for showing superscripts and subscripts.")
6035 (defun org-remove-font-lock-display-properties (beg end
)
6036 "Remove specific display properties that have been added by font lock.
6037 The will remove the raise properties that are used to show superscripts
6041 (setq next
(next-single-property-change beg
'display nil end
)
6042 prop
(get-text-property beg
'display
))
6043 (if (member prop org-script-display
)
6044 (put-text-property beg next
'display nil
))
6047 (defun org-raise-scripts (limit)
6048 "Add raise properties to sub/superscripts."
6049 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6050 (if (re-search-forward
6051 (if (eq org-use-sub-superscripts t
)
6052 org-match-substring-regexp
6053 org-match-substring-with-braces-regexp
)
6055 (let* ((pos (point)) table-p comment-p
6056 (mpos (match-beginning 3))
6057 (emph-p (get-text-property mpos
'org-emphasis
))
6058 (link-p (get-text-property mpos
'mouse-face
))
6059 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6060 (goto-char (point-at-bol))
6061 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6062 comment-p
(org-looking-at-p "[ \t]*#"))
6064 ;; FIXME: Should we go back one character here, for a_b^c
6065 ;; (goto-char (1- pos)) ;????????????????????
6066 (if (or comment-p emph-p link-p keyw-p
)
6068 (put-text-property (match-beginning 3) (match-end 0)
6070 (if (equal (char-after (match-beginning 2)) ?^
)
6071 (nth (if table-p
3 1) org-script-display
)
6072 (nth (if table-p
2 0) org-script-display
)))
6073 (add-text-properties (match-beginning 2) (match-end 2)
6075 'org-dwidth t
'org-dwidth-n
1))
6076 (if (and (eq (char-after (match-beginning 3)) ?
{)
6077 (eq (char-before (match-end 3)) ?
}))
6079 (add-text-properties
6080 (match-beginning 3) (1+ (match-beginning 3))
6081 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6082 (add-text-properties
6083 (1- (match-end 3)) (match-end 3)
6084 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6087 ;;;; Visibility cycling, including org-goto and indirect buffer
6091 (defvar org-cycle-global-status nil
)
6092 (make-variable-buffer-local 'org-cycle-global-status
)
6093 (defvar org-cycle-subtree-status nil
)
6094 (make-variable-buffer-local 'org-cycle-subtree-status
)
6098 (defvar org-inlinetask-min-level
)
6100 (defun org-cycle (&optional arg
)
6101 "TAB-action and visibility cycling for Org-mode.
6103 This is the command invoked in Org-mode by the TAB key. Its main purpose
6104 is outline visibility cycling, but it also invokes other actions
6105 in special contexts.
6107 - When this function is called with a prefix argument, rotate the entire
6108 buffer through 3 states (global cycling)
6109 1. OVERVIEW: Show only top-level headlines.
6110 2. CONTENTS: Show all headlines of all levels, but no body text.
6111 3. SHOW ALL: Show everything.
6112 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6113 determined by the variable `org-startup-folded', and by any VISIBILITY
6114 properties in the buffer.
6115 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6116 including any drawers.
6118 - When inside a table, re-align the table and move to the next field.
6120 - When point is at the beginning of a headline, rotate the subtree started
6121 by this line through 3 different states (local cycling)
6122 1. FOLDED: Only the main headline is shown.
6123 2. CHILDREN: The main headline and the direct children are shown.
6124 From this state, you can move to one of the children
6125 and zoom in further.
6126 3. SUBTREE: Show the entire subtree, including body text.
6127 If there is no subtree, switch directly from CHILDREN to FOLDED.
6129 - When point is at the beginning of an empty headline and the variable
6130 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6131 of the headline by demoting and promoting it to likely levels. This
6132 speeds up creation document structure by pressing TAB once or several
6133 times right after creating a new headline.
6135 - When there is a numeric prefix, go up to a heading with level ARG, do
6136 a `show-subtree' and return to the previous cursor position. If ARG
6137 is negative, go up that many levels.
6139 - When point is not at the beginning of a headline, execute the global
6140 binding for TAB, which is re-indenting the line. See the option
6141 `org-cycle-emulate-tab' for details.
6143 - Special case: if point is at the beginning of the buffer and there is
6144 no headline in line 1, this function will act as if called with prefix arg
6145 (C-u TAB, same as S-TAB) also when called without prefix arg.
6146 But only if also the variable `org-cycle-global-at-bob' is t."
6148 (org-load-modules-maybe)
6149 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6150 (and org-cycle-level-after-item
/entry-creation
6151 (or (org-cycle-level)
6152 (org-cycle-item-indentation))))
6154 (or org-cycle-max-level
6155 (and (boundp 'org-inlinetask-min-level
)
6156 org-inlinetask-min-level
6157 (1- org-inlinetask-min-level
))))
6158 (nstars (and limit-level
6159 (if org-odd-levels-only
6160 (and limit-level
(1- (* limit-level
2)))
6163 (if (not (eq major-mode
'org-mode
))
6165 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6166 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6167 (not (looking-at org-outline-regexp
))))
6170 (delq 'org-optimize-window-after-visibility-change
6171 (copy-sequence org-cycle-hook
))
6175 (if (or bob-special
(equal arg
'(4)))
6176 ;; special case: use global cycling
6182 (setq last-command
'dummy
)
6183 (org-set-startup-visibility)
6184 (message "Startup visibility, plus VISIBILITY properties"))
6188 (message "Entire buffer visible, including drawers"))
6190 ;; Table: enter it or move to the next field.
6191 ((org-at-table-p 'any
)
6192 (if (org-at-table.el-p
)
6193 (message "Use C-c ' to edit table.el tables")
6194 (if arg
(org-table-edit-field t
)
6195 (org-table-justify-field-maybe)
6196 (call-interactively 'org-table-next-field
))))
6198 ((run-hook-with-args-until-success
6199 'org-tab-after-check-for-table-hook
))
6201 ;; Global cycling: delegate to `org-cycle-internal-global'.
6202 ((eq arg t
) (org-cycle-internal-global))
6204 ;; Drawers: delegate to `org-flag-drawer'.
6205 ((and org-drawers org-drawer-regexp
6207 (beginning-of-line 1)
6208 (looking-at org-drawer-regexp
)))
6209 (org-flag-drawer ; toggle block visibility
6210 (not (get-char-property (match-end 0) 'invisible
))))
6212 ;; Show-subtree, ARG levels up from here.
6215 (org-back-to-heading)
6216 (outline-up-heading (if (< arg
0) (- arg
)
6217 (- (funcall outline-level
) arg
)))
6218 (org-show-subtree)))
6220 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6221 ((and (featurep 'org-inlinetask
)
6222 (org-inlinetask-at-task-p)
6223 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6224 (org-inlinetask-toggle-visibility))
6226 ((org-try-cdlatex-tab))
6228 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6229 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6230 (save-excursion (beginning-of-line 1)
6231 (looking-at org-outline-regexp
)))
6232 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6233 (org-cycle-internal-local))
6235 ;; From there: TAB emulation and template completion.
6236 (buffer-read-only (org-back-to-heading))
6238 ((run-hook-with-args-until-success
6239 'org-tab-after-check-for-cycling-hook
))
6241 ((org-try-structure-completion))
6243 ((run-hook-with-args-until-success
6244 'org-tab-before-tab-emulation-hook
))
6246 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6248 (not (looking-at org-outline-regexp
))))
6249 (call-interactively (global-key-binding "\t")))
6251 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6252 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6253 (or (and (eq org-cycle-emulate-tab
'white
)
6254 (= (match-end 0) (point-at-eol)))
6255 (and (eq org-cycle-emulate-tab
'whitestart
)
6256 (>= (match-end 0) pos
))))
6258 (eq org-cycle-emulate-tab t
))
6259 (call-interactively (global-key-binding "\t")))
6262 (org-back-to-heading)
6265 (defun org-cycle-internal-global ()
6266 "Do the global cycling action."
6267 ;; Hack to avoid display of messages for .org attachments in Gnus
6268 (let ((ga (string-match "\\*fontification" (buffer-name))))
6270 ((and (eq last-command this-command
)
6271 (eq org-cycle-global-status
'overview
))
6272 ;; We just created the overview - now do table of contents
6273 ;; This can be slow in very large buffers, so indicate action
6274 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6275 (unless ga
(message "CONTENTS..."))
6277 (unless ga
(message "CONTENTS...done"))
6278 (setq org-cycle-global-status
'contents
)
6279 (run-hook-with-args 'org-cycle-hook
'contents
))
6281 ((and (eq last-command this-command
)
6282 (eq org-cycle-global-status
'contents
))
6283 ;; We just showed the table of contents - now show everything
6284 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6286 (unless ga
(message "SHOW ALL"))
6287 (setq org-cycle-global-status
'all
)
6288 (run-hook-with-args 'org-cycle-hook
'all
))
6291 ;; Default action: go to overview
6292 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6294 (unless ga
(message "OVERVIEW"))
6295 (setq org-cycle-global-status
'overview
)
6296 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6298 (defun org-cycle-internal-local ()
6299 "Do the local cycling action."
6300 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6301 ;; First, determine end of headline (EOH), end of subtree or item
6302 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6307 (setq struct
(org-list-struct))
6308 (setq eoh
(point-at-eol))
6309 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6310 (setq has-children
(org-list-has-child-p (point) struct
)))
6311 (org-back-to-heading)
6312 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6313 (setq eos
(save-excursion
6314 (org-end-of-subtree t
)
6316 (skip-chars-forward " \t\n"))
6317 (if (eobp) (point) (1- (point)))))
6320 (let ((level (funcall outline-level
)))
6321 (outline-next-heading)
6322 (and (org-at-heading-p t
)
6323 (> (funcall outline-level
) level
))))
6325 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6326 ;; Determine end invisible part of buffer (EOL)
6327 (beginning-of-line 2)
6328 ;; XEmacs doesn't have `next-single-char-property-change'
6329 (if (featurep 'xemacs
)
6330 (while (and (not (eobp)) ;; this is like `next-line'
6331 (get-char-property (1- (point)) 'invisible
))
6332 (beginning-of-line 2))
6333 (while (and (not (eobp)) ;; this is like `next-line'
6334 (get-char-property (1- (point)) 'invisible
))
6335 (goto-char (next-single-char-property-change (point) 'invisible
))
6336 (and (eolp) (beginning-of-line 2))))
6338 ;; Find out what to do next and set `this-command'
6341 ;; Nothing is hidden behind this heading
6342 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
6343 (message "EMPTY ENTRY")
6344 (setq org-cycle-subtree-status nil
)
6347 (outline-next-heading)
6348 (if (outline-invisible-p) (org-flag-heading nil
))))
6349 ((and (or (>= eol eos
)
6350 (not (string-match "\\S-" (buffer-substring eol eos
))))
6352 (not (setq children-skipped
6353 org-cycle-skip-children-state-if-no-children
))))
6354 ;; Entire subtree is hidden in one line: children view
6355 (run-hook-with-args 'org-pre-cycle-hook
'children
)
6357 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6359 (org-with-limited-levels (show-children))
6360 (when (memq 'org-cycle-hide-drawers org-cycle-hook
)
6361 (org-cycle-hide-drawers 'subtree
))
6362 ;; Fold every list in subtree to top-level items.
6363 (when (eq org-cycle-include-plain-lists
'integrate
)
6365 (org-back-to-heading)
6366 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6367 (beginning-of-line 1)
6368 (let* ((struct (org-list-struct))
6369 (prevs (org-list-prevs-alist struct
))
6370 (end (org-list-get-bottom-point struct
)))
6371 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6372 (org-list-get-all-items (point) struct prevs
))
6373 (goto-char end
))))))
6374 (message "CHILDREN")
6377 (outline-next-heading)
6378 (if (outline-invisible-p) (org-flag-heading nil
)))
6379 (setq org-cycle-subtree-status
'children
)
6380 (run-hook-with-args 'org-cycle-hook
'children
))
6381 ((or children-skipped
6382 (and (eq last-command this-command
)
6383 (eq org-cycle-subtree-status
'children
)))
6384 ;; We just showed the children, or no children are there,
6385 ;; now show everything.
6386 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
6387 (outline-flag-region eoh eos nil
)
6388 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6389 (setq org-cycle-subtree-status
'subtree
)
6390 (run-hook-with-args 'org-cycle-hook
'subtree
))
6392 ;; Default action: hide the subtree.
6393 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6394 (outline-flag-region eoh eos t
)
6396 (setq org-cycle-subtree-status
'folded
)
6397 (run-hook-with-args 'org-cycle-hook
'folded
)))))
6400 (defun org-global-cycle (&optional arg
)
6401 "Cycle the global visibility. For details see `org-cycle'.
6402 With \\[universal-argument] prefix arg, switch to startup visibility.
6403 With a numeric prefix, show all headlines up to that level."
6405 (let ((org-cycle-include-plain-lists
6406 (if (eq major-mode
'org-mode
) org-cycle-include-plain-lists nil
)))
6410 (hide-sublevels arg
)
6411 (setq org-cycle-global-status
'contents
))
6413 (org-set-startup-visibility)
6414 (message "Startup visibility, plus VISIBILITY properties."))
6416 (org-cycle '(4))))))
6418 (defun org-set-startup-visibility ()
6419 "Set the visibility required by startup options and properties."
6421 ((eq org-startup-folded t
)
6423 ((eq org-startup-folded
'content
)
6424 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6425 (org-cycle '(4)) (org-cycle '(4)))))
6426 (unless (eq org-startup-folded
'showeverything
)
6427 (if org-hide-block-startup
(org-hide-block-all))
6428 (org-set-visibility-according-to-property 'no-cleanup
)
6429 (org-cycle-hide-archived-subtrees 'all
)
6430 (org-cycle-hide-drawers 'all
)
6431 (org-cycle-show-empty-lines t
)))
6433 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6434 "Switch subtree visibilities according to :VISIBILITY: property."
6436 (let (org-show-entry-below state
)
6438 (goto-char (point-min))
6439 (while (re-search-forward
6440 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6442 (setq state
(match-string 1))
6444 (org-back-to-heading t
)
6448 ((equal state
'("fold" "folded"))
6450 ((equal state
"children")
6451 (org-show-hidden-entry)
6453 ((equal state
"content")
6456 (org-narrow-to-subtree)
6458 ((member state
'("all" "showall"))
6461 (org-cycle-hide-archived-subtrees 'all
)
6462 (org-cycle-hide-drawers 'all
)
6463 (org-cycle-show-empty-lines 'all
)))))
6465 ;; This function uses outline-regexp instead of the more fundamental
6466 ;; org-outline-regexp so that org-cycle-global works outside of Org
6467 ;; buffers, where outline-regexp is needed.
6468 (defun org-overview ()
6469 "Switch to overview mode, showing only top-level headlines.
6470 Really, this shows all headlines with level equal or greater than the level
6471 of the first headline in the buffer. This is important, because if the
6472 first headline is not level one, then (hide-sublevels 1) gives confusing
6475 (let ((level (save-excursion
6476 (goto-char (point-min))
6477 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6479 (goto-char (match-beginning 0))
6480 (funcall outline-level
))))))
6481 (and level
(hide-sublevels level
))))
6483 (defun org-content (&optional arg
)
6484 "Show all headlines in the buffer, like a table of contents.
6485 With numerical argument N, show content up to level N."
6488 ;; Visit all headings and show their offspring
6489 (and (integerp arg
) (org-overview))
6490 (goto-char (point-max))
6492 (while (and (progn (condition-case nil
6493 (outline-previous-visible-heading 1)
6494 (error (goto-char (point-min))))
6496 (looking-at org-outline-regexp
))
6498 (show-children (1- arg
))
6500 (if (bobp) (throw 'exit nil
))))))
6503 (defun org-optimize-window-after-visibility-change (state)
6504 "Adjust the window after a change in outline visibility.
6505 This function is the default value of the hook `org-cycle-hook'."
6506 (when (get-buffer-window (current-buffer))
6508 ((eq state
'content
) nil
)
6509 ((eq state
'all
) nil
)
6510 ((eq state
'folded
) nil
)
6511 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6512 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6514 (defun org-remove-empty-overlays-at (pos)
6515 "Remove outline overlays that do not contain non-white stuff."
6518 (and (eq 'outline
(overlay-get o
'invisible
))
6519 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6521 (delete-overlay o
)))
6524 (defun org-clean-visibility-after-subtree-move ()
6525 "Fix visibility issues after moving a subtree."
6526 ;; First, find a reasonable region to look at:
6527 ;; Start two siblings above, end three below
6528 (let* ((beg (save-excursion
6529 (and (org-get-last-sibling)
6530 (org-get-last-sibling))
6532 (end (save-excursion
6533 (and (org-get-next-sibling)
6534 (org-get-next-sibling)
6535 (org-get-next-sibling))
6536 (if (org-at-heading-p)
6539 (level (looking-at "\\*+"))
6540 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6543 (narrow-to-region beg end
)
6545 ;; Properly fold already folded siblings
6546 (goto-char (point-min))
6547 (while (re-search-forward re nil t
)
6548 (if (and (not (outline-invisible-p))
6550 (goto-char (point-at-eol)) (outline-invisible-p)))
6552 (org-cycle-show-empty-lines 'overview
)
6553 (org-cycle-hide-drawers 'overview
)))))
6555 (defun org-cycle-show-empty-lines (state)
6556 "Show empty lines above all visible headlines.
6557 The region to be covered depends on STATE when called through
6558 `org-cycle-hook'. Lisp program can use t for STATE to get the
6559 entire buffer covered. Note that an empty line is only shown if there
6560 are at least `org-cycle-separator-lines' empty lines before the headline."
6561 (when (not (= org-cycle-separator-lines
0))
6563 (let* ((n (abs org-cycle-separator-lines
))
6565 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6566 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6567 (t (let ((ns (number-to-string (- n
2))))
6568 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6569 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6572 ((memq state
'(overview contents t
))
6573 (setq beg
(point-min) end
(point-max)))
6574 ((memq state
'(children folded
))
6575 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6576 (beginning-of-line 2)
6580 (while (re-search-forward re end t
)
6581 (unless (get-char-property (match-end 1) 'invisible
)
6582 (setq e
(match-end 1))
6583 (if (< org-cycle-separator-lines
0)
6584 (setq b
(save-excursion
6585 (goto-char (match-beginning 0))
6586 (org-back-over-empty-lines)
6588 (goto-char (max (point-min) (1- (point))))
6592 (setq b
(match-beginning 1)))
6593 (outline-flag-region b e nil
)))))))
6594 ;; Never hide empty lines at the end of the file.
6596 (goto-char (point-max))
6597 (outline-previous-heading)
6598 (outline-end-of-heading)
6599 (if (and (looking-at "[ \t\n]+")
6600 (= (match-end 0) (point-max)))
6601 (outline-flag-region (point) (match-end 0) nil
))))
6603 (defun org-show-empty-lines-in-parent ()
6604 "Move to the parent and re-show empty lines before visible headlines."
6606 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6607 (org-cycle-show-empty-lines context
))))
6609 (defun org-files-list ()
6610 "Return `org-agenda-files' list, plus all open org-mode files.
6611 This is useful for operations that need to scan all of a user's
6612 open and agenda-wise Org files."
6613 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6614 (dolist (buf (buffer-list))
6615 (with-current-buffer buf
6616 (if (and (eq major-mode
'org-mode
) (buffer-file-name))
6617 (let ((file (expand-file-name (buffer-file-name))))
6618 (unless (member file files
)
6619 (push file files
))))))
6622 (defsubst org-entry-beginning-position
()
6623 "Return the beginning position of the current entry."
6624 (save-excursion (outline-back-to-heading t
) (point)))
6626 (defsubst org-entry-end-position
()
6627 "Return the end position of the current entry."
6628 (save-excursion (outline-next-heading) (point)))
6630 (defun org-cycle-hide-drawers (state)
6631 "Re-hide all drawers after a visibility state change."
6632 (when (and (eq major-mode
'org-mode
)
6633 (not (memq state
'(overview folded contents
))))
6635 (let* ((globalp (memq state
'(contents all
)))
6636 (beg (if globalp
(point-min) (point)))
6637 (end (if globalp
(point-max)
6638 (if (eq state
'children
)
6639 (save-excursion (outline-next-heading) (point))
6640 (org-end-of-subtree t
)))))
6642 (while (re-search-forward org-drawer-regexp end t
)
6643 (org-flag-drawer t
))))))
6645 (defun org-flag-drawer (flag)
6647 (beginning-of-line 1)
6648 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6649 (let ((b (match-end 0)))
6650 (if (re-search-forward
6652 (save-excursion (outline-next-heading) (point)) t
)
6653 (outline-flag-region b
(point-at-eol) flag
)
6654 (error ":END: line missing at position %s" b
))))))
6656 (defun org-subtree-end-visible-p ()
6657 "Is the end of the current subtree visible?"
6658 (pos-visible-in-window-p
6659 (save-excursion (org-end-of-subtree t
) (point))))
6661 (defun org-first-headline-recenter (&optional N
)
6662 "Move cursor to the first headline and recenter the headline.
6663 Optional argument N means put the headline into the Nth line of the window."
6664 (goto-char (point-min))
6665 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6667 (recenter (prefix-numeric-value N
))))
6669 ;;; Saving and restoring visibility
6671 (defun org-outline-overlay-data (&optional use-markers
)
6672 "Return a list of the locations of all outline overlays.
6673 These are overlays with the `invisible' property value `outline'.
6674 The return value is a list of cons cells, with start and stop
6675 positions for each overlay.
6676 If USE-MARKERS is set, return the positions as markers."
6683 (when (eq (overlay-get o
'invisible
) 'outline
)
6684 (setq beg
(overlay-start o
)
6685 end
(overlay-end o
))
6686 (and beg end
(> end beg
)
6688 (cons (move-marker (make-marker) beg
)
6689 (move-marker (make-marker) end
))
6691 (overlays-in (point-min) (point-max))))))))
6693 (defun org-set-outline-overlay-data (data)
6694 "Create visibility overlays for all positions in DATA.
6695 DATA should have been made by `org-outline-overlay-data'."
6702 (outline-flag-region (car c
) (cdr c
) t
))
6705 ;;; Folding of blocks
6707 (defvar org-hide-block-overlays nil
6708 "Overlays hiding blocks.")
6709 (make-variable-buffer-local 'org-hide-block-overlays
)
6711 (defun org-block-map (function &optional start end
)
6712 "Call FUNCTION at the head of all source blocks in the current buffer.
6713 Optional arguments START and END can be used to limit the range."
6714 (let ((start (or start
(point-min)))
6715 (end (or end
(point-max))))
6718 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6721 (goto-char (match-beginning 0))
6722 (funcall function
)))))))
6724 (defun org-hide-block-toggle-all ()
6725 "Toggle the visibility of all blocks in the current buffer."
6726 (org-block-map #'org-hide-block-toggle
))
6728 (defun org-hide-block-all ()
6729 "Fold all blocks in the current buffer."
6731 (org-show-block-all)
6732 (org-block-map #'org-hide-block-toggle-maybe
))
6734 (defun org-show-block-all ()
6735 "Unfold all blocks in the current buffer."
6737 (mapc 'delete-overlay org-hide-block-overlays
)
6738 (setq org-hide-block-overlays nil
))
6740 (defun org-hide-block-toggle-maybe ()
6741 "Toggle visibility of block at point."
6743 (let ((case-fold-search t
))
6745 (beginning-of-line 1)
6746 (looking-at org-block-regexp
))
6747 (progn (org-hide-block-toggle)
6748 t
) ;; to signal that we took action
6749 nil
))) ;; to signal that we did not
6751 (defun org-hide-block-toggle (&optional force
)
6752 "Toggle the visibility of the current block."
6756 (if (re-search-forward org-block-regexp nil t
)
6757 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6758 (end (match-end 0)) ;; end of entire body
6760 (if (memq t
(mapcar (lambda (overlay)
6761 (eq (overlay-get overlay
'invisible
)
6763 (overlays-at start
)))
6764 (if (or (not force
) (eq force
'off
))
6766 (when (member ov org-hide-block-overlays
)
6767 (setq org-hide-block-overlays
6768 (delq ov org-hide-block-overlays
)))
6769 (when (eq (overlay-get ov
'invisible
)
6771 (delete-overlay ov
)))
6772 (overlays-at start
)))
6773 (setq ov
(make-overlay start end
))
6774 (overlay-put ov
'invisible
'org-hide-block
)
6775 ;; make the block accessible to isearch
6777 ov
'isearch-open-invisible
6779 (when (member ov org-hide-block-overlays
)
6780 (setq org-hide-block-overlays
6781 (delq ov org-hide-block-overlays
)))
6782 (when (eq (overlay-get ov
'invisible
)
6784 (delete-overlay ov
))))
6785 (push ov org-hide-block-overlays
)))
6786 (error "Not looking at a source block"))))
6788 ;; org-tab-after-check-for-cycling-hook
6789 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6790 ;; Remove overlays when changing major mode
6791 (add-hook 'org-mode-hook
6792 (lambda () (org-add-hook 'change-major-mode-hook
6793 'org-show-block-all
'append
'local
)))
6797 (defvar org-goto-window-configuration nil
)
6798 (defvar org-goto-marker nil
)
6799 (defvar org-goto-map
6800 (let ((map (make-sparse-keymap)))
6801 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6802 (while (setq cmd
(pop cmds
))
6803 (substitute-key-definition cmd cmd map global-map
)))
6804 (suppress-keymap map
)
6805 (org-defkey map
"\C-m" 'org-goto-ret
)
6806 (org-defkey map
[(return)] 'org-goto-ret
)
6807 (org-defkey map
[(left)] 'org-goto-left
)
6808 (org-defkey map
[(right)] 'org-goto-right
)
6809 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6810 (org-defkey map
"\C-i" 'org-cycle
)
6811 (org-defkey map
[(tab)] 'org-cycle
)
6812 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6813 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6814 (if org-goto-auto-isearch
6815 (if (fboundp 'define-key-after
)
6816 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6818 (org-defkey map "q" 'org-goto-quit)
6819 (org-defkey map "n" 'outline-next-visible-heading)
6820 (org-defkey map "p" 'outline-previous-visible-heading)
6821 (org-defkey map "f" 'outline-forward-same-level)
6822 (org-defkey map "b" 'outline-backward-same-level)
6823 (org-defkey map "u" 'outline-up-heading))
6824 (org-defkey map "/" 'org-occur)
6825 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6826 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6827 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6828 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6829 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6832 (defconst org-goto-help
6833 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6834 RET=jump to location [Q]uit and return to previous location
6835 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6837 (defvar org-goto-start-pos) ; dynamically scoped parameter
6839 ;; FIXME: Docstring does not mention both interfaces
6840 (defun org-goto (&optional alternative-interface)
6841 "Look up a different location in the current file, keeping current visibility.
6843 When you want look-up or go to a different location in a document, the
6844 fastest way is often to fold the entire buffer and then dive into the tree.
6845 This method has the disadvantage, that the previous location will be folded,
6846 which may not be what you want.
6848 This command works around this by showing a copy of the current buffer
6849 in an indirect buffer, in overview mode. You can dive into the tree in
6850 that copy, use org-occur and incremental search to find a location.
6851 When pressing RET or `Q', the command returns to the original buffer in
6852 which the visibility is still unchanged. After RET it will also jump to
6853 the location selected in the indirect buffer and expose the headline
6856 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6857 (org-refile-use-outline-path t)
6858 (org-refile-target-verify-function nil)
6860 (if (not alternative-interface)
6862 (if (eq org-goto-interface 'outline)
6863 'outline-path-completion
6865 (org-goto-start-pos (point))
6867 (if (eq interface 'outline)
6868 (car (org-get-location (current-buffer) org-goto-help))
6869 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6870 (org-refile-check-position pa)
6874 (org-mark-ring-push org-goto-start-pos)
6875 (goto-char selected-point)
6876 (if (or (outline-invisible-p) (org-invisible-p2))
6877 (org-show-context 'org-goto)))
6880 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6881 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6882 (defvar org-goto-local-auto-isearch-map) ; defined below
6884 (defun org-get-location (buf help)
6885 "Let the user select a location in the Org-mode buffer BUF.
6886 This function uses a recursive edit. It returns the selected position
6888 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6889 (isearch-hide-immediately nil)
6890 (isearch-search-fun-function
6891 (lambda () 'org-goto-local-search-headings))
6892 (org-goto-selected-point org-goto-exit-command)
6894 (special-display-buffer-names nil)
6895 (special-display-regexps nil)
6896 (special-display-function nil))
6898 (save-window-excursion
6899 (delete-other-windows)
6900 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6901 (org-pop-to-buffer-same-window
6903 (make-indirect-buffer (current-buffer) "*org-goto*")
6904 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6905 (with-output-to-temp-buffer "*Help*"
6907 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6908 (setq buffer-read-only nil)
6909 (let ((org-startup-truncated t)
6910 (org-startup-folded nil)
6911 (org-startup-align-all-tables nil))
6914 (setq buffer-read-only t)
6915 (if (and (boundp 'org-goto-start-pos)
6916 (integer-or-marker-p org-goto-start-pos))
6917 (let ((org-show-hierarchy-above t)
6918 (org-show-siblings t)
6919 (org-show-following-heading t))
6920 (goto-char org-goto-start-pos)
6921 (and (outline-invisible-p) (org-show-context)))
6922 (goto-char (point-min)))
6923 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6924 (message "Select location and press RET")
6925 (use-local-map org-goto-map)
6928 (kill-buffer "*org-goto*")
6929 (cons org-goto-selected-point org-goto-exit-command)))
6931 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6932 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6933 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6934 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6936 (defun org-goto-local-search-headings (string bound noerror)
6937 "Search and make sure that any matches are in headlines."
6939 (while (if isearch-forward
6940 (search-forward string bound noerror)
6941 (search-backward string bound noerror))
6942 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6943 (and (member :headline context)
6944 (not (member :tags context))))
6945 (throw 'return (point))))))
6947 (defun org-goto-local-auto-isearch ()
6950 (goto-char (point-min))
6951 (let ((keys (this-command-keys)))
6952 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6954 (isearch-process-search-char (string-to-char keys)))))
6956 (defun org-goto-ret (&optional arg)
6957 "Finish `org-goto' by going to the new location."
6959 (setq org-goto-selected-point (point)
6960 org-goto-exit-command 'return)
6963 (defun org-goto-left ()
6964 "Finish `org-goto' by going to the new location."
6966 (if (org-at-heading-p)
6968 (beginning-of-line 1)
6969 (setq org-goto-selected-point (point)
6970 org-goto-exit-command 'left)
6972 (error "Not on a heading")))
6974 (defun org-goto-right ()
6975 "Finish `org-goto' by going to the new location."
6977 (if (org-at-heading-p)
6979 (setq org-goto-selected-point (point)
6980 org-goto-exit-command 'right)
6982 (error "Not on a heading")))
6984 (defun org-goto-quit ()
6985 "Finish `org-goto' without cursor motion."
6987 (setq org-goto-selected-point nil)
6988 (setq org-goto-exit-command 'quit)
6991 ;;; Indirect buffer display of subtrees
6993 (defvar org-indirect-dedicated-frame nil
6994 "This is the frame being used for indirect tree display.")
6995 (defvar org-last-indirect-buffer nil)
6997 (defun org-tree-to-indirect-buffer (&optional arg)
6998 "Create indirect buffer and narrow it to current subtree.
6999 With numerical prefix ARG, go up to this level and then take that tree.
7000 If ARG is negative, go up that many levels.
7001 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7002 indirect buffer previously made with this command, to avoid proliferation of
7003 indirect buffers. However, when you call the command with a \
7004 \\[universal-argument] prefix, or
7005 when `org-indirect-buffer-display' is `new-frame', the last buffer
7006 is kept so that you can work with several indirect buffers at the same time.
7007 If `org-indirect-buffer-display' is `dedicated-frame', the \
7008 \\[universal-argument] prefix also
7009 requests that a new frame be made for the new buffer, so that the dedicated
7010 frame is not changed."
7012 (let ((cbuf (current-buffer))
7013 (cwin (selected-window))
7015 beg end level heading ibuf)
7017 (org-back-to-heading t)
7019 (setq level (org-outline-level))
7020 (if (< arg 0) (setq arg (+ level arg)))
7021 (while (> (setq level (org-outline-level)) arg)
7022 (outline-up-heading 1 t)))
7024 heading (org-get-heading))
7025 (org-end-of-subtree t t)
7026 (if (org-at-heading-p) (backward-char 1))
7028 (if (and (buffer-live-p org-last-indirect-buffer)
7029 (not (eq org-indirect-buffer-display 'new-frame))
7031 (kill-buffer org-last-indirect-buffer))
7032 (setq ibuf (org-get-indirect-buffer cbuf)
7033 org-last-indirect-buffer ibuf)
7035 ((or (eq org-indirect-buffer-display 'new-frame)
7036 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7037 (select-frame (make-frame))
7038 (delete-other-windows)
7039 (org-pop-to-buffer-same-window ibuf)
7040 (org-set-frame-title heading))
7041 ((eq org-indirect-buffer-display 'dedicated-frame)
7043 (select-frame (or (and org-indirect-dedicated-frame
7044 (frame-live-p org-indirect-dedicated-frame)
7045 org-indirect-dedicated-frame)
7046 (setq org-indirect-dedicated-frame (make-frame)))))
7047 (delete-other-windows)
7048 (org-pop-to-buffer-same-window ibuf)
7049 (org-set-frame-title (concat "Indirect: " heading)))
7050 ((eq org-indirect-buffer-display 'current-window)
7051 (org-pop-to-buffer-same-window ibuf))
7052 ((eq org-indirect-buffer-display 'other-window)
7053 (pop-to-buffer ibuf))
7054 (t (error "Invalid value")))
7055 (if (featurep 'xemacs)
7056 (save-excursion (org-mode) (turn-on-font-lock)))
7057 (narrow-to-region beg end)
7060 (run-hook-with-args 'org-cycle-hook 'all)
7061 (and (window-live-p cwin) (select-window cwin))))
7063 (defun org-get-indirect-buffer (&optional buffer)
7064 (setq buffer (or buffer (current-buffer)))
7065 (let ((n 1) (base (buffer-name buffer)) bname)
7066 (while (buffer-live-p
7067 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7070 (make-indirect-buffer buffer bname 'clone)
7071 (error (make-indirect-buffer buffer bname)))))
7073 (defun org-set-frame-title (title)
7074 "Set the title of the current frame to the string TITLE."
7075 ;; FIXME: how to name a single frame in XEmacs???
7076 (unless (featurep 'xemacs)
7077 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7079 ;;;; Structure editing
7081 ;;; Inserting headlines
7083 (defun org-previous-line-empty-p ()
7086 (or (beginning-of-line 0) t)
7088 (looking-at "[ \t]*$")))))
7090 (defun org-insert-heading (&optional force-heading invisible-ok)
7091 "Insert a new heading or item with same depth at point.
7092 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7093 If point is at the beginning of a headline, insert a sibling before the
7094 current headline. If point is not at the beginning, split the line,
7095 create the new headline with the text in the current line after point
7096 \(but see also the variable `org-M-RET-may-split-line').
7098 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7099 This is important for non-interactive uses of the command."
7101 (if (or (= (buffer-size) 0)
7102 (and (not (save-excursion
7103 (and (ignore-errors (org-back-to-heading invisible-ok))
7104 (org-at-heading-p))))
7105 (or force-heading (not (org-in-item-p)))))
7108 (run-hooks 'org-insert-heading-hook))
7109 (when (or force-heading (not (org-insert-item)))
7110 (let* ((empty-line-p nil)
7112 (on-heading (org-at-heading-p))
7113 (head (save-excursion
7116 (org-back-to-heading invisible-ok)
7117 (when (and (not on-heading)
7118 (featurep 'org-inlinetask)
7119 (integerp org-inlinetask-min-level)
7120 (>= (length (match-string 0))
7121 org-inlinetask-min-level))
7122 ;; Find a heading level before the inline task
7123 (while (and (setq level (org-up-heading-safe))
7124 (>= level org-inlinetask-min-level)))
7125 (if (org-at-heading-p)
7126 (org-back-to-heading invisible-ok)
7127 (error "This should not happen")))
7128 (setq empty-line-p (org-previous-line-empty-p))
7131 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7132 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7133 pos hide-previous previous-pos)
7135 ((and (org-at-heading-p) (bolp)
7137 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7138 ;; insert before the current line
7139 (open-line (if blank 2 1)))
7141 (not org-insert-heading-respect-content)
7144 (backward-char 1) (not (outline-invisible-p)))))
7145 ;; insert right here
7148 ;; somewhere in the line
7150 (setq previous-pos (point-at-bol))
7152 (setq hide-previous (outline-invisible-p)))
7153 (and org-insert-heading-respect-content (org-show-subtree))
7155 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7158 (goto-char (point-at-bol))
7159 (and (looking-at org-complex-heading-regexp)
7161 (> p (match-beginning 4)))))))
7164 (org-insert-heading-respect-content
7165 (org-end-of-subtree nil t)
7166 (when (featurep 'org-inlinetask)
7167 (while (and (not (eobp))
7168 (looking-at "\\(\\*+\\)[ \t]+")
7169 (>= (length (match-string 1))
7170 org-inlinetask-min-level))
7171 (org-end-of-subtree nil t)))
7172 (or (bolp) (newline))
7173 (or (org-previous-line-empty-p)
7174 (and blank (newline)))
7180 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7181 (setq tags (and (match-end 2) (match-string 2)))
7183 (delete-region (match-beginning 1) (match-end 1)))
7184 (setq pos (point-at-bol))
7185 (or split (end-of-line 1))
7186 (delete-horizontal-space)
7187 (if (string-match "\\`\\*+\\'"
7188 (buffer-substring (point-at-bol) (point)))
7190 (newline (if blank 2 1))
7196 (org-set-tags nil 'align))))
7198 (or split (end-of-line 1))
7199 (newline (if blank 2 1)))))))
7200 (insert head) (just-one-space)
7203 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7204 (when (and org-insert-heading-respect-content hide-previous)
7206 (goto-char previous-pos)
7208 (run-hooks 'org-insert-heading-hook)))))
7210 (defun org-get-heading (&optional no-tags no-todo)
7211 "Return the heading of the current entry, without the stars.
7212 When NO-TAGS is non-nil, don't include tags.
7213 When NO-TODO is non-nil, don't include TODO keywords."
7215 (org-back-to-heading t)
7217 ((and no-tags no-todo)
7218 (looking-at org-complex-heading-regexp)
7221 (looking-at (concat org-outline-regexp
7223 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7226 (looking-at org-todo-line-regexp)
7228 (t (looking-at org-heading-regexp)
7229 (match-string 2)))))
7231 (defun org-heading-components ()
7232 "Return the components of the current heading.
7233 This is a list with the following elements:
7234 - the level as an integer
7235 - the reduced level, different if `org-odd-levels-only' is set.
7236 - the TODO keyword, or nil
7237 - the priority character, like ?A, or nil if no priority is given
7238 - the headline text itself, or the tags string if no headline text
7239 - the tags string, or nil."
7241 (org-back-to-heading t)
7242 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7243 (list (length (match-string 1))
7244 (org-reduced-level (length (match-string 1)))
7245 (org-match-string-no-properties 2)
7246 (and (match-end 3) (aref (match-string 3) 2))
7247 (org-match-string-no-properties 4)
7248 (org-match-string-no-properties 5)))))
7250 (defun org-get-entry ()
7251 "Get the entry text, after heading, entire subtree."
7253 (org-back-to-heading t)
7254 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7256 (defun org-insert-heading-after-current ()
7257 "Insert a new heading with same level as current, after current subtree."
7259 (org-back-to-heading)
7260 (org-insert-heading)
7261 (org-move-subtree-down)
7264 (defun org-insert-heading-respect-content ()
7266 (let ((org-insert-heading-respect-content t))
7267 (org-insert-heading t)))
7269 (defun org-insert-todo-heading-respect-content (&optional force-state)
7271 (let ((org-insert-heading-respect-content t))
7272 (org-insert-todo-heading force-state t)))
7274 (defun org-insert-todo-heading (arg &optional force-heading)
7275 "Insert a new heading with the same level and TODO state as current heading.
7276 If the heading has no TODO state, or if the state is DONE, use the first
7277 state (TODO by default). Also with prefix arg, force first state."
7279 (when (or force-heading (not (org-insert-item 'checkbox)))
7280 (org-insert-heading force-heading)
7282 (org-back-to-heading)
7283 (outline-previous-heading)
7284 (looking-at org-todo-line-regexp))
7288 (not (match-beginning 2))
7289 (member (match-string 2) org-done-keywords))
7290 (car org-todo-keywords-1)
7294 (run-hook-with-args-until-success
7295 'org-todo-get-default-hook new-mark-x nil)
7297 (beginning-of-line 1)
7298 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7299 (if org-treat-insert-todo-heading-as-state-change
7301 (insert new-mark " "))))
7302 (when org-provide-todo-statistics
7303 (org-update-parent-todo-statistics))))
7305 (defun org-insert-subheading (arg)
7306 "Insert a new subheading and demote it.
7307 Works for outline headings and for plain lists alike."
7309 (org-insert-heading arg)
7311 ((org-at-heading-p) (org-do-demote))
7312 ((org-at-item-p) (org-indent-item))))
7314 (defun org-insert-todo-subheading (arg)
7315 "Insert a new subheading with TODO keyword or checkbox and demote it.
7316 Works for outline headings and for plain lists alike."
7318 (org-insert-todo-heading arg)
7320 ((org-at-heading-p) (org-do-demote))
7321 ((org-at-item-p) (org-indent-item))))
7323 ;;; Promotion and Demotion
7325 (defvar org-after-demote-entry-hook nil
7326 "Hook run after an entry has been demoted.
7327 The cursor will be at the beginning of the entry.
7328 When a subtree is being demoted, the hook will be called for each node.")
7330 (defvar org-after-promote-entry-hook nil
7331 "Hook run after an entry has been promoted.
7332 The cursor will be at the beginning of the entry.
7333 When a subtree is being promoted, the hook will be called for each node.")
7335 (defun org-promote-subtree ()
7336 "Promote the entire subtree.
7337 See also `org-promote'."
7340 (org-with-limited-levels (org-map-tree 'org-promote)))
7341 (org-fix-position-after-promote))
7343 (defun org-demote-subtree ()
7344 "Demote the entire subtree. See `org-demote'.
7345 See also `org-promote'."
7348 (org-with-limited-levels (org-map-tree 'org-demote)))
7349 (org-fix-position-after-promote))
7352 (defun org-do-promote ()
7353 "Promote the current heading higher up the tree.
7354 If the region is active in `transient-mark-mode', promote all headings
7358 (if (org-region-active-p)
7359 (org-map-region 'org-promote (region-beginning) (region-end))
7361 (org-fix-position-after-promote))
7363 (defun org-do-demote ()
7364 "Demote the current heading lower down the tree.
7365 If the region is active in `transient-mark-mode', demote all headings
7369 (if (org-region-active-p)
7370 (org-map-region 'org-demote (region-beginning) (region-end))
7372 (org-fix-position-after-promote))
7374 (defun org-fix-position-after-promote ()
7375 "Make sure that after pro/demotion cursor position is right."
7376 (let ((pos (point)))
7377 (when (save-excursion
7378 (beginning-of-line 1)
7379 (looking-at org-todo-line-regexp)
7380 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7381 (cond ((eobp) (insert " "))
7382 ((eolp) (insert " "))
7383 ((equal (char-after) ?\ ) (forward-char 1))))))
7385 (defun org-current-level ()
7386 "Return the level of the current entry, or nil if before the first headline.
7387 The level is the number of stars at the beginning of the headline."
7389 (org-with-limited-levels
7390 (if (ignore-errors (org-back-to-heading t))
7391 (funcall outline-level)))))
7393 (defun org-get-previous-line-level ()
7394 "Return the outline depth of the last headline before the current line.
7395 Returns 0 for the first headline in the buffer, and nil if before the
7397 (let ((current-level (org-current-level))
7398 (prev-level (when (> (line-number-at-pos) 1)
7400 (beginning-of-line 0)
7401 (org-current-level)))))
7402 (cond ((null current-level) nil) ; Before first headline
7403 ((null prev-level) 0) ; At first headline
7406 (defun org-reduced-level (l)
7407 "Compute the effective level of a heading.
7408 This takes into account the setting of `org-odd-levels-only'."
7411 (org-odd-levels-only (1+ (floor (/ l 2))))
7414 (defun org-level-increment ()
7415 "Return the number of stars that will be added or removed at a
7416 time to headlines when structure editing, based on the value of
7417 `org-odd-levels-only'."
7418 (if org-odd-levels-only 2 1))
7420 (defun org-get-valid-level (level &optional change)
7421 "Rectify a level change under the influence of `org-odd-levels-only'
7422 LEVEL is a current level, CHANGE is by how much the level should be
7423 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7424 even level numbers will become the next higher odd number."
7425 (if org-odd-levels-only
7426 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7427 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7428 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7429 (max 1 (+ level (or change 0)))))
7431 (if (boundp 'define-obsolete-function-alias)
7432 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7433 (define-obsolete-function-alias 'org-get-legal-level
7434 'org-get-valid-level)
7435 (define-obsolete-function-alias 'org-get-legal-level
7436 'org-get-valid-level "23.1")))
7438 (defun org-promote ()
7439 "Promote the current heading higher up the tree.
7440 If the region is active in `transient-mark-mode', promote all headings
7442 (org-back-to-heading t)
7443 (let* ((level (save-match-data (funcall outline-level)))
7444 (after-change-functions (remove 'flyspell-after-change-function
7445 after-change-functions))
7446 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7447 (diff (abs (- level (length up-head) -1))))
7448 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
7449 (replace-match up-head nil t)
7450 ;; Fixup tag positioning
7451 (and org-auto-align-tags (org-set-tags nil t))
7452 (if org-adapt-indentation (org-fixup-indentation (- diff)))
7453 (run-hooks 'org-after-promote-entry-hook)))
7455 (defun org-demote ()
7456 "Demote the current heading lower down the tree.
7457 If the region is active in `transient-mark-mode', demote all headings
7459 (org-back-to-heading t)
7460 (let* ((level (save-match-data (funcall outline-level)))
7461 (after-change-functions (remove 'flyspell-after-change-function
7462 after-change-functions))
7463 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7464 (diff (abs (- level (length down-head) -1))))
7465 (replace-match down-head nil t)
7466 ;; Fixup tag positioning
7467 (and org-auto-align-tags (org-set-tags nil t))
7468 (if org-adapt-indentation (org-fixup-indentation diff))
7469 (run-hooks 'org-after-demote-entry-hook)))
7471 (defun org-cycle-level ()
7472 "Cycle the level of an empty headline through possible states.
7473 This goes first to child, then to parent, level, then up the hierarchy.
7474 After top level, it switches back to sibling level."
7476 (let ((org-adapt-indentation nil))
7477 (when (org-point-at-end-of-empty-headline)
7478 (setq this-command 'org-cycle-level) ; Only needed for caching
7479 (let ((cur-level (org-current-level))
7480 (prev-level (org-get-previous-line-level)))
7482 ;; If first headline in file, promote to top-level.
7484 (loop repeat (/ (- cur-level 1) (org-level-increment))
7485 do (org-do-promote)))
7486 ;; If same level as prev, demote one.
7487 ((= prev-level cur-level)
7489 ;; If parent is top-level, promote to top level if not already.
7491 (loop repeat (/ (- cur-level 1) (org-level-increment))
7492 do (org-do-promote)))
7493 ;; If top-level, return to prev-level.
7495 (loop repeat (/ (- prev-level 1) (org-level-increment))
7496 do (org-do-demote)))
7497 ;; If less than prev-level, promote one.
7498 ((< cur-level prev-level)
7500 ;; If deeper than prev-level, promote until higher than
7502 ((> cur-level prev-level)
7503 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7504 do (org-do-promote))))
7507 (defun org-map-tree (fun)
7508 "Call FUN for every heading underneath the current one."
7509 (org-back-to-heading)
7510 (let ((level (funcall outline-level)))
7514 (outline-next-heading)
7515 (> (funcall outline-level) level))
7519 (defun org-map-region (fun beg end)
7520 "Call FUN for every heading between BEG and END."
7521 (let ((org-ignore-region t))
7523 (setq end (copy-marker end))
7525 (if (and (re-search-forward org-outline-regexp-bol nil t)
7529 (outline-next-heading)
7534 (defvar org-property-end-re) ; silence byte-compiler
7535 (defun org-fixup-indentation (diff)
7536 "Change the indentation in the current entry by DIFF.
7537 However, if any line in the current entry has no indentation, or if it
7538 would end up with no indentation after the change, nothing at all is done."
7540 (let ((end (save-excursion (outline-next-heading)
7542 (prohibit (if (> diff 0)
7544 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7546 (unless (save-excursion (end-of-line 1)
7547 (re-search-forward prohibit end t))
7548 (while (and (< (point) end)
7549 (re-search-forward "^[ \t]+" end t))
7550 (goto-char (match-end 0))
7551 (setq col (current-column))
7552 (if (< diff 0) (replace-match ""))
7553 (org-indent-to-column (+ diff col))))
7554 (move-marker end nil))))
7556 (defun org-convert-to-odd-levels ()
7557 "Convert an org-mode file with all levels allowed to one with odd levels.
7558 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7561 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7562 (let ((outline-level 'org-outline-level)
7563 (org-odd-levels-only nil) n)
7565 (goto-char (point-min))
7566 (while (re-search-forward "^\\*\\*+ " nil t)
7567 (setq n (- (length (match-string 0)) 2))
7568 (while (>= (setq n (1- n)) 0)
7570 (end-of-line 1))))))
7572 (defun org-convert-to-oddeven-levels ()
7573 "Convert an org-mode file with only odd levels to one with odd/even levels.
7574 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7575 file contains a section with an even level, conversion would
7576 destroy the structure of the file. An error is signaled in this
7579 (goto-char (point-min))
7580 ;; First check if there are no even levels
7581 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7582 (org-show-context t)
7583 (error "Not all levels are odd in this file. Conversion not possible"))
7584 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7585 (let ((outline-regexp org-outline-regexp)
7586 (outline-level 'org-outline-level)
7587 (org-odd-levels-only nil) n)
7589 (goto-char (point-min))
7590 (while (re-search-forward "^\\*\\*+ " nil t)
7591 (setq n (/ (1- (length (match-string 0))) 2))
7592 (while (>= (setq n (1- n)) 0)
7594 (end-of-line 1))))))
7596 (defun org-tr-level (n)
7597 "Make N odd if required."
7598 (if org-odd-levels-only (1+ (/ n 2)) n))
7600 ;;; Vertical tree motion, cutting and pasting of subtrees
7602 (defun org-move-subtree-up (&optional arg)
7603 "Move the current subtree up past ARG headlines of the same level."
7605 (org-move-subtree-down (- (prefix-numeric-value arg))))
7607 (defun org-move-subtree-down (&optional arg)
7608 "Move the current subtree down past ARG headlines of the same level."
7610 (setq arg (prefix-numeric-value arg))
7611 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7612 'org-get-last-sibling))
7613 (ins-point (make-marker))
7615 (col (current-column))
7616 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7618 (org-back-to-heading)
7621 (setq ne-beg (org-back-over-empty-lines))
7624 (save-excursion (outline-end-of-heading)
7625 (setq folded (outline-invisible-p)))
7626 (outline-end-of-subtree))
7627 (outline-next-heading)
7628 (setq ne-end (org-back-over-empty-lines))
7631 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7632 ;; include less whitespace
7635 (forward-line (- ne-beg ne-end))
7636 (setq beg (point))))
7637 ;; Find insertion point, with error handling
7639 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7640 (progn (goto-char beg0)
7641 (error "Cannot move past superior level or buffer limit")))
7642 (setq cnt (1- cnt)))
7644 ;; Moving forward - still need to move over subtree
7645 (progn (org-end-of-subtree t t)
7647 (org-back-over-empty-lines)
7648 (or (bolp) (newline)))))
7649 (setq ne-ins (org-back-over-empty-lines))
7650 (move-marker ins-point (point))
7651 (setq txt (buffer-substring beg end))
7652 (org-save-markers-in-region beg end)
7653 (delete-region beg end)
7654 (org-remove-empty-overlays-at beg)
7655 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7656 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7657 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7658 (let ((bbb (point)))
7659 (insert-before-markers txt)
7660 (org-reinstall-markers-in-region bbb)
7661 (move-marker ins-point bbb))
7662 (or (bolp) (insert "\n"))
7663 (setq ins-end (point))
7664 (goto-char ins-point)
7665 (org-skip-whitespace)
7666 (when (and (< arg 0)
7667 (org-first-sibling-p)
7669 ;; Move whitespace back to beginning
7672 (let ((kill-whole-line t))
7673 (kill-line (- ne-ins ne-beg)) (point)))
7674 (insert (make-string (- ne-ins ne-beg) ?\n)))
7675 (move-marker ins-point nil)
7680 (org-cycle-hide-drawers 'children))
7681 (org-clean-visibility-after-subtree-move)
7682 ;; move back to the initial column we were at
7683 (move-to-column col)))
7685 (defvar org-subtree-clip ""
7686 "Clipboard for cut and paste of subtrees.
7687 This is actually only a copy of the kill, because we use the normal kill
7688 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7690 (defvar org-subtree-clip-folded nil
7691 "Was the last copied subtree folded?
7692 This is used to fold the tree back after pasting.")
7694 (defun org-cut-subtree (&optional n)
7695 "Cut the current subtree into the clipboard.
7696 With prefix arg N, cut this many sequential subtrees.
7697 This is a short-hand for marking the subtree and then cutting it."
7699 (org-copy-subtree n 'cut))
7701 (defun org-copy-subtree (&optional n cut force-store-markers)
7702 "Cut the current subtree into the clipboard.
7703 With prefix arg N, cut this many sequential subtrees.
7704 This is a short-hand for marking the subtree and then copying it.
7705 If CUT is non-nil, actually cut the subtree.
7706 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7707 of some markers in the region, even if CUT is non-nil. This is
7708 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7710 (let (beg end folded (beg0 (point)))
7711 (if (org-called-interactively-p 'any)
7712 (org-back-to-heading nil) ; take what looks like a subtree
7713 (org-back-to-heading t)) ; take what is really there
7714 (org-back-over-empty-lines)
7716 (skip-chars-forward " \t\r\n")
7718 (save-excursion (outline-end-of-heading)
7719 (setq folded (outline-invisible-p)))
7721 (org-forward-same-level (1- n) t)
7723 (org-end-of-subtree t t))
7724 (org-back-over-empty-lines)
7728 (setq org-subtree-clip-folded folded)
7729 (when (or cut force-store-markers)
7730 (org-save-markers-in-region beg end))
7731 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7732 (setq org-subtree-clip (current-kill 0))
7733 (message "%s: Subtree(s) with %d characters"
7734 (if cut "Cut" "Copied")
7735 (length org-subtree-clip)))))
7737 (defun org-paste-subtree (&optional level tree for-yank)
7738 "Paste the clipboard as a subtree, with modification of headline level.
7739 The entire subtree is promoted or demoted in order to match a new headline
7742 If the cursor is at the beginning of a headline, the same level as
7743 that headline is used to paste the tree
7745 If not, the new level is derived from the *visible* headings
7746 before and after the insertion point, and taken to be the inferior headline
7747 level of the two. So if the previous visible heading is level 3 and the
7748 next is level 4 (or vice versa), level 4 will be used for insertion.
7749 This makes sure that the subtree remains an independent subtree and does
7750 not swallow low level entries.
7752 You can also force a different level, either by using a numeric prefix
7753 argument, or by inserting the heading marker by hand. For example, if the
7754 cursor is after \"*****\", then the tree will be shifted to level 5.
7756 If optional TREE is given, use this text instead of the kill ring.
7758 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7759 move back over whitespace before inserting, and move point to the end of
7760 the inserted text when done."
7762 (setq tree (or tree (and kill-ring (current-kill 0))))
7763 (unless (org-kill-is-subtree-p tree)
7765 (substitute-command-keys
7766 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7767 (org-with-limited-levels
7768 (let* ((visp (not (outline-invisible-p)))
7770 (^re_ "\\(\\*+\\)[ \t]*")
7771 (old-level (if (string-match org-outline-regexp-bol txt)
7772 (- (match-end 0) (match-beginning 0) 1)
7774 (force-level (cond (level (prefix-numeric-value level))
7775 ((and (looking-at "[ \t]*$")
7777 "^\\*+$" (buffer-substring
7778 (point-at-bol) (point))))
7779 (- (match-end 1) (match-beginning 1)))
7781 (looking-at org-outline-regexp))
7782 (- (match-end 0) (point) 1))
7784 (previous-level (save-excursion
7787 (outline-previous-visible-heading 1)
7788 (if (looking-at ^re_)
7789 (- (match-end 0) (match-beginning 0) 1)
7792 (next-level (save-excursion
7795 (or (looking-at org-outline-regexp)
7796 (outline-next-visible-heading 1))
7797 (if (looking-at ^re_)
7798 (- (match-end 0) (match-beginning 0) 1)
7801 (new-level (or force-level (max previous-level next-level)))
7802 (shift (if (or (= old-level -1)
7804 (= old-level new-level))
7806 (- new-level old-level)))
7807 (delta (if (> shift 0) -1 1))
7808 (func (if (> shift 0) 'org-demote 'org-promote))
7809 (org-odd-levels-only nil)
7811 ;; Remove the forced level indicator
7813 (delete-region (point-at-bol) (point)))
7815 (beginning-of-line (if (bolp) 1 2))
7816 (unless for-yank (org-back-over-empty-lines))
7818 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7819 (insert-before-markers txt)
7820 (unless (string-match "\n\\'" txt) (insert "\n"))
7821 (setq newend (point))
7822 (org-reinstall-markers-in-region beg)
7825 (skip-chars-forward " \t\n\r")
7827 (if (and (outline-invisible-p) visp)
7828 (save-excursion (outline-show-heading)))
7829 ;; Shift if necessary
7832 (narrow-to-region beg end)
7833 (while (not (= shift 0))
7834 (org-map-region func (point-min) (point-max))
7835 (setq shift (+ delta shift)))
7836 (goto-char (point-min))
7837 (setq newend (point-max))))
7838 (when (or (org-called-interactively-p 'interactive) for-yank)
7839 (message "Clipboard pasted as level %d subtree" new-level))
7840 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7842 (eq org-subtree-clip (current-kill 0))
7843 org-subtree-clip-folded)
7844 ;; The tree was folded before it was killed/copied
7846 (and for-yank (goto-char newend)))))
7848 (defun org-kill-is-subtree-p (&optional txt)
7849 "Check if the current kill is an outline subtree, or a set of trees.
7850 Returns nil if kill does not start with a headline, or if the first
7851 headline level is not the largest headline level in the tree.
7852 So this will actually accept several entries of equal levels as well,
7853 which is OK for `org-paste-subtree'.
7854 If optional TXT is given, check this string instead of the current kill."
7855 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7856 (re (org-get-limited-outline-regexp))
7857 (^re (concat "^" re))
7858 (start-level (and kill
7860 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7862 (- (match-end 2) (match-beginning 2) 1)))
7863 (start (1+ (or (match-beginning 2) -1))))
7864 (if (not start-level)
7866 nil) ;; does not even start with a heading
7868 (while (setq start (string-match ^re kill (1+ start)))
7869 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7873 (defvar org-markers-to-move nil
7874 "Markers that should be moved with a cut-and-paste operation.
7875 Those markers are stored together with their positions relative to
7876 the start of the region.")
7878 (defun org-save-markers-in-region (beg end)
7879 "Check markers in region.
7880 If these markers are between BEG and END, record their position relative
7881 to BEG, so that after moving the block of text, we can put the markers back
7883 This function gets called just before an entry or tree gets cut from the
7884 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7885 called immediately, to move the markers with the entries."
7886 (setq org-markers-to-move nil)
7887 (when (featurep 'org-clock)
7888 (org-clock-save-markers-for-cut-and-paste beg end))
7889 (when (featurep 'org-agenda)
7890 (org-agenda-save-markers-for-cut-and-paste beg end)))
7892 (defun org-check-and-save-marker (marker beg end)
7893 "Check if MARKER is between BEG and END.
7894 If yes, remember the marker and the distance to BEG."
7895 (when (and (marker-buffer marker)
7896 (equal (marker-buffer marker) (current-buffer)))
7897 (if (and (>= marker beg) (< marker end))
7898 (push (cons marker (- marker beg)) org-markers-to-move))))
7900 (defun org-reinstall-markers-in-region (beg)
7901 "Move all remembered markers to their position relative to BEG."
7903 (move-marker (car x) (+ beg (cdr x))))
7904 org-markers-to-move)
7905 (setq org-markers-to-move nil))
7907 (defun org-narrow-to-subtree ()
7908 "Narrow buffer to the current subtree."
7912 (org-with-limited-levels
7914 (progn (org-back-to-heading t) (point))
7915 (progn (org-end-of-subtree t t)
7916 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
7919 (defun org-narrow-to-block ()
7920 "Narrow buffer to the current block."
7922 (let* ((case-fold-search t)
7923 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
7924 "^[ \t]*#\\+end_.*")))
7926 (narrow-to-region (car blockp) (cdr blockp))
7927 (error "Not in a block"))))
7930 (defvar org-property-drawer-re))
7932 (defvar org-property-start-re) ;; defined below
7933 (defun org-clone-subtree-with-time-shift (n &optional shift)
7934 "Clone the task (subtree) at point N times.
7935 The clones will be inserted as siblings.
7937 In interactive use, the user will be prompted for the number of
7938 clones to be produced, and for a time SHIFT, which may be a
7939 repeater as used in time stamps, for example `+3d'.
7941 When a valid repeater is given and the entry contains any time
7942 stamps, the clones will become a sequence in time, with time
7943 stamps in the subtree shifted for each clone produced. If SHIFT
7944 is nil or the empty string, time stamps will be left alone. The
7945 ID property of the original subtree is removed.
7947 If the original subtree did contain time stamps with a repeater,
7948 the following will happen:
7949 - the repeater will be removed in each clone
7950 - an additional clone will be produced, with the current, unshifted
7951 date(s) in the entry.
7952 - the original entry will be placed *after* all the clones, with
7954 - the start days in the repeater in the original entry will be shifted
7955 to past the last clone.
7956 In this way you can spell out a number of instances of a repeating task,
7957 and still retain the repeater to cover future instances of the task."
7958 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7959 (let (beg end template task idprop
7960 shift-n shift-what doshift nmin nmax (n-no-remove -1)
7961 (drawer-re org-drawer-regexp))
7962 (if (not (and (integerp n) (> n 0)))
7963 (error "Invalid number of replications %s" n))
7964 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7965 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
7967 (error "Invalid shift specification %s" shift))
7969 (setq shift-n (string-to-number (match-string 1 shift))
7970 shift-what (cdr (assoc (match-string 2 shift)
7971 '(("d" . day) ("w" . week)
7972 ("m" . month) ("y" . year))))))
7973 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7974 (setq nmin 1 nmax n)
7975 (org-back-to-heading t)
7977 (setq idprop (org-entry-get nil "ID"))
7978 (org-end-of-subtree t t)
7979 (or (bolp) (insert "\n"))
7981 (setq template (buffer-substring beg end))
7983 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[dwmy][^<>\n]*>" template))
7984 (delete-region beg end)
7986 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7988 (loop for n from nmin to nmax do
7993 (goto-char (point-min))
7995 (and idprop (if org-clone-delete-id
7996 (org-entry-delete nil "ID")
7997 (org-id-get-create t)))
7999 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8001 (goto-char (point-min))
8002 (while (re-search-forward drawer-re nil t)
8004 (org-remove-empty-drawer-at d (point))) org-drawers)))
8005 (goto-char (point-min))
8007 (while (re-search-forward org-ts-regexp-both nil t)
8008 (org-timestamp-change (* n shift-n) shift-what))
8009 (unless (= n n-no-remove)
8010 (goto-char (point-min))
8011 (while (re-search-forward org-ts-regexp nil t)
8013 (goto-char (match-beginning 0))
8014 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[dwmy]\\)")
8015 (delete-region (match-beginning 1) (match-end 1)))))))
8016 (setq task (buffer-string)))
8022 (defun org-sort (with-case)
8023 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8024 Optional argument WITH-CASE means sort case-sensitively."
8027 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8028 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8030 (org-call-with-arg 'org-sort-entries with-case))))
8032 (defun org-sort-remove-invisible (s)
8033 (remove-text-properties 0 (length s) org-rm-props s)
8034 (while (string-match org-bracket-link-regexp s)
8035 (setq s (replace-match (if (match-end 2)
8037 (match-string 1 s)) t t s)))
8040 (defvar org-priority-regexp) ; defined later in the file
8042 (defvar org-after-sorting-entries-or-items-hook nil
8043 "Hook that is run after a bunch of entries or items have been sorted.
8044 When children are sorted, the cursor is in the parent line when this
8045 hook gets called. When a region or a plain list is sorted, the cursor
8046 will be in the first entry of the sorted region/list.")
8048 (defun org-sort-entries
8049 (&optional with-case sorting-type getkey-func compare-func property)
8050 "Sort entries on a certain level of an outline tree.
8051 If there is an active region, the entries in the region are sorted.
8052 Else, if the cursor is before the first entry, sort the top-level items.
8053 Else, the children of the entry at point are sorted.
8055 Sorting can be alphabetically, numerically, by date/time as given by
8056 a time stamp, by a property or by priority.
8058 The command prompts for the sorting type unless it has been given to the
8059 function through the SORTING-TYPE argument, which needs to be a character,
8060 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
8061 precise meaning of each character:
8063 n Numerically, by converting the beginning of the entry/item to a number.
8064 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8065 t By date/time, either the first active time stamp in the entry, or, if
8066 none exist, by the first inactive one.
8067 s By the scheduled date/time.
8068 d By deadline date/time.
8069 c By creation time, which is assumed to be the first inactive time stamp
8070 at the beginning of a line.
8071 p By priority according to the cookie.
8072 r By the value of a property.
8074 Capital letters will reverse the sort order.
8076 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8077 called with point at the beginning of the record. It must return either
8078 a string or a number that should serve as the sorting key for that record.
8080 Comparing entries ignores case by default. However, with an optional argument
8081 WITH-CASE, the sorting considers case as well."
8083 (let ((case-func (if with-case 'identity 'downcase))
8084 start beg end stars re re2
8086 ;; Find beginning and end of region to sort
8088 ((org-region-active-p)
8089 ;; we will sort the region
8090 (setq end (region-end)
8092 (goto-char (region-beginning))
8093 (if (not (org-at-heading-p)) (outline-next-heading))
8094 (setq start (point)))
8095 ((or (org-at-heading-p)
8096 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8097 ;; we will sort the children of the current headline
8098 (org-back-to-heading)
8100 end (progn (org-end-of-subtree t t)
8101 (or (bolp) (insert "\n"))
8102 (org-back-over-empty-lines)
8107 (outline-next-heading))
8109 ;; we will sort the top-level entries in this file
8110 (goto-char (point-min))
8111 (or (org-at-heading-p) (outline-next-heading))
8112 (setq start (point))
8113 (goto-char (point-max))
8114 (beginning-of-line 1)
8115 (when (looking-at ".*?\\S-")
8116 ;; File ends in a non-white line
8119 (setq end (point-max))
8120 (setq what "top-level")
8125 (if (>= beg end) (error "Nothing to sort"))
8127 (looking-at "\\(\\*+\\)")
8128 (setq stars (match-string 1)
8129 re (concat "^" (regexp-quote stars) " +")
8130 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8131 txt (buffer-substring beg end))
8132 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8133 (if (and (not (equal stars "*")) (string-match re2 txt))
8134 (error "Region to sort contains a level above the first entry"))
8136 (unless sorting-type
8138 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8139 [t]ime
[s]cheduled [d]eadline [c]reated
8140 A/N/T/S/D/C/P/O/F means reversed:"
8142 (setq sorting-type (read-char-exclusive))
8144 (and (= (downcase sorting-type) ?f)
8146 (org-icompleting-read "Sort using function: "
8147 obarray 'fboundp t nil nil))
8148 (setq getkey-func (intern getkey-func)))
8150 (and (= (downcase sorting-type) ?r)
8152 (org-icompleting-read "Property: "
8153 (mapcar 'list (org-buffer-property-keys t))
8156 (message "Sorting entries...")
8159 (narrow-to-region start end)
8160 (let ((dcst (downcase sorting-type))
8161 (case-fold-search nil)
8162 (now (current-time)))
8164 (/= dcst sorting-type)
8165 ;; This function moves to the beginning character of the "record" to
8168 (if (re-search-forward re nil t)
8169 (goto-char (match-beginning 0))
8170 (goto-char (point-max))))
8171 ;; This function moves to the last character of the "record" being
8176 (outline-forward-same-level 1)
8178 (goto-char (point-max))))))
8179 ;; This function returns the value that gets sorted against.
8183 (if (looking-at org-complex-heading-regexp)
8184 (string-to-number (match-string 4))
8187 (if (looking-at org-complex-heading-regexp)
8188 (funcall case-func (match-string 4))
8191 (let ((end (save-excursion (outline-next-heading) (point))))
8192 (if (or (re-search-forward org-ts-regexp end t)
8193 (re-search-forward org-ts-regexp-both end t))
8194 (org-time-string-to-seconds (match-string 0))
8195 (org-float-time now))))
8197 (let ((end (save-excursion (outline-next-heading) (point))))
8198 (if (re-search-forward
8199 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8201 (org-time-string-to-seconds (match-string 0))
8202 (org-float-time now))))
8204 (let ((end (save-excursion (outline-next-heading) (point))))
8205 (if (re-search-forward org-scheduled-time-regexp end t)
8206 (org-time-string-to-seconds (match-string 1))
8207 (org-float-time now))))
8209 (let ((end (save-excursion (outline-next-heading) (point))))
8210 (if (re-search-forward org-deadline-time-regexp end t)
8211 (org-time-string-to-seconds (match-string 1))
8212 (org-float-time now))))
8214 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8215 (string-to-char (match-string 2))
8216 org-default-priority))
8218 (or (org-entry-get nil property) ""))
8220 (if (looking-at org-complex-heading-regexp)
8221 (- 9999 (length (member (match-string 2)
8222 org-todo-keywords-1)))))
8226 (setq tmp (funcall getkey-func))
8227 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8229 (error "Invalid key function `%s'" getkey-func)))
8230 (t (error "Invalid sorting type `%c'" sorting-type))))
8233 ((= dcst ?a) 'string<)
8234 ((= dcst ?f) compare-func)
8235 ((member dcst '(?p ?t ?s ?d ?c)) '<)
8237 (run-hooks 'org-after-sorting-entries-or-items-hook)
8238 (message "Sorting entries...done")))
8240 (defun org-do-sort (table what &optional with-case sorting-type)
8241 "Sort TABLE of WHAT according to SORTING-TYPE.
8242 The user will be prompted for the SORTING-TYPE if the call to this
8243 function does not specify it. WHAT is only for the prompt, to indicate
8244 what is being sorted. The sorting key will be extracted from
8245 the car of the elements of the table.
8246 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8247 (unless sorting-type
8249 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
8251 (setq sorting-type (read-char-exclusive)))
8252 (let ((dcst (downcase sorting-type))
8253 extractfun comparefun)
8254 ;; Define the appropriate functions
8257 (setq extractfun 'string-to-number
8258 comparefun (if (= dcst sorting-type) '< '>)))
8260 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8261 (lambda(x) (downcase (org-sort-remove-invisible x))))
8262 comparefun (if (= dcst sorting-type)
8264 (lambda (a b) (and (not (string< a b))
8265 (not (string= a b)))))))
8269 (if (or (string-match org-ts-regexp x)
8270 (string-match org-ts-regexp-both x))
8272 (org-time-string-to-time (match-string 0 x)))
8274 comparefun (if (= dcst sorting-type) '< '>)))
8275 (t (error "Invalid sorting type `%c'" sorting-type)))
8277 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8279 (lambda (a b) (funcall comparefun (car a) (car b))))))
8282 ;;; The orgstruct minor mode
8284 ;; Define a minor mode which can be used in other modes in order to
8285 ;; integrate the org-mode structure editing commands.
8287 ;; This is really a hack, because the org-mode structure commands use
8288 ;; keys which normally belong to the major mode. Here is how it
8289 ;; works: The minor mode defines all the keys necessary to operate the
8290 ;; structure commands, but wraps the commands into a function which
8291 ;; tests if the cursor is currently at a headline or a plain list
8292 ;; item. If that is the case, the structure command is used,
8293 ;; temporarily setting many Org-mode variables like regular
8294 ;; expressions for filling etc. However, when any of those keys is
8295 ;; used at a different location, function uses `key-binding' to look
8296 ;; up if the key has an associated command in another currently active
8297 ;; keymap (minor modes, major mode, global), and executes that
8298 ;; command. There might be problems if any of the keys is otherwise
8299 ;; used as a prefix key.
8301 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8302 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8303 ;; addresses this by checking explicitly for both bindings.
8305 (defvar orgstruct-mode-map (make-sparse-keymap)
8306 "Keymap for the minor `orgstruct-mode'.")
8308 (defvar org-local-vars nil
8309 "List of local variables, for use by `orgstruct-mode'.")
8312 (define-minor-mode orgstruct-mode
8313 "Toggle the minor mode `orgstruct-mode'.
8314 This mode is for using Org-mode structure commands in other
8315 modes. The following keys behave as if Org-mode were active, if
8316 the cursor is on a headline, or on a plain list item (both as
8317 defined by Org-mode).
8319 M-up Move entry/item up
8320 M-down Move entry/item down
8323 M-S-up Move entry/item up
8324 M-S-down Move entry/item down
8325 M-S-left Promote subtree
8326 M-S-right Demote subtree
8327 M-q Fill paragraph and items like in Org-mode
8329 C-c - Cycle list bullet
8330 TAB Cycle item visibility
8331 M-RET Insert new heading/item
8332 S-M-RET Insert new TODO heading / Checkbox item
8333 C-c C-c Set tags / toggle checkbox"
8334 nil " OrgStruct" nil
8335 (org-load-modules-maybe)
8336 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8339 (defun turn-on-orgstruct ()
8340 "Unconditionally turn on `orgstruct-mode'."
8343 (defun orgstruct++-mode (&optional arg)
8344 "Toggle `orgstruct-mode', the enhanced version of it.
8345 In addition to setting orgstruct-mode, this also exports all indentation
8346 and autofilling variables from org-mode into the buffer. It will also
8347 recognize item context in multiline items.
8348 Note that turning off orgstruct-mode will *not* remove the
8349 indentation/paragraph settings. This can only be done by refreshing the
8350 major mode, for example with \\[normal-mode]."
8352 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8360 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8361 (symbol-name (car x)))
8362 (setq var (car x) val (nth 1 x))
8363 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8365 (org-set-local 'orgstruct-is-++ t))))
8367 (defvar orgstruct-is-++ nil
8368 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8369 (make-variable-buffer-local 'orgstruct-is-++)
8372 (defun turn-on-orgstruct++ ()
8373 "Unconditionally turn on `orgstruct++-mode'."
8374 (orgstruct++-mode 1))
8376 (defun orgstruct-error ()
8377 "Error when there is no default binding for a structure key."
8379 (error "This key has no function outside structure elements"))
8381 (defun orgstruct-setup ()
8382 "Setup orgstruct keymaps."
8386 '([(meta up)] org-metaup)
8387 '([(meta down)] org-metadown)
8388 '([(meta left)] org-metaleft)
8389 '([(meta right)] org-metaright)
8390 '([(meta shift up)] org-shiftmetaup)
8391 '([(meta shift down)] org-shiftmetadown)
8392 '([(meta shift left)] org-shiftmetaleft)
8393 '([(meta shift right)] org-shiftmetaright)
8394 '([?\e (up)] org-metaup)
8395 '([?\e (down)] org-metadown)
8396 '([?\e (left)] org-metaleft)
8397 '([?\e (right)] org-metaright)
8398 '([?\e (shift up)] org-shiftmetaup)
8399 '([?\e (shift down)] org-shiftmetadown)
8400 '([?\e (shift left)] org-shiftmetaleft)
8401 '([?\e (shift right)] org-shiftmetaright)
8402 '([(shift up)] org-shiftup)
8403 '([(shift down)] org-shiftdown)
8404 '([(shift left)] org-shiftleft)
8405 '([(shift right)] org-shiftright)
8406 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8407 '("\M-q" fill-paragraph)
8409 '("\C-c-" org-cycle-list-bullet)))
8411 (while (setq elt (pop bindings))
8412 (setq nfunc (1+ nfunc))
8413 (setq key (org-key (car elt))
8415 cmd (orgstruct-make-binding fun nfunc key))
8416 (org-defkey orgstruct-mode-map key cmd))
8418 ;; Special treatment needed for TAB and RET
8419 (org-defkey orgstruct-mode-map [(tab)]
8420 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8421 (org-defkey orgstruct-mode-map "\C-i"
8422 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8424 (org-defkey orgstruct-mode-map "\M-\C-m"
8425 (orgstruct-make-binding 'org-insert-heading 105
8426 "\M-\C-m" [(meta return)]))
8427 (org-defkey orgstruct-mode-map [(meta return)]
8428 (orgstruct-make-binding 'org-insert-heading 106
8429 [(meta return)] "\M-\C-m"))
8431 (org-defkey orgstruct-mode-map [(shift meta return)]
8432 (orgstruct-make-binding 'org-insert-todo-heading 107
8433 [(meta return)] "\M-\C-m"))
8435 (org-defkey orgstruct-mode-map "\e\C-m"
8436 (orgstruct-make-binding 'org-insert-heading 108
8437 "\e\C-m" [?\e (return)]))
8438 (org-defkey orgstruct-mode-map [?\e (return)]
8439 (orgstruct-make-binding 'org-insert-heading 109
8440 [?\e (return)] "\e\C-m"))
8441 (org-defkey orgstruct-mode-map [?\e (shift return)]
8442 (orgstruct-make-binding 'org-insert-todo-heading 110
8443 [?\e (return)] "\e\C-m"))
8445 (unless org-local-vars
8446 (setq org-local-vars (org-get-local-variables)))
8450 (defun orgstruct-make-binding (fun n &rest keys)
8451 "Create a function for binding in the structure minor mode.
8452 FUN is the command to call inside a table. N is used to create a unique
8453 command name. KEYS are keys that should be checked in for a command
8454 to execute outside of tables."
8457 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8459 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8460 "Outside of structure, run the binding of `"
8461 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8465 `(org-context-p 'headline 'item
8466 (and orgstruct-is-++
8467 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8469 (list 'org-run-like-in-org-mode (list 'quote fun))
8470 (list 'let '(orgstruct-mode)
8471 (list 'call-interactively
8474 (list 'key-binding k))
8476 '('orgstruct-error))))))))
8478 (defun org-context-p (&rest contexts)
8479 "Check if local context is any of CONTEXTS.
8480 Possible values in the list of contexts are `table', `headline', and `item'."
8481 (let ((pos (point)))
8482 (goto-char (point-at-bol))
8483 (prog1 (or (and (memq 'table contexts)
8484 (looking-at "[ \t]*|"))
8485 (and (memq 'headline contexts)
8486 (looking-at org-outline-regexp))
8487 (and (memq 'item contexts)
8488 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8489 (and (memq 'item-body contexts)
8493 (defun org-get-local-variables ()
8494 "Return a list of all local variables in an org-mode buffer."
8496 (with-current-buffer (get-buffer-create "*Org tmp*")
8499 (setq varlist (buffer-local-variables)))
8500 (kill-buffer "*Org tmp*")
8507 (list (car x) (list 'quote (cdr x)))))
8509 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8510 (symbol-name (car x)))
8514 (defun org-clone-local-variables (from-buffer &optional regexp)
8515 "Clone local variables from FROM-BUFFER.
8516 Optional argument REGEXP selects variables to clone."
8519 (and (symbolp (car pair))
8521 (string-match regexp (symbol-name (car pair))))
8522 (set (make-local-variable (car pair))
8524 (buffer-local-variables from-buffer)))
8527 (defun org-run-like-in-org-mode (cmd)
8528 "Run a command, pretending that the current buffer is in Org-mode.
8529 This will temporarily bind local variables that are typically bound in
8530 Org-mode to the values they have in Org-mode, and then interactively
8532 (org-load-modules-maybe)
8533 (unless org-local-vars
8534 (setq org-local-vars (org-get-local-variables)))
8535 (eval (list 'let org-local-vars
8536 (list 'call-interactively (list 'quote cmd)))))
8540 (defun org-get-category (&optional pos force-refresh)
8541 "Get the category applying to position POS."
8543 (if force-refresh (org-refresh-category-properties))
8544 (let ((pos (or pos (point))))
8545 (or (get-text-property pos 'org-category)
8546 (progn (org-refresh-category-properties)
8547 (get-text-property pos 'org-category))))))
8549 (defun org-refresh-category-properties ()
8550 "Refresh category text properties in the buffer."
8551 (let ((def-cat (cond
8552 ((null org-category)
8553 (if buffer-file-name
8554 (file-name-sans-extension
8555 (file-name-nondirectory buffer-file-name))
8557 ((symbolp org-category) (symbol-name org-category))
8559 beg end cat pos optionp)
8564 (goto-char (point-min))
8565 (put-text-property (point) (point-max) 'org-category def-cat)
8566 (while (re-search-forward
8567 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8568 (setq pos (match-end 0)
8569 optionp (equal (char-after (match-beginning 0)) ?#)
8570 cat (org-trim (match-string 2)))
8572 (setq beg (point-at-bol) end (point-max))
8573 (org-back-to-heading t)
8574 (setq beg (point) end (org-end-of-subtree t t)))
8575 (put-text-property beg end 'org-category cat)
8576 (put-text-property beg end 'org-category-position beg)
8577 (goto-char pos)))))))
8582 ;;; Link abbreviations
8584 (defun org-link-expand-abbrev (link)
8585 "Apply replacements as defined in `org-link-abbrev-alist."
8586 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8587 (let* ((key (match-string 1 link))
8588 (as (or (assoc key org-link-abbrev-alist-local)
8589 (assoc key org-link-abbrev-alist)))
8590 (tag (and (match-end 2) (match-string 3 link)))
8596 ((symbolp rpl) (funcall rpl tag))
8597 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8598 ((string-match "%h" rpl)
8599 (replace-match (url-hexify-string (or tag "")) t t rpl))
8600 (t (concat rpl tag)))))
8603 ;;; Storing and inserting links
8605 (defvar org-insert-link-history nil
8606 "Minibuffer history for links inserted with `org-insert-link'.")
8608 (defvar org-stored-links nil
8609 "Contains the links stored with `org-store-link'.")
8611 (defvar org-store-link-plist nil
8612 "Plist with info about the most recently link created with `org-store-link'.")
8614 (defvar org-link-protocols nil
8615 "Link protocols added to Org-mode using `org-add-link-type'.")
8617 (defvar org-store-link-functions nil
8618 "List of functions that are called to create and store a link.
8619 Each function will be called in turn until one returns a non-nil
8620 value. Each function should check if it is responsible for creating
8621 this link (for example by looking at the major mode).
8622 If not, it must exit and return nil.
8623 If yes, it should return a non-nil value after a calling
8624 `org-store-link-props' with a list of properties and values.
8625 Special properties are:
8627 :type The link prefix, like \"http\". This must be given.
8628 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8629 This is obligatory as well.
8630 :description Optional default description for the second pair
8631 of brackets in an Org-mode link. The user can still change
8632 this when inserting this link into an Org-mode buffer.
8634 In addition to these, any additional properties can be specified
8635 and then used in remember templates.")
8637 (defun org-add-link-type (type &optional follow export)
8638 "Add TYPE to the list of `org-link-types'.
8639 Re-compute all regular expressions depending on `org-link-types'
8641 FOLLOW and EXPORT are two functions.
8643 FOLLOW should take the link path as the single argument and do whatever
8644 is necessary to follow the link, for example find a file or display
8647 EXPORT should format the link path for export to one of the export formats.
8648 It should be a function accepting three arguments:
8650 path the path of the link, the text after the prefix (like \"http:\")
8651 desc the description of the link, if any, or a description added by
8652 org-export-normalize-links if there is none
8653 format the export format, a symbol like `html' or `latex' or `ascii'..
8655 The function may use the FORMAT information to return different values
8656 depending on the format. The return value will be put literally into
8657 the exported file. If the return value is nil, this means Org should
8658 do what it normally does with links which do not have EXPORT defined.
8660 Org-mode has a built-in default for exporting links. If you are happy with
8661 this default, there is no need to define an export function for the link
8662 type. For a simple example of an export function, see `org-bbdb.el'."
8663 (add-to-list 'org-link-types type t)
8664 (org-make-link-regexps)
8665 (if (assoc type org-link-protocols)
8666 (setcdr (assoc type org-link-protocols) (list follow export))
8667 (push (list type follow export) org-link-protocols)))
8669 (defvar org-agenda-buffer-name)
8672 (defun org-store-link (arg)
8673 "\\<org-mode-map>Store an org-link to the current location.
8674 This link is added to `org-stored-links' and can later be inserted
8675 into an org-buffer with \\[org-insert-link].
8677 For some link types, a prefix arg is interpreted:
8678 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8679 For file links, arg negates `org-context-in-file-links'."
8681 (org-load-modules-maybe)
8682 (setq org-store-link-plist nil) ; reset
8683 (org-with-limited-levels
8684 (let (link cpltxt desc description search txt custom-id agenda-link)
8687 ((run-hook-with-args-until-success 'org-store-link-functions)
8688 (setq link (plist-get org-store-link-plist :link)
8689 desc (or (plist-get org-store-link-plist :description) link)))
8691 ((org-src-edit-buffer-p)
8693 (while (or (not label)
8697 (goto-char (point-min))
8699 (regexp-quote (format org-coderef-label-format label))
8701 (when label (message "Label exists already") (sit-for 2))
8702 (setq label (read-string "Code line label: " label)))
8704 (setq link (format org-coderef-label-format label))
8705 (setq gc (- 79 (length link)))
8706 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8708 (setq link (concat "(" label ")") desc nil)))
8710 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8711 ;; We are in the agenda, link to referenced location
8712 (let ((m (or (get-text-property (point) 'org-hd-marker)
8713 (get-text-property (point) 'org-marker))))
8715 (org-with-point-at m
8717 (if (org-called-interactively-p 'any)
8718 (call-interactively 'org-store-link)
8719 (org-store-link nil)))))))
8721 ((eq major-mode 'calendar-mode)
8722 (let ((cd (calendar-cursor-to-date)))
8725 (car org-time-stamp-formats)
8727 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8729 (org-store-link-props :type "calendar" :date cd)))
8731 ((eq major-mode 'w3-mode)
8732 (setq cpltxt (if (and (buffer-name)
8733 (not (string-match "Untitled" (buffer-name))))
8736 link (org-make-link (url-view-url t)))
8737 (org-store-link-props :type "w3" :url (url-view-url t)))
8739 ((eq major-mode 'w3m-mode)
8740 (setq cpltxt (or w3m-current-title w3m-current-url)
8741 link (org-make-link w3m-current-url))
8742 (org-store-link-props :type "w3m" :url (url-view-url t)))
8744 ((setq search (run-hook-with-args-until-success
8745 'org-create-file-search-functions))
8746 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8748 (setq cpltxt (or description link)))
8750 ((eq major-mode 'image-mode)
8751 (setq cpltxt (concat "file:"
8752 (abbreviate-file-name buffer-file-name))
8753 link (org-make-link cpltxt))
8754 (org-store-link-props :type "image" :file buffer-file-name))
8756 ((eq major-mode 'dired-mode)
8757 ;; link to the file in the current line
8758 (let ((file (dired-get-filename nil t)))
8760 (abbreviate-file-name
8761 (expand-file-name (dired-get-filename nil t)))
8762 ;; otherwise, no file so use current directory.
8764 (setq cpltxt (concat "file:" file)
8765 link (org-make-link cpltxt))))
8767 ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
8768 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8770 ((org-in-regexp "<<\\(.*?\\)>>")
8773 (abbreviate-file-name
8774 (buffer-file-name (buffer-base-buffer)))
8775 "::" (match-string 1))
8776 link (org-make-link cpltxt)))
8777 ((and (featurep 'org-id)
8778 (or (eq org-link-to-org-use-id t)
8779 (and (eq org-link-to-org-use-id 'create-if-interactive)
8780 (org-called-interactively-p 'any))
8781 (and (eq org-link-to-org-use-id
8782 'create-if-interactive-and-no-custom-id)
8783 (org-called-interactively-p 'any)
8785 (and org-link-to-org-use-id
8786 (org-entry-get nil "ID"))))
8787 ;; We can make a link using the ID.
8788 (setq link (condition-case nil
8789 (prog1 (org-id-store-link)
8790 (setq desc (plist-get org-store-link-plist
8793 ;; probably before first headline, link to file only
8795 (abbreviate-file-name
8796 (buffer-file-name (buffer-base-buffer))))))))
8798 ;; Just link to current headline
8799 (setq cpltxt (concat "file:"
8800 (abbreviate-file-name
8801 (buffer-file-name (buffer-base-buffer)))))
8802 ;; Add a context search string
8803 (when (org-xor org-context-in-file-links arg)
8805 ((org-at-heading-p) nil)
8806 ((org-region-active-p)
8807 (buffer-substring (region-beginning) (region-end)))
8809 (when (or (null txt) (string-match "\\S-" txt))
8813 (org-make-org-heading-search-string txt)
8815 desc (or (nth 4 (ignore-errors
8816 (org-heading-components))) "NONE"))))
8817 (if (string-match "::\\'" cpltxt)
8818 (setq cpltxt (substring cpltxt 0 -2)))
8819 (setq link (org-make-link cpltxt)))))
8821 ((buffer-file-name (buffer-base-buffer))
8822 ;; Just link to this file here.
8823 (setq cpltxt (concat "file:"
8824 (abbreviate-file-name
8825 (buffer-file-name (buffer-base-buffer)))))
8826 ;; Add a context string
8827 (when (org-xor org-context-in-file-links arg)
8828 (setq txt (if (org-region-active-p)
8829 (buffer-substring (region-beginning) (region-end))
8830 (buffer-substring (point-at-bol) (point-at-eol))))
8831 ;; Only use search option if there is some text.
8832 (when (string-match "\\S-" txt)
8834 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8836 (setq link (org-make-link cpltxt)))
8838 ((org-called-interactively-p 'interactive)
8839 (error "Cannot link to a buffer which is not visiting a file"))
8841 (t (setq link nil)))
8843 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8844 (setq link (or link cpltxt)
8845 desc (or desc cpltxt))
8846 (if (equal desc "NONE") (setq desc nil))
8848 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8850 (setq org-stored-links
8851 (cons (list link desc) org-stored-links))
8852 (message "Stored: %s" (or desc link))
8854 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8856 (setq org-stored-links
8857 (cons (list link desc) org-stored-links))))
8858 (or agenda-link (and link (org-make-link-string link desc)))))))
8860 (defun org-store-link-props (&rest plist)
8861 "Store link properties, extract names and addresses."
8863 (when (setq x (plist-get plist :from))
8864 (setq adr (mail-extract-address-components x))
8865 (setq plist (plist-put plist :fromname (car adr)))
8866 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8867 (when (setq x (plist-get plist :to))
8868 (setq adr (mail-extract-address-components x))
8869 (setq plist (plist-put plist :toname (car adr)))
8870 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8871 (let ((from (plist-get plist :from))
8872 (to (plist-get plist :to)))
8873 (when (and from to org-from-is-user-regexp)
8875 (plist-put plist :fromto
8876 (if (string-match org-from-is-user-regexp from)
8878 (concat "from %f"))))))
8879 (setq org-store-link-plist plist))
8881 (defun org-add-link-props (&rest plist)
8882 "Add these properties to the link property list."
8885 (setq key (pop plist) value (pop plist))
8886 (setq org-store-link-plist
8887 (plist-put org-store-link-plist key value)))))
8889 (defun org-email-link-description (&optional fmt)
8890 "Return the description part of an email link.
8891 This takes information from `org-store-link-plist' and formats it
8892 according to FMT (default from `org-email-link-description-format')."
8893 (setq fmt (or fmt org-email-link-description-format))
8894 (let* ((p org-store-link-plist)
8895 (to (plist-get p :toaddress))
8896 (from (plist-get p :fromaddress))
8899 (cons "%c" (plist-get p :fromto))
8900 (cons "%F" (plist-get p :from))
8901 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8902 (cons "%T" (plist-get p :to))
8903 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8904 (cons "%s" (plist-get p :subject))
8905 (cons "%d" (plist-get p :date))
8906 (cons "%m" (plist-get p :message-id)))))
8907 (when (string-match "%c" fmt)
8908 ;; Check if the user wrote this message
8909 (if (and org-from-is-user-regexp from to
8910 (save-match-data (string-match org-from-is-user-regexp from)))
8911 (setq fmt (replace-match "to %t" t t fmt))
8912 (setq fmt (replace-match "from %f" t t fmt))))
8913 (org-replace-escapes fmt table)))
8915 (defun org-make-org-heading-search-string (&optional string heading)
8916 "Make search string for STRING or current headline."
8918 (let ((s (or string (org-get-heading)))
8919 (lines org-context-in-file-links))
8920 (unless (and string (not heading))
8921 ;; We are using a headline, clean up garbage in there.
8922 (if (string-match org-todo-regexp s)
8923 (setq s (replace-match "" t t s)))
8924 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
8925 (setq s (replace-match "" t t s)))
8926 (setq s (org-trim s))
8927 (if (string-match (concat "^\\(" org-quote-string "\\|"
8928 org-comment-string "\\)") s)
8929 (setq s (replace-match "" t t s)))
8930 (while (string-match org-ts-regexp s)
8931 (setq s (replace-match "" t t s))))
8932 (or string (setq s (concat "*" s))) ; Add * for headlines
8933 (when (and string (integerp lines) (> lines 0))
8934 (let ((slines (org-split-string s "\n")))
8935 (when (< lines (length slines))
8938 (reverse (nthcdr (- (length slines) lines)
8939 (reverse slines))) "\n")))))
8940 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8942 (defun org-make-link (&rest strings)
8943 "Concatenate STRINGS."
8944 (apply 'concat strings))
8946 (defun org-make-link-string (link &optional description)
8947 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8948 (unless (string-match "\\S-" link)
8949 (error "Empty link"))
8950 (when (and description
8951 (stringp description)
8952 (not (string-match "\\S-" description)))
8953 (setq description nil))
8954 (when (stringp description)
8955 ;; Remove brackets from the description, they are fatal.
8956 (while (string-match "\\[" description)
8957 (setq description (replace-match "{" t t description)))
8958 (while (string-match "\\]" description)
8959 (setq description (replace-match "}" t t description))))
8960 (when (equal link description)
8961 ;; No description needed, it is identical
8962 (setq description nil))
8963 (when (and (not description)
8964 (not (string-match (org-image-file-name-regexp) link))
8965 (not (equal link (org-link-escape link))))
8966 (setq description (org-extract-attributes link)))
8968 (cond ((string-match (org-image-file-name-regexp) link) link)
8969 ((string-match org-link-types-re link)
8970 (concat (match-string 1 link)
8971 (org-link-escape (substring link (match-end 1)))))
8972 (t (org-link-escape link))))
8973 (concat "[[" link "]"
8974 (if description (concat "[" description "]") "")
8977 (defconst org-link-escape-chars
8978 '(?\ ?\[ ?\] ?\; ?\= ?\+)
8979 "List of characters that should be escaped in link.
8980 This is the list that is used for internal purposes.")
8982 (defvar org-url-encoding-use-url-hexify nil)
8984 (defconst org-link-escape-chars-browser
8986 "List of escapes for characters that are problematic in links.
8987 This is the list that is used before handing over to the browser.")
8989 (defun org-link-escape (text &optional table merge)
8990 "Return percent escaped representation of TEXT.
8991 TEXT is a string with the text to escape.
8992 Optional argument TABLE is a list with characters that should be
8993 escaped. When nil, `org-link-escape-chars' is used.
8994 If optional argument MERGE is set, merge TABLE into
8995 `org-link-escape-chars'."
8996 (if (and org-url-encoding-use-url-hexify (not table))
8997 (url-hexify-string text)
9000 (mapc (lambda (defchr)
9001 (unless (member defchr table)
9002 (setq table (cons defchr table)))) org-link-escape-chars))
9004 (setq table org-link-escape-chars)))
9007 (if (or (member char table)
9008 (< char 32) (= char 37) (> char 126))
9009 (mapconcat (lambda (sequence-element)
9010 (format "%%%.2X" sequence-element))
9011 (or (encode-coding-char char 'utf-8)
9012 (error "Unable to percent escape character: %s"
9013 (char-to-string char))) "")
9014 (char-to-string char))) text "")))
9016 (defun org-link-unescape (str)
9017 "Unhex hexified Unicode strings as returned from the JavaScript function
9018 encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
9019 (unless (and (null str) (string= "" str))
9020 (let ((pos 0) (case-fold-search t) unhexed)
9021 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9022 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9023 (setq str (replace-match unhexed t t str))
9024 (setq pos (+ pos (length unhexed))))))
9027 (defun org-link-unescape-compound (hex)
9028 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
9029 Note: this function also decodes single byte encodings like
9030 `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
9032 (let* ((bytes (cdr (split-string hex "%")))
9037 (let* ((val (string-to-number (pop bytes) 16))
9041 ((>= val 252) (cons 6 252))
9042 ((>= val 248) (cons 5 248))
9043 ((>= val 240) (cons 4 240))
9044 ((>= val 224) (cons 3 224))
9045 ((>= val 192) (cons 2 192))
9048 (if (>= val 192) (setq eat (car shift-xor)))
9049 (setq val (logxor val (cdr shift-xor)))
9050 (setq sum (+ (lsh sum (car shift-xor)) val))
9051 (if (> eat 0) (setq eat (- eat 1)))
9053 ((= 0 eat) ;multi byte
9054 (setq ret (concat ret (org-char-to-string sum)))
9056 ((not bytes) ; single byte(s)
9057 (setq ret (org-link-unescape-single-byte-sequence hex))))
9058 )) ;; end (while bytes
9061 (defun org-link-unescape-single-byte-sequence (hex)
9062 "Unhexify hex-encoded single byte character sequences."
9063 (mapconcat (lambda (byte)
9064 (char-to-string (string-to-number byte 16)))
9065 (cdr (split-string hex "%")) ""))
9067 (defun org-xor (a b)
9071 (defun org-fixup-message-id-for-http (s)
9072 "Replace special characters in a message id, so it can be used in an http query."
9073 (when (string-match "%" s)
9074 (setq s (mapconcat (lambda (c)
9077 (char-to-string c)))
9079 (while (string-match "<" s)
9080 (setq s (replace-match "%3C" t t s)))
9081 (while (string-match ">" s)
9082 (setq s (replace-match "%3E" t t s)))
9083 (while (string-match "@" s)
9084 (setq s (replace-match "%40" t t s)))
9088 (defun org-insert-link-global ()
9089 "Insert a link like Org-mode does.
9090 This command can be called in any mode to insert a link in Org-mode syntax."
9092 (org-load-modules-maybe)
9093 (org-run-like-in-org-mode 'org-insert-link))
9095 (defun org-insert-link (&optional complete-file link-location default-description)
9096 "Insert a link. At the prompt, enter the link.
9098 Completion can be used to insert any of the link protocol prefixes like
9101 The history can be used to select a link previously stored with
9102 `org-store-link'. When the empty string is entered (i.e. if you just
9103 press RET at the prompt), the link defaults to the most recently
9104 stored link. As SPC triggers completion in the minibuffer, you need to
9105 use M-SPC or C-q SPC to force the insertion of a space character.
9107 You will also be prompted for a description, and if one is given, it will
9108 be displayed in the buffer instead of the link.
9110 If there is already a link at point, this command will allow you to edit link
9111 and description parts.
9113 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9114 be selected using completion. The path to the file will be relative to the
9115 current directory if the file is in the current directory or a subdirectory.
9116 Otherwise, the link will be the absolute path as completed in the minibuffer
9117 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9118 option `org-link-file-path-type'.
9120 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9121 the current directory or below.
9123 With three \\[universal-argument] prefixes, negate the meaning of
9124 `org-keep-stored-link-after-insertion'.
9126 If `org-make-link-description-function' is non-nil, this function will be
9127 called with the link target, and the result will be the default
9130 If the LINK-LOCATION parameter is non-nil, this value will be
9131 used as the link location instead of reading one interactively.
9133 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9134 be used as the default description."
9136 (let* ((wcf (current-window-configuration))
9137 (region (if (org-region-active-p)
9138 (buffer-substring (region-beginning) (region-end))))
9139 (remove (and region (list (region-beginning) (region-end))))
9141 tmphist ; byte-compile incorrectly complains about this
9142 (link link-location)
9143 entry file all-prefixes)
9145 (link-location) ; specified by arg, just use it.
9146 ((org-in-regexp org-bracket-link-regexp 1)
9147 ;; We do have a link at point, and we are going to edit it.
9148 (setq remove (list (match-beginning 0) (match-end 0)))
9149 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9150 (setq link (read-string "Link: "
9152 (org-match-string-no-properties 1)))))
9153 ((or (org-in-regexp org-angle-link-re)
9154 (org-in-regexp org-plain-link-re))
9155 ;; Convert to bracket link
9156 (setq remove (list (match-beginning 0) (match-end 0))
9157 link (read-string "Link: "
9158 (org-remove-angle-brackets (match-string 0)))))
9159 ((member complete-file '((4) (16)))
9160 ;; Completing read for file names.
9161 (setq link (org-file-complete-link complete-file)))
9163 ;; Read link, with completion for stored links.
9164 (with-output-to-temp-buffer "*Org Links*"
9165 (princ "Insert a link.
9166 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9167 (when org-stored-links
9168 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9171 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
9172 (reverse org-stored-links) "\n"))))
9173 (let ((cw (selected-window)))
9174 (select-window (get-buffer-window "*Org Links*" 'visible))
9175 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9176 (unless (pos-visible-in-window-p (point-max))
9177 (org-fit-window-to-buffer))
9178 (and (window-live-p cw) (select-window cw)))
9179 ;; Fake a link history, containing the stored links.
9180 (setq tmphist (append (mapcar 'car org-stored-links)
9181 org-insert-link-history))
9182 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
9183 (mapcar 'car org-link-abbrev-alist)
9188 (let ((org-completion-use-ido nil)
9189 (org-completion-use-iswitchb nil))
9190 (org-completing-read
9193 (mapcar (lambda (x) (list (concat x ":")))
9195 (mapcar 'car org-stored-links))
9198 (car (car org-stored-links)))))
9199 (if (not (string-match "\\S-" link))
9200 (error "No link selected"))
9201 (if (or (member link all-prefixes)
9202 (and (equal ":" (substring link -1))
9203 (member (substring link 0 -1) all-prefixes)
9204 (setq link (substring link 0 -1))))
9205 (setq link (org-link-try-special-completion link))))
9206 (set-window-configuration wcf)
9207 (kill-buffer "*Org Links*"))
9208 (setq entry (assoc link org-stored-links))
9209 (or entry (push link org-insert-link-history))
9210 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9211 (not org-keep-stored-link-after-insertion))
9212 (setq org-stored-links (delq (assoc link org-stored-links)
9214 (setq desc (or desc (nth 1 entry)))))
9216 (if (string-match org-plain-link-re link)
9217 ;; URL-like link, normalize the use of angular brackets.
9218 (setq link (org-make-link (org-remove-angle-brackets link))))
9220 ;; Check if we are linking to the current file with a search option
9221 ;; If yes, simplify the link by using only the search option.
9222 (when (and buffer-file-name
9223 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
9224 (let* ((path (match-string 1 link))
9225 (case-fold-search nil)
9226 (search (match-string 2 link)))
9228 (if (equal (file-truename buffer-file-name) (file-truename path))
9229 ;; We are linking to this same file, with a search option
9230 (setq link search)))))
9232 ;; Check if we can/should use a relative path. If yes, simplify the link
9233 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9234 (let* ((type (match-string 1 link))
9235 (path (match-string 2 link))
9237 (case-fold-search nil))
9239 ((or (eq org-link-file-path-type 'absolute)
9240 (equal complete-file '(16)))
9241 (setq path (abbreviate-file-name (expand-file-name path))))
9242 ((eq org-link-file-path-type 'noabbrev)
9243 (setq path (expand-file-name path)))
9244 ((eq org-link-file-path-type 'relative)
9245 (setq path (file-relative-name path)))
9248 (if (string-match (concat "^" (regexp-quote
9250 (file-name-as-directory
9251 default-directory))))
9252 (expand-file-name path))
9253 ;; We are linking a file with relative path name.
9254 (setq path (substring (expand-file-name path)
9256 (setq path (abbreviate-file-name (expand-file-name path)))))))
9257 (setq link (concat type path))
9258 (if (equal desc origpath)
9261 (if org-make-link-description-function
9262 (setq desc (funcall org-make-link-description-function link desc))
9263 (if default-description (setq desc default-description)))
9265 (setq desc (read-string "Description: " desc))
9266 (unless (string-match "\\S-" desc) (setq desc nil))
9267 (if remove (apply 'delete-region remove))
9268 (insert (org-make-link-string link desc))))
9270 (defun org-link-try-special-completion (type)
9271 "If there is completion support for link type TYPE, offer it."
9272 (let ((fun (intern (concat "org-" type "-complete-link"))))
9275 (read-string "Link (no completion support): " (concat type ":")))))
9277 (defun org-file-complete-link (&optional arg)
9278 "Create a file link using completion."
9280 (setq file (read-file-name "File: "))
9281 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9282 (pwd1 (file-name-as-directory (abbreviate-file-name
9283 (expand-file-name ".")))))
9286 (setq link (org-make-link
9288 (abbreviate-file-name (expand-file-name file)))))
9289 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9290 (setq link (org-make-link "file:" (match-string 1 file))))
9291 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9292 (expand-file-name file))
9293 (setq link (org-make-link
9294 "file:" (match-string 1 (expand-file-name file)))))
9295 (t (setq link (org-make-link "file:" file)))))
9298 (defun org-completing-read (&rest args)
9299 "Completing-read with SPACE being a normal character."
9300 (let ((enable-recursive-minibuffers t)
9301 (minibuffer-local-completion-map
9302 (copy-keymap minibuffer-local-completion-map)))
9303 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9304 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9305 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9306 (apply 'org-icompleting-read args)))
9308 (defun org-completing-read-no-i (&rest args)
9309 (let (org-completion-use-ido org-completion-use-iswitchb)
9310 (apply 'org-completing-read args)))
9312 (defun org-iswitchb-completing-read (prompt choices &rest args)
9313 "Use iswitch as a completing-read replacement to choose from choices.
9314 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9316 (let* ((iswitchb-use-virtual-buffers nil)
9317 (iswitchb-make-buflist-hook
9319 (setq iswitchb-temp-buflist choices))))
9320 (iswitchb-read-buffer prompt)))
9322 (defun org-icompleting-read (&rest args)
9323 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9324 (org-without-partial-completion
9325 (if (and org-completion-use-ido
9326 (fboundp 'ido-completing-read)
9327 (boundp 'ido-mode) ido-mode
9328 (listp (second args)))
9329 (let ((ido-enter-matching-directory nil))
9330 (apply 'ido-completing-read (concat (car args))
9331 (if (consp (car (nth 1 args)))
9332 (mapcar 'car (nth 1 args))
9335 (if (and org-completion-use-iswitchb
9336 (boundp 'iswitchb-mode) iswitchb-mode
9337 (listp (second args)))
9338 (apply 'org-iswitchb-completing-read (concat (car args))
9339 (if (consp (car (nth 1 args)))
9340 (mapcar 'car (nth 1 args))
9343 (apply 'completing-read args)))))
9345 (defun org-extract-attributes (s)
9346 "Extract the attributes cookie from a string and set as text property."
9347 (let (a attr (start 0) key value)
9349 (when (string-match "{{\\([^}]+\\)}}$" s)
9350 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9351 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9352 (setq key (match-string 1 a) value (match-string 2 a)
9354 attr (plist-put attr (intern key) value))))
9355 (org-add-props s nil 'org-attr attr))
9358 (defun org-extract-attributes-from-string (tag)
9359 (let (key value attr)
9360 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9361 (setq key (match-string 1 tag) value (match-string 2 tag)
9362 tag (replace-match "" t t tag)
9363 attr (plist-put attr (intern key) value)))
9366 (defun org-attributes-to-string (plist)
9367 "Format a property list into an HTML attribute list."
9368 (let ((s "") key value)
9370 (setq key (pop plist) value (pop plist))
9372 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9375 ;;; Opening/following a link
9377 (defvar org-link-search-failed nil)
9379 (defvar org-open-link-functions nil
9380 "Hook for functions finding a plain text link.
9381 These functions must take a single argument, the link content.
9382 They will be called for links that look like [[link text][description]]
9383 when LINK TEXT does not have a protocol like \"http:\" and does not look
9384 like a filename (e.g. \"./blue.png\").
9386 These functions will be called *before* Org attempts to resolve the
9387 link by doing text searches in the current buffer - so if you want a
9388 link \"[[target]]\" to still find \"<<target>>\", your function should
9389 handle this as a special case.
9391 When the function does handle the link, it must return a non-nil value.
9392 If it decides that it is not responsible for this link, it must return
9393 nil to indicate that that Org-mode can continue with other options
9394 like exact and fuzzy text search.")
9396 (defun org-next-link ()
9397 "Move forward to the next link.
9398 If the link is in hidden text, expose it."
9400 (when (and org-link-search-failed (eq this-command last-command))
9401 (goto-char (point-min))
9402 (message "Link search wrapped back to beginning of buffer"))
9403 (setq org-link-search-failed nil)
9404 (let* ((pos (point))
9406 (a (assoc :link ct)))
9407 (if a (goto-char (nth 2 a)))
9408 (if (re-search-forward org-any-link-re nil t)
9410 (goto-char (match-beginning 0))
9411 (if (outline-invisible-p) (org-show-context)))
9413 (setq org-link-search-failed t)
9414 (error "No further link found"))))
9416 (defun org-previous-link ()
9417 "Move backward to the previous link.
9418 If the link is in hidden text, expose it."
9420 (when (and org-link-search-failed (eq this-command last-command))
9421 (goto-char (point-max))
9422 (message "Link search wrapped back to end of buffer"))
9423 (setq org-link-search-failed nil)
9424 (let* ((pos (point))
9426 (a (assoc :link ct)))
9427 (if a (goto-char (nth 1 a)))
9428 (if (re-search-backward org-any-link-re nil t)
9430 (goto-char (match-beginning 0))
9431 (if (outline-invisible-p) (org-show-context)))
9433 (setq org-link-search-failed t)
9434 (error "No further link found"))))
9436 (defun org-translate-link (s)
9437 "Translate a link string if a translation function has been defined."
9438 (if (and org-link-translation-function
9439 (fboundp org-link-translation-function)
9440 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9442 (setq s (funcall org-link-translation-function
9443 (match-string 1) (match-string 2)))
9444 (concat (car s) ":" (cdr s)))
9447 (defun org-translate-link-from-planner (type path)
9448 "Translate a link from Emacs Planner syntax so that Org can follow it.
9449 This is still an experimental function, your mileage may vary."
9451 ((member type '("http" "https" "news" "ftp"))
9452 ;; standard Internet links are the same.
9454 ((and (equal type "irc") (string-match "^//" path))
9455 ;; Planner has two / at the beginning of an irc link, we have 1.
9456 ;; We should have zero, actually....
9457 (setq path (substring path 1)))
9458 ((and (equal type "lisp") (string-match "^/" path))
9459 ;; Planner has a slash, we do not.
9460 (setq type "elisp" path (substring path 1)))
9461 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9462 ;; A typical message link. Planner has the id after the final slash,
9463 ;; we separate it with a hash mark
9464 (setq path (concat (match-string 1 path) "#"
9465 (org-remove-angle-brackets (match-string 2 path)))))
9469 (defun org-find-file-at-mouse (ev)
9470 "Open file link or URL at mouse."
9472 (mouse-set-point ev)
9473 (org-open-at-point 'in-emacs))
9475 (defun org-open-at-mouse (ev)
9476 "Open file link or URL at mouse.
9477 See the docstring of `org-open-file' for details."
9479 (mouse-set-point ev)
9480 (if (eq major-mode 'org-agenda-mode)
9481 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9482 (org-open-at-point))
9484 (defvar org-window-config-before-follow-link nil
9485 "The window configuration before following a link.
9486 This is saved in case the need arises to restore it.")
9488 (defvar org-open-link-marker (make-marker)
9489 "Marker pointing to the location where `org-open-at-point; was called.")
9492 (defun org-open-at-point-global ()
9493 "Follow a link like Org-mode does.
9494 This command can be called in any mode to follow a link that has
9497 (org-run-like-in-org-mode 'org-open-at-point))
9500 (defun org-open-link-from-string (s &optional arg reference-buffer)
9501 "Open a link in the string S, as if it was in Org-mode."
9502 (interactive "sLink: \nP")
9503 (let ((reference-buffer (or reference-buffer (current-buffer))))
9505 (let ((org-inhibit-startup (not reference-buffer)))
9508 (goto-char (point-min))
9509 (when reference-buffer
9510 (setq org-link-abbrev-alist-local
9511 (with-current-buffer reference-buffer
9512 org-link-abbrev-alist-local)))
9513 (org-open-at-point arg reference-buffer)))))
9515 (defvar org-open-at-point-functions nil
9516 "Hook that is run when following a link at point.
9518 Functions in this hook must return t if they identify and follow
9519 a link at point. If they don't find anything interesting at point,
9520 they must return nil.")
9522 (defun org-open-at-point (&optional arg reference-buffer)
9523 "Open link at or after point.
9524 If there is no link at point, this function will search forward up to
9525 the end of the current line.
9526 Normally, files will be opened by an appropriate application. If the
9527 optional prefix argument ARG is non-nil, Emacs will visit the file.
9528 With a double prefix argument, try to open outside of Emacs, in the
9529 application the system uses for this file type."
9531 ;; if in a code block, then open the block's results
9532 (unless (call-interactively #'org-babel-open-src-block-result)
9533 (org-load-modules-maybe)
9534 (move-marker org-open-link-marker (point))
9535 (setq org-window-config-before-follow-link (current-window-configuration))
9536 (org-remove-occur-highlights nil nil t)
9538 ((and (org-at-heading-p)
9540 (concat org-plain-link-re "\\|"
9541 org-bracket-link-regexp "\\|"
9542 org-angle-link-re "\\|"
9543 "[ \t]:[^ \t\n]+:[ \t]*$")))
9544 (not (get-text-property (point) 'org-linked-text)))
9545 (or (org-offer-links-in-entry arg)
9546 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9547 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9548 ((org-at-timestamp-p t) (org-follow-timestamp-link))
9549 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9550 (not (org-in-regexp org-bracket-link-regexp)))
9551 (org-footnote-action))
9553 (let (type path link line search (pos (point)))
9556 (skip-chars-forward "^]\n\r")
9557 (when (org-in-regexp org-bracket-link-regexp 1)
9558 (setq link (org-extract-attributes
9559 (org-link-unescape (org-match-string-no-properties 1))))
9560 (while (string-match " *\n *" link)
9561 (setq link (replace-match " " t t link)))
9562 (setq link (org-link-expand-abbrev link))
9564 ((or (file-name-absolute-p link)
9565 (string-match "^\\.\\.?/" link))
9566 (setq type "file" path link))
9567 ((string-match org-link-re-with-space3 link)
9568 (setq type (match-string 1 link) path (match-string 2 link)))
9569 (t (setq type "thisfile" path link)))
9572 (when (get-text-property (point) 'org-linked-text)
9573 (setq type "thisfile"
9574 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9575 (1+ (point)) (point))
9576 path (buffer-substring
9577 (or (previous-single-property-change pos 'org-linked-text)
9579 (or (next-single-property-change pos 'org-linked-text)
9584 (when (or (org-in-regexp org-angle-link-re)
9585 (org-in-regexp org-plain-link-re))
9586 (setq type (match-string 1)
9587 path (org-link-unescape (match-string 2)))
9590 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9592 path (match-string 1))
9593 (while (string-match ":" path)
9594 (setq path (replace-match "+" t t path)))
9596 (when (org-in-regexp "<\\([^><\n]+\\)>")
9597 (setq type "tree-match"
9598 path (match-string 1))
9601 (error "No link found"))
9603 ;; switch back to reference buffer
9604 ;; needed when if called in a temporary buffer through
9605 ;; org-open-link-from-string
9606 (with-current-buffer (or reference-buffer (current-buffer))
9608 ;; Remove any trailing spaces in path
9609 (if (string-match " +\\'" path)
9610 (setq path (replace-match "" t t path)))
9611 (if (and org-link-translation-function
9612 (fboundp org-link-translation-function))
9613 ;; Check if we need to translate the link
9614 (let ((tmp (funcall org-link-translation-function type path)))
9615 (setq type (car tmp) path (cdr tmp))))
9619 ((assoc type org-link-protocols)
9620 (funcall (nth 1 (assoc type org-link-protocols)) path))
9622 ((equal type "mailto")
9623 (let ((cmd (car org-link-mailto-program))
9624 (args (cdr org-link-mailto-program)) args1
9625 (address path) (subject "") a)
9626 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9627 (setq address (match-string 1 path)
9628 subject (org-link-escape (match-string 2 path))))
9631 ((not (stringp (car args))) (push (pop args) args1))
9632 (t (setq a (pop args))
9633 (if (string-match "%a" a)
9634 (setq a (replace-match address t t a)))
9635 (if (string-match "%s" a)
9636 (setq a (replace-match subject t t a)))
9638 (apply cmd (nreverse args1))))
9640 ((member type '("http" "https" "ftp" "news"))
9641 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9643 path org-link-escape-chars-browser)
9646 ((string= type "doi")
9647 (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
9649 path org-link-escape-chars-browser)
9652 ((member type '("message"))
9653 (browse-url (concat type ":" path)))
9655 ((string= type "tags")
9656 (org-tags-view arg path))
9658 ((string= type "tree-match")
9659 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9661 ((string= type "file")
9662 (if (string-match "::\\([0-9]+\\)\\'" path)
9663 (setq line (string-to-number (match-string 1 path))
9664 path (substring path 0 (match-beginning 0)))
9665 (if (string-match "::\\(.+\\)\\'" path)
9666 (setq search (match-string 1 path)
9667 path (substring path 0 (match-beginning 0)))))
9668 (if (string-match "[*?{]" (file-name-nondirectory path))
9670 (org-open-file path arg line search)))
9672 ((string= type "shell")
9674 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9675 (string-match org-confirm-shell-link-not-regexp cmd))
9676 (not org-confirm-shell-link-function)
9677 (funcall org-confirm-shell-link-function
9678 (format "Execute \"%s\" in shell? "
9679 (org-add-props cmd nil
9680 'face 'org-warning))))
9682 (message "Executing %s" cmd)
9683 (shell-command cmd))
9686 ((string= type "elisp")
9688 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9689 (string-match org-confirm-elisp-link-not-regexp cmd))
9690 (not org-confirm-elisp-link-function)
9691 (funcall org-confirm-elisp-link-function
9692 (format "Execute \"%s\" as elisp? "
9693 (org-add-props cmd nil
9694 'face 'org-warning))))
9695 (message "%s => %s" cmd
9696 (if (equal (string-to-char cmd) ?\()
9698 (call-interactively (read cmd))))
9701 ((and (string= type "thisfile")
9702 (run-hook-with-args-until-success
9703 'org-open-link-functions path)))
9705 ((string= type "thisfile")
9707 (switch-to-buffer-other-window
9708 (org-get-buffer-for-internal-link (current-buffer)))
9709 (org-mark-ring-push))
9710 (let ((cmd `(org-link-search
9712 ,(cond ((equal arg '(4)) ''occur)
9713 ((equal arg '(16)) ''org-occur)
9716 (condition-case nil (let ((org-link-search-inhibit-query t))
9718 (error (progn (widen) (eval cmd))))))
9721 (browse-url-at-point)))))))
9722 (move-marker org-open-link-marker nil)
9723 (run-hook-with-args 'org-follow-link-hook)))
9725 (defun org-offer-links-in-entry (&optional nth zero)
9726 "Offer links in the current entry and follow the selected link.
9727 If there is only one link, follow it immediately as well.
9728 If NTH is an integer, immediately pick the NTH link found.
9729 If ZERO is a string, check also this string for a link, and if
9730 there is one, offer it as link number zero."
9731 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9732 "\\(" org-angle-link-re "\\)\\|"
9733 "\\(" org-plain-link-re "\\)"))
9735 (in-emacs (if (integerp nth) nil nth))
9736 have-zero end links link c)
9737 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9738 (push (match-string 0 zero) links)
9739 (setq cnt (1- cnt) have-zero t))
9741 (org-back-to-heading t)
9742 (setq end (save-excursion (outline-next-heading) (point)))
9743 (while (re-search-forward re end t)
9744 (push (match-string 0) links))
9745 (setq links (org-uniquify (reverse links))))
9749 (message "No links"))
9750 ((equal (length links) 1)
9751 (setq link (list (car links))))
9752 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9753 (setq link (list (nth (if have-zero nth (1- nth)) links))))
9754 (t ; we have to select a link
9756 (save-window-excursion
9757 (delete-other-windows)
9758 (with-output-to-temp-buffer "*Select Link*"
9760 (if (not (string-match org-bracket-link-regexp l))
9761 (princ (format "[%c] %s\n" (incf cnt)
9762 (org-remove-angle-brackets l)))
9764 (princ (format "[%c] %s (%s)\n" (incf cnt)
9765 (match-string 3 l) (match-string 1 l)))
9766 (princ (format "[%c] %s\n" (incf cnt)
9767 (match-string 1 l))))))
9769 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9770 (message "Select link to open, RET to open all:")
9771 (setq c (read-char-exclusive))
9772 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9773 (when (equal c ?q) (error "Abort"))
9777 (if have-zero (setq nth (1+ nth)))
9778 (unless (and (integerp nth) (>= (length links) nth))
9779 (error "Invalid link selection"))
9780 (setq link (list (nth (1- nth) links))))))
9782 (let ((buf (current-buffer)))
9784 (org-open-link-from-string l in-emacs buf))
9788 ;; Add special file links that specify the way of opening
9790 (org-add-link-type "file+sys" 'org-open-file-with-system)
9791 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9792 (defun org-open-file-with-system (path)
9793 "Open file at PATH using the system way of opening it."
9794 (org-open-file path 'system))
9795 (defun org-open-file-with-emacs (path)
9796 "Open file at PATH in Emacs."
9797 (org-open-file path 'emacs))
9798 (defun org-remove-file-link-modifiers ()
9799 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9800 (goto-char (point-min))
9801 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9803 (replace-match "file:" t t))))
9804 (eval-after-load "org-exp"
9805 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9806 'org-remove-file-link-modifiers))
9810 (defun org-get-effort (&optional pom)
9811 "Get the effort estimate for the current entry."
9812 (org-entry-get pom org-effort-property))
9816 (defvar org-create-file-search-functions nil
9817 "List of functions to construct the right search string for a file link.
9818 These functions are called in turn with point at the location to
9819 which the link should point.
9821 A function in the hook should first test if it would like to
9822 handle this file type, for example by checking the `major-mode'
9823 or the file extension. If it decides not to handle this file, it
9824 should just return nil to give other functions a chance. If it
9825 does handle the file, it must return the search string to be used
9826 when following the link. The search string will be part of the
9827 file link, given after a double colon, and `org-open-at-point'
9828 will automatically search for it. If special measures must be
9829 taken to make the search successful, another function should be
9830 added to the companion hook `org-execute-file-search-functions',
9833 A function in this hook may also use `setq' to set the variable
9834 `description' to provide a suggestion for the descriptive text to
9835 be used for this link when it gets inserted into an Org-mode
9836 buffer with \\[org-insert-link].")
9838 (defvar org-execute-file-search-functions nil
9839 "List of functions to execute a file search triggered by a link.
9841 Functions added to this hook must accept a single argument, the
9842 search string that was part of the file link, the part after the
9843 double colon. The function must first check if it would like to
9844 handle this search, for example by checking the `major-mode' or
9845 the file extension. If it decides not to handle this search, it
9846 should just return nil to give other functions a chance. If it
9847 does handle the search, it must return a non-nil value to keep
9848 other functions from trying.
9850 Each function can access the current prefix argument through the
9851 variable `current-prefix-argument'. Note that a single prefix is
9852 used to force opening a link in Emacs, so it may be good to only
9853 use a numeric or double prefix to guide the search function.
9855 In case this is needed, a function in this hook can also restore
9856 the window configuration before `org-open-at-point' was called using:
9858 (set-window-configuration org-window-config-before-follow-link)")
9860 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
9861 (defun org-link-search (s &optional type avoid-pos stealth)
9862 "Search for a link search option.
9863 If S is surrounded by forward slashes, it is interpreted as a
9864 regular expression. In org-mode files, this will create an `org-occur'
9865 sparse tree. In ordinary files, `occur' will be used to list matches.
9866 If the current buffer is in `dired-mode', grep will be used to search
9867 in all files. If AVOID-POS is given, ignore matches near that position.
9869 When optional argument STEALTH is non-nil, do not modify
9870 visibility around point, thus ignoring
9871 `org-show-hierarchy-above', `org-show-following-heading' and
9872 `org-show-siblings' variables."
9873 (let ((case-fold-search t)
9874 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
9875 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
9876 (append '(("") (" ") ("\t") ("\n"))
9880 (pre nil) (post nil)
9881 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
9883 ;; First check if there are any special search functions
9884 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
9885 ;; Now try the builtin stuff
9886 ((and (equal (string-to-char s0) ?#)
9889 (goto-char (point-min))
9892 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
9893 (setq type 'dedicated
9894 pos (match-beginning 0))))
9895 ;; There is an exact target for this
9897 (org-back-to-heading t)))
9899 (goto-char (point-min))
9902 (concat "<<" (regexp-quote s0) ">>") nil t)
9903 (setq type 'dedicated
9904 pos (match-beginning 0))))
9905 ;; There is an exact target for this
9908 (goto-char (point-min))
9911 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
9912 (setq type 'dedicated pos (match-beginning 0))))
9913 ;; Found an invisible target.
9916 (goto-char (point-min))
9919 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
9920 (setq type 'dedicated pos (match-beginning 0))))
9921 ;; Found an element with a matching #+name affiliated keyword.
9923 ((and (string-match "^(\\(.*\\))$" s0)
9925 (goto-char (point-min))
9928 (concat "[^[]" (regexp-quote
9929 (format org-coderef-label-format
9930 (match-string 1 s0))))
9932 (setq type 'dedicated
9933 pos (1+ (match-beginning 0))))))
9934 ;; There is a coderef target for this
9936 ((string-match "^/\\(.*\\)/$" s)
9937 ;; A regular expression
9939 ((eq major-mode 'org-mode)
9940 (org-occur (match-string 1 s)))
9941 ;;((eq major-mode 'dired-mode)
9942 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
9943 (t (org-do-occur (match-string 1 s)))))
9944 ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
9945 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
9946 (goto-char (point-min))
9948 ((let (case-fold-search)
9949 (re-search-forward (format org-complex-heading-regexp-format
9952 ;; OK, found a match
9953 (setq type 'dedicated)
9954 (goto-char (match-beginning 0)))
9955 ((and (not org-link-search-inhibit-query)
9956 (eq org-link-search-must-match-exact-headline 'query-to-create)
9957 (y-or-n-p "No match - create this as a new heading? "))
9958 (goto-char (point-max))
9959 (or (bolp) (newline))
9960 (insert "* " s "\n")
9961 (beginning-of-line 0))
9964 (error "No match"))))
9966 ;; A normal search string
9967 (when (equal (string-to-char s) ?*)
9968 ;; Anchor on headlines, post may include tags.
9969 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
9970 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
9972 (remove-text-properties
9974 '(face nil mouse-face nil keymap nil fontified nil) s)
9975 ;; Make a series of regular expressions to find a match
9976 (setq words (org-split-string s "[ \n\r\t]+")
9978 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
9979 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
9981 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
9982 re2a (concat "[ \t\r\n]" re2a_)
9983 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
9984 re4 (concat "[^a-zA-Z_]" re4_)
9986 re1 (concat pre re2 post)
9987 re3 (concat pre (if pre re4_ re4) post)
9988 re5 (concat pre ".*" re4)
9989 re2 (concat pre re2)
9990 re2a (concat pre (if pre re2a_ re2a))
9991 re4 (concat pre (if pre re4_ re4))
9992 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
9993 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
9997 ((eq type 'org-occur) (org-occur reall))
9998 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
9999 (t (goto-char (point-min))
10001 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10002 (org-search-not-self 1 re1 nil t)
10003 (org-search-not-self 1 re2 nil t)
10004 (org-search-not-self 1 re2a nil t)
10005 (org-search-not-self 1 re3 nil t)
10006 (org-search-not-self 1 re4 nil t)
10007 (org-search-not-self 1 re5 nil t)
10009 (goto-char (match-beginning 1))
10011 (error "No match"))))))
10012 (and (eq major-mode 'org-mode)
10014 (org-show-context 'link-search))
10017 (defun org-search-not-self (group &rest args)
10018 "Execute `re-search-forward', but only accept matches that do not
10019 enclose the position of `org-open-link-marker'."
10020 (let ((m org-open-link-marker))
10022 (while (apply 're-search-forward args)
10023 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10024 (goto-char (match-end group))
10025 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10026 (> (match-beginning 0) (marker-position m))
10027 (< (match-end 0) (marker-position m)))
10029 (or (not (org-in-regexp
10030 org-bracket-link-analytic-regexp 1))
10031 (not (match-end 4)) ; no description
10032 (and (<= (match-beginning 4) (point))
10033 (>= (match-end 4) (point))))))
10034 (throw 'exit (point))))))))
10036 (defun org-get-buffer-for-internal-link (buffer)
10037 "Return a buffer to be used for displaying the link target of internal links."
10039 ((not org-display-internal-link-with-indirect-buffer)
10041 ((string-match "(Clone)$" (buffer-name buffer))
10042 (message "Buffer is already a clone, not making another one")
10043 ;; we also do not modify visibility in this case
10045 (t ; make a new indirect buffer for displaying the link
10046 (let* ((bn (buffer-name buffer))
10047 (ibn (concat bn "(Clone)"))
10048 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10049 (with-current-buffer ib (org-overview))
10052 (defun org-do-occur (regexp &optional cleanup)
10053 "Call the Emacs command `occur'.
10054 If CLEANUP is non-nil, remove the printout of the regular expression
10055 in the *Occur* buffer. This is useful if the regex is long and not useful
10059 (let ((cwin (selected-window)) win beg end)
10060 (when (setq win (get-buffer-window "*Occur*"))
10061 (select-window win))
10062 (goto-char (point-min))
10063 (when (re-search-forward "match[a-z]+" nil t)
10064 (setq beg (match-end 0))
10065 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10066 (setq end (1- (match-beginning 0)))))
10067 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10068 (goto-char (point-min))
10069 (select-window cwin))))
10071 ;;; The mark ring for links jumps
10073 (defvar org-mark-ring nil
10074 "Mark ring for positions before jumps in Org-mode.")
10075 (defvar org-mark-ring-last-goto nil
10076 "Last position in the mark ring used to go back.")
10077 ;; Fill and close the ring
10078 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10079 (loop for i from 1 to org-mark-ring-length do
10080 (push (make-marker) org-mark-ring))
10081 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10084 (defun org-mark-ring-push (&optional pos buffer)
10085 "Put the current position or POS into the mark ring and rotate it."
10087 (setq pos (or pos (point)))
10088 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10089 (move-marker (car org-mark-ring)
10091 (or buffer (current-buffer)))
10093 (substitute-command-keys
10094 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10096 (defun org-mark-ring-goto (&optional n)
10097 "Jump to the previous position in the mark ring.
10098 With prefix arg N, jump back that many stored positions. When
10099 called several times in succession, walk through the entire ring.
10100 Org-mode commands jumping to a different position in the current file,
10101 or to another Org-mode file, automatically push the old position
10105 (if (eq last-command this-command)
10106 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10107 (setq p org-mark-ring))
10108 (setq org-mark-ring-last-goto p)
10110 (org-pop-to-buffer-same-window (marker-buffer m))
10112 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10114 (defun org-remove-angle-brackets (s)
10115 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10116 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10118 (defun org-add-angle-brackets (s)
10119 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10120 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10122 (defun org-remove-double-quotes (s)
10123 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10124 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10127 ;;; Following specific links
10129 (defun org-follow-timestamp-link ()
10131 ((org-at-date-range-p t)
10132 (let ((org-agenda-start-on-weekday)
10133 (t1 (match-string 1))
10134 (t2 (match-string 2)))
10135 (setq t1 (time-to-days (org-time-string-to-time t1))
10136 t2 (time-to-days (org-time-string-to-time t2)))
10137 (org-agenda-list nil t1 (1+ (- t2 t1)))))
10138 ((org-at-timestamp-p t)
10139 (org-agenda-list nil (time-to-days (org-time-string-to-time
10140 (substring (match-string 1) 0 10)))
10142 (t (error "This should not happen"))))
10145 ;;; Following file links
10146 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10147 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10148 (declare-function mailcap-mime-info
10149 "mailcap" (string &optional request no-decode))
10150 (defvar org-wait nil)
10151 (defun org-open-file (path &optional in-emacs line search)
10152 "Open the file at PATH.
10153 First, this expands any special file name abbreviations. Then the
10154 configuration variable `org-file-apps' is checked if it contains an
10155 entry for this file type, and if yes, the corresponding command is launched.
10157 If no application is found, Emacs simply visits the file.
10159 With optional prefix argument IN-EMACS, Emacs will visit the file.
10160 With a double \\[universal-argument] \\[universal-argument] \
10161 prefix arg, Org tries to avoid opening in Emacs
10162 and to use an external application to visit the file.
10164 Optional LINE specifies a line to go to, optional SEARCH a string
10165 to search for. If LINE or SEARCH is given, the file will be
10166 opened in Emacs, unless an entry from org-file-apps that makes
10167 use of groups in a regexp matches.
10169 If you want to change the way frames are used when following a
10170 link, please customize `org-link-frame-setup'.
10172 If the file does not exist, an error is thrown."
10173 (let* ((file (if (equal path "")
10175 (substitute-in-file-name (expand-file-name path))))
10176 (file-apps (append org-file-apps (org-default-apps)))
10177 (apps (org-remove-if
10178 'org-file-apps-entry-match-against-dlink-p file-apps))
10179 (apps-dlink (org-remove-if-not
10180 'org-file-apps-entry-match-against-dlink-p file-apps))
10181 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10182 (dirp (if remp nil (file-directory-p file)))
10183 (file (if (and dirp org-open-directory-means-index-dot-org)
10184 (concat (file-name-as-directory file) "index.org")
10186 (a-m-a-p (assq 'auto-mode apps))
10187 (dfile (downcase file))
10188 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10189 (link (cond ((and (eq line nil)
10193 (concat file "::" (number-to-string line)))
10195 (concat file "::" search))))
10196 (dlink (downcase link))
10197 (old-buffer (current-buffer))
10199 (old-mode major-mode)
10200 ext cmd link-match-data)
10201 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10202 (setq ext (match-string 1 dfile))
10203 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10204 (setq ext (match-string 1 dfile))))
10206 ((member in-emacs '((16) system))
10207 (setq cmd (cdr (assoc 'system apps))))
10208 (in-emacs (setq cmd 'emacs))
10210 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10211 (and dirp (cdr (assoc 'directory apps)))
10212 ; first, try matching against apps-dlink
10213 ; if we get a match here, store the match data for later
10214 (let ((match (assoc-default dlink apps-dlink
10217 (progn (setq link-match-data (match-data))
10219 (progn (setq in-emacs (or in-emacs line search))
10220 nil))) ; if we have no match in apps-dlink,
10221 ; always open the file in emacs if line or search
10222 ; is given (for backwards compatibility)
10223 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10225 (cdr (assoc ext apps))
10226 (cdr (assoc t apps))))))
10227 (when (eq cmd 'system)
10228 (setq cmd (cdr (assoc 'system apps))))
10229 (when (eq cmd 'default)
10230 (setq cmd (cdr (assoc t apps))))
10231 (when (eq cmd 'mailcap)
10233 (mailcap-parse-mailcaps)
10234 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10235 (command (mailcap-mime-info mime-type)))
10236 (if (stringp command)
10238 (setq cmd 'emacs))))
10239 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10240 (not (file-exists-p file))
10241 (not org-open-non-existing-files))
10242 (error "No such file: %s" file))
10244 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10245 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10246 (while (string-match "['\"]%s['\"]" cmd)
10247 (setq cmd (replace-match "%s" t t cmd)))
10248 (while (string-match "%s" cmd)
10249 (setq cmd (replace-match
10251 (shell-quote-argument
10252 (convert-standard-filename file)))
10255 ;; Replace "%1", "%2" etc. in command with group matches from regex
10257 (let ((match-index 1)
10258 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10259 (set-match-data link-match-data)
10260 (while (<= match-index number-of-groups)
10261 (let ((regex (concat "%" (number-to-string match-index)))
10262 (replace-with (match-string match-index dlink)))
10263 (while (string-match regex cmd)
10264 (setq cmd (replace-match replace-with t t cmd))))
10265 (setq match-index (+ match-index 1)))))
10267 (save-window-excursion
10268 (start-process-shell-command cmd nil cmd)
10269 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10273 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10275 (if line (org-goto-line line)
10276 (if search (org-link-search search))))
10278 (let ((file (convert-standard-filename file)))
10280 (set-match-data link-match-data)
10282 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10283 (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
10284 (or (not (equal old-buffer (current-buffer)))
10285 (not (equal old-pos (point))))
10286 (org-mark-ring-push old-pos old-buffer))))
10288 (defun org-file-apps-entry-match-against-dlink-p (entry)
10289 "This function returns non-nil if `entry' uses a regular
10290 expression which should be matched against the whole link by
10293 It assumes that is the case when the entry uses a regular
10294 expression which has at least one grouping construct and the
10295 action is either a lisp form or a command string containing
10296 '%1', i.e. using at least one subexpression match as a
10298 (let ((selector (car entry))
10299 (action (cdr entry)))
10300 (if (stringp selector)
10301 (and (> (regexp-opt-depth selector) 0)
10302 (or (and (stringp action)
10303 (string-match "%[0-9]" action))
10307 (defun org-default-apps ()
10308 "Return the default applications for this operating system."
10310 ((eq system-type 'darwin)
10311 org-file-apps-defaults-macosx)
10312 ((eq system-type 'windows-nt)
10313 org-file-apps-defaults-windowsnt)
10314 (t org-file-apps-defaults-gnu)))
10316 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10317 "Convert extensions to regular expressions in the cars of LIST.
10318 Also, weed out any non-string entries, because the return value is used
10319 only for regexp matching.
10320 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10321 point to the symbol `emacs', indicating that the file should
10322 be opened in Emacs."
10325 (mapcar (lambda (x)
10326 (if (not (stringp (car x)))
10328 (if (string-match "\\W" (car x))
10330 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10333 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10335 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10336 (defun org-file-remote-p (file)
10337 "Test whether FILE specifies a location on a remote system.
10338 Return non-nil if the location is indeed remote.
10340 For example, the filename \"/user@host:/foo\" specifies a location
10341 on the system \"/user@host:\"."
10342 (cond ((fboundp 'file-remote-p)
10343 (file-remote-p file))
10344 ((fboundp 'tramp-handle-file-remote-p)
10345 (tramp-handle-file-remote-p file))
10346 ((and (boundp 'ange-ftp-name-format)
10347 (string-match (car ange-ftp-name-format) file))
10354 (defun org-get-org-file ()
10355 "Read a filename, with default directory `org-directory'."
10356 (let ((default (or org-default-notes-file remember-data-file)))
10357 (read-file-name (format "File name [%s]: " default)
10358 (file-name-as-directory org-directory)
10361 (defun org-notes-order-reversed-p ()
10362 "Check if the current file should receive notes in reversed order."
10364 ((not org-reverse-note-order) nil)
10365 ((eq t org-reverse-note-order) t)
10366 ((not (listp org-reverse-note-order)) nil)
10368 (let ((all org-reverse-note-order)
10370 (while (setq entry (pop all))
10371 (if (string-match (car entry) buffer-file-name)
10372 (throw 'exit (cdr entry))))
10375 (defvar org-refile-target-table nil
10376 "The list of refile targets, created by `org-refile'.")
10378 (defvar org-agenda-new-buffers nil
10379 "Buffers created to visit agenda files.")
10381 (defvar org-refile-cache nil
10382 "Cache for refile targets.")
10384 (defvar org-refile-markers nil
10385 "All the markers used for caching refile locations.")
10387 (defun org-refile-marker (pos)
10388 "Get a new refile marker, but only if caching is in use."
10389 (if (not org-refile-use-cache)
10391 (let ((m (make-marker)))
10392 (move-marker m pos)
10393 (push m org-refile-markers)
10396 (defun org-refile-cache-clear ()
10397 "Clear the refile cache and disable all the markers."
10398 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10399 (setq org-refile-markers nil)
10400 (setq org-refile-cache nil)
10401 (message "Refile cache has been cleared"))
10403 (defun org-refile-cache-check-set (set)
10404 "Check if all the markers in the cache still have live buffers."
10407 (while (and set (setq marker (nth 3 (pop set))))
10408 ;; if org-refile-use-outline-path is 'file, marker may be nil
10409 (when (and marker (null (marker-buffer marker)))
10410 (message "not found") (sit-for 3)
10411 (throw 'exit nil)))
10414 (defun org-refile-cache-put (set &rest identifiers)
10415 "Push the refile targets SET into the cache, under IDENTIFIERS."
10416 (let* ((key (sha1 (prin1-to-string identifiers)))
10417 (entry (assoc key org-refile-cache)))
10420 (push (cons key set) org-refile-cache))))
10422 (defun org-refile-cache-get (&rest identifiers)
10423 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10425 ((not org-refile-cache) nil)
10426 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10428 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10429 org-refile-cache))))
10430 (and set (org-refile-cache-check-set set) set)))))
10432 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10433 "Produce a table with refile targets."
10434 (let ((case-fold-search nil)
10435 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10436 (entries (or org-refile-targets '((nil . (:level . 1)))))
10437 targets tgs txt re files f desc descre fast-path-p level pos0)
10438 (message "Getting targets...")
10439 (with-current-buffer (or default-buffer (current-buffer))
10440 (while (setq entry (pop entries))
10441 (setq files (car entry) desc (cdr entry))
10442 (setq fast-path-p nil)
10444 ((null files) (setq files (list (current-buffer))))
10445 ((eq files 'org-agenda-files)
10446 (setq files (org-agenda-files 'unrestricted)))
10447 ((and (symbolp files) (fboundp files))
10448 (setq files (funcall files)))
10449 ((and (symbolp files) (boundp files))
10450 (setq files (symbol-value files))))
10451 (if (stringp files) (setq files (list files)))
10453 ((eq (car desc) :tag)
10454 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10455 ((eq (car desc) :todo)
10456 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10457 ((eq (car desc) :regexp)
10458 (setq descre (cdr desc)))
10459 ((eq (car desc) :level)
10460 (setq descre (concat "^\\*\\{" (number-to-string
10461 (if org-odd-levels-only
10462 (1- (* 2 (cdr desc)))
10465 ((eq (car desc) :maxlevel)
10466 (setq fast-path-p t)
10467 (setq descre (concat "^\\*\\{1," (number-to-string
10468 (if org-odd-levels-only
10469 (1- (* 2 (cdr desc)))
10472 (t (error "Bad refiling target description %s" desc)))
10473 (while (setq f (pop files))
10474 (with-current-buffer
10475 (if (bufferp f) f (org-get-agenda-file-buffer f))
10477 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10479 (if (bufferp f) (setq f (buffer-file-name
10480 (buffer-base-buffer f))))
10481 (setq f (and f (expand-file-name f)))
10482 (if (eq org-refile-use-outline-path 'file)
10483 (push (list (file-name-nondirectory f) f nil nil) tgs))
10487 (goto-char (point-min))
10488 (while (re-search-forward descre nil t)
10489 (goto-char (setq pos0 (point-at-bol)))
10491 (when org-refile-target-verify-function
10493 (or (funcall org-refile-target-verify-function)
10495 (when (and (looking-at org-complex-heading-regexp)
10496 (not (member (match-string 4) excluded-entries))
10498 (setq level (org-reduced-level
10499 (- (match-end 1) (match-beginning 1)))
10500 txt (org-link-display-format (match-string 4))
10501 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10502 re (format org-complex-heading-regexp-format
10503 (regexp-quote (match-string 4))))
10504 (when org-refile-use-outline-path
10505 (setq txt (mapconcat
10508 (if (eq org-refile-use-outline-path
10510 (list (file-name-nondirectory
10512 (buffer-base-buffer))))
10513 (if (eq org-refile-use-outline-path
10515 (list (buffer-file-name
10516 (buffer-base-buffer)))))
10517 (org-get-outline-path fast-path-p
10521 (push (list txt f re (org-refile-marker (point)))
10523 (when (= (point) pos0)
10524 ;; verification function has not moved point
10525 (goto-char (point-at-eol))))))))
10526 (when org-refile-use-cache
10527 (org-refile-cache-put tgs (buffer-file-name) descre))
10528 (setq targets (append tgs targets))
10530 (message "Getting targets...done")
10531 (nreverse targets)))
10533 (defun org-protect-slash (s)
10534 (while (string-match "/" s)
10535 (setq s (replace-match "\\" t t s)))
10538 (defvar org-olpa (make-vector 20 nil))
10540 (defun org-get-outline-path (&optional fastp level heading)
10541 "Return the outline path to the current entry, as a list.
10543 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10544 routine which makes outline path derivations for an entire file,
10545 avoiding backtracing. Refile target collection makes use of that."
10549 (error "Outline path failure, more than 19 levels"))
10550 (loop for i from level upto 19 do
10551 (aset org-olpa i nil))
10553 (delq nil (append org-olpa nil))
10554 (aset org-olpa level heading)))
10555 (let (rtn case-fold-search)
10559 (while (org-up-heading-safe)
10560 (when (looking-at org-complex-heading-regexp)
10561 (push (org-match-string-no-properties 4) rtn)))
10564 (defun org-format-outline-path (path &optional width prefix)
10565 "Format the outline path PATH for display.
10566 Width is the maximum number of characters that is available.
10567 Prefix is a prefix to be included in the returned string,
10568 such as the file name."
10569 (setq width (or width 79))
10570 (if prefix (setq width (- width (length prefix))))
10573 (let* ((nsteps (length path))
10574 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10575 (maxwidth (if (<= total-width width)
10576 10000 ;; everything fits
10577 ;; we need to shorten the level headings
10578 (/ (- width nsteps) nsteps)))
10579 (org-odd-levels-only nil)
10581 (total (1+ (length prefix))))
10582 (setq maxwidth (max maxwidth 10))
10587 (if (and (= n nsteps) (< maxwidth 10000))
10588 (setq maxwidth (- total-width total)))
10589 (if (< (length h) maxwidth)
10590 (progn (setq total (+ total (length h) 1)) h)
10591 (setq h (substring h 0 (- maxwidth 2))
10592 total (+ total maxwidth 1))
10593 (if (string-match "[ \t]+\\'" h)
10594 (setq h (substring h 0 (match-beginning 0))))
10595 (setq h (concat h "..")))
10596 (org-add-props h nil 'face
10597 (nth (% (1- n) org-n-level-faces)
10602 (defun org-display-outline-path (&optional file current)
10603 "Display the current outline path in the echo area."
10605 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10606 (case-fold-search nil)
10607 (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
10608 (if current (setq path (append path
10610 (org-back-to-heading t)
10611 (if (looking-at org-complex-heading-regexp)
10612 (list (match-string 4)))))))
10614 (org-format-outline-path
10617 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10619 (defvar org-refile-history nil
10620 "History for refiling operations.")
10622 (defvar org-after-refile-insert-hook nil
10623 "Hook run after `org-refile' has inserted its stuff at the new location.
10624 Note that this is still *before* the stuff will be removed from
10625 the *old* location.")
10627 (defvar org-capture-last-stored-marker)
10628 (defun org-refile (&optional goto default-buffer rfloc)
10629 "Move the entry or entries at point to another heading.
10630 The list of target headings is compiled using the information in
10631 `org-refile-targets', which see.
10633 At the target location, the entry is filed as a subitem of the target
10634 heading. Depending on `org-reverse-note-order', the new subitem will
10635 either be the first or the last subitem.
10637 If there is an active region, all entries in that region will be moved.
10638 However, the region must fulfill the requirement that the first heading
10639 is the first one sets the top-level of the moved text - at most siblings
10640 below it are allowed.
10642 With prefix arg GOTO, the command will only visit the target location
10643 and not actually move anything.
10645 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10646 go to the location where the last refiling operation has put the subtree.
10647 With a prefix argument of `2', refile to the running clock.
10649 RFLOC can be a refile location obtained in a different way.
10651 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10653 If you are using target caching (see `org-refile-use-cache'),
10654 You have to clear the target cache in order to find new targets.
10655 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10656 prefix argument (`C-u C-u C-u C-c C-w')."
10659 (if (member goto '(0 (64)))
10660 (org-refile-cache-clear)
10661 (let* ((cbuf (current-buffer))
10662 (regionp (org-region-active-p))
10663 (region-start (and regionp (region-beginning)))
10664 (region-end (and regionp (region-end)))
10665 (region-length (and regionp (- region-end region-start)))
10666 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10667 pos it nbuf file re level reversed)
10668 (setq last-command nil)
10670 (goto-char region-start)
10671 (or (bolp) (goto-char (point-at-bol)))
10672 (setq region-start (point))
10673 (unless (or (org-kill-is-subtree-p
10674 (buffer-substring region-start region-end))
10675 (prog1 org-refile-active-region-within-subtree
10676 (org-toggle-heading)))
10677 (error "The region is not a (sequence of) subtree(s)")))
10678 (if (equal goto '(16))
10679 (org-refile-goto-last-stored)
10681 (and (equal goto 2)
10682 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10684 (setq it (list (or org-clock-heading "running clock")
10686 (marker-buffer org-clock-hd-marker))
10688 (marker-position org-clock-hd-marker)))
10691 (let (heading-text)
10694 (org-back-to-heading t)
10696 (nth 4 (org-heading-components))))
10697 (org-refile-get-location
10698 (cond (goto "Goto")
10699 (regionp "Refile region to")
10700 (t (concat "Refile subtree \""
10701 heading-text "\" to")))
10703 org-refile-allow-creating-parent-nodes
10705 (setq file (nth 1 it)
10708 (if (and (not goto)
10710 (equal (buffer-file-name) file)
10712 (and (>= pos region-start)
10713 (<= pos region-end))
10714 (and (>= pos (point))
10715 (< pos (save-excursion
10716 (org-end-of-subtree t t))))))
10717 (error "Cannot refile to position inside the tree or region"))
10719 (setq nbuf (or (find-buffer-visiting file)
10720 (find-file-noselect file)))
10723 (org-pop-to-buffer-same-window nbuf)
10725 (org-show-context 'org-goto))
10728 (org-kill-new (buffer-substring region-start region-end))
10729 (org-save-markers-in-region region-start region-end))
10730 (org-copy-subtree 1 nil t))
10731 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10732 (find-file-noselect file)))
10733 (setq reversed (org-notes-order-reversed-p))
10740 (looking-at org-outline-regexp)
10741 (setq level (org-get-valid-level (funcall outline-level) 1))
10744 (or (outline-next-heading) (point-max))
10745 (or (save-excursion (org-get-next-sibling))
10746 (org-end-of-subtree t t)
10750 (goto-char (point-max))
10751 (goto-char (point-min))
10752 (or (outline-next-heading) (goto-char (point-max)))))
10753 (if (not (bolp)) (newline))
10754 (org-paste-subtree level)
10755 (when org-log-refile
10756 (org-add-log-setup 'refile nil nil 'findpos
10758 (unless (eq org-log-refile 'note)
10759 (save-excursion (org-add-log-note))))
10760 (and org-auto-align-tags (org-set-tags nil t))
10761 (bookmark-set "org-refile-last-stored")
10762 ;; If we are refiling for capture, make sure that the
10763 ;; last-capture pointers point here
10764 (when (org-bound-and-true-p org-refile-for-capture)
10765 (bookmark-set "org-capture-last-stored-marker")
10766 (move-marker org-capture-last-stored-marker (point)))
10767 (if (fboundp 'deactivate-mark) (deactivate-mark))
10768 (run-hooks 'org-after-refile-insert-hook))))
10770 (delete-region (point) (+ (point) region-length))
10772 (when (featurep 'org-inlinetask)
10773 (org-inlinetask-remove-END-maybe))
10774 (setq org-markers-to-move nil)
10775 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10777 (defun org-refile-goto-last-stored ()
10778 "Go to the location where the last refile was stored."
10780 (bookmark-jump "org-refile-last-stored")
10781 (message "This is the location of the last refile"))
10783 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10785 "Prompt the user for a refile location, using PROMPT.
10786 PROMPT should not be suffixed with a colon and a space, because
10787 this function appends the default value from
10788 `org-refile-history' automatically, if that is not empty.
10789 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10790 this is used for the GOTO interface."
10791 (let ((org-refile-targets org-refile-targets)
10792 (org-refile-use-outline-path org-refile-use-outline-path)
10794 (when (and (eq major-mode 'org-mode)
10795 (not org-refile-use-cache)
10799 (setq excluded-entries
10800 (append excluded-entries (list (org-get-heading t t)))))))
10801 (setq org-refile-target-table
10802 (org-refile-get-targets default-buffer excluded-entries)))
10803 (unless org-refile-target-table
10804 (error "No refile targets"))
10805 (let* ((prompt (concat prompt
10806 (and (car org-refile-history)
10807 (concat " (default " (car org-refile-history) ")"))
10809 (cbuf (current-buffer))
10810 (partial-completion-mode nil)
10811 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10812 (cfunc (if (and org-refile-use-outline-path
10813 org-outline-path-complete-in-steps)
10814 'org-olpath-completing-read
10815 'org-icompleting-read))
10816 (extra (if org-refile-use-outline-path "/" ""))
10817 (filename (and cfn (expand-file-name cfn)))
10820 (if (and (not (member org-refile-use-outline-path
10821 '(file full-file-path)))
10822 (not (equal filename (nth 1 x))))
10823 (cons (concat (car x) extra " ("
10824 (file-name-nondirectory (nth 1 x)) ")")
10826 (cons (concat (car x) extra) (cdr x))))
10827 org-refile-target-table))
10828 (completion-ignore-case t)
10829 pa answ parent-target child parent old-hist)
10830 (setq old-hist org-refile-history)
10831 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10832 nil 'org-refile-history (car org-refile-history)))
10833 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
10834 (org-refile-check-position pa)
10837 (when (or (not org-refile-history)
10838 (not (eq old-hist org-refile-history))
10839 (not (equal (car pa) (car org-refile-history))))
10840 (setq org-refile-history
10841 (cons (car pa) (if (assoc (car org-refile-history) tbl)
10843 (cdr org-refile-history))))
10844 (if (equal (car org-refile-history) (nth 1 org-refile-history))
10845 (pop org-refile-history)))
10847 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
10849 (setq parent (match-string 1 answ)
10850 child (match-string 2 answ))
10851 (setq parent-target (or (assoc parent tbl)
10852 (assoc (concat parent "/") tbl)))
10853 (when (and parent-target
10854 (or (eq new-nodes t)
10855 (and (eq new-nodes 'confirm)
10856 (y-or-n-p (format "Create new node \"%s\"? "
10858 (org-refile-new-child parent-target child)))
10859 (error "Invalid target location")))))
10861 (defun org-refile-check-position (refile-pointer)
10862 "Check if the refile pointer matches the readline to which it points."
10863 (let* ((file (nth 1 refile-pointer))
10864 (re (nth 2 refile-pointer))
10865 (pos (nth 3 refile-pointer))
10867 (when (org-string-nw-p re)
10868 (setq buffer (if (markerp pos)
10869 (marker-buffer pos)
10870 (or (find-buffer-visiting file)
10871 (find-file-noselect file))))
10872 (with-current-buffer buffer
10877 (beginning-of-line 1)
10878 (unless (org-looking-at-p re)
10879 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
10881 (defun org-refile-new-child (parent-target child)
10882 "Use refile target PARENT-TARGET to add new CHILD below it."
10883 (unless parent-target
10884 (error "Cannot find parent for new node"))
10885 (let ((file (nth 1 parent-target))
10886 (pos (nth 3 parent-target))
10888 (with-current-buffer (or (find-buffer-visiting file)
10889 (find-file-noselect file))
10895 (goto-char (point-max))
10896 (if (not (bolp)) (newline)))
10897 (when (looking-at org-outline-regexp)
10898 (setq level (funcall outline-level))
10899 (org-end-of-subtree t t))
10900 (org-back-over-empty-lines)
10901 (insert "\n" (make-string
10902 (if pos (org-get-valid-level level 1) 1) ?*)
10904 (beginning-of-line 0)
10905 (list (concat (car parent-target) "/" child) file "" (point)))))))
10907 (defun org-olpath-completing-read (prompt collection &rest args)
10908 "Read an outline path like a file name."
10909 (let ((thetable collection)
10910 (org-completion-use-ido nil) ; does not work with ido.
10911 (org-completion-use-iswitchb nil)) ; or iswitchb
10913 'org-icompleting-read prompt
10914 (lambda (string predicate &optional flag)
10915 (let (rtn r f (l (length string)))
10919 (try-completion string thetable))
10922 (setq rtn (all-completions string thetable predicate))
10925 (setq r (substring x l))
10926 (if (string-match " ([^)]*)$" x)
10927 (setq f (match-string 0 x))
10929 (if (string-match "/" r)
10930 (concat string (substring r 0 (match-end 0)) f)
10935 (assoc string thetable)))
10939 ;;;; Dynamic blocks
10941 (defun org-find-dblock (name)
10942 "Find the first dynamic block with name NAME in the buffer.
10943 If not found, stay at current position and return nil."
10946 (goto-char (point-min))
10947 (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
10949 (match-beginning 0))))
10950 (if pos (goto-char pos))
10953 (defconst org-dblock-start-re
10954 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
10955 "Matches the start line of a dynamic block, with parameters.")
10957 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
10958 "Matches the end of a dynamic block.")
10960 (defun org-create-dblock (plist)
10961 "Create a dynamic block section, with parameters taken from PLIST.
10962 PLIST must contain a :name entry which is used as name of the block."
10963 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
10966 (let ((col (current-column))
10967 (name (plist-get plist :name)))
10968 (insert "#+BEGIN: " name)
10970 (if (eq (car plist) :name)
10971 (setq plist (cddr plist))
10972 (insert " " (prin1-to-string (pop plist)))))
10973 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
10974 (beginning-of-line -2)))
10976 (defun org-prepare-dblock ()
10977 "Prepare dynamic block for refresh.
10978 This empties the block, puts the cursor at the insert position and returns
10979 the property list including an extra property :name with the block name."
10980 (unless (looking-at org-dblock-start-re)
10981 (error "Not at a dynamic block"))
10982 (let* ((begdel (1+ (match-end 0)))
10983 (name (org-no-properties (match-string 1)))
10984 (params (append (list :name name)
10985 (read (concat "(" (match-string 3) ")")))))
10987 (beginning-of-line 1)
10988 (skip-chars-forward " \t")
10989 (setq params (plist-put params :indentation-column (current-column))))
10990 (unless (re-search-forward org-dblock-end-re nil t)
10991 (error "Dynamic block not terminated"))
10994 (list :content (buffer-substring
10995 begdel (match-beginning 0)))))
10996 (delete-region begdel (match-beginning 0))
11001 (defun org-map-dblocks (&optional command)
11002 "Apply COMMAND to all dynamic blocks in the current buffer.
11003 If COMMAND is not given, use `org-update-dblock'."
11004 (let ((cmd (or command 'org-update-dblock)))
11006 (goto-char (point-min))
11007 (while (re-search-forward org-dblock-start-re nil t)
11008 (goto-char (match-beginning 0))
11010 (condition-case nil
11012 (error (message "Error during update of dynamic block"))))
11013 (unless (re-search-forward org-dblock-end-re nil t)
11014 (error "Dynamic block not terminated"))))))
11016 (defun org-dblock-update (&optional arg)
11017 "User command for updating dynamic blocks.
11018 Update the dynamic block at point. With prefix ARG, update all dynamic
11019 blocks in the buffer."
11022 (org-update-all-dblocks)
11023 (or (looking-at org-dblock-start-re)
11024 (org-beginning-of-dblock))
11025 (org-update-dblock)))
11027 (defun org-update-dblock ()
11028 "Update the dynamic block at point.
11029 This means to empty the block, parse for parameters and then call
11030 the correct writing function."
11032 (save-window-excursion
11033 (let* ((pos (point))
11034 (line (org-current-line))
11035 (params (org-prepare-dblock))
11036 (name (plist-get params :name))
11037 (indent (plist-get params :indentation-column))
11038 (cmd (intern (concat "org-dblock-write:" name))))
11039 (message "Updating dynamic block `%s' at line %d..." name line)
11040 (funcall cmd params)
11041 (message "Updating dynamic block `%s' at line %d...done" name line)
11043 (when (and indent (> indent 0))
11044 (setq indent (make-string indent ?\ ))
11046 (org-beginning-of-dblock)
11048 (while (not (looking-at org-dblock-end-re))
11050 (beginning-of-line 2))
11051 (when (looking-at org-dblock-end-re)
11052 (and (looking-at "[ \t]+")
11053 (replace-match ""))
11054 (insert indent)))))))
11056 (defun org-beginning-of-dblock ()
11057 "Find the beginning of the dynamic block at point.
11058 Error if there is no such block at point."
11059 (let ((pos (point))
11062 (if (and (re-search-backward org-dblock-start-re nil t)
11063 (setq beg (match-beginning 0))
11064 (re-search-forward org-dblock-end-re nil t)
11065 (> (match-end 0) pos))
11068 (error "Not in a dynamic block"))))
11070 (defun org-update-all-dblocks ()
11071 "Update all dynamic blocks in the buffer.
11072 This function can be used in a hook."
11074 (when (eq major-mode 'org-mode)
11075 (org-map-dblocks 'org-update-dblock)))
11080 (defconst org-additional-option-like-keywords
11081 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11082 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11083 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11084 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11086 "ORGTBL" "TBLFM:" "TBLNAME:"
11087 "BEGIN_EXAMPLE" "END_EXAMPLE"
11088 "BEGIN_QUOTE" "END_QUOTE"
11089 "BEGIN_VERSE" "END_VERSE"
11090 "BEGIN_CENTER" "END_CENTER"
11091 "BEGIN_SRC" "END_SRC"
11092 "BEGIN_RESULT" "END_RESULT"
11094 "HEADER:" "HEADERS:"
11095 "CATEGORY:" "COLUMNS:" "PROPERTY:"
11096 "CAPTION:" "LABEL:"
11102 (defcustom org-structure-template-alist
11104 ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11105 "<src lang=\"?\">\n\n</src>")
11106 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11107 "<example>\n?\n</example>")
11108 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11109 "<quote>\n?\n</quote>")
11110 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11111 "<verse>\n?\n</verse>")
11112 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11113 "<center>\n?\n</center>")
11114 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11115 "<literal style=\"latex\">\n?\n</literal>")
11117 "<literal style=\"latex\">?</literal>")
11118 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11119 "<literal style=\"html\">\n?\n</literal>")
11121 "<literal style=\"html\">?</literal>")
11122 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11126 ("I" "#+INCLUDE %file ?"
11127 "<include file=%file markup=\"?\">")
11129 "Structure completion elements.
11130 This is a list of abbreviation keys and values. The value gets inserted
11131 if you type `<' followed by the key and then press the completion key,
11132 usually `M-TAB'. %file will be replaced by a file name after prompting
11133 for the file using completion. The cursor will be placed at the position
11134 of the `?` in the template.
11135 There are two templates for each key, the first uses the original Org syntax,
11136 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11137 the default when the /org-mtags.el/ module has been loaded. See also the
11138 variable `org-mtags-prefer-muse-templates'."
11139 :group 'org-completion
11141 (string :tag "Key")
11142 (string :tag "Template")
11143 (string :tag "Muse Template")))
11145 (defun org-try-structure-completion ()
11146 "Try to complete a structure template before point.
11147 This looks for strings like \"<e\" on an otherwise empty line and
11149 (let ((l (buffer-substring (point-at-bol) (point)))
11151 (when (and (looking-at "[ \t]*$")
11152 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11153 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11154 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11155 (match-beginning 1)) a)
11158 (defun org-complete-expand-structure-template (start cell)
11159 "Expand a structure template."
11160 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11161 (rpl (nth (if musep 2 1) cell))
11163 (delete-region start (point))
11164 (when (string-match "\\`#\\+" rpl)
11167 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11168 (setq ind (buffer-substring (point-at-bol) (point))))
11170 (setq start (point))
11171 (if (string-match "%file" rpl)
11172 (setq rpl (replace-match
11176 (abbreviate-file-name (read-file-name "Include file: ")))
11179 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11180 (concat "\n" ind)))
11182 (if (re-search-backward "\\?" start t) (delete-char 1))))
11184 ;;;; TODO, DEADLINE, Comments
11186 (defun org-toggle-comment ()
11187 "Change the COMMENT state of an entry."
11190 (org-back-to-heading)
11191 (let (case-fold-search)
11193 ((looking-at (format org-heading-keyword-regexp-format
11194 org-comment-string))
11195 (goto-char (match-end 1))
11196 (looking-at (concat " +" org-comment-string))
11197 (replace-match "" t t)
11198 (when (eolp) (insert " ")))
11199 ((looking-at org-outline-regexp)
11200 (goto-char (match-end 0))
11201 (insert org-comment-string " "))))))
11203 (defvar org-last-todo-state-is-todo nil
11204 "This is non-nil when the last TODO state change led to a TODO state.
11205 If the last change removed the TODO tag or switched to DONE, then
11208 (defvar org-setting-tags nil) ; dynamically skipped
11210 (defvar org-todo-setup-filter-hook nil
11211 "Hook for functions that pre-filter todo specs.
11212 Each function takes a todo spec and returns either nil or the spec
11213 transformed into canonical form." )
11215 (defvar org-todo-get-default-hook nil
11216 "Hook for functions that get a default item for todo.
11217 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11218 nil or a string to be used for the todo mark." )
11220 (defvar org-agenda-headline-snapshot-before-repeat)
11222 (defun org-current-effective-time ()
11223 "Return current time adjusted for `org-extend-today-until' variable"
11224 (let* ((ct (org-current-time))
11225 (dct (decode-time ct))
11227 (if (and org-use-effective-time
11228 (< (nth 2 dct) org-extend-today-until))
11229 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11233 (defun org-todo-yesterday (&optional arg)
11234 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11236 (if (eq major-mode 'org-agenda-mode)
11237 (apply 'org-agenda-todo-yesterday arg)
11238 (let* ((hour (third (decode-time
11239 (org-current-time))))
11240 (org-extend-today-until (1+ hour)))
11243 (defun org-todo (&optional arg)
11244 "Change the TODO state of an item.
11245 The state of an item is given by a keyword at the start of the heading,
11247 *** TODO Write paper
11250 The different keywords are specified in the variable `org-todo-keywords'.
11251 By default the available states are \"TODO\" and \"DONE\".
11252 So for this example: when the item starts with TODO, it is changed to DONE.
11253 When it starts with DONE, the DONE is removed. And when neither TODO nor
11254 DONE are present, add TODO at the beginning of the heading.
11256 With \\[universal-argument] prefix arg, use completion to determine the new \
11258 With numeric prefix arg, switch to that state.
11259 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11260 keywords (nextset).
11261 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11262 With a numeric prefix arg of 0, inhibit note taking for the change.
11264 For calling through lisp, arg is also interpreted in the following way:
11265 'none -> empty state
11266 \"\"(empty string) -> switch to empty state
11267 'done -> switch to DONE
11268 'nextset -> switch to the next set of keywords
11269 'previousset -> switch to the previous set of keywords
11270 \"WAITING\" -> switch to the specified keyword, but only if it
11271 really is a member of `org-todo-keywords'."
11273 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11274 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11275 'region-start-level 'region))
11276 org-loop-over-headlines-in-active-region)
11279 org-loop-over-headlines-in-active-region
11280 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11281 (if (equal arg '(16)) (setq arg 'nextset))
11282 (let ((org-blocker-hook org-blocker-hook)
11283 (case-fold-search nil))
11284 (when (equal arg '(64))
11285 (setq arg nil org-blocker-hook nil))
11286 (when (and org-blocker-hook
11287 (or org-inhibit-blocking
11288 (org-entry-get nil "NOBLOCKING")))
11289 (setq org-blocker-hook nil))
11292 (org-back-to-heading t)
11293 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11294 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11295 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11296 (let* ((match-data (match-data))
11297 (startpos (point-at-bol))
11298 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11299 (org-log-done org-log-done)
11300 (org-log-repeat org-log-repeat)
11301 (org-todo-log-states org-todo-log-states)
11302 (org-inhibit-logging
11304 (progn (setq arg nil) 'note) org-inhibit-logging))
11305 (this (match-string 1))
11306 (hl-pos (match-beginning 0))
11307 (head (org-get-todo-sequence-head this))
11308 (ass (assoc head org-todo-kwd-alist))
11309 (interpret (nth 1 ass))
11310 (done-word (nth 3 ass))
11311 (final-done-word (nth 4 ass))
11312 (last-state (or this ""))
11313 (completion-ignore-case t)
11314 (member (member this org-todo-keywords-1))
11315 (tail (cdr member))
11317 ((and org-todo-key-trigger
11318 (or (and (equal arg '(4))
11319 (eq org-use-fast-todo-selection 'prefix))
11320 (and (not arg) org-use-fast-todo-selection
11321 (not (eq org-use-fast-todo-selection
11323 ;; Use fast selection
11324 (org-fast-todo-selection))
11325 ((and (equal arg '(4))
11326 (or (not org-use-fast-todo-selection)
11327 (not org-todo-key-trigger)))
11328 ;; Read a state with completion
11329 (org-icompleting-read
11330 "State: " (mapcar (lambda(x) (list x))
11331 org-todo-keywords-1)
11335 (if tail (car tail) nil)
11336 (car org-todo-keywords-1)))
11338 (if (equal member org-todo-keywords-1)
11341 (nth (- (length org-todo-keywords-1)
11343 org-todo-keywords-1)
11344 (org-last org-todo-keywords-1))))
11345 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11346 (setq arg nil))) ; hack to fall back to cycling
11348 ;; user or caller requests a specific state
11350 ((equal arg "") nil)
11351 ((eq arg 'none) nil)
11352 ((eq arg 'done) (or done-word (car org-done-keywords)))
11354 (or (car (cdr (member head org-todo-heads)))
11355 (car org-todo-heads)))
11356 ((eq arg 'previousset)
11357 (let ((org-todo-heads (reverse org-todo-heads)))
11358 (or (car (cdr (member head org-todo-heads)))
11359 (car org-todo-heads))))
11360 ((car (member arg org-todo-keywords-1)))
11362 (error "State `%s' not valid in this file" arg))
11363 ((nth (1- (prefix-numeric-value arg))
11364 org-todo-keywords-1))))
11365 ((null member) (or head (car org-todo-keywords-1)))
11366 ((equal this final-done-word) nil) ;; -> make empty
11367 ((null tail) nil) ;; -> first entry
11368 ((memq interpret '(type priority))
11369 (if (eq this-command last-command)
11371 (if (> (length tail) 0)
11372 (or done-word (car org-done-keywords))
11377 (run-hook-with-args-until-success
11378 'org-todo-get-default-hook state last-state)
11380 (next (if state (concat " " state " ") " "))
11381 (change-plist (list :type 'todo-state-change :from this :to state
11382 :position startpos))
11384 (when org-blocker-hook
11385 (setq org-last-todo-state-is-todo
11386 (not (member this org-done-keywords)))
11387 (unless (save-excursion
11389 (org-with-wide-buffer
11390 (run-hook-with-args-until-failure
11391 'org-blocker-hook change-plist))))
11392 (if (org-called-interactively-p 'interactive)
11393 (error "TODO state change from %s to %s blocked" this state)
11395 (message "TODO state change from %s to %s blocked" this state)
11396 (throw 'exit nil))))
11397 (store-match-data match-data)
11398 (replace-match next t t)
11399 (unless (pos-visible-in-window-p hl-pos)
11400 (message "TODO state changed to %s" (org-trim next)))
11402 (setq head (org-get-todo-sequence-head state)
11403 ass (assoc head org-todo-kwd-alist)
11404 interpret (nth 1 ass)
11405 done-word (nth 3 ass)
11406 final-done-word (nth 4 ass)))
11407 (when (memq arg '(nextset previousset))
11408 (message "Keyword-Set %d/%d: %s"
11409 (- (length org-todo-sets) -1
11410 (length (memq (assoc state org-todo-sets) org-todo-sets)))
11411 (length org-todo-sets)
11412 (mapconcat 'identity (assoc state org-todo-sets) " ")))
11413 (setq org-last-todo-state-is-todo
11414 (not (member state org-done-keywords)))
11415 (setq now-done-p (and (member state org-done-keywords)
11416 (not (member this org-done-keywords))))
11417 (and logging (org-local-logging logging))
11418 (when (and (or org-todo-log-states org-log-done)
11419 (not (eq org-inhibit-logging t))
11420 (not (memq arg '(nextset previousset))))
11421 ;; we need to look at recording a time and note
11422 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
11423 (nth 2 (assoc this org-todo-log-states))))
11424 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11425 (setq dolog 'time))
11427 (member state org-not-done-keywords)
11428 (not (member this org-not-done-keywords)))
11429 ;; This is now a todo state and was not one before
11430 ;; If there was a CLOSED time stamp, get rid of it.
11431 (org-add-planning-info nil nil 'closed))
11432 (when (and now-done-p org-log-done)
11433 ;; It is now done, and it was not done before
11434 (org-add-planning-info 'closed (org-current-effective-time))
11435 (if (and (not dolog) (eq 'note org-log-done))
11436 (org-add-log-setup 'done state this 'findpos 'note)))
11437 (when (and state dolog)
11438 ;; This is a non-nil state, and we need to log it
11439 (org-add-log-setup 'state state this 'findpos dolog)))
11440 ;; Fixup tag positioning
11441 (org-todo-trigger-tag-changes state)
11442 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11443 (when org-provide-todo-statistics
11444 (org-update-parent-todo-statistics))
11445 (run-hooks 'org-after-todo-state-change-hook)
11446 (if (and arg (not (member state org-done-keywords)))
11447 (setq head (org-get-todo-sequence-head state)))
11448 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11449 ;; Do we need to trigger a repeat?
11451 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11452 ;; This is for the agenda, take a snapshot of the headline.
11454 (setq org-agenda-headline-snapshot-before-repeat
11455 (org-get-heading))))
11456 (org-auto-repeat-maybe state))
11457 ;; Fixup cursor location if close to the keyword
11458 (if (and (outline-on-heading-p)
11460 (save-excursion (beginning-of-line 1)
11461 (looking-at org-todo-line-regexp))
11462 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11464 (goto-char (or (match-end 2) (match-end 1)))
11465 (and (looking-at " ") (just-one-space))))
11466 (when org-trigger-hook
11468 (run-hook-with-args 'org-trigger-hook change-plist)))))))))
11470 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11471 "Block turning an entry into a TODO, using the hierarchy.
11472 This checks whether the current task should be blocked from state
11473 changes. Such blocking occurs when:
11475 1. The task has children which are not all in a completed state.
11477 2. A task has a parent with the property :ORDERED:, and there
11478 are siblings prior to the current task with incomplete
11481 3. The parent of the task is blocked because it has siblings that should
11482 be done first, or is child of a block grandparent TODO entry."
11484 (if (not org-enforce-todo-dependencies)
11485 t ; if locally turned off don't block
11487 ;; If this is not a todo state change, or if this entry is already DONE,
11489 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11490 (member (plist-get change-plist :from)
11491 (cons 'done org-done-keywords))
11492 (member (plist-get change-plist :to)
11493 (cons 'todo org-not-done-keywords))
11494 (not (plist-get change-plist :to)))
11495 (throw 'dont-block t))
11496 ;; If this task has children, and any are undone, it's blocked
11498 (org-back-to-heading t)
11499 (let ((this-level (funcall outline-level)))
11500 (outline-next-heading)
11501 (let ((child-level (funcall outline-level)))
11502 (while (and (not (eobp))
11503 (> child-level this-level))
11504 ;; this todo has children, check whether they are all
11506 (if (and (not (org-entry-is-done-p))
11507 (org-entry-is-todo-p))
11508 (throw 'dont-block nil))
11509 (outline-next-heading)
11510 (setq child-level (funcall outline-level))))))
11511 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11512 ;; any previous siblings are undone, it's blocked
11514 (org-back-to-heading t)
11515 (let* ((pos (point))
11516 (parent-pos (and (org-up-heading-safe) (point))))
11517 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11518 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11520 (re-search-forward org-not-done-heading-regexp pos t))
11521 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11522 ;; Search further up the hierarchy, to see if an ancestor is blocked
11524 (goto-char parent-pos)
11525 (if (not (looking-at org-not-done-heading-regexp))
11526 (throw 'dont-block t)) ; do not block, parent is not a TODO
11528 (setq parent-pos (and (org-up-heading-safe) (point)))
11529 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11530 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11532 (re-search-forward org-not-done-heading-regexp pos t))
11533 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11535 (defcustom org-track-ordered-property-with-tag nil
11536 "Should the ORDERED property also be shown as a tag?
11537 The ORDERED property decides if an entry should require subtasks to be
11538 completed in sequence. Since a property is not very visible, setting
11539 this option means that toggling the ORDERED property with the command
11540 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11541 not relevant for the behavior, but it makes things more visible.
11543 Note that toggling the tag with tags commands will not change the property
11544 and therefore not influence behavior!
11546 This can be t, meaning the tag ORDERED should be used, It can also be a
11547 string to select a different tag for this task."
11550 (const :tag "No tracking" nil)
11551 (const :tag "Track with ORDERED tag" t)
11552 (string :tag "Use other tag")))
11554 (defun org-toggle-ordered-property ()
11555 "Toggle the ORDERED property of the current entry.
11556 For better visibility, you can track the value of this property with a tag.
11557 See variable `org-track-ordered-property-with-tag'."
11559 (let* ((t1 org-track-ordered-property-with-tag)
11560 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11562 (org-back-to-heading)
11563 (if (org-entry-get nil "ORDERED")
11565 (org-delete-property "ORDERED")
11566 (and tag (org-toggle-tag tag 'off))
11567 (message "Subtasks can be completed in arbitrary order"))
11568 (org-entry-put nil "ORDERED" "t")
11569 (and tag (org-toggle-tag tag 'on))
11570 (message "Subtasks must be completed in sequence")))))
11572 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11573 (defun org-block-todo-from-checkboxes (change-plist)
11574 "Block turning an entry into a TODO, using checkboxes.
11575 This checks whether the current task should be blocked from state
11576 changes because there are unchecked boxes in this entry."
11577 (if (not org-enforce-todo-checkbox-dependencies)
11578 t ; if locally turned off don't block
11580 ;; If this is not a todo state change, or if this entry is already DONE,
11582 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11583 (member (plist-get change-plist :from)
11584 (cons 'done org-done-keywords))
11585 (member (plist-get change-plist :to)
11586 (cons 'todo org-not-done-keywords))
11587 (not (plist-get change-plist :to)))
11588 (throw 'dont-block t))
11589 ;; If this task has checkboxes that are not checked, it's blocked
11591 (org-back-to-heading t)
11592 (let ((beg (point)) end)
11593 (outline-next-heading)
11596 (if (org-list-search-forward
11597 (concat (org-item-beginning-re)
11598 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11602 (if (boundp 'org-blocked-by-checkboxes)
11603 (setq org-blocked-by-checkboxes t))
11604 (throw 'dont-block nil)))))
11605 t))) ; do not block
11607 (defun org-entry-blocked-p ()
11608 "Is the current entry blocked?"
11609 (if (org-entry-get nil "NOBLOCKING")
11610 nil ;; Never block this entry
11612 (run-hook-with-args-until-failure
11614 (list :type 'todo-state-change
11619 (defun org-update-statistics-cookies (all)
11620 "Update the statistics cookie, either from TODO or from checkboxes.
11621 This should be called with the cursor in a line with a statistics cookie."
11625 (org-update-checkbox-count 'all)
11626 (org-map-entries 'org-update-parent-todo-statistics))
11627 (if (not (org-at-heading-p))
11628 (org-update-checkbox-count)
11629 (let ((pos (move-marker (make-marker) (point)))
11631 (ignore-errors (org-back-to-heading t))
11632 (if (not (org-at-heading-p))
11633 (org-update-checkbox-count)
11634 (setq l1 (org-outline-level))
11635 (setq end (save-excursion
11636 (outline-next-heading)
11637 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11639 (if (and (save-excursion
11641 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11642 (not (save-excursion (re-search-forward
11643 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11644 (org-update-checkbox-count)
11645 (if (and l2 (> l2 l1))
11648 (org-update-parent-todo-statistics))
11650 (beginning-of-line 1)
11651 (while (re-search-forward
11652 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11654 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11656 (move-marker pos nil)))))
11658 (defvar org-entry-property-inherited-from) ;; defined below
11659 (defun org-update-parent-todo-statistics ()
11660 "Update any statistics cookie in the parent of the current headline.
11661 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11662 the entire subtree and this will travel up the hierarchy and update
11663 statistics everywhere."
11664 (let* ((prop (save-excursion (org-up-heading-safe)
11665 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11666 (recursive (or (not org-hierarchical-todo-statistics)
11667 (and prop (string-match "\\<recursive\\>" prop))))
11668 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11671 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11672 level ltoggle l1 new ndel
11673 (cnt-all 0) (cnt-done 0) is-percent kwd
11674 checkbox-beg ov ovs ove cookie-present)
11677 (beginning-of-line 1)
11678 (setq ltoggle (funcall outline-level))
11679 ;; Three situations are to consider:
11681 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11682 ;; to the top-level ancestor on the headline;
11684 ;; 2. If parent has "recursive" property, repeat up to the
11685 ;; headline setting that property, taking inheritance into
11688 ;; 3. Else, move up to direct parent and proceed only once.
11689 (while (and (setq level (org-up-heading-safe))
11690 (or recursive first)
11692 (setq first nil cookie-present nil)
11696 (downcase (or (org-entry-get nil "COOKIE_DATA")
11699 (while (re-search-forward box-re (point-at-eol) t)
11700 (setq cnt-all 0 cnt-done 0 cookie-present t)
11701 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11703 (unless (outline-next-heading) (throw 'exit nil))
11704 (while (and (looking-at org-complex-heading-regexp)
11705 (> (setq l1 (length (match-string 1))) level))
11706 (setq kwd (and (or recursive (= l1 ltoggle))
11708 (if (or (eq org-provide-todo-statistics 'all-headlines)
11709 (and (listp org-provide-todo-statistics)
11710 (or (member kwd org-provide-todo-statistics)
11711 (member kwd org-done-keywords))))
11712 (setq cnt-all (1+ cnt-all))
11713 (if (eq org-provide-todo-statistics t)
11714 (and kwd (setq cnt-all (1+ cnt-all)))))
11715 (and (member kwd org-done-keywords)
11716 (setq cnt-done (1+ cnt-done)))
11717 (outline-next-heading)))
11720 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11721 (format "[%d/%d]" cnt-done cnt-all))
11722 ndel (- (match-end 0) checkbox-beg))
11723 ;; handle overlays when updating cookie from column view
11724 (when (setq ov (car (overlays-at checkbox-beg)))
11725 (setq ovs (overlay-start ov) ove (overlay-end ov))
11726 (delete-overlay ov))
11727 (goto-char checkbox-beg)
11729 (delete-region (point) (+ (point) ndel))
11730 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11731 (when ov (move-overlay ov ovs ove)))
11732 (when cookie-present
11733 (run-hook-with-args 'org-after-todo-statistics-hook
11734 cnt-done (- cnt-all cnt-done))))))
11735 (run-hooks 'org-todo-statistics-hook)))
11737 (defvar org-after-todo-statistics-hook nil
11738 "Hook that is called after a TODO statistics cookie has been updated.
11739 Each function is called with two arguments: the number of not-done entries
11740 and the number of done entries.
11742 For example, the following function, when added to this hook, will switch
11743 an entry to DONE when all children are done, and back to TODO when new
11744 entries are set to a TODO status. Note that this hook is only called
11745 when there is a statistics cookie in the headline!
11747 (defun org-summary-todo (n-done n-not-done)
11748 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11749 (let (org-log-done org-log-states) ; turn off logging
11750 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11753 (defvar org-todo-statistics-hook nil
11754 "Hook that is run whenever Org thinks TODO statistics should be updated.
11755 This hook runs even if there is no statistics cookie present, in which case
11756 `org-after-todo-statistics-hook' would not run.")
11758 (defun org-todo-trigger-tag-changes (state)
11759 "Apply the changes defined in `org-todo-state-tags-triggers'."
11760 (let ((l org-todo-state-tags-triggers)
11762 (when (or (not state) (equal state ""))
11763 (setq changes (append changes (cdr (assoc "" l)))))
11764 (when (and (stringp state) (> (length state) 0))
11765 (setq changes (append changes (cdr (assoc state l)))))
11766 (when (member state org-not-done-keywords)
11767 (setq changes (append changes (cdr (assoc 'todo l)))))
11768 (when (member state org-done-keywords)
11769 (setq changes (append changes (cdr (assoc 'done l)))))
11770 (dolist (c changes)
11771 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11773 (defun org-local-logging (value)
11774 "Get logging settings from a property VALUE."
11776 ;; directly set the variables, they are already local.
11777 (setq org-log-done nil
11779 org-todo-log-states nil)
11780 (setq words (org-split-string value))
11781 (while (setq w (pop words))
11783 ((setq a (assoc w org-startup-options))
11784 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11785 (set (nth 1 a) (nth 2 a))))
11786 ((setq a (org-extract-log-state-settings w))
11787 (and (member (car a) org-todo-keywords-1)
11788 (push a org-todo-log-states)))))))
11790 (defun org-get-todo-sequence-head (kwd)
11791 "Return the head of the TODO sequence to which KWD belongs.
11792 If KWD is not set, check if there is a text property remembering the
11797 (or (get-text-property (point-at-bol) 'org-todo-head)
11799 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11800 nil (point-at-eol)))
11801 (get-text-property p 'org-todo-head))))
11802 ((not (member kwd org-todo-keywords-1))
11803 (car org-todo-keywords-1))
11804 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
11806 (defun org-fast-todo-selection ()
11807 "Fast TODO keyword selection with single keys.
11808 Returns the new TODO keyword, or nil if no state change should occur."
11809 (let* ((fulltable org-todo-key-alist)
11810 (done-keywords org-done-keywords) ;; needed for the faces.
11811 (maxlen (apply 'max (mapcar
11813 (if (stringp (car x)) (string-width (car x)) 0))
11816 (fwidth (+ maxlen 3 1 3))
11817 (ncol (/ (- (window-width) 4) fwidth))
11821 (save-window-excursion
11823 (set-buffer (get-buffer-create " *Org todo*"))
11824 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
11826 (org-set-local 'org-done-keywords done-keywords)
11827 (setq tbl fulltable cnt 0)
11828 (while (setq e (pop tbl))
11830 ((equal e '(:startgroup))
11831 (push '() groups) (setq ingroup t)
11832 (when (not (= cnt 0))
11836 ((equal e '(:endgroup))
11837 (setq ingroup nil cnt 0)
11839 ((equal e '(:newline))
11840 (when (not (= cnt 0))
11844 (while (equal (car tbl) '(:newline))
11846 (setq tbl (cdr tbl)))))
11848 (setq tg (car e) c (cdr e))
11849 (if ingroup (push tg (car groups)))
11850 (setq tg (org-add-props tg nil 'face
11851 (org-get-todo-face tg)))
11852 (if (and (= cnt 0) (not ingroup)) (insert " "))
11853 (insert "[" c "] " tg (make-string
11854 (- fwidth 4 (length tg)) ?\ ))
11855 (when (= (setq cnt (1+ cnt)) ncol)
11857 (if ingroup (insert " "))
11860 (goto-char (point-min))
11861 (if (not expert) (org-fit-window-to-buffer))
11862 (message "[a-z..]:Set [SPC]:clear")
11863 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11866 (and (= c ?q) (not (rassoc c fulltable))))
11867 (setq quit-flag t))
11869 ((setq e (rassoc c fulltable) tg (car e))
11871 (t (setq quit-flag t)))))))
11873 (defun org-entry-is-todo-p ()
11874 (member (org-get-todo-state) org-not-done-keywords))
11876 (defun org-entry-is-done-p ()
11877 (member (org-get-todo-state) org-done-keywords))
11879 (defun org-get-todo-state ()
11881 (org-back-to-heading t)
11882 (and (looking-at org-todo-line-regexp)
11884 (match-string 2))))
11886 (defun org-at-date-range-p (&optional inactive-ok)
11887 "Is the cursor inside a date range?"
11891 (let ((pos (point)))
11892 (skip-chars-backward "^[<\r\n")
11893 (skip-chars-backward "<[")
11894 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11895 (>= (match-end 0) pos)
11897 (skip-chars-backward "^<[\r\n")
11898 (skip-chars-backward "<[")
11899 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11900 (>= (match-end 0) pos)
11904 (defun org-get-repeat (&optional tagline)
11905 "Check if there is a deadline/schedule with repeater in this entry."
11908 (org-back-to-heading t)
11909 (and (re-search-forward (if tagline
11910 (concat tagline "\\s-*" org-repeat-re)
11912 (org-entry-end-position) t)
11913 (match-string-no-properties 1)))))
11915 (defvar org-last-changed-timestamp)
11916 (defvar org-last-inserted-timestamp)
11917 (defvar org-log-post-message)
11918 (defvar org-log-note-purpose)
11919 (defvar org-log-note-how)
11920 (defvar org-log-note-extra)
11921 (defun org-auto-repeat-maybe (done-word)
11922 "Check if the current headline contains a repeated deadline/schedule.
11923 If yes, set TODO state back to what it was and change the base date
11924 of repeating deadline/scheduled time stamps to new date.
11925 This function is run automatically after each state change to a DONE state."
11926 ;; last-state is dynamically scoped into this function
11927 (let* ((repeat (org-get-repeat))
11928 (aa (assoc last-state org-todo-kwd-alist))
11929 (interpret (nth 1 aa))
11931 (whata '(("d" . day) ("m" . month) ("y" . year)))
11932 (msg "Entry repeats: ")
11934 (org-todo-log-states nil)
11935 re type n what ts time to-state)
11937 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
11938 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
11939 org-todo-repeat-to-state))
11940 (unless (and to-state (member to-state org-todo-keywords-1))
11941 (setq to-state (if (eq interpret 'type) last-state head)))
11942 (org-todo to-state)
11943 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
11944 (org-entry-put nil "LAST_REPEAT" (format-time-string
11945 (org-time-stamp-format t t))))
11946 (when org-log-repeat
11947 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
11948 (memq 'org-add-log-note post-command-hook))
11949 ;; OK, we are already setup for some record
11950 (if (eq org-log-repeat 'note)
11951 ;; make sure we take a note, not only a time stamp
11952 (setq org-log-note-how 'note))
11953 ;; Set up for taking a record
11954 (org-add-log-setup 'state (or done-word (car org-done-keywords))
11956 'findpos org-log-repeat)))
11957 (org-back-to-heading t)
11958 (org-add-planning-info nil nil 'closed)
11959 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
11960 org-deadline-time-regexp "\\)\\|\\("
11961 org-ts-regexp "\\)"))
11962 (while (re-search-forward
11963 re (save-excursion (outline-next-heading) (point)) t)
11964 (setq type (if (match-end 1) org-scheduled-string
11965 (if (match-end 3) org-deadline-string "Plain:"))
11966 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
11967 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
11968 (setq n (string-to-number (match-string 2 ts))
11969 what (match-string 3 ts))
11970 (if (equal what "w") (setq n (* n 7) what "d"))
11971 ;; Preparation, see if we need to modify the start date for the change
11972 (when (match-end 1)
11973 (setq time (save-match-data (org-time-string-to-time ts)))
11975 ((equal (match-string 1 ts) ".")
11976 ;; Shift starting date to today
11977 (org-timestamp-change
11978 (- (org-today) (time-to-days time))
11980 ((equal (match-string 1 ts) "+")
11981 (let ((nshiftmax 10) (nshift 0))
11982 (while (or (= nshift 0)
11983 (<= (time-to-days time)
11984 (time-to-days (current-time))))
11985 (when (= (incf nshift) nshiftmax)
11986 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
11988 (org-timestamp-change n (cdr (assoc what whata)))
11989 (org-at-timestamp-p t)
11990 (setq ts (match-string 1))
11991 (setq time (save-match-data (org-time-string-to-time ts)))))
11992 (org-timestamp-change (- n) (cdr (assoc what whata)))
11993 ;; rematch, so that we have everything in place for the real shift
11994 (org-at-timestamp-p t)
11995 (setq ts (match-string 1))
11996 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
11997 (org-timestamp-change n (cdr (assoc what whata)))
11998 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
11999 (setq org-log-post-message msg)
12000 (message "%s" msg))))
12002 (defun org-show-todo-tree (arg)
12003 "Make a compact tree which shows all headlines marked with TODO.
12004 The tree will show the lines where the regexp matches, and all higher
12005 headlines above the match.
12006 With a \\[universal-argument] prefix, prompt for a regexp to match.
12007 With a numeric prefix N, construct a sparse tree for the Nth element
12008 of `org-todo-keywords-1'."
12010 (let ((case-fold-search nil)
12012 (cond ((null arg) org-not-done-regexp)
12014 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12015 (mapcar 'list org-todo-keywords-1))))
12017 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12019 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12020 (regexp-quote (nth (1- (prefix-numeric-value arg))
12021 org-todo-keywords-1)))
12022 (t (error "Invalid prefix argument: %s" arg)))))
12023 (message "%d TODO entries found"
12024 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12026 (defun org-deadline (&optional remove time)
12027 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12028 With argument REMOVE, remove any deadline from the item.
12029 With argument TIME, set the deadline at the corresponding date. TIME
12030 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12032 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12033 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12034 'region-start-level 'region))
12035 org-loop-over-headlines-in-active-region)
12037 `(org-deadline ',remove ,time)
12038 org-loop-over-headlines-in-active-region
12039 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12040 (let* ((old-date (org-entry-get nil "DEADLINE"))
12041 (repeater (and old-date
12043 "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
12045 (match-string 1 old-date))))
12048 (when (and old-date org-log-redeadline)
12049 (org-add-log-setup 'deldeadline nil old-date 'findpos
12050 org-log-redeadline))
12051 (org-remove-timestamp-with-keyword org-deadline-string)
12052 (message "Item no longer has a deadline."))
12053 (org-add-planning-info 'deadline time 'closed)
12054 (when (and old-date org-log-redeadline
12055 (not (equal old-date
12056 (substring org-last-inserted-timestamp 1 -1))))
12057 (org-add-log-setup 'redeadline nil old-date 'findpos
12058 org-log-redeadline))
12061 (org-back-to-heading t)
12062 (when (re-search-forward (concat org-deadline-string " "
12063 org-last-inserted-timestamp)
12065 (outline-next-heading) (point)) t)
12066 (goto-char (1- (match-end 0)))
12067 (insert " " repeater)
12068 (setq org-last-inserted-timestamp
12069 (concat (substring org-last-inserted-timestamp 0 -1)
12071 (substring org-last-inserted-timestamp -1))))))
12072 (message "Deadline on %s" org-last-inserted-timestamp)))))
12074 (defun org-schedule (&optional remove time)
12075 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12076 With argument REMOVE, remove any scheduling date from the item.
12077 With argument TIME, scheduled at the corresponding date. TIME can
12078 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12080 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12081 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12082 'region-start-level 'region))
12083 org-loop-over-headlines-in-active-region)
12085 `(org-schedule ',remove ,time)
12086 org-loop-over-headlines-in-active-region
12087 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12088 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12089 (repeater (and old-date
12091 "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
12093 (match-string 1 old-date))))
12096 (when (and old-date org-log-reschedule)
12097 (org-add-log-setup 'delschedule nil old-date 'findpos
12098 org-log-reschedule))
12099 (org-remove-timestamp-with-keyword org-scheduled-string)
12100 (message "Item is no longer scheduled."))
12101 (org-add-planning-info 'scheduled time 'closed)
12102 (when (and old-date org-log-reschedule
12103 (not (equal old-date
12104 (substring org-last-inserted-timestamp 1 -1))))
12105 (org-add-log-setup 'reschedule nil old-date 'findpos
12106 org-log-reschedule))
12109 (org-back-to-heading t)
12110 (when (re-search-forward (concat org-scheduled-string " "
12111 org-last-inserted-timestamp)
12113 (outline-next-heading) (point)) t)
12114 (goto-char (1- (match-end 0)))
12115 (insert " " repeater)
12116 (setq org-last-inserted-timestamp
12117 (concat (substring org-last-inserted-timestamp 0 -1)
12119 (substring org-last-inserted-timestamp -1))))))
12120 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12122 (defun org-get-scheduled-time (pom &optional inherit)
12123 "Get the scheduled time as a time tuple, of a format suitable
12124 for calling org-schedule with, or if there is no scheduling,
12126 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12128 (apply 'encode-time (org-parse-time-string time)))))
12130 (defun org-get-deadline-time (pom &optional inherit)
12131 "Get the deadline as a time tuple, of a format suitable for
12132 calling org-deadline with, or if there is no scheduling, returns
12134 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12136 (apply 'encode-time (org-parse-time-string time)))))
12138 (defun org-remove-timestamp-with-keyword (keyword)
12139 "Remove all time stamps with KEYWORD in the current entry."
12140 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12143 (org-back-to-heading t)
12145 (outline-next-heading)
12146 (while (re-search-backward re beg t)
12148 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12149 (equal (char-before) ?\ ))
12150 (backward-delete-char 1)
12151 (if (string-match "^[ \t]*$" (buffer-substring
12152 (point-at-bol) (point-at-eol)))
12153 (delete-region (point-at-bol)
12154 (min (point-max) (1+ (point-at-eol))))))))))
12156 (defun org-add-planning-info (what &optional time &rest remove)
12157 "Insert new timestamp with keyword in the line directly after the headline.
12158 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12159 If non is given, the user is prompted for a date.
12160 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12163 (let (org-time-was-given org-end-time-was-given ts
12164 end default-time default-input)
12167 (when (and (memq what '(scheduled deadline))
12169 (and (stringp time)
12170 (string-match "^[-+]+[0-9]" time))))
12171 ;; Try to get a default date/time from existing timestamp
12173 (org-back-to-heading t)
12174 (setq end (save-excursion (outline-next-heading) (point)))
12175 (when (re-search-forward (if (eq what 'scheduled)
12176 org-scheduled-time-regexp
12177 org-deadline-time-regexp)
12179 (setq ts (match-string 1)
12181 (apply 'encode-time (org-parse-time-string ts))
12182 default-input (and ts (org-get-compact-tod ts))))))
12186 ;; This is a string (relative or absolute), set proper date
12187 (apply 'encode-time
12188 (org-read-date-analyze
12189 time default-time (decode-time default-time)))
12190 ;; If necessary, get the time from the user
12191 (or time (org-read-date nil 'to-time nil nil
12192 default-time default-input)))))
12194 (when (and org-insert-labeled-timestamps-at-point
12195 (member what '(scheduled deadline)))
12197 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12198 (org-insert-time-stamp time org-time-was-given
12199 nil nil nil (list org-end-time-was-given))
12203 (let (col list elt ts buffer-invisibility-spec)
12204 (org-back-to-heading t)
12205 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12206 (goto-char (match-end 1))
12207 (setq col (current-column))
12208 (goto-char (match-end 0))
12209 (if (eobp) (insert "\n") (forward-char 1))
12210 (when (and (not what)
12213 org-keyword-time-not-clock-regexp))))
12214 ;; Nothing to add, nothing to remove...... :-)
12216 (if (and (not (looking-at org-outline-regexp))
12217 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12219 (not (equal (match-string 1) org-clock-string)))
12220 (narrow-to-region (match-beginning 0) (match-end 0))
12221 (insert-before-markers "\n")
12223 (narrow-to-region (point) (point))
12224 (and org-adapt-indentation (org-indent-to-column col)))
12225 ;; Check if we have to remove something.
12226 (setq list (cons what remove))
12228 (setq elt (pop list))
12229 (when (or (and (eq elt 'scheduled)
12230 (re-search-forward org-scheduled-time-regexp nil t))
12231 (and (eq elt 'deadline)
12232 (re-search-forward org-deadline-time-regexp nil t))
12233 (and (eq elt 'closed)
12234 (re-search-forward org-closed-time-regexp nil t)))
12236 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12237 (and (looking-at "[ \t]+") (replace-match ""))
12238 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12241 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12242 (cond ((eq what 'scheduled) org-scheduled-string)
12243 ((eq what 'deadline) org-deadline-string)
12244 ((eq what 'closed) org-closed-string))
12246 (setq ts (org-insert-time-stamp
12248 (or org-time-was-given
12249 (and (eq what 'closed) org-log-done-with-time))
12251 nil nil (list org-end-time-was-given)))
12253 (if (not (or (bolp) (eq (char-before) ?\ )
12254 (memq (char-after) '(32 10))
12257 (goto-char (point-min))
12259 (if (and (looking-at "[ \t]*\n")
12260 (equal (char-before) ?\n))
12261 (delete-region (1- (point)) (point-at-eol)))
12264 (defvar org-log-note-marker (make-marker))
12265 (defvar org-log-note-purpose nil)
12266 (defvar org-log-note-state nil)
12267 (defvar org-log-note-previous-state nil)
12268 (defvar org-log-note-how nil)
12269 (defvar org-log-note-extra nil)
12270 (defvar org-log-note-window-configuration nil)
12271 (defvar org-log-note-return-to (make-marker))
12272 (defvar org-log-note-effective-time nil
12273 "Remembered current time so that dynamically scoped
12274 `org-extend-today-until' affects tha timestamps in state change
12277 (defvar org-log-post-message nil
12278 "Message to be displayed after a log note has been stored.
12279 The auto-repeater uses this.")
12281 (defun org-add-note ()
12282 "Add a note to the current entry.
12283 This is done in the same way as adding a state change note."
12285 (org-add-log-setup 'note nil nil 'findpos nil))
12287 (defvar org-property-end-re)
12288 (defun org-add-log-setup (&optional purpose state prev-state
12290 "Set up the post command hook to take a note.
12291 If this is about to TODO state change, the new state is expected in STATE.
12292 When FINDPOS is non-nil, find the correct position for the note in
12293 the current entry. If not, assume that it can be inserted at point.
12294 HOW is an indicator what kind of note should be created.
12295 EXTRA is additional text that will be inserted into the notes buffer."
12296 (let* ((org-log-into-drawer (org-log-into-drawer))
12297 (drawer (cond ((stringp org-log-into-drawer)
12298 org-log-into-drawer)
12299 (org-log-into-drawer "LOGBOOK")
12304 (org-back-to-heading t)
12305 (narrow-to-region (point) (save-excursion
12306 (outline-next-heading) (point)))
12307 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12308 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12310 (goto-char (match-end 0))
12313 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12316 (goto-char (match-end 0))
12317 (or org-log-states-order-reversed
12318 (and (re-search-forward org-property-end-re nil t)
12319 (goto-char (1- (match-beginning 0))))))
12320 (insert "\n:" drawer ":\n:END:")
12321 (beginning-of-line 0)
12322 (org-indent-line-function)
12323 (beginning-of-line 2)
12324 (org-indent-line-function)
12326 ((and org-log-state-notes-insert-after-drawers
12328 (forward-line) (looking-at org-drawer-regexp)))
12330 (while (looking-at org-drawer-regexp)
12331 (goto-char (match-end 0))
12332 (re-search-forward org-property-end-re (point-max) t)
12334 (forward-line -1)))
12335 (unless org-log-states-order-reversed
12336 (and (= (char-after) ?\n) (forward-char 1))
12337 (org-skip-over-state-notes)
12338 (skip-chars-backward " \t\n\r")))
12339 (move-marker org-log-note-marker (point))
12340 (setq org-log-note-purpose purpose
12341 org-log-note-state state
12342 org-log-note-previous-state prev-state
12343 org-log-note-how how
12344 org-log-note-extra extra
12345 org-log-note-effective-time (org-current-effective-time))
12346 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12348 (defun org-skip-over-state-notes ()
12349 "Skip past the list of State notes in an entry."
12350 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12351 (when (ignore-errors (goto-char (org-in-item-p)))
12352 (let* ((struct (org-list-struct))
12353 (prevs (org-list-prevs-alist struct)))
12354 (while (looking-at "[ \t]*- State")
12355 (goto-char (or (org-list-get-next-item (point) struct prevs)
12356 (org-list-get-item-end (point) struct)))))))
12358 (defun org-add-log-note (&optional purpose)
12359 "Pop up a window for taking a note, and add this note later at point."
12360 (remove-hook 'post-command-hook 'org-add-log-note)
12361 (setq org-log-note-window-configuration (current-window-configuration))
12362 (delete-other-windows)
12363 (move-marker org-log-note-return-to (point))
12364 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12365 (goto-char org-log-note-marker)
12366 (org-switch-to-buffer-other-window "*Org Note*")
12368 (if (memq org-log-note-how '(time state))
12369 (let (current-prefix-arg) (org-store-log-note))
12370 (let ((org-inhibit-startup t)) (org-mode))
12371 (insert (format "# Insert note for %s.
12372 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12374 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12375 ((eq org-log-note-purpose 'done) "closed todo item")
12376 ((eq org-log-note-purpose 'state)
12377 (format "state change from \"%s\" to \"%s\""
12378 (or org-log-note-previous-state "")
12379 (or org-log-note-state "")))
12380 ((eq org-log-note-purpose 'reschedule)
12382 ((eq org-log-note-purpose 'delschedule)
12383 "no longer scheduled")
12384 ((eq org-log-note-purpose 'redeadline)
12385 "changing deadline")
12386 ((eq org-log-note-purpose 'deldeadline)
12387 "removing deadline")
12388 ((eq org-log-note-purpose 'refile)
12390 ((eq org-log-note-purpose 'note)
12392 (t (error "This should not happen")))))
12393 (if org-log-note-extra (insert org-log-note-extra))
12394 (org-set-local 'org-finish-function 'org-store-log-note)
12395 (run-hooks 'org-log-buffer-setup-hook)))
12397 (defvar org-note-abort nil) ; dynamically scoped
12398 (defun org-store-log-note ()
12399 "Finish taking a log note, and insert it to where it belongs."
12400 (let ((txt (buffer-string))
12401 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12403 (kill-buffer (current-buffer))
12404 (while (string-match "\\`#.*\n[ \t\n]*" txt)
12405 (setq txt (replace-match "" t t txt)))
12406 (if (string-match "\\s-+\\'" txt)
12407 (setq txt (replace-match "" t t txt)))
12408 (setq lines (org-split-string txt "\n"))
12409 (when (and note (string-match "\\S-" note))
12411 (org-replace-escapes
12413 (list (cons "%u" (user-login-name))
12414 (cons "%U" user-full-name)
12415 (cons "%t" (format-time-string
12416 (org-time-stamp-format 'long 'inactive)
12417 org-log-note-effective-time))
12418 (cons "%T" (format-time-string
12419 (org-time-stamp-format 'long nil)
12420 org-log-note-effective-time))
12421 (cons "%d" (format-time-string
12422 (org-time-stamp-format nil 'inactive)
12423 org-log-note-effective-time))
12424 (cons "%D" (format-time-string
12425 (org-time-stamp-format nil nil)
12426 org-log-note-effective-time))
12427 (cons "%s" (if org-log-note-state
12428 (concat "\"" org-log-note-state "\"")
12430 (cons "%S" (if org-log-note-previous-state
12431 (concat "\"" org-log-note-previous-state "\"")
12433 (if lines (setq note (concat note " \\\\")))
12435 (when (or current-prefix-arg org-note-abort)
12436 (when org-log-into-drawer
12437 (org-remove-empty-drawer-at
12438 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12439 org-log-note-marker))
12442 (with-current-buffer (marker-buffer org-log-note-marker)
12444 (goto-char org-log-note-marker)
12445 (move-marker org-log-note-marker nil)
12447 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12448 (setq ind (save-excursion
12449 (if (ignore-errors (goto-char (org-in-item-p)))
12450 (let ((struct (org-list-struct)))
12452 (org-list-get-top-point struct) struct))
12453 (skip-chars-backward " \r\t\n")
12455 ((and (org-at-heading-p)
12456 org-adapt-indentation)
12457 (1+ (org-current-level)))
12458 ((org-at-heading-p) 0)
12459 (t (org-get-indentation))))))
12460 (setq bul (org-list-bullet-string "-"))
12461 (org-indent-line-to ind)
12462 (insert bul (pop lines))
12463 (let ((ind-body (+ (length bul) ind)))
12466 (org-indent-line-to ind-body)
12467 (insert (pop lines))))
12468 (message "Note stored")
12469 (org-back-to-heading t)
12470 (org-cycle-hide-drawers 'children)))))
12471 (set-window-configuration org-log-note-window-configuration)
12472 (with-current-buffer (marker-buffer org-log-note-return-to)
12473 (goto-char org-log-note-return-to))
12474 (move-marker org-log-note-return-to nil)
12475 (and org-log-post-message (message "%s" org-log-post-message)))
12477 (defun org-remove-empty-drawer-at (drawer pos)
12478 "Remove an empty drawer DRAWER at position POS.
12479 POS may also be a marker."
12480 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12486 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12487 (replace-match ""))))))
12489 (defun org-sparse-tree (&optional arg)
12490 "Create a sparse tree, prompt for the details.
12491 This command can create sparse trees. You first need to select the type
12492 of match used to create the tree:
12494 t Show all TODO entries.
12495 T Show entries with a specific TODO keyword.
12496 m Show entries selected by a tags/property match.
12497 p Enter a property name and its value (both with completion on existing
12498 names/values) and show entries with that property.
12499 r Show entries matching a regular expression (`/' can be used as well)
12500 d Show deadlines due within `org-deadline-warning-days'.
12501 b Show deadlines and scheduled items before a date.
12502 a Show deadlines and scheduled items after a date."
12504 (let (ans kwd value)
12505 (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range")
12506 (setq ans (read-char-exclusive))
12509 (call-interactively 'org-check-deadlines))
12511 (call-interactively 'org-check-before-date))
12513 (call-interactively 'org-check-after-date))
12515 (call-interactively 'org-check-dates-range))
12517 (org-show-todo-tree nil))
12519 (org-show-todo-tree '(4)))
12520 ((member ans '(?T ?m))
12521 (call-interactively 'org-match-sparse-tree))
12522 ((member ans '(?p ?P))
12523 (setq kwd (org-icompleting-read "Property: "
12524 (mapcar 'list (org-buffer-property-keys))))
12525 (setq value (org-icompleting-read "Value: "
12526 (mapcar 'list (org-property-values kwd))))
12527 (unless (string-match "\\`{.*}\\'" value)
12528 (setq value (concat "\"" value "\"")))
12529 (org-match-sparse-tree arg (concat kwd "=" value)))
12530 ((member ans '(?r ?R ?/))
12531 (call-interactively 'org-occur))
12532 (t (error "No such sparse tree command \"%c\"" ans)))))
12534 (defvar org-occur-highlights nil
12535 "List of overlays used for occur matches.")
12536 (make-variable-buffer-local 'org-occur-highlights)
12537 (defvar org-occur-parameters nil
12538 "Parameters of the active org-occur calls.
12539 This is a list, each call to org-occur pushes as cons cell,
12540 containing the regular expression and the callback, onto the list.
12541 The list can contain several entries if `org-occur' has been called
12542 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12543 will only contain one set of parameters. When the highlights are
12544 removed (for example with `C-c C-c', or with the next edit (depending
12545 on `org-remove-highlights-with-change'), this variable is emptied
12547 (make-variable-buffer-local 'org-occur-parameters)
12549 (defun org-occur (regexp &optional keep-previous callback)
12550 "Make a compact tree which shows all matches of REGEXP.
12551 The tree will show the lines where the regexp matches, and all higher
12552 headlines above the match. It will also show the heading after the match,
12553 to make sure editing the matching entry is easy.
12554 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12555 call to `org-occur' will be kept, to allow stacking of calls to this
12557 If CALLBACK is non-nil, it is a function which is called to confirm
12558 that the match should indeed be shown."
12559 (interactive "sRegexp: \nP")
12560 (when (equal regexp "")
12561 (error "Regexp cannot be empty"))
12562 (unless keep-previous
12563 (org-remove-occur-highlights nil nil t))
12564 (push (cons regexp callback) org-occur-parameters)
12567 (goto-char (point-min))
12568 (if (or (not keep-previous) ; do not want to keep
12569 (not org-occur-highlights)) ; no previous matches
12572 (while (re-search-forward regexp nil t)
12573 (when (or (not callback)
12574 (save-match-data (funcall callback)))
12575 (setq cnt (1+ cnt))
12576 (when org-highlight-sparse-tree-matches
12577 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12578 (org-show-context 'occur-tree))))
12579 (when org-remove-highlights-with-change
12580 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12582 (unless org-sparse-tree-open-archived-trees
12583 (org-hide-archived-subtrees (point-min) (point-max)))
12584 (run-hooks 'org-occur-hook)
12585 (if (org-called-interactively-p 'interactive)
12586 (message "%d match(es) for regexp %s" cnt regexp))
12589 (defun org-occur-next-match (&optional n reset)
12590 "Function for `next-error-function' to find sparse tree matches.
12591 N is the number of matches to move, when negative move backwards.
12592 RESET is entirely ignored - this function always goes back to the
12593 starting point when no match is found."
12594 (let* ((limit (if (< n 0) (point-min) (point-max)))
12595 (search-func (if (< n 0)
12596 'previous-single-char-property-change
12597 'next-single-char-property-change))
12602 (while (setq p1 (funcall search-func (point) 'org-type))
12603 (when (equal p1 limit)
12605 (error "No more matches"))
12606 (when (equal (get-char-property p1 'org-type) 'org-occur)
12610 (throw 'exit (point))))
12613 (error "No more matches"))))
12615 (defun org-show-context (&optional key)
12616 "Make sure point and context are visible.
12617 How much context is shown depends upon the variables
12618 `org-show-hierarchy-above', `org-show-following-heading',
12619 `org-show-entry-below' and `org-show-siblings'."
12620 (let ((heading-p (org-at-heading-p t))
12621 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12622 (following-p (org-get-alist-option org-show-following-heading key))
12623 (entry-p (org-get-alist-option org-show-entry-below key))
12624 (siblings-p (org-get-alist-option org-show-siblings key)))
12626 ;; Show heading or entry text
12627 (if (and heading-p (not entry-p))
12628 (org-flag-heading nil) ; only show the heading
12629 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12630 (org-show-hidden-entry))) ; show entire entry
12632 ;; Show next sibling, or heading below text
12634 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12635 (org-flag-heading nil))))
12636 (when siblings-p (org-show-siblings))
12638 ;; show all higher headings, possibly with siblings
12640 (while (and (condition-case nil
12641 (progn (org-up-heading-all 1) t)
12644 (org-flag-heading nil)
12645 (when siblings-p (org-show-siblings))))))))
12647 (defvar org-reveal-start-hook nil
12648 "Hook run before revealing a location.")
12650 (defun org-reveal (&optional siblings)
12651 "Show current entry, hierarchy above it, and the following headline.
12652 This can be used to show a consistent set of context around locations
12653 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12654 not t for the search context.
12656 With optional argument SIBLINGS, on each level of the hierarchy all
12657 siblings are shown. This repairs the tree structure to what it would
12658 look like when opened with hierarchical calls to `org-cycle'.
12659 With double optional argument \\[universal-argument] \\[universal-argument], \
12660 go to the parent and show the
12663 (run-hooks 'org-reveal-start-hook)
12664 (let ((org-show-hierarchy-above t)
12665 (org-show-following-heading t)
12666 (org-show-siblings (if siblings t org-show-siblings)))
12667 (org-show-context nil))
12668 (when (equal siblings '(16))
12670 (when (org-up-heading-safe)
12672 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12674 (defun org-highlight-new-match (beg end)
12675 "Highlight from BEG to END and mark the highlight is an occur headline."
12676 (let ((ov (make-overlay beg end)))
12677 (overlay-put ov 'face 'secondary-selection)
12678 (overlay-put ov 'org-type 'org-occur)
12679 (push ov org-occur-highlights)))
12681 (defun org-remove-occur-highlights (&optional beg end noremove)
12682 "Remove the occur highlights from the buffer.
12683 BEG and END are ignored. If NOREMOVE is nil, remove this function
12684 from the `before-change-functions' in the current buffer."
12686 (unless org-inhibit-highlight-removal
12687 (mapc 'delete-overlay org-occur-highlights)
12688 (setq org-occur-highlights nil)
12689 (setq org-occur-parameters nil)
12691 (remove-hook 'before-change-functions
12692 'org-remove-occur-highlights 'local))))
12696 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12697 "Regular expression matching the priority indicator.")
12699 (defvar org-remove-priority-next-time nil)
12701 (defun org-priority-up ()
12702 "Increase the priority of the current item."
12704 (org-priority 'up))
12706 (defun org-priority-down ()
12707 "Decrease the priority of the current item."
12709 (org-priority 'down))
12711 (defun org-priority (&optional action)
12712 "Change the priority of an item by ARG.
12713 ACTION can be `set', `up', `down', or a character."
12715 (unless org-enable-priority-commands
12716 (error "Priority commands are disabled"))
12717 (setq action (or action 'set))
12718 (let (current new news have remove)
12720 (org-back-to-heading t)
12721 (if (looking-at org-priority-regexp)
12722 (setq current (string-to-char (match-string 2))
12725 ((eq action 'remove)
12726 (setq remove t new ?\ ))
12727 ((or (eq action 'set)
12728 (if (featurep 'xemacs) (characterp action) (integerp action)))
12729 (if (not (eq action 'set))
12731 (message "Priority %c-%c, SPC to remove: "
12732 org-highest-priority org-lowest-priority)
12734 (setq new (read-char-exclusive))))
12735 (if (and (= (upcase org-highest-priority) org-highest-priority)
12736 (= (upcase org-lowest-priority) org-lowest-priority))
12737 (setq new (upcase new)))
12738 (cond ((equal new ?\ ) (setq remove t))
12739 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12740 (error "Priority must be between `%c' and `%c'"
12741 org-highest-priority org-lowest-priority))))
12744 (1- current) ; normal cycling
12745 ;; last priority was empty
12746 (if (eq last-command this-command)
12747 org-lowest-priority ; wrap around empty to lowest
12749 (if org-priority-start-cycle-with-default
12750 org-default-priority
12751 (1- org-default-priority))))))
12754 (1+ current) ; normal cycling
12755 ;; last priority was empty
12756 (if (eq last-command this-command)
12757 org-highest-priority ; wrap around empty to highest
12759 (if org-priority-start-cycle-with-default
12760 org-default-priority
12761 (1+ org-default-priority))))))
12762 (t (error "Invalid action")))
12763 (if (or (< (upcase new) org-highest-priority)
12764 (> (upcase new) org-lowest-priority))
12765 (if (and (memq action '(up down))
12766 (not have) (not (eq last-command this-command)))
12767 ;; `new' is from default priority
12769 "The default can not be set, see `org-default-priority' why")
12770 ;; normal cycling: `new' is beyond highest/lowest priority
12771 ;; and is wrapped around to the empty priority
12773 (setq news (format "%c" new))
12776 (replace-match "" t t nil 1)
12777 (replace-match news t t nil 2))
12779 (error "No priority cookie found in line")
12780 (let ((case-fold-search nil))
12781 (looking-at org-todo-line-regexp))
12784 (goto-char (match-end 2))
12785 (insert " [#" news "]"))
12786 (goto-char (match-beginning 3))
12787 (insert "[#" news "] "))))
12788 (org-preserve-lc (org-set-tags nil 'align)))
12790 (message "Priority removed")
12791 (message "Priority of current item set to %s" news))))
12793 (defun org-get-priority (s)
12794 "Find priority cookie and return priority."
12795 (if (functionp org-get-priority-function)
12796 (funcall org-get-priority-function)
12798 (if (not (string-match org-priority-regexp s))
12799 (* 1000 (- org-lowest-priority org-default-priority))
12800 (* 1000 (- org-lowest-priority
12801 (string-to-char (match-string 2 s))))))))
12805 (defvar org-agenda-archives-mode)
12806 (defvar org-map-continue-from nil
12807 "Position from where mapping should continue.
12808 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
12810 (defvar org-scanner-tags nil
12811 "The current tag list while the tags scanner is running.")
12812 (defvar org-trust-scanner-tags nil
12813 "Should `org-get-tags-at' use the tags for the scanner.
12814 This is for internal dynamical scoping only.
12815 When this is non-nil, the function `org-get-tags-at' will return the value
12816 of `org-scanner-tags' instead of building the list by itself. This
12817 can lead to large speed-ups when the tags scanner is used in a file with
12818 many entries, and when the list of tags is retrieved, for example to
12819 obtain a list of properties. Building the tags list for each entry in such
12820 a file becomes an N^2 operation - but with this variable set, it scales
12823 (defun org-scan-tags (action matcher &optional todo-only start-level)
12824 "Scan headline tags with inheritance and produce output ACTION.
12826 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
12827 or `agenda' to produce an entry list for an agenda view. It can also be
12828 a Lisp form or a function that should be called at each matched headline, in
12829 this case the return value is a list of all return values from these calls.
12831 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
12832 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
12833 only lines with a TODO keyword are included in the output.
12835 START-LEVEL can be a string with asterisks, reducing the scope to
12836 headlines matching this string."
12837 (require 'org-agenda)
12838 (let* ((re (concat "^"
12840 ;; Get the correct level to match
12841 (concat "\\*\\{" (number-to-string start-level) "\\} ")
12842 org-outline-regexp)
12844 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
12846 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
12847 (props (list 'face 'default
12848 'done-face 'org-agenda-done
12849 'undone-face 'default
12850 'mouse-face 'highlight
12851 'org-not-done-regexp org-not-done-regexp
12852 'org-todo-regexp org-todo-regexp
12853 'org-complex-heading-regexp org-complex-heading-regexp
12855 (format "mouse-2 or RET jump to org file %s"
12856 (abbreviate-file-name
12857 (or (buffer-file-name (buffer-base-buffer))
12858 (buffer-name (buffer-base-buffer)))))))
12859 (case-fold-search nil)
12860 (org-map-continue-from nil)
12861 lspos tags tags-list
12862 (tags-alist (list (cons 0 org-file-tags)))
12863 (llast 0) rtn rtn1 level category i txt
12864 todo marker entry priority)
12865 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
12866 (setq action (list 'lambda nil action)))
12868 (goto-char (point-min))
12869 (when (eq action 'sparse-tree)
12871 (org-remove-occur-highlights))
12872 (while (re-search-forward re nil t)
12873 (setq org-map-continue-from nil)
12875 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
12876 tags (if (match-end 4) (org-match-string-no-properties 4)))
12877 (goto-char (setq lspos (match-beginning 0)))
12878 (setq level (org-reduced-level (funcall outline-level))
12879 category (org-get-category))
12880 (setq i llast llast level)
12881 ;; remove tag lists from same and sublevels
12882 (while (>= i level)
12883 (when (setq entry (assoc i tags-alist))
12884 (setq tags-alist (delete entry tags-alist)))
12886 ;; add the next tags
12888 (setq tags (org-split-string tags ":")
12890 (cons (cons level tags) tags-alist)))
12891 ;; compile tags for current headline
12893 (if org-use-tag-inheritance
12894 (apply 'append (mapcar 'cdr (reverse tags-alist)))
12896 org-scanner-tags tags-list)
12897 (when org-use-tag-inheritance
12898 (setcdr (car tags-alist)
12899 (mapcar (lambda (x)
12900 (setq x (copy-sequence x))
12901 (org-add-prop-inherited x))
12902 (cdar tags-alist))))
12903 (when (and tags org-use-tag-inheritance
12904 (or (not (eq t org-use-tag-inheritance))
12905 org-tags-exclude-from-inheritance))
12906 ;; selective inheritance, remove uninherited ones
12907 (setcdr (car tags-alist)
12908 (org-remove-uninherited-tags (cdar tags-alist))))
12911 ;; eval matcher only when the todo condition is OK
12912 (and (or (not todo-only) (member todo org-not-done-keywords))
12913 (let ((case-fold-search t) (org-trust-scanner-tags t))
12916 ;; Call the skipper, but return t if it does not skip,
12917 ;; so that the `and' form continues evaluating
12919 (unless (eq action 'sparse-tree) (org-agenda-skip))
12922 ;; Check if timestamps are deselecting this entry
12923 (or (not todo-only)
12924 (and (member todo org-not-done-keywords)
12925 (or (not org-agenda-tags-todo-honor-ignore-options)
12926 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
12928 ;; Extra check for the archive tag
12929 ;; FIXME: Does the skipper already do this????
12931 (not (member org-archive-tag tags-list))
12932 ;; we have an archive tag, should we use this anyway?
12933 (or (not org-agenda-skip-archived-trees)
12934 (and (eq action 'agenda) org-agenda-archives-mode))))
12936 ;; select this headline
12939 ((eq action 'sparse-tree)
12940 (and org-highlight-sparse-tree-matches
12941 (org-get-heading) (match-end 0)
12942 (org-highlight-new-match
12943 (match-beginning 1) (match-end 1)))
12944 (org-show-context 'tags-tree))
12945 ((eq action 'agenda)
12946 (setq txt (org-agenda-format-item
12949 (if (eq org-tags-match-list-sublevels 'indented)
12950 (make-string (1- level) ?.) "")
12954 priority (org-get-priority txt))
12956 (setq marker (org-agenda-new-marker))
12957 (org-add-props txt props
12958 'org-marker marker 'org-hd-marker marker 'org-category category
12960 'priority priority 'type "tagsmatch")
12962 ((functionp action)
12963 (setq org-map-continue-from nil)
12965 (setq rtn1 (funcall action))
12967 (t (error "Invalid action")))
12969 ;; if we are to skip sublevels, jump to end of subtree
12970 (unless org-tags-match-list-sublevels
12971 (org-end-of-subtree t)
12972 (backward-char 1))))
12973 ;; Get the correct position from where to continue
12974 (if org-map-continue-from
12975 (goto-char org-map-continue-from)
12976 (and (= (point) lspos) (end-of-line 1)))))
12977 (when (and (eq action 'sparse-tree)
12978 (not org-sparse-tree-open-archived-trees))
12979 (org-hide-archived-subtrees (point-min) (point-max)))
12982 (defun org-remove-uninherited-tags (tags)
12983 "Remove all tags that are not inherited from the list TAGS."
12985 ((eq org-use-tag-inheritance t)
12986 (if org-tags-exclude-from-inheritance
12987 (org-delete-all org-tags-exclude-from-inheritance tags)
12989 ((not org-use-tag-inheritance) nil)
12990 ((stringp org-use-tag-inheritance)
12993 (if (and (string-match org-use-tag-inheritance x)
12994 (not (member x org-tags-exclude-from-inheritance)))
12997 ((listp org-use-tag-inheritance)
13000 (if (member x org-use-tag-inheritance) x nil))
13003 (defvar todo-only) ;; dynamically scoped
13005 (defun org-match-sparse-tree (&optional todo-only match)
13006 "Create a sparse tree according to tags string MATCH.
13007 MATCH can contain positive and negative selection of tags, like
13008 \"+WORK+URGENT-WITHBOSS\".
13009 If optional argument TODO-ONLY is non-nil, only select lines that are
13012 (org-prepare-agenda-buffers (list (current-buffer)))
13013 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13015 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13017 (defvar org-cached-props nil)
13018 (defun org-cached-entry-get (pom property)
13019 (if (or (eq t org-use-property-inheritance)
13020 (and (stringp org-use-property-inheritance)
13021 (string-match org-use-property-inheritance property))
13022 (and (listp org-use-property-inheritance)
13023 (member property org-use-property-inheritance)))
13024 ;; Caching is not possible, check it directly
13025 (org-entry-get pom property 'inherit)
13026 ;; Get all properties, so that we can do complicated checks easily
13027 (cdr (assoc property (or org-cached-props
13028 (setq org-cached-props
13029 (org-entry-properties pom)))))))
13031 (defun org-global-tags-completion-table (&optional files)
13032 "Return the list of all tags in all agenda buffer/files.
13033 Optional FILES argument is a list of files to which can be used
13034 instead of the agenda files."
13041 (set-buffer (find-file-noselect file))
13042 (append (org-get-buffer-tags)
13043 (mapcar (lambda (x) (if (stringp (car-safe x))
13044 (list (car-safe x)) nil))
13046 (if (and files (car files))
13048 (org-agenda-files))))))))
13050 (defun org-make-tags-matcher (match)
13051 "Create the TAGS/TODO matcher form for the selection string MATCH."
13052 ;; todo-only is scoped dynamically into this function, and the function
13053 ;; may change it if the matcher asks for it.
13055 ;; Get a new match request, with completion
13056 (let ((org-last-tags-completion-table
13057 (org-global-tags-completion-table)))
13058 (setq match (org-completing-read-no-i
13059 "Match: " 'org-tags-completion-function nil nil nil
13060 'org-tags-history))))
13062 ;; Parse the string and create a lisp form
13063 (let ((match0 match)
13064 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13066 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13067 orterms term orlist re-p str-p level-p level-op time-p
13068 prop-p pn pv po gv rest)
13069 (if (string-match "/+" match)
13070 ;; match contains also a todo-matching request
13072 (setq tagsmatch (substring match 0 (match-beginning 0))
13073 todomatch (substring match (match-end 0)))
13074 (if (string-match "^!" todomatch)
13075 (setq todo-only t todomatch (substring todomatch 1)))
13076 (if (string-match "^\\s-*$" todomatch)
13077 (setq todomatch nil)))
13078 ;; only matching tags
13079 (setq tagsmatch match todomatch nil))
13081 ;; Make the tags matcher
13082 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13083 (setq tagsmatcher t)
13084 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13085 (while (setq term (pop orterms))
13086 (while (and (equal (substring term -1) "\\") orterms)
13087 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13088 (while (string-match re term)
13089 (setq rest (substring term (match-end 0))
13090 minus (and (match-end 1)
13091 (equal (match-string 1 term) "-"))
13092 tag (save-match-data (replace-regexp-in-string
13094 (match-string 2 term)))
13095 re-p (equal (string-to-char tag) ?{)
13096 level-p (match-end 4)
13097 prop-p (match-end 5)
13099 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13101 (setq level-op (org-op-to-function (match-string 3 term)))
13102 `(,level-op level ,(string-to-number
13103 (match-string 4 term))))
13105 (setq pn (match-string 5 term)
13106 po (match-string 6 term)
13107 pv (match-string 7 term)
13108 re-p (equal (string-to-char pv) ?{)
13109 str-p (equal (string-to-char pv) ?\")
13110 time-p (save-match-data
13111 (string-match "^\"[[<].*[]>]\"$" pv))
13112 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13113 (if time-p (setq pv (org-matcher-time pv)))
13114 (setq po (org-op-to-function po (if time-p 'time str-p)))
13116 ((equal pn "CATEGORY")
13117 (setq gv '(get-text-property (point) 'org-category)))
13121 (setq gv `(org-cached-entry-get nil ,pn))))
13124 `(not (string-match ,pv (or ,gv "")))
13125 `(string-match ,pv (or ,gv "")))
13127 `(,po (or ,gv "") ,pv)
13128 `(,po (string-to-number (or ,gv ""))
13129 ,(string-to-number pv) ))))
13130 (t `(member ,tag tags-list)))
13131 mm (if minus (list 'not mm) mm)
13133 (push mm tagsmatcher))
13134 (push (if (> (length tagsmatcher) 1)
13135 (cons 'and tagsmatcher)
13138 (setq tagsmatcher nil))
13139 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13141 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13142 ;; Make the todo matcher
13143 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13144 (setq todomatcher t)
13145 (setq orterms (org-split-string todomatch "|") orlist nil)
13146 (while (setq term (pop orterms))
13147 (while (string-match re term)
13148 (setq minus (and (match-end 1)
13149 (equal (match-string 1 term) "-"))
13150 kwd (match-string 2 term)
13151 re-p (equal (string-to-char kwd) ?{)
13152 term (substring term (match-end 0))
13154 `(string-match ,(substring kwd 1 -1) todo)
13155 (list 'equal 'todo kwd))
13156 mm (if minus (list 'not mm) mm))
13157 (push mm todomatcher))
13158 (push (if (> (length todomatcher) 1)
13159 (cons 'and todomatcher)
13162 (setq todomatcher nil))
13163 (setq todomatcher (if (> (length orlist) 1)
13164 (cons 'or orlist) (car orlist))))
13166 ;; Return the string and lisp forms of the matcher
13167 (setq matcher (if todomatcher
13168 (list 'and tagsmatcher todomatcher)
13170 (cons match0 matcher)))
13172 (defun org-op-to-function (op &optional stringp)
13173 "Turn an operator into the appropriate function."
13176 ((equal op "<" ) '(< string< org-time<))
13177 ((equal op ">" ) '(> org-string> org-time>))
13178 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13179 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13180 ((member op '("=" "==")) '(= string= org-time=))
13181 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13182 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13184 (defun org<> (a b) (not (= a b)))
13185 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13186 (defun org-string>= (a b) (not (string< a b)))
13187 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13188 (defun org-string<> (a b) (not (string= a b)))
13189 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13190 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13191 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13192 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13193 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13194 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13196 "Convert S to a floating point time.
13197 If S is already a number, just return it. If it is a string, parse
13198 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13202 (condition-case nil
13203 (float-time (apply 'encode-time (org-parse-time-string s)))
13207 (defun org-time-today ()
13208 "Time in seconds today at 0:00.
13209 Returns the float number of seconds since the beginning of the
13210 epoch to the beginning of today (00:00)."
13211 (float-time (apply 'encode-time
13212 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13214 (defun org-matcher-time (s)
13215 "Interpret a time comparison value."
13218 ((string= s "<now>") (float-time))
13219 ((string= s "<today>") (org-time-today))
13220 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13221 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13222 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
13223 (+ (org-time-today)
13224 (* (string-to-number (match-string 1 s))
13225 (cdr (assoc (match-string 2 s)
13226 '(("d" . 86400.0) ("w" . 604800.0)
13227 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13230 (defun org-match-any-p (re list)
13231 "Does re match any element of list?"
13232 (setq list (mapcar (lambda (x) (string-match re x)) list))
13235 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13236 (defvar org-tags-overlay (make-overlay 1 1))
13237 (org-detach-overlay org-tags-overlay)
13239 (defun org-get-local-tags-at (&optional pos)
13240 "Get a list of tags defined in the current headline."
13241 (org-get-tags-at pos 'local))
13243 (defun org-get-local-tags ()
13244 "Get a list of tags defined in the current headline."
13245 (org-get-tags-at nil 'local))
13247 (defun org-get-tags-at (&optional pos local)
13248 "Get a list of all headline tags applicable at POS.
13249 POS defaults to point. If tags are inherited, the list contains
13250 the targets in the same sequence as the headlines appear, i.e.
13251 the tags of the current headline come last.
13252 When LOCAL is non-nil, only return tags from the current headline,
13253 ignore inherited ones."
13255 (if (and org-trust-scanner-tags
13256 (or (not pos) (equal pos (point)))
13259 (let (tags ltags lastpos parent)
13263 (goto-char (or pos (point)))
13266 (condition-case nil
13268 (org-back-to-heading t)
13269 (while (not (equal lastpos (point)))
13270 (setq lastpos (point))
13272 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13273 (setq ltags (org-split-string
13274 (org-match-string-no-properties 1) ":"))
13276 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13279 (org-remove-uninherited-tags ltags)
13282 (or org-use-tag-inheritance (throw 'done t))
13283 (if local (throw 'done t))
13284 (or (org-up-heading-safe) (error nil))
13289 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13291 (defun org-add-prop-inherited (s)
13292 (add-text-properties 0 (length s) '(inherited t) s)
13295 (defun org-toggle-tag (tag &optional onoff)
13296 "Toggle the tag TAG for the current line.
13297 If ONOFF is `on' or `off', don't toggle but set to this state."
13300 (org-back-to-heading t)
13301 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13304 (setq current (match-string 1))
13305 (replace-match ""))
13307 (setq current (nreverse (org-split-string current ":")))
13311 (or (member tag current) (push tag current)))
13313 (or (not (member tag current)) (setq current (delete tag current))))
13314 (t (if (member tag current)
13315 (setq current (delete tag current))
13317 (push tag current))))
13321 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13322 (org-set-tags nil t))
13323 (delete-horizontal-space))
13324 (run-hooks 'org-after-tags-change-hook))
13327 (defun org-align-tags-here (to-col)
13328 ;; Assumes that this is a headline
13329 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13330 (beginning-of-line 1)
13331 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13332 (< pos (match-beginning 2)))
13334 (setq tags-l (- (match-end 2) (match-beginning 2)))
13335 (goto-char (match-beginning 1))
13337 (delete-region (point) (1+ (match-beginning 2)))
13338 (setq ncol (max (current-column)
13342 (- (abs to-col) tags-l))))
13344 (insert (make-string (- ncol (current-column)) ?\ ))
13345 (setq ncol (current-column))
13346 (when indent-tabs-mode (tabify p (point-at-eol)))
13347 (org-move-to-column (min ncol col) t))
13350 (defun org-set-tags-command (&optional arg just-align)
13351 "Call the set-tags command for the current entry."
13353 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13354 (org-set-tags arg just-align)
13356 (org-back-to-heading t)
13357 (org-set-tags arg just-align))))
13359 (defun org-set-tags-to (data)
13360 "Set the tags of the current entry to DATA, replacing the current tags.
13361 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13362 If DATA is nil or the empty string, any tags will be removed."
13363 (interactive "sTags: ")
13367 ((equal data "") "")
13369 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13372 (concat ":" (mapconcat 'identity data ":") ":"))
13376 (org-back-to-heading t)
13377 (when (looking-at org-complex-heading-regexp)
13380 (goto-char (match-beginning 5))
13382 (delete-region (point) (point-at-eol))
13383 (org-set-tags nil 'align))
13384 (goto-char (point-at-eol))
13386 (org-set-tags nil 'align)))
13387 (beginning-of-line 1)
13388 (if (looking-at ".*?\\([ \t]+\\)$")
13389 (delete-region (match-beginning 1) (match-end 1))))))
13391 (defun org-align-all-tags ()
13392 "Align the tags i all headings."
13395 (or (ignore-errors (org-back-to-heading t))
13396 (outline-next-heading))
13397 (if (org-at-heading-p)
13399 (message "No headings"))))
13401 (defvar org-indent-indentation-per-level)
13402 (defun org-set-tags (&optional arg just-align)
13403 "Set the tags for the current headline.
13404 With prefix ARG, realign all tags in headings in the current buffer."
13406 (let* ((re org-outline-regexp-bol)
13407 (current (unless arg (org-get-tags-string)))
13408 (col (current-column))
13409 (org-setting-tags t)
13410 table current-tags inherited-tags ; computed below when needed
13411 tags p0 c0 c1 rpl di tc level)
13414 (goto-char (point-min))
13415 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13416 (while (re-search-forward re nil t)
13417 (org-set-tags nil t)
13419 (message "All tags realigned to column %d" org-tags-column))
13421 (setq tags current)
13422 ;; Get a new set of tags from the user
13424 (setq table (append org-tag-persistent-alist
13425 (or org-tag-alist (org-get-buffer-tags))
13427 org-complete-tags-always-offer-all-agenda-tags
13428 (org-global-tags-completion-table
13429 (org-agenda-files))))
13430 org-last-tags-completion-table table
13431 current-tags (org-split-string current ":")
13432 inherited-tags (nreverse
13433 (nthcdr (length current-tags)
13434 (nreverse (org-get-tags-at))))
13436 (if (or (eq t org-use-fast-tag-selection)
13437 (and org-use-fast-tag-selection
13438 (delq nil (mapcar 'cdr table))))
13439 (org-fast-tag-selection
13440 current-tags inherited-tags table
13441 (if org-fast-tag-selection-include-todo
13442 org-todo-key-alist))
13443 (let ((org-add-colon-after-tag-completion t))
13445 (org-icompleting-read "Tags: "
13446 'org-tags-completion-function
13447 nil nil current 'org-tags-history))))))
13448 (while (string-match "[-+&]+" tags)
13449 ;; No boolean logic, just a list
13450 (setq tags (replace-match ":" t t tags))))
13452 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13454 (if org-tags-sort-function
13455 (setq tags (mapconcat 'identity
13456 (sort (org-split-string
13457 tags (org-re "[^[:alnum:]_@#%]+"))
13458 org-tags-sort-function) ":")))
13460 (if (string-match "\\`[\t ]*\\'" tags)
13462 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13463 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13465 ;; Insert new tags at the correct column
13466 (beginning-of-line 1)
13467 (setq level (or (and (looking-at org-outline-regexp)
13468 (- (match-end 0) (point) 1))
13471 ((and (equal current "") (equal tags "")))
13472 ((re-search-forward
13473 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13475 (if (equal tags "")
13477 (goto-char (match-beginning 0))
13478 (setq c0 (current-column)
13479 ;; compute offset for the case of org-indent-mode active
13480 di (if org-indent-mode
13481 (* (1- org-indent-indentation-per-level) (1- level))
13483 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13484 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13485 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13486 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13487 (replace-match rpl t t)
13488 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13490 (t (error "Tags alignment failed")))
13491 (org-move-to-column col)
13493 (run-hooks 'org-after-tags-change-hook)))))
13495 (defun org-change-tag-in-region (beg end tag off)
13496 "Add or remove TAG for each entry in the region.
13497 This works in the agenda, and also in an org-mode buffer."
13499 (list (region-beginning) (region-end)
13500 (let ((org-last-tags-completion-table
13501 (if (eq major-mode 'org-mode)
13502 (org-get-buffer-tags)
13503 (org-global-tags-completion-table))))
13504 (org-icompleting-read
13505 "Tag: " 'org-tags-completion-function nil nil nil
13506 'org-tags-history))
13508 (message "[s]et or
[r]emove? ")
13509 (equal (read-char-exclusive) ?r))))
13510 (if (fboundp 'deactivate-mark) (deactivate-mark))
13511 (let ((agendap (equal major-mode 'org-agenda-mode))
13512 l1 l2 m buf pos newhead (cnt 0))
13514 (setq l2 (1- (org-current-line)))
13516 (setq l1 (org-current-line))
13517 (loop for l from l1 to l2 do
13519 (setq m (get-text-property (point) 'org-hd-marker))
13520 (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
13522 (setq buf (if agendap (marker-buffer m) (current-buffer))
13523 pos (if agendap m (point)))
13524 (with-current-buffer buf
13528 (setq cnt (1+ cnt))
13529 (org-toggle-tag tag (if off 'off 'on))
13530 (setq newhead (org-get-heading)))))
13531 (and agendap (org-agenda-change-all-lines newhead m))))
13532 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13534 (defun org-tags-completion-function (string predicate &optional flag)
13535 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13536 (confirm (lambda (x) (stringp (car x)))))
13537 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13538 (setq s1 (match-string 1 string)
13539 s2 (match-string 2 string))
13540 (setq s1 "" s2 string))
13544 (setq rtn (try-completion s2 ctable confirm))
13547 (concat s1 s2 (substring rtn (length s2))
13548 (if (and org-add-colon-after-tag-completion
13549 (assoc rtn ctable))
13554 (all-completions s2 ctable confirm)
13558 (assoc s2 ctable)))
13561 (defun org-fast-tag-insert (kwd tags face &optional end)
13562 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13563 (insert (format "%-12s" (concat kwd ":"))
13564 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13567 (defun org-fast-tag-show-exit (flag)
13570 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13571 (replace-match ""))
13574 (org-move-to-column (- (window-width) 19) t)
13575 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13577 (defun org-set-current-tags-overlay (current prefix)
13578 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13579 (if (featurep 'xemacs)
13580 (org-overlay-display org-tags-overlay (concat prefix s)
13581 'secondary-selection)
13582 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13583 (org-overlay-display org-tags-overlay (concat prefix s)))))
13585 (defvar org-last-tag-selection-key nil)
13586 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13587 "Fast tag selection with single keys.
13588 CURRENT is the current list of tags in the headline, INHERITED is the
13589 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13590 possibly with grouping information. TODO-TABLE is a similar table with
13591 TODO keywords, should these have keys assigned to them.
13592 If the keys are nil, a-z are automatically assigned.
13593 Returns the new tags string, or nil to not change the current settings."
13594 (let* ((fulltable (append table todo-table))
13595 (maxlen (apply 'max (mapcar
13597 (if (stringp (car x)) (string-width (car x)) 0))
13599 (buf (current-buffer))
13600 (expert (eq org-fast-tag-selection-single-key 'expert))
13602 (fwidth (+ maxlen 3 1 3))
13603 (ncol (/ (- (window-width) 4) fwidth))
13606 tg cnt e c char c1 c2 ntable tbl rtn
13607 ov-start ov-end ov-prefix
13608 (exit-after-next org-fast-tag-selection-single-key)
13609 (done-keywords org-done-keywords)
13612 (beginning-of-line 1)
13614 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13615 (setq ov-start (match-beginning 1)
13616 ov-end (match-end 1)
13618 (setq ov-start (1- (point-at-eol))
13619 ov-end (1+ ov-start))
13620 (skip-chars-forward "^\n\r")
13623 (buffer-substring (1- (point)) (point))
13624 (if (> (current-column) org-tags-column)
13626 (make-string (- org-tags-column (current-column)) ?\ ))))))
13627 (move-overlay org-tags-overlay ov-start ov-end)
13628 (save-window-excursion
13630 (set-buffer (get-buffer-create " *Org tags*"))
13631 (delete-other-windows)
13632 (split-window-vertically)
13633 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13635 (org-set-local 'org-done-keywords done-keywords)
13636 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13637 (org-fast-tag-insert "Current" current c-face "\n\n")
13638 (org-fast-tag-show-exit exit-after-next)
13639 (org-set-current-tags-overlay current ov-prefix)
13640 (setq tbl fulltable char ?a cnt 0)
13641 (while (setq e (pop tbl))
13643 ((equal (car e) :startgroup)
13644 (push '() groups) (setq ingroup t)
13645 (when (not (= cnt 0))
13648 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13649 ((equal (car e) :endgroup)
13650 (setq ingroup nil cnt 0)
13651 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13652 ((equal e '(:newline))
13653 (when (not (= cnt 0))
13657 (while (equal (car tbl) '(:newline))
13659 (setq tbl (cdr tbl)))))
13661 (setq tg (copy-sequence (car e)) c2 nil)
13664 ;; automatically assign a character.
13665 (setq c1 (string-to-char
13666 (downcase (substring
13667 tg (if (= (string-to-char tg) ?@) 1 0)))))
13668 (if (or (rassoc c1 ntable) (rassoc c1 table))
13669 (while (or (rassoc char ntable) (rassoc char table))
13670 (setq char (1+ char)))
13672 (setq c (or c2 char)))
13673 (if ingroup (push tg (car groups)))
13674 (setq tg (org-add-props tg nil 'face
13676 ((not (assoc tg table))
13677 (org-get-todo-face tg))
13678 ((member tg current) c-face)
13679 ((member tg inherited) i-face)
13681 (if (and (= cnt 0) (not ingroup)) (insert " "))
13682 (insert "[" c "] " tg (make-string
13683 (- fwidth 4 (length tg)) ?\ ))
13684 (push (cons tg c) ntable)
13685 (when (= (setq cnt (1+ cnt)) ncol)
13687 (if ingroup (insert " "))
13689 (setq ntable (nreverse ntable))
13691 (goto-char (point-min))
13692 (if (not expert) (org-fit-window-to-buffer))
13696 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13697 (if (not groups) "no " "")
13698 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13699 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13700 (setq org-last-tag-selection-key c)
13702 ((= c ?\r) (throw 'exit t))
13704 (setq groups (not groups))
13705 (goto-char (point-min))
13706 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13709 (org-fast-tag-show-exit
13710 (setq exit-after-next (not exit-after-next)))
13712 (delete-other-windows)
13713 (set-window-buffer (split-window-vertically) " *Org tags*")
13714 (org-switch-to-buffer-other-window " *Org tags*")
13715 (org-fit-window-to-buffer)))
13717 (and (= c ?q) (not (rassoc c ntable))))
13718 (org-detach-overlay org-tags-overlay)
13719 (setq quit-flag t))
13722 (if exit-after-next (setq exit-after-next 'now)))
13724 (condition-case nil
13725 (setq tg (org-icompleting-read
13728 (with-current-buffer buf
13729 (org-get-buffer-tags)))))
13730 (quit (setq tg "")))
13731 (when (string-match "\\S-" tg)
13732 (add-to-list 'buffer-tags (list tg))
13733 (if (member tg current)
13734 (setq current (delete tg current))
13735 (push tg current)))
13736 (if exit-after-next (setq exit-after-next 'now)))
13737 ((setq e (rassoc c todo-table) tg (car e))
13738 (with-current-buffer buf
13739 (save-excursion (org-todo tg)))
13740 (if exit-after-next (setq exit-after-next 'now)))
13741 ((setq e (rassoc c ntable) tg (car e))
13742 (if (member tg current)
13743 (setq current (delete tg current))
13744 (loop for g in groups do
13747 (setq current (delete x current)))
13750 (if exit-after-next (setq exit-after-next 'now))))
13752 ;; Create a sorted list
13756 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13757 (if (eq exit-after-next 'now) (throw 'exit t))
13758 (goto-char (point-min))
13759 (beginning-of-line 2)
13760 (delete-region (point) (point-at-eol))
13761 (org-fast-tag-insert "Current" current c-face)
13762 (org-set-current-tags-overlay current ov-prefix)
13763 (while (re-search-forward
13764 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
13765 (setq tg (match-string 1))
13766 (add-text-properties
13767 (match-beginning 1) (match-end 1)
13770 ((member tg current) c-face)
13771 ((member tg inherited) i-face)
13772 (t (get-text-property (match-beginning 1) 'face))))))
13773 (goto-char (point-min)))))
13774 (org-detach-overlay org-tags-overlay)
13776 (mapconcat 'identity current ":")
13779 (defun org-get-tags-string ()
13780 "Get the TAGS string in the current headline."
13781 (unless (org-at-heading-p t)
13782 (error "Not on a heading"))
13784 (beginning-of-line 1)
13785 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13786 (org-match-string-no-properties 1)
13789 (defun org-get-tags ()
13790 "Get the list of tags specified in the current headline."
13791 (org-split-string (org-get-tags-string) ":"))
13793 (defun org-get-buffer-tags ()
13794 "Get a table of all tags used in the buffer, for completion."
13797 (goto-char (point-min))
13798 (while (re-search-forward
13799 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
13800 (when (equal (char-after (point-at-bol 0)) ?*)
13801 (mapc (lambda (x) (add-to-list 'tags x))
13802 (org-split-string (org-match-string-no-properties 1) ":")))))
13803 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
13804 (mapcar 'list tags)))
13806 ;;;; The mapping API
13809 (defun org-map-entries (func &optional match scope &rest skip)
13810 "Call FUNC at each headline selected by MATCH in SCOPE.
13812 FUNC is a function or a lisp form. The function will be called without
13813 arguments, with the cursor positioned at the beginning of the headline.
13814 The return values of all calls to the function will be collected and
13815 returned as a list.
13817 The call to FUNC will be wrapped into a save-excursion form, so FUNC
13818 does not need to preserve point. After evaluation, the cursor will be
13819 moved to the end of the line (presumably of the headline of the
13820 processed entry) and search continues from there. Under some
13821 circumstances, this may not produce the wanted results. For example,
13822 if you have removed (e.g. archived) the current (sub)tree it could
13823 mean that the next entry will be skipped entirely. In such cases, you
13824 can specify the position from where search should continue by making
13825 FUNC set the variable `org-map-continue-from' to the desired buffer
13828 MATCH is a tags/property/todo match as it is used in the agenda tags view.
13829 Only headlines that are matched by this query will be considered during
13830 the iteration. When MATCH is nil or t, all headlines will be
13831 visited by the iteration.
13833 SCOPE determines the scope of this command. It can be any of:
13835 nil The current buffer, respecting the restriction if any
13836 tree The subtree started with the entry at point
13837 region The entries within the active region, if any
13839 The entries within the active region, but only those at
13840 the same level than the first one.
13841 file The current buffer, without restriction
13843 The current buffer, and any archives associated with it
13844 agenda All agenda files
13845 agenda-with-archives
13846 All agenda files with any archive files associated with them
13848 If this is a list, all files in the list will be scanned
13850 The remaining args are treated as settings for the skipping facilities of
13851 the scanner. The following items can be given here:
13853 archive skip trees with the archive tag.
13854 comment skip trees with the COMMENT keyword
13855 function or Emacs Lisp form:
13856 will be used as value for `org-agenda-skip-function', so whenever
13857 the function returns t, FUNC will not be called for that
13858 entry and search will continue from the point where the
13859 function leaves it.
13861 If your function needs to retrieve the tags including inherited tags
13862 at the *current* entry, you can use the value of the variable
13863 `org-scanner-tags' which will be much faster than getting the value
13864 with `org-get-tags-at'. If your function gets properties with
13865 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
13866 to t around the call to `org-entry-properties' to get the same speedup.
13867 Note that if your function moves around to retrieve tags and properties at
13868 a *different* entry, you cannot use these techniques."
13869 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
13870 (not (org-region-active-p)))
13871 (let* ((org-agenda-archives-mode nil) ; just to make sure
13872 (org-agenda-skip-archived-trees (memq 'archive skip))
13873 (org-agenda-skip-comment-trees (memq 'comment skip))
13874 (org-agenda-skip-function
13875 (car (org-delete-all '(comment archive) skip)))
13876 (org-tags-match-list-sublevels t)
13877 (start-level (eq scope 'region-start-level))
13879 org-todo-keywords-for-agenda
13880 org-done-keywords-for-agenda
13881 org-todo-keyword-alist-for-agenda
13882 org-drawers-for-agenda
13883 org-tag-alist-for-agenda)
13886 ((eq match t) (setq matcher t))
13887 ((eq match nil) (setq matcher t))
13888 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
13892 (cond ((eq scope 'tree)
13893 (org-back-to-heading t)
13894 (org-narrow-to-subtree)
13896 ((and (or (eq scope 'region) (eq scope 'region-start-level))
13897 (org-region-active-p))
13898 ;; If needed, set start-level to a string like "2"
13901 (goto-char (region-beginning))
13902 (unless (org-at-heading-p) (outline-next-heading))
13903 (setq start-level (org-current-level))))
13904 (narrow-to-region (region-beginning)
13906 (goto-char (region-end))
13907 (unless (and (bolp) (org-at-heading-p))
13908 (outline-next-heading))
13914 (org-prepare-agenda-buffers
13915 (list (buffer-file-name (current-buffer))))
13916 (setq res (org-scan-tags func matcher nil start-level)))
13917 ;; Get the right scope
13919 ((and scope (listp scope) (symbolp (car scope)))
13920 (setq scope (eval scope)))
13921 ((eq scope 'agenda)
13922 (setq scope (org-agenda-files t)))
13923 ((eq scope 'agenda-with-archives)
13924 (setq scope (org-agenda-files t))
13925 (setq scope (org-add-archive-files scope)))
13927 (setq scope (list (buffer-file-name))))
13928 ((eq scope 'file-with-archives)
13929 (setq scope (org-add-archive-files (list (buffer-file-name))))))
13930 (org-prepare-agenda-buffers scope)
13931 (while (setq file (pop scope))
13932 (with-current-buffer (org-find-base-buffer-visiting file)
13936 (goto-char (point-min))
13937 (setq res (append res (org-scan-tags func matcher))))))))))
13942 ;;; Setting and retrieving properties
13944 (defconst org-special-properties
13945 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
13946 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
13947 "The special properties valid in Org-mode.
13949 These are properties that are not defined in the property drawer,
13950 but in some other way.")
13952 (defconst org-default-properties
13953 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
13954 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
13955 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
13956 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
13957 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
13958 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
13959 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
13960 "Some properties that are used by Org-mode for various purposes.
13961 Being in this list makes sure that they are offered for completion.")
13963 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
13964 "Regular expression matching the first line of a property drawer.")
13966 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
13967 "Regular expression matching the last line of a property drawer.")
13969 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
13970 "Regular expression matching the first line of a property drawer.")
13972 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
13973 "Regular expression matching the first line of a property drawer.")
13975 (defconst org-property-drawer-re
13976 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
13977 org-property-end-re "\\)\n?")
13978 "Matches an entire property drawer.")
13980 (defconst org-clock-drawer-re
13981 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
13982 org-property-end-re "\\)\n?")
13983 "Matches an entire clock drawer.")
13985 (defsubst org-re-property (property)
13986 "Return a regexp matching PROPERTY.
13987 Match group 1 will be set to the value "
13988 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
13990 (defun org-property-action ()
13991 "Do an action on properties."
13994 (org-at-property-p)
13995 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
13996 (setq c (read-char-exclusive))
13999 (call-interactively 'org-set-property))
14001 (call-interactively 'org-delete-property))
14003 (call-interactively 'org-delete-property-globally))
14005 (call-interactively 'org-compute-property-at-point))
14006 (t (error "No such property action %c" c)))))
14008 (defun org-set-effort (&optional value)
14009 "Set the effort property of the current entry.
14010 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
14013 (if (equal value 0) (setq value 10))
14014 (let* ((completion-ignore-case t)
14015 (prop org-effort-property)
14016 (cur (org-entry-get nil prop))
14017 (allowed (org-property-get-allowed-values nil prop 'table))
14018 (existing (mapcar 'list (org-property-values prop)))
14021 ((stringp value) value)
14022 ((and allowed (integerp value))
14023 (or (car (nth (1- value) allowed))
14024 (car (org-last allowed))))
14026 (message "Select 1-9,0, [RET%s]: %s"
14027 (if cur (concat "=" cur) "")
14028 (mapconcat 'car allowed " "))
14029 (setq rpl (read-char-exclusive))
14030 (if (equal rpl ?\r)
14032 (setq rpl (- rpl ?0))
14033 (if (equal rpl 0) (setq rpl 10))
14034 (if (and (> rpl 0) (<= rpl (length allowed)))
14035 (car (nth (1- rpl) allowed))
14036 (org-completing-read "Effort: " allowed nil))))
14038 (let (org-completion-use-ido org-completion-use-iswitchb)
14039 (org-completing-read
14040 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14041 (concat "[" cur "]") "")
14043 existing nil nil "" nil cur))))))
14044 (unless (equal (org-entry-get nil prop) val)
14045 (org-entry-put nil prop val))
14046 (message "%s is now %s" prop val)))
14048 (defun org-at-property-p ()
14049 "Is cursor inside a property drawer?"
14051 (beginning-of-line 1)
14052 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14053 (save-match-data ;; Used by calling procedures
14055 (range (unless (org-before-first-heading-p)
14056 (org-get-property-block))))
14057 (and range (<= (car range) p) (< p (cdr range))))))))
14059 (defun org-get-property-block (&optional beg end force)
14060 "Return the (beg . end) range of the body of the property drawer.
14061 BEG and END can be beginning and end of subtree, if not given
14062 they will be found.
14063 If the drawer does not exist and FORCE is non-nil, create the drawer."
14066 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
14067 (end (or end (progn (outline-next-heading) (point)))))
14069 (if (re-search-forward org-property-start-re end t)
14070 (setq beg (1+ (match-end 0)))
14073 (org-insert-property-drawer)
14074 (setq end (progn (outline-next-heading) (point))))
14077 (if (re-search-forward org-property-start-re end t)
14078 (setq beg (1+ (match-end 0)))))
14079 (if (re-search-forward org-property-end-re end t)
14080 (setq end (match-beginning 0))
14081 (or force (throw 'exit nil))
14084 (org-indent-line-function)
14085 (insert ":END:\n"))
14088 (defun org-entry-properties (&optional pom which specific)
14089 "Get all properties of the entry at point-or-marker POM.
14090 This includes the TODO keyword, the tags, time strings for deadline,
14091 scheduled, and clocking, and any additional properties defined in the
14092 entry. The return value is an alist, keys may occur multiple times
14093 if the property key was used several times.
14094 POM may also be nil, in which case the current entry is used.
14095 If WHICH is nil or `all', get all properties. If WHICH is
14096 `special' or `standard', only get that subclass. If WHICH
14097 is a string only get exactly this property. SPECIFIC can be a string, the
14098 specific property we are interested in. Specifying it can speed
14099 things up because then unnecessary parsing is avoided."
14100 (setq which (or which 'all))
14101 (org-with-point-at pom
14102 (let ((clockstr (substring org-clock-string 0 -1))
14103 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14104 (case-fold-search nil)
14105 beg end range props sum-props key key1 value string clocksum)
14107 (when (condition-case nil
14108 (and (eq major-mode 'org-mode) (org-back-to-heading t))
14111 (setq sum-props (get-text-property (point) 'org-summaries))
14112 (setq clocksum (get-text-property (point) :org-clock-minutes))
14113 (outline-next-heading)
14115 (when (memq which '(all special))
14116 ;; Get the special properties, like TODO and tags
14118 (when (and (or (not specific) (string= specific "TODO"))
14119 (looking-at org-todo-line-regexp) (match-end 2))
14120 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14121 (when (and (or (not specific) (string= specific "PRIORITY"))
14122 (looking-at org-priority-regexp))
14123 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14124 (when (or (not specific) (string= specific "FILE"))
14125 (push (cons "FILE" buffer-file-name) props))
14126 (when (and (or (not specific) (string= specific "TAGS"))
14127 (setq value (org-get-tags-string))
14128 (string-match "\\S-" value))
14129 (push (cons "TAGS" value) props))
14130 (when (and (or (not specific) (string= specific "ALLTAGS"))
14131 (setq value (org-get-tags-at)))
14132 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14135 (when (or (not specific) (string= specific "BLOCKED"))
14136 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14137 (when (or (not specific)
14139 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14140 "TIMESTAMP" "TIMESTAMP_IA")))
14142 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14143 (setq key (if (match-end 1)
14144 (substring (org-match-string-no-properties 1)
14146 string (if (equal key clockstr)
14150 (match-beginning 3) (goto-char
14152 (substring (org-match-string-no-properties 3)
14154 ;; Get the correct property name from the key. This is
14155 ;; necessary if the user has configured time keywords.
14156 (setq key1 (concat key ":"))
14160 (if (= (char-after (match-beginning 3)) ?\[)
14161 "TIMESTAMP_IA" "TIMESTAMP")))
14162 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14163 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14164 ((equal key1 org-closed-string) (setq key "CLOSED"))
14165 ((equal key1 org-clock-string) (setq key "CLOCK")))
14166 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14168 (push (cons key string) props)
14169 ;; no need to search further if match is found
14171 (when (or (equal key "CLOCK") (not (assoc key props)))
14172 (push (cons key string) props))))))
14175 (when (memq which '(all standard))
14176 ;; Get the standard properties, like :PROP: ...
14177 (setq range (org-get-property-block beg end))
14179 (goto-char (car range))
14180 (while (re-search-forward
14181 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14183 (setq key (org-match-string-no-properties 1)
14184 value (org-trim (or (org-match-string-no-properties 2) "")))
14185 (unless (member key excluded)
14186 (push (cons key (or value "")) props)))))
14188 (push (cons "CLOCKSUM"
14189 (org-columns-number-to-string (/ (float clocksum) 60.)
14192 (unless (assoc "CATEGORY" props)
14193 (push (cons "CATEGORY" (org-get-category)) props))
14194 (append sum-props (nreverse props)))))))
14196 (defun org-entry-get (pom property &optional inherit literal-nil)
14197 "Get value of PROPERTY for entry at point-or-marker POM.
14198 If INHERIT is non-nil and the entry does not have the property,
14199 then also check higher levels of the hierarchy.
14200 If INHERIT is the symbol `selective', use inheritance only if the setting
14201 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14202 If the property is present but empty, the return value is the empty string.
14203 If the property is not present at all, nil is returned.
14205 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14206 do not interpret it as the list atom nil. This is used for inheritance
14207 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14208 (org-with-point-at pom
14209 (if (and inherit (if (eq inherit 'selective)
14210 (org-property-inherit-p property)
14212 (org-entry-get-with-inheritance property literal-nil)
14213 (if (member property org-special-properties)
14214 ;; We need a special property. Use `org-entry-properties' to
14215 ;; retrieve it, but specify the wanted property
14216 (cdr (assoc property (org-entry-properties nil 'special property)))
14217 (let ((range (unless (org-before-first-heading-p)
14218 (org-get-property-block)))
14219 (props (list (or (assoc property org-file-properties)
14220 (assoc property org-global-properties)
14221 (assoc property org-global-properties-fixed))))
14224 (when (re-search-forward
14225 (org-re-property key) (cdr range) t)
14227 (org-update-property-plist
14230 (org-match-string-no-properties 1) "")
14232 (when (and range (goto-char (car range)))
14234 (goto-char (car range))
14235 (while (ap (concat property "+")))
14236 (setq val (cdr (assoc property props)))
14237 (when val (if literal-nil val (org-not-nil val))))))))))
14239 (defun org-property-or-variable-value (var &optional inherit)
14240 "Check if there is a property fixing the value of VAR.
14241 If yes, return this value. If not, return the current value of the variable."
14242 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14243 (if (and prop (stringp prop) (string-match "\\S-" prop))
14245 (symbol-value var))))
14247 (defun org-entry-delete (pom property)
14248 "Delete the property PROPERTY from entry at point-or-marker POM."
14249 (org-with-point-at pom
14250 (if (member property org-special-properties)
14251 nil ; cannot delete these properties.
14252 (let ((range (org-get-property-block)))
14254 (goto-char (car range))
14256 (org-re-property property)
14259 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14263 ;; Multi-values properties are properties that contain multiple values
14264 ;; These values are assumed to be single words, separated by whitespace.
14265 (defun org-entry-add-to-multivalued-property (pom property value)
14266 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14267 (let* ((old (org-entry-get pom property))
14268 (values (and old (org-split-string old "[ \t]"))))
14269 (setq value (org-entry-protect-space value))
14270 (unless (member value values)
14271 (setq values (cons value values))
14272 (org-entry-put pom property
14273 (mapconcat 'identity values " ")))))
14275 (defun org-entry-remove-from-multivalued-property (pom property value)
14276 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14277 (let* ((old (org-entry-get pom property))
14278 (values (and old (org-split-string old "[ \t]"))))
14279 (setq value (org-entry-protect-space value))
14280 (when (member value values)
14281 (setq values (delete value values))
14282 (org-entry-put pom property
14283 (mapconcat 'identity values " ")))))
14285 (defun org-entry-member-in-multivalued-property (pom property value)
14286 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14287 (let* ((old (org-entry-get pom property))
14288 (values (and old (org-split-string old "[ \t]"))))
14289 (setq value (org-entry-protect-space value))
14290 (member value values)))
14292 (defun org-entry-get-multivalued-property (pom property)
14293 "Return a list of values in a multivalued property."
14294 (let* ((value (org-entry-get pom property))
14295 (values (and value (org-split-string value "[ \t]"))))
14296 (mapcar 'org-entry-restore-space values)))
14298 (defun org-entry-put-multivalued-property (pom property &rest values)
14299 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14300 VALUES should be a list of strings. Spaces will be protected."
14301 (org-entry-put pom property
14302 (mapconcat 'org-entry-protect-space values " "))
14303 (let* ((value (org-entry-get pom property))
14304 (values (and value (org-split-string value "[ \t]"))))
14305 (mapcar 'org-entry-restore-space values)))
14307 (defun org-entry-protect-space (s)
14308 "Protect spaces and newline in string S."
14309 (while (string-match " " s)
14310 (setq s (replace-match "%20" t t s)))
14311 (while (string-match "\n" s)
14312 (setq s (replace-match "%0A" t t s)))
14315 (defun org-entry-restore-space (s)
14316 "Restore spaces and newline in string S."
14317 (while (string-match "%20" s)
14318 (setq s (replace-match " " t t s)))
14319 (while (string-match "%0A" s)
14320 (setq s (replace-match "\n" t t s)))
14323 (defvar org-entry-property-inherited-from (make-marker)
14324 "Marker pointing to the entry from where a property was inherited.
14325 Each call to `org-entry-get-with-inheritance' will set this marker to the
14326 location of the entry where the inheritance search matched. If there was
14327 no match, the marker will point nowhere.
14328 Note that also `org-entry-get' calls this function, if the INHERIT flag
14331 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14332 "Get entry property, and search higher levels if not present.
14333 The search will stop at the first ancestor which has the property defined.
14334 If the value found is \"nil\", return nil to show that the property
14335 should be considered as undefined (this is the meaning of nil here).
14336 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14337 (move-marker org-entry-property-inherited-from nil)
14339 (unless (org-before-first-heading-p)
14345 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14346 (org-back-to-heading t)
14347 (move-marker org-entry-property-inherited-from (point))
14349 (or (org-up-heading-safe) (throw 'ex nil)))))))
14351 (cdr (assoc property org-file-properties))
14352 (cdr (assoc property org-global-properties))
14353 (cdr (assoc property org-global-properties-fixed))))
14354 (if literal-nil tmp (org-not-nil tmp))))
14356 (defvar org-property-changed-functions nil
14357 "Hook called when the value of a property has changed.
14358 Each hook function should accept two arguments, the name of the property
14359 and the new value.")
14361 (defun org-entry-put (pom property value)
14362 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14363 (org-with-point-at pom
14364 (org-back-to-heading t)
14365 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14368 ((equal property "TODO")
14369 (when (and (stringp value) (string-match "\\S-" value)
14370 (not (member value org-todo-keywords-1)))
14371 (error "\"%s\" is not a valid TODO state" value))
14372 (if (or (not value)
14373 (not (string-match "\\S-" value)))
14374 (setq value 'none))
14376 (org-set-tags nil 'align))
14377 ((equal property "PRIORITY")
14378 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14379 (string-to-char value) ?\ ))
14380 (org-set-tags nil 'align))
14381 ((equal property "SCHEDULED")
14382 (if (re-search-forward org-scheduled-time-regexp end t)
14384 ((eq value 'earlier) (org-timestamp-change -1 'day))
14385 ((eq value 'later) (org-timestamp-change 1 'day))
14386 (t (call-interactively 'org-schedule)))
14387 (call-interactively 'org-schedule)))
14388 ((equal property "DEADLINE")
14389 (if (re-search-forward org-deadline-time-regexp end t)
14391 ((eq value 'earlier) (org-timestamp-change -1 'day))
14392 ((eq value 'later) (org-timestamp-change 1 'day))
14393 (t (call-interactively 'org-deadline)))
14394 (call-interactively 'org-deadline)))
14395 ((member property org-special-properties)
14396 (error "The %s property can not yet be set with `org-entry-put'"
14398 (t ; a non-special property
14399 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14400 (setq range (org-get-property-block beg end 'force))
14401 (goto-char (car range))
14402 (if (re-search-forward
14403 (org-re-property property) (cdr range) t)
14405 (delete-region (match-beginning 0) (match-end 0))
14406 (goto-char (match-beginning 0)))
14407 (goto-char (cdr range))
14410 (org-indent-line-function))
14411 (insert ":" property ":")
14412 (and value (insert " " value))
14413 (org-indent-line-function)))))
14414 (run-hook-with-args 'org-property-changed-functions property value)))
14416 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14417 "Get all property keys in the current buffer.
14418 With INCLUDE-SPECIALS, also list the special properties that reflect things
14419 like tags and TODO state.
14420 With INCLUDE-DEFAULTS, also include properties that has special meaning
14421 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14423 With INCLUDE-COLUMNS, also include property names given in COLUMN
14424 formats in the current buffer."
14425 (let (rtn range cfmt s p)
14429 (goto-char (point-min))
14430 (while (re-search-forward org-property-start-re nil t)
14431 (setq range (org-get-property-block))
14432 (goto-char (car range))
14433 (while (re-search-forward
14434 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14436 (add-to-list 'rtn (org-match-string-no-properties 1)))
14437 (outline-next-heading))))
14439 (when include-specials
14440 (setq rtn (append org-special-properties rtn)))
14442 (when include-defaults
14443 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14444 (add-to-list 'rtn org-effort-property))
14446 (when include-columns
14450 (goto-char (point-min))
14451 (while (re-search-forward
14452 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14454 (setq cfmt (match-string 2) s 0)
14455 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14457 (setq s (match-end 0)
14458 p (match-string 1 cfmt))
14459 (unless (or (equal p "ITEM")
14460 (member p org-special-properties))
14461 (add-to-list 'rtn (match-string 1 cfmt))))))))
14463 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14465 (defun org-property-values (key)
14466 "Return a list of all values of property KEY in the current buffer."
14470 (goto-char (point-min))
14471 (let ((re (org-re-property key))
14473 (while (re-search-forward re nil t)
14474 (add-to-list 'values (org-trim (match-string 1))))
14475 (delete "" values)))))
14477 (defun org-insert-property-drawer ()
14478 "Insert a property drawer into the current entry."
14479 (org-back-to-heading t)
14480 (looking-at org-outline-regexp)
14481 (let ((indent (if org-adapt-indentation
14482 (- (match-end 0) (match-beginning 0))
14485 (re (concat "^[ \t]*" org-keyword-time-regexp))
14487 (outline-next-heading)
14490 (while (re-search-forward re end t))
14491 (setq hiddenp (outline-invisible-p))
14493 (and (equal (char-after) ?\n) (forward-char 1))
14494 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14495 (if (member (match-string 1) '("CLOCK:" ":END:"))
14496 ;; just skip this line
14497 (beginning-of-line 2)
14498 ;; Drawer start, find the end
14499 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14500 (beginning-of-line 1)))
14501 (org-skip-over-state-notes)
14502 (skip-chars-backward " \t\n\r")
14503 (if (eq (char-before) ?*) (forward-char 1))
14504 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14505 (beginning-of-line 0)
14506 (org-indent-to-column indent)
14507 (beginning-of-line 2)
14508 (org-indent-to-column indent)
14509 (beginning-of-line 0)
14512 (org-back-to-heading t)
14514 (org-flag-drawer t))))
14516 (defun org-insert-drawer (&optional arg drawer)
14517 "Insert a drawer at point.
14519 Optional argument DRAWER, when non-nil, is a string representing
14520 drawer's name. Otherwise, the user is prompted for a name.
14522 If a region is active, insert the drawer around that region
14525 Point is left between drawer's boundaries."
14527 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14529 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14530 ;; internally by Org. They are meant to be inserted
14532 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14533 ;; Remove system drawers from list. Note: For some reason,
14534 ;; `org-completing-read' ignores the predicate while
14535 ;; `completing-read' handles it fine.
14536 (drawer (if arg "PROPERTIES"
14539 "Drawer: " org-drawers
14540 (lambda (d) (not (member d system-drawers))))))))
14542 ;; With C-u, fall back on `org-insert-property-drawer'
14543 (arg (org-insert-property-drawer))
14544 ;; With an active region, insert a drawer at point.
14545 ((not (org-region-active-p))
14547 (unless (bolp) (insert "\n"))
14548 (insert (format ":%s:\n\n:END:\n" drawer))
14549 (forward-line -2)))
14550 ;; Otherwise, insert the drawer at point
14552 (let ((rbeg (region-beginning))
14553 (rend (copy-marker (region-end))))
14557 (beginning-of-line)
14558 (when (save-excursion
14559 (re-search-forward org-outline-regexp-bol rend t))
14560 (error "Drawers cannot contain headlines"))
14561 ;; Position point at the beginning of the first
14562 ;; non-blank line in region. Insert drawer's opening
14563 ;; there, then indent it.
14564 (org-skip-whitespace)
14565 (beginning-of-line)
14566 (insert ":" drawer ":\n")
14568 (indent-for-tab-command)
14569 ;; Move point to the beginning of the first blank line
14570 ;; after the last non-blank line in region. Insert
14571 ;; drawer's closing, then indent it.
14573 (skip-chars-backward " \r\t\n")
14575 (indent-for-tab-command)
14576 (unless (eolp) (insert "\n")))
14577 ;; Clear marker, whatever the outcome of insertion is.
14578 (set-marker rend nil)))))))
14580 (defvar org-property-set-functions-alist nil
14581 "Property set function alist.
14582 Each entry should have the following format:
14584 (PROPERTY . READ-FUNCTION)
14586 The read function will be called with the same argument as
14587 `org-completing-read'.")
14589 (defun org-set-property-function (property)
14590 "Get the function that should be used to set PROPERTY.
14591 This is computed according to `org-property-set-functions-alist'."
14592 (or (cdr (assoc property org-property-set-functions-alist))
14593 'org-completing-read))
14595 (defun org-read-property-value (property)
14596 "Read PROPERTY value from user."
14597 (let* ((completion-ignore-case t)
14598 (allowed (org-property-get-allowed-values nil property 'table))
14599 (cur (org-entry-get nil property))
14600 (prompt (concat property " value"
14601 (if (and cur (string-match "\\S-" cur))
14602 (concat " [" cur "]") "") ": "))
14603 (set-function (org-set-property-function property))
14605 (funcall set-function prompt allowed nil
14606 (not (get-text-property 0 'org-unrestricted
14608 (let (org-completion-use-ido org-completion-use-iswitchb)
14609 (funcall set-function prompt
14610 (mapcar 'list (org-property-values property))
14611 nil nil "" nil cur)))))
14616 (defvar org-last-set-property nil)
14617 (defun org-read-property-name ()
14618 "Read a property name."
14619 (let* ((completion-ignore-case t)
14620 (keys (org-buffer-property-keys nil t t))
14621 (default-prop (or (save-excursion
14623 (beginning-of-line)
14624 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14625 (null (string= (match-string 1) "END"))
14626 (match-string 1))))
14627 org-last-set-property))
14628 (property (org-icompleting-read
14630 (if default-prop (concat " [" default-prop "]") "")
14632 (mapcar 'list keys)
14636 (if (member property keys)
14638 (or (cdr (assoc (downcase property)
14639 (mapcar (lambda (x) (cons (downcase x) x))
14643 (defun org-set-property (property value)
14644 "In the current entry, set PROPERTY to VALUE.
14645 When called interactively, this will prompt for a property name, offering
14646 completion on existing and default properties. And then it will prompt
14647 for a value, offering completion either on allowed values (via an inherited
14648 xxx_ALL property) or on existing values in other instances of this property
14649 in the current file."
14650 (interactive (list nil nil))
14651 (let* ((property (or property (org-read-property-name)))
14652 (value (or value (org-read-property-value property)))
14653 (fn (assoc property org-properties-postprocess-alist)))
14654 (setq org-last-set-property property)
14655 ;; Possibly postprocess the inserted value:
14656 (when fn (setq value (funcall (cadr fn) value)))
14657 (unless (equal (org-entry-get nil property) value)
14658 (org-entry-put nil property value))))
14660 (defun org-delete-property (property)
14661 "In the current entry, delete PROPERTY."
14663 (let* ((completion-ignore-case t)
14664 (prop (org-icompleting-read "Property: "
14665 (org-entry-properties nil 'standard))))
14667 (message "Property %s %s" property
14668 (if (org-entry-delete nil property)
14670 "was not present in the entry")))
14672 (defun org-delete-property-globally (property)
14673 "Remove PROPERTY globally, from all entries."
14675 (let* ((completion-ignore-case t)
14676 (prop (org-icompleting-read
14677 "Globally remove property: "
14678 (mapcar 'list (org-buffer-property-keys)))))
14683 (goto-char (point-min))
14685 (while (re-search-forward
14686 (org-re-property property)
14688 (setq cnt (1+ cnt))
14689 (replace-match ""))
14690 (message "Property \"%s\" removed from %d entries" property cnt)))))
14692 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14694 (defun org-compute-property-at-point ()
14695 "Compute the property at point.
14696 This looks for an enclosing column format, extracts the operator and
14697 then applies it to the property in the column format's scope."
14699 (unless (org-at-property-p)
14700 (error "Not at a property"))
14701 (let ((prop (org-match-string-no-properties 2)))
14702 (org-columns-get-format-and-top-level)
14703 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14704 (error "No operator defined for property %s" prop))
14705 (org-columns-compute prop)))
14707 (defvar org-property-allowed-value-functions nil
14708 "Hook for functions supplying allowed values for a specific property.
14709 The functions must take a single argument, the name of the property, and
14710 return a flat list of allowed values. If \":ETC\" is one of
14711 the values, this means that these values are intended as defaults for
14712 completion, but that other values should be allowed too.
14713 The functions must return nil if they are not responsible for this
14716 (defun org-property-get-allowed-values (pom property &optional table)
14717 "Get allowed values for the property PROPERTY.
14718 When TABLE is non-nil, return an alist that can directly be used for
14722 ((equal property "TODO")
14723 (setq vals (org-with-point-at pom
14724 (append org-todo-keywords-1 '("")))))
14725 ((equal property "PRIORITY")
14726 (let ((n org-lowest-priority))
14727 (while (>= n org-highest-priority)
14728 (push (char-to-string n) vals)
14730 ((member property org-special-properties))
14731 ((setq vals (run-hook-with-args-until-success
14732 'org-property-allowed-value-functions property)))
14734 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
14735 (when (and vals (string-match "\\S-" vals))
14736 (setq vals (car (read-from-string (concat "(" vals ")"))))
14737 (setq vals (mapcar (lambda (x)
14738 (cond ((stringp x) x)
14739 ((numberp x) (number-to-string x))
14740 ((symbolp x) (symbol-name x))
14743 (when (member ":ETC" vals)
14744 (setq vals (remove ":ETC" vals))
14745 (org-add-props (car vals) '(org-unrestricted t)))
14746 (if table (mapcar 'list vals) vals)))
14748 (defun org-property-previous-allowed-value (&optional previous)
14749 "Switch to the next allowed value for this property."
14751 (org-property-next-allowed-value t))
14753 (defun org-property-next-allowed-value (&optional previous)
14754 "Switch to the next allowed value for this property."
14756 (unless (org-at-property-p)
14757 (error "Not at a property"))
14758 (let* ((key (match-string 2))
14759 (value (match-string 3))
14760 (allowed (or (org-property-get-allowed-values (point) key)
14761 (and (member value '("[ ]" "[-]" "[X]"))
14765 (error "Allowed values for this property have not been defined"))
14766 (if previous (setq allowed (reverse allowed)))
14767 (if (member value allowed)
14768 (setq nval (car (cdr (member value allowed)))))
14769 (setq nval (or nval (car allowed)))
14770 (if (equal nval value)
14771 (error "Only one allowed value for this property"))
14772 (org-at-property-p)
14773 (replace-match (concat " :" key ": " nval) t t)
14774 (org-indent-line-function)
14775 (beginning-of-line 1)
14776 (skip-chars-forward " \t")
14777 (run-hook-with-args 'org-property-changed-functions key nval)))
14779 (defun org-find-olp (path &optional this-buffer)
14780 "Return a marker pointing to the entry at outline path OLP.
14781 If anything goes wrong, throw an error.
14782 You can wrap this call to catch the error like this:
14784 (condition-case msg
14785 (org-mobile-locate-entry (match-string 4))
14786 (error (nth 1 msg)))
14788 The return value will then be either a string with the error message,
14789 or a marker if everything is OK.
14791 If THIS-BUFFER is set, the outline path does not contain a file,
14793 (let* ((file (if this-buffer buffer-file-name (pop path)))
14794 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
14798 limit re end found pos heading cnt flevel)
14799 (unless buffer (error "File not found :%s" file))
14800 (with-current-buffer buffer
14804 (setq limit (point-max))
14805 (goto-char (point-min))
14806 (while (setq heading (pop path))
14807 (setq re (format org-complex-heading-regexp-format
14808 (regexp-quote heading)))
14809 (setq cnt 0 pos (point))
14810 (while (re-search-forward re end t)
14811 (setq level (- (match-end 1) (match-beginning 1)))
14812 (if (and (>= level lmin) (<= level lmax))
14813 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
14814 (when (= cnt 0) (error "Heading not found on level %d: %s"
14816 (when (> cnt 1) (error "Heading not unique on level %d: %s"
14819 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
14820 (setq end (save-excursion (org-end-of-subtree t t))))
14821 (when (org-at-heading-p)
14822 (move-marker (make-marker) (point))))))))
14824 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
14825 "Find node HEADING in BUFFER.
14826 Return a marker to the heading if it was found, or nil if not.
14827 If POS-ONLY is set, return just the position instead of a marker.
14829 The heading text must match exact, but it may have a TODO keyword,
14830 a priority cookie and tags in the standard locations."
14831 (with-current-buffer (or buffer (current-buffer))
14835 (goto-char (point-min))
14836 (let (case-fold-search)
14837 (if (re-search-forward
14838 (format org-complex-heading-regexp-format
14839 (regexp-quote heading)) nil t)
14841 (match-beginning 0)
14842 (move-marker (make-marker) (match-beginning 0)))))))))
14844 (defun org-find-exact-heading-in-directory (heading &optional dir)
14845 "Find Org node headline HEADING in all .org files in directory DIR.
14846 When the target headline is found, return a marker to this location."
14847 (let ((files (directory-files (or dir default-directory)
14848 nil "\\`[^.#].*\\.org\\'"))
14849 file visiting m buffer)
14851 (while (setq file (pop files))
14852 (message "trying %s" file)
14853 (setq visiting (org-find-base-buffer-visiting file))
14854 (setq buffer (or visiting (find-file-noselect file)))
14855 (setq m (org-find-exact-headline-in-buffer
14857 (when (and (not m) (not visiting)) (kill-buffer buffer))
14858 (and m (throw 'found m))))))
14860 (defun org-find-entry-with-id (ident)
14861 "Locate the entry that contains the ID property with exact value IDENT.
14862 IDENT can be a string, a symbol or a number, this function will search for
14863 the string representation of it.
14864 Return the position where this entry starts, or nil if there is no such entry."
14865 (interactive "sID: ")
14867 ((stringp ident) ident)
14868 ((symbol-name ident) (symbol-name ident))
14869 ((numberp ident) (number-to-string ident))
14870 (t (error "IDENT %s must be a string, symbol or number" ident))))
14871 (case-fold-search nil))
14875 (goto-char (point-min))
14876 (when (re-search-forward
14877 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
14879 (org-back-to-heading t)
14884 (defvar org-last-changed-timestamp nil)
14885 (defvar org-last-inserted-timestamp nil
14886 "The last time stamp inserted with `org-insert-time-stamp'.")
14887 (defvar org-time-was-given) ; dynamically scoped parameter
14888 (defvar org-end-time-was-given) ; dynamically scoped parameter
14889 (defvar org-ts-what) ; dynamically scoped parameter
14891 (defun org-time-stamp (arg &optional inactive)
14892 "Prompt for a date/time and insert a time stamp.
14893 If the user specifies a time like HH:MM, or if this command is called
14894 with a prefix argument, the time stamp will contain date and time.
14895 Otherwise, only the date will be included. All parts of a date not
14896 specified by the user will be filled in from the current date/time.
14897 So if you press just return without typing anything, the time stamp
14898 will represent the current date/time. If there is already a timestamp
14899 at the cursor, it will be modified."
14903 ;; Default time is either today, or, when entering a range,
14904 ;; the range start.
14905 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
14907 (re-search-backward
14908 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
14909 (- (point) 20) t)))
14910 (apply 'encode-time (org-parse-time-string (match-string 1)))
14912 (default-input (and ts (org-get-compact-tod ts)))
14913 (repeater (save-excursion
14915 (beginning-of-line)
14916 (when (re-search-forward
14917 "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
14918 (save-excursion (progn (end-of-line) (point))) t)
14919 (match-string 0)))))
14920 org-time-was-given org-end-time-was-given time)
14922 ((and (org-at-timestamp-p t)
14923 (memq last-command '(org-time-stamp org-time-stamp-inactive))
14924 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
14926 (setq time (let ((this-command this-command))
14927 (org-read-date arg 'totime nil nil
14928 default-time default-input inactive)))
14929 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
14930 ((org-at-timestamp-p t)
14931 (setq time (let ((this-command this-command))
14932 (org-read-date arg 'totime nil nil default-time default-input inactive)))
14933 (when (org-at-timestamp-p t) ; just to get the match data
14934 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
14936 (setq org-last-changed-timestamp
14937 (org-insert-time-stamp
14938 time (or org-time-was-given arg)
14939 inactive nil nil (list org-end-time-was-given)))
14940 (when repeater (goto-char (1- (point))) (insert " " repeater)
14941 (setq org-last-changed-timestamp
14942 (concat (substring org-last-inserted-timestamp 0 -1)
14943 " " repeater ">"))))
14944 (message "Timestamp updated"))
14946 (setq time (let ((this-command this-command))
14947 (org-read-date arg 'totime nil nil default-time default-input inactive)))
14948 (org-insert-time-stamp time (or org-time-was-given arg) inactive
14949 nil nil (list org-end-time-was-given))))))
14951 ;; FIXME: can we use this for something else, like computing time differences?
14952 (defun org-get-compact-tod (s)
14953 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
14954 (let* ((t1 (match-string 1 s))
14955 (h1 (string-to-number (match-string 2 s)))
14956 (m1 (string-to-number (match-string 3 s)))
14957 (t2 (and (match-end 4) (match-string 5 s)))
14958 (h2 (and t2 (string-to-number (match-string 6 s))))
14959 (m2 (and t2 (string-to-number (match-string 7 s))))
14963 (setq dh (- h2 h1) dm (- m2 m1))
14964 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
14965 (concat t1 "+" (number-to-string dh)
14966 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
14968 (defun org-time-stamp-inactive (&optional arg)
14969 "Insert an inactive time stamp.
14970 An inactive time stamp is enclosed in square brackets instead of angle
14971 brackets. It is inactive in the sense that it does not trigger agenda entries,
14972 does not link to the calendar and cannot be changed with the S-cursor keys.
14973 So these are more for recording a certain time/date."
14975 (org-time-stamp arg 'inactive))
14977 (defvar org-date-ovl (make-overlay 1 1))
14978 (overlay-put org-date-ovl 'face 'org-warning)
14979 (org-detach-overlay org-date-ovl)
14981 (defvar org-ans1) ; dynamically scoped parameter
14982 (defvar org-ans2) ; dynamically scoped parameter
14984 (defvar org-plain-time-of-day-regexp) ; defined below
14986 (defvar org-overriding-default-time nil) ; dynamically scoped
14987 (defvar org-read-date-overlay nil)
14988 (defvar org-dcst nil) ; dynamically scoped
14989 (defvar org-read-date-history nil)
14990 (defvar org-read-date-final-answer nil)
14991 (defvar org-read-date-analyze-futurep nil)
14992 (defvar org-read-date-analyze-forced-year nil)
14993 (defvar org-read-date-inactive)
14995 (defun org-read-date (&optional with-time to-time from-string prompt
14996 default-time default-input inactive)
14997 "Read a date, possibly a time, and make things smooth for the user.
14998 The prompt will suggest to enter an ISO date, but you can also enter anything
14999 which will at least partially be understood by `parse-time-string'.
15000 Unrecognized parts of the date will default to the current day, month, year,
15001 hour and minute. If this command is called to replace a timestamp at point,
15002 or to enter the second timestamp of a range, the default time is taken
15003 from the existing stamp. Furthermore, the command prefers the future,
15004 so if you are giving a date where the year is not given, and the day-month
15005 combination is already past in the current year, it will assume you
15006 mean next year. For details, see the manual. A few examples:
15008 3-2-5 --> 2003-02-05
15009 feb 15 --> currentyear-02-15
15010 2/15 --> currentyear-02-15
15011 sep 12 9 --> 2009-09-12
15012 12:45 --> today 12:45
15013 22 sept 0:34 --> currentyear-09-22 0:34
15014 12 --> currentyear-currentmonth-12
15015 Fri --> nearest Friday (today or later)
15018 Furthermore you can specify a relative date by giving, as the *first* thing
15019 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
15020 change in days weeks, months, years.
15021 With a single plus or minus, the date is relative to today. With a double
15022 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15023 +4d --> four days from today
15024 +4 --> same as above
15025 +2w --> two weeks from today
15026 ++5 --> five days from default date
15028 The function understands only English month and weekday abbreviations,
15029 but this can be configured with the variables `parse-time-months' and
15030 `parse-time-weekdays'.
15032 While prompting, a calendar is popped up - you can also select the
15033 date with the mouse (button 1). The calendar shows a period of three
15034 months. To scroll it to other months, use the keys `>' and `<'.
15035 If you don't like the calendar, turn it off with
15036 \(setq org-read-date-popup-calendar nil)
15038 With optional argument TO-TIME, the date will immediately be converted
15039 to an internal time.
15040 With an optional argument WITH-TIME, the prompt will suggest to also
15041 insert a time. Note that when WITH-TIME is not set, you can still
15042 enter a time, and this function will inform the calling routine about
15043 this change. The calling routine may then choose to change the format
15044 used to insert the time stamp into the buffer to include the time.
15045 With optional argument FROM-STRING, read from this string instead from
15046 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15047 the time/date that is used for everything that is not specified by the
15049 (require 'parse-time)
15050 (let* ((org-time-stamp-rounding-minutes
15051 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15052 (org-dcst org-display-custom-times)
15053 (ct (org-current-time))
15054 (def (or org-overriding-default-time default-time ct))
15055 (defdecode (decode-time def))
15057 (when (< (nth 2 defdecode) org-extend-today-until)
15058 (setcar (nthcdr 2 defdecode) -1)
15059 (setcar (nthcdr 1 defdecode) 59)
15060 (setq def (apply 'encode-time defdecode)
15061 defdecode (decode-time def)))))
15062 (calendar-frame-setup nil)
15063 (calendar-setup nil)
15064 (calendar-move-hook nil)
15065 (calendar-view-diary-initially-flag nil)
15066 (calendar-view-holidays-initially-flag nil)
15067 (timestr (format-time-string
15068 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
15069 (prompt (concat (if prompt (concat prompt " ") "")
15070 (format "Date+time [%s]: " timestr)))
15071 ans (org-ans0 "") org-ans1 org-ans2 final)
15074 (from-string (setq ans from-string))
15075 (org-read-date-popup-calendar
15077 (save-window-excursion
15081 (calendar-forward-day (- (time-to-days def)
15082 (calendar-absolute-from-gregorian
15083 (calendar-current-date))))
15084 (org-eval-in-calendar nil t)
15085 (let* ((old-map (current-local-map))
15086 (map (copy-keymap calendar-mode-map))
15087 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15088 (org-defkey map (kbd "RET") 'org-calendar-select)
15089 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15090 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15091 (org-defkey minibuffer-local-map [(meta shift left)]
15092 (lambda () (interactive)
15093 (org-eval-in-calendar '(calendar-backward-month 1))))
15094 (org-defkey minibuffer-local-map [(meta shift right)]
15095 (lambda () (interactive)
15096 (org-eval-in-calendar '(calendar-forward-month 1))))
15097 (org-defkey minibuffer-local-map [(meta shift up)]
15098 (lambda () (interactive)
15099 (org-eval-in-calendar '(calendar-backward-year 1))))
15100 (org-defkey minibuffer-local-map [(meta shift down)]
15101 (lambda () (interactive)
15102 (org-eval-in-calendar '(calendar-forward-year 1))))
15103 (org-defkey minibuffer-local-map [?\e (shift left)]
15104 (lambda () (interactive)
15105 (org-eval-in-calendar '(calendar-backward-month 1))))
15106 (org-defkey minibuffer-local-map [?\e (shift right)]
15107 (lambda () (interactive)
15108 (org-eval-in-calendar '(calendar-forward-month 1))))
15109 (org-defkey minibuffer-local-map [?\e (shift up)]
15110 (lambda () (interactive)
15111 (org-eval-in-calendar '(calendar-backward-year 1))))
15112 (org-defkey minibuffer-local-map [?\e (shift down)]
15113 (lambda () (interactive)
15114 (org-eval-in-calendar '(calendar-forward-year 1))))
15115 (org-defkey minibuffer-local-map [(shift up)]
15116 (lambda () (interactive)
15117 (org-eval-in-calendar '(calendar-backward-week 1))))
15118 (org-defkey minibuffer-local-map [(shift down)]
15119 (lambda () (interactive)
15120 (org-eval-in-calendar '(calendar-forward-week 1))))
15121 (org-defkey minibuffer-local-map [(shift left)]
15122 (lambda () (interactive)
15123 (org-eval-in-calendar '(calendar-backward-day 1))))
15124 (org-defkey minibuffer-local-map [(shift right)]
15125 (lambda () (interactive)
15126 (org-eval-in-calendar '(calendar-forward-day 1))))
15127 (org-defkey minibuffer-local-map ">"
15128 (lambda () (interactive)
15129 (org-eval-in-calendar '(scroll-calendar-left 1))))
15130 (org-defkey minibuffer-local-map "<"
15131 (lambda () (interactive)
15132 (org-eval-in-calendar '(scroll-calendar-right 1))))
15133 (org-defkey minibuffer-local-map "\C-v"
15134 (lambda () (interactive)
15135 (org-eval-in-calendar
15136 '(calendar-scroll-left-three-months 1))))
15137 (org-defkey minibuffer-local-map "\M-v"
15138 (lambda () (interactive)
15139 (org-eval-in-calendar
15140 '(calendar-scroll-right-three-months 1))))
15141 (run-hooks 'org-read-date-minibuffer-setup-hook)
15144 (use-local-map map)
15145 (setq org-read-date-inactive inactive)
15146 (add-hook 'post-command-hook 'org-read-date-display)
15147 (setq org-ans0 (read-string prompt default-input
15148 'org-read-date-history nil))
15149 ;; org-ans0: from prompt
15150 ;; org-ans1: from mouse click
15151 ;; org-ans2: from calendar motion
15152 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15153 (remove-hook 'post-command-hook 'org-read-date-display)
15154 (use-local-map old-map)
15155 (when org-read-date-overlay
15156 (delete-overlay org-read-date-overlay)
15157 (setq org-read-date-overlay nil)))))
15158 (bury-buffer "*Calendar*")))))
15160 (t ; Naked prompt only
15162 (setq ans (read-string prompt default-input
15163 'org-read-date-history timestr))
15164 (when org-read-date-overlay
15165 (delete-overlay org-read-date-overlay)
15166 (setq org-read-date-overlay nil)))))
15168 (setq final (org-read-date-analyze ans def defdecode))
15170 (when org-read-date-analyze-forced-year
15171 (message "Year was forced into %s"
15172 (if org-read-date-force-compatible-dates
15173 "compatible range (1970-2037)"
15174 "range representable on this machine"))
15177 ;; One round trip to get rid of 34th of August and stuff like that....
15178 (setq final (decode-time (apply 'encode-time final)))
15180 (setq org-read-date-final-answer ans)
15183 (apply 'encode-time final)
15184 (if (and (boundp 'org-time-was-given) org-time-was-given)
15185 (format "%04d-%02d-%02d %02d:%02d"
15186 (nth 5 final) (nth 4 final) (nth 3 final)
15187 (nth 2 final) (nth 1 final))
15188 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15193 (defun org-read-date-display ()
15194 "Display the current date prompt interpretation in the minibuffer."
15195 (when org-read-date-display-live
15196 (when org-read-date-overlay
15197 (delete-overlay org-read-date-overlay))
15200 (while (not (equal (buffer-substring
15201 (max (point-min) (- (point) 4)) (point))
15205 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15206 " " (or org-ans1 org-ans2)))
15207 (org-end-time-was-given nil)
15208 (f (org-read-date-analyze ans def defdecode))
15210 org-time-stamp-custom-formats
15211 org-time-stamp-formats))
15212 (fmt (if (or with-time
15213 (and (boundp 'org-time-was-given) org-time-was-given))
15216 (txt (format-time-string fmt (apply 'encode-time f)))
15217 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15218 (txt (concat "=> " txt)))
15219 (when (and org-end-time-was-given
15220 (string-match org-plain-time-of-day-regexp txt))
15221 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15222 org-end-time-was-given
15223 (substring txt (match-end 0)))))
15224 (when org-read-date-analyze-futurep
15225 (setq txt (concat txt " (=>F)")))
15226 (setq org-read-date-overlay
15227 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15228 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
15230 (defun org-read-date-analyze (ans def defdecode)
15231 "Analyze the combined answer of the date prompt."
15232 ;; FIXME: cleanup and comment
15233 (let ((nowdecode (decode-time (current-time)))
15234 delta deltan deltaw deltadef year month day
15235 hour minute second wday pm h2 m2 tl wday1
15236 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15237 (setq org-read-date-analyze-futurep nil
15238 org-read-date-analyze-forced-year nil)
15239 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15242 (when (setq delta (org-read-date-get-relative ans (current-time) def))
15243 (setq ans (replace-match "" t t ans)
15245 deltaw (nth 1 delta)
15246 deltadef (nth 2 delta)))
15248 ;; Check if there is an iso week date in there
15249 ;; If yes, store the info and postpone interpreting it until the rest
15250 ;; of the parsing is done
15251 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15252 (setq iso-year (if (match-end 1)
15253 (org-small-year-to-year
15254 (string-to-number (match-string 1 ans))))
15255 iso-weekday (if (match-end 3)
15256 (string-to-number (match-string 3 ans)))
15257 iso-week (string-to-number (match-string 2 ans)))
15258 (setq ans (replace-match "" t t ans)))
15260 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15261 (when (string-match
15262 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15263 (setq year (if (match-end 2)
15264 (string-to-number (match-string 2 ans))
15265 (progn (setq kill-year t)
15266 (string-to-number (format-time-string "%Y"))))
15267 month (string-to-number (match-string 3 ans))
15268 day (string-to-number (match-string 4 ans)))
15269 (if (< year 100) (setq year (+ 2000 year)))
15270 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15273 ;; Help matching dotted european dates
15274 (when (string-match
15275 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
15276 (setq year (if (match-end 3)
15277 (string-to-number (match-string 3 ans))
15278 (progn (setq kill-year t)
15279 (string-to-number (format-time-string "%Y"))))
15280 day (string-to-number (match-string 1 ans))
15281 month (string-to-number (match-string 2 ans))
15282 ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15285 ;; Help matching american dates, like 5/30 or 5/30/7
15286 (when (string-match
15287 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15288 (setq year (if (match-end 4)
15289 (string-to-number (match-string 4 ans))
15290 (progn (setq kill-year t)
15291 (string-to-number (format-time-string "%Y"))))
15292 month (string-to-number (match-string 1 ans))
15293 day (string-to-number (match-string 2 ans)))
15294 (if (< year 100) (setq year (+ 2000 year)))
15295 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15297 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15298 ;; If there is a time with am/pm, and *no* time without it, we convert
15299 ;; so that matching will be successful.
15300 (loop for i from 1 to 2 do ; twice, for end time as well
15301 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15302 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15303 (setq hour (string-to-number (match-string 1 ans))
15304 minute (if (match-end 3)
15305 (string-to-number (match-string 3 ans))
15308 (string-to-char (downcase (match-string 4 ans)))))
15309 (if (and (= hour 12) (not pm))
15311 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15312 (setq ans (replace-match (format "%02d:%02d" hour minute)
15315 ;; Check if a time range is given as a duration
15316 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15317 (setq hour (string-to-number (match-string 1 ans))
15318 h2 (+ hour (string-to-number (match-string 3 ans)))
15319 minute (string-to-number (match-string 2 ans))
15320 m2 (+ minute (if (match-end 5) (string-to-number
15321 (match-string 5 ans))0)))
15322 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15323 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15326 ;; Check if there is a time range
15327 (when (boundp 'org-end-time-was-given)
15328 (setq org-time-was-given nil)
15329 (when (and (string-match org-plain-time-of-day-regexp ans)
15331 (setq org-end-time-was-given (match-string 8 ans))
15332 (setq ans (concat (substring ans 0 (match-beginning 7))
15333 (substring ans (match-end 7))))))
15335 (setq tl (parse-time-string ans)
15336 day (or (nth 3 tl) (nth 3 defdecode))
15337 month (or (nth 4 tl)
15338 (if (and org-read-date-prefer-future
15339 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15340 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15341 (nth 4 defdecode)))
15342 year (or (and (not kill-year) (nth 5 tl))
15343 (if (and org-read-date-prefer-future
15344 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15345 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15346 (nth 5 defdecode)))
15347 hour (or (nth 2 tl) (nth 2 defdecode))
15348 minute (or (nth 1 tl) (nth 1 defdecode))
15349 second (or (nth 0 tl) 0)
15352 (when (and (eq org-read-date-prefer-future 'time)
15353 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15354 (equal day (nth 3 nowdecode))
15355 (equal month (nth 4 nowdecode))
15356 (equal year (nth 5 nowdecode))
15358 (or (< (nth 2 tl) (nth 2 nowdecode))
15359 (and (= (nth 2 tl) (nth 2 nowdecode))
15361 (< (nth 1 tl) (nth 1 nowdecode)))))
15365 ;; Special date definitions below
15368 ;; There was an iso week
15371 (setq year (or iso-year year)
15372 day (or iso-weekday wday 1)
15373 wday nil ; to make sure that the trigger below does not match
15374 iso-date (calendar-gregorian-from-absolute
15375 (calendar-absolute-from-iso
15376 (list iso-week day year))))
15377 ; FIXME: Should we also push ISO weeks into the future?
15378 ; (when (and org-read-date-prefer-future
15380 ; (< (calendar-absolute-from-gregorian iso-date)
15381 ; (time-to-days (current-time))))
15382 ; (setq year (1+ year)
15383 ; iso-date (calendar-gregorian-from-absolute
15384 ; (calendar-absolute-from-iso
15385 ; (list iso-week day year)))))
15386 (setq month (car iso-date)
15387 year (nth 2 iso-date)
15388 day (nth 1 iso-date)))
15392 (let ((now (decode-time (current-time))))
15393 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15394 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15395 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15396 ((equal deltaw "m") (setq month (+ month deltan)))
15397 ((equal deltaw "y") (setq year (+ year deltan)))))
15398 ((and wday (not (nth 3 tl)))
15400 ;; Weekday was given, but no day, so pick that day in the week
15401 ;; on or after the derived date.
15402 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15403 (unless (equal wday wday1)
15404 (setq day (+ day (% (- wday wday1 -7) 7))))))
15405 (if (and (boundp 'org-time-was-given)
15407 (setq org-time-was-given t))
15408 (if (< year 100) (setq year (+ 2000 year)))
15409 ;; Check of the date is representable
15410 (if org-read-date-force-compatible-dates
15413 (setq year 1970 org-read-date-analyze-forced-year t))
15415 (setq year 2037 org-read-date-analyze-forced-year t)))
15416 (condition-case nil
15417 (ignore (encode-time second minute hour day month year))
15419 (setq year (nth 5 defdecode))
15420 (setq org-read-date-analyze-forced-year t))))
15421 (setq org-read-date-analyze-futurep futurep)
15422 (list second minute hour day month year)))
15424 (defvar parse-time-weekdays)
15426 (defun org-read-date-get-relative (s today default)
15427 "Check string S for special relative date string.
15428 TODAY and DEFAULT are internal times, for today and for a default.
15429 Return shift list (N what def-flag)
15430 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15431 N is the number of WHATs to shift.
15432 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15433 the DEFAULT date rather than TODAY."
15434 (require 'parse-time)
15438 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15440 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15441 "\\([ \t]\\|$\\)") s)
15442 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15443 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15444 (string-to-char (substring (match-string 1 s) -1))
15446 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15447 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15448 (what (if (match-end 3) (match-string 3 s) "d"))
15449 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15450 (date (if rel default today))
15451 (wday (nth 6 (decode-time date)))
15455 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15456 (if (= dir ?-) (setq delta (- delta 7)))
15457 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15458 (list delta "d" rel))
15459 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15461 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15462 "Turn a user-specified date into the internal representation.
15463 The internal representation needed by the calendar is (month day year).
15464 This is a wrapper to handle the brain-dead convention in calendar that
15465 user function argument order change dependent on argument order."
15466 (if (boundp 'calendar-date-style)
15468 ((eq calendar-date-style 'american)
15469 (list arg1 arg2 arg3))
15470 ((eq calendar-date-style 'european)
15471 (list arg2 arg1 arg3))
15472 ((eq calendar-date-style 'iso)
15473 (list arg2 arg3 arg1)))
15474 (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15475 (if (org-bound-and-true-p european-calendar-style)
15476 (list arg2 arg1 arg3)
15477 (list arg1 arg2 arg3)))))
15479 (defun org-eval-in-calendar (form &optional keepdate)
15480 "Eval FORM in the calendar window and return to current window.
15481 Also, store the cursor date in variable org-ans2."
15482 (let ((sf (selected-frame))
15483 (sw (selected-window)))
15484 (select-window (get-buffer-window "*Calendar*" t))
15486 (when (and (not keepdate) (calendar-cursor-to-date))
15487 (let* ((date (calendar-cursor-to-date))
15488 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15489 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15490 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15492 (org-select-frame-set-input-focus sf)))
15494 (defun org-calendar-select ()
15495 "Return to `org-read-date' with the date currently selected.
15496 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15498 (when (calendar-cursor-to-date)
15499 (let* ((date (calendar-cursor-to-date))
15500 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15501 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15502 (if (active-minibuffer-window) (exit-minibuffer))))
15504 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15505 "Insert a date stamp for the date given by the internal TIME.
15506 WITH-HM means use the stamp format that includes the time of the day.
15507 INACTIVE means use square brackets instead of angular ones, so that the
15508 stamp will not contribute to the agenda.
15509 PRE and POST are optional strings to be inserted before and after the
15511 The command returns the inserted time stamp."
15512 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15514 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15515 (insert-before-markers (or pre ""))
15516 (when (listp extra)
15517 (setq extra (car extra))
15518 (if (and (stringp extra)
15519 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15520 (setq extra (format "-%02d:%02d"
15521 (string-to-number (match-string 1 extra))
15522 (string-to-number (match-string 2 extra))))
15525 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15526 (insert-before-markers (setq stamp (format-time-string fmt time)))
15527 (insert-before-markers (or post ""))
15528 (setq org-last-inserted-timestamp stamp)))
15530 (defun org-toggle-time-stamp-overlays ()
15531 "Toggle the use of custom time stamp formats."
15533 (setq org-display-custom-times (not org-display-custom-times))
15534 (unless org-display-custom-times
15535 (let ((p (point-min)) (bmp (buffer-modified-p)))
15536 (while (setq p (next-single-property-change p 'display))
15537 (if (and (get-text-property p 'display)
15538 (eq (get-text-property p 'face) 'org-date))
15539 (remove-text-properties
15540 p (setq p (next-single-property-change p 'display))
15542 (set-buffer-modified-p bmp)))
15543 (if (featurep 'xemacs)
15544 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15545 (org-restart-font-lock)
15546 (setq org-table-may-need-update t)
15547 (if org-display-custom-times
15548 (message "Time stamps are overlaid with custom format")
15549 (message "Time stamp overlays removed")))
15551 (defun org-display-custom-time (beg end)
15552 "Overlay modified time stamp format over timestamp between BEG and END."
15553 (let* ((ts (buffer-substring beg end))
15554 t1 w1 with-hm tf time str w2 (off 0))
15556 (setq t1 (org-parse-time-string ts t))
15557 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
15558 (setq off (- (match-end 0) (match-beginning 0)))))
15559 (setq end (- end off))
15560 (setq w1 (- end beg)
15561 with-hm (and (nth 1 t1) (nth 2 t1))
15562 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15563 time (org-fix-decoded-time t1)
15565 (format-time-string
15566 (substring tf 1 -1) (apply 'encode-time time))
15567 nil 'mouse-face 'highlight)
15569 (if (not (= w2 w1))
15570 (add-text-properties (1+ beg) (+ 2 beg)
15571 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15572 (if (featurep 'xemacs)
15574 (put-text-property beg end 'invisible t)
15575 (put-text-property beg end 'end-glyph (make-glyph str)))
15576 (put-text-property beg end 'display str))))
15578 (defun org-translate-time (string)
15579 "Translate all timestamps in STRING to custom format.
15580 But do this only if the variable `org-display-custom-times' is set."
15581 (when org-display-custom-times
15584 (re org-ts-regexp-both)
15585 t1 with-hm inactive tf time str beg end)
15586 (while (setq start (string-match re string start))
15587 (setq beg (match-beginning 0)
15589 t1 (save-match-data
15590 (org-parse-time-string (substring string beg end) t))
15591 with-hm (and (nth 1 t1) (nth 2 t1))
15592 inactive (equal (substring string beg (1+ beg)) "[")
15593 tf (funcall (if with-hm 'cdr 'car)
15594 org-time-stamp-custom-formats)
15595 time (org-fix-decoded-time t1)
15596 str (format-time-string
15598 (if inactive "[" "<") (substring tf 1 -1)
15599 (if inactive "]" ">"))
15600 (apply 'encode-time time))
15601 string (replace-match str t t string)
15602 start (+ start (length str)))))))
15605 (defun org-fix-decoded-time (time)
15606 "Set 0 instead of nil for the first 6 elements of time.
15607 Don't touch the rest."
15609 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15611 (defun org-days-to-time (timestamp-string)
15612 "Difference between TIMESTAMP-STRING and now in days."
15613 (- (time-to-days (org-time-string-to-time timestamp-string))
15614 (time-to-days (current-time))))
15616 (defun org-deadline-close (timestamp-string &optional ndays)
15617 "Is the time in TIMESTAMP-STRING close to the current date?"
15618 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15619 (and (< (org-days-to-time timestamp-string) ndays)
15620 (not (org-entry-is-done-p))))
15622 (defun org-get-wdays (ts)
15623 "Get the deadline lead time appropriate for timestring TS."
15625 ((<= org-deadline-warning-days 0)
15626 ;; 0 or negative, enforce this value no matter what
15627 (- org-deadline-warning-days))
15628 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
15629 ;; lead time is specified.
15630 (floor (* (string-to-number (match-string 1 ts))
15631 (cdr (assoc (match-string 2 ts)
15632 '(("d" . 1) ("w" . 7)
15633 ("m" . 30.4) ("y" . 365.25)))))))
15634 ;; go for the default.
15635 (t org-deadline-warning-days)))
15637 (defun org-calendar-select-mouse (ev)
15638 "Return to `org-read-date' with the date currently selected.
15639 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15641 (mouse-set-point ev)
15642 (when (calendar-cursor-to-date)
15643 (let* ((date (calendar-cursor-to-date))
15644 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15645 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15646 (if (active-minibuffer-window) (exit-minibuffer))))
15648 (defun org-check-deadlines (ndays)
15649 "Check if there are any deadlines due or past due.
15650 A deadline is considered due if it happens within `org-deadline-warning-days'
15651 days from today's date. If the deadline appears in an entry marked DONE,
15652 it is not shown. The prefix arg NDAYS can be used to test that many
15653 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15655 (let* ((org-warn-days
15657 ((equal ndays '(4)) 100000)
15658 (ndays (prefix-numeric-value ndays))
15659 (t (abs org-deadline-warning-days))))
15660 (case-fold-search nil)
15661 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15663 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15665 (message "%d deadlines past-due or due within %d days"
15666 (org-occur regexp nil callback)
15669 (defun org-check-before-date (date)
15670 "Check if there are deadlines or scheduled entries before DATE."
15671 (interactive (list (org-read-date)))
15672 (let ((case-fold-search nil)
15673 (regexp (concat "\\<\\(" org-deadline-string
15674 "\\|" org-scheduled-string
15675 "\\) *<\\([^>]+\\)>"))
15677 (lambda () (time-less-p
15678 (org-time-string-to-time (match-string 2))
15679 (org-time-string-to-time date)))))
15680 (message "%d entries before %s"
15681 (org-occur regexp nil callback) date)))
15683 (defun org-check-after-date (date)
15684 "Check if there are deadlines or scheduled entries after DATE."
15685 (interactive (list (org-read-date)))
15686 (let ((case-fold-search nil)
15687 (regexp (concat "\\<\\(" org-deadline-string
15688 "\\|" org-scheduled-string
15689 "\\) *<\\([^>]+\\)>"))
15693 (org-time-string-to-time (match-string 2))
15694 (org-time-string-to-time date))))))
15695 (message "%d entries after %s"
15696 (org-occur regexp nil callback) date)))
15698 (defun org-check-dates-range (start-date end-date)
15699 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
15700 (interactive (list (org-read-date nil nil nil "Range starts")
15701 (org-read-date nil nil nil "Range end")))
15702 (let ((case-fold-search nil)
15703 (regexp (concat "\\<\\(" org-deadline-string
15704 "\\|" org-scheduled-string
15705 "\\) *<\\([^>]+\\)>"))
15708 (let ((match (match-string 2)))
15711 (org-time-string-to-time match)
15712 (org-time-string-to-time start-date)))
15714 (org-time-string-to-time match)
15715 (org-time-string-to-time end-date)))))))
15716 (message "%d entries between %s and %s"
15717 (org-occur regexp nil callback) start-date end-date)))
15719 (defun org-evaluate-time-range (&optional to-buffer)
15720 "Evaluate a time range by computing the difference between start and end.
15721 Normally the result is just printed in the echo area, but with prefix arg
15722 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
15723 If the time range is actually in a table, the result is inserted into the
15725 For time difference computation, a year is assumed to be exactly 365
15726 days in order to avoid rounding problems."
15729 (org-clock-update-time-maybe)
15731 (unless (org-at-date-range-p t)
15732 (goto-char (point-at-bol))
15733 (re-search-forward org-tr-regexp-both (point-at-eol) t))
15734 (if (not (org-at-date-range-p t))
15735 (error "Not at a time-stamp range, and none found in current line")))
15736 (let* ((ts1 (match-string 1))
15737 (ts2 (match-string 2))
15738 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
15739 (match-end (match-end 0))
15740 (time1 (org-time-string-to-time ts1))
15741 (time2 (org-time-string-to-time ts2))
15742 (t1 (org-float-time time1))
15743 (t2 (org-float-time time2))
15744 (diff (abs (- t2 t1)))
15745 (negative (< (- t2 t1) 0))
15746 ;; (ys (floor (* 365 24 60 60)))
15749 (fy "%dy %dd %02d:%02d")
15751 (fd "%dd %02d:%02d")
15756 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15758 d (floor (/ diff ds)) diff (mod diff ds)
15759 h (floor (/ diff hs)) diff (mod diff hs)
15760 m (floor (/ diff 60)))
15761 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15763 d (floor (+ (/ diff ds) 0.5))
15765 (if (not to-buffer)
15766 (message "%s" (org-make-tdiff-string y d h m))
15767 (if (org-at-table-p)
15769 (goto-char match-end)
15771 (and (looking-at " *|") (goto-char (match-end 0))))
15772 (goto-char match-end))
15774 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
15775 (replace-match ""))
15776 (if negative (insert " -"))
15777 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
15778 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
15779 (insert " " (format fh h m))))
15780 (if align (org-table-align))
15781 (message "Time difference inserted")))))
15783 (defun org-make-tdiff-string (y d h m)
15786 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
15788 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
15790 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
15792 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
15794 (apply 'format fmt (nreverse l))))
15796 (defun org-time-string-to-time (s &optional buffer pos)
15797 (condition-case errdata
15798 (apply 'encode-time (org-parse-time-string s))
15799 (error (error "Bad timestamp `%s'%s\nError was: %s"
15800 s (if (not (and buffer pos))
15802 (format " at %d in buffer `%s'" pos buffer))
15805 (defun org-time-string-to-seconds (s)
15806 (org-float-time (org-time-string-to-time s)))
15808 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
15809 "Convert a time stamp to an absolute day number.
15810 If there is a specifier for a cyclic time stamp, get the closest date to
15812 PREFER and SHOW-ALL are passed through to `org-closest-date'.
15813 The variable date is bound by the calendar when this is called."
15815 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
15816 (if (org-diary-sexp-entry (match-string 1 s) "" date)
15819 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
15820 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
15821 (time-to-days (current-time))) (match-string 0 s)
15824 (condition-case errdata
15825 (apply 'encode-time (org-parse-time-string s))
15826 (error (error "Bad timestamp `%s'%s\nError was: %s"
15827 s (if (not (and buffer pos))
15829 (format " at %d in buffer `%s'" pos buffer))
15830 (cdr errdata))))))))
15832 (defun org-days-to-iso-week (days)
15833 "Return the iso week number."
15835 (car (calendar-iso-from-absolute days)))
15837 (defun org-small-year-to-year (year)
15838 "Convert 2-digit years into 4-digit years.
15839 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
15840 The year 2000 cannot be abbreviated. Any year larger than 99
15841 is returned unchanged."
15843 (setq year (+ 2000 year))
15845 (setq year (+ 1900 year))))
15848 (defun org-time-from-absolute (d)
15849 "Return the time corresponding to date D.
15850 D may be an absolute day number, or a calendar-type list (month day year)."
15851 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
15852 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
15854 (defun org-calendar-holiday ()
15855 "List of holidays, for Diary display in Org-mode."
15856 (require 'holidays)
15858 (if (fboundp 'calendar-check-holidays)
15859 'calendar-check-holidays 'check-calendar-holidays) date)))
15860 (if hl (mapconcat 'identity hl "; "))))
15862 (defun org-diary-sexp-entry (sexp entry date)
15863 "Process a SEXP diary ENTRY for DATE."
15864 (require 'diary-lib)
15865 (let ((result (if calendar-debug-sexp
15866 (let ((stack-trace-on-error t))
15867 (eval (car (read-from-string sexp))))
15868 (condition-case nil
15869 (eval (car (read-from-string sexp)))
15872 (message "Bad sexp at line %d in %s: %s"
15874 (buffer-file-name) sexp)
15876 (cond ((stringp result) (split-string result "; "))
15877 ((and (consp result)
15878 (not (consp (cdr result)))
15879 (stringp (cdr result))) (cdr result))
15880 ((and (consp result)
15881 (stringp (car result))) result)
15885 (defun org-diary-to-ical-string (frombuf)
15886 "Get iCalendar entries from diary entries in buffer FROMBUF.
15887 This uses the icalendar.el library."
15888 (let* ((tmpdir (if (featurep 'xemacs)
15890 temporary-file-directory))
15891 (tmpfile (make-temp-name
15892 (expand-file-name "orgics" tmpdir)))
15894 (with-current-buffer frombuf
15895 (icalendar-export-region (point-min) (point-max) tmpfile)
15896 (setq buf (find-buffer-visiting tmpfile))
15898 (goto-char (point-min))
15899 (if (re-search-forward "^BEGIN:VEVENT" nil t)
15900 (setq b (match-beginning 0)))
15901 (goto-char (point-max))
15902 (if (re-search-backward "^END:VEVENT" nil t)
15903 (setq e (match-end 0)))
15904 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
15906 (delete-file tmpfile)
15909 (defun org-closest-date (start current change prefer show-all)
15910 "Find the date closest to CURRENT that is consistent with START and CHANGE.
15911 When PREFER is `past', return a date that is either CURRENT or past.
15912 When PREFER is `future', return a date that is either CURRENT or future.
15913 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
15914 ;; Make the proper lists from the dates
15916 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
15917 dn dw sday cday n1 n2 n0
15918 d m y y1 y2 date1 date2 nmonths nm ny m2)
15920 (setq start (org-date-to-gregorian start)
15921 current (org-date-to-gregorian
15924 (time-to-days (current-time))))
15925 sday (calendar-absolute-from-gregorian start)
15926 cday (calendar-absolute-from-gregorian current))
15928 (if (<= cday sday) (throw 'exit sday))
15930 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
15931 (setq dn (string-to-number (match-string 1 change))
15932 dw (cdr (assoc (match-string 2 change) a1)))
15933 (error "Invalid change specifier: %s" change))
15934 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
15937 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
15940 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
15941 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
15942 (setq date1 (list m d y1)
15943 n1 (calendar-absolute-from-gregorian date1)
15944 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
15945 n2 (calendar-absolute-from-gregorian date2)))
15947 ;; approx number of month between the two dates
15948 (setq nmonths (floor (/ (- cday sday) 30.436875)))
15949 ;; How often does dn fit in there?
15950 (setq d (nth 1 start) m (car start) y (nth 2 start)
15951 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
15953 ny (floor (/ m 12))
15956 (while (> m 12) (setq m (- m 12) y (1+ y)))
15957 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
15958 (setq m2 (+ m dn) y2 y)
15959 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15960 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
15961 (while (<= n2 cday)
15962 (setq n1 n2 m m2 y y2)
15963 (setq m2 (+ m dn) y2 y)
15964 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15965 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
15966 ;; Make sure n1 is the earlier date
15967 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
15970 ((eq prefer 'past) (if (= cday n2) n2 n1))
15971 ((eq prefer 'future) (if (= cday n1) n1 n2))
15972 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
15974 ((eq prefer 'past) (if (= cday n2) n2 n1))
15975 ((eq prefer 'future) (if (= cday n1) n1 n2))
15976 (t (if (= cday n1) n1 n2)))))))
15978 (defun org-date-to-gregorian (date)
15979 "Turn any specification of DATE into a Gregorian date for the calendar."
15980 (cond ((integerp date) (calendar-gregorian-from-absolute date))
15981 ((and (listp date) (= (length date) 3)) date)
15983 (setq date (org-parse-time-string date))
15984 (list (nth 4 date) (nth 3 date) (nth 5 date)))
15986 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
15988 (defun org-parse-time-string (s &optional nodefault)
15989 "Parse the standard Org-mode time string.
15990 This should be a lot faster than the normal `parse-time-string'.
15991 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
15992 hour and minute fields will be nil if not given."
15993 (if (string-match org-ts-regexp0 s)
15995 (if (or (match-beginning 8) (not nodefault))
15996 (string-to-number (or (match-string 8 s) "0")))
15997 (if (or (match-beginning 7) (not nodefault))
15998 (string-to-number (or (match-string 7 s) "0")))
15999 (string-to-number (match-string 4 s))
16000 (string-to-number (match-string 3 s))
16001 (string-to-number (match-string 2 s))
16003 (error "Not a standard Org-mode time string: %s" s)))
16005 (defun org-timestamp-up (&optional arg)
16006 "Increase the date item at the cursor by one.
16007 If the cursor is on the year, change the year. If it is on the month,
16008 the day or the time, change that.
16009 With prefix ARG, change by that many units."
16011 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16013 (defun org-timestamp-down (&optional arg)
16014 "Decrease the date item at the cursor by one.
16015 If the cursor is on the year, change the year. If it is on the month,
16016 the day or the time, change that.
16017 With prefix ARG, change by that many units."
16019 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16021 (defun org-timestamp-up-day (&optional arg)
16022 "Increase the date in the time stamp by one day.
16023 With prefix ARG, change that many days."
16025 (if (and (not (org-at-timestamp-p t))
16026 (org-at-heading-p))
16028 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16030 (defun org-timestamp-down-day (&optional arg)
16031 "Decrease the date in the time stamp by one day.
16032 With prefix ARG, change that many days."
16034 (if (and (not (org-at-timestamp-p t))
16035 (org-at-heading-p))
16037 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16039 (defun org-at-timestamp-p (&optional inactive-ok)
16040 "Determine if the cursor is in or at a timestamp."
16042 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16044 (ans (or (looking-at tsr)
16046 (skip-chars-backward "^[<\n\r\t")
16047 (if (> (point) (point-min)) (backward-char 1))
16048 (and (looking-at tsr)
16049 (> (- (match-end 0) pos) -1))))))
16051 (boundp 'org-ts-what)
16054 ((= pos (match-beginning 0)) 'bracket)
16055 ;; Point is considered to be "on the bracket" whether
16056 ;; it's really on it or right after it.
16057 ((or (= pos (1- (match-end 0)))
16058 (= pos (match-end 0))) 'bracket)
16059 ((org-pos-in-match-range pos 2) 'year)
16060 ((org-pos-in-match-range pos 3) 'month)
16061 ((org-pos-in-match-range pos 7) 'hour)
16062 ((org-pos-in-match-range pos 8) 'minute)
16063 ((or (org-pos-in-match-range pos 4)
16064 (org-pos-in-match-range pos 5)) 'day)
16065 ((and (> pos (or (match-end 8) (match-end 5)))
16066 (< pos (match-end 0)))
16067 (- pos (or (match-end 8) (match-end 5))))
16071 (defun org-toggle-timestamp-type ()
16072 "Toggle the type (<active> or [inactive]) of a time stamp."
16074 (when (org-at-timestamp-p t)
16075 (let ((beg (match-beginning 0)) (end (match-end 0))
16076 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16079 (while (re-search-forward "[][<>]" end t)
16080 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16082 (message "Timestamp is now %sactive"
16083 (if (equal (char-after beg) ?<) "" "in")))))
16085 (defun org-timestamp-change (n &optional what updown)
16086 "Change the date in the time stamp at point.
16087 The date will be changed by N times WHAT. WHAT can be `day', `month',
16088 `year', `minute', `second'. If WHAT is not given, the cursor position
16089 in the timestamp determines what will be changed."
16090 (let ((origin (point)) origin-cat
16092 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16096 (if (not (org-at-timestamp-p t))
16097 (error "Not at a timestamp"))
16098 (if (and (not what) (eq org-ts-what 'bracket))
16099 (org-toggle-timestamp-type)
16100 ;; Point isn't on brackets. Remember the part of the time-stamp
16101 ;; the point was in. Indeed, size of time-stamps may change,
16102 ;; but point must be kept in the same category nonetheless.
16103 (setq origin-cat org-ts-what)
16104 (if (and (not what) (not (eq org-ts-what 'day))
16105 org-display-custom-times
16106 (get-text-property (point) 'display)
16107 (not (get-text-property (1- (point)) 'display)))
16108 (setq org-ts-what 'day))
16109 (setq org-ts-what (or what org-ts-what)
16110 inactive (= (char-after (match-beginning 0)) ?\[)
16111 ts (match-string 0))
16114 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
16116 (setq extra (match-string 1 ts)))
16117 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16119 (setq time0 (org-parse-time-string ts))
16121 (eq org-ts-what 'minute)
16122 (not current-prefix-arg))
16123 ;; This looks like s-up and s-down. Change by one rounding step.
16124 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16125 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16126 (setcar (cdr time0) (+ (nth 1 time0)
16127 (if (> n 0) (- rem) (- dm rem))))))
16129 (encode-time (or (car time0) 0)
16130 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16131 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16132 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16133 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16134 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16136 (when (and (member org-ts-what '(hour minute))
16138 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16139 (setq extra (org-modify-ts-extra
16141 (if (eq org-ts-what 'hour) 2 5)
16143 (when (integerp org-ts-what)
16144 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16145 (if (eq what 'calendar)
16146 (let ((cal-date (org-get-date-from-calendar)))
16147 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16148 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16149 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16150 (setcar time0 (or (car time0) 0))
16151 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16152 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16153 (setq time (apply 'encode-time time0))))
16154 ;; Insert the new time-stamp, and ensure point stays in the same
16155 ;; category as before (i.e. not after the last position in that
16157 (let ((pos (point)))
16158 ;; Stay before inserted string. `save-excursion' is of no use.
16159 (setq org-last-changed-timestamp
16160 (org-insert-time-stamp time with-hm inactive nil nil extra))
16163 (looking-at org-ts-regexp3)
16165 ;; `day' category ends before `hour' if any, or at
16166 ;; the end of the day name.
16167 ((eq origin-cat 'day)
16168 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16169 ((eq origin-cat 'hour) (min (match-end 7) origin))
16170 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16171 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16172 ;; `year' and `month' have both fixed size: point
16173 ;; couldn't have moved into another part.
16175 ;; Update clock if on a CLOCK line.
16176 (org-clock-update-time-maybe)
16177 ;; Try to recenter the calendar window, if any.
16178 (if (and org-calendar-follow-timestamp-change
16179 (get-buffer-window "*Calendar*" t)
16180 (memq org-ts-what '(day month year)))
16181 (org-recenter-calendar (time-to-days time))))))
16183 (defun org-modify-ts-extra (s pos n dm)
16184 "Change the different parts of the lead-time and repeat fields in timestamp."
16185 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16187 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16189 ((or (org-pos-in-match-range pos 2)
16190 (org-pos-in-match-range pos 3))
16191 (setq m (string-to-number (match-string 3 s))
16192 h (string-to-number (match-string 2 s)))
16193 (if (org-pos-in-match-range pos 2)
16195 (setq n (* dm (org-no-warnings (signum n))))
16196 (when (not (= 0 (setq rem (% m dm))))
16197 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16199 (if (< m 0) (setq m (+ m 60) h (1- h)))
16200 (if (> m 59) (setq m (- m 60) h (1+ h)))
16201 (setq h (min 24 (max 0 h)))
16202 (setq ng 1 new (format "-%02d:%02d" h m)))
16203 ((org-pos-in-match-range pos 6)
16204 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16205 ((org-pos-in-match-range pos 5)
16206 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16208 ((org-pos-in-match-range pos 9)
16209 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16210 ((org-pos-in-match-range pos 8)
16211 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16215 (substring s 0 (match-beginning ng))
16217 (substring s (match-end ng))))))
16220 (defun org-recenter-calendar (date)
16221 "If the calendar is visible, recenter it to DATE."
16222 (let ((cwin (get-buffer-window "*Calendar*" t)))
16224 (let ((calendar-move-hook nil))
16225 (with-selected-window cwin
16226 (calendar-goto-date (if (listp date) date
16227 (calendar-gregorian-from-absolute date))))))))
16229 (defun org-goto-calendar (&optional arg)
16230 "Go to the Emacs calendar at the current date.
16231 If there is a time stamp in the current line, go to that date.
16232 A prefix ARG can be used to force the current date."
16234 (let ((tsr org-ts-regexp) diff
16235 (calendar-move-hook nil)
16236 (calendar-view-holidays-initially-flag nil)
16237 (calendar-view-diary-initially-flag nil))
16238 (if (or (org-at-timestamp-p)
16240 (beginning-of-line 1)
16241 (looking-at (concat ".*" tsr))))
16242 (let ((d1 (time-to-days (current-time)))
16244 (org-time-string-to-time (match-string 1)))))
16245 (setq diff (- d2 d1))))
16247 (calendar-goto-today)
16248 (if (and diff (not arg)) (calendar-forward-day diff))))
16250 (defun org-get-date-from-calendar ()
16251 "Return a list (month day year) of date at point in calendar."
16252 (with-current-buffer "*Calendar*"
16254 (calendar-cursor-to-date))))
16256 (defun org-date-from-calendar ()
16257 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16258 If there is already a time stamp at the cursor position, update it."
16260 (if (org-at-timestamp-p t)
16261 (org-timestamp-change 0 'calendar)
16262 (let ((cal-date (org-get-date-from-calendar)))
16263 (org-insert-time-stamp
16264 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16266 (defun org-minutes-to-hh:mm-string (m)
16267 "Compute H:MM from a number of minutes."
16268 (let ((h (/ m 60)))
16269 (setq m (- m (* 60 h)))
16270 (format org-time-clocksum-format h m)))
16272 (defun org-hh:mm-string-to-minutes (s)
16273 "Convert a string H:MM to a number of minutes.
16274 If the string is just a number, interpret it as minutes.
16275 In fact, the first hh:mm or number in the string will be taken,
16276 there can be extra stuff in the string.
16277 If no number is found, the return value is 0."
16280 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16281 (+ (* (string-to-number (match-string 1 s)) 60)
16282 (string-to-number (match-string 2 s))))
16283 ((string-match "\\([0-9]+\\)" s)
16284 (string-to-number (match-string 1 s)))
16287 (defcustom org-effort-durations
16290 ("w" . ,(* 60 8 5))
16291 ("m" . ,(* 60 8 5 4))
16292 ("y" . ,(* 60 8 5 40)))
16293 "Conversion factor to minutes for an effort modifier.
16295 Each entry has the form (MODIFIER . MINUTES).
16297 In an effort string, a number followed by MODIFIER is multiplied
16298 by the specified number of MINUTES to obtain an effort in
16301 For example, if the value of this variable is ((\"hours\" . 60)), then an
16302 effort string \"2hours\" is equivalent to 120 minutes."
16305 :type '(alist :key-type (string :tag "Modifier")
16306 :value-type (number :tag "Minutes")))
16308 (defun org-duration-string-to-minutes (s)
16309 "Convert a duration string S to minutes.
16311 A bare number is interpreted as minutes, modifiers can be set by
16312 customizing `org-effort-durations' (which see).
16314 Entries containing a colon are interpreted as H:MM by
16315 `org-hh:mm-string-to-minutes'."
16317 (re (concat "\\([0-9]+\\) *\\("
16318 (regexp-opt (mapcar 'car org-effort-durations))
16320 (while (string-match re s)
16321 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16322 (string-to-number (match-string 1 s))))
16323 (setq s (replace-match "" nil t s)))
16324 (incf result (org-hh:mm-string-to-minutes s))
16329 (defun org-save-all-org-buffers ()
16330 "Save all Org-mode buffers without user confirmation."
16332 (message "Saving all Org-mode buffers...")
16333 (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
16334 (when (featurep 'org-id) (org-id-locations-save))
16335 (message "Saving all Org-mode buffers... done"))
16337 (defun org-revert-all-org-buffers ()
16338 "Revert all Org-mode buffers.
16339 Prompt for confirmation when there are unsaved changes.
16340 Be sure you know what you are doing before letting this function
16341 overwrite your changes.
16343 This function is useful in a setup where one tracks org files
16344 with a version control system, to revert on one machine after pulling
16345 changes from another. I believe the procedure must be like this:
16347 1. M-x org-save-all-org-buffers
16348 2. Pull changes from the other machine, resolve conflicts
16349 3. M-x org-revert-all-org-buffers"
16351 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16354 (save-window-excursion
16357 (when (and (with-current-buffer b (eq major-mode 'org-mode))
16358 (with-current-buffer b buffer-file-name))
16359 (org-pop-to-buffer-same-window b)
16360 (revert-buffer t 'no-confirm)))
16362 (when (and (featurep 'org-id) org-id-track-globally)
16363 (org-id-locations-load)))))
16368 (defun org-switchb (&optional arg)
16369 "Switch between Org buffers.
16370 With one prefix argument, restrict available buffers to files.
16371 With two prefix arguments, restrict available buffers to agenda files.
16373 Defaults to `iswitchb' for buffer name completion.
16374 Set `org-completion-use-ido' to make it use ido instead."
16376 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16377 ((equal arg '(16)) (org-buffer-list 'agenda))
16378 (t (org-buffer-list))))
16379 (org-completion-use-iswitchb org-completion-use-iswitchb)
16380 (org-completion-use-ido org-completion-use-ido))
16381 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16382 (setq org-completion-use-iswitchb t))
16383 (org-pop-to-buffer-same-window
16384 (org-icompleting-read "Org buffer: "
16385 (mapcar 'list (mapcar 'buffer-name blist))
16388 ;;; Define some older names previously used for this functionality
16390 (defalias 'org-ido-switchb 'org-switchb)
16392 (defalias 'org-iswitchb 'org-switchb)
16394 (defun org-buffer-list (&optional predicate exclude-tmp)
16395 "Return a list of Org buffers.
16396 PREDICATE can be `export', `files' or `agenda'.
16398 export restrict the list to Export buffers.
16399 files restrict the list to buffers visiting Org files.
16400 agenda restrict the list to buffers visiting agenda files.
16402 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16404 (agenda-files (and (eq predicate 'agenda)
16405 (mapcar 'file-truename (org-agenda-files t))))
16408 ((eq predicate 'files)
16409 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
16410 ((eq predicate 'export)
16411 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16412 ((eq predicate 'agenda)
16414 (with-current-buffer b
16415 (and (eq major-mode 'org-mode)
16416 (setq bfn (buffer-file-name b))
16417 (member (file-truename bfn) agenda-files)))))
16418 (t (lambda (b) (with-current-buffer b
16419 (or (eq major-mode 'org-mode)
16420 (string-match "\*Org .*Export"
16421 (buffer-name b)))))))))
16425 (if (and (funcall filter b)
16426 (or (not exclude-tmp)
16427 (not (string-match "tmp" (buffer-name b)))))
16432 (defun org-agenda-files (&optional unrestricted archives)
16433 "Get the list of agenda files.
16434 Optional UNRESTRICTED means return the full list even if a restriction
16435 is currently in place.
16436 When ARCHIVES is t, include all archive files that are really being
16437 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16438 `org-agenda-archives-mode' is t."
16441 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16442 ((stringp org-agenda-files) (org-read-agenda-file-list))
16443 ((listp org-agenda-files) org-agenda-files)
16444 (t (error "Invalid value of `org-agenda-files'")))))
16445 (setq files (apply 'append
16446 (mapcar (lambda (f)
16447 (if (file-directory-p f)
16449 f t org-agenda-file-regexp)
16452 (when org-agenda-skip-unavailable-files
16453 (setq files (delq nil
16456 (and (file-readable-p file) file)))
16458 (when (or (eq archives t)
16459 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16460 (setq files (org-add-archive-files files)))
16463 (defun org-agenda-file-p (&optional file)
16464 "Return non-nil, if FILE is an agenda file.
16465 If FILE is omitted, use the file associated with the current
16467 (member (or file (buffer-file-name))
16468 (org-agenda-files t)))
16470 (defun org-edit-agenda-file-list ()
16471 "Edit the list of agenda files.
16472 Depending on setup, this either uses customize to edit the variable
16473 `org-agenda-files', or it visits the file that is holding the list. In the
16474 latter case, the buffer is set up in a way that saving it automatically kills
16475 the buffer and restores the previous window configuration."
16477 (if (stringp org-agenda-files)
16478 (let ((cw (current-window-configuration)))
16479 (find-file org-agenda-files)
16480 (org-set-local 'org-window-configuration cw)
16481 (org-add-hook 'after-save-hook
16483 (set-window-configuration
16484 (prog1 org-window-configuration
16485 (kill-buffer (current-buffer))))
16486 (org-install-agenda-files-menu)
16487 (message "New agenda file list installed"))
16489 (message "%s" (substitute-command-keys
16490 "Edit list and finish with \\[save-buffer]")))
16491 (customize-variable 'org-agenda-files)))
16493 (defun org-store-new-agenda-file-list (list)
16494 "Set new value for the agenda file list and save it correctly."
16495 (if (stringp org-agenda-files)
16496 (let ((fe (org-read-agenda-file-list t)) b u)
16497 (while (setq b (find-buffer-visiting org-agenda-files))
16499 (with-temp-file org-agenda-files
16502 (lambda (f) ;; Keep un-expanded entries.
16503 (if (setq u (assoc f fe))
16508 (let ((org-mode-hook nil) (org-inhibit-startup t)
16509 (org-insert-mode-line-in-empty-file nil))
16510 (setq org-agenda-files list)
16511 (customize-save-variable 'org-agenda-files org-agenda-files))))
16513 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16514 "Read the list of agenda files from a file.
16515 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16516 filenames, used by `org-store-new-agenda-file-list' to write back
16517 un-expanded file names."
16518 (when (file-directory-p org-agenda-files)
16519 (error "`org-agenda-files' cannot be a single directory"))
16520 (when (stringp org-agenda-files)
16522 (insert-file-contents org-agenda-files)
16525 (let ((e (expand-file-name (substitute-in-file-name f)
16527 (if pair-with-expansion
16530 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16533 (defun org-cycle-agenda-files ()
16534 "Cycle through the files in `org-agenda-files'.
16535 If the current buffer visits an agenda file, find the next one in the list.
16536 If the current buffer does not, find the first agenda file."
16538 (let* ((fs (org-agenda-files t))
16539 (files (append fs (list (car fs))))
16540 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16542 (unless files (error "No agenda files"))
16544 (while (setq file (pop files))
16545 (if (equal (file-truename file) tcf)
16547 (find-file (car files))
16549 (find-file (car fs)))
16550 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16552 (defun org-agenda-file-to-front (&optional to-end)
16553 "Move/add the current file to the top of the agenda file list.
16554 If the file is not present in the list, it is added to the front. If it is
16555 present, it is moved there. With optional argument TO-END, add/move to the
16558 (let ((org-agenda-skip-unavailable-files nil)
16559 (file-alist (mapcar (lambda (x)
16560 (cons (file-truename x) x))
16561 (org-agenda-files t)))
16562 (ctf (file-truename buffer-file-name))
16564 (setq x (assoc ctf file-alist) had x)
16566 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16568 (setq file-alist (append (delq x file-alist) (list x)))
16569 (setq file-alist (cons x (delq x file-alist))))
16570 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16571 (org-install-agenda-files-menu)
16572 (message "File %s to %s of agenda file list"
16573 (if had "moved" "added") (if to-end "end" "front"))))
16575 (defun org-remove-file (&optional file)
16576 "Remove current file from the list of files in variable `org-agenda-files'.
16577 These are the files which are being checked for agenda entries.
16578 Optional argument FILE means use this file instead of the current."
16580 (let* ((org-agenda-skip-unavailable-files nil)
16581 (file (or file buffer-file-name))
16582 (true-file (file-truename file))
16583 (afile (abbreviate-file-name file))
16584 (files (delq nil (mapcar
16586 (if (equal true-file
16589 (org-agenda-files t)))))
16590 (if (not (= (length files) (length (org-agenda-files t))))
16592 (org-store-new-agenda-file-list files)
16593 (org-install-agenda-files-menu)
16594 (message "Removed file: %s" afile))
16595 (message "File was not in list: %s (not removed)" afile))))
16597 (defun org-file-menu-entry (file)
16598 (vector file (list 'find-file file) t))
16600 (defun org-check-agenda-file (file)
16601 "Make sure FILE exists. If not, ask user what to do."
16602 (when (not (file-exists-p file))
16603 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
16604 (abbreviate-file-name file))
16605 (let ((r (downcase (read-char-exclusive))))
16608 (org-remove-file file)
16609 (throw 'nextfile t))
16610 (t (error "Abort"))))))
16612 (defun org-get-agenda-file-buffer (file)
16613 "Get a buffer visiting FILE. If the buffer needs to be created, add
16614 it to the list of buffers which might be released later."
16615 (let ((buf (org-find-base-buffer-visiting file)))
16617 buf ; just return it
16618 ;; Make a new buffer and remember it
16619 (setq buf (find-file-noselect file))
16620 (if buf (push buf org-agenda-new-buffers))
16623 (defun org-release-buffers (blist)
16624 "Release all buffers in list, asking the user for confirmation when needed.
16625 When a buffer is unmodified, it is just killed. When modified, it is saved
16626 \(if the user agrees) and then killed."
16628 (while (setq buf (pop blist))
16629 (setq file (buffer-file-name buf))
16630 (when (and (buffer-modified-p buf)
16632 (y-or-n-p (format "Save file %s? " file)))
16633 (with-current-buffer buf (save-buffer)))
16634 (kill-buffer buf))))
16636 (defun org-prepare-agenda-buffers (files)
16637 "Create buffers for all agenda files, protect archived trees and comments."
16639 (let ((pa '(:org-archived t))
16640 (pc '(:org-comment t))
16641 (pall '(:org-archived t :org-comment t))
16642 (inhibit-read-only t)
16643 (rea (concat ":" org-archive-tag ":"))
16647 (while (setq file (pop files))
16651 (org-check-agenda-file file)
16652 (set-buffer (org-get-agenda-file-buffer file)))
16654 (setq bmp (buffer-modified-p))
16655 (org-refresh-category-properties)
16656 (setq org-todo-keywords-for-agenda
16657 (append org-todo-keywords-for-agenda org-todo-keywords-1))
16658 (setq org-done-keywords-for-agenda
16659 (append org-done-keywords-for-agenda org-done-keywords))
16660 (setq org-todo-keyword-alist-for-agenda
16661 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
16662 (setq org-drawers-for-agenda
16663 (append org-drawers-for-agenda org-drawers))
16664 (setq org-tag-alist-for-agenda
16665 (append org-tag-alist-for-agenda org-tag-alist))
16668 (remove-text-properties (point-min) (point-max) pall)
16669 (when org-agenda-skip-archived-trees
16670 (goto-char (point-min))
16671 (while (re-search-forward rea nil t)
16672 (if (org-at-heading-p t)
16673 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
16674 (goto-char (point-min))
16675 (setq re (format org-heading-keyword-regexp-format
16676 org-comment-string))
16677 (while (re-search-forward re nil t)
16678 (add-text-properties
16679 (match-beginning 0) (org-end-of-subtree t) pc)))
16680 (set-buffer-modified-p bmp)))))
16681 (setq org-todo-keywords-for-agenda
16682 (org-uniquify org-todo-keywords-for-agenda))
16683 (setq org-todo-keyword-alist-for-agenda
16684 (org-uniquify org-todo-keyword-alist-for-agenda)
16685 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
16687 ;;;; Embedded LaTeX
16689 (defvar org-cdlatex-mode-map (make-sparse-keymap)
16690 "Keymap for the minor `org-cdlatex-mode'.")
16692 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
16693 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
16694 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
16695 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
16696 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
16698 (defvar org-cdlatex-texmathp-advice-is-done nil
16699 "Flag remembering if we have applied the advice to texmathp already.")
16701 (define-minor-mode org-cdlatex-mode
16702 "Toggle the minor `org-cdlatex-mode'.
16703 This mode supports entering LaTeX environment and math in LaTeX fragments
16705 \\{org-cdlatex-mode-map}"
16707 (when org-cdlatex-mode
16709 (run-hooks 'cdlatex-mode-hook)
16710 (cdlatex-compute-tables))
16711 (unless org-cdlatex-texmathp-advice-is-done
16712 (setq org-cdlatex-texmathp-advice-is-done t)
16713 (defadvice texmathp (around org-math-always-on activate)
16714 "Always return t in org-mode buffers.
16715 This is because we want to insert math symbols without dollars even outside
16716 the LaTeX math segments. If Orgmode thinks that point is actually inside
16717 an embedded LaTeX fragment, let texmathp do its job.
16718 \\[org-cdlatex-mode-map]"
16722 ((not (eq major-mode 'org-mode)) ad-do-it)
16723 ((eq this-command 'cdlatex-math-symbol)
16724 (setq ad-return-value t
16725 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
16727 (let ((p (org-inside-LaTeX-fragment-p)))
16728 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
16729 (setq ad-return-value t
16730 texmathp-why '("Org-mode embedded math" . 0))
16731 (if p ad-do-it)))))))))
16733 (defun turn-on-org-cdlatex ()
16734 "Unconditionally turn on `org-cdlatex-mode'."
16735 (org-cdlatex-mode 1))
16737 (defun org-inside-LaTeX-fragment-p ()
16738 "Test if point is inside a LaTeX fragment.
16739 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
16740 sequence appearing also before point.
16741 Even though the matchers for math are configurable, this function assumes
16742 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
16743 delimiters are skipped when they have been removed by customization.
16744 The return value is nil, or a cons cell with the delimiter and the
16745 position of this delimiter.
16747 This function does a reasonably good job, but can locally be fooled by
16748 for example currency specifications. For example it will assume being in
16749 inline math after \"$22.34\". The LaTeX fragment formatter will only format
16750 fragments that are properly closed, but during editing, we have to live
16751 with the uncertainty caused by missing closing delimiters. This function
16752 looks only before point, not after."
16754 (let ((pos (point))
16755 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
16757 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
16759 dd-on str (start 0) m re)
16762 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
16763 re (nth 1 (assoc "$" org-latex-regexps)))
16764 (while (string-match re str start)
16766 ((= (match-end 0) (length str))
16767 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
16768 ((= (match-end 0) (- (length str) 5))
16770 (t (setq start (match-end 0))))))
16771 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
16773 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
16774 (and (match-beginning 2) (throw 'exit nil))
16776 (while (re-search-backward "\\$\\$" lim t)
16777 (setq dd-on (not dd-on)))
16779 (if dd-on (cons "$$" m))))))
16781 (defun org-inside-latex-macro-p ()
16782 "Is point inside a LaTeX macro or its arguments?"
16785 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
16787 (defun org-try-cdlatex-tab ()
16788 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
16789 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
16790 - inside a LaTeX fragment, or
16791 - after the first word in a line, where an abbreviation expansion could
16792 insert a LaTeX environment."
16793 (when org-cdlatex-mode
16795 ;; Before any word on the line: No expansion possible.
16796 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
16797 ;; Just after first word on the line: Expand it. Make sure it
16798 ;; cannot happen on headlines, though.
16800 (skip-chars-backward "a-zA-Z0-9*")
16801 (skip-chars-backward " \t")
16802 (and (bolp) (not (org-at-heading-p))))
16804 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
16806 (defun org-cdlatex-underscore-caret (&optional arg)
16807 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
16808 Revert to the normal definition outside of these fragments."
16810 (if (org-inside-LaTeX-fragment-p)
16811 (call-interactively 'cdlatex-sub-superscript)
16812 (let (org-cdlatex-mode)
16813 (call-interactively (key-binding (vector last-input-event))))))
16815 (defun org-cdlatex-math-modify (&optional arg)
16816 "Execute `cdlatex-math-modify' in LaTeX fragments.
16817 Revert to the normal definition outside of these fragments."
16819 (if (org-inside-LaTeX-fragment-p)
16820 (call-interactively 'cdlatex-math-modify)
16821 (let (org-cdlatex-mode)
16822 (call-interactively (key-binding (vector last-input-event))))))
16824 (defvar org-latex-fragment-image-overlays nil
16825 "List of overlays carrying the images of latex fragments.")
16826 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
16828 (defun org-remove-latex-fragment-image-overlays ()
16829 "Remove all overlays with LaTeX fragment images in current buffer."
16830 (mapc 'delete-overlay org-latex-fragment-image-overlays)
16831 (setq org-latex-fragment-image-overlays nil))
16833 (defun org-preview-latex-fragment (&optional subtree)
16834 "Preview the LaTeX fragment at point, or all locally or globally.
16835 If the cursor is in a LaTeX fragment, create the image and overlay
16836 it over the source code. If there is no fragment at point, display
16837 all fragments in the current text, from one headline to the next. With
16838 prefix SUBTREE, display all fragments in the current subtree. With a
16839 double prefix arg \\[universal-argument] \\[universal-argument], or when \
16840 the cursor is before the first headline,
16841 display all fragments in the buffer.
16842 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
16844 (unless buffer-file-name
16845 (error "Can't preview LaTeX fragment in a non-file buffer"))
16846 (org-remove-latex-fragment-image-overlays)
16849 (let (beg end at msg)
16851 ((or (equal subtree '(16))
16852 (not (save-excursion
16853 (re-search-backward org-outline-regexp-bol nil t))))
16854 (setq beg (point-min) end (point-max)
16855 msg "Creating images for buffer...%s"))
16856 ((equal subtree '(4))
16857 (org-back-to-heading)
16858 (setq beg (point) end (org-end-of-subtree t)
16859 msg "Creating images for subtree...%s"))
16861 (if (setq at (org-inside-LaTeX-fragment-p))
16862 (goto-char (max (point-min) (- (cdr at) 2)))
16863 (org-back-to-heading))
16864 (setq beg (point) end (progn (outline-next-heading) (point))
16865 msg (if at "Creating image...%s"
16866 "Creating images for entry...%s"))))
16868 (narrow-to-region beg end)
16871 (concat "ltxpng/" (file-name-sans-extension
16872 (file-name-nondirectory
16873 buffer-file-name)))
16874 default-directory 'overlays msg at 'forbuffer 'dvipng)
16875 (message msg "done. Use `C-c C-c' to remove images.")))))
16877 (defvar org-latex-regexps
16878 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
16879 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
16880 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
16881 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
16882 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
16883 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
16884 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
16885 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
16886 "Regular expressions for matching embedded LaTeX.")
16888 (defvar org-export-have-math nil) ;; dynamic scoping
16889 (defun org-format-latex (prefix &optional dir overlays msg at
16890 forbuffer processing-type)
16891 "Replace LaTeX fragments with links to an image, and produce images.
16892 Some of the options can be changed using the variable
16893 `org-format-latex-options'."
16894 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
16895 (let* ((prefixnodir (file-name-nondirectory prefix))
16896 (absprefix (expand-file-name prefix dir))
16897 (todir (file-name-directory absprefix))
16898 (opt org-format-latex-options)
16899 (matchers (plist-get opt :matchers))
16900 (re-list org-latex-regexps)
16901 (org-format-latex-header-extra
16902 (plist-get (org-infile-export-plist) :latex-header-extra))
16903 (cnt 0) txt hash link beg end re e checkdir
16904 executables-checked string
16905 m n block-type block linkfile movefile ov)
16906 ;; Check the different regular expressions
16907 (while (setq e (pop re-list))
16908 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
16909 block (if block-type "\n\n" ""))
16910 (when (member m matchers)
16911 (goto-char (point-min))
16912 (while (re-search-forward re nil t)
16913 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
16914 (not (get-text-property (match-beginning n)
16917 (not (eq (get-char-property (match-beginning n)
16919 'org-latex-overlay))))
16920 (setq org-export-have-math t)
16922 ((eq processing-type 'verbatim)
16923 ;; Leave the text verbatim, just protect it
16924 (add-text-properties (match-beginning n) (match-end n)
16925 '(org-protected t)))
16926 ((eq processing-type 'mathjax)
16927 ;; Prepare for MathJax processing
16928 (setq string (match-string n))
16929 (if (member m '("$" "$1"))
16931 (delete-region (match-beginning n) (match-end n))
16932 (goto-char (match-beginning n))
16933 (insert (org-add-props (concat "\\(" (substring string 1 -1)
16935 '(org-protected t))))
16936 (add-text-properties (match-beginning n) (match-end n)
16937 '(org-protected t))))
16938 ((eq processing-type 'dvipng)
16939 ;; Process to an image
16940 (setq txt (match-string n)
16941 beg (match-beginning n) end (match-end n)
16943 (let (print-length print-level) ; make sure full list is printed
16944 (setq hash (sha1 (prin1-to-string
16945 (list org-format-latex-header
16946 org-format-latex-header-extra
16947 org-export-latex-default-packages-alist
16948 org-export-latex-packages-alist
16949 org-format-latex-options
16951 linkfile (format "%s_%s.png" prefix hash)
16952 movefile (format "%s_%s.png" absprefix hash)))
16953 (setq link (concat block "[[file:" linkfile "]]" block))
16954 (if msg (message msg cnt))
16956 (unless checkdir ; make sure the directory exists
16958 (or (file-directory-p todir) (make-directory todir t)))
16960 (unless executables-checked
16961 (org-check-external-command
16962 "latex" "needed to convert LaTeX fragments to images")
16963 (org-check-external-command
16964 "dvipng" "needed to convert LaTeX fragments to images")
16965 (setq executables-checked t))
16967 (unless (file-exists-p movefile)
16968 (org-create-formula-image
16969 txt movefile opt forbuffer))
16973 (if (eq (overlay-get o 'org-overlay-type)
16974 'org-latex-overlay)
16975 (delete-overlay o)))
16976 (overlays-in beg end))
16977 (setq ov (make-overlay beg end))
16978 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
16979 (if (featurep 'xemacs)
16981 (overlay-put ov 'invisible t)
16984 (make-glyph (vector 'png :file movefile))))
16987 (list 'image :type 'png :file movefile :ascent 'center)))
16988 (push ov org-latex-fragment-image-overlays)
16990 (delete-region beg end)
16991 (insert (org-add-props link
16992 (list 'org-latex-src
16993 (replace-regexp-in-string
16995 'org-latex-src-embed-type
16996 (if block-type 'paragraph 'character))))))
16997 ((eq processing-type 'mathml)
16998 ;; Process to MathML
16999 (unless executables-checked
17000 (unless (save-match-data (org-format-latex-mathml-available-p))
17001 (error "LaTeX to MathML converter not configured"))
17002 (setq executables-checked t))
17003 (setq txt (match-string n)
17004 beg (match-beginning n) end (match-end n)
17006 (if msg (message msg cnt))
17008 (delete-region beg end)
17009 (insert (org-format-latex-as-mathml
17010 txt block-type prefix dir)))
17012 (error "Unknown conversion type %s for latex fragments"
17013 processing-type)))))))))
17015 (declare-function format-spec "format-spec" (format specification))
17017 (defun org-create-math-formula (latex-frag &optional mathml-file)
17018 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17019 Use `org-latex-to-mathml-convert-command'. If the conversion is
17020 sucessful, return the portion between \"<math...> </math>\"
17021 elements otherwise return nil. When MATHML-FILE is specified,
17022 write the results in to that file. When invoked as an
17023 interactive command, prompt for LATEX-FRAG, with initial value
17024 set to the current active region and echo the results for user
17026 (interactive (list (let ((frag (when (region-active-p)
17027 (buffer-substring-no-properties
17028 (region-beginning) (region-end)))))
17029 (read-string "LaTeX Fragment: " frag nil frag))))
17030 (unless latex-frag (error "Invalid latex-frag"))
17031 (let* ((tmp-in-file (file-relative-name
17032 (make-temp-name (expand-file-name "ltxmathml-in"))))
17033 (ignore (write-region latex-frag nil tmp-in-file))
17034 (tmp-out-file (file-relative-name
17035 (make-temp-name (expand-file-name "ltxmathml-out"))))
17037 org-latex-to-mathml-convert-command
17038 `((?j . ,(shell-quote-argument
17039 (expand-file-name org-latex-to-mathml-jar-file)))
17040 (?I . ,(shell-quote-argument tmp-in-file))
17041 (?o . ,(shell-quote-argument tmp-out-file)))))
17042 mathml shell-command-output)
17043 (when (org-called-interactively-p 'any)
17044 (unless (org-format-latex-mathml-available-p)
17045 (error "LaTeX to MathML converter not configured")))
17046 (message "Running %s" cmd)
17047 (setq shell-command-output (shell-command-to-string cmd))
17049 (when (file-readable-p tmp-out-file)
17050 (with-current-buffer (find-file-noselect tmp-out-file t)
17051 (goto-char (point-min))
17052 (when (re-search-forward
17055 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17057 (regexp-quote "</math>")) nil t)
17058 (prog1 (match-string 0) (kill-buffer))))))
17062 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17064 (write-region mathml nil mathml-file))
17065 (when (org-called-interactively-p 'any)
17067 ((message "LaTeX to MathML conversion failed")
17068 (message shell-command-output)))
17069 (delete-file tmp-in-file)
17070 (when (file-exists-p tmp-out-file)
17071 (delete-file tmp-out-file))
17074 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17075 prefix &optional dir)
17076 "Use `org-create-math-formula' but check local cache first."
17077 (let* ((absprefix (expand-file-name prefix dir))
17078 (print-length nil) (print-level nil)
17079 (formula-id (concat
17084 org-latex-to-mathml-convert-command)))))
17085 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17086 (formula-cache-dir (file-name-directory formula-cache)))
17088 (unless (file-directory-p formula-cache-dir)
17089 (make-directory formula-cache-dir t))
17091 (unless (file-exists-p formula-cache)
17092 (org-create-math-formula latex-frag formula-cache))
17094 (if (file-exists-p formula-cache)
17095 ;; Successful conversion. Return the link to MathML file.
17097 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17098 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17099 'org-latex-src-embed-type (if latex-frag-type
17100 'paragraph 'character)))
17101 ;; Failed conversion. Return the LaTeX fragment verbatim
17102 (add-text-properties
17103 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17106 ;; This function borrows from Ganesh Swami's latex2png.el
17107 (defun org-create-formula-image (string tofile options buffer)
17108 "This calls dvipng."
17109 (require 'org-latex)
17110 (let* ((tmpdir (if (featurep 'xemacs)
17112 temporary-file-directory))
17113 (texfilebase (make-temp-name
17114 (expand-file-name "orgtex" tmpdir)))
17115 (texfile (concat texfilebase ".tex"))
17116 (dvifile (concat texfilebase ".dvi"))
17117 (pngfile (concat texfilebase ".png"))
17118 (fnh (if (featurep 'xemacs)
17119 (font-height (face-font 'default))
17120 (face-attribute 'default :height nil)))
17121 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17122 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17123 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17125 (bg (or (plist-get options (if buffer :background :html-background))
17127 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17128 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17129 (with-temp-file texfile
17130 (insert (org-splice-latex-header
17131 org-format-latex-header
17132 org-export-latex-default-packages-alist
17133 org-export-latex-packages-alist t
17134 org-format-latex-header-extra))
17135 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17136 (require 'org-latex)
17137 (org-export-latex-fix-inputenc))
17138 (let ((dir default-directory))
17139 (condition-case nil
17142 (call-process "latex" nil nil nil texfile))
17145 (if (not (file-exists-p dvifile))
17146 (progn (message "Failed to create dvi file from %s" texfile) nil)
17147 (condition-case nil
17148 (if (featurep 'xemacs)
17149 (call-process "dvipng" nil nil nil
17154 (call-process "dvipng" nil nil nil
17157 ;;"-x" scale "-y" scale
17162 (if (not (file-exists-p pngfile))
17163 (if org-format-latex-signal-error
17164 (error "Failed to create png file from %s" texfile)
17165 (message "Failed to create png file from %s" texfile)
17167 ;; Use the requested file name and clean up
17168 (copy-file pngfile tofile 'replace)
17169 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
17170 (delete-file (concat texfilebase e)))
17173 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17174 "Fill a LaTeX header template TPL.
17175 In the template, the following place holders will be recognized:
17177 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17178 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17179 [PACKAGES] \\usepackage statements for PKG
17180 [NO-PACKAGES] do not include PKG
17181 [EXTRA] the string EXTRA
17182 [NO-EXTRA] do not include EXTRA
17184 For backward compatibility, if both the positive and the negative place
17185 holder is missing, the positive one (without the \"NO-\") will be
17186 assumed to be present at the end of the template.
17187 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17189 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17190 (let (rpl (end ""))
17191 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17192 (setq rpl (if (or (match-end 1) (not def-pkg))
17193 "" (org-latex-packages-to-string def-pkg snippets-p t))
17194 tpl (replace-match rpl t t tpl))
17195 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17197 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17198 (setq rpl (if (or (match-end 1) (not pkg))
17199 "" (org-latex-packages-to-string pkg snippets-p t))
17200 tpl (replace-match rpl t t tpl))
17203 (org-latex-packages-to-string pkg snippets-p)))))
17205 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17206 (setq rpl (if (or (match-end 1) (not extra))
17207 "" (concat extra "\n"))
17208 tpl (replace-match rpl t t tpl))
17209 (if (and extra (string-match "\\S-" extra))
17210 (setq end (concat end "\n" extra))))
17212 (if (string-match "\\S-" end)
17213 (concat tpl "\n" end)
17216 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17217 "Turn an alist of packages into a string with the \\usepackage macros."
17218 (setq pkg (mapconcat (lambda(p)
17221 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17222 (format "%% Package %s omitted" (cadr p)))
17223 ((equal "" (car p))
17224 (format "\\usepackage{%s}" (cadr p)))
17226 (format "\\usepackage[%s]{%s}"
17227 (car p) (cadr p)))))
17230 (if newline (concat pkg "\n") pkg))
17232 (defun org-dvipng-color (attr)
17233 "Return an rgb color specification for dvipng."
17234 (apply 'format "rgb %s %s %s"
17235 (mapcar 'org-normalize-color
17236 (if (featurep 'xemacs)
17237 (color-rgb-components
17238 (face-property 'default
17239 (cond ((eq attr :foreground) 'foreground)
17240 ((eq attr :background) 'background))))
17241 (color-values (face-attribute 'default attr nil))))))
17243 (defun org-normalize-color (value)
17244 "Return string to be used as color value for an RGB component."
17245 (format "%g" (/ value 65535.0)))
17250 (defvar org-inline-image-overlays nil)
17251 (make-variable-buffer-local 'org-inline-image-overlays)
17253 (defun org-toggle-inline-images (&optional include-linked)
17254 "Toggle the display of inline images.
17255 INCLUDE-LINKED is passed to `org-display-inline-images'."
17257 (if org-inline-image-overlays
17259 (org-remove-inline-images)
17260 (message "Inline image display turned off"))
17261 (org-display-inline-images include-linked)
17262 (if org-inline-image-overlays
17263 (message "%d images displayed inline"
17264 (length org-inline-image-overlays))
17265 (message "No images to display inline"))))
17267 (defun org-display-inline-images (&optional include-linked refresh beg end)
17268 "Display inline images.
17269 Normally only links without a description part are inlined, because this
17270 is how it will work for export. When INCLUDE-LINKED is set, also links
17271 with a description part will be inlined. This can be nice for a quick
17272 look at those images, but it does not reflect what exported files will look
17274 When REFRESH is set, refresh existing images between BEG and END.
17275 This will create new image displays only if necessary.
17276 BEG and END default to the buffer boundaries."
17279 (org-remove-inline-images)
17280 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17284 (setq beg (or beg (point-min)) end (or end (point-max)))
17285 (goto-char (point-min))
17286 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17287 (substring (org-image-file-name-regexp) 0 -2)
17288 "\\)\\]" (if include-linked "" "\\]")))
17290 (while (re-search-forward re end t)
17291 (setq old (get-char-property-and-overlay (match-beginning 1)
17292 'org-image-overlay))
17293 (setq file (expand-file-name
17294 (concat (or (match-string 3) "") (match-string 4))))
17295 (when (file-exists-p file)
17296 (if (and (car-safe old) refresh)
17297 (image-refresh (overlay-get (cdr old) 'display))
17298 (setq img (save-match-data (create-image file)))
17300 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17301 (overlay-put ov 'display img)
17302 (overlay-put ov 'face 'default)
17303 (overlay-put ov 'org-image-overlay t)
17304 (overlay-put ov 'modification-hooks
17305 (list 'org-display-inline-modification-hook))
17306 (push ov org-inline-image-overlays)))))))))
17308 (defun org-display-inline-modification-hook (ov after beg end &optional len)
17309 "Remove inline-display overlay if a corresponding region is modified."
17310 (let ((inhibit-modification-hooks t))
17311 (when (and ov after)
17312 (delete ov org-inline-image-overlays)
17313 (delete-overlay ov))))
17315 (defun org-remove-inline-images ()
17316 "Remove inline display of images."
17318 (mapc 'delete-overlay org-inline-image-overlays)
17319 (setq org-inline-image-overlays nil))
17323 ;; Outline functions from `outline-mode-prefix-map'
17324 ;; that can be remapped in Org:
17325 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17326 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17327 (define-key org-mode-map [remap outline-forward-same-level]
17328 'org-forward-same-level)
17329 (define-key org-mode-map [remap outline-backward-same-level]
17330 'org-backward-same-level)
17331 (define-key org-mode-map [remap show-branches]
17332 'org-kill-note-or-show-branches)
17333 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17334 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17335 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17337 ;; Outline functions from `outline-mode-prefix-map'
17338 ;; that can not be remapped in Org:
17339 ;; - the column "key binding" shows whether the Outline function is still
17340 ;; available in Org mode on the same key that it has been bound to in
17342 ;; - "overridden": key used for a different functionality in Org mode
17343 ;; - else: key still bound to the same Outline function in Org mode
17344 ;; | Outline function | key binding | Org replacement |
17345 ;; |------------------------------------+-------------+-----------------------|
17346 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17347 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17348 ;; | `show-children' | `C-c C-i' | visibility cycling |
17349 ;; | `hide-subtree' | overridden | visibility cycling |
17350 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17351 ;; | `hide-body' | overridden | no replacement |
17352 ;; | `show-all' | overridden | no replacement |
17353 ;; | `hide-entry' | overridden | visibility cycling |
17354 ;; | `show-entry' | overridden | no replacement |
17355 ;; | `hide-leaves' | overridden | no replacement |
17356 ;; | `hide-sublevels' | overridden | no replacement |
17357 ;; | `hide-other' | overridden | no replacement |
17358 ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
17359 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17361 ;; Make `C-c C-x' a prefix key
17362 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17364 ;; TAB key with modifiers
17365 (org-defkey org-mode-map "\C-i" 'org-cycle)
17366 (org-defkey org-mode-map [(tab)] 'org-cycle)
17367 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17368 (org-defkey org-mode-map [(meta tab)] 'pcomplete)
17369 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17370 (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
17371 ;; The following line is necessary under Suse GNU/Linux
17372 (unless (featurep 'xemacs)
17373 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17374 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17375 (define-key org-mode-map [backtab] 'org-shifttab)
17377 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17378 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17379 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17381 ;; Cursor keys with modifiers
17382 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17383 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17384 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17385 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17387 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17388 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17389 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17390 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17392 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17393 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17394 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17395 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17397 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17398 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17399 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17400 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17403 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17404 (mapc (lambda (pair)
17405 (define-key org-babel-map (car pair) (cdr pair)))
17406 org-babel-key-bindings)
17408 ;;; Extra keys for tty access.
17409 ;; We only set them when really needed because otherwise the
17410 ;; menus don't show the simple keys
17412 (when (or org-use-extra-keys
17413 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17414 (not window-system))
17415 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17416 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17417 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17418 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17419 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17420 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17421 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17422 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17423 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17424 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17425 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17426 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17427 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17428 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17429 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17430 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17431 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17432 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17433 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17434 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17435 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17436 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17437 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17438 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17439 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17440 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17441 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17442 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17444 ;; All the other keys
17446 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17447 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17448 (if (boundp 'narrow-map)
17449 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17450 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17451 (if (boundp 'narrow-map)
17452 (org-defkey narrow-map "b" 'org-narrow-to-block)
17453 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17454 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
17455 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
17456 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17457 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17458 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17459 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
17460 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17461 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17462 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17463 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17464 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17465 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17466 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
17467 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
17468 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
17469 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
17470 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
17471 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
17472 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
17473 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
17474 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
17475 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
17476 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
17477 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
17478 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
17479 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
17480 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
17481 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
17482 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
17483 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
17484 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
17485 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
17486 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
17487 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
17488 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
17489 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
17490 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
17491 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
17492 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
17493 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
17494 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
17495 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
17496 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17497 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
17498 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
17499 (org-defkey org-mode-map "\C-c^" 'org-sort)
17500 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
17501 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
17502 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
17503 (org-defkey org-mode-map "\C-m" 'org-return)
17504 (org-defkey org-mode-map "\C-j" 'org-return-indent)
17505 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
17506 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
17507 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
17508 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
17509 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
17510 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
17511 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
17512 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
17513 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
17514 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
17515 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
17516 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
17517 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
17518 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
17519 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
17520 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
17521 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
17522 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
17523 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
17524 (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
17525 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
17526 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
17528 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
17529 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
17530 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
17531 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
17533 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
17534 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
17535 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
17536 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
17537 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
17538 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
17539 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
17540 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
17541 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
17542 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
17543 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
17544 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
17545 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
17546 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
17547 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
17548 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
17549 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
17550 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
17552 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
17553 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
17554 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
17555 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
17556 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
17558 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
17560 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
17562 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
17563 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
17565 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
17568 (when (featurep 'xemacs)
17569 (org-defkey org-mode-map 'button3 'popup-mode-menu))
17572 (defconst org-speed-commands-default
17574 ("Outline Navigation")
17575 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
17576 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
17577 ("f" . (org-speed-move-safe 'org-forward-same-level))
17578 ("b" . (org-speed-move-safe 'org-backward-same-level))
17579 ("u" . (org-speed-move-safe 'outline-up-heading))
17581 ("g" . (org-refile t))
17582 ("Outline Visibility")
17584 ("C" . org-shifttab)
17585 (" " . org-display-outline-path)
17586 ("Outline Structure Editing")
17587 ("U" . org-shiftmetaup)
17588 ("D" . org-shiftmetadown)
17589 ("r" . org-metaright)
17590 ("l" . org-metaleft)
17591 ("R" . org-shiftmetaright)
17592 ("L" . org-shiftmetaleft)
17593 ("i" . (progn (forward-char 1) (call-interactively
17594 'org-insert-heading-respect-content)))
17597 ("a" . org-archive-subtree-default-with-confirmation)
17598 ("." . org-mark-subtree)
17600 ("I" . org-clock-in)
17601 ("O" . org-clock-out)
17602 ("Meta Data Editing")
17604 ("0" . (org-priority ?\ ))
17605 ("1" . (org-priority ?A))
17606 ("2" . (org-priority ?B))
17607 ("3" . (org-priority ?C))
17608 (";" . org-set-tags-command)
17609 ("e" . org-set-effort)
17610 ("Agenda Views etc")
17612 ("/" . org-sparse-tree)
17614 ("o" . org-open-at-point)
17615 ("?" . org-speed-command-help)
17616 ("<" . (org-agenda-set-restriction-lock 'subtree))
17617 (">" . (org-agenda-remove-restriction-lock))
17619 "The default speed commands.")
17621 (defun org-print-speed-command (e)
17622 (if (> (length (car e)) 1)
17627 (princ (make-string (length (car e)) ?-))
17631 (if (symbolp (cdr e))
17632 (princ (symbol-name (cdr e)))
17636 (defun org-speed-command-help ()
17637 "Show the available speed commands."
17639 (if (not org-use-speed-commands)
17640 (error "Speed commands are not activated, customize `org-use-speed-commands'")
17641 (with-output-to-temp-buffer "*Help*"
17642 (princ "User-defined Speed commands\n===========================\n")
17643 (mapc 'org-print-speed-command org-speed-commands-user)
17645 (princ "Built-in Speed commands\n=======================\n")
17646 (mapc 'org-print-speed-command org-speed-commands-default))
17647 (with-current-buffer "*Help*"
17648 (setq truncate-lines t))))
17650 (defun org-speed-move-safe (cmd)
17651 "Execute CMD, but make sure that the cursor always ends up in a headline.
17652 If not, return to the original position and throw an error."
17654 (let ((pos (point)))
17655 (call-interactively cmd)
17656 (unless (and (bolp) (org-at-heading-p))
17658 (error "Boundary reached while executing %s" cmd))))
17660 (defvar org-self-insert-command-undo-counter 0)
17662 (defvar org-table-auto-blank-field) ; defined in org-table.el
17663 (defvar org-speed-command nil)
17665 (defun org-speed-command-default-hook (keys)
17666 "Hook for activating single-letter speed commands.
17667 `org-speed-commands-default' specifies a minimal command set.
17668 Use `org-speed-commands-user' for further customization."
17669 (when (or (and (bolp) (looking-at org-outline-regexp))
17670 (and (functionp org-use-speed-commands)
17671 (funcall org-use-speed-commands)))
17672 (cdr (assoc keys (append org-speed-commands-user
17673 org-speed-commands-default)))))
17675 (defun org-babel-speed-command-hook (keys)
17676 "Hook for activating single-letter code block commands."
17677 (when (and (bolp) (looking-at org-babel-src-block-regexp))
17678 (cdr (assoc keys org-babel-key-bindings))))
17680 (defcustom org-speed-command-hook
17681 '(org-speed-command-default-hook org-babel-speed-command-hook)
17682 "Hook for activating speed commands at strategic locations.
17683 Hook functions are called in sequence until a valid handler is
17686 Each hook takes a single argument, a user-pressed command key
17687 which is also a `self-insert-command' from the global map.
17689 Within the hook, examine the cursor position and the command key
17690 and return nil or a valid handler as appropriate. Handler could
17691 be one of an interactive command, a function, or a form.
17693 Set `org-use-speed-commands' to non-nil value to enable this
17694 hook. The default setting is `org-speed-command-default-hook'."
17695 :group 'org-structure
17699 (defun org-self-insert-command (N)
17700 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
17701 If the cursor is in a table looking at whitespace, the whitespace is
17702 overwritten, and the table is not marked as requiring realignment."
17704 (org-check-before-invisible-edit 'insert)
17706 ((and org-use-speed-commands
17707 (setq org-speed-command
17708 (run-hook-with-args-until-success
17709 'org-speed-command-hook (this-command-keys))))
17711 ((commandp org-speed-command)
17712 (setq this-command org-speed-command)
17713 (call-interactively org-speed-command))
17714 ((functionp org-speed-command)
17715 (funcall org-speed-command))
17716 ((and org-speed-command (listp org-speed-command))
17717 (eval org-speed-command))
17718 (t (let (org-use-speed-commands)
17719 (call-interactively 'org-self-insert-command)))))
17723 ;; check if we blank the field, and if that triggers align
17724 (and (featurep 'org-table) org-table-auto-blank-field
17725 (member last-command
17726 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
17727 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
17728 ;; got extra space, this field does not determine column width
17729 (let (org-table-may-need-update) (org-table-blank-field))
17730 ;; no extra space, this field may determine column width
17731 (org-table-blank-field)))
17734 (looking-at "[^|\n]* |"))
17735 (let (org-table-may-need-update)
17736 (goto-char (1- (match-end 0)))
17738 (goto-char (match-beginning 0))
17739 (self-insert-command N)))
17741 (setq org-table-may-need-update t)
17742 (self-insert-command N)
17743 (org-fix-tags-on-the-fly)
17744 (if org-self-insert-cluster-for-undo
17745 (if (not (eq last-command 'org-self-insert-command))
17746 (setq org-self-insert-command-undo-counter 1)
17747 (if (>= org-self-insert-command-undo-counter 20)
17748 (setq org-self-insert-command-undo-counter 1)
17749 (and (> org-self-insert-command-undo-counter 0)
17750 buffer-undo-list (listp buffer-undo-list)
17751 (not (cadr buffer-undo-list)) ; remove nil entry
17752 (setcdr buffer-undo-list (cddr buffer-undo-list)))
17753 (setq org-self-insert-command-undo-counter
17754 (1+ org-self-insert-command-undo-counter))))))))
17756 (defun org-check-before-invisible-edit (kind)
17757 "Check is editing if kind KIND would be dangerous with invisible text around.
17758 The detailed reaction depends on the user option `org-catch-invisible-edits'."
17759 ;; First, try to get out of here as quickly as possible, to reduce overhead
17760 (if (and org-catch-invisible-edits
17761 (or (not (boundp 'visible-mode)) (not visible-mode))
17762 (or (get-char-property (point) 'invisible)
17763 (get-char-property (max (point-min) (1- (point))) 'invisible)))
17764 ;; OK, we need to take a closer look
17765 (let* ((invisible-at-point (get-char-property (point) 'invisible))
17766 (invisible-before-point (if (bobp) nil (get-char-property
17767 (1- (point)) 'invisible)))
17768 (border-and-ok-direction
17770 ;; Check if we are acting predictably before invisible text
17771 (and invisible-at-point (not invisible-before-point)
17772 (memq kind '(insert delete-backward)))
17773 ;; Check if we are acting predictably after invisible text
17774 ;; This works not well, and I have turned it off. It seems
17775 ;; better to always show and stop after invisible text.
17776 ;; (and (not invisible-at-point) invisible-before-point
17777 ;; (memq kind '(insert delete)))
17780 (when (or (memq invisible-at-point '(outline org-hide-block))
17781 (memq invisible-before-point '(outline org-hide-block)))
17782 (if (eq org-catch-invisible-edits 'error)
17783 (error "Editing in invisible areas is prohibited - make visible first"))
17784 ;; Make the area visible
17786 (if invisible-before-point
17787 (goto-char (previous-single-char-property-change
17788 (point) 'invisible)))
17791 ((eq org-catch-invisible-edits 'show)
17792 ;; That's it, we do the edit after showing
17794 "Unfolding invisible region around point before editing")
17796 ((and (eq org-catch-invisible-edits 'smart)
17797 border-and-ok-direction)
17798 (message "Unfolding invisible region around point before editing"))
17800 ;; Don't do the edit, make the user repeat it in full visibility
17801 (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
17803 (defun org-fix-tags-on-the-fly ()
17804 (when (and (equal (char-after (point-at-bol)) ?*)
17805 (org-at-heading-p))
17806 (org-align-tags-here org-tags-column)))
17808 (defun org-delete-backward-char (N)
17809 "Like `delete-backward-char', insert whitespace at field end in tables.
17810 When deleting backwards, in tables this function will insert whitespace in
17811 front of the next \"|\" separator, to keep the table aligned. The table will
17812 still be marked for re-alignment if the field did fill the entire column,
17813 because, in this case the deletion might narrow the column."
17815 (org-check-before-invisible-edit 'delete-backward)
17816 (if (and (org-table-p)
17818 (string-match "|" (buffer-substring (point-at-bol) (point)))
17819 (looking-at ".*?|"))
17820 (let ((pos (point))
17821 (noalign (looking-at "[^|\n\r]* |"))
17822 (c org-table-may-need-update))
17823 (backward-delete-char N)
17824 (if (not overwrite-mode)
17826 (skip-chars-forward "^|")
17828 (goto-char (1- pos))))
17829 ;; noalign: if there were two spaces at the end, this field
17830 ;; does not determine the width of the column.
17831 (if noalign (setq org-table-may-need-update c)))
17832 (backward-delete-char N)
17833 (org-fix-tags-on-the-fly)))
17835 (defun org-delete-char (N)
17836 "Like `delete-char', but insert whitespace at field end in tables.
17837 When deleting characters, in tables this function will insert whitespace in
17838 front of the next \"|\" separator, to keep the table aligned. The table will
17839 still be marked for re-alignment if the field did fill the entire column,
17840 because, in this case the deletion might narrow the column."
17842 (org-check-before-invisible-edit 'delete)
17843 (if (and (org-table-p)
17845 (not (= (char-after) ?|))
17847 (if (looking-at ".*?|")
17848 (let ((pos (point))
17849 (noalign (looking-at "[^|\n\r]* |"))
17850 (c org-table-may-need-update))
17851 (replace-match (concat
17852 (substring (match-string 0) 1 -1)
17855 ;; noalign: if there were two spaces at the end, this field
17856 ;; does not determine the width of the column.
17857 (if noalign (setq org-table-may-need-update c)))
17860 (org-fix-tags-on-the-fly)))
17862 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
17863 (put 'org-self-insert-command 'delete-selection t)
17864 (put 'orgtbl-self-insert-command 'delete-selection t)
17865 (put 'org-delete-char 'delete-selection 'supersede)
17866 (put 'org-delete-backward-char 'delete-selection 'supersede)
17867 (put 'org-yank 'delete-selection 'yank)
17869 ;; Make `flyspell-mode' delay after some commands
17870 (put 'org-self-insert-command 'flyspell-delayed t)
17871 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
17872 (put 'org-delete-char 'flyspell-delayed t)
17873 (put 'org-delete-backward-char 'flyspell-delayed t)
17875 ;; Make pabbrev-mode expand after org-mode commands
17876 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
17877 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
17879 ;; How to do this: Measure non-white length of current string
17880 ;; If equal to column width, we should realign.
17882 (defun org-remap (map &rest commands)
17883 "In MAP, remap the functions given in COMMANDS.
17884 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
17887 (setq old (pop commands) new (pop commands))
17888 (if (fboundp 'command-remapping)
17889 (org-defkey map (vector 'remap old) new)
17890 (substitute-key-definition old new map global-map)))))
17892 (when (eq org-enable-table-editor 'optimized)
17893 ;; If the user wants maximum table support, we need to hijack
17894 ;; some standard editing functions
17895 (org-remap org-mode-map
17896 'self-insert-command 'org-self-insert-command
17897 'delete-char 'org-delete-char
17898 'delete-backward-char 'org-delete-backward-char)
17899 (org-defkey org-mode-map "|" 'org-force-self-insert))
17901 (defvar org-ctrl-c-ctrl-c-hook nil
17902 "Hook for functions attaching themselves to `C-c C-c'.
17904 This can be used to add additional functionality to the C-c C-c
17905 key which executes context-dependent commands. This hook is run
17906 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
17907 run after the last test.
17909 Each function will be called with no arguments. The function
17910 must check if the context is appropriate for it to act. If yes,
17911 it should do its thing and then return a non-nil value. If the
17912 context is wrong, just do nothing and return nil.")
17914 (defvar org-ctrl-c-ctrl-c-final-hook nil
17915 "Hook for functions attaching themselves to `C-c C-c'.
17917 This can be used to add additional functionality to the C-c C-c
17918 key which executes context-dependent commands. This hook is run
17919 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
17920 before the first test.
17922 Each function will be called with no arguments. The function
17923 must check if the context is appropriate for it to act. If yes,
17924 it should do its thing and then return a non-nil value. If the
17925 context is wrong, just do nothing and return nil.")
17927 (defvar org-tab-first-hook nil
17928 "Hook for functions to attach themselves to TAB.
17929 See `org-ctrl-c-ctrl-c-hook' for more information.
17930 This hook runs as the first action when TAB is pressed, even before
17931 `org-cycle' messes around with the `outline-regexp' to cater for
17932 inline tasks and plain list item folding.
17933 If any function in this hook returns t, any other actions that
17934 would have been caused by TAB (such as table field motion or visibility
17935 cycling) will not occur.")
17937 (defvar org-tab-after-check-for-table-hook nil
17938 "Hook for functions to attach themselves to TAB.
17939 See `org-ctrl-c-ctrl-c-hook' for more information.
17940 This hook runs after it has been established that the cursor is not in a
17941 table, but before checking if the cursor is in a headline or if global cycling
17943 If any function in this hook returns t, not other actions like visibility
17944 cycling will be done.")
17946 (defvar org-tab-after-check-for-cycling-hook nil
17947 "Hook for functions to attach themselves to TAB.
17948 See `org-ctrl-c-ctrl-c-hook' for more information.
17949 This hook runs after it has been established that not table field motion and
17950 not visibility should be done because of current context. This is probably
17951 the place where a package like yasnippets can hook in.")
17953 (defvar org-tab-before-tab-emulation-hook nil
17954 "Hook for functions to attach themselves to TAB.
17955 See `org-ctrl-c-ctrl-c-hook' for more information.
17956 This hook runs after every other options for TAB have been exhausted, but
17957 before indentation and \t insertion takes place.")
17959 (defvar org-metaleft-hook nil
17960 "Hook for functions attaching themselves to `M-left'.
17961 See `org-ctrl-c-ctrl-c-hook' for more information.")
17962 (defvar org-metaright-hook nil
17963 "Hook for functions attaching themselves to `M-right'.
17964 See `org-ctrl-c-ctrl-c-hook' for more information.")
17965 (defvar org-metaup-hook nil
17966 "Hook for functions attaching themselves to `M-up'.
17967 See `org-ctrl-c-ctrl-c-hook' for more information.")
17968 (defvar org-metadown-hook nil
17969 "Hook for functions attaching themselves to `M-down'.
17970 See `org-ctrl-c-ctrl-c-hook' for more information.")
17971 (defvar org-shiftmetaleft-hook nil
17972 "Hook for functions attaching themselves to `M-S-left'.
17973 See `org-ctrl-c-ctrl-c-hook' for more information.")
17974 (defvar org-shiftmetaright-hook nil
17975 "Hook for functions attaching themselves to `M-S-right'.
17976 See `org-ctrl-c-ctrl-c-hook' for more information.")
17977 (defvar org-shiftmetaup-hook nil
17978 "Hook for functions attaching themselves to `M-S-up'.
17979 See `org-ctrl-c-ctrl-c-hook' for more information.")
17980 (defvar org-shiftmetadown-hook nil
17981 "Hook for functions attaching themselves to `M-S-down'.
17982 See `org-ctrl-c-ctrl-c-hook' for more information.")
17983 (defvar org-metareturn-hook nil
17984 "Hook for functions attaching themselves to `M-RET'.
17985 See `org-ctrl-c-ctrl-c-hook' for more information.")
17986 (defvar org-shiftup-hook nil
17987 "Hook for functions attaching themselves to `S-up'.
17988 See `org-ctrl-c-ctrl-c-hook' for more information.")
17989 (defvar org-shiftup-final-hook nil
17990 "Hook for functions attaching themselves to `S-up'.
17991 This one runs after all other options except shift-select have been excluded.
17992 See `org-ctrl-c-ctrl-c-hook' for more information.")
17993 (defvar org-shiftdown-hook nil
17994 "Hook for functions attaching themselves to `S-down'.
17995 See `org-ctrl-c-ctrl-c-hook' for more information.")
17996 (defvar org-shiftdown-final-hook nil
17997 "Hook for functions attaching themselves to `S-down'.
17998 This one runs after all other options except shift-select have been excluded.
17999 See `org-ctrl-c-ctrl-c-hook' for more information.")
18000 (defvar org-shiftleft-hook nil
18001 "Hook for functions attaching themselves to `S-left'.
18002 See `org-ctrl-c-ctrl-c-hook' for more information.")
18003 (defvar org-shiftleft-final-hook nil
18004 "Hook for functions attaching themselves to `S-left'.
18005 This one runs after all other options except shift-select have been excluded.
18006 See `org-ctrl-c-ctrl-c-hook' for more information.")
18007 (defvar org-shiftright-hook nil
18008 "Hook for functions attaching themselves to `S-right'.
18009 See `org-ctrl-c-ctrl-c-hook' for more information.")
18010 (defvar org-shiftright-final-hook nil
18011 "Hook for functions attaching themselves to `S-right'.
18012 This one runs after all other options except shift-select have been excluded.
18013 See `org-ctrl-c-ctrl-c-hook' for more information.")
18015 (defun org-modifier-cursor-error ()
18016 "Throw an error, a modified cursor command was applied in wrong context."
18017 (error "This command is active in special context like tables, headlines or items"))
18019 (defun org-shiftselect-error ()
18020 "Throw an error because Shift-Cursor command was applied in wrong context."
18021 (if (and (boundp 'shift-select-mode) shift-select-mode)
18022 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18023 (error "This command works only in special context like headlines or timestamps")))
18025 (defun org-call-for-shift-select (cmd)
18026 (let ((this-command-keys-shift-translated t))
18027 (call-interactively cmd)))
18029 (defun org-shifttab (&optional arg)
18030 "Global visibility cycling or move to previous table field.
18031 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18033 See the individual commands for more information."
18036 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18038 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18039 (message "Content view to level: %d" arg)
18040 (org-content (prefix-numeric-value arg2))
18041 (setq org-cycle-global-status 'overview)))
18042 (t (call-interactively 'org-global-cycle))))
18044 (defun org-shiftmetaleft ()
18045 "Promote subtree or delete table column.
18046 Calls `org-promote-subtree', `org-outdent-item',
18047 or `org-table-delete-column', depending on context.
18048 See the individual commands for more information."
18051 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18052 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18053 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18054 ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
18055 (t (org-modifier-cursor-error))))
18057 (defun org-shiftmetaright ()
18058 "Demote subtree or insert table column.
18059 Calls `org-demote-subtree', `org-indent-item',
18060 or `org-table-insert-column', depending on context.
18061 See the individual commands for more information."
18064 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18065 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18066 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18067 ((org-at-item-p) (call-interactively 'org-indent-item-tree))
18068 (t (org-modifier-cursor-error))))
18070 (defun org-shiftmetaup (&optional arg)
18071 "Move subtree up or kill table row.
18072 Calls `org-move-subtree-up' or `org-table-kill-row' or
18073 `org-move-item-up' depending on context. See the individual commands
18074 for more information."
18077 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18078 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18079 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18080 ((org-at-item-p) (call-interactively 'org-move-item-up))
18081 (t (org-modifier-cursor-error))))
18083 (defun org-shiftmetadown (&optional arg)
18084 "Move subtree down or insert table row.
18085 Calls `org-move-subtree-down' or `org-table-insert-row' or
18086 `org-move-item-down', depending on context. See the individual
18087 commands for more information."
18090 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18091 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18092 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18093 ((org-at-item-p) (call-interactively 'org-move-item-down))
18094 (t (org-modifier-cursor-error))))
18096 (defsubst org-hidden-tree-error ()
18098 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18100 (defun org-metaleft (&optional arg)
18101 "Promote heading or move table column to left.
18102 Calls `org-do-promote' or `org-table-move-column', depending on context.
18103 With no specific context, calls the Emacs default `backward-word'.
18104 See the individual commands for more information."
18107 ((run-hook-with-args-until-success 'org-metaleft-hook))
18108 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18109 ((org-with-limited-levels
18110 (or (org-at-heading-p)
18111 (and (org-region-active-p)
18113 (goto-char (region-beginning))
18114 (org-at-heading-p)))))
18115 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18116 (call-interactively 'org-do-promote))
18117 ;; At an inline task.
18118 ((org-at-heading-p)
18119 (call-interactively 'org-inlinetask-promote))
18120 ((or (org-at-item-p)
18121 (and (org-region-active-p)
18123 (goto-char (region-beginning))
18125 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18126 (call-interactively 'org-outdent-item))
18127 (t (call-interactively 'backward-word))))
18129 (defun org-metaright (&optional arg)
18130 "Demote a subtree, a list item or move table column to right.
18131 In front of a drawer or a block keyword, indent it correctly.
18132 With no specific context, calls the Emacs default `forward-word'.
18133 See the individual commands for more information."
18136 ((run-hook-with-args-until-success 'org-metaright-hook))
18137 ((org-at-table-p) (call-interactively 'org-table-move-column))
18138 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18139 ((org-at-block-p) (call-interactively 'org-indent-block))
18140 ((org-with-limited-levels
18141 (or (org-at-heading-p)
18142 (and (org-region-active-p)
18144 (goto-char (region-beginning))
18145 (org-at-heading-p)))))
18146 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18147 (call-interactively 'org-do-demote))
18148 ;; At an inline task.
18149 ((org-at-heading-p)
18150 (call-interactively 'org-inlinetask-demote))
18151 ((or (org-at-item-p)
18152 (and (org-region-active-p)
18154 (goto-char (region-beginning))
18156 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18157 (call-interactively 'org-indent-item))
18158 (t (call-interactively 'forward-word))))
18160 (defun org-check-for-hidden (what)
18161 "Check if there are hidden headlines/items in the current visual line.
18162 WHAT can be either `headlines' or `items'. If the current line is
18163 an outline or item heading and it has a folded subtree below it,
18164 this function returns t, nil otherwise."
18166 ((eq what 'headlines) org-outline-regexp-bol)
18167 ((eq what 'items) (org-item-beginning-re))
18168 (t (error "This should not happen"))))
18172 (unless (org-region-active-p)
18173 (setq beg (point-at-bol))
18174 (beginning-of-line 2)
18175 (while (and (not (eobp)) ;; this is like `next-line'
18176 (get-char-property (1- (point)) 'invisible))
18177 (beginning-of-line 2))
18180 (goto-char (point-at-eol))
18181 (setq end (max end (point)))
18182 (while (re-search-forward re end t)
18183 (if (get-char-property (match-beginning 0) 'invisible)
18187 (defun org-metaup (&optional arg)
18188 "Move subtree up or move table row up.
18189 Calls `org-move-subtree-up' or `org-table-move-row' or
18190 `org-move-item-up', depending on context. See the individual commands
18191 for more information."
18194 ((run-hook-with-args-until-success 'org-metaup-hook))
18195 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18196 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18197 ((org-at-item-p) (call-interactively 'org-move-item-up))
18198 (t (transpose-lines 1) (beginning-of-line -1))))
18200 (defun org-metadown (&optional arg)
18201 "Move subtree down or move table row down.
18202 Calls `org-move-subtree-down' or `org-table-move-row' or
18203 `org-move-item-down', depending on context. See the individual
18204 commands for more information."
18207 ((run-hook-with-args-until-success 'org-metadown-hook))
18208 ((org-at-table-p) (call-interactively 'org-table-move-row))
18209 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18210 ((org-at-item-p) (call-interactively 'org-move-item-down))
18211 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
18213 (defun org-shiftup (&optional arg)
18214 "Increase item in timestamp or increase priority of current headline.
18215 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18216 depending on context. See the individual commands for more information."
18219 ((run-hook-with-args-until-success 'org-shiftup-hook))
18220 ((and org-support-shift-select (org-region-active-p))
18221 (org-call-for-shift-select 'previous-line))
18222 ((org-at-timestamp-p t)
18223 (call-interactively (if org-edit-timestamp-down-means-later
18224 'org-timestamp-down 'org-timestamp-up)))
18225 ((and (not (eq org-support-shift-select 'always))
18226 org-enable-priority-commands
18227 (org-at-heading-p))
18228 (call-interactively 'org-priority-up))
18229 ((and (not org-support-shift-select) (org-at-item-p))
18230 (call-interactively 'org-previous-item))
18231 ((org-clocktable-try-shift 'up arg))
18232 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18233 (org-support-shift-select
18234 (org-call-for-shift-select 'previous-line))
18235 (t (org-shiftselect-error))))
18237 (defun org-shiftdown (&optional arg)
18238 "Decrease item in timestamp or decrease priority of current headline.
18239 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18240 depending on context. See the individual commands for more information."
18243 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18244 ((and org-support-shift-select (org-region-active-p))
18245 (org-call-for-shift-select 'next-line))
18246 ((org-at-timestamp-p t)
18247 (call-interactively (if org-edit-timestamp-down-means-later
18248 'org-timestamp-up 'org-timestamp-down)))
18249 ((and (not (eq org-support-shift-select 'always))
18250 org-enable-priority-commands
18251 (org-at-heading-p))
18252 (call-interactively 'org-priority-down))
18253 ((and (not org-support-shift-select) (org-at-item-p))
18254 (call-interactively 'org-next-item))
18255 ((org-clocktable-try-shift 'down arg))
18256 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18257 (org-support-shift-select
18258 (org-call-for-shift-select 'next-line))
18259 (t (org-shiftselect-error))))
18261 (defun org-shiftright (&optional arg)
18262 "Cycle the thing at point or in the current line, depending on context.
18263 Depending on context, this does one of the following:
18265 - switch a timestamp at point one day into the future
18266 - on a headline, switch to the next TODO keyword.
18267 - on an item, switch entire list to the next bullet type
18268 - on a property line, switch to the next allowed value
18269 - on a clocktable definition line, move time block into the future"
18272 ((run-hook-with-args-until-success 'org-shiftright-hook))
18273 ((and org-support-shift-select (org-region-active-p))
18274 (org-call-for-shift-select 'forward-char))
18275 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18276 ((and (not (eq org-support-shift-select 'always))
18277 (org-at-heading-p))
18278 (let ((org-inhibit-logging
18279 (not org-treat-S-cursor-todo-selection-as-state-change))
18280 (org-inhibit-blocking
18281 (not org-treat-S-cursor-todo-selection-as-state-change)))
18282 (org-call-with-arg 'org-todo 'right)))
18283 ((or (and org-support-shift-select
18284 (not (eq org-support-shift-select 'always))
18285 (org-at-item-bullet-p))
18286 (and (not org-support-shift-select) (org-at-item-p)))
18287 (org-call-with-arg 'org-cycle-list-bullet nil))
18288 ((and (not (eq org-support-shift-select 'always))
18289 (org-at-property-p))
18290 (call-interactively 'org-property-next-allowed-value))
18291 ((org-clocktable-try-shift 'right arg))
18292 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18293 (org-support-shift-select
18294 (org-call-for-shift-select 'forward-char))
18295 (t (org-shiftselect-error))))
18297 (defun org-shiftleft (&optional arg)
18298 "Cycle the thing at point or in the current line, depending on context.
18299 Depending on context, this does one of the following:
18301 - switch a timestamp at point one day into the past
18302 - on a headline, switch to the previous TODO keyword.
18303 - on an item, switch entire list to the previous bullet type
18304 - on a property line, switch to the previous allowed value
18305 - on a clocktable definition line, move time block into the past"
18308 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18309 ((and org-support-shift-select (org-region-active-p))
18310 (org-call-for-shift-select 'backward-char))
18311 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18312 ((and (not (eq org-support-shift-select 'always))
18313 (org-at-heading-p))
18314 (let ((org-inhibit-logging
18315 (not org-treat-S-cursor-todo-selection-as-state-change))
18316 (org-inhibit-blocking
18317 (not org-treat-S-cursor-todo-selection-as-state-change)))
18318 (org-call-with-arg 'org-todo 'left)))
18319 ((or (and org-support-shift-select
18320 (not (eq org-support-shift-select 'always))
18321 (org-at-item-bullet-p))
18322 (and (not org-support-shift-select) (org-at-item-p)))
18323 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18324 ((and (not (eq org-support-shift-select 'always))
18325 (org-at-property-p))
18326 (call-interactively 'org-property-previous-allowed-value))
18327 ((org-clocktable-try-shift 'left arg))
18328 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18329 (org-support-shift-select
18330 (org-call-for-shift-select 'backward-char))
18331 (t (org-shiftselect-error))))
18333 (defun org-shiftcontrolright ()
18334 "Switch to next TODO set."
18337 ((and org-support-shift-select (org-region-active-p))
18338 (org-call-for-shift-select 'forward-word))
18339 ((and (not (eq org-support-shift-select 'always))
18340 (org-at-heading-p))
18341 (org-call-with-arg 'org-todo 'nextset))
18342 (org-support-shift-select
18343 (org-call-for-shift-select 'forward-word))
18344 (t (org-shiftselect-error))))
18346 (defun org-shiftcontrolleft ()
18347 "Switch to previous TODO set."
18350 ((and org-support-shift-select (org-region-active-p))
18351 (org-call-for-shift-select 'backward-word))
18352 ((and (not (eq org-support-shift-select 'always))
18353 (org-at-heading-p))
18354 (org-call-with-arg 'org-todo 'previousset))
18355 (org-support-shift-select
18356 (org-call-for-shift-select 'backward-word))
18357 (t (org-shiftselect-error))))
18359 (defun org-shiftcontrolup ()
18360 "Change timestamps synchronously up in CLOCK log lines."
18362 (cond ((and (not org-support-shift-select)
18363 (org-at-clock-log-p)
18364 (org-at-timestamp-p t))
18365 (org-clock-timestamps-up))
18366 (t (org-shiftselect-error))))
18368 (defun org-shiftcontroldown ()
18369 "Change timestamps synchronously down in CLOCK log lines."
18371 (cond ((and (not org-support-shift-select)
18372 (org-at-clock-log-p)
18373 (org-at-timestamp-p t))
18374 (org-clock-timestamps-down))
18375 (t (org-shiftselect-error))))
18377 (defun org-ctrl-c-ret ()
18378 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18381 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18382 (t (call-interactively 'org-insert-heading))))
18384 (defun org-find-visible ()
18386 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18387 (get-char-property s 'invisible)))
18389 (defun org-find-invisible ()
18391 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18392 (not (get-char-property s 'invisible))))
18395 (defun org-copy-visible (beg end)
18396 "Copy the visible parts of the region."
18401 (narrow-to-region beg end)
18402 (setq s (goto-char (point-min)))
18403 (while (not (= (point) (point-max)))
18404 (goto-char (org-find-invisible))
18405 (push (buffer-substring s (point)) snippets)
18406 (setq s (goto-char (org-find-visible))))))
18407 (kill-new (apply 'concat (nreverse snippets)))))
18409 (defun org-copy-special ()
18410 "Copy region in table or copy current subtree.
18411 Calls `org-table-copy' or `org-copy-subtree', depending on context.
18412 See the individual commands for more information."
18414 (call-interactively
18415 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
18417 (defun org-cut-special ()
18418 "Cut region in table or cut current subtree.
18419 Calls `org-table-copy' or `org-cut-subtree', depending on context.
18420 See the individual commands for more information."
18422 (call-interactively
18423 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
18425 (defun org-paste-special (arg)
18426 "Paste rectangular region into table, or past subtree relative to level.
18427 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
18428 See the individual commands for more information."
18430 (if (org-at-table-p)
18431 (org-table-paste-rectangle)
18432 (org-paste-subtree arg)))
18434 (defun org-edit-special (&optional arg)
18435 "Call a special editor for the stuff at point.
18436 When at a table, call the formula editor with `org-table-edit-formulas'.
18437 When at the first line of an src example, call `org-edit-src-code'.
18438 When in an #+include line, visit the include file. Otherwise call
18439 `ffap' to visit the file at point."
18441 ;; possibly prep session before editing source
18443 (let* ((info (org-babel-get-src-block-info))
18444 (lang (nth 0 info))
18445 (params (nth 2 info))
18446 (session (cdr (assoc :session params))))
18447 (when (and info session) ;; we are in a source-code block with a session
18449 (intern (concat "org-babel-prep-session:" lang)) session params))))
18450 (cond ;; proceed with `org-edit-special'
18452 (beginning-of-line 1)
18453 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
18454 (find-file (org-trim (match-string 1))))
18455 ((org-edit-src-code))
18456 ((org-edit-fixed-width-region))
18457 ((org-at-table.el-p)
18458 (org-edit-src-code))
18459 ((or (org-at-table-p)
18461 (beginning-of-line 1)
18462 (looking-at "[ \t]*#\\+TBLFM:")))
18463 (call-interactively 'org-table-edit-formulas))
18464 (t (call-interactively 'ffap))))
18466 (defvar org-table-coordinate-overlays) ; defined in org-table.el
18467 (defun org-ctrl-c-ctrl-c (&optional arg)
18468 "Set tags in headline, or update according to changed information at point.
18470 This command does many different things, depending on context:
18472 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
18473 this is what we do.
18475 - If the cursor is on a statistics cookie, update it.
18477 - If the cursor is in a headline, prompt for tags and insert them
18478 into the current line, aligned to `org-tags-column'. When called
18479 with prefix arg, realign all tags in the current buffer.
18481 - If the cursor is in one of the special #+KEYWORD lines, this
18482 triggers scanning the buffer for these lines and updating the
18485 - If the cursor is inside a table, realign the table. This command
18486 works even if the automatic table editor has been turned off.
18488 - If the cursor is on a #+TBLFM line, re-apply the formulas to
18491 - If the cursor is at a footnote reference or definition, jump to
18492 the corresponding definition or references, respectively.
18494 - If the cursor is a the beginning of a dynamic block, update it.
18496 - If the current buffer is a capture buffer, close note and file it.
18498 - If the cursor is on a <<<target>>>, update radio targets and
18499 corresponding links in this buffer.
18501 - If the cursor is on a numbered item in a plain list, renumber the
18504 - If the cursor is on a checkbox, toggle it.
18506 - If the cursor is on a code block, evaluate it. The variable
18507 `org-confirm-babel-evaluate' can be used to control prompting
18508 before code block evaluation, by default every code block
18509 evaluation requires confirmation. Code block evaluation can be
18510 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
18512 (let ((org-enable-table-editor t))
18514 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
18515 org-occur-highlights
18516 org-latex-fragment-image-overlays)
18517 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
18518 (org-remove-occur-highlights)
18519 (org-remove-latex-fragment-image-overlays)
18520 (message "Temporary highlights/overlays removed from current buffer"))
18521 ((and (local-variable-p 'org-finish-function (current-buffer))
18522 (fboundp org-finish-function))
18523 (funcall org-finish-function))
18524 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
18525 ((org-in-regexp org-ts-regexp-both)
18526 (org-timestamp-change 0 'day))
18527 ((or (looking-at org-property-start-re)
18528 (org-at-property-p))
18529 (call-interactively 'org-property-action))
18530 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
18531 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
18532 (or (org-at-heading-p) (org-at-item-p)))
18533 (call-interactively 'org-update-statistics-cookies))
18534 ((org-at-heading-p) (call-interactively 'org-set-tags))
18535 ((org-at-table.el-p)
18536 (message "Use C-c ' to edit table.el tables"))
18538 (org-table-maybe-eval-formula)
18540 (call-interactively 'org-table-recalculate)
18541 (org-table-maybe-recalculate-line))
18542 (call-interactively 'org-table-align)
18543 (orgtbl-send-table 'maybe))
18544 ((or (org-footnote-at-reference-p)
18545 (org-footnote-at-definition-p))
18546 (call-interactively 'org-footnote-action))
18547 ((org-at-item-checkbox-p)
18548 ;; Cursor at a checkbox: repair list and update checkboxes. Send
18549 ;; list only if at top item.
18550 (let* ((cbox (match-string 1))
18551 (struct (org-list-struct))
18552 (old-struct (copy-tree struct))
18553 (parents (org-list-parents-alist struct))
18554 (orderedp (org-entry-get nil "ORDERED"))
18555 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
18557 ;; Use a light version of `org-toggle-checkbox' to avoid
18558 ;; computing list structure twice.
18559 (let ((new-box (cond
18560 ((equal arg '(16)) "[-]")
18561 ((equal arg '(4)) nil)
18562 ((equal "[X]" cbox) "[ ]")
18564 (if (and firstp arg)
18565 ;; If at first item of sub-list, remove check-box from
18566 ;; every item at the same level.
18568 (lambda (pos) (org-list-set-checkbox pos struct new-box))
18569 (org-list-get-all-items
18570 (point-at-bol) struct (org-list-prevs-alist struct)))
18571 (org-list-set-checkbox (point-at-bol) struct new-box)))
18572 ;; Replicate `org-list-write-struct', while grabbing a return
18573 ;; value from `org-list-struct-fix-box'.
18574 (org-list-struct-fix-ind struct parents 2)
18575 (org-list-struct-fix-item-end struct)
18576 (let ((prevs (org-list-prevs-alist struct)))
18577 (org-list-struct-fix-bul struct prevs)
18578 (org-list-struct-fix-ind struct parents)
18580 (org-list-struct-fix-box struct parents prevs orderedp)))
18581 (org-list-struct-apply-struct struct old-struct)
18582 (org-update-checkbox-count-maybe)
18585 "Checkboxes were removed due to unchecked box at line %d"
18586 (org-current-line block-item)))
18587 (when firstp (org-list-send-list 'maybe))))
18589 ;; Cursor at an item: repair list. Do checkbox related actions
18590 ;; only if function was called with an argument. Send list only
18592 (let* ((struct (org-list-struct))
18593 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
18596 (setq old-struct (copy-tree struct))
18598 ;; If at first item of sub-list, add check-box to every
18599 ;; item at the same level.
18602 (unless (org-list-get-checkbox pos struct)
18603 (org-list-set-checkbox pos struct "[ ]")))
18604 (org-list-get-all-items
18605 (point-at-bol) struct (org-list-prevs-alist struct)))
18606 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
18607 (org-list-write-struct
18608 struct (org-list-parents-alist struct) old-struct)
18609 (when arg (org-update-checkbox-count-maybe))
18610 (when firstp (org-list-send-list 'maybe))))
18611 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
18613 (beginning-of-line 1)
18614 (save-excursion (org-update-dblock)))
18616 (beginning-of-line 1)
18617 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
18619 ((equal (match-string 1) "TBLFM")
18620 ;; Recalculate the table before this line
18622 (beginning-of-line 1)
18623 (skip-chars-backward " \r\n\t")
18624 (if (org-at-table-p)
18625 (org-call-with-arg 'org-table-recalculate (or arg t)))))
18627 (let ((org-inhibit-startup-visibility-stuff t)
18628 (org-startup-align-all-tables nil))
18629 (when (boundp 'org-table-coordinate-overlays)
18630 (mapc 'delete-overlay org-table-coordinate-overlays)
18631 (setq org-table-coordinate-overlays nil))
18632 (org-save-outline-visibility 'use-markers (org-mode-restart)))
18633 (message "Local setup has been refreshed"))))
18634 ((org-clock-update-time-maybe))
18636 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
18637 (error "C-c C-c can do nothing useful at this location"))))))
18639 (defun org-mode-restart ()
18640 "Restart Org-mode, to scan again for special lines.
18641 Also updates the keyword regular expressions."
18644 (message "Org-mode restarted"))
18646 (defun org-kill-note-or-show-branches ()
18647 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
18649 (if (not org-finish-function)
18652 (call-interactively 'show-branches))
18653 (let ((org-note-abort t))
18654 (funcall org-finish-function))))
18656 (defun org-return (&optional indent)
18657 "Goto next table row or insert a newline.
18658 Calls `org-table-next-row' or `newline', depending on context.
18659 See the individual commands for more information."
18662 ((or (bobp) (org-in-src-block-p))
18663 (if indent (newline-and-indent) (newline)))
18665 (org-table-justify-field-maybe)
18666 (call-interactively 'org-table-next-row))
18667 ;; when `newline-and-indent' is called within a list, make sure
18668 ;; text moved stays inside the item.
18669 ((and (org-in-item-p) indent)
18670 (if (and (org-at-item-p) (>= (point) (match-end 0)))
18672 (save-match-data (newline))
18673 (org-indent-line-to (length (match-string 0))))
18674 (let ((ind (org-get-indentation)))
18676 (if (org-looking-back org-list-end-re)
18677 (org-indent-line-function)
18678 (org-indent-line-to ind)))))
18679 ((and org-return-follows-link
18680 (let ((tprop (get-text-property (point) 'face)))
18681 (or (eq tprop 'org-link)
18682 (and (listp tprop) (memq 'org-link tprop)))))
18683 (call-interactively 'org-open-at-point))
18684 ((and (org-at-heading-p)
18686 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
18690 (t (if indent (newline-and-indent) (newline)))))
18692 (defun org-return-indent ()
18693 "Goto next table row or insert a newline and indent.
18694 Calls `org-table-next-row' or `newline-and-indent', depending on
18695 context. See the individual commands for more information."
18699 (defun org-ctrl-c-star ()
18700 "Compute table, or change heading status of lines.
18701 Calls `org-table-recalculate' or `org-toggle-heading',
18702 depending on context."
18706 (call-interactively 'org-table-recalculate))
18708 ;; Convert all lines in region to list items
18709 (call-interactively 'org-toggle-heading))))
18711 (defun org-ctrl-c-minus ()
18712 "Insert separator line in table or modify bullet status of line.
18713 Also turns a plain line or a region of lines into list items.
18714 Calls `org-table-insert-hline', `org-toggle-item', or
18715 `org-cycle-list-bullet', depending on context."
18719 (call-interactively 'org-table-insert-hline))
18720 ((org-region-active-p)
18721 (call-interactively 'org-toggle-item))
18723 (call-interactively 'org-cycle-list-bullet))
18725 (call-interactively 'org-toggle-item))))
18727 (defun org-toggle-item (arg)
18728 "Convert headings or normal lines to items, items to normal lines.
18729 If there is no active region, only the current line is considered.
18731 If the first non blank line in the region is an headline, convert
18732 all headlines to items, shifting text accordingly.
18734 If it is an item, convert all items to normal lines.
18736 If it is normal text, change region into an item. With a prefix
18737 argument ARG, change each line in region into an item."
18741 ;; Shift text in current section to IND, from point to END.
18742 ;; The function leaves point to END line.
18744 (let ((min-i 1000) (end (copy-marker end)))
18745 ;; First determine the minimum indentation (MIN-I) of
18749 (while (< (point) end)
18750 (let ((i (org-get-indentation)))
18752 ;; Skip blank lines and inline tasks.
18753 ((looking-at "^[ \t]*$"))
18754 ((looking-at org-outline-regexp-bol))
18755 ;; We can't find less than 0 indentation.
18756 ((zerop i) (throw 'exit (setq min-i 0)))
18757 ((< i min-i) (setq min-i i))))
18759 ;; Then indent each line so that a line indented to
18760 ;; MIN-I becomes indented to IND. Ignore blank lines
18761 ;; and inline tasks in the process.
18762 (let ((delta (- ind min-i)))
18763 (while (< (point) end)
18764 (unless (or (looking-at "^[ \t]*$")
18765 (looking-at org-outline-regexp-bol))
18766 (org-indent-line-to (+ (org-get-indentation) delta)))
18767 (forward-line)))))))
18770 ;; Return beginning of first non-blank line, starting from
18775 (skip-chars-forward " \r\t\n")
18778 ;; Determine boundaries of changes.
18779 (if (org-region-active-p)
18780 (setq beg (funcall skip-blanks (region-beginning))
18781 end (copy-marker (region-end)))
18782 (setq beg (funcall skip-blanks (point-at-bol))
18783 end (copy-marker (point-at-eol))))
18784 ;; Depending on the starting line, choose an action on the text
18785 ;; between BEG and END.
18786 (org-with-limited-levels
18790 ;; Case 1. Start at an item: de-itemize. Note that it only
18791 ;; happens when a region is active: `org-ctrl-c-minus'
18792 ;; would call `org-cycle-list-bullet' otherwise.
18794 (while (< (point) end)
18795 (when (org-at-item-p)
18796 (skip-chars-forward " \t")
18797 (delete-region (point) (match-end 0)))
18799 ;; Case 2. Start at an heading: convert to items.
18800 ((org-at-heading-p)
18801 (let* ((bul (org-list-bullet-string "-"))
18802 (bul-len (length bul))
18803 ;; Indentation of the first heading. It should be
18804 ;; relative to the indentation of its parent, if any.
18805 (start-ind (save-excursion
18807 ((not org-adapt-indentation) 0)
18808 ((not (outline-previous-heading)) 0)
18809 (t (length (match-string 0))))))
18810 ;; Level of first heading. Further headings will be
18811 ;; compared to it to determine hierarchy in the list.
18812 (ref-level (org-reduced-level (org-outline-level))))
18813 (while (< (point) end)
18814 (let* ((level (org-reduced-level (org-outline-level)))
18815 (delta (max 0 (- level ref-level))))
18816 ;; If current headline is less indented than the first
18817 ;; one, set it as reference, in order to preserve
18819 (when (< level ref-level) (setq ref-level level))
18820 (replace-match bul t t)
18821 (org-indent-line-to (+ start-ind (* delta bul-len)))
18822 ;; Ensure all text down to END (or SECTION-END) belongs
18823 ;; to the newly created item.
18824 (let ((section-end (save-excursion
18825 (or (outline-next-heading) (point)))))
18827 (funcall shift-text
18828 (+ start-ind (* (1+ delta) bul-len))
18829 (min end section-end)))))))
18830 ;; Case 3. Normal line with ARG: turn each non-item line into
18833 (while (< (point) end)
18834 (unless (or (org-at-heading-p) (org-at-item-p))
18835 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
18837 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
18839 ;; Case 4. Normal line without ARG: make the first line of
18840 ;; region an item, and shift indentation of others
18841 ;; lines to set them as item's body.
18842 (t (let* ((bul (org-list-bullet-string "-"))
18843 (bul-len (length bul))
18844 (ref-ind (org-get-indentation)))
18845 (skip-chars-forward " \t")
18848 (while (< (point) end)
18849 ;; Ensure that lines less indented than first one
18850 ;; still get included in item body.
18851 (funcall shift-text
18852 (+ ref-ind bul-len)
18853 (min end (save-excursion (or (outline-next-heading)
18855 (forward-line)))))))))
18857 (defun org-toggle-heading (&optional nstars)
18858 "Convert headings to normal text, or items or text to headings.
18859 If there is no active region, only the current line is considered.
18861 If the first non blank line is an headline, remove the stars from
18862 all headlines in the region.
18864 If it is a plain list item, turn all plain list items into headings.
18866 If it is a normal line, turn each and every normal line (i.e. not
18867 an heading or an item) in the region into a heading.
18869 When converting a line into a heading, the number of stars is chosen
18870 such that the lines become children of the current entry. However,
18871 when a prefix argument is given, its value determines the number of
18876 ;; Return beginning of first non-blank line, starting from
18881 (skip-chars-forward " \r\t\n")
18884 ;; Determine boundaries of changes. If region ends at a bol, do
18885 ;; not consider the last line to be in the region.
18886 (if (org-region-active-p)
18887 (setq beg (funcall skip-blanks (region-beginning))
18888 end (copy-marker (save-excursion
18889 (goto-char (region-end))
18890 (if (bolp) (point) (point-at-eol)))))
18891 (setq beg (funcall skip-blanks (point-at-bol))
18892 end (copy-marker (point-at-eol))))
18893 ;; Ensure inline tasks don't count as headings.
18894 (org-with-limited-levels
18898 ;; Case 1. Started at an heading: de-star headings.
18899 ((org-at-heading-p)
18900 (while (< (point) end)
18901 (when (org-at-heading-p t)
18902 (looking-at org-outline-regexp) (replace-match ""))
18904 ;; Case 2. Started at an item: change items into headlines.
18905 ;; One star will be added by `org-list-to-subtree'.
18907 (let* ((stars (make-string
18909 ;; subtract the star that will be added again by
18910 ;; `org-list-to-subtree'
18911 (1- (prefix-numeric-value current-prefix-arg))
18912 (or (org-current-level) 0))
18915 (cond (nstars "") ; stars from prefix only
18916 ((equal stars "") "") ; before first heading
18917 (org-odd-levels-only "*") ; inside heading, odd
18918 (t "")))) ; inside heading, oddeven
18919 (while (< (point) end)
18920 (when (org-at-item-p)
18921 ;; Pay attention to cases when region ends before list.
18922 (let* ((struct (org-list-struct))
18923 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
18925 (narrow-to-region (point) list-end)
18927 (org-list-to-subtree
18928 (org-list-parse-list t)
18929 '(:istart (concat stars add-stars (funcall get-stars depth))
18930 :icount (concat stars add-stars (funcall get-stars depth))))))))
18932 ;; Case 3. Started at normal text: make every line an heading,
18933 ;; skipping headlines and items.
18934 (t (let* ((stars (make-string
18936 (prefix-numeric-value current-prefix-arg)
18937 (or (org-current-level) 0))
18940 (cond (nstars "") ; stars from prefix only
18941 ((equal stars "") "*") ; before first heading
18942 (org-odd-levels-only "**") ; inside heading, odd
18943 (t "*"))) ; inside heading, oddeven
18944 (rpl (concat stars add-stars " ")))
18945 (while (< (point) end)
18946 (when (and (not (org-at-heading-p)) (not (org-at-item-p))
18947 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
18948 (replace-match (concat rpl (match-string 2))))
18949 (forward-line)))))))))
18951 (defun org-meta-return (&optional arg)
18952 "Insert a new heading or wrap a region in a table.
18953 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
18954 See the individual commands for more information."
18957 ((run-hook-with-args-until-success 'org-metareturn-hook))
18958 ((or (org-at-drawer-p) (org-at-property-p))
18959 (newline-and-indent))
18961 (call-interactively 'org-table-wrap-region))
18962 (t (call-interactively 'org-insert-heading))))
18966 ;; Define the Org-mode menus
18967 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
18969 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
18970 ["Next Field" org-cycle (org-at-table-p)]
18971 ["Previous Field" org-shifttab (org-at-table-p)]
18972 ["Next Row" org-return (org-at-table-p)]
18974 ["Blank Field" org-table-blank-field (org-at-table-p)]
18975 ["Edit Field" org-table-edit-field (org-at-table-p)]
18976 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
18979 ["Move Column Left" org-metaleft (org-at-table-p)]
18980 ["Move Column Right" org-metaright (org-at-table-p)]
18981 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
18982 ["Insert Column" org-shiftmetaright (org-at-table-p)])
18984 ["Move Row Up" org-metaup (org-at-table-p)]
18985 ["Move Row Down" org-metadown (org-at-table-p)]
18986 ["Delete Row" org-shiftmetaup (org-at-table-p)]
18987 ["Insert Row" org-shiftmetadown (org-at-table-p)]
18988 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
18990 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
18992 ["Copy Rectangle" org-copy-special (org-at-table-p)]
18993 ["Cut Rectangle" org-cut-special (org-at-table-p)]
18994 ["Paste Rectangle" org-paste-special (org-at-table-p)]
18995 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
18998 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
18999 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19000 ["Edit Formulas" org-edit-special (org-at-table-p)]
19002 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19003 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19004 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19006 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19008 ["Sum Column/Rectangle" org-table-sum
19009 (or (org-at-table-p) (org-region-active-p))]
19010 ["Which Column?" org-table-current-column (org-at-table-p)])
19012 org-table-toggle-formula-debugger
19013 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19014 ["Show Col/Row Numbers"
19015 org-table-toggle-coordinate-overlays
19017 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19019 ["Create" org-table-create (and (not (org-at-table-p))
19020 org-enable-table-editor)]
19021 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19022 ["Import from File" org-table-import (not (org-at-table-p))]
19023 ["Export to File" org-table-export (org-at-table-p)]
19025 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19027 (easy-menu-define org-org-menu org-mode-map "Org menu"
19030 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19031 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19032 ["Sparse Tree..." org-sparse-tree t]
19033 ["Reveal Context" org-reveal t]
19034 ["Show All" show-all t]
19036 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19038 ["New Heading" org-insert-heading t]
19039 ("Navigate Headings"
19040 ["Up" outline-up-heading t]
19041 ["Next" outline-next-visible-heading t]
19042 ["Previous" outline-previous-visible-heading t]
19043 ["Next Same Level" outline-forward-same-level t]
19044 ["Previous Same Level" outline-backward-same-level t]
19046 ["Jump" org-goto t])
19048 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
19049 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
19051 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
19052 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
19053 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19055 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19057 ["Copy visible text" org-copy-visible t]
19059 ["Promote Heading" org-metaleft (not (org-at-table-p))]
19060 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
19061 ["Demote Heading" org-metaright (not (org-at-table-p))]
19062 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
19064 ["Sort Region/Children" org-sort (not (org-at-table-p))]
19066 ["Convert to odd levels" org-convert-to-odd-levels t]
19067 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19069 ["Emphasis..." org-emphasize t]
19070 ["Edit Source Example" org-edit-special t]
19072 ["Footnote new/jump" org-footnote-action t]
19073 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19075 ["Archive (default method)" org-archive-subtree-default t]
19077 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
19078 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
19079 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
19083 ["Store Link (Global)" org-store-link t]
19084 ["Find existing link to here" org-occur-link-in-agenda-files t]
19085 ["Insert Link" org-insert-link t]
19086 ["Follow Link" org-open-at-point t]
19088 ["Next link" org-next-link t]
19089 ["Previous link" org-previous-link t]
19091 ["Descriptive Links"
19092 org-toggle-link-display
19094 :selected org-descriptive-links
19097 org-toggle-link-display
19099 :selected (not org-descriptive-links)])
19102 ["TODO/DONE/-" org-todo t]
19104 ["Next keyword" org-shiftright (org-at-heading-p)]
19105 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19106 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19107 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19108 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19109 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19110 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19112 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19113 :selected org-enforce-todo-dependencies :style toggle :active t]
19114 "Settings for tree at point"
19115 ["Do Children sequentially" org-toggle-ordered-property :style radio
19116 :selected (org-entry-get nil "ORDERED")
19117 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19118 ["Do Children parallel" org-toggle-ordered-property :style radio
19119 :selected (not (org-entry-get nil "ORDERED"))
19120 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19122 ["Set Priority" org-priority t]
19123 ["Priority Up" org-shiftup t]
19124 ["Priority Down" org-shiftdown t]
19126 ["Get news from all feeds" org-feed-update-all t]
19127 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19128 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19129 ("TAGS and Properties"
19130 ["Set Tags" org-set-tags-command t]
19131 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19133 ["Set property" org-set-property t]
19134 ["Column view of properties" org-columns t]
19135 ["Insert Column View DBlock" org-insert-columns-dblock t])
19136 ("Dates and Scheduling"
19137 ["Timestamp" org-time-stamp t]
19138 ["Timestamp (inactive)" org-time-stamp-inactive t]
19140 ["1 Day Later" org-shiftright t]
19141 ["1 Day Earlier" org-shiftleft t]
19142 ["1 ... Later" org-shiftup t]
19143 ["1 ... Earlier" org-shiftdown t])
19144 ["Compute Time Range" org-evaluate-time-range t]
19145 ["Schedule Item" org-schedule t]
19146 ["Deadline" org-deadline t]
19148 ["Custom time format" org-toggle-time-stamp-overlays
19149 :style radio :selected org-display-custom-times]
19151 ["Goto Calendar" org-goto-calendar t]
19152 ["Date from Calendar" org-date-from-calendar t]
19154 ["Start/Restart Timer" org-timer-start t]
19155 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19156 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19157 ["Insert Timer String" org-timer t]
19158 ["Insert Timer Item" org-timer-item t])
19160 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19161 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19162 ["Clock out" org-clock-out t]
19163 ["Clock cancel" org-clock-cancel t]
19165 ["Mark as default task" org-clock-mark-default-task t]
19166 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19167 ["Goto running clock" org-clock-goto t]
19169 ["Display times" org-clock-display t]
19170 ["Create clock table" org-clock-report t]
19172 ["Record DONE time"
19173 (progn (setq org-log-done (not org-log-done))
19174 (message "Switching to %s will %s record a timestamp"
19175 (car org-done-keywords)
19176 (if org-log-done "automatically" "not")))
19177 :style toggle :selected org-log-done])
19179 ["Agenda Command..." org-agenda t]
19180 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19181 ("File List for Agenda")
19182 ("Special views current file"
19183 ["TODO Tree" org-show-todo-tree t]
19184 ["Check Deadlines" org-check-deadlines t]
19185 ["Timeline" org-timeline t]
19186 ["Tags/Property tree" org-match-sparse-tree t])
19188 ["Export/Publish..." org-export t]
19190 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19191 :selected org-cdlatex-mode]
19192 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19193 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19194 ["Modify math symbol" org-cdlatex-math-modify
19195 (org-inside-LaTeX-fragment-p)]
19196 ["Insert citation" org-reftex-citation t]
19198 ["Template for BEAMER" org-insert-beamer-options-template t])
19201 ["Push Files and Views" org-mobile-push t]
19202 ["Get Captured and Flagged" org-mobile-pull t]
19203 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19205 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19208 ["Show Version" org-version t]
19209 ["Info Documentation" org-info t])
19211 ["Browse Org Group" org-customize t]
19213 ["Expand This Menu" org-create-customize-menu
19214 (fboundp 'customize-menu-create)])
19215 ["Send bug report" org-submit-bug-report t]
19218 ["Refresh setup current buffer" org-mode-restart t]
19219 ["Reload Org (after update)" org-reload t]
19220 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
19223 (defun org-info (&optional node)
19224 "Read documentation for Org-mode in the info system.
19225 With optional NODE, go directly to that node."
19227 (info (format "(org)%s" (or node ""))))
19230 (defun org-submit-bug-report ()
19231 "Submit a bug report on Org-mode via mail.
19233 Don't hesitate to report any problems or inaccurate documentation.
19235 If you don't have setup sending mail from (X)Emacs, please copy the
19236 output buffer into your mail program, as it gives us important
19237 information about your Org-mode version and configuration."
19239 (require 'reporter)
19240 (org-load-modules-maybe)
19241 (org-require-autoloaded-modules)
19242 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19243 (reporter-submit-bug-report
19244 "emacs-orgmode@gnu.org"
19247 (save-window-excursion
19248 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19249 (delete-other-windows)
19251 (insert "You are about to submit a bug report to the Org-mode mailing list.
19253 We would like to add your full Org-mode and Outline configuration to the
19254 bug report. This greatly simplifies the work of the maintainer and
19255 other experts on the mailing list.
19257 HOWEVER, some variables you have customized may contain private
19258 information. The names of customers, colleagues, or friends, might
19259 appear in the form of file names, tags, todo states, or search strings.
19260 If you answer yes to the prompt, you might want to check and remove
19261 such private information before sending the email.")
19262 (add-text-properties (point-min) (point-max) '(face org-warning))
19263 (when (yes-or-no-p "Include your Org-mode configuration ")
19267 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19268 (or (and (symbol-value v)
19269 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19271 (get v 'custom-type) (get v 'standard-value)
19272 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19274 (kill-buffer (get-buffer "*Warn about privacy*"))
19277 "Remember to cover the basics, that is, what you expected to happen and
19278 what in fact did happen. You don't know how to make a good report? See
19280 http://orgmode.org/manual/Feedback.html#Feedback
19282 Your bug report will be posted to the Org-mode mailing list.
19283 ------------------------------------------------------------------------")
19285 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19286 (replace-match "\\1Bug: \\3 [\\2]")))))
19289 (defun org-install-agenda-files-menu ()
19290 (let ((bl (buffer-list)))
19293 (set-buffer (pop bl))
19294 (if (eq major-mode 'org-mode) (setq bl nil)))
19295 (when (eq major-mode 'org-mode)
19297 '("Org") "File List for Agenda"
19300 ["Edit File List" (org-edit-agenda-file-list) t]
19301 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19302 ["Remove Current File from List" org-remove-file t]
19303 ["Cycle through agenda files" org-cycle-agenda-files t]
19304 ["Occur in all agenda files" org-occur-in-agenda-files t]
19306 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19311 (defun org-require-autoloaded-modules ()
19314 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19315 org-docbook org-exp org-html org-icalendar
19317 org-publish org-remember org-table
19318 org-timer org-xoxo)))
19321 (defun org-reload (&optional uncompiled)
19322 "Reload all org lisp files.
19323 With prefix arg UNCOMPILED, load the uncompiled versions."
19325 (require 'find-func)
19326 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
19327 (dir-org (file-name-directory (org-find-library-name "org")))
19328 (dir-org-contrib (ignore-errors
19329 (file-name-directory
19330 (org-find-library-name "org-contribdir"))))
19332 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19333 (append (list nil "comint" "eval" "exp" "keys"
19334 "lob" "ref" "table" "tangle")
19338 (when (cdr lang) (symbol-name (car lang))))
19339 org-babel-load-languages)))))
19341 (append (directory-files dir-org t file-re)
19343 (and dir-org-contrib
19344 (directory-files dir-org-contrib t file-re))))
19345 (remove-re (concat (if (featurep 'xemacs)
19346 "org-colview" "org-colview-xemacs")
19348 (setq files (mapcar 'file-name-sans-extension files))
19349 (setq files (mapcar
19350 (lambda (x) (if (string-match remove-re x) nil x))
19352 (setq files (delq nil files))
19355 (when (featurep (intern (file-name-nondirectory f)))
19356 (if (and (not uncompiled)
19357 (file-exists-p (concat f ".elc")))
19358 (load (concat f ".elc") nil nil t)
19359 (load (concat f ".el") nil nil t))))
19364 (defun org-customize ()
19365 "Call the customize function with org as argument."
19367 (org-load-modules-maybe)
19368 (org-require-autoloaded-modules)
19369 (customize-browse 'org))
19371 (defun org-create-customize-menu ()
19372 "Create a full customization menu for Org-mode, insert it into the menu."
19374 (org-load-modules-maybe)
19375 (org-require-autoloaded-modules)
19376 (if (fboundp 'customize-menu-create)
19379 '("Org") "Customize"
19380 `(["Browse Org group" org-customize t]
19382 ,(customize-menu-create 'org)
19383 ["Set" Custom-set t]
19384 ["Save" Custom-save t]
19385 ["Reset to Current" Custom-reset-current t]
19386 ["Reset to Saved" Custom-reset-saved t]
19387 ["Reset to Standard Settings" Custom-reset-standard t]))
19388 (message "\"Org\"-menu now contains full customization menu"))
19389 (error "Cannot expand menu (outdated version of cus-edit.el)")))
19391 ;;;; Miscellaneous stuff
19393 ;;; Generally useful functions
19395 (defun org-get-at-bol (property)
19396 "Get text property PROPERTY at beginning of line."
19397 (get-text-property (point-at-bol) property))
19399 (defun org-find-text-property-in-string (prop s)
19400 "Return the first non-nil value of property PROP in string S."
19401 (or (get-text-property 0 prop s)
19402 (get-text-property (or (next-single-property-change 0 prop s) 0)
19405 (defun org-display-warning (message) ;; Copied from Emacs-Muse
19406 "Display the given MESSAGE as a warning."
19407 (if (fboundp 'display-warning)
19408 (display-warning 'org message
19409 (if (featurep 'xemacs) 'warning :warning))
19410 (let ((buf (get-buffer-create "*Org warnings*")))
19411 (with-current-buffer buf
19412 (goto-char (point-max))
19413 (insert "Warning (Org): " message)
19416 (display-buffer buf)
19419 (defun org-eval (form)
19420 "Eval FORM and return result."
19421 (condition-case error
19423 (error (format "%%![Error: %s]" error))))
19425 (defun org-in-clocktable-p ()
19426 "Check if the cursor is in a clocktable."
19427 (let ((pos (point)) start)
19430 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
19431 (setq start (match-beginning 0))
19432 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
19433 (>= (match-end 0) pos)
19436 (defun org-in-commented-line ()
19437 "Is point in a line starting with `#'?"
19438 (equal (char-after (point-at-bol)) ?#))
19440 (defun org-in-indented-comment-line ()
19441 "Is point in a line starting with `#' after some white space?"
19444 (goto-char (point-at-bol))
19445 (looking-at "[ \t]*#"))))
19447 (defun org-in-verbatim-emphasis ()
19449 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
19451 (defun org-goto-marker-or-bmk (marker &optional bookmark)
19452 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
19453 (if (and marker (marker-buffer marker)
19454 (buffer-live-p (marker-buffer marker)))
19456 (org-pop-to-buffer-same-window (marker-buffer marker))
19457 (if (or (> marker (point-max)) (< marker (point-min)))
19460 (org-show-context 'org-goto))
19462 (bookmark-jump bookmark)
19463 (error "Cannot find location"))))
19465 (defun org-quote-csv-field (s)
19466 "Quote field for inclusion in CSV material."
19467 (if (string-match "[\",]" s)
19468 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
19471 (defun org-force-self-insert (N)
19472 "Needed to enforce self-insert under remapping."
19474 (self-insert-command N))
19476 (defun org-string-width (s)
19477 "Compute width of string, ignoring invisible characters.
19478 This ignores character with invisibility property `org-link', and also
19479 characters with property `org-cwidth', because these will become invisible
19480 upon the next fontification round."
19482 (when (or (eq t buffer-invisibility-spec)
19483 (assq 'org-link buffer-invisibility-spec))
19484 (while (setq b (text-property-any 0 (length s)
19485 'invisible 'org-link s))
19486 (setq s (concat (substring s 0 b)
19487 (substring s (or (next-single-property-change
19488 b 'invisible s) (length s)))))))
19489 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
19490 (setq s (concat (substring s 0 b)
19491 (substring s (or (next-single-property-change
19492 b 'org-cwidth s) (length s))))))
19493 (setq l (string-width s) b -1)
19494 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
19495 (setq l (- l (get-text-property b 'org-dwidth-n s))))
19498 (defun org-shorten-string (s maxlength)
19499 "Shorten string S so tht it is no longer than MAXLENGTH characters.
19500 If the string is shorter or has length MAXLENGTH, just return the
19501 original string. If it is longer, the functions finds a space in the
19502 string, breaks this string off at that locations and adds three dots
19503 as ellipsis. Including the ellipsis, the string will not be longer
19504 than MAXLENGTH. If finding a good breaking point in the string does
19505 not work, the string is just chopped off in the middle of a word
19507 (if (<= (length s) maxlength)
19509 (let* ((n (max (- maxlength 4) 1))
19510 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
19511 (if (string-match re s)
19512 (concat (match-string 1 s) "...")
19513 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
19515 (defun org-get-indentation (&optional line)
19516 "Get the indentation of the current line, interpreting tabs.
19517 When LINE is given, assume it represents a line and compute its indentation."
19519 (if (string-match "^ *" (org-remove-tabs line))
19522 (beginning-of-line 1)
19523 (skip-chars-forward " \t")
19524 (current-column))))
19526 (defun org-get-string-indentation (s)
19527 "What indentation has S due to SPACE and TAB at the beginning of the string?"
19528 (let ((n -1) (i 0) (w tab-width) c)
19530 (while (< (setq n (1+ n)) (length s))
19531 (setq c (aref s n))
19532 (cond ((= c ?\ ) (setq i (1+ i)))
19533 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
19534 (t (throw 'exit t)))))
19537 (defun org-remove-tabs (s &optional width)
19538 "Replace tabulators in S with spaces.
19539 Assumes that s is a single line, starting in column 0."
19540 (setq width (or width tab-width))
19541 (while (string-match "\t" s)
19542 (setq s (replace-match
19544 (- (* width (/ (+ (match-beginning 0) width) width))
19545 (match-beginning 0)) ?\ )
19549 (defun org-fix-indentation (line ind)
19550 "Fix indentation in LINE.
19551 IND is a cons cell with target and minimum indentation.
19552 If the current indentation in LINE is smaller than the minimum,
19553 leave it alone. If it is larger than ind, set it to the target."
19554 (let* ((l (org-remove-tabs line))
19555 (i (org-get-indentation l))
19556 (i1 (car ind)) (i2 (cdr ind)))
19557 (if (>= i i2) (setq l (substring line i2)))
19559 (concat (make-string i1 ?\ ) l)
19562 (defun org-remove-indentation (code &optional n)
19563 "Remove the maximum common indentation from the lines in CODE.
19564 N may optionally be the number of spaces to remove."
19567 (org-do-remove-indentation n)
19570 (defun org-do-remove-indentation (&optional n)
19571 "Remove the maximum common indentation from the buffer."
19572 (untabify (point-min) (point-max))
19573 (let ((min 10000) re)
19576 (goto-char (point-min))
19577 (while (re-search-forward "^ *[^ \n]" nil t)
19578 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
19579 (unless (or (= min 0) (= min 10000))
19580 (setq re (format "^ \\{%d\\}" min))
19581 (goto-char (point-min))
19582 (while (re-search-forward re nil t)
19587 (defun org-fill-template (template alist)
19588 "Find each %key of ALIST in TEMPLATE and replace it."
19589 (let ((case-fold-search nil)
19591 (setq alist (sort (copy-sequence alist)
19592 (lambda (a b) (< (length (car a)) (length (car b))))))
19593 (while (setq entry (pop alist))
19595 (replace-regexp-in-string
19596 (concat "%" (regexp-quote (car entry)))
19597 (cdr entry) template t t)))
19600 (defun org-base-buffer (buffer)
19601 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
19604 (or (buffer-base-buffer buffer)
19607 (defun org-trim (s)
19608 "Remove whitespace at beginning and end of string."
19609 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
19610 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
19613 (defun org-wrap (string &optional width lines)
19614 "Wrap string to either a number of lines, or a width in characters.
19615 If WIDTH is non-nil, the string is wrapped to that width, however many lines
19616 that costs. If there is a word longer than WIDTH, the text is actually
19617 wrapped to the length of that word.
19618 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
19619 many lines, whatever width that takes.
19620 The return value is a list of lines, without newlines at the end."
19621 (let* ((words (org-split-string string "[ \t\n]+"))
19622 (maxword (apply 'max (mapcar 'org-string-width words)))
19625 (org-do-wrap words (max maxword width)))
19628 (setq ll (org-do-wrap words maxword))
19629 (if (<= (length ll) lines)
19632 (while (> (length ll) lines)
19634 (setq ll (org-do-wrap words w)))
19636 (t (error "Cannot wrap this")))))
19638 (defun org-do-wrap (words width)
19639 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
19642 (setq line (pop words))
19643 (while (and words (< (+ (length line) (length (car words))) width))
19644 (setq line (concat line " " (pop words))))
19645 (setq lines (push line lines)))
19648 (defun org-split-string (string &optional separators)
19649 "Splits STRING into substrings at SEPARATORS.
19650 No empty strings are returned if there are matches at the beginning
19651 and end of string."
19652 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
19656 (while (and (string-match rexp string
19658 (= start (match-beginning 0))
19659 (< start (length string)))
19661 (< (match-beginning 0) (length string)))
19663 (or (eq (match-beginning 0) 0)
19664 (and (eq (match-beginning 0) (match-end 0))
19665 (eq (match-beginning 0) start))
19667 (cons (substring string start (match-beginning 0))
19669 (setq start (match-end 0)))
19670 (or (eq start (length string))
19672 (cons (substring string start)
19676 (defun org-quote-vert (s)
19677 "Replace \"|\" with \"\\vert\"."
19678 (while (string-match "|" s)
19679 (setq s (replace-match "\\vert" t t s)))
19682 (defun org-uuidgen-p (s)
19683 "Is S an ID created by UUIDGEN?"
19684 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
19686 (defun org-in-src-block-p nil
19687 "Whether point is in a code source block."
19689 (when (setq ov (overlays-at (point)))
19690 (memq 'org-block-background
19691 (overlay-properties
19694 (defun org-context ()
19695 "Return a list of contexts of the current cursor position.
19696 If several contexts apply, all are returned.
19697 Each context entry is a list with a symbol naming the context, and
19698 two positions indicating start and end of the context. Possible
19701 :headline anywhere in a headline
19702 :headline-stars on the leading stars in a headline
19703 :todo-keyword on a TODO keyword (including DONE) in a headline
19704 :tags on the TAGS in a headline
19705 :priority on the priority cookie in a headline
19706 :item on the first line of a plain list item
19707 :item-bullet on the bullet/number of a plain list item
19708 :checkbox on the checkbox in a plain list item
19709 :table in an org-mode table
19710 :table-special on a special filed in a table
19711 :table-table in a table.el table
19712 :clocktable in a clocktable
19713 :src-block in a source block
19714 :link on a hyperlink
19715 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
19716 :target on a <<target>>
19717 :radio-target on a <<<radio-target>>>
19718 :latex-fragment on a LaTeX fragment
19719 :latex-preview on a LaTeX fragment with overlaid preview image
19721 This function expects the position to be visible because it uses font-lock
19722 faces as a help to recognize the following contexts: :table-special, :link,
19724 (let* ((f (get-text-property (point) 'face))
19725 (faces (if (listp f) f (list f)))
19726 (case-fold-search t)
19727 (p (point)) clist o)
19728 ;; First the large context
19730 ((org-at-heading-p t)
19731 (push (list :headline (point-at-bol) (point-at-eol)) clist)
19733 (beginning-of-line 1)
19734 (looking-at org-todo-line-tags-regexp))
19735 (push (org-point-in-group p 1 :headline-stars) clist)
19736 (push (org-point-in-group p 2 :todo-keyword) clist)
19737 (push (org-point-in-group p 4 :tags) clist))
19739 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
19740 (if (looking-at "\\[#[A-Z0-9]\\]")
19741 (push (org-point-in-group p 0 :priority) clist)))
19744 (push (org-point-in-group p 2 :item-bullet) clist)
19745 (push (list :item (point-at-bol)
19746 (save-excursion (org-end-of-item) (point)))
19748 (and (org-at-item-checkbox-p)
19749 (push (org-point-in-group p 0 :checkbox) clist)))
19752 (push (list :table (org-table-begin) (org-table-end)) clist)
19753 (if (memq 'org-formula faces)
19754 (push (list :table-special
19755 (previous-single-property-change p 'face)
19756 (next-single-property-change p 'face)) clist)))
19757 ((org-at-table-p 'any)
19758 (push (list :table-table) clist)))
19761 ;; New the "medium" contexts: clocktables, source blocks
19762 (cond ((org-in-clocktable-p)
19763 (push (list :clocktable
19764 (and (or (looking-at "#\\+BEGIN: clocktable")
19765 (search-backward "#+BEGIN: clocktable" nil t))
19766 (match-beginning 0))
19767 (and (re-search-forward "#\\+END:?" nil t)
19768 (match-end 0))) clist))
19769 ((org-in-src-block-p)
19770 (push (list :src-block
19771 (and (or (looking-at "#\\+BEGIN_SRC")
19772 (search-backward "#+BEGIN_SRC" nil t))
19773 (match-beginning 0))
19774 (and (search-forward "#+END_SRC" nil t)
19775 (match-beginning 0))) clist)))
19778 ;; Now the small context
19780 ((org-at-timestamp-p)
19781 (push (org-point-in-group p 0 :timestamp) clist))
19782 ((memq 'org-link faces)
19784 (previous-single-property-change p 'face)
19785 (next-single-property-change p 'face)) clist))
19786 ((memq 'org-special-keyword faces)
19787 (push (list :keyword
19788 (previous-single-property-change p 'face)
19789 (next-single-property-change p 'face)) clist))
19791 (push (org-point-in-group p 0 :target) clist)
19792 (goto-char (1- (match-beginning 0)))
19793 (if (looking-at org-radio-target-regexp)
19794 (push (org-point-in-group p 0 :radio-target) clist))
19796 ((setq o (car (delq nil
19799 (if (memq x org-latex-fragment-image-overlays) x))
19800 (overlays-at (point))))))
19801 (push (list :latex-fragment
19802 (overlay-start o) (overlay-end o)) clist)
19803 (push (list :latex-preview
19804 (overlay-start o) (overlay-end o)) clist))
19805 ((org-inside-LaTeX-fragment-p)
19806 ;; FIXME: positions wrong.
19807 (push (list :latex-fragment (point) (point)) clist)))
19809 (setq clist (nreverse (delq nil clist)))
19812 ;; FIXME: Compare with at-regexp-p Do we need both?
19813 (defun org-in-regexp (re &optional nlines visually)
19814 "Check if point is inside a match of regexp.
19815 Normally only the current line is checked, but you can include NLINES extra
19816 lines both before and after point into the search.
19817 If VISUALLY is set, require that the cursor is not after the match but
19818 really on, so that the block visually is on the match."
19820 (let ((pos (point))
19821 (eol (point-at-eol (+ 1 (or nlines 0))))
19822 (inc (if visually 1 0)))
19824 (beginning-of-line (- 1 (or nlines 0)))
19825 (while (re-search-forward re eol t)
19826 (if (and (<= (match-beginning 0) pos)
19827 (>= (+ inc (match-end 0)) pos))
19828 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
19830 (defun org-at-regexp-p (regexp)
19831 "Is point inside a match of REGEXP in the current line?"
19834 (let ((pos (point)) (end (point-at-eol)))
19835 (beginning-of-line 1)
19836 (while (re-search-forward regexp end t)
19837 (if (and (<= (match-beginning 0) pos)
19838 (>= (match-end 0) pos))
19842 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
19843 "Non-nil when point is between matches of START-RE and END-RE.
19845 Also return a non-nil value when point is on one of the matches.
19847 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
19848 buffer positions. Default values are the positions of headlines
19849 surrounding the point.
19851 The functions returns a cons cell whose car (resp. cdr) is the
19852 position before START-RE (resp. after END-RE)."
19854 (let ((pos (point))
19855 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
19856 (limit-down (or lim-down (save-excursion (outline-next-heading))))
19859 ;; Point is on a block when on START-RE or if START-RE can be
19860 ;; found before it...
19861 (and (or (org-at-regexp-p start-re)
19862 (re-search-backward start-re limit-up t))
19863 (setq beg (match-beginning 0))
19864 ;; ... and END-RE after it...
19865 (goto-char (match-end 0))
19866 (re-search-forward end-re limit-down t)
19867 (> (setq end (match-end 0)) pos)
19868 ;; ... without another START-RE in-between.
19869 (goto-char (match-beginning 0))
19870 (not (re-search-backward start-re (1+ beg) t))
19872 (cons beg end))))))
19874 (defun org-in-block-p (names)
19875 "Non-nil when point belongs to a block whose name belongs to NAMES.
19877 NAMES is a list of strings containing names of blocks.
19879 Return first block name matched, or nil. Beware that in case of
19880 nested blocks, the returned name may not belong to the closest
19884 (let ((case-fold-search t)
19885 (lim-up (save-excursion (outline-previous-heading)))
19886 (lim-down (save-excursion (outline-next-heading))))
19887 (mapc (lambda (name)
19888 (let ((n (regexp-quote name)))
19889 (when (org-between-regexps-p
19890 (concat "^[ \t]*#\\+begin_" n)
19891 (concat "^[ \t]*#\\+end_" n)
19897 (defun org-occur-in-agenda-files (regexp &optional nlines)
19898 "Call `multi-occur' with buffers for all agenda files."
19899 (interactive "sOrg-files matching: \np")
19900 (let* ((files (org-agenda-files))
19901 (tnames (mapcar 'file-truename files))
19902 (extra org-agenda-text-search-extra-files)
19904 (when (eq (car extra) 'agenda-archives)
19905 (setq extra (cdr extra))
19906 (setq files (org-add-archive-files files)))
19907 (while (setq f (pop extra))
19908 (unless (member (file-truename f) tnames)
19909 (add-to-list 'files f 'append)
19910 (add-to-list 'tnames (file-truename f) 'append)))
19912 (mapcar (lambda (x)
19913 (with-current-buffer
19914 (or (get-file-buffer x) (find-file-noselect x))
19920 (if (boundp 'occur-mode-find-occurrence-hook)
19922 (add-hook 'occur-mode-find-occurrence-hook
19924 (when (eq major-mode 'org-mode)
19927 (defadvice occur-mode-goto-occurrence
19928 (after org-occur-reveal activate)
19929 (and (eq major-mode 'org-mode) (org-reveal)))
19930 (defadvice occur-mode-goto-occurrence-other-window
19931 (after org-occur-reveal activate)
19932 (and (eq major-mode 'org-mode) (org-reveal)))
19933 (defadvice occur-mode-display-occurrence
19934 (after org-occur-reveal activate)
19935 (when (eq major-mode 'org-mode)
19936 (let ((pos (occur-mode-find-occurrence)))
19937 (with-current-buffer (marker-buffer pos)
19942 (defun org-occur-link-in-agenda-files ()
19943 "Create a link and search for it in the agendas.
19944 The link is not stored in `org-stored-links', it is just created
19945 for the search purpose."
19947 (let ((link (condition-case nil
19948 (org-store-link nil)
19949 (error "Unable to create a link to here"))))
19950 (org-occur-in-agenda-files (regexp-quote link))))
19952 (defun org-uniquify (list)
19953 "Remove duplicate elements from LIST."
19955 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
19958 (defun org-delete-all (elts list)
19959 "Remove all elements in ELTS from LIST."
19961 (setq list (delete (pop elts) list)))
19964 (defun org-count (cl-item cl-seq)
19965 "Count the number of occurrences of ITEM in SEQ.
19966 Taken from `count' in cl-seq.el with all keyword arguments removed."
19967 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
19968 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
19969 (while (< cl-start cl-end)
19970 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
19971 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
19972 (setq cl-start (1+ cl-start)))
19975 (defun org-remove-if (predicate seq)
19976 "Remove everything from SEQ that fulfills PREDICATE."
19980 (if (not (funcall predicate e)) (push e res)))
19983 (defun org-remove-if-not (predicate seq)
19984 "Remove everything from SEQ that does not fulfill PREDICATE."
19988 (if (funcall predicate e) (push e res)))
19991 (defun org-reduce (cl-func cl-seq &rest cl-keys)
19992 "Reduce two-argument FUNCTION across SEQ.
19993 Taken from `reduce' in cl-seq.el with all keyword arguments but
19994 \":initial-value\" removed."
19995 (let ((cl-accum (cond ((memq :initial-value cl-keys)
19996 (cadr (memq :initial-value cl-keys)))
19997 (cl-seq (pop cl-seq))
19998 (t (funcall cl-func)))))
20000 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20003 (defun org-back-over-empty-lines ()
20004 "Move backwards over whitespace, to the beginning of the first empty line.
20005 Returns the number of empty lines passed."
20006 (let ((pos (point)))
20007 (if (cdr (assoc 'heading org-blank-before-new-entry))
20008 (skip-chars-backward " \t\n\r")
20010 (forward-line -1)))
20011 (beginning-of-line 2)
20012 (goto-char (min (point) pos))
20013 (count-lines (point) pos)))
20015 (defun org-skip-whitespace ()
20016 (skip-chars-forward " \t\n\r"))
20018 (defun org-point-in-group (point group &optional context)
20019 "Check if POINT is in match-group GROUP.
20020 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20021 match. If the match group does not exist or point is not inside it,
20023 (and (match-beginning group)
20024 (>= point (match-beginning group))
20025 (<= point (match-end group))
20027 (list context (match-beginning group) (match-end group))
20030 (defun org-switch-to-buffer-other-window (&rest args)
20031 "Switch to buffer in a second window on the current frame.
20032 In particular, do not allow pop-up frames.
20033 Returns the newly created buffer."
20034 (let (pop-up-frames special-display-buffer-names special-display-regexps
20035 special-display-function)
20036 (apply 'switch-to-buffer-other-window args)))
20038 (defun org-combine-plists (&rest plists)
20039 "Create a single property list from all plists in PLISTS.
20040 The process starts by copying the first list, and then setting properties
20041 from the other lists. Settings in the last list are the most significant
20042 ones and overrule settings in the other lists."
20043 (let ((rtn (copy-sequence (pop plists)))
20046 (setq ls (pop plists))
20048 (setq p (pop ls) v (pop ls))
20049 (setq rtn (plist-put rtn p v))))
20052 (defun org-move-line-down (arg)
20053 "Move the current line down. With prefix argument, move it past ARG lines."
20055 (let ((col (current-column))
20057 (beginning-of-line 1) (setq beg (point))
20058 (beginning-of-line 2) (setq end (point))
20059 (beginning-of-line (+ 1 arg))
20060 (setq pos (move-marker (make-marker) (point)))
20061 (insert (delete-and-extract-region beg end))
20063 (org-move-to-column col)))
20065 (defun org-move-line-up (arg)
20066 "Move the current line up. With prefix argument, move it past ARG lines."
20068 (let ((col (current-column))
20070 (beginning-of-line 1) (setq beg (point))
20071 (beginning-of-line 2) (setq end (point))
20072 (beginning-of-line (- arg))
20073 (setq pos (move-marker (make-marker) (point)))
20074 (insert (delete-and-extract-region beg end))
20076 (org-move-to-column col)))
20078 (defun org-replace-escapes (string table)
20079 "Replace %-escapes in STRING with values in TABLE.
20080 TABLE is an association list with keys like \"%a\" and string values.
20081 The sequences in STRING may contain normal field width and padding information,
20082 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20083 so values can contain further %-escapes if they are define later in TABLE."
20084 (let ((tbl (copy-alist table))
20085 (case-fold-search nil)
20088 (while (setq e (pop tbl))
20089 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20090 (when (and (cdr e) (string-match re (cdr e)))
20091 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20093 (add-text-properties 0 3 (list 'sref sref) safe)
20094 (setcdr e (replace-match safe t t (cdr e)))))
20095 (while (string-match re string)
20096 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20098 (setq string (replace-match rpl t t string))))
20099 (while (setq pchg (next-property-change pchg string))
20100 (let ((sref (get-text-property pchg 'sref string)))
20101 (when (and sref (string-match "SREF" string pchg))
20102 (setq string (replace-match sref t t string)))))
20105 (defun org-sublist (list start end)
20106 "Return a section of LIST, from START to END.
20107 Counting starts at 1."
20108 (let (rtn (c start))
20109 (setq list (nthcdr (1- start) list))
20110 (while (and list (<= c end))
20111 (push (pop list) rtn)
20115 (defun org-find-base-buffer-visiting (file)
20116 "Like `find-buffer-visiting' but always return the base buffer and
20117 not an indirect buffer."
20118 (let ((buf (or (get-file-buffer file)
20119 (find-buffer-visiting file))))
20121 (or (buffer-base-buffer buf) buf)
20124 (defun org-image-file-name-regexp (&optional extensions)
20125 "Return regexp matching the file names of images.
20126 If EXTENSIONS is given, only match these."
20127 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20128 (image-file-name-regexp)
20129 (let ((image-file-name-extensions
20131 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20132 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20134 (regexp-opt (nconc (mapcar 'upcase
20135 image-file-name-extensions)
20136 image-file-name-extensions)
20140 (defun org-file-image-p (file &optional extensions)
20141 "Return non-nil if FILE is an image."
20143 (string-match (org-image-file-name-regexp extensions) file)))
20145 (defun org-get-cursor-date ()
20146 "Return the date at cursor in as a time.
20147 This works in the calendar and in the agenda, anywhere else it just
20148 returns the current time."
20149 (let (date day defd)
20151 ((eq major-mode 'calendar-mode)
20152 (setq date (calendar-cursor-to-date)
20153 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20154 ((eq major-mode 'org-agenda-mode)
20155 (setq day (get-text-property (point) 'day))
20157 (setq date (calendar-gregorian-from-absolute day)
20158 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20160 (or defd (current-time))))
20162 (defvar org-agenda-action-marker (make-marker)
20163 "Marker pointing to the entry for the next agenda action.")
20165 (defun org-mark-entry-for-agenda-action ()
20166 "Mark the current entry as target of an agenda action.
20167 Agenda actions are actions executed from the agenda with the key `k',
20168 which make use of the date at the cursor."
20170 (move-marker org-agenda-action-marker
20171 (save-excursion (org-back-to-heading t) (point))
20174 "Entry marked for action; press `k' at desired date in agenda or calendar"))
20176 (defun org-mark-subtree ()
20177 "Mark the current subtree.
20178 This puts point at the start of the current subtree, and mark at the end.
20180 If point is in an inline task, mark that task instead."
20182 (let ((inline-task-p
20183 (and (featurep 'org-inlinetask)
20184 (org-inlinetask-in-task-p)))
20186 ;; Get beginning of subtree
20188 (inline-task-p (org-inlinetask-goto-beginning))
20189 ((org-at-heading-p) (beginning-of-line))
20190 (t (org-with-limited-levels (outline-previous-visible-heading 1))))
20194 (org-inlinetask-goto-end)
20195 (org-end-of-subtree))
20197 (push-mark (point) nil t)
20200 ;;; Paragraph filling stuff.
20201 ;; We want this to be just right, so use the full arsenal.
20203 (defun org-indent-line-function ()
20204 "Indent line depending on context."
20206 (let* ((pos (point))
20207 (itemp (org-at-item-p))
20208 (case-fold-search t)
20209 (org-drawer-regexp (or org-drawer-regexp "\000"))
20210 (inline-task-p (and (featurep 'org-inlinetask)
20211 (org-inlinetask-in-task-p)))
20212 (inline-re (and inline-task-p
20213 (org-inlinetask-outline-regexp)))
20215 (beginning-of-line 1)
20218 ((looking-at "# ") (setq column 0))
20220 ((looking-at org-outline-regexp) (setq column 0))
20222 ((looking-at "#\\+include:") (setq column 0))
20223 ;; Footnote definition
20224 ((looking-at org-footnote-definition-re) (setq column 0))
20225 ;; Literal examples
20226 ((looking-at "[ \t]*:\\( \\|$\\)")
20227 (setq column (org-get-indentation))) ; do nothing
20229 ((ignore-errors (goto-char (org-in-item-p)))
20230 (setq column (if itemp
20231 (org-get-indentation)
20232 (org-list-item-body-column (point))))
20235 ((and (looking-at "[ \t]*:END:")
20236 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20238 (goto-char (1- (match-beginning 1)))
20239 (setq column (current-column))))
20241 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20243 (re-search-backward
20244 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20245 (setq column (org-get-indentation (match-string 0))))
20246 ((and (not (looking-at "[ \t]*#\\+begin_"))
20247 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20249 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20251 (cond ((equal (downcase (match-string 1)) "src")
20252 ;; src blocks: let `org-edit-src-exit' handle them
20253 (org-get-indentation))
20254 ((equal (downcase (match-string 1)) "example")
20255 (max (org-get-indentation)
20256 (org-get-indentation (match-string 0))))
20258 (org-get-indentation (match-string 0))))))
20259 ;; This line has nothing special, look at the previous relevant
20260 ;; line to compute indentation
20262 (beginning-of-line 0)
20263 (while (and (not (bobp))
20264 (not (looking-at org-drawer-regexp))
20265 ;; When point started in an inline task, do not move
20266 ;; above task starting line.
20267 (not (and inline-task-p (looking-at inline-re)))
20268 ;; Skip drawers, blocks, empty lines, verbatim,
20269 ;; comments, tables, footnotes definitions, lists,
20271 (or (and (looking-at "[ \t]*:END:")
20272 (re-search-backward org-drawer-regexp nil t))
20273 (and (looking-at "[ \t]*#\\+end_")
20274 (re-search-backward "[ \t]*#\\+begin_"nil t))
20275 (looking-at "[ \t]*[\n:#|]")
20276 (looking-at org-footnote-definition-re)
20277 (and (ignore-errors (goto-char (org-in-item-p)))
20279 (org-list-get-top-point (org-list-struct))))
20280 (and (not inline-task-p)
20281 (featurep 'org-inlinetask)
20282 (org-inlinetask-in-task-p)
20283 (or (org-inlinetask-goto-beginning) t))))
20284 (beginning-of-line 0))
20286 ;; There was an heading above.
20287 ((looking-at "\\*+[ \t]+")
20288 (if (not org-adapt-indentation)
20290 (goto-char (match-end 0))
20291 (setq column (current-column))))
20292 ;; A drawer had started and is unfinished
20293 ((looking-at org-drawer-regexp)
20294 (goto-char (1- (match-beginning 1)))
20295 (setq column (current-column)))
20296 ;; Else, nothing noticeable found: get indentation and go on.
20297 (t (setq column (org-get-indentation))))))
20298 ;; Now apply indentation and move cursor accordingly
20300 (if (<= (current-column) (current-indentation))
20301 (org-indent-line-to column)
20302 (save-excursion (org-indent-line-to column)))
20303 ;; Special polishing for properties, see `org-property-format'
20304 (setq column (current-column))
20305 (beginning-of-line 1)
20307 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20308 (replace-match (concat (match-string 1)
20309 (format org-property-format
20310 (match-string 2) (match-string 3)))
20312 (org-move-to-column column)))
20314 (defun org-indent-drawer ()
20315 "Indent the drawer at point."
20318 (e (and (save-excursion (re-search-forward ":END:" nil t))
20323 (when (overlays-at (point))
20324 (member 'invisible (overlay-properties
20325 (car (overlays-at (point)))))))))
20326 (when folded (org-cycle))
20327 (indent-for-tab-command)
20328 (while (and (move-beginning-of-line 2) (< (point) e))
20329 (indent-for-tab-command))
20331 (when folded (org-cycle)))
20332 (message "Drawer at point indented"))
20334 (defun org-indent-block ()
20335 "Indent the block at point."
20338 (case-fold-search t)
20339 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20344 (when (overlays-at (point))
20345 (member 'invisible (overlay-properties
20346 (car (overlays-at (point)))))))))
20347 (when folded (org-cycle))
20348 (indent-for-tab-command)
20349 (while (and (move-beginning-of-line 2) (< (point) e))
20350 (indent-for-tab-command))
20352 (when folded (org-cycle)))
20353 (message "Block at point indented"))
20355 (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
20356 "Variable to store copy of `adaptive-fill-regexp'.
20357 Since `adaptive-fill-regexp' is set to never match, we need to
20358 store a backup of its value before entering `org-mode' so that
20359 the functionality can be provided as a fall-back.")
20361 (defun org-set-autofill-regexps ()
20363 ;; In the paragraph separator we include headlines, because filling
20364 ;; text in a line directly attached to a headline would otherwise
20365 ;; fill the headline as well.
20366 (org-set-local 'comment-start-skip "^#+[ \t]*")
20367 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
20368 ;; The paragraph starter includes hand-formatted lists.
20374 org-outline-regexp "\\|"
20376 (org-item-re) "\\|"
20379 "\\\\\\(begin\\|end\\|[][]\\)"))
20380 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
20381 ;; But only if the user has not turned off tables or fixed-width regions
20383 'auto-fill-inhibit-regexp
20384 (concat org-outline-regexp
20386 "\\|[ \t]*" org-keyword-time-regexp
20387 (if (or org-enable-table-editor org-enable-fixed-width-editor)
20390 (if org-enable-table-editor "|" "")
20391 (if org-enable-fixed-width-editor ":" "")
20393 ;; We use our own fill-paragraph function, to make sure that tables
20394 ;; and fixed-width regions are not wrapped. That function will pass
20395 ;; through to `fill-paragraph' when appropriate.
20396 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20397 ;; Prevent auto-fill from inserting unwanted new items.
20398 (if (boundp 'fill-nobreak-predicate)
20400 'fill-nobreak-predicate
20402 (append fill-nobreak-predicate
20403 '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
20404 ;; Adaptive filling: To get full control, first make sure that
20405 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
20406 (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
20407 (org-set-local 'org-adaptive-fill-regexp-backup
20408 adaptive-fill-regexp))
20409 (org-set-local 'adaptive-fill-regexp "\000")
20410 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20411 (org-set-local 'adaptive-fill-function
20412 'org-adaptive-fill-function)
20414 'align-mode-rules-list
20415 '((org-in-buffer-settings
20416 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
20417 (modes . '(org-mode))))))
20419 (defun org-fill-item-nobreak-p ()
20420 "Non-nil when a line break at point would insert a new item."
20421 (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
20423 (defun org-fill-line-break-nobreak-p ()
20424 "Non-nil when a line break at point would create an Org line break."
20426 (skip-chars-backward "[ \t]")
20427 (skip-chars-backward "\\\\")
20428 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
20430 (defun org-fill-paragraph (&optional justify)
20431 "Re-align a table, pass through to fill-paragraph if no table."
20432 (let ((table-p (org-at-table-p))
20433 (table.el-p (org-at-table.el-p))
20434 (itemp (org-in-item-p)))
20435 (cond ((and (equal (char-after (point-at-bol)) ?*)
20436 (save-excursion (goto-char (point-at-bol))
20437 (looking-at org-outline-regexp)))
20438 t) ; skip headlines
20439 (table.el-p t) ; skip table.el tables
20440 (table-p (org-table-align) t) ; align Org tables
20441 (itemp ; align text in items
20442 (let* ((struct (save-excursion (goto-char itemp)
20443 (org-list-struct)))
20444 (parents (org-list-parents-alist struct))
20445 (children (org-list-get-children itemp struct parents))
20446 beg end prev next prefix)
20447 ;; Determine in which part of item point is: before
20448 ;; first child, after last child, between two
20449 ;; sub-lists, or simply in item if there's no child.
20452 (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
20454 end (org-list-get-item-end itemp struct)))
20455 ((< (point) (setq next (car children)))
20456 (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
20459 ((> (point) (setq prev (car (last children))))
20460 (setq beg (org-list-get-item-end prev struct)
20461 end (org-list-get-item-end itemp struct)
20462 prefix (save-excursion
20464 (skip-chars-forward " \t")
20465 (make-string (current-column) ?\ ))))
20467 (while (setq next (pop children))
20468 (if (> (point) next)
20470 (setq beg (org-list-get-item-end prev struct)
20472 prefix (save-excursion
20474 (skip-chars-forward " \t")
20475 (make-string (current-column) ?\ )))
20476 (throw 'exit nil))))))
20477 ;; Use `fill-paragraph' with buffer narrowed to item
20478 ;; without any child, and with our computed PREFIX.
20479 (flet ((fill-context-prefix (from to &optional flr) prefix))
20481 (narrow-to-region beg end)
20482 (save-excursion (fill-paragraph justify)))) t))
20483 ;; Special case where point is not in a list but is on
20484 ;; a paragraph adjacent to a list: make sure this paragraph
20485 ;; doesn't get merged with the end of the list by narrowing
20487 ((save-excursion (forward-paragraph -1)
20488 (setq itemp (org-in-item-p)))
20489 (let ((struct (save-excursion (goto-char itemp)
20490 (org-list-struct))))
20492 (narrow-to-region (org-list-get-bottom-point struct)
20493 (save-excursion (forward-paragraph 1)
20495 (fill-paragraph justify) t)))
20496 ;; Else simply call `fill-paragraph'.
20499 ;; For reference, this is the default value of adaptive-fill-regexp
20500 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
20502 (defun org-adaptive-fill-function ()
20503 "Return a fill prefix for org-mode files."
20508 ((looking-at "#[ \t]+")
20509 (match-string-no-properties 0))
20512 (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
20513 ;; Point is in a list after `backward-paragraph': original
20514 ;; point wasn't in the list, or filling would have been taken
20515 ;; care of by `org-auto-fill-function', but the list and the
20516 ;; real paragraph are not separated by a blank line. Thus, move
20517 ;; point after the list to go back to real paragraph and
20518 ;; determine fill-prefix.
20519 ((setq itemp (org-in-item-p))
20521 (let* ((struct (org-list-struct))
20522 (bottom (org-list-get-bottom-point struct)))
20524 (make-string (org-get-indentation) ?\ )))
20526 ((looking-at org-adaptive-fill-regexp-backup)
20527 (match-string-no-properties 0))))))
20529 (defun org-auto-fill-function ()
20530 "Auto-fill function."
20531 (let (itemp prefix)
20532 ;; When in a list, compute an appropriate fill-prefix and make
20533 ;; sure it will be used by `do-auto-fill'.
20534 (if (setq itemp (org-in-item-p))
20536 (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
20537 (flet ((fill-context-prefix (from to &optional flr) prefix))
20539 ;; Else just use `do-auto-fill'.
20544 (defun org-toggle-fixed-width-section (arg)
20545 "Toggle the fixed-width export.
20546 If there is no active region, the QUOTE keyword at the current headline is
20547 inserted or removed. When present, it causes the text between this headline
20548 and the next to be exported as fixed-width text, and unmodified.
20549 If there is an active region, this command adds or removes a colon as the
20550 first character of this line. If the first character of a line is a colon,
20551 this line is also exported in fixed-width font."
20554 (regionp (org-region-active-p))
20555 (beg (if regionp (region-beginning) (point)))
20556 (end (if regionp (region-end)))
20557 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
20558 (case-fold-search nil)
20559 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
20564 (setq cc (current-column))
20565 (beginning-of-line 1)
20566 (setq off (looking-at re))
20567 (while (> nlines 0)
20568 (setq nlines (1- nlines))
20569 (beginning-of-line 1)
20572 (org-move-to-column cc t)
20575 ((and off (looking-at re))
20576 (replace-match "" t t nil 1))
20577 ((not off) (org-move-to-column cc t) (insert ": ")))
20580 (org-back-to-heading)
20582 ((looking-at (format org-heading-keyword-regexp-format
20584 (goto-char (match-end 1))
20585 (looking-at (concat " +" org-quote-string))
20586 (replace-match "" t t)
20587 (when (eolp) (insert " ")))
20588 ((looking-at org-outline-regexp)
20589 (goto-char (match-end 0))
20590 (insert org-quote-string " ")))))))
20592 (defun org-reftex-citation ()
20593 "Use reftex-citation to insert a citation into the buffer.
20594 This looks for a line like
20596 #+BIBLIOGRAPHY: foo plain option:-d
20598 and derives from it that foo.bib is the bibliography file relevant
20599 for this document. It then installs the necessary environment for RefTeX
20600 to work in this buffer and calls `reftex-citation' to insert a citation
20603 Export of such citations to both LaTeX and HTML is handled by the contributed
20604 package org-exp-bibtex by Taru Karttunen."
20606 (let ((reftex-docstruct-symbol 'rds)
20607 (reftex-cite-format "\\cite{%l}")
20612 (let ((case-fold-search t)
20613 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
20614 (if (not (save-excursion
20615 (or (re-search-forward re nil t)
20616 (re-search-backward re nil t))))
20617 (error "No bibliography defined in file")
20618 (setq bib (concat (match-string 1) ".bib")
20619 rds (list (list 'bib bib)))))))
20620 (call-interactively 'reftex-citation)))
20622 ;;;; Functions extending outline functionality
20624 (defun org-beginning-of-line (&optional arg)
20625 "Go to the beginning of the current line. If that is invisible, continue
20626 to a visible line beginning. This makes the function of C-a more intuitive.
20627 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
20628 first attempt, and only move to after the tags when the cursor is already
20629 beyond the end of the headline."
20631 (let ((pos (point))
20632 (special (if (consp org-special-ctrl-a/e)
20633 (car org-special-ctrl-a/e)
20634 org-special-ctrl-a/e))
20636 (if (org-bound-and-true-p line-move-visual)
20637 (beginning-of-visual-line 1)
20638 (beginning-of-line 1))
20639 (if (and arg (fboundp 'move-beginning-of-line))
20640 (call-interactively 'move-beginning-of-line)
20644 (if (org-truely-invisible-p)
20645 (while (and (not (bobp)) (org-truely-invisible-p))
20647 (beginning-of-line 1))
20648 (forward-char 1))))
20651 ((and (looking-at org-complex-heading-regexp)
20652 (= (char-after (match-end 1)) ?\ ))
20653 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
20657 (cond ((> pos refpos) refpos)
20658 ((= pos (point)) refpos)
20660 (cond ((> pos (point)) (point))
20661 ((not (eq last-command this-command)) (point))
20664 ;; Being at an item and not looking at an the item means point
20665 ;; was previously moved to beginning of a visual line, whiche
20666 ;; doesn't contain the item. Therefore, do nothing special,
20668 (when (looking-at org-list-full-item-re)
20669 ;; Set special position at first white space character after
20670 ;; bullet, and check-box, if any.
20671 (let ((after-bullet
20672 (let ((box (match-end 3)))
20673 (if (not box) (match-end 1)
20674 (let ((after (char-after box)))
20675 (if (and after (= after ? )) (1+ box) box))))))
20676 ;; Special case: Move point to special position when
20677 ;; currently after it or at beginning of line.
20679 (when (or (> pos after-bullet) (= (point) pos))
20680 (goto-char after-bullet))
20681 ;; Reversed case: Move point to special position when
20682 ;; point was already at beginning of line and command is
20684 (when (and (= (point) pos) (eq last-command this-command))
20685 (goto-char after-bullet))))))))
20687 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
20689 (defun org-end-of-line (&optional arg)
20690 "Go to the end of the line.
20691 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
20692 first attempt, and only move to after the tags when the cursor is already
20693 beyond the end of the headline."
20695 (let ((special (if (consp org-special-ctrl-a/e)
20696 (cdr org-special-ctrl-a/e)
20697 org-special-ctrl-a/e)))
20699 ((or (not special) arg
20700 (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
20701 (call-interactively
20702 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
20703 ((fboundp 'move-end-of-line) 'move-end-of-line)
20704 (t 'end-of-line))))
20705 ((org-at-heading-p)
20706 (let ((pos (point)))
20707 (beginning-of-line 1)
20708 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
20710 (if (or (< pos (match-beginning 1))
20711 (= pos (match-end 0)))
20712 (goto-char (match-beginning 1))
20713 (goto-char (match-end 0)))
20714 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
20715 (goto-char (match-end 0))
20716 (goto-char (match-beginning 1))))
20717 (call-interactively (if (fboundp 'move-end-of-line)
20721 (move-end-of-line 1)
20722 (when (overlays-at (1- (point))) (backward-char 1)))
20723 ;; At an item: Move before any hidden text.
20724 (t (call-interactively
20725 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
20726 ((fboundp 'move-end-of-line) 'move-end-of-line)
20727 (t 'end-of-line)))))
20729 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
20731 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
20732 (define-key org-mode-map "\C-e" 'org-end-of-line)
20734 (defun org-backward-sentence (&optional arg)
20735 "Go to beginning of sentence, or beginning of table field.
20736 This will call `backward-sentence' or `org-table-beginning-of-field',
20737 depending on context."
20740 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
20741 (t (call-interactively 'backward-sentence))))
20743 (defun org-forward-sentence (&optional arg)
20744 "Go to end of sentence, or end of table field.
20745 This will call `forward-sentence' or `org-table-end-of-field',
20746 depending on context."
20749 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
20750 (t (call-interactively 'forward-sentence))))
20752 (define-key org-mode-map "\M-a" 'org-backward-sentence)
20753 (define-key org-mode-map "\M-e" 'org-forward-sentence)
20755 (defun org-kill-line (&optional arg)
20756 "Kill line, to tags or end of line."
20759 ((or (not org-special-ctrl-k)
20761 (not (org-at-heading-p)))
20762 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
20763 org-ctrl-k-protect-subtree)
20764 (if (or (eq org-ctrl-k-protect-subtree 'error)
20765 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
20766 (error "C-k aborted - would kill hidden subtree")))
20767 (call-interactively
20768 (if visual-line-mode 'kill-visual-line 'kill-line)))
20769 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
20770 (kill-region (point) (match-beginning 1))
20771 (org-set-tags nil t))
20772 (t (kill-region (point) (point-at-eol)))))
20774 (define-key org-mode-map "\C-k" 'org-kill-line)
20776 (defun org-yank (&optional arg)
20777 "Yank. If the kill is a subtree, treat it specially.
20778 This command will look at the current kill and check if is a single
20779 subtree, or a series of subtrees[1]. If it passes the test, and if the
20780 cursor is at the beginning of a line or after the stars of a currently
20781 empty headline, then the yank is handled specially. How exactly depends
20782 on the value of the following variables, both set by default.
20784 org-yank-folded-subtrees
20785 When set, the subtree(s) will be folded after insertion, but only
20786 if doing so would now swallow text after the yanked text.
20788 org-yank-adjusted-subtrees
20789 When set, the subtree will be promoted or demoted in order to
20790 fit into the local outline tree structure, which means that the level
20791 will be adjusted so that it becomes the smaller one of the two
20792 *visible* surrounding headings.
20794 Any prefix to this command will cause `yank' to be called directly with
20795 no special treatment. In particular, a simple \\[universal-argument] prefix \
20797 plainly yank the text as it is.
20799 \[1] The test checks if the first non-white line is a heading
20800 and if there are no other headings with fewer stars."
20802 (org-yank-generic 'yank arg))
20804 (defun org-yank-generic (command arg)
20805 "Perform some yank-like command.
20807 This function implements the behavior described in the `org-yank'
20808 documentation. However, it has been generalized to work for any
20809 interactive command with similar behavior."
20811 ;; pretend to be command COMMAND
20812 (setq this-command command)
20815 (call-interactively command)
20817 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
20818 (and (org-kill-is-subtree-p)
20820 (and (looking-at "[ \t]*$")
20823 (buffer-substring (point-at-bol) (point)))))))
20826 ((and subtreep org-yank-folded-subtrees)
20827 (let ((beg (point))
20829 (if (and subtreep org-yank-adjusted-subtrees)
20830 (org-paste-subtree nil nil 'for-yank)
20831 (call-interactively command))
20835 (when (and (bolp) subtreep
20836 (not (setq swallowp
20837 (org-yank-folding-would-swallow-text beg end))))
20838 (org-with-limited-levels
20839 (or (looking-at org-outline-regexp)
20840 (re-search-forward org-outline-regexp-bol end t))
20841 (while (and (< (point) end) (looking-at org-outline-regexp))
20843 (org-cycle-show-empty-lines 'folded)
20844 (condition-case nil
20845 (outline-forward-same-level 1)
20846 (error (goto-char end))))))
20849 "Inserted text not folded because that would swallow text"))
20852 (skip-chars-forward " \t\n\r")
20853 (beginning-of-line 1)
20854 (push-mark beg 'nomsg)))
20855 ((and subtreep org-yank-adjusted-subtrees)
20856 (let ((beg (point-at-bol)))
20857 (org-paste-subtree nil nil 'for-yank)
20858 (push-mark beg 'nomsg)))
20860 (call-interactively command))))))
20862 (defun org-yank-folding-would-swallow-text (beg end)
20863 "Would hide-subtree at BEG swallow any text after END?"
20865 (org-with-limited-levels
20868 (when (or (looking-at org-outline-regexp)
20869 (re-search-forward org-outline-regexp-bol end t))
20870 (setq level (org-outline-level)))
20872 (skip-chars-forward " \t\r\n\v\f")
20874 (and (bolp) (looking-at org-outline-regexp)
20875 (<= (org-outline-level) level)))
20876 nil ; Nothing would be swallowed
20877 t))))) ; something would swallow
20879 (define-key org-mode-map "\C-y" 'org-yank)
20881 (defun org-truely-invisible-p ()
20882 "Check if point is at a character currently not visible.
20883 This version does not only check the character property, but also
20885 ;; Early versions of noutline don't have `outline-invisible-p'.
20886 (if (org-bound-and-true-p visible-mode)
20888 (outline-invisible-p)))
20890 (defun org-invisible-p2 ()
20891 "Check if point is at a character currently not visible."
20893 (if (and (eolp) (not (bobp))) (backward-char 1))
20894 ;; Early versions of noutline don't have `outline-invisible-p'.
20895 (outline-invisible-p)))
20897 (defun org-back-to-heading (&optional invisible-ok)
20898 "Call `outline-back-to-heading', but provide a better error message."
20899 (condition-case nil
20900 (outline-back-to-heading invisible-ok)
20901 (error (error "Before first headline at position %d in buffer %s"
20902 (point) (current-buffer)))))
20904 (defun org-beginning-of-defun ()
20905 "Go to the beginning of the subtree, i.e. back to the heading."
20906 (org-back-to-heading))
20907 (defun org-end-of-defun ()
20908 "Go to the end of the subtree."
20909 (org-end-of-subtree nil t))
20911 (defun org-before-first-heading-p ()
20912 "Before first heading?"
20915 (null (re-search-backward org-outline-regexp-bol nil t))))
20917 (defun org-at-heading-p (&optional ignored)
20918 (outline-on-heading-p t))
20919 ;; Compatibility alias with Org versions < 7.8.03
20920 (defalias 'org-on-heading-p 'org-at-heading-p)
20922 (defun org-at-drawer-p nil
20923 "Is cursor at a drawer keyword?"
20925 (move-beginning-of-line 1)
20926 (looking-at org-drawer-regexp)))
20928 (defun org-at-block-p nil
20929 "Is cursor at a block keyword?"
20931 (move-beginning-of-line 1)
20932 (looking-at org-block-regexp)))
20934 (defun org-point-at-end-of-empty-headline ()
20935 "If point is at the end of an empty headline, return t, else nil.
20936 If the heading only contains a TODO keyword, it is still still considered
20938 (and (looking-at "[ \t]*$")
20940 (beginning-of-line 1)
20941 (let ((case-fold-search nil))
20942 (looking-at org-todo-line-regexp)))
20943 (string= (match-string 3) "")))
20945 (defun org-at-heading-or-item-p ()
20946 (or (org-at-heading-p) (org-at-item-p)))
20948 (defun org-at-target-p ()
20949 (or (org-in-regexp org-radio-target-regexp)
20950 (org-in-regexp org-target-regexp)))
20951 ;; Compatibility alias with Org versions < 7.8.03
20952 (defalias 'org-on-target-p 'org-at-target-p)
20954 (defun org-up-heading-all (arg)
20955 "Move to the heading line of which the present line is a subheading.
20956 This function considers both visible and invisible heading lines.
20957 With argument, move up ARG levels."
20958 (if (fboundp 'outline-up-heading-all)
20959 (outline-up-heading-all arg) ; emacs 21 version of outline.el
20960 (outline-up-heading arg t))) ; emacs 22 version of outline.el
20962 (defun org-up-heading-safe ()
20963 "Move to the heading line of which the present line is a subheading.
20964 This version will not throw an error. It will return the level of the
20965 headline found, or nil if no higher level is found.
20967 Also, this function will be a lot faster than `outline-up-heading',
20968 because it relies on stars being the outline starters. This can really
20969 make a significant difference in outlines with very many siblings."
20970 (let (start-level re)
20971 (org-back-to-heading t)
20972 (setq start-level (funcall outline-level))
20973 (if (equal start-level 1)
20975 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
20976 (if (re-search-backward re nil t)
20977 (funcall outline-level)))))
20979 (defun org-first-sibling-p ()
20980 "Is this heading the first child of its parents?"
20982 (let ((re org-outline-regexp-bol)
20984 (unless (org-at-heading-p t)
20985 (error "Not at a heading"))
20986 (setq level (funcall outline-level))
20988 (if (not (re-search-backward re nil t))
20990 (setq l (funcall outline-level))
20993 (defun org-goto-sibling (&optional previous)
20994 "Goto the next sibling, even if it is invisible.
20995 When PREVIOUS is set, go to the previous sibling instead. Returns t
20996 when a sibling was found. When none is found, return nil and don't
20998 (let ((fun (if previous 're-search-backward 're-search-forward))
21000 (re org-outline-regexp-bol)
21002 (when (condition-case nil (org-back-to-heading t) (error nil))
21003 (setq level (funcall outline-level))
21005 (or previous (forward-char 1))
21006 (while (funcall fun re nil t)
21007 (setq l (funcall outline-level))
21008 (when (< l level) (goto-char pos) (throw 'exit nil))
21009 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21013 (defun org-show-siblings ()
21014 "Show all siblings of the current headline."
21016 (while (org-goto-sibling) (org-flag-heading nil)))
21018 (while (org-goto-sibling 'previous)
21019 (org-flag-heading nil))))
21021 (defun org-goto-first-child ()
21022 "Goto the first child, even if it is invisible.
21023 Return t when a child was found. Otherwise don't move point and
21025 (let (level (pos (point)) (re org-outline-regexp-bol))
21026 (when (condition-case nil (org-back-to-heading t) (error nil))
21027 (setq level (outline-level))
21029 (if (and (re-search-forward re nil t) (> (outline-level) level))
21030 (progn (goto-char (match-beginning 0)) t)
21031 (goto-char pos) nil))))
21033 (defun org-show-hidden-entry ()
21034 "Show an entry where even the heading is hidden."
21038 (defun org-flag-heading (flag &optional entry)
21039 "Flag the current heading. FLAG non-nil means make invisible.
21040 When ENTRY is non-nil, show the entire entry."
21042 (org-back-to-heading t)
21043 ;; Check if we should show the entire entry
21048 (and (outline-next-heading)
21049 (org-flag-heading nil))))
21050 (outline-flag-region (max (point-min) (1- (point)))
21051 (save-excursion (outline-end-of-heading) (point))
21054 (defun org-get-next-sibling ()
21055 "Move to next heading of the same level, and return point.
21056 If there is no such heading, return nil.
21057 This is like outline-next-sibling, but invisible headings are ok."
21058 (let ((level (funcall outline-level)))
21059 (outline-next-heading)
21060 (while (and (not (eobp)) (> (funcall outline-level) level))
21061 (outline-next-heading))
21062 (if (or (eobp) (< (funcall outline-level) level))
21066 (defun org-get-last-sibling ()
21067 "Move to previous heading of the same level, and return point.
21068 If there is no such heading, return nil."
21069 (let ((opoint (point))
21070 (level (funcall outline-level)))
21071 (outline-previous-heading)
21072 (when (and (/= (point) opoint) (outline-on-heading-p t))
21073 (while (and (> (funcall outline-level) level)
21075 (outline-previous-heading))
21076 (if (< (funcall outline-level) level)
21080 (defun org-end-of-subtree (&optional invisible-OK to-heading)
21081 ;; This contains an exact copy of the original function, but it uses
21082 ;; `org-back-to-heading', to make it work also in invisible
21083 ;; trees. And is uses an invisible-OK argument.
21084 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21085 ;; Furthermore, when used inside Org, finding the end of a large subtree
21086 ;; with many children and grandchildren etc, this can be much faster
21087 ;; than the outline version.
21088 (org-back-to-heading invisible-OK)
21090 (level (funcall outline-level)))
21091 (if (and (eq major-mode 'org-mode) (< level 1000))
21092 ;; A true heading (not a plain list item), in Org-mode
21093 ;; This means we can easily find the end by looking
21094 ;; only for the right number of stars. Using a regexp to do
21095 ;; this is so much faster than using a Lisp loop.
21096 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21098 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21099 ;; something else, do it the slow way
21100 (while (and (not (eobp))
21101 (or first (> (funcall outline-level) level)))
21103 (outline-next-heading)))
21105 (if (memq (preceding-char) '(?\n ?\^M))
21107 ;; Go to end of line before heading
21109 (if (memq (preceding-char) '(?\n ?\^M))
21110 ;; leave blank line before heading
21111 (forward-char -1))))))
21114 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21115 "Use Org version in org-mode, for dramatic speed-up."
21116 (if (eq major-mode 'org-mode)
21118 (org-end-of-subtree nil t)
21119 (unless (eobp) (backward-char 1)))
21122 (defun org-end-of-meta-data-and-drawers ()
21123 "Jump to the first text after meta data and drawers in the current entry.
21124 This will move over empty lines, lines with planning time stamps,
21125 clocking lines, and drawers."
21126 (org-back-to-heading t)
21127 (let ((end (save-excursion (outline-next-heading) (point)))
21128 (re (concat "\\(" org-drawer-regexp "\\)"
21129 "\\|" "[ \t]*" org-keyword-time-regexp)))
21131 (while (re-search-forward re end t)
21132 (if (not (match-end 1))
21133 ;; empty or planning line
21135 ;; a drawer, find the end
21136 (re-search-forward "^[ \t]*:END:" end 'move)
21138 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21141 (defun org-forward-same-level (arg &optional invisible-ok)
21142 "Move forward to the arg'th subheading at same level as this one.
21143 Stop at the first and last subheadings of a superior heading.
21144 Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
21145 it wil also look at invisible ones."
21147 (org-back-to-heading invisible-ok)
21149 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21150 (re (format "^\\*\\{1,%d\\} " level))
21154 (while (and (re-search-forward re nil 'move)
21155 (setq l (- (match-end 0) (match-beginning 0) 1))
21158 (progn (backward-char 1) (outline-invisible-p)))
21159 (if (< l level) (setq arg 1)))
21160 (setq arg (1- arg)))
21161 (beginning-of-line 1)))
21163 (defun org-backward-same-level (arg &optional invisible-ok)
21164 "Move backward to the arg'th subheading at same level as this one.
21165 Stop at the first and last subheadings of a superior heading."
21167 (org-back-to-heading)
21169 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21170 (re (format "^\\*\\{1,%d\\} " level))
21173 (while (and (re-search-backward re nil 'move)
21174 (setq l (- (match-end 0) (match-beginning 0) 1))
21177 (outline-invisible-p))
21178 (if (< l level) (setq arg 1)))
21179 (setq arg (1- arg)))))
21181 (defun org-show-subtree ()
21182 "Show everything after this heading at deeper levels."
21183 (outline-flag-region
21186 (org-end-of-subtree t t))
21189 (defun org-show-entry ()
21190 "Show the body directly following this heading.
21191 Show the heading too, if it is currently invisible."
21194 (condition-case nil
21196 (org-back-to-heading t)
21197 (outline-flag-region
21198 (max (point-min) (1- (point)))
21200 (if (re-search-forward
21201 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
21202 (match-beginning 1)
21205 (org-cycle-hide-drawers 'children))
21208 (defun org-make-options-regexp (kwds &optional extra)
21209 "Make a regular expression for keyword lines."
21213 (mapconcat 'regexp-quote kwds "\\|")
21214 (if extra (concat "\\|" extra))
21218 ;; Make isearch reveal the necessary context
21219 (defun org-isearch-end ()
21220 "Reveal context after isearch exits."
21221 (when isearch-success ; only if search was successful
21222 (if (featurep 'xemacs)
21223 ;; Under XEmacs, the hook is run in the correct place,
21224 ;; we directly show the context.
21225 (org-show-context 'isearch)
21226 ;; In Emacs the hook runs *before* restoring the overlays.
21227 ;; So we have to use a one-time post-command-hook to do this.
21228 ;; (Emacs 22 has a special variable, see function `org-mode')
21229 (unless (and (boundp 'isearch-mode-end-hook-quit)
21230 isearch-mode-end-hook-quit)
21231 ;; Only when the isearch was not quitted.
21232 (org-add-hook 'post-command-hook 'org-isearch-post-command
21233 'append 'local)))))
21235 (defun org-isearch-post-command ()
21236 "Remove self from hook, and show context."
21237 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
21238 (org-show-context 'isearch))
21241 ;;;; Integration with and fixes for other packages
21245 (defvar org-imenu-markers nil
21246 "All markers currently used by Imenu.")
21247 (make-variable-buffer-local 'org-imenu-markers)
21249 (defun org-imenu-new-marker (&optional pos)
21250 "Return a new marker for use by Imenu, and remember the marker."
21251 (let ((m (make-marker)))
21252 (move-marker m (or pos (point)))
21253 (push m org-imenu-markers)
21256 (defun org-imenu-get-tree ()
21257 "Produce the index for Imenu."
21258 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
21259 (setq org-imenu-markers nil)
21260 (let* ((n org-imenu-depth)
21261 (re (concat "^" (org-get-limited-outline-regexp)))
21262 (subs (make-vector (1+ n) nil))
21268 (goto-char (point-max))
21269 (while (re-search-backward re nil t)
21270 (setq level (org-reduced-level (funcall outline-level)))
21272 (looking-at org-complex-heading-regexp)
21273 (setq head (org-link-display-format
21274 (org-match-string-no-properties 4))
21275 m (org-imenu-new-marker))
21276 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
21277 (if (>= level last-level)
21278 (push (cons head m) (aref subs level))
21279 (push (cons head (aref subs (1+ level))) (aref subs level))
21280 (loop for i from (1+ level) to n do (aset subs i nil)))
21281 (setq last-level level)))))
21284 (eval-after-load "imenu"
21286 (add-hook 'imenu-after-jump-hook
21288 (if (eq major-mode 'org-mode)
21289 (org-show-context 'org-goto))))))
21291 (defun org-link-display-format (link)
21292 "Replace a link with either the description, or the link target
21293 if no description is present"
21295 (if (string-match org-bracket-link-analytic-regexp link)
21296 (replace-match (if (match-end 5)
21297 (match-string 5 link)
21298 (concat (match-string 1 link)
21299 (match-string 3 link)))
21303 (defun org-toggle-link-display ()
21304 "Toggle the literal or descriptive display of links."
21306 (if org-descriptive-links
21307 (progn (org-remove-from-invisibility-spec '(org-link))
21308 (org-restart-font-lock)
21309 (setq org-descriptive-links nil))
21310 (progn (add-to-invisibility-spec '(org-link))
21311 (org-restart-font-lock)
21312 (setq org-descriptive-links t))))
21314 ;; Speedbar support
21316 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
21317 "Overlay marking the agenda restriction line in speedbar.")
21318 (overlay-put org-speedbar-restriction-lock-overlay
21319 'face 'org-agenda-restriction-lock)
21320 (overlay-put org-speedbar-restriction-lock-overlay
21321 'help-echo "Agendas are currently limited to this item.")
21322 (org-detach-overlay org-speedbar-restriction-lock-overlay)
21324 (defun org-speedbar-set-agenda-restriction ()
21325 "Restrict future agenda commands to the location at point in speedbar.
21326 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
21328 (require 'org-agenda)
21329 (let (p m tp np dir txt)
21331 ((setq p (text-property-any (point-at-bol) (point-at-eol)
21333 (setq m (get-text-property p 'org-imenu-marker))
21334 (with-current-buffer (marker-buffer m)
21336 (org-agenda-set-restriction-lock 'subtree)))
21337 ((setq p (text-property-any (point-at-bol) (point-at-eol)
21338 'speedbar-function 'speedbar-find-file))
21339 (setq tp (previous-single-property-change
21340 (1+ p) 'speedbar-function)
21341 np (next-single-property-change
21342 tp 'speedbar-function)
21343 dir (speedbar-line-directory)
21344 txt (buffer-substring-no-properties (or tp (point-min))
21345 (or np (point-max))))
21346 (with-current-buffer (find-file-noselect
21347 (let ((default-directory dir))
21348 (expand-file-name txt)))
21349 (unless (eq major-mode 'org-mode)
21350 (error "Cannot restrict to non-Org-mode file"))
21351 (org-agenda-set-restriction-lock 'file)))
21352 (t (error "Don't know how to restrict Org-mode's agenda")))
21353 (move-overlay org-speedbar-restriction-lock-overlay
21354 (point-at-bol) (point-at-eol))
21355 (setq current-prefix-arg nil)
21356 (org-agenda-maybe-redo)))
21358 (eval-after-load "speedbar"
21360 (speedbar-add-supported-extension ".org")
21361 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
21362 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
21363 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
21364 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
21365 (add-hook 'speedbar-visiting-tag-hook
21366 (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
21368 ;;; Fixes and Hacks for problems with other packages
21370 ;; Make flyspell not check words in links, to not mess up our keymap
21371 (defun org-mode-flyspell-verify ()
21372 "Don't let flyspell put overlays at active buttons, or on
21373 {todo,all-time,additional-option-like}-keywords."
21374 (let ((pos (max (1- (point)) (point-min)))
21375 (word (thing-at-point 'word)))
21376 (and (not (get-text-property pos 'keymap))
21377 (not (get-text-property pos 'org-no-flyspell))
21378 (not (member word org-todo-keywords-1))
21379 (not (member word org-all-time-keywords))
21380 (not (member word org-additional-option-like-keywords)))))
21382 (defun org-remove-flyspell-overlays-in (beg end)
21383 "Remove flyspell overlays in region."
21384 (and (org-bound-and-true-p flyspell-mode)
21385 (fboundp 'flyspell-delete-region-overlays)
21386 (flyspell-delete-region-overlays beg end))
21387 (add-text-properties beg end '(org-no-flyspell t)))
21389 ;; Make `bookmark-jump' shows the jump location if it was hidden.
21390 (eval-after-load "bookmark"
21391 '(if (boundp 'bookmark-after-jump-hook)
21392 ;; We can use the hook
21393 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
21394 ;; Hook not available, use advice
21395 (defadvice bookmark-jump (after org-make-visible activate)
21396 "Make the position visible."
21397 (org-bookmark-jump-unhide))))
21399 ;; Make sure saveplace shows the location if it was hidden
21400 (eval-after-load "saveplace"
21401 '(defadvice save-place-find-file-hook (after org-make-visible activate)
21402 "Make the position visible."
21403 (org-bookmark-jump-unhide)))
21405 ;; Make sure ecb shows the location if it was hidden
21406 (eval-after-load "ecb"
21407 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
21408 "Make hierarchy visible when jumping into location from ECB tree buffer."
21409 (if (eq major-mode 'org-mode)
21410 (org-show-context))))
21412 (defun org-bookmark-jump-unhide ()
21413 "Unhide the current position, to show the bookmark location."
21414 (and (eq major-mode 'org-mode)
21415 (or (outline-invisible-p)
21416 (save-excursion (goto-char (max (point-min) (1- (point))))
21417 (outline-invisible-p)))
21418 (org-show-context 'bookmark-jump)))
21420 ;; Make session.el ignore our circular variable
21421 (eval-after-load "session"
21422 '(add-to-list 'session-globals-exclude 'org-mark-ring))
21424 ;;;; Experimental code
21426 (defun org-closed-in-range ()
21427 "Sparse tree of items closed in a certain time range.
21428 Still experimental, may disappear in the future."
21430 ;; Get the time interval from the user.
21431 (let* ((time1 (org-float-time
21432 (org-read-date nil 'to-time nil "Starting date: ")))
21433 (time2 (org-float-time
21434 (org-read-date nil 'to-time nil "End date:")))
21435 ;; callback function
21436 (callback (lambda ()
21439 (apply 'encode-time
21440 (org-parse-time-string
21441 (match-string 1))))))
21442 ;; check if time in interval
21443 (and (>= time time1) (<= time time2))))))
21444 ;; make tree, check each match with the callback
21445 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
21451 (run-hooks 'org-load-hook)
21453 ;;; org.el ends here