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.08"
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 `org-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 :set
(lambda (var val
)
2276 (add-hook 'org-blocker-hook
2277 'org-block-todo-from-children-or-siblings-or-parent
)
2278 (remove-hook 'org-blocker-hook
2279 'org-block-todo-from-children-or-siblings-or-parent
)))
2283 (defcustom org-enforce-todo-checkbox-dependencies nil
2284 "Non-nil means unchecked boxes will block switching the parent to DONE.
2285 When this is nil, checkboxes have no influence on switching TODO states.
2286 When non-nil, you first need to check off all check boxes before the TODO
2287 entry can be switched to DONE.
2288 This variable needs to be set before org.el is loaded, and you need to
2289 restart Emacs after a change to make the change effective. The only way
2290 to change is while Emacs is running is through the customize interface."
2291 :set
(lambda (var val
)
2294 (add-hook 'org-blocker-hook
2295 'org-block-todo-from-checkboxes
)
2296 (remove-hook 'org-blocker-hook
2297 'org-block-todo-from-checkboxes
)))
2301 (defcustom org-treat-insert-todo-heading-as-state-change nil
2302 "Non-nil means inserting a TODO heading is treated as state change.
2303 So when the command \\[org-insert-todo-heading] is used, state change
2304 logging will apply if appropriate. When nil, the new TODO item will
2305 be inserted directly, and no logging will take place."
2309 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2310 "Non-nil means switching TODO states with S-cursor counts as state change.
2311 This is the default behavior. However, setting this to nil allows a
2312 convenient way to select a TODO state and bypass any logging associated
2317 (defcustom org-todo-state-tags-triggers nil
2318 "Tag changes that should be triggered by TODO state changes.
2319 This is a list. Each entry is
2321 (state-change (tag . flag) .......)
2323 State-change can be a string with a state, and empty string to indicate the
2324 state that has no TODO keyword, or it can be one of the symbols `todo'
2325 or `done', meaning any not-done or done state, respectively."
2329 (cons (choice :tag
"When changing to"
2330 (const :tag
"Not-done state" todo
)
2331 (const :tag
"Done state" done
)
2332 (string :tag
"State"))
2334 (cons :tag
"Tag action"
2336 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2338 (defcustom org-log-done nil
2339 "Information to record when a task moves to the DONE state.
2341 Possible values are:
2343 nil Don't add anything, just change the keyword
2344 time Add a time stamp to the task
2345 note Prompt for a note and add it with template `org-log-note-headings'
2347 This option can also be set with on a per-file-basis with
2349 #+STARTUP: nologdone
2351 #+STARTUP: lognotedone
2353 You can have local logging settings for a subtree by setting the LOGGING
2354 property to one or more of these keywords."
2356 :group
'org-progress
2358 (const :tag
"No logging" nil
)
2359 (const :tag
"Record CLOSED timestamp" time
)
2360 (const :tag
"Record CLOSED timestamp with note." note
)))
2362 ;; Normalize old uses of org-log-done.
2364 ((eq org-log-done t
) (setq org-log-done
'time
))
2365 ((and (listp org-log-done
) (memq 'done org-log-done
))
2366 (setq org-log-done
'note
)))
2368 (defcustom org-log-reschedule nil
2369 "Information to record when the scheduling date of a tasks is modified.
2371 Possible values are:
2373 nil Don't add anything, just change the date
2374 time Add a time stamp to the task
2375 note Prompt for a note and add it with template `org-log-note-headings'
2377 This option can also be set with on a per-file-basis with
2379 #+STARTUP: nologreschedule
2380 #+STARTUP: logreschedule
2381 #+STARTUP: lognotereschedule"
2383 :group
'org-progress
2385 (const :tag
"No logging" nil
)
2386 (const :tag
"Record timestamp" time
)
2387 (const :tag
"Record timestamp with note." note
)))
2389 (defcustom org-log-redeadline nil
2390 "Information to record when the deadline date of a tasks is modified.
2392 Possible values are:
2394 nil Don't add anything, just change the date
2395 time Add a time stamp to the task
2396 note Prompt for a note and add it with template `org-log-note-headings'
2398 This option can also be set with on a per-file-basis with
2400 #+STARTUP: nologredeadline
2401 #+STARTUP: logredeadline
2402 #+STARTUP: lognoteredeadline
2404 You can have local logging settings for a subtree by setting the LOGGING
2405 property to one or more of these keywords."
2407 :group
'org-progress
2409 (const :tag
"No logging" nil
)
2410 (const :tag
"Record timestamp" time
)
2411 (const :tag
"Record timestamp with note." note
)))
2413 (defcustom org-log-note-clock-out nil
2414 "Non-nil means record a note when clocking out of an item.
2415 This can also be configured on a per-file basis by adding one of
2416 the following lines anywhere in the buffer:
2418 #+STARTUP: lognoteclock-out
2419 #+STARTUP: nolognoteclock-out"
2421 :group
'org-progress
2424 (defcustom org-log-done-with-time t
2425 "Non-nil means the CLOSED time stamp will contain date and time.
2426 When nil, only the date will be recorded."
2427 :group
'org-progress
2430 (defcustom org-log-note-headings
2431 '((done .
"CLOSING NOTE %t")
2432 (state .
"State %-12s from %-12S %t")
2433 (note .
"Note taken on %t")
2434 (reschedule .
"Rescheduled from %S on %t")
2435 (delschedule .
"Not scheduled, was %S on %t")
2436 (redeadline .
"New deadline from %S on %t")
2437 (deldeadline .
"Removed deadline, was %S on %t")
2438 (refile .
"Refiled on %t")
2440 "Headings for notes added to entries.
2441 The value is an alist, with the car being a symbol indicating the note
2442 context, and the cdr is the heading to be used. The heading may also be the
2444 %t in the heading will be replaced by a time stamp.
2445 %T will be an active time stamp instead the default inactive one
2446 %d will be replaced by a short-format time stamp.
2447 %D will be replaced by an active short-format time stamp.
2448 %s will be replaced by the new TODO state, in double quotes.
2449 %S will be replaced by the old TODO state, in double quotes.
2450 %u will be replaced by the user name.
2451 %U will be replaced by the full user name.
2453 In fact, it is not a good idea to change the `state' entry, because
2454 agenda log mode depends on the format of these entries."
2456 :group
'org-progress
2457 :type
'(list :greedy t
2458 (cons (const :tag
"Heading when closing an item" done
) string
)
2460 "Heading when changing todo state (todo sequence only)"
2462 (cons (const :tag
"Heading when just taking a note" note
) string
)
2463 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2464 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2465 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2466 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2467 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2468 (cons (const :tag
"Heading when refiling" refile
) string
)))
2470 (unless (assq 'note org-log-note-headings
)
2471 (push '(note .
"%t") org-log-note-headings
))
2473 (defcustom org-log-into-drawer nil
2474 "Non-nil means insert state change notes and time stamps into a drawer.
2475 When nil, state changes notes will be inserted after the headline and
2476 any scheduling and clock lines, but not inside a drawer.
2478 The value of this variable should be the name of the drawer to use.
2479 LOGBOOK is proposed as the default drawer for this purpose, you can
2480 also set this to a string to define the drawer of your choice.
2482 A value of t is also allowed, representing \"LOGBOOK\".
2484 If this variable is set, `org-log-state-notes-insert-after-drawers'
2487 You can set the property LOG_INTO_DRAWER to overrule this setting for
2490 :group
'org-progress
2492 (const :tag
"Not into a drawer" nil
)
2493 (const :tag
"LOGBOOK" t
)
2494 (string :tag
"Other")))
2496 (if (fboundp 'defvaralias
)
2497 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2499 (defun org-log-into-drawer ()
2500 "Return the value of `org-log-into-drawer', but let properties overrule.
2501 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2502 used instead of the default value."
2503 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
)))
2505 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2506 ((equal p
"t") "LOGBOOK")
2509 (defcustom org-log-state-notes-insert-after-drawers nil
2510 "Non-nil means insert state change notes after any drawers in entry.
2511 Only the drawers that *immediately* follow the headline and the
2512 deadline/scheduled line are skipped.
2513 When nil, insert notes right after the heading and perhaps the line
2514 with deadline/scheduling if present.
2516 This variable will have no effect if `org-log-into-drawer' is
2519 :group
'org-progress
2522 (defcustom org-log-states-order-reversed t
2523 "Non-nil means the latest state note will be directly after heading.
2524 When nil, the state change notes will be ordered according to time."
2526 :group
'org-progress
2529 (defcustom org-todo-repeat-to-state nil
2530 "The TODO state to which a repeater should return the repeating task.
2531 By default this is the first task in a TODO sequence, or the previous state
2532 in a TODO_TYP set. But you can specify another task here.
2533 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2536 :type
'(choice (const :tag
"Head of sequence" nil
)
2537 (string :tag
"Specific state")))
2539 (defcustom org-log-repeat
'time
2540 "Non-nil means record moving through the DONE state when triggering repeat.
2541 An auto-repeating task is immediately switched back to TODO when
2542 marked DONE. If you are not logging state changes (by adding \"@\"
2543 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2544 record a closing note, there will be no record of the task moving
2545 through DONE. This variable forces taking a note anyway.
2547 nil Don't force a record
2548 time Record a time stamp
2551 This option can also be set with on a per-file-basis with
2553 #+STARTUP: logrepeat
2554 #+STARTUP: lognoterepeat
2555 #+STARTUP: nologrepeat
2557 You can have local logging settings for a subtree by setting the LOGGING
2558 property to one or more of these keywords."
2560 :group
'org-progress
2562 (const :tag
"Don't force a record" nil
)
2563 (const :tag
"Force recording the DONE state" time
)
2564 (const :tag
"Force recording a note with the DONE state" note
)))
2567 (defgroup org-priorities nil
2568 "Priorities in Org-mode."
2569 :tag
"Org Priorities"
2572 (defcustom org-enable-priority-commands t
2573 "Non-nil means priority commands are active.
2574 When nil, these commands will be disabled, so that you never accidentally
2576 :group
'org-priorities
2579 (defcustom org-highest-priority ?A
2580 "The highest priority of TODO items. A character like ?A, ?B etc.
2581 Must have a smaller ASCII number than `org-lowest-priority'."
2582 :group
'org-priorities
2585 (defcustom org-lowest-priority ?C
2586 "The lowest priority of TODO items. A character like ?A, ?B etc.
2587 Must have a larger ASCII number than `org-highest-priority'."
2588 :group
'org-priorities
2591 (defcustom org-default-priority ?B
2592 "The default priority of TODO items.
2593 This is the priority an item gets if no explicit priority is given.
2594 When starting to cycle on an empty priority the first step in the cycle
2595 depends on `org-priority-start-cycle-with-default'. The resulting first
2596 step priority must not exceed the range from `org-highest-priority' to
2597 `org-lowest-priority' which means that `org-default-priority' has to be
2598 in this range exclusive or inclusive the range boundaries. Else the
2599 first step refuses to set the default and the second will fall back
2600 to (depending on the command used) the highest or lowest priority."
2601 :group
'org-priorities
2604 (defcustom org-priority-start-cycle-with-default t
2605 "Non-nil means start with default priority when starting to cycle.
2606 When this is nil, the first step in the cycle will be (depending on the
2607 command used) one higher or lower than the default priority.
2608 See also `org-default-priority'."
2609 :group
'org-priorities
2612 (defcustom org-get-priority-function nil
2613 "Function to extract the priority from a string.
2614 The string is normally the headline. If this is nil Org computes the
2615 priority from the priority cookie like [#A] in the headline. It returns
2616 an integer, increasing by 1000 for each priority level.
2617 The user can set a different function here, which should take a string
2618 as an argument and return the numeric priority."
2619 :group
'org-priorities
2623 (defgroup org-time nil
2624 "Options concerning time stamps and deadlines in Org-mode."
2628 (defcustom org-insert-labeled-timestamps-at-point nil
2629 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2630 When nil, these labeled time stamps are forces into the second line of an
2631 entry, just after the headline. When scheduling from the global TODO list,
2632 the time stamp will always be forced into the second line."
2636 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2637 "Formats for `format-time-string' which are used for time stamps.
2638 It is not recommended to change this constant.")
2640 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2641 "Number of minutes to round time stamps to.
2642 These are two values, the first applies when first creating a time stamp.
2643 The second applies when changing it with the commands `S-up' and `S-down'.
2644 When changing the time stamp, this means that it will change in steps
2645 of N minutes, as given by the second value.
2647 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2648 numbers should be factors of 60, so for example 5, 10, 15.
2650 When this is larger than 1, you can still force an exact time stamp by using
2651 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2652 and by using a prefix arg to `S-up/down' to specify the exact number
2653 of minutes to shift."
2655 :get
#'(lambda (var) ; Make sure both elements are there
2656 (if (integerp (default-value var
))
2657 (list (default-value var
) 5)
2658 (default-value var
)))
2660 (integer :tag
"when inserting times")
2661 (integer :tag
"when modifying times")))
2663 ;; Normalize old customizations of this variable.
2664 (when (integerp org-time-stamp-rounding-minutes
)
2665 (setq org-time-stamp-rounding-minutes
2666 (list org-time-stamp-rounding-minutes
2667 org-time-stamp-rounding-minutes
)))
2669 (defcustom org-display-custom-times nil
2670 "Non-nil means overlay custom formats over all time stamps.
2671 The formats are defined through the variable `org-time-stamp-custom-formats'.
2672 To turn this on on a per-file basis, insert anywhere in the file:
2673 #+STARTUP: customtime"
2677 (make-variable-buffer-local 'org-display-custom-times
)
2679 (defcustom org-time-stamp-custom-formats
2680 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2681 "Custom formats for time stamps. See `format-time-string' for the syntax.
2682 These are overlaid over the default ISO format if the variable
2683 `org-display-custom-times' is set. Time like %H:%M should be at the
2684 end of the second format. The custom formats are also honored by export
2685 commands, if custom time display is turned on at the time of export."
2689 (defun org-time-stamp-format (&optional long inactive
)
2690 "Get the right format for a time string."
2691 (let ((f (if long
(cdr org-time-stamp-formats
)
2692 (car org-time-stamp-formats
))))
2694 (concat "[" (substring f
1 -
1) "]")
2697 (defcustom org-time-clocksum-format
"%d:%02d"
2698 "The format string used when creating CLOCKSUM lines.
2699 This is also used when org-mode generates a time duration."
2703 (defcustom org-time-clocksum-use-fractional nil
2704 "If non-nil, \\[org-clock-display] uses fractional times.
2705 org-mode generates a time duration."
2709 (defcustom org-time-clocksum-fractional-format
"%.2f"
2710 "The format string used when creating CLOCKSUM lines, or when
2711 org-mode generates a time duration."
2715 (defcustom org-deadline-warning-days
14
2716 "No. of days before expiration during which a deadline becomes active.
2717 This variable governs the display in sparse trees and in the agenda.
2718 When 0 or negative, it means use this number (the absolute value of it)
2719 even if a deadline has a different individual lead time specified.
2721 Custom commands can set this variable in the options section."
2723 :group
'org-agenda-daily
/weekly
2726 (defcustom org-read-date-prefer-future t
2727 "Non-nil means assume future for incomplete date input from user.
2728 This affects the following situations:
2729 1. The user gives a month but not a year.
2730 For example, if it is April and you enter \"feb 2\", this will be read
2731 as Feb 2, *next* year. \"May 5\", however, will be this year.
2732 2. The user gives a day, but no month.
2733 For example, if today is the 15th, and you enter \"3\", Org-mode will
2734 read this as the third of *next* month. However, if you enter \"17\",
2735 it will be considered as *this* month.
2737 If you set this variable to the symbol `time', then also the following
2740 3. If the user gives a time, but no day. If the time is before now,
2741 to will be interpreted as tomorrow.
2743 Currently none of this works for ISO week specifications.
2745 When this option is nil, the current day, month and year will always be
2748 See also `org-agenda-jump-prefer-future'."
2751 (const :tag
"Never" nil
)
2752 (const :tag
"Check month and day" t
)
2753 (const :tag
"Check month, day, and time" time
)))
2755 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2756 "Should the agenda jump command prefer the future for incomplete dates?
2757 The default is to do the same as configured in `org-read-date-prefer-future'.
2758 But you can also set a deviating value here.
2759 This may t or nil, or the symbol `org-read-date-prefer-future'."
2764 (const :tag
"Use org-read-date-prefer-future"
2765 org-read-date-prefer-future
)
2766 (const :tag
"Never" nil
)
2767 (const :tag
"Always" t
)))
2769 (defcustom org-read-date-force-compatible-dates t
2770 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2772 Depending on the system Emacs is running on, certain dates cannot
2773 be represented with the type used internally to represent time.
2774 Dates between 1970-1-1 and 2038-1-1 can always be represented
2775 correctly. Some systems allow for earlier dates, some for later,
2776 some for both. One way to find out it to insert any date into an
2777 Org buffer, putting the cursor on the year and hitting S-up and
2778 S-down to test the range.
2780 When this variable is set to t, the date/time prompt will not let
2781 you specify dates outside the 1970-2037 range, so it is certain that
2782 these dates will work in whatever version of Emacs you are
2783 running, and also that you can move a file from one Emacs implementation
2784 to another. WHenever Org is forcing the year for you, it will display
2787 When this variable is nil, Org will check if the date is
2788 representable in the specific Emacs implementation you are using.
2789 If not, it will force a year, usually the current year, and beep
2790 to remind you. Currently this setting is not recommended because
2791 the likelihood that you will open your Org files in an Emacs that
2792 has limited date range is not negligible.
2794 A workaround for this problem is to use diary sexp dates for time
2795 stamps outside of this range."
2800 (defcustom org-read-date-display-live t
2801 "Non-nil means display current interpretation of date prompt live.
2802 This display will be in an overlay, in the minibuffer."
2806 (defcustom org-read-date-popup-calendar t
2807 "Non-nil means pop up a calendar when prompting for a date.
2808 In the calendar, the date can be selected with mouse-1. However, the
2809 minibuffer will also be active, and you can simply enter the date as well.
2810 When nil, only the minibuffer will be available."
2813 (if (fboundp 'defvaralias
)
2814 (defvaralias 'org-popup-calendar-for-date-prompt
2815 'org-read-date-popup-calendar
))
2817 (defcustom org-read-date-minibuffer-setup-hook nil
2818 "Hook to be used to set up keys for the date/time interface.
2819 Add key definitions to `minibuffer-local-map', which will be a temporary
2824 (defcustom org-extend-today-until
0
2825 "The hour when your day really ends. Must be an integer.
2826 This has influence for the following applications:
2827 - When switching the agenda to \"today\". It it is still earlier than
2828 the time given here, the day recognized as TODAY is actually yesterday.
2829 - When a date is read from the user and it is still before the time given
2830 here, the current date and time will be assumed to be yesterday, 23:59.
2831 Also, timestamps inserted in remember templates follow this rule.
2833 IMPORTANT: This is a feature whose implementation is and likely will
2834 remain incomplete. Really, it is only here because past midnight seems to
2835 be the favorite working time of John Wiegley :-)"
2839 (defcustom org-use-effective-time nil
2840 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2841 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2842 \"effective time\" of any timestamps between midnight and 8am will be
2843 23:59 of the previous day."
2848 (defcustom org-edit-timestamp-down-means-later nil
2849 "Non-nil means S-down will increase the time in a time stamp.
2850 When nil, S-up will increase."
2854 (defcustom org-calendar-follow-timestamp-change t
2855 "Non-nil means make the calendar window follow timestamp changes.
2856 When a timestamp is modified and the calendar window is visible, it will be
2857 moved to the new date."
2861 (defgroup org-tags nil
2862 "Options concerning tags in Org-mode."
2866 (defcustom org-tag-alist nil
2867 "List of tags allowed in Org-mode files.
2868 When this list is nil, Org-mode will base TAG input on what is already in the
2870 The value of this variable is an alist, the car of each entry must be a
2871 keyword as a string, the cdr may be a character that is used to select
2872 that tag through the fast-tag-selection interface.
2873 See the manual for details."
2877 (cons (string :tag
"Tag name")
2878 (character :tag
"Access char"))
2879 (list :tag
"Start radio group"
2881 (option (string :tag
"Group description")))
2882 (list :tag
"End radio group"
2884 (option (string :tag
"Group description")))
2885 (const :tag
"New line" (:newline
)))))
2887 (defcustom org-tag-persistent-alist nil
2888 "List of tags that will always appear in all Org-mode files.
2889 This is in addition to any in buffer settings or customizations
2891 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2892 The value of this variable is an alist, the car of each entry must be a
2893 keyword as a string, the cdr may be a character that is used to select
2894 that tag through the fast-tag-selection interface.
2895 See the manual for details.
2896 To disable these tags on a per-file basis, insert anywhere in the file:
2901 (cons (string :tag
"Tag name")
2902 (character :tag
"Access char"))
2903 (const :tag
"Start radio group" (:startgroup
))
2904 (const :tag
"End radio group" (:endgroup
))
2905 (const :tag
"New line" (:newline
)))))
2907 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2908 "If non-nil, always offer completion for all tags of all agenda files.
2909 Instead of customizing this variable directly, you might want to
2910 set it locally for capture buffers, because there no list of
2911 tags in that file can be created dynamically (there are none).
2913 (add-hook 'org-capture-mode-hook
2915 (set (make-local-variable
2916 'org-complete-tags-always-offer-all-agenda-tags)
2922 (defvar org-file-tags nil
2923 "List of tags that can be inherited by all entries in the file.
2924 The tags will be inherited if the variable `org-use-tag-inheritance'
2925 says they should be.
2926 This variable is populated from #+FILETAGS lines.")
2928 (defcustom org-use-fast-tag-selection
'auto
2929 "Non-nil means use fast tag selection scheme.
2930 This is a special interface to select and deselect tags with single keys.
2931 When nil, fast selection is never used.
2932 When the symbol `auto', fast selection is used if and only if selection
2933 characters for tags have been configured, either through the variable
2934 `org-tag-alist' or through a #+TAGS line in the buffer.
2935 When t, fast selection is always used and selection keys are assigned
2936 automatically if necessary."
2939 (const :tag
"Always" t
)
2940 (const :tag
"Never" nil
)
2941 (const :tag
"When selection characters are configured" 'auto
)))
2943 (defcustom org-fast-tag-selection-single-key nil
2944 "Non-nil means fast tag selection exits after first change.
2945 When nil, you have to press RET to exit it.
2946 During fast tag selection, you can toggle this flag with `C-c'.
2947 This variable can also have the value `expert'. In this case, the window
2948 displaying the tags menu is not even shown, until you press C-c again."
2951 (const :tag
"No" nil
)
2952 (const :tag
"Yes" t
)
2953 (const :tag
"Expert" expert
)))
2955 (defvar org-fast-tag-selection-include-todo nil
2956 "Non-nil means fast tags selection interface will also offer TODO states.
2957 This is an undocumented feature, you should not rely on it.")
2959 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2960 "The column to which tags should be indented in a headline.
2961 If this number is positive, it specifies the column. If it is negative,
2962 it means that the tags should be flushright to that column. For example,
2963 -80 works well for a normal 80 character screen.
2964 When 0, place tags directly after headline text, with only one space in
2969 (defcustom org-auto-align-tags t
2970 "Non-nil keeps tags aligned when modifying headlines.
2971 Some operations (i.e. demoting) change the length of a headline and
2972 therefore shift the tags around. With this option turned on, after
2973 each such operation the tags are again aligned to `org-tags-column'."
2977 (defcustom org-use-tag-inheritance t
2978 "Non-nil means tags in levels apply also for sublevels.
2979 When nil, only the tags directly given in a specific line apply there.
2980 This may also be a list of tags that should be inherited, or a regexp that
2981 matches tags that should be inherited. Additional control is possible
2982 with the variable `org-tags-exclude-from-inheritance' which gives an
2983 explicit list of tags to be excluded from inheritance., even if the value of
2984 `org-use-tag-inheritance' would select it for inheritance.
2986 If this option is t, a match early-on in a tree can lead to a large
2987 number of matches in the subtree when constructing the agenda or creating
2988 a sparse tree. If you only want to see the first match in a tree during
2989 a search, check out the variable `org-tags-match-list-sublevels'."
2992 (const :tag
"Not" nil
)
2993 (const :tag
"Always" t
)
2994 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2995 (regexp :tag
"Tags matched by regexp")))
2997 (defcustom org-tags-exclude-from-inheritance nil
2998 "List of tags that should never be inherited.
2999 This is a way to exclude a few tags from inheritance. For way to do
3000 the opposite, to actively allow inheritance for selected tags,
3001 see the variable `org-use-tag-inheritance'."
3003 :type
'(repeat (string :tag
"Tag")))
3005 (defun org-tag-inherit-p (tag)
3006 "Check if TAG is one that should be inherited."
3008 ((member tag org-tags-exclude-from-inheritance
) nil
)
3009 ((eq org-use-tag-inheritance t
) t
)
3010 ((not org-use-tag-inheritance
) nil
)
3011 ((stringp org-use-tag-inheritance
)
3012 (string-match org-use-tag-inheritance tag
))
3013 ((listp org-use-tag-inheritance
)
3014 (member tag org-use-tag-inheritance
))
3015 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3017 (defcustom org-tags-match-list-sublevels t
3018 "Non-nil means list also sublevels of headlines matching a search.
3019 This variable applies to tags/property searches, and also to stuck
3020 projects because this search is based on a tags match as well.
3022 When set to the symbol `indented', sublevels are indented with
3025 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3026 the sublevels of a headline matching a tag search often also match
3027 the same search. Listing all of them can create very long lists.
3028 Setting this variable to nil causes subtrees of a match to be skipped.
3030 This variable is semi-obsolete and probably should always be true. It
3031 is better to limit inheritance to certain tags using the variables
3032 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3035 (const :tag
"No, don't list them" nil
)
3036 (const :tag
"Yes, do list them" t
)
3037 (const :tag
"List them, indented with leading dots" indented
)))
3039 (defcustom org-tags-sort-function nil
3040 "When set, tags are sorted using this function as a comparator."
3043 (const :tag
"No sorting" nil
)
3044 (const :tag
"Alphabetical" string
<)
3045 (const :tag
"Reverse alphabetical" string
>)
3046 (function :tag
"Custom function" nil
)))
3048 (defvar org-tags-history nil
3049 "History of minibuffer reads for tags.")
3050 (defvar org-last-tags-completion-table nil
3051 "The last used completion table for tags.")
3052 (defvar org-after-tags-change-hook nil
3053 "Hook that is run after the tags in a line have changed.")
3055 (defgroup org-properties nil
3056 "Options concerning properties in Org-mode."
3057 :tag
"Org Properties"
3060 (defcustom org-property-format
"%-10s %s"
3061 "How property key/value pairs should be formatted by `indent-line'.
3062 When `indent-line' hits a property definition, it will format the line
3063 according to this format, mainly to make sure that the values are
3064 lined-up with respect to each other."
3065 :group
'org-properties
3068 (defcustom org-properties-postprocess-alist nil
3069 "Alist of properties and functions to adjust inserted values.
3070 Elements of this alist must be of the form
3072 ([string] [function])
3074 where [string] must be a property name and [function] must be a
3075 lambda expression: this lambda expression must take one argument,
3076 the value to adjust, and return the new value as a string.
3078 For example, this element will allow the property \"Remaining\"
3079 to be updated wrt the relation between the \"Effort\" property
3080 and the clock summary:
3082 ((\"Remaining\" (lambda(value)
3083 (let ((clocksum (org-clock-sum-current-item))
3084 (effort (org-duration-string-to-minutes
3085 (org-entry-get (point) \"Effort\"))))
3086 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3087 :group
'org-properties
3091 (defcustom org-use-property-inheritance nil
3092 "Non-nil means properties apply also for sublevels.
3094 This setting is chiefly used during property searches. Turning it on can
3095 cause significant overhead when doing a search, which is why it is not
3098 When nil, only the properties directly given in the current entry count.
3099 When t, every property is inherited. The value may also be a list of
3100 properties that should have inheritance, or a regular expression matching
3101 properties that should be inherited.
3103 However, note that some special properties use inheritance under special
3104 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3105 and the properties ending in \"_ALL\" when they are used as descriptor
3106 for valid values of a property.
3108 Note for programmers:
3109 When querying an entry with `org-entry-get', you can control if inheritance
3110 should be used. By default, `org-entry-get' looks only at the local
3111 properties. You can request inheritance by setting the inherit argument
3112 to t (to force inheritance) or to `selective' (to respect the setting
3114 :group
'org-properties
3116 (const :tag
"Not" nil
)
3117 (const :tag
"Always" t
)
3118 (repeat :tag
"Specific properties" (string :tag
"Property"))
3119 (regexp :tag
"Properties matched by regexp")))
3121 (defun org-property-inherit-p (property)
3122 "Check if PROPERTY is one that should be inherited."
3124 ((eq org-use-property-inheritance t
) t
)
3125 ((not org-use-property-inheritance
) nil
)
3126 ((stringp org-use-property-inheritance
)
3127 (string-match org-use-property-inheritance property
))
3128 ((listp org-use-property-inheritance
)
3129 (member property org-use-property-inheritance
))
3130 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3132 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3133 "The default column format, if no other format has been defined.
3134 This variable can be set on the per-file basis by inserting a line
3136 #+COLUMNS: %25ITEM ....."
3137 :group
'org-properties
3140 (defcustom org-columns-ellipses
".."
3141 "The ellipses to be used when a field in column view is truncated.
3142 When this is the empty string, as many characters as possible are shown,
3143 but then there will be no visual indication that the field has been truncated.
3144 When this is a string of length N, the last N characters of a truncated
3145 field are replaced by this string. If the column is narrower than the
3146 ellipses string, only part of the ellipses string will be shown."
3147 :group
'org-properties
3150 (defcustom org-columns-modify-value-for-display-function nil
3151 "Function that modifies values for display in column view.
3152 For example, it can be used to cut out a certain part from a time stamp.
3153 The function must take 2 arguments:
3155 column-title The title of the column (*not* the property name)
3156 value The value that should be modified.
3158 The function should return the value that should be displayed,
3159 or nil if the normal value should be used."
3160 :group
'org-properties
3163 (defcustom org-effort-property
"Effort"
3164 "The property that is being used to keep track of effort estimates.
3165 Effort estimates given in this property need to have the format H:MM."
3166 :group
'org-properties
3167 :group
'org-progress
3168 :type
'(string :tag
"Property"))
3170 (defconst org-global-properties-fixed
3171 '(("VISIBILITY_ALL" .
"folded children content all")
3172 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3173 "List of property/value pairs that can be inherited by any entry.
3175 These are fixed values, for the preset properties. The user variable
3176 that can be used to add to this list is `org-global-properties'.
3178 The entries in this list are cons cells where the car is a property
3179 name and cdr is a string with the value. If the value represents
3180 multiple items like an \"_ALL\" property, separate the items by
3183 (defcustom org-global-properties nil
3184 "List of property/value pairs that can be inherited by any entry.
3186 This list will be combined with the constant `org-global-properties-fixed'.
3188 The entries in this list are cons cells where the car is a property
3189 name and cdr is a string with the value.
3191 You can set buffer-local values for the same purpose in the variable
3192 `org-file-properties' this by adding lines like
3194 #+PROPERTY: NAME VALUE"
3195 :group
'org-properties
3197 (cons (string :tag
"Property")
3198 (string :tag
"Value"))))
3200 (defvar org-file-properties nil
3201 "List of property/value pairs that can be inherited by any entry.
3202 Valid for the current buffer.
3203 This variable is populated from #+PROPERTY lines.")
3204 (make-variable-buffer-local 'org-file-properties
)
3206 (defgroup org-agenda nil
3207 "Options concerning agenda views in Org-mode."
3211 (defvar org-category nil
3212 "Variable used by org files to set a category for agenda display.
3213 Such files should use a file variable to set it, for example
3215 # -*- mode: org; org-category: \"ELisp\"
3217 or contain a special line
3221 If the file does not specify a category, then file's base name
3223 (make-variable-buffer-local 'org-category
)
3224 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3226 (defcustom org-agenda-files nil
3227 "The files to be used for agenda display.
3228 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3229 \\[org-remove-file]. You can also use customize to edit the list.
3231 If an entry is a directory, all files in that directory that are matched by
3232 `org-agenda-file-regexp' will be part of the file list.
3234 If the value of the variable is not a list but a single file name, then
3235 the list of agenda files is actually stored and maintained in that file, one
3236 agenda file per line. In this file paths can be given relative to
3237 `org-directory'. Tilde expansion and environment variable substitution
3241 (repeat :tag
"List of files and directories" file
)
3242 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3244 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3245 "Regular expression to match files for `org-agenda-files'.
3246 If any element in the list in that variable contains a directory instead
3247 of a normal file, all files in that directory that are matched by this
3248 regular expression will be included."
3252 (defcustom org-agenda-text-search-extra-files nil
3253 "List of extra files to be searched by text search commands.
3254 These files will be search in addition to the agenda files by the
3255 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3256 Note that these files will only be searched for text search commands,
3257 not for the other agenda views like todo lists, tag searches or the weekly
3258 agenda. This variable is intended to list notes and possibly archive files
3259 that should also be searched by these two commands.
3260 In fact, if the first element in the list is the symbol `agenda-archives',
3261 than all archive files of all agenda files will be added to the search
3264 :type
'(set :greedy t
3265 (const :tag
"Agenda Archives" agenda-archives
)
3266 (repeat :inline t
(file))))
3268 (if (fboundp 'defvaralias
)
3269 (defvaralias 'org-agenda-multi-occur-extra-files
3270 'org-agenda-text-search-extra-files
))
3272 (defcustom org-agenda-skip-unavailable-files nil
3273 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3274 A nil value means to remove them, after a query, from the list."
3278 (defcustom org-calendar-to-agenda-key
[?c
]
3279 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3280 The command `org-calendar-goto-agenda' will be bound to this key. The
3281 default is the character `c' because then `c' can be used to switch back and
3282 forth between agenda and calendar."
3286 (defcustom org-calendar-agenda-action-key
[?k
]
3287 "The key to be installed in `calendar-mode-map' for agenda-action.
3288 The command `org-agenda-action' will be bound to this key. The
3289 default is the character `k' because we use the same key in the agenda."
3293 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3294 "The key to be installed in `calendar-mode-map' for adding diary entries.
3295 This option is irrelevant until `org-agenda-diary-file' has been configured
3296 to point to an Org-mode file. When that is the case, the command
3297 `org-agenda-diary-entry' will be bound to the key given here, by default
3298 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3299 if you want to continue doing this, you need to change this to a different
3304 (defcustom org-agenda-diary-file
'diary-file
3305 "File to which to add new entries with the `i' key in agenda and calendar.
3306 When this is the symbol `diary-file', the functionality in the Emacs
3307 calendar will be used to add entries to the `diary-file'. But when this
3308 points to a file, `org-agenda-diary-entry' will be used instead."
3311 (const :tag
"The standard Emacs diary file" diary-file
)
3312 (file :tag
"Special Org file diary entries")))
3314 (eval-after-load "calendar"
3316 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3317 'org-calendar-goto-agenda
)
3318 (org-defkey calendar-mode-map org-calendar-agenda-action-key
3320 (add-hook 'calendar-mode-hook
3322 (unless (eq org-agenda-diary-file
'diary-file
)
3323 (define-key calendar-mode-map
3324 org-calendar-insert-diary-entry-key
3325 'org-agenda-diary-entry
))))))
3327 (defgroup org-latex nil
3328 "Options for embedding LaTeX code into Org-mode."
3332 (defcustom org-format-latex-options
3333 '(:foreground default
:background default
:scale
1.0
3334 :html-foreground
"Black" :html-background
"Transparent"
3335 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3336 "Options for creating images from LaTeX fragments.
3337 This is a property list with the following properties:
3338 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3339 `default' means use the foreground of the default face.
3340 :background the background color, or \"Transparent\".
3341 `default' means use the background of the default face.
3342 :scale a scaling factor for the size of the images, to get more pixels
3343 :html-foreground, :html-background, :html-scale
3344 the same numbers for HTML export.
3345 :matchers a list indicating which matchers should be used to
3346 find LaTeX fragments. Valid members of this list are:
3347 \"begin\" find environments
3348 \"$1\" find single characters surrounded by $.$
3349 \"$\" find math expressions surrounded by $...$
3350 \"$$\" find math expressions surrounded by $$....$$
3351 \"\\(\" find math expressions surrounded by \\(...\\)
3352 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3356 (defcustom org-format-latex-signal-error t
3357 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3358 When nil, just push out a message."
3363 (defcustom org-latex-to-mathml-jar-file nil
3364 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3365 Use this to specify additional executable file say a jar file.
3367 When using MathToWeb as the converter, specify the full-path to
3368 your mathtoweb.jar file."
3372 (const :tag
"None" nil
)
3373 (file :tag
"JAR file" :must-match t
)))
3375 (defcustom org-latex-to-mathml-convert-command nil
3376 "Command to convert LaTeX fragments to MathML.
3377 Replace format-specifiers in the command as noted below and use
3378 `shell-command' to convert LaTeX to MathML.
3379 %j: Executable file in fully expanded form as specified by
3380 `org-latex-to-mathml-jar-file'.
3381 %I: Input LaTeX file in fully expanded form
3382 %o: Output MathML file
3383 This command is used by `org-create-math-formula'.
3385 When using MathToWeb as the converter, set this to
3386 \"java -jar %j -unicode -force -df %o %I\"."
3390 (const :tag
"None" nil
)
3391 (string :tag
"\nShell command")))
3393 (defun org-format-latex-mathml-available-p ()
3394 "Return t if `org-latex-to-mathml-convert-command' is usable."
3396 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3397 org-latex-to-mathml-convert-command
)
3398 (let ((executable (car (split-string
3399 org-latex-to-mathml-convert-command
))))
3400 (when (executable-find executable
)
3402 "%j" org-latex-to-mathml-convert-command
)
3403 (file-readable-p org-latex-to-mathml-jar-file
)
3406 (defcustom org-format-latex-header
"\\documentclass{article}
3407 \\usepackage[usenames]{color}
3408 \\usepackage{amsmath}
3409 \\usepackage[mathscr]{eucal}
3410 \\pagestyle{empty} % do not remove
3413 % The settings below are copied from fullpage.sty
3414 \\setlength{\\textwidth}{\\paperwidth}
3415 \\addtolength{\\textwidth}{-3cm}
3416 \\setlength{\\oddsidemargin}{1.5cm}
3417 \\addtolength{\\oddsidemargin}{-2.54cm}
3418 \\setlength{\\evensidemargin}{\\oddsidemargin}
3419 \\setlength{\\textheight}{\\paperheight}
3420 \\addtolength{\\textheight}{-\\headheight}
3421 \\addtolength{\\textheight}{-\\headsep}
3422 \\addtolength{\\textheight}{-\\footskip}
3423 \\addtolength{\\textheight}{-3cm}
3424 \\setlength{\\topmargin}{1.5cm}
3425 \\addtolength{\\topmargin}{-2.54cm}"
3426 "The document header used for processing LaTeX fragments.
3427 It is imperative that this header make sure that no page number
3428 appears on the page. The package defined in the variables
3429 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3430 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3435 (defvar org-format-latex-header-extra nil
)
3437 (defun org-set-packages-alist (var val
)
3438 "Set the packages alist and make sure it has 3 elements per entry."
3439 (set var
(mapcar (lambda (x)
3440 (if (and (consp x
) (= (length x
) 2))
3441 (list (car x
) (nth 1 x
) t
)
3445 (defun org-get-packages-alist (var)
3447 "Get the packages alist and make sure it has 3 elements per entry."
3449 (if (and (consp x
) (= (length x
) 2))
3450 (list (car x
) (nth 1 x
) t
)
3452 (default-value var
)))
3454 ;; The following variables are defined here because is it also used
3455 ;; when formatting latex fragments. Originally it was part of the
3456 ;; LaTeX exporter, which is why the name includes "export".
3457 (defcustom org-export-latex-default-packages-alist
3458 '(("AUTO" "inputenc" t
)
3462 ("" "longtable" nil
)
3474 "Alist of default packages to be inserted in the header.
3475 Change this only if one of the packages here causes an incompatibility
3476 with another package you are using.
3477 The packages in this list are needed by one part or another of Org-mode
3478 to function properly.
3480 - inputenc, fontenc: for basic font and character selection
3481 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3482 for interpreting the entities in `org-entities'. You can skip some of these
3483 packages if you don't use any of the symbols in it.
3484 - graphicx: for including images
3485 - float, wrapfig: for figure placement
3486 - longtable: for long tables
3487 - hyperref: for cross references
3489 Therefore you should not modify this variable unless you know what you
3490 are doing. The one reason to change it anyway is that you might be loading
3491 some other package that conflicts with one of the default packages.
3492 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3493 If SNIPPET-FLAG is t, the package also needs to be included when
3494 compiling LaTeX snippets into images for inclusion into HTML."
3495 :group
'org-export-latex
3496 :set
'org-set-packages-alist
3497 :get
'org-get-packages-alist
3501 (list :tag
"options/package pair"
3502 (string :tag
"options")
3503 (string :tag
"package")
3504 (boolean :tag
"Snippet"))
3505 (string :tag
"A line of LaTeX"))))
3507 (defcustom org-export-latex-packages-alist nil
3508 "Alist of packages to be inserted in every LaTeX header.
3509 These will be inserted after `org-export-latex-default-packages-alist'.
3510 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3511 SNIPPET-FLAG, when t, indicates that this package is also needed when
3512 turning LaTeX snippets into images for inclusion into HTML.
3513 Make sure that you only list packages here which:
3514 - you want in every file
3515 - do not conflict with the default packages in
3516 `org-export-latex-default-packages-alist'
3517 - do not conflict with the setup in `org-format-latex-header'."
3518 :group
'org-export-latex
3519 :set
'org-set-packages-alist
3520 :get
'org-get-packages-alist
3523 (list :tag
"options/package pair"
3524 (string :tag
"options")
3525 (string :tag
"package")
3526 (boolean :tag
"Snippet"))
3527 (string :tag
"A line of LaTeX"))))
3530 (defgroup org-appearance nil
3531 "Settings for Org-mode appearance."
3532 :tag
"Org Appearance"
3535 (defcustom org-level-color-stars-only nil
3536 "Non-nil means fontify only the stars in each headline.
3537 When nil, the entire headline is fontified.
3538 Changing it requires restart of `font-lock-mode' to become effective
3539 also in regions already fontified."
3540 :group
'org-appearance
3543 (defcustom org-hide-leading-stars nil
3544 "Non-nil means hide the first N-1 stars in a headline.
3545 This works by using the face `org-hide' for these stars. This
3546 face is white for a light background, and black for a dark
3547 background. You may have to customize the face `org-hide' to
3549 Changing it requires restart of `font-lock-mode' to become effective
3550 also in regions already fontified.
3551 You may also set this on a per-file basis by adding one of the following
3552 lines to the buffer:
3554 #+STARTUP: hidestars
3555 #+STARTUP: showstars"
3556 :group
'org-appearance
3559 (defcustom org-hidden-keywords nil
3560 "List of symbols corresponding to keywords to be hidden the org buffer.
3561 For example, a value '(title) for this list will make the document's title
3562 appear in the buffer without the initial #+TITLE: keyword."
3563 :group
'org-appearance
3565 :type
'(set (const :tag
"#+AUTHOR" author
)
3566 (const :tag
"#+DATE" date
)
3567 (const :tag
"#+EMAIL" email
)
3568 (const :tag
"#+TITLE" title
)))
3570 (defcustom org-fontify-done-headline nil
3571 "Non-nil means change the face of a headline if it is marked DONE.
3572 Normally, only the TODO/DONE keyword indicates the state of a headline.
3573 When this is non-nil, the headline after the keyword is set to the
3574 `org-headline-done' as an additional indication."
3575 :group
'org-appearance
3578 (defcustom org-fontify-emphasized-text t
3579 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3580 Changing this variable requires a restart of Emacs to take effect."
3581 :group
'org-appearance
3584 (defcustom org-fontify-whole-heading-line nil
3585 "Non-nil means fontify the whole line for headings.
3586 This is useful when setting a background color for the
3588 :group
'org-appearance
3591 (defcustom org-highlight-latex-fragments-and-specials nil
3592 "Non-nil means fontify what is treated specially by the exporters."
3593 :group
'org-appearance
3596 (defcustom org-hide-emphasis-markers nil
3597 "Non-nil mean font-lock should hide the emphasis marker characters."
3598 :group
'org-appearance
3601 (defcustom org-pretty-entities nil
3602 "Non-nil means show entities as UTF8 characters.
3603 When nil, the \\name form remains in the buffer."
3604 :group
'org-appearance
3608 (defcustom org-pretty-entities-include-sub-superscripts t
3609 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3610 :group
'org-appearance
3614 (defvar org-emph-re nil
3615 "Regular expression for matching emphasis.
3616 After a match, the match groups contain these elements:
3617 0 The match of the full regular expression, including the characters
3618 before and after the proper match
3619 1 The character before the proper match, or empty at beginning of line
3620 2 The proper match, including the leading and trailing markers
3621 3 The leading marker like * or /, indicating the type of highlighting
3622 4 The text between the emphasis markers, not including the markers
3623 5 The character after the match, empty at the end of a line")
3624 (defvar org-verbatim-re nil
3625 "Regular expression for matching verbatim text.")
3626 (defvar org-emphasis-regexp-components
) ; defined just below
3627 (defvar org-emphasis-alist
) ; defined just below
3628 (defun org-set-emph-re (var val
)
3629 "Set variable and compute the emphasis regular expression."
3631 (when (and (boundp 'org-emphasis-alist
)
3632 (boundp 'org-emphasis-regexp-components
)
3633 org-emphasis-alist org-emphasis-regexp-components
)
3634 (let* ((e org-emphasis-regexp-components
)
3640 (body1 (concat body
"*?"))
3641 (markers (mapconcat 'car org-emphasis-alist
""))
3642 (vmarkers (mapconcat
3643 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3644 org-emphasis-alist
"")))
3645 ;; make sure special characters appear at the right position in the class
3646 (if (string-match "\\^" markers
)
3647 (setq markers
(concat (replace-match "" t t markers
) "^")))
3648 (if (string-match "-" markers
)
3649 (setq markers
(concat (replace-match "" t t markers
) "-")))
3650 (if (string-match "\\^" vmarkers
)
3651 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3652 (if (string-match "-" vmarkers
)
3653 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3655 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3656 (int-to-string nl
) "\\}")))
3659 (concat "\\([" pre
"]\\|^\\)"
3661 "\\([" markers
"]\\)"
3669 "\\([" post
"]\\|$\\)"))
3670 (setq org-verbatim-re
3671 (concat "\\([" pre
"]\\|^\\)"
3673 "\\([" vmarkers
"]\\)"
3681 "\\([" post
"]\\|$\\)")))))
3683 (defcustom org-emphasis-regexp-components
3684 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3685 "Components used to build the regular expression for emphasis.
3686 This is a list with five entries. Terminology: In an emphasis string
3687 like \" *strong word* \", we call the initial space PREMATCH, the final
3688 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3689 and \"trong wor\" is the body. The different components in this variable
3690 specify what is allowed/forbidden in each part:
3692 pre Chars allowed as prematch. Beginning of line will be allowed too.
3693 post Chars allowed as postmatch. End of line will be allowed too.
3694 border The chars *forbidden* as border characters.
3695 body-regexp A regexp like \".\" to match a body character. Don't use
3696 non-shy groups here, and don't allow newline here.
3697 newline The maximum number of newlines allowed in an emphasis exp.
3699 Use customize to modify this, or restart Emacs after changing it."
3700 :group
'org-appearance
3701 :set
'org-set-emph-re
3703 (sexp :tag
"Allowed chars in pre ")
3704 (sexp :tag
"Allowed chars in post ")
3705 (sexp :tag
"Forbidden chars in border ")
3706 (sexp :tag
"Regexp for body ")
3707 (integer :tag
"number of newlines allowed")
3708 (option (boolean :tag
"Please ignore this button"))))
3710 (defcustom org-emphasis-alist
3711 `(("*" bold
"<b>" "</b>")
3712 ("/" italic
"<i>" "</i>")
3713 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3714 ("=" org-code
"<code>" "</code>" verbatim
)
3715 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3716 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3719 "Special syntax for emphasized text.
3720 Text starting and ending with a special character will be emphasized, for
3721 example *bold*, _underlined_ and /italic/. This variable sets the marker
3722 characters, the face to be used by font-lock for highlighting in Org-mode
3723 Emacs buffers, and the HTML tags to be used for this.
3724 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3725 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3726 Use customize to modify this, or restart Emacs after changing it."
3727 :group
'org-appearance
3728 :set
'org-set-emph-re
3731 (string :tag
"Marker character")
3733 (face :tag
"Font-lock-face")
3734 (plist :tag
"Face property list"))
3735 (string :tag
"HTML start tag")
3736 (string :tag
"HTML end tag")
3737 (option (const verbatim
)))))
3739 (defvar org-protecting-blocks
3740 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3741 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3742 This is needed for font-lock setup.")
3744 ;;; Miscellaneous options
3746 (defgroup org-completion nil
3747 "Completion in Org-mode."
3748 :tag
"Org Completion"
3751 (defcustom org-completion-use-ido nil
3752 "Non-nil means use ido completion wherever possible.
3753 Note that `ido-mode' must be active for this variable to be relevant.
3754 If you decide to turn this variable on, you might well want to turn off
3755 `org-outline-path-complete-in-steps'.
3756 See also `org-completion-use-iswitchb'."
3757 :group
'org-completion
3760 (defcustom org-completion-use-iswitchb nil
3761 "Non-nil means use iswitchb completion wherever possible.
3762 Note that `iswitchb-mode' must be active for this variable to be relevant.
3763 If you decide to turn this variable on, you might well want to turn off
3764 `org-outline-path-complete-in-steps'.
3765 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3766 :group
'org-completion
3769 (defcustom org-completion-fallback-command
'hippie-expand
3770 "The expansion command called by \\[pcomplete] in normal context.
3771 Normal means, no org-mode-specific context."
3772 :group
'org-completion
3775 ;;; Functions and variables from their packages
3776 ;; Declared here to avoid compiler warnings
3779 (defvar outline-mode-menu-heading
)
3780 (defvar outline-mode-menu-show
)
3781 (defvar outline-mode-menu-hide
)
3782 (defvar zmacs-regions
) ; XEmacs regions
3785 (defvar mark-active
)
3788 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3789 (declare-function calendar-forward-day
"cal-move" (arg))
3790 (declare-function calendar-goto-date
"cal-move" (date))
3791 (declare-function calendar-goto-today
"cal-move" ())
3792 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3793 (defvar calc-embedded-close-formula
)
3794 (defvar calc-embedded-open-formula
)
3795 (declare-function cdlatex-tab
"ext:cdlatex" ())
3796 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3797 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3798 (defvar font-lock-unfontify-region-function
)
3799 (declare-function iswitchb-read-buffer
"iswitchb"
3800 (prompt &optional default require-match start matches-set
))
3801 (defvar iswitchb-temp-buflist
)
3802 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3803 (defvar org-agenda-tags-todo-honor-ignore-options
)
3804 (declare-function org-agenda-skip
"org-agenda" ())
3806 org-agenda-format-item
"org-agenda"
3807 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3808 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3809 (declare-function org-agenda-change-all-lines
"org-agenda"
3810 (newhead hdmarker
&optional fixface just-this
))
3811 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3812 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3813 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3815 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3816 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3817 "org-agenda" (&optional end
))
3818 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3819 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3820 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3821 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3822 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3823 (declare-function parse-time-string
"parse-time" (string))
3824 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3825 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3826 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3827 (defvar remember-data-file
)
3828 (defvar texmathp-why
)
3829 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3830 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3832 (defvar w3m-current-url
)
3833 (defvar w3m-current-title
)
3835 (defvar org-latex-regexps
)
3837 ;;; Autoload and prepare some org modules
3839 ;; Some table stuff that needs to be defined here, because it is used
3840 ;; by the functions setting up org-mode or checking for table context.
3842 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3843 "Detect an org-type or table-type table.")
3844 (defconst org-table-line-regexp
"^[ \t]*|"
3845 "Detect an org-type table line.")
3846 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3847 "Detect an org-type table line.")
3848 (defconst org-table-hline-regexp
"^[ \t]*|-"
3849 "Detect an org-type table hline.")
3850 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3851 "Detect a table-type table hline.")
3852 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3853 "Detect the first line outside a table when searching from within it.
3854 This works for both table types.")
3856 ;; Autoload the functions in org-table.el that are needed by functions here.
3859 (org-autoload "org-table"
3860 '(org-table-align org-table-begin org-table-blank-field
3861 org-table-convert org-table-convert-region org-table-copy-down
3862 org-table-copy-region org-table-create
3863 org-table-create-or-convert-from-region
3864 org-table-create-with-table.el org-table-current-dline
3865 org-table-cut-region org-table-delete-column org-table-edit-field
3866 org-table-edit-formulas org-table-end org-table-eval-formula
3867 org-table-export org-table-field-info
3868 org-table-get-stored-formulas org-table-goto-column
3869 org-table-hline-and-move org-table-import org-table-insert-column
3870 org-table-insert-hline org-table-insert-row org-table-iterate
3871 org-table-justify-field-maybe org-table-kill-row
3872 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3873 org-table-move-column org-table-move-column-left
3874 org-table-move-column-right org-table-move-row
3875 org-table-move-row-down org-table-move-row-up
3876 org-table-next-field org-table-next-row org-table-paste-rectangle
3877 org-table-previous-field org-table-recalculate
3878 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3879 org-table-toggle-coordinate-overlays
3880 org-table-toggle-formula-debugger org-table-wrap-region
3881 orgtbl-mode turn-on-orgtbl org-table-to-lisp
3882 orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
3883 orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo
)))
3885 (defun org-at-table-p (&optional table-type
)
3886 "Return t if the cursor is inside an org-type table.
3887 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3888 (if org-enable-table-editor
3890 (beginning-of-line 1)
3891 (looking-at (if table-type org-table-any-line-regexp
3892 org-table-line-regexp
)))
3894 (defsubst org-table-p
() (org-at-table-p))
3896 (defun org-at-table.el-p
()
3897 "Return t if and only if we are at a table.el table."
3898 (and (org-at-table-p 'any
)
3900 (goto-char (org-table-begin 'any
))
3901 (looking-at org-table1-hline-regexp
))))
3902 (defun org-table-recognize-table.el
()
3903 "If there is a table.el table nearby, recognize it and move into it."
3904 (if org-table-tab-recognizes-table.el
3905 (if (org-at-table.el-p
)
3907 (beginning-of-line 1)
3908 (if (looking-at org-table-dataline-regexp
)
3910 (if (looking-at org-table1-hline-regexp
)
3912 (beginning-of-line 2)
3913 (if (looking-at org-table-any-border-regexp
)
3914 (beginning-of-line -
1)))))
3915 (if (re-search-forward "|" (org-table-end t
) t
)
3918 (if (table--at-cell-p (point))
3920 (message "recognizing table.el table...")
3921 (table-recognize-table)
3922 (message "recognizing table.el table...done")))
3923 (error "This should not happen"))
3928 (defun org-at-table-hline-p ()
3929 "Return t if the cursor is inside a hline in a table."
3930 (if org-enable-table-editor
3932 (beginning-of-line 1)
3933 (looking-at org-table-hline-regexp
))
3936 (defvar org-table-clean-did-remove-column nil
)
3938 (defun org-table-map-tables (function &optional quietly
)
3939 "Apply FUNCTION to the start of all tables in the buffer."
3943 (goto-char (point-min))
3944 (while (re-search-forward org-table-any-line-regexp nil t
)
3946 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3947 (beginning-of-line 1)
3948 (when (and (looking-at org-table-line-regexp
)
3949 ;; Exclude tables in src/example/verbatim/clocktable blocks
3950 (not (org-in-block-p '("src" "example"))))
3951 (save-excursion (funcall function
))
3952 (or (looking-at org-table-line-regexp
)
3954 (re-search-forward org-table-any-border-regexp nil
1))))
3955 (unless quietly
(message "Mapping tables: done")))
3957 ;; Declare and autoload functions from org-exp.el & Co
3959 (declare-function org-default-export-plist
"org-exp")
3960 (declare-function org-infile-export-plist
"org-exp")
3961 (declare-function org-get-current-options
"org-exp")
3963 (org-autoload "org-exp"
3964 '(org-export org-export-visible
3965 org-insert-export-options-template
3966 org-table-clean-before-export
))
3967 (org-autoload "org-ascii"
3968 '(org-export-as-ascii org-export-ascii-preprocess
3969 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3970 org-export-region-as-ascii
))
3971 (org-autoload "org-latex"
3972 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3973 org-replace-region-by-latex org-export-region-as-latex
3974 org-export-as-latex org-export-as-pdf
3975 org-export-as-pdf-and-open
))
3976 (org-autoload "org-html"
3977 '(org-export-as-html-and-open
3978 org-export-as-html-batch org-export-as-html-to-buffer
3979 org-replace-region-by-html org-export-region-as-html
3980 org-export-as-html
))
3981 (org-autoload "org-docbook"
3982 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
3983 org-replace-region-by-docbook org-export-region-as-docbook
3984 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
3985 org-export-as-docbook
))
3986 (org-autoload "org-icalendar"
3987 '(org-export-icalendar-this-file
3988 org-export-icalendar-all-agenda-files
3989 org-export-icalendar-combine-agenda-files
))
3990 (org-autoload "org-xoxo" '(org-export-as-xoxo))
3991 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning
)))
3993 ;; Declare and autoload functions from org-agenda.el
3996 (org-autoload "org-agenda"
3997 '(org-agenda org-agenda-list org-search-view
3998 org-todo-list org-tags-view org-agenda-list-stuck-projects
3999 org-diary org-agenda-to-appt
4000 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
4002 ;; Autoload org-remember
4005 (org-autoload "org-remember"
4006 '(org-remember-insinuate org-remember-annotation
4007 org-remember-apply-template org-remember org-remember-handler
)))
4010 (org-autoload "org-capture"
4011 '(org-capture org-capture-insert-template-here
4012 org-capture-import-remember-templates
)))
4014 ;; Autoload org-clock.el
4016 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
4018 (declare-function org-clock-update-mode-line
"org-clock" ())
4019 (declare-function org-resolve-clocks
"org-clock"
4020 (&optional also-non-dangling-p prompt last-valid
))
4021 (defvar org-clock-start-time
)
4022 (defvar org-clock-marker
(make-marker)
4023 "Marker recording the last clock-in.")
4024 (defvar org-clock-hd-marker
(make-marker)
4025 "Marker recording the last clock-in, but the headline position.")
4026 (defvar org-clock-heading
""
4027 "The heading of the current clock entry.")
4028 (defun org-clock-is-active ()
4029 "Return non-nil if clock is currently running.
4030 The return value is actually the clock marker."
4031 (marker-buffer org-clock-marker
))
4036 '(org-clock-in org-clock-out org-clock-cancel
4037 org-clock-goto org-clock-sum org-clock-display
4038 org-clock-remove-overlays org-clock-report
4039 org-clocktable-shift org-dblock-write
:clocktable
4040 org-get-clocktable org-resolve-clocks
)))
4042 (defun org-clock-update-time-maybe ()
4043 "If this is a CLOCK line, update it and return t.
4044 Otherwise, return nil."
4047 (beginning-of-line 1)
4048 (skip-chars-forward " \t")
4049 (when (looking-at org-clock-string
)
4050 (let ((re (concat "[ \t]*" org-clock-string
4051 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
4052 "\\([ \t]*=>.*\\)?\\)?"))
4055 ((not (looking-at re
))
4057 ((not (match-end 2))
4058 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4059 (> org-clock-marker
(point))
4060 (<= org-clock-marker
(point-at-eol)))
4061 ;; The clock is running here
4062 (setq org-clock-start-time
4064 (org-parse-time-string (match-string 1))))
4065 (org-clock-update-mode-line)))
4067 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
4069 (setq ts
(match-string 1)
4070 te
(match-string 3))
4071 (setq s
(- (org-float-time
4072 (apply 'encode-time
(org-parse-time-string te
)))
4074 (apply 'encode-time
(org-parse-time-string ts
))))
4077 h
(floor (/ s
3600))
4081 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
4084 (defun org-check-running-clock ()
4085 "Check if the current buffer contains the running clock.
4086 If yes, offer to stop it and to save the buffer with the changes."
4087 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4088 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4091 (when (y-or-n-p "Save changed buffer?")
4094 (defun org-clocktable-try-shift (dir n
)
4095 "Check if this line starts a clock table, if yes, shift the time block."
4096 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4097 (org-clocktable-shift dir n
)))
4099 ;; Autoload org-timer.el
4104 '(org-timer-start org-timer org-timer-item
4105 org-timer-change-times-in-region
4107 org-timer-reset-timers
4108 org-timer-show-remaining-time
)))
4110 ;; Autoload org-feed.el
4115 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
4118 ;; Autoload org-indent.el
4120 ;; Define the variable already here, to make sure we have it.
4121 (defvar org-indent-mode nil
4122 "Non-nil if Org-Indent mode is enabled.
4123 Use the command `org-indent-mode' to change this variable.")
4128 '(org-indent-mode)))
4130 ;; Autoload org-mobile.el
4135 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
4137 ;; Autoload archiving code
4138 ;; The stuff that is needed for cycling and tags has to be defined here.
4140 (defgroup org-archive nil
4141 "Options concerning archiving in Org-mode."
4143 :group
'org-structure
)
4145 (defcustom org-archive-location
"%s_archive::"
4146 "The location where subtrees should be archived.
4148 The value of this variable is a string, consisting of two parts,
4149 separated by a double-colon. The first part is a filename and
4150 the second part is a headline.
4152 When the filename is omitted, archiving happens in the same file.
4153 %s in the filename will be replaced by the current file
4154 name (without the directory part). Archiving to a different file
4155 is useful to keep archived entries from contributing to the
4158 The archived entries will be filed as subtrees of the specified
4159 headline. When the headline is omitted, the subtrees are simply
4160 filed away at the end of the file, as top-level entries. Also in
4161 the heading you can use %s to represent the file name, this can be
4162 useful when using the same archive for a number of different files.
4164 Here are a few examples:
4166 If the current file is Projects.org, archive in file
4167 Projects.org_archive, as top-level trees. This is the default.
4169 \"::* Archived Tasks\"
4170 Archive in the current file, under the top-level headline
4171 \"* Archived Tasks\".
4173 \"~/org/archive.org::\"
4174 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4176 \"~/org/archive.org::* From %s\"
4177 Archive in file ~/org/archive.org (absolute path), under headlines
4178 \"From FILENAME\" where file name is the current file name.
4180 \"basement::** Finished Tasks\"
4181 Archive in file ./basement (relative path), as level 3 trees
4182 below the level 2 heading \"** Finished Tasks\".
4184 You may set this option on a per-file basis by adding to the buffer a
4187 #+ARCHIVE: basement::** Finished Tasks
4189 You may also define it locally for a subtree by setting an ARCHIVE property
4190 in the entry. If such a property is found in an entry, or anywhere up
4191 the hierarchy, it will be used."
4195 (defcustom org-archive-tag
"ARCHIVE"
4196 "The tag that marks a subtree as archived.
4197 An archived subtree does not open during visibility cycling, and does
4198 not contribute to the agenda listings.
4199 After changing this, font-lock must be restarted in the relevant buffers to
4200 get the proper fontification."
4202 :group
'org-keywords
4205 (defcustom org-agenda-skip-archived-trees t
4206 "Non-nil means the agenda will skip any items located in archived trees.
4207 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4208 variable is no longer recommended, you should leave it at the value t.
4209 Instead, use the key `v' to cycle the archives-mode in the agenda."
4211 :group
'org-agenda-skip
4214 (defcustom org-columns-skip-archived-trees t
4215 "Non-nil means ignore archived trees when creating column view."
4217 :group
'org-properties
4220 (defcustom org-cycle-open-archived-trees nil
4221 "Non-nil means `org-cycle' will open archived trees.
4222 An archived tree is a tree marked with the tag ARCHIVE.
4223 When nil, archived trees will stay folded. You can still open them with
4224 normal outline commands like `show-all', but not with the cycling commands."
4229 (defcustom org-sparse-tree-open-archived-trees nil
4230 "Non-nil means sparse tree construction shows matches in archived trees.
4231 When nil, matches in these trees are highlighted, but the trees are kept in
4234 :group
'org-sparse-trees
4237 (defun org-cycle-hide-archived-subtrees (state)
4238 "Re-hide all archived subtrees after a visibility state change."
4239 (when (and (not org-cycle-open-archived-trees
)
4240 (not (memq state
'(overview folded
))))
4242 (let* ((globalp (memq state
'(contents all
)))
4243 (beg (if globalp
(point-min) (point)))
4244 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4245 (org-hide-archived-subtrees beg end
)
4247 (if (looking-at (concat ".*:" org-archive-tag
":"))
4248 (message "%s" (substitute-command-keys
4249 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4251 (defun org-force-cycle-archived ()
4252 "Cycle subtree even if it is archived."
4254 (setq this-command
'org-cycle
)
4255 (let ((org-cycle-open-archived-trees t
))
4256 (call-interactively 'org-cycle
)))
4258 (defun org-hide-archived-subtrees (beg end
)
4259 "Re-hide all archived subtrees after a visibility state change."
4261 (let* ((re (concat ":" org-archive-tag
":")))
4263 (while (re-search-forward re end t
)
4264 (when (org-at-heading-p)
4265 (org-flag-subtree t
)
4266 (org-end-of-subtree t
))))))
4268 (defun org-flag-subtree (flag)
4270 (org-back-to-heading t
)
4271 (outline-end-of-heading)
4272 (outline-flag-region (point)
4273 (progn (org-end-of-subtree t
) (point))
4276 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4279 (org-autoload "org-archive"
4280 '(org-add-archive-files org-archive-subtree
4281 org-archive-to-archive-sibling org-toggle-archive-tag
4282 org-archive-subtree-default
4283 org-archive-subtree-default-with-confirmation
)))
4285 ;; Autoload Column View Code
4287 (declare-function org-columns-number-to-string
"org-colview")
4288 (declare-function org-columns-get-format-and-top-level
"org-colview")
4289 (declare-function org-columns-compute
"org-colview")
4291 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4292 '(org-columns-number-to-string org-columns-get-format-and-top-level
4293 org-columns-compute org-agenda-columns org-columns-remove-overlays
4294 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
4298 (declare-function org-id-store-link
"org-id")
4299 (declare-function org-id-locations-load
"org-id")
4300 (declare-function org-id-locations-save
"org-id")
4301 (defvar org-id-track-globally
)
4302 (org-autoload "org-id"
4303 '(org-id-get-create org-id-new org-id-copy org-id-get
4304 org-id-get-with-outline-path-completion
4305 org-id-get-with-outline-drilling org-id-store-link
4306 org-id-goto org-id-find org-id-store-link
))
4308 ;; Autoload Plotting Code
4310 (org-autoload "org-plot"
4311 '(org-plot/gnuplot
))
4313 ;;; Variables for pre-computed regular expressions, all buffer local
4315 (defvar org-drawer-regexp nil
4316 "Matches first line of a hidden block.")
4317 (make-variable-buffer-local 'org-drawer-regexp
)
4318 (defvar org-todo-regexp nil
4319 "Matches any of the TODO state keywords.")
4320 (make-variable-buffer-local 'org-todo-regexp
)
4321 (defvar org-not-done-regexp nil
4322 "Matches any of the TODO state keywords except the last one.")
4323 (make-variable-buffer-local 'org-not-done-regexp
)
4324 (defvar org-not-done-heading-regexp nil
4325 "Matches a TODO headline that is not done.")
4326 (make-variable-buffer-local 'org-not-done-regexp
)
4327 (defvar org-todo-line-regexp nil
4328 "Matches a headline and puts TODO state into group 2 if present.")
4329 (make-variable-buffer-local 'org-todo-line-regexp
)
4330 (defvar org-complex-heading-regexp nil
4331 "Matches a headline and puts everything into groups:
4333 group 2: The todo keyword, maybe
4334 group 3: Priority cookie
4335 group 4: True headline
4337 (make-variable-buffer-local 'org-complex-heading-regexp
)
4338 (defvar org-complex-heading-regexp-format nil
4339 "Printf format to make regexp to match an exact headline.
4340 This regexp will match the headline of any node which has the
4341 exact headline text that is put into the format, but may have any
4342 TODO state, priority and tags.")
4343 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4344 (defvar org-todo-line-tags-regexp nil
4345 "Matches a headline and puts TODO state into group 2 if present.
4346 Also put tags into group 4 if tags are present.")
4347 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4348 (defvar org-ds-keyword-length
12
4349 "Maximum length of the Deadline and SCHEDULED keywords.")
4350 (make-variable-buffer-local 'org-ds-keyword-length
)
4351 (defvar org-deadline-regexp nil
4352 "Matches the DEADLINE keyword.")
4353 (make-variable-buffer-local 'org-deadline-regexp
)
4354 (defvar org-deadline-time-regexp nil
4355 "Matches the DEADLINE keyword together with a time stamp.")
4356 (make-variable-buffer-local 'org-deadline-time-regexp
)
4357 (defvar org-deadline-line-regexp nil
4358 "Matches the DEADLINE keyword and the rest of the line.")
4359 (make-variable-buffer-local 'org-deadline-line-regexp
)
4360 (defvar org-scheduled-regexp nil
4361 "Matches the SCHEDULED keyword.")
4362 (make-variable-buffer-local 'org-scheduled-regexp
)
4363 (defvar org-scheduled-time-regexp nil
4364 "Matches the SCHEDULED keyword together with a time stamp.")
4365 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4366 (defvar org-closed-time-regexp nil
4367 "Matches the CLOSED keyword together with a time stamp.")
4368 (make-variable-buffer-local 'org-closed-time-regexp
)
4370 (defvar org-keyword-time-regexp nil
4371 "Matches any of the 4 keywords, together with the time stamp.")
4372 (make-variable-buffer-local 'org-keyword-time-regexp
)
4373 (defvar org-keyword-time-not-clock-regexp nil
4374 "Matches any of the 3 keywords, together with the time stamp.")
4375 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4376 (defvar org-maybe-keyword-time-regexp nil
4377 "Matches a timestamp, possibly preceded by a keyword.")
4378 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4379 (defvar org-planning-or-clock-line-re nil
4380 "Matches a line with planning or clock info.")
4381 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
4382 (defvar org-all-time-keywords nil
4383 "List of time keywords.")
4384 (make-variable-buffer-local 'org-all-time-keywords
)
4386 (defconst org-plain-time-of-day-regexp
4389 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4392 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4394 "Regular expression to match a plain time or time range.
4395 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4396 groups carry important information:
4398 1 the first time, range or not
4399 8 the second time, if it is a range.")
4401 (defconst org-plain-time-extension-regexp
4404 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4405 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4406 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4407 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4408 groups carry important information:
4411 9 minutes of duration")
4413 (defconst org-stamp-time-of-day-regexp
4415 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4416 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4418 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4419 "Regular expression to match a timestamp time or time range.
4420 After a match, the following groups carry important information:
4422 1 date plus weekday, for back referencing to make sure both times are on the same day
4423 2 the first time, range or not
4424 4 the second time, if it is a range.")
4426 (defconst org-startup-options
4427 '(("fold" org-startup-folded t
)
4428 ("overview" org-startup-folded t
)
4429 ("nofold" org-startup-folded nil
)
4430 ("showall" org-startup-folded nil
)
4431 ("showeverything" org-startup-folded showeverything
)
4432 ("content" org-startup-folded content
)
4433 ("indent" org-startup-indented t
)
4434 ("noindent" org-startup-indented nil
)
4435 ("hidestars" org-hide-leading-stars t
)
4436 ("showstars" org-hide-leading-stars nil
)
4437 ("odd" org-odd-levels-only t
)
4438 ("oddeven" org-odd-levels-only nil
)
4439 ("align" org-startup-align-all-tables t
)
4440 ("noalign" org-startup-align-all-tables nil
)
4441 ("inlineimages" org-startup-with-inline-images t
)
4442 ("noinlineimages" org-startup-with-inline-images nil
)
4443 ("customtime" org-display-custom-times t
)
4444 ("logdone" org-log-done time
)
4445 ("lognotedone" org-log-done note
)
4446 ("nologdone" org-log-done nil
)
4447 ("lognoteclock-out" org-log-note-clock-out t
)
4448 ("nolognoteclock-out" org-log-note-clock-out nil
)
4449 ("logrepeat" org-log-repeat state
)
4450 ("lognoterepeat" org-log-repeat note
)
4451 ("nologrepeat" org-log-repeat nil
)
4452 ("logreschedule" org-log-reschedule time
)
4453 ("lognotereschedule" org-log-reschedule note
)
4454 ("nologreschedule" org-log-reschedule nil
)
4455 ("logredeadline" org-log-redeadline time
)
4456 ("lognoteredeadline" org-log-redeadline note
)
4457 ("nologredeadline" org-log-redeadline nil
)
4458 ("logrefile" org-log-refile time
)
4459 ("lognoterefile" org-log-refile note
)
4460 ("nologrefile" org-log-refile nil
)
4461 ("fninline" org-footnote-define-inline t
)
4462 ("nofninline" org-footnote-define-inline nil
)
4463 ("fnlocal" org-footnote-section nil
)
4464 ("fnauto" org-footnote-auto-label t
)
4465 ("fnprompt" org-footnote-auto-label nil
)
4466 ("fnconfirm" org-footnote-auto-label confirm
)
4467 ("fnplain" org-footnote-auto-label plain
)
4468 ("fnadjust" org-footnote-auto-adjust t
)
4469 ("nofnadjust" org-footnote-auto-adjust nil
)
4470 ("constcgs" constants-unit-system cgs
)
4471 ("constSI" constants-unit-system SI
)
4472 ("noptag" org-tag-persistent-alist nil
)
4473 ("hideblocks" org-hide-block-startup t
)
4474 ("nohideblocks" org-hide-block-startup nil
)
4475 ("beamer" org-startup-with-beamer-mode t
)
4476 ("entitiespretty" org-pretty-entities t
)
4477 ("entitiesplain" org-pretty-entities nil
))
4478 "Variable associated with STARTUP options for org-mode.
4479 Each element is a list of three items: The startup options as written
4480 in the #+STARTUP line, the corresponding variable, and the value to
4481 set this variable to if the option is found. An optional forth element PUSH
4482 means to push this value onto the list in the variable.")
4484 (defun org-update-property-plist (key val props
)
4485 "Update PROPS with KEY and VAL."
4486 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4487 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4488 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4489 (previous (cdr (assoc key props
))))
4491 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4492 (cons (cons key val
) remainder
))))
4494 (defconst org-block-regexp
4495 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4496 "Regular expression for hiding blocks.")
4497 (defconst org-heading-keyword-regexp-format
4498 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4499 "Printf format for a regexp matching an headline with some keyword.
4500 This regexp will match the headline of any node which has the
4501 exact keyword that is put into the format. The keyword isn't in
4502 any group by default, but the stars and the body are.")
4503 (defconst org-heading-keyword-maybe-regexp-format
4504 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4505 "Printf format for a regexp matching an headline, possibly with some keyword.
4506 This regexp can match any headline with the specified keyword, or
4507 without a keyword. The keyword isn't in any group by default,
4508 but the stars and the body are.")
4510 (defun org-set-regexps-and-options ()
4511 "Precompute regular expressions for current buffer."
4512 (when (eq major-mode
'org-mode
)
4513 (org-set-local 'org-todo-kwd-alist nil
)
4514 (org-set-local 'org-todo-key-alist nil
)
4515 (org-set-local 'org-todo-key-trigger nil
)
4516 (org-set-local 'org-todo-keywords-1 nil
)
4517 (org-set-local 'org-done-keywords nil
)
4518 (org-set-local 'org-todo-heads nil
)
4519 (org-set-local 'org-todo-sets nil
)
4520 (org-set-local 'org-todo-log-states nil
)
4521 (org-set-local 'org-file-properties nil
)
4522 (org-set-local 'org-file-tags nil
)
4523 (let ((re (org-make-options-regexp
4524 '("CATEGORY" "TODO" "COLUMNS"
4525 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4526 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4528 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4530 (scripts org-use-sub-superscripts
)
4531 kwds kws0 kwsa key log value cat arch tags const links hw dws
4532 tail sep kws1 prio props ftags drawers beamer-p
4533 ext-setup-or-nil setup-contents
(start 0))
4537 (goto-char (point-min))
4538 (while (or (and ext-setup-or-nil
4539 (string-match re ext-setup-or-nil start
)
4540 (setq start
(match-end 0)))
4541 (and (setq ext-setup-or-nil nil start
0)
4542 (re-search-forward re nil t
)))
4543 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4544 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4545 (if (stringp value
) (setq value
(org-trim value
)))
4547 ((equal key
"CATEGORY")
4549 ((member key
'("SEQ_TODO" "TODO"))
4550 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4551 ((equal key
"TYP_TODO")
4552 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4553 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4554 ;; general TODO-like setup
4555 (push (cons (intern (downcase (match-string 1 key
)))
4556 (org-split-string value splitre
)) kwds
))
4558 (setq tags
(append tags
(if tags
'("\\n") nil
)
4559 (org-split-string value splitre
))))
4560 ((equal key
"COLUMNS")
4561 (org-set-local 'org-columns-default-format value
))
4563 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4564 (push (cons (match-string 1 value
)
4565 (org-trim (match-string 2 value
)))
4567 ((equal key
"PRIORITIES")
4568 (setq prio
(org-split-string value
" +")))
4569 ((equal key
"PROPERTY")
4570 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4571 (setq props
(org-update-property-plist (match-string 1 value
)
4572 (match-string 2 value
)
4574 ((equal key
"FILETAGS")
4575 (when (string-match "\\S-" value
)
4580 (mapcar (lambda (x) (org-split-string x
":"))
4581 (org-split-string value
)))))))
4582 ((equal key
"DRAWERS")
4583 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4584 ((equal key
"CONSTANTS")
4585 (setq const
(append const
(org-split-string value splitre
))))
4586 ((equal key
"STARTUP")
4587 (let ((opts (org-split-string value splitre
))
4589 (while (setq l
(pop opts
))
4590 (when (setq l
(assoc l org-startup-options
))
4591 (setq var
(nth 1 l
) val
(nth 2 l
))
4593 (set (make-local-variable var
) val
)
4594 (if (not (listp (symbol-value var
)))
4595 (set (make-local-variable var
) nil
))
4596 (set (make-local-variable var
) (symbol-value var
))
4597 (add-to-list var val
))))))
4598 ((equal key
"ARCHIVE")
4600 (remove-text-properties 0 (length arch
)
4601 '(face t fontified t
) arch
))
4602 ((equal key
"LATEX_CLASS")
4603 (setq beamer-p
(equal value
"beamer")))
4604 ((equal key
"OPTIONS")
4605 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4606 (setq scripts
(read (match-string 2 value
)))))
4607 ((equal key
"SETUPFILE")
4608 (setq setup-contents
(org-file-contents
4610 (org-remove-double-quotes value
))
4612 (if (not ext-setup-or-nil
)
4613 (setq ext-setup-or-nil setup-contents start
0)
4614 (setq ext-setup-or-nil
4615 (concat (substring ext-setup-or-nil
0 start
)
4616 "\n" setup-contents
"\n"
4617 (substring ext-setup-or-nil start
)))))))
4618 ;; search for property blocks
4619 (goto-char (point-min))
4620 (while (re-search-forward org-block-regexp nil t
)
4621 (when (equal "PROPERTY" (upcase (match-string 1)))
4622 (setq value
(replace-regexp-in-string
4623 "[\n\r]" " " (match-string 4)))
4624 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4625 (setq props
(org-update-property-plist (match-string 1 value
)
4626 (match-string 2 value
)
4628 (org-set-local 'org-use-sub-superscripts scripts
)
4630 (org-set-local 'org-category
(intern cat
))
4631 (push (cons "CATEGORY" cat
) props
))
4633 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4634 (setq prio
(mapcar 'string-to-char prio
))
4635 (org-set-local 'org-highest-priority
(nth 0 prio
))
4636 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4637 (org-set-local 'org-default-priority
(nth 2 prio
)))
4638 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4639 (and ftags
(org-set-local 'org-file-tags
4640 (mapcar 'org-add-prop-inherited ftags
)))
4641 (and drawers
(org-set-local 'org-drawers drawers
))
4642 (and arch
(org-set-local 'org-archive-location arch
))
4643 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4644 ;; Process the TODO keywords
4646 ;; Use the global values as if they had been given locally.
4647 (setq kwds
(default-value 'org-todo-keywords
))
4648 (if (stringp (car kwds
))
4649 (setq kwds
(list (cons org-todo-interpretation
4650 (default-value 'org-todo-keywords
)))))
4651 (setq kwds
(reverse kwds
)))
4652 (setq kwds
(nreverse kwds
))
4654 (while (setq kws
(pop kwds
))
4656 (run-hook-with-args-until-success
4657 'org-todo-setup-filter-hook kws
)
4659 (setq inter
(pop kws
) sep
(member "|" kws
)
4660 kws0
(delete "|" (copy-sequence kws
))
4665 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4667 (setq kw
(match-string 1 x
)
4668 key
(and (match-end 2) (match-string 2 x
))
4669 log
(org-extract-log-state-settings x
))
4670 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4671 (and log
(push log org-todo-log-states
))
4673 (error "Invalid TODO keyword %s" x
)))
4675 kwsa
(if kwsa
(append '((:startgroup
))
4679 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4680 tail
(list inter hw
(car dws
) (org-last dws
))))
4681 (add-to-list 'org-todo-heads hw
'append
)
4682 (push kws1 org-todo-sets
)
4683 (setq org-done-keywords
(append org-done-keywords dws nil
))
4684 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4685 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4686 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4687 (setq org-todo-sets
(nreverse org-todo-sets
)
4688 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4689 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4690 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4691 ;; Process the constants
4694 (while (setq e
(pop const
))
4695 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4696 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4697 (setq org-table-formula-constants-local cst
)))
4699 ;; Process the tags.
4702 (while (setq e
(pop tags
))
4704 ((equal e
"{") (push '(:startgroup
) tgs
))
4705 ((equal e
"}") (push '(:endgroup
) tgs
))
4706 ((equal e
"\\n") (push '(:newline
) tgs
))
4707 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4708 (push (cons (match-string 1 e
)
4709 (string-to-char (match-string 2 e
)))
4711 (t (push (list e
) tgs
))))
4712 (org-set-local 'org-tag-alist nil
)
4713 (while (setq e
(pop tgs
))
4714 (or (and (stringp (car e
))
4715 (assoc (car e
) org-tag-alist
))
4716 (push e org-tag-alist
)))))
4718 ;; Compute the regular expressions and other local variables.
4719 ;; Using `org-outline-regexp-bol' would complicate them much,
4720 ;; because of the fixed white space at the end of that string.
4721 (if (not org-done-keywords
)
4722 (setq org-done-keywords
(and org-todo-keywords-1
4723 (list (org-last org-todo-keywords-1
)))))
4724 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4725 (length org-scheduled-string
)
4726 (length org-clock-string
)
4727 (length org-closed-string
)))
4729 (concat "^[ \t]*:\\("
4730 (mapconcat 'regexp-quote org-drawers
"\\|")
4732 org-not-done-keywords
4733 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4736 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4740 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4742 org-not-done-heading-regexp
4743 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4744 org-todo-line-regexp
4745 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4746 org-complex-heading-regexp
4747 (concat "^\\(\\*+\\)"
4748 "\\(?: +" org-todo-regexp
"\\)?"
4749 "\\(?: +\\(\\[#.\\]\\)\\)?"
4750 "\\(?: +\\(.*?\\)\\)?"
4751 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4753 org-complex-heading-regexp-format
4754 (concat "^\\(\\*+\\)"
4755 "\\(?: +" org-todo-regexp
"\\)?"
4756 "\\(?: +\\(\\[#.\\]\\)\\)?"
4758 ;; Stats cookies can be stuck to body.
4759 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4761 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4763 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4765 org-todo-line-tags-regexp
4766 (concat "^\\(\\*+\\)"
4767 "\\(?: +" org-todo-regexp
"\\)?"
4768 "\\(?: +\\(.*?\\)\\)?"
4769 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4771 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4772 org-deadline-time-regexp
4773 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4774 org-deadline-line-regexp
4775 (concat "\\<\\(" org-deadline-string
"\\).*")
4776 org-scheduled-regexp
4777 (concat "\\<" org-scheduled-string
)
4778 org-scheduled-time-regexp
4779 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4780 org-closed-time-regexp
4781 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4782 org-keyword-time-regexp
4783 (concat "\\<\\(" org-scheduled-string
4784 "\\|" org-deadline-string
4785 "\\|" org-closed-string
4786 "\\|" org-clock-string
"\\)"
4787 " *[[<]\\([^]>]+\\)[]>]")
4788 org-keyword-time-not-clock-regexp
4789 (concat "\\<\\(" org-scheduled-string
4790 "\\|" org-deadline-string
4791 "\\|" org-closed-string
4793 " *[[<]\\([^]>]+\\)[]>]")
4794 org-maybe-keyword-time-regexp
4795 (concat "\\(\\<\\(" org-scheduled-string
4796 "\\|" org-deadline-string
4797 "\\|" org-closed-string
4798 "\\|" org-clock-string
"\\)\\)?"
4799 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4800 org-planning-or-clock-line-re
4801 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4802 "\\|" org-deadline-string
4803 "\\|" org-closed-string
"\\|" org-clock-string
4805 org-all-time-keywords
4806 (mapcar (lambda (w) (substring w
0 -
1))
4807 (list org-scheduled-string org-deadline-string
4808 org-clock-string org-closed-string
))
4810 (org-compute-latex-and-specials-regexp)
4811 (org-set-font-lock-defaults))))
4813 (defun org-file-contents (file &optional noerror
)
4814 "Return the contents of FILE, as a string."
4816 (not (file-readable-p file
)))
4819 (message "Cannot read file \"%s\"" file
)
4822 (error "Cannot read file \"%s\"" file
))
4824 (insert-file-contents file
)
4827 (defun org-extract-log-state-settings (x)
4828 "Extract the log state setting from a TODO keyword string.
4829 This will extract info from a string like \"WAIT(w@/!)\"."
4830 (let (kw key log1 log2
)
4831 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4832 (setq kw
(match-string 1 x
)
4833 key
(and (match-end 2) (match-string 2 x
))
4834 log1
(and (match-end 3) (match-string 3 x
))
4835 log2
(and (match-end 4) (match-string 4 x
)))
4838 (and log1
(if (equal log1
"!") 'time
'note
))
4839 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4841 (defun org-remove-keyword-keys (list)
4842 "Remove a pair of parenthesis at the end of each string in LIST."
4844 (if (string-match "(.*)$" x
)
4845 (substring x
0 (match-beginning 0))
4849 (defun org-assign-fast-keys (alist)
4850 "Assign fast keys to a keyword-key alist.
4851 Respect keys that are already there."
4852 (let (new e
(alt ?
0))
4853 (while (setq e
(pop alist
))
4854 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4855 (cdr e
)) ;; Key already assigned.
4857 (let ((clist (string-to-list (downcase (car e
))))
4858 (used (append new alist
)))
4859 (when (= (car clist
) ?
@)
4861 (while (and clist
(rassoc (car clist
) used
))
4864 (while (rassoc alt used
)
4866 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4869 ;;; Some variables used in various places
4871 (defvar org-window-configuration nil
4872 "Used in various places to store a window configuration.")
4873 (defvar org-selected-window nil
4874 "Used in various places to store a window configuration.")
4875 (defvar org-finish-function nil
4876 "Function to be called when `C-c C-c' is used.
4877 This is for getting out of special buffers like remember.")
4880 ;; FIXME: Occasionally check by commenting these, to make sure
4881 ;; no other functions uses these, forgetting to let-bind them.
4883 (defvar org-last-state
)
4886 ;; Defined somewhere in this file, but used before definition.
4887 (defvar org-entities
) ;; defined in org-entities.el
4888 (defvar org-struct-menu
)
4889 (defvar org-org-menu
)
4890 (defvar org-tbl-menu
)
4892 ;;;; Define the Org-mode
4894 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4895 (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"))
4898 ;; We use a before-change function to check if a table might need
4900 (defvar org-table-may-need-update t
4901 "Indicates that a table might need an update.
4902 This variable is set by `org-before-change-function'.
4903 `org-table-align' sets it back to nil.")
4904 (defun org-before-change-function (beg end
)
4905 "Every change indicates that a table might need an update."
4906 (setq org-table-may-need-update t
))
4907 (defvar org-mode-map
)
4908 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4909 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4910 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4911 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4912 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4913 (defvar org-table-buffer-is-an nil
)
4915 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
4916 ;; some places -- e.g. see the macro org-with-limited-levels.
4918 ;; In Org buffers, the value of `outline-regexp' is that of
4919 ;; `org-outline-regexp'. The only function still directly relying on
4920 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
4921 ;; job when `orgstruct-mode' is active.
4922 (defvar org-outline-regexp
"\\*+ "
4923 "Regexp to match Org headlines.")
4924 (defconst org-outline-regexp-bol
"^\\*+ "
4925 "Regexp to match Org headlines.
4926 This is similar to `org-outline-regexp' but additionally makes
4927 sure that we are at the beginning of the line.")
4929 (defconst org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4930 "Matches an headline, putting stars and text into groups.
4931 Stars are put in group 1 and the trimmed body in group 2.")
4933 (defvar bidi-paragraph-direction
)
4934 (defvar buffer-face-mode-face
)
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 (when (>= emacs-major-version
23)
5070 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
)
5073 ;; If empty file that did not turn on org-mode automatically, make it to.
5074 (if (and org-insert-mode-line-in-empty-file
5075 (org-called-interactively-p 'any
)
5076 (= (point-min) (point-max)))
5077 (insert "# -*- mode: org -*-\n\n"))
5078 (unless org-inhibit-startup
5079 (when org-startup-align-all-tables
5080 (let ((bmp (buffer-modified-p)))
5081 (org-table-map-tables 'org-table-align
'quietly
)
5082 (set-buffer-modified-p bmp
)))
5083 (when org-startup-with-inline-images
5084 (org-display-inline-images))
5085 (when org-startup-indented
5086 (require 'org-indent
)
5087 (org-indent-mode 1))
5088 (unless org-inhibit-startup-visibility-stuff
5089 (org-set-startup-visibility))))
5091 (when (fboundp 'abbrev-table-put
)
5092 (abbrev-table-put org-mode-abbrev-table
5093 :parents
(list text-mode-abbrev-table
)))
5095 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5097 (defun org-current-time ()
5098 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5099 (if (> (car org-time-stamp-rounding-minutes
) 1)
5100 (let ((r (car org-time-stamp-rounding-minutes
))
5101 (time (decode-time)))
5103 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5108 "Return today date, considering `org-extend-today-until'."
5110 (time-subtract (current-time)
5111 (list 0 (* 3600 org-extend-today-until
) 0))))
5113 ;;;; Font-Lock stuff, including the activators
5115 (defvar org-mouse-map
(make-sparse-keymap))
5116 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5117 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5118 (when org-mouse-1-follows-link
5119 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5120 (when org-tab-follows-link
5121 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5122 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5124 (require 'font-lock
)
5126 (defconst org-non-link-chars
"]\t\n\r<>")
5127 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5128 "shell" "elisp" "doi" "message"))
5129 (defvar org-link-types-re nil
5130 "Matches a link that has a url-like prefix like \"http:\"")
5131 (defvar org-link-re-with-space nil
5132 "Matches a link with spaces, optional angular brackets around it.")
5133 (defvar org-link-re-with-space2 nil
5134 "Matches a link with spaces, optional angular brackets around it.")
5135 (defvar org-link-re-with-space3 nil
5136 "Matches a link with spaces, only for internal part in bracket links.")
5137 (defvar org-angle-link-re nil
5138 "Matches link with angular brackets, spaces are allowed.")
5139 (defvar org-plain-link-re nil
5140 "Matches plain link, without spaces.")
5141 (defvar org-bracket-link-regexp nil
5142 "Matches a link in double brackets.")
5143 (defvar org-bracket-link-analytic-regexp nil
5144 "Regular expression used to analyze links.
5145 Here is what the match groups contain after a match:
5151 (defvar org-bracket-link-analytic-regexp
++ nil
5152 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5153 (defvar org-any-link-re nil
5154 "Regular expression matching any link.")
5156 (defcustom org-match-sexp-depth
3
5157 "Number of stacked braces for sub/superscript matching.
5158 This has to be set before loading org.el to be effective."
5159 :group
'org-export-translation
; ??????????????????????????/
5162 (defun org-create-multibrace-regexp (left right n
)
5163 "Create a regular expression which will match a balanced sexp.
5164 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5165 as single character strings.
5166 The regexp returned will match the entire expression including the
5167 delimiters. It will also define a single group which contains the
5168 match except for the outermost delimiters. The maximum depth of
5169 stacked delimiters is N. Escaping delimiters is not possible."
5170 (let* ((nothing (concat "[^" left right
"]*?"))
5173 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5176 re
(concat re or next
)
5177 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5178 (concat left
"\\(" re
"\\)" right
)))
5180 (defvar org-match-substring-regexp
5182 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5183 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5185 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5187 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5188 "The regular expression matching a sub- or superscript.")
5190 (defvar org-match-substring-with-braces-regexp
5192 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5193 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5195 "The regular expression matching a sub- or superscript, forcing braces.")
5197 (defun org-make-link-regexps ()
5198 "Update the link regular expressions.
5199 This should be called after the variable `org-link-types' has changed."
5200 (setq org-link-types-re
5202 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5203 org-link-re-with-space
5205 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5206 "\\([^" org-non-link-chars
" ]"
5207 "[^" org-non-link-chars
"]*"
5208 "[^" org-non-link-chars
" ]\\)>?")
5209 org-link-re-with-space2
5211 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5212 "\\([^" org-non-link-chars
" ]"
5214 "[^" org-non-link-chars
" ]\\)>?")
5215 org-link-re-with-space3
5217 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5218 "\\([^" org-non-link-chars
" ]"
5222 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5223 "\\([^" org-non-link-chars
" ]"
5224 "[^" org-non-link-chars
"]*"
5228 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5229 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5230 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5231 org-bracket-link-regexp
5232 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5233 org-bracket-link-analytic-regexp
5236 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5239 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5241 org-bracket-link-analytic-regexp
++
5244 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5247 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5250 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5251 org-angle-link-re
"\\)\\|\\("
5252 org-plain-link-re
"\\)")))
5254 (org-make-link-regexps)
5256 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5257 "Regular expression for fast time stamp matching.")
5258 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5259 "Regular expression for fast time stamp matching.")
5260 (defconst org-ts-regexp0
5261 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5262 "Regular expression matching time strings for analysis.
5263 This one does not require the space after the date, so it can be used
5264 on a string that terminates immediately after the date.")
5265 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5266 "Regular expression matching time strings for analysis.")
5267 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5268 "Regular expression matching time stamps, with groups.")
5269 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5270 "Regular expression matching time stamps (also [..]), with groups.")
5271 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5272 "Regular expression matching a time stamp range.")
5273 (defconst org-tr-regexp-both
5274 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5275 "Regular expression matching a time stamp range.")
5276 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5277 org-ts-regexp
"\\)?")
5278 "Regular expression matching a time stamp or time stamp range.")
5279 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5280 org-ts-regexp-both
"\\)?")
5281 "Regular expression matching a time stamp or time stamp range.
5282 The time stamps may be either active or inactive.")
5284 (defvar org-emph-face nil
)
5286 (defun org-do-emphasis-faces (limit)
5287 "Run through the buffer and add overlays to emphasized strings."
5289 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5290 (if (not (= (char-after (match-beginning 3))
5291 (char-after (match-beginning 4))))
5294 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5295 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5299 (org-remove-flyspell-overlays-in
5300 (match-beginning 0) (match-end 0)))
5301 (add-text-properties (match-beginning 2) (match-end 2)
5302 '(font-lock-multiline t org-emphasis t
))
5303 (when org-hide-emphasis-markers
5304 (add-text-properties (match-end 4) (match-beginning 5)
5305 '(invisible org-link
))
5306 (add-text-properties (match-beginning 3) (match-end 3)
5307 '(invisible org-link
)))))
5311 (defun org-emphasize (&optional char
)
5312 "Insert or change an emphasis, i.e. a font like bold or italic.
5313 If there is an active region, change that region to a new emphasis.
5314 If there is no region, just insert the marker characters and position
5315 the cursor between them.
5316 CHAR should be either the marker character, or the first character of the
5317 HTML tag associated with that emphasis. If CHAR is a space, the means
5318 to remove the emphasis of the selected region.
5319 If char is not given (for example in an interactive call) it
5320 will be prompted for."
5322 (let ((eal org-emphasis-alist
) e det
5323 (erc org-emphasis-regexp-components
)
5325 (string "") beg end move tag c s
)
5326 (if (org-region-active-p)
5327 (setq beg
(region-beginning) end
(region-end)
5328 string
(buffer-substring beg end
))
5331 (while (setq e
(pop eal
))
5332 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5334 (push (cons c
(string-to-char (car e
))) det
)
5335 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5336 (substring tag
1)))))
5337 (setq det
(nreverse det
))
5339 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5340 (setq char
(read-char-exclusive)))
5341 (setq char
(or (cdr (assoc char det
)) char
))
5342 (if (equal char ?\
)
5343 (setq s
"" move nil
)
5344 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5345 (error "No such emphasis marker: \"%c\"" char
))
5346 (setq s
(char-to-string char
)))
5347 (while (and (> (length string
) 1)
5348 (equal (substring string
0 1) (substring string -
1))
5349 (assoc (substring string
0 1) org-emphasis-alist
))
5350 (setq string
(substring string
1 -
1)))
5351 (setq string
(concat s string s
))
5352 (if beg
(delete-region beg end
))
5354 (string-match (concat "[" (nth 0 erc
) "\n]")
5355 (char-to-string (char-before (point)))))
5358 (string-match (concat "[" (nth 1 erc
) "\n]")
5359 (char-to-string (char-after (point)))))
5360 (insert " ") (backward-char 1))
5362 (and move
(backward-char 1))))
5364 (defconst org-nonsticky-props
5365 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
5367 (defsubst org-rear-nonsticky-at
(pos)
5368 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5370 (defun org-activate-plain-links (limit)
5371 "Run through the buffer and add overlays to links."
5374 (if (re-search-forward org-plain-link-re limit t
)
5376 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5377 (setq f
(get-text-property (match-beginning 0) 'face
))
5378 (if (or (eq f
'org-tag
)
5379 (and (listp f
) (memq 'org-tag f
)))
5381 (add-text-properties (match-beginning 0) (match-end 0)
5382 (list 'mouse-face
'highlight
5384 'keymap org-mouse-map
))
5385 (org-rear-nonsticky-at (match-end 0)))
5388 (defun org-activate-code (limit)
5389 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5391 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5392 (remove-text-properties (match-beginning 0) (match-end 0)
5393 '(display t invisible t intangible t
))
5396 (defcustom org-src-fontify-natively nil
5397 "When non-nil, fontify code in code blocks."
5400 :group
'org-appearance
5403 (defun org-fontify-meta-lines-and-blocks (limit)
5405 (org-fontify-meta-lines-and-blocks-1 limit
)
5406 (error (message "org-mode fontification error"))))
5408 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5409 "Fontify #+ lines and blocks, in the correct ways."
5410 (let ((case-fold-search t
))
5411 (if (re-search-forward
5412 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5414 (let ((beg (match-beginning 0))
5415 (block-start (match-end 0))
5417 (lang (match-string 7))
5418 (beg1 (line-beginning-position 2))
5419 (dc1 (downcase (match-string 2)))
5420 (dc3 (downcase (match-string 3)))
5421 end end1 quoting block-type ovl
)
5423 ((member dc1
'("html:" "ascii:" "latex:" "docbook:"))
5424 ;; a single line of backend-specific content
5425 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5426 (remove-text-properties (match-beginning 0) (match-end 0)
5427 '(display t invisible t intangible t
))
5428 (add-text-properties (match-beginning 1) (match-end 3)
5429 '(font-lock-fontified t face org-meta-line
))
5430 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5431 '(font-lock-fontified t face org-block
))
5432 ; for backend-specific code
5434 ((and (match-end 4) (equal dc3
"begin"))
5436 (setq block-type
(downcase (match-string 5))
5437 quoting
(member block-type org-protecting-blocks
))
5438 (when (re-search-forward
5439 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5440 nil t
) ;; on purpose, we look further than LIMIT
5441 (setq end
(min (point-max) (match-end 0))
5442 end1
(min (point-max) (1- (match-beginning 0))))
5443 (setq block-end
(match-beginning 0))
5445 (remove-text-properties beg end
5446 '(display t invisible t intangible t
)))
5447 (add-text-properties
5449 '(font-lock-fontified t font-lock-multiline t
))
5450 (add-text-properties beg beg1
'(face org-meta-line
))
5451 (add-text-properties end1
(min (point-max) (1+ end
))
5452 '(face org-meta-line
)) ; for end_src
5454 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5455 (org-src-font-lock-fontify-block lang block-start block-end
)
5456 ;; remove old background overlays
5458 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5459 (delete-overlay ov
)))
5460 (overlays-at (/ (+ beg1 block-end
) 2)))
5461 ;; add a background overlay
5462 (setq ovl
(make-overlay beg1 block-end
))
5463 (overlay-put ovl
'face
'org-block-background
)
5464 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5466 (add-text-properties beg1
(min (point-max) (1+ end1
))
5467 '(face org-block
))) ; end of source block
5468 ((not org-fontify-quote-and-verse-blocks
))
5469 ((string= block-type
"quote")
5470 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5471 ((string= block-type
"verse")
5472 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5473 (add-text-properties beg beg1
'(face org-block-begin-line
))
5474 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5475 '(face org-block-end-line
))
5477 ((member dc1
'("title:" "author:" "email:" "date:"))
5478 (add-text-properties
5480 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5481 '(font-lock-fontified t invisible t
)
5482 '(font-lock-fontified t face org-document-info-keyword
)))
5483 (add-text-properties
5484 (match-beginning 6) (match-end 6)
5485 (if (string-equal dc1
"title:")
5486 '(font-lock-fontified t face org-document-title
)
5487 '(font-lock-fontified t face org-document-info
))))
5488 ((not (member (char-after beg
) '(?\ ?
\t)))
5489 ;; just any other in-buffer setting, but not indented
5490 (add-text-properties
5492 '(font-lock-fontified t face org-meta-line
))
5494 ((or (member dc1
'("begin:" "end:" "caption:" "label:"
5495 "orgtbl:" "tblfm:" "tblname:" "results:"
5496 "call:" "header:" "headers:" "name:"))
5497 (and (match-end 4) (equal dc3
"attr")))
5498 (add-text-properties
5500 '(font-lock-fontified t face org-meta-line
))
5502 ((member dc3
'(" " ""))
5503 (add-text-properties
5505 '(font-lock-fontified t face font-lock-comment-face
)))
5508 (defun org-strip-protective-commas (beg end
)
5509 "Strip protective commas between BEG and END in the current buffer."
5514 (let ((front-line (save-excursion
5516 "[^[:space:]]" end t
)
5517 (goto-char (match-beginning 0))
5519 (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t
)
5520 (goto-char (match-beginning 1))
5521 (when (= (current-column) front-line
)
5522 (replace-match "" nil nil nil
1)))))))
5524 (defun org-activate-angle-links (limit)
5525 "Run through the buffer and add overlays to links."
5526 (if (re-search-forward org-angle-link-re limit t
)
5528 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5529 (add-text-properties (match-beginning 0) (match-end 0)
5530 (list 'mouse-face
'highlight
5531 'keymap org-mouse-map
))
5532 (org-rear-nonsticky-at (match-end 0))
5535 (defun org-activate-footnote-links (limit)
5536 "Run through the buffer and add overlays to footnotes."
5537 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5539 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5540 (org-remove-flyspell-overlays-in beg end
)
5541 (add-text-properties beg end
5542 (list 'mouse-face
'highlight
5543 'keymap org-mouse-map
5545 (if (= (point-at-bol) beg
)
5546 "Footnote definition"
5547 "Footnote reference")
5548 'font-lock-fontified t
5549 'font-lock-multiline t
5550 'face
'org-footnote
))))))
5552 (defun org-activate-bracket-links (limit)
5553 "Run through the buffer and add overlays to bracketed links."
5554 (if (re-search-forward org-bracket-link-regexp limit t
)
5555 (let* ((help (concat "LINK: "
5556 (org-match-string-no-properties 1)))
5557 ;; FIXME: above we should remove the escapes.
5558 ;; but that requires another match, protecting match data,
5559 ;; a lot of overhead for font-lock.
5560 (ip (org-maybe-intangible
5561 (list 'invisible
'org-link
5562 'keymap org-mouse-map
'mouse-face
'highlight
5563 'font-lock-multiline t
'help-echo help
)))
5564 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5565 'font-lock-multiline t
'help-echo help
)))
5566 ;; We need to remove the invisible property here. Table narrowing
5567 ;; may have made some of this invisible.
5568 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5569 (remove-text-properties (match-beginning 0) (match-end 0)
5573 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5574 (org-rear-nonsticky-at (match-beginning 3))
5575 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5576 (org-rear-nonsticky-at (match-end 3))
5577 (add-text-properties (match-end 3) (match-end 0) ip
)
5578 (org-rear-nonsticky-at (match-end 0)))
5579 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5580 (org-rear-nonsticky-at (match-beginning 1))
5581 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5582 (org-rear-nonsticky-at (match-end 1))
5583 (add-text-properties (match-end 1) (match-end 0) ip
)
5584 (org-rear-nonsticky-at (match-end 0)))
5587 (defun org-activate-dates (limit)
5588 "Run through the buffer and add overlays to dates."
5589 (if (re-search-forward org-tsr-regexp-both limit t
)
5591 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5592 (add-text-properties (match-beginning 0) (match-end 0)
5593 (list 'mouse-face
'highlight
5594 'keymap org-mouse-map
))
5595 (org-rear-nonsticky-at (match-end 0))
5596 (when org-display-custom-times
5598 (org-display-custom-time (match-beginning 3) (match-end 3)))
5599 (org-display-custom-time (match-beginning 1) (match-end 1)))
5602 (defvar org-target-link-regexp nil
5603 "Regular expression matching radio targets in plain text.")
5604 (make-variable-buffer-local 'org-target-link-regexp
)
5605 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5606 "Regular expression matching a link target.")
5607 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5608 "Regular expression matching a radio target.")
5609 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5610 "Regular expression matching any target.")
5612 (defun org-activate-target-links (limit)
5613 "Run through the buffer and add overlays to target matches."
5614 (when org-target-link-regexp
5615 (let ((case-fold-search t
))
5616 (if (re-search-forward org-target-link-regexp limit t
)
5618 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5619 (add-text-properties (match-beginning 0) (match-end 0)
5620 (list 'mouse-face
'highlight
5621 'keymap org-mouse-map
5622 'help-echo
"Radio target link"
5623 'org-linked-text t
))
5624 (org-rear-nonsticky-at (match-end 0))
5627 (defun org-update-radio-target-regexp ()
5628 "Find all radio targets in this file and update the regular expression."
5630 (when (memq 'radio org-activate-links
)
5631 (setq org-target-link-regexp
5632 (org-make-target-link-regexp (org-all-targets 'radio
)))
5633 (org-restart-font-lock)))
5635 (defun org-hide-wide-columns (limit)
5637 (setq s
(text-property-any (point) (or limit
(point-max))
5640 (setq e
(next-single-property-change s
'org-cwidth
))
5641 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5645 (defvar org-latex-and-specials-regexp nil
5646 "Regular expression for highlighting export special stuff.")
5647 (defvar org-match-substring-regexp
)
5648 (defvar org-match-substring-with-braces-regexp
)
5650 ;; This should be with the exporter code, but we also use if for font-locking
5651 (defconst org-export-html-special-string-regexps
5652 '(("\\\\-" .
"­")
5653 ("---\\([^-]\\)" .
"—\\1")
5654 ("--\\([^-]\\)" .
"–\\1")
5655 ("\\.\\.\\." .
"…"))
5656 "Regular expressions for special string conversion.")
5659 (defun org-compute-latex-and-specials-regexp ()
5660 "Compute regular expression for stuff treated specially by exporters."
5661 (if (not org-highlight-latex-fragments-and-specials
)
5662 (org-set-local 'org-latex-and-specials-regexp nil
)
5665 ((matchers (plist-get org-format-latex-options
:matchers
))
5666 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5667 org-latex-regexps
)))
5668 (org-export-allow-BIND nil
)
5669 (options (org-combine-plists (org-default-export-plist)
5670 (org-infile-export-plist)))
5671 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5672 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5673 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5674 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5675 (org-export-with-special-strings (plist-get options
:special-strings
))
5678 ((equal org-export-with-sub-superscripts
'{})
5679 (list org-match-substring-with-braces-regexp
))
5680 (org-export-with-sub-superscripts
5681 (list org-match-substring-regexp
))
5684 (if org-export-with-LaTeX-fragments
5685 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5687 (if org-export-with-TeX-macros
5688 (list (concat "\\\\"
5694 (append org-entities-user
5696 (if (boundp 'org-latex-entities
)
5698 (or (car-safe x
) x
))
5703 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5704 (re-special (if org-export-with-special-strings
5705 (mapcar (lambda (x) (car x
))
5706 org-export-html-special-string-regexps
)))
5710 (if org-export-html-expand
"@<[^>\n]+>")
5713 'org-latex-and-specials-regexp
5714 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5717 (defun org-do-latex-and-special-faces (limit)
5718 "Run through the buffer and add overlays to links."
5719 (when org-latex-and-specials-regexp
5721 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5723 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5725 '(org-code org-verbatim underline
)))
5728 d
(cond ((member (char-after (1+ (match-beginning 0)))
5731 (font-lock-prepend-text-property
5732 (+ d
(match-beginning 0)) (match-end 0)
5733 'face
'org-latex-and-export-specials
)
5734 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5735 '(font-lock-multiline t
)))))
5738 (defun org-restart-font-lock ()
5739 "Restart `font-lock-mode', to force refontification."
5740 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5742 (font-lock-mode 1)))
5744 (defun org-all-targets (&optional radio
)
5745 "Return a list of all targets in this file.
5746 With optional argument RADIO, only find radio targets."
5747 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5750 (goto-char (point-min))
5751 (while (re-search-forward re nil t
)
5752 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5755 (defun org-make-target-link-regexp (targets)
5756 "Make regular expression matching all strings in TARGETS.
5757 The regular expression finds the targets also if there is a line break
5764 (setq x
(regexp-quote x
))
5765 (while (string-match " +" x
)
5766 (setq x
(replace-match "\\s-+" t t x
)))
5772 (defun org-activate-tags (limit)
5773 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5775 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5776 (add-text-properties (match-beginning 1) (match-end 1)
5777 (list 'mouse-face
'highlight
5778 'keymap org-mouse-map
))
5779 (org-rear-nonsticky-at (match-end 1))
5782 (defun org-outline-level ()
5783 "Compute the outline level of the heading at point.
5784 This function assumes that the cursor is at the beginning of a line matched
5785 by `outline-regexp'. Otherwise it returns garbage.
5786 If this is called at a normal headline, the level is the number of stars.
5787 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5789 (looking-at org-outline-regexp
)
5790 (1- (- (match-end 0) (match-beginning 0)))))
5792 (defvar org-font-lock-keywords nil
)
5794 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5795 "Regular expression matching a property line.")
5797 (defvar org-font-lock-hook nil
5798 "Functions to be called for special font lock stuff.")
5800 (defvar org-font-lock-set-keywords-hook nil
5801 "Functions that can manipulate `org-font-lock-extra-keywords'.
5802 This is called after `org-font-lock-extra-keywords' is defined, but before
5803 it is installed to be used by font lock. This can be useful if something
5804 needs to be inserted at a specific position in the font-lock sequence.")
5806 (defun org-font-lock-hook (limit)
5807 (run-hook-with-args 'org-font-lock-hook limit
))
5809 (defun org-set-font-lock-defaults ()
5810 (let* ((em org-fontify-emphasized-text
)
5811 (lk org-activate-links
)
5812 (org-font-lock-extra-keywords
5815 '(org-font-lock-hook)
5817 `(,(if org-fontify-whole-heading-line
5818 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5819 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5820 (1 (org-get-level-face 1))
5821 (2 (org-get-level-face 2))
5822 (3 (org-get-level-face 3)))
5824 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5827 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5828 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5829 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5830 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5832 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5833 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5835 (list org-property-re
5836 '(1 'org-special-keyword t
)
5837 '(3 'org-property-value t
))
5839 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5840 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5841 (if (memq 'plain lk
) '(org-activate-plain-links))
5842 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5843 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5844 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5845 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5846 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5847 '(org-hide-wide-columns (0 nil append
))
5849 (list (format org-heading-keyword-regexp-format
5851 '(2 (org-get-todo-face 2) t
))
5853 (if org-fontify-done-headline
5854 (list (format org-heading-keyword-regexp-format
5857 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5859 '(2 'org-headline-done t
))
5862 '(org-font-lock-add-priority-faces)
5864 '(org-font-lock-add-tag-faces)
5866 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5867 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5868 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5869 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5872 (if (featurep 'xemacs
)
5873 '(org-do-emphasis-faces (0 nil append
))
5874 '(org-do-emphasis-faces)))
5876 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5877 1 'org-checkbox prepend
)
5878 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5879 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5880 (0 (org-get-checkbox-statistics-face) t
)))
5881 ;; Description list items
5882 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5884 ;; ARCHIVEd headings
5886 org-outline-regexp-bol
5887 "\\(.*:" org-archive-tag
":.*\\)")
5888 '(1 'org-archived prepend
))
5890 '(org-do-latex-and-special-faces)
5891 '(org-fontify-entities)
5892 '(org-raise-scripts)
5894 '(org-activate-code (1 'org-code t
))
5896 (list (format org-heading-keyword-regexp-format
5898 org-comment-string
"\\|" org-quote-string
5900 '(2 'org-special-keyword t
))
5901 '("^#.*" (0 'font-lock-comment-face t
))
5902 ;; Blocks and meta lines
5903 '(org-fontify-meta-lines-and-blocks)
5905 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5906 (run-hooks 'org-font-lock-set-keywords-hook
)
5907 ;; Now set the full font-lock-keywords
5908 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5909 (org-set-local 'font-lock-defaults
5910 '(org-font-lock-keywords t nil nil backward-paragraph
))
5911 (kill-local-variable 'font-lock-keywords
) nil
))
5913 (defun org-toggle-pretty-entities ()
5914 "Toggle the composition display of entities as UTF8 characters."
5916 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5917 (org-restart-font-lock)
5918 (if org-pretty-entities
5919 (message "Entities are displayed as UTF8 characters")
5922 (org-decompose-region (point-min) (point-max))
5923 (message "Entities are displayed plain"))))
5925 (defun org-fontify-entities (limit)
5926 "Find an entity to fontify."
5928 (when org-pretty-entities
5930 (while (re-search-forward
5931 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
5933 (if (and (not (org-in-indented-comment-line))
5934 (setq ee
(org-entity-get (match-string 1)))
5935 (= (length (nth 6 ee
)) 1))
5937 (add-text-properties
5938 (match-beginning 0) (match-end 1)
5939 (list 'font-lock-fontified t
))
5940 (compose-region (match-beginning 0) (match-end 1)
5946 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5947 "Fontify string S like in Org-mode."
5950 (let ((org-odd-levels-only odd-levels
))
5952 (font-lock-fontify-buffer)
5958 (defun org-get-level-face (n)
5959 "Get the right face for match N in font-lock matching of headlines."
5960 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5961 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5962 (if org-cycle-level-faces
5963 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5964 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
5966 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5968 (t (if org-level-color-stars-only nil org-f
))))
5971 (defun org-get-todo-face (kwd)
5972 "Get the right face for a TODO keyword KWD.
5973 If KWD is a number, get the corresponding match group."
5974 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5975 (or (org-face-from-face-or-color
5976 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5977 (and (member kwd org-done-keywords
) 'org-done
)
5980 (defun org-face-from-face-or-color (context inherit face-or-color
)
5981 "Create a face list that inherits INHERIT, but sets the foreground color.
5982 When FACE-OR-COLOR is not a string, just return it."
5983 (if (stringp face-or-color
)
5984 (list :inherit inherit
5985 (cdr (assoc context org-faces-easy-properties
))
5989 (defun org-font-lock-add-tag-faces (limit)
5990 "Add the special tag faces."
5991 (when (and org-tag-faces org-tags-special-faces-re
)
5992 (while (re-search-forward org-tags-special-faces-re limit t
)
5993 (add-text-properties (match-beginning 1) (match-end 1)
5994 (list 'face
(org-get-tag-face 1)
5995 'font-lock-fontified t
))
5996 (backward-char 1))))
5998 (defun org-font-lock-add-priority-faces (limit)
5999 "Add the special priority faces."
6000 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6001 (add-text-properties
6002 (match-beginning 0) (match-end 0)
6003 (list 'face
(or (org-face-from-face-or-color
6004 'priority
'org-special-keyword
6005 (cdr (assoc (char-after (match-beginning 1))
6006 org-priority-faces
)))
6007 'org-special-keyword
)
6008 'font-lock-fontified t
))))
6010 (defun org-get-tag-face (kwd)
6011 "Get the right face for a TODO keyword KWD.
6012 If KWD is a number, get the corresponding match group."
6013 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6014 (or (org-face-from-face-or-color
6015 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6018 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6019 "Remove fontification and activation overlays from links."
6020 (font-lock-default-unfontify-region beg end
)
6021 (let* ((buffer-undo-list t
)
6022 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6023 (inhibit-modification-hooks t
)
6024 deactivate-mark buffer-file-name buffer-file-truename
)
6025 (org-decompose-region beg end
)
6026 (remove-text-properties beg end
6027 '(mouse-face t keymap t org-linked-text t
6028 invisible t intangible t
6029 org-no-flyspell t org-emphasis t
))
6030 (org-remove-font-lock-display-properties beg end
)))
6032 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6033 ((raise 0.3) (height 0.7))
6036 "Display properties for showing superscripts and subscripts.")
6038 (defun org-remove-font-lock-display-properties (beg end
)
6039 "Remove specific display properties that have been added by font lock.
6040 The will remove the raise properties that are used to show superscripts
6044 (setq next
(next-single-property-change beg
'display nil end
)
6045 prop
(get-text-property beg
'display
))
6046 (if (member prop org-script-display
)
6047 (put-text-property beg next
'display nil
))
6050 (defun org-raise-scripts (limit)
6051 "Add raise properties to sub/superscripts."
6052 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6053 (if (re-search-forward
6054 (if (eq org-use-sub-superscripts t
)
6055 org-match-substring-regexp
6056 org-match-substring-with-braces-regexp
)
6058 (let* ((pos (point)) table-p comment-p
6059 (mpos (match-beginning 3))
6060 (emph-p (get-text-property mpos
'org-emphasis
))
6061 (link-p (get-text-property mpos
'mouse-face
))
6062 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6063 (goto-char (point-at-bol))
6064 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6065 comment-p
(org-looking-at-p "[ \t]*#"))
6067 ;; FIXME: Should we go back one character here, for a_b^c
6068 ;; (goto-char (1- pos)) ;????????????????????
6069 (if (or comment-p emph-p link-p keyw-p
)
6071 (put-text-property (match-beginning 3) (match-end 0)
6073 (if (equal (char-after (match-beginning 2)) ?^
)
6074 (nth (if table-p
3 1) org-script-display
)
6075 (nth (if table-p
2 0) org-script-display
)))
6076 (add-text-properties (match-beginning 2) (match-end 2)
6078 'org-dwidth t
'org-dwidth-n
1))
6079 (if (and (eq (char-after (match-beginning 3)) ?
{)
6080 (eq (char-before (match-end 3)) ?
}))
6082 (add-text-properties
6083 (match-beginning 3) (1+ (match-beginning 3))
6084 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6085 (add-text-properties
6086 (1- (match-end 3)) (match-end 3)
6087 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6090 ;;;; Visibility cycling, including org-goto and indirect buffer
6094 (defvar org-cycle-global-status nil
)
6095 (make-variable-buffer-local 'org-cycle-global-status
)
6096 (defvar org-cycle-subtree-status nil
)
6097 (make-variable-buffer-local 'org-cycle-subtree-status
)
6101 (defvar org-inlinetask-min-level
)
6103 (defun org-cycle (&optional arg
)
6104 "TAB-action and visibility cycling for Org-mode.
6106 This is the command invoked in Org-mode by the TAB key. Its main purpose
6107 is outline visibility cycling, but it also invokes other actions
6108 in special contexts.
6110 - When this function is called with a prefix argument, rotate the entire
6111 buffer through 3 states (global cycling)
6112 1. OVERVIEW: Show only top-level headlines.
6113 2. CONTENTS: Show all headlines of all levels, but no body text.
6114 3. SHOW ALL: Show everything.
6115 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6116 determined by the variable `org-startup-folded', and by any VISIBILITY
6117 properties in the buffer.
6118 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6119 including any drawers.
6121 - When inside a table, re-align the table and move to the next field.
6123 - When point is at the beginning of a headline, rotate the subtree started
6124 by this line through 3 different states (local cycling)
6125 1. FOLDED: Only the main headline is shown.
6126 2. CHILDREN: The main headline and the direct children are shown.
6127 From this state, you can move to one of the children
6128 and zoom in further.
6129 3. SUBTREE: Show the entire subtree, including body text.
6130 If there is no subtree, switch directly from CHILDREN to FOLDED.
6132 - When point is at the beginning of an empty headline and the variable
6133 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6134 of the headline by demoting and promoting it to likely levels. This
6135 speeds up creation document structure by pressing TAB once or several
6136 times right after creating a new headline.
6138 - When there is a numeric prefix, go up to a heading with level ARG, do
6139 a `show-subtree' and return to the previous cursor position. If ARG
6140 is negative, go up that many levels.
6142 - When point is not at the beginning of a headline, execute the global
6143 binding for TAB, which is re-indenting the line. See the option
6144 `org-cycle-emulate-tab' for details.
6146 - Special case: if point is at the beginning of the buffer and there is
6147 no headline in line 1, this function will act as if called with prefix arg
6148 (C-u TAB, same as S-TAB) also when called without prefix arg.
6149 But only if also the variable `org-cycle-global-at-bob' is t."
6151 (org-load-modules-maybe)
6152 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6153 (and org-cycle-level-after-item
/entry-creation
6154 (or (org-cycle-level)
6155 (org-cycle-item-indentation))))
6157 (or org-cycle-max-level
6158 (and (boundp 'org-inlinetask-min-level
)
6159 org-inlinetask-min-level
6160 (1- org-inlinetask-min-level
))))
6161 (nstars (and limit-level
6162 (if org-odd-levels-only
6163 (and limit-level
(1- (* limit-level
2)))
6166 (if (not (eq major-mode
'org-mode
))
6168 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6169 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6170 (not (looking-at org-outline-regexp
))))
6173 (delq 'org-optimize-window-after-visibility-change
6174 (copy-sequence org-cycle-hook
))
6178 (if (or bob-special
(equal arg
'(4)))
6179 ;; special case: use global cycling
6185 (setq last-command
'dummy
)
6186 (org-set-startup-visibility)
6187 (message "Startup visibility, plus VISIBILITY properties"))
6191 (message "Entire buffer visible, including drawers"))
6193 ;; Table: enter it or move to the next field.
6194 ((org-at-table-p 'any
)
6195 (if (org-at-table.el-p
)
6196 (message "Use C-c ' to edit table.el tables")
6197 (if arg
(org-table-edit-field t
)
6198 (org-table-justify-field-maybe)
6199 (call-interactively 'org-table-next-field
))))
6201 ((run-hook-with-args-until-success
6202 'org-tab-after-check-for-table-hook
))
6204 ;; Global cycling: delegate to `org-cycle-internal-global'.
6205 ((eq arg t
) (org-cycle-internal-global))
6207 ;; Drawers: delegate to `org-flag-drawer'.
6208 ((and org-drawers org-drawer-regexp
6210 (beginning-of-line 1)
6211 (looking-at org-drawer-regexp
)))
6212 (org-flag-drawer ; toggle block visibility
6213 (not (get-char-property (match-end 0) 'invisible
))))
6215 ;; Show-subtree, ARG levels up from here.
6218 (org-back-to-heading)
6219 (outline-up-heading (if (< arg
0) (- arg
)
6220 (- (funcall outline-level
) arg
)))
6221 (org-show-subtree)))
6223 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6224 ((and (featurep 'org-inlinetask
)
6225 (org-inlinetask-at-task-p)
6226 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6227 (org-inlinetask-toggle-visibility))
6229 ((org-try-cdlatex-tab))
6231 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6232 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6233 (save-excursion (beginning-of-line 1)
6234 (looking-at org-outline-regexp
)))
6235 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6236 (org-cycle-internal-local))
6238 ;; From there: TAB emulation and template completion.
6239 (buffer-read-only (org-back-to-heading))
6241 ((run-hook-with-args-until-success
6242 'org-tab-after-check-for-cycling-hook
))
6244 ((org-try-structure-completion))
6246 ((run-hook-with-args-until-success
6247 'org-tab-before-tab-emulation-hook
))
6249 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6251 (not (looking-at org-outline-regexp
))))
6252 (call-interactively (global-key-binding "\t")))
6254 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6255 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6256 (or (and (eq org-cycle-emulate-tab
'white
)
6257 (= (match-end 0) (point-at-eol)))
6258 (and (eq org-cycle-emulate-tab
'whitestart
)
6259 (>= (match-end 0) pos
))))
6261 (eq org-cycle-emulate-tab t
))
6262 (call-interactively (global-key-binding "\t")))
6265 (org-back-to-heading)
6268 (defun org-cycle-internal-global ()
6269 "Do the global cycling action."
6270 ;; Hack to avoid display of messages for .org attachments in Gnus
6271 (let ((ga (string-match "\\*fontification" (buffer-name))))
6273 ((and (eq last-command this-command
)
6274 (eq org-cycle-global-status
'overview
))
6275 ;; We just created the overview - now do table of contents
6276 ;; This can be slow in very large buffers, so indicate action
6277 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6278 (unless ga
(message "CONTENTS..."))
6280 (unless ga
(message "CONTENTS...done"))
6281 (setq org-cycle-global-status
'contents
)
6282 (run-hook-with-args 'org-cycle-hook
'contents
))
6284 ((and (eq last-command this-command
)
6285 (eq org-cycle-global-status
'contents
))
6286 ;; We just showed the table of contents - now show everything
6287 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6289 (unless ga
(message "SHOW ALL"))
6290 (setq org-cycle-global-status
'all
)
6291 (run-hook-with-args 'org-cycle-hook
'all
))
6294 ;; Default action: go to overview
6295 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6297 (unless ga
(message "OVERVIEW"))
6298 (setq org-cycle-global-status
'overview
)
6299 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6301 (defun org-cycle-internal-local ()
6302 "Do the local cycling action."
6303 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6304 ;; First, determine end of headline (EOH), end of subtree or item
6305 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6310 (setq struct
(org-list-struct))
6311 (setq eoh
(point-at-eol))
6312 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6313 (setq has-children
(org-list-has-child-p (point) struct
)))
6314 (org-back-to-heading)
6315 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6316 (setq eos
(save-excursion
6317 (org-end-of-subtree t
)
6319 (skip-chars-forward " \t\n"))
6320 (if (eobp) (point) (1- (point)))))
6323 (let ((level (funcall outline-level
)))
6324 (outline-next-heading)
6325 (and (org-at-heading-p t
)
6326 (> (funcall outline-level
) level
))))
6328 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6329 ;; Determine end invisible part of buffer (EOL)
6330 (beginning-of-line 2)
6331 ;; XEmacs doesn't have `next-single-char-property-change'
6332 (if (featurep 'xemacs
)
6333 (while (and (not (eobp)) ;; this is like `next-line'
6334 (get-char-property (1- (point)) 'invisible
))
6335 (beginning-of-line 2))
6336 (while (and (not (eobp)) ;; this is like `next-line'
6337 (get-char-property (1- (point)) 'invisible
))
6338 (goto-char (next-single-char-property-change (point) 'invisible
))
6339 (and (eolp) (beginning-of-line 2))))
6341 ;; Find out what to do next and set `this-command'
6344 ;; Nothing is hidden behind this heading
6345 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
6346 (message "EMPTY ENTRY")
6347 (setq org-cycle-subtree-status nil
)
6350 (outline-next-heading)
6351 (if (outline-invisible-p) (org-flag-heading nil
))))
6352 ((and (or (>= eol eos
)
6353 (not (string-match "\\S-" (buffer-substring eol eos
))))
6355 (not (setq children-skipped
6356 org-cycle-skip-children-state-if-no-children
))))
6357 ;; Entire subtree is hidden in one line: children view
6358 (run-hook-with-args 'org-pre-cycle-hook
'children
)
6360 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6362 (org-with-limited-levels (show-children))
6363 ;; FIXME: This slows down the func way too much.
6364 ;; How keep drawers hidden in subtree anyway?
6365 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6366 ;; (org-cycle-hide-drawers 'subtree))
6368 ;; Fold every list in subtree to top-level items.
6369 (when (eq org-cycle-include-plain-lists
'integrate
)
6371 (org-back-to-heading)
6372 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6373 (beginning-of-line 1)
6374 (let* ((struct (org-list-struct))
6375 (prevs (org-list-prevs-alist struct
))
6376 (end (org-list-get-bottom-point struct
)))
6377 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6378 (org-list-get-all-items (point) struct prevs
))
6379 (goto-char end
))))))
6380 (message "CHILDREN")
6383 (outline-next-heading)
6384 (if (outline-invisible-p) (org-flag-heading nil
)))
6385 (setq org-cycle-subtree-status
'children
)
6386 (run-hook-with-args 'org-cycle-hook
'children
))
6387 ((or children-skipped
6388 (and (eq last-command this-command
)
6389 (eq org-cycle-subtree-status
'children
)))
6390 ;; We just showed the children, or no children are there,
6391 ;; now show everything.
6392 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
6393 (outline-flag-region eoh eos nil
)
6394 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6395 (setq org-cycle-subtree-status
'subtree
)
6396 (run-hook-with-args 'org-cycle-hook
'subtree
))
6398 ;; Default action: hide the subtree.
6399 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6400 (outline-flag-region eoh eos t
)
6402 (setq org-cycle-subtree-status
'folded
)
6403 (run-hook-with-args 'org-cycle-hook
'folded
)))))
6406 (defun org-global-cycle (&optional arg
)
6407 "Cycle the global visibility. For details see `org-cycle'.
6408 With \\[universal-argument] prefix arg, switch to startup visibility.
6409 With a numeric prefix, show all headlines up to that level."
6411 (let ((org-cycle-include-plain-lists
6412 (if (eq major-mode
'org-mode
) org-cycle-include-plain-lists nil
)))
6416 (hide-sublevels arg
)
6417 (setq org-cycle-global-status
'contents
))
6419 (org-set-startup-visibility)
6420 (message "Startup visibility, plus VISIBILITY properties."))
6422 (org-cycle '(4))))))
6424 (defun org-set-startup-visibility ()
6425 "Set the visibility required by startup options and properties."
6427 ((eq org-startup-folded t
)
6429 ((eq org-startup-folded
'content
)
6430 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6431 (org-cycle '(4)) (org-cycle '(4)))))
6432 (unless (eq org-startup-folded
'showeverything
)
6433 (if org-hide-block-startup
(org-hide-block-all))
6434 (org-set-visibility-according-to-property 'no-cleanup
)
6435 (org-cycle-hide-archived-subtrees 'all
)
6436 (org-cycle-hide-drawers 'all
)
6437 (org-cycle-show-empty-lines t
)))
6439 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6440 "Switch subtree visibilities according to :VISIBILITY: property."
6442 (let (org-show-entry-below state
)
6444 (goto-char (point-min))
6445 (while (re-search-forward
6446 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6448 (setq state
(match-string 1))
6450 (org-back-to-heading t
)
6454 ((equal state
'("fold" "folded"))
6456 ((equal state
"children")
6457 (org-show-hidden-entry)
6459 ((equal state
"content")
6462 (org-narrow-to-subtree)
6464 ((member state
'("all" "showall"))
6467 (org-cycle-hide-archived-subtrees 'all
)
6468 (org-cycle-hide-drawers 'all
)
6469 (org-cycle-show-empty-lines 'all
)))))
6471 ;; This function uses outline-regexp instead of the more fundamental
6472 ;; org-outline-regexp so that org-cycle-global works outside of Org
6473 ;; buffers, where outline-regexp is needed.
6474 (defun org-overview ()
6475 "Switch to overview mode, showing only top-level headlines.
6476 Really, this shows all headlines with level equal or greater than the level
6477 of the first headline in the buffer. This is important, because if the
6478 first headline is not level one, then (hide-sublevels 1) gives confusing
6481 (let ((level (save-excursion
6482 (goto-char (point-min))
6483 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6485 (goto-char (match-beginning 0))
6486 (funcall outline-level
))))))
6487 (and level
(hide-sublevels level
))))
6489 (defun org-content (&optional arg
)
6490 "Show all headlines in the buffer, like a table of contents.
6491 With numerical argument N, show content up to level N."
6494 ;; Visit all headings and show their offspring
6495 (and (integerp arg
) (org-overview))
6496 (goto-char (point-max))
6498 (while (and (progn (condition-case nil
6499 (outline-previous-visible-heading 1)
6500 (error (goto-char (point-min))))
6502 (looking-at org-outline-regexp
))
6504 (show-children (1- arg
))
6506 (if (bobp) (throw 'exit nil
))))))
6509 (defun org-optimize-window-after-visibility-change (state)
6510 "Adjust the window after a change in outline visibility.
6511 This function is the default value of the hook `org-cycle-hook'."
6512 (when (get-buffer-window (current-buffer))
6514 ((eq state
'content
) nil
)
6515 ((eq state
'all
) nil
)
6516 ((eq state
'folded
) nil
)
6517 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6518 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6520 (defun org-remove-empty-overlays-at (pos)
6521 "Remove outline overlays that do not contain non-white stuff."
6524 (and (eq 'outline
(overlay-get o
'invisible
))
6525 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6527 (delete-overlay o
)))
6530 (defun org-clean-visibility-after-subtree-move ()
6531 "Fix visibility issues after moving a subtree."
6532 ;; First, find a reasonable region to look at:
6533 ;; Start two siblings above, end three below
6534 (let* ((beg (save-excursion
6535 (and (org-get-last-sibling)
6536 (org-get-last-sibling))
6538 (end (save-excursion
6539 (and (org-get-next-sibling)
6540 (org-get-next-sibling)
6541 (org-get-next-sibling))
6542 (if (org-at-heading-p)
6545 (level (looking-at "\\*+"))
6546 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6549 (narrow-to-region beg end
)
6551 ;; Properly fold already folded siblings
6552 (goto-char (point-min))
6553 (while (re-search-forward re nil t
)
6554 (if (and (not (outline-invisible-p))
6556 (goto-char (point-at-eol)) (outline-invisible-p)))
6558 (org-cycle-show-empty-lines 'overview
)
6559 (org-cycle-hide-drawers 'overview
)))))
6561 (defun org-cycle-show-empty-lines (state)
6562 "Show empty lines above all visible headlines.
6563 The region to be covered depends on STATE when called through
6564 `org-cycle-hook'. Lisp program can use t for STATE to get the
6565 entire buffer covered. Note that an empty line is only shown if there
6566 are at least `org-cycle-separator-lines' empty lines before the headline."
6567 (when (not (= org-cycle-separator-lines
0))
6569 (let* ((n (abs org-cycle-separator-lines
))
6571 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6572 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6573 (t (let ((ns (number-to-string (- n
2))))
6574 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6575 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6578 ((memq state
'(overview contents t
))
6579 (setq beg
(point-min) end
(point-max)))
6580 ((memq state
'(children folded
))
6581 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6582 (beginning-of-line 2)
6586 (while (re-search-forward re end t
)
6587 (unless (get-char-property (match-end 1) 'invisible
)
6588 (setq e
(match-end 1))
6589 (if (< org-cycle-separator-lines
0)
6590 (setq b
(save-excursion
6591 (goto-char (match-beginning 0))
6592 (org-back-over-empty-lines)
6594 (goto-char (max (point-min) (1- (point))))
6598 (setq b
(match-beginning 1)))
6599 (outline-flag-region b e nil
)))))))
6600 ;; Never hide empty lines at the end of the file.
6602 (goto-char (point-max))
6603 (outline-previous-heading)
6604 (outline-end-of-heading)
6605 (if (and (looking-at "[ \t\n]+")
6606 (= (match-end 0) (point-max)))
6607 (outline-flag-region (point) (match-end 0) nil
))))
6609 (defun org-show-empty-lines-in-parent ()
6610 "Move to the parent and re-show empty lines before visible headlines."
6612 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6613 (org-cycle-show-empty-lines context
))))
6615 (defun org-files-list ()
6616 "Return `org-agenda-files' list, plus all open org-mode files.
6617 This is useful for operations that need to scan all of a user's
6618 open and agenda-wise Org files."
6619 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6620 (dolist (buf (buffer-list))
6621 (with-current-buffer buf
6622 (if (and (eq major-mode
'org-mode
) (buffer-file-name))
6623 (let ((file (expand-file-name (buffer-file-name))))
6624 (unless (member file files
)
6625 (push file files
))))))
6628 (defsubst org-entry-beginning-position
()
6629 "Return the beginning position of the current entry."
6630 (save-excursion (outline-back-to-heading t
) (point)))
6632 (defsubst org-entry-end-position
()
6633 "Return the end position of the current entry."
6634 (save-excursion (outline-next-heading) (point)))
6636 (defun org-cycle-hide-drawers (state)
6637 "Re-hide all drawers after a visibility state change."
6638 (when (and (eq major-mode
'org-mode
)
6639 (not (memq state
'(overview folded contents
))))
6641 (let* ((globalp (memq state
'(contents all
)))
6642 (beg (if globalp
(point-min) (point)))
6643 (end (if globalp
(point-max)
6644 (if (eq state
'children
)
6645 (save-excursion (outline-next-heading) (point))
6646 (org-end-of-subtree t
)))))
6648 (while (re-search-forward org-drawer-regexp end t
)
6649 (org-flag-drawer t
))))))
6651 (defun org-flag-drawer (flag)
6653 (beginning-of-line 1)
6654 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6655 (let ((b (match-end 0)))
6656 (if (re-search-forward
6658 (save-excursion (outline-next-heading) (point)) t
)
6659 (outline-flag-region b
(point-at-eol) flag
)
6660 (error ":END: line missing at position %s" b
))))))
6662 (defun org-subtree-end-visible-p ()
6663 "Is the end of the current subtree visible?"
6664 (pos-visible-in-window-p
6665 (save-excursion (org-end-of-subtree t
) (point))))
6667 (defun org-first-headline-recenter (&optional N
)
6668 "Move cursor to the first headline and recenter the headline.
6669 Optional argument N means put the headline into the Nth line of the window."
6670 (goto-char (point-min))
6671 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6673 (recenter (prefix-numeric-value N
))))
6675 ;;; Saving and restoring visibility
6677 (defun org-outline-overlay-data (&optional use-markers
)
6678 "Return a list of the locations of all outline overlays.
6679 These are overlays with the `invisible' property value `outline'.
6680 The return value is a list of cons cells, with start and stop
6681 positions for each overlay.
6682 If USE-MARKERS is set, return the positions as markers."
6689 (when (eq (overlay-get o
'invisible
) 'outline
)
6690 (setq beg
(overlay-start o
)
6691 end
(overlay-end o
))
6692 (and beg end
(> end beg
)
6694 (cons (move-marker (make-marker) beg
)
6695 (move-marker (make-marker) end
))
6697 (overlays-in (point-min) (point-max))))))))
6699 (defun org-set-outline-overlay-data (data)
6700 "Create visibility overlays for all positions in DATA.
6701 DATA should have been made by `org-outline-overlay-data'."
6708 (outline-flag-region (car c
) (cdr c
) t
))
6711 ;;; Folding of blocks
6713 (defvar org-hide-block-overlays nil
6714 "Overlays hiding blocks.")
6715 (make-variable-buffer-local 'org-hide-block-overlays
)
6717 (defun org-block-map (function &optional start end
)
6718 "Call FUNCTION at the head of all source blocks in the current buffer.
6719 Optional arguments START and END can be used to limit the range."
6720 (let ((start (or start
(point-min)))
6721 (end (or end
(point-max))))
6724 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6727 (goto-char (match-beginning 0))
6728 (funcall function
)))))))
6730 (defun org-hide-block-toggle-all ()
6731 "Toggle the visibility of all blocks in the current buffer."
6732 (org-block-map #'org-hide-block-toggle
))
6734 (defun org-hide-block-all ()
6735 "Fold all blocks in the current buffer."
6737 (org-show-block-all)
6738 (org-block-map #'org-hide-block-toggle-maybe
))
6740 (defun org-show-block-all ()
6741 "Unfold all blocks in the current buffer."
6743 (mapc 'delete-overlay org-hide-block-overlays
)
6744 (setq org-hide-block-overlays nil
))
6746 (defun org-hide-block-toggle-maybe ()
6747 "Toggle visibility of block at point."
6749 (let ((case-fold-search t
))
6751 (beginning-of-line 1)
6752 (looking-at org-block-regexp
))
6753 (progn (org-hide-block-toggle)
6754 t
) ;; to signal that we took action
6755 nil
))) ;; to signal that we did not
6757 (defun org-hide-block-toggle (&optional force
)
6758 "Toggle the visibility of the current block."
6762 (if (re-search-forward org-block-regexp nil t
)
6763 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6764 (end (match-end 0)) ;; end of entire body
6766 (if (memq t
(mapcar (lambda (overlay)
6767 (eq (overlay-get overlay
'invisible
)
6769 (overlays-at start
)))
6770 (if (or (not force
) (eq force
'off
))
6772 (when (member ov org-hide-block-overlays
)
6773 (setq org-hide-block-overlays
6774 (delq ov org-hide-block-overlays
)))
6775 (when (eq (overlay-get ov
'invisible
)
6777 (delete-overlay ov
)))
6778 (overlays-at start
)))
6779 (setq ov
(make-overlay start end
))
6780 (overlay-put ov
'invisible
'org-hide-block
)
6781 ;; make the block accessible to isearch
6783 ov
'isearch-open-invisible
6785 (when (member ov org-hide-block-overlays
)
6786 (setq org-hide-block-overlays
6787 (delq ov org-hide-block-overlays
)))
6788 (when (eq (overlay-get ov
'invisible
)
6790 (delete-overlay ov
))))
6791 (push ov org-hide-block-overlays
)))
6792 (error "Not looking at a source block"))))
6794 ;; org-tab-after-check-for-cycling-hook
6795 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6796 ;; Remove overlays when changing major mode
6797 (add-hook 'org-mode-hook
6798 (lambda () (org-add-hook 'change-major-mode-hook
6799 'org-show-block-all
'append
'local
)))
6803 (defvar org-goto-window-configuration nil
)
6804 (defvar org-goto-marker nil
)
6805 (defvar org-goto-map
6806 (let ((map (make-sparse-keymap)))
6807 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6808 (while (setq cmd
(pop cmds
))
6809 (substitute-key-definition cmd cmd map global-map
)))
6810 (suppress-keymap map
)
6811 (org-defkey map
"\C-m" 'org-goto-ret
)
6812 (org-defkey map
[(return)] 'org-goto-ret
)
6813 (org-defkey map
[(left)] 'org-goto-left
)
6814 (org-defkey map
[(right)] 'org-goto-right
)
6815 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6816 (org-defkey map
"\C-i" 'org-cycle
)
6817 (org-defkey map
[(tab)] 'org-cycle
)
6818 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6819 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6820 (if org-goto-auto-isearch
6821 (if (fboundp 'define-key-after
)
6822 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6824 (org-defkey map "q" 'org-goto-quit)
6825 (org-defkey map "n" 'outline-next-visible-heading)
6826 (org-defkey map "p" 'outline-previous-visible-heading)
6827 (org-defkey map "f" 'outline-forward-same-level)
6828 (org-defkey map "b" 'outline-backward-same-level)
6829 (org-defkey map "u" 'outline-up-heading))
6830 (org-defkey map "/" 'org-occur)
6831 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6832 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6833 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6834 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6835 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6838 (defconst org-goto-help
6839 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6840 RET=jump to location [Q]uit and return to previous location
6841 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6843 (defvar org-goto-start-pos) ; dynamically scoped parameter
6845 ;; FIXME: Docstring does not mention both interfaces
6846 (defun org-goto (&optional alternative-interface)
6847 "Look up a different location in the current file, keeping current visibility.
6849 When you want look-up or go to a different location in a document, the
6850 fastest way is often to fold the entire buffer and then dive into the tree.
6851 This method has the disadvantage, that the previous location will be folded,
6852 which may not be what you want.
6854 This command works around this by showing a copy of the current buffer
6855 in an indirect buffer, in overview mode. You can dive into the tree in
6856 that copy, use org-occur and incremental search to find a location.
6857 When pressing RET or `Q', the command returns to the original buffer in
6858 which the visibility is still unchanged. After RET it will also jump to
6859 the location selected in the indirect buffer and expose the headline
6862 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6863 (org-refile-use-outline-path t)
6864 (org-refile-target-verify-function nil)
6866 (if (not alternative-interface)
6868 (if (eq org-goto-interface 'outline)
6869 'outline-path-completion
6871 (org-goto-start-pos (point))
6873 (if (eq interface 'outline)
6874 (car (org-get-location (current-buffer) org-goto-help))
6875 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6876 (org-refile-check-position pa)
6880 (org-mark-ring-push org-goto-start-pos)
6881 (goto-char selected-point)
6882 (if (or (outline-invisible-p) (org-invisible-p2))
6883 (org-show-context 'org-goto)))
6886 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6887 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6888 (defvar org-goto-local-auto-isearch-map) ; defined below
6890 (defun org-get-location (buf help)
6891 "Let the user select a location in the Org-mode buffer BUF.
6892 This function uses a recursive edit. It returns the selected position
6894 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6895 (isearch-hide-immediately nil)
6896 (isearch-search-fun-function
6897 (lambda () 'org-goto-local-search-headings))
6898 (org-goto-selected-point org-goto-exit-command)
6900 (special-display-buffer-names nil)
6901 (special-display-regexps nil)
6902 (special-display-function nil))
6904 (save-window-excursion
6905 (delete-other-windows)
6906 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6907 (org-pop-to-buffer-same-window
6909 (make-indirect-buffer (current-buffer) "*org-goto*")
6910 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6911 (with-output-to-temp-buffer "*Help*"
6913 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6914 (setq buffer-read-only nil)
6915 (let ((org-startup-truncated t)
6916 (org-startup-folded nil)
6917 (org-startup-align-all-tables nil))
6920 (setq buffer-read-only t)
6921 (if (and (boundp 'org-goto-start-pos)
6922 (integer-or-marker-p org-goto-start-pos))
6923 (let ((org-show-hierarchy-above t)
6924 (org-show-siblings t)
6925 (org-show-following-heading t))
6926 (goto-char org-goto-start-pos)
6927 (and (outline-invisible-p) (org-show-context)))
6928 (goto-char (point-min)))
6929 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6930 (message "Select location and press RET")
6931 (use-local-map org-goto-map)
6934 (kill-buffer "*org-goto*")
6935 (cons org-goto-selected-point org-goto-exit-command)))
6937 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6938 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6939 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6940 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6942 (defun org-goto-local-search-headings (string bound noerror)
6943 "Search and make sure that any matches are in headlines."
6945 (while (if isearch-forward
6946 (search-forward string bound noerror)
6947 (search-backward string bound noerror))
6948 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6949 (and (member :headline context)
6950 (not (member :tags context))))
6951 (throw 'return (point))))))
6953 (defun org-goto-local-auto-isearch ()
6956 (goto-char (point-min))
6957 (let ((keys (this-command-keys)))
6958 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6960 (isearch-process-search-char (string-to-char keys)))))
6962 (defun org-goto-ret (&optional arg)
6963 "Finish `org-goto' by going to the new location."
6965 (setq org-goto-selected-point (point)
6966 org-goto-exit-command 'return)
6969 (defun org-goto-left ()
6970 "Finish `org-goto' by going to the new location."
6972 (if (org-at-heading-p)
6974 (beginning-of-line 1)
6975 (setq org-goto-selected-point (point)
6976 org-goto-exit-command 'left)
6978 (error "Not on a heading")))
6980 (defun org-goto-right ()
6981 "Finish `org-goto' by going to the new location."
6983 (if (org-at-heading-p)
6985 (setq org-goto-selected-point (point)
6986 org-goto-exit-command 'right)
6988 (error "Not on a heading")))
6990 (defun org-goto-quit ()
6991 "Finish `org-goto' without cursor motion."
6993 (setq org-goto-selected-point nil)
6994 (setq org-goto-exit-command 'quit)
6997 ;;; Indirect buffer display of subtrees
6999 (defvar org-indirect-dedicated-frame nil
7000 "This is the frame being used for indirect tree display.")
7001 (defvar org-last-indirect-buffer nil)
7003 (defun org-tree-to-indirect-buffer (&optional arg)
7004 "Create indirect buffer and narrow it to current subtree.
7005 With numerical prefix ARG, go up to this level and then take that tree.
7006 If ARG is negative, go up that many levels.
7007 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7008 indirect buffer previously made with this command, to avoid proliferation of
7009 indirect buffers. However, when you call the command with a \
7010 \\[universal-argument] prefix, or
7011 when `org-indirect-buffer-display' is `new-frame', the last buffer
7012 is kept so that you can work with several indirect buffers at the same time.
7013 If `org-indirect-buffer-display' is `dedicated-frame', the \
7014 \\[universal-argument] prefix also
7015 requests that a new frame be made for the new buffer, so that the dedicated
7016 frame is not changed."
7018 (let ((cbuf (current-buffer))
7019 (cwin (selected-window))
7021 beg end level heading ibuf)
7023 (org-back-to-heading t)
7025 (setq level (org-outline-level))
7026 (if (< arg 0) (setq arg (+ level arg)))
7027 (while (> (setq level (org-outline-level)) arg)
7028 (outline-up-heading 1 t)))
7030 heading (org-get-heading))
7031 (org-end-of-subtree t t)
7032 (if (org-at-heading-p) (backward-char 1))
7034 (if (and (buffer-live-p org-last-indirect-buffer)
7035 (not (eq org-indirect-buffer-display 'new-frame))
7037 (kill-buffer org-last-indirect-buffer))
7038 (setq ibuf (org-get-indirect-buffer cbuf)
7039 org-last-indirect-buffer ibuf)
7041 ((or (eq org-indirect-buffer-display 'new-frame)
7042 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7043 (select-frame (make-frame))
7044 (delete-other-windows)
7045 (org-pop-to-buffer-same-window ibuf)
7046 (org-set-frame-title heading))
7047 ((eq org-indirect-buffer-display 'dedicated-frame)
7049 (select-frame (or (and org-indirect-dedicated-frame
7050 (frame-live-p org-indirect-dedicated-frame)
7051 org-indirect-dedicated-frame)
7052 (setq org-indirect-dedicated-frame (make-frame)))))
7053 (delete-other-windows)
7054 (org-pop-to-buffer-same-window ibuf)
7055 (org-set-frame-title (concat "Indirect: " heading)))
7056 ((eq org-indirect-buffer-display 'current-window)
7057 (org-pop-to-buffer-same-window ibuf))
7058 ((eq org-indirect-buffer-display 'other-window)
7059 (pop-to-buffer ibuf))
7060 (t (error "Invalid value")))
7061 (if (featurep 'xemacs)
7062 (save-excursion (org-mode) (turn-on-font-lock)))
7063 (narrow-to-region beg end)
7066 (run-hook-with-args 'org-cycle-hook 'all)
7067 (and (window-live-p cwin) (select-window cwin))))
7069 (defun org-get-indirect-buffer (&optional buffer)
7070 (setq buffer (or buffer (current-buffer)))
7071 (let ((n 1) (base (buffer-name buffer)) bname)
7072 (while (buffer-live-p
7073 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7076 (make-indirect-buffer buffer bname 'clone)
7077 (error (make-indirect-buffer buffer bname)))))
7079 (defun org-set-frame-title (title)
7080 "Set the title of the current frame to the string TITLE."
7081 ;; FIXME: how to name a single frame in XEmacs???
7082 (unless (featurep 'xemacs)
7083 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7085 ;;;; Structure editing
7087 ;;; Inserting headlines
7089 (defun org-previous-line-empty-p ()
7092 (or (beginning-of-line 0) t)
7094 (looking-at "[ \t]*$")))))
7096 (defun org-insert-heading (&optional force-heading invisible-ok)
7097 "Insert a new heading or item with same depth at point.
7098 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7099 If point is at the beginning of a headline, insert a sibling before the
7100 current headline. If point is not at the beginning, split the line,
7101 create the new headline with the text in the current line after point
7102 \(but see also the variable `org-M-RET-may-split-line').
7104 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7105 This is important for non-interactive uses of the command."
7107 (if (or (= (buffer-size) 0)
7108 (and (not (save-excursion
7109 (and (ignore-errors (org-back-to-heading invisible-ok))
7110 (org-at-heading-p))))
7111 (or force-heading (not (org-in-item-p)))))
7114 (run-hooks 'org-insert-heading-hook))
7115 (when (or force-heading (not (org-insert-item)))
7116 (let* ((empty-line-p nil)
7118 (on-heading (org-at-heading-p))
7119 (head (save-excursion
7122 (org-back-to-heading invisible-ok)
7123 (when (and (not on-heading)
7124 (featurep 'org-inlinetask)
7125 (integerp org-inlinetask-min-level)
7126 (>= (length (match-string 0))
7127 org-inlinetask-min-level))
7128 ;; Find a heading level before the inline task
7129 (while (and (setq level (org-up-heading-safe))
7130 (>= level org-inlinetask-min-level)))
7131 (if (org-at-heading-p)
7132 (org-back-to-heading invisible-ok)
7133 (error "This should not happen")))
7134 (setq empty-line-p (org-previous-line-empty-p))
7137 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7138 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7139 pos hide-previous previous-pos)
7141 ((and (org-at-heading-p) (bolp)
7143 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7144 ;; insert before the current line
7145 (open-line (if blank 2 1)))
7147 (not org-insert-heading-respect-content)
7150 (backward-char 1) (not (outline-invisible-p)))))
7151 ;; insert right here
7154 ;; somewhere in the line
7156 (setq previous-pos (point-at-bol))
7158 (setq hide-previous (outline-invisible-p)))
7159 (and org-insert-heading-respect-content (org-show-subtree))
7161 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7164 (goto-char (point-at-bol))
7165 (and (looking-at org-complex-heading-regexp)
7167 (> p (match-beginning 4)))))))
7170 (org-insert-heading-respect-content
7171 (org-end-of-subtree nil t)
7172 (when (featurep 'org-inlinetask)
7173 (while (and (not (eobp))
7174 (looking-at "\\(\\*+\\)[ \t]+")
7175 (>= (length (match-string 1))
7176 org-inlinetask-min-level))
7177 (org-end-of-subtree nil t)))
7178 (or (bolp) (newline))
7179 (or (org-previous-line-empty-p)
7180 (and blank (newline)))
7186 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7187 (setq tags (and (match-end 2) (match-string 2)))
7189 (delete-region (match-beginning 1) (match-end 1)))
7190 (setq pos (point-at-bol))
7191 (or split (end-of-line 1))
7192 (delete-horizontal-space)
7193 (if (string-match "\\`\\*+\\'"
7194 (buffer-substring (point-at-bol) (point)))
7196 (newline (if blank 2 1))
7202 (org-set-tags nil 'align))))
7204 (or split (end-of-line 1))
7205 (newline (if blank 2 1)))))))
7206 (insert head) (just-one-space)
7209 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7210 (when (and org-insert-heading-respect-content hide-previous)
7212 (goto-char previous-pos)
7214 (run-hooks 'org-insert-heading-hook)))))
7216 (defun org-get-heading (&optional no-tags no-todo)
7217 "Return the heading of the current entry, without the stars.
7218 When NO-TAGS is non-nil, don't include tags.
7219 When NO-TODO is non-nil, don't include TODO keywords."
7221 (org-back-to-heading t)
7223 ((and no-tags no-todo)
7224 (looking-at org-complex-heading-regexp)
7227 (looking-at (concat org-outline-regexp
7229 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7232 (looking-at org-todo-line-regexp)
7234 (t (looking-at org-heading-regexp)
7235 (match-string 2)))))
7237 (defun org-heading-components ()
7238 "Return the components of the current heading.
7239 This is a list with the following elements:
7240 - the level as an integer
7241 - the reduced level, different if `org-odd-levels-only' is set.
7242 - the TODO keyword, or nil
7243 - the priority character, like ?A, or nil if no priority is given
7244 - the headline text itself, or the tags string if no headline text
7245 - the tags string, or nil."
7247 (org-back-to-heading t)
7248 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7249 (list (length (match-string 1))
7250 (org-reduced-level (length (match-string 1)))
7251 (org-match-string-no-properties 2)
7252 (and (match-end 3) (aref (match-string 3) 2))
7253 (org-match-string-no-properties 4)
7254 (org-match-string-no-properties 5)))))
7256 (defun org-get-entry ()
7257 "Get the entry text, after heading, entire subtree."
7259 (org-back-to-heading t)
7260 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7262 (defun org-insert-heading-after-current ()
7263 "Insert a new heading with same level as current, after current subtree."
7265 (org-back-to-heading)
7266 (org-insert-heading)
7267 (org-move-subtree-down)
7270 (defun org-insert-heading-respect-content ()
7272 (let ((org-insert-heading-respect-content t))
7273 (org-insert-heading t)))
7275 (defun org-insert-todo-heading-respect-content (&optional force-state)
7277 (let ((org-insert-heading-respect-content t))
7278 (org-insert-todo-heading force-state t)))
7280 (defun org-insert-todo-heading (arg &optional force-heading)
7281 "Insert a new heading with the same level and TODO state as current heading.
7282 If the heading has no TODO state, or if the state is DONE, use the first
7283 state (TODO by default). Also with prefix arg, force first state."
7285 (when (or force-heading (not (org-insert-item 'checkbox)))
7286 (org-insert-heading force-heading)
7288 (org-back-to-heading)
7289 (outline-previous-heading)
7290 (looking-at org-todo-line-regexp))
7294 (not (match-beginning 2))
7295 (member (match-string 2) org-done-keywords))
7296 (car org-todo-keywords-1)
7300 (run-hook-with-args-until-success
7301 'org-todo-get-default-hook new-mark-x nil)
7303 (beginning-of-line 1)
7304 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7305 (if org-treat-insert-todo-heading-as-state-change
7307 (insert new-mark " "))))
7308 (when org-provide-todo-statistics
7309 (org-update-parent-todo-statistics))))
7311 (defun org-insert-subheading (arg)
7312 "Insert a new subheading and demote it.
7313 Works for outline headings and for plain lists alike."
7315 (org-insert-heading arg)
7317 ((org-at-heading-p) (org-do-demote))
7318 ((org-at-item-p) (org-indent-item))))
7320 (defun org-insert-todo-subheading (arg)
7321 "Insert a new subheading with TODO keyword or checkbox and demote it.
7322 Works for outline headings and for plain lists alike."
7324 (org-insert-todo-heading arg)
7326 ((org-at-heading-p) (org-do-demote))
7327 ((org-at-item-p) (org-indent-item))))
7329 ;;; Promotion and Demotion
7331 (defvar org-after-demote-entry-hook nil
7332 "Hook run after an entry has been demoted.
7333 The cursor will be at the beginning of the entry.
7334 When a subtree is being demoted, the hook will be called for each node.")
7336 (defvar org-after-promote-entry-hook nil
7337 "Hook run after an entry has been promoted.
7338 The cursor will be at the beginning of the entry.
7339 When a subtree is being promoted, the hook will be called for each node.")
7341 (defun org-promote-subtree ()
7342 "Promote the entire subtree.
7343 See also `org-promote'."
7346 (org-with-limited-levels (org-map-tree 'org-promote)))
7347 (org-fix-position-after-promote))
7349 (defun org-demote-subtree ()
7350 "Demote the entire subtree. See `org-demote'.
7351 See also `org-promote'."
7354 (org-with-limited-levels (org-map-tree 'org-demote)))
7355 (org-fix-position-after-promote))
7358 (defun org-do-promote ()
7359 "Promote the current heading higher up the tree.
7360 If the region is active in `transient-mark-mode', promote all headings
7364 (if (org-region-active-p)
7365 (org-map-region 'org-promote (region-beginning) (region-end))
7367 (org-fix-position-after-promote))
7369 (defun org-do-demote ()
7370 "Demote the current heading lower down the tree.
7371 If the region is active in `transient-mark-mode', demote all headings
7375 (if (org-region-active-p)
7376 (org-map-region 'org-demote (region-beginning) (region-end))
7378 (org-fix-position-after-promote))
7380 (defun org-fix-position-after-promote ()
7381 "Make sure that after pro/demotion cursor position is right."
7382 (let ((pos (point)))
7383 (when (save-excursion
7384 (beginning-of-line 1)
7385 (looking-at org-todo-line-regexp)
7386 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7387 (cond ((eobp) (insert " "))
7388 ((eolp) (insert " "))
7389 ((equal (char-after) ?\ ) (forward-char 1))))))
7391 (defun org-current-level ()
7392 "Return the level of the current entry, or nil if before the first headline.
7393 The level is the number of stars at the beginning of the headline."
7395 (org-with-limited-levels
7396 (if (ignore-errors (org-back-to-heading t))
7397 (funcall outline-level)))))
7399 (defun org-get-previous-line-level ()
7400 "Return the outline depth of the last headline before the current line.
7401 Returns 0 for the first headline in the buffer, and nil if before the
7403 (let ((current-level (org-current-level))
7404 (prev-level (when (> (line-number-at-pos) 1)
7406 (beginning-of-line 0)
7407 (org-current-level)))))
7408 (cond ((null current-level) nil) ; Before first headline
7409 ((null prev-level) 0) ; At first headline
7412 (defun org-reduced-level (l)
7413 "Compute the effective level of a heading.
7414 This takes into account the setting of `org-odd-levels-only'."
7417 (org-odd-levels-only (1+ (floor (/ l 2))))
7420 (defun org-level-increment ()
7421 "Return the number of stars that will be added or removed at a
7422 time to headlines when structure editing, based on the value of
7423 `org-odd-levels-only'."
7424 (if org-odd-levels-only 2 1))
7426 (defun org-get-valid-level (level &optional change)
7427 "Rectify a level change under the influence of `org-odd-levels-only'
7428 LEVEL is a current level, CHANGE is by how much the level should be
7429 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7430 even level numbers will become the next higher odd number."
7431 (if org-odd-levels-only
7432 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7433 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7434 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7435 (max 1 (+ level (or change 0)))))
7437 (if (boundp 'define-obsolete-function-alias)
7438 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7439 (define-obsolete-function-alias 'org-get-legal-level
7440 'org-get-valid-level)
7441 (define-obsolete-function-alias 'org-get-legal-level
7442 'org-get-valid-level "23.1")))
7444 (defun org-promote ()
7445 "Promote the current heading higher up the tree.
7446 If the region is active in `transient-mark-mode', promote all headings
7448 (org-back-to-heading t)
7449 (let* ((level (save-match-data (funcall outline-level)))
7450 (after-change-functions (remove 'flyspell-after-change-function
7451 after-change-functions))
7452 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7453 (diff (abs (- level (length up-head) -1))))
7454 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
7455 (replace-match up-head nil t)
7456 ;; Fixup tag positioning
7457 (and org-auto-align-tags (org-set-tags nil t))
7458 (if org-adapt-indentation (org-fixup-indentation (- diff)))
7459 (run-hooks 'org-after-promote-entry-hook)))
7461 (defun org-demote ()
7462 "Demote the current heading lower down the tree.
7463 If the region is active in `transient-mark-mode', demote all headings
7465 (org-back-to-heading t)
7466 (let* ((level (save-match-data (funcall outline-level)))
7467 (after-change-functions (remove 'flyspell-after-change-function
7468 after-change-functions))
7469 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7470 (diff (abs (- level (length down-head) -1))))
7471 (replace-match down-head nil t)
7472 ;; Fixup tag positioning
7473 (and org-auto-align-tags (org-set-tags nil t))
7474 (if org-adapt-indentation (org-fixup-indentation diff))
7475 (run-hooks 'org-after-demote-entry-hook)))
7477 (defun org-cycle-level ()
7478 "Cycle the level of an empty headline through possible states.
7479 This goes first to child, then to parent, level, then up the hierarchy.
7480 After top level, it switches back to sibling level."
7482 (let ((org-adapt-indentation nil))
7483 (when (org-point-at-end-of-empty-headline)
7484 (setq this-command 'org-cycle-level) ; Only needed for caching
7485 (let ((cur-level (org-current-level))
7486 (prev-level (org-get-previous-line-level)))
7488 ;; If first headline in file, promote to top-level.
7490 (loop repeat (/ (- cur-level 1) (org-level-increment))
7491 do (org-do-promote)))
7492 ;; If same level as prev, demote one.
7493 ((= prev-level cur-level)
7495 ;; If parent is top-level, promote to top level if not already.
7497 (loop repeat (/ (- cur-level 1) (org-level-increment))
7498 do (org-do-promote)))
7499 ;; If top-level, return to prev-level.
7501 (loop repeat (/ (- prev-level 1) (org-level-increment))
7502 do (org-do-demote)))
7503 ;; If less than prev-level, promote one.
7504 ((< cur-level prev-level)
7506 ;; If deeper than prev-level, promote until higher than
7508 ((> cur-level prev-level)
7509 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7510 do (org-do-promote))))
7513 (defun org-map-tree (fun)
7514 "Call FUN for every heading underneath the current one."
7515 (org-back-to-heading)
7516 (let ((level (funcall outline-level)))
7520 (outline-next-heading)
7521 (> (funcall outline-level) level))
7525 (defun org-map-region (fun beg end)
7526 "Call FUN for every heading between BEG and END."
7527 (let ((org-ignore-region t))
7529 (setq end (copy-marker end))
7531 (if (and (re-search-forward org-outline-regexp-bol nil t)
7535 (outline-next-heading)
7540 (defvar org-property-end-re) ; silence byte-compiler
7541 (defun org-fixup-indentation (diff)
7542 "Change the indentation in the current entry by DIFF.
7543 However, if any line in the current entry has no indentation, or if it
7544 would end up with no indentation after the change, nothing at all is done."
7546 (let ((end (save-excursion (outline-next-heading)
7548 (prohibit (if (> diff 0)
7550 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7552 (unless (save-excursion (end-of-line 1)
7553 (re-search-forward prohibit end t))
7554 (while (and (< (point) end)
7555 (re-search-forward "^[ \t]+" end t))
7556 (goto-char (match-end 0))
7557 (setq col (current-column))
7558 (if (< diff 0) (replace-match ""))
7559 (org-indent-to-column (+ diff col))))
7560 (move-marker end nil))))
7562 (defun org-convert-to-odd-levels ()
7563 "Convert an org-mode file with all levels allowed to one with odd levels.
7564 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7567 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7568 (let ((outline-level 'org-outline-level)
7569 (org-odd-levels-only nil) n)
7571 (goto-char (point-min))
7572 (while (re-search-forward "^\\*\\*+ " nil t)
7573 (setq n (- (length (match-string 0)) 2))
7574 (while (>= (setq n (1- n)) 0)
7576 (end-of-line 1))))))
7578 (defun org-convert-to-oddeven-levels ()
7579 "Convert an org-mode file with only odd levels to one with odd/even levels.
7580 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7581 file contains a section with an even level, conversion would
7582 destroy the structure of the file. An error is signaled in this
7585 (goto-char (point-min))
7586 ;; First check if there are no even levels
7587 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7588 (org-show-context t)
7589 (error "Not all levels are odd in this file. Conversion not possible"))
7590 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7591 (let ((outline-regexp org-outline-regexp)
7592 (outline-level 'org-outline-level)
7593 (org-odd-levels-only nil) n)
7595 (goto-char (point-min))
7596 (while (re-search-forward "^\\*\\*+ " nil t)
7597 (setq n (/ (1- (length (match-string 0))) 2))
7598 (while (>= (setq n (1- n)) 0)
7600 (end-of-line 1))))))
7602 (defun org-tr-level (n)
7603 "Make N odd if required."
7604 (if org-odd-levels-only (1+ (/ n 2)) n))
7606 ;;; Vertical tree motion, cutting and pasting of subtrees
7608 (defun org-move-subtree-up (&optional arg)
7609 "Move the current subtree up past ARG headlines of the same level."
7611 (org-move-subtree-down (- (prefix-numeric-value arg))))
7613 (defun org-move-subtree-down (&optional arg)
7614 "Move the current subtree down past ARG headlines of the same level."
7616 (setq arg (prefix-numeric-value arg))
7617 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7618 'org-get-last-sibling))
7619 (ins-point (make-marker))
7621 (col (current-column))
7622 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7624 (org-back-to-heading)
7627 (setq ne-beg (org-back-over-empty-lines))
7630 (save-excursion (outline-end-of-heading)
7631 (setq folded (outline-invisible-p)))
7632 (outline-end-of-subtree))
7633 (outline-next-heading)
7634 (setq ne-end (org-back-over-empty-lines))
7637 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7638 ;; include less whitespace
7641 (forward-line (- ne-beg ne-end))
7642 (setq beg (point))))
7643 ;; Find insertion point, with error handling
7645 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7646 (progn (goto-char beg0)
7647 (error "Cannot move past superior level or buffer limit")))
7648 (setq cnt (1- cnt)))
7650 ;; Moving forward - still need to move over subtree
7651 (progn (org-end-of-subtree t t)
7653 (org-back-over-empty-lines)
7654 (or (bolp) (newline)))))
7655 (setq ne-ins (org-back-over-empty-lines))
7656 (move-marker ins-point (point))
7657 (setq txt (buffer-substring beg end))
7658 (org-save-markers-in-region beg end)
7659 (delete-region beg end)
7660 (org-remove-empty-overlays-at beg)
7661 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7662 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7663 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7664 (let ((bbb (point)))
7665 (insert-before-markers txt)
7666 (org-reinstall-markers-in-region bbb)
7667 (move-marker ins-point bbb))
7668 (or (bolp) (insert "\n"))
7669 (setq ins-end (point))
7670 (goto-char ins-point)
7671 (org-skip-whitespace)
7672 (when (and (< arg 0)
7673 (org-first-sibling-p)
7675 ;; Move whitespace back to beginning
7678 (let ((kill-whole-line t))
7679 (kill-line (- ne-ins ne-beg)) (point)))
7680 (insert (make-string (- ne-ins ne-beg) ?\n)))
7681 (move-marker ins-point nil)
7686 (org-cycle-hide-drawers 'children))
7687 (org-clean-visibility-after-subtree-move)
7688 ;; move back to the initial column we were at
7689 (move-to-column col)))
7691 (defvar org-subtree-clip ""
7692 "Clipboard for cut and paste of subtrees.
7693 This is actually only a copy of the kill, because we use the normal kill
7694 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7696 (defvar org-subtree-clip-folded nil
7697 "Was the last copied subtree folded?
7698 This is used to fold the tree back after pasting.")
7700 (defun org-cut-subtree (&optional n)
7701 "Cut the current subtree into the clipboard.
7702 With prefix arg N, cut this many sequential subtrees.
7703 This is a short-hand for marking the subtree and then cutting it."
7705 (org-copy-subtree n 'cut))
7707 (defun org-copy-subtree (&optional n cut force-store-markers)
7708 "Cut the current subtree into the clipboard.
7709 With prefix arg N, cut this many sequential subtrees.
7710 This is a short-hand for marking the subtree and then copying it.
7711 If CUT is non-nil, actually cut the subtree.
7712 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7713 of some markers in the region, even if CUT is non-nil. This is
7714 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7716 (let (beg end folded (beg0 (point)))
7717 (if (org-called-interactively-p 'any)
7718 (org-back-to-heading nil) ; take what looks like a subtree
7719 (org-back-to-heading t)) ; take what is really there
7720 (org-back-over-empty-lines)
7722 (skip-chars-forward " \t\r\n")
7724 (save-excursion (outline-end-of-heading)
7725 (setq folded (outline-invisible-p)))
7727 (org-forward-same-level (1- n) t)
7729 (org-end-of-subtree t t))
7730 (org-back-over-empty-lines)
7734 (setq org-subtree-clip-folded folded)
7735 (when (or cut force-store-markers)
7736 (org-save-markers-in-region beg end))
7737 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7738 (setq org-subtree-clip (current-kill 0))
7739 (message "%s: Subtree(s) with %d characters"
7740 (if cut "Cut" "Copied")
7741 (length org-subtree-clip)))))
7743 (defun org-paste-subtree (&optional level tree for-yank)
7744 "Paste the clipboard as a subtree, with modification of headline level.
7745 The entire subtree is promoted or demoted in order to match a new headline
7748 If the cursor is at the beginning of a headline, the same level as
7749 that headline is used to paste the tree
7751 If not, the new level is derived from the *visible* headings
7752 before and after the insertion point, and taken to be the inferior headline
7753 level of the two. So if the previous visible heading is level 3 and the
7754 next is level 4 (or vice versa), level 4 will be used for insertion.
7755 This makes sure that the subtree remains an independent subtree and does
7756 not swallow low level entries.
7758 You can also force a different level, either by using a numeric prefix
7759 argument, or by inserting the heading marker by hand. For example, if the
7760 cursor is after \"*****\", then the tree will be shifted to level 5.
7762 If optional TREE is given, use this text instead of the kill ring.
7764 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7765 move back over whitespace before inserting, and move point to the end of
7766 the inserted text when done."
7768 (setq tree (or tree (and kill-ring (current-kill 0))))
7769 (unless (org-kill-is-subtree-p tree)
7771 (substitute-command-keys
7772 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7773 (org-with-limited-levels
7774 (let* ((visp (not (outline-invisible-p)))
7776 (^re_ "\\(\\*+\\)[ \t]*")
7777 (old-level (if (string-match org-outline-regexp-bol txt)
7778 (- (match-end 0) (match-beginning 0) 1)
7780 (force-level (cond (level (prefix-numeric-value level))
7781 ((and (looking-at "[ \t]*$")
7783 "^\\*+$" (buffer-substring
7784 (point-at-bol) (point))))
7785 (- (match-end 1) (match-beginning 1)))
7787 (looking-at org-outline-regexp))
7788 (- (match-end 0) (point) 1))
7790 (previous-level (save-excursion
7793 (outline-previous-visible-heading 1)
7794 (if (looking-at ^re_)
7795 (- (match-end 0) (match-beginning 0) 1)
7798 (next-level (save-excursion
7801 (or (looking-at org-outline-regexp)
7802 (outline-next-visible-heading 1))
7803 (if (looking-at ^re_)
7804 (- (match-end 0) (match-beginning 0) 1)
7807 (new-level (or force-level (max previous-level next-level)))
7808 (shift (if (or (= old-level -1)
7810 (= old-level new-level))
7812 (- new-level old-level)))
7813 (delta (if (> shift 0) -1 1))
7814 (func (if (> shift 0) 'org-demote 'org-promote))
7815 (org-odd-levels-only nil)
7817 ;; Remove the forced level indicator
7819 (delete-region (point-at-bol) (point)))
7821 (beginning-of-line (if (bolp) 1 2))
7822 (unless for-yank (org-back-over-empty-lines))
7824 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7825 (insert-before-markers txt)
7826 (unless (string-match "\n\\'" txt) (insert "\n"))
7827 (setq newend (point))
7828 (org-reinstall-markers-in-region beg)
7831 (skip-chars-forward " \t\n\r")
7833 (if (and (outline-invisible-p) visp)
7834 (save-excursion (outline-show-heading)))
7835 ;; Shift if necessary
7838 (narrow-to-region beg end)
7839 (while (not (= shift 0))
7840 (org-map-region func (point-min) (point-max))
7841 (setq shift (+ delta shift)))
7842 (goto-char (point-min))
7843 (setq newend (point-max))))
7844 (when (or (org-called-interactively-p 'interactive) for-yank)
7845 (message "Clipboard pasted as level %d subtree" new-level))
7846 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7848 (eq org-subtree-clip (current-kill 0))
7849 org-subtree-clip-folded)
7850 ;; The tree was folded before it was killed/copied
7852 (and for-yank (goto-char newend)))))
7854 (defun org-kill-is-subtree-p (&optional txt)
7855 "Check if the current kill is an outline subtree, or a set of trees.
7856 Returns nil if kill does not start with a headline, or if the first
7857 headline level is not the largest headline level in the tree.
7858 So this will actually accept several entries of equal levels as well,
7859 which is OK for `org-paste-subtree'.
7860 If optional TXT is given, check this string instead of the current kill."
7861 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7862 (re (org-get-limited-outline-regexp))
7863 (^re (concat "^" re))
7864 (start-level (and kill
7866 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7868 (- (match-end 2) (match-beginning 2) 1)))
7869 (start (1+ (or (match-beginning 2) -1))))
7870 (if (not start-level)
7872 nil) ;; does not even start with a heading
7874 (while (setq start (string-match ^re kill (1+ start)))
7875 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7879 (defvar org-markers-to-move nil
7880 "Markers that should be moved with a cut-and-paste operation.
7881 Those markers are stored together with their positions relative to
7882 the start of the region.")
7884 (defun org-save-markers-in-region (beg end)
7885 "Check markers in region.
7886 If these markers are between BEG and END, record their position relative
7887 to BEG, so that after moving the block of text, we can put the markers back
7889 This function gets called just before an entry or tree gets cut from the
7890 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7891 called immediately, to move the markers with the entries."
7892 (setq org-markers-to-move nil)
7893 (when (featurep 'org-clock)
7894 (org-clock-save-markers-for-cut-and-paste beg end))
7895 (when (featurep 'org-agenda)
7896 (org-agenda-save-markers-for-cut-and-paste beg end)))
7898 (defun org-check-and-save-marker (marker beg end)
7899 "Check if MARKER is between BEG and END.
7900 If yes, remember the marker and the distance to BEG."
7901 (when (and (marker-buffer marker)
7902 (equal (marker-buffer marker) (current-buffer)))
7903 (if (and (>= marker beg) (< marker end))
7904 (push (cons marker (- marker beg)) org-markers-to-move))))
7906 (defun org-reinstall-markers-in-region (beg)
7907 "Move all remembered markers to their position relative to BEG."
7909 (move-marker (car x) (+ beg (cdr x))))
7910 org-markers-to-move)
7911 (setq org-markers-to-move nil))
7913 (defun org-narrow-to-subtree ()
7914 "Narrow buffer to the current subtree."
7918 (org-with-limited-levels
7920 (progn (org-back-to-heading t) (point))
7921 (progn (org-end-of-subtree t t)
7922 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
7925 (defun org-narrow-to-block ()
7926 "Narrow buffer to the current block."
7928 (let* ((case-fold-search t)
7929 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
7930 "^[ \t]*#\\+end_.*")))
7932 (narrow-to-region (car blockp) (cdr blockp))
7933 (error "Not in a block"))))
7936 (defvar org-property-drawer-re))
7938 (defvar org-property-start-re) ;; defined below
7939 (defun org-clone-subtree-with-time-shift (n &optional shift)
7940 "Clone the task (subtree) at point N times.
7941 The clones will be inserted as siblings.
7943 In interactive use, the user will be prompted for the number of
7944 clones to be produced, and for a time SHIFT, which may be a
7945 repeater as used in time stamps, for example `+3d'.
7947 When a valid repeater is given and the entry contains any time
7948 stamps, the clones will become a sequence in time, with time
7949 stamps in the subtree shifted for each clone produced. If SHIFT
7950 is nil or the empty string, time stamps will be left alone. The
7951 ID property of the original subtree is removed.
7953 If the original subtree did contain time stamps with a repeater,
7954 the following will happen:
7955 - the repeater will be removed in each clone
7956 - an additional clone will be produced, with the current, unshifted
7957 date(s) in the entry.
7958 - the original entry will be placed *after* all the clones, with
7960 - the start days in the repeater in the original entry will be shifted
7961 to past the last clone.
7962 In this way you can spell out a number of instances of a repeating task,
7963 and still retain the repeater to cover future instances of the task."
7964 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7965 (let (beg end template task idprop
7966 shift-n shift-what doshift nmin nmax (n-no-remove -1)
7967 (drawer-re org-drawer-regexp))
7968 (if (not (and (integerp n) (> n 0)))
7969 (error "Invalid number of replications %s" n))
7970 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7971 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
7973 (error "Invalid shift specification %s" shift))
7975 (setq shift-n (string-to-number (match-string 1 shift))
7976 shift-what (cdr (assoc (match-string 2 shift)
7977 '(("d" . day) ("w" . week)
7978 ("m" . month) ("y" . year))))))
7979 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7980 (setq nmin 1 nmax n)
7981 (org-back-to-heading t)
7983 (setq idprop (org-entry-get nil "ID"))
7984 (org-end-of-subtree t t)
7985 (or (bolp) (insert "\n"))
7987 (setq template (buffer-substring beg end))
7989 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[dwmy][^<>\n]*>" template))
7990 (delete-region beg end)
7992 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7994 (loop for n from nmin to nmax do
7999 (goto-char (point-min))
8001 (and idprop (if org-clone-delete-id
8002 (org-entry-delete nil "ID")
8003 (org-id-get-create t)))
8005 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8007 (goto-char (point-min))
8008 (while (re-search-forward drawer-re nil t)
8010 (org-remove-empty-drawer-at d (point))) org-drawers)))
8011 (goto-char (point-min))
8013 (while (re-search-forward org-ts-regexp-both nil t)
8014 (org-timestamp-change (* n shift-n) shift-what))
8015 (unless (= n n-no-remove)
8016 (goto-char (point-min))
8017 (while (re-search-forward org-ts-regexp nil t)
8019 (goto-char (match-beginning 0))
8020 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[dwmy]\\)")
8021 (delete-region (match-beginning 1) (match-end 1)))))))
8022 (setq task (buffer-string)))
8028 (defun org-sort (with-case)
8029 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8030 Optional argument WITH-CASE means sort case-sensitively."
8033 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8034 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8036 (org-call-with-arg 'org-sort-entries with-case))))
8038 (defun org-sort-remove-invisible (s)
8039 (remove-text-properties 0 (length s) org-rm-props s)
8040 (while (string-match org-bracket-link-regexp s)
8041 (setq s (replace-match (if (match-end 2)
8043 (match-string 1 s)) t t s)))
8046 (defvar org-priority-regexp) ; defined later in the file
8048 (defvar org-after-sorting-entries-or-items-hook nil
8049 "Hook that is run after a bunch of entries or items have been sorted.
8050 When children are sorted, the cursor is in the parent line when this
8051 hook gets called. When a region or a plain list is sorted, the cursor
8052 will be in the first entry of the sorted region/list.")
8054 (defun org-sort-entries
8055 (&optional with-case sorting-type getkey-func compare-func property)
8056 "Sort entries on a certain level of an outline tree.
8057 If there is an active region, the entries in the region are sorted.
8058 Else, if the cursor is before the first entry, sort the top-level items.
8059 Else, the children of the entry at point are sorted.
8061 Sorting can be alphabetically, numerically, by date/time as given by
8062 a time stamp, by a property or by priority.
8064 The command prompts for the sorting type unless it has been given to the
8065 function through the SORTING-TYPE argument, which needs to be a character,
8066 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
8067 precise meaning of each character:
8069 n Numerically, by converting the beginning of the entry/item to a number.
8070 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8071 t By date/time, either the first active time stamp in the entry, or, if
8072 none exist, by the first inactive one.
8073 s By the scheduled date/time.
8074 d By deadline date/time.
8075 c By creation time, which is assumed to be the first inactive time stamp
8076 at the beginning of a line.
8077 p By priority according to the cookie.
8078 r By the value of a property.
8080 Capital letters will reverse the sort order.
8082 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8083 called with point at the beginning of the record. It must return either
8084 a string or a number that should serve as the sorting key for that record.
8086 Comparing entries ignores case by default. However, with an optional argument
8087 WITH-CASE, the sorting considers case as well."
8089 (let ((case-func (if with-case 'identity 'downcase))
8090 start beg end stars re re2
8092 ;; Find beginning and end of region to sort
8094 ((org-region-active-p)
8095 ;; we will sort the region
8096 (setq end (region-end)
8098 (goto-char (region-beginning))
8099 (if (not (org-at-heading-p)) (outline-next-heading))
8100 (setq start (point)))
8101 ((or (org-at-heading-p)
8102 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8103 ;; we will sort the children of the current headline
8104 (org-back-to-heading)
8106 end (progn (org-end-of-subtree t t)
8107 (or (bolp) (insert "\n"))
8108 (org-back-over-empty-lines)
8113 (outline-next-heading))
8115 ;; we will sort the top-level entries in this file
8116 (goto-char (point-min))
8117 (or (org-at-heading-p) (outline-next-heading))
8118 (setq start (point))
8119 (goto-char (point-max))
8120 (beginning-of-line 1)
8121 (when (looking-at ".*?\\S-")
8122 ;; File ends in a non-white line
8125 (setq end (point-max))
8126 (setq what "top-level")
8131 (if (>= beg end) (error "Nothing to sort"))
8133 (looking-at "\\(\\*+\\)")
8134 (setq stars (match-string 1)
8135 re (concat "^" (regexp-quote stars) " +")
8136 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8137 txt (buffer-substring beg end))
8138 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8139 (if (and (not (equal stars "*")) (string-match re2 txt))
8140 (error "Region to sort contains a level above the first entry"))
8142 (unless sorting-type
8144 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8145 [t]ime
[s]cheduled [d]eadline [c]reated
8146 A/N/T/S/D/C/P/O/F means reversed:"
8148 (setq sorting-type (read-char-exclusive))
8150 (and (= (downcase sorting-type) ?f)
8152 (org-icompleting-read "Sort using function: "
8153 obarray 'fboundp t nil nil))
8154 (setq getkey-func (intern getkey-func)))
8156 (and (= (downcase sorting-type) ?r)
8158 (org-icompleting-read "Property: "
8159 (mapcar 'list (org-buffer-property-keys t))
8162 (message "Sorting entries...")
8165 (narrow-to-region start end)
8166 (let ((dcst (downcase sorting-type))
8167 (case-fold-search nil)
8168 (now (current-time)))
8170 (/= dcst sorting-type)
8171 ;; This function moves to the beginning character of the "record" to
8174 (if (re-search-forward re nil t)
8175 (goto-char (match-beginning 0))
8176 (goto-char (point-max))))
8177 ;; This function moves to the last character of the "record" being
8182 (outline-forward-same-level 1)
8184 (goto-char (point-max))))))
8185 ;; This function returns the value that gets sorted against.
8189 (if (looking-at org-complex-heading-regexp)
8190 (string-to-number (match-string 4))
8193 (if (looking-at org-complex-heading-regexp)
8194 (funcall case-func (match-string 4))
8197 (let ((end (save-excursion (outline-next-heading) (point))))
8198 (if (or (re-search-forward org-ts-regexp end t)
8199 (re-search-forward org-ts-regexp-both end t))
8200 (org-time-string-to-seconds (match-string 0))
8201 (org-float-time now))))
8203 (let ((end (save-excursion (outline-next-heading) (point))))
8204 (if (re-search-forward
8205 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8207 (org-time-string-to-seconds (match-string 0))
8208 (org-float-time now))))
8210 (let ((end (save-excursion (outline-next-heading) (point))))
8211 (if (re-search-forward org-scheduled-time-regexp end t)
8212 (org-time-string-to-seconds (match-string 1))
8213 (org-float-time now))))
8215 (let ((end (save-excursion (outline-next-heading) (point))))
8216 (if (re-search-forward org-deadline-time-regexp end t)
8217 (org-time-string-to-seconds (match-string 1))
8218 (org-float-time now))))
8220 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8221 (string-to-char (match-string 2))
8222 org-default-priority))
8224 (or (org-entry-get nil property) ""))
8226 (if (looking-at org-complex-heading-regexp)
8227 (- 9999 (length (member (match-string 2)
8228 org-todo-keywords-1)))))
8232 (setq tmp (funcall getkey-func))
8233 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8235 (error "Invalid key function `%s'" getkey-func)))
8236 (t (error "Invalid sorting type `%c'" sorting-type))))
8239 ((= dcst ?a) 'string<)
8240 ((= dcst ?f) compare-func)
8241 ((member dcst '(?p ?t ?s ?d ?c)) '<)
8243 (run-hooks 'org-after-sorting-entries-or-items-hook)
8244 (message "Sorting entries...done")))
8246 (defun org-do-sort (table what &optional with-case sorting-type)
8247 "Sort TABLE of WHAT according to SORTING-TYPE.
8248 The user will be prompted for the SORTING-TYPE if the call to this
8249 function does not specify it. WHAT is only for the prompt, to indicate
8250 what is being sorted. The sorting key will be extracted from
8251 the car of the elements of the table.
8252 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8253 (unless sorting-type
8255 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
8257 (setq sorting-type (read-char-exclusive)))
8258 (let ((dcst (downcase sorting-type))
8259 extractfun comparefun)
8260 ;; Define the appropriate functions
8263 (setq extractfun 'string-to-number
8264 comparefun (if (= dcst sorting-type) '< '>)))
8266 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8267 (lambda(x) (downcase (org-sort-remove-invisible x))))
8268 comparefun (if (= dcst sorting-type)
8270 (lambda (a b) (and (not (string< a b))
8271 (not (string= a b)))))))
8275 (if (or (string-match org-ts-regexp x)
8276 (string-match org-ts-regexp-both x))
8278 (org-time-string-to-time (match-string 0 x)))
8280 comparefun (if (= dcst sorting-type) '< '>)))
8281 (t (error "Invalid sorting type `%c'" sorting-type)))
8283 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8285 (lambda (a b) (funcall comparefun (car a) (car b))))))
8288 ;;; The orgstruct minor mode
8290 ;; Define a minor mode which can be used in other modes in order to
8291 ;; integrate the org-mode structure editing commands.
8293 ;; This is really a hack, because the org-mode structure commands use
8294 ;; keys which normally belong to the major mode. Here is how it
8295 ;; works: The minor mode defines all the keys necessary to operate the
8296 ;; structure commands, but wraps the commands into a function which
8297 ;; tests if the cursor is currently at a headline or a plain list
8298 ;; item. If that is the case, the structure command is used,
8299 ;; temporarily setting many Org-mode variables like regular
8300 ;; expressions for filling etc. However, when any of those keys is
8301 ;; used at a different location, function uses `key-binding' to look
8302 ;; up if the key has an associated command in another currently active
8303 ;; keymap (minor modes, major mode, global), and executes that
8304 ;; command. There might be problems if any of the keys is otherwise
8305 ;; used as a prefix key.
8307 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8308 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8309 ;; addresses this by checking explicitly for both bindings.
8311 (defvar orgstruct-mode-map (make-sparse-keymap)
8312 "Keymap for the minor `orgstruct-mode'.")
8314 (defvar org-local-vars nil
8315 "List of local variables, for use by `orgstruct-mode'.")
8318 (define-minor-mode orgstruct-mode
8319 "Toggle the minor mode `orgstruct-mode'.
8320 This mode is for using Org-mode structure commands in other
8321 modes. The following keys behave as if Org-mode were active, if
8322 the cursor is on a headline, or on a plain list item (both as
8323 defined by Org-mode).
8325 M-up Move entry/item up
8326 M-down Move entry/item down
8329 M-S-up Move entry/item up
8330 M-S-down Move entry/item down
8331 M-S-left Promote subtree
8332 M-S-right Demote subtree
8333 M-q Fill paragraph and items like in Org-mode
8335 C-c - Cycle list bullet
8336 TAB Cycle item visibility
8337 M-RET Insert new heading/item
8338 S-M-RET Insert new TODO heading / Checkbox item
8339 C-c C-c Set tags / toggle checkbox"
8340 nil " OrgStruct" nil
8341 (org-load-modules-maybe)
8342 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8345 (defun turn-on-orgstruct ()
8346 "Unconditionally turn on `orgstruct-mode'."
8349 (defun orgstruct++-mode (&optional arg)
8350 "Toggle `orgstruct-mode', the enhanced version of it.
8351 In addition to setting orgstruct-mode, this also exports all indentation
8352 and autofilling variables from org-mode into the buffer. It will also
8353 recognize item context in multiline items.
8354 Note that turning off orgstruct-mode will *not* remove the
8355 indentation/paragraph settings. This can only be done by refreshing the
8356 major mode, for example with \\[normal-mode]."
8358 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8366 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8367 (symbol-name (car x)))
8368 (setq var (car x) val (nth 1 x))
8369 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8371 (org-set-local 'orgstruct-is-++ t))))
8373 (defvar orgstruct-is-++ nil
8374 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8375 (make-variable-buffer-local 'orgstruct-is-++)
8378 (defun turn-on-orgstruct++ ()
8379 "Unconditionally turn on `orgstruct++-mode'."
8380 (orgstruct++-mode 1))
8382 (defun orgstruct-error ()
8383 "Error when there is no default binding for a structure key."
8385 (error "This key has no function outside structure elements"))
8387 (defun orgstruct-setup ()
8388 "Setup orgstruct keymaps."
8392 '([(meta up)] org-metaup)
8393 '([(meta down)] org-metadown)
8394 '([(meta left)] org-metaleft)
8395 '([(meta right)] org-metaright)
8396 '([(meta shift up)] org-shiftmetaup)
8397 '([(meta shift down)] org-shiftmetadown)
8398 '([(meta shift left)] org-shiftmetaleft)
8399 '([(meta shift right)] org-shiftmetaright)
8400 '([?\e (up)] org-metaup)
8401 '([?\e (down)] org-metadown)
8402 '([?\e (left)] org-metaleft)
8403 '([?\e (right)] org-metaright)
8404 '([?\e (shift up)] org-shiftmetaup)
8405 '([?\e (shift down)] org-shiftmetadown)
8406 '([?\e (shift left)] org-shiftmetaleft)
8407 '([?\e (shift right)] org-shiftmetaright)
8408 '([(shift up)] org-shiftup)
8409 '([(shift down)] org-shiftdown)
8410 '([(shift left)] org-shiftleft)
8411 '([(shift right)] org-shiftright)
8412 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8413 '("\M-q" fill-paragraph)
8415 '("\C-c-" org-cycle-list-bullet)))
8417 (while (setq elt (pop bindings))
8418 (setq nfunc (1+ nfunc))
8419 (setq key (org-key (car elt))
8421 cmd (orgstruct-make-binding fun nfunc key))
8422 (org-defkey orgstruct-mode-map key cmd))
8424 ;; Special treatment needed for TAB and RET
8425 (org-defkey orgstruct-mode-map [(tab)]
8426 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8427 (org-defkey orgstruct-mode-map "\C-i"
8428 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8430 (org-defkey orgstruct-mode-map "\M-\C-m"
8431 (orgstruct-make-binding 'org-insert-heading 105
8432 "\M-\C-m" [(meta return)]))
8433 (org-defkey orgstruct-mode-map [(meta return)]
8434 (orgstruct-make-binding 'org-insert-heading 106
8435 [(meta return)] "\M-\C-m"))
8437 (org-defkey orgstruct-mode-map [(shift meta return)]
8438 (orgstruct-make-binding 'org-insert-todo-heading 107
8439 [(meta return)] "\M-\C-m"))
8441 (org-defkey orgstruct-mode-map "\e\C-m"
8442 (orgstruct-make-binding 'org-insert-heading 108
8443 "\e\C-m" [?\e (return)]))
8444 (org-defkey orgstruct-mode-map [?\e (return)]
8445 (orgstruct-make-binding 'org-insert-heading 109
8446 [?\e (return)] "\e\C-m"))
8447 (org-defkey orgstruct-mode-map [?\e (shift return)]
8448 (orgstruct-make-binding 'org-insert-todo-heading 110
8449 [?\e (return)] "\e\C-m"))
8451 (unless org-local-vars
8452 (setq org-local-vars (org-get-local-variables)))
8456 (defun orgstruct-make-binding (fun n &rest keys)
8457 "Create a function for binding in the structure minor mode.
8458 FUN is the command to call inside a table. N is used to create a unique
8459 command name. KEYS are keys that should be checked in for a command
8460 to execute outside of tables."
8463 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8465 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8466 "Outside of structure, run the binding of `"
8467 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8471 `(org-context-p 'headline 'item
8472 (and orgstruct-is-++
8473 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8475 (list 'org-run-like-in-org-mode (list 'quote fun))
8476 (list 'let '(orgstruct-mode)
8477 (list 'call-interactively
8480 (list 'key-binding k))
8482 '('orgstruct-error))))))))
8484 (defun org-context-p (&rest contexts)
8485 "Check if local context is any of CONTEXTS.
8486 Possible values in the list of contexts are `table', `headline', and `item'."
8487 (let ((pos (point)))
8488 (goto-char (point-at-bol))
8489 (prog1 (or (and (memq 'table contexts)
8490 (looking-at "[ \t]*|"))
8491 (and (memq 'headline contexts)
8492 (looking-at org-outline-regexp))
8493 (and (memq 'item contexts)
8494 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8495 (and (memq 'item-body contexts)
8499 (defun org-get-local-variables ()
8500 "Return a list of all local variables in an org-mode buffer."
8502 (with-current-buffer (get-buffer-create "*Org tmp*")
8505 (setq varlist (buffer-local-variables)))
8506 (kill-buffer "*Org tmp*")
8513 (list (car x) (list 'quote (cdr x)))))
8515 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8516 (symbol-name (car x)))
8520 (defun org-clone-local-variables (from-buffer &optional regexp)
8521 "Clone local variables from FROM-BUFFER.
8522 Optional argument REGEXP selects variables to clone."
8525 (and (symbolp (car pair))
8527 (string-match regexp (symbol-name (car pair))))
8528 (set (make-local-variable (car pair))
8530 (buffer-local-variables from-buffer)))
8533 (defun org-run-like-in-org-mode (cmd)
8534 "Run a command, pretending that the current buffer is in Org-mode.
8535 This will temporarily bind local variables that are typically bound in
8536 Org-mode to the values they have in Org-mode, and then interactively
8538 (org-load-modules-maybe)
8539 (unless org-local-vars
8540 (setq org-local-vars (org-get-local-variables)))
8541 (eval (list 'let org-local-vars
8542 (list 'call-interactively (list 'quote cmd)))))
8546 (defun org-get-category (&optional pos force-refresh)
8547 "Get the category applying to position POS."
8549 (if force-refresh (org-refresh-category-properties))
8550 (let ((pos (or pos (point))))
8551 (or (get-text-property pos 'org-category)
8552 (progn (org-refresh-category-properties)
8553 (get-text-property pos 'org-category))))))
8555 (defun org-refresh-category-properties ()
8556 "Refresh category text properties in the buffer."
8557 (let ((def-cat (cond
8558 ((null org-category)
8559 (if buffer-file-name
8560 (file-name-sans-extension
8561 (file-name-nondirectory buffer-file-name))
8563 ((symbolp org-category) (symbol-name org-category))
8565 beg end cat pos optionp)
8570 (goto-char (point-min))
8571 (put-text-property (point) (point-max) 'org-category def-cat)
8572 (while (re-search-forward
8573 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8574 (setq pos (match-end 0)
8575 optionp (equal (char-after (match-beginning 0)) ?#)
8576 cat (org-trim (match-string 2)))
8578 (setq beg (point-at-bol) end (point-max))
8579 (org-back-to-heading t)
8580 (setq beg (point) end (org-end-of-subtree t t)))
8581 (put-text-property beg end 'org-category cat)
8582 (put-text-property beg end 'org-category-position beg)
8583 (goto-char pos)))))))
8588 ;;; Link abbreviations
8590 (defun org-link-expand-abbrev (link)
8591 "Apply replacements as defined in `org-link-abbrev-alist."
8592 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8593 (let* ((key (match-string 1 link))
8594 (as (or (assoc key org-link-abbrev-alist-local)
8595 (assoc key org-link-abbrev-alist)))
8596 (tag (and (match-end 2) (match-string 3 link)))
8602 ((symbolp rpl) (funcall rpl tag))
8603 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8604 ((string-match "%h" rpl)
8605 (replace-match (url-hexify-string (or tag "")) t t rpl))
8606 (t (concat rpl tag)))))
8609 ;;; Storing and inserting links
8611 (defvar org-insert-link-history nil
8612 "Minibuffer history for links inserted with `org-insert-link'.")
8614 (defvar org-stored-links nil
8615 "Contains the links stored with `org-store-link'.")
8617 (defvar org-store-link-plist nil
8618 "Plist with info about the most recently link created with `org-store-link'.")
8620 (defvar org-link-protocols nil
8621 "Link protocols added to Org-mode using `org-add-link-type'.")
8623 (defvar org-store-link-functions nil
8624 "List of functions that are called to create and store a link.
8625 Each function will be called in turn until one returns a non-nil
8626 value. Each function should check if it is responsible for creating
8627 this link (for example by looking at the major mode).
8628 If not, it must exit and return nil.
8629 If yes, it should return a non-nil value after a calling
8630 `org-store-link-props' with a list of properties and values.
8631 Special properties are:
8633 :type The link prefix, like \"http\". This must be given.
8634 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8635 This is obligatory as well.
8636 :description Optional default description for the second pair
8637 of brackets in an Org-mode link. The user can still change
8638 this when inserting this link into an Org-mode buffer.
8640 In addition to these, any additional properties can be specified
8641 and then used in remember templates.")
8643 (defun org-add-link-type (type &optional follow export)
8644 "Add TYPE to the list of `org-link-types'.
8645 Re-compute all regular expressions depending on `org-link-types'
8647 FOLLOW and EXPORT are two functions.
8649 FOLLOW should take the link path as the single argument and do whatever
8650 is necessary to follow the link, for example find a file or display
8653 EXPORT should format the link path for export to one of the export formats.
8654 It should be a function accepting three arguments:
8656 path the path of the link, the text after the prefix (like \"http:\")
8657 desc the description of the link, if any, or a description added by
8658 org-export-normalize-links if there is none
8659 format the export format, a symbol like `html' or `latex' or `ascii'..
8661 The function may use the FORMAT information to return different values
8662 depending on the format. The return value will be put literally into
8663 the exported file. If the return value is nil, this means Org should
8664 do what it normally does with links which do not have EXPORT defined.
8666 Org-mode has a built-in default for exporting links. If you are happy with
8667 this default, there is no need to define an export function for the link
8668 type. For a simple example of an export function, see `org-bbdb.el'."
8669 (add-to-list 'org-link-types type t)
8670 (org-make-link-regexps)
8671 (if (assoc type org-link-protocols)
8672 (setcdr (assoc type org-link-protocols) (list follow export))
8673 (push (list type follow export) org-link-protocols)))
8675 (defvar org-agenda-buffer-name)
8678 (defun org-store-link (arg)
8679 "\\<org-mode-map>Store an org-link to the current location.
8680 This link is added to `org-stored-links' and can later be inserted
8681 into an org-buffer with \\[org-insert-link].
8683 For some link types, a prefix arg is interpreted:
8684 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8685 For file links, arg negates `org-context-in-file-links'."
8687 (org-load-modules-maybe)
8688 (setq org-store-link-plist nil) ; reset
8689 (org-with-limited-levels
8690 (let (link cpltxt desc description search txt custom-id agenda-link)
8693 ((run-hook-with-args-until-success 'org-store-link-functions)
8694 (setq link (plist-get org-store-link-plist :link)
8695 desc (or (plist-get org-store-link-plist :description) link)))
8697 ((org-src-edit-buffer-p)
8699 (while (or (not label)
8703 (goto-char (point-min))
8705 (regexp-quote (format org-coderef-label-format label))
8707 (when label (message "Label exists already") (sit-for 2))
8708 (setq label (read-string "Code line label: " label)))
8710 (setq link (format org-coderef-label-format label))
8711 (setq gc (- 79 (length link)))
8712 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8714 (setq link (concat "(" label ")") desc nil)))
8716 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8717 ;; We are in the agenda, link to referenced location
8718 (let ((m (or (get-text-property (point) 'org-hd-marker)
8719 (get-text-property (point) 'org-marker))))
8721 (org-with-point-at m
8723 (if (org-called-interactively-p 'any)
8724 (call-interactively 'org-store-link)
8725 (org-store-link nil)))))))
8727 ((eq major-mode 'calendar-mode)
8728 (let ((cd (calendar-cursor-to-date)))
8731 (car org-time-stamp-formats)
8733 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8735 (org-store-link-props :type "calendar" :date cd)))
8737 ((eq major-mode 'w3-mode)
8738 (setq cpltxt (if (and (buffer-name)
8739 (not (string-match "Untitled" (buffer-name))))
8742 link (org-make-link (url-view-url t)))
8743 (org-store-link-props :type "w3" :url (url-view-url t)))
8745 ((eq major-mode 'w3m-mode)
8746 (setq cpltxt (or w3m-current-title w3m-current-url)
8747 link (org-make-link w3m-current-url))
8748 (org-store-link-props :type "w3m" :url (url-view-url t)))
8750 ((setq search (run-hook-with-args-until-success
8751 'org-create-file-search-functions))
8752 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8754 (setq cpltxt (or description link)))
8756 ((eq major-mode 'image-mode)
8757 (setq cpltxt (concat "file:"
8758 (abbreviate-file-name buffer-file-name))
8759 link (org-make-link cpltxt))
8760 (org-store-link-props :type "image" :file buffer-file-name))
8762 ((eq major-mode 'dired-mode)
8763 ;; link to the file in the current line
8764 (let ((file (dired-get-filename nil t)))
8766 (abbreviate-file-name
8767 (expand-file-name (dired-get-filename nil t)))
8768 ;; otherwise, no file so use current directory.
8770 (setq cpltxt (concat "file:" file)
8771 link (org-make-link cpltxt))))
8773 ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
8774 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8776 ((org-in-regexp "<<\\(.*?\\)>>")
8779 (abbreviate-file-name
8780 (buffer-file-name (buffer-base-buffer)))
8781 "::" (match-string 1))
8782 link (org-make-link cpltxt)))
8783 ((and (featurep 'org-id)
8784 (or (eq org-link-to-org-use-id t)
8785 (and (eq org-link-to-org-use-id 'create-if-interactive)
8786 (org-called-interactively-p 'any))
8787 (and (eq org-link-to-org-use-id
8788 'create-if-interactive-and-no-custom-id)
8789 (org-called-interactively-p 'any)
8791 (and org-link-to-org-use-id
8792 (org-entry-get nil "ID"))))
8793 ;; We can make a link using the ID.
8794 (setq link (condition-case nil
8795 (prog1 (org-id-store-link)
8796 (setq desc (plist-get org-store-link-plist
8799 ;; probably before first headline, link to file only
8801 (abbreviate-file-name
8802 (buffer-file-name (buffer-base-buffer))))))))
8804 ;; Just link to current headline
8805 (setq cpltxt (concat "file:"
8806 (abbreviate-file-name
8807 (buffer-file-name (buffer-base-buffer)))))
8808 ;; Add a context search string
8809 (when (org-xor org-context-in-file-links arg)
8811 ((org-at-heading-p) nil)
8812 ((org-region-active-p)
8813 (buffer-substring (region-beginning) (region-end)))
8815 (when (or (null txt) (string-match "\\S-" txt))
8819 (org-make-org-heading-search-string txt)
8821 desc (or (nth 4 (ignore-errors
8822 (org-heading-components))) "NONE"))))
8823 (if (string-match "::\\'" cpltxt)
8824 (setq cpltxt (substring cpltxt 0 -2)))
8825 (setq link (org-make-link cpltxt)))))
8827 ((buffer-file-name (buffer-base-buffer))
8828 ;; Just link to this file here.
8829 (setq cpltxt (concat "file:"
8830 (abbreviate-file-name
8831 (buffer-file-name (buffer-base-buffer)))))
8832 ;; Add a context string
8833 (when (org-xor org-context-in-file-links arg)
8834 (setq txt (if (org-region-active-p)
8835 (buffer-substring (region-beginning) (region-end))
8836 (buffer-substring (point-at-bol) (point-at-eol))))
8837 ;; Only use search option if there is some text.
8838 (when (string-match "\\S-" txt)
8840 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8842 (setq link (org-make-link cpltxt)))
8844 ((org-called-interactively-p 'interactive)
8845 (error "Cannot link to a buffer which is not visiting a file"))
8847 (t (setq link nil)))
8849 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8850 (setq link (or link cpltxt)
8851 desc (or desc cpltxt))
8852 (if (equal desc "NONE") (setq desc nil))
8854 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8856 (setq org-stored-links
8857 (cons (list link desc) org-stored-links))
8858 (message "Stored: %s" (or desc link))
8860 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8862 (setq org-stored-links
8863 (cons (list link desc) org-stored-links))))
8864 (or agenda-link (and link (org-make-link-string link desc)))))))
8866 (defun org-store-link-props (&rest plist)
8867 "Store link properties, extract names and addresses."
8869 (when (setq x (plist-get plist :from))
8870 (setq adr (mail-extract-address-components x))
8871 (setq plist (plist-put plist :fromname (car adr)))
8872 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8873 (when (setq x (plist-get plist :to))
8874 (setq adr (mail-extract-address-components x))
8875 (setq plist (plist-put plist :toname (car adr)))
8876 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8877 (let ((from (plist-get plist :from))
8878 (to (plist-get plist :to)))
8879 (when (and from to org-from-is-user-regexp)
8881 (plist-put plist :fromto
8882 (if (string-match org-from-is-user-regexp from)
8884 (concat "from %f"))))))
8885 (setq org-store-link-plist plist))
8887 (defun org-add-link-props (&rest plist)
8888 "Add these properties to the link property list."
8891 (setq key (pop plist) value (pop plist))
8892 (setq org-store-link-plist
8893 (plist-put org-store-link-plist key value)))))
8895 (defun org-email-link-description (&optional fmt)
8896 "Return the description part of an email link.
8897 This takes information from `org-store-link-plist' and formats it
8898 according to FMT (default from `org-email-link-description-format')."
8899 (setq fmt (or fmt org-email-link-description-format))
8900 (let* ((p org-store-link-plist)
8901 (to (plist-get p :toaddress))
8902 (from (plist-get p :fromaddress))
8905 (cons "%c" (plist-get p :fromto))
8906 (cons "%F" (plist-get p :from))
8907 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8908 (cons "%T" (plist-get p :to))
8909 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8910 (cons "%s" (plist-get p :subject))
8911 (cons "%d" (plist-get p :date))
8912 (cons "%m" (plist-get p :message-id)))))
8913 (when (string-match "%c" fmt)
8914 ;; Check if the user wrote this message
8915 (if (and org-from-is-user-regexp from to
8916 (save-match-data (string-match org-from-is-user-regexp from)))
8917 (setq fmt (replace-match "to %t" t t fmt))
8918 (setq fmt (replace-match "from %f" t t fmt))))
8919 (org-replace-escapes fmt table)))
8921 (defun org-make-org-heading-search-string (&optional string heading)
8922 "Make search string for STRING or current headline."
8924 (let ((s (or string (org-get-heading)))
8925 (lines org-context-in-file-links))
8926 (unless (and string (not heading))
8927 ;; We are using a headline, clean up garbage in there.
8928 (if (string-match org-todo-regexp s)
8929 (setq s (replace-match "" t t s)))
8930 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
8931 (setq s (replace-match "" t t s)))
8932 (setq s (org-trim s))
8933 (if (string-match (concat "^\\(" org-quote-string "\\|"
8934 org-comment-string "\\)") s)
8935 (setq s (replace-match "" t t s)))
8936 (while (string-match org-ts-regexp s)
8937 (setq s (replace-match "" t t s))))
8938 (or string (setq s (concat "*" s))) ; Add * for headlines
8939 (when (and string (integerp lines) (> lines 0))
8940 (let ((slines (org-split-string s "\n")))
8941 (when (< lines (length slines))
8944 (reverse (nthcdr (- (length slines) lines)
8945 (reverse slines))) "\n")))))
8946 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8948 (defun org-make-link (&rest strings)
8949 "Concatenate STRINGS."
8950 (apply 'concat strings))
8952 (defun org-make-link-string (link &optional description)
8953 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8954 (unless (string-match "\\S-" link)
8955 (error "Empty link"))
8956 (when (and description
8957 (stringp description)
8958 (not (string-match "\\S-" description)))
8959 (setq description nil))
8960 (when (stringp description)
8961 ;; Remove brackets from the description, they are fatal.
8962 (while (string-match "\\[" description)
8963 (setq description (replace-match "{" t t description)))
8964 (while (string-match "\\]" description)
8965 (setq description (replace-match "}" t t description))))
8966 (when (equal link description)
8967 ;; No description needed, it is identical
8968 (setq description nil))
8969 (when (and (not description)
8970 (not (string-match (org-image-file-name-regexp) link))
8971 (not (equal link (org-link-escape link))))
8972 (setq description (org-extract-attributes link)))
8974 (cond ((string-match (org-image-file-name-regexp) link) link)
8975 ((string-match org-link-types-re link)
8976 (concat (match-string 1 link)
8977 (org-link-escape (substring link (match-end 1)))))
8978 (t (org-link-escape link))))
8979 (concat "[[" link "]"
8980 (if description (concat "[" description "]") "")
8983 (defconst org-link-escape-chars
8984 '(?\ ?\[ ?\] ?\; ?\= ?\+)
8985 "List of characters that should be escaped in link.
8986 This is the list that is used for internal purposes.")
8988 (defvar org-url-encoding-use-url-hexify nil)
8990 (defconst org-link-escape-chars-browser
8992 "List of escapes for characters that are problematic in links.
8993 This is the list that is used before handing over to the browser.")
8995 (defun org-link-escape (text &optional table merge)
8996 "Return percent escaped representation of TEXT.
8997 TEXT is a string with the text to escape.
8998 Optional argument TABLE is a list with characters that should be
8999 escaped. When nil, `org-link-escape-chars' is used.
9000 If optional argument MERGE is set, merge TABLE into
9001 `org-link-escape-chars'."
9002 (if (and org-url-encoding-use-url-hexify (not table))
9003 (url-hexify-string text)
9006 (mapc (lambda (defchr)
9007 (unless (member defchr table)
9008 (setq table (cons defchr table)))) org-link-escape-chars))
9010 (setq table org-link-escape-chars)))
9013 (if (or (member char table)
9014 (< char 32) (= char 37) (> char 126))
9015 (mapconcat (lambda (sequence-element)
9016 (format "%%%.2X" sequence-element))
9017 (or (encode-coding-char char 'utf-8)
9018 (error "Unable to percent escape character: %s"
9019 (char-to-string char))) "")
9020 (char-to-string char))) text "")))
9022 (defun org-link-unescape (str)
9023 "Unhex hexified Unicode strings as returned from the JavaScript function
9024 encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
9025 (unless (and (null str) (string= "" str))
9026 (let ((pos 0) (case-fold-search t) unhexed)
9027 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9028 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9029 (setq str (replace-match unhexed t t str))
9030 (setq pos (+ pos (length unhexed))))))
9033 (defun org-link-unescape-compound (hex)
9034 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
9035 Note: this function also decodes single byte encodings like
9036 `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
9038 (let* ((bytes (cdr (split-string hex "%")))
9043 (let* ((val (string-to-number (pop bytes) 16))
9047 ((>= val 252) (cons 6 252))
9048 ((>= val 248) (cons 5 248))
9049 ((>= val 240) (cons 4 240))
9050 ((>= val 224) (cons 3 224))
9051 ((>= val 192) (cons 2 192))
9054 (if (>= val 192) (setq eat (car shift-xor)))
9055 (setq val (logxor val (cdr shift-xor)))
9056 (setq sum (+ (lsh sum (car shift-xor)) val))
9057 (if (> eat 0) (setq eat (- eat 1)))
9059 ((= 0 eat) ;multi byte
9060 (setq ret (concat ret (org-char-to-string sum)))
9062 ((not bytes) ; single byte(s)
9063 (setq ret (org-link-unescape-single-byte-sequence hex))))
9064 )) ;; end (while bytes
9067 (defun org-link-unescape-single-byte-sequence (hex)
9068 "Unhexify hex-encoded single byte character sequences."
9069 (mapconcat (lambda (byte)
9070 (char-to-string (string-to-number byte 16)))
9071 (cdr (split-string hex "%")) ""))
9073 (defun org-xor (a b)
9077 (defun org-fixup-message-id-for-http (s)
9078 "Replace special characters in a message id, so it can be used in an http query."
9079 (when (string-match "%" s)
9080 (setq s (mapconcat (lambda (c)
9083 (char-to-string c)))
9085 (while (string-match "<" s)
9086 (setq s (replace-match "%3C" t t s)))
9087 (while (string-match ">" s)
9088 (setq s (replace-match "%3E" t t s)))
9089 (while (string-match "@" s)
9090 (setq s (replace-match "%40" t t s)))
9094 (defun org-insert-link-global ()
9095 "Insert a link like Org-mode does.
9096 This command can be called in any mode to insert a link in Org-mode syntax."
9098 (org-load-modules-maybe)
9099 (org-run-like-in-org-mode 'org-insert-link))
9101 (defun org-insert-link (&optional complete-file link-location default-description)
9102 "Insert a link. At the prompt, enter the link.
9104 Completion can be used to insert any of the link protocol prefixes like
9107 The history can be used to select a link previously stored with
9108 `org-store-link'. When the empty string is entered (i.e. if you just
9109 press RET at the prompt), the link defaults to the most recently
9110 stored link. As SPC triggers completion in the minibuffer, you need to
9111 use M-SPC or C-q SPC to force the insertion of a space character.
9113 You will also be prompted for a description, and if one is given, it will
9114 be displayed in the buffer instead of the link.
9116 If there is already a link at point, this command will allow you to edit link
9117 and description parts.
9119 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9120 be selected using completion. The path to the file will be relative to the
9121 current directory if the file is in the current directory or a subdirectory.
9122 Otherwise, the link will be the absolute path as completed in the minibuffer
9123 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9124 option `org-link-file-path-type'.
9126 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9127 the current directory or below.
9129 With three \\[universal-argument] prefixes, negate the meaning of
9130 `org-keep-stored-link-after-insertion'.
9132 If `org-make-link-description-function' is non-nil, this function will be
9133 called with the link target, and the result will be the default
9136 If the LINK-LOCATION parameter is non-nil, this value will be
9137 used as the link location instead of reading one interactively.
9139 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9140 be used as the default description."
9142 (let* ((wcf (current-window-configuration))
9143 (region (if (org-region-active-p)
9144 (buffer-substring (region-beginning) (region-end))))
9145 (remove (and region (list (region-beginning) (region-end))))
9147 tmphist ; byte-compile incorrectly complains about this
9148 (link link-location)
9149 entry file all-prefixes)
9151 (link-location) ; specified by arg, just use it.
9152 ((org-in-regexp org-bracket-link-regexp 1)
9153 ;; We do have a link at point, and we are going to edit it.
9154 (setq remove (list (match-beginning 0) (match-end 0)))
9155 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9156 (setq link (read-string "Link: "
9158 (org-match-string-no-properties 1)))))
9159 ((or (org-in-regexp org-angle-link-re)
9160 (org-in-regexp org-plain-link-re))
9161 ;; Convert to bracket link
9162 (setq remove (list (match-beginning 0) (match-end 0))
9163 link (read-string "Link: "
9164 (org-remove-angle-brackets (match-string 0)))))
9165 ((member complete-file '((4) (16)))
9166 ;; Completing read for file names.
9167 (setq link (org-file-complete-link complete-file)))
9169 ;; Read link, with completion for stored links.
9170 (with-output-to-temp-buffer "*Org Links*"
9171 (princ "Insert a link.
9172 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9173 (when org-stored-links
9174 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9177 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
9178 (reverse org-stored-links) "\n"))))
9179 (let ((cw (selected-window)))
9180 (select-window (get-buffer-window "*Org Links*" 'visible))
9181 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9182 (unless (pos-visible-in-window-p (point-max))
9183 (org-fit-window-to-buffer))
9184 (and (window-live-p cw) (select-window cw)))
9185 ;; Fake a link history, containing the stored links.
9186 (setq tmphist (append (mapcar 'car org-stored-links)
9187 org-insert-link-history))
9188 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
9189 (mapcar 'car org-link-abbrev-alist)
9194 (let ((org-completion-use-ido nil)
9195 (org-completion-use-iswitchb nil))
9196 (org-completing-read
9199 (mapcar (lambda (x) (list (concat x ":")))
9201 (mapcar 'car org-stored-links))
9204 (car (car org-stored-links)))))
9205 (if (not (string-match "\\S-" link))
9206 (error "No link selected"))
9207 (if (or (member link all-prefixes)
9208 (and (equal ":" (substring link -1))
9209 (member (substring link 0 -1) all-prefixes)
9210 (setq link (substring link 0 -1))))
9211 (setq link (org-link-try-special-completion link))))
9212 (set-window-configuration wcf)
9213 (kill-buffer "*Org Links*"))
9214 (setq entry (assoc link org-stored-links))
9215 (or entry (push link org-insert-link-history))
9216 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9217 (not org-keep-stored-link-after-insertion))
9218 (setq org-stored-links (delq (assoc link org-stored-links)
9220 (setq desc (or desc (nth 1 entry)))))
9222 (if (string-match org-plain-link-re link)
9223 ;; URL-like link, normalize the use of angular brackets.
9224 (setq link (org-make-link (org-remove-angle-brackets link))))
9226 ;; Check if we are linking to the current file with a search option
9227 ;; If yes, simplify the link by using only the search option.
9228 (when (and buffer-file-name
9229 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
9230 (let* ((path (match-string 1 link))
9231 (case-fold-search nil)
9232 (search (match-string 2 link)))
9234 (if (equal (file-truename buffer-file-name) (file-truename path))
9235 ;; We are linking to this same file, with a search option
9236 (setq link search)))))
9238 ;; Check if we can/should use a relative path. If yes, simplify the link
9239 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9240 (let* ((type (match-string 1 link))
9241 (path (match-string 2 link))
9243 (case-fold-search nil))
9245 ((or (eq org-link-file-path-type 'absolute)
9246 (equal complete-file '(16)))
9247 (setq path (abbreviate-file-name (expand-file-name path))))
9248 ((eq org-link-file-path-type 'noabbrev)
9249 (setq path (expand-file-name path)))
9250 ((eq org-link-file-path-type 'relative)
9251 (setq path (file-relative-name path)))
9254 (if (string-match (concat "^" (regexp-quote
9256 (file-name-as-directory
9257 default-directory))))
9258 (expand-file-name path))
9259 ;; We are linking a file with relative path name.
9260 (setq path (substring (expand-file-name path)
9262 (setq path (abbreviate-file-name (expand-file-name path)))))))
9263 (setq link (concat type path))
9264 (if (equal desc origpath)
9267 (if org-make-link-description-function
9268 (setq desc (funcall org-make-link-description-function link desc))
9269 (if default-description (setq desc default-description)))
9271 (setq desc (read-string "Description: " desc))
9272 (unless (string-match "\\S-" desc) (setq desc nil))
9273 (if remove (apply 'delete-region remove))
9274 (insert (org-make-link-string link desc))))
9276 (defun org-link-try-special-completion (type)
9277 "If there is completion support for link type TYPE, offer it."
9278 (let ((fun (intern (concat "org-" type "-complete-link"))))
9281 (read-string "Link (no completion support): " (concat type ":")))))
9283 (defun org-file-complete-link (&optional arg)
9284 "Create a file link using completion."
9286 (setq file (read-file-name "File: "))
9287 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9288 (pwd1 (file-name-as-directory (abbreviate-file-name
9289 (expand-file-name ".")))))
9292 (setq link (org-make-link
9294 (abbreviate-file-name (expand-file-name file)))))
9295 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9296 (setq link (org-make-link "file:" (match-string 1 file))))
9297 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9298 (expand-file-name file))
9299 (setq link (org-make-link
9300 "file:" (match-string 1 (expand-file-name file)))))
9301 (t (setq link (org-make-link "file:" file)))))
9304 (defun org-completing-read (&rest args)
9305 "Completing-read with SPACE being a normal character."
9306 (let ((enable-recursive-minibuffers t)
9307 (minibuffer-local-completion-map
9308 (copy-keymap minibuffer-local-completion-map)))
9309 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9310 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9311 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9312 (apply 'org-icompleting-read args)))
9314 (defun org-completing-read-no-i (&rest args)
9315 (let (org-completion-use-ido org-completion-use-iswitchb)
9316 (apply 'org-completing-read args)))
9318 (defun org-iswitchb-completing-read (prompt choices &rest args)
9319 "Use iswitch as a completing-read replacement to choose from choices.
9320 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9322 (let* ((iswitchb-use-virtual-buffers nil)
9323 (iswitchb-make-buflist-hook
9325 (setq iswitchb-temp-buflist choices))))
9326 (iswitchb-read-buffer prompt)))
9328 (defun org-icompleting-read (&rest args)
9329 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9330 (org-without-partial-completion
9331 (if (and org-completion-use-ido
9332 (fboundp 'ido-completing-read)
9333 (boundp 'ido-mode) ido-mode
9334 (listp (second args)))
9335 (let ((ido-enter-matching-directory nil))
9336 (apply 'ido-completing-read (concat (car args))
9337 (if (consp (car (nth 1 args)))
9338 (mapcar 'car (nth 1 args))
9341 (if (and org-completion-use-iswitchb
9342 (boundp 'iswitchb-mode) iswitchb-mode
9343 (listp (second args)))
9344 (apply 'org-iswitchb-completing-read (concat (car args))
9345 (if (consp (car (nth 1 args)))
9346 (mapcar 'car (nth 1 args))
9349 (apply 'completing-read args)))))
9351 (defun org-extract-attributes (s)
9352 "Extract the attributes cookie from a string and set as text property."
9353 (let (a attr (start 0) key value)
9355 (when (string-match "{{\\([^}]+\\)}}$" s)
9356 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9357 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9358 (setq key (match-string 1 a) value (match-string 2 a)
9360 attr (plist-put attr (intern key) value))))
9361 (org-add-props s nil 'org-attr attr))
9364 (defun org-extract-attributes-from-string (tag)
9365 (let (key value attr)
9366 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9367 (setq key (match-string 1 tag) value (match-string 2 tag)
9368 tag (replace-match "" t t tag)
9369 attr (plist-put attr (intern key) value)))
9372 (defun org-attributes-to-string (plist)
9373 "Format a property list into an HTML attribute list."
9374 (let ((s "") key value)
9376 (setq key (pop plist) value (pop plist))
9378 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9381 ;;; Opening/following a link
9383 (defvar org-link-search-failed nil)
9385 (defvar org-open-link-functions nil
9386 "Hook for functions finding a plain text link.
9387 These functions must take a single argument, the link content.
9388 They will be called for links that look like [[link text][description]]
9389 when LINK TEXT does not have a protocol like \"http:\" and does not look
9390 like a filename (e.g. \"./blue.png\").
9392 These functions will be called *before* Org attempts to resolve the
9393 link by doing text searches in the current buffer - so if you want a
9394 link \"[[target]]\" to still find \"<<target>>\", your function should
9395 handle this as a special case.
9397 When the function does handle the link, it must return a non-nil value.
9398 If it decides that it is not responsible for this link, it must return
9399 nil to indicate that that Org-mode can continue with other options
9400 like exact and fuzzy text search.")
9402 (defun org-next-link ()
9403 "Move forward to the next link.
9404 If the link is in hidden text, expose it."
9406 (when (and org-link-search-failed (eq this-command last-command))
9407 (goto-char (point-min))
9408 (message "Link search wrapped back to beginning of buffer"))
9409 (setq org-link-search-failed nil)
9410 (let* ((pos (point))
9412 (a (assoc :link ct)))
9413 (if a (goto-char (nth 2 a)))
9414 (if (re-search-forward org-any-link-re nil t)
9416 (goto-char (match-beginning 0))
9417 (if (outline-invisible-p) (org-show-context)))
9419 (setq org-link-search-failed t)
9420 (error "No further link found"))))
9422 (defun org-previous-link ()
9423 "Move backward to the previous link.
9424 If the link is in hidden text, expose it."
9426 (when (and org-link-search-failed (eq this-command last-command))
9427 (goto-char (point-max))
9428 (message "Link search wrapped back to end of buffer"))
9429 (setq org-link-search-failed nil)
9430 (let* ((pos (point))
9432 (a (assoc :link ct)))
9433 (if a (goto-char (nth 1 a)))
9434 (if (re-search-backward org-any-link-re nil t)
9436 (goto-char (match-beginning 0))
9437 (if (outline-invisible-p) (org-show-context)))
9439 (setq org-link-search-failed t)
9440 (error "No further link found"))))
9442 (defun org-translate-link (s)
9443 "Translate a link string if a translation function has been defined."
9444 (if (and org-link-translation-function
9445 (fboundp org-link-translation-function)
9446 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9448 (setq s (funcall org-link-translation-function
9449 (match-string 1) (match-string 2)))
9450 (concat (car s) ":" (cdr s)))
9453 (defun org-translate-link-from-planner (type path)
9454 "Translate a link from Emacs Planner syntax so that Org can follow it.
9455 This is still an experimental function, your mileage may vary."
9457 ((member type '("http" "https" "news" "ftp"))
9458 ;; standard Internet links are the same.
9460 ((and (equal type "irc") (string-match "^//" path))
9461 ;; Planner has two / at the beginning of an irc link, we have 1.
9462 ;; We should have zero, actually....
9463 (setq path (substring path 1)))
9464 ((and (equal type "lisp") (string-match "^/" path))
9465 ;; Planner has a slash, we do not.
9466 (setq type "elisp" path (substring path 1)))
9467 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9468 ;; A typical message link. Planner has the id after the final slash,
9469 ;; we separate it with a hash mark
9470 (setq path (concat (match-string 1 path) "#"
9471 (org-remove-angle-brackets (match-string 2 path)))))
9475 (defun org-find-file-at-mouse (ev)
9476 "Open file link or URL at mouse."
9478 (mouse-set-point ev)
9479 (org-open-at-point 'in-emacs))
9481 (defun org-open-at-mouse (ev)
9482 "Open file link or URL at mouse.
9483 See the docstring of `org-open-file' for details."
9485 (mouse-set-point ev)
9486 (if (eq major-mode 'org-agenda-mode)
9487 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9488 (org-open-at-point))
9490 (defvar org-window-config-before-follow-link nil
9491 "The window configuration before following a link.
9492 This is saved in case the need arises to restore it.")
9494 (defvar org-open-link-marker (make-marker)
9495 "Marker pointing to the location where `org-open-at-point; was called.")
9498 (defun org-open-at-point-global ()
9499 "Follow a link like Org-mode does.
9500 This command can be called in any mode to follow a link that has
9503 (org-run-like-in-org-mode 'org-open-at-point))
9506 (defun org-open-link-from-string (s &optional arg reference-buffer)
9507 "Open a link in the string S, as if it was in Org-mode."
9508 (interactive "sLink: \nP")
9509 (let ((reference-buffer (or reference-buffer (current-buffer))))
9511 (let ((org-inhibit-startup (not reference-buffer)))
9514 (goto-char (point-min))
9515 (when reference-buffer
9516 (setq org-link-abbrev-alist-local
9517 (with-current-buffer reference-buffer
9518 org-link-abbrev-alist-local)))
9519 (org-open-at-point arg reference-buffer)))))
9521 (defvar org-open-at-point-functions nil
9522 "Hook that is run when following a link at point.
9524 Functions in this hook must return t if they identify and follow
9525 a link at point. If they don't find anything interesting at point,
9526 they must return nil.")
9528 (defun org-open-at-point (&optional arg reference-buffer)
9529 "Open link at or after point.
9530 If there is no link at point, this function will search forward up to
9531 the end of the current line.
9532 Normally, files will be opened by an appropriate application. If the
9533 optional prefix argument ARG is non-nil, Emacs will visit the file.
9534 With a double prefix argument, try to open outside of Emacs, in the
9535 application the system uses for this file type."
9537 ;; if in a code block, then open the block's results
9538 (unless (call-interactively #'org-babel-open-src-block-result)
9539 (org-load-modules-maybe)
9540 (move-marker org-open-link-marker (point))
9541 (setq org-window-config-before-follow-link (current-window-configuration))
9542 (org-remove-occur-highlights nil nil t)
9544 ((and (org-at-heading-p)
9546 (concat org-plain-link-re "\\|"
9547 org-bracket-link-regexp "\\|"
9548 org-angle-link-re "\\|"
9549 "[ \t]:[^ \t\n]+:[ \t]*$")))
9550 (not (get-text-property (point) 'org-linked-text)))
9551 (or (org-offer-links-in-entry arg)
9552 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9553 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9554 ((org-at-timestamp-p t) (org-follow-timestamp-link))
9555 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9556 (not (org-in-regexp org-bracket-link-regexp)))
9557 (org-footnote-action))
9559 (let (type path link line search (pos (point)))
9562 (skip-chars-forward "^]\n\r")
9563 (when (org-in-regexp org-bracket-link-regexp 1)
9564 (setq link (org-extract-attributes
9565 (org-link-unescape (org-match-string-no-properties 1))))
9566 (while (string-match " *\n *" link)
9567 (setq link (replace-match " " t t link)))
9568 (setq link (org-link-expand-abbrev link))
9570 ((or (file-name-absolute-p link)
9571 (string-match "^\\.\\.?/" link))
9572 (setq type "file" path link))
9573 ((string-match org-link-re-with-space3 link)
9574 (setq type (match-string 1 link) path (match-string 2 link)))
9575 (t (setq type "thisfile" path link)))
9578 (when (get-text-property (point) 'org-linked-text)
9579 (setq type "thisfile"
9580 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9581 (1+ (point)) (point))
9582 path (buffer-substring
9583 (or (previous-single-property-change pos 'org-linked-text)
9585 (or (next-single-property-change pos 'org-linked-text)
9590 (when (or (org-in-regexp org-angle-link-re)
9591 (org-in-regexp org-plain-link-re))
9592 (setq type (match-string 1)
9593 path (org-link-unescape (match-string 2)))
9596 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9598 path (match-string 1))
9599 (while (string-match ":" path)
9600 (setq path (replace-match "+" t t path)))
9602 (when (org-in-regexp "<\\([^><\n]+\\)>")
9603 (setq type "tree-match"
9604 path (match-string 1))
9607 (error "No link found"))
9609 ;; switch back to reference buffer
9610 ;; needed when if called in a temporary buffer through
9611 ;; org-open-link-from-string
9612 (with-current-buffer (or reference-buffer (current-buffer))
9614 ;; Remove any trailing spaces in path
9615 (if (string-match " +\\'" path)
9616 (setq path (replace-match "" t t path)))
9617 (if (and org-link-translation-function
9618 (fboundp org-link-translation-function))
9619 ;; Check if we need to translate the link
9620 (let ((tmp (funcall org-link-translation-function type path)))
9621 (setq type (car tmp) path (cdr tmp))))
9625 ((assoc type org-link-protocols)
9626 (funcall (nth 1 (assoc type org-link-protocols)) path))
9628 ((equal type "mailto")
9629 (let ((cmd (car org-link-mailto-program))
9630 (args (cdr org-link-mailto-program)) args1
9631 (address path) (subject "") a)
9632 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9633 (setq address (match-string 1 path)
9634 subject (org-link-escape (match-string 2 path))))
9637 ((not (stringp (car args))) (push (pop args) args1))
9638 (t (setq a (pop args))
9639 (if (string-match "%a" a)
9640 (setq a (replace-match address t t a)))
9641 (if (string-match "%s" a)
9642 (setq a (replace-match subject t t a)))
9644 (apply cmd (nreverse args1))))
9646 ((member type '("http" "https" "ftp" "news"))
9647 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9649 path org-link-escape-chars-browser)
9652 ((string= type "doi")
9653 (browse-url (concat "http://dx.doi.org/" (if (org-string-match-p "[[:nonascii:] ]" path)
9655 path org-link-escape-chars-browser)
9658 ((member type '("message"))
9659 (browse-url (concat type ":" path)))
9661 ((string= type "tags")
9662 (org-tags-view arg path))
9664 ((string= type "tree-match")
9665 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9667 ((string= type "file")
9668 (if (string-match "::\\([0-9]+\\)\\'" path)
9669 (setq line (string-to-number (match-string 1 path))
9670 path (substring path 0 (match-beginning 0)))
9671 (if (string-match "::\\(.+\\)\\'" path)
9672 (setq search (match-string 1 path)
9673 path (substring path 0 (match-beginning 0)))))
9674 (if (string-match "[*?{]" (file-name-nondirectory path))
9676 (org-open-file path arg line search)))
9678 ((string= type "shell")
9680 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9681 (string-match org-confirm-shell-link-not-regexp cmd))
9682 (not org-confirm-shell-link-function)
9683 (funcall org-confirm-shell-link-function
9684 (format "Execute \"%s\" in shell? "
9685 (org-add-props cmd nil
9686 'face 'org-warning))))
9688 (message "Executing %s" cmd)
9689 (shell-command cmd))
9692 ((string= type "elisp")
9694 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9695 (string-match org-confirm-elisp-link-not-regexp cmd))
9696 (not org-confirm-elisp-link-function)
9697 (funcall org-confirm-elisp-link-function
9698 (format "Execute \"%s\" as elisp? "
9699 (org-add-props cmd nil
9700 'face 'org-warning))))
9701 (message "%s => %s" cmd
9702 (if (equal (string-to-char cmd) ?\()
9704 (call-interactively (read cmd))))
9707 ((and (string= type "thisfile")
9708 (run-hook-with-args-until-success
9709 'org-open-link-functions path)))
9711 ((string= type "thisfile")
9713 (switch-to-buffer-other-window
9714 (org-get-buffer-for-internal-link (current-buffer)))
9715 (org-mark-ring-push))
9716 (let ((cmd `(org-link-search
9718 ,(cond ((equal arg '(4)) ''occur)
9719 ((equal arg '(16)) ''org-occur)
9722 (condition-case nil (let ((org-link-search-inhibit-query t))
9724 (error (progn (widen) (eval cmd))))))
9727 (browse-url-at-point)))))))
9728 (move-marker org-open-link-marker nil)
9729 (run-hook-with-args 'org-follow-link-hook)))
9731 (defun org-offer-links-in-entry (&optional nth zero)
9732 "Offer links in the current entry and follow the selected link.
9733 If there is only one link, follow it immediately as well.
9734 If NTH is an integer, immediately pick the NTH link found.
9735 If ZERO is a string, check also this string for a link, and if
9736 there is one, offer it as link number zero."
9737 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9738 "\\(" org-angle-link-re "\\)\\|"
9739 "\\(" org-plain-link-re "\\)"))
9741 (in-emacs (if (integerp nth) nil nth))
9742 have-zero end links link c)
9743 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9744 (push (match-string 0 zero) links)
9745 (setq cnt (1- cnt) have-zero t))
9747 (org-back-to-heading t)
9748 (setq end (save-excursion (outline-next-heading) (point)))
9749 (while (re-search-forward re end t)
9750 (push (match-string 0) links))
9751 (setq links (org-uniquify (reverse links))))
9755 (message "No links"))
9756 ((equal (length links) 1)
9757 (setq link (list (car links))))
9758 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9759 (setq link (list (nth (if have-zero nth (1- nth)) links))))
9760 (t ; we have to select a link
9762 (save-window-excursion
9763 (delete-other-windows)
9764 (with-output-to-temp-buffer "*Select Link*"
9766 (if (not (string-match org-bracket-link-regexp l))
9767 (princ (format "[%c] %s\n" (incf cnt)
9768 (org-remove-angle-brackets l)))
9770 (princ (format "[%c] %s (%s)\n" (incf cnt)
9771 (match-string 3 l) (match-string 1 l)))
9772 (princ (format "[%c] %s\n" (incf cnt)
9773 (match-string 1 l))))))
9775 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9776 (message "Select link to open, RET to open all:")
9777 (setq c (read-char-exclusive))
9778 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9779 (when (equal c ?q) (error "Abort"))
9783 (if have-zero (setq nth (1+ nth)))
9784 (unless (and (integerp nth) (>= (length links) nth))
9785 (error "Invalid link selection"))
9786 (setq link (list (nth (1- nth) links))))))
9788 (let ((buf (current-buffer)))
9790 (org-open-link-from-string l in-emacs buf))
9794 ;; Add special file links that specify the way of opening
9796 (org-add-link-type "file+sys" 'org-open-file-with-system)
9797 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9798 (defun org-open-file-with-system (path)
9799 "Open file at PATH using the system way of opening it."
9800 (org-open-file path 'system))
9801 (defun org-open-file-with-emacs (path)
9802 "Open file at PATH in Emacs."
9803 (org-open-file path 'emacs))
9804 (defun org-remove-file-link-modifiers ()
9805 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9806 (goto-char (point-min))
9807 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9809 (replace-match "file:" t t))))
9810 (eval-after-load "org-exp"
9811 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9812 'org-remove-file-link-modifiers))
9816 (defun org-get-effort (&optional pom)
9817 "Get the effort estimate for the current entry."
9818 (org-entry-get pom org-effort-property))
9822 (defvar org-create-file-search-functions nil
9823 "List of functions to construct the right search string for a file link.
9824 These functions are called in turn with point at the location to
9825 which the link should point.
9827 A function in the hook should first test if it would like to
9828 handle this file type, for example by checking the `major-mode'
9829 or the file extension. If it decides not to handle this file, it
9830 should just return nil to give other functions a chance. If it
9831 does handle the file, it must return the search string to be used
9832 when following the link. The search string will be part of the
9833 file link, given after a double colon, and `org-open-at-point'
9834 will automatically search for it. If special measures must be
9835 taken to make the search successful, another function should be
9836 added to the companion hook `org-execute-file-search-functions',
9839 A function in this hook may also use `setq' to set the variable
9840 `description' to provide a suggestion for the descriptive text to
9841 be used for this link when it gets inserted into an Org-mode
9842 buffer with \\[org-insert-link].")
9844 (defvar org-execute-file-search-functions nil
9845 "List of functions to execute a file search triggered by a link.
9847 Functions added to this hook must accept a single argument, the
9848 search string that was part of the file link, the part after the
9849 double colon. The function must first check if it would like to
9850 handle this search, for example by checking the `major-mode' or
9851 the file extension. If it decides not to handle this search, it
9852 should just return nil to give other functions a chance. If it
9853 does handle the search, it must return a non-nil value to keep
9854 other functions from trying.
9856 Each function can access the current prefix argument through the
9857 variable `current-prefix-argument'. Note that a single prefix is
9858 used to force opening a link in Emacs, so it may be good to only
9859 use a numeric or double prefix to guide the search function.
9861 In case this is needed, a function in this hook can also restore
9862 the window configuration before `org-open-at-point' was called using:
9864 (set-window-configuration org-window-config-before-follow-link)")
9866 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
9867 (defun org-link-search (s &optional type avoid-pos stealth)
9868 "Search for a link search option.
9869 If S is surrounded by forward slashes, it is interpreted as a
9870 regular expression. In org-mode files, this will create an `org-occur'
9871 sparse tree. In ordinary files, `occur' will be used to list matches.
9872 If the current buffer is in `dired-mode', grep will be used to search
9873 in all files. If AVOID-POS is given, ignore matches near that position.
9875 When optional argument STEALTH is non-nil, do not modify
9876 visibility around point, thus ignoring
9877 `org-show-hierarchy-above', `org-show-following-heading' and
9878 `org-show-siblings' variables."
9879 (let ((case-fold-search t)
9880 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
9881 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
9882 (append '(("") (" ") ("\t") ("\n"))
9886 (pre nil) (post nil)
9887 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
9889 ;; First check if there are any special search functions
9890 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
9891 ;; Now try the builtin stuff
9892 ((and (equal (string-to-char s0) ?#)
9895 (goto-char (point-min))
9898 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
9899 (setq type 'dedicated
9900 pos (match-beginning 0))))
9901 ;; There is an exact target for this
9903 (org-back-to-heading t)))
9905 (goto-char (point-min))
9908 (concat "<<" (regexp-quote s0) ">>") nil t)
9909 (setq type 'dedicated
9910 pos (match-beginning 0))))
9911 ;; There is an exact target for this
9914 (goto-char (point-min))
9917 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
9918 (setq type 'dedicated pos (match-beginning 0))))
9919 ;; Found an invisible target.
9922 (goto-char (point-min))
9925 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
9926 (setq type 'dedicated pos (match-beginning 0))))
9927 ;; Found an element with a matching #+name affiliated keyword.
9929 ((and (string-match "^(\\(.*\\))$" s0)
9931 (goto-char (point-min))
9934 (concat "[^[]" (regexp-quote
9935 (format org-coderef-label-format
9936 (match-string 1 s0))))
9938 (setq type 'dedicated
9939 pos (1+ (match-beginning 0))))))
9940 ;; There is a coderef target for this
9942 ((string-match "^/\\(.*\\)/$" s)
9943 ;; A regular expression
9945 ((eq major-mode 'org-mode)
9946 (org-occur (match-string 1 s)))
9947 ;;((eq major-mode 'dired-mode)
9948 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
9949 (t (org-do-occur (match-string 1 s)))))
9950 ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
9951 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
9952 (goto-char (point-min))
9954 ((let (case-fold-search)
9955 (re-search-forward (format org-complex-heading-regexp-format
9958 ;; OK, found a match
9959 (setq type 'dedicated)
9960 (goto-char (match-beginning 0)))
9961 ((and (not org-link-search-inhibit-query)
9962 (eq org-link-search-must-match-exact-headline 'query-to-create)
9963 (y-or-n-p "No match - create this as a new heading? "))
9964 (goto-char (point-max))
9965 (or (bolp) (newline))
9966 (insert "* " s "\n")
9967 (beginning-of-line 0))
9970 (error "No match"))))
9972 ;; A normal search string
9973 (when (equal (string-to-char s) ?*)
9974 ;; Anchor on headlines, post may include tags.
9975 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
9976 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
9978 (remove-text-properties
9980 '(face nil mouse-face nil keymap nil fontified nil) s)
9981 ;; Make a series of regular expressions to find a match
9982 (setq words (org-split-string s "[ \n\r\t]+")
9984 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
9985 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
9987 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
9988 re2a (concat "[ \t\r\n]" re2a_)
9989 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
9990 re4 (concat "[^a-zA-Z_]" re4_)
9992 re1 (concat pre re2 post)
9993 re3 (concat pre (if pre re4_ re4) post)
9994 re5 (concat pre ".*" re4)
9995 re2 (concat pre re2)
9996 re2a (concat pre (if pre re2a_ re2a))
9997 re4 (concat pre (if pre re4_ re4))
9998 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
9999 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10003 ((eq type 'org-occur) (org-occur reall))
10004 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10005 (t (goto-char (point-min))
10007 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10008 (org-search-not-self 1 re1 nil t)
10009 (org-search-not-self 1 re2 nil t)
10010 (org-search-not-self 1 re2a nil t)
10011 (org-search-not-self 1 re3 nil t)
10012 (org-search-not-self 1 re4 nil t)
10013 (org-search-not-self 1 re5 nil t)
10015 (goto-char (match-beginning 1))
10017 (error "No match"))))))
10018 (and (eq major-mode 'org-mode)
10020 (org-show-context 'link-search))
10023 (defun org-search-not-self (group &rest args)
10024 "Execute `re-search-forward', but only accept matches that do not
10025 enclose the position of `org-open-link-marker'."
10026 (let ((m org-open-link-marker))
10028 (while (apply 're-search-forward args)
10029 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10030 (goto-char (match-end group))
10031 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10032 (> (match-beginning 0) (marker-position m))
10033 (< (match-end 0) (marker-position m)))
10035 (or (not (org-in-regexp
10036 org-bracket-link-analytic-regexp 1))
10037 (not (match-end 4)) ; no description
10038 (and (<= (match-beginning 4) (point))
10039 (>= (match-end 4) (point))))))
10040 (throw 'exit (point))))))))
10042 (defun org-get-buffer-for-internal-link (buffer)
10043 "Return a buffer to be used for displaying the link target of internal links."
10045 ((not org-display-internal-link-with-indirect-buffer)
10047 ((string-match "(Clone)$" (buffer-name buffer))
10048 (message "Buffer is already a clone, not making another one")
10049 ;; we also do not modify visibility in this case
10051 (t ; make a new indirect buffer for displaying the link
10052 (let* ((bn (buffer-name buffer))
10053 (ibn (concat bn "(Clone)"))
10054 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10055 (with-current-buffer ib (org-overview))
10058 (defun org-do-occur (regexp &optional cleanup)
10059 "Call the Emacs command `occur'.
10060 If CLEANUP is non-nil, remove the printout of the regular expression
10061 in the *Occur* buffer. This is useful if the regex is long and not useful
10065 (let ((cwin (selected-window)) win beg end)
10066 (when (setq win (get-buffer-window "*Occur*"))
10067 (select-window win))
10068 (goto-char (point-min))
10069 (when (re-search-forward "match[a-z]+" nil t)
10070 (setq beg (match-end 0))
10071 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10072 (setq end (1- (match-beginning 0)))))
10073 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10074 (goto-char (point-min))
10075 (select-window cwin))))
10077 ;;; The mark ring for links jumps
10079 (defvar org-mark-ring nil
10080 "Mark ring for positions before jumps in Org-mode.")
10081 (defvar org-mark-ring-last-goto nil
10082 "Last position in the mark ring used to go back.")
10083 ;; Fill and close the ring
10084 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10085 (loop for i from 1 to org-mark-ring-length do
10086 (push (make-marker) org-mark-ring))
10087 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10090 (defun org-mark-ring-push (&optional pos buffer)
10091 "Put the current position or POS into the mark ring and rotate it."
10093 (setq pos (or pos (point)))
10094 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10095 (move-marker (car org-mark-ring)
10097 (or buffer (current-buffer)))
10099 (substitute-command-keys
10100 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10102 (defun org-mark-ring-goto (&optional n)
10103 "Jump to the previous position in the mark ring.
10104 With prefix arg N, jump back that many stored positions. When
10105 called several times in succession, walk through the entire ring.
10106 Org-mode commands jumping to a different position in the current file,
10107 or to another Org-mode file, automatically push the old position
10111 (if (eq last-command this-command)
10112 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10113 (setq p org-mark-ring))
10114 (setq org-mark-ring-last-goto p)
10116 (org-pop-to-buffer-same-window (marker-buffer m))
10118 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10120 (defun org-remove-angle-brackets (s)
10121 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10122 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10124 (defun org-add-angle-brackets (s)
10125 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10126 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10128 (defun org-remove-double-quotes (s)
10129 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10130 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10133 ;;; Following specific links
10135 (defun org-follow-timestamp-link ()
10137 ((org-at-date-range-p t)
10138 (let ((org-agenda-start-on-weekday)
10139 (t1 (match-string 1))
10140 (t2 (match-string 2)))
10141 (setq t1 (time-to-days (org-time-string-to-time t1))
10142 t2 (time-to-days (org-time-string-to-time t2)))
10143 (org-agenda-list nil t1 (1+ (- t2 t1)))))
10144 ((org-at-timestamp-p t)
10145 (org-agenda-list nil (time-to-days (org-time-string-to-time
10146 (substring (match-string 1) 0 10)))
10148 (t (error "This should not happen"))))
10151 ;;; Following file links
10152 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10153 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10154 (declare-function mailcap-mime-info
10155 "mailcap" (string &optional request no-decode))
10156 (defvar org-wait nil)
10157 (defun org-open-file (path &optional in-emacs line search)
10158 "Open the file at PATH.
10159 First, this expands any special file name abbreviations. Then the
10160 configuration variable `org-file-apps' is checked if it contains an
10161 entry for this file type, and if yes, the corresponding command is launched.
10163 If no application is found, Emacs simply visits the file.
10165 With optional prefix argument IN-EMACS, Emacs will visit the file.
10166 With a double \\[universal-argument] \\[universal-argument] \
10167 prefix arg, Org tries to avoid opening in Emacs
10168 and to use an external application to visit the file.
10170 Optional LINE specifies a line to go to, optional SEARCH a string
10171 to search for. If LINE or SEARCH is given, the file will be
10172 opened in Emacs, unless an entry from org-file-apps that makes
10173 use of groups in a regexp matches.
10175 If you want to change the way frames are used when following a
10176 link, please customize `org-link-frame-setup'.
10178 If the file does not exist, an error is thrown."
10179 (let* ((file (if (equal path "")
10181 (substitute-in-file-name (expand-file-name path))))
10182 (file-apps (append org-file-apps (org-default-apps)))
10183 (apps (org-remove-if
10184 'org-file-apps-entry-match-against-dlink-p file-apps))
10185 (apps-dlink (org-remove-if-not
10186 'org-file-apps-entry-match-against-dlink-p file-apps))
10187 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10188 (dirp (if remp nil (file-directory-p file)))
10189 (file (if (and dirp org-open-directory-means-index-dot-org)
10190 (concat (file-name-as-directory file) "index.org")
10192 (a-m-a-p (assq 'auto-mode apps))
10193 (dfile (downcase file))
10194 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10195 (link (cond ((and (eq line nil)
10199 (concat file "::" (number-to-string line)))
10201 (concat file "::" search))))
10202 (dlink (downcase link))
10203 (old-buffer (current-buffer))
10205 (old-mode major-mode)
10206 ext cmd link-match-data)
10207 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10208 (setq ext (match-string 1 dfile))
10209 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10210 (setq ext (match-string 1 dfile))))
10212 ((member in-emacs '((16) system))
10213 (setq cmd (cdr (assoc 'system apps))))
10214 (in-emacs (setq cmd 'emacs))
10216 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10217 (and dirp (cdr (assoc 'directory apps)))
10218 ; first, try matching against apps-dlink
10219 ; if we get a match here, store the match data for later
10220 (let ((match (assoc-default dlink apps-dlink
10223 (progn (setq link-match-data (match-data))
10225 (progn (setq in-emacs (or in-emacs line search))
10226 nil))) ; if we have no match in apps-dlink,
10227 ; always open the file in emacs if line or search
10228 ; is given (for backwards compatibility)
10229 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10231 (cdr (assoc ext apps))
10232 (cdr (assoc t apps))))))
10233 (when (eq cmd 'system)
10234 (setq cmd (cdr (assoc 'system apps))))
10235 (when (eq cmd 'default)
10236 (setq cmd (cdr (assoc t apps))))
10237 (when (eq cmd 'mailcap)
10239 (mailcap-parse-mailcaps)
10240 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10241 (command (mailcap-mime-info mime-type)))
10242 (if (stringp command)
10244 (setq cmd 'emacs))))
10245 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10246 (not (file-exists-p file))
10247 (not org-open-non-existing-files))
10248 (error "No such file: %s" file))
10250 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10251 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10252 (while (string-match "['\"]%s['\"]" cmd)
10253 (setq cmd (replace-match "%s" t t cmd)))
10254 (while (string-match "%s" cmd)
10255 (setq cmd (replace-match
10257 (shell-quote-argument
10258 (convert-standard-filename file)))
10261 ;; Replace "%1", "%2" etc. in command with group matches from regex
10263 (let ((match-index 1)
10264 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10265 (set-match-data link-match-data)
10266 (while (<= match-index number-of-groups)
10267 (let ((regex (concat "%" (number-to-string match-index)))
10268 (replace-with (match-string match-index dlink)))
10269 (while (string-match regex cmd)
10270 (setq cmd (replace-match replace-with t t cmd))))
10271 (setq match-index (+ match-index 1)))))
10273 (save-window-excursion
10274 (start-process-shell-command cmd nil cmd)
10275 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10279 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10281 (if line (org-goto-line line)
10282 (if search (org-link-search search))))
10284 (let ((file (convert-standard-filename file)))
10286 (set-match-data link-match-data)
10288 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10289 (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
10290 (or (not (equal old-buffer (current-buffer)))
10291 (not (equal old-pos (point))))
10292 (org-mark-ring-push old-pos old-buffer))))
10294 (defun org-file-apps-entry-match-against-dlink-p (entry)
10295 "This function returns non-nil if `entry' uses a regular
10296 expression which should be matched against the whole link by
10299 It assumes that is the case when the entry uses a regular
10300 expression which has at least one grouping construct and the
10301 action is either a lisp form or a command string containing
10302 '%1', i.e. using at least one subexpression match as a
10304 (let ((selector (car entry))
10305 (action (cdr entry)))
10306 (if (stringp selector)
10307 (and (> (regexp-opt-depth selector) 0)
10308 (or (and (stringp action)
10309 (string-match "%[0-9]" action))
10313 (defun org-default-apps ()
10314 "Return the default applications for this operating system."
10316 ((eq system-type 'darwin)
10317 org-file-apps-defaults-macosx)
10318 ((eq system-type 'windows-nt)
10319 org-file-apps-defaults-windowsnt)
10320 (t org-file-apps-defaults-gnu)))
10322 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10323 "Convert extensions to regular expressions in the cars of LIST.
10324 Also, weed out any non-string entries, because the return value is used
10325 only for regexp matching.
10326 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10327 point to the symbol `emacs', indicating that the file should
10328 be opened in Emacs."
10331 (mapcar (lambda (x)
10332 (if (not (stringp (car x)))
10334 (if (string-match "\\W" (car x))
10336 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10339 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10341 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10342 (defun org-file-remote-p (file)
10343 "Test whether FILE specifies a location on a remote system.
10344 Return non-nil if the location is indeed remote.
10346 For example, the filename \"/user@host:/foo\" specifies a location
10347 on the system \"/user@host:\"."
10348 (cond ((fboundp 'file-remote-p)
10349 (file-remote-p file))
10350 ((fboundp 'tramp-handle-file-remote-p)
10351 (tramp-handle-file-remote-p file))
10352 ((and (boundp 'ange-ftp-name-format)
10353 (string-match (car ange-ftp-name-format) file))
10360 (defun org-get-org-file ()
10361 "Read a filename, with default directory `org-directory'."
10362 (let ((default (or org-default-notes-file remember-data-file)))
10363 (read-file-name (format "File name [%s]: " default)
10364 (file-name-as-directory org-directory)
10367 (defun org-notes-order-reversed-p ()
10368 "Check if the current file should receive notes in reversed order."
10370 ((not org-reverse-note-order) nil)
10371 ((eq t org-reverse-note-order) t)
10372 ((not (listp org-reverse-note-order)) nil)
10374 (let ((all org-reverse-note-order)
10376 (while (setq entry (pop all))
10377 (if (string-match (car entry) buffer-file-name)
10378 (throw 'exit (cdr entry))))
10381 (defvar org-refile-target-table nil
10382 "The list of refile targets, created by `org-refile'.")
10384 (defvar org-agenda-new-buffers nil
10385 "Buffers created to visit agenda files.")
10387 (defvar org-refile-cache nil
10388 "Cache for refile targets.")
10390 (defvar org-refile-markers nil
10391 "All the markers used for caching refile locations.")
10393 (defun org-refile-marker (pos)
10394 "Get a new refile marker, but only if caching is in use."
10395 (if (not org-refile-use-cache)
10397 (let ((m (make-marker)))
10398 (move-marker m pos)
10399 (push m org-refile-markers)
10402 (defun org-refile-cache-clear ()
10403 "Clear the refile cache and disable all the markers."
10404 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10405 (setq org-refile-markers nil)
10406 (setq org-refile-cache nil)
10407 (message "Refile cache has been cleared"))
10409 (defun org-refile-cache-check-set (set)
10410 "Check if all the markers in the cache still have live buffers."
10413 (while (and set (setq marker (nth 3 (pop set))))
10414 ;; if org-refile-use-outline-path is 'file, marker may be nil
10415 (when (and marker (null (marker-buffer marker)))
10416 (message "not found") (sit-for 3)
10417 (throw 'exit nil)))
10420 (defun org-refile-cache-put (set &rest identifiers)
10421 "Push the refile targets SET into the cache, under IDENTIFIERS."
10422 (let* ((key (sha1 (prin1-to-string identifiers)))
10423 (entry (assoc key org-refile-cache)))
10426 (push (cons key set) org-refile-cache))))
10428 (defun org-refile-cache-get (&rest identifiers)
10429 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10431 ((not org-refile-cache) nil)
10432 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10434 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10435 org-refile-cache))))
10436 (and set (org-refile-cache-check-set set) set)))))
10438 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10439 "Produce a table with refile targets."
10440 (let ((case-fold-search nil)
10441 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10442 (entries (or org-refile-targets '((nil . (:level . 1)))))
10443 targets tgs txt re files f desc descre fast-path-p level pos0)
10444 (message "Getting targets...")
10445 (with-current-buffer (or default-buffer (current-buffer))
10446 (while (setq entry (pop entries))
10447 (setq files (car entry) desc (cdr entry))
10448 (setq fast-path-p nil)
10450 ((null files) (setq files (list (current-buffer))))
10451 ((eq files 'org-agenda-files)
10452 (setq files (org-agenda-files 'unrestricted)))
10453 ((and (symbolp files) (fboundp files))
10454 (setq files (funcall files)))
10455 ((and (symbolp files) (boundp files))
10456 (setq files (symbol-value files))))
10457 (if (stringp files) (setq files (list files)))
10459 ((eq (car desc) :tag)
10460 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10461 ((eq (car desc) :todo)
10462 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10463 ((eq (car desc) :regexp)
10464 (setq descre (cdr desc)))
10465 ((eq (car desc) :level)
10466 (setq descre (concat "^\\*\\{" (number-to-string
10467 (if org-odd-levels-only
10468 (1- (* 2 (cdr desc)))
10471 ((eq (car desc) :maxlevel)
10472 (setq fast-path-p t)
10473 (setq descre (concat "^\\*\\{1," (number-to-string
10474 (if org-odd-levels-only
10475 (1- (* 2 (cdr desc)))
10478 (t (error "Bad refiling target description %s" desc)))
10479 (while (setq f (pop files))
10480 (with-current-buffer
10481 (if (bufferp f) f (org-get-agenda-file-buffer f))
10483 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10485 (if (bufferp f) (setq f (buffer-file-name
10486 (buffer-base-buffer f))))
10487 (setq f (and f (expand-file-name f)))
10488 (if (eq org-refile-use-outline-path 'file)
10489 (push (list (file-name-nondirectory f) f nil nil) tgs))
10493 (goto-char (point-min))
10494 (while (re-search-forward descre nil t)
10495 (goto-char (setq pos0 (point-at-bol)))
10497 (when org-refile-target-verify-function
10499 (or (funcall org-refile-target-verify-function)
10501 (when (and (looking-at org-complex-heading-regexp)
10502 (not (member (match-string 4) excluded-entries))
10504 (setq level (org-reduced-level
10505 (- (match-end 1) (match-beginning 1)))
10506 txt (org-link-display-format (match-string 4))
10507 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10508 re (format org-complex-heading-regexp-format
10509 (regexp-quote (match-string 4))))
10510 (when org-refile-use-outline-path
10511 (setq txt (mapconcat
10514 (if (eq org-refile-use-outline-path
10516 (list (file-name-nondirectory
10518 (buffer-base-buffer))))
10519 (if (eq org-refile-use-outline-path
10521 (list (buffer-file-name
10522 (buffer-base-buffer)))))
10523 (org-get-outline-path fast-path-p
10527 (push (list txt f re (org-refile-marker (point)))
10529 (when (= (point) pos0)
10530 ;; verification function has not moved point
10531 (goto-char (point-at-eol))))))))
10532 (when org-refile-use-cache
10533 (org-refile-cache-put tgs (buffer-file-name) descre))
10534 (setq targets (append tgs targets))
10536 (message "Getting targets...done")
10537 (nreverse targets)))
10539 (defun org-protect-slash (s)
10540 (while (string-match "/" s)
10541 (setq s (replace-match "\\" t t s)))
10544 (defvar org-olpa (make-vector 20 nil))
10546 (defun org-get-outline-path (&optional fastp level heading)
10547 "Return the outline path to the current entry, as a list.
10549 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10550 routine which makes outline path derivations for an entire file,
10551 avoiding backtracing. Refile target collection makes use of that."
10555 (error "Outline path failure, more than 19 levels"))
10556 (loop for i from level upto 19 do
10557 (aset org-olpa i nil))
10559 (delq nil (append org-olpa nil))
10560 (aset org-olpa level heading)))
10561 (let (rtn case-fold-search)
10565 (while (org-up-heading-safe)
10566 (when (looking-at org-complex-heading-regexp)
10567 (push (org-match-string-no-properties 4) rtn)))
10570 (defun org-format-outline-path (path &optional width prefix)
10571 "Format the outline path PATH for display.
10572 Width is the maximum number of characters that is available.
10573 Prefix is a prefix to be included in the returned string,
10574 such as the file name."
10575 (setq width (or width 79))
10576 (if prefix (setq width (- width (length prefix))))
10579 (let* ((nsteps (length path))
10580 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10581 (maxwidth (if (<= total-width width)
10582 10000 ;; everything fits
10583 ;; we need to shorten the level headings
10584 (/ (- width nsteps) nsteps)))
10585 (org-odd-levels-only nil)
10587 (total (1+ (length prefix))))
10588 (setq maxwidth (max maxwidth 10))
10593 (if (and (= n nsteps) (< maxwidth 10000))
10594 (setq maxwidth (- total-width total)))
10595 (if (< (length h) maxwidth)
10596 (progn (setq total (+ total (length h) 1)) h)
10597 (setq h (substring h 0 (- maxwidth 2))
10598 total (+ total maxwidth 1))
10599 (if (string-match "[ \t]+\\'" h)
10600 (setq h (substring h 0 (match-beginning 0))))
10601 (setq h (concat h "..")))
10602 (org-add-props h nil 'face
10603 (nth (% (1- n) org-n-level-faces)
10608 (defun org-display-outline-path (&optional file current)
10609 "Display the current outline path in the echo area."
10611 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10612 (case-fold-search nil)
10613 (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
10614 (if current (setq path (append path
10616 (org-back-to-heading t)
10617 (if (looking-at org-complex-heading-regexp)
10618 (list (match-string 4)))))))
10620 (org-format-outline-path
10623 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10625 (defvar org-refile-history nil
10626 "History for refiling operations.")
10628 (defvar org-after-refile-insert-hook nil
10629 "Hook run after `org-refile' has inserted its stuff at the new location.
10630 Note that this is still *before* the stuff will be removed from
10631 the *old* location.")
10633 (defvar org-capture-last-stored-marker)
10634 (defun org-refile (&optional goto default-buffer rfloc)
10635 "Move the entry or entries at point to another heading.
10636 The list of target headings is compiled using the information in
10637 `org-refile-targets', which see.
10639 At the target location, the entry is filed as a subitem of the target
10640 heading. Depending on `org-reverse-note-order', the new subitem will
10641 either be the first or the last subitem.
10643 If there is an active region, all entries in that region will be moved.
10644 However, the region must fulfill the requirement that the first heading
10645 is the first one sets the top-level of the moved text - at most siblings
10646 below it are allowed.
10648 With prefix arg GOTO, the command will only visit the target location
10649 and not actually move anything.
10651 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10652 go to the location where the last refiling operation has put the subtree.
10653 With a prefix argument of `2', refile to the running clock.
10655 RFLOC can be a refile location obtained in a different way.
10657 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10659 If you are using target caching (see `org-refile-use-cache'),
10660 You have to clear the target cache in order to find new targets.
10661 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10662 prefix argument (`C-u C-u C-u C-c C-w')."
10665 (if (member goto '(0 (64)))
10666 (org-refile-cache-clear)
10667 (let* ((cbuf (current-buffer))
10668 (regionp (org-region-active-p))
10669 (region-start (and regionp (region-beginning)))
10670 (region-end (and regionp (region-end)))
10671 (region-length (and regionp (- region-end region-start)))
10672 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10673 pos it nbuf file re level reversed)
10674 (setq last-command nil)
10676 (goto-char region-start)
10677 (or (bolp) (goto-char (point-at-bol)))
10678 (setq region-start (point))
10679 (unless (or (org-kill-is-subtree-p
10680 (buffer-substring region-start region-end))
10681 (prog1 org-refile-active-region-within-subtree
10682 (org-toggle-heading)))
10683 (error "The region is not a (sequence of) subtree(s)")))
10684 (if (equal goto '(16))
10685 (org-refile-goto-last-stored)
10687 (and (equal goto 2)
10688 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10690 (setq it (list (or org-clock-heading "running clock")
10692 (marker-buffer org-clock-hd-marker))
10694 (marker-position org-clock-hd-marker)))
10697 (let (heading-text)
10700 (org-back-to-heading t)
10702 (nth 4 (org-heading-components))))
10703 (org-refile-get-location
10704 (cond (goto "Goto")
10705 (regionp "Refile region to")
10706 (t (concat "Refile subtree \""
10707 heading-text "\" to")))
10709 (and (not (equal '(4) goto))
10710 org-refile-allow-creating-parent-nodes)
10712 (setq file (nth 1 it)
10715 (if (and (not goto)
10717 (equal (buffer-file-name) file)
10719 (and (>= pos region-start)
10720 (<= pos region-end))
10721 (and (>= pos (point))
10722 (< pos (save-excursion
10723 (org-end-of-subtree t t))))))
10724 (error "Cannot refile to position inside the tree or region"))
10726 (setq nbuf (or (find-buffer-visiting file)
10727 (find-file-noselect file)))
10730 (org-pop-to-buffer-same-window nbuf)
10732 (org-show-context 'org-goto))
10735 (org-kill-new (buffer-substring region-start region-end))
10736 (org-save-markers-in-region region-start region-end))
10737 (org-copy-subtree 1 nil t))
10738 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10739 (find-file-noselect file)))
10740 (setq reversed (org-notes-order-reversed-p))
10747 (looking-at org-outline-regexp)
10748 (setq level (org-get-valid-level (funcall outline-level) 1))
10751 (or (outline-next-heading) (point-max))
10752 (or (save-excursion (org-get-next-sibling))
10753 (org-end-of-subtree t t)
10757 (goto-char (point-max))
10758 (goto-char (point-min))
10759 (or (outline-next-heading) (goto-char (point-max)))))
10760 (if (not (bolp)) (newline))
10761 (org-paste-subtree level)
10762 (when org-log-refile
10763 (org-add-log-setup 'refile nil nil 'findpos
10765 (unless (eq org-log-refile 'note)
10766 (save-excursion (org-add-log-note))))
10767 (and org-auto-align-tags (org-set-tags nil t))
10768 (bookmark-set "org-refile-last-stored")
10769 ;; If we are refiling for capture, make sure that the
10770 ;; last-capture pointers point here
10771 (when (org-bound-and-true-p org-refile-for-capture)
10772 (bookmark-set "org-capture-last-stored-marker")
10773 (move-marker org-capture-last-stored-marker (point)))
10774 (if (fboundp 'deactivate-mark) (deactivate-mark))
10775 (run-hooks 'org-after-refile-insert-hook))))
10777 (delete-region (point) (+ (point) region-length))
10779 (when (featurep 'org-inlinetask)
10780 (org-inlinetask-remove-END-maybe))
10781 (setq org-markers-to-move nil)
10782 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10784 (defun org-refile-goto-last-stored ()
10785 "Go to the location where the last refile was stored."
10787 (bookmark-jump "org-refile-last-stored")
10788 (message "This is the location of the last refile"))
10790 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10792 "Prompt the user for a refile location, using PROMPT.
10793 PROMPT should not be suffixed with a colon and a space, because
10794 this function appends the default value from
10795 `org-refile-history' automatically, if that is not empty.
10796 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10797 this is used for the GOTO interface."
10798 (let ((org-refile-targets org-refile-targets)
10799 (org-refile-use-outline-path org-refile-use-outline-path)
10801 (when (and (eq major-mode 'org-mode)
10802 (not org-refile-use-cache)
10806 (setq excluded-entries
10807 (append excluded-entries (list (org-get-heading t t)))))))
10808 (setq org-refile-target-table
10809 (org-refile-get-targets default-buffer excluded-entries)))
10810 (unless org-refile-target-table
10811 (error "No refile targets"))
10812 (let* ((prompt (concat prompt
10813 (and (car org-refile-history)
10814 (concat " (default " (car org-refile-history) ")"))
10816 (cbuf (current-buffer))
10817 (partial-completion-mode nil)
10818 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10819 (cfunc (if (and org-refile-use-outline-path
10820 org-outline-path-complete-in-steps)
10821 'org-olpath-completing-read
10822 'org-icompleting-read))
10823 (extra (if org-refile-use-outline-path "/" ""))
10824 (filename (and cfn (expand-file-name cfn)))
10827 (if (and (not (member org-refile-use-outline-path
10828 '(file full-file-path)))
10829 (not (equal filename (nth 1 x))))
10830 (cons (concat (car x) extra " ("
10831 (file-name-nondirectory (nth 1 x)) ")")
10833 (cons (concat (car x) extra) (cdr x))))
10834 org-refile-target-table))
10835 (completion-ignore-case t)
10836 pa answ parent-target child parent old-hist)
10837 (setq old-hist org-refile-history)
10838 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10839 nil 'org-refile-history (car org-refile-history)))
10840 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
10841 (org-refile-check-position pa)
10844 (when (or (not org-refile-history)
10845 (not (eq old-hist org-refile-history))
10846 (not (equal (car pa) (car org-refile-history))))
10847 (setq org-refile-history
10848 (cons (car pa) (if (assoc (car org-refile-history) tbl)
10850 (cdr org-refile-history))))
10851 (if (equal (car org-refile-history) (nth 1 org-refile-history))
10852 (pop org-refile-history)))
10854 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
10856 (setq parent (match-string 1 answ)
10857 child (match-string 2 answ))
10858 (setq parent-target (or (assoc parent tbl)
10859 (assoc (concat parent "/") tbl)))
10860 (when (and parent-target
10861 (or (eq new-nodes t)
10862 (and (eq new-nodes 'confirm)
10863 (y-or-n-p (format "Create new node \"%s\"? "
10865 (org-refile-new-child parent-target child)))
10866 (error "Invalid target location")))))
10868 (declare-function org-string-nw-p "org-macs.el" (s))
10869 (defun org-refile-check-position (refile-pointer)
10870 "Check if the refile pointer matches the readline to which it points."
10871 (let* ((file (nth 1 refile-pointer))
10872 (re (nth 2 refile-pointer))
10873 (pos (nth 3 refile-pointer))
10875 (when (org-string-nw-p re)
10876 (setq buffer (if (markerp pos)
10877 (marker-buffer pos)
10878 (or (find-buffer-visiting file)
10879 (find-file-noselect file))))
10880 (with-current-buffer buffer
10885 (beginning-of-line 1)
10886 (unless (org-looking-at-p re)
10887 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
10889 (defun org-refile-new-child (parent-target child)
10890 "Use refile target PARENT-TARGET to add new CHILD below it."
10891 (unless parent-target
10892 (error "Cannot find parent for new node"))
10893 (let ((file (nth 1 parent-target))
10894 (pos (nth 3 parent-target))
10896 (with-current-buffer (or (find-buffer-visiting file)
10897 (find-file-noselect file))
10903 (goto-char (point-max))
10904 (if (not (bolp)) (newline)))
10905 (when (looking-at org-outline-regexp)
10906 (setq level (funcall outline-level))
10907 (org-end-of-subtree t t))
10908 (org-back-over-empty-lines)
10909 (insert "\n" (make-string
10910 (if pos (org-get-valid-level level 1) 1) ?*)
10912 (beginning-of-line 0)
10913 (list (concat (car parent-target) "/" child) file "" (point)))))))
10915 (defun org-olpath-completing-read (prompt collection &rest args)
10916 "Read an outline path like a file name."
10917 (let ((thetable collection)
10918 (org-completion-use-ido nil) ; does not work with ido.
10919 (org-completion-use-iswitchb nil)) ; or iswitchb
10921 'org-icompleting-read prompt
10922 (lambda (string predicate &optional flag)
10923 (let (rtn r f (l (length string)))
10927 (try-completion string thetable))
10930 (setq rtn (all-completions string thetable predicate))
10933 (setq r (substring x l))
10934 (if (string-match " ([^)]*)$" x)
10935 (setq f (match-string 0 x))
10937 (if (string-match "/" r)
10938 (concat string (substring r 0 (match-end 0)) f)
10943 (assoc string thetable)))
10947 ;;;; Dynamic blocks
10949 (defun org-find-dblock (name)
10950 "Find the first dynamic block with name NAME in the buffer.
10951 If not found, stay at current position and return nil."
10954 (goto-char (point-min))
10955 (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
10957 (match-beginning 0))))
10958 (if pos (goto-char pos))
10961 (defconst org-dblock-start-re
10962 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
10963 "Matches the start line of a dynamic block, with parameters.")
10965 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
10966 "Matches the end of a dynamic block.")
10968 (defun org-create-dblock (plist)
10969 "Create a dynamic block section, with parameters taken from PLIST.
10970 PLIST must contain a :name entry which is used as name of the block."
10971 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
10974 (let ((col (current-column))
10975 (name (plist-get plist :name)))
10976 (insert "#+BEGIN: " name)
10978 (if (eq (car plist) :name)
10979 (setq plist (cddr plist))
10980 (insert " " (prin1-to-string (pop plist)))))
10981 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
10982 (beginning-of-line -2)))
10984 (defun org-prepare-dblock ()
10985 "Prepare dynamic block for refresh.
10986 This empties the block, puts the cursor at the insert position and returns
10987 the property list including an extra property :name with the block name."
10988 (unless (looking-at org-dblock-start-re)
10989 (error "Not at a dynamic block"))
10990 (let* ((begdel (1+ (match-end 0)))
10991 (name (org-no-properties (match-string 1)))
10992 (params (append (list :name name)
10993 (read (concat "(" (match-string 3) ")")))))
10995 (beginning-of-line 1)
10996 (skip-chars-forward " \t")
10997 (setq params (plist-put params :indentation-column (current-column))))
10998 (unless (re-search-forward org-dblock-end-re nil t)
10999 (error "Dynamic block not terminated"))
11002 (list :content (buffer-substring
11003 begdel (match-beginning 0)))))
11004 (delete-region begdel (match-beginning 0))
11009 (defun org-map-dblocks (&optional command)
11010 "Apply COMMAND to all dynamic blocks in the current buffer.
11011 If COMMAND is not given, use `org-update-dblock'."
11012 (let ((cmd (or command 'org-update-dblock)))
11014 (goto-char (point-min))
11015 (while (re-search-forward org-dblock-start-re nil t)
11016 (goto-char (match-beginning 0))
11018 (condition-case nil
11020 (error (message "Error during update of dynamic block"))))
11021 (unless (re-search-forward org-dblock-end-re nil t)
11022 (error "Dynamic block not terminated"))))))
11024 (defun org-dblock-update (&optional arg)
11025 "User command for updating dynamic blocks.
11026 Update the dynamic block at point. With prefix ARG, update all dynamic
11027 blocks in the buffer."
11030 (org-update-all-dblocks)
11031 (or (looking-at org-dblock-start-re)
11032 (org-beginning-of-dblock))
11033 (org-update-dblock)))
11035 (defun org-update-dblock ()
11036 "Update the dynamic block at point.
11037 This means to empty the block, parse for parameters and then call
11038 the correct writing function."
11040 (save-window-excursion
11041 (let* ((pos (point))
11042 (line (org-current-line))
11043 (params (org-prepare-dblock))
11044 (name (plist-get params :name))
11045 (indent (plist-get params :indentation-column))
11046 (cmd (intern (concat "org-dblock-write:" name))))
11047 (message "Updating dynamic block `%s' at line %d..." name line)
11048 (funcall cmd params)
11049 (message "Updating dynamic block `%s' at line %d...done" name line)
11051 (when (and indent (> indent 0))
11052 (setq indent (make-string indent ?\ ))
11054 (org-beginning-of-dblock)
11056 (while (not (looking-at org-dblock-end-re))
11058 (beginning-of-line 2))
11059 (when (looking-at org-dblock-end-re)
11060 (and (looking-at "[ \t]+")
11061 (replace-match ""))
11062 (insert indent)))))))
11064 (defun org-beginning-of-dblock ()
11065 "Find the beginning of the dynamic block at point.
11066 Error if there is no such block at point."
11067 (let ((pos (point))
11070 (if (and (re-search-backward org-dblock-start-re nil t)
11071 (setq beg (match-beginning 0))
11072 (re-search-forward org-dblock-end-re nil t)
11073 (> (match-end 0) pos))
11076 (error "Not in a dynamic block"))))
11078 (defun org-update-all-dblocks ()
11079 "Update all dynamic blocks in the buffer.
11080 This function can be used in a hook."
11082 (when (eq major-mode 'org-mode)
11083 (org-map-dblocks 'org-update-dblock)))
11088 (defconst org-additional-option-like-keywords
11089 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11090 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11091 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11092 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11094 "ORGTBL" "TBLFM:" "TBLNAME:"
11095 "BEGIN_EXAMPLE" "END_EXAMPLE"
11096 "BEGIN_QUOTE" "END_QUOTE"
11097 "BEGIN_VERSE" "END_VERSE"
11098 "BEGIN_CENTER" "END_CENTER"
11099 "BEGIN_SRC" "END_SRC"
11100 "BEGIN_RESULT" "END_RESULT"
11102 "HEADER:" "HEADERS:"
11103 "CATEGORY:" "COLUMNS:" "PROPERTY:"
11104 "CAPTION:" "LABEL:"
11110 (defcustom org-structure-template-alist
11112 ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11113 "<src lang=\"?\">\n\n</src>")
11114 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11115 "<example>\n?\n</example>")
11116 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11117 "<quote>\n?\n</quote>")
11118 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11119 "<verse>\n?\n</verse>")
11120 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11121 "<center>\n?\n</center>")
11122 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11123 "<literal style=\"latex\">\n?\n</literal>")
11125 "<literal style=\"latex\">?</literal>")
11126 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11127 "<literal style=\"html\">\n?\n</literal>")
11129 "<literal style=\"html\">?</literal>")
11130 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11134 ("I" "#+INCLUDE %file ?"
11135 "<include file=%file markup=\"?\">")
11137 "Structure completion elements.
11138 This is a list of abbreviation keys and values. The value gets inserted
11139 if you type `<' followed by the key and then press the completion key,
11140 usually `M-TAB'. %file will be replaced by a file name after prompting
11141 for the file using completion. The cursor will be placed at the position
11142 of the `?` in the template.
11143 There are two templates for each key, the first uses the original Org syntax,
11144 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11145 the default when the /org-mtags.el/ module has been loaded. See also the
11146 variable `org-mtags-prefer-muse-templates'."
11147 :group 'org-completion
11149 (string :tag "Key")
11150 (string :tag "Template")
11151 (string :tag "Muse Template")))
11153 (defun org-try-structure-completion ()
11154 "Try to complete a structure template before point.
11155 This looks for strings like \"<e\" on an otherwise empty line and
11157 (let ((l (buffer-substring (point-at-bol) (point)))
11159 (when (and (looking-at "[ \t]*$")
11160 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11161 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11162 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11163 (match-beginning 1)) a)
11166 (defun org-complete-expand-structure-template (start cell)
11167 "Expand a structure template."
11168 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11169 (rpl (nth (if musep 2 1) cell))
11171 (delete-region start (point))
11172 (when (string-match "\\`#\\+" rpl)
11175 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11176 (setq ind (buffer-substring (point-at-bol) (point))))
11178 (setq start (point))
11179 (if (string-match "%file" rpl)
11180 (setq rpl (replace-match
11184 (abbreviate-file-name (read-file-name "Include file: ")))
11187 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11188 (concat "\n" ind)))
11190 (if (re-search-backward "\\?" start t) (delete-char 1))))
11192 ;;;; TODO, DEADLINE, Comments
11194 (defun org-toggle-comment ()
11195 "Change the COMMENT state of an entry."
11198 (org-back-to-heading)
11199 (let (case-fold-search)
11201 ((looking-at (format org-heading-keyword-regexp-format
11202 org-comment-string))
11203 (goto-char (match-end 1))
11204 (looking-at (concat " +" org-comment-string))
11205 (replace-match "" t t)
11206 (when (eolp) (insert " ")))
11207 ((looking-at org-outline-regexp)
11208 (goto-char (match-end 0))
11209 (insert org-comment-string " "))))))
11211 (defvar org-last-todo-state-is-todo nil
11212 "This is non-nil when the last TODO state change led to a TODO state.
11213 If the last change removed the TODO tag or switched to DONE, then
11216 (defvar org-setting-tags nil) ; dynamically skipped
11218 (defvar org-todo-setup-filter-hook nil
11219 "Hook for functions that pre-filter todo specs.
11220 Each function takes a todo spec and returns either nil or the spec
11221 transformed into canonical form." )
11223 (defvar org-todo-get-default-hook nil
11224 "Hook for functions that get a default item for todo.
11225 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11226 nil or a string to be used for the todo mark." )
11228 (defvar org-agenda-headline-snapshot-before-repeat)
11230 (defun org-current-effective-time ()
11231 "Return current time adjusted for `org-extend-today-until' variable"
11232 (let* ((ct (org-current-time))
11233 (dct (decode-time ct))
11235 (if (and org-use-effective-time
11236 (< (nth 2 dct) org-extend-today-until))
11237 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11241 (defun org-todo-yesterday (&optional arg)
11242 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11244 (if (eq major-mode 'org-agenda-mode)
11245 (apply 'org-agenda-todo-yesterday arg)
11246 (let* ((hour (third (decode-time
11247 (org-current-time))))
11248 (org-extend-today-until (1+ hour)))
11251 (defun org-todo (&optional arg)
11252 "Change the TODO state of an item.
11253 The state of an item is given by a keyword at the start of the heading,
11255 *** TODO Write paper
11258 The different keywords are specified in the variable `org-todo-keywords'.
11259 By default the available states are \"TODO\" and \"DONE\".
11260 So for this example: when the item starts with TODO, it is changed to DONE.
11261 When it starts with DONE, the DONE is removed. And when neither TODO nor
11262 DONE are present, add TODO at the beginning of the heading.
11264 With \\[universal-argument] prefix arg, use completion to determine the new \
11266 With numeric prefix arg, switch to that state.
11267 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11268 keywords (nextset).
11269 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11270 With a numeric prefix arg of 0, inhibit note taking for the change.
11272 For calling through lisp, arg is also interpreted in the following way:
11273 'none -> empty state
11274 \"\"(empty string) -> switch to empty state
11275 'done -> switch to DONE
11276 'nextset -> switch to the next set of keywords
11277 'previousset -> switch to the previous set of keywords
11278 \"WAITING\" -> switch to the specified keyword, but only if it
11279 really is a member of `org-todo-keywords'."
11281 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11282 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11283 'region-start-level 'region))
11284 org-loop-over-headlines-in-active-region)
11287 org-loop-over-headlines-in-active-region
11288 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11289 (if (equal arg '(16)) (setq arg 'nextset))
11290 (let ((org-blocker-hook org-blocker-hook)
11291 (case-fold-search nil))
11292 (when (equal arg '(64))
11293 (setq arg nil org-blocker-hook nil))
11294 (when (and org-blocker-hook
11295 (or org-inhibit-blocking
11296 (org-entry-get nil "NOBLOCKING")))
11297 (setq org-blocker-hook nil))
11300 (org-back-to-heading t)
11301 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11302 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11303 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11304 (let* ((match-data (match-data))
11305 (startpos (point-at-bol))
11306 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11307 (org-log-done org-log-done)
11308 (org-log-repeat org-log-repeat)
11309 (org-todo-log-states org-todo-log-states)
11310 (org-inhibit-logging
11312 (progn (setq arg nil) 'note) org-inhibit-logging))
11313 (this (match-string 1))
11314 (hl-pos (match-beginning 0))
11315 (head (org-get-todo-sequence-head this))
11316 (ass (assoc head org-todo-kwd-alist))
11317 (interpret (nth 1 ass))
11318 (done-word (nth 3 ass))
11319 (final-done-word (nth 4 ass))
11320 (org-last-state (or this ""))
11321 (completion-ignore-case t)
11322 (member (member this org-todo-keywords-1))
11323 (tail (cdr member))
11325 ((and org-todo-key-trigger
11326 (or (and (equal arg '(4))
11327 (eq org-use-fast-todo-selection 'prefix))
11328 (and (not arg) org-use-fast-todo-selection
11329 (not (eq org-use-fast-todo-selection
11331 ;; Use fast selection
11332 (org-fast-todo-selection))
11333 ((and (equal arg '(4))
11334 (or (not org-use-fast-todo-selection)
11335 (not org-todo-key-trigger)))
11336 ;; Read a state with completion
11337 (org-icompleting-read
11338 "State: " (mapcar (lambda(x) (list x))
11339 org-todo-keywords-1)
11343 (if tail (car tail) nil)
11344 (car org-todo-keywords-1)))
11346 (if (equal member org-todo-keywords-1)
11349 (nth (- (length org-todo-keywords-1)
11351 org-todo-keywords-1)
11352 (org-last org-todo-keywords-1))))
11353 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11354 (setq arg nil))) ; hack to fall back to cycling
11356 ;; user or caller requests a specific state
11358 ((equal arg "") nil)
11359 ((eq arg 'none) nil)
11360 ((eq arg 'done) (or done-word (car org-done-keywords)))
11362 (or (car (cdr (member head org-todo-heads)))
11363 (car org-todo-heads)))
11364 ((eq arg 'previousset)
11365 (let ((org-todo-heads (reverse org-todo-heads)))
11366 (or (car (cdr (member head org-todo-heads)))
11367 (car org-todo-heads))))
11368 ((car (member arg org-todo-keywords-1)))
11370 (error "State `%s' not valid in this file" arg))
11371 ((nth (1- (prefix-numeric-value arg))
11372 org-todo-keywords-1))))
11373 ((null member) (or head (car org-todo-keywords-1)))
11374 ((equal this final-done-word) nil) ;; -> make empty
11375 ((null tail) nil) ;; -> first entry
11376 ((memq interpret '(type priority))
11377 (if (eq this-command last-command)
11379 (if (> (length tail) 0)
11380 (or done-word (car org-done-keywords))
11385 (run-hook-with-args-until-success
11386 'org-todo-get-default-hook org-state org-last-state)
11388 (next (if org-state (concat " " org-state " ") " "))
11389 (change-plist (list :type 'todo-state-change :from this :to org-state
11390 :position startpos))
11392 (when org-blocker-hook
11393 (setq org-last-todo-state-is-todo
11394 (not (member this org-done-keywords)))
11395 (unless (save-excursion
11397 (org-with-wide-buffer
11398 (run-hook-with-args-until-failure
11399 'org-blocker-hook change-plist))))
11400 (if (org-called-interactively-p 'interactive)
11401 (error "TODO state change from %s to %s blocked" this org-state)
11403 (message "TODO state change from %s to %s blocked" this org-state)
11404 (throw 'exit nil))))
11405 (store-match-data match-data)
11406 (replace-match next t t)
11407 (unless (pos-visible-in-window-p hl-pos)
11408 (message "TODO state changed to %s" (org-trim next)))
11410 (setq head (org-get-todo-sequence-head org-state)
11411 ass (assoc head org-todo-kwd-alist)
11412 interpret (nth 1 ass)
11413 done-word (nth 3 ass)
11414 final-done-word (nth 4 ass)))
11415 (when (memq arg '(nextset previousset))
11416 (message "Keyword-Set %d/%d: %s"
11417 (- (length org-todo-sets) -1
11418 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11419 (length org-todo-sets)
11420 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11421 (setq org-last-todo-state-is-todo
11422 (not (member org-state org-done-keywords)))
11423 (setq now-done-p (and (member org-state org-done-keywords)
11424 (not (member this org-done-keywords))))
11425 (and logging (org-local-logging logging))
11426 (when (and (or org-todo-log-states org-log-done)
11427 (not (eq org-inhibit-logging t))
11428 (not (memq arg '(nextset previousset))))
11429 ;; we need to look at recording a time and note
11430 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11431 (nth 2 (assoc this org-todo-log-states))))
11432 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11433 (setq dolog 'time))
11434 (when (and org-state
11435 (member org-state org-not-done-keywords)
11436 (not (member this org-not-done-keywords)))
11437 ;; This is now a todo state and was not one before
11438 ;; If there was a CLOSED time stamp, get rid of it.
11439 (org-add-planning-info nil nil 'closed))
11440 (when (and now-done-p org-log-done)
11441 ;; It is now done, and it was not done before
11442 (org-add-planning-info 'closed (org-current-effective-time))
11443 (if (and (not dolog) (eq 'note org-log-done))
11444 (org-add-log-setup 'done org-state this 'findpos 'note)))
11445 (when (and org-state dolog)
11446 ;; This is a non-nil state, and we need to log it
11447 (org-add-log-setup 'state org-state this 'findpos dolog)))
11448 ;; Fixup tag positioning
11449 (org-todo-trigger-tag-changes org-state)
11450 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11451 (when org-provide-todo-statistics
11452 (org-update-parent-todo-statistics))
11453 (run-hooks 'org-after-todo-state-change-hook)
11454 (if (and arg (not (member org-state org-done-keywords)))
11455 (setq head (org-get-todo-sequence-head org-state)))
11456 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11457 ;; Do we need to trigger a repeat?
11459 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11460 ;; This is for the agenda, take a snapshot of the headline.
11462 (setq org-agenda-headline-snapshot-before-repeat
11463 (org-get-heading))))
11464 (org-auto-repeat-maybe org-state))
11465 ;; Fixup cursor location if close to the keyword
11466 (if (and (outline-on-heading-p)
11468 (save-excursion (beginning-of-line 1)
11469 (looking-at org-todo-line-regexp))
11470 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11472 (goto-char (or (match-end 2) (match-end 1)))
11473 (and (looking-at " ") (just-one-space))))
11474 (when org-trigger-hook
11476 (run-hook-with-args 'org-trigger-hook change-plist)))))))))
11478 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11479 "Block turning an entry into a TODO, using the hierarchy.
11480 This checks whether the current task should be blocked from state
11481 changes. Such blocking occurs when:
11483 1. The task has children which are not all in a completed state.
11485 2. A task has a parent with the property :ORDERED:, and there
11486 are siblings prior to the current task with incomplete
11489 3. The parent of the task is blocked because it has siblings that should
11490 be done first, or is child of a block grandparent TODO entry."
11492 (if (not org-enforce-todo-dependencies)
11493 t ; if locally turned off don't block
11495 ;; If this is not a todo state change, or if this entry is already DONE,
11497 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11498 (member (plist-get change-plist :from)
11499 (cons 'done org-done-keywords))
11500 (member (plist-get change-plist :to)
11501 (cons 'todo org-not-done-keywords))
11502 (not (plist-get change-plist :to)))
11503 (throw 'dont-block t))
11504 ;; If this task has children, and any are undone, it's blocked
11506 (org-back-to-heading t)
11507 (let ((this-level (funcall outline-level)))
11508 (outline-next-heading)
11509 (let ((child-level (funcall outline-level)))
11510 (while (and (not (eobp))
11511 (> child-level this-level))
11512 ;; this todo has children, check whether they are all
11514 (if (and (not (org-entry-is-done-p))
11515 (org-entry-is-todo-p))
11516 (throw 'dont-block nil))
11517 (outline-next-heading)
11518 (setq child-level (funcall outline-level))))))
11519 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11520 ;; any previous siblings are undone, it's blocked
11522 (org-back-to-heading t)
11523 (let* ((pos (point))
11524 (parent-pos (and (org-up-heading-safe) (point))))
11525 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11526 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11528 (re-search-forward org-not-done-heading-regexp pos t))
11529 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11530 ;; Search further up the hierarchy, to see if an ancestor is blocked
11532 (goto-char parent-pos)
11533 (if (not (looking-at org-not-done-heading-regexp))
11534 (throw 'dont-block t)) ; do not block, parent is not a TODO
11536 (setq parent-pos (and (org-up-heading-safe) (point)))
11537 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11538 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11540 (re-search-forward org-not-done-heading-regexp pos t))
11541 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11543 (defcustom org-track-ordered-property-with-tag nil
11544 "Should the ORDERED property also be shown as a tag?
11545 The ORDERED property decides if an entry should require subtasks to be
11546 completed in sequence. Since a property is not very visible, setting
11547 this option means that toggling the ORDERED property with the command
11548 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11549 not relevant for the behavior, but it makes things more visible.
11551 Note that toggling the tag with tags commands will not change the property
11552 and therefore not influence behavior!
11554 This can be t, meaning the tag ORDERED should be used, It can also be a
11555 string to select a different tag for this task."
11558 (const :tag "No tracking" nil)
11559 (const :tag "Track with ORDERED tag" t)
11560 (string :tag "Use other tag")))
11562 (defun org-toggle-ordered-property ()
11563 "Toggle the ORDERED property of the current entry.
11564 For better visibility, you can track the value of this property with a tag.
11565 See variable `org-track-ordered-property-with-tag'."
11567 (let* ((t1 org-track-ordered-property-with-tag)
11568 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11570 (org-back-to-heading)
11571 (if (org-entry-get nil "ORDERED")
11573 (org-delete-property "ORDERED")
11574 (and tag (org-toggle-tag tag 'off))
11575 (message "Subtasks can be completed in arbitrary order"))
11576 (org-entry-put nil "ORDERED" "t")
11577 (and tag (org-toggle-tag tag 'on))
11578 (message "Subtasks must be completed in sequence")))))
11580 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11581 (defun org-block-todo-from-checkboxes (change-plist)
11582 "Block turning an entry into a TODO, using checkboxes.
11583 This checks whether the current task should be blocked from state
11584 changes because there are unchecked boxes in this entry."
11585 (if (not org-enforce-todo-checkbox-dependencies)
11586 t ; if locally turned off don't block
11588 ;; If this is not a todo state change, or if this entry is already DONE,
11590 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11591 (member (plist-get change-plist :from)
11592 (cons 'done org-done-keywords))
11593 (member (plist-get change-plist :to)
11594 (cons 'todo org-not-done-keywords))
11595 (not (plist-get change-plist :to)))
11596 (throw 'dont-block t))
11597 ;; If this task has checkboxes that are not checked, it's blocked
11599 (org-back-to-heading t)
11600 (let ((beg (point)) end)
11601 (outline-next-heading)
11604 (if (org-list-search-forward
11605 (concat (org-item-beginning-re)
11606 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11610 (if (boundp 'org-blocked-by-checkboxes)
11611 (setq org-blocked-by-checkboxes t))
11612 (throw 'dont-block nil)))))
11613 t))) ; do not block
11615 (defun org-entry-blocked-p ()
11616 "Is the current entry blocked?"
11617 (if (org-entry-get nil "NOBLOCKING")
11618 nil ;; Never block this entry
11620 (run-hook-with-args-until-failure
11622 (list :type 'todo-state-change
11627 (defun org-update-statistics-cookies (all)
11628 "Update the statistics cookie, either from TODO or from checkboxes.
11629 This should be called with the cursor in a line with a statistics cookie."
11633 (org-update-checkbox-count 'all)
11634 (org-map-entries 'org-update-parent-todo-statistics))
11635 (if (not (org-at-heading-p))
11636 (org-update-checkbox-count)
11637 (let ((pos (move-marker (make-marker) (point)))
11639 (ignore-errors (org-back-to-heading t))
11640 (if (not (org-at-heading-p))
11641 (org-update-checkbox-count)
11642 (setq l1 (org-outline-level))
11643 (setq end (save-excursion
11644 (outline-next-heading)
11645 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11647 (if (and (save-excursion
11649 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11650 (not (save-excursion (re-search-forward
11651 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11652 (org-update-checkbox-count)
11653 (if (and l2 (> l2 l1))
11656 (org-update-parent-todo-statistics))
11658 (beginning-of-line 1)
11659 (while (re-search-forward
11660 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11662 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11664 (move-marker pos nil)))))
11666 (defvar org-entry-property-inherited-from) ;; defined below
11667 (defun org-update-parent-todo-statistics ()
11668 "Update any statistics cookie in the parent of the current headline.
11669 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11670 the entire subtree and this will travel up the hierarchy and update
11671 statistics everywhere."
11672 (let* ((prop (save-excursion (org-up-heading-safe)
11673 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11674 (recursive (or (not org-hierarchical-todo-statistics)
11675 (and prop (string-match "\\<recursive\\>" prop))))
11676 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11679 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11680 level ltoggle l1 new ndel
11681 (cnt-all 0) (cnt-done 0) is-percent kwd
11682 checkbox-beg ov ovs ove cookie-present)
11685 (beginning-of-line 1)
11686 (setq ltoggle (funcall outline-level))
11687 ;; Three situations are to consider:
11689 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11690 ;; to the top-level ancestor on the headline;
11692 ;; 2. If parent has "recursive" property, repeat up to the
11693 ;; headline setting that property, taking inheritance into
11696 ;; 3. Else, move up to direct parent and proceed only once.
11697 (while (and (setq level (org-up-heading-safe))
11698 (or recursive first)
11700 (setq first nil cookie-present nil)
11704 (downcase (or (org-entry-get nil "COOKIE_DATA")
11707 (while (re-search-forward box-re (point-at-eol) t)
11708 (setq cnt-all 0 cnt-done 0 cookie-present t)
11709 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11711 (unless (outline-next-heading) (throw 'exit nil))
11712 (while (and (looking-at org-complex-heading-regexp)
11713 (> (setq l1 (length (match-string 1))) level))
11714 (setq kwd (and (or recursive (= l1 ltoggle))
11716 (if (or (eq org-provide-todo-statistics 'all-headlines)
11717 (and (listp org-provide-todo-statistics)
11718 (or (member kwd org-provide-todo-statistics)
11719 (member kwd org-done-keywords))))
11720 (setq cnt-all (1+ cnt-all))
11721 (if (eq org-provide-todo-statistics t)
11722 (and kwd (setq cnt-all (1+ cnt-all)))))
11723 (and (member kwd org-done-keywords)
11724 (setq cnt-done (1+ cnt-done)))
11725 (outline-next-heading)))
11728 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11729 (format "[%d/%d]" cnt-done cnt-all))
11730 ndel (- (match-end 0) checkbox-beg))
11731 ;; handle overlays when updating cookie from column view
11732 (when (setq ov (car (overlays-at checkbox-beg)))
11733 (setq ovs (overlay-start ov) ove (overlay-end ov))
11734 (delete-overlay ov))
11735 (goto-char checkbox-beg)
11737 (delete-region (point) (+ (point) ndel))
11738 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11739 (when ov (move-overlay ov ovs ove)))
11740 (when cookie-present
11741 (run-hook-with-args 'org-after-todo-statistics-hook
11742 cnt-done (- cnt-all cnt-done))))))
11743 (run-hooks 'org-todo-statistics-hook)))
11745 (defvar org-after-todo-statistics-hook nil
11746 "Hook that is called after a TODO statistics cookie has been updated.
11747 Each function is called with two arguments: the number of not-done entries
11748 and the number of done entries.
11750 For example, the following function, when added to this hook, will switch
11751 an entry to DONE when all children are done, and back to TODO when new
11752 entries are set to a TODO status. Note that this hook is only called
11753 when there is a statistics cookie in the headline!
11755 (defun org-summary-todo (n-done n-not-done)
11756 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11757 (let (org-log-done org-log-states) ; turn off logging
11758 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11761 (defvar org-todo-statistics-hook nil
11762 "Hook that is run whenever Org thinks TODO statistics should be updated.
11763 This hook runs even if there is no statistics cookie present, in which case
11764 `org-after-todo-statistics-hook' would not run.")
11766 (defun org-todo-trigger-tag-changes (state)
11767 "Apply the changes defined in `org-todo-state-tags-triggers'."
11768 (let ((l org-todo-state-tags-triggers)
11770 (when (or (not state) (equal state ""))
11771 (setq changes (append changes (cdr (assoc "" l)))))
11772 (when (and (stringp state) (> (length state) 0))
11773 (setq changes (append changes (cdr (assoc state l)))))
11774 (when (member state org-not-done-keywords)
11775 (setq changes (append changes (cdr (assoc 'todo l)))))
11776 (when (member state org-done-keywords)
11777 (setq changes (append changes (cdr (assoc 'done l)))))
11778 (dolist (c changes)
11779 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11781 (defun org-local-logging (value)
11782 "Get logging settings from a property VALUE."
11784 ;; directly set the variables, they are already local.
11785 (setq org-log-done nil
11787 org-todo-log-states nil)
11788 (setq words (org-split-string value))
11789 (while (setq w (pop words))
11791 ((setq a (assoc w org-startup-options))
11792 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11793 (set (nth 1 a) (nth 2 a))))
11794 ((setq a (org-extract-log-state-settings w))
11795 (and (member (car a) org-todo-keywords-1)
11796 (push a org-todo-log-states)))))))
11798 (defun org-get-todo-sequence-head (kwd)
11799 "Return the head of the TODO sequence to which KWD belongs.
11800 If KWD is not set, check if there is a text property remembering the
11805 (or (get-text-property (point-at-bol) 'org-todo-head)
11807 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11808 nil (point-at-eol)))
11809 (get-text-property p 'org-todo-head))))
11810 ((not (member kwd org-todo-keywords-1))
11811 (car org-todo-keywords-1))
11812 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
11814 (defun org-fast-todo-selection ()
11815 "Fast TODO keyword selection with single keys.
11816 Returns the new TODO keyword, or nil if no state change should occur."
11817 (let* ((fulltable org-todo-key-alist)
11818 (done-keywords org-done-keywords) ;; needed for the faces.
11819 (maxlen (apply 'max (mapcar
11821 (if (stringp (car x)) (string-width (car x)) 0))
11824 (fwidth (+ maxlen 3 1 3))
11825 (ncol (/ (- (window-width) 4) fwidth))
11829 (save-window-excursion
11831 (set-buffer (get-buffer-create " *Org todo*"))
11832 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
11834 (org-set-local 'org-done-keywords done-keywords)
11835 (setq tbl fulltable cnt 0)
11836 (while (setq e (pop tbl))
11838 ((equal e '(:startgroup))
11839 (push '() groups) (setq ingroup t)
11840 (when (not (= cnt 0))
11844 ((equal e '(:endgroup))
11845 (setq ingroup nil cnt 0)
11847 ((equal e '(:newline))
11848 (when (not (= cnt 0))
11852 (while (equal (car tbl) '(:newline))
11854 (setq tbl (cdr tbl)))))
11856 (setq tg (car e) c (cdr e))
11857 (if ingroup (push tg (car groups)))
11858 (setq tg (org-add-props tg nil 'face
11859 (org-get-todo-face tg)))
11860 (if (and (= cnt 0) (not ingroup)) (insert " "))
11861 (insert "[" c "] " tg (make-string
11862 (- fwidth 4 (length tg)) ?\ ))
11863 (when (= (setq cnt (1+ cnt)) ncol)
11865 (if ingroup (insert " "))
11868 (goto-char (point-min))
11869 (if (not expert) (org-fit-window-to-buffer))
11870 (message "[a-z..]:Set [SPC]:clear")
11871 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11874 (and (= c ?q) (not (rassoc c fulltable))))
11875 (setq quit-flag t))
11877 ((setq e (rassoc c fulltable) tg (car e))
11879 (t (setq quit-flag t)))))))
11881 (defun org-entry-is-todo-p ()
11882 (member (org-get-todo-state) org-not-done-keywords))
11884 (defun org-entry-is-done-p ()
11885 (member (org-get-todo-state) org-done-keywords))
11887 (defun org-get-todo-state ()
11889 (org-back-to-heading t)
11890 (and (looking-at org-todo-line-regexp)
11892 (match-string 2))))
11894 (defun org-at-date-range-p (&optional inactive-ok)
11895 "Is the cursor inside a date range?"
11899 (let ((pos (point)))
11900 (skip-chars-backward "^[<\r\n")
11901 (skip-chars-backward "<[")
11902 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11903 (>= (match-end 0) pos)
11905 (skip-chars-backward "^<[\r\n")
11906 (skip-chars-backward "<[")
11907 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11908 (>= (match-end 0) pos)
11912 (defun org-get-repeat (&optional tagline)
11913 "Check if there is a deadline/schedule with repeater in this entry."
11916 (org-back-to-heading t)
11917 (and (re-search-forward (if tagline
11918 (concat tagline "\\s-*" org-repeat-re)
11920 (org-entry-end-position) t)
11921 (match-string-no-properties 1)))))
11923 (defvar org-last-changed-timestamp)
11924 (defvar org-last-inserted-timestamp)
11925 (defvar org-log-post-message)
11926 (defvar org-log-note-purpose)
11927 (defvar org-log-note-how)
11928 (defvar org-log-note-extra)
11929 (defun org-auto-repeat-maybe (done-word)
11930 "Check if the current headline contains a repeated deadline/schedule.
11931 If yes, set TODO state back to what it was and change the base date
11932 of repeating deadline/scheduled time stamps to new date.
11933 This function is run automatically after each state change to a DONE state."
11934 ;; last-state is dynamically scoped into this function
11935 (let* ((repeat (org-get-repeat))
11936 (aa (assoc org-last-state org-todo-kwd-alist))
11937 (interpret (nth 1 aa))
11939 (whata '(("d" . day) ("m" . month) ("y" . year)))
11940 (msg "Entry repeats: ")
11942 (org-todo-log-states nil)
11943 re type n what ts time to-state)
11945 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
11946 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
11947 org-todo-repeat-to-state))
11948 (unless (and to-state (member to-state org-todo-keywords-1))
11949 (setq to-state (if (eq interpret 'type) org-last-state head)))
11950 (org-todo to-state)
11951 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
11952 (org-entry-put nil "LAST_REPEAT" (format-time-string
11953 (org-time-stamp-format t t))))
11954 (when org-log-repeat
11955 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
11956 (memq 'org-add-log-note post-command-hook))
11957 ;; OK, we are already setup for some record
11958 (if (eq org-log-repeat 'note)
11959 ;; make sure we take a note, not only a time stamp
11960 (setq org-log-note-how 'note))
11961 ;; Set up for taking a record
11962 (org-add-log-setup 'state (or done-word (car org-done-keywords))
11964 'findpos org-log-repeat)))
11965 (org-back-to-heading t)
11966 (org-add-planning-info nil nil 'closed)
11967 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
11968 org-deadline-time-regexp "\\)\\|\\("
11969 org-ts-regexp "\\)"))
11970 (while (re-search-forward
11971 re (save-excursion (outline-next-heading) (point)) t)
11972 (setq type (if (match-end 1) org-scheduled-string
11973 (if (match-end 3) org-deadline-string "Plain:"))
11974 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
11975 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
11976 (setq n (string-to-number (match-string 2 ts))
11977 what (match-string 3 ts))
11978 (if (equal what "w") (setq n (* n 7) what "d"))
11979 ;; Preparation, see if we need to modify the start date for the change
11980 (when (match-end 1)
11981 (setq time (save-match-data (org-time-string-to-time ts)))
11983 ((equal (match-string 1 ts) ".")
11984 ;; Shift starting date to today
11985 (org-timestamp-change
11986 (- (org-today) (time-to-days time))
11988 ((equal (match-string 1 ts) "+")
11989 (let ((nshiftmax 10) (nshift 0))
11990 (while (or (= nshift 0)
11991 (<= (time-to-days time)
11992 (time-to-days (current-time))))
11993 (when (= (incf nshift) nshiftmax)
11994 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
11996 (org-timestamp-change n (cdr (assoc what whata)))
11997 (org-at-timestamp-p t)
11998 (setq ts (match-string 1))
11999 (setq time (save-match-data (org-time-string-to-time ts)))))
12000 (org-timestamp-change (- n) (cdr (assoc what whata)))
12001 ;; rematch, so that we have everything in place for the real shift
12002 (org-at-timestamp-p t)
12003 (setq ts (match-string 1))
12004 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
12005 (org-timestamp-change n (cdr (assoc what whata)))
12006 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12007 (setq org-log-post-message msg)
12008 (message "%s" msg))))
12010 (defun org-show-todo-tree (arg)
12011 "Make a compact tree which shows all headlines marked with TODO.
12012 The tree will show the lines where the regexp matches, and all higher
12013 headlines above the match.
12014 With a \\[universal-argument] prefix, prompt for a regexp to match.
12015 With a numeric prefix N, construct a sparse tree for the Nth element
12016 of `org-todo-keywords-1'."
12018 (let ((case-fold-search nil)
12020 (cond ((null arg) org-not-done-regexp)
12022 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12023 (mapcar 'list org-todo-keywords-1))))
12025 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12027 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12028 (regexp-quote (nth (1- (prefix-numeric-value arg))
12029 org-todo-keywords-1)))
12030 (t (error "Invalid prefix argument: %s" arg)))))
12031 (message "%d TODO entries found"
12032 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12034 (defun org-deadline (&optional remove time)
12035 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12036 With argument REMOVE, remove any deadline from the item.
12037 With argument TIME, set the deadline at the corresponding date. TIME
12038 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12040 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12041 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12042 'region-start-level 'region))
12043 org-loop-over-headlines-in-active-region)
12045 `(org-deadline ',remove ,time)
12046 org-loop-over-headlines-in-active-region
12047 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12048 (let* ((old-date (org-entry-get nil "DEADLINE"))
12049 (repeater (and old-date
12051 "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
12053 (match-string 1 old-date))))
12056 (when (and old-date org-log-redeadline)
12057 (org-add-log-setup 'deldeadline nil old-date 'findpos
12058 org-log-redeadline))
12059 (org-remove-timestamp-with-keyword org-deadline-string)
12060 (message "Item no longer has a deadline."))
12061 (org-add-planning-info 'deadline time 'closed)
12062 (when (and old-date org-log-redeadline
12063 (not (equal old-date
12064 (substring org-last-inserted-timestamp 1 -1))))
12065 (org-add-log-setup 'redeadline nil old-date 'findpos
12066 org-log-redeadline))
12069 (org-back-to-heading t)
12070 (when (re-search-forward (concat org-deadline-string " "
12071 org-last-inserted-timestamp)
12073 (outline-next-heading) (point)) t)
12074 (goto-char (1- (match-end 0)))
12075 (insert " " repeater)
12076 (setq org-last-inserted-timestamp
12077 (concat (substring org-last-inserted-timestamp 0 -1)
12079 (substring org-last-inserted-timestamp -1))))))
12080 (message "Deadline on %s" org-last-inserted-timestamp)))))
12082 (defun org-schedule (&optional remove time)
12083 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12084 With argument REMOVE, remove any scheduling date from the item.
12085 With argument TIME, scheduled at the corresponding date. TIME can
12086 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12088 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12089 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12090 'region-start-level 'region))
12091 org-loop-over-headlines-in-active-region)
12093 `(org-schedule ',remove ,time)
12094 org-loop-over-headlines-in-active-region
12095 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12096 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12097 (repeater (and old-date
12099 "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
12101 (match-string 1 old-date))))
12104 (when (and old-date org-log-reschedule)
12105 (org-add-log-setup 'delschedule nil old-date 'findpos
12106 org-log-reschedule))
12107 (org-remove-timestamp-with-keyword org-scheduled-string)
12108 (message "Item is no longer scheduled."))
12109 (org-add-planning-info 'scheduled time 'closed)
12110 (when (and old-date org-log-reschedule
12111 (not (equal old-date
12112 (substring org-last-inserted-timestamp 1 -1))))
12113 (org-add-log-setup 'reschedule nil old-date 'findpos
12114 org-log-reschedule))
12117 (org-back-to-heading t)
12118 (when (re-search-forward (concat org-scheduled-string " "
12119 org-last-inserted-timestamp)
12121 (outline-next-heading) (point)) t)
12122 (goto-char (1- (match-end 0)))
12123 (insert " " repeater)
12124 (setq org-last-inserted-timestamp
12125 (concat (substring org-last-inserted-timestamp 0 -1)
12127 (substring org-last-inserted-timestamp -1))))))
12128 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12130 (defun org-get-scheduled-time (pom &optional inherit)
12131 "Get the scheduled time as a time tuple, of a format suitable
12132 for calling org-schedule with, or if there is no scheduling,
12134 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12136 (apply 'encode-time (org-parse-time-string time)))))
12138 (defun org-get-deadline-time (pom &optional inherit)
12139 "Get the deadline as a time tuple, of a format suitable for
12140 calling org-deadline with, or if there is no scheduling, returns
12142 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12144 (apply 'encode-time (org-parse-time-string time)))))
12146 (defun org-remove-timestamp-with-keyword (keyword)
12147 "Remove all time stamps with KEYWORD in the current entry."
12148 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12151 (org-back-to-heading t)
12153 (outline-next-heading)
12154 (while (re-search-backward re beg t)
12156 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12157 (equal (char-before) ?\ ))
12158 (backward-delete-char 1)
12159 (if (string-match "^[ \t]*$" (buffer-substring
12160 (point-at-bol) (point-at-eol)))
12161 (delete-region (point-at-bol)
12162 (min (point-max) (1+ (point-at-eol))))))))))
12164 (defun org-add-planning-info (what &optional time &rest remove)
12165 "Insert new timestamp with keyword in the line directly after the headline.
12166 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12167 If non is given, the user is prompted for a date.
12168 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12171 (let (org-time-was-given org-end-time-was-given ts
12172 end default-time default-input)
12175 (when (and (memq what '(scheduled deadline))
12177 (and (stringp time)
12178 (string-match "^[-+]+[0-9]" time))))
12179 ;; Try to get a default date/time from existing timestamp
12181 (org-back-to-heading t)
12182 (setq end (save-excursion (outline-next-heading) (point)))
12183 (when (re-search-forward (if (eq what 'scheduled)
12184 org-scheduled-time-regexp
12185 org-deadline-time-regexp)
12187 (setq ts (match-string 1)
12189 (apply 'encode-time (org-parse-time-string ts))
12190 default-input (and ts (org-get-compact-tod ts))))))
12194 ;; This is a string (relative or absolute), set proper date
12195 (apply 'encode-time
12196 (org-read-date-analyze
12197 time default-time (decode-time default-time)))
12198 ;; If necessary, get the time from the user
12199 (or time (org-read-date nil 'to-time nil nil
12200 default-time default-input)))))
12202 (when (and org-insert-labeled-timestamps-at-point
12203 (member what '(scheduled deadline)))
12205 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12206 (org-insert-time-stamp time org-time-was-given
12207 nil nil nil (list org-end-time-was-given))
12211 (let (col list elt ts buffer-invisibility-spec)
12212 (org-back-to-heading t)
12213 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12214 (goto-char (match-end 1))
12215 (setq col (current-column))
12216 (goto-char (match-end 0))
12217 (if (eobp) (insert "\n") (forward-char 1))
12218 (when (and (not what)
12221 org-keyword-time-not-clock-regexp))))
12222 ;; Nothing to add, nothing to remove...... :-)
12224 (if (and (not (looking-at org-outline-regexp))
12225 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12227 (not (equal (match-string 1) org-clock-string)))
12228 (narrow-to-region (match-beginning 0) (match-end 0))
12229 (insert-before-markers "\n")
12231 (narrow-to-region (point) (point))
12232 (and org-adapt-indentation (org-indent-to-column col)))
12233 ;; Check if we have to remove something.
12234 (setq list (cons what remove))
12236 (setq elt (pop list))
12237 (when (or (and (eq elt 'scheduled)
12238 (re-search-forward org-scheduled-time-regexp nil t))
12239 (and (eq elt 'deadline)
12240 (re-search-forward org-deadline-time-regexp nil t))
12241 (and (eq elt 'closed)
12242 (re-search-forward org-closed-time-regexp nil t)))
12244 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12245 (and (looking-at "[ \t]+") (replace-match ""))
12246 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12249 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12250 (cond ((eq what 'scheduled) org-scheduled-string)
12251 ((eq what 'deadline) org-deadline-string)
12252 ((eq what 'closed) org-closed-string))
12254 (setq ts (org-insert-time-stamp
12256 (or org-time-was-given
12257 (and (eq what 'closed) org-log-done-with-time))
12259 nil nil (list org-end-time-was-given)))
12261 (if (not (or (bolp) (eq (char-before) ?\ )
12262 (memq (char-after) '(32 10))
12265 (goto-char (point-min))
12267 (if (and (looking-at "[ \t]*\n")
12268 (equal (char-before) ?\n))
12269 (delete-region (1- (point)) (point-at-eol)))
12272 (defvar org-log-note-marker (make-marker))
12273 (defvar org-log-note-purpose nil)
12274 (defvar org-log-note-state nil)
12275 (defvar org-log-note-previous-state nil)
12276 (defvar org-log-note-how nil)
12277 (defvar org-log-note-extra nil)
12278 (defvar org-log-note-window-configuration nil)
12279 (defvar org-log-note-return-to (make-marker))
12280 (defvar org-log-note-effective-time nil
12281 "Remembered current time so that dynamically scoped
12282 `org-extend-today-until' affects tha timestamps in state change
12285 (defvar org-log-post-message nil
12286 "Message to be displayed after a log note has been stored.
12287 The auto-repeater uses this.")
12289 (defun org-add-note ()
12290 "Add a note to the current entry.
12291 This is done in the same way as adding a state change note."
12293 (org-add-log-setup 'note nil nil 'findpos nil))
12295 (defvar org-property-end-re)
12296 (defun org-add-log-setup (&optional purpose state prev-state
12298 "Set up the post command hook to take a note.
12299 If this is about to TODO state change, the new state is expected in STATE.
12300 When FINDPOS is non-nil, find the correct position for the note in
12301 the current entry. If not, assume that it can be inserted at point.
12302 HOW is an indicator what kind of note should be created.
12303 EXTRA is additional text that will be inserted into the notes buffer."
12304 (let* ((org-log-into-drawer (org-log-into-drawer))
12305 (drawer (cond ((stringp org-log-into-drawer)
12306 org-log-into-drawer)
12307 (org-log-into-drawer "LOGBOOK")
12312 (org-back-to-heading t)
12313 (narrow-to-region (point) (save-excursion
12314 (outline-next-heading) (point)))
12315 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12316 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12318 (goto-char (match-end 0))
12321 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12324 (goto-char (match-end 0))
12325 (or org-log-states-order-reversed
12326 (and (re-search-forward org-property-end-re nil t)
12327 (goto-char (1- (match-beginning 0))))))
12328 (insert "\n:" drawer ":\n:END:")
12329 (beginning-of-line 0)
12330 (org-indent-line-function)
12331 (beginning-of-line 2)
12332 (org-indent-line-function)
12334 ((and org-log-state-notes-insert-after-drawers
12336 (forward-line) (looking-at org-drawer-regexp)))
12338 (while (looking-at org-drawer-regexp)
12339 (goto-char (match-end 0))
12340 (re-search-forward org-property-end-re (point-max) t)
12342 (forward-line -1)))
12343 (unless org-log-states-order-reversed
12344 (and (= (char-after) ?\n) (forward-char 1))
12345 (org-skip-over-state-notes)
12346 (skip-chars-backward " \t\n\r")))
12347 (move-marker org-log-note-marker (point))
12348 (setq org-log-note-purpose purpose
12349 org-log-note-state state
12350 org-log-note-previous-state prev-state
12351 org-log-note-how how
12352 org-log-note-extra extra
12353 org-log-note-effective-time (org-current-effective-time))
12354 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12356 (defun org-skip-over-state-notes ()
12357 "Skip past the list of State notes in an entry."
12358 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12359 (when (ignore-errors (goto-char (org-in-item-p)))
12360 (let* ((struct (org-list-struct))
12361 (prevs (org-list-prevs-alist struct)))
12362 (while (looking-at "[ \t]*- State")
12363 (goto-char (or (org-list-get-next-item (point) struct prevs)
12364 (org-list-get-item-end (point) struct)))))))
12366 (defun org-add-log-note (&optional purpose)
12367 "Pop up a window for taking a note, and add this note later at point."
12368 (remove-hook 'post-command-hook 'org-add-log-note)
12369 (setq org-log-note-window-configuration (current-window-configuration))
12370 (delete-other-windows)
12371 (move-marker org-log-note-return-to (point))
12372 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12373 (goto-char org-log-note-marker)
12374 (org-switch-to-buffer-other-window "*Org Note*")
12376 (if (memq org-log-note-how '(time state))
12377 (let (current-prefix-arg) (org-store-log-note))
12378 (let ((org-inhibit-startup t)) (org-mode))
12379 (insert (format "# Insert note for %s.
12380 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12382 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12383 ((eq org-log-note-purpose 'done) "closed todo item")
12384 ((eq org-log-note-purpose 'state)
12385 (format "state change from \"%s\" to \"%s\""
12386 (or org-log-note-previous-state "")
12387 (or org-log-note-state "")))
12388 ((eq org-log-note-purpose 'reschedule)
12390 ((eq org-log-note-purpose 'delschedule)
12391 "no longer scheduled")
12392 ((eq org-log-note-purpose 'redeadline)
12393 "changing deadline")
12394 ((eq org-log-note-purpose 'deldeadline)
12395 "removing deadline")
12396 ((eq org-log-note-purpose 'refile)
12398 ((eq org-log-note-purpose 'note)
12400 (t (error "This should not happen")))))
12401 (if org-log-note-extra (insert org-log-note-extra))
12402 (org-set-local 'org-finish-function 'org-store-log-note)
12403 (run-hooks 'org-log-buffer-setup-hook)))
12405 (defvar org-note-abort nil) ; dynamically scoped
12406 (defun org-store-log-note ()
12407 "Finish taking a log note, and insert it to where it belongs."
12408 (let ((txt (buffer-string))
12409 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12411 (kill-buffer (current-buffer))
12412 (while (string-match "\\`#.*\n[ \t\n]*" txt)
12413 (setq txt (replace-match "" t t txt)))
12414 (if (string-match "\\s-+\\'" txt)
12415 (setq txt (replace-match "" t t txt)))
12416 (setq lines (org-split-string txt "\n"))
12417 (when (and note (string-match "\\S-" note))
12419 (org-replace-escapes
12421 (list (cons "%u" (user-login-name))
12422 (cons "%U" user-full-name)
12423 (cons "%t" (format-time-string
12424 (org-time-stamp-format 'long 'inactive)
12425 org-log-note-effective-time))
12426 (cons "%T" (format-time-string
12427 (org-time-stamp-format 'long nil)
12428 org-log-note-effective-time))
12429 (cons "%d" (format-time-string
12430 (org-time-stamp-format nil 'inactive)
12431 org-log-note-effective-time))
12432 (cons "%D" (format-time-string
12433 (org-time-stamp-format nil nil)
12434 org-log-note-effective-time))
12435 (cons "%s" (if org-log-note-state
12436 (concat "\"" org-log-note-state "\"")
12438 (cons "%S" (if org-log-note-previous-state
12439 (concat "\"" org-log-note-previous-state "\"")
12441 (if lines (setq note (concat note " \\\\")))
12443 (when (or current-prefix-arg org-note-abort)
12444 (when org-log-into-drawer
12445 (org-remove-empty-drawer-at
12446 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12447 org-log-note-marker))
12450 (with-current-buffer (marker-buffer org-log-note-marker)
12452 (goto-char org-log-note-marker)
12453 (move-marker org-log-note-marker nil)
12455 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12456 (setq ind (save-excursion
12457 (if (ignore-errors (goto-char (org-in-item-p)))
12458 (let ((struct (org-list-struct)))
12460 (org-list-get-top-point struct) struct))
12461 (skip-chars-backward " \r\t\n")
12463 ((and (org-at-heading-p)
12464 org-adapt-indentation)
12465 (1+ (org-current-level)))
12466 ((org-at-heading-p) 0)
12467 (t (org-get-indentation))))))
12468 (setq bul (org-list-bullet-string "-"))
12469 (org-indent-line-to ind)
12470 (insert bul (pop lines))
12471 (let ((ind-body (+ (length bul) ind)))
12474 (org-indent-line-to ind-body)
12475 (insert (pop lines))))
12476 (message "Note stored")
12477 (org-back-to-heading t)
12478 (org-cycle-hide-drawers 'children)))))
12479 (set-window-configuration org-log-note-window-configuration)
12480 (with-current-buffer (marker-buffer org-log-note-return-to)
12481 (goto-char org-log-note-return-to))
12482 (move-marker org-log-note-return-to nil)
12483 (and org-log-post-message (message "%s" org-log-post-message)))
12485 (defun org-remove-empty-drawer-at (drawer pos)
12486 "Remove an empty drawer DRAWER at position POS.
12487 POS may also be a marker."
12488 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12494 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12495 (replace-match ""))))))
12497 (defun org-sparse-tree (&optional arg)
12498 "Create a sparse tree, prompt for the details.
12499 This command can create sparse trees. You first need to select the type
12500 of match used to create the tree:
12502 t Show all TODO entries.
12503 T Show entries with a specific TODO keyword.
12504 m Show entries selected by a tags/property match.
12505 p Enter a property name and its value (both with completion on existing
12506 names/values) and show entries with that property.
12507 r Show entries matching a regular expression (`/' can be used as well)
12508 d Show deadlines due within `org-deadline-warning-days'.
12509 b Show deadlines and scheduled items before a date.
12510 a Show deadlines and scheduled items after a date."
12512 (let (ans kwd value)
12513 (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")
12514 (setq ans (read-char-exclusive))
12517 (call-interactively 'org-check-deadlines))
12519 (call-interactively 'org-check-before-date))
12521 (call-interactively 'org-check-after-date))
12523 (call-interactively 'org-check-dates-range))
12525 (org-show-todo-tree nil))
12527 (org-show-todo-tree '(4)))
12528 ((member ans '(?T ?m))
12529 (call-interactively 'org-match-sparse-tree))
12530 ((member ans '(?p ?P))
12531 (setq kwd (org-icompleting-read "Property: "
12532 (mapcar 'list (org-buffer-property-keys))))
12533 (setq value (org-icompleting-read "Value: "
12534 (mapcar 'list (org-property-values kwd))))
12535 (unless (string-match "\\`{.*}\\'" value)
12536 (setq value (concat "\"" value "\"")))
12537 (org-match-sparse-tree arg (concat kwd "=" value)))
12538 ((member ans '(?r ?R ?/))
12539 (call-interactively 'org-occur))
12540 (t (error "No such sparse tree command \"%c\"" ans)))))
12542 (defvar org-occur-highlights nil
12543 "List of overlays used for occur matches.")
12544 (make-variable-buffer-local 'org-occur-highlights)
12545 (defvar org-occur-parameters nil
12546 "Parameters of the active org-occur calls.
12547 This is a list, each call to org-occur pushes as cons cell,
12548 containing the regular expression and the callback, onto the list.
12549 The list can contain several entries if `org-occur' has been called
12550 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12551 will only contain one set of parameters. When the highlights are
12552 removed (for example with `C-c C-c', or with the next edit (depending
12553 on `org-remove-highlights-with-change'), this variable is emptied
12555 (make-variable-buffer-local 'org-occur-parameters)
12557 (defun org-occur (regexp &optional keep-previous callback)
12558 "Make a compact tree which shows all matches of REGEXP.
12559 The tree will show the lines where the regexp matches, and all higher
12560 headlines above the match. It will also show the heading after the match,
12561 to make sure editing the matching entry is easy.
12562 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12563 call to `org-occur' will be kept, to allow stacking of calls to this
12565 If CALLBACK is non-nil, it is a function which is called to confirm
12566 that the match should indeed be shown."
12567 (interactive "sRegexp: \nP")
12568 (when (equal regexp "")
12569 (error "Regexp cannot be empty"))
12570 (unless keep-previous
12571 (org-remove-occur-highlights nil nil t))
12572 (push (cons regexp callback) org-occur-parameters)
12575 (goto-char (point-min))
12576 (if (or (not keep-previous) ; do not want to keep
12577 (not org-occur-highlights)) ; no previous matches
12580 (while (re-search-forward regexp nil t)
12581 (when (or (not callback)
12582 (save-match-data (funcall callback)))
12583 (setq cnt (1+ cnt))
12584 (when org-highlight-sparse-tree-matches
12585 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12586 (org-show-context 'occur-tree))))
12587 (when org-remove-highlights-with-change
12588 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12590 (unless org-sparse-tree-open-archived-trees
12591 (org-hide-archived-subtrees (point-min) (point-max)))
12592 (run-hooks 'org-occur-hook)
12593 (if (org-called-interactively-p 'interactive)
12594 (message "%d match(es) for regexp %s" cnt regexp))
12597 (defun org-occur-next-match (&optional n reset)
12598 "Function for `next-error-function' to find sparse tree matches.
12599 N is the number of matches to move, when negative move backwards.
12600 RESET is entirely ignored - this function always goes back to the
12601 starting point when no match is found."
12602 (let* ((limit (if (< n 0) (point-min) (point-max)))
12603 (search-func (if (< n 0)
12604 'previous-single-char-property-change
12605 'next-single-char-property-change))
12610 (while (setq p1 (funcall search-func (point) 'org-type))
12611 (when (equal p1 limit)
12613 (error "No more matches"))
12614 (when (equal (get-char-property p1 'org-type) 'org-occur)
12618 (throw 'exit (point))))
12621 (error "No more matches"))))
12623 (defun org-show-context (&optional key)
12624 "Make sure point and context are visible.
12625 How much context is shown depends upon the variables
12626 `org-show-hierarchy-above', `org-show-following-heading',
12627 `org-show-entry-below' and `org-show-siblings'."
12628 (let ((heading-p (org-at-heading-p t))
12629 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12630 (following-p (org-get-alist-option org-show-following-heading key))
12631 (entry-p (org-get-alist-option org-show-entry-below key))
12632 (siblings-p (org-get-alist-option org-show-siblings key)))
12634 ;; Show heading or entry text
12635 (if (and heading-p (not entry-p))
12636 (org-flag-heading nil) ; only show the heading
12637 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12638 (org-show-hidden-entry))) ; show entire entry
12640 ;; Show next sibling, or heading below text
12642 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12643 (org-flag-heading nil))))
12644 (when siblings-p (org-show-siblings))
12646 ;; show all higher headings, possibly with siblings
12648 (while (and (condition-case nil
12649 (progn (org-up-heading-all 1) t)
12652 (org-flag-heading nil)
12653 (when siblings-p (org-show-siblings))))))))
12655 (defvar org-reveal-start-hook nil
12656 "Hook run before revealing a location.")
12658 (defun org-reveal (&optional siblings)
12659 "Show current entry, hierarchy above it, and the following headline.
12660 This can be used to show a consistent set of context around locations
12661 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12662 not t for the search context.
12664 With optional argument SIBLINGS, on each level of the hierarchy all
12665 siblings are shown. This repairs the tree structure to what it would
12666 look like when opened with hierarchical calls to `org-cycle'.
12667 With double optional argument \\[universal-argument] \\[universal-argument], \
12668 go to the parent and show the
12671 (run-hooks 'org-reveal-start-hook)
12672 (let ((org-show-hierarchy-above t)
12673 (org-show-following-heading t)
12674 (org-show-siblings (if siblings t org-show-siblings)))
12675 (org-show-context nil))
12676 (when (equal siblings '(16))
12678 (when (org-up-heading-safe)
12680 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12682 (defun org-highlight-new-match (beg end)
12683 "Highlight from BEG to END and mark the highlight is an occur headline."
12684 (let ((ov (make-overlay beg end)))
12685 (overlay-put ov 'face 'secondary-selection)
12686 (overlay-put ov 'org-type 'org-occur)
12687 (push ov org-occur-highlights)))
12689 (defun org-remove-occur-highlights (&optional beg end noremove)
12690 "Remove the occur highlights from the buffer.
12691 BEG and END are ignored. If NOREMOVE is nil, remove this function
12692 from the `before-change-functions' in the current buffer."
12694 (unless org-inhibit-highlight-removal
12695 (mapc 'delete-overlay org-occur-highlights)
12696 (setq org-occur-highlights nil)
12697 (setq org-occur-parameters nil)
12699 (remove-hook 'before-change-functions
12700 'org-remove-occur-highlights 'local))))
12704 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12705 "Regular expression matching the priority indicator.")
12707 (defvar org-remove-priority-next-time nil)
12709 (defun org-priority-up ()
12710 "Increase the priority of the current item."
12712 (org-priority 'up))
12714 (defun org-priority-down ()
12715 "Decrease the priority of the current item."
12717 (org-priority 'down))
12719 (defun org-priority (&optional action)
12720 "Change the priority of an item by ARG.
12721 ACTION can be `set', `up', `down', or a character."
12723 (unless org-enable-priority-commands
12724 (error "Priority commands are disabled"))
12725 (setq action (or action 'set))
12726 (let (current new news have remove)
12728 (org-back-to-heading t)
12729 (if (looking-at org-priority-regexp)
12730 (setq current (string-to-char (match-string 2))
12733 ((eq action 'remove)
12734 (setq remove t new ?\ ))
12735 ((or (eq action 'set)
12736 (if (featurep 'xemacs) (characterp action) (integerp action)))
12737 (if (not (eq action 'set))
12739 (message "Priority %c-%c, SPC to remove: "
12740 org-highest-priority org-lowest-priority)
12742 (setq new (read-char-exclusive))))
12743 (if (and (= (upcase org-highest-priority) org-highest-priority)
12744 (= (upcase org-lowest-priority) org-lowest-priority))
12745 (setq new (upcase new)))
12746 (cond ((equal new ?\ ) (setq remove t))
12747 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12748 (error "Priority must be between `%c' and `%c'"
12749 org-highest-priority org-lowest-priority))))
12752 (1- current) ; normal cycling
12753 ;; last priority was empty
12754 (if (eq last-command this-command)
12755 org-lowest-priority ; wrap around empty to lowest
12757 (if org-priority-start-cycle-with-default
12758 org-default-priority
12759 (1- org-default-priority))))))
12762 (1+ current) ; normal cycling
12763 ;; last priority was empty
12764 (if (eq last-command this-command)
12765 org-highest-priority ; wrap around empty to highest
12767 (if org-priority-start-cycle-with-default
12768 org-default-priority
12769 (1+ org-default-priority))))))
12770 (t (error "Invalid action")))
12771 (if (or (< (upcase new) org-highest-priority)
12772 (> (upcase new) org-lowest-priority))
12773 (if (and (memq action '(up down))
12774 (not have) (not (eq last-command this-command)))
12775 ;; `new' is from default priority
12777 "The default can not be set, see `org-default-priority' why")
12778 ;; normal cycling: `new' is beyond highest/lowest priority
12779 ;; and is wrapped around to the empty priority
12781 (setq news (format "%c" new))
12784 (replace-match "" t t nil 1)
12785 (replace-match news t t nil 2))
12787 (error "No priority cookie found in line")
12788 (let ((case-fold-search nil))
12789 (looking-at org-todo-line-regexp))
12792 (goto-char (match-end 2))
12793 (insert " [#" news "]"))
12794 (goto-char (match-beginning 3))
12795 (insert "[#" news "] "))))
12796 (org-preserve-lc (org-set-tags nil 'align)))
12798 (message "Priority removed")
12799 (message "Priority of current item set to %s" news))))
12801 (defun org-get-priority (s)
12802 "Find priority cookie and return priority."
12803 (if (functionp org-get-priority-function)
12804 (funcall org-get-priority-function)
12806 (if (not (string-match org-priority-regexp s))
12807 (* 1000 (- org-lowest-priority org-default-priority))
12808 (* 1000 (- org-lowest-priority
12809 (string-to-char (match-string 2 s))))))))
12813 (defvar org-agenda-archives-mode)
12814 (defvar org-map-continue-from nil
12815 "Position from where mapping should continue.
12816 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
12818 (defvar org-scanner-tags nil
12819 "The current tag list while the tags scanner is running.")
12820 (defvar org-trust-scanner-tags nil
12821 "Should `org-get-tags-at' use the tags for the scanner.
12822 This is for internal dynamical scoping only.
12823 When this is non-nil, the function `org-get-tags-at' will return the value
12824 of `org-scanner-tags' instead of building the list by itself. This
12825 can lead to large speed-ups when the tags scanner is used in a file with
12826 many entries, and when the list of tags is retrieved, for example to
12827 obtain a list of properties. Building the tags list for each entry in such
12828 a file becomes an N^2 operation - but with this variable set, it scales
12831 (defun org-scan-tags (action matcher todo-only &optional start-level)
12832 "Scan headline tags with inheritance and produce output ACTION.
12834 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
12835 or `agenda' to produce an entry list for an agenda view. It can also be
12836 a Lisp form or a function that should be called at each matched headline, in
12837 this case the return value is a list of all return values from these calls.
12839 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
12840 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
12841 only lines with a not-done TODO keyword are included in the output.
12842 This should be the same variable that was scoped into
12843 and set by `org-make-tags-matcher' when it constructed MATCHER.
12845 START-LEVEL can be a string with asterisks, reducing the scope to
12846 headlines matching this string."
12847 (require 'org-agenda)
12848 (let* ((re (concat "^"
12850 ;; Get the correct level to match
12851 (concat "\\*\\{" (number-to-string start-level) "\\} ")
12852 org-outline-regexp)
12854 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
12856 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
12857 (props (list 'face 'default
12858 'done-face 'org-agenda-done
12859 'undone-face 'default
12860 'mouse-face 'highlight
12861 'org-not-done-regexp org-not-done-regexp
12862 'org-todo-regexp org-todo-regexp
12863 'org-complex-heading-regexp org-complex-heading-regexp
12865 (format "mouse-2 or RET jump to org file %s"
12866 (abbreviate-file-name
12867 (or (buffer-file-name (buffer-base-buffer))
12868 (buffer-name (buffer-base-buffer)))))))
12869 (case-fold-search nil)
12870 (org-map-continue-from nil)
12871 lspos tags tags-list
12872 (tags-alist (list (cons 0 org-file-tags)))
12873 (llast 0) rtn rtn1 level category i txt
12874 todo marker entry priority)
12875 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
12876 (setq action (list 'lambda nil action)))
12878 (goto-char (point-min))
12879 (when (eq action 'sparse-tree)
12881 (org-remove-occur-highlights))
12882 (while (re-search-forward re nil t)
12883 (setq org-map-continue-from nil)
12885 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
12886 tags (if (match-end 4) (org-match-string-no-properties 4)))
12887 (goto-char (setq lspos (match-beginning 0)))
12888 (setq level (org-reduced-level (funcall outline-level))
12889 category (org-get-category))
12890 (setq i llast llast level)
12891 ;; remove tag lists from same and sublevels
12892 (while (>= i level)
12893 (when (setq entry (assoc i tags-alist))
12894 (setq tags-alist (delete entry tags-alist)))
12896 ;; add the next tags
12898 (setq tags (org-split-string tags ":")
12900 (cons (cons level tags) tags-alist)))
12901 ;; compile tags for current headline
12903 (if org-use-tag-inheritance
12904 (apply 'append (mapcar 'cdr (reverse tags-alist)))
12906 org-scanner-tags tags-list)
12907 (when org-use-tag-inheritance
12908 (setcdr (car tags-alist)
12909 (mapcar (lambda (x)
12910 (setq x (copy-sequence x))
12911 (org-add-prop-inherited x))
12912 (cdar tags-alist))))
12913 (when (and tags org-use-tag-inheritance
12914 (or (not (eq t org-use-tag-inheritance))
12915 org-tags-exclude-from-inheritance))
12916 ;; selective inheritance, remove uninherited ones
12917 (setcdr (car tags-alist)
12918 (org-remove-uninherited-tags (cdar tags-alist))))
12921 ;; eval matcher only when the todo condition is OK
12922 (and (or (not todo-only) (member todo org-not-done-keywords))
12923 (let ((case-fold-search t) (org-trust-scanner-tags t))
12926 ;; Call the skipper, but return t if it does not skip,
12927 ;; so that the `and' form continues evaluating
12929 (unless (eq action 'sparse-tree) (org-agenda-skip))
12932 ;; Check if timestamps are deselecting this entry
12933 (or (not todo-only)
12934 (and (member todo org-not-done-keywords)
12935 (or (not org-agenda-tags-todo-honor-ignore-options)
12936 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
12938 ;; Extra check for the archive tag
12939 ;; FIXME: Does the skipper already do this????
12941 (not (member org-archive-tag tags-list))
12942 ;; we have an archive tag, should we use this anyway?
12943 (or (not org-agenda-skip-archived-trees)
12944 (and (eq action 'agenda) org-agenda-archives-mode))))
12946 ;; select this headline
12949 ((eq action 'sparse-tree)
12950 (and org-highlight-sparse-tree-matches
12951 (org-get-heading) (match-end 0)
12952 (org-highlight-new-match
12953 (match-beginning 1) (match-end 1)))
12954 (org-show-context 'tags-tree))
12955 ((eq action 'agenda)
12956 (setq txt (org-agenda-format-item
12959 (if (eq org-tags-match-list-sublevels 'indented)
12960 (make-string (1- level) ?.) "")
12964 priority (org-get-priority txt))
12966 (setq marker (org-agenda-new-marker))
12967 (org-add-props txt props
12968 'org-marker marker 'org-hd-marker marker 'org-category category
12970 'priority priority 'type "tagsmatch")
12972 ((functionp action)
12973 (setq org-map-continue-from nil)
12975 (setq rtn1 (funcall action))
12977 (t (error "Invalid action")))
12979 ;; if we are to skip sublevels, jump to end of subtree
12980 (unless org-tags-match-list-sublevels
12981 (org-end-of-subtree t)
12982 (backward-char 1))))
12983 ;; Get the correct position from where to continue
12984 (if org-map-continue-from
12985 (goto-char org-map-continue-from)
12986 (and (= (point) lspos) (end-of-line 1)))))
12987 (when (and (eq action 'sparse-tree)
12988 (not org-sparse-tree-open-archived-trees))
12989 (org-hide-archived-subtrees (point-min) (point-max)))
12992 (defun org-remove-uninherited-tags (tags)
12993 "Remove all tags that are not inherited from the list TAGS."
12995 ((eq org-use-tag-inheritance t)
12996 (if org-tags-exclude-from-inheritance
12997 (org-delete-all org-tags-exclude-from-inheritance tags)
12999 ((not org-use-tag-inheritance) nil)
13000 ((stringp org-use-tag-inheritance)
13003 (if (and (string-match org-use-tag-inheritance x)
13004 (not (member x org-tags-exclude-from-inheritance)))
13007 ((listp org-use-tag-inheritance)
13010 (if (member x org-use-tag-inheritance) x nil))
13013 (defun org-match-sparse-tree (&optional todo-only match)
13014 "Create a sparse tree according to tags string MATCH.
13015 MATCH can contain positive and negative selection of tags, like
13016 \"+WORK+URGENT-WITHBOSS\".
13017 If optional argument TODO-ONLY is non-nil, only select lines that are
13020 (org-prepare-agenda-buffers (list (current-buffer)))
13021 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13023 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13025 (defvar org-cached-props nil)
13026 (defun org-cached-entry-get (pom property)
13027 (if (or (eq t org-use-property-inheritance)
13028 (and (stringp org-use-property-inheritance)
13029 (string-match org-use-property-inheritance property))
13030 (and (listp org-use-property-inheritance)
13031 (member property org-use-property-inheritance)))
13032 ;; Caching is not possible, check it directly
13033 (org-entry-get pom property 'inherit)
13034 ;; Get all properties, so that we can do complicated checks easily
13035 (cdr (assoc property (or org-cached-props
13036 (setq org-cached-props
13037 (org-entry-properties pom)))))))
13039 (defun org-global-tags-completion-table (&optional files)
13040 "Return the list of all tags in all agenda buffer/files.
13041 Optional FILES argument is a list of files to which can be used
13042 instead of the agenda files."
13049 (set-buffer (find-file-noselect file))
13050 (append (org-get-buffer-tags)
13051 (mapcar (lambda (x) (if (stringp (car-safe x))
13052 (list (car-safe x)) nil))
13054 (if (and files (car files))
13056 (org-agenda-files))))))))
13058 (defun org-make-tags-matcher (match)
13059 "Create the TAGS/TODO matcher form for the selection string MATCH.
13061 The variable `todo-only' is scoped dynamically into this function; it will be
13062 set to t if the matcher restricts matching to TODO entries,
13063 otherwise will not be touched.
13065 Returns a cons of the selection string MATCH and the constructed
13066 lisp form implementing the matcher. The matcher is to be
13067 evaluated at an Org entry, with point on the headline,
13068 and returns t if the entry matches the
13069 selection string MATCH. The returned lisp form references
13070 two variables with information about the entry, which must be
13071 bound around the form's evaluation: todo, the TODO keyword at the
13072 entry (or nil of none); and tags-list, the list of all tags at the
13073 entry including inherited ones. Additionally, the category
13074 of the entry (if any) must be specified as the text property
13075 'org-category on the headline.
13077 See also `org-scan-tags'.
13079 (declare (special todo-only))
13080 (unless (boundp 'todo-only)
13081 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13083 ;; Get a new match request, with completion
13084 (let ((org-last-tags-completion-table
13085 (org-global-tags-completion-table)))
13086 (setq match (org-completing-read-no-i
13087 "Match: " 'org-tags-completion-function nil nil nil
13088 'org-tags-history))))
13090 ;; Parse the string and create a lisp form
13091 (let ((match0 match)
13092 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13094 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13095 orterms term orlist re-p str-p level-p level-op time-p
13096 prop-p pn pv po gv rest)
13097 (if (string-match "/+" match)
13098 ;; match contains also a todo-matching request
13100 (setq tagsmatch (substring match 0 (match-beginning 0))
13101 todomatch (substring match (match-end 0)))
13102 (if (string-match "^!" todomatch)
13103 (setq todo-only t todomatch (substring todomatch 1)))
13104 (if (string-match "^\\s-*$" todomatch)
13105 (setq todomatch nil)))
13106 ;; only matching tags
13107 (setq tagsmatch match todomatch nil))
13109 ;; Make the tags matcher
13110 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13111 (setq tagsmatcher t)
13112 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13113 (while (setq term (pop orterms))
13114 (while (and (equal (substring term -1) "\\") orterms)
13115 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13116 (while (string-match re term)
13117 (setq rest (substring term (match-end 0))
13118 minus (and (match-end 1)
13119 (equal (match-string 1 term) "-"))
13120 tag (save-match-data (replace-regexp-in-string
13122 (match-string 2 term)))
13123 re-p (equal (string-to-char tag) ?{)
13124 level-p (match-end 4)
13125 prop-p (match-end 5)
13127 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13129 (setq level-op (org-op-to-function (match-string 3 term)))
13130 `(,level-op level ,(string-to-number
13131 (match-string 4 term))))
13133 (setq pn (match-string 5 term)
13134 po (match-string 6 term)
13135 pv (match-string 7 term)
13136 re-p (equal (string-to-char pv) ?{)
13137 str-p (equal (string-to-char pv) ?\")
13138 time-p (save-match-data
13139 (string-match "^\"[[<].*[]>]\"$" pv))
13140 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13141 (if time-p (setq pv (org-matcher-time pv)))
13142 (setq po (org-op-to-function po (if time-p 'time str-p)))
13144 ((equal pn "CATEGORY")
13145 (setq gv '(get-text-property (point) 'org-category)))
13149 (setq gv `(org-cached-entry-get nil ,pn))))
13152 `(not (string-match ,pv (or ,gv "")))
13153 `(string-match ,pv (or ,gv "")))
13155 `(,po (or ,gv "") ,pv)
13156 `(,po (string-to-number (or ,gv ""))
13157 ,(string-to-number pv) ))))
13158 (t `(member ,tag tags-list)))
13159 mm (if minus (list 'not mm) mm)
13161 (push mm tagsmatcher))
13162 (push (if (> (length tagsmatcher) 1)
13163 (cons 'and tagsmatcher)
13166 (setq tagsmatcher nil))
13167 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13169 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13170 ;; Make the todo matcher
13171 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13172 (setq todomatcher t)
13173 (setq orterms (org-split-string todomatch "|") orlist nil)
13174 (while (setq term (pop orterms))
13175 (while (string-match re term)
13176 (setq minus (and (match-end 1)
13177 (equal (match-string 1 term) "-"))
13178 kwd (match-string 2 term)
13179 re-p (equal (string-to-char kwd) ?{)
13180 term (substring term (match-end 0))
13182 `(string-match ,(substring kwd 1 -1) todo)
13183 (list 'equal 'todo kwd))
13184 mm (if minus (list 'not mm) mm))
13185 (push mm todomatcher))
13186 (push (if (> (length todomatcher) 1)
13187 (cons 'and todomatcher)
13190 (setq todomatcher nil))
13191 (setq todomatcher (if (> (length orlist) 1)
13192 (cons 'or orlist) (car orlist))))
13194 ;; Return the string and lisp forms of the matcher
13195 (setq matcher (if todomatcher
13196 (list 'and tagsmatcher todomatcher)
13199 (setq matcher (list 'and '(member todo org-not-done-keywords)
13201 (cons match0 matcher)))
13203 (defun org-op-to-function (op &optional stringp)
13204 "Turn an operator into the appropriate function."
13207 ((equal op "<" ) '(< string< org-time<))
13208 ((equal op ">" ) '(> org-string> org-time>))
13209 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13210 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13211 ((member op '("=" "==")) '(= string= org-time=))
13212 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13213 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13215 (defun org<> (a b) (not (= a b)))
13216 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13217 (defun org-string>= (a b) (not (string< a b)))
13218 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13219 (defun org-string<> (a b) (not (string= a b)))
13220 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13221 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13222 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13223 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13224 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13225 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13227 "Convert S to a floating point time.
13228 If S is already a number, just return it. If it is a string, parse
13229 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13233 (condition-case nil
13234 (float-time (apply 'encode-time (org-parse-time-string s)))
13238 (defun org-time-today ()
13239 "Time in seconds today at 0:00.
13240 Returns the float number of seconds since the beginning of the
13241 epoch to the beginning of today (00:00)."
13242 (float-time (apply 'encode-time
13243 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13245 (defun org-matcher-time (s)
13246 "Interpret a time comparison value."
13249 ((string= s "<now>") (float-time))
13250 ((string= s "<today>") (org-time-today))
13251 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13252 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13253 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
13254 (+ (org-time-today)
13255 (* (string-to-number (match-string 1 s))
13256 (cdr (assoc (match-string 2 s)
13257 '(("d" . 86400.0) ("w" . 604800.0)
13258 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13261 (defun org-match-any-p (re list)
13262 "Does re match any element of list?"
13263 (setq list (mapcar (lambda (x) (string-match re x)) list))
13266 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13267 (defvar org-tags-overlay (make-overlay 1 1))
13268 (org-detach-overlay org-tags-overlay)
13270 (defun org-get-local-tags-at (&optional pos)
13271 "Get a list of tags defined in the current headline."
13272 (org-get-tags-at pos 'local))
13274 (defun org-get-local-tags ()
13275 "Get a list of tags defined in the current headline."
13276 (org-get-tags-at nil 'local))
13278 (defun org-get-tags-at (&optional pos local)
13279 "Get a list of all headline tags applicable at POS.
13280 POS defaults to point. If tags are inherited, the list contains
13281 the targets in the same sequence as the headlines appear, i.e.
13282 the tags of the current headline come last.
13283 When LOCAL is non-nil, only return tags from the current headline,
13284 ignore inherited ones."
13286 (if (and org-trust-scanner-tags
13287 (or (not pos) (equal pos (point)))
13290 (let (tags ltags lastpos parent)
13294 (goto-char (or pos (point)))
13297 (condition-case nil
13299 (org-back-to-heading t)
13300 (while (not (equal lastpos (point)))
13301 (setq lastpos (point))
13303 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13304 (setq ltags (org-split-string
13305 (org-match-string-no-properties 1) ":"))
13307 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13310 (org-remove-uninherited-tags ltags)
13313 (or org-use-tag-inheritance (throw 'done t))
13314 (if local (throw 'done t))
13315 (or (org-up-heading-safe) (error nil))
13320 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13322 (defun org-add-prop-inherited (s)
13323 (add-text-properties 0 (length s) '(inherited t) s)
13326 (defun org-toggle-tag (tag &optional onoff)
13327 "Toggle the tag TAG for the current line.
13328 If ONOFF is `on' or `off', don't toggle but set to this state."
13331 (org-back-to-heading t)
13332 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13335 (setq current (match-string 1))
13336 (replace-match ""))
13338 (setq current (nreverse (org-split-string current ":")))
13342 (or (member tag current) (push tag current)))
13344 (or (not (member tag current)) (setq current (delete tag current))))
13345 (t (if (member tag current)
13346 (setq current (delete tag current))
13348 (push tag current))))
13352 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13353 (org-set-tags nil t))
13354 (delete-horizontal-space))
13355 (run-hooks 'org-after-tags-change-hook))
13358 (defun org-align-tags-here (to-col)
13359 ;; Assumes that this is a headline
13360 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13361 (beginning-of-line 1)
13362 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13363 (< pos (match-beginning 2)))
13365 (setq tags-l (- (match-end 2) (match-beginning 2)))
13366 (goto-char (match-beginning 1))
13368 (delete-region (point) (1+ (match-beginning 2)))
13369 (setq ncol (max (current-column)
13373 (- (abs to-col) tags-l))))
13375 (insert (make-string (- ncol (current-column)) ?\ ))
13376 (setq ncol (current-column))
13377 (when indent-tabs-mode (tabify p (point-at-eol)))
13378 (org-move-to-column (min ncol col) t))
13381 (defun org-set-tags-command (&optional arg just-align)
13382 "Call the set-tags command for the current entry."
13384 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13385 (org-set-tags arg just-align)
13387 (org-back-to-heading t)
13388 (org-set-tags arg just-align))))
13390 (defun org-set-tags-to (data)
13391 "Set the tags of the current entry to DATA, replacing the current tags.
13392 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13393 If DATA is nil or the empty string, any tags will be removed."
13394 (interactive "sTags: ")
13398 ((equal data "") "")
13400 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13403 (concat ":" (mapconcat 'identity data ":") ":"))
13407 (org-back-to-heading t)
13408 (when (looking-at org-complex-heading-regexp)
13411 (goto-char (match-beginning 5))
13413 (delete-region (point) (point-at-eol))
13414 (org-set-tags nil 'align))
13415 (goto-char (point-at-eol))
13417 (org-set-tags nil 'align)))
13418 (beginning-of-line 1)
13419 (if (looking-at ".*?\\([ \t]+\\)$")
13420 (delete-region (match-beginning 1) (match-end 1))))))
13422 (defun org-align-all-tags ()
13423 "Align the tags i all headings."
13426 (or (ignore-errors (org-back-to-heading t))
13427 (outline-next-heading))
13428 (if (org-at-heading-p)
13430 (message "No headings"))))
13432 (defvar org-indent-indentation-per-level)
13433 (defun org-set-tags (&optional arg just-align)
13434 "Set the tags for the current headline.
13435 With prefix ARG, realign all tags in headings in the current buffer."
13437 (let* ((re org-outline-regexp-bol)
13438 (current (unless arg (org-get-tags-string)))
13439 (col (current-column))
13440 (org-setting-tags t)
13441 table current-tags inherited-tags ; computed below when needed
13442 tags p0 c0 c1 rpl di tc level)
13445 (goto-char (point-min))
13446 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13447 (while (re-search-forward re nil t)
13448 (org-set-tags nil t)
13450 (message "All tags realigned to column %d" org-tags-column))
13452 (setq tags current)
13453 ;; Get a new set of tags from the user
13455 (setq table (append org-tag-persistent-alist
13456 (or org-tag-alist (org-get-buffer-tags))
13458 org-complete-tags-always-offer-all-agenda-tags
13459 (org-global-tags-completion-table
13460 (org-agenda-files))))
13461 org-last-tags-completion-table table
13462 current-tags (org-split-string current ":")
13463 inherited-tags (nreverse
13464 (nthcdr (length current-tags)
13465 (nreverse (org-get-tags-at))))
13467 (if (or (eq t org-use-fast-tag-selection)
13468 (and org-use-fast-tag-selection
13469 (delq nil (mapcar 'cdr table))))
13470 (org-fast-tag-selection
13471 current-tags inherited-tags table
13472 (if org-fast-tag-selection-include-todo
13473 org-todo-key-alist))
13474 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13476 (org-icompleting-read "Tags: "
13477 'org-tags-completion-function
13478 nil nil current 'org-tags-history))))))
13479 (while (string-match "[-+&]+" tags)
13480 ;; No boolean logic, just a list
13481 (setq tags (replace-match ":" t t tags))))
13483 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13485 (if org-tags-sort-function
13486 (setq tags (mapconcat 'identity
13487 (sort (org-split-string
13488 tags (org-re "[^[:alnum:]_@#%]+"))
13489 org-tags-sort-function) ":")))
13491 (if (string-match "\\`[\t ]*\\'" tags)
13493 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13494 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13496 ;; Insert new tags at the correct column
13497 (beginning-of-line 1)
13498 (setq level (or (and (looking-at org-outline-regexp)
13499 (- (match-end 0) (point) 1))
13502 ((and (equal current "") (equal tags "")))
13503 ((re-search-forward
13504 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13506 (if (equal tags "")
13508 (goto-char (match-beginning 0))
13509 (setq c0 (current-column)
13510 ;; compute offset for the case of org-indent-mode active
13511 di (if org-indent-mode
13512 (* (1- org-indent-indentation-per-level) (1- level))
13514 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13515 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13516 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13517 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13518 (replace-match rpl t t)
13519 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13521 (t (error "Tags alignment failed")))
13522 (org-move-to-column col)
13524 (run-hooks 'org-after-tags-change-hook)))))
13526 (defun org-change-tag-in-region (beg end tag off)
13527 "Add or remove TAG for each entry in the region.
13528 This works in the agenda, and also in an org-mode buffer."
13530 (list (region-beginning) (region-end)
13531 (let ((org-last-tags-completion-table
13532 (if (eq major-mode 'org-mode)
13533 (org-get-buffer-tags)
13534 (org-global-tags-completion-table))))
13535 (org-icompleting-read
13536 "Tag: " 'org-tags-completion-function nil nil nil
13537 'org-tags-history))
13539 (message "[s]et or
[r]emove? ")
13540 (equal (read-char-exclusive) ?r))))
13541 (if (fboundp 'deactivate-mark) (deactivate-mark))
13542 (let ((agendap (equal major-mode 'org-agenda-mode))
13543 l1 l2 m buf pos newhead (cnt 0))
13545 (setq l2 (1- (org-current-line)))
13547 (setq l1 (org-current-line))
13548 (loop for l from l1 to l2 do
13550 (setq m (get-text-property (point) 'org-hd-marker))
13551 (when (or (and (eq major-mode 'org-mode) (org-at-heading-p))
13553 (setq buf (if agendap (marker-buffer m) (current-buffer))
13554 pos (if agendap m (point)))
13555 (with-current-buffer buf
13559 (setq cnt (1+ cnt))
13560 (org-toggle-tag tag (if off 'off 'on))
13561 (setq newhead (org-get-heading)))))
13562 (and agendap (org-agenda-change-all-lines newhead m))))
13563 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13565 (defun org-tags-completion-function (string predicate &optional flag)
13566 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13567 (confirm (lambda (x) (stringp (car x)))))
13568 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13569 (setq s1 (match-string 1 string)
13570 s2 (match-string 2 string))
13571 (setq s1 "" s2 string))
13575 (setq rtn (try-completion s2 ctable confirm))
13578 (concat s1 s2 (substring rtn (length s2))
13579 (if (and org-add-colon-after-tag-completion
13580 (assoc rtn ctable))
13585 (all-completions s2 ctable confirm)
13589 (assoc s2 ctable)))
13592 (defun org-fast-tag-insert (kwd tags face &optional end)
13593 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13594 (insert (format "%-12s" (concat kwd ":"))
13595 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13598 (defun org-fast-tag-show-exit (flag)
13601 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13602 (replace-match ""))
13605 (org-move-to-column (- (window-width) 19) t)
13606 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13608 (defun org-set-current-tags-overlay (current prefix)
13609 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13610 (if (featurep 'xemacs)
13611 (org-overlay-display org-tags-overlay (concat prefix s)
13612 'secondary-selection)
13613 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13614 (org-overlay-display org-tags-overlay (concat prefix s)))))
13616 (defvar org-last-tag-selection-key nil)
13617 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13618 "Fast tag selection with single keys.
13619 CURRENT is the current list of tags in the headline, INHERITED is the
13620 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13621 possibly with grouping information. TODO-TABLE is a similar table with
13622 TODO keywords, should these have keys assigned to them.
13623 If the keys are nil, a-z are automatically assigned.
13624 Returns the new tags string, or nil to not change the current settings."
13625 (let* ((fulltable (append table todo-table))
13626 (maxlen (apply 'max (mapcar
13628 (if (stringp (car x)) (string-width (car x)) 0))
13630 (buf (current-buffer))
13631 (expert (eq org-fast-tag-selection-single-key 'expert))
13633 (fwidth (+ maxlen 3 1 3))
13634 (ncol (/ (- (window-width) 4) fwidth))
13637 tg cnt e c char c1 c2 ntable tbl rtn
13638 ov-start ov-end ov-prefix
13639 (exit-after-next org-fast-tag-selection-single-key)
13640 (done-keywords org-done-keywords)
13643 (beginning-of-line 1)
13645 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13646 (setq ov-start (match-beginning 1)
13647 ov-end (match-end 1)
13649 (setq ov-start (1- (point-at-eol))
13650 ov-end (1+ ov-start))
13651 (skip-chars-forward "^\n\r")
13654 (buffer-substring (1- (point)) (point))
13655 (if (> (current-column) org-tags-column)
13657 (make-string (- org-tags-column (current-column)) ?\ ))))))
13658 (move-overlay org-tags-overlay ov-start ov-end)
13659 (save-window-excursion
13661 (set-buffer (get-buffer-create " *Org tags*"))
13662 (delete-other-windows)
13663 (split-window-vertically)
13664 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13666 (org-set-local 'org-done-keywords done-keywords)
13667 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13668 (org-fast-tag-insert "Current" current c-face "\n\n")
13669 (org-fast-tag-show-exit exit-after-next)
13670 (org-set-current-tags-overlay current ov-prefix)
13671 (setq tbl fulltable char ?a cnt 0)
13672 (while (setq e (pop tbl))
13674 ((equal (car e) :startgroup)
13675 (push '() groups) (setq ingroup t)
13676 (when (not (= cnt 0))
13679 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13680 ((equal (car e) :endgroup)
13681 (setq ingroup nil cnt 0)
13682 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13683 ((equal e '(:newline))
13684 (when (not (= cnt 0))
13688 (while (equal (car tbl) '(:newline))
13690 (setq tbl (cdr tbl)))))
13692 (setq tg (copy-sequence (car e)) c2 nil)
13695 ;; automatically assign a character.
13696 (setq c1 (string-to-char
13697 (downcase (substring
13698 tg (if (= (string-to-char tg) ?@) 1 0)))))
13699 (if (or (rassoc c1 ntable) (rassoc c1 table))
13700 (while (or (rassoc char ntable) (rassoc char table))
13701 (setq char (1+ char)))
13703 (setq c (or c2 char)))
13704 (if ingroup (push tg (car groups)))
13705 (setq tg (org-add-props tg nil 'face
13707 ((not (assoc tg table))
13708 (org-get-todo-face tg))
13709 ((member tg current) c-face)
13710 ((member tg inherited) i-face)
13712 (if (and (= cnt 0) (not ingroup)) (insert " "))
13713 (insert "[" c "] " tg (make-string
13714 (- fwidth 4 (length tg)) ?\ ))
13715 (push (cons tg c) ntable)
13716 (when (= (setq cnt (1+ cnt)) ncol)
13718 (if ingroup (insert " "))
13720 (setq ntable (nreverse ntable))
13722 (goto-char (point-min))
13723 (if (not expert) (org-fit-window-to-buffer))
13727 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13728 (if (not groups) "no " "")
13729 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13730 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13731 (setq org-last-tag-selection-key c)
13733 ((= c ?\r) (throw 'exit t))
13735 (setq groups (not groups))
13736 (goto-char (point-min))
13737 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13740 (org-fast-tag-show-exit
13741 (setq exit-after-next (not exit-after-next)))
13743 (delete-other-windows)
13744 (set-window-buffer (split-window-vertically) " *Org tags*")
13745 (org-switch-to-buffer-other-window " *Org tags*")
13746 (org-fit-window-to-buffer)))
13748 (and (= c ?q) (not (rassoc c ntable))))
13749 (org-detach-overlay org-tags-overlay)
13750 (setq quit-flag t))
13753 (if exit-after-next (setq exit-after-next 'now)))
13755 (condition-case nil
13756 (setq tg (org-icompleting-read
13759 (with-current-buffer buf
13760 (org-get-buffer-tags)))))
13761 (quit (setq tg "")))
13762 (when (string-match "\\S-" tg)
13763 (add-to-list 'buffer-tags (list tg))
13764 (if (member tg current)
13765 (setq current (delete tg current))
13766 (push tg current)))
13767 (if exit-after-next (setq exit-after-next 'now)))
13768 ((setq e (rassoc c todo-table) tg (car e))
13769 (with-current-buffer buf
13770 (save-excursion (org-todo tg)))
13771 (if exit-after-next (setq exit-after-next 'now)))
13772 ((setq e (rassoc c ntable) tg (car e))
13773 (if (member tg current)
13774 (setq current (delete tg current))
13775 (loop for g in groups do
13778 (setq current (delete x current)))
13781 (if exit-after-next (setq exit-after-next 'now))))
13783 ;; Create a sorted list
13787 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13788 (if (eq exit-after-next 'now) (throw 'exit t))
13789 (goto-char (point-min))
13790 (beginning-of-line 2)
13791 (delete-region (point) (point-at-eol))
13792 (org-fast-tag-insert "Current" current c-face)
13793 (org-set-current-tags-overlay current ov-prefix)
13794 (while (re-search-forward
13795 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
13796 (setq tg (match-string 1))
13797 (add-text-properties
13798 (match-beginning 1) (match-end 1)
13801 ((member tg current) c-face)
13802 ((member tg inherited) i-face)
13803 (t (get-text-property (match-beginning 1) 'face))))))
13804 (goto-char (point-min)))))
13805 (org-detach-overlay org-tags-overlay)
13807 (mapconcat 'identity current ":")
13810 (defun org-get-tags-string ()
13811 "Get the TAGS string in the current headline."
13812 (unless (org-at-heading-p t)
13813 (error "Not on a heading"))
13815 (beginning-of-line 1)
13816 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13817 (org-match-string-no-properties 1)
13820 (defun org-get-tags ()
13821 "Get the list of tags specified in the current headline."
13822 (org-split-string (org-get-tags-string) ":"))
13824 (defun org-get-buffer-tags ()
13825 "Get a table of all tags used in the buffer, for completion."
13828 (goto-char (point-min))
13829 (while (re-search-forward
13830 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
13831 (when (equal (char-after (point-at-bol 0)) ?*)
13832 (mapc (lambda (x) (add-to-list 'tags x))
13833 (org-split-string (org-match-string-no-properties 1) ":")))))
13834 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
13835 (mapcar 'list tags)))
13837 ;;;; The mapping API
13840 (defun org-map-entries (func &optional match scope &rest skip)
13841 "Call FUNC at each headline selected by MATCH in SCOPE.
13843 FUNC is a function or a lisp form. The function will be called without
13844 arguments, with the cursor positioned at the beginning of the headline.
13845 The return values of all calls to the function will be collected and
13846 returned as a list.
13848 The call to FUNC will be wrapped into a save-excursion form, so FUNC
13849 does not need to preserve point. After evaluation, the cursor will be
13850 moved to the end of the line (presumably of the headline of the
13851 processed entry) and search continues from there. Under some
13852 circumstances, this may not produce the wanted results. For example,
13853 if you have removed (e.g. archived) the current (sub)tree it could
13854 mean that the next entry will be skipped entirely. In such cases, you
13855 can specify the position from where search should continue by making
13856 FUNC set the variable `org-map-continue-from' to the desired buffer
13859 MATCH is a tags/property/todo match as it is used in the agenda tags view.
13860 Only headlines that are matched by this query will be considered during
13861 the iteration. When MATCH is nil or t, all headlines will be
13862 visited by the iteration.
13864 SCOPE determines the scope of this command. It can be any of:
13866 nil The current buffer, respecting the restriction if any
13867 tree The subtree started with the entry at point
13868 region The entries within the active region, if any
13870 The entries within the active region, but only those at
13871 the same level than the first one.
13872 file The current buffer, without restriction
13874 The current buffer, and any archives associated with it
13875 agenda All agenda files
13876 agenda-with-archives
13877 All agenda files with any archive files associated with them
13879 If this is a list, all files in the list will be scanned
13881 The remaining args are treated as settings for the skipping facilities of
13882 the scanner. The following items can be given here:
13884 archive skip trees with the archive tag.
13885 comment skip trees with the COMMENT keyword
13886 function or Emacs Lisp form:
13887 will be used as value for `org-agenda-skip-function', so whenever
13888 the function returns t, FUNC will not be called for that
13889 entry and search will continue from the point where the
13890 function leaves it.
13892 If your function needs to retrieve the tags including inherited tags
13893 at the *current* entry, you can use the value of the variable
13894 `org-scanner-tags' which will be much faster than getting the value
13895 with `org-get-tags-at'. If your function gets properties with
13896 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
13897 to t around the call to `org-entry-properties' to get the same speedup.
13898 Note that if your function moves around to retrieve tags and properties at
13899 a *different* entry, you cannot use these techniques."
13900 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
13901 (not (org-region-active-p)))
13902 (let* ((org-agenda-archives-mode nil) ; just to make sure
13903 (org-agenda-skip-archived-trees (memq 'archive skip))
13904 (org-agenda-skip-comment-trees (memq 'comment skip))
13905 (org-agenda-skip-function
13906 (car (org-delete-all '(comment archive) skip)))
13907 (org-tags-match-list-sublevels t)
13908 (start-level (eq scope 'region-start-level))
13910 org-todo-keywords-for-agenda
13911 org-done-keywords-for-agenda
13912 org-todo-keyword-alist-for-agenda
13913 org-drawers-for-agenda
13914 org-tag-alist-for-agenda
13918 ((eq match t) (setq matcher t))
13919 ((eq match nil) (setq matcher t))
13920 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
13924 (cond ((eq scope 'tree)
13925 (org-back-to-heading t)
13926 (org-narrow-to-subtree)
13928 ((and (or (eq scope 'region) (eq scope 'region-start-level))
13929 (org-region-active-p))
13930 ;; If needed, set start-level to a string like "2"
13933 (goto-char (region-beginning))
13934 (unless (org-at-heading-p) (outline-next-heading))
13935 (setq start-level (org-current-level))))
13936 (narrow-to-region (region-beginning)
13938 (goto-char (region-end))
13939 (unless (and (bolp) (org-at-heading-p))
13940 (outline-next-heading))
13946 (org-prepare-agenda-buffers
13947 (list (buffer-file-name (current-buffer))))
13948 (setq res (org-scan-tags func matcher todo-only start-level)))
13949 ;; Get the right scope
13951 ((and scope (listp scope) (symbolp (car scope)))
13952 (setq scope (eval scope)))
13953 ((eq scope 'agenda)
13954 (setq scope (org-agenda-files t)))
13955 ((eq scope 'agenda-with-archives)
13956 (setq scope (org-agenda-files t))
13957 (setq scope (org-add-archive-files scope)))
13959 (setq scope (list (buffer-file-name))))
13960 ((eq scope 'file-with-archives)
13961 (setq scope (org-add-archive-files (list (buffer-file-name))))))
13962 (org-prepare-agenda-buffers scope)
13963 (while (setq file (pop scope))
13964 (with-current-buffer (org-find-base-buffer-visiting file)
13968 (goto-char (point-min))
13969 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
13974 ;;; Setting and retrieving properties
13976 (defconst org-special-properties
13977 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
13978 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
13979 "The special properties valid in Org-mode.
13981 These are properties that are not defined in the property drawer,
13982 but in some other way.")
13984 (defconst org-default-properties
13985 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
13986 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
13987 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
13988 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
13989 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
13990 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
13991 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
13992 "Some properties that are used by Org-mode for various purposes.
13993 Being in this list makes sure that they are offered for completion.")
13995 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
13996 "Regular expression matching the first line of a property drawer.")
13998 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
13999 "Regular expression matching the last line of a property drawer.")
14001 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14002 "Regular expression matching the first line of a property drawer.")
14004 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14005 "Regular expression matching the first line of a property drawer.")
14007 (defconst org-property-drawer-re
14008 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14009 org-property-end-re "\\)\n?")
14010 "Matches an entire property drawer.")
14012 (defconst org-clock-drawer-re
14013 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14014 org-property-end-re "\\)\n?")
14015 "Matches an entire clock drawer.")
14017 (defsubst org-re-property (property)
14018 "Return a regexp matching PROPERTY.
14019 Match group 1 will be set to the value "
14020 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14022 (defun org-property-action ()
14023 "Do an action on properties."
14026 (org-at-property-p)
14027 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14028 (setq c (read-char-exclusive))
14031 (call-interactively 'org-set-property))
14033 (call-interactively 'org-delete-property))
14035 (call-interactively 'org-delete-property-globally))
14037 (call-interactively 'org-compute-property-at-point))
14038 (t (error "No such property action %c" c)))))
14040 (defun org-set-effort (&optional value)
14041 "Set the effort property of the current entry.
14042 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
14045 (if (equal value 0) (setq value 10))
14046 (let* ((completion-ignore-case t)
14047 (prop org-effort-property)
14048 (cur (org-entry-get nil prop))
14049 (allowed (org-property-get-allowed-values nil prop 'table))
14050 (existing (mapcar 'list (org-property-values prop)))
14053 ((stringp value) value)
14054 ((and allowed (integerp value))
14055 (or (car (nth (1- value) allowed))
14056 (car (org-last allowed))))
14058 (message "Select 1-9,0, [RET%s]: %s"
14059 (if cur (concat "=" cur) "")
14060 (mapconcat 'car allowed " "))
14061 (setq rpl (read-char-exclusive))
14062 (if (equal rpl ?\r)
14064 (setq rpl (- rpl ?0))
14065 (if (equal rpl 0) (setq rpl 10))
14066 (if (and (> rpl 0) (<= rpl (length allowed)))
14067 (car (nth (1- rpl) allowed))
14068 (org-completing-read "Effort: " allowed nil))))
14070 (let (org-completion-use-ido org-completion-use-iswitchb)
14071 (org-completing-read
14072 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14073 (concat "[" cur "]") "")
14075 existing nil nil "" nil cur))))))
14076 (unless (equal (org-entry-get nil prop) val)
14077 (org-entry-put nil prop val))
14078 (message "%s is now %s" prop val)))
14080 (defun org-at-property-p ()
14081 "Is cursor inside a property drawer?"
14083 (beginning-of-line 1)
14084 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14085 (save-match-data ;; Used by calling procedures
14087 (range (unless (org-before-first-heading-p)
14088 (org-get-property-block))))
14089 (and range (<= (car range) p) (< p (cdr range))))))))
14091 (defun org-get-property-block (&optional beg end force)
14092 "Return the (beg . end) range of the body of the property drawer.
14093 BEG and END can be beginning and end of subtree, if not given
14094 they will be found.
14095 If the drawer does not exist and FORCE is non-nil, create the drawer."
14098 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
14099 (end (or end (progn (outline-next-heading) (point)))))
14101 (if (re-search-forward org-property-start-re end t)
14102 (setq beg (1+ (match-end 0)))
14105 (org-insert-property-drawer)
14106 (setq end (progn (outline-next-heading) (point))))
14109 (if (re-search-forward org-property-start-re end t)
14110 (setq beg (1+ (match-end 0)))))
14111 (if (re-search-forward org-property-end-re end t)
14112 (setq end (match-beginning 0))
14113 (or force (throw 'exit nil))
14116 (org-indent-line-function)
14117 (insert ":END:\n"))
14120 (defun org-entry-properties (&optional pom which specific)
14121 "Get all properties of the entry at point-or-marker POM.
14122 This includes the TODO keyword, the tags, time strings for deadline,
14123 scheduled, and clocking, and any additional properties defined in the
14124 entry. The return value is an alist, keys may occur multiple times
14125 if the property key was used several times.
14126 POM may also be nil, in which case the current entry is used.
14127 If WHICH is nil or `all', get all properties. If WHICH is
14128 `special' or `standard', only get that subclass. If WHICH
14129 is a string only get exactly this property. SPECIFIC can be a string, the
14130 specific property we are interested in. Specifying it can speed
14131 things up because then unnecessary parsing is avoided."
14132 (setq which (or which 'all))
14133 (org-with-point-at pom
14134 (let ((clockstr (substring org-clock-string 0 -1))
14135 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14136 (case-fold-search nil)
14137 beg end range props sum-props key key1 value string clocksum)
14139 (when (condition-case nil
14140 (and (eq major-mode 'org-mode) (org-back-to-heading t))
14143 (setq sum-props (get-text-property (point) 'org-summaries))
14144 (setq clocksum (get-text-property (point) :org-clock-minutes))
14145 (outline-next-heading)
14147 (when (memq which '(all special))
14148 ;; Get the special properties, like TODO and tags
14150 (when (and (or (not specific) (string= specific "TODO"))
14151 (looking-at org-todo-line-regexp) (match-end 2))
14152 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14153 (when (and (or (not specific) (string= specific "PRIORITY"))
14154 (looking-at org-priority-regexp))
14155 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14156 (when (or (not specific) (string= specific "FILE"))
14157 (push (cons "FILE" buffer-file-name) props))
14158 (when (and (or (not specific) (string= specific "TAGS"))
14159 (setq value (org-get-tags-string))
14160 (string-match "\\S-" value))
14161 (push (cons "TAGS" value) props))
14162 (when (and (or (not specific) (string= specific "ALLTAGS"))
14163 (setq value (org-get-tags-at)))
14164 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14167 (when (or (not specific) (string= specific "BLOCKED"))
14168 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14169 (when (or (not specific)
14171 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14172 "TIMESTAMP" "TIMESTAMP_IA")))
14174 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14175 (setq key (if (match-end 1)
14176 (substring (org-match-string-no-properties 1)
14178 string (if (equal key clockstr)
14182 (match-beginning 3) (goto-char
14184 (substring (org-match-string-no-properties 3)
14186 ;; Get the correct property name from the key. This is
14187 ;; necessary if the user has configured time keywords.
14188 (setq key1 (concat key ":"))
14192 (if (= (char-after (match-beginning 3)) ?\[)
14193 "TIMESTAMP_IA" "TIMESTAMP")))
14194 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14195 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14196 ((equal key1 org-closed-string) (setq key "CLOSED"))
14197 ((equal key1 org-clock-string) (setq key "CLOCK")))
14198 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14200 (push (cons key string) props)
14201 ;; no need to search further if match is found
14203 (when (or (equal key "CLOCK") (not (assoc key props)))
14204 (push (cons key string) props))))))
14207 (when (memq which '(all standard))
14208 ;; Get the standard properties, like :PROP: ...
14209 (setq range (org-get-property-block beg end))
14211 (goto-char (car range))
14212 (while (re-search-forward
14213 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14215 (setq key (org-match-string-no-properties 1)
14216 value (org-trim (or (org-match-string-no-properties 2) "")))
14217 (unless (member key excluded)
14218 (push (cons key (or value "")) props)))))
14220 (push (cons "CLOCKSUM"
14221 (org-columns-number-to-string (/ (float clocksum) 60.)
14224 (unless (assoc "CATEGORY" props)
14225 (push (cons "CATEGORY" (org-get-category)) props))
14226 (append sum-props (nreverse props)))))))
14228 (defun org-entry-get (pom property &optional inherit literal-nil)
14229 "Get value of PROPERTY for entry at point-or-marker POM.
14230 If INHERIT is non-nil and the entry does not have the property,
14231 then also check higher levels of the hierarchy.
14232 If INHERIT is the symbol `selective', use inheritance only if the setting
14233 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14234 If the property is present but empty, the return value is the empty string.
14235 If the property is not present at all, nil is returned.
14237 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14238 do not interpret it as the list atom nil. This is used for inheritance
14239 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14240 (org-with-point-at pom
14241 (if (and inherit (if (eq inherit 'selective)
14242 (org-property-inherit-p property)
14244 (org-entry-get-with-inheritance property literal-nil)
14245 (if (member property org-special-properties)
14246 ;; We need a special property. Use `org-entry-properties' to
14247 ;; retrieve it, but specify the wanted property
14248 (cdr (assoc property (org-entry-properties nil 'special property)))
14249 (let ((range (unless (org-before-first-heading-p)
14250 (org-get-property-block)))
14251 (props (list (or (assoc property org-file-properties)
14252 (assoc property org-global-properties)
14253 (assoc property org-global-properties-fixed))))
14256 (when (re-search-forward
14257 (org-re-property key) (cdr range) t)
14259 (org-update-property-plist
14262 (org-match-string-no-properties 1) "")
14264 (when (and range (goto-char (car range)))
14266 (goto-char (car range))
14267 (while (ap (concat property "+")))
14268 (setq val (cdr (assoc property props)))
14269 (when val (if literal-nil val (org-not-nil val))))))))))
14271 (defun org-property-or-variable-value (var &optional inherit)
14272 "Check if there is a property fixing the value of VAR.
14273 If yes, return this value. If not, return the current value of the variable."
14274 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14275 (if (and prop (stringp prop) (string-match "\\S-" prop))
14277 (symbol-value var))))
14279 (defun org-entry-delete (pom property)
14280 "Delete the property PROPERTY from entry at point-or-marker POM."
14281 (org-with-point-at pom
14282 (if (member property org-special-properties)
14283 nil ; cannot delete these properties.
14284 (let ((range (org-get-property-block)))
14286 (goto-char (car range))
14288 (org-re-property property)
14291 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14295 ;; Multi-values properties are properties that contain multiple values
14296 ;; These values are assumed to be single words, separated by whitespace.
14297 (defun org-entry-add-to-multivalued-property (pom property value)
14298 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14299 (let* ((old (org-entry-get pom property))
14300 (values (and old (org-split-string old "[ \t]"))))
14301 (setq value (org-entry-protect-space value))
14302 (unless (member value values)
14303 (setq values (cons value values))
14304 (org-entry-put pom property
14305 (mapconcat 'identity values " ")))))
14307 (defun org-entry-remove-from-multivalued-property (pom property value)
14308 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14309 (let* ((old (org-entry-get pom property))
14310 (values (and old (org-split-string old "[ \t]"))))
14311 (setq value (org-entry-protect-space value))
14312 (when (member value values)
14313 (setq values (delete value values))
14314 (org-entry-put pom property
14315 (mapconcat 'identity values " ")))))
14317 (defun org-entry-member-in-multivalued-property (pom property value)
14318 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14319 (let* ((old (org-entry-get pom property))
14320 (values (and old (org-split-string old "[ \t]"))))
14321 (setq value (org-entry-protect-space value))
14322 (member value values)))
14324 (defun org-entry-get-multivalued-property (pom property)
14325 "Return a list of values in a multivalued property."
14326 (let* ((value (org-entry-get pom property))
14327 (values (and value (org-split-string value "[ \t]"))))
14328 (mapcar 'org-entry-restore-space values)))
14330 (defun org-entry-put-multivalued-property (pom property &rest values)
14331 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14332 VALUES should be a list of strings. Spaces will be protected."
14333 (org-entry-put pom property
14334 (mapconcat 'org-entry-protect-space values " "))
14335 (let* ((value (org-entry-get pom property))
14336 (values (and value (org-split-string value "[ \t]"))))
14337 (mapcar 'org-entry-restore-space values)))
14339 (defun org-entry-protect-space (s)
14340 "Protect spaces and newline in string S."
14341 (while (string-match " " s)
14342 (setq s (replace-match "%20" t t s)))
14343 (while (string-match "\n" s)
14344 (setq s (replace-match "%0A" t t s)))
14347 (defun org-entry-restore-space (s)
14348 "Restore spaces and newline in string S."
14349 (while (string-match "%20" s)
14350 (setq s (replace-match " " t t s)))
14351 (while (string-match "%0A" s)
14352 (setq s (replace-match "\n" t t s)))
14355 (defvar org-entry-property-inherited-from (make-marker)
14356 "Marker pointing to the entry from where a property was inherited.
14357 Each call to `org-entry-get-with-inheritance' will set this marker to the
14358 location of the entry where the inheritance search matched. If there was
14359 no match, the marker will point nowhere.
14360 Note that also `org-entry-get' calls this function, if the INHERIT flag
14363 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14364 "Get entry property, and search higher levels if not present.
14365 The search will stop at the first ancestor which has the property defined.
14366 If the value found is \"nil\", return nil to show that the property
14367 should be considered as undefined (this is the meaning of nil here).
14368 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14369 (move-marker org-entry-property-inherited-from nil)
14371 (unless (org-before-first-heading-p)
14377 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14378 (org-back-to-heading t)
14379 (move-marker org-entry-property-inherited-from (point))
14381 (or (org-up-heading-safe) (throw 'ex nil)))))))
14383 (cdr (assoc property org-file-properties))
14384 (cdr (assoc property org-global-properties))
14385 (cdr (assoc property org-global-properties-fixed))))
14386 (if literal-nil tmp (org-not-nil tmp))))
14388 (defvar org-property-changed-functions nil
14389 "Hook called when the value of a property has changed.
14390 Each hook function should accept two arguments, the name of the property
14391 and the new value.")
14393 (defun org-entry-put (pom property value)
14394 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14395 (org-with-point-at pom
14396 (org-back-to-heading t)
14397 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14400 ((equal property "TODO")
14401 (when (and (stringp value) (string-match "\\S-" value)
14402 (not (member value org-todo-keywords-1)))
14403 (error "\"%s\" is not a valid TODO state" value))
14404 (if (or (not value)
14405 (not (string-match "\\S-" value)))
14406 (setq value 'none))
14408 (org-set-tags nil 'align))
14409 ((equal property "PRIORITY")
14410 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14411 (string-to-char value) ?\ ))
14412 (org-set-tags nil 'align))
14413 ((equal property "SCHEDULED")
14414 (if (re-search-forward org-scheduled-time-regexp end t)
14416 ((eq value 'earlier) (org-timestamp-change -1 'day))
14417 ((eq value 'later) (org-timestamp-change 1 'day))
14418 (t (call-interactively 'org-schedule)))
14419 (call-interactively 'org-schedule)))
14420 ((equal property "DEADLINE")
14421 (if (re-search-forward org-deadline-time-regexp end t)
14423 ((eq value 'earlier) (org-timestamp-change -1 'day))
14424 ((eq value 'later) (org-timestamp-change 1 'day))
14425 (t (call-interactively 'org-deadline)))
14426 (call-interactively 'org-deadline)))
14427 ((member property org-special-properties)
14428 (error "The %s property can not yet be set with `org-entry-put'"
14430 (t ; a non-special property
14431 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14432 (setq range (org-get-property-block beg end 'force))
14433 (goto-char (car range))
14434 (if (re-search-forward
14435 (org-re-property property) (cdr range) t)
14437 (delete-region (match-beginning 0) (match-end 0))
14438 (goto-char (match-beginning 0)))
14439 (goto-char (cdr range))
14442 (org-indent-line-function))
14443 (insert ":" property ":")
14444 (and value (insert " " value))
14445 (org-indent-line-function)))))
14446 (run-hook-with-args 'org-property-changed-functions property value)))
14448 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14449 "Get all property keys in the current buffer.
14450 With INCLUDE-SPECIALS, also list the special properties that reflect things
14451 like tags and TODO state.
14452 With INCLUDE-DEFAULTS, also include properties that has special meaning
14453 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14455 With INCLUDE-COLUMNS, also include property names given in COLUMN
14456 formats in the current buffer."
14457 (let (rtn range cfmt s p)
14461 (goto-char (point-min))
14462 (while (re-search-forward org-property-start-re nil t)
14463 (setq range (org-get-property-block))
14464 (goto-char (car range))
14465 (while (re-search-forward
14466 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14468 (add-to-list 'rtn (org-match-string-no-properties 1)))
14469 (outline-next-heading))))
14471 (when include-specials
14472 (setq rtn (append org-special-properties rtn)))
14474 (when include-defaults
14475 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14476 (add-to-list 'rtn org-effort-property))
14478 (when include-columns
14482 (goto-char (point-min))
14483 (while (re-search-forward
14484 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14486 (setq cfmt (match-string 2) s 0)
14487 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14489 (setq s (match-end 0)
14490 p (match-string 1 cfmt))
14491 (unless (or (equal p "ITEM")
14492 (member p org-special-properties))
14493 (add-to-list 'rtn (match-string 1 cfmt))))))))
14495 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14497 (defun org-property-values (key)
14498 "Return a list of all values of property KEY in the current buffer."
14502 (goto-char (point-min))
14503 (let ((re (org-re-property key))
14505 (while (re-search-forward re nil t)
14506 (add-to-list 'values (org-trim (match-string 1))))
14507 (delete "" values)))))
14509 (defun org-insert-property-drawer ()
14510 "Insert a property drawer into the current entry."
14511 (org-back-to-heading t)
14512 (looking-at org-outline-regexp)
14513 (let ((indent (if org-adapt-indentation
14514 (- (match-end 0) (match-beginning 0))
14517 (re (concat "^[ \t]*" org-keyword-time-regexp))
14519 (outline-next-heading)
14522 (while (re-search-forward re end t))
14523 (setq hiddenp (outline-invisible-p))
14525 (and (equal (char-after) ?\n) (forward-char 1))
14526 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14527 (if (member (match-string 1) '("CLOCK:" ":END:"))
14528 ;; just skip this line
14529 (beginning-of-line 2)
14530 ;; Drawer start, find the end
14531 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14532 (beginning-of-line 1)))
14533 (org-skip-over-state-notes)
14534 (skip-chars-backward " \t\n\r")
14535 (if (eq (char-before) ?*) (forward-char 1))
14536 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14537 (beginning-of-line 0)
14538 (org-indent-to-column indent)
14539 (beginning-of-line 2)
14540 (org-indent-to-column indent)
14541 (beginning-of-line 0)
14544 (org-back-to-heading t)
14546 (org-flag-drawer t))))
14548 (defun org-insert-drawer (&optional arg drawer)
14549 "Insert a drawer at point.
14551 Optional argument DRAWER, when non-nil, is a string representing
14552 drawer's name. Otherwise, the user is prompted for a name.
14554 If a region is active, insert the drawer around that region
14557 Point is left between drawer's boundaries."
14559 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14561 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14562 ;; internally by Org. They are meant to be inserted
14564 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14565 ;; Remove system drawers from list. Note: For some reason,
14566 ;; `org-completing-read' ignores the predicate while
14567 ;; `completing-read' handles it fine.
14568 (drawer (if arg "PROPERTIES"
14571 "Drawer: " org-drawers
14572 (lambda (d) (not (member d system-drawers))))))))
14574 ;; With C-u, fall back on `org-insert-property-drawer'
14575 (arg (org-insert-property-drawer))
14576 ;; With an active region, insert a drawer at point.
14577 ((not (org-region-active-p))
14579 (unless (bolp) (insert "\n"))
14580 (insert (format ":%s:\n\n:END:\n" drawer))
14581 (forward-line -2)))
14582 ;; Otherwise, insert the drawer at point
14584 (let ((rbeg (region-beginning))
14585 (rend (copy-marker (region-end))))
14589 (beginning-of-line)
14590 (when (save-excursion
14591 (re-search-forward org-outline-regexp-bol rend t))
14592 (error "Drawers cannot contain headlines"))
14593 ;; Position point at the beginning of the first
14594 ;; non-blank line in region. Insert drawer's opening
14595 ;; there, then indent it.
14596 (org-skip-whitespace)
14597 (beginning-of-line)
14598 (insert ":" drawer ":\n")
14600 (indent-for-tab-command)
14601 ;; Move point to the beginning of the first blank line
14602 ;; after the last non-blank line in region. Insert
14603 ;; drawer's closing, then indent it.
14605 (skip-chars-backward " \r\t\n")
14607 (indent-for-tab-command)
14608 (unless (eolp) (insert "\n")))
14609 ;; Clear marker, whatever the outcome of insertion is.
14610 (set-marker rend nil)))))))
14612 (defvar org-property-set-functions-alist nil
14613 "Property set function alist.
14614 Each entry should have the following format:
14616 (PROPERTY . READ-FUNCTION)
14618 The read function will be called with the same argument as
14619 `org-completing-read'.")
14621 (defun org-set-property-function (property)
14622 "Get the function that should be used to set PROPERTY.
14623 This is computed according to `org-property-set-functions-alist'."
14624 (or (cdr (assoc property org-property-set-functions-alist))
14625 'org-completing-read))
14627 (defun org-read-property-value (property)
14628 "Read PROPERTY value from user."
14629 (let* ((completion-ignore-case t)
14630 (allowed (org-property-get-allowed-values nil property 'table))
14631 (cur (org-entry-get nil property))
14632 (prompt (concat property " value"
14633 (if (and cur (string-match "\\S-" cur))
14634 (concat " [" cur "]") "") ": "))
14635 (set-function (org-set-property-function property))
14637 (funcall set-function prompt allowed nil
14638 (not (get-text-property 0 'org-unrestricted
14640 (let (org-completion-use-ido org-completion-use-iswitchb)
14641 (funcall set-function prompt
14642 (mapcar 'list (org-property-values property))
14643 nil nil "" nil cur)))))
14648 (defvar org-last-set-property nil)
14649 (defun org-read-property-name ()
14650 "Read a property name."
14651 (let* ((completion-ignore-case t)
14652 (keys (org-buffer-property-keys nil t t))
14653 (default-prop (or (save-excursion
14655 (beginning-of-line)
14656 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14657 (null (string= (match-string 1) "END"))
14658 (match-string 1))))
14659 org-last-set-property))
14660 (property (org-icompleting-read
14662 (if default-prop (concat " [" default-prop "]") "")
14664 (mapcar 'list keys)
14668 (if (member property keys)
14670 (or (cdr (assoc (downcase property)
14671 (mapcar (lambda (x) (cons (downcase x) x))
14675 (defun org-set-property (property value)
14676 "In the current entry, set PROPERTY to VALUE.
14677 When called interactively, this will prompt for a property name, offering
14678 completion on existing and default properties. And then it will prompt
14679 for a value, offering completion either on allowed values (via an inherited
14680 xxx_ALL property) or on existing values in other instances of this property
14681 in the current file."
14682 (interactive (list nil nil))
14683 (let* ((property (or property (org-read-property-name)))
14684 (value (or value (org-read-property-value property)))
14685 (fn (assoc property org-properties-postprocess-alist)))
14686 (setq org-last-set-property property)
14687 ;; Possibly postprocess the inserted value:
14688 (when fn (setq value (funcall (cadr fn) value)))
14689 (unless (equal (org-entry-get nil property) value)
14690 (org-entry-put nil property value))))
14692 (defun org-delete-property (property)
14693 "In the current entry, delete PROPERTY."
14695 (let* ((completion-ignore-case t)
14696 (prop (org-icompleting-read "Property: "
14697 (org-entry-properties nil 'standard))))
14699 (message "Property %s %s" property
14700 (if (org-entry-delete nil property)
14702 "was not present in the entry")))
14704 (defun org-delete-property-globally (property)
14705 "Remove PROPERTY globally, from all entries."
14707 (let* ((completion-ignore-case t)
14708 (prop (org-icompleting-read
14709 "Globally remove property: "
14710 (mapcar 'list (org-buffer-property-keys)))))
14715 (goto-char (point-min))
14717 (while (re-search-forward
14718 (org-re-property property)
14720 (setq cnt (1+ cnt))
14721 (delete-region (match-beginning 0) (1+ (point-at-eol))))
14722 (message "Property \"%s\" removed from %d entries" property cnt)))))
14724 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14726 (defun org-compute-property-at-point ()
14727 "Compute the property at point.
14728 This looks for an enclosing column format, extracts the operator and
14729 then applies it to the property in the column format's scope."
14731 (unless (org-at-property-p)
14732 (error "Not at a property"))
14733 (let ((prop (org-match-string-no-properties 2)))
14734 (org-columns-get-format-and-top-level)
14735 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14736 (error "No operator defined for property %s" prop))
14737 (org-columns-compute prop)))
14739 (defvar org-property-allowed-value-functions nil
14740 "Hook for functions supplying allowed values for a specific property.
14741 The functions must take a single argument, the name of the property, and
14742 return a flat list of allowed values. If \":ETC\" is one of
14743 the values, this means that these values are intended as defaults for
14744 completion, but that other values should be allowed too.
14745 The functions must return nil if they are not responsible for this
14748 (defun org-property-get-allowed-values (pom property &optional table)
14749 "Get allowed values for the property PROPERTY.
14750 When TABLE is non-nil, return an alist that can directly be used for
14754 ((equal property "TODO")
14755 (setq vals (org-with-point-at pom
14756 (append org-todo-keywords-1 '("")))))
14757 ((equal property "PRIORITY")
14758 (let ((n org-lowest-priority))
14759 (while (>= n org-highest-priority)
14760 (push (char-to-string n) vals)
14762 ((member property org-special-properties))
14763 ((setq vals (run-hook-with-args-until-success
14764 'org-property-allowed-value-functions property)))
14766 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
14767 (when (and vals (string-match "\\S-" vals))
14768 (setq vals (car (read-from-string (concat "(" vals ")"))))
14769 (setq vals (mapcar (lambda (x)
14770 (cond ((stringp x) x)
14771 ((numberp x) (number-to-string x))
14772 ((symbolp x) (symbol-name x))
14775 (when (member ":ETC" vals)
14776 (setq vals (remove ":ETC" vals))
14777 (org-add-props (car vals) '(org-unrestricted t)))
14778 (if table (mapcar 'list vals) vals)))
14780 (defun org-property-previous-allowed-value (&optional previous)
14781 "Switch to the next allowed value for this property."
14783 (org-property-next-allowed-value t))
14785 (defun org-property-next-allowed-value (&optional previous)
14786 "Switch to the next allowed value for this property."
14788 (unless (org-at-property-p)
14789 (error "Not at a property"))
14790 (let* ((key (match-string 2))
14791 (value (match-string 3))
14792 (allowed (or (org-property-get-allowed-values (point) key)
14793 (and (member value '("[ ]" "[-]" "[X]"))
14797 (error "Allowed values for this property have not been defined"))
14798 (if previous (setq allowed (reverse allowed)))
14799 (if (member value allowed)
14800 (setq nval (car (cdr (member value allowed)))))
14801 (setq nval (or nval (car allowed)))
14802 (if (equal nval value)
14803 (error "Only one allowed value for this property"))
14804 (org-at-property-p)
14805 (replace-match (concat " :" key ": " nval) t t)
14806 (org-indent-line-function)
14807 (beginning-of-line 1)
14808 (skip-chars-forward " \t")
14809 (run-hook-with-args 'org-property-changed-functions key nval)))
14811 (defun org-find-olp (path &optional this-buffer)
14812 "Return a marker pointing to the entry at outline path OLP.
14813 If anything goes wrong, throw an error.
14814 You can wrap this call to catch the error like this:
14816 (condition-case msg
14817 (org-mobile-locate-entry (match-string 4))
14818 (error (nth 1 msg)))
14820 The return value will then be either a string with the error message,
14821 or a marker if everything is OK.
14823 If THIS-BUFFER is set, the outline path does not contain a file,
14825 (let* ((file (if this-buffer buffer-file-name (pop path)))
14826 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
14830 limit re end found pos heading cnt flevel)
14831 (unless buffer (error "File not found :%s" file))
14832 (with-current-buffer buffer
14836 (setq limit (point-max))
14837 (goto-char (point-min))
14838 (while (setq heading (pop path))
14839 (setq re (format org-complex-heading-regexp-format
14840 (regexp-quote heading)))
14841 (setq cnt 0 pos (point))
14842 (while (re-search-forward re end t)
14843 (setq level (- (match-end 1) (match-beginning 1)))
14844 (if (and (>= level lmin) (<= level lmax))
14845 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
14846 (when (= cnt 0) (error "Heading not found on level %d: %s"
14848 (when (> cnt 1) (error "Heading not unique on level %d: %s"
14851 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
14852 (setq end (save-excursion (org-end-of-subtree t t))))
14853 (when (org-at-heading-p)
14854 (move-marker (make-marker) (point))))))))
14856 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
14857 "Find node HEADING in BUFFER.
14858 Return a marker to the heading if it was found, or nil if not.
14859 If POS-ONLY is set, return just the position instead of a marker.
14861 The heading text must match exact, but it may have a TODO keyword,
14862 a priority cookie and tags in the standard locations."
14863 (with-current-buffer (or buffer (current-buffer))
14867 (goto-char (point-min))
14868 (let (case-fold-search)
14869 (if (re-search-forward
14870 (format org-complex-heading-regexp-format
14871 (regexp-quote heading)) nil t)
14873 (match-beginning 0)
14874 (move-marker (make-marker) (match-beginning 0)))))))))
14876 (defun org-find-exact-heading-in-directory (heading &optional dir)
14877 "Find Org node headline HEADING in all .org files in directory DIR.
14878 When the target headline is found, return a marker to this location."
14879 (let ((files (directory-files (or dir default-directory)
14880 nil "\\`[^.#].*\\.org\\'"))
14881 file visiting m buffer)
14883 (while (setq file (pop files))
14884 (message "trying %s" file)
14885 (setq visiting (org-find-base-buffer-visiting file))
14886 (setq buffer (or visiting (find-file-noselect file)))
14887 (setq m (org-find-exact-headline-in-buffer
14889 (when (and (not m) (not visiting)) (kill-buffer buffer))
14890 (and m (throw 'found m))))))
14892 (defun org-find-entry-with-id (ident)
14893 "Locate the entry that contains the ID property with exact value IDENT.
14894 IDENT can be a string, a symbol or a number, this function will search for
14895 the string representation of it.
14896 Return the position where this entry starts, or nil if there is no such entry."
14897 (interactive "sID: ")
14899 ((stringp ident) ident)
14900 ((symbol-name ident) (symbol-name ident))
14901 ((numberp ident) (number-to-string ident))
14902 (t (error "IDENT %s must be a string, symbol or number" ident))))
14903 (case-fold-search nil))
14907 (goto-char (point-min))
14908 (when (re-search-forward
14909 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
14911 (org-back-to-heading t)
14916 (defvar org-last-changed-timestamp nil)
14917 (defvar org-last-inserted-timestamp nil
14918 "The last time stamp inserted with `org-insert-time-stamp'.")
14919 (defvar org-time-was-given) ; dynamically scoped parameter
14920 (defvar org-end-time-was-given) ; dynamically scoped parameter
14921 (defvar org-ts-what) ; dynamically scoped parameter
14923 (defun org-time-stamp (arg &optional inactive)
14924 "Prompt for a date/time and insert a time stamp.
14925 If the user specifies a time like HH:MM, or if this command is called
14926 with a prefix argument, the time stamp will contain date and time.
14927 Otherwise, only the date will be included. All parts of a date not
14928 specified by the user will be filled in from the current date/time.
14929 So if you press just return without typing anything, the time stamp
14930 will represent the current date/time. If there is already a timestamp
14931 at the cursor, it will be modified."
14935 ;; Default time is either today, or, when entering a range,
14936 ;; the range start.
14937 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
14939 (re-search-backward
14940 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
14941 (- (point) 20) t)))
14942 (apply 'encode-time (org-parse-time-string (match-string 1)))
14944 (default-input (and ts (org-get-compact-tod ts)))
14945 (repeater (save-excursion
14947 (beginning-of-line)
14948 (when (re-search-forward
14949 "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
14950 (save-excursion (progn (end-of-line) (point))) t)
14951 (match-string 0)))))
14952 org-time-was-given org-end-time-was-given time)
14954 ((and (org-at-timestamp-p t)
14955 (memq last-command '(org-time-stamp org-time-stamp-inactive))
14956 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
14958 (setq time (let ((this-command this-command))
14959 (org-read-date arg 'totime nil nil
14960 default-time default-input inactive)))
14961 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
14962 ((org-at-timestamp-p t)
14963 (setq time (let ((this-command this-command))
14964 (org-read-date arg 'totime nil nil default-time default-input inactive)))
14965 (when (org-at-timestamp-p t) ; just to get the match data
14966 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
14968 (setq org-last-changed-timestamp
14969 (org-insert-time-stamp
14970 time (or org-time-was-given arg)
14971 inactive nil nil (list org-end-time-was-given)))
14972 (when repeater (goto-char (1- (point))) (insert " " repeater)
14973 (setq org-last-changed-timestamp
14974 (concat (substring org-last-inserted-timestamp 0 -1)
14975 " " repeater ">"))))
14976 (message "Timestamp updated"))
14978 (setq time (let ((this-command this-command))
14979 (org-read-date arg 'totime nil nil default-time default-input inactive)))
14980 (org-insert-time-stamp time (or org-time-was-given arg) inactive
14981 nil nil (list org-end-time-was-given))))))
14983 ;; FIXME: can we use this for something else, like computing time differences?
14984 (defun org-get-compact-tod (s)
14985 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
14986 (let* ((t1 (match-string 1 s))
14987 (h1 (string-to-number (match-string 2 s)))
14988 (m1 (string-to-number (match-string 3 s)))
14989 (t2 (and (match-end 4) (match-string 5 s)))
14990 (h2 (and t2 (string-to-number (match-string 6 s))))
14991 (m2 (and t2 (string-to-number (match-string 7 s))))
14995 (setq dh (- h2 h1) dm (- m2 m1))
14996 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
14997 (concat t1 "+" (number-to-string dh)
14998 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15000 (defun org-time-stamp-inactive (&optional arg)
15001 "Insert an inactive time stamp.
15002 An inactive time stamp is enclosed in square brackets instead of angle
15003 brackets. It is inactive in the sense that it does not trigger agenda entries,
15004 does not link to the calendar and cannot be changed with the S-cursor keys.
15005 So these are more for recording a certain time/date."
15007 (org-time-stamp arg 'inactive))
15009 (defvar org-date-ovl (make-overlay 1 1))
15010 (overlay-put org-date-ovl 'face 'org-warning)
15011 (org-detach-overlay org-date-ovl)
15013 (defvar org-ans1) ; dynamically scoped parameter
15014 (defvar org-ans2) ; dynamically scoped parameter
15016 (defvar org-plain-time-of-day-regexp) ; defined below
15018 (defvar org-overriding-default-time nil) ; dynamically scoped
15019 (defvar org-read-date-overlay nil)
15020 (defvar org-dcst nil) ; dynamically scoped
15021 (defvar org-read-date-history nil)
15022 (defvar org-read-date-final-answer nil)
15023 (defvar org-read-date-analyze-futurep nil)
15024 (defvar org-read-date-analyze-forced-year nil)
15025 (defvar org-read-date-inactive)
15027 (defun org-read-date (&optional org-with-time to-time from-string prompt
15028 default-time default-input inactive)
15029 "Read a date, possibly a time, and make things smooth for the user.
15030 The prompt will suggest to enter an ISO date, but you can also enter anything
15031 which will at least partially be understood by `parse-time-string'.
15032 Unrecognized parts of the date will default to the current day, month, year,
15033 hour and minute. If this command is called to replace a timestamp at point,
15034 or to enter the second timestamp of a range, the default time is taken
15035 from the existing stamp. Furthermore, the command prefers the future,
15036 so if you are giving a date where the year is not given, and the day-month
15037 combination is already past in the current year, it will assume you
15038 mean next year. For details, see the manual. A few examples:
15040 3-2-5 --> 2003-02-05
15041 feb 15 --> currentyear-02-15
15042 2/15 --> currentyear-02-15
15043 sep 12 9 --> 2009-09-12
15044 12:45 --> today 12:45
15045 22 sept 0:34 --> currentyear-09-22 0:34
15046 12 --> currentyear-currentmonth-12
15047 Fri --> nearest Friday (today or later)
15050 Furthermore you can specify a relative date by giving, as the *first* thing
15051 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
15052 change in days weeks, months, years.
15053 With a single plus or minus, the date is relative to today. With a double
15054 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15055 +4d --> four days from today
15056 +4 --> same as above
15057 +2w --> two weeks from today
15058 ++5 --> five days from default date
15060 The function understands only English month and weekday abbreviations.
15062 While prompting, a calendar is popped up - you can also select the
15063 date with the mouse (button 1). The calendar shows a period of three
15064 months. To scroll it to other months, use the keys `>' and `<'.
15065 If you don't like the calendar, turn it off with
15066 \(setq org-read-date-popup-calendar nil)
15068 With optional argument TO-TIME, the date will immediately be converted
15069 to an internal time.
15070 With an optional argument WITH-TIME, the prompt will suggest to also
15071 insert a time. Note that when WITH-TIME is not set, you can still
15072 enter a time, and this function will inform the calling routine about
15073 this change. The calling routine may then choose to change the format
15074 used to insert the time stamp into the buffer to include the time.
15075 With optional argument FROM-STRING, read from this string instead from
15076 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15077 the time/date that is used for everything that is not specified by the
15079 (require 'parse-time)
15080 (let* ((org-time-stamp-rounding-minutes
15081 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15082 (org-dcst org-display-custom-times)
15083 (ct (org-current-time))
15084 (org-def (or org-overriding-default-time default-time ct))
15085 (org-defdecode (decode-time org-def))
15087 (when (< (nth 2 org-defdecode) org-extend-today-until)
15088 (setcar (nthcdr 2 org-defdecode) -1)
15089 (setcar (nthcdr 1 org-defdecode) 59)
15090 (setq org-def (apply 'encode-time org-defdecode)
15091 org-defdecode (decode-time org-def)))))
15092 (calendar-frame-setup nil)
15093 (calendar-setup nil)
15094 (calendar-move-hook nil)
15095 (calendar-view-diary-initially-flag nil)
15096 (calendar-view-holidays-initially-flag nil)
15097 (timestr (format-time-string
15098 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15099 (prompt (concat (if prompt (concat prompt " ") "")
15100 (format "Date+time [%s]: " timestr)))
15101 ans (org-ans0 "") org-ans1 org-ans2 final)
15104 (from-string (setq ans from-string))
15105 (org-read-date-popup-calendar
15107 (save-window-excursion
15111 (calendar-forward-day (- (time-to-days org-def)
15112 (calendar-absolute-from-gregorian
15113 (calendar-current-date))))
15114 (org-eval-in-calendar nil t)
15115 (let* ((old-map (current-local-map))
15116 (map (copy-keymap calendar-mode-map))
15117 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15118 (org-defkey map (kbd "RET") 'org-calendar-select)
15119 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15120 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15121 (org-defkey minibuffer-local-map [(meta shift left)]
15122 (lambda () (interactive)
15123 (org-eval-in-calendar '(calendar-backward-month 1))))
15124 (org-defkey minibuffer-local-map [(meta shift right)]
15125 (lambda () (interactive)
15126 (org-eval-in-calendar '(calendar-forward-month 1))))
15127 (org-defkey minibuffer-local-map [(meta shift up)]
15128 (lambda () (interactive)
15129 (org-eval-in-calendar '(calendar-backward-year 1))))
15130 (org-defkey minibuffer-local-map [(meta shift down)]
15131 (lambda () (interactive)
15132 (org-eval-in-calendar '(calendar-forward-year 1))))
15133 (org-defkey minibuffer-local-map [?\e (shift left)]
15134 (lambda () (interactive)
15135 (org-eval-in-calendar '(calendar-backward-month 1))))
15136 (org-defkey minibuffer-local-map [?\e (shift right)]
15137 (lambda () (interactive)
15138 (org-eval-in-calendar '(calendar-forward-month 1))))
15139 (org-defkey minibuffer-local-map [?\e (shift up)]
15140 (lambda () (interactive)
15141 (org-eval-in-calendar '(calendar-backward-year 1))))
15142 (org-defkey minibuffer-local-map [?\e (shift down)]
15143 (lambda () (interactive)
15144 (org-eval-in-calendar '(calendar-forward-year 1))))
15145 (org-defkey minibuffer-local-map [(shift up)]
15146 (lambda () (interactive)
15147 (org-eval-in-calendar '(calendar-backward-week 1))))
15148 (org-defkey minibuffer-local-map [(shift down)]
15149 (lambda () (interactive)
15150 (org-eval-in-calendar '(calendar-forward-week 1))))
15151 (org-defkey minibuffer-local-map [(shift left)]
15152 (lambda () (interactive)
15153 (org-eval-in-calendar '(calendar-backward-day 1))))
15154 (org-defkey minibuffer-local-map [(shift right)]
15155 (lambda () (interactive)
15156 (org-eval-in-calendar '(calendar-forward-day 1))))
15157 (org-defkey minibuffer-local-map ">"
15158 (lambda () (interactive)
15159 (org-eval-in-calendar '(scroll-calendar-left 1))))
15160 (org-defkey minibuffer-local-map "<"
15161 (lambda () (interactive)
15162 (org-eval-in-calendar '(scroll-calendar-right 1))))
15163 (org-defkey minibuffer-local-map "\C-v"
15164 (lambda () (interactive)
15165 (org-eval-in-calendar
15166 '(calendar-scroll-left-three-months 1))))
15167 (org-defkey minibuffer-local-map "\M-v"
15168 (lambda () (interactive)
15169 (org-eval-in-calendar
15170 '(calendar-scroll-right-three-months 1))))
15171 (run-hooks 'org-read-date-minibuffer-setup-hook)
15174 (use-local-map map)
15175 (setq org-read-date-inactive inactive)
15176 (add-hook 'post-command-hook 'org-read-date-display)
15177 (setq org-ans0 (read-string prompt default-input
15178 'org-read-date-history nil))
15179 ;; org-ans0: from prompt
15180 ;; org-ans1: from mouse click
15181 ;; org-ans2: from calendar motion
15182 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15183 (remove-hook 'post-command-hook 'org-read-date-display)
15184 (use-local-map old-map)
15185 (when org-read-date-overlay
15186 (delete-overlay org-read-date-overlay)
15187 (setq org-read-date-overlay nil)))))
15188 (bury-buffer "*Calendar*")))))
15190 (t ; Naked prompt only
15192 (setq ans (read-string prompt default-input
15193 'org-read-date-history timestr))
15194 (when org-read-date-overlay
15195 (delete-overlay org-read-date-overlay)
15196 (setq org-read-date-overlay nil)))))
15198 (setq final (org-read-date-analyze ans org-def org-defdecode))
15200 (when org-read-date-analyze-forced-year
15201 (message "Year was forced into %s"
15202 (if org-read-date-force-compatible-dates
15203 "compatible range (1970-2037)"
15204 "range representable on this machine"))
15207 ;; One round trip to get rid of 34th of August and stuff like that....
15208 (setq final (decode-time (apply 'encode-time final)))
15210 (setq org-read-date-final-answer ans)
15213 (apply 'encode-time final)
15214 (if (and (boundp 'org-time-was-given) org-time-was-given)
15215 (format "%04d-%02d-%02d %02d:%02d"
15216 (nth 5 final) (nth 4 final) (nth 3 final)
15217 (nth 2 final) (nth 1 final))
15218 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15221 (defvar org-defdecode)
15222 (defvar org-with-time)
15223 (defun org-read-date-display ()
15224 "Display the current date prompt interpretation in the minibuffer."
15225 (when org-read-date-display-live
15226 (when org-read-date-overlay
15227 (delete-overlay org-read-date-overlay))
15230 (while (not (equal (buffer-substring
15231 (max (point-min) (- (point) 4)) (point))
15235 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15236 " " (or org-ans1 org-ans2)))
15237 (org-end-time-was-given nil)
15238 (f (org-read-date-analyze ans org-def org-defdecode))
15240 org-time-stamp-custom-formats
15241 org-time-stamp-formats))
15242 (fmt (if (or org-with-time
15243 (and (boundp 'org-time-was-given) org-time-was-given))
15246 (txt (format-time-string fmt (apply 'encode-time f)))
15247 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15248 (txt (concat "=> " txt)))
15249 (when (and org-end-time-was-given
15250 (string-match org-plain-time-of-day-regexp txt))
15251 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15252 org-end-time-was-given
15253 (substring txt (match-end 0)))))
15254 (when org-read-date-analyze-futurep
15255 (setq txt (concat txt " (=>F)")))
15256 (setq org-read-date-overlay
15257 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15258 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
15260 (defun org-read-date-analyze (ans org-def org-defdecode)
15261 "Analyze the combined answer of the date prompt."
15262 ;; FIXME: cleanup and comment
15263 (let ((nowdecode (decode-time (current-time)))
15264 delta deltan deltaw deltadef year month day
15265 hour minute second wday pm h2 m2 tl wday1
15266 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15267 (setq org-read-date-analyze-futurep nil
15268 org-read-date-analyze-forced-year nil)
15269 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15272 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15273 (setq ans (replace-match "" t t ans)
15275 deltaw (nth 1 delta)
15276 deltadef (nth 2 delta)))
15278 ;; Check if there is an iso week date in there
15279 ;; If yes, store the info and postpone interpreting it until the rest
15280 ;; of the parsing is done
15281 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15282 (setq iso-year (if (match-end 1)
15283 (org-small-year-to-year
15284 (string-to-number (match-string 1 ans))))
15285 iso-weekday (if (match-end 3)
15286 (string-to-number (match-string 3 ans)))
15287 iso-week (string-to-number (match-string 2 ans)))
15288 (setq ans (replace-match "" t t ans)))
15290 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15291 (when (string-match
15292 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15293 (setq year (if (match-end 2)
15294 (string-to-number (match-string 2 ans))
15295 (progn (setq kill-year t)
15296 (string-to-number (format-time-string "%Y"))))
15297 month (string-to-number (match-string 3 ans))
15298 day (string-to-number (match-string 4 ans)))
15299 (if (< year 100) (setq year (+ 2000 year)))
15300 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15303 ;; Help matching dotted european dates
15304 (when (string-match
15305 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
15306 (setq year (if (match-end 3)
15307 (string-to-number (match-string 3 ans))
15308 (progn (setq kill-year t)
15309 (string-to-number (format-time-string "%Y"))))
15310 day (string-to-number (match-string 1 ans))
15311 month (string-to-number (match-string 2 ans))
15312 ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15315 ;; Help matching american dates, like 5/30 or 5/30/7
15316 (when (string-match
15317 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15318 (setq year (if (match-end 4)
15319 (string-to-number (match-string 4 ans))
15320 (progn (setq kill-year t)
15321 (string-to-number (format-time-string "%Y"))))
15322 month (string-to-number (match-string 1 ans))
15323 day (string-to-number (match-string 2 ans)))
15324 (if (< year 100) (setq year (+ 2000 year)))
15325 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15327 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15328 ;; If there is a time with am/pm, and *no* time without it, we convert
15329 ;; so that matching will be successful.
15330 (loop for i from 1 to 2 do ; twice, for end time as well
15331 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15332 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15333 (setq hour (string-to-number (match-string 1 ans))
15334 minute (if (match-end 3)
15335 (string-to-number (match-string 3 ans))
15338 (string-to-char (downcase (match-string 4 ans)))))
15339 (if (and (= hour 12) (not pm))
15341 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15342 (setq ans (replace-match (format "%02d:%02d" hour minute)
15345 ;; Check if a time range is given as a duration
15346 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15347 (setq hour (string-to-number (match-string 1 ans))
15348 h2 (+ hour (string-to-number (match-string 3 ans)))
15349 minute (string-to-number (match-string 2 ans))
15350 m2 (+ minute (if (match-end 5) (string-to-number
15351 (match-string 5 ans))0)))
15352 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15353 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15356 ;; Check if there is a time range
15357 (when (boundp 'org-end-time-was-given)
15358 (setq org-time-was-given nil)
15359 (when (and (string-match org-plain-time-of-day-regexp ans)
15361 (setq org-end-time-was-given (match-string 8 ans))
15362 (setq ans (concat (substring ans 0 (match-beginning 7))
15363 (substring ans (match-end 7))))))
15365 (setq tl (parse-time-string ans)
15366 day (or (nth 3 tl) (nth 3 org-defdecode))
15367 month (or (nth 4 tl)
15368 (if (and org-read-date-prefer-future
15369 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15370 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15371 (nth 4 org-defdecode)))
15372 year (or (and (not kill-year) (nth 5 tl))
15373 (if (and org-read-date-prefer-future
15374 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15375 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15376 (nth 5 org-defdecode)))
15377 hour (or (nth 2 tl) (nth 2 org-defdecode))
15378 minute (or (nth 1 tl) (nth 1 org-defdecode))
15379 second (or (nth 0 tl) 0)
15382 (when (and (eq org-read-date-prefer-future 'time)
15383 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15384 (equal day (nth 3 nowdecode))
15385 (equal month (nth 4 nowdecode))
15386 (equal year (nth 5 nowdecode))
15388 (or (< (nth 2 tl) (nth 2 nowdecode))
15389 (and (= (nth 2 tl) (nth 2 nowdecode))
15391 (< (nth 1 tl) (nth 1 nowdecode)))))
15395 ;; Special date definitions below
15398 ;; There was an iso week
15401 (setq year (or iso-year year)
15402 day (or iso-weekday wday 1)
15403 wday nil ; to make sure that the trigger below does not match
15404 iso-date (calendar-gregorian-from-absolute
15405 (calendar-absolute-from-iso
15406 (list iso-week day year))))
15407 ; FIXME: Should we also push ISO weeks into the future?
15408 ; (when (and org-read-date-prefer-future
15410 ; (< (calendar-absolute-from-gregorian iso-date)
15411 ; (time-to-days (current-time))))
15412 ; (setq year (1+ year)
15413 ; iso-date (calendar-gregorian-from-absolute
15414 ; (calendar-absolute-from-iso
15415 ; (list iso-week day year)))))
15416 (setq month (car iso-date)
15417 year (nth 2 iso-date)
15418 day (nth 1 iso-date)))
15422 (let ((now (decode-time (current-time))))
15423 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15424 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15425 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15426 ((equal deltaw "m") (setq month (+ month deltan)))
15427 ((equal deltaw "y") (setq year (+ year deltan)))))
15428 ((and wday (not (nth 3 tl)))
15430 ;; Weekday was given, but no day, so pick that day in the week
15431 ;; on or after the derived date.
15432 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15433 (unless (equal wday wday1)
15434 (setq day (+ day (% (- wday wday1 -7) 7))))))
15435 (if (and (boundp 'org-time-was-given)
15437 (setq org-time-was-given t))
15438 (if (< year 100) (setq year (+ 2000 year)))
15439 ;; Check of the date is representable
15440 (if org-read-date-force-compatible-dates
15443 (setq year 1970 org-read-date-analyze-forced-year t))
15445 (setq year 2037 org-read-date-analyze-forced-year t)))
15446 (condition-case nil
15447 (ignore (encode-time second minute hour day month year))
15449 (setq year (nth 5 org-defdecode))
15450 (setq org-read-date-analyze-forced-year t))))
15451 (setq org-read-date-analyze-futurep futurep)
15452 (list second minute hour day month year)))
15454 (defvar parse-time-weekdays)
15455 (defun org-read-date-get-relative (s today default)
15456 "Check string S for special relative date string.
15457 TODAY and DEFAULT are internal times, for today and for a default.
15458 Return shift list (N what def-flag)
15459 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15460 N is the number of WHATs to shift.
15461 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15462 the DEFAULT date rather than TODAY."
15463 (require 'parse-time)
15467 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15469 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15470 "\\([ \t]\\|$\\)") s)
15471 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15472 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15473 (string-to-char (substring (match-string 1 s) -1))
15475 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15476 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15477 (what (if (match-end 3) (match-string 3 s) "d"))
15478 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15479 (date (if rel default today))
15480 (wday (nth 6 (decode-time date)))
15484 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15485 (if (= dir ?-) (setq delta (- delta 7)))
15486 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15487 (list delta "d" rel))
15488 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15490 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15491 "Turn a user-specified date into the internal representation.
15492 The internal representation needed by the calendar is (month day year).
15493 This is a wrapper to handle the brain-dead convention in calendar that
15494 user function argument order change dependent on argument order."
15495 (if (boundp 'calendar-date-style)
15497 ((eq calendar-date-style 'american)
15498 (list arg1 arg2 arg3))
15499 ((eq calendar-date-style 'european)
15500 (list arg2 arg1 arg3))
15501 ((eq calendar-date-style 'iso)
15502 (list arg2 arg3 arg1)))
15503 (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15504 (if (org-bound-and-true-p european-calendar-style)
15505 (list arg2 arg1 arg3)
15506 (list arg1 arg2 arg3)))))
15508 (defun org-eval-in-calendar (form &optional keepdate)
15509 "Eval FORM in the calendar window and return to current window.
15510 Also, store the cursor date in variable org-ans2."
15511 (let ((sf (selected-frame))
15512 (sw (selected-window)))
15513 (select-window (get-buffer-window "*Calendar*" t))
15515 (when (and (not keepdate) (calendar-cursor-to-date))
15516 (let* ((date (calendar-cursor-to-date))
15517 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15518 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15519 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15521 (org-select-frame-set-input-focus sf)))
15523 (defun org-calendar-select ()
15524 "Return to `org-read-date' with the date currently selected.
15525 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15527 (when (calendar-cursor-to-date)
15528 (let* ((date (calendar-cursor-to-date))
15529 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15530 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15531 (if (active-minibuffer-window) (exit-minibuffer))))
15533 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15534 "Insert a date stamp for the date given by the internal TIME.
15535 WITH-HM means use the stamp format that includes the time of the day.
15536 INACTIVE means use square brackets instead of angular ones, so that the
15537 stamp will not contribute to the agenda.
15538 PRE and POST are optional strings to be inserted before and after the
15540 The command returns the inserted time stamp."
15541 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15543 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15544 (insert-before-markers (or pre ""))
15545 (when (listp extra)
15546 (setq extra (car extra))
15547 (if (and (stringp extra)
15548 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15549 (setq extra (format "-%02d:%02d"
15550 (string-to-number (match-string 1 extra))
15551 (string-to-number (match-string 2 extra))))
15554 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15555 (insert-before-markers (setq stamp (format-time-string fmt time)))
15556 (insert-before-markers (or post ""))
15557 (setq org-last-inserted-timestamp stamp)))
15559 (defun org-toggle-time-stamp-overlays ()
15560 "Toggle the use of custom time stamp formats."
15562 (setq org-display-custom-times (not org-display-custom-times))
15563 (unless org-display-custom-times
15564 (let ((p (point-min)) (bmp (buffer-modified-p)))
15565 (while (setq p (next-single-property-change p 'display))
15566 (if (and (get-text-property p 'display)
15567 (eq (get-text-property p 'face) 'org-date))
15568 (remove-text-properties
15569 p (setq p (next-single-property-change p 'display))
15571 (set-buffer-modified-p bmp)))
15572 (if (featurep 'xemacs)
15573 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15574 (org-restart-font-lock)
15575 (setq org-table-may-need-update t)
15576 (if org-display-custom-times
15577 (message "Time stamps are overlaid with custom format")
15578 (message "Time stamp overlays removed")))
15580 (defun org-display-custom-time (beg end)
15581 "Overlay modified time stamp format over timestamp between BEG and END."
15582 (let* ((ts (buffer-substring beg end))
15583 t1 w1 with-hm tf time str w2 (off 0))
15585 (setq t1 (org-parse-time-string ts t))
15586 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
15587 (setq off (- (match-end 0) (match-beginning 0)))))
15588 (setq end (- end off))
15589 (setq w1 (- end beg)
15590 with-hm (and (nth 1 t1) (nth 2 t1))
15591 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15592 time (org-fix-decoded-time t1)
15594 (format-time-string
15595 (substring tf 1 -1) (apply 'encode-time time))
15596 nil 'mouse-face 'highlight)
15598 (if (not (= w2 w1))
15599 (add-text-properties (1+ beg) (+ 2 beg)
15600 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15601 (if (featurep 'xemacs)
15603 (put-text-property beg end 'invisible t)
15604 (put-text-property beg end 'end-glyph (make-glyph str)))
15605 (put-text-property beg end 'display str))))
15607 (defun org-translate-time (string)
15608 "Translate all timestamps in STRING to custom format.
15609 But do this only if the variable `org-display-custom-times' is set."
15610 (when org-display-custom-times
15613 (re org-ts-regexp-both)
15614 t1 with-hm inactive tf time str beg end)
15615 (while (setq start (string-match re string start))
15616 (setq beg (match-beginning 0)
15618 t1 (save-match-data
15619 (org-parse-time-string (substring string beg end) t))
15620 with-hm (and (nth 1 t1) (nth 2 t1))
15621 inactive (equal (substring string beg (1+ beg)) "[")
15622 tf (funcall (if with-hm 'cdr 'car)
15623 org-time-stamp-custom-formats)
15624 time (org-fix-decoded-time t1)
15625 str (format-time-string
15627 (if inactive "[" "<") (substring tf 1 -1)
15628 (if inactive "]" ">"))
15629 (apply 'encode-time time))
15630 string (replace-match str t t string)
15631 start (+ start (length str)))))))
15634 (defun org-fix-decoded-time (time)
15635 "Set 0 instead of nil for the first 6 elements of time.
15636 Don't touch the rest."
15638 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15640 (defun org-days-to-time (timestamp-string)
15641 "Difference between TIMESTAMP-STRING and now in days."
15642 (- (time-to-days (org-time-string-to-time timestamp-string))
15643 (time-to-days (current-time))))
15645 (defun org-deadline-close (timestamp-string &optional ndays)
15646 "Is the time in TIMESTAMP-STRING close to the current date?"
15647 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15648 (and (< (org-days-to-time timestamp-string) ndays)
15649 (not (org-entry-is-done-p))))
15651 (defun org-get-wdays (ts)
15652 "Get the deadline lead time appropriate for timestring TS."
15654 ((<= org-deadline-warning-days 0)
15655 ;; 0 or negative, enforce this value no matter what
15656 (- org-deadline-warning-days))
15657 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
15658 ;; lead time is specified.
15659 (floor (* (string-to-number (match-string 1 ts))
15660 (cdr (assoc (match-string 2 ts)
15661 '(("d" . 1) ("w" . 7)
15662 ("m" . 30.4) ("y" . 365.25)))))))
15663 ;; go for the default.
15664 (t org-deadline-warning-days)))
15666 (defun org-calendar-select-mouse (ev)
15667 "Return to `org-read-date' with the date currently selected.
15668 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15670 (mouse-set-point ev)
15671 (when (calendar-cursor-to-date)
15672 (let* ((date (calendar-cursor-to-date))
15673 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15674 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15675 (if (active-minibuffer-window) (exit-minibuffer))))
15677 (defun org-check-deadlines (ndays)
15678 "Check if there are any deadlines due or past due.
15679 A deadline is considered due if it happens within `org-deadline-warning-days'
15680 days from today's date. If the deadline appears in an entry marked DONE,
15681 it is not shown. The prefix arg NDAYS can be used to test that many
15682 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15684 (let* ((org-warn-days
15686 ((equal ndays '(4)) 100000)
15687 (ndays (prefix-numeric-value ndays))
15688 (t (abs org-deadline-warning-days))))
15689 (case-fold-search nil)
15690 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15692 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15694 (message "%d deadlines past-due or due within %d days"
15695 (org-occur regexp nil callback)
15698 (defun org-check-before-date (date)
15699 "Check if there are deadlines or scheduled entries before DATE."
15700 (interactive (list (org-read-date)))
15701 (let ((case-fold-search nil)
15702 (regexp (concat "\\<\\(" org-deadline-string
15703 "\\|" org-scheduled-string
15704 "\\) *<\\([^>]+\\)>"))
15706 (lambda () (time-less-p
15707 (org-time-string-to-time (match-string 2))
15708 (org-time-string-to-time date)))))
15709 (message "%d entries before %s"
15710 (org-occur regexp nil callback) date)))
15712 (defun org-check-after-date (date)
15713 "Check if there are deadlines or scheduled entries after DATE."
15714 (interactive (list (org-read-date)))
15715 (let ((case-fold-search nil)
15716 (regexp (concat "\\<\\(" org-deadline-string
15717 "\\|" org-scheduled-string
15718 "\\) *<\\([^>]+\\)>"))
15722 (org-time-string-to-time (match-string 2))
15723 (org-time-string-to-time date))))))
15724 (message "%d entries after %s"
15725 (org-occur regexp nil callback) date)))
15727 (defun org-check-dates-range (start-date end-date)
15728 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
15729 (interactive (list (org-read-date nil nil nil "Range starts")
15730 (org-read-date nil nil nil "Range end")))
15731 (let ((case-fold-search nil)
15732 (regexp (concat "\\<\\(" org-deadline-string
15733 "\\|" org-scheduled-string
15734 "\\) *<\\([^>]+\\)>"))
15737 (let ((match (match-string 2)))
15740 (org-time-string-to-time match)
15741 (org-time-string-to-time start-date)))
15743 (org-time-string-to-time match)
15744 (org-time-string-to-time end-date)))))))
15745 (message "%d entries between %s and %s"
15746 (org-occur regexp nil callback) start-date end-date)))
15748 (defun org-evaluate-time-range (&optional to-buffer)
15749 "Evaluate a time range by computing the difference between start and end.
15750 Normally the result is just printed in the echo area, but with prefix arg
15751 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
15752 If the time range is actually in a table, the result is inserted into the
15754 For time difference computation, a year is assumed to be exactly 365
15755 days in order to avoid rounding problems."
15758 (org-clock-update-time-maybe)
15760 (unless (org-at-date-range-p t)
15761 (goto-char (point-at-bol))
15762 (re-search-forward org-tr-regexp-both (point-at-eol) t))
15763 (if (not (org-at-date-range-p t))
15764 (error "Not at a time-stamp range, and none found in current line")))
15765 (let* ((ts1 (match-string 1))
15766 (ts2 (match-string 2))
15767 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
15768 (match-end (match-end 0))
15769 (time1 (org-time-string-to-time ts1))
15770 (time2 (org-time-string-to-time ts2))
15771 (t1 (org-float-time time1))
15772 (t2 (org-float-time time2))
15773 (diff (abs (- t2 t1)))
15774 (negative (< (- t2 t1) 0))
15775 ;; (ys (floor (* 365 24 60 60)))
15778 (fy "%dy %dd %02d:%02d")
15780 (fd "%dd %02d:%02d")
15785 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15787 d (floor (/ diff ds)) diff (mod diff ds)
15788 h (floor (/ diff hs)) diff (mod diff hs)
15789 m (floor (/ diff 60)))
15790 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15792 d (floor (+ (/ diff ds) 0.5))
15794 (if (not to-buffer)
15795 (message "%s" (org-make-tdiff-string y d h m))
15796 (if (org-at-table-p)
15798 (goto-char match-end)
15800 (and (looking-at " *|") (goto-char (match-end 0))))
15801 (goto-char match-end))
15803 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
15804 (replace-match ""))
15805 (if negative (insert " -"))
15806 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
15807 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
15808 (insert " " (format fh h m))))
15809 (if align (org-table-align))
15810 (message "Time difference inserted")))))
15812 (defun org-make-tdiff-string (y d h m)
15815 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
15817 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
15819 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
15821 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
15823 (apply 'format fmt (nreverse l))))
15825 (defun org-time-string-to-time (s &optional buffer pos)
15826 (condition-case errdata
15827 (apply 'encode-time (org-parse-time-string s))
15828 (error (error "Bad timestamp `%s'%s\nError was: %s"
15829 s (if (not (and buffer pos))
15831 (format " at %d in buffer `%s'" pos buffer))
15834 (defun org-time-string-to-seconds (s)
15835 (org-float-time (org-time-string-to-time s)))
15837 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
15838 "Convert a time stamp to an absolute day number.
15839 If there is a specifier for a cyclic time stamp, get the closest date to
15841 PREFER and SHOW-ALL are passed through to `org-closest-date'.
15842 The variable date is bound by the calendar when this is called."
15844 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
15845 (if (org-diary-sexp-entry (match-string 1 s) "" date)
15848 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
15849 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
15850 (time-to-days (current-time))) (match-string 0 s)
15853 (condition-case errdata
15854 (apply 'encode-time (org-parse-time-string s))
15855 (error (error "Bad timestamp `%s'%s\nError was: %s"
15856 s (if (not (and buffer pos))
15858 (format " at %d in buffer `%s'" pos buffer))
15859 (cdr errdata))))))))
15861 (defun org-days-to-iso-week (days)
15862 "Return the iso week number."
15864 (car (calendar-iso-from-absolute days)))
15866 (defun org-small-year-to-year (year)
15867 "Convert 2-digit years into 4-digit years.
15868 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
15869 The year 2000 cannot be abbreviated. Any year larger than 99
15870 is returned unchanged."
15872 (setq year (+ 2000 year))
15874 (setq year (+ 1900 year))))
15877 (defun org-time-from-absolute (d)
15878 "Return the time corresponding to date D.
15879 D may be an absolute day number, or a calendar-type list (month day year)."
15880 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
15881 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
15883 (defun org-calendar-holiday ()
15884 "List of holidays, for Diary display in Org-mode."
15885 (require 'holidays)
15887 (if (fboundp 'calendar-check-holidays)
15888 'calendar-check-holidays 'check-calendar-holidays) date)))
15889 (if hl (mapconcat 'identity hl "; "))))
15891 (defun org-diary-sexp-entry (sexp entry date)
15892 "Process a SEXP diary ENTRY for DATE."
15893 (require 'diary-lib)
15894 (let ((result (if calendar-debug-sexp
15895 (let ((stack-trace-on-error t))
15896 (eval (car (read-from-string sexp))))
15897 (condition-case nil
15898 (eval (car (read-from-string sexp)))
15901 (message "Bad sexp at line %d in %s: %s"
15903 (buffer-file-name) sexp)
15905 (cond ((stringp result) (split-string result "; "))
15906 ((and (consp result)
15907 (not (consp (cdr result)))
15908 (stringp (cdr result))) (cdr result))
15909 ((and (consp result)
15910 (stringp (car result))) result)
15914 (defun org-diary-to-ical-string (frombuf)
15915 "Get iCalendar entries from diary entries in buffer FROMBUF.
15916 This uses the icalendar.el library."
15917 (let* ((tmpdir (if (featurep 'xemacs)
15919 temporary-file-directory))
15920 (tmpfile (make-temp-name
15921 (expand-file-name "orgics" tmpdir)))
15923 (with-current-buffer frombuf
15924 (icalendar-export-region (point-min) (point-max) tmpfile)
15925 (setq buf (find-buffer-visiting tmpfile))
15927 (goto-char (point-min))
15928 (if (re-search-forward "^BEGIN:VEVENT" nil t)
15929 (setq b (match-beginning 0)))
15930 (goto-char (point-max))
15931 (if (re-search-backward "^END:VEVENT" nil t)
15932 (setq e (match-end 0)))
15933 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
15935 (delete-file tmpfile)
15938 (defun org-closest-date (start current change prefer show-all)
15939 "Find the date closest to CURRENT that is consistent with START and CHANGE.
15940 When PREFER is `past', return a date that is either CURRENT or past.
15941 When PREFER is `future', return a date that is either CURRENT or future.
15942 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
15943 ;; Make the proper lists from the dates
15945 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
15946 dn dw sday cday n1 n2 n0
15947 d m y y1 y2 date1 date2 nmonths nm ny m2)
15949 (setq start (org-date-to-gregorian start)
15950 current (org-date-to-gregorian
15953 (time-to-days (current-time))))
15954 sday (calendar-absolute-from-gregorian start)
15955 cday (calendar-absolute-from-gregorian current))
15957 (if (<= cday sday) (throw 'exit sday))
15959 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
15960 (setq dn (string-to-number (match-string 1 change))
15961 dw (cdr (assoc (match-string 2 change) a1)))
15962 (error "Invalid change specifier: %s" change))
15963 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
15966 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
15969 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
15970 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
15971 (setq date1 (list m d y1)
15972 n1 (calendar-absolute-from-gregorian date1)
15973 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
15974 n2 (calendar-absolute-from-gregorian date2)))
15976 ;; approx number of month between the two dates
15977 (setq nmonths (floor (/ (- cday sday) 30.436875)))
15978 ;; How often does dn fit in there?
15979 (setq d (nth 1 start) m (car start) y (nth 2 start)
15980 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
15982 ny (floor (/ m 12))
15985 (while (> m 12) (setq m (- m 12) y (1+ y)))
15986 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
15987 (setq m2 (+ m dn) y2 y)
15988 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15989 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
15990 (while (<= n2 cday)
15991 (setq n1 n2 m m2 y y2)
15992 (setq m2 (+ m dn) y2 y)
15993 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15994 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
15995 ;; Make sure n1 is the earlier date
15996 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
15999 ((eq prefer 'past) (if (= cday n2) n2 n1))
16000 ((eq prefer 'future) (if (= cday n1) n1 n2))
16001 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16003 ((eq prefer 'past) (if (= cday n2) n2 n1))
16004 ((eq prefer 'future) (if (= cday n1) n1 n2))
16005 (t (if (= cday n1) n1 n2)))))))
16007 (defun org-date-to-gregorian (date)
16008 "Turn any specification of DATE into a Gregorian date for the calendar."
16009 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16010 ((and (listp date) (= (length date) 3)) date)
16012 (setq date (org-parse-time-string date))
16013 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16015 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16017 (defun org-parse-time-string (s &optional nodefault)
16018 "Parse the standard Org-mode time string.
16019 This should be a lot faster than the normal `parse-time-string'.
16020 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16021 hour and minute fields will be nil if not given."
16022 (if (string-match org-ts-regexp0 s)
16024 (if (or (match-beginning 8) (not nodefault))
16025 (string-to-number (or (match-string 8 s) "0")))
16026 (if (or (match-beginning 7) (not nodefault))
16027 (string-to-number (or (match-string 7 s) "0")))
16028 (string-to-number (match-string 4 s))
16029 (string-to-number (match-string 3 s))
16030 (string-to-number (match-string 2 s))
16032 (error "Not a standard Org-mode time string: %s" s)))
16034 (defun org-timestamp-up (&optional arg)
16035 "Increase the date item at the cursor by one.
16036 If the cursor is on the year, change the year. If it is on the month,
16037 the day or the time, change that.
16038 With prefix ARG, change by that many units."
16040 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16042 (defun org-timestamp-down (&optional arg)
16043 "Decrease the date item at the cursor by one.
16044 If the cursor is on the year, change the year. If it is on the month,
16045 the day or the time, change that.
16046 With prefix ARG, change by that many units."
16048 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16050 (defun org-timestamp-up-day (&optional arg)
16051 "Increase the date in the time stamp by one day.
16052 With prefix ARG, change that many days."
16054 (if (and (not (org-at-timestamp-p t))
16055 (org-at-heading-p))
16057 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16059 (defun org-timestamp-down-day (&optional arg)
16060 "Decrease the date in the time stamp by one day.
16061 With prefix ARG, change that many days."
16063 (if (and (not (org-at-timestamp-p t))
16064 (org-at-heading-p))
16066 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16068 (defun org-at-timestamp-p (&optional inactive-ok)
16069 "Determine if the cursor is in or at a timestamp."
16071 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16073 (ans (or (looking-at tsr)
16075 (skip-chars-backward "^[<\n\r\t")
16076 (if (> (point) (point-min)) (backward-char 1))
16077 (and (looking-at tsr)
16078 (> (- (match-end 0) pos) -1))))))
16080 (boundp 'org-ts-what)
16083 ((= pos (match-beginning 0)) 'bracket)
16084 ;; Point is considered to be "on the bracket" whether
16085 ;; it's really on it or right after it.
16086 ((or (= pos (1- (match-end 0)))
16087 (= pos (match-end 0))) 'bracket)
16088 ((org-pos-in-match-range pos 2) 'year)
16089 ((org-pos-in-match-range pos 3) 'month)
16090 ((org-pos-in-match-range pos 7) 'hour)
16091 ((org-pos-in-match-range pos 8) 'minute)
16092 ((or (org-pos-in-match-range pos 4)
16093 (org-pos-in-match-range pos 5)) 'day)
16094 ((and (> pos (or (match-end 8) (match-end 5)))
16095 (< pos (match-end 0)))
16096 (- pos (or (match-end 8) (match-end 5))))
16100 (defun org-toggle-timestamp-type ()
16101 "Toggle the type (<active> or [inactive]) of a time stamp."
16103 (when (org-at-timestamp-p t)
16104 (let ((beg (match-beginning 0)) (end (match-end 0))
16105 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16108 (while (re-search-forward "[][<>]" end t)
16109 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16111 (message "Timestamp is now %sactive"
16112 (if (equal (char-after beg) ?<) "" "in")))))
16114 (defun org-timestamp-change (n &optional what updown)
16115 "Change the date in the time stamp at point.
16116 The date will be changed by N times WHAT. WHAT can be `day', `month',
16117 `year', `minute', `second'. If WHAT is not given, the cursor position
16118 in the timestamp determines what will be changed."
16119 (let ((origin (point)) origin-cat
16121 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16125 (if (not (org-at-timestamp-p t))
16126 (error "Not at a timestamp"))
16127 (if (and (not what) (eq org-ts-what 'bracket))
16128 (org-toggle-timestamp-type)
16129 ;; Point isn't on brackets. Remember the part of the time-stamp
16130 ;; the point was in. Indeed, size of time-stamps may change,
16131 ;; but point must be kept in the same category nonetheless.
16132 (setq origin-cat org-ts-what)
16133 (if (and (not what) (not (eq org-ts-what 'day))
16134 org-display-custom-times
16135 (get-text-property (point) 'display)
16136 (not (get-text-property (1- (point)) 'display)))
16137 (setq org-ts-what 'day))
16138 (setq org-ts-what (or what org-ts-what)
16139 inactive (= (char-after (match-beginning 0)) ?\[)
16140 ts (match-string 0))
16143 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
16145 (setq extra (match-string 1 ts)))
16146 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16148 (setq time0 (org-parse-time-string ts))
16150 (eq org-ts-what 'minute)
16151 (not current-prefix-arg))
16152 ;; This looks like s-up and s-down. Change by one rounding step.
16153 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16154 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16155 (setcar (cdr time0) (+ (nth 1 time0)
16156 (if (> n 0) (- rem) (- dm rem))))))
16158 (encode-time (or (car time0) 0)
16159 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16160 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16161 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16162 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16163 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16165 (when (and (member org-ts-what '(hour minute))
16167 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16168 (setq extra (org-modify-ts-extra
16170 (if (eq org-ts-what 'hour) 2 5)
16172 (when (integerp org-ts-what)
16173 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16174 (if (eq what 'calendar)
16175 (let ((cal-date (org-get-date-from-calendar)))
16176 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16177 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16178 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16179 (setcar time0 (or (car time0) 0))
16180 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16181 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16182 (setq time (apply 'encode-time time0))))
16183 ;; Insert the new time-stamp, and ensure point stays in the same
16184 ;; category as before (i.e. not after the last position in that
16186 (let ((pos (point)))
16187 ;; Stay before inserted string. `save-excursion' is of no use.
16188 (setq org-last-changed-timestamp
16189 (org-insert-time-stamp time with-hm inactive nil nil extra))
16192 (looking-at org-ts-regexp3)
16194 ;; `day' category ends before `hour' if any, or at
16195 ;; the end of the day name.
16196 ((eq origin-cat 'day)
16197 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16198 ((eq origin-cat 'hour) (min (match-end 7) origin))
16199 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16200 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16201 ;; `year' and `month' have both fixed size: point
16202 ;; couldn't have moved into another part.
16204 ;; Update clock if on a CLOCK line.
16205 (org-clock-update-time-maybe)
16206 ;; Try to recenter the calendar window, if any.
16207 (if (and org-calendar-follow-timestamp-change
16208 (get-buffer-window "*Calendar*" t)
16209 (memq org-ts-what '(day month year)))
16210 (org-recenter-calendar (time-to-days time))))))
16212 (defun org-modify-ts-extra (s pos n dm)
16213 "Change the different parts of the lead-time and repeat fields in timestamp."
16214 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16216 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16218 ((or (org-pos-in-match-range pos 2)
16219 (org-pos-in-match-range pos 3))
16220 (setq m (string-to-number (match-string 3 s))
16221 h (string-to-number (match-string 2 s)))
16222 (if (org-pos-in-match-range pos 2)
16224 (setq n (* dm (org-no-warnings (signum n))))
16225 (when (not (= 0 (setq rem (% m dm))))
16226 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16228 (if (< m 0) (setq m (+ m 60) h (1- h)))
16229 (if (> m 59) (setq m (- m 60) h (1+ h)))
16230 (setq h (min 24 (max 0 h)))
16231 (setq ng 1 new (format "-%02d:%02d" h m)))
16232 ((org-pos-in-match-range pos 6)
16233 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16234 ((org-pos-in-match-range pos 5)
16235 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16237 ((org-pos-in-match-range pos 9)
16238 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16239 ((org-pos-in-match-range pos 8)
16240 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16244 (substring s 0 (match-beginning ng))
16246 (substring s (match-end ng))))))
16249 (defun org-recenter-calendar (date)
16250 "If the calendar is visible, recenter it to DATE."
16251 (let ((cwin (get-buffer-window "*Calendar*" t)))
16253 (let ((calendar-move-hook nil))
16254 (with-selected-window cwin
16255 (calendar-goto-date (if (listp date) date
16256 (calendar-gregorian-from-absolute date))))))))
16258 (defun org-goto-calendar (&optional arg)
16259 "Go to the Emacs calendar at the current date.
16260 If there is a time stamp in the current line, go to that date.
16261 A prefix ARG can be used to force the current date."
16263 (let ((tsr org-ts-regexp) diff
16264 (calendar-move-hook nil)
16265 (calendar-view-holidays-initially-flag nil)
16266 (calendar-view-diary-initially-flag nil))
16267 (if (or (org-at-timestamp-p)
16269 (beginning-of-line 1)
16270 (looking-at (concat ".*" tsr))))
16271 (let ((d1 (time-to-days (current-time)))
16273 (org-time-string-to-time (match-string 1)))))
16274 (setq diff (- d2 d1))))
16276 (calendar-goto-today)
16277 (if (and diff (not arg)) (calendar-forward-day diff))))
16279 (defun org-get-date-from-calendar ()
16280 "Return a list (month day year) of date at point in calendar."
16281 (with-current-buffer "*Calendar*"
16283 (calendar-cursor-to-date))))
16285 (defun org-date-from-calendar ()
16286 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16287 If there is already a time stamp at the cursor position, update it."
16289 (if (org-at-timestamp-p t)
16290 (org-timestamp-change 0 'calendar)
16291 (let ((cal-date (org-get-date-from-calendar)))
16292 (org-insert-time-stamp
16293 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16295 (defun org-minutes-to-hh:mm-string (m)
16296 "Compute H:MM from a number of minutes."
16297 (let ((h (/ m 60)))
16298 (setq m (- m (* 60 h)))
16299 (format org-time-clocksum-format h m)))
16301 (defun org-hh:mm-string-to-minutes (s)
16302 "Convert a string H:MM to a number of minutes.
16303 If the string is just a number, interpret it as minutes.
16304 In fact, the first hh:mm or number in the string will be taken,
16305 there can be extra stuff in the string.
16306 If no number is found, the return value is 0."
16309 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16310 (+ (* (string-to-number (match-string 1 s)) 60)
16311 (string-to-number (match-string 2 s))))
16312 ((string-match "\\([0-9]+\\)" s)
16313 (string-to-number (match-string 1 s)))
16316 (defcustom org-effort-durations
16319 ("w" . ,(* 60 8 5))
16320 ("m" . ,(* 60 8 5 4))
16321 ("y" . ,(* 60 8 5 40)))
16322 "Conversion factor to minutes for an effort modifier.
16324 Each entry has the form (MODIFIER . MINUTES).
16326 In an effort string, a number followed by MODIFIER is multiplied
16327 by the specified number of MINUTES to obtain an effort in
16330 For example, if the value of this variable is ((\"hours\" . 60)), then an
16331 effort string \"2hours\" is equivalent to 120 minutes."
16334 :type '(alist :key-type (string :tag "Modifier")
16335 :value-type (number :tag "Minutes")))
16337 (defun org-duration-string-to-minutes (s)
16338 "Convert a duration string S to minutes.
16340 A bare number is interpreted as minutes, modifiers can be set by
16341 customizing `org-effort-durations' (which see).
16343 Entries containing a colon are interpreted as H:MM by
16344 `org-hh:mm-string-to-minutes'."
16346 (re (concat "\\([0-9]+\\) *\\("
16347 (regexp-opt (mapcar 'car org-effort-durations))
16349 (while (string-match re s)
16350 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16351 (string-to-number (match-string 1 s))))
16352 (setq s (replace-match "" nil t s)))
16353 (incf result (org-hh:mm-string-to-minutes s))
16358 (defun org-save-all-org-buffers ()
16359 "Save all Org-mode buffers without user confirmation."
16361 (message "Saving all Org-mode buffers...")
16362 (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
16363 (when (featurep 'org-id) (org-id-locations-save))
16364 (message "Saving all Org-mode buffers... done"))
16366 (defun org-revert-all-org-buffers ()
16367 "Revert all Org-mode buffers.
16368 Prompt for confirmation when there are unsaved changes.
16369 Be sure you know what you are doing before letting this function
16370 overwrite your changes.
16372 This function is useful in a setup where one tracks org files
16373 with a version control system, to revert on one machine after pulling
16374 changes from another. I believe the procedure must be like this:
16376 1. M-x org-save-all-org-buffers
16377 2. Pull changes from the other machine, resolve conflicts
16378 3. M-x org-revert-all-org-buffers"
16380 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16383 (save-window-excursion
16386 (when (and (with-current-buffer b (eq major-mode 'org-mode))
16387 (with-current-buffer b buffer-file-name))
16388 (org-pop-to-buffer-same-window b)
16389 (revert-buffer t 'no-confirm)))
16391 (when (and (featurep 'org-id) org-id-track-globally)
16392 (org-id-locations-load)))))
16397 (defun org-switchb (&optional arg)
16398 "Switch between Org buffers.
16399 With one prefix argument, restrict available buffers to files.
16400 With two prefix arguments, restrict available buffers to agenda files.
16402 Defaults to `iswitchb' for buffer name completion.
16403 Set `org-completion-use-ido' to make it use ido instead."
16405 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16406 ((equal arg '(16)) (org-buffer-list 'agenda))
16407 (t (org-buffer-list))))
16408 (org-completion-use-iswitchb org-completion-use-iswitchb)
16409 (org-completion-use-ido org-completion-use-ido))
16410 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16411 (setq org-completion-use-iswitchb t))
16412 (org-pop-to-buffer-same-window
16413 (org-icompleting-read "Org buffer: "
16414 (mapcar 'list (mapcar 'buffer-name blist))
16417 ;;; Define some older names previously used for this functionality
16419 (defalias 'org-ido-switchb 'org-switchb)
16421 (defalias 'org-iswitchb 'org-switchb)
16423 (defun org-buffer-list (&optional predicate exclude-tmp)
16424 "Return a list of Org buffers.
16425 PREDICATE can be `export', `files' or `agenda'.
16427 export restrict the list to Export buffers.
16428 files restrict the list to buffers visiting Org files.
16429 agenda restrict the list to buffers visiting agenda files.
16431 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16433 (agenda-files (and (eq predicate 'agenda)
16434 (mapcar 'file-truename (org-agenda-files t))))
16437 ((eq predicate 'files)
16438 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
16439 ((eq predicate 'export)
16440 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16441 ((eq predicate 'agenda)
16443 (with-current-buffer b
16444 (and (eq major-mode 'org-mode)
16445 (setq bfn (buffer-file-name b))
16446 (member (file-truename bfn) agenda-files)))))
16447 (t (lambda (b) (with-current-buffer b
16448 (or (eq major-mode 'org-mode)
16449 (string-match "\*Org .*Export"
16450 (buffer-name b)))))))))
16454 (if (and (funcall filter b)
16455 (or (not exclude-tmp)
16456 (not (string-match "tmp" (buffer-name b)))))
16461 (defun org-agenda-files (&optional unrestricted archives)
16462 "Get the list of agenda files.
16463 Optional UNRESTRICTED means return the full list even if a restriction
16464 is currently in place.
16465 When ARCHIVES is t, include all archive files that are really being
16466 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16467 `org-agenda-archives-mode' is t."
16470 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16471 ((stringp org-agenda-files) (org-read-agenda-file-list))
16472 ((listp org-agenda-files) org-agenda-files)
16473 (t (error "Invalid value of `org-agenda-files'")))))
16474 (setq files (apply 'append
16475 (mapcar (lambda (f)
16476 (if (file-directory-p f)
16478 f t org-agenda-file-regexp)
16481 (when org-agenda-skip-unavailable-files
16482 (setq files (delq nil
16485 (and (file-readable-p file) file)))
16487 (when (or (eq archives t)
16488 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16489 (setq files (org-add-archive-files files)))
16492 (defun org-agenda-file-p (&optional file)
16493 "Return non-nil, if FILE is an agenda file.
16494 If FILE is omitted, use the file associated with the current
16496 (member (or file (buffer-file-name))
16497 (org-agenda-files t)))
16499 (defun org-edit-agenda-file-list ()
16500 "Edit the list of agenda files.
16501 Depending on setup, this either uses customize to edit the variable
16502 `org-agenda-files', or it visits the file that is holding the list. In the
16503 latter case, the buffer is set up in a way that saving it automatically kills
16504 the buffer and restores the previous window configuration."
16506 (if (stringp org-agenda-files)
16507 (let ((cw (current-window-configuration)))
16508 (find-file org-agenda-files)
16509 (org-set-local 'org-window-configuration cw)
16510 (org-add-hook 'after-save-hook
16512 (set-window-configuration
16513 (prog1 org-window-configuration
16514 (kill-buffer (current-buffer))))
16515 (org-install-agenda-files-menu)
16516 (message "New agenda file list installed"))
16518 (message "%s" (substitute-command-keys
16519 "Edit list and finish with \\[save-buffer]")))
16520 (customize-variable 'org-agenda-files)))
16522 (defun org-store-new-agenda-file-list (list)
16523 "Set new value for the agenda file list and save it correctly."
16524 (if (stringp org-agenda-files)
16525 (let ((fe (org-read-agenda-file-list t)) b u)
16526 (while (setq b (find-buffer-visiting org-agenda-files))
16528 (with-temp-file org-agenda-files
16531 (lambda (f) ;; Keep un-expanded entries.
16532 (if (setq u (assoc f fe))
16537 (let ((org-mode-hook nil) (org-inhibit-startup t)
16538 (org-insert-mode-line-in-empty-file nil))
16539 (setq org-agenda-files list)
16540 (customize-save-variable 'org-agenda-files org-agenda-files))))
16542 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16543 "Read the list of agenda files from a file.
16544 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16545 filenames, used by `org-store-new-agenda-file-list' to write back
16546 un-expanded file names."
16547 (when (file-directory-p org-agenda-files)
16548 (error "`org-agenda-files' cannot be a single directory"))
16549 (when (stringp org-agenda-files)
16551 (insert-file-contents org-agenda-files)
16554 (let ((e (expand-file-name (substitute-in-file-name f)
16556 (if pair-with-expansion
16559 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16562 (defun org-cycle-agenda-files ()
16563 "Cycle through the files in `org-agenda-files'.
16564 If the current buffer visits an agenda file, find the next one in the list.
16565 If the current buffer does not, find the first agenda file."
16567 (let* ((fs (org-agenda-files t))
16568 (files (append fs (list (car fs))))
16569 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16571 (unless files (error "No agenda files"))
16573 (while (setq file (pop files))
16574 (if (equal (file-truename file) tcf)
16576 (find-file (car files))
16578 (find-file (car fs)))
16579 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16581 (defun org-agenda-file-to-front (&optional to-end)
16582 "Move/add the current file to the top of the agenda file list.
16583 If the file is not present in the list, it is added to the front. If it is
16584 present, it is moved there. With optional argument TO-END, add/move to the
16587 (let ((org-agenda-skip-unavailable-files nil)
16588 (file-alist (mapcar (lambda (x)
16589 (cons (file-truename x) x))
16590 (org-agenda-files t)))
16591 (ctf (file-truename buffer-file-name))
16593 (setq x (assoc ctf file-alist) had x)
16595 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16597 (setq file-alist (append (delq x file-alist) (list x)))
16598 (setq file-alist (cons x (delq x file-alist))))
16599 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16600 (org-install-agenda-files-menu)
16601 (message "File %s to %s of agenda file list"
16602 (if had "moved" "added") (if to-end "end" "front"))))
16604 (defun org-remove-file (&optional file)
16605 "Remove current file from the list of files in variable `org-agenda-files'.
16606 These are the files which are being checked for agenda entries.
16607 Optional argument FILE means use this file instead of the current."
16609 (let* ((org-agenda-skip-unavailable-files nil)
16610 (file (or file buffer-file-name))
16611 (true-file (file-truename file))
16612 (afile (abbreviate-file-name file))
16613 (files (delq nil (mapcar
16615 (if (equal true-file
16618 (org-agenda-files t)))))
16619 (if (not (= (length files) (length (org-agenda-files t))))
16621 (org-store-new-agenda-file-list files)
16622 (org-install-agenda-files-menu)
16623 (message "Removed file: %s" afile))
16624 (message "File was not in list: %s (not removed)" afile))))
16626 (defun org-file-menu-entry (file)
16627 (vector file (list 'find-file file) t))
16629 (defun org-check-agenda-file (file)
16630 "Make sure FILE exists. If not, ask user what to do."
16631 (when (not (file-exists-p file))
16632 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
16633 (abbreviate-file-name file))
16634 (let ((r (downcase (read-char-exclusive))))
16637 (org-remove-file file)
16638 (throw 'nextfile t))
16639 (t (error "Abort"))))))
16641 (defun org-get-agenda-file-buffer (file)
16642 "Get a buffer visiting FILE. If the buffer needs to be created, add
16643 it to the list of buffers which might be released later."
16644 (let ((buf (org-find-base-buffer-visiting file)))
16646 buf ; just return it
16647 ;; Make a new buffer and remember it
16648 (setq buf (find-file-noselect file))
16649 (if buf (push buf org-agenda-new-buffers))
16652 (defun org-release-buffers (blist)
16653 "Release all buffers in list, asking the user for confirmation when needed.
16654 When a buffer is unmodified, it is just killed. When modified, it is saved
16655 \(if the user agrees) and then killed."
16657 (while (setq buf (pop blist))
16658 (setq file (buffer-file-name buf))
16659 (when (and (buffer-modified-p buf)
16661 (y-or-n-p (format "Save file %s? " file)))
16662 (with-current-buffer buf (save-buffer)))
16663 (kill-buffer buf))))
16665 (defun org-prepare-agenda-buffers (files)
16666 "Create buffers for all agenda files, protect archived trees and comments."
16668 (let ((pa '(:org-archived t))
16669 (pc '(:org-comment t))
16670 (pall '(:org-archived t :org-comment t))
16671 (inhibit-read-only t)
16672 (rea (concat ":" org-archive-tag ":"))
16676 (while (setq file (pop files))
16680 (org-check-agenda-file file)
16681 (set-buffer (org-get-agenda-file-buffer file)))
16683 (setq bmp (buffer-modified-p))
16684 (org-refresh-category-properties)
16685 (setq org-todo-keywords-for-agenda
16686 (append org-todo-keywords-for-agenda org-todo-keywords-1))
16687 (setq org-done-keywords-for-agenda
16688 (append org-done-keywords-for-agenda org-done-keywords))
16689 (setq org-todo-keyword-alist-for-agenda
16690 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
16691 (setq org-drawers-for-agenda
16692 (append org-drawers-for-agenda org-drawers))
16693 (setq org-tag-alist-for-agenda
16694 (append org-tag-alist-for-agenda org-tag-alist))
16697 (remove-text-properties (point-min) (point-max) pall)
16698 (when org-agenda-skip-archived-trees
16699 (goto-char (point-min))
16700 (while (re-search-forward rea nil t)
16701 (if (org-at-heading-p t)
16702 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
16703 (goto-char (point-min))
16704 (setq re (format org-heading-keyword-regexp-format
16705 org-comment-string))
16706 (while (re-search-forward re nil t)
16707 (add-text-properties
16708 (match-beginning 0) (org-end-of-subtree t) pc)))
16709 (set-buffer-modified-p bmp)))))
16710 (setq org-todo-keywords-for-agenda
16711 (org-uniquify org-todo-keywords-for-agenda))
16712 (setq org-todo-keyword-alist-for-agenda
16713 (org-uniquify org-todo-keyword-alist-for-agenda)
16714 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
16716 ;;;; Embedded LaTeX
16718 (defvar org-cdlatex-mode-map (make-sparse-keymap)
16719 "Keymap for the minor `org-cdlatex-mode'.")
16721 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
16722 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
16723 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
16724 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
16725 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
16727 (defvar org-cdlatex-texmathp-advice-is-done nil
16728 "Flag remembering if we have applied the advice to texmathp already.")
16730 (define-minor-mode org-cdlatex-mode
16731 "Toggle the minor `org-cdlatex-mode'.
16732 This mode supports entering LaTeX environment and math in LaTeX fragments
16734 \\{org-cdlatex-mode-map}"
16736 (when org-cdlatex-mode
16738 (run-hooks 'cdlatex-mode-hook)
16739 (cdlatex-compute-tables))
16740 (unless org-cdlatex-texmathp-advice-is-done
16741 (setq org-cdlatex-texmathp-advice-is-done t)
16742 (defadvice texmathp (around org-math-always-on activate)
16743 "Always return t in org-mode buffers.
16744 This is because we want to insert math symbols without dollars even outside
16745 the LaTeX math segments. If Orgmode thinks that point is actually inside
16746 an embedded LaTeX fragment, let texmathp do its job.
16747 \\[org-cdlatex-mode-map]"
16751 ((not (eq major-mode 'org-mode)) ad-do-it)
16752 ((eq this-command 'cdlatex-math-symbol)
16753 (setq ad-return-value t
16754 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
16756 (let ((p (org-inside-LaTeX-fragment-p)))
16757 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
16758 (setq ad-return-value t
16759 texmathp-why '("Org-mode embedded math" . 0))
16760 (if p ad-do-it)))))))))
16762 (defun turn-on-org-cdlatex ()
16763 "Unconditionally turn on `org-cdlatex-mode'."
16764 (org-cdlatex-mode 1))
16766 (defun org-inside-LaTeX-fragment-p ()
16767 "Test if point is inside a LaTeX fragment.
16768 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
16769 sequence appearing also before point.
16770 Even though the matchers for math are configurable, this function assumes
16771 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
16772 delimiters are skipped when they have been removed by customization.
16773 The return value is nil, or a cons cell with the delimiter and the
16774 position of this delimiter.
16776 This function does a reasonably good job, but can locally be fooled by
16777 for example currency specifications. For example it will assume being in
16778 inline math after \"$22.34\". The LaTeX fragment formatter will only format
16779 fragments that are properly closed, but during editing, we have to live
16780 with the uncertainty caused by missing closing delimiters. This function
16781 looks only before point, not after."
16783 (let ((pos (point))
16784 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
16786 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
16788 dd-on str (start 0) m re)
16791 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
16792 re (nth 1 (assoc "$" org-latex-regexps)))
16793 (while (string-match re str start)
16795 ((= (match-end 0) (length str))
16796 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
16797 ((= (match-end 0) (- (length str) 5))
16799 (t (setq start (match-end 0))))))
16800 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
16802 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
16803 (and (match-beginning 2) (throw 'exit nil))
16805 (while (re-search-backward "\\$\\$" lim t)
16806 (setq dd-on (not dd-on)))
16808 (if dd-on (cons "$$" m))))))
16810 (defun org-inside-latex-macro-p ()
16811 "Is point inside a LaTeX macro or its arguments?"
16814 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
16816 (defun org-try-cdlatex-tab ()
16817 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
16818 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
16819 - inside a LaTeX fragment, or
16820 - after the first word in a line, where an abbreviation expansion could
16821 insert a LaTeX environment."
16822 (when org-cdlatex-mode
16824 ;; Before any word on the line: No expansion possible.
16825 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
16826 ;; Just after first word on the line: Expand it. Make sure it
16827 ;; cannot happen on headlines, though.
16829 (skip-chars-backward "a-zA-Z0-9*")
16830 (skip-chars-backward " \t")
16831 (and (bolp) (not (org-at-heading-p))))
16833 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
16835 (defun org-cdlatex-underscore-caret (&optional arg)
16836 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
16837 Revert to the normal definition outside of these fragments."
16839 (if (org-inside-LaTeX-fragment-p)
16840 (call-interactively 'cdlatex-sub-superscript)
16841 (let (org-cdlatex-mode)
16842 (call-interactively (key-binding (vector last-input-event))))))
16844 (defun org-cdlatex-math-modify (&optional arg)
16845 "Execute `cdlatex-math-modify' in LaTeX fragments.
16846 Revert to the normal definition outside of these fragments."
16848 (if (org-inside-LaTeX-fragment-p)
16849 (call-interactively 'cdlatex-math-modify)
16850 (let (org-cdlatex-mode)
16851 (call-interactively (key-binding (vector last-input-event))))))
16853 (defvar org-latex-fragment-image-overlays nil
16854 "List of overlays carrying the images of latex fragments.")
16855 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
16857 (defun org-remove-latex-fragment-image-overlays ()
16858 "Remove all overlays with LaTeX fragment images in current buffer."
16859 (mapc 'delete-overlay org-latex-fragment-image-overlays)
16860 (setq org-latex-fragment-image-overlays nil))
16862 (defun org-preview-latex-fragment (&optional subtree)
16863 "Preview the LaTeX fragment at point, or all locally or globally.
16864 If the cursor is in a LaTeX fragment, create the image and overlay
16865 it over the source code. If there is no fragment at point, display
16866 all fragments in the current text, from one headline to the next. With
16867 prefix SUBTREE, display all fragments in the current subtree. With a
16868 double prefix arg \\[universal-argument] \\[universal-argument], or when \
16869 the cursor is before the first headline,
16870 display all fragments in the buffer.
16871 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
16873 (unless buffer-file-name
16874 (error "Can't preview LaTeX fragment in a non-file buffer"))
16875 (org-remove-latex-fragment-image-overlays)
16878 (let (beg end at msg)
16880 ((or (equal subtree '(16))
16881 (not (save-excursion
16882 (re-search-backward org-outline-regexp-bol nil t))))
16883 (setq beg (point-min) end (point-max)
16884 msg "Creating images for buffer...%s"))
16885 ((equal subtree '(4))
16886 (org-back-to-heading)
16887 (setq beg (point) end (org-end-of-subtree t)
16888 msg "Creating images for subtree...%s"))
16890 (if (setq at (org-inside-LaTeX-fragment-p))
16891 (goto-char (max (point-min) (- (cdr at) 2)))
16892 (org-back-to-heading))
16893 (setq beg (point) end (progn (outline-next-heading) (point))
16894 msg (if at "Creating image...%s"
16895 "Creating images for entry...%s"))))
16897 (narrow-to-region beg end)
16900 (concat "ltxpng/" (file-name-sans-extension
16901 (file-name-nondirectory
16902 buffer-file-name)))
16903 default-directory 'overlays msg at 'forbuffer 'dvipng)
16904 (message msg "done. Use `C-c C-c' to remove images.")))))
16906 (defvar org-latex-regexps
16907 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
16908 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
16909 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
16910 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
16911 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
16912 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
16913 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
16914 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
16915 "Regular expressions for matching embedded LaTeX.")
16917 (defvar org-export-have-math nil) ;; dynamic scoping
16918 (defun org-format-latex (prefix &optional dir overlays msg at
16919 forbuffer processing-type)
16920 "Replace LaTeX fragments with links to an image, and produce images.
16921 Some of the options can be changed using the variable
16922 `org-format-latex-options'."
16923 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
16924 (let* ((prefixnodir (file-name-nondirectory prefix))
16925 (absprefix (expand-file-name prefix dir))
16926 (todir (file-name-directory absprefix))
16927 (opt org-format-latex-options)
16928 (matchers (plist-get opt :matchers))
16929 (re-list org-latex-regexps)
16930 (org-format-latex-header-extra
16931 (plist-get (org-infile-export-plist) :latex-header-extra))
16932 (cnt 0) txt hash link beg end re e checkdir
16933 executables-checked string
16934 m n block-type block linkfile movefile ov)
16935 ;; Check the different regular expressions
16936 (while (setq e (pop re-list))
16937 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
16938 block (if block-type "\n\n" ""))
16939 (when (member m matchers)
16940 (goto-char (point-min))
16941 (while (re-search-forward re nil t)
16942 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
16943 (not (get-text-property (match-beginning n)
16946 (not (eq (get-char-property (match-beginning n)
16948 'org-latex-overlay))))
16949 (setq org-export-have-math t)
16951 ((eq processing-type 'verbatim)
16952 ;; Leave the text verbatim, just protect it
16953 (add-text-properties (match-beginning n) (match-end n)
16954 '(org-protected t)))
16955 ((eq processing-type 'mathjax)
16956 ;; Prepare for MathJax processing
16957 (setq string (match-string n))
16958 (if (member m '("$" "$1"))
16960 (delete-region (match-beginning n) (match-end n))
16961 (goto-char (match-beginning n))
16962 (insert (org-add-props (concat "\\(" (substring string 1 -1)
16964 '(org-protected t))))
16965 (add-text-properties (match-beginning n) (match-end n)
16966 '(org-protected t))))
16967 ((eq processing-type 'dvipng)
16968 ;; Process to an image
16969 (setq txt (match-string n)
16970 beg (match-beginning n) end (match-end n)
16972 (let (print-length print-level) ; make sure full list is printed
16973 (setq hash (sha1 (prin1-to-string
16974 (list org-format-latex-header
16975 org-format-latex-header-extra
16976 org-export-latex-default-packages-alist
16977 org-export-latex-packages-alist
16978 org-format-latex-options
16980 linkfile (format "%s_%s.png" prefix hash)
16981 movefile (format "%s_%s.png" absprefix hash)))
16982 (setq link (concat block "[[file:" linkfile "]]" block))
16983 (if msg (message msg cnt))
16985 (unless checkdir ; make sure the directory exists
16987 (or (file-directory-p todir) (make-directory todir t)))
16989 (unless executables-checked
16990 (org-check-external-command
16991 "latex" "needed to convert LaTeX fragments to images")
16992 (org-check-external-command
16993 "dvipng" "needed to convert LaTeX fragments to images")
16994 (setq executables-checked t))
16996 (unless (file-exists-p movefile)
16997 (org-create-formula-image
16998 txt movefile opt forbuffer))
17002 (if (eq (overlay-get o 'org-overlay-type)
17003 'org-latex-overlay)
17004 (delete-overlay o)))
17005 (overlays-in beg end))
17006 (setq ov (make-overlay beg end))
17007 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17008 (if (featurep 'xemacs)
17010 (overlay-put ov 'invisible t)
17013 (make-glyph (vector 'png :file movefile))))
17016 (list 'image :type 'png :file movefile :ascent 'center)))
17017 (push ov org-latex-fragment-image-overlays)
17019 (delete-region beg end)
17020 (insert (org-add-props link
17021 (list 'org-latex-src
17022 (replace-regexp-in-string
17024 'org-latex-src-embed-type
17025 (if block-type 'paragraph 'character))))))
17026 ((eq processing-type 'mathml)
17027 ;; Process to MathML
17028 (unless executables-checked
17029 (unless (save-match-data (org-format-latex-mathml-available-p))
17030 (error "LaTeX to MathML converter not configured"))
17031 (setq executables-checked t))
17032 (setq txt (match-string n)
17033 beg (match-beginning n) end (match-end n)
17035 (if msg (message msg cnt))
17037 (delete-region beg end)
17038 (insert (org-format-latex-as-mathml
17039 txt block-type prefix dir)))
17041 (error "Unknown conversion type %s for latex fragments"
17042 processing-type)))))))))
17044 (defun org-create-math-formula (latex-frag &optional mathml-file)
17045 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17046 Use `org-latex-to-mathml-convert-command'. If the conversion is
17047 sucessful, return the portion between \"<math...> </math>\"
17048 elements otherwise return nil. When MATHML-FILE is specified,
17049 write the results in to that file. When invoked as an
17050 interactive command, prompt for LATEX-FRAG, with initial value
17051 set to the current active region and echo the results for user
17053 (interactive (list (let ((frag (when (region-active-p)
17054 (buffer-substring-no-properties
17055 (region-beginning) (region-end)))))
17056 (read-string "LaTeX Fragment: " frag nil frag))))
17057 (unless latex-frag (error "Invalid latex-frag"))
17058 (let* ((tmp-in-file (file-relative-name
17059 (make-temp-name (expand-file-name "ltxmathml-in"))))
17060 (ignore (write-region latex-frag nil tmp-in-file))
17061 (tmp-out-file (file-relative-name
17062 (make-temp-name (expand-file-name "ltxmathml-out"))))
17064 org-latex-to-mathml-convert-command
17065 `((?j . ,(shell-quote-argument
17066 (expand-file-name org-latex-to-mathml-jar-file)))
17067 (?I . ,(shell-quote-argument tmp-in-file))
17068 (?o . ,(shell-quote-argument tmp-out-file)))))
17069 mathml shell-command-output)
17070 (when (org-called-interactively-p 'any)
17071 (unless (org-format-latex-mathml-available-p)
17072 (error "LaTeX to MathML converter not configured")))
17073 (message "Running %s" cmd)
17074 (setq shell-command-output (shell-command-to-string cmd))
17076 (when (file-readable-p tmp-out-file)
17077 (with-current-buffer (find-file-noselect tmp-out-file t)
17078 (goto-char (point-min))
17079 (when (re-search-forward
17082 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17084 (regexp-quote "</math>")) nil t)
17085 (prog1 (match-string 0) (kill-buffer))))))
17089 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17091 (write-region mathml nil mathml-file))
17092 (when (org-called-interactively-p 'any)
17094 ((message "LaTeX to MathML conversion failed")
17095 (message shell-command-output)))
17096 (delete-file tmp-in-file)
17097 (when (file-exists-p tmp-out-file)
17098 (delete-file tmp-out-file))
17101 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17102 prefix &optional dir)
17103 "Use `org-create-math-formula' but check local cache first."
17104 (let* ((absprefix (expand-file-name prefix dir))
17105 (print-length nil) (print-level nil)
17106 (formula-id (concat
17111 org-latex-to-mathml-convert-command)))))
17112 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17113 (formula-cache-dir (file-name-directory formula-cache)))
17115 (unless (file-directory-p formula-cache-dir)
17116 (make-directory formula-cache-dir t))
17118 (unless (file-exists-p formula-cache)
17119 (org-create-math-formula latex-frag formula-cache))
17121 (if (file-exists-p formula-cache)
17122 ;; Successful conversion. Return the link to MathML file.
17124 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17125 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17126 'org-latex-src-embed-type (if latex-frag-type
17127 'paragraph 'character)))
17128 ;; Failed conversion. Return the LaTeX fragment verbatim
17129 (add-text-properties
17130 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17133 ;; This function borrows from Ganesh Swami's latex2png.el
17134 (defun org-create-formula-image (string tofile options buffer)
17135 "This calls dvipng."
17136 (require 'org-latex)
17137 (let* ((tmpdir (if (featurep 'xemacs)
17139 temporary-file-directory))
17140 (texfilebase (make-temp-name
17141 (expand-file-name "orgtex" tmpdir)))
17142 (texfile (concat texfilebase ".tex"))
17143 (dvifile (concat texfilebase ".dvi"))
17144 (pngfile (concat texfilebase ".png"))
17145 (fnh (if (featurep 'xemacs)
17146 (font-height (face-font 'default))
17147 (face-attribute 'default :height nil)))
17148 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17149 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17150 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17152 (bg (or (plist-get options (if buffer :background :html-background))
17154 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17155 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17156 (with-temp-file texfile
17157 (insert (org-splice-latex-header
17158 org-format-latex-header
17159 org-export-latex-default-packages-alist
17160 org-export-latex-packages-alist t
17161 org-format-latex-header-extra))
17162 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17163 (require 'org-latex)
17164 (org-export-latex-fix-inputenc))
17165 (let ((dir default-directory))
17166 (condition-case nil
17169 (call-process "latex" nil nil nil texfile))
17172 (if (not (file-exists-p dvifile))
17173 (progn (message "Failed to create dvi file from %s" texfile) nil)
17174 (condition-case nil
17175 (if (featurep 'xemacs)
17176 (call-process "dvipng" nil nil nil
17181 (call-process "dvipng" nil nil nil
17184 ;;"-x" scale "-y" scale
17189 (if (not (file-exists-p pngfile))
17190 (if org-format-latex-signal-error
17191 (error "Failed to create png file from %s" texfile)
17192 (message "Failed to create png file from %s" texfile)
17194 ;; Use the requested file name and clean up
17195 (copy-file pngfile tofile 'replace)
17196 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
17197 (delete-file (concat texfilebase e)))
17200 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17201 "Fill a LaTeX header template TPL.
17202 In the template, the following place holders will be recognized:
17204 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17205 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17206 [PACKAGES] \\usepackage statements for PKG
17207 [NO-PACKAGES] do not include PKG
17208 [EXTRA] the string EXTRA
17209 [NO-EXTRA] do not include EXTRA
17211 For backward compatibility, if both the positive and the negative place
17212 holder is missing, the positive one (without the \"NO-\") will be
17213 assumed to be present at the end of the template.
17214 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17216 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17217 (let (rpl (end ""))
17218 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17219 (setq rpl (if (or (match-end 1) (not def-pkg))
17220 "" (org-latex-packages-to-string def-pkg snippets-p t))
17221 tpl (replace-match rpl t t tpl))
17222 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17224 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17225 (setq rpl (if (or (match-end 1) (not pkg))
17226 "" (org-latex-packages-to-string pkg snippets-p t))
17227 tpl (replace-match rpl t t tpl))
17230 (org-latex-packages-to-string pkg snippets-p)))))
17232 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17233 (setq rpl (if (or (match-end 1) (not extra))
17234 "" (concat extra "\n"))
17235 tpl (replace-match rpl t t tpl))
17236 (if (and extra (string-match "\\S-" extra))
17237 (setq end (concat end "\n" extra))))
17239 (if (string-match "\\S-" end)
17240 (concat tpl "\n" end)
17243 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17244 "Turn an alist of packages into a string with the \\usepackage macros."
17245 (setq pkg (mapconcat (lambda(p)
17248 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17249 (format "%% Package %s omitted" (cadr p)))
17250 ((equal "" (car p))
17251 (format "\\usepackage{%s}" (cadr p)))
17253 (format "\\usepackage[%s]{%s}"
17254 (car p) (cadr p)))))
17257 (if newline (concat pkg "\n") pkg))
17259 (defun org-dvipng-color (attr)
17260 "Return an rgb color specification for dvipng."
17261 (apply 'format "rgb %s %s %s"
17262 (mapcar 'org-normalize-color
17263 (if (featurep 'xemacs)
17264 (color-rgb-components
17265 (face-property 'default
17266 (cond ((eq attr :foreground) 'foreground)
17267 ((eq attr :background) 'background))))
17268 (color-values (face-attribute 'default attr nil))))))
17270 (defun org-normalize-color (value)
17271 "Return string to be used as color value for an RGB component."
17272 (format "%g" (/ value 65535.0)))
17277 (defvar org-inline-image-overlays nil)
17278 (make-variable-buffer-local 'org-inline-image-overlays)
17280 (defun org-toggle-inline-images (&optional include-linked)
17281 "Toggle the display of inline images.
17282 INCLUDE-LINKED is passed to `org-display-inline-images'."
17284 (if org-inline-image-overlays
17286 (org-remove-inline-images)
17287 (message "Inline image display turned off"))
17288 (org-display-inline-images include-linked)
17289 (if org-inline-image-overlays
17290 (message "%d images displayed inline"
17291 (length org-inline-image-overlays))
17292 (message "No images to display inline"))))
17294 (defun org-display-inline-images (&optional include-linked refresh beg end)
17295 "Display inline images.
17296 Normally only links without a description part are inlined, because this
17297 is how it will work for export. When INCLUDE-LINKED is set, also links
17298 with a description part will be inlined. This can be nice for a quick
17299 look at those images, but it does not reflect what exported files will look
17301 When REFRESH is set, refresh existing images between BEG and END.
17302 This will create new image displays only if necessary.
17303 BEG and END default to the buffer boundaries."
17306 (org-remove-inline-images)
17307 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17311 (setq beg (or beg (point-min)) end (or end (point-max)))
17313 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17314 (substring (org-image-file-name-regexp) 0 -2)
17315 "\\)\\]" (if include-linked "" "\\]")))
17317 (while (re-search-forward re end t)
17318 (setq old (get-char-property-and-overlay (match-beginning 1)
17319 'org-image-overlay))
17320 (setq file (expand-file-name
17321 (concat (or (match-string 3) "") (match-string 4))))
17322 (when (file-exists-p file)
17323 (if (and (car-safe old) refresh)
17324 (image-refresh (overlay-get (cdr old) 'display))
17325 (setq img (save-match-data (create-image file)))
17327 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17328 (overlay-put ov 'display img)
17329 (overlay-put ov 'face 'default)
17330 (overlay-put ov 'org-image-overlay t)
17331 (overlay-put ov 'modification-hooks
17332 (list 'org-display-inline-modification-hook))
17333 (push ov org-inline-image-overlays)))))))))
17335 (defun org-display-inline-modification-hook (ov after beg end &optional len)
17336 "Remove inline-display overlay if a corresponding region is modified."
17337 (let ((inhibit-modification-hooks t))
17338 (when (and ov after)
17339 (delete ov org-inline-image-overlays)
17340 (delete-overlay ov))))
17342 (defun org-remove-inline-images ()
17343 "Remove inline display of images."
17345 (mapc 'delete-overlay org-inline-image-overlays)
17346 (setq org-inline-image-overlays nil))
17350 ;; Outline functions from `outline-mode-prefix-map'
17351 ;; that can be remapped in Org:
17352 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17353 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17354 (define-key org-mode-map [remap outline-forward-same-level]
17355 'org-forward-same-level)
17356 (define-key org-mode-map [remap outline-backward-same-level]
17357 'org-backward-same-level)
17358 (define-key org-mode-map [remap show-branches]
17359 'org-kill-note-or-show-branches)
17360 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17361 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17362 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17364 ;; Outline functions from `outline-mode-prefix-map'
17365 ;; that can not be remapped in Org:
17366 ;; - the column "key binding" shows whether the Outline function is still
17367 ;; available in Org mode on the same key that it has been bound to in
17369 ;; - "overridden": key used for a different functionality in Org mode
17370 ;; - else: key still bound to the same Outline function in Org mode
17371 ;; | Outline function | key binding | Org replacement |
17372 ;; |------------------------------------+-------------+-----------------------|
17373 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17374 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17375 ;; | `show-children' | `C-c C-i' | visibility cycling |
17376 ;; | `hide-subtree' | overridden | visibility cycling |
17377 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17378 ;; | `hide-body' | overridden | no replacement |
17379 ;; | `show-all' | overridden | no replacement |
17380 ;; | `hide-entry' | overridden | visibility cycling |
17381 ;; | `show-entry' | overridden | no replacement |
17382 ;; | `hide-leaves' | overridden | no replacement |
17383 ;; | `hide-sublevels' | overridden | no replacement |
17384 ;; | `hide-other' | overridden | no replacement |
17385 ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
17386 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17388 ;; Make `C-c C-x' a prefix key
17389 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17391 ;; TAB key with modifiers
17392 (org-defkey org-mode-map "\C-i" 'org-cycle)
17393 (org-defkey org-mode-map [(tab)] 'org-cycle)
17394 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17395 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17396 ;; The following line is necessary under Suse GNU/Linux
17397 (unless (featurep 'xemacs)
17398 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17399 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17400 (define-key org-mode-map [backtab] 'org-shifttab)
17402 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17403 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17404 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17406 ;; Cursor keys with modifiers
17407 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17408 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17409 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17410 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17412 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17413 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17414 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17415 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17417 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17418 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17419 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17420 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17422 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17423 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17424 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17425 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17428 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17429 (mapc (lambda (pair)
17430 (define-key org-babel-map (car pair) (cdr pair)))
17431 org-babel-key-bindings)
17433 ;;; Extra keys for tty access.
17434 ;; We only set them when really needed because otherwise the
17435 ;; menus don't show the simple keys
17437 (when (or org-use-extra-keys
17438 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17439 (not window-system))
17440 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17441 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17442 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17443 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17444 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17445 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17446 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17447 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17448 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17449 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17450 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17451 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17452 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17453 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17454 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17455 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17456 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17457 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17458 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17459 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17460 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17461 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17462 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17463 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17464 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17465 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17466 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17467 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17469 ;; All the other keys
17471 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17472 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17473 (if (boundp 'narrow-map)
17474 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17475 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17476 (if (boundp 'narrow-map)
17477 (org-defkey narrow-map "b" 'org-narrow-to-block)
17478 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17479 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
17480 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
17481 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17482 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17483 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17484 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
17485 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17486 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17487 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17488 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17489 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17490 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17491 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
17492 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
17493 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
17494 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
17495 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
17496 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
17497 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
17498 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
17499 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
17500 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
17501 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
17502 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
17503 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
17504 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
17505 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
17506 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
17507 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
17508 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
17509 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
17510 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
17511 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
17512 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
17513 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
17514 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
17515 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
17516 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
17517 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
17518 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
17519 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
17520 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
17521 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17522 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
17523 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
17524 (org-defkey org-mode-map "\C-c^" 'org-sort)
17525 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
17526 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
17527 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
17528 (org-defkey org-mode-map "\C-m" 'org-return)
17529 (org-defkey org-mode-map "\C-j" 'org-return-indent)
17530 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
17531 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
17532 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
17533 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
17534 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
17535 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
17536 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
17537 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
17538 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
17539 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
17540 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
17541 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
17542 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
17543 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
17544 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
17545 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
17546 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
17547 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
17548 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
17549 (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
17550 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
17551 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
17553 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
17554 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
17555 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
17556 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
17558 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
17559 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
17560 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
17561 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
17562 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
17563 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
17564 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
17565 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
17566 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
17567 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
17568 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
17569 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
17570 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
17571 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
17572 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
17573 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
17574 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
17575 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
17577 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
17578 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
17579 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
17580 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
17581 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
17583 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
17585 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
17587 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
17588 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
17590 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
17593 (when (featurep 'xemacs)
17594 (org-defkey org-mode-map 'button3 'popup-mode-menu))
17597 (defconst org-speed-commands-default
17599 ("Outline Navigation")
17600 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
17601 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
17602 ("f" . (org-speed-move-safe 'org-forward-same-level))
17603 ("b" . (org-speed-move-safe 'org-backward-same-level))
17604 ("u" . (org-speed-move-safe 'outline-up-heading))
17606 ("g" . (org-refile t))
17607 ("Outline Visibility")
17609 ("C" . org-shifttab)
17610 (" " . org-display-outline-path)
17611 ("Outline Structure Editing")
17612 ("U" . org-shiftmetaup)
17613 ("D" . org-shiftmetadown)
17614 ("r" . org-metaright)
17615 ("l" . org-metaleft)
17616 ("R" . org-shiftmetaright)
17617 ("L" . org-shiftmetaleft)
17618 ("i" . (progn (forward-char 1) (call-interactively
17619 'org-insert-heading-respect-content)))
17622 ("a" . org-archive-subtree-default-with-confirmation)
17623 ("." . org-mark-subtree)
17625 ("I" . org-clock-in)
17626 ("O" . org-clock-out)
17627 ("Meta Data Editing")
17629 ("0" . (org-priority ?\ ))
17630 ("1" . (org-priority ?A))
17631 ("2" . (org-priority ?B))
17632 ("3" . (org-priority ?C))
17633 (";" . org-set-tags-command)
17634 ("e" . org-set-effort)
17635 ("Agenda Views etc")
17637 ("/" . org-sparse-tree)
17639 ("o" . org-open-at-point)
17640 ("?" . org-speed-command-help)
17641 ("<" . (org-agenda-set-restriction-lock 'subtree))
17642 (">" . (org-agenda-remove-restriction-lock))
17644 "The default speed commands.")
17646 (defun org-print-speed-command (e)
17647 (if (> (length (car e)) 1)
17652 (princ (make-string (length (car e)) ?-))
17656 (if (symbolp (cdr e))
17657 (princ (symbol-name (cdr e)))
17661 (defun org-speed-command-help ()
17662 "Show the available speed commands."
17664 (if (not org-use-speed-commands)
17665 (error "Speed commands are not activated, customize `org-use-speed-commands'")
17666 (with-output-to-temp-buffer "*Help*"
17667 (princ "User-defined Speed commands\n===========================\n")
17668 (mapc 'org-print-speed-command org-speed-commands-user)
17670 (princ "Built-in Speed commands\n=======================\n")
17671 (mapc 'org-print-speed-command org-speed-commands-default))
17672 (with-current-buffer "*Help*"
17673 (setq truncate-lines t))))
17675 (defun org-speed-move-safe (cmd)
17676 "Execute CMD, but make sure that the cursor always ends up in a headline.
17677 If not, return to the original position and throw an error."
17679 (let ((pos (point)))
17680 (call-interactively cmd)
17681 (unless (and (bolp) (org-at-heading-p))
17683 (error "Boundary reached while executing %s" cmd))))
17685 (defvar org-self-insert-command-undo-counter 0)
17687 (defvar org-table-auto-blank-field) ; defined in org-table.el
17688 (defvar org-speed-command nil)
17690 (defun org-speed-command-default-hook (keys)
17691 "Hook for activating single-letter speed commands.
17692 `org-speed-commands-default' specifies a minimal command set.
17693 Use `org-speed-commands-user' for further customization."
17694 (when (or (and (bolp) (looking-at org-outline-regexp))
17695 (and (functionp org-use-speed-commands)
17696 (funcall org-use-speed-commands)))
17697 (cdr (assoc keys (append org-speed-commands-user
17698 org-speed-commands-default)))))
17700 (defun org-babel-speed-command-hook (keys)
17701 "Hook for activating single-letter code block commands."
17702 (when (and (bolp) (looking-at org-babel-src-block-regexp))
17703 (cdr (assoc keys org-babel-key-bindings))))
17705 (defcustom org-speed-command-hook
17706 '(org-speed-command-default-hook org-babel-speed-command-hook)
17707 "Hook for activating speed commands at strategic locations.
17708 Hook functions are called in sequence until a valid handler is
17711 Each hook takes a single argument, a user-pressed command key
17712 which is also a `self-insert-command' from the global map.
17714 Within the hook, examine the cursor position and the command key
17715 and return nil or a valid handler as appropriate. Handler could
17716 be one of an interactive command, a function, or a form.
17718 Set `org-use-speed-commands' to non-nil value to enable this
17719 hook. The default setting is `org-speed-command-default-hook'."
17720 :group 'org-structure
17724 (defun org-self-insert-command (N)
17725 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
17726 If the cursor is in a table looking at whitespace, the whitespace is
17727 overwritten, and the table is not marked as requiring realignment."
17729 (org-check-before-invisible-edit 'insert)
17731 ((and org-use-speed-commands
17732 (setq org-speed-command
17733 (run-hook-with-args-until-success
17734 'org-speed-command-hook (this-command-keys))))
17736 ((commandp org-speed-command)
17737 (setq this-command org-speed-command)
17738 (call-interactively org-speed-command))
17739 ((functionp org-speed-command)
17740 (funcall org-speed-command))
17741 ((and org-speed-command (listp org-speed-command))
17742 (eval org-speed-command))
17743 (t (let (org-use-speed-commands)
17744 (call-interactively 'org-self-insert-command)))))
17748 ;; check if we blank the field, and if that triggers align
17749 (and (featurep 'org-table) org-table-auto-blank-field
17750 (member last-command
17751 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
17752 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
17753 ;; got extra space, this field does not determine column width
17754 (let (org-table-may-need-update) (org-table-blank-field))
17755 ;; no extra space, this field may determine column width
17756 (org-table-blank-field)))
17759 (looking-at "[^|\n]* |"))
17760 (let (org-table-may-need-update)
17761 (goto-char (1- (match-end 0)))
17762 (backward-delete-char 1)
17763 (goto-char (match-beginning 0))
17764 (self-insert-command N)))
17766 (setq org-table-may-need-update t)
17767 (self-insert-command N)
17768 (org-fix-tags-on-the-fly)
17769 (if org-self-insert-cluster-for-undo
17770 (if (not (eq last-command 'org-self-insert-command))
17771 (setq org-self-insert-command-undo-counter 1)
17772 (if (>= org-self-insert-command-undo-counter 20)
17773 (setq org-self-insert-command-undo-counter 1)
17774 (and (> org-self-insert-command-undo-counter 0)
17775 buffer-undo-list (listp buffer-undo-list)
17776 (not (cadr buffer-undo-list)) ; remove nil entry
17777 (setcdr buffer-undo-list (cddr buffer-undo-list)))
17778 (setq org-self-insert-command-undo-counter
17779 (1+ org-self-insert-command-undo-counter))))))))
17781 (defun org-check-before-invisible-edit (kind)
17782 "Check is editing if kind KIND would be dangerous with invisible text around.
17783 The detailed reaction depends on the user option `org-catch-invisible-edits'."
17784 ;; First, try to get out of here as quickly as possible, to reduce overhead
17785 (if (and org-catch-invisible-edits
17786 (or (not (boundp 'visible-mode)) (not visible-mode))
17787 (or (get-char-property (point) 'invisible)
17788 (get-char-property (max (point-min) (1- (point))) 'invisible)))
17789 ;; OK, we need to take a closer look
17790 (let* ((invisible-at-point (get-char-property (point) 'invisible))
17791 (invisible-before-point (if (bobp) nil (get-char-property
17792 (1- (point)) 'invisible)))
17793 (border-and-ok-direction
17795 ;; Check if we are acting predictably before invisible text
17796 (and invisible-at-point (not invisible-before-point)
17797 (memq kind '(insert delete-backward)))
17798 ;; Check if we are acting predictably after invisible text
17799 ;; This works not well, and I have turned it off. It seems
17800 ;; better to always show and stop after invisible text.
17801 ;; (and (not invisible-at-point) invisible-before-point
17802 ;; (memq kind '(insert delete)))
17805 (when (or (memq invisible-at-point '(outline org-hide-block))
17806 (memq invisible-before-point '(outline org-hide-block)))
17807 (if (eq org-catch-invisible-edits 'error)
17808 (error "Editing in invisible areas is prohibited - make visible first"))
17809 ;; Make the area visible
17811 (if invisible-before-point
17812 (goto-char (previous-single-char-property-change
17813 (point) 'invisible)))
17816 ((eq org-catch-invisible-edits 'show)
17817 ;; That's it, we do the edit after showing
17819 "Unfolding invisible region around point before editing")
17821 ((and (eq org-catch-invisible-edits 'smart)
17822 border-and-ok-direction)
17823 (message "Unfolding invisible region around point before editing"))
17825 ;; Don't do the edit, make the user repeat it in full visibility
17826 (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
17828 (defun org-fix-tags-on-the-fly ()
17829 (when (and (equal (char-after (point-at-bol)) ?*)
17830 (org-at-heading-p))
17831 (org-align-tags-here org-tags-column)))
17833 (defun org-delete-backward-char (N)
17834 "Like `delete-backward-char', insert whitespace at field end in tables.
17835 When deleting backwards, in tables this function will insert whitespace in
17836 front of the next \"|\" separator, to keep the table aligned. The table will
17837 still be marked for re-alignment if the field did fill the entire column,
17838 because, in this case the deletion might narrow the column."
17840 (org-check-before-invisible-edit 'delete-backward)
17841 (if (and (org-table-p)
17843 (string-match "|" (buffer-substring (point-at-bol) (point)))
17844 (looking-at ".*?|"))
17845 (let ((pos (point))
17846 (noalign (looking-at "[^|\n\r]* |"))
17847 (c org-table-may-need-update))
17848 (backward-delete-char N)
17849 (if (not overwrite-mode)
17851 (skip-chars-forward "^|")
17853 (goto-char (1- pos))))
17854 ;; noalign: if there were two spaces at the end, this field
17855 ;; does not determine the width of the column.
17856 (if noalign (setq org-table-may-need-update c)))
17857 (backward-delete-char N)
17858 (org-fix-tags-on-the-fly)))
17860 (defun org-delete-char (N)
17861 "Like `delete-char', but insert whitespace at field end in tables.
17862 When deleting characters, in tables this function will insert whitespace in
17863 front of the next \"|\" separator, to keep the table aligned. The table will
17864 still be marked for re-alignment if the field did fill the entire column,
17865 because, in this case the deletion might narrow the column."
17867 (org-check-before-invisible-edit 'delete)
17868 (if (and (org-table-p)
17870 (not (= (char-after) ?|))
17872 (if (looking-at ".*?|")
17873 (let ((pos (point))
17874 (noalign (looking-at "[^|\n\r]* |"))
17875 (c org-table-may-need-update))
17876 (replace-match (concat
17877 (substring (match-string 0) 1 -1)
17880 ;; noalign: if there were two spaces at the end, this field
17881 ;; does not determine the width of the column.
17882 (if noalign (setq org-table-may-need-update c)))
17885 (org-fix-tags-on-the-fly)))
17887 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
17888 (put 'org-self-insert-command 'delete-selection t)
17889 (put 'orgtbl-self-insert-command 'delete-selection t)
17890 (put 'org-delete-char 'delete-selection 'supersede)
17891 (put 'org-delete-backward-char 'delete-selection 'supersede)
17892 (put 'org-yank 'delete-selection 'yank)
17894 ;; Make `flyspell-mode' delay after some commands
17895 (put 'org-self-insert-command 'flyspell-delayed t)
17896 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
17897 (put 'org-delete-char 'flyspell-delayed t)
17898 (put 'org-delete-backward-char 'flyspell-delayed t)
17900 ;; Make pabbrev-mode expand after org-mode commands
17901 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
17902 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
17904 ;; How to do this: Measure non-white length of current string
17905 ;; If equal to column width, we should realign.
17907 (defun org-remap (map &rest commands)
17908 "In MAP, remap the functions given in COMMANDS.
17909 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
17912 (setq old (pop commands) new (pop commands))
17913 (if (fboundp 'command-remapping)
17914 (org-defkey map (vector 'remap old) new)
17915 (substitute-key-definition old new map global-map)))))
17917 (when (eq org-enable-table-editor 'optimized)
17918 ;; If the user wants maximum table support, we need to hijack
17919 ;; some standard editing functions
17920 (org-remap org-mode-map
17921 'self-insert-command 'org-self-insert-command
17922 'delete-char 'org-delete-char
17923 'delete-backward-char 'org-delete-backward-char)
17924 (org-defkey org-mode-map "|" 'org-force-self-insert))
17926 (defvar org-ctrl-c-ctrl-c-hook nil
17927 "Hook for functions attaching themselves to `C-c C-c'.
17929 This can be used to add additional functionality to the C-c C-c
17930 key which executes context-dependent commands. This hook is run
17931 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
17932 run after the last test.
17934 Each function will be called with no arguments. The function
17935 must check if the context is appropriate for it to act. If yes,
17936 it should do its thing and then return a non-nil value. If the
17937 context is wrong, just do nothing and return nil.")
17939 (defvar org-ctrl-c-ctrl-c-final-hook nil
17940 "Hook for functions attaching themselves to `C-c C-c'.
17942 This can be used to add additional functionality to the C-c C-c
17943 key which executes context-dependent commands. This hook is run
17944 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
17945 before the first test.
17947 Each function will be called with no arguments. The function
17948 must check if the context is appropriate for it to act. If yes,
17949 it should do its thing and then return a non-nil value. If the
17950 context is wrong, just do nothing and return nil.")
17952 (defvar org-tab-first-hook nil
17953 "Hook for functions to attach themselves to TAB.
17954 See `org-ctrl-c-ctrl-c-hook' for more information.
17955 This hook runs as the first action when TAB is pressed, even before
17956 `org-cycle' messes around with the `outline-regexp' to cater for
17957 inline tasks and plain list item folding.
17958 If any function in this hook returns t, any other actions that
17959 would have been caused by TAB (such as table field motion or visibility
17960 cycling) will not occur.")
17962 (defvar org-tab-after-check-for-table-hook nil
17963 "Hook for functions to attach themselves to TAB.
17964 See `org-ctrl-c-ctrl-c-hook' for more information.
17965 This hook runs after it has been established that the cursor is not in a
17966 table, but before checking if the cursor is in a headline or if global cycling
17968 If any function in this hook returns t, not other actions like visibility
17969 cycling will be done.")
17971 (defvar org-tab-after-check-for-cycling-hook nil
17972 "Hook for functions to attach themselves to TAB.
17973 See `org-ctrl-c-ctrl-c-hook' for more information.
17974 This hook runs after it has been established that not table field motion and
17975 not visibility should be done because of current context. This is probably
17976 the place where a package like yasnippets can hook in.")
17978 (defvar org-tab-before-tab-emulation-hook nil
17979 "Hook for functions to attach themselves to TAB.
17980 See `org-ctrl-c-ctrl-c-hook' for more information.
17981 This hook runs after every other options for TAB have been exhausted, but
17982 before indentation and \t insertion takes place.")
17984 (defvar org-metaleft-hook nil
17985 "Hook for functions attaching themselves to `M-left'.
17986 See `org-ctrl-c-ctrl-c-hook' for more information.")
17987 (defvar org-metaright-hook nil
17988 "Hook for functions attaching themselves to `M-right'.
17989 See `org-ctrl-c-ctrl-c-hook' for more information.")
17990 (defvar org-metaup-hook nil
17991 "Hook for functions attaching themselves to `M-up'.
17992 See `org-ctrl-c-ctrl-c-hook' for more information.")
17993 (defvar org-metadown-hook nil
17994 "Hook for functions attaching themselves to `M-down'.
17995 See `org-ctrl-c-ctrl-c-hook' for more information.")
17996 (defvar org-shiftmetaleft-hook nil
17997 "Hook for functions attaching themselves to `M-S-left'.
17998 See `org-ctrl-c-ctrl-c-hook' for more information.")
17999 (defvar org-shiftmetaright-hook nil
18000 "Hook for functions attaching themselves to `M-S-right'.
18001 See `org-ctrl-c-ctrl-c-hook' for more information.")
18002 (defvar org-shiftmetaup-hook nil
18003 "Hook for functions attaching themselves to `M-S-up'.
18004 See `org-ctrl-c-ctrl-c-hook' for more information.")
18005 (defvar org-shiftmetadown-hook nil
18006 "Hook for functions attaching themselves to `M-S-down'.
18007 See `org-ctrl-c-ctrl-c-hook' for more information.")
18008 (defvar org-metareturn-hook nil
18009 "Hook for functions attaching themselves to `M-RET'.
18010 See `org-ctrl-c-ctrl-c-hook' for more information.")
18011 (defvar org-shiftup-hook nil
18012 "Hook for functions attaching themselves to `S-up'.
18013 See `org-ctrl-c-ctrl-c-hook' for more information.")
18014 (defvar org-shiftup-final-hook nil
18015 "Hook for functions attaching themselves to `S-up'.
18016 This one runs after all other options except shift-select have been excluded.
18017 See `org-ctrl-c-ctrl-c-hook' for more information.")
18018 (defvar org-shiftdown-hook nil
18019 "Hook for functions attaching themselves to `S-down'.
18020 See `org-ctrl-c-ctrl-c-hook' for more information.")
18021 (defvar org-shiftdown-final-hook nil
18022 "Hook for functions attaching themselves to `S-down'.
18023 This one runs after all other options except shift-select have been excluded.
18024 See `org-ctrl-c-ctrl-c-hook' for more information.")
18025 (defvar org-shiftleft-hook nil
18026 "Hook for functions attaching themselves to `S-left'.
18027 See `org-ctrl-c-ctrl-c-hook' for more information.")
18028 (defvar org-shiftleft-final-hook nil
18029 "Hook for functions attaching themselves to `S-left'.
18030 This one runs after all other options except shift-select have been excluded.
18031 See `org-ctrl-c-ctrl-c-hook' for more information.")
18032 (defvar org-shiftright-hook nil
18033 "Hook for functions attaching themselves to `S-right'.
18034 See `org-ctrl-c-ctrl-c-hook' for more information.")
18035 (defvar org-shiftright-final-hook nil
18036 "Hook for functions attaching themselves to `S-right'.
18037 This one runs after all other options except shift-select have been excluded.
18038 See `org-ctrl-c-ctrl-c-hook' for more information.")
18040 (defun org-modifier-cursor-error ()
18041 "Throw an error, a modified cursor command was applied in wrong context."
18042 (error "This command is active in special context like tables, headlines or items"))
18044 (defun org-shiftselect-error ()
18045 "Throw an error because Shift-Cursor command was applied in wrong context."
18046 (if (and (boundp 'shift-select-mode) shift-select-mode)
18047 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18048 (error "This command works only in special context like headlines or timestamps")))
18050 (defun org-call-for-shift-select (cmd)
18051 (let ((this-command-keys-shift-translated t))
18052 (call-interactively cmd)))
18054 (defun org-shifttab (&optional arg)
18055 "Global visibility cycling or move to previous table field.
18056 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18058 See the individual commands for more information."
18061 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18063 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18064 (message "Content view to level: %d" arg)
18065 (org-content (prefix-numeric-value arg2))
18066 (setq org-cycle-global-status 'overview)))
18067 (t (call-interactively 'org-global-cycle))))
18069 (defun org-shiftmetaleft ()
18070 "Promote subtree or delete table column.
18071 Calls `org-promote-subtree', `org-outdent-item',
18072 or `org-table-delete-column', depending on context.
18073 See the individual commands for more information."
18076 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18077 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18078 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18079 ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
18080 (t (org-modifier-cursor-error))))
18082 (defun org-shiftmetaright ()
18083 "Demote subtree or insert table column.
18084 Calls `org-demote-subtree', `org-indent-item',
18085 or `org-table-insert-column', depending on context.
18086 See the individual commands for more information."
18089 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18090 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18091 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18092 ((org-at-item-p) (call-interactively 'org-indent-item-tree))
18093 (t (org-modifier-cursor-error))))
18095 (defun org-shiftmetaup (&optional arg)
18096 "Move subtree up or kill table row.
18097 Calls `org-move-subtree-up' or `org-table-kill-row' or
18098 `org-move-item-up' depending on context. See the individual commands
18099 for more information."
18102 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18103 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18104 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18105 ((org-at-item-p) (call-interactively 'org-move-item-up))
18106 (t (org-modifier-cursor-error))))
18108 (defun org-shiftmetadown (&optional arg)
18109 "Move subtree down or insert table row.
18110 Calls `org-move-subtree-down' or `org-table-insert-row' or
18111 `org-move-item-down', depending on context. See the individual
18112 commands for more information."
18115 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18116 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18117 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18118 ((org-at-item-p) (call-interactively 'org-move-item-down))
18119 (t (org-modifier-cursor-error))))
18121 (defsubst org-hidden-tree-error ()
18123 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18125 (defun org-metaleft (&optional arg)
18126 "Promote heading or move table column to left.
18127 Calls `org-do-promote' or `org-table-move-column', depending on context.
18128 With no specific context, calls the Emacs default `backward-word'.
18129 See the individual commands for more information."
18132 ((run-hook-with-args-until-success 'org-metaleft-hook))
18133 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18134 ((org-with-limited-levels
18135 (or (org-at-heading-p)
18136 (and (org-region-active-p)
18138 (goto-char (region-beginning))
18139 (org-at-heading-p)))))
18140 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18141 (call-interactively 'org-do-promote))
18142 ;; At an inline task.
18143 ((org-at-heading-p)
18144 (call-interactively 'org-inlinetask-promote))
18145 ((or (org-at-item-p)
18146 (and (org-region-active-p)
18148 (goto-char (region-beginning))
18150 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18151 (call-interactively 'org-outdent-item))
18152 (t (call-interactively 'backward-word))))
18154 (defun org-metaright (&optional arg)
18155 "Demote a subtree, a list item or move table column to right.
18156 In front of a drawer or a block keyword, indent it correctly.
18157 With no specific context, calls the Emacs default `forward-word'.
18158 See the individual commands for more information."
18161 ((run-hook-with-args-until-success 'org-metaright-hook))
18162 ((org-at-table-p) (call-interactively 'org-table-move-column))
18163 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18164 ((org-at-block-p) (call-interactively 'org-indent-block))
18165 ((org-with-limited-levels
18166 (or (org-at-heading-p)
18167 (and (org-region-active-p)
18169 (goto-char (region-beginning))
18170 (org-at-heading-p)))))
18171 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18172 (call-interactively 'org-do-demote))
18173 ;; At an inline task.
18174 ((org-at-heading-p)
18175 (call-interactively 'org-inlinetask-demote))
18176 ((or (org-at-item-p)
18177 (and (org-region-active-p)
18179 (goto-char (region-beginning))
18181 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18182 (call-interactively 'org-indent-item))
18183 (t (call-interactively 'forward-word))))
18185 (defun org-check-for-hidden (what)
18186 "Check if there are hidden headlines/items in the current visual line.
18187 WHAT can be either `headlines' or `items'. If the current line is
18188 an outline or item heading and it has a folded subtree below it,
18189 this function returns t, nil otherwise."
18191 ((eq what 'headlines) org-outline-regexp-bol)
18192 ((eq what 'items) (org-item-beginning-re))
18193 (t (error "This should not happen"))))
18197 (unless (org-region-active-p)
18198 (setq beg (point-at-bol))
18199 (beginning-of-line 2)
18200 (while (and (not (eobp)) ;; this is like `next-line'
18201 (get-char-property (1- (point)) 'invisible))
18202 (beginning-of-line 2))
18205 (goto-char (point-at-eol))
18206 (setq end (max end (point)))
18207 (while (re-search-forward re end t)
18208 (if (get-char-property (match-beginning 0) 'invisible)
18212 (defun org-metaup (&optional arg)
18213 "Move subtree up or move table row up.
18214 Calls `org-move-subtree-up' or `org-table-move-row' or
18215 `org-move-item-up', depending on context. See the individual commands
18216 for more information."
18219 ((run-hook-with-args-until-success 'org-metaup-hook))
18220 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18221 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18222 ((org-at-item-p) (call-interactively 'org-move-item-up))
18223 (t (transpose-lines 1) (beginning-of-line -1))))
18225 (defun org-metadown (&optional arg)
18226 "Move subtree down or move table row down.
18227 Calls `org-move-subtree-down' or `org-table-move-row' or
18228 `org-move-item-down', depending on context. See the individual
18229 commands for more information."
18232 ((run-hook-with-args-until-success 'org-metadown-hook))
18233 ((org-at-table-p) (call-interactively 'org-table-move-row))
18234 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18235 ((org-at-item-p) (call-interactively 'org-move-item-down))
18236 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
18238 (defun org-shiftup (&optional arg)
18239 "Increase item in timestamp or increase priority of current headline.
18240 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18241 depending on context. See the individual commands for more information."
18244 ((run-hook-with-args-until-success 'org-shiftup-hook))
18245 ((and org-support-shift-select (org-region-active-p))
18246 (org-call-for-shift-select 'previous-line))
18247 ((org-at-timestamp-p t)
18248 (call-interactively (if org-edit-timestamp-down-means-later
18249 'org-timestamp-down 'org-timestamp-up)))
18250 ((and (not (eq org-support-shift-select 'always))
18251 org-enable-priority-commands
18252 (org-at-heading-p))
18253 (call-interactively 'org-priority-up))
18254 ((and (not org-support-shift-select) (org-at-item-p))
18255 (call-interactively 'org-previous-item))
18256 ((org-clocktable-try-shift 'up arg))
18257 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18258 (org-support-shift-select
18259 (org-call-for-shift-select 'previous-line))
18260 (t (org-shiftselect-error))))
18262 (defun org-shiftdown (&optional arg)
18263 "Decrease item in timestamp or decrease priority of current headline.
18264 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18265 depending on context. See the individual commands for more information."
18268 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18269 ((and org-support-shift-select (org-region-active-p))
18270 (org-call-for-shift-select 'next-line))
18271 ((org-at-timestamp-p t)
18272 (call-interactively (if org-edit-timestamp-down-means-later
18273 'org-timestamp-up 'org-timestamp-down)))
18274 ((and (not (eq org-support-shift-select 'always))
18275 org-enable-priority-commands
18276 (org-at-heading-p))
18277 (call-interactively 'org-priority-down))
18278 ((and (not org-support-shift-select) (org-at-item-p))
18279 (call-interactively 'org-next-item))
18280 ((org-clocktable-try-shift 'down arg))
18281 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18282 (org-support-shift-select
18283 (org-call-for-shift-select 'next-line))
18284 (t (org-shiftselect-error))))
18286 (defun org-shiftright (&optional arg)
18287 "Cycle the thing at point or in the current line, depending on context.
18288 Depending on context, this does one of the following:
18290 - switch a timestamp at point one day into the future
18291 - on a headline, switch to the next TODO keyword.
18292 - on an item, switch entire list to the next bullet type
18293 - on a property line, switch to the next allowed value
18294 - on a clocktable definition line, move time block into the future"
18297 ((run-hook-with-args-until-success 'org-shiftright-hook))
18298 ((and org-support-shift-select (org-region-active-p))
18299 (org-call-for-shift-select 'forward-char))
18300 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18301 ((and (not (eq org-support-shift-select 'always))
18302 (org-at-heading-p))
18303 (let ((org-inhibit-logging
18304 (not org-treat-S-cursor-todo-selection-as-state-change))
18305 (org-inhibit-blocking
18306 (not org-treat-S-cursor-todo-selection-as-state-change)))
18307 (org-call-with-arg 'org-todo 'right)))
18308 ((or (and org-support-shift-select
18309 (not (eq org-support-shift-select 'always))
18310 (org-at-item-bullet-p))
18311 (and (not org-support-shift-select) (org-at-item-p)))
18312 (org-call-with-arg 'org-cycle-list-bullet nil))
18313 ((and (not (eq org-support-shift-select 'always))
18314 (org-at-property-p))
18315 (call-interactively 'org-property-next-allowed-value))
18316 ((org-clocktable-try-shift 'right arg))
18317 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18318 (org-support-shift-select
18319 (org-call-for-shift-select 'forward-char))
18320 (t (org-shiftselect-error))))
18322 (defun org-shiftleft (&optional arg)
18323 "Cycle the thing at point or in the current line, depending on context.
18324 Depending on context, this does one of the following:
18326 - switch a timestamp at point one day into the past
18327 - on a headline, switch to the previous TODO keyword.
18328 - on an item, switch entire list to the previous bullet type
18329 - on a property line, switch to the previous allowed value
18330 - on a clocktable definition line, move time block into the past"
18333 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18334 ((and org-support-shift-select (org-region-active-p))
18335 (org-call-for-shift-select 'backward-char))
18336 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18337 ((and (not (eq org-support-shift-select 'always))
18338 (org-at-heading-p))
18339 (let ((org-inhibit-logging
18340 (not org-treat-S-cursor-todo-selection-as-state-change))
18341 (org-inhibit-blocking
18342 (not org-treat-S-cursor-todo-selection-as-state-change)))
18343 (org-call-with-arg 'org-todo 'left)))
18344 ((or (and org-support-shift-select
18345 (not (eq org-support-shift-select 'always))
18346 (org-at-item-bullet-p))
18347 (and (not org-support-shift-select) (org-at-item-p)))
18348 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18349 ((and (not (eq org-support-shift-select 'always))
18350 (org-at-property-p))
18351 (call-interactively 'org-property-previous-allowed-value))
18352 ((org-clocktable-try-shift 'left arg))
18353 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18354 (org-support-shift-select
18355 (org-call-for-shift-select 'backward-char))
18356 (t (org-shiftselect-error))))
18358 (defun org-shiftcontrolright ()
18359 "Switch to next TODO set."
18362 ((and org-support-shift-select (org-region-active-p))
18363 (org-call-for-shift-select 'forward-word))
18364 ((and (not (eq org-support-shift-select 'always))
18365 (org-at-heading-p))
18366 (org-call-with-arg 'org-todo 'nextset))
18367 (org-support-shift-select
18368 (org-call-for-shift-select 'forward-word))
18369 (t (org-shiftselect-error))))
18371 (defun org-shiftcontrolleft ()
18372 "Switch to previous TODO set."
18375 ((and org-support-shift-select (org-region-active-p))
18376 (org-call-for-shift-select 'backward-word))
18377 ((and (not (eq org-support-shift-select 'always))
18378 (org-at-heading-p))
18379 (org-call-with-arg 'org-todo 'previousset))
18380 (org-support-shift-select
18381 (org-call-for-shift-select 'backward-word))
18382 (t (org-shiftselect-error))))
18384 (defun org-shiftcontrolup ()
18385 "Change timestamps synchronously up in CLOCK log lines."
18387 (cond ((and (not org-support-shift-select)
18388 (org-at-clock-log-p)
18389 (org-at-timestamp-p t))
18390 (org-clock-timestamps-up))
18391 (t (org-shiftselect-error))))
18393 (defun org-shiftcontroldown ()
18394 "Change timestamps synchronously down in CLOCK log lines."
18396 (cond ((and (not org-support-shift-select)
18397 (org-at-clock-log-p)
18398 (org-at-timestamp-p t))
18399 (org-clock-timestamps-down))
18400 (t (org-shiftselect-error))))
18402 (defun org-ctrl-c-ret ()
18403 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18406 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18407 (t (call-interactively 'org-insert-heading))))
18409 (defun org-find-visible ()
18411 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18412 (get-char-property s 'invisible)))
18414 (defun org-find-invisible ()
18416 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18417 (not (get-char-property s 'invisible))))
18420 (defun org-copy-visible (beg end)
18421 "Copy the visible parts of the region."
18426 (narrow-to-region beg end)
18427 (setq s (goto-char (point-min)))
18428 (while (not (= (point) (point-max)))
18429 (goto-char (org-find-invisible))
18430 (push (buffer-substring s (point)) snippets)
18431 (setq s (goto-char (org-find-visible))))))
18432 (kill-new (apply 'concat (nreverse snippets)))))
18434 (defun org-copy-special ()
18435 "Copy region in table or copy current subtree.
18436 Calls `org-table-copy' or `org-copy-subtree', depending on context.
18437 See the individual commands for more information."
18439 (call-interactively
18440 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
18442 (defun org-cut-special ()
18443 "Cut region in table or cut current subtree.
18444 Calls `org-table-copy' or `org-cut-subtree', depending on context.
18445 See the individual commands for more information."
18447 (call-interactively
18448 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
18450 (defun org-paste-special (arg)
18451 "Paste rectangular region into table, or past subtree relative to level.
18452 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
18453 See the individual commands for more information."
18455 (if (org-at-table-p)
18456 (org-table-paste-rectangle)
18457 (org-paste-subtree arg)))
18459 (defun org-edit-special (&optional arg)
18460 "Call a special editor for the stuff at point.
18461 When at a table, call the formula editor with `org-table-edit-formulas'.
18462 When at the first line of an src example, call `org-edit-src-code'.
18463 When in an #+include line, visit the include file. Otherwise call
18464 `ffap' to visit the file at point."
18466 ;; possibly prep session before editing source
18468 (let* ((info (org-babel-get-src-block-info))
18469 (lang (nth 0 info))
18470 (params (nth 2 info))
18471 (session (cdr (assoc :session params))))
18472 (when (and info session) ;; we are in a source-code block with a session
18474 (intern (concat "org-babel-prep-session:" lang)) session params))))
18475 (cond ;; proceed with `org-edit-special'
18477 (beginning-of-line 1)
18478 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
18479 (find-file (org-trim (match-string 1))))
18480 ((org-edit-src-code))
18481 ((org-edit-fixed-width-region))
18482 ((org-at-table.el-p)
18483 (org-edit-src-code))
18484 ((or (org-at-table-p)
18486 (beginning-of-line 1)
18487 (looking-at "[ \t]*#\\+TBLFM:")))
18488 (call-interactively 'org-table-edit-formulas))
18489 (t (call-interactively 'ffap))))
18491 (defvar org-table-coordinate-overlays) ; defined in org-table.el
18492 (defun org-ctrl-c-ctrl-c (&optional arg)
18493 "Set tags in headline, or update according to changed information at point.
18495 This command does many different things, depending on context:
18497 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
18498 this is what we do.
18500 - If the cursor is on a statistics cookie, update it.
18502 - If the cursor is in a headline, prompt for tags and insert them
18503 into the current line, aligned to `org-tags-column'. When called
18504 with prefix arg, realign all tags in the current buffer.
18506 - If the cursor is in one of the special #+KEYWORD lines, this
18507 triggers scanning the buffer for these lines and updating the
18510 - If the cursor is inside a table, realign the table. This command
18511 works even if the automatic table editor has been turned off.
18513 - If the cursor is on a #+TBLFM line, re-apply the formulas to
18516 - If the cursor is at a footnote reference or definition, jump to
18517 the corresponding definition or references, respectively.
18519 - If the cursor is a the beginning of a dynamic block, update it.
18521 - If the current buffer is a capture buffer, close note and file it.
18523 - If the cursor is on a <<<target>>>, update radio targets and
18524 corresponding links in this buffer.
18526 - If the cursor is on a numbered item in a plain list, renumber the
18529 - If the cursor is on a checkbox, toggle it.
18531 - If the cursor is on a code block, evaluate it. The variable
18532 `org-confirm-babel-evaluate' can be used to control prompting
18533 before code block evaluation, by default every code block
18534 evaluation requires confirmation. Code block evaluation can be
18535 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
18537 (let ((org-enable-table-editor t))
18539 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
18540 org-occur-highlights
18541 org-latex-fragment-image-overlays)
18542 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
18543 (org-remove-occur-highlights)
18544 (org-remove-latex-fragment-image-overlays)
18545 (message "Temporary highlights/overlays removed from current buffer"))
18546 ((and (local-variable-p 'org-finish-function (current-buffer))
18547 (fboundp org-finish-function))
18548 (funcall org-finish-function))
18549 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
18550 ((org-in-regexp org-ts-regexp-both)
18551 (org-timestamp-change 0 'day))
18552 ((or (looking-at org-property-start-re)
18553 (org-at-property-p))
18554 (call-interactively 'org-property-action))
18555 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
18556 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
18557 (or (org-at-heading-p) (org-at-item-p)))
18558 (call-interactively 'org-update-statistics-cookies))
18559 ((org-at-heading-p) (call-interactively 'org-set-tags))
18560 ((org-at-table.el-p)
18561 (message "Use C-c ' to edit table.el tables"))
18563 (org-table-maybe-eval-formula)
18565 (call-interactively 'org-table-recalculate)
18566 (org-table-maybe-recalculate-line))
18567 (call-interactively 'org-table-align)
18568 (orgtbl-send-table 'maybe))
18569 ((or (org-footnote-at-reference-p)
18570 (org-footnote-at-definition-p))
18571 (call-interactively 'org-footnote-action))
18572 ((org-at-item-checkbox-p)
18573 ;; Cursor at a checkbox: repair list and update checkboxes. Send
18574 ;; list only if at top item.
18575 (let* ((cbox (match-string 1))
18576 (struct (org-list-struct))
18577 (old-struct (copy-tree struct))
18578 (parents (org-list-parents-alist struct))
18579 (orderedp (org-entry-get nil "ORDERED"))
18580 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
18582 ;; Use a light version of `org-toggle-checkbox' to avoid
18583 ;; computing list structure twice.
18584 (let ((new-box (cond
18585 ((equal arg '(16)) "[-]")
18586 ((equal arg '(4)) nil)
18587 ((equal "[X]" cbox) "[ ]")
18589 (if (and firstp arg)
18590 ;; If at first item of sub-list, remove check-box from
18591 ;; every item at the same level.
18593 (lambda (pos) (org-list-set-checkbox pos struct new-box))
18594 (org-list-get-all-items
18595 (point-at-bol) struct (org-list-prevs-alist struct)))
18596 (org-list-set-checkbox (point-at-bol) struct new-box)))
18597 ;; Replicate `org-list-write-struct', while grabbing a return
18598 ;; value from `org-list-struct-fix-box'.
18599 (org-list-struct-fix-ind struct parents 2)
18600 (org-list-struct-fix-item-end struct)
18601 (let ((prevs (org-list-prevs-alist struct)))
18602 (org-list-struct-fix-bul struct prevs)
18603 (org-list-struct-fix-ind struct parents)
18605 (org-list-struct-fix-box struct parents prevs orderedp)))
18606 (org-list-struct-apply-struct struct old-struct)
18607 (org-update-checkbox-count-maybe)
18610 "Checkboxes were removed due to unchecked box at line %d"
18611 (org-current-line block-item)))
18612 (when firstp (org-list-send-list 'maybe))))
18614 ;; Cursor at an item: repair list. Do checkbox related actions
18615 ;; only if function was called with an argument. Send list only
18617 (let* ((struct (org-list-struct))
18618 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
18621 (setq old-struct (copy-tree struct))
18623 ;; If at first item of sub-list, add check-box to every
18624 ;; item at the same level.
18627 (unless (org-list-get-checkbox pos struct)
18628 (org-list-set-checkbox pos struct "[ ]")))
18629 (org-list-get-all-items
18630 (point-at-bol) struct (org-list-prevs-alist struct)))
18631 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
18632 (org-list-write-struct
18633 struct (org-list-parents-alist struct) old-struct)
18634 (when arg (org-update-checkbox-count-maybe))
18635 (when firstp (org-list-send-list 'maybe))))
18636 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
18638 (beginning-of-line 1)
18639 (save-excursion (org-update-dblock)))
18641 (beginning-of-line 1)
18642 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
18644 ((equal (match-string 1) "TBLFM")
18645 ;; Recalculate the table before this line
18647 (beginning-of-line 1)
18648 (skip-chars-backward " \r\n\t")
18649 (if (org-at-table-p)
18650 (org-call-with-arg 'org-table-recalculate (or arg t)))))
18652 (let ((org-inhibit-startup-visibility-stuff t)
18653 (org-startup-align-all-tables nil))
18654 (when (boundp 'org-table-coordinate-overlays)
18655 (mapc 'delete-overlay org-table-coordinate-overlays)
18656 (setq org-table-coordinate-overlays nil))
18657 (org-save-outline-visibility 'use-markers (org-mode-restart)))
18658 (message "Local setup has been refreshed"))))
18659 ((org-clock-update-time-maybe))
18661 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
18662 (error "C-c C-c can do nothing useful at this location"))))))
18664 (defun org-mode-restart ()
18665 "Restart Org-mode, to scan again for special lines.
18666 Also updates the keyword regular expressions."
18669 (message "Org-mode restarted"))
18671 (defun org-kill-note-or-show-branches ()
18672 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
18674 (if (not org-finish-function)
18677 (call-interactively 'show-branches))
18678 (let ((org-note-abort t))
18679 (funcall org-finish-function))))
18681 (defun org-return (&optional indent)
18682 "Goto next table row or insert a newline.
18683 Calls `org-table-next-row' or `newline', depending on context.
18684 See the individual commands for more information."
18687 ((or (bobp) (org-in-src-block-p))
18688 (if indent (newline-and-indent) (newline)))
18690 (org-table-justify-field-maybe)
18691 (call-interactively 'org-table-next-row))
18692 ;; when `newline-and-indent' is called within a list, make sure
18693 ;; text moved stays inside the item.
18694 ((and (org-in-item-p) indent)
18695 (if (and (org-at-item-p) (>= (point) (match-end 0)))
18697 (save-match-data (newline))
18698 (org-indent-line-to (length (match-string 0))))
18699 (let ((ind (org-get-indentation)))
18701 (if (org-looking-back org-list-end-re)
18702 (org-indent-line-function)
18703 (org-indent-line-to ind)))))
18704 ((and org-return-follows-link
18705 (let ((tprop (get-text-property (point) 'face)))
18706 (or (eq tprop 'org-link)
18707 (and (listp tprop) (memq 'org-link tprop)))))
18708 (call-interactively 'org-open-at-point))
18709 ((and (org-at-heading-p)
18711 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
18715 (t (if indent (newline-and-indent) (newline)))))
18717 (defun org-return-indent ()
18718 "Goto next table row or insert a newline and indent.
18719 Calls `org-table-next-row' or `newline-and-indent', depending on
18720 context. See the individual commands for more information."
18724 (defun org-ctrl-c-star ()
18725 "Compute table, or change heading status of lines.
18726 Calls `org-table-recalculate' or `org-toggle-heading',
18727 depending on context."
18731 (call-interactively 'org-table-recalculate))
18733 ;; Convert all lines in region to list items
18734 (call-interactively 'org-toggle-heading))))
18736 (defun org-ctrl-c-minus ()
18737 "Insert separator line in table or modify bullet status of line.
18738 Also turns a plain line or a region of lines into list items.
18739 Calls `org-table-insert-hline', `org-toggle-item', or
18740 `org-cycle-list-bullet', depending on context."
18744 (call-interactively 'org-table-insert-hline))
18745 ((org-region-active-p)
18746 (call-interactively 'org-toggle-item))
18748 (call-interactively 'org-cycle-list-bullet))
18750 (call-interactively 'org-toggle-item))))
18752 (defun org-toggle-item (arg)
18753 "Convert headings or normal lines to items, items to normal lines.
18754 If there is no active region, only the current line is considered.
18756 If the first non blank line in the region is an headline, convert
18757 all headlines to items, shifting text accordingly.
18759 If it is an item, convert all items to normal lines.
18761 If it is normal text, change region into an item. With a prefix
18762 argument ARG, change each line in region into an item."
18766 ;; Shift text in current section to IND, from point to END.
18767 ;; The function leaves point to END line.
18769 (let ((min-i 1000) (end (copy-marker end)))
18770 ;; First determine the minimum indentation (MIN-I) of
18774 (while (< (point) end)
18775 (let ((i (org-get-indentation)))
18777 ;; Skip blank lines and inline tasks.
18778 ((looking-at "^[ \t]*$"))
18779 ((looking-at org-outline-regexp-bol))
18780 ;; We can't find less than 0 indentation.
18781 ((zerop i) (throw 'exit (setq min-i 0)))
18782 ((< i min-i) (setq min-i i))))
18784 ;; Then indent each line so that a line indented to
18785 ;; MIN-I becomes indented to IND. Ignore blank lines
18786 ;; and inline tasks in the process.
18787 (let ((delta (- ind min-i)))
18788 (while (< (point) end)
18789 (unless (or (looking-at "^[ \t]*$")
18790 (looking-at org-outline-regexp-bol))
18791 (org-indent-line-to (+ (org-get-indentation) delta)))
18792 (forward-line)))))))
18795 ;; Return beginning of first non-blank line, starting from
18800 (skip-chars-forward " \r\t\n")
18803 ;; Determine boundaries of changes.
18804 (if (org-region-active-p)
18805 (setq beg (funcall skip-blanks (region-beginning))
18806 end (copy-marker (region-end)))
18807 (setq beg (funcall skip-blanks (point-at-bol))
18808 end (copy-marker (point-at-eol))))
18809 ;; Depending on the starting line, choose an action on the text
18810 ;; between BEG and END.
18811 (org-with-limited-levels
18815 ;; Case 1. Start at an item: de-itemize. Note that it only
18816 ;; happens when a region is active: `org-ctrl-c-minus'
18817 ;; would call `org-cycle-list-bullet' otherwise.
18819 (while (< (point) end)
18820 (when (org-at-item-p)
18821 (skip-chars-forward " \t")
18822 (delete-region (point) (match-end 0)))
18824 ;; Case 2. Start at an heading: convert to items.
18825 ((org-at-heading-p)
18826 (let* ((bul (org-list-bullet-string "-"))
18827 (bul-len (length bul))
18828 ;; Indentation of the first heading. It should be
18829 ;; relative to the indentation of its parent, if any.
18830 (start-ind (save-excursion
18832 ((not org-adapt-indentation) 0)
18833 ((not (outline-previous-heading)) 0)
18834 (t (length (match-string 0))))))
18835 ;; Level of first heading. Further headings will be
18836 ;; compared to it to determine hierarchy in the list.
18837 (ref-level (org-reduced-level (org-outline-level))))
18838 (while (< (point) end)
18839 (let* ((level (org-reduced-level (org-outline-level)))
18840 (delta (max 0 (- level ref-level))))
18841 ;; If current headline is less indented than the first
18842 ;; one, set it as reference, in order to preserve
18844 (when (< level ref-level) (setq ref-level level))
18845 (replace-match bul t t)
18846 (org-indent-line-to (+ start-ind (* delta bul-len)))
18847 ;; Ensure all text down to END (or SECTION-END) belongs
18848 ;; to the newly created item.
18849 (let ((section-end (save-excursion
18850 (or (outline-next-heading) (point)))))
18852 (funcall shift-text
18853 (+ start-ind (* (1+ delta) bul-len))
18854 (min end section-end)))))))
18855 ;; Case 3. Normal line with ARG: turn each non-item line into
18858 (while (< (point) end)
18859 (unless (or (org-at-heading-p) (org-at-item-p))
18860 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
18862 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
18864 ;; Case 4. Normal line without ARG: make the first line of
18865 ;; region an item, and shift indentation of others
18866 ;; lines to set them as item's body.
18867 (t (let* ((bul (org-list-bullet-string "-"))
18868 (bul-len (length bul))
18869 (ref-ind (org-get-indentation)))
18870 (skip-chars-forward " \t")
18873 (while (< (point) end)
18874 ;; Ensure that lines less indented than first one
18875 ;; still get included in item body.
18876 (funcall shift-text
18877 (+ ref-ind bul-len)
18878 (min end (save-excursion (or (outline-next-heading)
18880 (forward-line)))))))))
18882 (defun org-toggle-heading (&optional nstars)
18883 "Convert headings to normal text, or items or text to headings.
18884 If there is no active region, only the current line is considered.
18886 If the first non blank line is an headline, remove the stars from
18887 all headlines in the region.
18889 If it is a plain list item, turn all plain list items into headings.
18891 If it is a normal line, turn each and every normal line (i.e. not
18892 an heading or an item) in the region into a heading.
18894 When converting a line into a heading, the number of stars is chosen
18895 such that the lines become children of the current entry. However,
18896 when a prefix argument is given, its value determines the number of
18901 ;; Return beginning of first non-blank line, starting from
18906 (skip-chars-forward " \r\t\n")
18909 ;; Determine boundaries of changes. If region ends at a bol, do
18910 ;; not consider the last line to be in the region.
18911 (if (org-region-active-p)
18912 (setq beg (funcall skip-blanks (region-beginning))
18913 end (copy-marker (save-excursion
18914 (goto-char (region-end))
18915 (if (bolp) (point) (point-at-eol)))))
18916 (setq beg (funcall skip-blanks (point-at-bol))
18917 end (copy-marker (point-at-eol))))
18918 ;; Ensure inline tasks don't count as headings.
18919 (org-with-limited-levels
18923 ;; Case 1. Started at an heading: de-star headings.
18924 ((org-at-heading-p)
18925 (while (< (point) end)
18926 (when (org-at-heading-p t)
18927 (looking-at org-outline-regexp) (replace-match ""))
18929 ;; Case 2. Started at an item: change items into headlines.
18930 ;; One star will be added by `org-list-to-subtree'.
18932 (let* ((stars (make-string
18934 ;; subtract the star that will be added again by
18935 ;; `org-list-to-subtree'
18936 (1- (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 (while (< (point) end)
18945 (when (org-at-item-p)
18946 ;; Pay attention to cases when region ends before list.
18947 (let* ((struct (org-list-struct))
18948 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
18950 (narrow-to-region (point) list-end)
18952 (org-list-to-subtree
18953 (org-list-parse-list t)
18954 '(:istart (concat stars add-stars (funcall get-stars depth))
18955 :icount (concat stars add-stars (funcall get-stars depth))))))))
18957 ;; Case 3. Started at normal text: make every line an heading,
18958 ;; skipping headlines and items.
18959 (t (let* ((stars (make-string
18961 (prefix-numeric-value current-prefix-arg)
18962 (or (org-current-level) 0))
18965 (cond (nstars "") ; stars from prefix only
18966 ((equal stars "") "*") ; before first heading
18967 (org-odd-levels-only "**") ; inside heading, odd
18968 (t "*"))) ; inside heading, oddeven
18969 (rpl (concat stars add-stars " ")))
18970 (while (< (point) end)
18971 (when (and (not (org-at-heading-p)) (not (org-at-item-p))
18972 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
18973 (replace-match (concat rpl (match-string 2))))
18974 (forward-line)))))))))
18976 (defun org-meta-return (&optional arg)
18977 "Insert a new heading or wrap a region in a table.
18978 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
18979 See the individual commands for more information."
18982 ((run-hook-with-args-until-success 'org-metareturn-hook))
18983 ((or (org-at-drawer-p) (org-at-property-p))
18984 (newline-and-indent))
18986 (call-interactively 'org-table-wrap-region))
18987 (t (call-interactively 'org-insert-heading))))
18991 ;; Define the Org-mode menus
18992 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
18994 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
18995 ["Next Field" org-cycle (org-at-table-p)]
18996 ["Previous Field" org-shifttab (org-at-table-p)]
18997 ["Next Row" org-return (org-at-table-p)]
18999 ["Blank Field" org-table-blank-field (org-at-table-p)]
19000 ["Edit Field" org-table-edit-field (org-at-table-p)]
19001 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19004 ["Move Column Left" org-metaleft (org-at-table-p)]
19005 ["Move Column Right" org-metaright (org-at-table-p)]
19006 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19007 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19009 ["Move Row Up" org-metaup (org-at-table-p)]
19010 ["Move Row Down" org-metadown (org-at-table-p)]
19011 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19012 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19013 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19015 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19017 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19018 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19019 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19020 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19023 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19024 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19025 ["Edit Formulas" org-edit-special (org-at-table-p)]
19027 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19028 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19029 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19031 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19033 ["Sum Column/Rectangle" org-table-sum
19034 (or (org-at-table-p) (org-region-active-p))]
19035 ["Which Column?" org-table-current-column (org-at-table-p)])
19037 org-table-toggle-formula-debugger
19038 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19039 ["Show Col/Row Numbers"
19040 org-table-toggle-coordinate-overlays
19042 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19044 ["Create" org-table-create (and (not (org-at-table-p))
19045 org-enable-table-editor)]
19046 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19047 ["Import from File" org-table-import (not (org-at-table-p))]
19048 ["Export to File" org-table-export (org-at-table-p)]
19050 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19052 (easy-menu-define org-org-menu org-mode-map "Org menu"
19055 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19056 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19057 ["Sparse Tree..." org-sparse-tree t]
19058 ["Reveal Context" org-reveal t]
19059 ["Show All" show-all t]
19061 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19063 ["New Heading" org-insert-heading t]
19064 ("Navigate Headings"
19065 ["Up" outline-up-heading t]
19066 ["Next" outline-next-visible-heading t]
19067 ["Previous" outline-previous-visible-heading t]
19068 ["Next Same Level" outline-forward-same-level t]
19069 ["Previous Same Level" outline-backward-same-level t]
19071 ["Jump" org-goto t])
19073 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
19074 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
19076 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
19077 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
19078 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19080 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19082 ["Copy visible text" org-copy-visible t]
19084 ["Promote Heading" org-metaleft (not (org-at-table-p))]
19085 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
19086 ["Demote Heading" org-metaright (not (org-at-table-p))]
19087 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
19089 ["Sort Region/Children" org-sort (not (org-at-table-p))]
19091 ["Convert to odd levels" org-convert-to-odd-levels t]
19092 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19094 ["Emphasis..." org-emphasize t]
19095 ["Edit Source Example" org-edit-special t]
19097 ["Footnote new/jump" org-footnote-action t]
19098 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19100 ["Archive (default method)" org-archive-subtree-default t]
19102 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
19103 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
19104 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
19108 ["Store Link (Global)" org-store-link t]
19109 ["Find existing link to here" org-occur-link-in-agenda-files t]
19110 ["Insert Link" org-insert-link t]
19111 ["Follow Link" org-open-at-point t]
19113 ["Next link" org-next-link t]
19114 ["Previous link" org-previous-link t]
19116 ["Descriptive Links"
19117 org-toggle-link-display
19119 :selected org-descriptive-links
19122 org-toggle-link-display
19124 :selected (not org-descriptive-links)])
19127 ["TODO/DONE/-" org-todo t]
19129 ["Next keyword" org-shiftright (org-at-heading-p)]
19130 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19131 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19132 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19133 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19134 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19135 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19137 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19138 :selected org-enforce-todo-dependencies :style toggle :active t]
19139 "Settings for tree at point"
19140 ["Do Children sequentially" org-toggle-ordered-property :style radio
19141 :selected (org-entry-get nil "ORDERED")
19142 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19143 ["Do Children parallel" org-toggle-ordered-property :style radio
19144 :selected (not (org-entry-get nil "ORDERED"))
19145 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19147 ["Set Priority" org-priority t]
19148 ["Priority Up" org-shiftup t]
19149 ["Priority Down" org-shiftdown t]
19151 ["Get news from all feeds" org-feed-update-all t]
19152 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19153 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19154 ("TAGS and Properties"
19155 ["Set Tags" org-set-tags-command t]
19156 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19158 ["Set property" org-set-property t]
19159 ["Column view of properties" org-columns t]
19160 ["Insert Column View DBlock" org-insert-columns-dblock t])
19161 ("Dates and Scheduling"
19162 ["Timestamp" org-time-stamp t]
19163 ["Timestamp (inactive)" org-time-stamp-inactive t]
19165 ["1 Day Later" org-shiftright t]
19166 ["1 Day Earlier" org-shiftleft t]
19167 ["1 ... Later" org-shiftup t]
19168 ["1 ... Earlier" org-shiftdown t])
19169 ["Compute Time Range" org-evaluate-time-range t]
19170 ["Schedule Item" org-schedule t]
19171 ["Deadline" org-deadline t]
19173 ["Custom time format" org-toggle-time-stamp-overlays
19174 :style radio :selected org-display-custom-times]
19176 ["Goto Calendar" org-goto-calendar t]
19177 ["Date from Calendar" org-date-from-calendar t]
19179 ["Start/Restart Timer" org-timer-start t]
19180 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19181 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19182 ["Insert Timer String" org-timer t]
19183 ["Insert Timer Item" org-timer-item t])
19185 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19186 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19187 ["Clock out" org-clock-out t]
19188 ["Clock cancel" org-clock-cancel t]
19190 ["Mark as default task" org-clock-mark-default-task t]
19191 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19192 ["Goto running clock" org-clock-goto t]
19194 ["Display times" org-clock-display t]
19195 ["Create clock table" org-clock-report t]
19197 ["Record DONE time"
19198 (progn (setq org-log-done (not org-log-done))
19199 (message "Switching to %s will %s record a timestamp"
19200 (car org-done-keywords)
19201 (if org-log-done "automatically" "not")))
19202 :style toggle :selected org-log-done])
19204 ["Agenda Command..." org-agenda t]
19205 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19206 ("File List for Agenda")
19207 ("Special views current file"
19208 ["TODO Tree" org-show-todo-tree t]
19209 ["Check Deadlines" org-check-deadlines t]
19210 ["Timeline" org-timeline t]
19211 ["Tags/Property tree" org-match-sparse-tree t])
19213 ["Export/Publish..." org-export t]
19215 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19216 :selected org-cdlatex-mode]
19217 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19218 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19219 ["Modify math symbol" org-cdlatex-math-modify
19220 (org-inside-LaTeX-fragment-p)]
19221 ["Insert citation" org-reftex-citation t]
19223 ["Template for BEAMER" (progn (require 'org-beamer)
19224 (org-insert-beamer-options-template)) t])
19227 ["Push Files and Views" org-mobile-push t]
19228 ["Get Captured and Flagged" org-mobile-pull t]
19229 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19231 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19234 ["Show Version" org-version t]
19235 ["Info Documentation" org-info t])
19237 ["Browse Org Group" org-customize t]
19239 ["Expand This Menu" org-create-customize-menu
19240 (fboundp 'customize-menu-create)])
19241 ["Send bug report" org-submit-bug-report t]
19244 ["Refresh setup current buffer" org-mode-restart t]
19245 ["Reload Org (after update)" org-reload t]
19246 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
19249 (defun org-info (&optional node)
19250 "Read documentation for Org-mode in the info system.
19251 With optional NODE, go directly to that node."
19253 (info (format "(org)%s" (or node ""))))
19256 (defun org-submit-bug-report ()
19257 "Submit a bug report on Org-mode via mail.
19259 Don't hesitate to report any problems or inaccurate documentation.
19261 If you don't have setup sending mail from (X)Emacs, please copy the
19262 output buffer into your mail program, as it gives us important
19263 information about your Org-mode version and configuration."
19265 (require 'reporter)
19266 (org-load-modules-maybe)
19267 (org-require-autoloaded-modules)
19268 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19269 (reporter-submit-bug-report
19270 "emacs-orgmode@gnu.org"
19273 (save-window-excursion
19274 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19275 (delete-other-windows)
19277 (insert "You are about to submit a bug report to the Org-mode mailing list.
19279 We would like to add your full Org-mode and Outline configuration to the
19280 bug report. This greatly simplifies the work of the maintainer and
19281 other experts on the mailing list.
19283 HOWEVER, some variables you have customized may contain private
19284 information. The names of customers, colleagues, or friends, might
19285 appear in the form of file names, tags, todo states, or search strings.
19286 If you answer yes to the prompt, you might want to check and remove
19287 such private information before sending the email.")
19288 (add-text-properties (point-min) (point-max) '(face org-warning))
19289 (when (yes-or-no-p "Include your Org-mode configuration ")
19293 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19294 (or (and (symbol-value v)
19295 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19297 (get v 'custom-type) (get v 'standard-value)
19298 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19300 (kill-buffer (get-buffer "*Warn about privacy*"))
19303 "Remember to cover the basics, that is, what you expected to happen and
19304 what in fact did happen. You don't know how to make a good report? See
19306 http://orgmode.org/manual/Feedback.html#Feedback
19308 Your bug report will be posted to the Org-mode mailing list.
19309 ------------------------------------------------------------------------")
19311 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19312 (replace-match "\\1Bug: \\3 [\\2]")))))
19315 (defun org-install-agenda-files-menu ()
19316 (let ((bl (buffer-list)))
19319 (set-buffer (pop bl))
19320 (if (eq major-mode 'org-mode) (setq bl nil)))
19321 (when (eq major-mode 'org-mode)
19323 '("Org") "File List for Agenda"
19326 ["Edit File List" (org-edit-agenda-file-list) t]
19327 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19328 ["Remove Current File from List" org-remove-file t]
19329 ["Cycle through agenda files" org-cycle-agenda-files t]
19330 ["Occur in all agenda files" org-occur-in-agenda-files t]
19332 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19337 (defun org-require-autoloaded-modules ()
19340 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19341 org-docbook org-exp org-html org-icalendar
19343 org-publish org-remember org-table
19344 org-timer org-xoxo)))
19347 (defun org-reload (&optional uncompiled)
19348 "Reload all org lisp files.
19349 With prefix arg UNCOMPILED, load the uncompiled versions."
19351 (require 'find-func)
19352 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
19353 (dir-org (file-name-directory (org-find-library-name "org")))
19354 (dir-org-contrib (ignore-errors
19355 (file-name-directory
19356 (org-find-library-name "org-contribdir"))))
19358 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19359 (append (list nil "comint" "eval" "exp" "keys"
19360 "lob" "ref" "table" "tangle")
19364 (when (cdr lang) (symbol-name (car lang))))
19365 org-babel-load-languages)))))
19367 (append (directory-files dir-org t file-re)
19369 (and dir-org-contrib
19370 (directory-files dir-org-contrib t file-re))))
19371 (remove-re (concat (if (featurep 'xemacs)
19372 "org-colview" "org-colview-xemacs")
19374 (setq files (mapcar 'file-name-sans-extension files))
19375 (setq files (mapcar
19376 (lambda (x) (if (string-match remove-re x) nil x))
19378 (setq files (delq nil files))
19381 (when (featurep (intern (file-name-nondirectory f)))
19382 (if (and (not uncompiled)
19383 (file-exists-p (concat f ".elc")))
19384 (load (concat f ".elc") nil nil t)
19385 (load (concat f ".el") nil nil t))))
19390 (defun org-customize ()
19391 "Call the customize function with org as argument."
19393 (org-load-modules-maybe)
19394 (org-require-autoloaded-modules)
19395 (customize-browse 'org))
19397 (defun org-create-customize-menu ()
19398 "Create a full customization menu for Org-mode, insert it into the menu."
19400 (org-load-modules-maybe)
19401 (org-require-autoloaded-modules)
19402 (if (fboundp 'customize-menu-create)
19405 '("Org") "Customize"
19406 `(["Browse Org group" org-customize t]
19408 ,(customize-menu-create 'org)
19409 ["Set" Custom-set t]
19410 ["Save" Custom-save t]
19411 ["Reset to Current" Custom-reset-current t]
19412 ["Reset to Saved" Custom-reset-saved t]
19413 ["Reset to Standard Settings" Custom-reset-standard t]))
19414 (message "\"Org\"-menu now contains full customization menu"))
19415 (error "Cannot expand menu (outdated version of cus-edit.el)")))
19417 ;;;; Miscellaneous stuff
19419 ;;; Generally useful functions
19421 (defun org-get-at-bol (property)
19422 "Get text property PROPERTY at beginning of line."
19423 (get-text-property (point-at-bol) property))
19425 (defun org-find-text-property-in-string (prop s)
19426 "Return the first non-nil value of property PROP in string S."
19427 (or (get-text-property 0 prop s)
19428 (get-text-property (or (next-single-property-change 0 prop s) 0)
19431 (defun org-display-warning (message) ;; Copied from Emacs-Muse
19432 "Display the given MESSAGE as a warning."
19433 (if (fboundp 'display-warning)
19434 (display-warning 'org message
19435 (if (featurep 'xemacs) 'warning :warning))
19436 (let ((buf (get-buffer-create "*Org warnings*")))
19437 (with-current-buffer buf
19438 (goto-char (point-max))
19439 (insert "Warning (Org): " message)
19442 (display-buffer buf)
19445 (defun org-eval (form)
19446 "Eval FORM and return result."
19447 (condition-case error
19449 (error (format "%%![Error: %s]" error))))
19451 (defun org-in-clocktable-p ()
19452 "Check if the cursor is in a clocktable."
19453 (let ((pos (point)) start)
19456 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
19457 (setq start (match-beginning 0))
19458 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
19459 (>= (match-end 0) pos)
19462 (defun org-in-commented-line ()
19463 "Is point in a line starting with `#'?"
19464 (equal (char-after (point-at-bol)) ?#))
19466 (defun org-in-indented-comment-line ()
19467 "Is point in a line starting with `#' after some white space?"
19470 (goto-char (point-at-bol))
19471 (looking-at "[ \t]*#"))))
19473 (defun org-in-verbatim-emphasis ()
19475 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
19477 (defun org-goto-marker-or-bmk (marker &optional bookmark)
19478 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
19479 (if (and marker (marker-buffer marker)
19480 (buffer-live-p (marker-buffer marker)))
19482 (org-pop-to-buffer-same-window (marker-buffer marker))
19483 (if (or (> marker (point-max)) (< marker (point-min)))
19486 (org-show-context 'org-goto))
19488 (bookmark-jump bookmark)
19489 (error "Cannot find location"))))
19491 (defun org-quote-csv-field (s)
19492 "Quote field for inclusion in CSV material."
19493 (if (string-match "[\",]" s)
19494 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
19497 (defun org-force-self-insert (N)
19498 "Needed to enforce self-insert under remapping."
19500 (self-insert-command N))
19502 (defun org-string-width (s)
19503 "Compute width of string, ignoring invisible characters.
19504 This ignores character with invisibility property `org-link', and also
19505 characters with property `org-cwidth', because these will become invisible
19506 upon the next fontification round."
19508 (when (or (eq t buffer-invisibility-spec)
19509 (assq 'org-link buffer-invisibility-spec))
19510 (while (setq b (text-property-any 0 (length s)
19511 'invisible 'org-link s))
19512 (setq s (concat (substring s 0 b)
19513 (substring s (or (next-single-property-change
19514 b 'invisible s) (length s)))))))
19515 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
19516 (setq s (concat (substring s 0 b)
19517 (substring s (or (next-single-property-change
19518 b 'org-cwidth s) (length s))))))
19519 (setq l (string-width s) b -1)
19520 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
19521 (setq l (- l (get-text-property b 'org-dwidth-n s))))
19524 (defun org-shorten-string (s maxlength)
19525 "Shorten string S so tht it is no longer than MAXLENGTH characters.
19526 If the string is shorter or has length MAXLENGTH, just return the
19527 original string. If it is longer, the functions finds a space in the
19528 string, breaks this string off at that locations and adds three dots
19529 as ellipsis. Including the ellipsis, the string will not be longer
19530 than MAXLENGTH. If finding a good breaking point in the string does
19531 not work, the string is just chopped off in the middle of a word
19533 (if (<= (length s) maxlength)
19535 (let* ((n (max (- maxlength 4) 1))
19536 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
19537 (if (string-match re s)
19538 (concat (match-string 1 s) "...")
19539 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
19541 (defun org-get-indentation (&optional line)
19542 "Get the indentation of the current line, interpreting tabs.
19543 When LINE is given, assume it represents a line and compute its indentation."
19545 (if (string-match "^ *" (org-remove-tabs line))
19548 (beginning-of-line 1)
19549 (skip-chars-forward " \t")
19550 (current-column))))
19552 (defun org-get-string-indentation (s)
19553 "What indentation has S due to SPACE and TAB at the beginning of the string?"
19554 (let ((n -1) (i 0) (w tab-width) c)
19556 (while (< (setq n (1+ n)) (length s))
19557 (setq c (aref s n))
19558 (cond ((= c ?\ ) (setq i (1+ i)))
19559 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
19560 (t (throw 'exit t)))))
19563 (defun org-remove-tabs (s &optional width)
19564 "Replace tabulators in S with spaces.
19565 Assumes that s is a single line, starting in column 0."
19566 (setq width (or width tab-width))
19567 (while (string-match "\t" s)
19568 (setq s (replace-match
19570 (- (* width (/ (+ (match-beginning 0) width) width))
19571 (match-beginning 0)) ?\ )
19575 (defun org-fix-indentation (line ind)
19576 "Fix indentation in LINE.
19577 IND is a cons cell with target and minimum indentation.
19578 If the current indentation in LINE is smaller than the minimum,
19579 leave it alone. If it is larger than ind, set it to the target."
19580 (let* ((l (org-remove-tabs line))
19581 (i (org-get-indentation l))
19582 (i1 (car ind)) (i2 (cdr ind)))
19583 (if (>= i i2) (setq l (substring line i2)))
19585 (concat (make-string i1 ?\ ) l)
19588 (defun org-remove-indentation (code &optional n)
19589 "Remove the maximum common indentation from the lines in CODE.
19590 N may optionally be the number of spaces to remove."
19593 (org-do-remove-indentation n)
19596 (defun org-do-remove-indentation (&optional n)
19597 "Remove the maximum common indentation from the buffer."
19598 (untabify (point-min) (point-max))
19599 (let ((min 10000) re)
19602 (goto-char (point-min))
19603 (while (re-search-forward "^ *[^ \n]" nil t)
19604 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
19605 (unless (or (= min 0) (= min 10000))
19606 (setq re (format "^ \\{%d\\}" min))
19607 (goto-char (point-min))
19608 (while (re-search-forward re nil t)
19613 (defun org-fill-template (template alist)
19614 "Find each %key of ALIST in TEMPLATE and replace it."
19615 (let ((case-fold-search nil)
19617 (setq alist (sort (copy-sequence alist)
19618 (lambda (a b) (< (length (car a)) (length (car b))))))
19619 (while (setq entry (pop alist))
19621 (replace-regexp-in-string
19622 (concat "%" (regexp-quote (car entry)))
19623 (cdr entry) template t t)))
19626 (defun org-base-buffer (buffer)
19627 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
19630 (or (buffer-base-buffer buffer)
19633 (defun org-trim (s)
19634 "Remove whitespace at beginning and end of string."
19635 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
19636 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
19639 (defun org-wrap (string &optional width lines)
19640 "Wrap string to either a number of lines, or a width in characters.
19641 If WIDTH is non-nil, the string is wrapped to that width, however many lines
19642 that costs. If there is a word longer than WIDTH, the text is actually
19643 wrapped to the length of that word.
19644 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
19645 many lines, whatever width that takes.
19646 The return value is a list of lines, without newlines at the end."
19647 (let* ((words (org-split-string string "[ \t\n]+"))
19648 (maxword (apply 'max (mapcar 'org-string-width words)))
19651 (org-do-wrap words (max maxword width)))
19654 (setq ll (org-do-wrap words maxword))
19655 (if (<= (length ll) lines)
19658 (while (> (length ll) lines)
19660 (setq ll (org-do-wrap words w)))
19662 (t (error "Cannot wrap this")))))
19664 (defun org-do-wrap (words width)
19665 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
19668 (setq line (pop words))
19669 (while (and words (< (+ (length line) (length (car words))) width))
19670 (setq line (concat line " " (pop words))))
19671 (setq lines (push line lines)))
19674 (defun org-split-string (string &optional separators)
19675 "Splits STRING into substrings at SEPARATORS.
19676 No empty strings are returned if there are matches at the beginning
19677 and end of string."
19678 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
19682 (while (and (string-match rexp string
19684 (= start (match-beginning 0))
19685 (< start (length string)))
19687 (< (match-beginning 0) (length string)))
19689 (or (eq (match-beginning 0) 0)
19690 (and (eq (match-beginning 0) (match-end 0))
19691 (eq (match-beginning 0) start))
19693 (cons (substring string start (match-beginning 0))
19695 (setq start (match-end 0)))
19696 (or (eq start (length string))
19698 (cons (substring string start)
19702 (defun org-quote-vert (s)
19703 "Replace \"|\" with \"\\vert\"."
19704 (while (string-match "|" s)
19705 (setq s (replace-match "\\vert" t t s)))
19708 (defun org-uuidgen-p (s)
19709 "Is S an ID created by UUIDGEN?"
19710 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
19712 (defun org-in-src-block-p nil
19713 "Whether point is in a code source block."
19715 (when (setq ov (overlays-at (point)))
19716 (memq 'org-block-background
19717 (overlay-properties
19720 (defun org-context ()
19721 "Return a list of contexts of the current cursor position.
19722 If several contexts apply, all are returned.
19723 Each context entry is a list with a symbol naming the context, and
19724 two positions indicating start and end of the context. Possible
19727 :headline anywhere in a headline
19728 :headline-stars on the leading stars in a headline
19729 :todo-keyword on a TODO keyword (including DONE) in a headline
19730 :tags on the TAGS in a headline
19731 :priority on the priority cookie in a headline
19732 :item on the first line of a plain list item
19733 :item-bullet on the bullet/number of a plain list item
19734 :checkbox on the checkbox in a plain list item
19735 :table in an org-mode table
19736 :table-special on a special filed in a table
19737 :table-table in a table.el table
19738 :clocktable in a clocktable
19739 :src-block in a source block
19740 :link on a hyperlink
19741 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
19742 :target on a <<target>>
19743 :radio-target on a <<<radio-target>>>
19744 :latex-fragment on a LaTeX fragment
19745 :latex-preview on a LaTeX fragment with overlaid preview image
19747 This function expects the position to be visible because it uses font-lock
19748 faces as a help to recognize the following contexts: :table-special, :link,
19750 (let* ((f (get-text-property (point) 'face))
19751 (faces (if (listp f) f (list f)))
19752 (case-fold-search t)
19753 (p (point)) clist o)
19754 ;; First the large context
19756 ((org-at-heading-p t)
19757 (push (list :headline (point-at-bol) (point-at-eol)) clist)
19759 (beginning-of-line 1)
19760 (looking-at org-todo-line-tags-regexp))
19761 (push (org-point-in-group p 1 :headline-stars) clist)
19762 (push (org-point-in-group p 2 :todo-keyword) clist)
19763 (push (org-point-in-group p 4 :tags) clist))
19765 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
19766 (if (looking-at "\\[#[A-Z0-9]\\]")
19767 (push (org-point-in-group p 0 :priority) clist)))
19770 (push (org-point-in-group p 2 :item-bullet) clist)
19771 (push (list :item (point-at-bol)
19772 (save-excursion (org-end-of-item) (point)))
19774 (and (org-at-item-checkbox-p)
19775 (push (org-point-in-group p 0 :checkbox) clist)))
19778 (push (list :table (org-table-begin) (org-table-end)) clist)
19779 (if (memq 'org-formula faces)
19780 (push (list :table-special
19781 (previous-single-property-change p 'face)
19782 (next-single-property-change p 'face)) clist)))
19783 ((org-at-table-p 'any)
19784 (push (list :table-table) clist)))
19787 ;; New the "medium" contexts: clocktables, source blocks
19788 (cond ((org-in-clocktable-p)
19789 (push (list :clocktable
19790 (and (or (looking-at "#\\+BEGIN: clocktable")
19791 (search-backward "#+BEGIN: clocktable" nil t))
19792 (match-beginning 0))
19793 (and (re-search-forward "#\\+END:?" nil t)
19794 (match-end 0))) clist))
19795 ((org-in-src-block-p)
19796 (push (list :src-block
19797 (and (or (looking-at "#\\+BEGIN_SRC")
19798 (search-backward "#+BEGIN_SRC" nil t))
19799 (match-beginning 0))
19800 (and (search-forward "#+END_SRC" nil t)
19801 (match-beginning 0))) clist)))
19804 ;; Now the small context
19806 ((org-at-timestamp-p)
19807 (push (org-point-in-group p 0 :timestamp) clist))
19808 ((memq 'org-link faces)
19810 (previous-single-property-change p 'face)
19811 (next-single-property-change p 'face)) clist))
19812 ((memq 'org-special-keyword faces)
19813 (push (list :keyword
19814 (previous-single-property-change p 'face)
19815 (next-single-property-change p 'face)) clist))
19817 (push (org-point-in-group p 0 :target) clist)
19818 (goto-char (1- (match-beginning 0)))
19819 (if (looking-at org-radio-target-regexp)
19820 (push (org-point-in-group p 0 :radio-target) clist))
19822 ((setq o (car (delq nil
19825 (if (memq x org-latex-fragment-image-overlays) x))
19826 (overlays-at (point))))))
19827 (push (list :latex-fragment
19828 (overlay-start o) (overlay-end o)) clist)
19829 (push (list :latex-preview
19830 (overlay-start o) (overlay-end o)) clist))
19831 ((org-inside-LaTeX-fragment-p)
19832 ;; FIXME: positions wrong.
19833 (push (list :latex-fragment (point) (point)) clist)))
19835 (setq clist (nreverse (delq nil clist)))
19838 ;; FIXME: Compare with at-regexp-p Do we need both?
19839 (defun org-in-regexp (re &optional nlines visually)
19840 "Check if point is inside a match of regexp.
19841 Normally only the current line is checked, but you can include NLINES extra
19842 lines both before and after point into the search.
19843 If VISUALLY is set, require that the cursor is not after the match but
19844 really on, so that the block visually is on the match."
19846 (let ((pos (point))
19847 (eol (point-at-eol (+ 1 (or nlines 0))))
19848 (inc (if visually 1 0)))
19850 (beginning-of-line (- 1 (or nlines 0)))
19851 (while (re-search-forward re eol t)
19852 (if (and (<= (match-beginning 0) pos)
19853 (>= (+ inc (match-end 0)) pos))
19854 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
19856 (defun org-at-regexp-p (regexp)
19857 "Is point inside a match of REGEXP in the current line?"
19860 (let ((pos (point)) (end (point-at-eol)))
19861 (beginning-of-line 1)
19862 (while (re-search-forward regexp end t)
19863 (if (and (<= (match-beginning 0) pos)
19864 (>= (match-end 0) pos))
19868 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
19869 "Non-nil when point is between matches of START-RE and END-RE.
19871 Also return a non-nil value when point is on one of the matches.
19873 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
19874 buffer positions. Default values are the positions of headlines
19875 surrounding the point.
19877 The functions returns a cons cell whose car (resp. cdr) is the
19878 position before START-RE (resp. after END-RE)."
19880 (let ((pos (point))
19881 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
19882 (limit-down (or lim-down (save-excursion (outline-next-heading))))
19885 ;; Point is on a block when on START-RE or if START-RE can be
19886 ;; found before it...
19887 (and (or (org-at-regexp-p start-re)
19888 (re-search-backward start-re limit-up t))
19889 (setq beg (match-beginning 0))
19890 ;; ... and END-RE after it...
19891 (goto-char (match-end 0))
19892 (re-search-forward end-re limit-down t)
19893 (> (setq end (match-end 0)) pos)
19894 ;; ... without another START-RE in-between.
19895 (goto-char (match-beginning 0))
19896 (not (re-search-backward start-re (1+ beg) t))
19898 (cons beg end))))))
19900 (defun org-in-block-p (names)
19901 "Non-nil when point belongs to a block whose name belongs to NAMES.
19903 NAMES is a list of strings containing names of blocks.
19905 Return first block name matched, or nil. Beware that in case of
19906 nested blocks, the returned name may not belong to the closest
19910 (let ((case-fold-search t)
19911 (lim-up (save-excursion (outline-previous-heading)))
19912 (lim-down (save-excursion (outline-next-heading))))
19913 (mapc (lambda (name)
19914 (let ((n (regexp-quote name)))
19915 (when (org-between-regexps-p
19916 (concat "^[ \t]*#\\+begin_" n)
19917 (concat "^[ \t]*#\\+end_" n)
19923 (defun org-occur-in-agenda-files (regexp &optional nlines)
19924 "Call `multi-occur' with buffers for all agenda files."
19925 (interactive "sOrg-files matching: \np")
19926 (let* ((files (org-agenda-files))
19927 (tnames (mapcar 'file-truename files))
19928 (extra org-agenda-text-search-extra-files)
19930 (when (eq (car extra) 'agenda-archives)
19931 (setq extra (cdr extra))
19932 (setq files (org-add-archive-files files)))
19933 (while (setq f (pop extra))
19934 (unless (member (file-truename f) tnames)
19935 (add-to-list 'files f 'append)
19936 (add-to-list 'tnames (file-truename f) 'append)))
19938 (mapcar (lambda (x)
19939 (with-current-buffer
19940 (or (get-file-buffer x) (find-file-noselect x))
19946 (if (boundp 'occur-mode-find-occurrence-hook)
19948 (add-hook 'occur-mode-find-occurrence-hook
19950 (when (eq major-mode 'org-mode)
19953 (defadvice occur-mode-goto-occurrence
19954 (after org-occur-reveal activate)
19955 (and (eq major-mode 'org-mode) (org-reveal)))
19956 (defadvice occur-mode-goto-occurrence-other-window
19957 (after org-occur-reveal activate)
19958 (and (eq major-mode 'org-mode) (org-reveal)))
19959 (defadvice occur-mode-display-occurrence
19960 (after org-occur-reveal activate)
19961 (when (eq major-mode 'org-mode)
19962 (let ((pos (occur-mode-find-occurrence)))
19963 (with-current-buffer (marker-buffer pos)
19968 (defun org-occur-link-in-agenda-files ()
19969 "Create a link and search for it in the agendas.
19970 The link is not stored in `org-stored-links', it is just created
19971 for the search purpose."
19973 (let ((link (condition-case nil
19974 (org-store-link nil)
19975 (error "Unable to create a link to here"))))
19976 (org-occur-in-agenda-files (regexp-quote link))))
19978 (defun org-uniquify (list)
19979 "Remove duplicate elements from LIST."
19981 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
19984 (defun org-delete-all (elts list)
19985 "Remove all elements in ELTS from LIST."
19987 (setq list (delete (pop elts) list)))
19990 (defun org-count (cl-item cl-seq)
19991 "Count the number of occurrences of ITEM in SEQ.
19992 Taken from `count' in cl-seq.el with all keyword arguments removed."
19993 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
19994 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
19995 (while (< cl-start cl-end)
19996 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
19997 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
19998 (setq cl-start (1+ cl-start)))
20001 (defun org-remove-if (predicate seq)
20002 "Remove everything from SEQ that fulfills PREDICATE."
20006 (if (not (funcall predicate e)) (push e res)))
20009 (defun org-remove-if-not (predicate seq)
20010 "Remove everything from SEQ that does not fulfill PREDICATE."
20014 (if (funcall predicate e) (push e res)))
20017 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20018 "Reduce two-argument FUNCTION across SEQ.
20019 Taken from `reduce' in cl-seq.el with all keyword arguments but
20020 \":initial-value\" removed."
20021 (let ((cl-accum (cond ((memq :initial-value cl-keys)
20022 (cadr (memq :initial-value cl-keys)))
20023 (cl-seq (pop cl-seq))
20024 (t (funcall cl-func)))))
20026 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20029 (defun org-back-over-empty-lines ()
20030 "Move backwards over whitespace, to the beginning of the first empty line.
20031 Returns the number of empty lines passed."
20032 (let ((pos (point)))
20033 (if (cdr (assoc 'heading org-blank-before-new-entry))
20034 (skip-chars-backward " \t\n\r")
20036 (forward-line -1)))
20037 (beginning-of-line 2)
20038 (goto-char (min (point) pos))
20039 (count-lines (point) pos)))
20041 (defun org-skip-whitespace ()
20042 (skip-chars-forward " \t\n\r"))
20044 (defun org-point-in-group (point group &optional context)
20045 "Check if POINT is in match-group GROUP.
20046 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20047 match. If the match group does not exist or point is not inside it,
20049 (and (match-beginning group)
20050 (>= point (match-beginning group))
20051 (<= point (match-end group))
20053 (list context (match-beginning group) (match-end group))
20056 (defun org-switch-to-buffer-other-window (&rest args)
20057 "Switch to buffer in a second window on the current frame.
20058 In particular, do not allow pop-up frames.
20059 Returns the newly created buffer."
20060 (let (pop-up-frames special-display-buffer-names special-display-regexps
20061 special-display-function)
20062 (apply 'switch-to-buffer-other-window args)))
20064 (defun org-combine-plists (&rest plists)
20065 "Create a single property list from all plists in PLISTS.
20066 The process starts by copying the first list, and then setting properties
20067 from the other lists. Settings in the last list are the most significant
20068 ones and overrule settings in the other lists."
20069 (let ((rtn (copy-sequence (pop plists)))
20072 (setq ls (pop plists))
20074 (setq p (pop ls) v (pop ls))
20075 (setq rtn (plist-put rtn p v))))
20078 (defun org-move-line-down (arg)
20079 "Move the current line down. With prefix argument, move it past ARG lines."
20081 (let ((col (current-column))
20083 (beginning-of-line 1) (setq beg (point))
20084 (beginning-of-line 2) (setq end (point))
20085 (beginning-of-line (+ 1 arg))
20086 (setq pos (move-marker (make-marker) (point)))
20087 (insert (delete-and-extract-region beg end))
20089 (org-move-to-column col)))
20091 (defun org-move-line-up (arg)
20092 "Move the current line up. With prefix argument, move it past ARG lines."
20094 (let ((col (current-column))
20096 (beginning-of-line 1) (setq beg (point))
20097 (beginning-of-line 2) (setq end (point))
20098 (beginning-of-line (- arg))
20099 (setq pos (move-marker (make-marker) (point)))
20100 (insert (delete-and-extract-region beg end))
20102 (org-move-to-column col)))
20104 (defun org-replace-escapes (string table)
20105 "Replace %-escapes in STRING with values in TABLE.
20106 TABLE is an association list with keys like \"%a\" and string values.
20107 The sequences in STRING may contain normal field width and padding information,
20108 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20109 so values can contain further %-escapes if they are define later in TABLE."
20110 (let ((tbl (copy-alist table))
20111 (case-fold-search nil)
20114 (while (setq e (pop tbl))
20115 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20116 (when (and (cdr e) (string-match re (cdr e)))
20117 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20119 (add-text-properties 0 3 (list 'sref sref) safe)
20120 (setcdr e (replace-match safe t t (cdr e)))))
20121 (while (string-match re string)
20122 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20124 (setq string (replace-match rpl t t string))))
20125 (while (setq pchg (next-property-change pchg string))
20126 (let ((sref (get-text-property pchg 'sref string)))
20127 (when (and sref (string-match "SREF" string pchg))
20128 (setq string (replace-match sref t t string)))))
20131 (defun org-sublist (list start end)
20132 "Return a section of LIST, from START to END.
20133 Counting starts at 1."
20134 (let (rtn (c start))
20135 (setq list (nthcdr (1- start) list))
20136 (while (and list (<= c end))
20137 (push (pop list) rtn)
20141 (defun org-find-base-buffer-visiting (file)
20142 "Like `find-buffer-visiting' but always return the base buffer and
20143 not an indirect buffer."
20144 (let ((buf (or (get-file-buffer file)
20145 (find-buffer-visiting file))))
20147 (or (buffer-base-buffer buf) buf)
20150 (defun org-image-file-name-regexp (&optional extensions)
20151 "Return regexp matching the file names of images.
20152 If EXTENSIONS is given, only match these."
20153 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20154 (image-file-name-regexp)
20155 (let ((image-file-name-extensions
20157 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20158 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20160 (regexp-opt (nconc (mapcar 'upcase
20161 image-file-name-extensions)
20162 image-file-name-extensions)
20166 (defun org-file-image-p (file &optional extensions)
20167 "Return non-nil if FILE is an image."
20169 (string-match (org-image-file-name-regexp extensions) file)))
20171 (defun org-get-cursor-date ()
20172 "Return the date at cursor in as a time.
20173 This works in the calendar and in the agenda, anywhere else it just
20174 returns the current time."
20175 (let (date day defd)
20177 ((eq major-mode 'calendar-mode)
20178 (setq date (calendar-cursor-to-date)
20179 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20180 ((eq major-mode 'org-agenda-mode)
20181 (setq day (get-text-property (point) 'day))
20183 (setq date (calendar-gregorian-from-absolute day)
20184 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20186 (or defd (current-time))))
20188 (defvar org-agenda-action-marker (make-marker)
20189 "Marker pointing to the entry for the next agenda action.")
20191 (defun org-mark-entry-for-agenda-action ()
20192 "Mark the current entry as target of an agenda action.
20193 Agenda actions are actions executed from the agenda with the key `k',
20194 which make use of the date at the cursor."
20196 (move-marker org-agenda-action-marker
20197 (save-excursion (org-back-to-heading t) (point))
20200 "Entry marked for action; press `k' at desired date in agenda or calendar"))
20202 (defun org-mark-subtree ()
20203 "Mark the current subtree.
20204 This puts point at the start of the current subtree, and mark at the end.
20206 If point is in an inline task, mark that task instead."
20208 (let ((inline-task-p
20209 (and (featurep 'org-inlinetask)
20210 (org-inlinetask-in-task-p)))
20212 ;; Get beginning of subtree
20214 (inline-task-p (org-inlinetask-goto-beginning))
20215 ((org-at-heading-p) (beginning-of-line))
20216 (t (org-with-limited-levels (outline-previous-visible-heading 1))))
20220 (org-inlinetask-goto-end)
20221 (org-end-of-subtree))
20223 (push-mark (point) nil t)
20226 ;;; Paragraph filling stuff.
20227 ;; We want this to be just right, so use the full arsenal.
20229 (defun org-indent-line-function ()
20230 "Indent line depending on context."
20232 (let* ((pos (point))
20233 (itemp (org-at-item-p))
20234 (case-fold-search t)
20235 (org-drawer-regexp (or org-drawer-regexp "\000"))
20236 (inline-task-p (and (featurep 'org-inlinetask)
20237 (org-inlinetask-in-task-p)))
20238 (inline-re (and inline-task-p
20239 (org-inlinetask-outline-regexp)))
20241 (beginning-of-line 1)
20244 ((looking-at "# ") (setq column 0))
20246 ((looking-at org-outline-regexp) (setq column 0))
20248 ((looking-at "#\\+include:") (setq column 0))
20249 ;; Footnote definition
20250 ((looking-at org-footnote-definition-re) (setq column 0))
20251 ;; Literal examples
20252 ((looking-at "[ \t]*:\\( \\|$\\)")
20253 (setq column (org-get-indentation))) ; do nothing
20255 ((ignore-errors (goto-char (org-in-item-p)))
20256 (setq column (if itemp
20257 (org-get-indentation)
20258 (org-list-item-body-column (point))))
20261 ((and (looking-at "[ \t]*:END:")
20262 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20264 (goto-char (1- (match-beginning 1)))
20265 (setq column (current-column))))
20267 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20269 (re-search-backward
20270 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20271 (setq column (org-get-indentation (match-string 0))))
20272 ((and (not (looking-at "[ \t]*#\\+begin_"))
20273 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20275 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20277 (cond ((equal (downcase (match-string 1)) "src")
20278 ;; src blocks: let `org-edit-src-exit' handle them
20279 (org-get-indentation))
20280 ((equal (downcase (match-string 1)) "example")
20281 (max (org-get-indentation)
20282 (org-get-indentation (match-string 0))))
20284 (org-get-indentation (match-string 0))))))
20285 ;; This line has nothing special, look at the previous relevant
20286 ;; line to compute indentation
20288 (beginning-of-line 0)
20289 (while (and (not (bobp))
20290 (not (looking-at org-drawer-regexp))
20291 ;; When point started in an inline task, do not move
20292 ;; above task starting line.
20293 (not (and inline-task-p (looking-at inline-re)))
20294 ;; Skip drawers, blocks, empty lines, verbatim,
20295 ;; comments, tables, footnotes definitions, lists,
20297 (or (and (looking-at "[ \t]*:END:")
20298 (re-search-backward org-drawer-regexp nil t))
20299 (and (looking-at "[ \t]*#\\+end_")
20300 (re-search-backward "[ \t]*#\\+begin_"nil t))
20301 (looking-at "[ \t]*[\n:#|]")
20302 (looking-at org-footnote-definition-re)
20303 (and (ignore-errors (goto-char (org-in-item-p)))
20305 (org-list-get-top-point (org-list-struct))))
20306 (and (not inline-task-p)
20307 (featurep 'org-inlinetask)
20308 (org-inlinetask-in-task-p)
20309 (or (org-inlinetask-goto-beginning) t))))
20310 (beginning-of-line 0))
20312 ;; There was an heading above.
20313 ((looking-at "\\*+[ \t]+")
20314 (if (not org-adapt-indentation)
20316 (goto-char (match-end 0))
20317 (setq column (current-column))))
20318 ;; A drawer had started and is unfinished
20319 ((looking-at org-drawer-regexp)
20320 (goto-char (1- (match-beginning 1)))
20321 (setq column (current-column)))
20322 ;; Else, nothing noticeable found: get indentation and go on.
20323 (t (setq column (org-get-indentation))))))
20324 ;; Now apply indentation and move cursor accordingly
20326 (if (<= (current-column) (current-indentation))
20327 (org-indent-line-to column)
20328 (save-excursion (org-indent-line-to column)))
20329 ;; Special polishing for properties, see `org-property-format'
20330 (setq column (current-column))
20331 (beginning-of-line 1)
20333 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20334 (replace-match (concat (match-string 1)
20335 (format org-property-format
20336 (match-string 2) (match-string 3)))
20338 (org-move-to-column column)))
20340 (defun org-indent-drawer ()
20341 "Indent the drawer at point."
20344 (e (and (save-excursion (re-search-forward ":END:" nil t))
20349 (when (overlays-at (point))
20350 (member 'invisible (overlay-properties
20351 (car (overlays-at (point)))))))))
20352 (when folded (org-cycle))
20353 (indent-for-tab-command)
20354 (while (and (move-beginning-of-line 2) (< (point) e))
20355 (indent-for-tab-command))
20357 (when folded (org-cycle)))
20358 (message "Drawer at point indented"))
20360 (defun org-indent-block ()
20361 "Indent the block at point."
20364 (case-fold-search t)
20365 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20370 (when (overlays-at (point))
20371 (member 'invisible (overlay-properties
20372 (car (overlays-at (point)))))))))
20373 (when folded (org-cycle))
20374 (indent-for-tab-command)
20375 (while (and (move-beginning-of-line 2) (< (point) e))
20376 (indent-for-tab-command))
20378 (when folded (org-cycle)))
20379 (message "Block at point indented"))
20381 (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
20382 "Variable to store copy of `adaptive-fill-regexp'.
20383 Since `adaptive-fill-regexp' is set to never match, we need to
20384 store a backup of its value before entering `org-mode' so that
20385 the functionality can be provided as a fall-back.")
20387 (defun org-set-autofill-regexps ()
20389 ;; In the paragraph separator we include headlines, because filling
20390 ;; text in a line directly attached to a headline would otherwise
20391 ;; fill the headline as well.
20392 (org-set-local 'comment-start-skip "^#+[ \t]*")
20393 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
20394 ;; The paragraph starter includes hand-formatted lists.
20400 org-outline-regexp "\\|"
20402 (org-item-re) "\\|"
20405 "\\\\\\(begin\\|end\\|[][]\\)"))
20406 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
20407 ;; But only if the user has not turned off tables or fixed-width regions
20409 'auto-fill-inhibit-regexp
20410 (concat org-outline-regexp
20412 "\\|[ \t]*" org-keyword-time-regexp
20413 (if (or org-enable-table-editor org-enable-fixed-width-editor)
20416 (if org-enable-table-editor "|" "")
20417 (if org-enable-fixed-width-editor ":" "")
20419 ;; We use our own fill-paragraph function, to make sure that tables
20420 ;; and fixed-width regions are not wrapped. That function will pass
20421 ;; through to `fill-paragraph' when appropriate.
20422 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20423 ;; Prevent auto-fill from inserting unwanted new items.
20424 (if (boundp 'fill-nobreak-predicate)
20426 'fill-nobreak-predicate
20428 (append fill-nobreak-predicate
20429 '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
20430 ;; Adaptive filling: To get full control, first make sure that
20431 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
20432 (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
20433 (org-set-local 'org-adaptive-fill-regexp-backup
20434 adaptive-fill-regexp))
20435 (org-set-local 'adaptive-fill-regexp "\000")
20436 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20437 (org-set-local 'adaptive-fill-function
20438 'org-adaptive-fill-function)
20440 'align-mode-rules-list
20441 '((org-in-buffer-settings
20442 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
20443 (modes . '(org-mode))))))
20445 (defun org-fill-item-nobreak-p ()
20446 "Non-nil when a line break at point would insert a new item."
20447 (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
20449 (defun org-fill-line-break-nobreak-p ()
20450 "Non-nil when a line break at point would create an Org line break."
20452 (skip-chars-backward "[ \t]")
20453 (skip-chars-backward "\\\\")
20454 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
20456 (defun org-fill-paragraph (&optional justify)
20457 "Re-align a table, pass through to fill-paragraph if no table."
20458 (let ((table-p (org-at-table-p))
20459 (table.el-p (org-at-table.el-p))
20460 (itemp (org-in-item-p)))
20461 (cond ((and (equal (char-after (point-at-bol)) ?*)
20462 (save-excursion (goto-char (point-at-bol))
20463 (looking-at org-outline-regexp)))
20464 t) ; skip headlines
20465 (table.el-p t) ; skip table.el tables
20466 (table-p (org-table-align) t) ; align Org tables
20467 (itemp ; align text in items
20468 (let* ((struct (save-excursion (goto-char itemp)
20469 (org-list-struct)))
20470 (parents (org-list-parents-alist struct))
20471 (children (org-list-get-children itemp struct parents))
20472 beg end prev next prefix)
20473 ;; Determine in which part of item point is: before
20474 ;; first child, after last child, between two
20475 ;; sub-lists, or simply in item if there's no child.
20478 (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
20480 end (org-list-get-item-end itemp struct)))
20481 ((< (point) (setq next (car children)))
20482 (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
20485 ((> (point) (setq prev (car (last children))))
20486 (setq beg (org-list-get-item-end prev struct)
20487 end (org-list-get-item-end itemp struct)
20488 prefix (save-excursion
20490 (skip-chars-forward " \t")
20491 (make-string (current-column) ?\ ))))
20493 (while (setq next (pop children))
20494 (if (> (point) next)
20496 (setq beg (org-list-get-item-end prev struct)
20498 prefix (save-excursion
20500 (skip-chars-forward " \t")
20501 (make-string (current-column) ?\ )))
20502 (throw 'exit nil))))))
20503 ;; Use `fill-paragraph' with buffer narrowed to item
20504 ;; without any child, and with our computed PREFIX.
20505 (flet ((fill-context-prefix (from to &optional flr) prefix))
20507 (narrow-to-region beg end)
20508 (save-excursion (fill-paragraph justify)))) t))
20509 ;; Special case where point is not in a list but is on
20510 ;; a paragraph adjacent to a list: make sure this paragraph
20511 ;; doesn't get merged with the end of the list by narrowing
20513 ((save-excursion (forward-paragraph -1)
20514 (setq itemp (org-in-item-p)))
20515 (let ((struct (save-excursion (goto-char itemp)
20516 (org-list-struct))))
20518 (narrow-to-region (org-list-get-bottom-point struct)
20519 (save-excursion (forward-paragraph 1)
20521 (fill-paragraph justify) t)))
20522 ;; Don't fill schedule/deadline line before a paragraph
20523 ((save-excursion (forward-paragraph -1)
20524 (or (looking-at (concat "^[^\n]*" org-scheduled-regexp ".*$"))
20525 (looking-at (concat "^[^\n]*" org-deadline-regexp ".*$"))))
20527 (narrow-to-region (1+ (match-end 0))
20528 (save-excursion (forward-paragraph 1) (point)))
20529 (fill-paragraph justify) t))
20530 ;; Else simply call `fill-paragraph'.
20533 ;; For reference, this is the default value of adaptive-fill-regexp
20534 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
20536 (defun org-adaptive-fill-function ()
20537 "Return a fill prefix for org-mode files."
20542 ((looking-at "#[ \t]+")
20543 (match-string-no-properties 0))
20546 (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
20547 ;; Point is in a list after `backward-paragraph': original
20548 ;; point wasn't in the list, or filling would have been taken
20549 ;; care of by `org-auto-fill-function', but the list and the
20550 ;; real paragraph are not separated by a blank line. Thus, move
20551 ;; point after the list to go back to real paragraph and
20552 ;; determine fill-prefix.
20553 ((setq itemp (org-in-item-p))
20555 (let* ((struct (org-list-struct))
20556 (bottom (org-list-get-bottom-point struct)))
20558 (make-string (org-get-indentation) ?\ )))
20560 ((looking-at org-adaptive-fill-regexp-backup)
20561 (match-string-no-properties 0))))))
20563 (defun org-auto-fill-function ()
20564 "Auto-fill function."
20565 (let (itemp prefix)
20566 ;; When in a list, compute an appropriate fill-prefix and make
20567 ;; sure it will be used by `do-auto-fill'.
20568 (if (setq itemp (org-in-item-p))
20570 (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
20571 (flet ((fill-context-prefix (from to &optional flr) prefix))
20573 ;; Else just use `do-auto-fill'.
20578 (defun org-toggle-fixed-width-section (arg)
20579 "Toggle the fixed-width export.
20580 If there is no active region, the QUOTE keyword at the current headline is
20581 inserted or removed. When present, it causes the text between this headline
20582 and the next to be exported as fixed-width text, and unmodified.
20583 If there is an active region, this command adds or removes a colon as the
20584 first character of this line. If the first character of a line is a colon,
20585 this line is also exported in fixed-width font."
20588 (regionp (org-region-active-p))
20589 (beg (if regionp (region-beginning) (point)))
20590 (end (if regionp (region-end)))
20591 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
20592 (case-fold-search nil)
20593 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
20598 (setq cc (current-column))
20599 (beginning-of-line 1)
20600 (setq off (looking-at re))
20601 (while (> nlines 0)
20602 (setq nlines (1- nlines))
20603 (beginning-of-line 1)
20606 (org-move-to-column cc t)
20609 ((and off (looking-at re))
20610 (replace-match "" t t nil 1))
20611 ((not off) (org-move-to-column cc t) (insert ": ")))
20614 (org-back-to-heading)
20616 ((looking-at (format org-heading-keyword-regexp-format
20618 (goto-char (match-end 1))
20619 (looking-at (concat " +" org-quote-string))
20620 (replace-match "" t t)
20621 (when (eolp) (insert " ")))
20622 ((looking-at org-outline-regexp)
20623 (goto-char (match-end 0))
20624 (insert org-quote-string " ")))))))
20626 (defun org-reftex-citation ()
20627 "Use reftex-citation to insert a citation into the buffer.
20628 This looks for a line like
20630 #+BIBLIOGRAPHY: foo plain option:-d
20632 and derives from it that foo.bib is the bibliography file relevant
20633 for this document. It then installs the necessary environment for RefTeX
20634 to work in this buffer and calls `reftex-citation' to insert a citation
20637 Export of such citations to both LaTeX and HTML is handled by the contributed
20638 package org-exp-bibtex by Taru Karttunen."
20640 (let ((reftex-docstruct-symbol 'rds)
20641 (reftex-cite-format "\\cite{%l}")
20646 (let ((case-fold-search t)
20647 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
20648 (if (not (save-excursion
20649 (or (re-search-forward re nil t)
20650 (re-search-backward re nil t))))
20651 (error "No bibliography defined in file")
20652 (setq bib (concat (match-string 1) ".bib")
20653 rds (list (list 'bib bib)))))))
20654 (call-interactively 'reftex-citation)))
20656 ;;;; Functions extending outline functionality
20658 (defun org-beginning-of-line (&optional arg)
20659 "Go to the beginning of the current line. If that is invisible, continue
20660 to a visible line beginning. This makes the function of C-a more intuitive.
20661 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
20662 first attempt, and only move to after the tags when the cursor is already
20663 beyond the end of the headline."
20665 (let ((pos (point))
20666 (special (if (consp org-special-ctrl-a/e)
20667 (car org-special-ctrl-a/e)
20668 org-special-ctrl-a/e))
20670 (if (org-bound-and-true-p line-move-visual)
20671 (beginning-of-visual-line 1)
20672 (beginning-of-line 1))
20673 (if (and arg (fboundp 'move-beginning-of-line))
20674 (call-interactively 'move-beginning-of-line)
20678 (if (org-truely-invisible-p)
20679 (while (and (not (bobp)) (org-truely-invisible-p))
20681 (beginning-of-line 1))
20682 (forward-char 1))))
20685 ((and (looking-at org-complex-heading-regexp)
20686 (= (char-after (match-end 1)) ?\ ))
20687 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
20691 (cond ((> pos refpos) refpos)
20692 ((= pos (point)) refpos)
20694 (cond ((> pos (point)) (point))
20695 ((not (eq last-command this-command)) (point))
20698 ;; Being at an item and not looking at an the item means point
20699 ;; was previously moved to beginning of a visual line, which
20700 ;; doesn't contain the item. Therefore, do nothing special,
20702 (when (looking-at org-list-full-item-re)
20703 ;; Set special position at first white space character after
20704 ;; bullet, and check-box, if any.
20705 (let ((after-bullet
20706 (let ((box (match-end 3)))
20707 (if (not box) (match-end 1)
20708 (let ((after (char-after box)))
20709 (if (and after (= after ? )) (1+ box) box))))))
20710 ;; Special case: Move point to special position when
20711 ;; currently after it or at beginning of line.
20713 (when (or (> pos after-bullet) (= (point) pos))
20714 (goto-char after-bullet))
20715 ;; Reversed case: Move point to special position when
20716 ;; point was already at beginning of line and command is
20718 (when (and (= (point) pos) (eq last-command this-command))
20719 (goto-char after-bullet))))))))
20721 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
20723 (defun org-end-of-line (&optional arg)
20724 "Go to the end of the line.
20725 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
20726 first attempt, and only move to after the tags when the cursor is already
20727 beyond the end of the headline."
20729 (let ((special (if (consp org-special-ctrl-a/e)
20730 (cdr org-special-ctrl-a/e)
20731 org-special-ctrl-a/e)))
20733 ((or (not special) arg
20734 (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
20735 (call-interactively
20736 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
20737 ((fboundp 'move-end-of-line) 'move-end-of-line)
20738 (t 'end-of-line))))
20739 ((org-at-heading-p)
20740 (let ((pos (point)))
20741 (beginning-of-line 1)
20742 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
20744 (if (or (< pos (match-beginning 1))
20745 (= pos (match-end 0)))
20746 (goto-char (match-beginning 1))
20747 (goto-char (match-end 0)))
20748 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
20749 (goto-char (match-end 0))
20750 (goto-char (match-beginning 1))))
20751 (call-interactively (if (fboundp 'move-end-of-line)
20755 (move-end-of-line 1)
20756 (when (overlays-at (1- (point))) (backward-char 1)))
20757 ;; At an item: Move before any hidden text.
20758 (t (call-interactively
20759 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
20760 ((fboundp 'move-end-of-line) 'move-end-of-line)
20761 (t 'end-of-line)))))
20763 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
20765 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
20766 (define-key org-mode-map "\C-e" 'org-end-of-line)
20768 (defun org-backward-sentence (&optional arg)
20769 "Go to beginning of sentence, or beginning of table field.
20770 This will call `backward-sentence' or `org-table-beginning-of-field',
20771 depending on context."
20774 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
20775 (t (call-interactively 'backward-sentence))))
20777 (defun org-forward-sentence (&optional arg)
20778 "Go to end of sentence, or end of table field.
20779 This will call `forward-sentence' or `org-table-end-of-field',
20780 depending on context."
20783 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
20784 (t (call-interactively 'forward-sentence))))
20786 (define-key org-mode-map "\M-a" 'org-backward-sentence)
20787 (define-key org-mode-map "\M-e" 'org-forward-sentence)
20789 (defun org-kill-line (&optional arg)
20790 "Kill line, to tags or end of line."
20793 ((or (not org-special-ctrl-k)
20795 (not (org-at-heading-p)))
20796 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
20797 org-ctrl-k-protect-subtree)
20798 (if (or (eq org-ctrl-k-protect-subtree 'error)
20799 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
20800 (error "C-k aborted - would kill hidden subtree")))
20801 (call-interactively
20802 (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
20803 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
20804 (kill-region (point) (match-beginning 1))
20805 (org-set-tags nil t))
20806 (t (kill-region (point) (point-at-eol)))))
20808 (define-key org-mode-map "\C-k" 'org-kill-line)
20810 (defun org-yank (&optional arg)
20811 "Yank. If the kill is a subtree, treat it specially.
20812 This command will look at the current kill and check if is a single
20813 subtree, or a series of subtrees[1]. If it passes the test, and if the
20814 cursor is at the beginning of a line or after the stars of a currently
20815 empty headline, then the yank is handled specially. How exactly depends
20816 on the value of the following variables, both set by default.
20818 org-yank-folded-subtrees
20819 When set, the subtree(s) will be folded after insertion, but only
20820 if doing so would now swallow text after the yanked text.
20822 org-yank-adjusted-subtrees
20823 When set, the subtree will be promoted or demoted in order to
20824 fit into the local outline tree structure, which means that the level
20825 will be adjusted so that it becomes the smaller one of the two
20826 *visible* surrounding headings.
20828 Any prefix to this command will cause `yank' to be called directly with
20829 no special treatment. In particular, a simple \\[universal-argument] prefix \
20831 plainly yank the text as it is.
20833 \[1] The test checks if the first non-white line is a heading
20834 and if there are no other headings with fewer stars."
20836 (org-yank-generic 'yank arg))
20838 (defun org-yank-generic (command arg)
20839 "Perform some yank-like command.
20841 This function implements the behavior described in the `org-yank'
20842 documentation. However, it has been generalized to work for any
20843 interactive command with similar behavior."
20845 ;; pretend to be command COMMAND
20846 (setq this-command command)
20849 (call-interactively command)
20851 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
20852 (and (org-kill-is-subtree-p)
20854 (and (looking-at "[ \t]*$")
20857 (buffer-substring (point-at-bol) (point)))))))
20860 ((and subtreep org-yank-folded-subtrees)
20861 (let ((beg (point))
20863 (if (and subtreep org-yank-adjusted-subtrees)
20864 (org-paste-subtree nil nil 'for-yank)
20865 (call-interactively command))
20869 (when (and (bolp) subtreep
20870 (not (setq swallowp
20871 (org-yank-folding-would-swallow-text beg end))))
20872 (org-with-limited-levels
20873 (or (looking-at org-outline-regexp)
20874 (re-search-forward org-outline-regexp-bol end t))
20875 (while (and (< (point) end) (looking-at org-outline-regexp))
20877 (org-cycle-show-empty-lines 'folded)
20878 (condition-case nil
20879 (outline-forward-same-level 1)
20880 (error (goto-char end))))))
20883 "Inserted text not folded because that would swallow text"))
20886 (skip-chars-forward " \t\n\r")
20887 (beginning-of-line 1)
20888 (push-mark beg 'nomsg)))
20889 ((and subtreep org-yank-adjusted-subtrees)
20890 (let ((beg (point-at-bol)))
20891 (org-paste-subtree nil nil 'for-yank)
20892 (push-mark beg 'nomsg)))
20894 (call-interactively command))))))
20896 (defun org-yank-folding-would-swallow-text (beg end)
20897 "Would hide-subtree at BEG swallow any text after END?"
20899 (org-with-limited-levels
20902 (when (or (looking-at org-outline-regexp)
20903 (re-search-forward org-outline-regexp-bol end t))
20904 (setq level (org-outline-level)))
20906 (skip-chars-forward " \t\r\n\v\f")
20908 (and (bolp) (looking-at org-outline-regexp)
20909 (<= (org-outline-level) level)))
20910 nil ; Nothing would be swallowed
20911 t))))) ; something would swallow
20913 (define-key org-mode-map "\C-y" 'org-yank)
20915 (defun org-truely-invisible-p ()
20916 "Check if point is at a character currently not visible.
20917 This version does not only check the character property, but also
20919 ;; Early versions of noutline don't have `outline-invisible-p'.
20920 (if (org-bound-and-true-p visible-mode)
20922 (outline-invisible-p)))
20924 (defun org-invisible-p2 ()
20925 "Check if point is at a character currently not visible."
20927 (if (and (eolp) (not (bobp))) (backward-char 1))
20928 ;; Early versions of noutline don't have `outline-invisible-p'.
20929 (outline-invisible-p)))
20931 (defun org-back-to-heading (&optional invisible-ok)
20932 "Call `outline-back-to-heading', but provide a better error message."
20933 (condition-case nil
20934 (outline-back-to-heading invisible-ok)
20935 (error (error "Before first headline at position %d in buffer %s"
20936 (point) (current-buffer)))))
20938 (defun org-beginning-of-defun ()
20939 "Go to the beginning of the subtree, i.e. back to the heading."
20940 (org-back-to-heading))
20941 (defun org-end-of-defun ()
20942 "Go to the end of the subtree."
20943 (org-end-of-subtree nil t))
20945 (defun org-before-first-heading-p ()
20946 "Before first heading?"
20949 (null (re-search-backward org-outline-regexp-bol nil t))))
20951 (defun org-at-heading-p (&optional ignored)
20952 (outline-on-heading-p t))
20953 ;; Compatibility alias with Org versions < 7.8.03
20954 (defalias 'org-on-heading-p 'org-at-heading-p)
20956 (defun org-at-drawer-p nil
20957 "Is cursor at a drawer keyword?"
20959 (move-beginning-of-line 1)
20960 (looking-at org-drawer-regexp)))
20962 (defun org-at-block-p nil
20963 "Is cursor at a block keyword?"
20965 (move-beginning-of-line 1)
20966 (looking-at org-block-regexp)))
20968 (defun org-point-at-end-of-empty-headline ()
20969 "If point is at the end of an empty headline, return t, else nil.
20970 If the heading only contains a TODO keyword, it is still still considered
20972 (and (looking-at "[ \t]*$")
20973 (when org-todo-line-regexp
20975 (beginning-of-line 1)
20976 (let ((case-fold-search nil))
20977 (looking-at org-todo-line-regexp)
20978 (string= (match-string 3) ""))))))
20980 (defun org-at-heading-or-item-p ()
20981 (or (org-at-heading-p) (org-at-item-p)))
20983 (defun org-at-target-p ()
20984 (or (org-in-regexp org-radio-target-regexp)
20985 (org-in-regexp org-target-regexp)))
20986 ;; Compatibility alias with Org versions < 7.8.03
20987 (defalias 'org-on-target-p 'org-at-target-p)
20989 (defun org-up-heading-all (arg)
20990 "Move to the heading line of which the present line is a subheading.
20991 This function considers both visible and invisible heading lines.
20992 With argument, move up ARG levels."
20993 (if (fboundp 'outline-up-heading-all)
20994 (outline-up-heading-all arg) ; emacs 21 version of outline.el
20995 (outline-up-heading arg t))) ; emacs 22 version of outline.el
20997 (defun org-up-heading-safe ()
20998 "Move to the heading line of which the present line is a subheading.
20999 This version will not throw an error. It will return the level of the
21000 headline found, or nil if no higher level is found.
21002 Also, this function will be a lot faster than `outline-up-heading',
21003 because it relies on stars being the outline starters. This can really
21004 make a significant difference in outlines with very many siblings."
21005 (let (start-level re)
21006 (org-back-to-heading t)
21007 (setq start-level (funcall outline-level))
21008 (if (equal start-level 1)
21010 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
21011 (if (re-search-backward re nil t)
21012 (funcall outline-level)))))
21014 (defun org-first-sibling-p ()
21015 "Is this heading the first child of its parents?"
21017 (let ((re org-outline-regexp-bol)
21019 (unless (org-at-heading-p t)
21020 (error "Not at a heading"))
21021 (setq level (funcall outline-level))
21023 (if (not (re-search-backward re nil t))
21025 (setq l (funcall outline-level))
21028 (defun org-goto-sibling (&optional previous)
21029 "Goto the next sibling, even if it is invisible.
21030 When PREVIOUS is set, go to the previous sibling instead. Returns t
21031 when a sibling was found. When none is found, return nil and don't
21033 (let ((fun (if previous 're-search-backward 're-search-forward))
21035 (re org-outline-regexp-bol)
21037 (when (condition-case nil (org-back-to-heading t) (error nil))
21038 (setq level (funcall outline-level))
21040 (or previous (forward-char 1))
21041 (while (funcall fun re nil t)
21042 (setq l (funcall outline-level))
21043 (when (< l level) (goto-char pos) (throw 'exit nil))
21044 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21048 (defun org-show-siblings ()
21049 "Show all siblings of the current headline."
21051 (while (org-goto-sibling) (org-flag-heading nil)))
21053 (while (org-goto-sibling 'previous)
21054 (org-flag-heading nil))))
21056 (defun org-goto-first-child ()
21057 "Goto the first child, even if it is invisible.
21058 Return t when a child was found. Otherwise don't move point and
21060 (let (level (pos (point)) (re org-outline-regexp-bol))
21061 (when (condition-case nil (org-back-to-heading t) (error nil))
21062 (setq level (outline-level))
21064 (if (and (re-search-forward re nil t) (> (outline-level) level))
21065 (progn (goto-char (match-beginning 0)) t)
21066 (goto-char pos) nil))))
21068 (defun org-show-hidden-entry ()
21069 "Show an entry where even the heading is hidden."
21073 (defun org-flag-heading (flag &optional entry)
21074 "Flag the current heading. FLAG non-nil means make invisible.
21075 When ENTRY is non-nil, show the entire entry."
21077 (org-back-to-heading t)
21078 ;; Check if we should show the entire entry
21083 (and (outline-next-heading)
21084 (org-flag-heading nil))))
21085 (outline-flag-region (max (point-min) (1- (point)))
21086 (save-excursion (outline-end-of-heading) (point))
21089 (defun org-get-next-sibling ()
21090 "Move to next heading of the same level, and return point.
21091 If there is no such heading, return nil.
21092 This is like outline-next-sibling, but invisible headings are ok."
21093 (let ((level (funcall outline-level)))
21094 (outline-next-heading)
21095 (while (and (not (eobp)) (> (funcall outline-level) level))
21096 (outline-next-heading))
21097 (if (or (eobp) (< (funcall outline-level) level))
21101 (defun org-get-last-sibling ()
21102 "Move to previous heading of the same level, and return point.
21103 If there is no such heading, return nil."
21104 (let ((opoint (point))
21105 (level (funcall outline-level)))
21106 (outline-previous-heading)
21107 (when (and (/= (point) opoint) (outline-on-heading-p t))
21108 (while (and (> (funcall outline-level) level)
21110 (outline-previous-heading))
21111 (if (< (funcall outline-level) level)
21115 (defun org-end-of-subtree (&optional invisible-OK to-heading)
21116 ;; This contains an exact copy of the original function, but it uses
21117 ;; `org-back-to-heading', to make it work also in invisible
21118 ;; trees. And is uses an invisible-OK argument.
21119 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21120 ;; Furthermore, when used inside Org, finding the end of a large subtree
21121 ;; with many children and grandchildren etc, this can be much faster
21122 ;; than the outline version.
21123 (org-back-to-heading invisible-OK)
21125 (level (funcall outline-level)))
21126 (if (and (eq major-mode 'org-mode) (< level 1000))
21127 ;; A true heading (not a plain list item), in Org-mode
21128 ;; This means we can easily find the end by looking
21129 ;; only for the right number of stars. Using a regexp to do
21130 ;; this is so much faster than using a Lisp loop.
21131 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21133 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21134 ;; something else, do it the slow way
21135 (while (and (not (eobp))
21136 (or first (> (funcall outline-level) level)))
21138 (outline-next-heading)))
21140 (if (memq (preceding-char) '(?\n ?\^M))
21142 ;; Go to end of line before heading
21144 (if (memq (preceding-char) '(?\n ?\^M))
21145 ;; leave blank line before heading
21146 (forward-char -1))))))
21149 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21150 "Use Org version in org-mode, for dramatic speed-up."
21151 (if (eq major-mode 'org-mode)
21153 (org-end-of-subtree nil t)
21154 (unless (eobp) (backward-char 1)))
21157 (defun org-end-of-meta-data-and-drawers ()
21158 "Jump to the first text after meta data and drawers in the current entry.
21159 This will move over empty lines, lines with planning time stamps,
21160 clocking lines, and drawers."
21161 (org-back-to-heading t)
21162 (let ((end (save-excursion (outline-next-heading) (point)))
21163 (re (concat "\\(" org-drawer-regexp "\\)"
21164 "\\|" "[ \t]*" org-keyword-time-regexp)))
21166 (while (re-search-forward re end t)
21167 (if (not (match-end 1))
21168 ;; empty or planning line
21170 ;; a drawer, find the end
21171 (re-search-forward "^[ \t]*:END:" end 'move)
21173 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21176 (defun org-forward-same-level (arg &optional invisible-ok)
21177 "Move forward to the arg'th subheading at same level as this one.
21178 Stop at the first and last subheadings of a superior heading.
21179 Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
21180 it wil also look at invisible ones."
21182 (org-back-to-heading invisible-ok)
21184 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21185 (re (format "^\\*\\{1,%d\\} " level))
21189 (while (and (re-search-forward re nil 'move)
21190 (setq l (- (match-end 0) (match-beginning 0) 1))
21193 (progn (backward-char 1) (outline-invisible-p)))
21194 (if (< l level) (setq arg 1)))
21195 (setq arg (1- arg)))
21196 (beginning-of-line 1)))
21198 (defun org-backward-same-level (arg &optional invisible-ok)
21199 "Move backward to the arg'th subheading at same level as this one.
21200 Stop at the first and last subheadings of a superior heading."
21202 (org-back-to-heading)
21204 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21205 (re (format "^\\*\\{1,%d\\} " level))
21208 (while (and (re-search-backward re nil 'move)
21209 (setq l (- (match-end 0) (match-beginning 0) 1))
21212 (outline-invisible-p))
21213 (if (< l level) (setq arg 1)))
21214 (setq arg (1- arg)))))
21216 (defun org-show-subtree ()
21217 "Show everything after this heading at deeper levels."
21219 (outline-flag-region
21222 (org-end-of-subtree t t))
21225 (defun org-show-entry ()
21226 "Show the body directly following this heading.
21227 Show the heading too, if it is currently invisible."
21230 (condition-case nil
21232 (org-back-to-heading t)
21233 (outline-flag-region
21234 (max (point-min) (1- (point)))
21236 (if (re-search-forward
21237 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
21238 (match-beginning 1)
21241 (org-cycle-hide-drawers 'children))
21244 (defun org-make-options-regexp (kwds &optional extra)
21245 "Make a regular expression for keyword lines."
21249 (mapconcat 'regexp-quote kwds "\\|")
21250 (if extra (concat "\\|" extra))
21254 ;; Make isearch reveal the necessary context
21255 (defun org-isearch-end ()
21256 "Reveal context after isearch exits."
21257 (when isearch-success ; only if search was successful
21258 (if (featurep 'xemacs)
21259 ;; Under XEmacs, the hook is run in the correct place,
21260 ;; we directly show the context.
21261 (org-show-context 'isearch)
21262 ;; In Emacs the hook runs *before* restoring the overlays.
21263 ;; So we have to use a one-time post-command-hook to do this.
21264 ;; (Emacs 22 has a special variable, see function `org-mode')
21265 (unless (and (boundp 'isearch-mode-end-hook-quit)
21266 isearch-mode-end-hook-quit)
21267 ;; Only when the isearch was not quitted.
21268 (org-add-hook 'post-command-hook 'org-isearch-post-command
21269 'append 'local)))))
21271 (defun org-isearch-post-command ()
21272 "Remove self from hook, and show context."
21273 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
21274 (org-show-context 'isearch))
21277 ;;;; Integration with and fixes for other packages
21281 (defvar org-imenu-markers nil
21282 "All markers currently used by Imenu.")
21283 (make-variable-buffer-local 'org-imenu-markers)
21285 (defun org-imenu-new-marker (&optional pos)
21286 "Return a new marker for use by Imenu, and remember the marker."
21287 (let ((m (make-marker)))
21288 (move-marker m (or pos (point)))
21289 (push m org-imenu-markers)
21292 (defun org-imenu-get-tree ()
21293 "Produce the index for Imenu."
21294 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
21295 (setq org-imenu-markers nil)
21296 (let* ((n org-imenu-depth)
21297 (re (concat "^" (org-get-limited-outline-regexp)))
21298 (subs (make-vector (1+ n) nil))
21304 (goto-char (point-max))
21305 (while (re-search-backward re nil t)
21306 (setq level (org-reduced-level (funcall outline-level)))
21307 (when (and (<= level n)
21308 (looking-at org-complex-heading-regexp))
21309 (setq head (org-link-display-format
21310 (org-match-string-no-properties 4))
21311 m (org-imenu-new-marker))
21312 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
21313 (if (>= level last-level)
21314 (push (cons head m) (aref subs level))
21315 (push (cons head (aref subs (1+ level))) (aref subs level))
21316 (loop for i from (1+ level) to n do (aset subs i nil)))
21317 (setq last-level level)))))
21320 (eval-after-load "imenu"
21322 (add-hook 'imenu-after-jump-hook
21324 (if (eq major-mode 'org-mode)
21325 (org-show-context 'org-goto))))))
21327 (defun org-link-display-format (link)
21328 "Replace a link with either the description, or the link target
21329 if no description is present"
21331 (if (string-match org-bracket-link-analytic-regexp link)
21332 (replace-match (if (match-end 5)
21333 (match-string 5 link)
21334 (concat (match-string 1 link)
21335 (match-string 3 link)))
21339 (defun org-toggle-link-display ()
21340 "Toggle the literal or descriptive display of links."
21342 (if org-descriptive-links
21343 (progn (org-remove-from-invisibility-spec '(org-link))
21344 (org-restart-font-lock)
21345 (setq org-descriptive-links nil))
21346 (progn (add-to-invisibility-spec '(org-link))
21347 (org-restart-font-lock)
21348 (setq org-descriptive-links t))))
21350 ;; Speedbar support
21352 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
21353 "Overlay marking the agenda restriction line in speedbar.")
21354 (overlay-put org-speedbar-restriction-lock-overlay
21355 'face 'org-agenda-restriction-lock)
21356 (overlay-put org-speedbar-restriction-lock-overlay
21357 'help-echo "Agendas are currently limited to this item.")
21358 (org-detach-overlay org-speedbar-restriction-lock-overlay)
21360 (defun org-speedbar-set-agenda-restriction ()
21361 "Restrict future agenda commands to the location at point in speedbar.
21362 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
21364 (require 'org-agenda)
21365 (let (p m tp np dir txt)
21367 ((setq p (text-property-any (point-at-bol) (point-at-eol)
21369 (setq m (get-text-property p 'org-imenu-marker))
21370 (with-current-buffer (marker-buffer m)
21372 (org-agenda-set-restriction-lock 'subtree)))
21373 ((setq p (text-property-any (point-at-bol) (point-at-eol)
21374 'speedbar-function 'speedbar-find-file))
21375 (setq tp (previous-single-property-change
21376 (1+ p) 'speedbar-function)
21377 np (next-single-property-change
21378 tp 'speedbar-function)
21379 dir (speedbar-line-directory)
21380 txt (buffer-substring-no-properties (or tp (point-min))
21381 (or np (point-max))))
21382 (with-current-buffer (find-file-noselect
21383 (let ((default-directory dir))
21384 (expand-file-name txt)))
21385 (unless (eq major-mode 'org-mode)
21386 (error "Cannot restrict to non-Org-mode file"))
21387 (org-agenda-set-restriction-lock 'file)))
21388 (t (error "Don't know how to restrict Org-mode's agenda")))
21389 (move-overlay org-speedbar-restriction-lock-overlay
21390 (point-at-bol) (point-at-eol))
21391 (setq current-prefix-arg nil)
21392 (org-agenda-maybe-redo)))
21394 (eval-after-load "speedbar"
21396 (speedbar-add-supported-extension ".org")
21397 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
21398 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
21399 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
21400 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
21401 (add-hook 'speedbar-visiting-tag-hook
21402 (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
21404 ;;; Fixes and Hacks for problems with other packages
21406 ;; Make flyspell not check words in links, to not mess up our keymap
21407 (defun org-mode-flyspell-verify ()
21408 "Don't let flyspell put overlays at active buttons, or on
21409 {todo,all-time,additional-option-like}-keywords."
21410 (let ((pos (max (1- (point)) (point-min)))
21411 (word (thing-at-point 'word)))
21412 (and (not (get-text-property pos 'keymap))
21413 (not (get-text-property pos 'org-no-flyspell))
21414 (not (member word org-todo-keywords-1))
21415 (not (member word org-all-time-keywords))
21416 (not (member word org-additional-option-like-keywords)))))
21418 (defun org-remove-flyspell-overlays-in (beg end)
21419 "Remove flyspell overlays in region."
21420 (and (org-bound-and-true-p flyspell-mode)
21421 (fboundp 'flyspell-delete-region-overlays)
21422 (flyspell-delete-region-overlays beg end))
21423 (add-text-properties beg end '(org-no-flyspell t)))
21425 ;; Make `bookmark-jump' shows the jump location if it was hidden.
21426 (eval-after-load "bookmark"
21427 '(if (boundp 'bookmark-after-jump-hook)
21428 ;; We can use the hook
21429 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
21430 ;; Hook not available, use advice
21431 (defadvice bookmark-jump (after org-make-visible activate)
21432 "Make the position visible."
21433 (org-bookmark-jump-unhide))))
21435 ;; Make sure saveplace shows the location if it was hidden
21436 (eval-after-load "saveplace"
21437 '(defadvice save-place-find-file-hook (after org-make-visible activate)
21438 "Make the position visible."
21439 (org-bookmark-jump-unhide)))
21441 ;; Make sure ecb shows the location if it was hidden
21442 (eval-after-load "ecb"
21443 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
21444 "Make hierarchy visible when jumping into location from ECB tree buffer."
21445 (if (eq major-mode 'org-mode)
21446 (org-show-context))))
21448 (defun org-bookmark-jump-unhide ()
21449 "Unhide the current position, to show the bookmark location."
21450 (and (eq major-mode 'org-mode)
21451 (or (outline-invisible-p)
21452 (save-excursion (goto-char (max (point-min) (1- (point))))
21453 (outline-invisible-p)))
21454 (org-show-context 'bookmark-jump)))
21456 ;; Make session.el ignore our circular variable
21457 (eval-after-load "session"
21458 '(add-to-list 'session-globals-exclude 'org-mark-ring))
21460 ;;;; Experimental code
21462 (defun org-closed-in-range ()
21463 "Sparse tree of items closed in a certain time range.
21464 Still experimental, may disappear in the future."
21466 ;; Get the time interval from the user.
21467 (let* ((time1 (org-float-time
21468 (org-read-date nil 'to-time nil "Starting date: ")))
21469 (time2 (org-float-time
21470 (org-read-date nil 'to-time nil "End date:")))
21471 ;; callback function
21472 (callback (lambda ()
21475 (apply 'encode-time
21476 (org-parse-time-string
21477 (match-string 1))))))
21478 ;; check if time in interval
21479 (and (>= time time1) (<= time time2))))))
21480 ;; make tree, check each match with the callback
21481 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
21487 (run-hooks 'org-load-hook)
21489 ;;; org.el ends here