1 ;;; org.el --- Outline-based notes management and organizer
2 ;; Carstens outline-mode for keeping track of everything.
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
30 ;; project planning with a fast and effective plain-text system.
32 ;; Org-mode develops organizational tasks around NOTES files that contain
33 ;; information about projects as plain text. Org-mode is implemented on
34 ;; top of outline-mode, which makes it possible to keep the content of
35 ;; large files well structured. Visibility cycling and structure editing
36 ;; help to work with the tree. Tables are easily created with a built-in
37 ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
38 ;; and scheduling. It dynamically compiles entries into an agenda that
39 ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
40 ;; Plain text URL-like links connect to websites, emails, Usenet
41 ;; messages, BBDB entries, and any files related to the projects. For
42 ;; printing and sharing of notes, an Org-mode file can be exported as a
43 ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
44 ;; iCalendar file. It can also serve as a publishing tool for a set of
47 ;; Installation and Activation
48 ;; ---------------------------
49 ;; See the corresponding sections in the manual at
51 ;; http://orgmode.org/org.html#Installation
55 ;; The documentation of Org-mode can be found in the TeXInfo file. The
56 ;; distribution also contains a PDF version of it. At the homepage of
57 ;; Org-mode, you can read the same text online as HTML. There is also an
58 ;; excellent reference card made by Philip Rooke. This card can be found
59 ;; in the etc/ directory of Emacs 22.
61 ;; A list of recent changes can be found at
62 ;; http://orgmode.org/Changes.html
66 (defvar org-inhibit-highlight-removal nil
) ; dynamically scoped param
67 (defvar org-table-formula-constants-local nil
68 "Local version of `org-table-formula-constants'.")
69 (make-variable-buffer-local 'org-table-formula-constants-local
)
71 ;;;; Require other packages
79 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
80 (when (fboundp 'defvaralias
)
81 (unless (boundp 'calendar-view-holidays-initially-flag
)
82 (defvaralias 'calendar-view-holidays-initially-flag
83 'view-calendar-holidays-initially
))
84 (unless (boundp 'calendar-view-diary-initially-flag
)
85 (defvaralias 'calendar-view-diary-initially-flag
86 'view-diary-entries-initially
))
87 (unless (boundp 'diary-fancy-buffer
)
88 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
90 (require 'outline
) (require 'noutline
)
91 ;; Other stuff we need.
93 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
98 (require 'org-entities
)
102 (require 'org-pcomplete
)
104 (require 'org-footnote
)
106 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
107 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
108 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
109 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
110 (declare-function org-at-clock-log-p
"org-clock" ())
111 (declare-function org-clock-timestamps-up
"org-clock" ())
112 (declare-function org-clock-timestamps-down
"org-clock" ())
123 ;; load languages based on value of `org-babel-load-languages'
124 (defvar org-babel-load-languages
)
126 (defun org-babel-do-load-languages (sym value
)
127 "Load the languages defined in `org-babel-load-languages'."
128 (set-default sym value
)
130 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
133 (require (intern (concat "ob-" lang
))))
135 (funcall 'fmakunbound
136 (intern (concat "org-babel-execute:" lang
)))
137 (funcall 'fmakunbound
138 (intern (concat "org-babel-expand-body:" lang
)))))))
139 org-babel-load-languages
))
141 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
142 "Languages which can be evaluated in Org-mode buffers.
143 This list can be used to load support for any of the languages
144 below, note that each language will depend on a different set of
145 system executables and/or Emacs modes. When a language is
146 \"loaded\", then code blocks in that language can be evaluated
147 with `org-babel-execute-src-block' bound by default to C-c
148 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
149 be set to remove code block evaluation from the C-c C-c
150 keybinding. By default only Emacs Lisp (which has no
151 requirements) is loaded."
153 :set
'org-babel-do-load-languages
154 :type
'(alist :tag
"Babel Languages"
157 (const :tag
"Awk" awk
)
160 (const :tag
"Asymptote" asymptote
)
161 (const :tag
"Calc" calc
)
162 (const :tag
"Clojure" clojure
)
163 (const :tag
"CSS" css
)
164 (const :tag
"Ditaa" ditaa
)
165 (const :tag
"Dot" dot
)
166 (const :tag
"Emacs Lisp" emacs-lisp
)
167 (const :tag
"Fortran" fortran
)
168 (const :tag
"Gnuplot" gnuplot
)
169 (const :tag
"Haskell" haskell
)
170 (const :tag
"Java" java
)
171 (const :tag
"Javascript" js
)
172 (const :tag
"Latex" latex
)
173 (const :tag
"Ledger" ledger
)
174 (const :tag
"Lilypond" lilypond
)
175 (const :tag
"Maxima" maxima
)
176 (const :tag
"Matlab" matlab
)
177 (const :tag
"Mscgen" mscgen
)
178 (const :tag
"Ocaml" ocaml
)
179 (const :tag
"Octave" octave
)
180 (const :tag
"Org" org
)
181 (const :tag
"Perl" perl
)
182 (const :tag
"Pico Lisp" picolisp
)
183 (const :tag
"PlantUML" plantuml
)
184 (const :tag
"Python" python
)
185 (const :tag
"Ruby" ruby
)
186 (const :tag
"Sass" sass
)
187 (const :tag
"Scheme" scheme
)
188 (const :tag
"Screen" screen
)
189 (const :tag
"Shell Script" sh
)
190 (const :tag
"Shen" shen
)
191 (const :tag
"Sql" sql
)
192 (const :tag
"Sqlite" sqlite
))
193 :value-type
(boolean :tag
"Activate" :value t
)))
195 ;;;; Customization variables
196 (defcustom org-clone-delete-id nil
197 "Remove ID property of clones of a subtree.
198 When non-nil, clones of a subtree don't inherit the ID property.
199 Otherwise they inherit the ID property with a new unique
206 (defconst org-version
"7.8.02"
207 "The version number of the file org.el.")
209 (defun org-version (&optional here
)
210 "Show the org-mode version in the echo area.
211 With prefix arg HERE, insert it at point."
213 (let* ((origin default-directory
)
214 (version org-version
)
216 (dir (concat (file-name-directory (locate-library "org")) "../" )))
217 (when (and (file-exists-p (expand-file-name ".git" dir
))
218 (executable-find "git"))
222 (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
223 (with-current-buffer "*Shell Command Output*"
224 (goto-char (point-min))
225 (setq git-version
(buffer-substring (point) (point-at-eol))))
226 (subst-char-in-string ?- ?. git-version t
)
227 (when (string-match "\\S-"
228 (shell-command-to-string
229 "git diff-index --name-only HEAD --"))
230 (setq git-version
(concat git-version
".dirty")))
231 (setq version
(concat version
" (" git-version
")"))))
233 (setq version
(format "Org-mode version %s" version
))
234 (if here
(insert version
))
237 ;;; Compatibility constants
239 ;;; The custom variables
242 "Outline-based notes management and organizer."
247 (defcustom org-mode-hook nil
248 "Mode hook for Org-mode, run after the mode was turned on."
252 (defcustom org-load-hook nil
253 "Hook that is run after org.el has been loaded."
257 (defcustom org-log-buffer-setup-hook nil
258 "Hook that is run after an Org log buffer is created."
262 (defvar org-modules
) ; defined below
263 (defvar org-modules-loaded nil
264 "Have the modules been loaded already?")
266 (defun org-load-modules-maybe (&optional force
)
267 "Load all extensions listed in `org-modules'."
268 (when (or force
(not org-modules-loaded
))
270 (condition-case nil
(require ext
)
271 (error (message "Problems while trying to load feature `%s'" ext
))))
273 (setq org-modules-loaded t
)))
275 (defun org-set-modules (var value
)
276 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
278 (when (featurep 'org
)
279 (org-load-modules-maybe 'force
)))
281 (when (org-bound-and-true-p org-modules
)
282 (let ((a (member 'org-infojs org-modules
)))
283 (and a
(setcar a
'org-jsinfo
))))
285 (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
)
286 "Modules that should always be loaded together with org.el.
287 If a description starts with <C>, the file is not part of Emacs
288 and loading it will require that you have downloaded and properly installed
289 the org-mode distribution.
291 You can also use this system to load external packages (i.e. neither Org
292 core modules, nor modules from the CONTRIB directory). Just add symbols
293 to the end of the list. If the package is called org-xyz.el, then you need
294 to add the symbol `xyz', and the package must have a call to
298 :set
'org-set-modules
301 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
302 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
303 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
304 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
305 (const :tag
" docview: Links to doc-view buffers" org-docview
)
306 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
307 (const :tag
" id: Global IDs for identifying entries" org-id
)
308 (const :tag
" info: Links to Info nodes" org-info
)
309 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
310 (const :tag
" habit: Track your consistency with habits" org-habit
)
311 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
312 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
313 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
314 (const :tag
" mew Links to Mew folders/messages" org-mew
)
315 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
316 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
317 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
318 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
319 (const :tag
" vm: Links to VM folders/messages" org-vm
)
320 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
321 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
322 (const :tag
" mouse: Additional mouse support" org-mouse
)
323 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
325 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
326 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
327 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
328 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
329 (const :tag
"C collector: Collect properties into tables" org-collector
)
330 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
331 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
332 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
333 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
334 (const :tag
"C eval: Include command output as text" org-eval
)
335 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
336 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
337 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
338 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
339 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
341 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
343 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
344 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
345 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
346 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
347 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
348 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
349 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
350 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
351 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
352 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
353 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
354 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
355 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
356 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
357 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
358 (const :tag
"C track: Keep up with Org-mode development" org-track
)
359 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
360 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
361 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
363 (defcustom org-support-shift-select nil
364 "Non-nil means make shift-cursor commands select text when possible.
366 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
367 start selecting a region, or enlarge regions started in this way.
368 In Org-mode, in special contexts, these same keys are used for
369 other purposes, important enough to compete with shift selection.
370 Org tries to balance these needs by supporting `shift-select-mode'
371 outside these special contexts, under control of this variable.
373 The default of this variable is nil, to avoid confusing behavior. Shifted
374 cursor keys will then execute Org commands in the following contexts:
375 - on a headline, changing TODO state (left/right) and priority (up/down)
376 - on a time stamp, changing the time
377 - in a plain list item, changing the bullet type
378 - in a property definition line, switching between allowed values
379 - in the BEGIN line of a clock table (changing the time block).
380 Outside these contexts, the commands will throw an error.
382 When this variable is t and the cursor is not in a special
383 context, Org-mode will support shift-selection for making and
384 enlarging regions. To make this more effective, the bullet
385 cycling will no longer happen anywhere in an item line, but only
386 if the cursor is exactly on the bullet.
388 If you set this variable to the symbol `always', then the keys
389 will not be special in headlines, property lines, and item lines,
390 to make shift selection work there as well. If this is what you
391 want, you can use the following alternative commands: `C-c C-t'
392 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
393 can be used to switch TODO sets, `C-c -' to cycle item bullet
394 types, and properties can be edited by hand or in column view.
396 However, when the cursor is on a timestamp, shift-cursor commands
397 will still edit the time stamp - this is just too good to give up.
399 XEmacs user should have this variable set to nil, because
400 `shift-select-mode' is in Emacs 23 or later only."
403 (const :tag
"Never" nil
)
404 (const :tag
"When outside special context" t
)
405 (const :tag
"Everywhere except timestamps" always
)))
407 (defcustom org-loop-over-headlines-in-active-region nil
408 "Shall some commands act upon headlines in the active region?
410 When set to `t', some commands will be performed in all headlines
411 within the active region.
413 When set to a string, those commands will be performed on the
414 matching headlines within the active region. Such string must be
415 a tags/property/todo match as it is used in the agenda tags view.
417 The list of commands is:
420 :type
'(choice (const :tag
"Don't loop" nil
)
421 (const :tag
"All headlines in active region" t
)
422 (string :tag
"Tags/Property/Todo matcher"))
426 (defgroup org-startup nil
427 "Options concerning startup of Org-mode."
431 (defcustom org-startup-folded t
432 "Non-nil means entering Org-mode will switch to OVERVIEW.
433 This can also be configured on a per-file basis by adding one of
434 the following lines anywhere in the buffer:
436 #+STARTUP: fold (or `overview', this is equivalent)
437 #+STARTUP: nofold (or `showall', this is equivalent)
439 #+STARTUP: showeverything"
442 (const :tag
"nofold: show all" nil
)
443 (const :tag
"fold: overview" t
)
444 (const :tag
"content: all headlines" content
)
445 (const :tag
"show everything, even drawers" showeverything
)))
447 (defcustom org-startup-truncated t
448 "Non-nil means entering Org-mode will set `truncate-lines'.
449 This is useful since some lines containing links can be very long and
450 uninteresting. Also tables look terrible when wrapped."
454 (defcustom org-startup-indented nil
455 "Non-nil means turn on `org-indent-mode' on startup.
456 This can also be configured on a per-file basis by adding one of
457 the following lines anywhere in the buffer:
461 :group
'org-structure
463 (const :tag
"Not" nil
)
464 (const :tag
"Globally (slow on startup in large files)" t
)))
466 (defcustom org-use-sub-superscripts t
467 "Non-nil means interpret \"_\" and \"^\" for export.
468 When this option is turned on, you can use TeX-like syntax for sub- and
469 superscripts. Several characters after \"_\" or \"^\" will be
470 considered as a single item - so grouping with {} is normally not
471 needed. For example, the following things will be parsed as single
472 sub- or superscripts.
474 10^24 or 10^tau several digits will be considered 1 item.
475 10^-12 or 10^-tau a leading sign with digits or a word
476 x^2-y^3 will be read as x^2 - y^3, because items are
477 terminated by almost any nonword/nondigit char.
478 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
480 Still, ambiguity is possible - so when in doubt use {} to enclose the
481 sub/superscript. If you set this variable to the symbol `{}',
482 the braces are *required* in order to trigger interpretations as
483 sub/superscript. This can be helpful in documents that need \"_\"
484 frequently in plain text.
486 Not all export backends support this, but HTML does.
488 This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
490 :group
'org-export-translation
492 (const :tag
"Always interpret" t
)
493 (const :tag
"Only with braces" {})
494 (const :tag
"Never interpret" nil
)))
496 (if (fboundp 'defvaralias
)
497 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
500 (defcustom org-startup-with-beamer-mode nil
501 "Non-nil means turn on `org-beamer-mode' on startup.
502 This can also be configured on a per-file basis by adding one of
503 the following lines anywhere in the buffer:
509 (defcustom org-startup-align-all-tables nil
510 "Non-nil means align all tables when visiting a file.
511 This is useful when the column width in tables is forced with <N> cookies
512 in table fields. Such tables will look correct only after the first re-align.
513 This can also be configured on a per-file basis by adding one of
514 the following lines anywhere in the buffer:
520 (defcustom org-startup-with-inline-images nil
521 "Non-nil means show inline images when loading a new Org file.
522 This can also be configured on a per-file basis by adding one of
523 the following lines anywhere in the buffer:
524 #+STARTUP: inlineimages
525 #+STARTUP: noinlineimages"
529 (defcustom org-insert-mode-line-in-empty-file nil
530 "Non-nil means insert the first line setting Org-mode in empty files.
531 When the function `org-mode' is called interactively in an empty file, this
532 normally means that the file name does not automatically trigger Org-mode.
533 To ensure that the file will always be in Org-mode in the future, a
534 line enforcing Org-mode will be inserted into the buffer, if this option
539 (defcustom org-replace-disputed-keys nil
540 "Non-nil means use alternative key bindings for some keys.
541 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
542 These keys are also used by other packages like shift-selection-mode'
543 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
544 If you want to use Org-mode together with one of these other modes,
545 or more generally if you would like to move some Org-mode commands to
546 other keys, set this variable and configure the keys with the variable
549 This option is only relevant at load-time of Org-mode, and must be set
550 *before* org.el is loaded. Changing it requires a restart of Emacs to
555 (defcustom org-use-extra-keys nil
556 "Non-nil means use extra key sequence definitions for certain commands.
557 This happens automatically if you run XEmacs or if `window-system'
558 is nil. This variable lets you do the same manually. You must
559 set it before loading org.
561 Example: on Carbon Emacs 22 running graphically, with an external
562 keyboard on a Powerbook, the default way of setting M-left might
563 not work for either Alt or ESC. Setting this variable will make
568 (if (fboundp 'defvaralias
)
569 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
571 (defcustom org-disputed-keys
572 '(([(shift up
)] .
[(meta p
)])
573 ([(shift down
)] .
[(meta n
)])
574 ([(shift left
)] .
[(meta -
)])
575 ([(shift right
)] .
[(meta +)])
576 ([(control shift right
)] .
[(meta shift
+)])
577 ([(control shift left
)] .
[(meta shift -
)]))
578 "Keys for which Org-mode and other modes compete.
579 This is an alist, cars are the default keys, second element specifies
580 the alternative to use when `org-replace-disputed-keys' is t.
582 Keys can be specified in any syntax supported by `define-key'.
583 The value of this option takes effect only at Org-mode's startup,
584 therefore you'll have to restart Emacs to apply it after changing."
589 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
590 Or return the original if not disputed.
591 Also apply the translations defined in `org-xemacs-key-equivalents'."
592 (when org-replace-disputed-keys
593 (let* ((nkey (key-description key
))
594 (x (org-find-if (lambda (x)
595 (equal (key-description (car x
)) nkey
))
597 (setq key
(if x
(cdr x
) key
))))
598 (when (featurep 'xemacs
)
599 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
602 (defun org-find-if (predicate seq
)
605 (if (funcall predicate
(car seq
))
606 (throw 'exit
(car seq
))
609 (defun org-defkey (keymap key def
)
610 "Define a key, possibly translated, as returned by `org-key'."
611 (define-key keymap
(org-key key
) def
))
613 (defcustom org-ellipsis nil
614 "The ellipsis to use in the Org-mode outline.
615 When nil, just use the standard three dots. When a string, use that instead,
616 When a face, use the standard 3 dots, but with the specified face.
617 The change affects only Org-mode (which will then use its own display table).
618 Changing this requires executing `M-x org-mode' in a buffer to become
621 :type
'(choice (const :tag
"Default" nil
)
622 (face :tag
"Face" :value org-warning
)
623 (string :tag
"String" :value
"...#")))
625 (defvar org-display-table nil
626 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
628 (defgroup org-keywords nil
629 "Keywords in Org-mode."
633 (defcustom org-deadline-string
"DEADLINE:"
634 "String to mark deadline entries.
635 A deadline is this string, followed by a time stamp. Should be a word,
636 terminated by a colon. You can insert a schedule keyword and
637 a timestamp with \\[org-deadline].
638 Changes become only effective after restarting Emacs."
642 (defcustom org-scheduled-string
"SCHEDULED:"
643 "String to mark scheduled TODO entries.
644 A schedule is this string, followed by a time stamp. Should be a word,
645 terminated by a colon. You can insert a schedule keyword and
646 a timestamp with \\[org-schedule].
647 Changes become only effective after restarting Emacs."
651 (defcustom org-closed-string
"CLOSED:"
652 "String used as the prefix for timestamps logging closing a TODO entry."
656 (defcustom org-clock-string
"CLOCK:"
657 "String used as prefix for timestamps clocking work hours on an item."
661 (defcustom org-comment-string
"COMMENT"
662 "Entries starting with this keyword will never be exported.
663 An entry can be toggled between COMMENT and normal with
664 \\[org-toggle-comment].
665 Changes become only effective after restarting Emacs."
669 (defcustom org-quote-string
"QUOTE"
670 "Entries starting with this keyword will be exported in fixed-width font.
671 Quoting applies only to the text in the entry following the headline, and does
672 not extend beyond the next headline, even if that is lower level.
673 An entry can be toggled between QUOTE and normal with
674 \\[org-toggle-fixed-width-section]."
678 (defconst org-repeat-re
679 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
680 "Regular expression for specifying repeated events.
681 After a match, group 1 contains the repeat expression.")
683 (defgroup org-structure nil
684 "Options concerning the general structure of Org-mode files."
688 (defgroup org-reveal-location nil
689 "Options about how to make context of a location visible."
690 :tag
"Org Reveal Location"
691 :group
'org-structure
)
693 (defconst org-context-choice
695 (const :tag
"Always" t
)
696 (const :tag
"Never" nil
)
697 (repeat :greedy t
:tag
"Individual contexts"
699 (choice :tag
"Context"
706 (const bookmark-jump
)
710 "Contexts for the reveal options.")
712 (defcustom org-show-hierarchy-above
'((default . t
))
713 "Non-nil means show full hierarchy when revealing a location.
714 Org-mode often shows locations in an org-mode file which might have
715 been invisible before. When this is set, the hierarchy of headings
716 above the exposed location is shown.
717 Turning this off for example for sparse trees makes them very compact.
718 Instead of t, this can also be an alist specifying this option for different
719 contexts. Valid contexts are
720 agenda when exposing an entry from the agenda
721 org-goto when using the command `org-goto' on key C-c C-j
722 occur-tree when using the command `org-occur' on key C-c /
723 tags-tree when constructing a sparse tree based on tags matches
724 link-search when exposing search matches associated with a link
725 mark-goto when exposing the jump goal of a mark
726 bookmark-jump when exposing a bookmark location
727 isearch when exiting from an incremental search
728 default default for all contexts not set explicitly"
729 :group
'org-reveal-location
730 :type org-context-choice
)
732 (defcustom org-show-following-heading
'((default . nil
))
733 "Non-nil means show following heading when revealing a location.
734 Org-mode often shows locations in an org-mode file which might have
735 been invisible before. When this is set, the heading following the
737 Turning this off for example for sparse trees makes them very compact,
738 but makes it harder to edit the location of the match. In such a case,
739 use the command \\[org-reveal] to show more context.
740 Instead of t, this can also be an alist specifying this option for different
741 contexts. See `org-show-hierarchy-above' for valid contexts."
742 :group
'org-reveal-location
743 :type org-context-choice
)
745 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
746 "Non-nil means show all sibling heading when revealing a location.
747 Org-mode often shows locations in an org-mode file which might have
748 been invisible before. When this is set, the sibling of the current entry
749 heading are all made visible. If `org-show-hierarchy-above' is t,
750 the same happens on each level of the hierarchy above the current entry.
752 By default this is on for the isearch context, off for all other contexts.
753 Turning this off for example for sparse trees makes them very compact,
754 but makes it harder to edit the location of the match. In such a case,
755 use the command \\[org-reveal] to show more context.
756 Instead of t, this can also be an alist specifying this option for different
757 contexts. See `org-show-hierarchy-above' for valid contexts."
758 :group
'org-reveal-location
759 :type org-context-choice
)
761 (defcustom org-show-entry-below
'((default . nil
))
762 "Non-nil means show the entry below a headline when revealing a location.
763 Org-mode often shows locations in an org-mode file which might have
764 been invisible before. When this is set, the text below the headline that is
765 exposed is also shown.
767 By default this is off for all contexts.
768 Instead of t, this can also be an alist specifying this option for different
769 contexts. See `org-show-hierarchy-above' for valid contexts."
770 :group
'org-reveal-location
771 :type org-context-choice
)
773 (defcustom org-indirect-buffer-display
'other-window
774 "How should indirect tree buffers be displayed?
775 This applies to indirect buffers created with the commands
776 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
778 current-window Display in the current window
779 other-window Just display in another window.
780 dedicated-frame Create one new frame, and re-use it each time.
781 new-frame Make a new frame each time. Note that in this case
782 previously-made indirect buffers are kept, and you need to
783 kill these buffers yourself."
784 :group
'org-structure
785 :group
'org-agenda-windows
787 (const :tag
"In current window" current-window
)
788 (const :tag
"In current frame, other window" other-window
)
789 (const :tag
"Each time a new frame" new-frame
)
790 (const :tag
"One dedicated frame" dedicated-frame
)))
792 (defcustom org-use-speed-commands nil
793 "Non-nil means activate single letter commands at beginning of a headline.
794 This may also be a function to test for appropriate locations where speed
795 commands should be active."
796 :group
'org-structure
798 (const :tag
"Never" nil
)
799 (const :tag
"At beginning of headline stars" t
)
802 (defcustom org-speed-commands-user nil
803 "Alist of additional speed commands.
804 This list will be checked before `org-speed-commands-default'
805 when the variable `org-use-speed-commands' is non-nil
806 and when the cursor is at the beginning of a headline.
807 The car if each entry is a string with a single letter, which must
808 be assigned to `self-insert-command' in the global map.
809 The cdr is either a command to be called interactively, a function
810 to be called, or a form to be evaluated.
811 An entry that is just a list with a single string will be interpreted
812 as a descriptive headline that will be added when listing the speed
813 commands in the Help buffer using the `?' speed command."
814 :group
'org-structure
815 :type
'(repeat :value
("k" . ignore
)
816 (choice :value
("k" . ignore
)
817 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
818 (cons :tag
"Letter and Command"
819 (string :tag
"Command letter")
824 (defgroup org-cycle nil
825 "Options concerning visibility cycling in Org-mode."
827 :group
'org-structure
)
829 (defcustom org-cycle-skip-children-state-if-no-children t
830 "Non-nil means skip CHILDREN state in entries that don't have any."
834 (defcustom org-cycle-max-level nil
835 "Maximum level which should still be subject to visibility cycling.
836 Levels higher than this will, for cycling, be treated as text, not a headline.
837 When `org-odd-levels-only' is set, a value of N in this variable actually
838 means 2N-1 stars as the limiting headline.
839 When nil, cycle all levels.
840 Note that the limiting level of cycling is also influenced by
841 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
842 `org-inlinetask-min-level' is, cycling will be limited to levels one less
846 (const :tag
"No limit" nil
)
847 (integer :tag
"Maximum level")))
849 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
850 "Names of drawers. Drawers are not opened by cycling on the headline above.
851 Drawers only open with a TAB on the drawer line itself. A drawer looks like
856 The drawer \"PROPERTIES\" is special for capturing properties through
859 Drawers can be defined on the per-file basis with a line like:
861 #+DRAWERS: HIDDEN STATE PROPERTIES"
862 :group
'org-structure
864 :type
'(repeat (string :tag
"Drawer Name")))
866 (defcustom org-hide-block-startup nil
867 "Non-nil means entering Org-mode will fold all blocks.
868 This can also be set in on a per-file basis with
870 #+STARTUP: hideblocks
871 #+STARTUP: showblocks"
876 (defcustom org-cycle-global-at-bob nil
877 "Cycle globally if cursor is at beginning of buffer and not at a headline.
878 This makes it possible to do global cycling without having to use S-TAB or
879 \\[universal-argument] TAB. For this special case to work, the first line \
881 must not be a headline - it may be empty or some other text. When used in
882 this way, `org-cycle-hook' is disables temporarily, to make sure the
883 cursor stays at the beginning of the buffer.
884 When this option is nil, don't do anything special at the beginning
889 (defcustom org-cycle-level-after-item
/entry-creation t
890 "Non-nil means cycle entry level or item indentation in new empty entries.
892 When the cursor is at the end of an empty headline, i.e with only stars
893 and maybe a TODO keyword, TAB will then switch the entry to become a child,
894 and then all possible ancestor states, before returning to the original state.
895 This makes data entry extremely fast: M-RET to create a new headline,
896 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
898 When the cursor is at the end of an empty plain list item, one TAB will
899 make it a subitem, two or more tabs will back up to make this an item
900 higher up in the item hierarchy."
904 (defcustom org-cycle-emulate-tab t
905 "Where should `org-cycle' emulate TAB.
907 white Only in completely white lines
908 whitestart Only at the beginning of lines, before the first non-white char
909 t Everywhere except in headlines
910 exc-hl-bol Everywhere except at the start of a headline
911 If TAB is used in a place where it does not emulate TAB, the current subtree
912 visibility is cycled."
914 :type
'(choice (const :tag
"Never" nil
)
915 (const :tag
"Only in completely white lines" white
)
916 (const :tag
"Before first char in a line" whitestart
)
917 (const :tag
"Everywhere except in headlines" t
)
918 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
921 (defcustom org-cycle-separator-lines
2
922 "Number of empty lines needed to keep an empty line between collapsed trees.
923 If you leave an empty line between the end of a subtree and the following
924 headline, this empty line is hidden when the subtree is folded.
925 Org-mode will leave (exactly) one empty line visible if the number of
926 empty lines is equal or larger to the number given in this variable.
927 So the default 2 means at least 2 empty lines after the end of a subtree
928 are needed to produce free space between a collapsed subtree and the
931 If the number is negative, and the number of empty lines is at least -N,
932 all empty lines are shown.
934 Special case: when 0, never leave empty lines in collapsed view."
937 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
939 (defcustom org-pre-cycle-hook nil
940 "Hook that is run before visibility cycling is happening.
941 The function(s) in this hook must accept a single argument which indicates
942 the new state that will be set right after running this hook. The
943 argument is a symbol. Before a global state change, it can have the values
944 `overview', `content', or `all'. Before a local state change, it can have
945 the values `folded', `children', or `subtree'."
949 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
950 org-cycle-hide-drawers
951 org-cycle-show-empty-lines
952 org-optimize-window-after-visibility-change
)
953 "Hook that is run after `org-cycle' has changed the buffer visibility.
954 The function(s) in this hook must accept a single argument which indicates
955 the new state that was set by the most recent `org-cycle' command. The
956 argument is a symbol. After a global state change, it can have the values
957 `overview', `content', or `all'. After a local state change, it can have
958 the values `folded', `children', or `subtree'."
962 (defgroup org-edit-structure nil
963 "Options concerning structure editing in Org-mode."
964 :tag
"Org Edit Structure"
965 :group
'org-structure
)
967 (defcustom org-odd-levels-only nil
968 "Non-nil means skip even levels and only use odd levels for the outline.
969 This has the effect that two stars are being added/taken away in
970 promotion/demotion commands. It also influences how levels are
971 handled by the exporters.
972 Changing it requires restart of `font-lock-mode' to become effective
973 for fontification also in regions already fontified.
974 You may also set this on a per-file basis by adding one of the following
979 :group
'org-edit-structure
980 :group
'org-appearance
983 (defcustom org-adapt-indentation t
984 "Non-nil means adapt indentation to outline node level.
986 When this variable is set, Org assumes that you write outlines by
987 indenting text in each node to align with the headline (after the stars).
988 The following issues are influenced by this variable:
990 - When this is set and the *entire* text in an entry is indented, the
991 indentation is increased by one space in a demotion command, and
992 decreased by one in a promotion command. If any line in the entry
993 body starts with text at column 0, indentation is not changed at all.
995 - Property drawers and planning information is inserted indented when
996 this variable s set. When nil, they will not be indented.
998 - TAB indents a line relative to context. The lines below a headline
999 will be indented when this variable is set.
1001 Note that this is all about true indentation, by adding and removing
1002 space characters. See also `org-indent.el' which does level-dependent
1003 indentation in a virtual way, i.e. at display time in Emacs."
1004 :group
'org-edit-structure
1007 (defcustom org-special-ctrl-a
/e nil
1008 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1010 When t, `C-a' will bring back the cursor to the beginning of the
1011 headline text, i.e. after the stars and after a possible TODO keyword.
1012 In an item, this will be the position after the bullet.
1013 When the cursor is already at that position, another `C-a' will bring
1014 it to the beginning of the line.
1016 `C-e' will jump to the end of the headline, ignoring the presence of tags
1017 in the headline. A second `C-e' will then jump to the true end of the
1018 line, after any tags. This also means that, when this variable is
1019 non-nil, `C-e' also will never jump beyond the end of the heading of a
1020 folded section, i.e. not after the ellipses.
1022 When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
1023 going to the true line boundary first. Only a directly following, identical
1024 keypress will bring the cursor to the special positions.
1026 This may also be a cons cell where the behavior for `C-a' and `C-e' is
1028 :group
'org-edit-structure
1030 (const :tag
"off" nil
)
1031 (const :tag
"on: after stars/bullet and before tags first" t
)
1032 (const :tag
"reversed: true line boundary first" reversed
)
1033 (cons :tag
"Set C-a and C-e separately"
1034 (choice :tag
"Special C-a"
1035 (const :tag
"off" nil
)
1036 (const :tag
"on: after stars/bullet first" t
)
1037 (const :tag
"reversed: before stars/bullet first" reversed
))
1038 (choice :tag
"Special C-e"
1039 (const :tag
"off" nil
)
1040 (const :tag
"on: before tags first" t
)
1041 (const :tag
"reversed: after tags first" reversed
)))))
1042 (if (fboundp 'defvaralias
)
1043 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1045 (defcustom org-special-ctrl-k nil
1046 "Non-nil means `C-k' will behave specially in headlines.
1047 When nil, `C-k' will call the default `kill-line' command.
1048 When t, the following will happen while the cursor is in the headline:
1050 - When the cursor is at the beginning of a headline, kill the entire
1051 line and possible the folded subtree below the line.
1052 - When in the middle of the headline text, kill the headline up to the tags.
1053 - When after the headline text, kill the tags."
1054 :group
'org-edit-structure
1057 (defcustom org-ctrl-k-protect-subtree nil
1058 "Non-nil means, do not delete a hidden subtree with C-k.
1059 When set to the symbol `error', simply throw an error when C-k is
1060 used to kill (part-of) a headline that has hidden text behind it.
1061 Any other non-nil value will result in a query to the user, if it is
1062 OK to kill that hidden subtree. When nil, kill without remorse."
1063 :group
'org-edit-structure
1065 (const :tag
"Do not protect hidden subtrees" nil
)
1066 (const :tag
"Protect hidden subtrees with a security query" t
)
1067 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1069 (defcustom org-catch-invisible-edits nil
1070 "Check if in invisible region before inserting or deleting a character.
1073 nil Do not check, so just do invisible edits.
1074 error Throw an error and do nothing.
1075 show Make point visible, and do the requested edit.
1076 show-and-error Make point visible, then throw an error and abort the edit.
1077 smart Make point visible, and do insertion/deletion if it is
1078 adjacent to visible text and the change feels predictable.
1079 Never delete a previously invisible character or add in the
1080 middle or right after an invisible region. Basically, this
1081 allows insertion and backward-delete right before ellipses.
1082 FIXME: maybe in this case we should not even show?"
1083 :group
'org-edit-structure
1085 (const :tag
"Do not check" nil
)
1086 (const :tag
"Throw error when trying to edit" error
)
1087 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1088 (const :tag
"Show invisible part and do the edit" show
)
1089 (const :tag
"Be smart and do the right thing" smart
)))
1091 (defcustom org-yank-folded-subtrees t
1092 "Non-nil means when yanking subtrees, fold them.
1093 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1094 it starts with a heading and all other headings in it are either children
1095 or siblings, then fold all the subtrees. However, do this only if no
1096 text after the yank would be swallowed into a folded tree by this action."
1097 :group
'org-edit-structure
1100 (defcustom org-yank-adjusted-subtrees nil
1101 "Non-nil means when yanking subtrees, adjust the level.
1102 With this setting, `org-paste-subtree' is used to insert the subtree, see
1103 this function for details."
1104 :group
'org-edit-structure
1107 (defcustom org-M-RET-may-split-line
'((default . t
))
1108 "Non-nil means M-RET will split the line at the cursor position.
1109 When nil, it will go to the end of the line before making a
1111 You may also set this option in a different way for different
1112 contexts. Valid contexts are:
1114 headline when creating a new headline
1115 item when creating a new item
1116 table in a table field
1117 default the value to be used for all contexts not explicitly
1119 :group
'org-structure
1122 (const :tag
"Always" t
)
1123 (const :tag
"Never" nil
)
1124 (repeat :greedy t
:tag
"Individual contexts"
1126 (choice :tag
"Context"
1134 (defcustom org-insert-heading-respect-content nil
1135 "Non-nil means insert new headings after the current subtree.
1136 When nil, the new heading is created directly after the current line.
1137 The commands \\[org-insert-heading-respect-content] and
1138 \\[org-insert-todo-heading-respect-content] turn this variable on
1139 for the duration of the command."
1140 :group
'org-structure
1143 (defcustom org-blank-before-new-entry
'((heading . auto
)
1144 (plain-list-item . auto
))
1145 "Should `org-insert-heading' leave a blank line before new heading/item?
1146 The value is an alist, with `heading' and `plain-list-item' as CAR,
1147 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1148 which case Org will look at the surrounding headings/items and try to
1149 make an intelligent decision whether to insert a blank line or not.
1151 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1152 set, the setting here is ignored and no empty line is inserted, to avoid
1153 breaking the list structure."
1154 :group
'org-edit-structure
1156 (cons (const heading
)
1157 (choice (const :tag
"Never" nil
)
1158 (const :tag
"Always" t
)
1159 (const :tag
"Auto" auto
)))
1160 (cons (const plain-list-item
)
1161 (choice (const :tag
"Never" nil
)
1162 (const :tag
"Always" t
)
1163 (const :tag
"Auto" auto
)))))
1165 (defcustom org-insert-heading-hook nil
1166 "Hook being run after inserting a new heading."
1167 :group
'org-edit-structure
1170 (defcustom org-enable-fixed-width-editor t
1171 "Non-nil means lines starting with \":\" are treated as fixed-width.
1172 This currently only means they are never auto-wrapped.
1173 When nil, such lines will be treated like ordinary lines.
1174 See also the QUOTE keyword."
1175 :group
'org-edit-structure
1178 (defcustom org-goto-auto-isearch t
1179 "Non-nil means typing characters in `org-goto' starts incremental search."
1180 :group
'org-edit-structure
1183 (defgroup org-sparse-trees nil
1184 "Options concerning sparse trees in Org-mode."
1185 :tag
"Org Sparse Trees"
1186 :group
'org-structure
)
1188 (defcustom org-highlight-sparse-tree-matches t
1189 "Non-nil means highlight all matches that define a sparse tree.
1190 The highlights will automatically disappear the next time the buffer is
1191 changed by an edit command."
1192 :group
'org-sparse-trees
1195 (defcustom org-remove-highlights-with-change t
1196 "Non-nil means any change to the buffer will remove temporary highlights.
1197 Such highlights are created by `org-occur' and `org-clock-display'.
1198 When nil, `C-c C-c needs to be used to get rid of the highlights.
1199 The highlights created by `org-preview-latex-fragment' always need
1200 `C-c C-c' to be removed."
1201 :group
'org-sparse-trees
1206 (defcustom org-occur-hook
'(org-first-headline-recenter)
1207 "Hook that is run after `org-occur' has constructed a sparse tree.
1208 This can be used to recenter the window to show as much of the structure
1210 :group
'org-sparse-trees
1213 (defgroup org-imenu-and-speedbar nil
1214 "Options concerning imenu and speedbar in Org-mode."
1215 :tag
"Org Imenu and Speedbar"
1216 :group
'org-structure
)
1218 (defcustom org-imenu-depth
2
1219 "The maximum level for Imenu access to Org-mode headlines.
1220 This also applied for speedbar access."
1221 :group
'org-imenu-and-speedbar
1224 (defgroup org-table nil
1225 "Options concerning tables in Org-mode."
1229 (defcustom org-enable-table-editor
'optimized
1230 "Non-nil means lines starting with \"|\" are handled by the table editor.
1231 When nil, such lines will be treated like ordinary lines.
1233 When equal to the symbol `optimized', the table editor will be optimized to
1235 - Automatic overwrite mode in front of whitespace in table fields.
1236 This makes the structure of the table stay in tact as long as the edited
1237 field does not exceed the column width.
1238 - Minimize the number of realigns. Normally, the table is aligned each time
1239 TAB or RET are pressed to move to another field. With optimization this
1240 happens only if changes to a field might have changed the column width.
1241 Optimization requires replacing the functions `self-insert-command',
1242 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1243 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1244 very good at guessing when a re-align will be necessary, but you can always
1245 force one with \\[org-ctrl-c-ctrl-c].
1247 If you would like to use the optimized version in Org-mode, but the
1248 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1250 This variable can be used to turn on and off the table editor during a session,
1251 but in order to toggle optimization, a restart is required.
1253 See also the variable `org-table-auto-blank-field'."
1256 (const :tag
"off" nil
)
1258 (const :tag
"on, optimized" optimized
)))
1260 (defcustom org-self-insert-cluster-for-undo t
1261 "Non-nil means cluster self-insert commands for undo when possible.
1262 If this is set, then, like in the Emacs command loop, 20 consecutive
1263 characters will be undone together.
1264 This is configurable, because there is some impact on typing performance."
1268 (defcustom org-table-tab-recognizes-table.el t
1269 "Non-nil means TAB will automatically notice a table.el table.
1270 When it sees such a table, it moves point into it and - if necessary -
1271 calls `table-recognize-table'."
1272 :group
'org-table-editing
1275 (defgroup org-link nil
1276 "Options concerning links in Org-mode."
1280 (defvar org-link-abbrev-alist-local nil
1281 "Buffer-local version of `org-link-abbrev-alist', which see.
1282 The value of this is taken from the #+LINK lines.")
1283 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1285 (defcustom org-link-abbrev-alist nil
1286 "Alist of link abbreviations.
1287 The car of each element is a string, to be replaced at the start of a link.
1288 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1289 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1291 [[linkkey:tag][description]]
1293 The 'linkkey' must be a word word, starting with a letter, followed
1294 by letters, numbers, '-' or '_'.
1296 If REPLACE is a string, the tag will simply be appended to create the link.
1297 If the string contains \"%s\", the tag will be inserted there. Alternatively,
1298 the placeholder \"%h\" will cause a url-encoded version of the tag to
1299 be inserted at that point (see the function `url-hexify-string').
1301 REPLACE may also be a function that will be called with the tag as the
1302 only argument to create the link, which should be returned as a string.
1304 See the manual for examples."
1308 (string :tag
"Protocol")
1310 (string :tag
"Format")
1313 (defcustom org-descriptive-links t
1314 "Non-nil means Org will display descriptive links.
1315 E.g. [[http://orgmode.org][Org website]] will be displayed as
1316 \"Org Website\", hiding the link itself and just displaying its
1317 description. When set to `nil', Org will display the full links
1320 You can interactively set the value of this variable by calling
1321 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1325 (defcustom org-link-file-path-type
'adaptive
1326 "How the path name in file links should be stored.
1329 relative Relative to the current directory, i.e. the directory of the file
1330 into which the link is being inserted.
1331 absolute Absolute path, if possible with ~ for home directory.
1332 noabbrev Absolute path, no abbreviation of home directory.
1333 adaptive Use relative path for files in the current directory and sub-
1334 directories of it. For other files, use an absolute path."
1342 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1343 "Types of links that should be activated in Org-mode files.
1344 This is a list of symbols, each leading to the activation of a certain link
1345 type. In principle, it does not hurt to turn on most link types - there may
1346 be a small gain when turning off unused link types. The types are:
1348 bracket The recommended [[link][description]] or [[link]] links with hiding.
1349 angle Links in angular brackets that may contain whitespace like
1350 <bbdb:Carsten Dominik>.
1351 plain Plain links in normal text, no whitespace, like http://google.com.
1352 radio Text that is matched by a radio target, see manual for details.
1353 tag Tag settings in a headline (link to tag search).
1354 date Time stamps (link to calendar).
1355 footnote Footnote labels.
1357 Changing this variable requires a restart of Emacs to become effective."
1359 :type
'(set :greedy t
1360 (const :tag
"Double bracket links" bracket
)
1361 (const :tag
"Angular bracket links" angle
)
1362 (const :tag
"Plain text links" plain
)
1363 (const :tag
"Radio target matches" radio
)
1364 (const :tag
"Tags" tag
)
1365 (const :tag
"Timestamps" date
)
1366 (const :tag
"Footnotes" footnote
)))
1368 (defcustom org-make-link-description-function nil
1369 "Function to use to generate link descriptions from links.
1370 If nil the link location will be used. This function must take
1371 two parameters; the first is the link and the second the
1372 description `org-insert-link' has generated, and should return the
1373 description to use."
1377 (defgroup org-link-store nil
1378 "Options concerning storing links in Org-mode."
1379 :tag
"Org Store Link"
1382 (defcustom org-email-link-description-format
"Email %c: %.30s"
1383 "Format of the description part of a link to an email or usenet message.
1384 The following %-escapes will be replaced by corresponding information:
1386 %F full \"From\" field
1387 %f name, taken from \"From\" field, address if no name
1388 %T full \"To\" field
1389 %t first name in \"To\" field, address if no name
1390 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1391 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1396 You may use normal field width specification between the % and the letter.
1397 This is for example useful to limit the length of the subject.
1399 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1400 :group
'org-link-store
1403 (defcustom org-from-is-user-regexp
1405 (when (and user-mail-address
(not (string= user-mail-address
"")))
1406 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1407 (when (and user-full-name
(not (string= user-full-name
"")))
1408 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1409 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1410 "Regexp matched against the \"From:\" header of an email or usenet message.
1411 It should match if the message is from the user him/herself."
1412 :group
'org-link-store
1415 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1416 "Non-nil means storing a link to an Org file will use entry IDs.
1418 Note that before this variable is even considered, org-id must be loaded,
1419 so please customize `org-modules' and turn it on.
1421 The variable can have the following values:
1423 t Create an ID if needed to make a link to the current entry.
1425 create-if-interactive
1426 If `org-store-link' is called directly (interactively, as a user
1427 command), do create an ID to support the link. But when doing the
1428 job for remember, only use the ID if it already exists. The
1429 purpose of this setting is to avoid proliferation of unwanted
1430 IDs, just because you happen to be in an Org file when you
1431 call `org-remember' that automatically and preemptively
1432 creates a link. If you do want to get an ID link in a remember
1433 template to an entry not having an ID, create it first by
1434 explicitly creating a link to it, using `C-c C-l' first.
1436 create-if-interactive-and-no-custom-id
1437 Like create-if-interactive, but do not create an ID if there is
1438 a CUSTOM_ID property defined in the entry. This is the default.
1441 Use existing ID, do not create one.
1443 nil Never use an ID to make a link, instead link using a text search for
1445 :group
'org-link-store
1447 (const :tag
"Create ID to make link" t
)
1448 (const :tag
"Create if storing link interactively"
1449 create-if-interactive
)
1450 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1451 create-if-interactive-and-no-custom-id
)
1452 (const :tag
"Only use existing" use-existing
)
1453 (const :tag
"Do not use ID to create link" nil
)))
1455 (defcustom org-context-in-file-links t
1456 "Non-nil means file links from `org-store-link' contain context.
1457 A search string will be added to the file name with :: as separator and
1458 used to find the context when the link is activated by the command
1459 `org-open-at-point'. When this option is t, the entire active region
1460 will be placed in the search string of the file link. If set to a
1461 positive integer, only the first n lines of context will be stored.
1463 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1464 negates this setting for the duration of the command."
1465 :group
'org-link-store
1466 :type
'(choice boolean integer
))
1468 (defcustom org-keep-stored-link-after-insertion nil
1469 "Non-nil means keep link in list for entire session.
1471 The command `org-store-link' adds a link pointing to the current
1472 location to an internal list. These links accumulate during a session.
1473 The command `org-insert-link' can be used to insert links into any
1474 Org-mode file (offering completion for all stored links). When this
1475 option is nil, every link which has been inserted once using \\[org-insert-link]
1476 will be removed from the list, to make completing the unused links
1478 :group
'org-link-store
1481 (defgroup org-link-follow nil
1482 "Options concerning following links in Org-mode."
1483 :tag
"Org Follow Link"
1486 (defcustom org-link-translation-function nil
1487 "Function to translate links with different syntax to Org syntax.
1488 This can be used to translate links created for example by the Planner
1489 or emacs-wiki packages to Org syntax.
1490 The function must accept two parameters, a TYPE containing the link
1491 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1492 which is everything after the link protocol. It should return a cons
1493 with possibly modified values of type and path.
1494 Org contains a function for this, so if you set this variable to
1495 `org-translate-link-from-planner', you should be able follow many
1496 links created by planner."
1497 :group
'org-link-follow
1500 (defcustom org-follow-link-hook nil
1501 "Hook that is run after a link has been followed."
1502 :group
'org-link-follow
1505 (defcustom org-tab-follows-link nil
1506 "Non-nil means on links TAB will follow the link.
1507 Needs to be set before org.el is loaded.
1508 This really should not be used, it does not make sense, and the
1509 implementation is bad."
1510 :group
'org-link-follow
1513 (defcustom org-return-follows-link nil
1514 "Non-nil means on links RET will follow the link."
1515 :group
'org-link-follow
1518 (defcustom org-mouse-1-follows-link
1519 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1520 "Non-nil means mouse-1 on a link will follow the link.
1521 A longer mouse click will still set point. Does not work on XEmacs.
1522 Needs to be set before org.el is loaded."
1523 :group
'org-link-follow
1526 (defcustom org-mark-ring-length
4
1527 "Number of different positions to be recorded in the ring.
1528 Changing this requires a restart of Emacs to work correctly."
1529 :group
'org-link-follow
1532 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1533 "Non-nil means internal links in Org files must exactly match a headline.
1534 When nil, the link search tries to match a phrase with all words
1535 in the search text."
1536 :group
'org-link-follow
1538 (const :tag
"Use fuzzy text search" nil
)
1539 (const :tag
"Match only exact headline" t
)
1540 (const :tag
"Match exact headline or query to create it"
1543 (defcustom org-link-frame-setup
1544 '((vm . vm-visit-folder-other-frame
)
1545 (gnus . org-gnus-no-new-news
)
1546 (file . find-file-other-window
)
1547 (wl . wl-other-frame
))
1548 "Setup the frame configuration for following links.
1549 When following a link with Emacs, it may often be useful to display
1550 this link in another window or frame. This variable can be used to
1551 set this up for the different types of links.
1554 `vm-visit-folder-other-window'
1555 `vm-visit-folder-other-frame'
1556 For Gnus, use any of
1559 `org-gnus-no-new-news'
1560 For FILE, use any of
1562 `find-file-other-window'
1563 `find-file-other-frame'
1564 For Wanderlust use any of
1567 For the calendar, use the variable `calendar-setup'.
1568 For BBDB, it is currently only possible to display the matches in
1570 :group
'org-link-follow
1574 (const vm-visit-folder
)
1575 (const vm-visit-folder-other-window
)
1576 (const vm-visit-folder-other-frame
)))
1580 (const gnus-other-frame
)
1581 (const org-gnus-no-new-news
)))
1585 (const find-file-other-window
)
1586 (const find-file-other-frame
)))
1590 (const wl-other-frame
)))))
1592 (defcustom org-display-internal-link-with-indirect-buffer nil
1593 "Non-nil means use indirect buffer to display infile links.
1594 Activating internal links (from one location in a file to another location
1595 in the same file) normally just jumps to the location. When the link is
1596 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1598 another window. When this option is set, the other window actually displays
1599 an indirect buffer clone of the current buffer, to avoid any visibility
1600 changes to the current buffer."
1601 :group
'org-link-follow
1604 (defcustom org-open-non-existing-files nil
1605 "Non-nil means `org-open-file' will open non-existing files.
1606 When nil, an error will be generated.
1607 This variable applies only to external applications because they
1608 might choke on non-existing files. If the link is to a file that
1609 will be opened in Emacs, the variable is ignored."
1610 :group
'org-link-follow
1613 (defcustom org-open-directory-means-index-dot-org nil
1614 "Non-nil means a link to a directory really means to index.org.
1615 When nil, following a directory link will run dired or open a finder/explorer
1616 window on that directory."
1617 :group
'org-link-follow
1620 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1621 "Function and arguments to call for following mailto links.
1622 This is a list with the first element being a Lisp function, and the
1623 remaining elements being arguments to the function. In string arguments,
1624 %a will be replaced by the address, and %s will be replaced by the subject
1625 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1626 :group
'org-link-follow
1628 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1629 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1630 (const :tag
"message-mail" (message-mail "%a" "%s"))
1631 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1633 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1634 "Non-nil means ask for confirmation before executing shell links.
1635 Shell links can be dangerous: just think about a link
1637 [[shell:rm -rf ~/*][Google Search]]
1639 This link would show up in your Org-mode document as \"Google Search\",
1640 but really it would remove your entire home directory.
1641 Therefore we advise against setting this variable to nil.
1642 Just change it to `y-or-n-p' if you want to confirm with a
1643 single keystroke rather than having to type \"yes\"."
1644 :group
'org-link-follow
1646 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1647 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1648 (const :tag
"no confirmation (dangerous)" nil
)))
1649 (put 'org-confirm-shell-link-function
1650 'safe-local-variable
1651 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1653 (defcustom org-confirm-shell-link-not-regexp
""
1654 "A regexp to skip confirmation for shell links."
1655 :group
'org-link-follow
1658 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1659 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1660 Elisp links can be dangerous: just think about a link
1662 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1664 This link would show up in your Org-mode document as \"Google Search\",
1665 but really it would remove your entire home directory.
1666 Therefore we advise against setting this variable to nil.
1667 Just change it to `y-or-n-p' if you want to confirm with a
1668 single keystroke rather than having to type \"yes\"."
1669 :group
'org-link-follow
1671 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1672 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1673 (const :tag
"no confirmation (dangerous)" nil
)))
1674 (put 'org-confirm-shell-link-function
1675 'safe-local-variable
1676 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1678 (defcustom org-confirm-elisp-link-not-regexp
""
1679 "A regexp to skip confirmation for Elisp links."
1680 :group
'org-link-follow
1683 (defconst org-file-apps-defaults-gnu
1687 "Default file applications on a UNIX or GNU/Linux system.
1688 See `org-file-apps'.")
1690 (defconst org-file-apps-defaults-macosx
1693 (system .
"open %s")
1695 ("eps.gz" .
"gv %s")
1697 ("fig" .
"xfig %s"))
1698 "Default file applications on a MacOS X system.
1699 The system \"open\" is known as a default, but we use X11 applications
1700 for some files for which the OS does not have a good default.
1701 See `org-file-apps'.")
1703 (defconst org-file-apps-defaults-windowsnt
1707 (list (if (featurep 'xemacs
)
1708 'mswindows-shell-execute
1712 (list (if (featurep 'xemacs
)
1713 'mswindows-shell-execute
1716 "Default file applications on a Windows NT system.
1717 The system \"open\" is used for most files.
1718 See `org-file-apps'.")
1720 (defcustom org-file-apps
1723 ("\\.mm\\'" . default
)
1724 ("\\.x?html?\\'" . default
)
1725 ("\\.pdf\\'" . default
)
1727 "External applications for opening `file:path' items in a document.
1728 Org-mode uses system defaults for different file types, but
1729 you can use this variable to set the application for a given file
1730 extension. The entries in this list are cons cells where the car identifies
1731 files and the cdr the corresponding command. Possible values for the
1733 \"string\" A string as a file identifier can be interpreted in different
1734 ways, depending on its contents:
1736 - Alphanumeric characters only:
1737 Match links with this file extension.
1738 Example: (\"pdf\" . \"evince %s\")
1739 to open PDFs with evince.
1741 - Regular expression: Match links where the
1742 filename matches the regexp. If you want to
1743 use groups here, use shy groups.
1745 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1746 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1747 to open *.html and *.xhtml with firefox.
1749 - Regular expression which contains (non-shy) groups:
1750 Match links where the whole link, including \"::\", and
1751 anything after that, matches the regexp.
1752 In a custom command string, %1, %2, etc. are replaced with
1753 the parts of the link that were matched by the groups.
1754 For backwards compatibility, if a command string is given
1755 that does not use any of the group matches, this case is
1756 handled identically to the second one (i.e. match against
1758 In a custom lisp form, you can access the group matches with
1759 (match-string n link).
1761 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1762 to open [[file:document.pdf::5]] with evince at page 5.
1764 `directory' Matches a directory
1765 `remote' Matches a remote file, accessible through tramp or efs.
1766 Remote files most likely should be visited through Emacs
1767 because external applications cannot handle such paths.
1768 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1769 so all files Emacs knows how to handle. Using this with
1770 command `emacs' will open most files in Emacs. Beware that this
1771 will also open html files inside Emacs, unless you add
1772 (\"html\" . default) to the list as well.
1773 t Default for files not matched by any of the other options.
1774 `system' The system command to open files, like `open' on Windows
1775 and Mac OS X, and mailcap under GNU/Linux. This is the command
1776 that will be selected if you call `C-c C-o' with a double
1777 \\[universal-argument] \\[universal-argument] prefix.
1779 Possible values for the command are:
1780 `emacs' The file will be visited by the current Emacs process.
1781 `default' Use the default application for this file type, which is the
1782 association for t in the list, most likely in the system-specific
1784 This can be used to overrule an unwanted setting in the
1785 system-specific variable.
1786 `system' Use the system command for opening files, like \"open\".
1787 This command is specified by the entry whose car is `system'.
1788 Most likely, the system-specific version of this variable
1789 does define this command, but you can overrule/replace it
1791 string A command to be executed by a shell; %s will be replaced
1792 by the path to the file.
1793 sexp A Lisp form which will be evaluated. The file path will
1794 be available in the Lisp variable `file'.
1795 For more examples, see the system specific constants
1796 `org-file-apps-defaults-macosx'
1797 `org-file-apps-defaults-windowsnt'
1798 `org-file-apps-defaults-gnu'."
1799 :group
'org-link-follow
1801 (cons (choice :value
""
1802 (string :tag
"Extension")
1803 (const :tag
"System command to open files" system
)
1804 (const :tag
"Default for unrecognized files" t
)
1805 (const :tag
"Remote file" remote
)
1806 (const :tag
"Links to a directory" directory
)
1807 (const :tag
"Any files that have Emacs modes"
1810 (const :tag
"Visit with Emacs" emacs
)
1811 (const :tag
"Use default" default
)
1812 (const :tag
"Use the system command" system
)
1813 (string :tag
"Command")
1814 (sexp :tag
"Lisp form")))))
1818 (defgroup org-refile nil
1819 "Options concerning refiling entries in Org-mode."
1823 (defcustom org-directory
"~/org"
1824 "Directory with org files.
1825 This is just a default location to look for Org files. There is no need
1826 at all to put your files into this directory. It is only used in the
1827 following situations:
1829 1. When a remember template specifies a target file that is not an
1830 absolute path. The path will then be interpreted relative to
1832 2. When a remember note is filed away in an interactive way (when exiting the
1833 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1834 with `org-directory' as the default path."
1836 :group
'org-remember
1839 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1840 "Default target for storing notes.
1841 Used as a fall back file for org-remember.el and org-capture.el, for
1842 templates that do not specify a target file."
1844 :group
'org-remember
1846 (const :tag
"Default from remember-data-file" nil
)
1849 (defcustom org-goto-interface
'outline
1850 "The default interface to be used for `org-goto'.
1852 outline The interface shows an outline of the relevant file
1853 and the correct heading is found by moving through
1854 the outline or by searching with incremental search.
1855 outline-path-completion Headlines in the current buffer are offered via
1856 completion. This is the interface also used by
1857 the refile command."
1860 (const :tag
"Outline" outline
)
1861 (const :tag
"Outline-path-completion" outline-path-completion
)))
1863 (defcustom org-goto-max-level
5
1864 "Maximum target level when running `org-goto' with refile interface."
1868 (defcustom org-reverse-note-order nil
1869 "Non-nil means store new notes at the beginning of a file or entry.
1870 When nil, new notes will be filed to the end of a file or entry.
1871 This can also be a list with cons cells of regular expressions that
1872 are matched against file names, and values."
1873 :group
'org-remember
1876 (const :tag
"Reverse always" t
)
1877 (const :tag
"Reverse never" nil
)
1878 (repeat :tag
"By file name regexp"
1879 (cons regexp boolean
))))
1881 (defcustom org-log-refile nil
1882 "Information to record when a task is refiled.
1884 Possible values are:
1886 nil Don't add anything
1887 time Add a time stamp to the task
1888 note Prompt for a note and add it with template `org-log-note-headings'
1890 This option can also be set with on a per-file-basis with
1892 #+STARTUP: nologrefile
1893 #+STARTUP: logrefile
1894 #+STARTUP: lognoterefile
1896 You can have local logging settings for a subtree by setting the LOGGING
1897 property to one or more of these keywords.
1899 When bulk-refiling from the agenda, the value `note' is forbidden and
1900 will temporarily be changed to `time'."
1902 :group
'org-progress
1904 (const :tag
"No logging" nil
)
1905 (const :tag
"Record timestamp" time
)
1906 (const :tag
"Record timestamp with note." note
)))
1908 (defcustom org-refile-targets nil
1909 "Targets for refiling entries with \\[org-refile].
1910 This is a list of cons cells. Each cell contains:
1911 - a specification of the files to be considered, either a list of files,
1912 or a symbol whose function or variable value will be used to retrieve
1913 a file name or a list of file names. If you use `org-agenda-files' for
1914 that, all agenda files will be scanned for targets. Nil means consider
1915 headings in the current buffer.
1916 - A specification of how to find candidate refile targets. This may be
1918 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1919 This tag has to be present in all target headlines, inheritance will
1921 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1923 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1924 headlines that are refiling targets.
1925 - a cons cell (:level . N). Any headline of level N is considered a target.
1926 Note that, when `org-odd-levels-only' is set, level corresponds to
1927 order in hierarchy, not to the number of stars.
1928 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1929 Note that, when `org-odd-levels-only' is set, level corresponds to
1930 order in hierarchy, not to the number of stars.
1932 Each element of this list generates a set of possible targets.
1933 The union of these sets is presented (with completion) to
1934 the user by `org-refile'.
1936 You can set the variable `org-refile-target-verify-function' to a function
1937 to verify each headline found by the simple criteria above.
1939 When this variable is nil, all top-level headlines in the current buffer
1940 are used, equivalent to the value `((nil . (:level . 1))'."
1944 (choice :value org-agenda-files
1945 (const :tag
"All agenda files" org-agenda-files
)
1946 (const :tag
"Current buffer" nil
)
1947 (function) (variable) (file))
1948 (choice :tag
"Identify target headline by"
1949 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1950 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1951 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1952 (cons :tag
"Level number" (const :value
:level
) (integer))
1953 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1955 (defcustom org-refile-target-verify-function nil
1956 "Function to verify if the headline at point should be a refile target.
1957 The function will be called without arguments, with point at the
1958 beginning of the headline. It should return t and leave point
1959 where it is if the headline is a valid target for refiling.
1961 If the target should not be selected, the function must return nil.
1962 In addition to this, it may move point to a place from where the search
1963 should be continued. For example, the function may decide that the entire
1964 subtree of the current entry should be excluded and move point to the end
1969 (defcustom org-refile-use-cache nil
1970 "Non-nil means cache refile targets to speed up the process.
1971 The cache for a particular file will be updated automatically when
1972 the buffer has been killed, or when any of the marker used for flagging
1973 refile targets no longer points at a live buffer.
1974 If you have added new entries to a buffer that might themselves be targets,
1975 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
1976 find that easier, `C-u C-u C-u C-c C-w'."
1980 (defcustom org-refile-use-outline-path nil
1981 "Non-nil means provide refile targets as paths.
1982 So a level 3 headline will be available as level1/level2/level3.
1984 When the value is `file', also include the file name (without directory)
1985 into the path. In this case, you can also stop the completion after
1986 the file name, to get entries inserted as top level in the file.
1988 When `full-file-path', include the full file path."
1991 (const :tag
"Not" nil
)
1992 (const :tag
"Yes" t
)
1993 (const :tag
"Start with file name" file
)
1994 (const :tag
"Start with full file path" full-file-path
)))
1996 (defcustom org-outline-path-complete-in-steps t
1997 "Non-nil means complete the outline path in hierarchical steps.
1998 When Org-mode uses the refile interface to select an outline path
1999 \(see variable `org-refile-use-outline-path'), the completion of
2000 the path can be done is a single go, or if can be done in steps down
2001 the headline hierarchy. Going in steps is probably the best if you
2002 do not use a special completion package like `ido' or `icicles'.
2003 However, when using these packages, going in one step can be very
2004 fast, while still showing the whole path to the entry."
2008 (defcustom org-refile-allow-creating-parent-nodes nil
2009 "Non-nil means allow to create new nodes as refile targets.
2010 New nodes are then created by adding \"/new node name\" to the completion
2011 of an existing node. When the value of this variable is `confirm',
2012 new node creation must be confirmed by the user (recommended)
2013 When nil, the completion must match an existing entry.
2015 Note that, if the new heading is not seen by the criteria
2016 listed in `org-refile-targets', multiple instances of the same
2017 heading would be created by trying again to file under the new
2021 (const :tag
"Never" nil
)
2022 (const :tag
"Always" t
)
2023 (const :tag
"Prompt for confirmation" confirm
)))
2025 (defcustom org-refile-active-region-within-subtree nil
2026 "Non-nil means also refile active region within a subtree.
2028 By default `org-refile' doesn't allow refiling regions if they
2029 don't contain a set of subtrees, but it might be convenient to
2030 do so sometimes: in that case, the first line of the region is
2031 converted to a headline before refiling."
2035 (defgroup org-todo nil
2036 "Options concerning TODO items in Org-mode."
2040 (defgroup org-progress nil
2041 "Options concerning Progress logging in Org-mode."
2045 (defvar org-todo-interpretation-widgets
2046 '((:tag
"Sequence (cycling hits every state)" sequence
)
2047 (:tag
"Type (cycling directly to DONE)" type
))
2048 "The available interpretation symbols for customizing `org-todo-keywords'.
2049 Interested libraries should add to this list.")
2051 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2052 "List of TODO entry keyword sequences and their interpretation.
2053 \\<org-mode-map>This is a list of sequences.
2055 Each sequence starts with a symbol, either `sequence' or `type',
2056 indicating if the keywords should be interpreted as a sequence of
2057 action steps, or as different types of TODO items. The first
2058 keywords are states requiring action - these states will select a headline
2059 for inclusion into the global TODO list Org-mode produces. If one of
2060 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2061 signify that no further action is necessary. If \"|\" is not found,
2062 the last keyword is treated as the only DONE state of the sequence.
2064 The command \\[org-todo] cycles an entry through these states, and one
2065 additional state where no keyword is present. For details about this
2066 cycling, see the manual.
2068 TODO keywords and interpretation can also be set on a per-file basis with
2069 the special #+SEQ_TODO and #+TYP_TODO lines.
2071 Each keyword can optionally specify a character for fast state selection
2072 \(in combination with the variable `org-use-fast-todo-selection')
2073 and specifiers for state change logging, using the same syntax
2074 that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
2075 that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2076 indicates to record a time stamp each time this state is selected.
2078 Each keyword may also specify if a timestamp or a note should be
2079 recorded when entering or leaving the state, by adding additional
2080 characters in the parenthesis after the keyword. This looks like this:
2081 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2082 record only the time of the state change. With X and Y being either
2083 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2084 Y when leaving the state if and only if the *target* state does not
2085 define X. You may omit any of the fast-selection key or X or /Y,
2086 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2088 For backward compatibility, this variable may also be just a list
2089 of keywords - in this case the interpretation (sequence or type) will be
2090 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2092 :group
'org-keywords
2094 (repeat :tag
"Old syntax, just keywords"
2095 (string :tag
"Keyword"))
2096 (repeat :tag
"New syntax"
2099 :tag
"Interpretation"
2100 ;;Quick and dirty way to see
2101 ;;`org-todo-interpretations'. This takes the
2102 ;;place of item arguments
2110 org-todo-interpretation-widgets
))
2113 (string :tag
"Keyword"))))))
2115 (defvar org-todo-keywords-1 nil
2116 "All TODO and DONE keywords active in a buffer.")
2117 (make-variable-buffer-local 'org-todo-keywords-1
)
2118 (defvar org-todo-keywords-for-agenda nil
)
2119 (defvar org-done-keywords-for-agenda nil
)
2120 (defvar org-drawers-for-agenda nil
)
2121 (defvar org-todo-keyword-alist-for-agenda nil
)
2122 (defvar org-tag-alist-for-agenda nil
)
2123 (defvar org-agenda-contributing-files nil
)
2124 (defvar org-not-done-keywords nil
)
2125 (make-variable-buffer-local 'org-not-done-keywords
)
2126 (defvar org-done-keywords nil
)
2127 (make-variable-buffer-local 'org-done-keywords
)
2128 (defvar org-todo-heads nil
)
2129 (make-variable-buffer-local 'org-todo-heads
)
2130 (defvar org-todo-sets nil
)
2131 (make-variable-buffer-local 'org-todo-sets
)
2132 (defvar org-todo-log-states nil
)
2133 (make-variable-buffer-local 'org-todo-log-states
)
2134 (defvar org-todo-kwd-alist nil
)
2135 (make-variable-buffer-local 'org-todo-kwd-alist
)
2136 (defvar org-todo-key-alist nil
)
2137 (make-variable-buffer-local 'org-todo-key-alist
)
2138 (defvar org-todo-key-trigger nil
)
2139 (make-variable-buffer-local 'org-todo-key-trigger
)
2141 (defcustom org-todo-interpretation
'sequence
2142 "Controls how TODO keywords are interpreted.
2143 This variable is in principle obsolete and is only used for
2144 backward compatibility, if the interpretation of todo keywords is
2145 not given already in `org-todo-keywords'. See that variable for
2148 :group
'org-keywords
2149 :type
'(choice (const sequence
)
2152 (defcustom org-use-fast-todo-selection t
2153 "Non-nil means use the fast todo selection scheme with C-c C-t.
2154 This variable describes if and under what circumstances the cycling
2155 mechanism for TODO keywords will be replaced by a single-key, direct
2158 When nil, fast selection is never used.
2160 When the symbol `prefix', it will be used when `org-todo' is called with
2161 a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
2162 in an agenda buffer.
2164 When t, fast selection is used by default. In this case, the prefix
2165 argument forces cycling instead.
2167 In all cases, the special interface is only used if access keys have actually
2168 been assigned by the user, i.e. if keywords in the configuration are followed
2169 by a letter in parenthesis, like TODO(t)."
2172 (const :tag
"Never" nil
)
2173 (const :tag
"By default" t
)
2174 (const :tag
"Only with C-u C-c C-t" prefix
)))
2176 (defcustom org-provide-todo-statistics t
2177 "Non-nil means update todo statistics after insert and toggle.
2178 ALL-HEADLINES means update todo statistics by including headlines
2179 with no TODO keyword as well, counting them as not done.
2180 A list of TODO keywords means the same, but skip keywords that are
2183 When this is set, todo statistics is updated in the parent of the
2184 current entry each time a todo state is changed."
2187 (const :tag
"Yes, only for TODO entries" t
)
2188 (const :tag
"Yes, including all entries" 'all-headlines
)
2189 (repeat :tag
"Yes, for TODOs in this list"
2190 (string :tag
"TODO keyword"))
2191 (other :tag
"No TODO statistics" nil
)))
2193 (defcustom org-hierarchical-todo-statistics t
2194 "Non-nil means TODO statistics covers just direct children.
2195 When nil, all entries in the subtree are considered.
2196 This has only an effect if `org-provide-todo-statistics' is set.
2197 To set this to nil for only a single subtree, use a COOKIE_DATA
2198 property and include the word \"recursive\" into the value."
2202 (defcustom org-after-todo-state-change-hook nil
2203 "Hook which is run after the state of a TODO item was changed.
2204 The new state (a string with a TODO keyword, or nil) is available in the
2205 Lisp variable `state'."
2209 (defvar org-blocker-hook nil
2210 "Hook for functions that are allowed to block a state change.
2212 Each function gets as its single argument a property list, see
2213 `org-trigger-hook' for more information about this list.
2215 If any of the functions in this hook returns nil, the state change
2218 (defvar org-trigger-hook nil
2219 "Hook for functions that are triggered by a state change.
2221 Each function gets as its single argument a property list with at least
2222 the following elements:
2224 (:type type-of-change :position pos-at-entry-start
2225 :from old-state :to new-state)
2227 Depending on the type, more properties may be present.
2229 This mechanism is currently implemented for:
2233 :type todo-state-change
2234 :from previous state (keyword as a string), or nil, or a symbol
2235 'todo' or 'done', to indicate the general type of state.
2236 :to new state, like in :from")
2238 (defcustom org-enforce-todo-dependencies nil
2239 "Non-nil means undone TODO entries will block switching the parent to DONE.
2240 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2241 be blocked if any prior sibling is not yet done.
2242 Finally, if the parent is blocked because of ordered siblings of its own,
2243 the child will also be blocked.
2244 This variable needs to be set before org.el is loaded, and you need to
2245 restart Emacs after a change to make the change effective. The only way
2246 to change is while Emacs is running is through the customize interface."
2247 :set
(lambda (var val
)
2250 (add-hook 'org-blocker-hook
2251 'org-block-todo-from-children-or-siblings-or-parent
)
2252 (remove-hook 'org-blocker-hook
2253 'org-block-todo-from-children-or-siblings-or-parent
)))
2257 (defcustom org-enforce-todo-checkbox-dependencies nil
2258 "Non-nil means unchecked boxes will block switching the parent to DONE.
2259 When this is nil, checkboxes have no influence on switching TODO states.
2260 When non-nil, you first need to check off all check boxes before the TODO
2261 entry can be switched to DONE.
2262 This variable needs to be set before org.el is loaded, and you need to
2263 restart Emacs after a change to make the change effective. The only way
2264 to change is while Emacs is running is through the customize interface."
2265 :set
(lambda (var val
)
2268 (add-hook 'org-blocker-hook
2269 'org-block-todo-from-checkboxes
)
2270 (remove-hook 'org-blocker-hook
2271 'org-block-todo-from-checkboxes
)))
2275 (defcustom org-treat-insert-todo-heading-as-state-change nil
2276 "Non-nil means inserting a TODO heading is treated as state change.
2277 So when the command \\[org-insert-todo-heading] is used, state change
2278 logging will apply if appropriate. When nil, the new TODO item will
2279 be inserted directly, and no logging will take place."
2283 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2284 "Non-nil means switching TODO states with S-cursor counts as state change.
2285 This is the default behavior. However, setting this to nil allows a
2286 convenient way to select a TODO state and bypass any logging associated
2291 (defcustom org-todo-state-tags-triggers nil
2292 "Tag changes that should be triggered by TODO state changes.
2293 This is a list. Each entry is
2295 (state-change (tag . flag) .......)
2297 State-change can be a string with a state, and empty string to indicate the
2298 state that has no TODO keyword, or it can be one of the symbols `todo'
2299 or `done', meaning any not-done or done state, respectively."
2303 (cons (choice :tag
"When changing to"
2304 (const :tag
"Not-done state" todo
)
2305 (const :tag
"Done state" done
)
2306 (string :tag
"State"))
2308 (cons :tag
"Tag action"
2310 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2312 (defcustom org-log-done nil
2313 "Information to record when a task moves to the DONE state.
2315 Possible values are:
2317 nil Don't add anything, just change the keyword
2318 time Add a time stamp to the task
2319 note Prompt for a note and add it with template `org-log-note-headings'
2321 This option can also be set with on a per-file-basis with
2323 #+STARTUP: nologdone
2325 #+STARTUP: lognotedone
2327 You can have local logging settings for a subtree by setting the LOGGING
2328 property to one or more of these keywords."
2330 :group
'org-progress
2332 (const :tag
"No logging" nil
)
2333 (const :tag
"Record CLOSED timestamp" time
)
2334 (const :tag
"Record CLOSED timestamp with note." note
)))
2336 ;; Normalize old uses of org-log-done.
2338 ((eq org-log-done t
) (setq org-log-done
'time
))
2339 ((and (listp org-log-done
) (memq 'done org-log-done
))
2340 (setq org-log-done
'note
)))
2342 (defcustom org-log-reschedule nil
2343 "Information to record when the scheduling date of a tasks is modified.
2345 Possible values are:
2347 nil Don't add anything, just change the date
2348 time Add a time stamp to the task
2349 note Prompt for a note and add it with template `org-log-note-headings'
2351 This option can also be set with on a per-file-basis with
2353 #+STARTUP: nologreschedule
2354 #+STARTUP: logreschedule
2355 #+STARTUP: lognotereschedule"
2357 :group
'org-progress
2359 (const :tag
"No logging" nil
)
2360 (const :tag
"Record timestamp" time
)
2361 (const :tag
"Record timestamp with note." note
)))
2363 (defcustom org-log-redeadline nil
2364 "Information to record when the deadline date of a tasks is modified.
2366 Possible values are:
2368 nil Don't add anything, just change the date
2369 time Add a time stamp to the task
2370 note Prompt for a note and add it with template `org-log-note-headings'
2372 This option can also be set with on a per-file-basis with
2374 #+STARTUP: nologredeadline
2375 #+STARTUP: logredeadline
2376 #+STARTUP: lognoteredeadline
2378 You can have local logging settings for a subtree by setting the LOGGING
2379 property to one or more of these keywords."
2381 :group
'org-progress
2383 (const :tag
"No logging" nil
)
2384 (const :tag
"Record timestamp" time
)
2385 (const :tag
"Record timestamp with note." note
)))
2387 (defcustom org-log-note-clock-out nil
2388 "Non-nil means record a note when clocking out of an item.
2389 This can also be configured on a per-file basis by adding one of
2390 the following lines anywhere in the buffer:
2392 #+STARTUP: lognoteclock-out
2393 #+STARTUP: nolognoteclock-out"
2395 :group
'org-progress
2398 (defcustom org-log-done-with-time t
2399 "Non-nil means the CLOSED time stamp will contain date and time.
2400 When nil, only the date will be recorded."
2401 :group
'org-progress
2404 (defcustom org-log-note-headings
2405 '((done .
"CLOSING NOTE %t")
2406 (state .
"State %-12s from %-12S %t")
2407 (note .
"Note taken on %t")
2408 (reschedule .
"Rescheduled from %S on %t")
2409 (delschedule .
"Not scheduled, was %S on %t")
2410 (redeadline .
"New deadline from %S on %t")
2411 (deldeadline .
"Removed deadline, was %S on %t")
2412 (refile .
"Refiled on %t")
2414 "Headings for notes added to entries.
2415 The value is an alist, with the car being a symbol indicating the note
2416 context, and the cdr is the heading to be used. The heading may also be the
2418 %t in the heading will be replaced by a time stamp.
2419 %T will be an active time stamp instead the default inactive one
2420 %d will be replaced by a short-format time stamp.
2421 %D will be replaced by an active short-format time stamp.
2422 %s will be replaced by the new TODO state, in double quotes.
2423 %S will be replaced by the old TODO state, in double quotes.
2424 %u will be replaced by the user name.
2425 %U will be replaced by the full user name.
2427 In fact, it is not a good idea to change the `state' entry, because
2428 agenda log mode depends on the format of these entries."
2430 :group
'org-progress
2431 :type
'(list :greedy t
2432 (cons (const :tag
"Heading when closing an item" done
) string
)
2434 "Heading when changing todo state (todo sequence only)"
2436 (cons (const :tag
"Heading when just taking a note" note
) string
)
2437 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2438 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2439 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2440 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2441 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2442 (cons (const :tag
"Heading when refiling" refile
) string
)))
2444 (unless (assq 'note org-log-note-headings
)
2445 (push '(note .
"%t") org-log-note-headings
))
2447 (defcustom org-log-into-drawer nil
2448 "Non-nil means insert state change notes and time stamps into a drawer.
2449 When nil, state changes notes will be inserted after the headline and
2450 any scheduling and clock lines, but not inside a drawer.
2452 The value of this variable should be the name of the drawer to use.
2453 LOGBOOK is proposed as the default drawer for this purpose, you can
2454 also set this to a string to define the drawer of your choice.
2456 A value of t is also allowed, representing \"LOGBOOK\".
2458 If this variable is set, `org-log-state-notes-insert-after-drawers'
2461 You can set the property LOG_INTO_DRAWER to overrule this setting for
2464 :group
'org-progress
2466 (const :tag
"Not into a drawer" nil
)
2467 (const :tag
"LOGBOOK" t
)
2468 (string :tag
"Other")))
2470 (if (fboundp 'defvaralias
)
2471 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2473 (defun org-log-into-drawer ()
2474 "Return the value of `org-log-into-drawer', but let properties overrule.
2475 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2476 used instead of the default value."
2477 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
)))
2479 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2480 ((equal p
"t") "LOGBOOK")
2483 (defcustom org-log-state-notes-insert-after-drawers nil
2484 "Non-nil means insert state change notes after any drawers in entry.
2485 Only the drawers that *immediately* follow the headline and the
2486 deadline/scheduled line are skipped.
2487 When nil, insert notes right after the heading and perhaps the line
2488 with deadline/scheduling if present.
2490 This variable will have no effect if `org-log-into-drawer' is
2493 :group
'org-progress
2496 (defcustom org-log-states-order-reversed t
2497 "Non-nil means the latest state note will be directly after heading.
2498 When nil, the state change notes will be ordered according to time."
2500 :group
'org-progress
2503 (defcustom org-todo-repeat-to-state nil
2504 "The TODO state to which a repeater should return the repeating task.
2505 By default this is the first task in a TODO sequence, or the previous state
2506 in a TODO_TYP set. But you can specify another task here.
2507 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2509 :type
'(choice (const :tag
"Head of sequence" nil
)
2510 (string :tag
"Specific state")))
2512 (defcustom org-log-repeat
'time
2513 "Non-nil means record moving through the DONE state when triggering repeat.
2514 An auto-repeating task is immediately switched back to TODO when
2515 marked DONE. If you are not logging state changes (by adding \"@\"
2516 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2517 record a closing note, there will be no record of the task moving
2518 through DONE. This variable forces taking a note anyway.
2520 nil Don't force a record
2521 time Record a time stamp
2524 This option can also be set with on a per-file-basis with
2526 #+STARTUP: logrepeat
2527 #+STARTUP: lognoterepeat
2528 #+STARTUP: nologrepeat
2530 You can have local logging settings for a subtree by setting the LOGGING
2531 property to one or more of these keywords."
2533 :group
'org-progress
2535 (const :tag
"Don't force a record" nil
)
2536 (const :tag
"Force recording the DONE state" time
)
2537 (const :tag
"Force recording a note with the DONE state" note
)))
2540 (defgroup org-priorities nil
2541 "Priorities in Org-mode."
2542 :tag
"Org Priorities"
2545 (defcustom org-enable-priority-commands t
2546 "Non-nil means priority commands are active.
2547 When nil, these commands will be disabled, so that you never accidentally
2549 :group
'org-priorities
2552 (defcustom org-highest-priority ?A
2553 "The highest priority of TODO items. A character like ?A, ?B etc.
2554 Must have a smaller ASCII number than `org-lowest-priority'."
2555 :group
'org-priorities
2558 (defcustom org-lowest-priority ?C
2559 "The lowest priority of TODO items. A character like ?A, ?B etc.
2560 Must have a larger ASCII number than `org-highest-priority'."
2561 :group
'org-priorities
2564 (defcustom org-default-priority ?B
2565 "The default priority of TODO items.
2566 This is the priority an item gets if no explicit priority is given.
2567 When starting to cycle on an empty priority the first step in the cycle
2568 depends on `org-priority-start-cycle-with-default'. The resulting first
2569 step priority must not exceed the range from `org-highest-priority' to
2570 `org-lowest-priority' which means that `org-default-priority' has to be
2571 in this range exclusive or inclusive the range boundaries. Else the
2572 first step refuses to set the default and the second will fall back
2573 to (depending on the command used) the highest or lowest priority."
2574 :group
'org-priorities
2577 (defcustom org-priority-start-cycle-with-default t
2578 "Non-nil means start with default priority when starting to cycle.
2579 When this is nil, the first step in the cycle will be (depending on the
2580 command used) one higher or lower than the default priority.
2581 See also `org-default-priority'."
2582 :group
'org-priorities
2585 (defcustom org-get-priority-function nil
2586 "Function to extract the priority from a string.
2587 The string is normally the headline. If this is nil Org computes the
2588 priority from the priority cookie like [#A] in the headline. It returns
2589 an integer, increasing by 1000 for each priority level.
2590 The user can set a different function here, which should take a string
2591 as an argument and return the numeric priority."
2592 :group
'org-priorities
2595 (defgroup org-time nil
2596 "Options concerning time stamps and deadlines in Org-mode."
2600 (defcustom org-insert-labeled-timestamps-at-point nil
2601 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2602 When nil, these labeled time stamps are forces into the second line of an
2603 entry, just after the headline. When scheduling from the global TODO list,
2604 the time stamp will always be forced into the second line."
2608 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2609 "Formats for `format-time-string' which are used for time stamps.
2610 It is not recommended to change this constant.")
2612 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2613 "Number of minutes to round time stamps to.
2614 These are two values, the first applies when first creating a time stamp.
2615 The second applies when changing it with the commands `S-up' and `S-down'.
2616 When changing the time stamp, this means that it will change in steps
2617 of N minutes, as given by the second value.
2619 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2620 numbers should be factors of 60, so for example 5, 10, 15.
2622 When this is larger than 1, you can still force an exact time stamp by using
2623 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2624 and by using a prefix arg to `S-up/down' to specify the exact number
2625 of minutes to shift."
2627 :get
#'(lambda (var) ; Make sure both elements are there
2628 (if (integerp (default-value var
))
2629 (list (default-value var
) 5)
2630 (default-value var
)))
2632 (integer :tag
"when inserting times")
2633 (integer :tag
"when modifying times")))
2635 ;; Normalize old customizations of this variable.
2636 (when (integerp org-time-stamp-rounding-minutes
)
2637 (setq org-time-stamp-rounding-minutes
2638 (list org-time-stamp-rounding-minutes
2639 org-time-stamp-rounding-minutes
)))
2641 (defcustom org-display-custom-times nil
2642 "Non-nil means overlay custom formats over all time stamps.
2643 The formats are defined through the variable `org-time-stamp-custom-formats'.
2644 To turn this on on a per-file basis, insert anywhere in the file:
2645 #+STARTUP: customtime"
2649 (make-variable-buffer-local 'org-display-custom-times
)
2651 (defcustom org-time-stamp-custom-formats
2652 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2653 "Custom formats for time stamps. See `format-time-string' for the syntax.
2654 These are overlaid over the default ISO format if the variable
2655 `org-display-custom-times' is set. Time like %H:%M should be at the
2656 end of the second format. The custom formats are also honored by export
2657 commands, if custom time display is turned on at the time of export."
2661 (defun org-time-stamp-format (&optional long inactive
)
2662 "Get the right format for a time string."
2663 (let ((f (if long
(cdr org-time-stamp-formats
)
2664 (car org-time-stamp-formats
))))
2666 (concat "[" (substring f
1 -
1) "]")
2669 (defcustom org-time-clocksum-format
"%d:%02d"
2670 "The format string used when creating CLOCKSUM lines.
2671 This is also used when org-mode generates a time duration."
2675 (defcustom org-time-clocksum-use-fractional nil
2676 "If non-nil, \\[org-clock-display] uses fractional times.
2677 org-mode generates a time duration."
2681 (defcustom org-time-clocksum-fractional-format
"%.2f"
2682 "The format string used when creating CLOCKSUM lines, or when
2683 org-mode generates a time duration."
2687 (defcustom org-deadline-warning-days
14
2688 "No. of days before expiration during which a deadline becomes active.
2689 This variable governs the display in sparse trees and in the agenda.
2690 When 0 or negative, it means use this number (the absolute value of it)
2691 even if a deadline has a different individual lead time specified.
2693 Custom commands can set this variable in the options section."
2695 :group
'org-agenda-daily
/weekly
2698 (defcustom org-read-date-prefer-future t
2699 "Non-nil means assume future for incomplete date input from user.
2700 This affects the following situations:
2701 1. The user gives a month but not a year.
2702 For example, if it is April and you enter \"feb 2\", this will be read
2703 as Feb 2, *next* year. \"May 5\", however, will be this year.
2704 2. The user gives a day, but no month.
2705 For example, if today is the 15th, and you enter \"3\", Org-mode will
2706 read this as the third of *next* month. However, if you enter \"17\",
2707 it will be considered as *this* month.
2709 If you set this variable to the symbol `time', then also the following
2712 3. If the user gives a time, but no day. If the time is before now,
2713 to will be interpreted as tomorrow.
2715 Currently none of this works for ISO week specifications.
2717 When this option is nil, the current day, month and year will always be
2720 See also `org-agenda-jump-prefer-future'."
2723 (const :tag
"Never" nil
)
2724 (const :tag
"Check month and day" t
)
2725 (const :tag
"Check month, day, and time" time
)))
2727 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2728 "Should the agenda jump command prefer the future for incomplete dates?
2729 The default is to do the same as configured in `org-read-date-prefer-future'.
2730 But you can also set a deviating value here.
2731 This may t or nil, or the symbol `org-read-date-prefer-future'."
2735 (const :tag
"Use org-read-date-prefer-future"
2736 org-read-date-prefer-future
)
2737 (const :tag
"Never" nil
)
2738 (const :tag
"Always" t
)))
2740 (defcustom org-read-date-force-compatible-dates t
2741 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2743 Depending on the system Emacs is running on, certain dates cannot
2744 be represented with the type used internally to represent time.
2745 Dates between 1970-1-1 and 2038-1-1 can always be represented
2746 correctly. Some systems allow for earlier dates, some for later,
2747 some for both. One way to find out it to insert any date into an
2748 Org buffer, putting the cursor on the year and hitting S-up and
2749 S-down to test the range.
2751 When this variable is set to t, the date/time prompt will not let
2752 you specify dates outside the 1970-2037 range, so it is certain that
2753 these dates will work in whatever version of Emacs you are
2754 running, and also that you can move a file from one Emacs implementation
2755 to another. WHenever Org is forcing the year for you, it will display
2758 When this variable is nil, Org will check if the date is
2759 representable in the specific Emacs implementation you are using.
2760 If not, it will force a year, usually the current year, and beep
2761 to remind you. Currently this setting is not recommended because
2762 the likelihood that you will open your Org files in an Emacs that
2763 has limited date range is not negligible.
2765 A workaround for this problem is to use diary sexp dates for time
2766 stamps outside of this range."
2770 (defcustom org-read-date-display-live t
2771 "Non-nil means display current interpretation of date prompt live.
2772 This display will be in an overlay, in the minibuffer."
2776 (defcustom org-read-date-popup-calendar t
2777 "Non-nil means pop up a calendar when prompting for a date.
2778 In the calendar, the date can be selected with mouse-1. However, the
2779 minibuffer will also be active, and you can simply enter the date as well.
2780 When nil, only the minibuffer will be available."
2783 (if (fboundp 'defvaralias
)
2784 (defvaralias 'org-popup-calendar-for-date-prompt
2785 'org-read-date-popup-calendar
))
2787 (defcustom org-read-date-minibuffer-setup-hook nil
2788 "Hook to be used to set up keys for the date/time interface.
2789 Add key definitions to `minibuffer-local-map', which will be a temporary
2794 (defcustom org-extend-today-until
0
2795 "The hour when your day really ends. Must be an integer.
2796 This has influence for the following applications:
2797 - When switching the agenda to \"today\". It it is still earlier than
2798 the time given here, the day recognized as TODAY is actually yesterday.
2799 - When a date is read from the user and it is still before the time given
2800 here, the current date and time will be assumed to be yesterday, 23:59.
2801 Also, timestamps inserted in remember templates follow this rule.
2803 IMPORTANT: This is a feature whose implementation is and likely will
2804 remain incomplete. Really, it is only here because past midnight seems to
2805 be the favorite working time of John Wiegley :-)"
2809 (defcustom org-use-effective-time nil
2810 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2811 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2812 \"effective time\" of any timestamps between midnight and 8am will be
2813 23:59 of the previous day."
2817 (defcustom org-edit-timestamp-down-means-later nil
2818 "Non-nil means S-down will increase the time in a time stamp.
2819 When nil, S-up will increase."
2823 (defcustom org-calendar-follow-timestamp-change t
2824 "Non-nil means make the calendar window follow timestamp changes.
2825 When a timestamp is modified and the calendar window is visible, it will be
2826 moved to the new date."
2830 (defgroup org-tags nil
2831 "Options concerning tags in Org-mode."
2835 (defcustom org-tag-alist nil
2836 "List of tags allowed in Org-mode files.
2837 When this list is nil, Org-mode will base TAG input on what is already in the
2839 The value of this variable is an alist, the car of each entry must be a
2840 keyword as a string, the cdr may be a character that is used to select
2841 that tag through the fast-tag-selection interface.
2842 See the manual for details."
2846 (cons (string :tag
"Tag name")
2847 (character :tag
"Access char"))
2848 (list :tag
"Start radio group"
2850 (option (string :tag
"Group description")))
2851 (list :tag
"End radio group"
2853 (option (string :tag
"Group description")))
2854 (const :tag
"New line" (:newline
)))))
2856 (defcustom org-tag-persistent-alist nil
2857 "List of tags that will always appear in all Org-mode files.
2858 This is in addition to any in buffer settings or customizations
2860 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2861 The value of this variable is an alist, the car of each entry must be a
2862 keyword as a string, the cdr may be a character that is used to select
2863 that tag through the fast-tag-selection interface.
2864 See the manual for details.
2865 To disable these tags on a per-file basis, insert anywhere in the file:
2870 (cons (string :tag
"Tag name")
2871 (character :tag
"Access char"))
2872 (const :tag
"Start radio group" (:startgroup
))
2873 (const :tag
"End radio group" (:endgroup
))
2874 (const :tag
"New line" (:newline
)))))
2876 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2877 "If non-nil, always offer completion for all tags of all agenda files.
2878 Instead of customizing this variable directly, you might want to
2879 set it locally for capture buffers, because there no list of
2880 tags in that file can be created dynamically (there are none).
2882 (add-hook 'org-capture-mode-hook
2884 (set (make-local-variable
2885 'org-complete-tags-always-offer-all-agenda-tags)
2890 (defvar org-file-tags nil
2891 "List of tags that can be inherited by all entries in the file.
2892 The tags will be inherited if the variable `org-use-tag-inheritance'
2893 says they should be.
2894 This variable is populated from #+FILETAGS lines.")
2896 (defcustom org-use-fast-tag-selection
'auto
2897 "Non-nil means use fast tag selection scheme.
2898 This is a special interface to select and deselect tags with single keys.
2899 When nil, fast selection is never used.
2900 When the symbol `auto', fast selection is used if and only if selection
2901 characters for tags have been configured, either through the variable
2902 `org-tag-alist' or through a #+TAGS line in the buffer.
2903 When t, fast selection is always used and selection keys are assigned
2904 automatically if necessary."
2907 (const :tag
"Always" t
)
2908 (const :tag
"Never" nil
)
2909 (const :tag
"When selection characters are configured" 'auto
)))
2911 (defcustom org-fast-tag-selection-single-key nil
2912 "Non-nil means fast tag selection exits after first change.
2913 When nil, you have to press RET to exit it.
2914 During fast tag selection, you can toggle this flag with `C-c'.
2915 This variable can also have the value `expert'. In this case, the window
2916 displaying the tags menu is not even shown, until you press C-c again."
2919 (const :tag
"No" nil
)
2920 (const :tag
"Yes" t
)
2921 (const :tag
"Expert" expert
)))
2923 (defvar org-fast-tag-selection-include-todo nil
2924 "Non-nil means fast tags selection interface will also offer TODO states.
2925 This is an undocumented feature, you should not rely on it.")
2927 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2928 "The column to which tags should be indented in a headline.
2929 If this number is positive, it specifies the column. If it is negative,
2930 it means that the tags should be flushright to that column. For example,
2931 -80 works well for a normal 80 character screen.
2932 When 0, place tags directly after headline text, with only one space in
2937 (defcustom org-auto-align-tags t
2938 "Non-nil keeps tags aligned when modifying headlines.
2939 Some operations (i.e. demoting) change the length of a headline and
2940 therefore shift the tags around. With this option turned on, after
2941 each such operation the tags are again aligned to `org-tags-column'."
2945 (defcustom org-use-tag-inheritance t
2946 "Non-nil means tags in levels apply also for sublevels.
2947 When nil, only the tags directly given in a specific line apply there.
2948 This may also be a list of tags that should be inherited, or a regexp that
2949 matches tags that should be inherited. Additional control is possible
2950 with the variable `org-tags-exclude-from-inheritance' which gives an
2951 explicit list of tags to be excluded from inheritance., even if the value of
2952 `org-use-tag-inheritance' would select it for inheritance.
2954 If this option is t, a match early-on in a tree can lead to a large
2955 number of matches in the subtree when constructing the agenda or creating
2956 a sparse tree. If you only want to see the first match in a tree during
2957 a search, check out the variable `org-tags-match-list-sublevels'."
2960 (const :tag
"Not" nil
)
2961 (const :tag
"Always" t
)
2962 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2963 (regexp :tag
"Tags matched by regexp")))
2965 (defcustom org-tags-exclude-from-inheritance nil
2966 "List of tags that should never be inherited.
2967 This is a way to exclude a few tags from inheritance. For way to do
2968 the opposite, to actively allow inheritance for selected tags,
2969 see the variable `org-use-tag-inheritance'."
2971 :type
'(repeat (string :tag
"Tag")))
2973 (defun org-tag-inherit-p (tag)
2974 "Check if TAG is one that should be inherited."
2976 ((member tag org-tags-exclude-from-inheritance
) nil
)
2977 ((eq org-use-tag-inheritance t
) t
)
2978 ((not org-use-tag-inheritance
) nil
)
2979 ((stringp org-use-tag-inheritance
)
2980 (string-match org-use-tag-inheritance tag
))
2981 ((listp org-use-tag-inheritance
)
2982 (member tag org-use-tag-inheritance
))
2983 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
2985 (defcustom org-tags-match-list-sublevels t
2986 "Non-nil means list also sublevels of headlines matching a search.
2987 This variable applies to tags/property searches, and also to stuck
2988 projects because this search is based on a tags match as well.
2990 When set to the symbol `indented', sublevels are indented with
2993 Because of tag inheritance (see variable `org-use-tag-inheritance'),
2994 the sublevels of a headline matching a tag search often also match
2995 the same search. Listing all of them can create very long lists.
2996 Setting this variable to nil causes subtrees of a match to be skipped.
2998 This variable is semi-obsolete and probably should always be true. It
2999 is better to limit inheritance to certain tags using the variables
3000 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3003 (const :tag
"No, don't list them" nil
)
3004 (const :tag
"Yes, do list them" t
)
3005 (const :tag
"List them, indented with leading dots" indented
)))
3007 (defcustom org-tags-sort-function nil
3008 "When set, tags are sorted using this function as a comparator."
3011 (const :tag
"No sorting" nil
)
3012 (const :tag
"Alphabetical" string
<)
3013 (const :tag
"Reverse alphabetical" string
>)
3014 (function :tag
"Custom function" nil
)))
3016 (defvar org-tags-history nil
3017 "History of minibuffer reads for tags.")
3018 (defvar org-last-tags-completion-table nil
3019 "The last used completion table for tags.")
3020 (defvar org-after-tags-change-hook nil
3021 "Hook that is run after the tags in a line have changed.")
3023 (defgroup org-properties nil
3024 "Options concerning properties in Org-mode."
3025 :tag
"Org Properties"
3028 (defcustom org-property-format
"%-10s %s"
3029 "How property key/value pairs should be formatted by `indent-line'.
3030 When `indent-line' hits a property definition, it will format the line
3031 according to this format, mainly to make sure that the values are
3032 lined-up with respect to each other."
3033 :group
'org-properties
3036 (defcustom org-properties-postprocess-alist nil
3037 "Alist of properties and functions to adjust inserted values.
3038 Elements of this alist must be of the form
3040 ([string] [function])
3042 where [string] must be a property name and [function] must be a
3043 lambda expression: this lambda expression must take one argument,
3044 the value to adjust, and return the new value as a string.
3046 For example, this element will allow the property \"Remaining\"
3047 to be updated wrt the relation between the \"Effort\" property
3048 and the clock summary:
3050 ((\"Remaining\" (lambda(value)
3051 (let ((clocksum (org-clock-sum-current-item))
3052 (effort (org-duration-string-to-minutes
3053 (org-entry-get (point) \"Effort\"))))
3054 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3055 :group
'org-properties
3058 (defcustom org-use-property-inheritance nil
3059 "Non-nil means properties apply also for sublevels.
3061 This setting is chiefly used during property searches. Turning it on can
3062 cause significant overhead when doing a search, which is why it is not
3065 When nil, only the properties directly given in the current entry count.
3066 When t, every property is inherited. The value may also be a list of
3067 properties that should have inheritance, or a regular expression matching
3068 properties that should be inherited.
3070 However, note that some special properties use inheritance under special
3071 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3072 and the properties ending in \"_ALL\" when they are used as descriptor
3073 for valid values of a property.
3075 Note for programmers:
3076 When querying an entry with `org-entry-get', you can control if inheritance
3077 should be used. By default, `org-entry-get' looks only at the local
3078 properties. You can request inheritance by setting the inherit argument
3079 to t (to force inheritance) or to `selective' (to respect the setting
3081 :group
'org-properties
3083 (const :tag
"Not" nil
)
3084 (const :tag
"Always" t
)
3085 (repeat :tag
"Specific properties" (string :tag
"Property"))
3086 (regexp :tag
"Properties matched by regexp")))
3088 (defun org-property-inherit-p (property)
3089 "Check if PROPERTY is one that should be inherited."
3091 ((eq org-use-property-inheritance t
) t
)
3092 ((not org-use-property-inheritance
) nil
)
3093 ((stringp org-use-property-inheritance
)
3094 (string-match org-use-property-inheritance property
))
3095 ((listp org-use-property-inheritance
)
3096 (member property org-use-property-inheritance
))
3097 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3099 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3100 "The default column format, if no other format has been defined.
3101 This variable can be set on the per-file basis by inserting a line
3103 #+COLUMNS: %25ITEM ....."
3104 :group
'org-properties
3107 (defcustom org-columns-ellipses
".."
3108 "The ellipses to be used when a field in column view is truncated.
3109 When this is the empty string, as many characters as possible are shown,
3110 but then there will be no visual indication that the field has been truncated.
3111 When this is a string of length N, the last N characters of a truncated
3112 field are replaced by this string. If the column is narrower than the
3113 ellipses string, only part of the ellipses string will be shown."
3114 :group
'org-properties
3117 (defcustom org-columns-modify-value-for-display-function nil
3118 "Function that modifies values for display in column view.
3119 For example, it can be used to cut out a certain part from a time stamp.
3120 The function must take 2 arguments:
3122 column-title The title of the column (*not* the property name)
3123 value The value that should be modified.
3125 The function should return the value that should be displayed,
3126 or nil if the normal value should be used."
3127 :group
'org-properties
3130 (defcustom org-effort-property
"Effort"
3131 "The property that is being used to keep track of effort estimates.
3132 Effort estimates given in this property need to have the format H:MM."
3133 :group
'org-properties
3134 :group
'org-progress
3135 :type
'(string :tag
"Property"))
3137 (defconst org-global-properties-fixed
3138 '(("VISIBILITY_ALL" .
"folded children content all")
3139 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3140 "List of property/value pairs that can be inherited by any entry.
3142 These are fixed values, for the preset properties. The user variable
3143 that can be used to add to this list is `org-global-properties'.
3145 The entries in this list are cons cells where the car is a property
3146 name and cdr is a string with the value. If the value represents
3147 multiple items like an \"_ALL\" property, separate the items by
3150 (defcustom org-global-properties nil
3151 "List of property/value pairs that can be inherited by any entry.
3153 This list will be combined with the constant `org-global-properties-fixed'.
3155 The entries in this list are cons cells where the car is a property
3156 name and cdr is a string with the value.
3158 You can set buffer-local values for the same purpose in the variable
3159 `org-file-properties' this by adding lines like
3161 #+PROPERTY: NAME VALUE"
3162 :group
'org-properties
3164 (cons (string :tag
"Property")
3165 (string :tag
"Value"))))
3167 (defvar org-file-properties nil
3168 "List of property/value pairs that can be inherited by any entry.
3169 Valid for the current buffer.
3170 This variable is populated from #+PROPERTY lines.")
3171 (make-variable-buffer-local 'org-file-properties
)
3173 (defgroup org-agenda nil
3174 "Options concerning agenda views in Org-mode."
3178 (defvar org-category nil
3179 "Variable used by org files to set a category for agenda display.
3180 Such files should use a file variable to set it, for example
3182 # -*- mode: org; org-category: \"ELisp\"
3184 or contain a special line
3188 If the file does not specify a category, then file's base name
3190 (make-variable-buffer-local 'org-category
)
3191 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3193 (defcustom org-agenda-files nil
3194 "The files to be used for agenda display.
3195 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3196 \\[org-remove-file]. You can also use customize to edit the list.
3198 If an entry is a directory, all files in that directory that are matched by
3199 `org-agenda-file-regexp' will be part of the file list.
3201 If the value of the variable is not a list but a single file name, then
3202 the list of agenda files is actually stored and maintained in that file, one
3203 agenda file per line. In this file paths can be given relative to
3204 `org-directory'. Tilde expansion and environment variable substitution
3208 (repeat :tag
"List of files and directories" file
)
3209 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3211 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3212 "Regular expression to match files for `org-agenda-files'.
3213 If any element in the list in that variable contains a directory instead
3214 of a normal file, all files in that directory that are matched by this
3215 regular expression will be included."
3219 (defcustom org-agenda-text-search-extra-files nil
3220 "List of extra files to be searched by text search commands.
3221 These files will be search in addition to the agenda files by the
3222 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3223 Note that these files will only be searched for text search commands,
3224 not for the other agenda views like todo lists, tag searches or the weekly
3225 agenda. This variable is intended to list notes and possibly archive files
3226 that should also be searched by these two commands.
3227 In fact, if the first element in the list is the symbol `agenda-archives',
3228 than all archive files of all agenda files will be added to the search
3231 :type
'(set :greedy t
3232 (const :tag
"Agenda Archives" agenda-archives
)
3233 (repeat :inline t
(file))))
3235 (if (fboundp 'defvaralias
)
3236 (defvaralias 'org-agenda-multi-occur-extra-files
3237 'org-agenda-text-search-extra-files
))
3239 (defcustom org-agenda-skip-unavailable-files nil
3240 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3241 A nil value means to remove them, after a query, from the list."
3245 (defcustom org-calendar-to-agenda-key
[?c
]
3246 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3247 The command `org-calendar-goto-agenda' will be bound to this key. The
3248 default is the character `c' because then `c' can be used to switch back and
3249 forth between agenda and calendar."
3253 (defcustom org-calendar-agenda-action-key
[?k
]
3254 "The key to be installed in `calendar-mode-map' for agenda-action.
3255 The command `org-agenda-action' will be bound to this key. The
3256 default is the character `k' because we use the same key in the agenda."
3260 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3261 "The key to be installed in `calendar-mode-map' for adding diary entries.
3262 This option is irrelevant until `org-agenda-diary-file' has been configured
3263 to point to an Org-mode file. When that is the case, the command
3264 `org-agenda-diary-entry' will be bound to the key given here, by default
3265 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3266 if you want to continue doing this, you need to change this to a different
3271 (defcustom org-agenda-diary-file
'diary-file
3272 "File to which to add new entries with the `i' key in agenda and calendar.
3273 When this is the symbol `diary-file', the functionality in the Emacs
3274 calendar will be used to add entries to the `diary-file'. But when this
3275 points to a file, `org-agenda-diary-entry' will be used instead."
3278 (const :tag
"The standard Emacs diary file" diary-file
)
3279 (file :tag
"Special Org file diary entries")))
3281 (eval-after-load "calendar"
3283 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3284 'org-calendar-goto-agenda
)
3285 (org-defkey calendar-mode-map org-calendar-agenda-action-key
3287 (add-hook 'calendar-mode-hook
3289 (unless (eq org-agenda-diary-file
'diary-file
)
3290 (define-key calendar-mode-map
3291 org-calendar-insert-diary-entry-key
3292 'org-agenda-diary-entry
))))))
3294 (defgroup org-latex nil
3295 "Options for embedding LaTeX code into Org-mode."
3299 (defcustom org-format-latex-options
3300 '(:foreground default
:background default
:scale
1.0
3301 :html-foreground
"Black" :html-background
"Transparent"
3302 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3303 "Options for creating images from LaTeX fragments.
3304 This is a property list with the following properties:
3305 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3306 `default' means use the foreground of the default face.
3307 :background the background color, or \"Transparent\".
3308 `default' means use the background of the default face.
3309 :scale a scaling factor for the size of the images, to get more pixels
3310 :html-foreground, :html-background, :html-scale
3311 the same numbers for HTML export.
3312 :matchers a list indicating which matchers should be used to
3313 find LaTeX fragments. Valid members of this list are:
3314 \"begin\" find environments
3315 \"$1\" find single characters surrounded by $.$
3316 \"$\" find math expressions surrounded by $...$
3317 \"$$\" find math expressions surrounded by $$....$$
3318 \"\\(\" find math expressions surrounded by \\(...\\)
3319 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3323 (defcustom org-format-latex-signal-error t
3324 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3325 When nil, just push out a message."
3328 (defcustom org-latex-to-mathml-jar-file nil
3329 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3330 Use this to specify additional executable file say a jar file.
3332 When using MathToWeb as the converter, specify the full-path to
3333 your mathtoweb.jar file."
3336 (const :tag
"None" nil
)
3337 (file :tag
"JAR file" :must-match t
)))
3339 (defcustom org-latex-to-mathml-convert-command nil
3340 "Command to convert LaTeX fragments to MathML.
3341 Replace format-specifiers in the command as noted below and use
3342 `shell-command' to convert LaTeX to MathML.
3343 %j: Executable file in fully expanded form as specified by
3344 `org-latex-to-mathml-jar-file'.
3345 %I: Input LaTeX file in fully expanded form
3346 %o: Output MathML file
3347 This command is used by `org-create-math-formula'.
3349 When using MathToWeb as the converter, set this to
3350 \"java -jar %j -unicode -force -df %o %I\"."
3353 (const :tag
"None" nil
)
3354 (string :tag
"\nShell command")))
3356 (defun org-format-latex-mathml-available-p ()
3357 "Return t if `org-latex-to-mathml-convert-command' is usable."
3359 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3360 org-latex-to-mathml-convert-command
)
3361 (let ((executable (car (split-string
3362 org-latex-to-mathml-convert-command
))))
3363 (when (executable-find executable
)
3365 "%j" org-latex-to-mathml-convert-command
)
3366 (file-readable-p org-latex-to-mathml-jar-file
)
3369 (defcustom org-format-latex-header
"\\documentclass{article}
3370 \\usepackage[usenames]{color}
3371 \\usepackage{amsmath}
3372 \\usepackage[mathscr]{eucal}
3373 \\pagestyle{empty} % do not remove
3376 % The settings below are copied from fullpage.sty
3377 \\setlength{\\textwidth}{\\paperwidth}
3378 \\addtolength{\\textwidth}{-3cm}
3379 \\setlength{\\oddsidemargin}{1.5cm}
3380 \\addtolength{\\oddsidemargin}{-2.54cm}
3381 \\setlength{\\evensidemargin}{\\oddsidemargin}
3382 \\setlength{\\textheight}{\\paperheight}
3383 \\addtolength{\\textheight}{-\\headheight}
3384 \\addtolength{\\textheight}{-\\headsep}
3385 \\addtolength{\\textheight}{-\\footskip}
3386 \\addtolength{\\textheight}{-3cm}
3387 \\setlength{\\topmargin}{1.5cm}
3388 \\addtolength{\\topmargin}{-2.54cm}"
3389 "The document header used for processing LaTeX fragments.
3390 It is imperative that this header make sure that no page number
3391 appears on the page. The package defined in the variables
3392 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3393 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3398 (defvar org-format-latex-header-extra nil
)
3400 (defun org-set-packages-alist (var val
)
3401 "Set the packages alist and make sure it has 3 elements per entry."
3402 (set var
(mapcar (lambda (x)
3403 (if (and (consp x
) (= (length x
) 2))
3404 (list (car x
) (nth 1 x
) t
)
3408 (defun org-get-packages-alist (var)
3410 "Get the packages alist and make sure it has 3 elements per entry."
3412 (if (and (consp x
) (= (length x
) 2))
3413 (list (car x
) (nth 1 x
) t
)
3415 (default-value var
)))
3417 ;; The following variables are defined here because is it also used
3418 ;; when formatting latex fragments. Originally it was part of the
3419 ;; LaTeX exporter, which is why the name includes "export".
3420 (defcustom org-export-latex-default-packages-alist
3421 '(("AUTO" "inputenc" t
)
3425 ("" "longtable" nil
)
3437 "Alist of default packages to be inserted in the header.
3438 Change this only if one of the packages here causes an incompatibility
3439 with another package you are using.
3440 The packages in this list are needed by one part or another of Org-mode
3441 to function properly.
3443 - inputenc, fontenc: for basic font and character selection
3444 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3445 for interpreting the entities in `org-entities'. You can skip some of these
3446 packages if you don't use any of the symbols in it.
3447 - graphicx: for including images
3448 - float, wrapfig: for figure placement
3449 - longtable: for long tables
3450 - hyperref: for cross references
3452 Therefore you should not modify this variable unless you know what you
3453 are doing. The one reason to change it anyway is that you might be loading
3454 some other package that conflicts with one of the default packages.
3455 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3456 If SNIPPET-FLAG is t, the package also needs to be included when
3457 compiling LaTeX snippets into images for inclusion into HTML."
3458 :group
'org-export-latex
3459 :set
'org-set-packages-alist
3460 :get
'org-get-packages-alist
3463 (list :tag
"options/package pair"
3464 (string :tag
"options")
3465 (string :tag
"package")
3466 (boolean :tag
"Snippet"))
3467 (string :tag
"A line of LaTeX"))))
3469 (defcustom org-export-latex-packages-alist nil
3470 "Alist of packages to be inserted in every LaTeX header.
3471 These will be inserted after `org-export-latex-default-packages-alist'.
3472 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3473 SNIPPET-FLAG, when t, indicates that this package is also needed when
3474 turning LaTeX snippets into images for inclusion into HTML.
3475 Make sure that you only list packages here which:
3476 - you want in every file
3477 - do not conflict with the default packages in
3478 `org-export-latex-default-packages-alist'
3479 - do not conflict with the setup in `org-format-latex-header'."
3480 :group
'org-export-latex
3481 :set
'org-set-packages-alist
3482 :get
'org-get-packages-alist
3485 (list :tag
"options/package pair"
3486 (string :tag
"options")
3487 (string :tag
"package")
3488 (boolean :tag
"Snippet"))
3489 (string :tag
"A line of LaTeX"))))
3492 (defgroup org-appearance nil
3493 "Settings for Org-mode appearance."
3494 :tag
"Org Appearance"
3497 (defcustom org-level-color-stars-only nil
3498 "Non-nil means fontify only the stars in each headline.
3499 When nil, the entire headline is fontified.
3500 Changing it requires restart of `font-lock-mode' to become effective
3501 also in regions already fontified."
3502 :group
'org-appearance
3505 (defcustom org-hide-leading-stars nil
3506 "Non-nil means hide the first N-1 stars in a headline.
3507 This works by using the face `org-hide' for these stars. This
3508 face is white for a light background, and black for a dark
3509 background. You may have to customize the face `org-hide' to
3511 Changing it requires restart of `font-lock-mode' to become effective
3512 also in regions already fontified.
3513 You may also set this on a per-file basis by adding one of the following
3514 lines to the buffer:
3516 #+STARTUP: hidestars
3517 #+STARTUP: showstars"
3518 :group
'org-appearance
3521 (defcustom org-hidden-keywords nil
3522 "List of symbols corresponding to keywords to be hidden the org buffer.
3523 For example, a value '(title) for this list will make the document's title
3524 appear in the buffer without the initial #+TITLE: keyword."
3525 :group
'org-appearance
3526 :type
'(set (const :tag
"#+AUTHOR" author
)
3527 (const :tag
"#+DATE" date
)
3528 (const :tag
"#+EMAIL" email
)
3529 (const :tag
"#+TITLE" title
)))
3531 (defcustom org-fontify-done-headline nil
3532 "Non-nil means change the face of a headline if it is marked DONE.
3533 Normally, only the TODO/DONE keyword indicates the state of a headline.
3534 When this is non-nil, the headline after the keyword is set to the
3535 `org-headline-done' as an additional indication."
3536 :group
'org-appearance
3539 (defcustom org-fontify-emphasized-text t
3540 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3541 Changing this variable requires a restart of Emacs to take effect."
3542 :group
'org-appearance
3545 (defcustom org-fontify-whole-heading-line nil
3546 "Non-nil means fontify the whole line for headings.
3547 This is useful when setting a background color for the
3549 :group
'org-appearance
3552 (defcustom org-highlight-latex-fragments-and-specials nil
3553 "Non-nil means fontify what is treated specially by the exporters."
3554 :group
'org-appearance
3557 (defcustom org-hide-emphasis-markers nil
3558 "Non-nil mean font-lock should hide the emphasis marker characters."
3559 :group
'org-appearance
3562 (defcustom org-pretty-entities nil
3563 "Non-nil means show entities as UTF8 characters.
3564 When nil, the \\name form remains in the buffer."
3565 :group
'org-appearance
3568 (defcustom org-pretty-entities-include-sub-superscripts t
3569 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3570 :group
'org-appearance
3573 (defvar org-emph-re nil
3574 "Regular expression for matching emphasis.
3575 After a match, the match groups contain these elements:
3576 0 The match of the full regular expression, including the characters
3577 before and after the proper match
3578 1 The character before the proper match, or empty at beginning of line
3579 2 The proper match, including the leading and trailing markers
3580 3 The leading marker like * or /, indicating the type of highlighting
3581 4 The text between the emphasis markers, not including the markers
3582 5 The character after the match, empty at the end of a line")
3583 (defvar org-verbatim-re nil
3584 "Regular expression for matching verbatim text.")
3585 (defvar org-emphasis-regexp-components
) ; defined just below
3586 (defvar org-emphasis-alist
) ; defined just below
3587 (defun org-set-emph-re (var val
)
3588 "Set variable and compute the emphasis regular expression."
3590 (when (and (boundp 'org-emphasis-alist
)
3591 (boundp 'org-emphasis-regexp-components
)
3592 org-emphasis-alist org-emphasis-regexp-components
)
3593 (let* ((e org-emphasis-regexp-components
)
3599 (body1 (concat body
"*?"))
3600 (markers (mapconcat 'car org-emphasis-alist
""))
3601 (vmarkers (mapconcat
3602 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3603 org-emphasis-alist
"")))
3604 ;; make sure special characters appear at the right position in the class
3605 (if (string-match "\\^" markers
)
3606 (setq markers
(concat (replace-match "" t t markers
) "^")))
3607 (if (string-match "-" markers
)
3608 (setq markers
(concat (replace-match "" t t markers
) "-")))
3609 (if (string-match "\\^" vmarkers
)
3610 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3611 (if (string-match "-" vmarkers
)
3612 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3614 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3615 (int-to-string nl
) "\\}")))
3618 (concat "\\([" pre
"]\\|^\\)"
3620 "\\([" markers
"]\\)"
3628 "\\([" post
"]\\|$\\)"))
3629 (setq org-verbatim-re
3630 (concat "\\([" pre
"]\\|^\\)"
3632 "\\([" vmarkers
"]\\)"
3640 "\\([" post
"]\\|$\\)")))))
3642 (defcustom org-emphasis-regexp-components
3643 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3644 "Components used to build the regular expression for emphasis.
3645 This is a list with five entries. Terminology: In an emphasis string
3646 like \" *strong word* \", we call the initial space PREMATCH, the final
3647 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3648 and \"trong wor\" is the body. The different components in this variable
3649 specify what is allowed/forbidden in each part:
3651 pre Chars allowed as prematch. Beginning of line will be allowed too.
3652 post Chars allowed as postmatch. End of line will be allowed too.
3653 border The chars *forbidden* as border characters.
3654 body-regexp A regexp like \".\" to match a body character. Don't use
3655 non-shy groups here, and don't allow newline here.
3656 newline The maximum number of newlines allowed in an emphasis exp.
3658 Use customize to modify this, or restart Emacs after changing it."
3659 :group
'org-appearance
3660 :set
'org-set-emph-re
3662 (sexp :tag
"Allowed chars in pre ")
3663 (sexp :tag
"Allowed chars in post ")
3664 (sexp :tag
"Forbidden chars in border ")
3665 (sexp :tag
"Regexp for body ")
3666 (integer :tag
"number of newlines allowed")
3667 (option (boolean :tag
"Please ignore this button"))))
3669 (defcustom org-emphasis-alist
3670 `(("*" bold
"<b>" "</b>")
3671 ("/" italic
"<i>" "</i>")
3672 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3673 ("=" org-code
"<code>" "</code>" verbatim
)
3674 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3675 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3678 "Special syntax for emphasized text.
3679 Text starting and ending with a special character will be emphasized, for
3680 example *bold*, _underlined_ and /italic/. This variable sets the marker
3681 characters, the face to be used by font-lock for highlighting in Org-mode
3682 Emacs buffers, and the HTML tags to be used for this.
3683 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3684 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3685 Use customize to modify this, or restart Emacs after changing it."
3686 :group
'org-appearance
3687 :set
'org-set-emph-re
3690 (string :tag
"Marker character")
3692 (face :tag
"Font-lock-face")
3693 (plist :tag
"Face property list"))
3694 (string :tag
"HTML start tag")
3695 (string :tag
"HTML end tag")
3696 (option (const verbatim
)))))
3698 (defvar org-protecting-blocks
3699 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3700 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3701 This is needed for font-lock setup.")
3703 ;;; Miscellaneous options
3705 (defgroup org-completion nil
3706 "Completion in Org-mode."
3707 :tag
"Org Completion"
3710 (defcustom org-completion-use-ido nil
3711 "Non-nil means use ido completion wherever possible.
3712 Note that `ido-mode' must be active for this variable to be relevant.
3713 If you decide to turn this variable on, you might well want to turn off
3714 `org-outline-path-complete-in-steps'.
3715 See also `org-completion-use-iswitchb'."
3716 :group
'org-completion
3719 (defcustom org-completion-use-iswitchb nil
3720 "Non-nil means use iswitchb completion wherever possible.
3721 Note that `iswitchb-mode' must be active for this variable to be relevant.
3722 If you decide to turn this variable on, you might well want to turn off
3723 `org-outline-path-complete-in-steps'.
3724 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3725 :group
'org-completion
3728 (defcustom org-completion-fallback-command
'hippie-expand
3729 "The expansion command called by \\[pcomplete] in normal context.
3730 Normal means, no org-mode-specific context."
3731 :group
'org-completion
3734 ;;; Functions and variables from their packages
3735 ;; Declared here to avoid compiler warnings
3738 (defvar outline-mode-menu-heading
)
3739 (defvar outline-mode-menu-show
)
3740 (defvar outline-mode-menu-hide
)
3741 (defvar zmacs-regions
) ; XEmacs regions
3744 (defvar mark-active
)
3747 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3748 (declare-function calendar-forward-day
"cal-move" (arg))
3749 (declare-function calendar-goto-date
"cal-move" (date))
3750 (declare-function calendar-goto-today
"cal-move" ())
3751 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3752 (defvar calc-embedded-close-formula
)
3753 (defvar calc-embedded-open-formula
)
3754 (declare-function cdlatex-tab
"ext:cdlatex" ())
3755 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3756 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3757 (defvar font-lock-unfontify-region-function
)
3758 (declare-function iswitchb-read-buffer
"iswitchb"
3759 (prompt &optional default require-match start matches-set
))
3760 (defvar iswitchb-temp-buflist
)
3761 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3762 (defvar org-agenda-tags-todo-honor-ignore-options
)
3763 (declare-function org-agenda-skip
"org-agenda" ())
3765 org-agenda-format-item
"org-agenda"
3766 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3767 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3768 (declare-function org-agenda-change-all-lines
"org-agenda"
3769 (newhead hdmarker
&optional fixface just-this
))
3770 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3771 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3772 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3774 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3775 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3776 "org-agenda" (&optional end
))
3777 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3778 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3779 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3780 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3781 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3782 (declare-function parse-time-string
"parse-time" (string))
3783 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3784 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3785 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3786 (defvar remember-data-file
)
3787 (defvar texmathp-why
)
3788 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3789 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3791 (defvar w3m-current-url
)
3792 (defvar w3m-current-title
)
3794 (defvar org-latex-regexps
)
3796 ;;; Autoload and prepare some org modules
3798 ;; Some table stuff that needs to be defined here, because it is used
3799 ;; by the functions setting up org-mode or checking for table context.
3801 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3802 "Detect an org-type or table-type table.")
3803 (defconst org-table-line-regexp
"^[ \t]*|"
3804 "Detect an org-type table line.")
3805 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3806 "Detect an org-type table line.")
3807 (defconst org-table-hline-regexp
"^[ \t]*|-"
3808 "Detect an org-type table hline.")
3809 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3810 "Detect a table-type table hline.")
3811 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3812 "Detect the first line outside a table when searching from within it.
3813 This works for both table types.")
3815 ;; Autoload the functions in org-table.el that are needed by functions here.
3818 (org-autoload "org-table"
3819 '(org-table-align org-table-begin org-table-blank-field
3820 org-table-convert org-table-convert-region org-table-copy-down
3821 org-table-copy-region org-table-create
3822 org-table-create-or-convert-from-region
3823 org-table-create-with-table.el org-table-current-dline
3824 org-table-cut-region org-table-delete-column org-table-edit-field
3825 org-table-edit-formulas org-table-end org-table-eval-formula
3826 org-table-export org-table-field-info
3827 org-table-get-stored-formulas org-table-goto-column
3828 org-table-hline-and-move org-table-import org-table-insert-column
3829 org-table-insert-hline org-table-insert-row org-table-iterate
3830 org-table-justify-field-maybe org-table-kill-row
3831 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3832 org-table-move-column org-table-move-column-left
3833 org-table-move-column-right org-table-move-row
3834 org-table-move-row-down org-table-move-row-up
3835 org-table-next-field org-table-next-row org-table-paste-rectangle
3836 org-table-previous-field org-table-recalculate
3837 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3838 org-table-toggle-coordinate-overlays
3839 org-table-toggle-formula-debugger org-table-wrap-region
3840 orgtbl-mode turn-on-orgtbl org-table-to-lisp
3841 orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
3842 orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo
)))
3844 (defun org-at-table-p (&optional table-type
)
3845 "Return t if the cursor is inside an org-type table.
3846 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3847 (if org-enable-table-editor
3849 (beginning-of-line 1)
3850 (looking-at (if table-type org-table-any-line-regexp
3851 org-table-line-regexp
)))
3853 (defsubst org-table-p
() (org-at-table-p))
3855 (defun org-at-table.el-p
()
3856 "Return t if and only if we are at a table.el table."
3857 (and (org-at-table-p 'any
)
3859 (goto-char (org-table-begin 'any
))
3860 (looking-at org-table1-hline-regexp
))))
3861 (defun org-table-recognize-table.el
()
3862 "If there is a table.el table nearby, recognize it and move into it."
3863 (if org-table-tab-recognizes-table.el
3864 (if (org-at-table.el-p
)
3866 (beginning-of-line 1)
3867 (if (looking-at org-table-dataline-regexp
)
3869 (if (looking-at org-table1-hline-regexp
)
3871 (beginning-of-line 2)
3872 (if (looking-at org-table-any-border-regexp
)
3873 (beginning-of-line -
1)))))
3874 (if (re-search-forward "|" (org-table-end t
) t
)
3877 (if (table--at-cell-p (point))
3879 (message "recognizing table.el table...")
3880 (table-recognize-table)
3881 (message "recognizing table.el table...done")))
3882 (error "This should not happen"))
3887 (defun org-at-table-hline-p ()
3888 "Return t if the cursor is inside a hline in a table."
3889 (if org-enable-table-editor
3891 (beginning-of-line 1)
3892 (looking-at org-table-hline-regexp
))
3895 (defvar org-table-clean-did-remove-column nil
)
3897 (defun org-table-map-tables (function &optional quietly
)
3898 "Apply FUNCTION to the start of all tables in the buffer."
3902 (goto-char (point-min))
3903 (while (re-search-forward org-table-any-line-regexp nil t
)
3905 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3906 (beginning-of-line 1)
3907 (when (looking-at org-table-line-regexp
)
3908 (save-excursion (funcall function
))
3909 (or (looking-at org-table-line-regexp
)
3911 (re-search-forward org-table-any-border-regexp nil
1))))
3912 (unless quietly
(message "Mapping tables: done")))
3914 ;; Declare and autoload functions from org-exp.el & Co
3916 (declare-function org-default-export-plist
"org-exp")
3917 (declare-function org-infile-export-plist
"org-exp")
3918 (declare-function org-get-current-options
"org-exp")
3920 (org-autoload "org-exp"
3921 '(org-export org-export-visible
3922 org-insert-export-options-template
3923 org-table-clean-before-export
))
3924 (org-autoload "org-ascii"
3925 '(org-export-as-ascii org-export-ascii-preprocess
3926 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3927 org-export-region-as-ascii
))
3928 (org-autoload "org-latex"
3929 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3930 org-replace-region-by-latex org-export-region-as-latex
3931 org-export-as-latex org-export-as-pdf
3932 org-export-as-pdf-and-open
))
3933 (org-autoload "org-html"
3934 '(org-export-as-html-and-open
3935 org-export-as-html-batch org-export-as-html-to-buffer
3936 org-replace-region-by-html org-export-region-as-html
3937 org-export-as-html
))
3938 (org-autoload "org-docbook"
3939 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
3940 org-replace-region-by-docbook org-export-region-as-docbook
3941 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
3942 org-export-as-docbook
))
3943 (org-autoload "org-icalendar"
3944 '(org-export-icalendar-this-file
3945 org-export-icalendar-all-agenda-files
3946 org-export-icalendar-combine-agenda-files
))
3947 (org-autoload "org-xoxo" '(org-export-as-xoxo))
3948 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning
)))
3950 ;; Declare and autoload functions from org-agenda.el
3953 (org-autoload "org-agenda"
3954 '(org-agenda org-agenda-list org-search-view
3955 org-todo-list org-tags-view org-agenda-list-stuck-projects
3956 org-diary org-agenda-to-appt
3957 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
3959 ;; Autoload org-remember
3962 (org-autoload "org-remember"
3963 '(org-remember-insinuate org-remember-annotation
3964 org-remember-apply-template org-remember org-remember-handler
)))
3967 (org-autoload "org-capture"
3968 '(org-capture org-capture-insert-template-here
3969 org-capture-import-remember-templates
)))
3971 ;; Autoload org-clock.el
3973 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
3975 (declare-function org-clock-update-mode-line
"org-clock" ())
3976 (declare-function org-resolve-clocks
"org-clock"
3977 (&optional also-non-dangling-p prompt last-valid
))
3978 (defvar org-clock-start-time
)
3979 (defvar org-clock-marker
(make-marker)
3980 "Marker recording the last clock-in.")
3981 (defvar org-clock-hd-marker
(make-marker)
3982 "Marker recording the last clock-in, but the headline position.")
3983 (defvar org-clock-heading
""
3984 "The heading of the current clock entry.")
3985 (defun org-clock-is-active ()
3986 "Return non-nil if clock is currently running.
3987 The return value is actually the clock marker."
3988 (marker-buffer org-clock-marker
))
3993 '(org-clock-in org-clock-out org-clock-cancel
3994 org-clock-goto org-clock-sum org-clock-display
3995 org-clock-remove-overlays org-clock-report
3996 org-clocktable-shift org-dblock-write
:clocktable
3997 org-get-clocktable org-resolve-clocks
)))
3999 (defun org-clock-update-time-maybe ()
4000 "If this is a CLOCK line, update it and return t.
4001 Otherwise, return nil."
4004 (beginning-of-line 1)
4005 (skip-chars-forward " \t")
4006 (when (looking-at org-clock-string
)
4007 (let ((re (concat "[ \t]*" org-clock-string
4008 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
4009 "\\([ \t]*=>.*\\)?\\)?"))
4012 ((not (looking-at re
))
4014 ((not (match-end 2))
4015 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4016 (> org-clock-marker
(point))
4017 (<= org-clock-marker
(point-at-eol)))
4018 ;; The clock is running here
4019 (setq org-clock-start-time
4021 (org-parse-time-string (match-string 1))))
4022 (org-clock-update-mode-line)))
4024 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
4026 (setq ts
(match-string 1)
4027 te
(match-string 3))
4028 (setq s
(- (org-float-time
4029 (apply 'encode-time
(org-parse-time-string te
)))
4031 (apply 'encode-time
(org-parse-time-string ts
))))
4034 h
(floor (/ s
3600))
4038 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
4041 (defun org-check-running-clock ()
4042 "Check if the current buffer contains the running clock.
4043 If yes, offer to stop it and to save the buffer with the changes."
4044 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4045 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4048 (when (y-or-n-p "Save changed buffer?")
4051 (defun org-clocktable-try-shift (dir n
)
4052 "Check if this line starts a clock table, if yes, shift the time block."
4053 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4054 (org-clocktable-shift dir n
)))
4056 ;; Autoload org-timer.el
4061 '(org-timer-start org-timer org-timer-item
4062 org-timer-change-times-in-region
4064 org-timer-reset-timers
4065 org-timer-show-remaining-time
)))
4067 ;; Autoload org-feed.el
4072 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
4075 ;; Autoload org-indent.el
4077 ;; Define the variable already here, to make sure we have it.
4078 (defvar org-indent-mode nil
4079 "Non-nil if Org-Indent mode is enabled.
4080 Use the command `org-indent-mode' to change this variable.")
4085 '(org-indent-mode)))
4087 ;; Autoload org-mobile.el
4092 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
4094 ;; Autoload archiving code
4095 ;; The stuff that is needed for cycling and tags has to be defined here.
4097 (defgroup org-archive nil
4098 "Options concerning archiving in Org-mode."
4100 :group
'org-structure
)
4102 (defcustom org-archive-location
"%s_archive::"
4103 "The location where subtrees should be archived.
4105 The value of this variable is a string, consisting of two parts,
4106 separated by a double-colon. The first part is a filename and
4107 the second part is a headline.
4109 When the filename is omitted, archiving happens in the same file.
4110 %s in the filename will be replaced by the current file
4111 name (without the directory part). Archiving to a different file
4112 is useful to keep archived entries from contributing to the
4115 The archived entries will be filed as subtrees of the specified
4116 headline. When the headline is omitted, the subtrees are simply
4117 filed away at the end of the file, as top-level entries. Also in
4118 the heading you can use %s to represent the file name, this can be
4119 useful when using the same archive for a number of different files.
4121 Here are a few examples:
4123 If the current file is Projects.org, archive in file
4124 Projects.org_archive, as top-level trees. This is the default.
4126 \"::* Archived Tasks\"
4127 Archive in the current file, under the top-level headline
4128 \"* Archived Tasks\".
4130 \"~/org/archive.org::\"
4131 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4133 \"~/org/archive.org::* From %s\"
4134 Archive in file ~/org/archive.org (absolute path), under headlines
4135 \"From FILENAME\" where file name is the current file name.
4137 \"basement::** Finished Tasks\"
4138 Archive in file ./basement (relative path), as level 3 trees
4139 below the level 2 heading \"** Finished Tasks\".
4141 You may set this option on a per-file basis by adding to the buffer a
4144 #+ARCHIVE: basement::** Finished Tasks
4146 You may also define it locally for a subtree by setting an ARCHIVE property
4147 in the entry. If such a property is found in an entry, or anywhere up
4148 the hierarchy, it will be used."
4152 (defcustom org-archive-tag
"ARCHIVE"
4153 "The tag that marks a subtree as archived.
4154 An archived subtree does not open during visibility cycling, and does
4155 not contribute to the agenda listings.
4156 After changing this, font-lock must be restarted in the relevant buffers to
4157 get the proper fontification."
4159 :group
'org-keywords
4162 (defcustom org-agenda-skip-archived-trees t
4163 "Non-nil means the agenda will skip any items located in archived trees.
4164 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4165 variable is no longer recommended, you should leave it at the value t.
4166 Instead, use the key `v' to cycle the archives-mode in the agenda."
4168 :group
'org-agenda-skip
4171 (defcustom org-columns-skip-archived-trees t
4172 "Non-nil means ignore archived trees when creating column view."
4174 :group
'org-properties
4177 (defcustom org-cycle-open-archived-trees nil
4178 "Non-nil means `org-cycle' will open archived trees.
4179 An archived tree is a tree marked with the tag ARCHIVE.
4180 When nil, archived trees will stay folded. You can still open them with
4181 normal outline commands like `show-all', but not with the cycling commands."
4186 (defcustom org-sparse-tree-open-archived-trees nil
4187 "Non-nil means sparse tree construction shows matches in archived trees.
4188 When nil, matches in these trees are highlighted, but the trees are kept in
4191 :group
'org-sparse-trees
4194 (defun org-cycle-hide-archived-subtrees (state)
4195 "Re-hide all archived subtrees after a visibility state change."
4196 (when (and (not org-cycle-open-archived-trees
)
4197 (not (memq state
'(overview folded
))))
4199 (let* ((globalp (memq state
'(contents all
)))
4200 (beg (if globalp
(point-min) (point)))
4201 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4202 (org-hide-archived-subtrees beg end
)
4204 (if (looking-at (concat ".*:" org-archive-tag
":"))
4205 (message "%s" (substitute-command-keys
4206 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4208 (defun org-force-cycle-archived ()
4209 "Cycle subtree even if it is archived."
4211 (setq this-command
'org-cycle
)
4212 (let ((org-cycle-open-archived-trees t
))
4213 (call-interactively 'org-cycle
)))
4215 (defun org-hide-archived-subtrees (beg end
)
4216 "Re-hide all archived subtrees after a visibility state change."
4218 (let* ((re (concat ":" org-archive-tag
":")))
4220 (while (re-search-forward re end t
)
4221 (when (org-on-heading-p)
4222 (org-flag-subtree t
)
4223 (org-end-of-subtree t
))))))
4225 (defun org-flag-subtree (flag)
4227 (org-back-to-heading t
)
4228 (outline-end-of-heading)
4229 (outline-flag-region (point)
4230 (progn (org-end-of-subtree t
) (point))
4233 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4236 (org-autoload "org-archive"
4237 '(org-add-archive-files org-archive-subtree
4238 org-archive-to-archive-sibling org-toggle-archive-tag
4239 org-archive-subtree-default
4240 org-archive-subtree-default-with-confirmation
)))
4242 ;; Autoload Column View Code
4244 (declare-function org-columns-number-to-string
"org-colview")
4245 (declare-function org-columns-get-format-and-top-level
"org-colview")
4246 (declare-function org-columns-compute
"org-colview")
4248 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4249 '(org-columns-number-to-string org-columns-get-format-and-top-level
4250 org-columns-compute org-agenda-columns org-columns-remove-overlays
4251 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
4255 (declare-function org-id-store-link
"org-id")
4256 (declare-function org-id-locations-load
"org-id")
4257 (declare-function org-id-locations-save
"org-id")
4258 (defvar org-id-track-globally
)
4259 (org-autoload "org-id"
4260 '(org-id-get-create org-id-new org-id-copy org-id-get
4261 org-id-get-with-outline-path-completion
4262 org-id-get-with-outline-drilling org-id-store-link
4263 org-id-goto org-id-find org-id-store-link
))
4265 ;; Autoload Plotting Code
4267 (org-autoload "org-plot"
4268 '(org-plot/gnuplot
))
4270 ;;; Variables for pre-computed regular expressions, all buffer local
4272 (defvar org-drawer-regexp nil
4273 "Matches first line of a hidden block.")
4274 (make-variable-buffer-local 'org-drawer-regexp
)
4275 (defvar org-todo-regexp nil
4276 "Matches any of the TODO state keywords.")
4277 (make-variable-buffer-local 'org-todo-regexp
)
4278 (defvar org-not-done-regexp nil
4279 "Matches any of the TODO state keywords except the last one.")
4280 (make-variable-buffer-local 'org-not-done-regexp
)
4281 (defvar org-not-done-heading-regexp nil
4282 "Matches a TODO headline that is not done.")
4283 (make-variable-buffer-local 'org-not-done-regexp
)
4284 (defvar org-todo-line-regexp nil
4285 "Matches a headline and puts TODO state into group 2 if present.")
4286 (make-variable-buffer-local 'org-todo-line-regexp
)
4287 (defvar org-complex-heading-regexp nil
4288 "Matches a headline and puts everything into groups:
4290 group 2: The todo keyword, maybe
4291 group 3: Priority cookie
4292 group 4: True headline
4294 (make-variable-buffer-local 'org-complex-heading-regexp
)
4295 (defvar org-complex-heading-regexp-format nil
4296 "Printf format to make regexp to match an exact headline.
4297 This regexp will match the headline of any node which has the
4298 exact headline text that is put into the format, but may have any
4299 TODO state, priority and tags.")
4300 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4301 (defvar org-todo-line-tags-regexp nil
4302 "Matches a headline and puts TODO state into group 2 if present.
4303 Also put tags into group 4 if tags are present.")
4304 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4305 (defvar org-ds-keyword-length
12
4306 "Maximum length of the Deadline and SCHEDULED keywords.")
4307 (make-variable-buffer-local 'org-ds-keyword-length
)
4308 (defvar org-deadline-regexp nil
4309 "Matches the DEADLINE keyword.")
4310 (make-variable-buffer-local 'org-deadline-regexp
)
4311 (defvar org-deadline-time-regexp nil
4312 "Matches the DEADLINE keyword together with a time stamp.")
4313 (make-variable-buffer-local 'org-deadline-time-regexp
)
4314 (defvar org-deadline-line-regexp nil
4315 "Matches the DEADLINE keyword and the rest of the line.")
4316 (make-variable-buffer-local 'org-deadline-line-regexp
)
4317 (defvar org-scheduled-regexp nil
4318 "Matches the SCHEDULED keyword.")
4319 (make-variable-buffer-local 'org-scheduled-regexp
)
4320 (defvar org-scheduled-time-regexp nil
4321 "Matches the SCHEDULED keyword together with a time stamp.")
4322 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4323 (defvar org-closed-time-regexp nil
4324 "Matches the CLOSED keyword together with a time stamp.")
4325 (make-variable-buffer-local 'org-closed-time-regexp
)
4327 (defvar org-keyword-time-regexp nil
4328 "Matches any of the 4 keywords, together with the time stamp.")
4329 (make-variable-buffer-local 'org-keyword-time-regexp
)
4330 (defvar org-keyword-time-not-clock-regexp nil
4331 "Matches any of the 3 keywords, together with the time stamp.")
4332 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4333 (defvar org-maybe-keyword-time-regexp nil
4334 "Matches a timestamp, possibly preceded by a keyword.")
4335 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4336 (defvar org-planning-or-clock-line-re nil
4337 "Matches a line with planning or clock info.")
4338 (make-variable-buffer-local 'org-planning-or-clock-line-re
)
4339 (defvar org-all-time-keywords nil
4340 "List of time keywords.")
4341 (make-variable-buffer-local 'org-all-time-keywords
)
4343 (defconst org-plain-time-of-day-regexp
4346 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4349 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4351 "Regular expression to match a plain time or time range.
4352 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4353 groups carry important information:
4355 1 the first time, range or not
4356 8 the second time, if it is a range.")
4358 (defconst org-plain-time-extension-regexp
4361 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4362 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4363 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4364 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4365 groups carry important information:
4368 9 minutes of duration")
4370 (defconst org-stamp-time-of-day-regexp
4372 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4373 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4375 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4376 "Regular expression to match a timestamp time or time range.
4377 After a match, the following groups carry important information:
4379 1 date plus weekday, for back referencing to make sure both times are on the same day
4380 2 the first time, range or not
4381 4 the second time, if it is a range.")
4383 (defconst org-startup-options
4384 '(("fold" org-startup-folded t
)
4385 ("overview" org-startup-folded t
)
4386 ("nofold" org-startup-folded nil
)
4387 ("showall" org-startup-folded nil
)
4388 ("showeverything" org-startup-folded showeverything
)
4389 ("content" org-startup-folded content
)
4390 ("indent" org-startup-indented t
)
4391 ("noindent" org-startup-indented nil
)
4392 ("hidestars" org-hide-leading-stars t
)
4393 ("showstars" org-hide-leading-stars nil
)
4394 ("odd" org-odd-levels-only t
)
4395 ("oddeven" org-odd-levels-only nil
)
4396 ("align" org-startup-align-all-tables t
)
4397 ("noalign" org-startup-align-all-tables nil
)
4398 ("inlineimages" org-startup-with-inline-images t
)
4399 ("noinlineimages" org-startup-with-inline-images nil
)
4400 ("customtime" org-display-custom-times t
)
4401 ("logdone" org-log-done time
)
4402 ("lognotedone" org-log-done note
)
4403 ("nologdone" org-log-done nil
)
4404 ("lognoteclock-out" org-log-note-clock-out t
)
4405 ("nolognoteclock-out" org-log-note-clock-out nil
)
4406 ("logrepeat" org-log-repeat state
)
4407 ("lognoterepeat" org-log-repeat note
)
4408 ("nologrepeat" org-log-repeat nil
)
4409 ("logreschedule" org-log-reschedule time
)
4410 ("lognotereschedule" org-log-reschedule note
)
4411 ("nologreschedule" org-log-reschedule nil
)
4412 ("logredeadline" org-log-redeadline time
)
4413 ("lognoteredeadline" org-log-redeadline note
)
4414 ("nologredeadline" org-log-redeadline nil
)
4415 ("logrefile" org-log-refile time
)
4416 ("lognoterefile" org-log-refile note
)
4417 ("nologrefile" org-log-refile nil
)
4418 ("fninline" org-footnote-define-inline t
)
4419 ("nofninline" org-footnote-define-inline nil
)
4420 ("fnlocal" org-footnote-section nil
)
4421 ("fnauto" org-footnote-auto-label t
)
4422 ("fnprompt" org-footnote-auto-label nil
)
4423 ("fnconfirm" org-footnote-auto-label confirm
)
4424 ("fnplain" org-footnote-auto-label plain
)
4425 ("fnadjust" org-footnote-auto-adjust t
)
4426 ("nofnadjust" org-footnote-auto-adjust nil
)
4427 ("constcgs" constants-unit-system cgs
)
4428 ("constSI" constants-unit-system SI
)
4429 ("noptag" org-tag-persistent-alist nil
)
4430 ("hideblocks" org-hide-block-startup t
)
4431 ("nohideblocks" org-hide-block-startup nil
)
4432 ("beamer" org-startup-with-beamer-mode t
)
4433 ("entitiespretty" org-pretty-entities t
)
4434 ("entitiesplain" org-pretty-entities nil
))
4435 "Variable associated with STARTUP options for org-mode.
4436 Each element is a list of three items: The startup options as written
4437 in the #+STARTUP line, the corresponding variable, and the value to
4438 set this variable to if the option is found. An optional forth element PUSH
4439 means to push this value onto the list in the variable.")
4441 (defun org-update-property-plist (key val props
)
4442 "Update PROPS with KEY and VAL."
4443 (if (string= "+" (substring key
(- (length key
) 1)))
4444 (let* ((key (substring key
0 (- (length key
) 1)))
4445 (previous (cdr (assoc key props
))))
4446 (cons (cons key
(concat previous
" " val
))
4447 (org-remove-if (lambda (p) (string= (car p
) key
)) props
)))
4448 (cons (cons key val
) props
)))
4450 (defconst org-block-regexp
4451 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4452 "Regular expression for hiding blocks.")
4453 (defconst org-heading-keyword-regexp-format
4454 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4455 "Printf format for a regexp matching an headline with some keyword.
4456 This regexp will match the headline of any node which has the
4457 exact keyword that is put into the format. The keyword isn't in
4458 any group by default, but the stars and the body are.")
4459 (defconst org-heading-keyword-maybe-regexp-format
4460 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4461 "Printf format for a regexp matching an headline, possibly with some keyword.
4462 This regexp can match any headline with the specified keyword, or
4463 without a keyword. The keyword isn't in any group by default,
4464 but the stars and the body are.")
4466 (defun org-set-regexps-and-options ()
4467 "Precompute regular expressions for current buffer."
4468 (when (eq major-mode
'org-mode
)
4469 (org-set-local 'org-todo-kwd-alist nil
)
4470 (org-set-local 'org-todo-key-alist nil
)
4471 (org-set-local 'org-todo-key-trigger nil
)
4472 (org-set-local 'org-todo-keywords-1 nil
)
4473 (org-set-local 'org-done-keywords nil
)
4474 (org-set-local 'org-todo-heads nil
)
4475 (org-set-local 'org-todo-sets nil
)
4476 (org-set-local 'org-todo-log-states nil
)
4477 (org-set-local 'org-file-properties nil
)
4478 (org-set-local 'org-file-tags nil
)
4479 (let ((re (org-make-options-regexp
4480 '("CATEGORY" "TODO" "COLUMNS"
4481 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4482 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4484 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4486 (scripts org-use-sub-superscripts
)
4487 kwds kws0 kwsa key log value cat arch tags const links hw dws
4488 tail sep kws1 prio props ftags drawers beamer-p
4489 ext-setup-or-nil setup-contents
(start 0))
4493 (goto-char (point-min))
4494 (while (or (and ext-setup-or-nil
4495 (string-match re ext-setup-or-nil start
)
4496 (setq start
(match-end 0)))
4497 (and (setq ext-setup-or-nil nil start
0)
4498 (re-search-forward re nil t
)))
4499 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4500 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4501 (if (stringp value
) (setq value
(org-trim value
)))
4503 ((equal key
"CATEGORY")
4505 ((member key
'("SEQ_TODO" "TODO"))
4506 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4507 ((equal key
"TYP_TODO")
4508 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4509 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4510 ;; general TODO-like setup
4511 (push (cons (intern (downcase (match-string 1 key
)))
4512 (org-split-string value splitre
)) kwds
))
4514 (setq tags
(append tags
(if tags
'("\\n") nil
)
4515 (org-split-string value splitre
))))
4516 ((equal key
"COLUMNS")
4517 (org-set-local 'org-columns-default-format value
))
4519 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4520 (push (cons (match-string 1 value
)
4521 (org-trim (match-string 2 value
)))
4523 ((equal key
"PRIORITIES")
4524 (setq prio
(org-split-string value
" +")))
4525 ((equal key
"PROPERTY")
4526 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4527 (setq props
(org-update-property-plist (match-string 1 value
)
4528 (match-string 2 value
)
4530 ((equal key
"FILETAGS")
4531 (when (string-match "\\S-" value
)
4536 (mapcar (lambda (x) (org-split-string x
":"))
4537 (org-split-string value
)))))))
4538 ((equal key
"DRAWERS")
4539 (setq drawers
(org-split-string value splitre
)))
4540 ((equal key
"CONSTANTS")
4541 (setq const
(append const
(org-split-string value splitre
))))
4542 ((equal key
"STARTUP")
4543 (let ((opts (org-split-string value splitre
))
4545 (while (setq l
(pop opts
))
4546 (when (setq l
(assoc l org-startup-options
))
4547 (setq var
(nth 1 l
) val
(nth 2 l
))
4549 (set (make-local-variable var
) val
)
4550 (if (not (listp (symbol-value var
)))
4551 (set (make-local-variable var
) nil
))
4552 (set (make-local-variable var
) (symbol-value var
))
4553 (add-to-list var val
))))))
4554 ((equal key
"ARCHIVE")
4556 (remove-text-properties 0 (length arch
)
4557 '(face t fontified t
) arch
))
4558 ((equal key
"LATEX_CLASS")
4559 (setq beamer-p
(equal value
"beamer")))
4560 ((equal key
"OPTIONS")
4561 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4562 (setq scripts
(read (match-string 2 value
)))))
4563 ((equal key
"SETUPFILE")
4564 (setq setup-contents
(org-file-contents
4566 (org-remove-double-quotes value
))
4568 (if (not ext-setup-or-nil
)
4569 (setq ext-setup-or-nil setup-contents start
0)
4570 (setq ext-setup-or-nil
4571 (concat (substring ext-setup-or-nil
0 start
)
4572 "\n" setup-contents
"\n"
4573 (substring ext-setup-or-nil start
)))))))
4574 ;; search for property blocks
4575 (goto-char (point-min))
4576 (while (re-search-forward org-block-regexp nil t
)
4577 (when (equal "PROPERTY" (upcase (match-string 1)))
4578 (setq value
(replace-regexp-in-string
4579 "[\n\r]" " " (match-string 4)))
4580 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4581 (setq props
(org-update-property-plist (match-string 1 value
)
4582 (match-string 2 value
)
4584 (org-set-local 'org-use-sub-superscripts scripts
)
4586 (org-set-local 'org-category
(intern cat
))
4587 (push (cons "CATEGORY" cat
) props
))
4589 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4590 (setq prio
(mapcar 'string-to-char prio
))
4591 (org-set-local 'org-highest-priority
(nth 0 prio
))
4592 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4593 (org-set-local 'org-default-priority
(nth 2 prio
)))
4594 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4595 (and ftags
(org-set-local 'org-file-tags
4596 (mapcar 'org-add-prop-inherited ftags
)))
4597 (and drawers
(org-set-local 'org-drawers drawers
))
4598 (and arch
(org-set-local 'org-archive-location arch
))
4599 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4600 ;; Process the TODO keywords
4602 ;; Use the global values as if they had been given locally.
4603 (setq kwds
(default-value 'org-todo-keywords
))
4604 (if (stringp (car kwds
))
4605 (setq kwds
(list (cons org-todo-interpretation
4606 (default-value 'org-todo-keywords
)))))
4607 (setq kwds
(reverse kwds
)))
4608 (setq kwds
(nreverse kwds
))
4610 (while (setq kws
(pop kwds
))
4612 (run-hook-with-args-until-success
4613 'org-todo-setup-filter-hook kws
)
4615 (setq inter
(pop kws
) sep
(member "|" kws
)
4616 kws0
(delete "|" (copy-sequence kws
))
4621 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4623 (setq kw
(match-string 1 x
)
4624 key
(and (match-end 2) (match-string 2 x
))
4625 log
(org-extract-log-state-settings x
))
4626 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4627 (and log
(push log org-todo-log-states
))
4629 (error "Invalid TODO keyword %s" x
)))
4631 kwsa
(if kwsa
(append '((:startgroup
))
4635 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4636 tail
(list inter hw
(car dws
) (org-last dws
))))
4637 (add-to-list 'org-todo-heads hw
'append
)
4638 (push kws1 org-todo-sets
)
4639 (setq org-done-keywords
(append org-done-keywords dws nil
))
4640 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4641 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4642 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4643 (setq org-todo-sets
(nreverse org-todo-sets
)
4644 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4645 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4646 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4647 ;; Process the constants
4650 (while (setq e
(pop const
))
4651 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4652 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4653 (setq org-table-formula-constants-local cst
)))
4655 ;; Process the tags.
4658 (while (setq e
(pop tags
))
4660 ((equal e
"{") (push '(:startgroup
) tgs
))
4661 ((equal e
"}") (push '(:endgroup
) tgs
))
4662 ((equal e
"\\n") (push '(:newline
) tgs
))
4663 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4664 (push (cons (match-string 1 e
)
4665 (string-to-char (match-string 2 e
)))
4667 (t (push (list e
) tgs
))))
4668 (org-set-local 'org-tag-alist nil
)
4669 (while (setq e
(pop tgs
))
4670 (or (and (stringp (car e
))
4671 (assoc (car e
) org-tag-alist
))
4672 (push e org-tag-alist
)))))
4674 ;; Compute the regular expressions and other local variables.
4675 ;; Using `org-outline-regexp-bol' would complicate them much,
4676 ;; because of the fixed white space at the end of that string.
4677 (if (not org-done-keywords
)
4678 (setq org-done-keywords
(and org-todo-keywords-1
4679 (list (org-last org-todo-keywords-1
)))))
4680 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4681 (length org-scheduled-string
)
4682 (length org-clock-string
)
4683 (length org-closed-string
)))
4685 (concat "^[ \t]*:\\("
4686 (mapconcat 'regexp-quote org-drawers
"\\|")
4688 org-not-done-keywords
4689 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4692 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4696 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4698 org-not-done-heading-regexp
4699 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4700 org-todo-line-regexp
4701 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4702 org-complex-heading-regexp
4703 (concat "^\\(\\*+\\)"
4704 "\\(?: +" org-todo-regexp
"\\)?"
4705 "\\(?: +\\(\\[#.\\]\\)\\)?"
4706 "\\(?: +\\(.*?\\)\\)?"
4707 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4709 org-complex-heading-regexp-format
4710 (concat "^\\(\\*+\\)"
4711 "\\(?: +" org-todo-regexp
"\\)?"
4712 "\\(?: +\\(\\[#.\\]\\)\\)?"
4714 ;; Stats cookies can be sticked to body.
4715 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4717 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4719 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4721 org-todo-line-tags-regexp
4722 (concat "^\\(\\*+\\)"
4723 "\\(?: +" org-todo-regexp
"\\)?"
4724 "\\(?: +\\(.*?\\)\\)?"
4725 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4727 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4728 org-deadline-time-regexp
4729 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4730 org-deadline-line-regexp
4731 (concat "\\<\\(" org-deadline-string
"\\).*")
4732 org-scheduled-regexp
4733 (concat "\\<" org-scheduled-string
)
4734 org-scheduled-time-regexp
4735 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4736 org-closed-time-regexp
4737 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4738 org-keyword-time-regexp
4739 (concat "\\<\\(" org-scheduled-string
4740 "\\|" org-deadline-string
4741 "\\|" org-closed-string
4742 "\\|" org-clock-string
"\\)"
4743 " *[[<]\\([^]>]+\\)[]>]")
4744 org-keyword-time-not-clock-regexp
4745 (concat "\\<\\(" org-scheduled-string
4746 "\\|" org-deadline-string
4747 "\\|" org-closed-string
4749 " *[[<]\\([^]>]+\\)[]>]")
4750 org-maybe-keyword-time-regexp
4751 (concat "\\(\\<\\(" org-scheduled-string
4752 "\\|" org-deadline-string
4753 "\\|" org-closed-string
4754 "\\|" org-clock-string
"\\)\\)?"
4755 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4756 org-planning-or-clock-line-re
4757 (concat "\\(?:^[ \t]*\\(" org-scheduled-string
4758 "\\|" org-deadline-string
4759 "\\|" org-closed-string
"\\|" org-clock-string
4761 org-all-time-keywords
4762 (mapcar (lambda (w) (substring w
0 -
1))
4763 (list org-scheduled-string org-deadline-string
4764 org-clock-string org-closed-string
))
4766 (org-compute-latex-and-specials-regexp)
4767 (org-set-font-lock-defaults))))
4769 (defun org-file-contents (file &optional noerror
)
4770 "Return the contents of FILE, as a string."
4772 (not (file-readable-p file
)))
4775 (message "Cannot read file \"%s\"" file
)
4778 (error "Cannot read file \"%s\"" file
))
4780 (insert-file-contents file
)
4783 (defun org-extract-log-state-settings (x)
4784 "Extract the log state setting from a TODO keyword string.
4785 This will extract info from a string like \"WAIT(w@/!)\"."
4786 (let (kw key log1 log2
)
4787 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4788 (setq kw
(match-string 1 x
)
4789 key
(and (match-end 2) (match-string 2 x
))
4790 log1
(and (match-end 3) (match-string 3 x
))
4791 log2
(and (match-end 4) (match-string 4 x
)))
4794 (and log1
(if (equal log1
"!") 'time
'note
))
4795 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4797 (defun org-remove-keyword-keys (list)
4798 "Remove a pair of parenthesis at the end of each string in LIST."
4800 (if (string-match "(.*)$" x
)
4801 (substring x
0 (match-beginning 0))
4805 (defun org-assign-fast-keys (alist)
4806 "Assign fast keys to a keyword-key alist.
4807 Respect keys that are already there."
4808 (let (new e
(alt ?
0))
4809 (while (setq e
(pop alist
))
4810 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4811 (cdr e
)) ;; Key already assigned.
4813 (let ((clist (string-to-list (downcase (car e
))))
4814 (used (append new alist
)))
4815 (when (= (car clist
) ?
@)
4817 (while (and clist
(rassoc (car clist
) used
))
4820 (while (rassoc alt used
)
4822 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4825 ;;; Some variables used in various places
4827 (defvar org-window-configuration nil
4828 "Used in various places to store a window configuration.")
4829 (defvar org-selected-window nil
4830 "Used in various places to store a window configuration.")
4831 (defvar org-finish-function nil
4832 "Function to be called when `C-c C-c' is used.
4833 This is for getting out of special buffers like remember.")
4836 ;; FIXME: Occasionally check by commenting these, to make sure
4837 ;; no other functions uses these, forgetting to let-bind them.
4842 ;; Defined somewhere in this file, but used before definition.
4843 (defvar org-entities
) ;; defined in org-entities.el
4844 (defvar org-struct-menu
)
4845 (defvar org-org-menu
)
4846 (defvar org-tbl-menu
)
4848 ;;;; Define the Org-mode
4850 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4851 (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"))
4854 ;; We use a before-change function to check if a table might need
4856 (defvar org-table-may-need-update t
4857 "Indicates that a table might need an update.
4858 This variable is set by `org-before-change-function'.
4859 `org-table-align' sets it back to nil.")
4860 (defun org-before-change-function (beg end
)
4861 "Every change indicates that a table might need an update."
4862 (setq org-table-may-need-update t
))
4863 (defvar org-mode-map
)
4864 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4865 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4866 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4867 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4868 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4869 (defvar org-table-buffer-is-an nil
)
4871 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
4872 ;; some places -- e.g. see the macro org-with-limited-levels.
4874 ;; In Org buffers, the value of `outline-regexp' is that of
4875 ;; `org-outline-regexp'. The only function still directly relying on
4876 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
4877 ;; job when `orgstruct-mode' is active.
4878 (defvar org-outline-regexp
"\\*+ "
4879 "Regexp to match Org headlines.")
4880 (defconst org-outline-regexp-bol
"^\\*+ "
4881 "Regexp to match Org headlines.
4882 This is similar to `org-outline-regexp' but additionally makes
4883 sure that we are at the beginning of the line.")
4885 (defconst org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4886 "Matches an headline, putting stars and text into groups.
4887 Stars are put in group 1 and the trimmed body in group 2.")
4890 (define-derived-mode org-mode outline-mode
"Org"
4891 "Outline-based notes management and organizer, alias
4892 \"Carsten's outline-mode for keeping track of everything.\"
4894 Org-mode develops organizational tasks around a NOTES file which
4895 contains information about projects as plain text. Org-mode is
4896 implemented on top of outline-mode, which is ideal to keep the content
4897 of large files well structured. It supports ToDo items, deadlines and
4898 time stamps, which magically appear in the diary listing of the Emacs
4899 calendar. Tables are easily created with a built-in table editor.
4900 Plain text URL-like links connect to websites, emails (VM), Usenet
4901 messages (Gnus), BBDB entries, and any files related to the project.
4902 For printing and sharing of notes, an Org-mode file (or a part of it)
4903 can be exported as a structured ASCII or HTML file.
4905 The following commands are available:
4909 ;; Get rid of Outline menus, they are not needed
4910 ;; Need to do this here because define-derived-mode sets up
4911 ;; the keymap so late. Still, it is a waste to call this each time
4912 ;; we switch another buffer into org-mode.
4913 (if (featurep 'xemacs
)
4914 (when (boundp 'outline-mode-menu-heading
)
4915 ;; Assume this is Greg's port, it uses easymenu
4916 (easy-menu-remove outline-mode-menu-heading
)
4917 (easy-menu-remove outline-mode-menu-show
)
4918 (easy-menu-remove outline-mode-menu-hide
))
4919 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4920 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4921 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4923 (org-load-modules-maybe)
4924 (easy-menu-add org-org-menu
)
4925 (easy-menu-add org-tbl-menu
)
4926 (org-install-agenda-files-menu)
4927 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
4928 (add-to-invisibility-spec '(org-cwidth))
4929 (add-to-invisibility-spec '(org-hide-block . t
))
4930 (when (featurep 'xemacs
)
4931 (org-set-local 'line-move-ignore-invisible t
))
4932 (org-set-local 'outline-regexp org-outline-regexp
)
4933 (org-set-local 'outline-level
'org-outline-level
)
4934 (setq bidi-paragraph-direction
'left-to-right
)
4935 (when (and org-ellipsis
4936 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4937 (fboundp 'make-glyph-code
))
4938 (unless org-display-table
4939 (setq org-display-table
(make-display-table)))
4940 (set-display-table-slot
4943 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4945 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4946 (setq buffer-display-table org-display-table
))
4947 (org-set-regexps-and-options)
4948 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4949 ;; tag faces set outside customize.... force initialization.
4950 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4952 (org-set-local 'calc-embedded-open-mode
"# ")
4953 (modify-syntax-entry ?
@ "w")
4954 (if org-startup-truncated
(setq truncate-lines t
))
4955 (org-set-local 'font-lock-unfontify-region-function
4956 'org-unfontify-region
)
4957 ;; Activate before-change-function
4958 (org-set-local 'org-table-may-need-update t
)
4959 (org-add-hook 'before-change-functions
'org-before-change-function nil
4961 ;; Check for running clock before killing a buffer
4962 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4963 ;; Paragraphs and auto-filling
4964 (org-set-autofill-regexps)
4965 (setq indent-line-function
'org-indent-line-function
)
4966 (org-update-radio-target-regexp)
4967 ;; Beginning/end of defun
4968 (org-set-local 'beginning-of-defun-function
'org-beginning-of-defun
)
4969 (org-set-local 'end-of-defun-function
'org-end-of-defun
)
4970 ;; Next error for sparse trees
4971 (org-set-local 'next-error-function
'org-occur-next-match
)
4972 ;; Make sure dependence stuff works reliably, even for users who set it
4974 (if org-enforce-todo-dependencies
4975 (add-hook 'org-blocker-hook
4976 'org-block-todo-from-children-or-siblings-or-parent
)
4977 (remove-hook 'org-blocker-hook
4978 'org-block-todo-from-children-or-siblings-or-parent
))
4979 (if org-enforce-todo-checkbox-dependencies
4980 (add-hook 'org-blocker-hook
4981 'org-block-todo-from-checkboxes
)
4982 (remove-hook 'org-blocker-hook
4983 'org-block-todo-from-checkboxes
))
4985 ;; Comment characters
4986 (org-set-local 'comment-start
"#")
4987 (org-set-local 'comment-padding
" ")
4989 ;; Align options lines
4991 'align-mode-rules-list
4992 '((org-in-buffer-settings
4993 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4994 (modes .
'(org-mode)))))
4997 (org-set-local 'imenu-create-index-function
4998 'org-imenu-get-tree
)
5000 ;; Make isearch reveal context
5001 (if (or (featurep 'xemacs
)
5002 (not (boundp 'outline-isearch-open-invisible-function
)))
5003 ;; Emacs 21 and XEmacs make use of the hook
5004 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5005 ;; Emacs 22 deals with this through a special variable
5006 (org-set-local 'outline-isearch-open-invisible-function
5007 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5009 ;; Turn on org-beamer-mode?
5010 (and org-startup-with-beamer-mode
(org-beamer-mode 1))
5012 ;; Setup the pcomplete hooks
5013 (set (make-local-variable 'pcomplete-command-completion-function
)
5014 'org-pcomplete-initial
)
5015 (set (make-local-variable 'pcomplete-command-name-function
)
5016 'org-command-at-point
)
5017 (set (make-local-variable 'pcomplete-default-completion-function
)
5019 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5020 'org-parse-arguments
)
5021 (set (make-local-variable 'pcomplete-termination-string
) "")
5022 (set (make-local-variable 'face-remapping-alist
)
5023 '((default org-default
)))
5025 ;; If empty file that did not turn on org-mode automatically, make it to.
5026 (if (and org-insert-mode-line-in-empty-file
5027 (org-called-interactively-p 'any
)
5028 (= (point-min) (point-max)))
5029 (insert "# -*- mode: org -*-\n\n"))
5030 (unless org-inhibit-startup
5031 (when org-startup-align-all-tables
5032 (let ((bmp (buffer-modified-p)))
5033 (org-table-map-tables 'org-table-align
'quietly
)
5034 (set-buffer-modified-p bmp
)))
5035 (when org-startup-with-inline-images
5036 (org-display-inline-images))
5037 (when org-startup-indented
5038 (require 'org-indent
)
5039 (org-indent-mode 1))
5040 (unless org-inhibit-startup-visibility-stuff
5041 (org-set-startup-visibility))))
5043 (when (fboundp 'abbrev-table-put
)
5044 (abbrev-table-put org-mode-abbrev-table
5045 :parents
(list text-mode-abbrev-table
)))
5047 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5049 (defun org-current-time ()
5050 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5051 (if (> (car org-time-stamp-rounding-minutes
) 1)
5052 (let ((r (car org-time-stamp-rounding-minutes
))
5053 (time (decode-time)))
5055 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5060 "Return today date, considering `org-extend-today-until'."
5062 (time-subtract (current-time)
5063 (list 0 (* 3600 org-extend-today-until
) 0))))
5065 ;;;; Font-Lock stuff, including the activators
5067 (defvar org-mouse-map
(make-sparse-keymap))
5068 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5069 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5070 (when org-mouse-1-follows-link
5071 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5072 (when org-tab-follows-link
5073 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5074 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5076 (require 'font-lock
)
5078 (defconst org-non-link-chars
"]\t\n\r<>")
5079 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5080 "shell" "elisp" "doi" "message"))
5081 (defvar org-link-types-re nil
5082 "Matches a link that has a url-like prefix like \"http:\"")
5083 (defvar org-link-re-with-space nil
5084 "Matches a link with spaces, optional angular brackets around it.")
5085 (defvar org-link-re-with-space2 nil
5086 "Matches a link with spaces, optional angular brackets around it.")
5087 (defvar org-link-re-with-space3 nil
5088 "Matches a link with spaces, only for internal part in bracket links.")
5089 (defvar org-angle-link-re nil
5090 "Matches link with angular brackets, spaces are allowed.")
5091 (defvar org-plain-link-re nil
5092 "Matches plain link, without spaces.")
5093 (defvar org-bracket-link-regexp nil
5094 "Matches a link in double brackets.")
5095 (defvar org-bracket-link-analytic-regexp nil
5096 "Regular expression used to analyze links.
5097 Here is what the match groups contain after a match:
5103 (defvar org-bracket-link-analytic-regexp
++ nil
5104 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5105 (defvar org-any-link-re nil
5106 "Regular expression matching any link.")
5108 (defcustom org-match-sexp-depth
3
5109 "Number of stacked braces for sub/superscript matching.
5110 This has to be set before loading org.el to be effective."
5111 :group
'org-export-translation
; ??????????????????????????/
5114 (defun org-create-multibrace-regexp (left right n
)
5115 "Create a regular expression which will match a balanced sexp.
5116 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5117 as single character strings.
5118 The regexp returned will match the entire expression including the
5119 delimiters. It will also define a single group which contains the
5120 match except for the outermost delimiters. The maximum depth of
5121 stacked delimiters is N. Escaping delimiters is not possible."
5122 (let* ((nothing (concat "[^" left right
"]*?"))
5125 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5128 re
(concat re or next
)
5129 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5130 (concat left
"\\(" re
"\\)" right
)))
5132 (defvar org-match-substring-regexp
5134 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5135 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5137 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5139 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5140 "The regular expression matching a sub- or superscript.")
5142 (defvar org-match-substring-with-braces-regexp
5144 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5145 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5147 "The regular expression matching a sub- or superscript, forcing braces.")
5149 (defun org-make-link-regexps ()
5150 "Update the link regular expressions.
5151 This should be called after the variable `org-link-types' has changed."
5152 (setq org-link-types-re
5154 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5155 org-link-re-with-space
5157 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5158 "\\([^" org-non-link-chars
" ]"
5159 "[^" org-non-link-chars
"]*"
5160 "[^" org-non-link-chars
" ]\\)>?")
5161 org-link-re-with-space2
5163 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5164 "\\([^" org-non-link-chars
" ]"
5166 "[^" org-non-link-chars
" ]\\)>?")
5167 org-link-re-with-space3
5169 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5170 "\\([^" org-non-link-chars
" ]"
5174 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5175 "\\([^" org-non-link-chars
" ]"
5176 "[^" org-non-link-chars
"]*"
5180 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5181 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5182 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5183 org-bracket-link-regexp
5184 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5185 org-bracket-link-analytic-regexp
5188 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5191 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5193 org-bracket-link-analytic-regexp
++
5196 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5199 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5202 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5203 org-angle-link-re
"\\)\\|\\("
5204 org-plain-link-re
"\\)")))
5206 (org-make-link-regexps)
5208 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5209 "Regular expression for fast time stamp matching.")
5210 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5211 "Regular expression for fast time stamp matching.")
5212 (defconst org-ts-regexp0
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5213 "Regular expression matching time strings for analysis.
5214 This one does not require the space after the date, so it can be used
5215 on a string that terminates immediately after the date.")
5216 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5217 "Regular expression matching time strings for analysis.")
5218 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5219 "Regular expression matching time stamps, with groups.")
5220 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5221 "Regular expression matching time stamps (also [..]), with groups.")
5222 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5223 "Regular expression matching a time stamp range.")
5224 (defconst org-tr-regexp-both
5225 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5226 "Regular expression matching a time stamp range.")
5227 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5228 org-ts-regexp
"\\)?")
5229 "Regular expression matching a time stamp or time stamp range.")
5230 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5231 org-ts-regexp-both
"\\)?")
5232 "Regular expression matching a time stamp or time stamp range.
5233 The time stamps may be either active or inactive.")
5235 (defvar org-emph-face nil
)
5237 (defun org-do-emphasis-faces (limit)
5238 "Run through the buffer and add overlays to emphasised strings."
5240 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5241 (if (not (= (char-after (match-beginning 3))
5242 (char-after (match-beginning 4))))
5245 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5246 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5250 (org-remove-flyspell-overlays-in
5251 (match-beginning 0) (match-end 0)))
5252 (add-text-properties (match-beginning 2) (match-end 2)
5253 '(font-lock-multiline t org-emphasis t
))
5254 (when org-hide-emphasis-markers
5255 (add-text-properties (match-end 4) (match-beginning 5)
5256 '(invisible org-link
))
5257 (add-text-properties (match-beginning 3) (match-end 3)
5258 '(invisible org-link
)))))
5262 (defun org-emphasize (&optional char
)
5263 "Insert or change an emphasis, i.e. a font like bold or italic.
5264 If there is an active region, change that region to a new emphasis.
5265 If there is no region, just insert the marker characters and position
5266 the cursor between them.
5267 CHAR should be either the marker character, or the first character of the
5268 HTML tag associated with that emphasis. If CHAR is a space, the means
5269 to remove the emphasis of the selected region.
5270 If char is not given (for example in an interactive call) it
5271 will be prompted for."
5273 (let ((eal org-emphasis-alist
) e det
5274 (erc org-emphasis-regexp-components
)
5276 (string "") beg end move tag c s
)
5277 (if (org-region-active-p)
5278 (setq beg
(region-beginning) end
(region-end)
5279 string
(buffer-substring beg end
))
5282 (while (setq e
(pop eal
))
5283 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5285 (push (cons c
(string-to-char (car e
))) det
)
5286 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5287 (substring tag
1)))))
5288 (setq det
(nreverse det
))
5290 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5291 (setq char
(read-char-exclusive)))
5292 (setq char
(or (cdr (assoc char det
)) char
))
5293 (if (equal char ?\
)
5294 (setq s
"" move nil
)
5295 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5296 (error "No such emphasis marker: \"%c\"" char
))
5297 (setq s
(char-to-string char
)))
5298 (while (and (> (length string
) 1)
5299 (equal (substring string
0 1) (substring string -
1))
5300 (assoc (substring string
0 1) org-emphasis-alist
))
5301 (setq string
(substring string
1 -
1)))
5302 (setq string
(concat s string s
))
5303 (if beg
(delete-region beg end
))
5305 (string-match (concat "[" (nth 0 erc
) "\n]")
5306 (char-to-string (char-before (point)))))
5309 (string-match (concat "[" (nth 1 erc
) "\n]")
5310 (char-to-string (char-after (point)))))
5311 (insert " ") (backward-char 1))
5313 (and move
(backward-char 1))))
5315 (defconst org-nonsticky-props
5316 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
5318 (defsubst org-rear-nonsticky-at
(pos)
5319 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5321 (defun org-activate-plain-links (limit)
5322 "Run through the buffer and add overlays to links."
5325 (if (re-search-forward org-plain-link-re limit t
)
5327 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5328 (setq f
(get-text-property (match-beginning 0) 'face
))
5329 (if (or (eq f
'org-tag
)
5330 (and (listp f
) (memq 'org-tag f
)))
5332 (add-text-properties (match-beginning 0) (match-end 0)
5333 (list 'mouse-face
'highlight
5335 'keymap org-mouse-map
))
5336 (org-rear-nonsticky-at (match-end 0)))
5339 (defun org-activate-code (limit)
5340 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5342 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5343 (remove-text-properties (match-beginning 0) (match-end 0)
5344 '(display t invisible t intangible t
))
5347 (defcustom org-src-fontify-natively nil
5348 "When non-nil, fontify code in code blocks."
5350 :group
'org-appearance
5353 (defun org-fontify-meta-lines-and-blocks (limit)
5355 (org-fontify-meta-lines-and-blocks-1 limit
)
5356 (error (message "org-mode fontification error"))))
5358 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5359 "Fontify #+ lines and blocks, in the correct ways."
5360 (let ((case-fold-search t
))
5361 (if (re-search-forward
5362 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5364 (let ((beg (match-beginning 0))
5365 (block-start (match-end 0))
5367 (lang (match-string 7))
5368 (beg1 (line-beginning-position 2))
5369 (dc1 (downcase (match-string 2)))
5370 (dc3 (downcase (match-string 3)))
5371 end end1 quoting block-type ovl
)
5373 ((member dc1
'("html:" "ascii:" "latex:" "docbook:"))
5374 ;; a single line of backend-specific content
5375 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5376 (remove-text-properties (match-beginning 0) (match-end 0)
5377 '(display t invisible t intangible t
))
5378 (add-text-properties (match-beginning 1) (match-end 3)
5379 '(font-lock-fontified t face org-meta-line
))
5380 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5381 '(font-lock-fontified t face org-block
))
5382 ; for backend-specific code
5384 ((and (match-end 4) (equal dc3
"begin"))
5386 (setq block-type
(downcase (match-string 5))
5387 quoting
(member block-type org-protecting-blocks
))
5388 (when (re-search-forward
5389 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5390 nil t
) ;; on purpose, we look further than LIMIT
5391 (setq end
(match-end 0) end1
(1- (match-beginning 0)))
5392 (setq block-end
(match-beginning 0))
5394 (remove-text-properties beg end
5395 '(display t invisible t intangible t
)))
5396 (add-text-properties
5398 '(font-lock-fontified t font-lock-multiline t
))
5399 (add-text-properties beg beg1
'(face org-meta-line
))
5400 (add-text-properties end1
(min (point-max) (1+ end
))
5401 '(face org-meta-line
)) ; for end_src
5403 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5404 (org-src-font-lock-fontify-block lang block-start block-end
)
5405 ;; remove old background overlays
5407 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5408 (delete-overlay ov
)))
5409 (overlays-at (/ (+ beg1 block-end
) 2)))
5410 ;; add a background overlay
5411 (setq ovl
(make-overlay beg1 block-end
))
5412 (overlay-put ovl
'face
'org-block-background
)
5413 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5415 (add-text-properties beg1
(min (point-max) (1+ end1
))
5416 '(face org-block
))) ; end of source block
5417 ((not org-fontify-quote-and-verse-blocks
))
5418 ((string= block-type
"quote")
5419 (add-text-properties beg1
(1+ end1
) '(face org-quote
)))
5420 ((string= block-type
"verse")
5421 (add-text-properties beg1
(1+ end1
) '(face org-verse
))))
5422 (add-text-properties beg beg1
'(face org-block-begin-line
))
5423 (add-text-properties (1+ end
) (1+ end1
) '(face org-block-end-line
))
5425 ((member dc1
'("title:" "author:" "email:" "date:"))
5426 (add-text-properties
5428 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5429 '(font-lock-fontified t invisible t
)
5430 '(font-lock-fontified t face org-document-info-keyword
)))
5431 (add-text-properties
5432 (match-beginning 6) (match-end 6)
5433 (if (string-equal dc1
"title:")
5434 '(font-lock-fontified t face org-document-title
)
5435 '(font-lock-fontified t face org-document-info
))))
5436 ((not (member (char-after beg
) '(?\ ?
\t)))
5437 ;; just any other in-buffer setting, but not indented
5438 (add-text-properties
5439 beg
(1+ (match-end 0))
5440 '(font-lock-fontified t face org-meta-line
))
5442 ((or (member dc1
'("begin:" "end:" "caption:" "label:"
5443 "orgtbl:" "tblfm:" "tblname:" "results:"
5444 "call:" "header:" "headers:" "name:"))
5445 (and (match-end 4) (equal dc3
"attr")))
5446 (add-text-properties
5448 '(font-lock-fontified t face org-meta-line
))
5450 ((member dc3
'(" " ""))
5451 (add-text-properties
5453 '(font-lock-fontified t face font-lock-comment-face
)))
5456 (defun org-activate-angle-links (limit)
5457 "Run through the buffer and add overlays to links."
5458 (if (re-search-forward org-angle-link-re limit t
)
5460 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5461 (add-text-properties (match-beginning 0) (match-end 0)
5462 (list 'mouse-face
'highlight
5463 'keymap org-mouse-map
))
5464 (org-rear-nonsticky-at (match-end 0))
5467 (defun org-activate-footnote-links (limit)
5468 "Run through the buffer and add overlays to footnotes."
5469 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5471 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5472 (org-remove-flyspell-overlays-in beg end
)
5473 (add-text-properties beg end
5474 (list 'mouse-face
'highlight
5475 'keymap org-mouse-map
5477 (if (= (point-at-bol) beg
)
5478 "Footnote definition"
5479 "Footnote reference")
5480 'font-lock-fontified t
5481 'font-lock-multiline t
5482 'face
'org-footnote
))))))
5484 (defun org-activate-bracket-links (limit)
5485 "Run through the buffer and add overlays to bracketed links."
5486 (if (re-search-forward org-bracket-link-regexp limit t
)
5487 (let* ((help (concat "LINK: "
5488 (org-match-string-no-properties 1)))
5489 ;; FIXME: above we should remove the escapes.
5490 ;; but that requires another match, protecting match data,
5491 ;; a lot of overhead for font-lock.
5492 (ip (org-maybe-intangible
5493 (list 'invisible
'org-link
5494 'keymap org-mouse-map
'mouse-face
'highlight
5495 'font-lock-multiline t
'help-echo help
)))
5496 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5497 'font-lock-multiline t
'help-echo help
)))
5498 ;; We need to remove the invisible property here. Table narrowing
5499 ;; may have made some of this invisible.
5500 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5501 (remove-text-properties (match-beginning 0) (match-end 0)
5505 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5506 (org-rear-nonsticky-at (match-beginning 3))
5507 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5508 (org-rear-nonsticky-at (match-end 3))
5509 (add-text-properties (match-end 3) (match-end 0) ip
)
5510 (org-rear-nonsticky-at (match-end 0)))
5511 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5512 (org-rear-nonsticky-at (match-beginning 1))
5513 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5514 (org-rear-nonsticky-at (match-end 1))
5515 (add-text-properties (match-end 1) (match-end 0) ip
)
5516 (org-rear-nonsticky-at (match-end 0)))
5519 (defun org-activate-dates (limit)
5520 "Run through the buffer and add overlays to dates."
5521 (if (re-search-forward org-tsr-regexp-both limit t
)
5523 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5524 (add-text-properties (match-beginning 0) (match-end 0)
5525 (list 'mouse-face
'highlight
5526 'keymap org-mouse-map
))
5527 (org-rear-nonsticky-at (match-end 0))
5528 (when org-display-custom-times
5530 (org-display-custom-time (match-beginning 3) (match-end 3)))
5531 (org-display-custom-time (match-beginning 1) (match-end 1)))
5534 (defvar org-target-link-regexp nil
5535 "Regular expression matching radio targets in plain text.")
5536 (make-variable-buffer-local 'org-target-link-regexp
)
5537 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5538 "Regular expression matching a link target.")
5539 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5540 "Regular expression matching a radio target.")
5541 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5542 "Regular expression matching any target.")
5544 (defun org-activate-target-links (limit)
5545 "Run through the buffer and add overlays to target matches."
5546 (when org-target-link-regexp
5547 (let ((case-fold-search t
))
5548 (if (re-search-forward org-target-link-regexp limit t
)
5550 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5551 (add-text-properties (match-beginning 0) (match-end 0)
5552 (list 'mouse-face
'highlight
5553 'keymap org-mouse-map
5554 'help-echo
"Radio target link"
5555 'org-linked-text t
))
5556 (org-rear-nonsticky-at (match-end 0))
5559 (defun org-update-radio-target-regexp ()
5560 "Find all radio targets in this file and update the regular expression."
5562 (when (memq 'radio org-activate-links
)
5563 (setq org-target-link-regexp
5564 (org-make-target-link-regexp (org-all-targets 'radio
)))
5565 (org-restart-font-lock)))
5567 (defun org-hide-wide-columns (limit)
5569 (setq s
(text-property-any (point) (or limit
(point-max))
5572 (setq e
(next-single-property-change s
'org-cwidth
))
5573 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5577 (defvar org-latex-and-specials-regexp nil
5578 "Regular expression for highlighting export special stuff.")
5579 (defvar org-match-substring-regexp
)
5580 (defvar org-match-substring-with-braces-regexp
)
5582 ;; This should be with the exporter code, but we also use if for font-locking
5583 (defconst org-export-html-special-string-regexps
5584 '(("\\\\-" .
"­")
5585 ("---\\([^-]\\)" .
"—\\1")
5586 ("--\\([^-]\\)" .
"–\\1")
5587 ("\\.\\.\\." .
"…"))
5588 "Regular expressions for special string conversion.")
5591 (defun org-compute-latex-and-specials-regexp ()
5592 "Compute regular expression for stuff treated specially by exporters."
5593 (if (not org-highlight-latex-fragments-and-specials
)
5594 (org-set-local 'org-latex-and-specials-regexp nil
)
5597 ((matchers (plist-get org-format-latex-options
:matchers
))
5598 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5599 org-latex-regexps
)))
5600 (org-export-allow-BIND nil
)
5601 (options (org-combine-plists (org-default-export-plist)
5602 (org-infile-export-plist)))
5603 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5604 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5605 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5606 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5607 (org-export-with-special-strings (plist-get options
:special-strings
))
5610 ((equal org-export-with-sub-superscripts
'{})
5611 (list org-match-substring-with-braces-regexp
))
5612 (org-export-with-sub-superscripts
5613 (list org-match-substring-regexp
))
5616 (if org-export-with-LaTeX-fragments
5617 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5619 (if org-export-with-TeX-macros
5620 (list (concat "\\\\"
5626 (append org-entities-user
5628 (if (boundp 'org-latex-entities
)
5630 (or (car-safe x
) x
))
5635 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5636 (re-special (if org-export-with-special-strings
5637 (mapcar (lambda (x) (car x
))
5638 org-export-html-special-string-regexps
)))
5642 (if org-export-html-expand
"@<[^>\n]+>")
5645 'org-latex-and-specials-regexp
5646 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5649 (defun org-do-latex-and-special-faces (limit)
5650 "Run through the buffer and add overlays to links."
5651 (when org-latex-and-specials-regexp
5653 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5655 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5657 '(org-code org-verbatim underline
)))
5660 d
(cond ((member (char-after (1+ (match-beginning 0)))
5663 (font-lock-prepend-text-property
5664 (+ d
(match-beginning 0)) (match-end 0)
5665 'face
'org-latex-and-export-specials
)
5666 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5667 '(font-lock-multiline t
)))))
5670 (defun org-restart-font-lock ()
5671 "Restart `font-lock-mode', to force refontification."
5672 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5674 (font-lock-mode 1)))
5676 (defun org-all-targets (&optional radio
)
5677 "Return a list of all targets in this file.
5678 With optional argument RADIO, only find radio targets."
5679 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5682 (goto-char (point-min))
5683 (while (re-search-forward re nil t
)
5684 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5687 (defun org-make-target-link-regexp (targets)
5688 "Make regular expression matching all strings in TARGETS.
5689 The regular expression finds the targets also if there is a line break
5696 (setq x
(regexp-quote x
))
5697 (while (string-match " +" x
)
5698 (setq x
(replace-match "\\s-+" t t x
)))
5704 (defun org-activate-tags (limit)
5705 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5707 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5708 (add-text-properties (match-beginning 1) (match-end 1)
5709 (list 'mouse-face
'highlight
5710 'keymap org-mouse-map
))
5711 (org-rear-nonsticky-at (match-end 1))
5714 (defun org-outline-level ()
5715 "Compute the outline level of the heading at point.
5716 This function assumes that the cursor is at the beginning of a line matched
5717 by `outline-regexp'. Otherwise it returns garbage.
5718 If this is called at a normal headline, the level is the number of stars.
5719 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5721 (looking-at org-outline-regexp
)
5722 (1- (- (match-end 0) (match-beginning 0)))))
5724 (defvar org-font-lock-keywords nil
)
5726 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5727 "Regular expression matching a property line.")
5729 (defvar org-font-lock-hook nil
5730 "Functions to be called for special font lock stuff.")
5732 (defvar org-font-lock-set-keywords-hook nil
5733 "Functions that can manipulate `org-font-lock-extra-keywords'.
5734 This is calles after `org-font-lock-extra-keywords' is defined, but before
5735 it is installed to be used by font lock. This can be useful if something
5736 needs to be inserted at a specific position in the font-lock sequence.")
5738 (defun org-font-lock-hook (limit)
5739 (run-hook-with-args 'org-font-lock-hook limit
))
5741 (defun org-set-font-lock-defaults ()
5742 (let* ((em org-fontify-emphasized-text
)
5743 (lk org-activate-links
)
5744 (org-font-lock-extra-keywords
5747 '(org-font-lock-hook)
5749 `(,(if org-fontify-whole-heading-line
5750 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5751 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5752 (1 (org-get-level-face 1))
5753 (2 (org-get-level-face 2))
5754 (3 (org-get-level-face 3)))
5756 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5759 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5760 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5761 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5762 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5764 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5765 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5767 (list org-property-re
5768 '(1 'org-special-keyword t
)
5769 '(3 'org-property-value t
))
5771 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5772 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5773 (if (memq 'plain lk
) '(org-activate-plain-links))
5774 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5775 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5776 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5777 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5778 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5779 '(org-hide-wide-columns (0 nil append
))
5781 (list (format org-heading-keyword-regexp-format
5783 '(2 (org-get-todo-face 2) t
))
5785 (if org-fontify-done-headline
5786 (list (format org-heading-keyword-regexp-format
5789 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5791 '(2 'org-headline-done t
))
5794 '(org-font-lock-add-priority-faces)
5796 '(org-font-lock-add-tag-faces)
5798 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5799 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5800 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5801 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5804 (if (featurep 'xemacs
)
5805 '(org-do-emphasis-faces (0 nil append
))
5806 '(org-do-emphasis-faces)))
5808 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5809 1 'org-checkbox prepend
)
5810 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5811 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5812 (0 (org-get-checkbox-statistics-face) t
)))
5813 ;; Description list items
5814 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5816 ;; ARCHIVEd headings
5818 org-outline-regexp-bol
5819 "\\(.*:" org-archive-tag
":.*\\)")
5820 '(1 'org-archived prepend
))
5822 '(org-do-latex-and-special-faces)
5823 '(org-fontify-entities)
5824 '(org-raise-scripts)
5826 '(org-activate-code (1 'org-code t
))
5828 (list (format org-heading-keyword-regexp-format
5830 org-comment-string
"\\|" org-quote-string
5832 '(2 'org-special-keyword t
))
5833 '("^#.*" (0 'font-lock-comment-face t
))
5834 ;; Blocks and meta lines
5835 '(org-fontify-meta-lines-and-blocks)
5837 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5838 (run-hooks 'org-font-lock-set-keywords-hook
)
5839 ;; Now set the full font-lock-keywords
5840 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5841 (org-set-local 'font-lock-defaults
5842 '(org-font-lock-keywords t nil nil backward-paragraph
))
5843 (kill-local-variable 'font-lock-keywords
) nil
))
5845 (defun org-toggle-pretty-entities ()
5846 "Toggle the composition display of entities as UTF8 characters."
5848 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5849 (org-restart-font-lock)
5850 (if org-pretty-entities
5851 (message "Entities are displayed as UTF8 characers")
5854 (org-decompose-region (point-min) (point-max))
5855 (message "Entities are displayed plain"))))
5857 (defun org-fontify-entities (limit)
5858 "Find an entity to fontify."
5860 (when org-pretty-entities
5862 (while (re-search-forward
5863 "\\\\\\(frac[13][24]\\|[a-zA-Z]+\\)\\($\\|[^[:alpha:]\n]\\)"
5865 (if (and (not (org-in-indented-comment-line))
5866 (setq ee
(org-entity-get (match-string 1)))
5867 (= (length (nth 6 ee
)) 1))
5869 (add-text-properties
5870 (match-beginning 0) (match-end 1)
5871 (list 'font-lock-fontified t
))
5872 (compose-region (match-beginning 0) (match-end 1)
5878 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5879 "Fontify string S like in Org-mode."
5882 (let ((org-odd-levels-only odd-levels
))
5884 (font-lock-fontify-buffer)
5890 (defun org-get-level-face (n)
5891 "Get the right face for match N in font-lock matching of headlines."
5892 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5893 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5894 (if org-cycle-level-faces
5895 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5896 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
5898 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5900 (t (if org-level-color-stars-only nil org-f
))))
5903 (defun org-get-todo-face (kwd)
5904 "Get the right face for a TODO keyword KWD.
5905 If KWD is a number, get the corresponding match group."
5906 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5907 (or (org-face-from-face-or-color
5908 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5909 (and (member kwd org-done-keywords
) 'org-done
)
5912 (defun org-face-from-face-or-color (context inherit face-or-color
)
5913 "Create a face list that inherits INHERIT, but sets the foreground color.
5914 When FACE-OR-COLOR is not a string, just return it."
5915 (if (stringp face-or-color
)
5916 (list :inherit inherit
5917 (cdr (assoc context org-faces-easy-properties
))
5921 (defun org-font-lock-add-tag-faces (limit)
5922 "Add the special tag faces."
5923 (when (and org-tag-faces org-tags-special-faces-re
)
5924 (while (re-search-forward org-tags-special-faces-re limit t
)
5925 (add-text-properties (match-beginning 1) (match-end 1)
5926 (list 'face
(org-get-tag-face 1)
5927 'font-lock-fontified t
))
5928 (backward-char 1))))
5930 (defun org-font-lock-add-priority-faces (limit)
5931 "Add the special priority faces."
5932 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
5933 (add-text-properties
5934 (match-beginning 0) (match-end 0)
5935 (list 'face
(or (org-face-from-face-or-color
5936 'priority
'org-special-keyword
5937 (cdr (assoc (char-after (match-beginning 1))
5938 org-priority-faces
)))
5939 'org-special-keyword
)
5940 'font-lock-fontified t
))))
5942 (defun org-get-tag-face (kwd)
5943 "Get the right face for a TODO keyword KWD.
5944 If KWD is a number, get the corresponding match group."
5945 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5946 (or (org-face-from-face-or-color
5947 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
5950 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
5951 "Remove fontification and activation overlays from links."
5952 (font-lock-default-unfontify-region beg end
)
5953 (let* ((buffer-undo-list t
)
5954 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
5955 (inhibit-modification-hooks t
)
5956 deactivate-mark buffer-file-name buffer-file-truename
)
5957 (org-decompose-region beg end
)
5958 (remove-text-properties beg end
5959 '(mouse-face t keymap t org-linked-text t
5960 invisible t intangible t
5961 org-no-flyspell t org-emphasis t
))
5962 (org-remove-font-lock-display-properties beg end
)))
5964 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
5965 ((raise 0.3) (height 0.7))
5968 "Display properties for showing superscripts and subscripts.")
5970 (defun org-remove-font-lock-display-properties (beg end
)
5971 "Remove specific display properties that have been added by font lock.
5972 The will remove the raise properties that are used to show superscripts
5976 (setq next
(next-single-property-change beg
'display nil end
)
5977 prop
(get-text-property beg
'display
))
5978 (if (member prop org-script-display
)
5979 (put-text-property beg next
'display nil
))
5982 (defun org-raise-scripts (limit)
5983 "Add raise properties to sub/superscripts."
5984 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
5985 (if (re-search-forward
5986 (if (eq org-use-sub-superscripts t
)
5987 org-match-substring-regexp
5988 org-match-substring-with-braces-regexp
)
5990 (let* ((pos (point)) table-p comment-p
5991 (mpos (match-beginning 3))
5992 (emph-p (get-text-property mpos
'org-emphasis
))
5993 (link-p (get-text-property mpos
'mouse-face
))
5994 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
5995 (goto-char (point-at-bol))
5996 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
5997 comment-p
(org-looking-at-p "[ \t]*#"))
5999 ;; FIXME: Should we go back one character here, for a_b^c
6000 ;; (goto-char (1- pos)) ;????????????????????
6001 (if (or comment-p emph-p link-p keyw-p
)
6003 (put-text-property (match-beginning 3) (match-end 0)
6005 (if (equal (char-after (match-beginning 2)) ?^
)
6006 (nth (if table-p
3 1) org-script-display
)
6007 (nth (if table-p
2 0) org-script-display
)))
6008 (add-text-properties (match-beginning 2) (match-end 2)
6010 'org-dwidth t
'org-dwidth-n
1))
6011 (if (and (eq (char-after (match-beginning 3)) ?
{)
6012 (eq (char-before (match-end 3)) ?
}))
6014 (add-text-properties
6015 (match-beginning 3) (1+ (match-beginning 3))
6016 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6017 (add-text-properties
6018 (1- (match-end 3)) (match-end 3)
6019 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6022 ;;;; Visibility cycling, including org-goto and indirect buffer
6026 (defvar org-cycle-global-status nil
)
6027 (make-variable-buffer-local 'org-cycle-global-status
)
6028 (defvar org-cycle-subtree-status nil
)
6029 (make-variable-buffer-local 'org-cycle-subtree-status
)
6033 (defvar org-inlinetask-min-level
)
6035 (defun org-cycle (&optional arg
)
6036 "TAB-action and visibility cycling for Org-mode.
6038 This is the command invoked in Org-mode by the TAB key. Its main purpose
6039 is outline visibility cycling, but it also invokes other actions
6040 in special contexts.
6042 - When this function is called with a prefix argument, rotate the entire
6043 buffer through 3 states (global cycling)
6044 1. OVERVIEW: Show only top-level headlines.
6045 2. CONTENTS: Show all headlines of all levels, but no body text.
6046 3. SHOW ALL: Show everything.
6047 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6048 determined by the variable `org-startup-folded', and by any VISIBILITY
6049 properties in the buffer.
6050 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6051 including any drawers.
6053 - When inside a table, re-align the table and move to the next field.
6055 - When point is at the beginning of a headline, rotate the subtree started
6056 by this line through 3 different states (local cycling)
6057 1. FOLDED: Only the main headline is shown.
6058 2. CHILDREN: The main headline and the direct children are shown.
6059 From this state, you can move to one of the children
6060 and zoom in further.
6061 3. SUBTREE: Show the entire subtree, including body text.
6062 If there is no subtree, switch directly from CHILDREN to FOLDED.
6064 - When point is at the beginning of an empty headline and the variable
6065 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6066 of the headline by demoting and promoting it to likely levels. This
6067 speeds up creation document structure by pressing TAB once or several
6068 times right after creating a new headline.
6070 - When there is a numeric prefix, go up to a heading with level ARG, do
6071 a `show-subtree' and return to the previous cursor position. If ARG
6072 is negative, go up that many levels.
6074 - When point is not at the beginning of a headline, execute the global
6075 binding for TAB, which is re-indenting the line. See the option
6076 `org-cycle-emulate-tab' for details.
6078 - Special case: if point is at the beginning of the buffer and there is
6079 no headline in line 1, this function will act as if called with prefix arg
6080 (C-u TAB, same as S-TAB) also when called without prefix arg.
6081 But only if also the variable `org-cycle-global-at-bob' is t."
6083 (org-load-modules-maybe)
6084 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6085 (and org-cycle-level-after-item
/entry-creation
6086 (or (org-cycle-level)
6087 (org-cycle-item-indentation))))
6089 (or org-cycle-max-level
6090 (and (boundp 'org-inlinetask-min-level
)
6091 org-inlinetask-min-level
6092 (1- org-inlinetask-min-level
))))
6093 (nstars (and limit-level
6094 (if org-odd-levels-only
6095 (and limit-level
(1- (* limit-level
2)))
6098 (if (not (eq major-mode
'org-mode
))
6100 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6101 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6102 (not (looking-at org-outline-regexp
))))
6105 (delq 'org-optimize-window-after-visibility-change
6106 (copy-sequence org-cycle-hook
))
6110 (if (or bob-special
(equal arg
'(4)))
6111 ;; special case: use global cycling
6117 (setq last-command
'dummy
)
6118 (org-set-startup-visibility)
6119 (message "Startup visibility, plus VISIBILITY properties"))
6123 (message "Entire buffer visible, including drawers"))
6125 ;; Table: enter it or move to the next field.
6126 ((org-at-table-p 'any
)
6127 (if (org-at-table.el-p
)
6128 (message "Use C-c ' to edit table.el tables")
6129 (if arg
(org-table-edit-field t
)
6130 (org-table-justify-field-maybe)
6131 (call-interactively 'org-table-next-field
))))
6133 ((run-hook-with-args-until-success
6134 'org-tab-after-check-for-table-hook
))
6136 ;; Global cycling: delegate to `org-cycle-internal-global'.
6137 ((eq arg t
) (org-cycle-internal-global))
6139 ;; Drawers: delegate to `org-flag-drawer'.
6140 ((and org-drawers org-drawer-regexp
6142 (beginning-of-line 1)
6143 (looking-at org-drawer-regexp
)))
6144 (org-flag-drawer ; toggle block visibility
6145 (not (get-char-property (match-end 0) 'invisible
))))
6147 ;; Show-subtree, ARG levels up from here.
6150 (org-back-to-heading)
6151 (outline-up-heading (if (< arg
0) (- arg
)
6152 (- (funcall outline-level
) arg
)))
6153 (org-show-subtree)))
6155 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6156 ((and (featurep 'org-inlinetask
)
6157 (org-inlinetask-at-task-p)
6158 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6159 (org-inlinetask-toggle-visibility))
6161 ((org-try-cdlatex-tab))
6163 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6164 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6165 (save-excursion (beginning-of-line 1)
6166 (looking-at org-outline-regexp
)))
6167 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6168 (org-cycle-internal-local))
6170 ;; From there: TAB emulation and template completion.
6171 (buffer-read-only (org-back-to-heading))
6173 ((run-hook-with-args-until-success
6174 'org-tab-after-check-for-cycling-hook
))
6176 ((org-try-structure-completion))
6178 ((run-hook-with-args-until-success
6179 'org-tab-before-tab-emulation-hook
))
6181 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6183 (not (looking-at org-outline-regexp
))))
6184 (call-interactively (global-key-binding "\t")))
6186 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6187 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6188 (or (and (eq org-cycle-emulate-tab
'white
)
6189 (= (match-end 0) (point-at-eol)))
6190 (and (eq org-cycle-emulate-tab
'whitestart
)
6191 (>= (match-end 0) pos
))))
6193 (eq org-cycle-emulate-tab t
))
6194 (call-interactively (global-key-binding "\t")))
6197 (org-back-to-heading)
6200 (defun org-cycle-internal-global ()
6201 "Do the global cycling action."
6203 ((and (eq last-command this-command
)
6204 (eq org-cycle-global-status
'overview
))
6205 ;; We just created the overview - now do table of contents
6206 ;; This can be slow in very large buffers, so indicate action
6207 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6208 (message "CONTENTS...")
6210 (message "CONTENTS...done")
6211 (setq org-cycle-global-status
'contents
)
6212 (run-hook-with-args 'org-cycle-hook
'contents
))
6214 ((and (eq last-command this-command
)
6215 (eq org-cycle-global-status
'contents
))
6216 ;; We just showed the table of contents - now show everything
6217 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6219 (message "SHOW ALL")
6220 (setq org-cycle-global-status
'all
)
6221 (run-hook-with-args 'org-cycle-hook
'all
))
6224 ;; Default action: go to overview
6225 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6227 (message "OVERVIEW")
6228 (setq org-cycle-global-status
'overview
)
6229 (run-hook-with-args 'org-cycle-hook
'overview
))))
6231 (defun org-cycle-internal-local ()
6232 "Do the local cycling action."
6233 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6234 ;; First, determine end of headline (EOH), end of subtree or item
6235 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6240 (setq struct
(org-list-struct))
6241 (setq eoh
(point-at-eol))
6242 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6243 (setq has-children
(org-list-has-child-p (point) struct
)))
6244 (org-back-to-heading)
6245 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6246 (setq eos
(save-excursion
6247 (org-end-of-subtree t
)
6249 (skip-chars-forward " \t\n"))
6250 (if (eobp) (point) (1- (point)))))
6253 (let ((level (funcall outline-level
)))
6254 (outline-next-heading)
6255 (and (org-at-heading-p t
)
6256 (> (funcall outline-level
) level
))))
6258 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6259 ;; Determine end invisible part of buffer (EOL)
6260 (beginning-of-line 2)
6261 ;; XEmacs doesn't have `next-single-char-property-change'
6262 (if (featurep 'xemacs
)
6263 (while (and (not (eobp)) ;; this is like `next-line'
6264 (get-char-property (1- (point)) 'invisible
))
6265 (beginning-of-line 2))
6266 (while (and (not (eobp)) ;; this is like `next-line'
6267 (get-char-property (1- (point)) 'invisible
))
6268 (goto-char (next-single-char-property-change (point) 'invisible
))
6269 (and (eolp) (beginning-of-line 2))))
6271 ;; Find out what to do next and set `this-command'
6274 ;; Nothing is hidden behind this heading
6275 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
6276 (message "EMPTY ENTRY")
6277 (setq org-cycle-subtree-status nil
)
6280 (outline-next-heading)
6281 (if (outline-invisible-p) (org-flag-heading nil
))))
6282 ((and (or (>= eol eos
)
6283 (not (string-match "\\S-" (buffer-substring eol eos
))))
6285 (not (setq children-skipped
6286 org-cycle-skip-children-state-if-no-children
))))
6287 ;; Entire subtree is hidden in one line: children view
6288 (run-hook-with-args 'org-pre-cycle-hook
'children
)
6290 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6293 ;; Fold every list in subtree to top-level items.
6294 (when (eq org-cycle-include-plain-lists
'integrate
)
6296 (org-back-to-heading)
6297 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6298 (beginning-of-line 1)
6299 (let* ((struct (org-list-struct))
6300 (prevs (org-list-prevs-alist struct
))
6301 (end (org-list-get-bottom-point struct
)))
6302 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6303 (org-list-get-all-items (point) struct prevs
))
6304 (goto-char end
))))))
6305 (message "CHILDREN")
6308 (outline-next-heading)
6309 (if (outline-invisible-p) (org-flag-heading nil
)))
6310 (setq org-cycle-subtree-status
'children
)
6311 (run-hook-with-args 'org-cycle-hook
'children
))
6312 ((or children-skipped
6313 (and (eq last-command this-command
)
6314 (eq org-cycle-subtree-status
'children
)))
6315 ;; We just showed the children, or no children are there,
6316 ;; now show everything.
6317 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
6318 (outline-flag-region eoh eos nil
)
6319 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6320 (setq org-cycle-subtree-status
'subtree
)
6321 (run-hook-with-args 'org-cycle-hook
'subtree
))
6323 ;; Default action: hide the subtree.
6324 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6325 (outline-flag-region eoh eos t
)
6327 (setq org-cycle-subtree-status
'folded
)
6328 (run-hook-with-args 'org-cycle-hook
'folded
)))))
6331 (defun org-global-cycle (&optional arg
)
6332 "Cycle the global visibility. For details see `org-cycle'.
6333 With \\[universal-argument] prefix arg, switch to startup visibility.
6334 With a numeric prefix, show all headlines up to that level."
6336 (let ((org-cycle-include-plain-lists
6337 (if (eq major-mode
'org-mode
) org-cycle-include-plain-lists nil
)))
6341 (hide-sublevels arg
)
6342 (setq org-cycle-global-status
'contents
))
6344 (org-set-startup-visibility)
6345 (message "Startup visibility, plus VISIBILITY properties."))
6347 (org-cycle '(4))))))
6349 (defun org-set-startup-visibility ()
6350 "Set the visibility required by startup options and properties."
6352 ((eq org-startup-folded t
)
6354 ((eq org-startup-folded
'content
)
6355 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6356 (org-cycle '(4)) (org-cycle '(4)))))
6357 (unless (eq org-startup-folded
'showeverything
)
6358 (if org-hide-block-startup
(org-hide-block-all))
6359 (org-set-visibility-according-to-property 'no-cleanup
)
6360 (org-cycle-hide-archived-subtrees 'all
)
6361 (org-cycle-hide-drawers 'all
)
6362 (org-cycle-show-empty-lines t
)))
6364 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6365 "Switch subtree visibilities according to :VISIBILITY: property."
6367 (let (org-show-entry-below state
)
6369 (goto-char (point-min))
6370 (while (re-search-forward
6371 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6373 (setq state
(match-string 1))
6375 (org-back-to-heading t
)
6379 ((equal state
'("fold" "folded"))
6381 ((equal state
"children")
6382 (org-show-hidden-entry)
6384 ((equal state
"content")
6387 (org-narrow-to-subtree)
6389 ((member state
'("all" "showall"))
6392 (org-cycle-hide-archived-subtrees 'all
)
6393 (org-cycle-hide-drawers 'all
)
6394 (org-cycle-show-empty-lines 'all
)))))
6396 ;; This function uses outline-regexp instead of the more fundamental
6397 ;; org-outline-regexp so that org-cycle-global works outside of Org
6398 ;; buffers, where outline-regexp is needed.
6399 (defun org-overview ()
6400 "Switch to overview mode, showing only top-level headlines.
6401 Really, this shows all headlines with level equal or greater than the level
6402 of the first headline in the buffer. This is important, because if the
6403 first headline is not level one, then (hide-sublevels 1) gives confusing
6406 (let ((level (save-excursion
6407 (goto-char (point-min))
6408 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6410 (goto-char (match-beginning 0))
6411 (funcall outline-level
))))))
6412 (and level
(hide-sublevels level
))))
6414 (defun org-content (&optional arg
)
6415 "Show all headlines in the buffer, like a table of contents.
6416 With numerical argument N, show content up to level N."
6419 ;; Visit all headings and show their offspring
6420 (and (integerp arg
) (org-overview))
6421 (goto-char (point-max))
6423 (while (and (progn (condition-case nil
6424 (outline-previous-visible-heading 1)
6425 (error (goto-char (point-min))))
6427 (looking-at org-outline-regexp
))
6429 (show-children (1- arg
))
6431 (if (bobp) (throw 'exit nil
))))))
6434 (defun org-optimize-window-after-visibility-change (state)
6435 "Adjust the window after a change in outline visibility.
6436 This function is the default value of the hook `org-cycle-hook'."
6437 (when (get-buffer-window (current-buffer))
6439 ((eq state
'content
) nil
)
6440 ((eq state
'all
) nil
)
6441 ((eq state
'folded
) nil
)
6442 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6443 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6445 (defun org-remove-empty-overlays-at (pos)
6446 "Remove outline overlays that do not contain non-white stuff."
6449 (and (eq 'outline
(overlay-get o
'invisible
))
6450 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6452 (delete-overlay o
)))
6455 (defun org-clean-visibility-after-subtree-move ()
6456 "Fix visibility issues after moving a subtree."
6457 ;; First, find a reasonable region to look at:
6458 ;; Start two siblings above, end three below
6459 (let* ((beg (save-excursion
6460 (and (org-get-last-sibling)
6461 (org-get-last-sibling))
6463 (end (save-excursion
6464 (and (org-get-next-sibling)
6465 (org-get-next-sibling)
6466 (org-get-next-sibling))
6467 (if (org-at-heading-p)
6470 (level (looking-at "\\*+"))
6471 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6474 (narrow-to-region beg end
)
6476 ;; Properly fold already folded siblings
6477 (goto-char (point-min))
6478 (while (re-search-forward re nil t
)
6479 (if (and (not (outline-invisible-p))
6481 (goto-char (point-at-eol)) (outline-invisible-p)))
6483 (org-cycle-show-empty-lines 'overview
)
6484 (org-cycle-hide-drawers 'overview
)))))
6486 (defun org-cycle-show-empty-lines (state)
6487 "Show empty lines above all visible headlines.
6488 The region to be covered depends on STATE when called through
6489 `org-cycle-hook'. Lisp program can use t for STATE to get the
6490 entire buffer covered. Note that an empty line is only shown if there
6491 are at least `org-cycle-separator-lines' empty lines before the headline."
6492 (when (not (= org-cycle-separator-lines
0))
6494 (let* ((n (abs org-cycle-separator-lines
))
6496 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6497 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6498 (t (let ((ns (number-to-string (- n
2))))
6499 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6500 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6503 ((memq state
'(overview contents t
))
6504 (setq beg
(point-min) end
(point-max)))
6505 ((memq state
'(children folded
))
6506 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6507 (beginning-of-line 2)
6511 (while (re-search-forward re end t
)
6512 (unless (get-char-property (match-end 1) 'invisible
)
6513 (setq e
(match-end 1))
6514 (if (< org-cycle-separator-lines
0)
6515 (setq b
(save-excursion
6516 (goto-char (match-beginning 0))
6517 (org-back-over-empty-lines)
6519 (goto-char (max (point-min) (1- (point))))
6523 (setq b
(match-beginning 1)))
6524 (outline-flag-region b e nil
)))))))
6525 ;; Never hide empty lines at the end of the file.
6527 (goto-char (point-max))
6528 (outline-previous-heading)
6529 (outline-end-of-heading)
6530 (if (and (looking-at "[ \t\n]+")
6531 (= (match-end 0) (point-max)))
6532 (outline-flag-region (point) (match-end 0) nil
))))
6534 (defun org-show-empty-lines-in-parent ()
6535 "Move to the parent and re-show empty lines before visible headlines."
6537 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6538 (org-cycle-show-empty-lines context
))))
6540 (defun org-files-list ()
6541 "Return `org-agenda-files' list, plus all open org-mode files.
6542 This is useful for operations that need to scan all of a user's
6543 open and agenda-wise Org files."
6544 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6545 (dolist (buf (buffer-list))
6546 (with-current-buffer buf
6547 (if (and (eq major-mode
'org-mode
) (buffer-file-name))
6548 (let ((file (expand-file-name (buffer-file-name))))
6549 (unless (member file files
)
6550 (push file files
))))))
6553 (defsubst org-entry-beginning-position
()
6554 "Return the beginning position of the current entry."
6555 (save-excursion (outline-back-to-heading t
) (point)))
6557 (defsubst org-entry-end-position
()
6558 "Return the end position of the current entry."
6559 (save-excursion (outline-next-heading) (point)))
6561 (defun org-cycle-hide-drawers (state)
6562 "Re-hide all drawers after a visibility state change."
6563 (when (and (eq major-mode
'org-mode
)
6564 (not (memq state
'(overview folded contents
))))
6566 (let* ((globalp (memq state
'(contents all
)))
6567 (beg (if globalp
(point-min) (point)))
6568 (end (if globalp
(point-max)
6569 (if (eq state
'children
)
6570 (save-excursion (outline-next-heading) (point))
6571 (org-end-of-subtree t
)))))
6573 (while (re-search-forward org-drawer-regexp end t
)
6574 (org-flag-drawer t
))))))
6576 (defun org-flag-drawer (flag)
6578 (beginning-of-line 1)
6579 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6580 (let ((b (match-end 0)))
6581 (if (re-search-forward
6583 (save-excursion (outline-next-heading) (point)) t
)
6584 (outline-flag-region b
(point-at-eol) flag
)
6585 (error ":END: line missing at position %s" b
))))))
6587 (defun org-subtree-end-visible-p ()
6588 "Is the end of the current subtree visible?"
6589 (pos-visible-in-window-p
6590 (save-excursion (org-end-of-subtree t
) (point))))
6592 (defun org-first-headline-recenter (&optional N
)
6593 "Move cursor to the first headline and recenter the headline.
6594 Optional argument N means put the headline into the Nth line of the window."
6595 (goto-char (point-min))
6596 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6598 (recenter (prefix-numeric-value N
))))
6600 ;;; Saving and restoring visibility
6602 (defun org-outline-overlay-data (&optional use-markers
)
6603 "Return a list of the locations of all outline overlays.
6604 These are overlays with the `invisible' property value `outline'.
6605 The return value is a list of cons cells, with start and stop
6606 positions for each overlay.
6607 If USE-MARKERS is set, return the positions as markers."
6614 (when (eq (overlay-get o
'invisible
) 'outline
)
6615 (setq beg
(overlay-start o
)
6616 end
(overlay-end o
))
6617 (and beg end
(> end beg
)
6619 (cons (move-marker (make-marker) beg
)
6620 (move-marker (make-marker) end
))
6622 (overlays-in (point-min) (point-max))))))))
6624 (defun org-set-outline-overlay-data (data)
6625 "Create visibility overlays for all positions in DATA.
6626 DATA should have been made by `org-outline-overlay-data'."
6633 (outline-flag-region (car c
) (cdr c
) t
))
6636 ;;; Folding of blocks
6638 (defvar org-hide-block-overlays nil
6639 "Overlays hiding blocks.")
6640 (make-variable-buffer-local 'org-hide-block-overlays
)
6642 (defun org-block-map (function &optional start end
)
6643 "Call FUNCTION at the head of all source blocks in the current buffer.
6644 Optional arguments START and END can be used to limit the range."
6645 (let ((start (or start
(point-min)))
6646 (end (or end
(point-max))))
6649 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6652 (goto-char (match-beginning 0))
6653 (funcall function
)))))))
6655 (defun org-hide-block-toggle-all ()
6656 "Toggle the visibility of all blocks in the current buffer."
6657 (org-block-map #'org-hide-block-toggle
))
6659 (defun org-hide-block-all ()
6660 "Fold all blocks in the current buffer."
6662 (org-show-block-all)
6663 (org-block-map #'org-hide-block-toggle-maybe
))
6665 (defun org-show-block-all ()
6666 "Unfold all blocks in the current buffer."
6668 (mapc 'delete-overlay org-hide-block-overlays
)
6669 (setq org-hide-block-overlays nil
))
6671 (defun org-hide-block-toggle-maybe ()
6672 "Toggle visibility of block at point."
6674 (let ((case-fold-search t
))
6676 (beginning-of-line 1)
6677 (looking-at org-block-regexp
))
6678 (progn (org-hide-block-toggle)
6679 t
) ;; to signal that we took action
6680 nil
))) ;; to signal that we did not
6682 (defun org-hide-block-toggle (&optional force
)
6683 "Toggle the visibility of the current block."
6687 (if (re-search-forward org-block-regexp nil t
)
6688 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6689 (end (match-end 0)) ;; end of entire body
6691 (if (memq t
(mapcar (lambda (overlay)
6692 (eq (overlay-get overlay
'invisible
)
6694 (overlays-at start
)))
6695 (if (or (not force
) (eq force
'off
))
6697 (when (member ov org-hide-block-overlays
)
6698 (setq org-hide-block-overlays
6699 (delq ov org-hide-block-overlays
)))
6700 (when (eq (overlay-get ov
'invisible
)
6702 (delete-overlay ov
)))
6703 (overlays-at start
)))
6704 (setq ov
(make-overlay start end
))
6705 (overlay-put ov
'invisible
'org-hide-block
)
6706 ;; make the block accessible to isearch
6708 ov
'isearch-open-invisible
6710 (when (member ov org-hide-block-overlays
)
6711 (setq org-hide-block-overlays
6712 (delq ov org-hide-block-overlays
)))
6713 (when (eq (overlay-get ov
'invisible
)
6715 (delete-overlay ov
))))
6716 (push ov org-hide-block-overlays
)))
6717 (error "Not looking at a source block"))))
6719 ;; org-tab-after-check-for-cycling-hook
6720 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6721 ;; Remove overlays when changing major mode
6722 (add-hook 'org-mode-hook
6723 (lambda () (org-add-hook 'change-major-mode-hook
6724 'org-show-block-all
'append
'local
)))
6728 (defvar org-goto-window-configuration nil
)
6729 (defvar org-goto-marker nil
)
6730 (defvar org-goto-map
6731 (let ((map (make-sparse-keymap)))
6732 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6733 (while (setq cmd
(pop cmds
))
6734 (substitute-key-definition cmd cmd map global-map
)))
6735 (suppress-keymap map
)
6736 (org-defkey map
"\C-m" 'org-goto-ret
)
6737 (org-defkey map
[(return)] 'org-goto-ret
)
6738 (org-defkey map
[(left)] 'org-goto-left
)
6739 (org-defkey map
[(right)] 'org-goto-right
)
6740 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6741 (org-defkey map
"\C-i" 'org-cycle
)
6742 (org-defkey map
[(tab)] 'org-cycle
)
6743 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6744 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6745 (if org-goto-auto-isearch
6746 (if (fboundp 'define-key-after
)
6747 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6749 (org-defkey map "q" 'org-goto-quit)
6750 (org-defkey map "n" 'outline-next-visible-heading)
6751 (org-defkey map "p" 'outline-previous-visible-heading)
6752 (org-defkey map "f" 'outline-forward-same-level)
6753 (org-defkey map "b" 'outline-backward-same-level)
6754 (org-defkey map "u" 'outline-up-heading))
6755 (org-defkey map "/" 'org-occur)
6756 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6757 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6758 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6759 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6760 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6763 (defconst org-goto-help
6764 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6765 RET=jump to location [Q]uit and return to previous location
6766 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6768 (defvar org-goto-start-pos) ; dynamically scoped parameter
6770 ;; FIXME: Docstring does not mention both interfaces
6771 (defun org-goto (&optional alternative-interface)
6772 "Look up a different location in the current file, keeping current visibility.
6774 When you want look-up or go to a different location in a document, the
6775 fastest way is often to fold the entire buffer and then dive into the tree.
6776 This method has the disadvantage, that the previous location will be folded,
6777 which may not be what you want.
6779 This command works around this by showing a copy of the current buffer
6780 in an indirect buffer, in overview mode. You can dive into the tree in
6781 that copy, use org-occur and incremental search to find a location.
6782 When pressing RET or `Q', the command returns to the original buffer in
6783 which the visibility is still unchanged. After RET is will also jump to
6784 the location selected in the indirect buffer and expose the headline
6787 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6788 (org-refile-use-outline-path t)
6789 (org-refile-target-verify-function nil)
6791 (if (not alternative-interface)
6793 (if (eq org-goto-interface 'outline)
6794 'outline-path-completion
6796 (org-goto-start-pos (point))
6798 (if (eq interface 'outline)
6799 (car (org-get-location (current-buffer) org-goto-help))
6800 (let ((pa (org-refile-get-location "Goto")))
6801 (org-refile-check-position pa)
6805 (org-mark-ring-push org-goto-start-pos)
6806 (goto-char selected-point)
6807 (if (or (outline-invisible-p) (org-invisible-p2))
6808 (org-show-context 'org-goto)))
6811 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6812 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6813 (defvar org-goto-local-auto-isearch-map) ; defined below
6815 (defun org-get-location (buf help)
6816 "Let the user select a location in the Org-mode buffer BUF.
6817 This function uses a recursive edit. It returns the selected position
6819 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6820 (isearch-hide-immediately nil)
6821 (isearch-search-fun-function
6822 (lambda () 'org-goto-local-search-headings))
6823 (org-goto-selected-point org-goto-exit-command)
6825 (special-display-buffer-names nil)
6826 (special-display-regexps nil)
6827 (special-display-function nil))
6829 (save-window-excursion
6830 (delete-other-windows)
6831 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6832 (org-pop-to-buffer-same-window
6834 (make-indirect-buffer (current-buffer) "*org-goto*")
6835 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6836 (with-output-to-temp-buffer "*Help*"
6838 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6839 (setq buffer-read-only nil)
6840 (let ((org-startup-truncated t)
6841 (org-startup-folded nil)
6842 (org-startup-align-all-tables nil))
6845 (setq buffer-read-only t)
6846 (if (and (boundp 'org-goto-start-pos)
6847 (integer-or-marker-p org-goto-start-pos))
6848 (let ((org-show-hierarchy-above t)
6849 (org-show-siblings t)
6850 (org-show-following-heading t))
6851 (goto-char org-goto-start-pos)
6852 (and (outline-invisible-p) (org-show-context)))
6853 (goto-char (point-min)))
6854 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6855 (message "Select location and press RET")
6856 (use-local-map org-goto-map)
6859 (kill-buffer "*org-goto*")
6860 (cons org-goto-selected-point org-goto-exit-command)))
6862 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6863 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6864 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6865 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6867 (defun org-goto-local-search-headings (string bound noerror)
6868 "Search and make sure that any matches are in headlines."
6870 (while (if isearch-forward
6871 (search-forward string bound noerror)
6872 (search-backward string bound noerror))
6873 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6874 (and (member :headline context)
6875 (not (member :tags context))))
6876 (throw 'return (point))))))
6878 (defun org-goto-local-auto-isearch ()
6881 (goto-char (point-min))
6882 (let ((keys (this-command-keys)))
6883 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6885 (isearch-process-search-char (string-to-char keys)))))
6887 (defun org-goto-ret (&optional arg)
6888 "Finish `org-goto' by going to the new location."
6890 (setq org-goto-selected-point (point)
6891 org-goto-exit-command 'return)
6894 (defun org-goto-left ()
6895 "Finish `org-goto' by going to the new location."
6897 (if (org-on-heading-p)
6899 (beginning-of-line 1)
6900 (setq org-goto-selected-point (point)
6901 org-goto-exit-command 'left)
6903 (error "Not on a heading")))
6905 (defun org-goto-right ()
6906 "Finish `org-goto' by going to the new location."
6908 (if (org-on-heading-p)
6910 (setq org-goto-selected-point (point)
6911 org-goto-exit-command 'right)
6913 (error "Not on a heading")))
6915 (defun org-goto-quit ()
6916 "Finish `org-goto' without cursor motion."
6918 (setq org-goto-selected-point nil)
6919 (setq org-goto-exit-command 'quit)
6922 ;;; Indirect buffer display of subtrees
6924 (defvar org-indirect-dedicated-frame nil
6925 "This is the frame being used for indirect tree display.")
6926 (defvar org-last-indirect-buffer nil)
6928 (defun org-tree-to-indirect-buffer (&optional arg)
6929 "Create indirect buffer and narrow it to current subtree.
6930 With numerical prefix ARG, go up to this level and then take that tree.
6931 If ARG is negative, go up that many levels.
6932 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6933 indirect buffer previously made with this command, to avoid proliferation of
6934 indirect buffers. However, when you call the command with a \
6935 \\[universal-argument] prefix, or
6936 when `org-indirect-buffer-display' is `new-frame', the last buffer
6937 is kept so that you can work with several indirect buffers at the same time.
6938 If `org-indirect-buffer-display' is `dedicated-frame', the \
6939 \\[universal-argument] prefix also
6940 requests that a new frame be made for the new buffer, so that the dedicated
6941 frame is not changed."
6943 (let ((cbuf (current-buffer))
6944 (cwin (selected-window))
6946 beg end level heading ibuf)
6948 (org-back-to-heading t)
6950 (setq level (org-outline-level))
6951 (if (< arg 0) (setq arg (+ level arg)))
6952 (while (> (setq level (org-outline-level)) arg)
6953 (outline-up-heading 1 t)))
6955 heading (org-get-heading))
6956 (org-end-of-subtree t t)
6957 (if (org-on-heading-p) (backward-char 1))
6959 (if (and (buffer-live-p org-last-indirect-buffer)
6960 (not (eq org-indirect-buffer-display 'new-frame))
6962 (kill-buffer org-last-indirect-buffer))
6963 (setq ibuf (org-get-indirect-buffer cbuf)
6964 org-last-indirect-buffer ibuf)
6966 ((or (eq org-indirect-buffer-display 'new-frame)
6967 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6968 (select-frame (make-frame))
6969 (delete-other-windows)
6970 (org-pop-to-buffer-same-window ibuf)
6971 (org-set-frame-title heading))
6972 ((eq org-indirect-buffer-display 'dedicated-frame)
6974 (select-frame (or (and org-indirect-dedicated-frame
6975 (frame-live-p org-indirect-dedicated-frame)
6976 org-indirect-dedicated-frame)
6977 (setq org-indirect-dedicated-frame (make-frame)))))
6978 (delete-other-windows)
6979 (org-pop-to-buffer-same-window ibuf)
6980 (org-set-frame-title (concat "Indirect: " heading)))
6981 ((eq org-indirect-buffer-display 'current-window)
6982 (org-pop-to-buffer-same-window ibuf))
6983 ((eq org-indirect-buffer-display 'other-window)
6984 (pop-to-buffer ibuf))
6985 (t (error "Invalid value")))
6986 (if (featurep 'xemacs)
6987 (save-excursion (org-mode) (turn-on-font-lock)))
6988 (narrow-to-region beg end)
6991 (run-hook-with-args 'org-cycle-hook 'all)
6992 (and (window-live-p cwin) (select-window cwin))))
6994 (defun org-get-indirect-buffer (&optional buffer)
6995 (setq buffer (or buffer (current-buffer)))
6996 (let ((n 1) (base (buffer-name buffer)) bname)
6997 (while (buffer-live-p
6998 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7001 (make-indirect-buffer buffer bname 'clone)
7002 (error (make-indirect-buffer buffer bname)))))
7004 (defun org-set-frame-title (title)
7005 "Set the title of the current frame to the string TITLE."
7006 ;; FIXME: how to name a single frame in XEmacs???
7007 (unless (featurep 'xemacs)
7008 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7010 ;;;; Structure editing
7012 ;;; Inserting headlines
7014 (defun org-previous-line-empty-p ()
7017 (or (beginning-of-line 0) t)
7019 (looking-at "[ \t]*$")))))
7021 (defun org-insert-heading (&optional force-heading invisible-ok)
7022 "Insert a new heading or item with same depth at point.
7023 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7024 If point is at the beginning of a headline, insert a sibling before the
7025 current headline. If point is not at the beginning, split the line,
7026 create the new headline with the text in the current line after point
7027 \(but see also the variable `org-M-RET-may-split-line').
7029 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7030 This is important for non-interactive uses of the command."
7032 (if (or (= (buffer-size) 0)
7033 (and (not (save-excursion
7034 (and (ignore-errors (org-back-to-heading invisible-ok))
7035 (org-on-heading-p))))
7036 (or force-heading (not (org-in-item-p)))))
7039 (run-hooks 'org-insert-heading-hook))
7040 (when (or force-heading (not (org-insert-item)))
7041 (let* ((empty-line-p nil)
7043 (on-heading (org-on-heading-p))
7044 (head (save-excursion
7047 (org-back-to-heading invisible-ok)
7048 (when (and (not on-heading)
7049 (featurep 'org-inlinetask)
7050 (integerp org-inlinetask-min-level)
7051 (>= (length (match-string 0))
7052 org-inlinetask-min-level))
7053 ;; Find a heading level before the inline task
7054 (while (and (setq level (org-up-heading-safe))
7055 (>= level org-inlinetask-min-level)))
7056 (if (org-on-heading-p)
7057 (org-back-to-heading invisible-ok)
7058 (error "This should not happen")))
7059 (setq empty-line-p (org-previous-line-empty-p))
7062 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7063 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7064 pos hide-previous previous-pos)
7066 ((and (org-on-heading-p) (bolp)
7068 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7069 ;; insert before the current line
7070 (open-line (if blank 2 1)))
7072 (not org-insert-heading-respect-content)
7075 (backward-char 1) (not (outline-invisible-p)))))
7076 ;; insert right here
7079 ;; somewhere in the line
7081 (setq previous-pos (point-at-bol))
7083 (setq hide-previous (outline-invisible-p)))
7084 (and org-insert-heading-respect-content (org-show-subtree))
7086 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7089 (goto-char (point-at-bol))
7090 (and (looking-at org-complex-heading-regexp)
7092 (> p (match-beginning 4)))))))
7095 (org-insert-heading-respect-content
7096 (org-end-of-subtree nil t)
7097 (when (featurep 'org-inlinetask)
7098 (while (and (not (eobp))
7099 (looking-at "\\(\\*+\\)[ \t]+")
7100 (>= (length (match-string 1))
7101 org-inlinetask-min-level))
7102 (org-end-of-subtree nil t)))
7103 (or (bolp) (newline))
7104 (or (org-previous-line-empty-p)
7105 (and blank (newline)))
7111 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7112 (setq tags (and (match-end 2) (match-string 2)))
7114 (delete-region (match-beginning 1) (match-end 1)))
7115 (setq pos (point-at-bol))
7116 (or split (end-of-line 1))
7117 (delete-horizontal-space)
7118 (if (string-match "\\`\\*+\\'"
7119 (buffer-substring (point-at-bol) (point)))
7121 (newline (if blank 2 1))
7127 (org-set-tags nil 'align))))
7129 (or split (end-of-line 1))
7130 (newline (if blank 2 1)))))))
7131 (insert head) (just-one-space)
7134 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7135 (when (and org-insert-heading-respect-content hide-previous)
7137 (goto-char previous-pos)
7139 (run-hooks 'org-insert-heading-hook)))))
7141 (defun org-get-heading (&optional no-tags no-todo)
7142 "Return the heading of the current entry, without the stars.
7143 When NO-TAGS is non-nil, don't include tags.
7144 When NO-TODO is non-nil, don't include TODO keywords."
7146 (org-back-to-heading t)
7148 ((and no-tags no-todo)
7149 (looking-at org-complex-heading-regexp)
7152 (looking-at (concat org-outline-regexp
7154 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7157 (looking-at org-todo-line-regexp)
7159 (t (looking-at org-heading-regexp)
7160 (match-string 2)))))
7162 (defun org-heading-components ()
7163 "Return the components of the current heading.
7164 This is a list with the following elements:
7165 - the level as an integer
7166 - the reduced level, different if `org-odd-levels-only' is set.
7167 - the TODO keyword, or nil
7168 - the priority character, like ?A, or nil if no priority is given
7169 - the headline text itself, or the tags string if no headline text
7170 - the tags string, or nil."
7172 (org-back-to-heading t)
7173 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7174 (list (length (match-string 1))
7175 (org-reduced-level (length (match-string 1)))
7176 (org-match-string-no-properties 2)
7177 (and (match-end 3) (aref (match-string 3) 2))
7178 (org-match-string-no-properties 4)
7179 (org-match-string-no-properties 5)))))
7181 (defun org-get-entry ()
7182 "Get the entry text, after heading, entire subtree."
7184 (org-back-to-heading t)
7185 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7187 (defun org-insert-heading-after-current ()
7188 "Insert a new heading with same level as current, after current subtree."
7190 (org-back-to-heading)
7191 (org-insert-heading)
7192 (org-move-subtree-down)
7195 (defun org-insert-heading-respect-content ()
7197 (let ((org-insert-heading-respect-content t))
7198 (org-insert-heading t)))
7200 (defun org-insert-todo-heading-respect-content (&optional force-state)
7202 (let ((org-insert-heading-respect-content t))
7203 (org-insert-todo-heading force-state t)))
7205 (defun org-insert-todo-heading (arg &optional force-heading)
7206 "Insert a new heading with the same level and TODO state as current heading.
7207 If the heading has no TODO state, or if the state is DONE, use the first
7208 state (TODO by default). Also with prefix arg, force first state."
7210 (when (or force-heading (not (org-insert-item 'checkbox)))
7211 (org-insert-heading force-heading)
7213 (org-back-to-heading)
7214 (outline-previous-heading)
7215 (looking-at org-todo-line-regexp))
7219 (not (match-beginning 2))
7220 (member (match-string 2) org-done-keywords))
7221 (car org-todo-keywords-1)
7225 (run-hook-with-args-until-success
7226 'org-todo-get-default-hook new-mark-x nil)
7228 (beginning-of-line 1)
7229 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7230 (if org-treat-insert-todo-heading-as-state-change
7232 (insert new-mark " "))))
7233 (when org-provide-todo-statistics
7234 (org-update-parent-todo-statistics))))
7236 (defun org-insert-subheading (arg)
7237 "Insert a new subheading and demote it.
7238 Works for outline headings and for plain lists alike."
7240 (org-insert-heading arg)
7242 ((org-on-heading-p) (org-do-demote))
7243 ((org-at-item-p) (org-indent-item))))
7245 (defun org-insert-todo-subheading (arg)
7246 "Insert a new subheading with TODO keyword or checkbox and demote it.
7247 Works for outline headings and for plain lists alike."
7249 (org-insert-todo-heading arg)
7251 ((org-on-heading-p) (org-do-demote))
7252 ((org-at-item-p) (org-indent-item))))
7254 ;;; Promotion and Demotion
7256 (defvar org-after-demote-entry-hook nil
7257 "Hook run after an entry has been demoted.
7258 The cursor will be at the beginning of the entry.
7259 When a subtree is being demoted, the hook will be called for each node.")
7261 (defvar org-after-promote-entry-hook nil
7262 "Hook run after an entry has been promoted.
7263 The cursor will be at the beginning of the entry.
7264 When a subtree is being promoted, the hook will be called for each node.")
7266 (defun org-promote-subtree ()
7267 "Promote the entire subtree.
7268 See also `org-promote'."
7271 (org-with-limited-levels (org-map-tree 'org-promote)))
7272 (org-fix-position-after-promote))
7274 (defun org-demote-subtree ()
7275 "Demote the entire subtree. See `org-demote'.
7276 See also `org-promote'."
7279 (org-with-limited-levels (org-map-tree 'org-demote)))
7280 (org-fix-position-after-promote))
7283 (defun org-do-promote ()
7284 "Promote the current heading higher up the tree.
7285 If the region is active in `transient-mark-mode', promote all headings
7289 (if (org-region-active-p)
7290 (org-map-region 'org-promote (region-beginning) (region-end))
7292 (org-fix-position-after-promote))
7294 (defun org-do-demote ()
7295 "Demote the current heading lower down the tree.
7296 If the region is active in `transient-mark-mode', demote all headings
7300 (if (org-region-active-p)
7301 (org-map-region 'org-demote (region-beginning) (region-end))
7303 (org-fix-position-after-promote))
7305 (defun org-fix-position-after-promote ()
7306 "Make sure that after pro/demotion cursor position is right."
7307 (let ((pos (point)))
7308 (when (save-excursion
7309 (beginning-of-line 1)
7310 (looking-at org-todo-line-regexp)
7311 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7312 (cond ((eobp) (insert " "))
7313 ((eolp) (insert " "))
7314 ((equal (char-after) ?\ ) (forward-char 1))))))
7316 (defun org-current-level ()
7317 "Return the level of the current entry, or nil if before the first headline.
7318 The level is the number of stars at the beginning of the headline."
7320 (org-with-limited-levels
7321 (if (ignore-errors (org-back-to-heading t))
7322 (funcall outline-level)))))
7324 (defun org-get-previous-line-level ()
7325 "Return the outline depth of the last headline before the current line.
7326 Returns 0 for the first headline in the buffer, and nil if before the
7328 (let ((current-level (org-current-level))
7329 (prev-level (when (> (line-number-at-pos) 1)
7331 (beginning-of-line 0)
7332 (org-current-level)))))
7333 (cond ((null current-level) nil) ; Before first headline
7334 ((null prev-level) 0) ; At first headline
7337 (defun org-reduced-level (l)
7338 "Compute the effective level of a heading.
7339 This takes into account the setting of `org-odd-levels-only'."
7342 (org-odd-levels-only (1+ (floor (/ l 2))))
7345 (defun org-level-increment ()
7346 "Return the number of stars that will be added or removed at a
7347 time to headlines when structure editing, based on the value of
7348 `org-odd-levels-only'."
7349 (if org-odd-levels-only 2 1))
7351 (defun org-get-valid-level (level &optional change)
7352 "Rectify a level change under the influence of `org-odd-levels-only'
7353 LEVEL is a current level, CHANGE is by how much the level should be
7354 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7355 even level numbers will become the next higher odd number."
7356 (if org-odd-levels-only
7357 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7358 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7359 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7360 (max 1 (+ level (or change 0)))))
7362 (if (boundp 'define-obsolete-function-alias)
7363 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7364 (define-obsolete-function-alias 'org-get-legal-level
7365 'org-get-valid-level)
7366 (define-obsolete-function-alias 'org-get-legal-level
7367 'org-get-valid-level "23.1")))
7369 (defun org-promote ()
7370 "Promote the current heading higher up the tree.
7371 If the region is active in `transient-mark-mode', promote all headings
7373 (org-back-to-heading t)
7374 (let* ((level (save-match-data (funcall outline-level)))
7375 (after-change-functions (remove 'flyspell-after-change-function
7376 after-change-functions))
7377 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7378 (diff (abs (- level (length up-head) -1))))
7379 (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
7380 (replace-match up-head nil t)
7381 ;; Fixup tag positioning
7382 (and org-auto-align-tags (org-set-tags nil t))
7383 (if org-adapt-indentation (org-fixup-indentation (- diff)))
7384 (run-hooks 'org-after-promote-entry-hook)))
7386 (defun org-demote ()
7387 "Demote the current heading lower down the tree.
7388 If the region is active in `transient-mark-mode', demote all headings
7390 (org-back-to-heading t)
7391 (let* ((level (save-match-data (funcall outline-level)))
7392 (after-change-functions (remove 'flyspell-after-change-function
7393 after-change-functions))
7394 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7395 (diff (abs (- level (length down-head) -1))))
7396 (replace-match down-head nil t)
7397 ;; Fixup tag positioning
7398 (and org-auto-align-tags (org-set-tags nil t))
7399 (if org-adapt-indentation (org-fixup-indentation diff))
7400 (run-hooks 'org-after-demote-entry-hook)))
7402 (defun org-cycle-level ()
7403 "Cycle the level of an empty headline through possible states.
7404 This goes first to child, then to parent, level, then up the hierarchy.
7405 After top level, it switches back to sibling level."
7407 (let ((org-adapt-indentation nil))
7408 (when (org-point-at-end-of-empty-headline)
7409 (setq this-command 'org-cycle-level) ; Only needed for caching
7410 (let ((cur-level (org-current-level))
7411 (prev-level (org-get-previous-line-level)))
7413 ;; If first headline in file, promote to top-level.
7415 (loop repeat (/ (- cur-level 1) (org-level-increment))
7416 do (org-do-promote)))
7417 ;; If same level as prev, demote one.
7418 ((= prev-level cur-level)
7420 ;; If parent is top-level, promote to top level if not already.
7422 (loop repeat (/ (- cur-level 1) (org-level-increment))
7423 do (org-do-promote)))
7424 ;; If top-level, return to prev-level.
7426 (loop repeat (/ (- prev-level 1) (org-level-increment))
7427 do (org-do-demote)))
7428 ;; If less than prev-level, promote one.
7429 ((< cur-level prev-level)
7431 ;; If deeper than prev-level, promote until higher than
7433 ((> cur-level prev-level)
7434 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7435 do (org-do-promote))))
7438 (defun org-map-tree (fun)
7439 "Call FUN for every heading underneath the current one."
7440 (org-back-to-heading)
7441 (let ((level (funcall outline-level)))
7445 (outline-next-heading)
7446 (> (funcall outline-level) level))
7450 (defun org-map-region (fun beg end)
7451 "Call FUN for every heading between BEG and END."
7452 (let ((org-ignore-region t))
7454 (setq end (copy-marker end))
7456 (if (and (re-search-forward org-outline-regexp-bol nil t)
7460 (outline-next-heading)
7465 (defvar org-property-end-re) ; silence byte-compiler
7466 (defun org-fixup-indentation (diff)
7467 "Change the indentation in the current entry by DIFF.
7468 However, if any line in the current entry has no indentation, or if it
7469 would end up with no indentation after the change, nothing at all is done."
7471 (let ((end (save-excursion (outline-next-heading)
7473 (prohibit (if (> diff 0)
7475 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7477 (unless (save-excursion (end-of-line 1)
7478 (re-search-forward prohibit end t))
7479 (while (and (< (point) end)
7480 (re-search-forward "^[ \t]+" end t))
7481 (goto-char (match-end 0))
7482 (setq col (current-column))
7483 (if (< diff 0) (replace-match ""))
7484 (org-indent-to-column (+ diff col))))
7485 (move-marker end nil))))
7487 (defun org-convert-to-odd-levels ()
7488 "Convert an org-mode file with all levels allowed to one with odd levels.
7489 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7492 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7493 (let ((outline-level 'org-outline-level)
7494 (org-odd-levels-only nil) n)
7496 (goto-char (point-min))
7497 (while (re-search-forward "^\\*\\*+ " nil t)
7498 (setq n (- (length (match-string 0)) 2))
7499 (while (>= (setq n (1- n)) 0)
7501 (end-of-line 1))))))
7503 (defun org-convert-to-oddeven-levels ()
7504 "Convert an org-mode file with only odd levels to one with odd/even levels.
7505 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7506 file contains a section with an even level, conversion would
7507 destroy the structure of the file. An error is signaled in this
7510 (goto-char (point-min))
7511 ;; First check if there are no even levels
7512 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7513 (org-show-context t)
7514 (error "Not all levels are odd in this file. Conversion not possible"))
7515 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7516 (let ((outline-regexp org-outline-regexp)
7517 (outline-level 'org-outline-level)
7518 (org-odd-levels-only nil) n)
7520 (goto-char (point-min))
7521 (while (re-search-forward "^\\*\\*+ " nil t)
7522 (setq n (/ (1- (length (match-string 0))) 2))
7523 (while (>= (setq n (1- n)) 0)
7525 (end-of-line 1))))))
7527 (defun org-tr-level (n)
7528 "Make N odd if required."
7529 (if org-odd-levels-only (1+ (/ n 2)) n))
7531 ;;; Vertical tree motion, cutting and pasting of subtrees
7533 (defun org-move-subtree-up (&optional arg)
7534 "Move the current subtree up past ARG headlines of the same level."
7536 (org-move-subtree-down (- (prefix-numeric-value arg))))
7538 (defun org-move-subtree-down (&optional arg)
7539 "Move the current subtree down past ARG headlines of the same level."
7541 (setq arg (prefix-numeric-value arg))
7542 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7543 'org-get-last-sibling))
7544 (ins-point (make-marker))
7546 (col (current-column))
7547 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7549 (org-back-to-heading)
7552 (setq ne-beg (org-back-over-empty-lines))
7555 (save-excursion (outline-end-of-heading)
7556 (setq folded (outline-invisible-p)))
7557 (outline-end-of-subtree))
7558 (outline-next-heading)
7559 (setq ne-end (org-back-over-empty-lines))
7562 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7563 ;; include less whitespace
7566 (forward-line (- ne-beg ne-end))
7567 (setq beg (point))))
7568 ;; Find insertion point, with error handling
7570 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7571 (progn (goto-char beg0)
7572 (error "Cannot move past superior level or buffer limit")))
7573 (setq cnt (1- cnt)))
7575 ;; Moving forward - still need to move over subtree
7576 (progn (org-end-of-subtree t t)
7578 (org-back-over-empty-lines)
7579 (or (bolp) (newline)))))
7580 (setq ne-ins (org-back-over-empty-lines))
7581 (move-marker ins-point (point))
7582 (setq txt (buffer-substring beg end))
7583 (org-save-markers-in-region beg end)
7584 (delete-region beg end)
7585 (org-remove-empty-overlays-at beg)
7586 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7587 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7588 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7589 (let ((bbb (point)))
7590 (insert-before-markers txt)
7591 (org-reinstall-markers-in-region bbb)
7592 (move-marker ins-point bbb))
7593 (or (bolp) (insert "\n"))
7594 (setq ins-end (point))
7595 (goto-char ins-point)
7596 (org-skip-whitespace)
7597 (when (and (< arg 0)
7598 (org-first-sibling-p)
7600 ;; Move whitespace back to beginning
7603 (let ((kill-whole-line t))
7604 (kill-line (- ne-ins ne-beg)) (point)))
7605 (insert (make-string (- ne-ins ne-beg) ?\n)))
7606 (move-marker ins-point nil)
7611 (org-cycle-hide-drawers 'children))
7612 (org-clean-visibility-after-subtree-move)
7613 ;; move back to the initial column we were at
7614 (move-to-column col)))
7616 (defvar org-subtree-clip ""
7617 "Clipboard for cut and paste of subtrees.
7618 This is actually only a copy of the kill, because we use the normal kill
7619 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7621 (defvar org-subtree-clip-folded nil
7622 "Was the last copied subtree folded?
7623 This is used to fold the tree back after pasting.")
7625 (defun org-cut-subtree (&optional n)
7626 "Cut the current subtree into the clipboard.
7627 With prefix arg N, cut this many sequential subtrees.
7628 This is a short-hand for marking the subtree and then cutting it."
7630 (org-copy-subtree n 'cut))
7632 (defun org-copy-subtree (&optional n cut force-store-markers)
7633 "Cut the current subtree into the clipboard.
7634 With prefix arg N, cut this many sequential subtrees.
7635 This is a short-hand for marking the subtree and then copying it.
7636 If CUT is non-nil, actually cut the subtree.
7637 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7638 of some markers in the region, even if CUT is non-nil. This is
7639 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7641 (let (beg end folded (beg0 (point)))
7642 (if (org-called-interactively-p 'any)
7643 (org-back-to-heading nil) ; take what looks like a subtree
7644 (org-back-to-heading t)) ; take what is really there
7645 (org-back-over-empty-lines)
7647 (skip-chars-forward " \t\r\n")
7649 (save-excursion (outline-end-of-heading)
7650 (setq folded (outline-invisible-p)))
7652 (org-forward-same-level (1- n) t)
7654 (org-end-of-subtree t t))
7655 (org-back-over-empty-lines)
7659 (setq org-subtree-clip-folded folded)
7660 (when (or cut force-store-markers)
7661 (org-save-markers-in-region beg end))
7662 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7663 (setq org-subtree-clip (current-kill 0))
7664 (message "%s: Subtree(s) with %d characters"
7665 (if cut "Cut" "Copied")
7666 (length org-subtree-clip)))))
7668 (defun org-paste-subtree (&optional level tree for-yank)
7669 "Paste the clipboard as a subtree, with modification of headline level.
7670 The entire subtree is promoted or demoted in order to match a new headline
7673 If the cursor is at the beginning of a headline, the same level as
7674 that headline is used to paste the tree
7676 If not, the new level is derived from the *visible* headings
7677 before and after the insertion point, and taken to be the inferior headline
7678 level of the two. So if the previous visible heading is level 3 and the
7679 next is level 4 (or vice versa), level 4 will be used for insertion.
7680 This makes sure that the subtree remains an independent subtree and does
7681 not swallow low level entries.
7683 You can also force a different level, either by using a numeric prefix
7684 argument, or by inserting the heading marker by hand. For example, if the
7685 cursor is after \"*****\", then the tree will be shifted to level 5.
7687 If optional TREE is given, use this text instead of the kill ring.
7689 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7690 move back over whitespace before inserting, and move point to the end of
7691 the inserted text when done."
7693 (setq tree (or tree (and kill-ring (current-kill 0))))
7694 (unless (org-kill-is-subtree-p tree)
7696 (substitute-command-keys
7697 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7698 (org-with-limited-levels
7699 (let* ((visp (not (outline-invisible-p)))
7701 (^re_ "\\(\\*+\\)[ \t]*")
7702 (old-level (if (string-match org-outline-regexp-bol txt)
7703 (- (match-end 0) (match-beginning 0) 1)
7705 (force-level (cond (level (prefix-numeric-value level))
7706 ((and (looking-at "[ \t]*$")
7708 "^\\*+$" (buffer-substring
7709 (point-at-bol) (point))))
7710 (- (match-end 1) (match-beginning 1)))
7712 (looking-at org-outline-regexp))
7713 (- (match-end 0) (point) 1))
7715 (previous-level (save-excursion
7718 (outline-previous-visible-heading 1)
7719 (if (looking-at ^re_)
7720 (- (match-end 0) (match-beginning 0) 1)
7723 (next-level (save-excursion
7726 (or (looking-at org-outline-regexp)
7727 (outline-next-visible-heading 1))
7728 (if (looking-at ^re_)
7729 (- (match-end 0) (match-beginning 0) 1)
7732 (new-level (or force-level (max previous-level next-level)))
7733 (shift (if (or (= old-level -1)
7735 (= old-level new-level))
7737 (- new-level old-level)))
7738 (delta (if (> shift 0) -1 1))
7739 (func (if (> shift 0) 'org-demote 'org-promote))
7740 (org-odd-levels-only nil)
7742 ;; Remove the forced level indicator
7744 (delete-region (point-at-bol) (point)))
7746 (beginning-of-line (if (bolp) 1 2))
7747 (unless for-yank (org-back-over-empty-lines))
7749 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7750 (insert-before-markers txt)
7751 (unless (string-match "\n\\'" txt) (insert "\n"))
7752 (setq newend (point))
7753 (org-reinstall-markers-in-region beg)
7756 (skip-chars-forward " \t\n\r")
7758 (if (and (outline-invisible-p) visp)
7759 (save-excursion (outline-show-heading)))
7760 ;; Shift if necessary
7763 (narrow-to-region beg end)
7764 (while (not (= shift 0))
7765 (org-map-region func (point-min) (point-max))
7766 (setq shift (+ delta shift)))
7767 (goto-char (point-min))
7768 (setq newend (point-max))))
7769 (when (or (org-called-interactively-p 'interactive) for-yank)
7770 (message "Clipboard pasted as level %d subtree" new-level))
7771 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7773 (eq org-subtree-clip (current-kill 0))
7774 org-subtree-clip-folded)
7775 ;; The tree was folded before it was killed/copied
7777 (and for-yank (goto-char newend)))))
7779 (defun org-kill-is-subtree-p (&optional txt)
7780 "Check if the current kill is an outline subtree, or a set of trees.
7781 Returns nil if kill does not start with a headline, or if the first
7782 headline level is not the largest headline level in the tree.
7783 So this will actually accept several entries of equal levels as well,
7784 which is OK for `org-paste-subtree'.
7785 If optional TXT is given, check this string instead of the current kill."
7786 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7787 (re (org-get-limited-outline-regexp))
7788 (^re (concat "^" re))
7789 (start-level (and kill
7791 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7793 (- (match-end 2) (match-beginning 2) 1)))
7794 (start (1+ (or (match-beginning 2) -1))))
7795 (if (not start-level)
7797 nil) ;; does not even start with a heading
7799 (while (setq start (string-match ^re kill (1+ start)))
7800 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7804 (defvar org-markers-to-move nil
7805 "Markers that should be moved with a cut-and-paste operation.
7806 Those markers are stored together with their positions relative to
7807 the start of the region.")
7809 (defun org-save-markers-in-region (beg end)
7810 "Check markers in region.
7811 If these markers are between BEG and END, record their position relative
7812 to BEG, so that after moving the block of text, we can put the markers back
7814 This function gets called just before an entry or tree gets cut from the
7815 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7816 called immediately, to move the markers with the entries."
7817 (setq org-markers-to-move nil)
7818 (when (featurep 'org-clock)
7819 (org-clock-save-markers-for-cut-and-paste beg end))
7820 (when (featurep 'org-agenda)
7821 (org-agenda-save-markers-for-cut-and-paste beg end)))
7823 (defun org-check-and-save-marker (marker beg end)
7824 "Check if MARKER is between BEG and END.
7825 If yes, remember the marker and the distance to BEG."
7826 (when (and (marker-buffer marker)
7827 (equal (marker-buffer marker) (current-buffer)))
7828 (if (and (>= marker beg) (< marker end))
7829 (push (cons marker (- marker beg)) org-markers-to-move))))
7831 (defun org-reinstall-markers-in-region (beg)
7832 "Move all remembered markers to their position relative to BEG."
7834 (move-marker (car x) (+ beg (cdr x))))
7835 org-markers-to-move)
7836 (setq org-markers-to-move nil))
7838 (defun org-narrow-to-subtree ()
7839 "Narrow buffer to the current subtree."
7843 (org-with-limited-levels
7845 (progn (org-back-to-heading t) (point))
7846 (progn (org-end-of-subtree t t)
7847 (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
7850 (defun org-narrow-to-block ()
7851 "Narrow buffer to the current block."
7853 (let* ((case-fold-search t)
7854 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
7855 "^[ \t]*#\\+end_.*")))
7857 (narrow-to-region (car blockp) (cdr blockp))
7858 (error "Not in a block"))))
7861 (defvar org-property-drawer-re))
7863 (defvar org-property-start-re) ;; defined below
7864 (defun org-clone-subtree-with-time-shift (n &optional shift)
7865 "Clone the task (subtree) at point N times.
7866 The clones will be inserted as siblings.
7868 In interactive use, the user will be prompted for the number of
7869 clones to be produced, and for a time SHIFT, which may be a
7870 repeater as used in time stamps, for example `+3d'.
7872 When a valid repeater is given and the entry contains any time
7873 stamps, the clones will become a sequence in time, with time
7874 stamps in the subtree shifted for each clone produced. If SHIFT
7875 is nil or the empty string, time stamps will be left alone. The
7876 ID property of the original subtree is removed.
7878 If the original subtree did contain time stamps with a repeater,
7879 the following will happen:
7880 - the repeater will be removed in each clone
7881 - an additional clone will be produced, with the current, unshifted
7882 date(s) in the entry.
7883 - the original entry will be placed *after* all the clones, with
7885 - the start days in the repeater in the original entry will be shifted
7886 to past the last clone.
7887 I this way you can spell out a number of instances of a repeating task,
7888 and still retain the repeater to cover future instances of the task."
7889 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7890 (let (beg end template task idprop
7891 shift-n shift-what doshift nmin nmax (n-no-remove -1)
7892 (drawer-re org-drawer-regexp))
7893 (if (not (and (integerp n) (> n 0)))
7894 (error "Invalid number of replications %s" n))
7895 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7896 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
7898 (error "Invalid shift specification %s" shift))
7900 (setq shift-n (string-to-number (match-string 1 shift))
7901 shift-what (cdr (assoc (match-string 2 shift)
7902 '(("d" . day) ("w" . week)
7903 ("m" . month) ("y" . year))))))
7904 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7905 (setq nmin 1 nmax n)
7906 (org-back-to-heading t)
7908 (setq idprop (org-entry-get nil "ID"))
7909 (org-end-of-subtree t t)
7910 (or (bolp) (insert "\n"))
7912 (setq template (buffer-substring beg end))
7914 (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
7915 (delete-region beg end)
7917 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7919 (loop for n from nmin to nmax do
7924 (goto-char (point-min))
7926 (and idprop (if org-clone-delete-id
7927 (org-entry-delete nil "ID")
7928 (org-id-get-create t)))
7930 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
7932 (goto-char (point-min))
7933 (while (re-search-forward drawer-re nil t)
7935 (org-remove-empty-drawer-at d (point))) org-drawers)))
7936 (goto-char (point-min))
7938 (while (re-search-forward org-ts-regexp-both nil t)
7939 (org-timestamp-change (* n shift-n) shift-what))
7940 (unless (= n n-no-remove)
7941 (goto-char (point-min))
7942 (while (re-search-forward org-ts-regexp nil t)
7944 (goto-char (match-beginning 0))
7945 (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
7946 (delete-region (match-beginning 1) (match-end 1)))))))
7947 (setq task (buffer-string)))
7953 (defun org-sort (with-case)
7954 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
7955 Optional argument WITH-CASE means sort case-sensitively.
7956 With a double prefix argument, also remove duplicate entries."
7959 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
7960 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
7962 (org-call-with-arg 'org-sort-entries with-case))))
7964 (defun org-sort-remove-invisible (s)
7965 (remove-text-properties 0 (length s) org-rm-props s)
7966 (while (string-match org-bracket-link-regexp s)
7967 (setq s (replace-match (if (match-end 2)
7969 (match-string 1 s)) t t s)))
7972 (defvar org-priority-regexp) ; defined later in the file
7974 (defvar org-after-sorting-entries-or-items-hook nil
7975 "Hook that is run after a bunch of entries or items have been sorted.
7976 When children are sorted, the cursor is in the parent line when this
7977 hook gets called. When a region or a plain list is sorted, the cursor
7978 will be in the first entry of the sorted region/list.")
7980 (defun org-sort-entries
7981 (&optional with-case sorting-type getkey-func compare-func property)
7982 "Sort entries on a certain level of an outline tree.
7983 If there is an active region, the entries in the region are sorted.
7984 Else, if the cursor is before the first entry, sort the top-level items.
7985 Else, the children of the entry at point are sorted.
7987 Sorting can be alphabetically, numerically, by date/time as given by
7988 a time stamp, by a property or by priority.
7990 The command prompts for the sorting type unless it has been given to the
7991 function through the SORTING-TYPE argument, which needs to be a character,
7992 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
7993 precise meaning of each character:
7995 n Numerically, by converting the beginning of the entry/item to a number.
7996 a Alphabetically, ignoring the TODO keyword and the priority, if any.
7997 t By date/time, either the first active time stamp in the entry, or, if
7998 none exist, by the first inactive one.
7999 s By the scheduled date/time.
8000 d By deadline date/time.
8001 c By creation time, which is assumed to be the first inactive time stamp
8002 at the beginning of a line.
8003 p By priority according to the cookie.
8004 r By the value of a property.
8006 Capital letters will reverse the sort order.
8008 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8009 called with point at the beginning of the record. It must return either
8010 a string or a number that should serve as the sorting key for that record.
8012 Comparing entries ignores case by default. However, with an optional argument
8013 WITH-CASE, the sorting considers case as well."
8015 (let ((case-func (if with-case 'identity 'downcase))
8016 start beg end stars re re2
8018 ;; Find beginning and end of region to sort
8020 ((org-region-active-p)
8021 ;; we will sort the region
8022 (setq end (region-end)
8024 (goto-char (region-beginning))
8025 (if (not (org-on-heading-p)) (outline-next-heading))
8026 (setq start (point)))
8027 ((or (org-on-heading-p)
8028 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8029 ;; we will sort the children of the current headline
8030 (org-back-to-heading)
8032 end (progn (org-end-of-subtree t t)
8033 (or (bolp) (insert "\n"))
8034 (org-back-over-empty-lines)
8039 (outline-next-heading))
8041 ;; we will sort the top-level entries in this file
8042 (goto-char (point-min))
8043 (or (org-on-heading-p) (outline-next-heading))
8044 (setq start (point))
8045 (goto-char (point-max))
8046 (beginning-of-line 1)
8047 (when (looking-at ".*?\\S-")
8048 ;; File ends in a non-white line
8051 (setq end (point-max))
8052 (setq what "top-level")
8057 (if (>= beg end) (error "Nothing to sort"))
8059 (looking-at "\\(\\*+\\)")
8060 (setq stars (match-string 1)
8061 re (concat "^" (regexp-quote stars) " +")
8062 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8063 txt (buffer-substring beg end))
8064 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8065 (if (and (not (equal stars "*")) (string-match re2 txt))
8066 (error "Region to sort contains a level above the first entry"))
8068 (unless sorting-type
8070 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8071 [t]ime
[s]cheduled [d]eadline [c]reated
8072 A/N/T/S/D/C/P/O/F means reversed:"
8074 (setq sorting-type (read-char-exclusive))
8076 (and (= (downcase sorting-type) ?f)
8078 (org-icompleting-read "Sort using function: "
8079 obarray 'fboundp t nil nil))
8080 (setq getkey-func (intern getkey-func)))
8082 (and (= (downcase sorting-type) ?r)
8084 (org-icompleting-read "Property: "
8085 (mapcar 'list (org-buffer-property-keys t))
8088 (message "Sorting entries...")
8091 (narrow-to-region start end)
8092 (let ((dcst (downcase sorting-type))
8093 (case-fold-search nil)
8094 (now (current-time)))
8096 (/= dcst sorting-type)
8097 ;; This function moves to the beginning character of the "record" to
8100 (if (re-search-forward re nil t)
8101 (goto-char (match-beginning 0))
8102 (goto-char (point-max))))
8103 ;; This function moves to the last character of the "record" being
8108 (outline-forward-same-level 1)
8110 (goto-char (point-max))))))
8111 ;; This function returns the value that gets sorted against.
8115 (if (looking-at org-complex-heading-regexp)
8116 (string-to-number (match-string 4))
8119 (if (looking-at org-complex-heading-regexp)
8120 (funcall case-func (match-string 4))
8123 (let ((end (save-excursion (outline-next-heading) (point))))
8124 (if (or (re-search-forward org-ts-regexp end t)
8125 (re-search-forward org-ts-regexp-both end t))
8126 (org-time-string-to-seconds (match-string 0))
8127 (org-float-time now))))
8129 (let ((end (save-excursion (outline-next-heading) (point))))
8130 (if (re-search-forward
8131 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8133 (org-time-string-to-seconds (match-string 0))
8134 (org-float-time now))))
8136 (let ((end (save-excursion (outline-next-heading) (point))))
8137 (if (re-search-forward org-scheduled-time-regexp end t)
8138 (org-time-string-to-seconds (match-string 1))
8139 (org-float-time now))))
8141 (let ((end (save-excursion (outline-next-heading) (point))))
8142 (if (re-search-forward org-deadline-time-regexp end t)
8143 (org-time-string-to-seconds (match-string 1))
8144 (org-float-time now))))
8146 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8147 (string-to-char (match-string 2))
8148 org-default-priority))
8150 (or (org-entry-get nil property) ""))
8152 (if (looking-at org-complex-heading-regexp)
8153 (- 9999 (length (member (match-string 2)
8154 org-todo-keywords-1)))))
8158 (setq tmp (funcall getkey-func))
8159 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8161 (error "Invalid key function `%s'" getkey-func)))
8162 (t (error "Invalid sorting type `%c'" sorting-type))))
8165 ((= dcst ?a) 'string<)
8166 ((= dcst ?f) compare-func)
8167 ((member dcst '(?p ?t ?s ?d ?c)) '<)
8169 (run-hooks 'org-after-sorting-entries-or-items-hook)
8170 (message "Sorting entries...done")))
8172 (defun org-do-sort (table what &optional with-case sorting-type)
8173 "Sort TABLE of WHAT according to SORTING-TYPE.
8174 The user will be prompted for the SORTING-TYPE if the call to this
8175 function does not specify it. WHAT is only for the prompt, to indicate
8176 what is being sorted. The sorting key will be extracted from
8177 the car of the elements of the table.
8178 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8179 (unless sorting-type
8181 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
8183 (setq sorting-type (read-char-exclusive)))
8184 (let ((dcst (downcase sorting-type))
8185 extractfun comparefun)
8186 ;; Define the appropriate functions
8189 (setq extractfun 'string-to-number
8190 comparefun (if (= dcst sorting-type) '< '>)))
8192 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8193 (lambda(x) (downcase (org-sort-remove-invisible x))))
8194 comparefun (if (= dcst sorting-type)
8196 (lambda (a b) (and (not (string< a b))
8197 (not (string= a b)))))))
8201 (if (or (string-match org-ts-regexp x)
8202 (string-match org-ts-regexp-both x))
8204 (org-time-string-to-time (match-string 0 x)))
8206 comparefun (if (= dcst sorting-type) '< '>)))
8207 (t (error "Invalid sorting type `%c'" sorting-type)))
8209 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8211 (lambda (a b) (funcall comparefun (car a) (car b))))))
8214 ;;; The orgstruct minor mode
8216 ;; Define a minor mode which can be used in other modes in order to
8217 ;; integrate the org-mode structure editing commands.
8219 ;; This is really a hack, because the org-mode structure commands use
8220 ;; keys which normally belong to the major mode. Here is how it
8221 ;; works: The minor mode defines all the keys necessary to operate the
8222 ;; structure commands, but wraps the commands into a function which
8223 ;; tests if the cursor is currently at a headline or a plain list
8224 ;; item. If that is the case, the structure command is used,
8225 ;; temporarily setting many Org-mode variables like regular
8226 ;; expressions for filling etc. However, when any of those keys is
8227 ;; used at a different location, function uses `key-binding' to look
8228 ;; up if the key has an associated command in another currently active
8229 ;; keymap (minor modes, major mode, global), and executes that
8230 ;; command. There might be problems if any of the keys is otherwise
8231 ;; used as a prefix key.
8233 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8234 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8235 ;; addresses this by checking explicitly for both bindings.
8237 (defvar orgstruct-mode-map (make-sparse-keymap)
8238 "Keymap for the minor `orgstruct-mode'.")
8240 (defvar org-local-vars nil
8241 "List of local variables, for use by `orgstruct-mode'.")
8244 (define-minor-mode orgstruct-mode
8245 "Toggle the minor mode `orgstruct-mode'.
8246 This mode is for using Org-mode structure commands in other
8247 modes. The following keys behave as if Org-mode were active, if
8248 the cursor is on a headline, or on a plain list item (both as
8249 defined by Org-mode).
8251 M-up Move entry/item up
8252 M-down Move entry/item down
8255 M-S-up Move entry/item up
8256 M-S-down Move entry/item down
8257 M-S-left Promote subtree
8258 M-S-right Demote subtree
8259 M-q Fill paragraph and items like in Org-mode
8261 C-c - Cycle list bullet
8262 TAB Cycle item visibility
8263 M-RET Insert new heading/item
8264 S-M-RET Insert new TODO heading / Checkbox item
8265 C-c C-c Set tags / toggle checkbox"
8266 nil " OrgStruct" nil
8267 (org-load-modules-maybe)
8268 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8271 (defun turn-on-orgstruct ()
8272 "Unconditionally turn on `orgstruct-mode'."
8275 (defun orgstruct++-mode (&optional arg)
8276 "Toggle `orgstruct-mode', the enhanced version of it.
8277 In addition to setting orgstruct-mode, this also exports all indentation
8278 and autofilling variables from org-mode into the buffer. It will also
8279 recognize item context in multiline items.
8280 Note that turning off orgstruct-mode will *not* remove the
8281 indentation/paragraph settings. This can only be done by refreshing the
8282 major mode, for example with \\[normal-mode]."
8284 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8292 "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8293 (symbol-name (car x)))
8294 (setq var (car x) val (nth 1 x))
8295 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8297 (org-set-local 'orgstruct-is-++ t))))
8299 (defvar orgstruct-is-++ nil
8300 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8301 (make-variable-buffer-local 'orgstruct-is-++)
8304 (defun turn-on-orgstruct++ ()
8305 "Unconditionally turn on `orgstruct++-mode'."
8306 (orgstruct++-mode 1))
8308 (defun orgstruct-error ()
8309 "Error when there is no default binding for a structure key."
8311 (error "This key has no function outside structure elements"))
8313 (defun orgstruct-setup ()
8314 "Setup orgstruct keymaps."
8318 '([(meta up)] org-metaup)
8319 '([(meta down)] org-metadown)
8320 '([(meta left)] org-metaleft)
8321 '([(meta right)] org-metaright)
8322 '([(meta shift up)] org-shiftmetaup)
8323 '([(meta shift down)] org-shiftmetadown)
8324 '([(meta shift left)] org-shiftmetaleft)
8325 '([(meta shift right)] org-shiftmetaright)
8326 '([?\e (up)] org-metaup)
8327 '([?\e (down)] org-metadown)
8328 '([?\e (left)] org-metaleft)
8329 '([?\e (right)] org-metaright)
8330 '([?\e (shift up)] org-shiftmetaup)
8331 '([?\e (shift down)] org-shiftmetadown)
8332 '([?\e (shift left)] org-shiftmetaleft)
8333 '([?\e (shift right)] org-shiftmetaright)
8334 '([(shift up)] org-shiftup)
8335 '([(shift down)] org-shiftdown)
8336 '([(shift left)] org-shiftleft)
8337 '([(shift right)] org-shiftright)
8338 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8339 '("\M-q" fill-paragraph)
8341 '("\C-c-" org-cycle-list-bullet)))
8343 (while (setq elt (pop bindings))
8344 (setq nfunc (1+ nfunc))
8345 (setq key (org-key (car elt))
8347 cmd (orgstruct-make-binding fun nfunc key))
8348 (org-defkey orgstruct-mode-map key cmd))
8350 ;; Special treatment needed for TAB and RET
8351 (org-defkey orgstruct-mode-map [(tab)]
8352 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8353 (org-defkey orgstruct-mode-map "\C-i"
8354 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8356 (org-defkey orgstruct-mode-map "\M-\C-m"
8357 (orgstruct-make-binding 'org-insert-heading 105
8358 "\M-\C-m" [(meta return)]))
8359 (org-defkey orgstruct-mode-map [(meta return)]
8360 (orgstruct-make-binding 'org-insert-heading 106
8361 [(meta return)] "\M-\C-m"))
8363 (org-defkey orgstruct-mode-map [(shift meta return)]
8364 (orgstruct-make-binding 'org-insert-todo-heading 107
8365 [(meta return)] "\M-\C-m"))
8367 (org-defkey orgstruct-mode-map "\e\C-m"
8368 (orgstruct-make-binding 'org-insert-heading 108
8369 "\e\C-m" [?\e (return)]))
8370 (org-defkey orgstruct-mode-map [?\e (return)]
8371 (orgstruct-make-binding 'org-insert-heading 109
8372 [?\e (return)] "\e\C-m"))
8373 (org-defkey orgstruct-mode-map [?\e (shift return)]
8374 (orgstruct-make-binding 'org-insert-todo-heading 110
8375 [?\e (return)] "\e\C-m"))
8377 (unless org-local-vars
8378 (setq org-local-vars (org-get-local-variables)))
8382 (defun orgstruct-make-binding (fun n &rest keys)
8383 "Create a function for binding in the structure minor mode.
8384 FUN is the command to call inside a table. N is used to create a unique
8385 command name. KEYS are keys that should be checked in for a command
8386 to execute outside of tables."
8389 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8391 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8392 "Outside of structure, run the binding of `"
8393 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8397 `(org-context-p 'headline 'item
8398 (and orgstruct-is-++
8399 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8401 (list 'org-run-like-in-org-mode (list 'quote fun))
8402 (list 'let '(orgstruct-mode)
8403 (list 'call-interactively
8406 (list 'key-binding k))
8408 '('orgstruct-error))))))))
8410 (defun org-context-p (&rest contexts)
8411 "Check if local context is any of CONTEXTS.
8412 Possible values in the list of contexts are `table', `headline', and `item'."
8413 (let ((pos (point)))
8414 (goto-char (point-at-bol))
8415 (prog1 (or (and (memq 'table contexts)
8416 (looking-at "[ \t]*|"))
8417 (and (memq 'headline contexts)
8418 (looking-at org-outline-regexp))
8419 (and (memq 'item contexts)
8420 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8421 (and (memq 'item-body contexts)
8425 (defun org-get-local-variables ()
8426 "Return a list of all local variables in an org-mode buffer."
8428 (with-current-buffer (get-buffer-create "*Org tmp*")
8431 (setq varlist (buffer-local-variables)))
8432 (kill-buffer "*Org tmp*")
8439 (list (car x) (list 'quote (cdr x)))))
8441 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
8442 (symbol-name (car x)))
8446 (defun org-clone-local-variables (from-buffer &optional regexp)
8447 "Clone local variables from FROM-BUFFER.
8448 Optional argument REGEXP selects variables to clone."
8451 (and (symbolp (car pair))
8453 (string-match regexp (symbol-name (car pair))))
8454 (set (make-local-variable (car pair))
8456 (buffer-local-variables from-buffer)))
8459 (defun org-run-like-in-org-mode (cmd)
8460 "Run a command, pretending that the current buffer is in Org-mode.
8461 This will temporarily bind local variables that are typically bound in
8462 Org-mode to the values they have in Org-mode, and then interactively
8464 (org-load-modules-maybe)
8465 (unless org-local-vars
8466 (setq org-local-vars (org-get-local-variables)))
8467 (eval (list 'let org-local-vars
8468 (list 'call-interactively (list 'quote cmd)))))
8472 (defun org-get-category (&optional pos force-refresh)
8473 "Get the category applying to position POS."
8474 (if force-refresh (org-refresh-category-properties))
8475 (let ((pos (or pos (point))))
8476 (or (get-text-property pos 'org-category)
8477 (progn (org-refresh-category-properties)
8478 (get-text-property pos 'org-category)))))
8480 (defun org-refresh-category-properties ()
8481 "Refresh category text properties in the buffer."
8482 (let ((def-cat (cond
8483 ((null org-category)
8484 (if buffer-file-name
8485 (file-name-sans-extension
8486 (file-name-nondirectory buffer-file-name))
8488 ((symbolp org-category) (symbol-name org-category))
8490 beg end cat pos optionp)
8495 (goto-char (point-min))
8496 (put-text-property (point) (point-max) 'org-category def-cat)
8497 (while (re-search-forward
8498 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8499 (setq pos (match-end 0)
8500 optionp (equal (char-after (match-beginning 0)) ?#)
8501 cat (org-trim (match-string 2)))
8503 (setq beg (point-at-bol) end (point-max))
8504 (org-back-to-heading t)
8505 (setq beg (point) end (org-end-of-subtree t t)))
8506 (put-text-property beg end 'org-category cat)
8507 (put-text-property beg end 'org-category-position beg)
8508 (goto-char pos)))))))
8513 ;;; Link abbreviations
8515 (defun org-link-expand-abbrev (link)
8516 "Apply replacements as defined in `org-link-abbrev-alist."
8517 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8518 (let* ((key (match-string 1 link))
8519 (as (or (assoc key org-link-abbrev-alist-local)
8520 (assoc key org-link-abbrev-alist)))
8521 (tag (and (match-end 2) (match-string 3 link)))
8527 ((symbolp rpl) (funcall rpl tag))
8528 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8529 ((string-match "%h" rpl)
8530 (replace-match (url-hexify-string (or tag "")) t t rpl))
8531 (t (concat rpl tag)))))
8534 ;;; Storing and inserting links
8536 (defvar org-insert-link-history nil
8537 "Minibuffer history for links inserted with `org-insert-link'.")
8539 (defvar org-stored-links nil
8540 "Contains the links stored with `org-store-link'.")
8542 (defvar org-store-link-plist nil
8543 "Plist with info about the most recently link created with `org-store-link'.")
8545 (defvar org-link-protocols nil
8546 "Link protocols added to Org-mode using `org-add-link-type'.")
8548 (defvar org-store-link-functions nil
8549 "List of functions that are called to create and store a link.
8550 Each function will be called in turn until one returns a non-nil
8551 value. Each function should check if it is responsible for creating
8552 this link (for example by looking at the major mode).
8553 If not, it must exit and return nil.
8554 If yes, it should return a non-nil value after a calling
8555 `org-store-link-props' with a list of properties and values.
8556 Special properties are:
8558 :type The link prefix, like \"http\". This must be given.
8559 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8560 This is obligatory as well.
8561 :description Optional default description for the second pair
8562 of brackets in an Org-mode link. The user can still change
8563 this when inserting this link into an Org-mode buffer.
8565 In addition to these, any additional properties can be specified
8566 and then used in remember templates.")
8568 (defun org-add-link-type (type &optional follow export)
8569 "Add TYPE to the list of `org-link-types'.
8570 Re-compute all regular expressions depending on `org-link-types'
8572 FOLLOW and EXPORT are two functions.
8574 FOLLOW should take the link path as the single argument and do whatever
8575 is necessary to follow the link, for example find a file or display
8578 EXPORT should format the link path for export to one of the export formats.
8579 It should be a function accepting three arguments:
8581 path the path of the link, the text after the prefix (like \"http:\")
8582 desc the description of the link, if any, or a description added by
8583 org-export-normalize-links if there is none
8584 format the export format, a symbol like `html' or `latex' or `ascii'..
8586 The function may use the FORMAT information to return different values
8587 depending on the format. The return value will be put literally into
8588 the exported file. If the return value is nil, this means Org should
8589 do what it normally does with links which do not have EXPORT defined.
8591 Org-mode has a built-in default for exporting links. If you are happy with
8592 this default, there is no need to define an export function for the link
8593 type. For a simple example of an export function, see `org-bbdb.el'."
8594 (add-to-list 'org-link-types type t)
8595 (org-make-link-regexps)
8596 (if (assoc type org-link-protocols)
8597 (setcdr (assoc type org-link-protocols) (list follow export))
8598 (push (list type follow export) org-link-protocols)))
8600 (defvar org-agenda-buffer-name)
8603 (defun org-store-link (arg)
8604 "\\<org-mode-map>Store an org-link to the current location.
8605 This link is added to `org-stored-links' and can later be inserted
8606 into an org-buffer with \\[org-insert-link].
8608 For some link types, a prefix arg is interpreted:
8609 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8610 For file links, arg negates `org-context-in-file-links'."
8612 (org-load-modules-maybe)
8613 (setq org-store-link-plist nil) ; reset
8614 (org-with-limited-levels
8615 (let (link cpltxt desc description search txt custom-id agenda-link)
8618 ((run-hook-with-args-until-success 'org-store-link-functions)
8619 (setq link (plist-get org-store-link-plist :link)
8620 desc (or (plist-get org-store-link-plist :description) link)))
8622 ((equal (buffer-name) "*Org Edit Src Example*")
8624 (while (or (not label)
8628 (goto-char (point-min))
8630 (regexp-quote (format org-coderef-label-format label))
8632 (when label (message "Label exists already") (sit-for 2))
8633 (setq label (read-string "Code line label: " label)))
8635 (setq link (format org-coderef-label-format label))
8636 (setq gc (- 79 (length link)))
8637 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8639 (setq link (concat "(" label ")") desc nil)))
8641 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8642 ;; We are in the agenda, link to referenced location
8643 (let ((m (or (get-text-property (point) 'org-hd-marker)
8644 (get-text-property (point) 'org-marker))))
8646 (org-with-point-at m
8648 (if (org-called-interactively-p 'any)
8649 (call-interactively 'org-store-link)
8650 (org-store-link nil)))))))
8652 ((eq major-mode 'calendar-mode)
8653 (let ((cd (calendar-cursor-to-date)))
8656 (car org-time-stamp-formats)
8658 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8660 (org-store-link-props :type "calendar" :date cd)))
8662 ((eq major-mode 'w3-mode)
8663 (setq cpltxt (if (and (buffer-name)
8664 (not (string-match "Untitled" (buffer-name))))
8667 link (org-make-link (url-view-url t)))
8668 (org-store-link-props :type "w3" :url (url-view-url t)))
8670 ((eq major-mode 'w3m-mode)
8671 (setq cpltxt (or w3m-current-title w3m-current-url)
8672 link (org-make-link w3m-current-url))
8673 (org-store-link-props :type "w3m" :url (url-view-url t)))
8675 ((setq search (run-hook-with-args-until-success
8676 'org-create-file-search-functions))
8677 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8679 (setq cpltxt (or description link)))
8681 ((eq major-mode 'image-mode)
8682 (setq cpltxt (concat "file:"
8683 (abbreviate-file-name buffer-file-name))
8684 link (org-make-link cpltxt))
8685 (org-store-link-props :type "image" :file buffer-file-name))
8687 ((eq major-mode 'dired-mode)
8688 ;; link to the file in the current line
8689 (let ((file (dired-get-filename nil t)))
8691 (abbreviate-file-name
8692 (expand-file-name (dired-get-filename nil t)))
8693 ;; otherwise, no file so use current directory.
8695 (setq cpltxt (concat "file:" file)
8696 link (org-make-link cpltxt))))
8698 ((and (buffer-file-name (buffer-base-buffer)) (eq major-mode 'org-mode))
8699 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8701 ((org-in-regexp "<<\\(.*?\\)>>")
8704 (abbreviate-file-name
8705 (buffer-file-name (buffer-base-buffer)))
8706 "::" (match-string 1))
8707 link (org-make-link cpltxt)))
8708 ((and (featurep 'org-id)
8709 (or (eq org-link-to-org-use-id t)
8710 (and (eq org-link-to-org-use-id 'create-if-interactive)
8711 (org-called-interactively-p 'any))
8712 (and (eq org-link-to-org-use-id
8713 'create-if-interactive-and-no-custom-id)
8714 (org-called-interactively-p 'any)
8716 (and org-link-to-org-use-id
8717 (org-entry-get nil "ID"))))
8718 ;; We can make a link using the ID.
8719 (setq link (condition-case nil
8720 (prog1 (org-id-store-link)
8721 (setq desc (plist-get org-store-link-plist
8724 ;; probably before first headline, link to file only
8726 (abbreviate-file-name
8727 (buffer-file-name (buffer-base-buffer))))))))
8729 ;; Just link to current headline
8730 (setq cpltxt (concat "file:"
8731 (abbreviate-file-name
8732 (buffer-file-name (buffer-base-buffer)))))
8733 ;; Add a context search string
8734 (when (org-xor org-context-in-file-links arg)
8736 ((org-on-heading-p) nil)
8737 ((org-region-active-p)
8738 (buffer-substring (region-beginning) (region-end)))
8740 (when (or (null txt) (string-match "\\S-" txt))
8744 (org-make-org-heading-search-string txt)
8746 desc (or (nth 4 (ignore-errors
8747 (org-heading-components))) "NONE"))))
8748 (if (string-match "::\\'" cpltxt)
8749 (setq cpltxt (substring cpltxt 0 -2)))
8750 (setq link (org-make-link cpltxt)))))
8752 ((buffer-file-name (buffer-base-buffer))
8753 ;; Just link to this file here.
8754 (setq cpltxt (concat "file:"
8755 (abbreviate-file-name
8756 (buffer-file-name (buffer-base-buffer)))))
8757 ;; Add a context string
8758 (when (org-xor org-context-in-file-links arg)
8759 (setq txt (if (org-region-active-p)
8760 (buffer-substring (region-beginning) (region-end))
8761 (buffer-substring (point-at-bol) (point-at-eol))))
8762 ;; Only use search option if there is some text.
8763 (when (string-match "\\S-" txt)
8765 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8767 (setq link (org-make-link cpltxt)))
8769 ((org-called-interactively-p 'interactive)
8770 (error "Cannot link to a buffer which is not visiting a file"))
8772 (t (setq link nil)))
8774 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8775 (setq link (or link cpltxt)
8776 desc (or desc cpltxt))
8777 (if (equal desc "NONE") (setq desc nil))
8779 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8781 (setq org-stored-links
8782 (cons (list link desc) org-stored-links))
8783 (message "Stored: %s" (or desc link))
8785 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8787 (setq org-stored-links
8788 (cons (list link desc) org-stored-links))))
8789 (or agenda-link (and link (org-make-link-string link desc)))))))
8791 (defun org-store-link-props (&rest plist)
8792 "Store link properties, extract names and addresses."
8794 (when (setq x (plist-get plist :from))
8795 (setq adr (mail-extract-address-components x))
8796 (setq plist (plist-put plist :fromname (car adr)))
8797 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8798 (when (setq x (plist-get plist :to))
8799 (setq adr (mail-extract-address-components x))
8800 (setq plist (plist-put plist :toname (car adr)))
8801 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8802 (let ((from (plist-get plist :from))
8803 (to (plist-get plist :to)))
8804 (when (and from to org-from-is-user-regexp)
8806 (plist-put plist :fromto
8807 (if (string-match org-from-is-user-regexp from)
8809 (concat "from %f"))))))
8810 (setq org-store-link-plist plist))
8812 (defun org-add-link-props (&rest plist)
8813 "Add these properties to the link property list."
8816 (setq key (pop plist) value (pop plist))
8817 (setq org-store-link-plist
8818 (plist-put org-store-link-plist key value)))))
8820 (defun org-email-link-description (&optional fmt)
8821 "Return the description part of an email link.
8822 This takes information from `org-store-link-plist' and formats it
8823 according to FMT (default from `org-email-link-description-format')."
8824 (setq fmt (or fmt org-email-link-description-format))
8825 (let* ((p org-store-link-plist)
8826 (to (plist-get p :toaddress))
8827 (from (plist-get p :fromaddress))
8830 (cons "%c" (plist-get p :fromto))
8831 (cons "%F" (plist-get p :from))
8832 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8833 (cons "%T" (plist-get p :to))
8834 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8835 (cons "%s" (plist-get p :subject))
8836 (cons "%d" (plist-get p :date))
8837 (cons "%m" (plist-get p :message-id)))))
8838 (when (string-match "%c" fmt)
8839 ;; Check if the user wrote this message
8840 (if (and org-from-is-user-regexp from to
8841 (save-match-data (string-match org-from-is-user-regexp from)))
8842 (setq fmt (replace-match "to %t" t t fmt))
8843 (setq fmt (replace-match "from %f" t t fmt))))
8844 (org-replace-escapes fmt table)))
8846 (defun org-make-org-heading-search-string (&optional string heading)
8847 "Make search string for STRING or current headline."
8849 (let ((s (or string (org-get-heading)))
8850 (lines org-context-in-file-links))
8851 (unless (and string (not heading))
8852 ;; We are using a headline, clean up garbage in there.
8853 (if (string-match org-todo-regexp s)
8854 (setq s (replace-match "" t t s)))
8855 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
8856 (setq s (replace-match "" t t s)))
8857 (setq s (org-trim s))
8858 (if (string-match (concat "^\\(" org-quote-string "\\|"
8859 org-comment-string "\\)") s)
8860 (setq s (replace-match "" t t s)))
8861 (while (string-match org-ts-regexp s)
8862 (setq s (replace-match "" t t s))))
8863 (or string (setq s (concat "*" s))) ; Add * for headlines
8864 (when (and string (integerp lines) (> lines 0))
8865 (let ((slines (org-split-string s "\n")))
8866 (when (< lines (length slines))
8869 (reverse (nthcdr (- (length slines) lines)
8870 (reverse slines))) "\n")))))
8871 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8873 (defun org-make-link (&rest strings)
8874 "Concatenate STRINGS."
8875 (apply 'concat strings))
8877 (defun org-make-link-string (link &optional description)
8878 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8879 (unless (string-match "\\S-" link)
8880 (error "Empty link"))
8881 (when (and description
8882 (stringp description)
8883 (not (string-match "\\S-" description)))
8884 (setq description nil))
8885 (when (stringp description)
8886 ;; Remove brackets from the description, they are fatal.
8887 (while (string-match "\\[" description)
8888 (setq description (replace-match "{" t t description)))
8889 (while (string-match "\\]" description)
8890 (setq description (replace-match "}" t t description))))
8891 (when (equal link description)
8892 ;; No description needed, it is identical
8893 (setq description nil))
8894 (when (and (not description)
8895 (not (string-match (org-image-file-name-regexp) link))
8896 (not (equal link (org-link-escape link))))
8897 (setq description (org-extract-attributes link)))
8899 (cond ((string-match (org-image-file-name-regexp) link) link)
8900 ((string-match org-link-types-re link)
8901 (concat (match-string 1 link)
8902 (org-link-escape (substring link (match-end 1)))))
8903 (t (org-link-escape link))))
8904 (concat "[[" link "]"
8905 (if description (concat "[" description "]") "")
8908 (defconst org-link-escape-chars
8909 '(?\ ?\[ ?\] ?\; ?\= ?\+)
8910 "List of characters that should be escaped in link.
8911 This is the list that is used for internal purposes.")
8913 (defvar org-url-encoding-use-url-hexify nil)
8915 (defconst org-link-escape-chars-browser
8917 "List of escapes for characters that are problematic in links.
8918 This is the list that is used before handing over to the browser.")
8920 (defun org-link-escape (text &optional table merge)
8921 "Return percent escaped representation of TEXT.
8922 TEXT is a string with the text to escape.
8923 Optional argument TABLE is a list with characters that should be
8924 escaped. When nil, `org-link-escape-chars' is used.
8925 If optional argument MERGE is set, merge TABLE into
8926 `org-link-escape-chars'."
8927 (if (and org-url-encoding-use-url-hexify (not table))
8928 (url-hexify-string text)
8931 (mapc (lambda (defchr)
8932 (unless (member defchr table)
8933 (setq table (cons defchr table)))) org-link-escape-chars))
8935 (setq table org-link-escape-chars)))
8938 (if (or (member char table)
8939 (< char 32) (= char 37) (> char 126))
8940 (mapconcat (lambda (sequence-element)
8941 (format "%%%.2X" sequence-element))
8942 (or (encode-coding-char char 'utf-8)
8943 (error "Unable to percent escape character: %s"
8944 (char-to-string char))) "")
8945 (char-to-string char))) text "")))
8947 (defun org-link-unescape (str)
8948 "Unhex hexified Unicode strings as returned from the JavaScript function
8949 encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
8950 (unless (and (null str) (string= "" str))
8951 (let ((pos 0) (case-fold-search t) unhexed)
8952 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
8953 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
8954 (setq str (replace-match unhexed t t str))
8955 (setq pos (+ pos (length unhexed))))))
8958 (defun org-link-unescape-compound (hex)
8959 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
8960 Note: this function also decodes single byte encodings like
8961 `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
8963 (let* ((bytes (cdr (split-string hex "%")))
8968 (let* ((val (string-to-number (pop bytes) 16))
8972 ((>= val 252) (cons 6 252))
8973 ((>= val 248) (cons 5 248))
8974 ((>= val 240) (cons 4 240))
8975 ((>= val 224) (cons 3 224))
8976 ((>= val 192) (cons 2 192))
8979 (if (>= val 192) (setq eat (car shift-xor)))
8980 (setq val (logxor val (cdr shift-xor)))
8981 (setq sum (+ (lsh sum (car shift-xor)) val))
8982 (if (> eat 0) (setq eat (- eat 1)))
8984 ((= 0 eat) ;multi byte
8985 (setq ret (concat ret (org-char-to-string sum)))
8987 ((not bytes) ; single byte(s)
8988 (setq ret (org-link-unescape-single-byte-sequence hex))))
8989 )) ;; end (while bytes
8992 (defun org-link-unescape-single-byte-sequence (hex)
8993 "Unhexify hex-encoded single byte character sequences."
8994 (mapconcat (lambda (byte)
8995 (char-to-string (string-to-number byte 16)))
8996 (cdr (split-string hex "%")) ""))
8998 (defun org-xor (a b)
9002 (defun org-fixup-message-id-for-http (s)
9003 "Replace special characters in a message id, so it can be used in an http query."
9004 (when (string-match "%" s)
9005 (setq s (mapconcat (lambda (c)
9008 (char-to-string c)))
9010 (while (string-match "<" s)
9011 (setq s (replace-match "%3C" t t s)))
9012 (while (string-match ">" s)
9013 (setq s (replace-match "%3E" t t s)))
9014 (while (string-match "@" s)
9015 (setq s (replace-match "%40" t t s)))
9019 (defun org-insert-link-global ()
9020 "Insert a link like Org-mode does.
9021 This command can be called in any mode to insert a link in Org-mode syntax."
9023 (org-load-modules-maybe)
9024 (org-run-like-in-org-mode 'org-insert-link))
9026 (defun org-insert-link (&optional complete-file link-location default-description)
9027 "Insert a link. At the prompt, enter the link.
9029 Completion can be used to insert any of the link protocol prefixes like
9032 The history can be used to select a link previously stored with
9033 `org-store-link'. When the empty string is entered (i.e. if you just
9034 press RET at the prompt), the link defaults to the most recently
9035 stored link. As SPC triggers completion in the minibuffer, you need to
9036 use M-SPC or C-q SPC to force the insertion of a space character.
9038 You will also be prompted for a description, and if one is given, it will
9039 be displayed in the buffer instead of the link.
9041 If there is already a link at point, this command will allow you to edit link
9042 and description parts.
9044 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9045 be selected using completion. The path to the file will be relative to the
9046 current directory if the file is in the current directory or a subdirectory.
9047 Otherwise, the link will be the absolute path as completed in the minibuffer
9048 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9049 option `org-link-file-path-type'.
9051 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9052 the current directory or below.
9054 With three \\[universal-argument] prefixes, negate the meaning of
9055 `org-keep-stored-link-after-insertion'.
9057 If `org-make-link-description-function' is non-nil, this function will be
9058 called with the link target, and the result will be the default
9061 If the LINK-LOCATION parameter is non-nil, this value will be
9062 used as the link location instead of reading one interactively.
9064 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9065 be used as the default description."
9067 (let* ((wcf (current-window-configuration))
9068 (region (if (org-region-active-p)
9069 (buffer-substring (region-beginning) (region-end))))
9070 (remove (and region (list (region-beginning) (region-end))))
9072 tmphist ; byte-compile incorrectly complains about this
9073 (link link-location)
9074 entry file all-prefixes)
9076 (link-location) ; specified by arg, just use it.
9077 ((org-in-regexp org-bracket-link-regexp 1)
9078 ;; We do have a link at point, and we are going to edit it.
9079 (setq remove (list (match-beginning 0) (match-end 0)))
9080 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9081 (setq link (read-string "Link: "
9083 (org-match-string-no-properties 1)))))
9084 ((or (org-in-regexp org-angle-link-re)
9085 (org-in-regexp org-plain-link-re))
9086 ;; Convert to bracket link
9087 (setq remove (list (match-beginning 0) (match-end 0))
9088 link (read-string "Link: "
9089 (org-remove-angle-brackets (match-string 0)))))
9090 ((member complete-file '((4) (16)))
9091 ;; Completing read for file names.
9092 (setq link (org-file-complete-link complete-file)))
9094 ;; Read link, with completion for stored links.
9095 (with-output-to-temp-buffer "*Org Links*"
9096 (princ "Insert a link.
9097 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9098 (when org-stored-links
9099 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9102 (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
9103 (reverse org-stored-links) "\n"))))
9104 (let ((cw (selected-window)))
9105 (select-window (get-buffer-window "*Org Links*" 'visible))
9106 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9107 (unless (pos-visible-in-window-p (point-max))
9108 (org-fit-window-to-buffer))
9109 (and (window-live-p cw) (select-window cw)))
9110 ;; Fake a link history, containing the stored links.
9111 (setq tmphist (append (mapcar 'car org-stored-links)
9112 org-insert-link-history))
9113 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
9114 (mapcar 'car org-link-abbrev-alist)
9119 (let ((org-completion-use-ido nil)
9120 (org-completion-use-iswitchb nil))
9121 (org-completing-read
9124 (mapcar (lambda (x) (list (concat x ":")))
9126 (mapcar 'car org-stored-links))
9129 (car (car org-stored-links)))))
9130 (if (not (string-match "\\S-" link))
9131 (error "No link selected"))
9132 (if (or (member link all-prefixes)
9133 (and (equal ":" (substring link -1))
9134 (member (substring link 0 -1) all-prefixes)
9135 (setq link (substring link 0 -1))))
9136 (setq link (org-link-try-special-completion link))))
9137 (set-window-configuration wcf)
9138 (kill-buffer "*Org Links*"))
9139 (setq entry (assoc link org-stored-links))
9140 (or entry (push link org-insert-link-history))
9141 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9142 (not org-keep-stored-link-after-insertion))
9143 (setq org-stored-links (delq (assoc link org-stored-links)
9145 (setq desc (or desc (nth 1 entry)))))
9147 (if (string-match org-plain-link-re link)
9148 ;; URL-like link, normalize the use of angular brackets.
9149 (setq link (org-make-link (org-remove-angle-brackets link))))
9151 ;; Check if we are linking to the current file with a search option
9152 ;; If yes, simplify the link by using only the search option.
9153 (when (and buffer-file-name
9154 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
9155 (let* ((path (match-string 1 link))
9156 (case-fold-search nil)
9157 (search (match-string 2 link)))
9159 (if (equal (file-truename buffer-file-name) (file-truename path))
9160 ;; We are linking to this same file, with a search option
9161 (setq link search)))))
9163 ;; Check if we can/should use a relative path. If yes, simplify the link
9164 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9165 (let* ((type (match-string 1 link))
9166 (path (match-string 2 link))
9168 (case-fold-search nil))
9170 ((or (eq org-link-file-path-type 'absolute)
9171 (equal complete-file '(16)))
9172 (setq path (abbreviate-file-name (expand-file-name path))))
9173 ((eq org-link-file-path-type 'noabbrev)
9174 (setq path (expand-file-name path)))
9175 ((eq org-link-file-path-type 'relative)
9176 (setq path (file-relative-name path)))
9179 (if (string-match (concat "^" (regexp-quote
9181 (file-name-as-directory
9182 default-directory))))
9183 (expand-file-name path))
9184 ;; We are linking a file with relative path name.
9185 (setq path (substring (expand-file-name path)
9187 (setq path (abbreviate-file-name (expand-file-name path)))))))
9188 (setq link (concat type path))
9189 (if (equal desc origpath)
9192 (if org-make-link-description-function
9193 (setq desc (funcall org-make-link-description-function link desc))
9194 (if default-description (setq desc default-description)))
9196 (setq desc (read-string "Description: " desc))
9197 (unless (string-match "\\S-" desc) (setq desc nil))
9198 (if remove (apply 'delete-region remove))
9199 (insert (org-make-link-string link desc))))
9201 (defun org-link-try-special-completion (type)
9202 "If there is completion support for link type TYPE, offer it."
9203 (let ((fun (intern (concat "org-" type "-complete-link"))))
9206 (read-string "Link (no completion support): " (concat type ":")))))
9208 (defun org-file-complete-link (&optional arg)
9209 "Create a file link using completion."
9211 (setq file (read-file-name "File: "))
9212 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9213 (pwd1 (file-name-as-directory (abbreviate-file-name
9214 (expand-file-name ".")))))
9217 (setq link (org-make-link
9219 (abbreviate-file-name (expand-file-name file)))))
9220 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9221 (setq link (org-make-link "file:" (match-string 1 file))))
9222 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9223 (expand-file-name file))
9224 (setq link (org-make-link
9225 "file:" (match-string 1 (expand-file-name file)))))
9226 (t (setq link (org-make-link "file:" file)))))
9229 (defun org-completing-read (&rest args)
9230 "Completing-read with SPACE being a normal character."
9231 (let ((enable-recursive-minibuffers t)
9232 (minibuffer-local-completion-map
9233 (copy-keymap minibuffer-local-completion-map)))
9234 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9235 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9236 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9237 (apply 'org-icompleting-read args)))
9239 (defun org-completing-read-no-i (&rest args)
9240 (let (org-completion-use-ido org-completion-use-iswitchb)
9241 (apply 'org-completing-read args)))
9243 (defun org-iswitchb-completing-read (prompt choices &rest args)
9244 "Use iswitch as a completing-read replacement to choose from choices.
9245 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9247 (let* ((iswitchb-use-virtual-buffers nil)
9248 (iswitchb-make-buflist-hook
9250 (setq iswitchb-temp-buflist choices))))
9251 (iswitchb-read-buffer prompt)))
9253 (defun org-icompleting-read (&rest args)
9254 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9255 (org-without-partial-completion
9256 (if (and org-completion-use-ido
9257 (fboundp 'ido-completing-read)
9258 (boundp 'ido-mode) ido-mode
9259 (listp (second args)))
9260 (let ((ido-enter-matching-directory nil))
9261 (apply 'ido-completing-read (concat (car args))
9262 (if (consp (car (nth 1 args)))
9263 (mapcar 'car (nth 1 args))
9266 (if (and org-completion-use-iswitchb
9267 (boundp 'iswitchb-mode) iswitchb-mode
9268 (listp (second args)))
9269 (apply 'org-iswitchb-completing-read (concat (car args))
9270 (if (consp (car (nth 1 args)))
9271 (mapcar 'car (nth 1 args))
9274 (apply 'completing-read args)))))
9276 (defun org-extract-attributes (s)
9277 "Extract the attributes cookie from a string and set as text property."
9278 (let (a attr (start 0) key value)
9280 (when (string-match "{{\\([^}]+\\)}}$" s)
9281 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9282 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9283 (setq key (match-string 1 a) value (match-string 2 a)
9285 attr (plist-put attr (intern key) value))))
9286 (org-add-props s nil 'org-attr attr))
9289 (defun org-extract-attributes-from-string (tag)
9290 (let (key value attr)
9291 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9292 (setq key (match-string 1 tag) value (match-string 2 tag)
9293 tag (replace-match "" t t tag)
9294 attr (plist-put attr (intern key) value)))
9297 (defun org-attributes-to-string (plist)
9298 "Format a property list into an HTML attribute list."
9299 (let ((s "") key value)
9301 (setq key (pop plist) value (pop plist))
9303 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9306 ;;; Opening/following a link
9308 (defvar org-link-search-failed nil)
9310 (defvar org-open-link-functions nil
9311 "Hook for functions finding a plain text link.
9312 These functions must take a single argument, the link content.
9313 They will be called for links that look like [[link text][description]]
9314 when LINK TEXT does not have a protocol like \"http:\" and does not look
9315 like a filename (e.g. \"./blue.png\").
9317 These functions will be called *before* Org attempts to resolve the
9318 link by doing text searches in the current buffer - so if you want a
9319 link \"[[target]]\" to still find \"<<target>>\", your function should
9320 handle this as a special case.
9322 When the function does handle the link, it must return a non-nil value.
9323 If it decides that it is not responsible for this link, it must return
9324 nil to indicate that that Org-mode can continue with other options
9325 like exact and fuzzy text search.")
9327 (defun org-next-link ()
9328 "Move forward to the next link.
9329 If the link is in hidden text, expose it."
9331 (when (and org-link-search-failed (eq this-command last-command))
9332 (goto-char (point-min))
9333 (message "Link search wrapped back to beginning of buffer"))
9334 (setq org-link-search-failed nil)
9335 (let* ((pos (point))
9337 (a (assoc :link ct)))
9338 (if a (goto-char (nth 2 a)))
9339 (if (re-search-forward org-any-link-re nil t)
9341 (goto-char (match-beginning 0))
9342 (if (outline-invisible-p) (org-show-context)))
9344 (setq org-link-search-failed t)
9345 (error "No further link found"))))
9347 (defun org-previous-link ()
9348 "Move backward to the previous link.
9349 If the link is in hidden text, expose it."
9351 (when (and org-link-search-failed (eq this-command last-command))
9352 (goto-char (point-max))
9353 (message "Link search wrapped back to end of buffer"))
9354 (setq org-link-search-failed nil)
9355 (let* ((pos (point))
9357 (a (assoc :link ct)))
9358 (if a (goto-char (nth 1 a)))
9359 (if (re-search-backward org-any-link-re nil t)
9361 (goto-char (match-beginning 0))
9362 (if (outline-invisible-p) (org-show-context)))
9364 (setq org-link-search-failed t)
9365 (error "No further link found"))))
9367 (defun org-translate-link (s)
9368 "Translate a link string if a translation function has been defined."
9369 (if (and org-link-translation-function
9370 (fboundp org-link-translation-function)
9371 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9373 (setq s (funcall org-link-translation-function
9374 (match-string 1) (match-string 2)))
9375 (concat (car s) ":" (cdr s)))
9378 (defun org-translate-link-from-planner (type path)
9379 "Translate a link from Emacs Planner syntax so that Org can follow it.
9380 This is still an experimental function, your mileage may vary."
9382 ((member type '("http" "https" "news" "ftp"))
9383 ;; standard Internet links are the same.
9385 ((and (equal type "irc") (string-match "^//" path))
9386 ;; Planner has two / at the beginning of an irc link, we have 1.
9387 ;; We should have zero, actually....
9388 (setq path (substring path 1)))
9389 ((and (equal type "lisp") (string-match "^/" path))
9390 ;; Planner has a slash, we do not.
9391 (setq type "elisp" path (substring path 1)))
9392 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9393 ;; A typical message link. Planner has the id after the final slash,
9394 ;; we separate it with a hash mark
9395 (setq path (concat (match-string 1 path) "#"
9396 (org-remove-angle-brackets (match-string 2 path)))))
9400 (defun org-find-file-at-mouse (ev)
9401 "Open file link or URL at mouse."
9403 (mouse-set-point ev)
9404 (org-open-at-point 'in-emacs))
9406 (defun org-open-at-mouse (ev)
9407 "Open file link or URL at mouse.
9408 See the docstring of `org-open-file' for details."
9410 (mouse-set-point ev)
9411 (if (eq major-mode 'org-agenda-mode)
9412 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9413 (org-open-at-point))
9415 (defvar org-window-config-before-follow-link nil
9416 "The window configuration before following a link.
9417 This is saved in case the need arises to restore it.")
9419 (defvar org-open-link-marker (make-marker)
9420 "Marker pointing to the location where `org-open-at-point; was called.")
9423 (defun org-open-at-point-global ()
9424 "Follow a link like Org-mode does.
9425 This command can be called in any mode to follow a link that has
9428 (org-run-like-in-org-mode 'org-open-at-point))
9431 (defun org-open-link-from-string (s &optional arg reference-buffer)
9432 "Open a link in the string S, as if it was in Org-mode."
9433 (interactive "sLink: \nP")
9434 (let ((reference-buffer (or reference-buffer (current-buffer))))
9436 (let ((org-inhibit-startup t))
9439 (goto-char (point-min))
9440 (when reference-buffer
9441 (setq org-link-abbrev-alist-local
9442 (with-current-buffer reference-buffer
9443 org-link-abbrev-alist-local)))
9444 (org-open-at-point arg reference-buffer)))))
9446 (defvar org-open-at-point-functions nil
9447 "Hook that is run when following a link at point.
9449 Functions in this hook must return t if they identify and follow
9450 a link at point. If they don't find anything interesting at point,
9451 they must return nil.")
9453 (defun org-open-at-point (&optional arg reference-buffer)
9454 "Open link at or after point.
9455 If there is no link at point, this function will search forward up to
9456 the end of the current line.
9457 Normally, files will be opened by an appropriate application. If the
9458 optional prefix argument ARG is non-nil, Emacs will visit the file.
9459 With a double prefix argument, try to open outside of Emacs, in the
9460 application the system uses for this file type."
9462 ;; if in a code block, then open the block's results
9463 (unless (call-interactively #'org-babel-open-src-block-result)
9464 (org-load-modules-maybe)
9465 (move-marker org-open-link-marker (point))
9466 (setq org-window-config-before-follow-link (current-window-configuration))
9467 (org-remove-occur-highlights nil nil t)
9469 ((and (org-on-heading-p)
9471 (concat org-plain-link-re "\\|"
9472 org-bracket-link-regexp "\\|"
9473 org-angle-link-re "\\|"
9474 "[ \t]:[^ \t\n]+:[ \t]*$")))
9475 (not (get-text-property (point) 'org-linked-text)))
9476 (or (org-offer-links-in-entry arg)
9477 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9478 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9479 ((org-at-timestamp-p t) (org-follow-timestamp-link))
9480 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9481 (not (org-in-regexp org-bracket-link-regexp)))
9482 (org-footnote-action))
9484 (let (type path link line search (pos (point)))
9487 (skip-chars-forward "^]\n\r")
9488 (when (org-in-regexp org-bracket-link-regexp 1)
9489 (setq link (org-extract-attributes
9490 (org-link-unescape (org-match-string-no-properties 1))))
9491 (while (string-match " *\n *" link)
9492 (setq link (replace-match " " t t link)))
9493 (setq link (org-link-expand-abbrev link))
9495 ((or (file-name-absolute-p link)
9496 (string-match "^\\.\\.?/" link))
9497 (setq type "file" path link))
9498 ((string-match org-link-re-with-space3 link)
9499 (setq type (match-string 1 link) path (match-string 2 link)))
9500 (t (setq type "thisfile" path link)))
9503 (when (get-text-property (point) 'org-linked-text)
9504 (setq type "thisfile"
9505 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9506 (1+ (point)) (point))
9507 path (buffer-substring
9508 (or (previous-single-property-change pos 'org-linked-text)
9510 (or (next-single-property-change pos 'org-linked-text)
9515 (when (or (org-in-regexp org-angle-link-re)
9516 (org-in-regexp org-plain-link-re))
9517 (setq type (match-string 1)
9518 path (org-link-unescape (match-string 2)))
9521 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9523 path (match-string 1))
9524 (while (string-match ":" path)
9525 (setq path (replace-match "+" t t path)))
9527 (when (org-in-regexp "<\\([^><\n]+\\)>")
9528 (setq type "tree-match"
9529 path (match-string 1))
9532 (error "No link found"))
9534 ;; switch back to reference buffer
9535 ;; needed when if called in a temporary buffer through
9536 ;; org-open-link-from-string
9537 (with-current-buffer (or reference-buffer (current-buffer))
9539 ;; Remove any trailing spaces in path
9540 (if (string-match " +\\'" path)
9541 (setq path (replace-match "" t t path)))
9542 (if (and org-link-translation-function
9543 (fboundp org-link-translation-function))
9544 ;; Check if we need to translate the link
9545 (let ((tmp (funcall org-link-translation-function type path)))
9546 (setq type (car tmp) path (cdr tmp))))
9550 ((assoc type org-link-protocols)
9551 (funcall (nth 1 (assoc type org-link-protocols)) path))
9553 ((equal type "mailto")
9554 (let ((cmd (car org-link-mailto-program))
9555 (args (cdr org-link-mailto-program)) args1
9556 (address path) (subject "") a)
9557 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9558 (setq address (match-string 1 path)
9559 subject (org-link-escape (match-string 2 path))))
9562 ((not (stringp (car args))) (push (pop args) args1))
9563 (t (setq a (pop args))
9564 (if (string-match "%a" a)
9565 (setq a (replace-match address t t a)))
9566 (if (string-match "%s" a)
9567 (setq a (replace-match subject t t a)))
9569 (apply cmd (nreverse args1))))
9571 ((member type '("http" "https" "ftp" "news"))
9572 (browse-url (concat type ":" (org-link-escape
9573 path org-link-escape-chars-browser))))
9575 ((string= type "doi")
9576 (browse-url (concat "http://dx.doi.org/"
9578 path org-link-escape-chars-browser))))
9580 ((member type '("message"))
9581 (browse-url (concat type ":" path)))
9583 ((string= type "tags")
9584 (org-tags-view arg path))
9586 ((string= type "tree-match")
9587 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9589 ((string= type "file")
9590 (if (string-match "::\\([0-9]+\\)\\'" path)
9591 (setq line (string-to-number (match-string 1 path))
9592 path (substring path 0 (match-beginning 0)))
9593 (if (string-match "::\\(.+\\)\\'" path)
9594 (setq search (match-string 1 path)
9595 path (substring path 0 (match-beginning 0)))))
9596 (if (string-match "[*?{]" (file-name-nondirectory path))
9598 (org-open-file path arg line search)))
9600 ((string= type "shell")
9602 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9603 (string-match org-confirm-shell-link-not-regexp cmd))
9604 (not org-confirm-shell-link-function)
9605 (funcall org-confirm-shell-link-function
9606 (format "Execute \"%s\" in shell? "
9607 (org-add-props cmd nil
9608 'face 'org-warning))))
9610 (message "Executing %s" cmd)
9611 (shell-command cmd))
9614 ((string= type "elisp")
9616 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9617 (string-match org-confirm-elisp-link-not-regexp cmd))
9618 (not org-confirm-elisp-link-function)
9619 (funcall org-confirm-elisp-link-function
9620 (format "Execute \"%s\" as elisp? "
9621 (org-add-props cmd nil
9622 'face 'org-warning))))
9623 (message "%s => %s" cmd
9624 (if (equal (string-to-char cmd) ?\()
9626 (call-interactively (read cmd))))
9629 ((and (string= type "thisfile")
9630 (run-hook-with-args-until-success
9631 'org-open-link-functions path)))
9633 ((string= type "thisfile")
9635 (switch-to-buffer-other-window
9636 (org-get-buffer-for-internal-link (current-buffer)))
9637 (org-mark-ring-push))
9638 (let ((cmd `(org-link-search
9640 ,(cond ((equal arg '(4)) ''occur)
9641 ((equal arg '(16)) ''org-occur)
9644 (condition-case nil (let ((org-link-search-inhibit-query t))
9646 (error (progn (widen) (eval cmd))))))
9649 (browse-url-at-point)))))))
9650 (move-marker org-open-link-marker nil)
9651 (run-hook-with-args 'org-follow-link-hook)))
9653 (defun org-offer-links-in-entry (&optional nth zero)
9654 "Offer links in the current entry and follow the selected link.
9655 If there is only one link, follow it immediately as well.
9656 If NTH is an integer, immediately pick the NTH link found.
9657 If ZERO is a string, check also this string for a link, and if
9658 there is one, offer it as link number zero."
9659 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9660 "\\(" org-angle-link-re "\\)\\|"
9661 "\\(" org-plain-link-re "\\)"))
9663 (in-emacs (if (integerp nth) nil nth))
9664 have-zero end links link c)
9665 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9666 (push (match-string 0 zero) links)
9667 (setq cnt (1- cnt) have-zero t))
9669 (org-back-to-heading t)
9670 (setq end (save-excursion (outline-next-heading) (point)))
9671 (while (re-search-forward re end t)
9672 (push (match-string 0) links))
9673 (setq links (org-uniquify (reverse links))))
9677 (message "No links"))
9678 ((equal (length links) 1)
9679 (setq link (list (car links))))
9680 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9681 (setq link (list (nth (if have-zero nth (1- nth)) links))))
9682 (t ; we have to select a link
9684 (save-window-excursion
9685 (delete-other-windows)
9686 (with-output-to-temp-buffer "*Select Link*"
9688 (if (not (string-match org-bracket-link-regexp l))
9689 (princ (format "[%c] %s\n" (incf cnt)
9690 (org-remove-angle-brackets l)))
9692 (princ (format "[%c] %s (%s)\n" (incf cnt)
9693 (match-string 3 l) (match-string 1 l)))
9694 (princ (format "[%c] %s\n" (incf cnt)
9695 (match-string 1 l))))))
9697 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9698 (message "Select link to open, RET to open all:")
9699 (setq c (read-char-exclusive))
9700 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9701 (when (equal c ?q) (error "Abort"))
9705 (if have-zero (setq nth (1+ nth)))
9706 (unless (and (integerp nth) (>= (length links) nth))
9707 (error "Invalid link selection"))
9708 (setq link (list (nth (1- nth) links))))))
9710 (let ((buf (current-buffer)))
9712 (org-open-link-from-string l in-emacs buf))
9716 ;; Add special file links that specify the way of opening
9718 (org-add-link-type "file+sys" 'org-open-file-with-system)
9719 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9720 (defun org-open-file-with-system (path)
9721 "Open file at PATH using the system way of opening it."
9722 (org-open-file path 'system))
9723 (defun org-open-file-with-emacs (path)
9724 "Open file at PATH in Emacs."
9725 (org-open-file path 'emacs))
9726 (defun org-remove-file-link-modifiers ()
9727 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9728 (goto-char (point-min))
9729 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9731 (replace-match "file:" t t))))
9732 (eval-after-load "org-exp"
9733 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9734 'org-remove-file-link-modifiers))
9738 (defun org-get-effort (&optional pom)
9739 "Get the effort estimate for the current entry."
9740 (org-entry-get pom org-effort-property))
9744 (defvar org-create-file-search-functions nil
9745 "List of functions to construct the right search string for a file link.
9746 These functions are called in turn with point at the location to
9747 which the link should point.
9749 A function in the hook should first test if it would like to
9750 handle this file type, for example by checking the `major-mode'
9751 or the file extension. If it decides not to handle this file, it
9752 should just return nil to give other functions a chance. If it
9753 does handle the file, it must return the search string to be used
9754 when following the link. The search string will be part of the
9755 file link, given after a double colon, and `org-open-at-point'
9756 will automatically search for it. If special measures must be
9757 taken to make the search successful, another function should be
9758 added to the companion hook `org-execute-file-search-functions',
9761 A function in this hook may also use `setq' to set the variable
9762 `description' to provide a suggestion for the descriptive text to
9763 be used for this link when it gets inserted into an Org-mode
9764 buffer with \\[org-insert-link].")
9766 (defvar org-execute-file-search-functions nil
9767 "List of functions to execute a file search triggered by a link.
9769 Functions added to this hook must accept a single argument, the
9770 search string that was part of the file link, the part after the
9771 double colon. The function must first check if it would like to
9772 handle this search, for example by checking the `major-mode' or
9773 the file extension. If it decides not to handle this search, it
9774 should just return nil to give other functions a chance. If it
9775 does handle the search, it must return a non-nil value to keep
9776 other functions from trying.
9778 Each function can access the current prefix argument through the
9779 variable `current-prefix-argument'. Note that a single prefix is
9780 used to force opening a link in Emacs, so it may be good to only
9781 use a numeric or double prefix to guide the search function.
9783 In case this is needed, a function in this hook can also restore
9784 the window configuration before `org-open-at-point' was called using:
9786 (set-window-configuration org-window-config-before-follow-link)")
9788 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
9789 (defun org-link-search (s &optional type avoid-pos stealth)
9790 "Search for a link search option.
9791 If S is surrounded by forward slashes, it is interpreted as a
9792 regular expression. In org-mode files, this will create an `org-occur'
9793 sparse tree. In ordinary files, `occur' will be used to list matches.
9794 If the current buffer is in `dired-mode', grep will be used to search
9795 in all files. If AVOID-POS is given, ignore matches near that position.
9797 When optional argument STEALTH is non-nil, do not modify
9798 visibility around point, thus ignoring
9799 `org-show-hierarchy-above', `org-show-following-heading' and
9800 `org-show-siblings' variables."
9801 (let ((case-fold-search t)
9802 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
9803 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
9804 (append '(("") (" ") ("\t") ("\n"))
9808 (pre nil) (post nil)
9809 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
9811 ;; First check if there are any special search functions
9812 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
9813 ;; Now try the builtin stuff
9814 ((and (equal (string-to-char s0) ?#)
9817 (goto-char (point-min))
9820 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
9821 (setq type 'dedicated
9822 pos (match-beginning 0))))
9823 ;; There is an exact target for this
9825 (org-back-to-heading t)))
9827 (goto-char (point-min))
9830 (concat "<<" (regexp-quote s0) ">>") nil t)
9831 (setq type 'dedicated
9832 pos (match-beginning 0))))
9833 ;; There is an exact target for this
9835 ((and (string-match "^(\\(.*\\))$" s0)
9837 (goto-char (point-min))
9840 (concat "[^[]" (regexp-quote
9841 (format org-coderef-label-format
9842 (match-string 1 s0))))
9844 (setq type 'dedicated
9845 pos (1+ (match-beginning 0))))))
9846 ;; There is a coderef target for this
9848 ((string-match "^/\\(.*\\)/$" s)
9849 ;; A regular expression
9851 ((eq major-mode 'org-mode)
9852 (org-occur (match-string 1 s)))
9853 ;;((eq major-mode 'dired-mode)
9854 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
9855 (t (org-do-occur (match-string 1 s)))))
9856 ((and (eq major-mode 'org-mode) org-link-search-must-match-exact-headline)
9857 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
9858 (goto-char (point-min))
9860 ((let (case-fold-search)
9861 (re-search-forward (format org-complex-heading-regexp-format
9864 ;; OK, found a match
9865 (setq type 'dedicated)
9866 (goto-char (match-beginning 0)))
9867 ((and (not org-link-search-inhibit-query)
9868 (eq org-link-search-must-match-exact-headline 'query-to-create)
9869 (y-or-n-p "No match - create this as a new heading? "))
9870 (goto-char (point-max))
9871 (or (bolp) (newline))
9872 (insert "* " s "\n")
9873 (beginning-of-line 0))
9876 (error "No match"))))
9878 ;; A normal search string
9879 (when (equal (string-to-char s) ?*)
9880 ;; Anchor on headlines, post may include tags.
9881 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
9882 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
9884 (remove-text-properties
9886 '(face nil mouse-face nil keymap nil fontified nil) s)
9887 ;; Make a series of regular expressions to find a match
9888 (setq words (org-split-string s "[ \n\r\t]+")
9890 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
9891 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
9893 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
9894 re2a (concat "[ \t\r\n]" re2a_)
9895 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
9896 re4 (concat "[^a-zA-Z_]" re4_)
9898 re1 (concat pre re2 post)
9899 re3 (concat pre (if pre re4_ re4) post)
9900 re5 (concat pre ".*" re4)
9901 re2 (concat pre re2)
9902 re2a (concat pre (if pre re2a_ re2a))
9903 re4 (concat pre (if pre re4_ re4))
9904 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
9905 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
9909 ((eq type 'org-occur) (org-occur reall))
9910 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
9911 (t (goto-char (point-min))
9913 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
9914 (org-search-not-self 1 re1 nil t)
9915 (org-search-not-self 1 re2 nil t)
9916 (org-search-not-self 1 re2a nil t)
9917 (org-search-not-self 1 re3 nil t)
9918 (org-search-not-self 1 re4 nil t)
9919 (org-search-not-self 1 re5 nil t)
9921 (goto-char (match-beginning 1))
9923 (error "No match"))))))
9924 (and (eq major-mode 'org-mode)
9926 (org-show-context 'link-search))
9929 (defun org-search-not-self (group &rest args)
9930 "Execute `re-search-forward', but only accept matches that do not
9931 enclose the position of `org-open-link-marker'."
9932 (let ((m org-open-link-marker))
9934 (while (apply 're-search-forward args)
9935 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
9936 (goto-char (match-end group))
9937 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
9938 (> (match-beginning 0) (marker-position m))
9939 (< (match-end 0) (marker-position m)))
9941 (or (not (org-in-regexp
9942 org-bracket-link-analytic-regexp 1))
9943 (not (match-end 4)) ; no description
9944 (and (<= (match-beginning 4) (point))
9945 (>= (match-end 4) (point))))))
9946 (throw 'exit (point))))))))
9948 (defun org-get-buffer-for-internal-link (buffer)
9949 "Return a buffer to be used for displaying the link target of internal links."
9951 ((not org-display-internal-link-with-indirect-buffer)
9953 ((string-match "(Clone)$" (buffer-name buffer))
9954 (message "Buffer is already a clone, not making another one")
9955 ;; we also do not modify visibility in this case
9957 (t ; make a new indirect buffer for displaying the link
9958 (let* ((bn (buffer-name buffer))
9959 (ibn (concat bn "(Clone)"))
9960 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
9961 (with-current-buffer ib (org-overview))
9964 (defun org-do-occur (regexp &optional cleanup)
9965 "Call the Emacs command `occur'.
9966 If CLEANUP is non-nil, remove the printout of the regular expression
9967 in the *Occur* buffer. This is useful if the regex is long and not useful
9971 (let ((cwin (selected-window)) win beg end)
9972 (when (setq win (get-buffer-window "*Occur*"))
9973 (select-window win))
9974 (goto-char (point-min))
9975 (when (re-search-forward "match[a-z]+" nil t)
9976 (setq beg (match-end 0))
9977 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
9978 (setq end (1- (match-beginning 0)))))
9979 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
9980 (goto-char (point-min))
9981 (select-window cwin))))
9983 ;;; The mark ring for links jumps
9985 (defvar org-mark-ring nil
9986 "Mark ring for positions before jumps in Org-mode.")
9987 (defvar org-mark-ring-last-goto nil
9988 "Last position in the mark ring used to go back.")
9989 ;; Fill and close the ring
9990 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
9991 (loop for i from 1 to org-mark-ring-length do
9992 (push (make-marker) org-mark-ring))
9993 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
9996 (defun org-mark-ring-push (&optional pos buffer)
9997 "Put the current position or POS into the mark ring and rotate it."
9999 (setq pos (or pos (point)))
10000 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10001 (move-marker (car org-mark-ring)
10003 (or buffer (current-buffer)))
10005 (substitute-command-keys
10006 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10008 (defun org-mark-ring-goto (&optional n)
10009 "Jump to the previous position in the mark ring.
10010 With prefix arg N, jump back that many stored positions. When
10011 called several times in succession, walk through the entire ring.
10012 Org-mode commands jumping to a different position in the current file,
10013 or to another Org-mode file, automatically push the old position
10017 (if (eq last-command this-command)
10018 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10019 (setq p org-mark-ring))
10020 (setq org-mark-ring-last-goto p)
10022 (org-pop-to-buffer-same-window (marker-buffer m))
10024 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10026 (defun org-remove-angle-brackets (s)
10027 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10028 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10030 (defun org-add-angle-brackets (s)
10031 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10032 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10034 (defun org-remove-double-quotes (s)
10035 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10036 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10039 ;;; Following specific links
10041 (defun org-follow-timestamp-link ()
10043 ((org-at-date-range-p t)
10044 (let ((org-agenda-start-on-weekday)
10045 (t1 (match-string 1))
10046 (t2 (match-string 2)))
10047 (setq t1 (time-to-days (org-time-string-to-time t1))
10048 t2 (time-to-days (org-time-string-to-time t2)))
10049 (org-agenda-list nil t1 (1+ (- t2 t1)))))
10050 ((org-at-timestamp-p t)
10051 (org-agenda-list nil (time-to-days (org-time-string-to-time
10052 (substring (match-string 1) 0 10)))
10054 (t (error "This should not happen"))))
10057 ;;; Following file links
10058 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10059 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10060 (declare-function mailcap-mime-info
10061 "mailcap" (string &optional request no-decode))
10062 (defvar org-wait nil)
10063 (defun org-open-file (path &optional in-emacs line search)
10064 "Open the file at PATH.
10065 First, this expands any special file name abbreviations. Then the
10066 configuration variable `org-file-apps' is checked if it contains an
10067 entry for this file type, and if yes, the corresponding command is launched.
10069 If no application is found, Emacs simply visits the file.
10071 With optional prefix argument IN-EMACS, Emacs will visit the file.
10072 With a double \\[universal-argument] \\[universal-argument] \
10073 prefix arg, Org tries to avoid opening in Emacs
10074 and to use an external application to visit the file.
10076 Optional LINE specifies a line to go to, optional SEARCH a string
10077 to search for. If LINE or SEARCH is given, the file will be
10078 opened in Emacs, unless an entry from org-file-apps that makes
10079 use of groups in a regexp matches.
10081 If you want to change the way frames are used when following a
10082 link, please customize `org-link-frame-setup'.
10084 If the file does not exist, an error is thrown."
10085 (let* ((file (if (equal path "")
10087 (substitute-in-file-name (expand-file-name path))))
10088 (file-apps (append org-file-apps (org-default-apps)))
10089 (apps (org-remove-if
10090 'org-file-apps-entry-match-against-dlink-p file-apps))
10091 (apps-dlink (org-remove-if-not
10092 'org-file-apps-entry-match-against-dlink-p file-apps))
10093 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10094 (dirp (if remp nil (file-directory-p file)))
10095 (file (if (and dirp org-open-directory-means-index-dot-org)
10096 (concat (file-name-as-directory file) "index.org")
10098 (a-m-a-p (assq 'auto-mode apps))
10099 (dfile (downcase file))
10100 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10101 (link (cond ((and (eq line nil)
10105 (concat file "::" (number-to-string line)))
10107 (concat file "::" search))))
10108 (dlink (downcase link))
10109 (old-buffer (current-buffer))
10111 (old-mode major-mode)
10112 ext cmd link-match-data)
10113 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10114 (setq ext (match-string 1 dfile))
10115 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10116 (setq ext (match-string 1 dfile))))
10118 ((member in-emacs '((16) system))
10119 (setq cmd (cdr (assoc 'system apps))))
10120 (in-emacs (setq cmd 'emacs))
10122 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10123 (and dirp (cdr (assoc 'directory apps)))
10124 ; first, try matching against apps-dlink
10125 ; if we get a match here, store the match data for later
10126 (let ((match (assoc-default dlink apps-dlink
10129 (progn (setq link-match-data (match-data))
10131 (progn (setq in-emacs (or in-emacs line search))
10132 nil))) ; if we have no match in apps-dlink,
10133 ; always open the file in emacs if line or search
10134 ; is given (for backwards compatibility)
10135 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10137 (cdr (assoc ext apps))
10138 (cdr (assoc t apps))))))
10139 (when (eq cmd 'system)
10140 (setq cmd (cdr (assoc 'system apps))))
10141 (when (eq cmd 'default)
10142 (setq cmd (cdr (assoc t apps))))
10143 (when (eq cmd 'mailcap)
10145 (mailcap-parse-mailcaps)
10146 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10147 (command (mailcap-mime-info mime-type)))
10148 (if (stringp command)
10150 (setq cmd 'emacs))))
10151 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10152 (not (file-exists-p file))
10153 (not org-open-non-existing-files))
10154 (error "No such file: %s" file))
10156 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10157 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10158 (while (string-match "['\"]%s['\"]" cmd)
10159 (setq cmd (replace-match "%s" t t cmd)))
10160 (while (string-match "%s" cmd)
10161 (setq cmd (replace-match
10163 (shell-quote-argument
10164 (convert-standard-filename file)))
10167 ;; Replace "%1", "%2" etc. in command with group matches from regex
10169 (let ((match-index 1)
10170 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10171 (set-match-data link-match-data)
10172 (while (<= match-index number-of-groups)
10173 (let ((regex (concat "%" (number-to-string match-index)))
10174 (replace-with (match-string match-index dlink)))
10175 (while (string-match regex cmd)
10176 (setq cmd (replace-match replace-with t t cmd))))
10177 (setq match-index (+ match-index 1)))))
10179 (save-window-excursion
10180 (start-process-shell-command cmd nil cmd)
10181 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10185 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10187 (if line (org-goto-line line)
10188 (if search (org-link-search search))))
10190 (let ((file (convert-standard-filename file)))
10192 (set-match-data link-match-data)
10194 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10195 (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
10196 (or (not (equal old-buffer (current-buffer)))
10197 (not (equal old-pos (point))))
10198 (org-mark-ring-push old-pos old-buffer))))
10200 (defun org-file-apps-entry-match-against-dlink-p (entry)
10201 "This function returns non-nil if `entry' uses a regular
10202 expression which should be matched against the whole link by
10205 It assumes that is the case when the entry uses a regular
10206 expression which has at least one grouping construct and the
10207 action is either a lisp form or a command string containing
10208 '%1', i.e. using at least one subexpression match as a
10210 (let ((selector (car entry))
10211 (action (cdr entry)))
10212 (if (stringp selector)
10213 (and (> (regexp-opt-depth selector) 0)
10214 (or (and (stringp action)
10215 (string-match "%[0-9]" action))
10219 (defun org-default-apps ()
10220 "Return the default applications for this operating system."
10222 ((eq system-type 'darwin)
10223 org-file-apps-defaults-macosx)
10224 ((eq system-type 'windows-nt)
10225 org-file-apps-defaults-windowsnt)
10226 (t org-file-apps-defaults-gnu)))
10228 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10229 "Convert extensions to regular expressions in the cars of LIST.
10230 Also, weed out any non-string entries, because the return value is used
10231 only for regexp matching.
10232 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10233 point to the symbol `emacs', indicating that the file should
10234 be opened in Emacs."
10237 (mapcar (lambda (x)
10238 (if (not (stringp (car x)))
10240 (if (string-match "\\W" (car x))
10242 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10245 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10247 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10248 (defun org-file-remote-p (file)
10249 "Test whether FILE specifies a location on a remote system.
10250 Return non-nil if the location is indeed remote.
10252 For example, the filename \"/user@host:/foo\" specifies a location
10253 on the system \"/user@host:\"."
10254 (cond ((fboundp 'file-remote-p)
10255 (file-remote-p file))
10256 ((fboundp 'tramp-handle-file-remote-p)
10257 (tramp-handle-file-remote-p file))
10258 ((and (boundp 'ange-ftp-name-format)
10259 (string-match (car ange-ftp-name-format) file))
10266 (defun org-get-org-file ()
10267 "Read a filename, with default directory `org-directory'."
10268 (let ((default (or org-default-notes-file remember-data-file)))
10269 (read-file-name (format "File name [%s]: " default)
10270 (file-name-as-directory org-directory)
10273 (defun org-notes-order-reversed-p ()
10274 "Check if the current file should receive notes in reversed order."
10276 ((not org-reverse-note-order) nil)
10277 ((eq t org-reverse-note-order) t)
10278 ((not (listp org-reverse-note-order)) nil)
10280 (let ((all org-reverse-note-order)
10282 (while (setq entry (pop all))
10283 (if (string-match (car entry) buffer-file-name)
10284 (throw 'exit (cdr entry))))
10287 (defvar org-refile-target-table nil
10288 "The list of refile targets, created by `org-refile'.")
10290 (defvar org-agenda-new-buffers nil
10291 "Buffers created to visit agenda files.")
10293 (defvar org-refile-cache nil
10294 "Cache for refile targets.")
10296 (defvar org-refile-markers nil
10297 "All the markers used for caching refile locations.")
10299 (defun org-refile-marker (pos)
10300 "Get a new refile marker, but only if caching is in use."
10301 (if (not org-refile-use-cache)
10303 (let ((m (make-marker)))
10304 (move-marker m pos)
10305 (push m org-refile-markers)
10308 (defun org-refile-cache-clear ()
10309 "Clear the refile cache and disable all the markers."
10310 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10311 (setq org-refile-markers nil)
10312 (setq org-refile-cache nil)
10313 (message "Refile cache has been cleared"))
10315 (defun org-refile-cache-check-set (set)
10316 "Check if all the markers in the cache still have live buffers."
10319 (while (and set (setq marker (nth 3 (pop set))))
10320 ;; if org-refile-use-outline-path is 'file, marker may be nil
10321 (when (and marker (null (marker-buffer marker)))
10322 (message "not found") (sit-for 3)
10323 (throw 'exit nil)))
10326 (defun org-refile-cache-put (set &rest identifiers)
10327 "Push the refile targets SET into the cache, under IDENTIFIERS."
10328 (let* ((key (sha1 (prin1-to-string identifiers)))
10329 (entry (assoc key org-refile-cache)))
10332 (push (cons key set) org-refile-cache))))
10334 (defun org-refile-cache-get (&rest identifiers)
10335 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10337 ((not org-refile-cache) nil)
10338 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10340 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10341 org-refile-cache))))
10342 (and set (org-refile-cache-check-set set) set)))))
10344 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10345 "Produce a table with refile targets."
10346 (let ((case-fold-search nil)
10347 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10348 (entries (or org-refile-targets '((nil . (:level . 1)))))
10349 targets tgs txt re files f desc descre fast-path-p level pos0)
10350 (message "Getting targets...")
10351 (with-current-buffer (or default-buffer (current-buffer))
10352 (while (setq entry (pop entries))
10353 (setq files (car entry) desc (cdr entry))
10354 (setq fast-path-p nil)
10356 ((null files) (setq files (list (current-buffer))))
10357 ((eq files 'org-agenda-files)
10358 (setq files (org-agenda-files 'unrestricted)))
10359 ((and (symbolp files) (fboundp files))
10360 (setq files (funcall files)))
10361 ((and (symbolp files) (boundp files))
10362 (setq files (symbol-value files))))
10363 (if (stringp files) (setq files (list files)))
10365 ((eq (car desc) :tag)
10366 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10367 ((eq (car desc) :todo)
10368 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10369 ((eq (car desc) :regexp)
10370 (setq descre (cdr desc)))
10371 ((eq (car desc) :level)
10372 (setq descre (concat "^\\*\\{" (number-to-string
10373 (if org-odd-levels-only
10374 (1- (* 2 (cdr desc)))
10377 ((eq (car desc) :maxlevel)
10378 (setq fast-path-p t)
10379 (setq descre (concat "^\\*\\{1," (number-to-string
10380 (if org-odd-levels-only
10381 (1- (* 2 (cdr desc)))
10384 (t (error "Bad refiling target description %s" desc)))
10385 (while (setq f (pop files))
10386 (with-current-buffer
10387 (if (bufferp f) f (org-get-agenda-file-buffer f))
10389 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10391 (if (bufferp f) (setq f (buffer-file-name
10392 (buffer-base-buffer f))))
10393 (setq f (and f (expand-file-name f)))
10394 (if (eq org-refile-use-outline-path 'file)
10395 (push (list (file-name-nondirectory f) f nil nil) tgs))
10399 (goto-char (point-min))
10400 (while (re-search-forward descre nil t)
10401 (goto-char (setq pos0 (point-at-bol)))
10403 (when org-refile-target-verify-function
10405 (or (funcall org-refile-target-verify-function)
10407 (when (and (looking-at org-complex-heading-regexp)
10408 (not (member (match-string 4) excluded-entries))
10410 (setq level (org-reduced-level
10411 (- (match-end 1) (match-beginning 1)))
10412 txt (org-link-display-format (match-string 4))
10413 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10414 re (format org-complex-heading-regexp-format
10415 (regexp-quote (match-string 4))))
10416 (when org-refile-use-outline-path
10417 (setq txt (mapconcat
10420 (if (eq org-refile-use-outline-path
10422 (list (file-name-nondirectory
10424 (buffer-base-buffer))))
10425 (if (eq org-refile-use-outline-path
10427 (list (buffer-file-name
10428 (buffer-base-buffer)))))
10429 (org-get-outline-path fast-path-p
10433 (push (list txt f re (org-refile-marker (point)))
10435 (when (= (point) pos0)
10436 ;; verification function has not moved point
10437 (goto-char (point-at-eol))))))))
10438 (when org-refile-use-cache
10439 (org-refile-cache-put tgs (buffer-file-name) descre))
10440 (setq targets (append tgs targets))
10442 (message "Getting targets...done")
10443 (nreverse targets)))
10445 (defun org-protect-slash (s)
10446 (while (string-match "/" s)
10447 (setq s (replace-match "\\" t t s)))
10450 (defvar org-olpa (make-vector 20 nil))
10452 (defun org-get-outline-path (&optional fastp level heading)
10453 "Return the outline path to the current entry, as a list.
10455 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10456 routine which makes outline path derivations for an entire file,
10457 avoiding backtracing. Refile target collection makes use of that."
10461 (error "Outline path failure, more than 19 levels"))
10462 (loop for i from level upto 19 do
10463 (aset org-olpa i nil))
10465 (delq nil (append org-olpa nil))
10466 (aset org-olpa level heading)))
10467 (let (rtn case-fold-search)
10471 (while (org-up-heading-safe)
10472 (when (looking-at org-complex-heading-regexp)
10473 (push (org-match-string-no-properties 4) rtn)))
10476 (defun org-format-outline-path (path &optional width prefix)
10477 "Format the outline path PATH for display.
10478 Width is the maximum number of characters that is available.
10479 Prefix is a prefix to be included in the returned string,
10480 such as the file name."
10481 (setq width (or width 79))
10482 (if prefix (setq width (- width (length prefix))))
10485 (let* ((nsteps (length path))
10486 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10487 (maxwidth (if (<= total-width width)
10488 10000 ;; everything fits
10489 ;; we need to shorten the level headings
10490 (/ (- width nsteps) nsteps)))
10491 (org-odd-levels-only nil)
10493 (total (1+ (length prefix))))
10494 (setq maxwidth (max maxwidth 10))
10499 (if (and (= n nsteps) (< maxwidth 10000))
10500 (setq maxwidth (- total-width total)))
10501 (if (< (length h) maxwidth)
10502 (progn (setq total (+ total (length h) 1)) h)
10503 (setq h (substring h 0 (- maxwidth 2))
10504 total (+ total maxwidth 1))
10505 (if (string-match "[ \t]+\\'" h)
10506 (setq h (substring h 0 (match-beginning 0))))
10507 (setq h (concat h "..")))
10508 (org-add-props h nil 'face
10509 (nth (% (1- n) org-n-level-faces)
10514 (defun org-display-outline-path (&optional file current)
10515 "Display the current outline path in the echo area."
10517 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10518 (case-fold-search nil)
10519 (path (and (eq major-mode 'org-mode) (org-get-outline-path))))
10520 (if current (setq path (append path
10522 (org-back-to-heading t)
10523 (if (looking-at org-complex-heading-regexp)
10524 (list (match-string 4)))))))
10526 (org-format-outline-path
10529 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10531 (defvar org-refile-history nil
10532 "History for refiling operations.")
10534 (defvar org-after-refile-insert-hook nil
10535 "Hook run after `org-refile' has inserted its stuff at the new location.
10536 Note that this is still *before* the stuff will be removed from
10537 the *old* location.")
10539 (defvar org-capture-last-stored-marker)
10540 (defun org-refile (&optional goto default-buffer rfloc)
10541 "Move the entry or entries at point to another heading.
10542 The list of target headings is compiled using the information in
10543 `org-refile-targets', which see.
10545 At the target location, the entry is filed as a subitem of the target
10546 heading. Depending on `org-reverse-note-order', the new subitem will
10547 either be the first or the last subitem.
10549 If there is an active region, all entries in that region will be moved.
10550 However, the region must fulfill the requirement that the first heading
10551 is the first one sets the top-level of the moved text - at most siblings
10552 below it are allowed.
10554 With prefix arg GOTO, the command will only visit the target location
10555 and not actually move anything.
10557 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10558 go to the location where the last refiling operation has put the subtree.
10559 With a prefix argument of `2', refile to the running clock.
10561 RFLOC can be a refile location obtained in a different way.
10563 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10565 If you are using target caching (see `org-refile-use-cache'),
10566 You have to clear the target cache in order to find new targets.
10567 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10568 prefix argument (`C-u C-u C-u C-c C-w')."
10571 (if (member goto '(0 (64)))
10572 (org-refile-cache-clear)
10573 (let* ((cbuf (current-buffer))
10574 (regionp (org-region-active-p))
10575 (region-start (and regionp (region-beginning)))
10576 (region-end (and regionp (region-end)))
10577 (region-length (and regionp (- region-end region-start)))
10578 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10579 pos it nbuf file re level reversed)
10580 (setq last-command nil)
10582 (goto-char region-start)
10583 (or (bolp) (goto-char (point-at-bol)))
10584 (setq region-start (point))
10585 (unless (or (org-kill-is-subtree-p
10586 (buffer-substring region-start region-end))
10587 (prog1 org-refile-active-region-within-subtree
10588 (org-toggle-heading)))
10589 (error "The region is not a (sequence of) subtree(s)")))
10590 (if (equal goto '(16))
10591 (org-refile-goto-last-stored)
10593 (and (equal goto 2)
10594 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10596 (setq it (list (or org-clock-heading "running clock")
10598 (marker-buffer org-clock-hd-marker))
10600 (marker-position org-clock-hd-marker)))
10603 (let (heading-text)
10606 (org-back-to-heading t)
10608 (nth 4 (org-heading-components))))
10609 (org-refile-get-location
10610 (cond (goto "Goto")
10611 (regionp "Refile region to")
10612 (t (concat "Refile subtree \""
10613 heading-text "\" to")))
10615 org-refile-allow-creating-parent-nodes
10617 (setq file (nth 1 it)
10620 (if (and (not goto)
10622 (equal (buffer-file-name) file)
10624 (and (>= pos region-start)
10625 (<= pos region-end))
10626 (and (>= pos (point))
10627 (< pos (save-excursion
10628 (org-end-of-subtree t t))))))
10629 (error "Cannot refile to position inside the tree or region"))
10631 (setq nbuf (or (find-buffer-visiting file)
10632 (find-file-noselect file)))
10635 (org-pop-to-buffer-same-window nbuf)
10637 (org-show-context 'org-goto))
10640 (org-kill-new (buffer-substring region-start region-end))
10641 (org-save-markers-in-region region-start region-end))
10642 (org-copy-subtree 1 nil t))
10643 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10644 (find-file-noselect file)))
10645 (setq reversed (org-notes-order-reversed-p))
10652 (looking-at org-outline-regexp)
10653 (setq level (org-get-valid-level (funcall outline-level) 1))
10656 (or (outline-next-heading) (point-max))
10657 (or (save-excursion (org-get-next-sibling))
10658 (org-end-of-subtree t t)
10662 (goto-char (point-max))
10663 (goto-char (point-min))
10664 (or (outline-next-heading) (goto-char (point-max)))))
10665 (if (not (bolp)) (newline))
10666 (org-paste-subtree level)
10667 (when org-log-refile
10668 (org-add-log-setup 'refile nil nil 'findpos
10670 (unless (eq org-log-refile 'note)
10671 (save-excursion (org-add-log-note))))
10672 (and org-auto-align-tags (org-set-tags nil t))
10673 (bookmark-set "org-refile-last-stored")
10674 ;; If we are refiling for capture, make sure that the
10675 ;; last-capture pointers point here
10676 (when (org-bound-and-true-p org-refile-for-capture)
10677 (bookmark-set "org-capture-last-stored-marker")
10678 (move-marker org-capture-last-stored-marker (point)))
10679 (if (fboundp 'deactivate-mark) (deactivate-mark))
10680 (run-hooks 'org-after-refile-insert-hook))))
10682 (delete-region (point) (+ (point) region-length))
10684 (when (featurep 'org-inlinetask)
10685 (org-inlinetask-remove-END-maybe))
10686 (setq org-markers-to-move nil)
10687 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10689 (defun org-refile-goto-last-stored ()
10690 "Go to the location where the last refile was stored."
10692 (bookmark-jump "org-refile-last-stored")
10693 (message "This is the location of the last refile"))
10695 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10697 "Prompt the user for a refile location, using PROMPT.
10698 PROMPT should not be suffixed with a colon and a space, because
10699 this function appends the default value from
10700 `org-refile-history' automatically, if that is not empty.
10701 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10702 this is used for the GOTO interface."
10703 (let ((org-refile-targets org-refile-targets)
10704 (org-refile-use-outline-path org-refile-use-outline-path)
10706 (when (and (eq major-mode 'org-mode)
10707 (not org-refile-use-cache)
10711 (setq excluded-entries
10712 (append excluded-entries (list (org-get-heading t t)))))))
10713 (setq org-refile-target-table
10714 (org-refile-get-targets default-buffer excluded-entries)))
10715 (unless org-refile-target-table
10716 (error "No refile targets"))
10717 (let* ((prompt (concat prompt
10718 (and (car org-refile-history)
10719 (concat " (default " (car org-refile-history) ")"))
10721 (cbuf (current-buffer))
10722 (partial-completion-mode nil)
10723 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10724 (cfunc (if (and org-refile-use-outline-path
10725 org-outline-path-complete-in-steps)
10726 'org-olpath-completing-read
10727 'org-icompleting-read))
10728 (extra (if org-refile-use-outline-path "/" ""))
10729 (filename (and cfn (expand-file-name cfn)))
10732 (if (and (not (member org-refile-use-outline-path
10733 '(file full-file-path)))
10734 (not (equal filename (nth 1 x))))
10735 (cons (concat (car x) extra " ("
10736 (file-name-nondirectory (nth 1 x)) ")")
10738 (cons (concat (car x) extra) (cdr x))))
10739 org-refile-target-table))
10740 (completion-ignore-case t)
10741 pa answ parent-target child parent old-hist)
10742 (setq old-hist org-refile-history)
10743 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10744 nil 'org-refile-history (car org-refile-history)))
10745 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
10746 (org-refile-check-position pa)
10749 (when (or (not org-refile-history)
10750 (not (eq old-hist org-refile-history))
10751 (not (equal (car pa) (car org-refile-history))))
10752 (setq org-refile-history
10753 (cons (car pa) (if (assoc (car org-refile-history) tbl)
10755 (cdr org-refile-history))))
10756 (if (equal (car org-refile-history) (nth 1 org-refile-history))
10757 (pop org-refile-history)))
10759 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
10761 (setq parent (match-string 1 answ)
10762 child (match-string 2 answ))
10763 (setq parent-target (or (assoc parent tbl)
10764 (assoc (concat parent "/") tbl)))
10765 (when (and parent-target
10766 (or (eq new-nodes t)
10767 (and (eq new-nodes 'confirm)
10768 (y-or-n-p (format "Create new node \"%s\"? "
10770 (org-refile-new-child parent-target child)))
10771 (error "Invalid target location")))))
10773 (defun org-refile-check-position (refile-pointer)
10774 "Check if the refile pointer matches the readline to which it points."
10775 (let* ((file (nth 1 refile-pointer))
10776 (re (nth 2 refile-pointer))
10777 (pos (nth 3 refile-pointer))
10779 (when (org-string-nw-p re)
10780 (setq buffer (if (markerp pos)
10781 (marker-buffer pos)
10782 (or (find-buffer-visiting file)
10783 (find-file-noselect file))))
10784 (with-current-buffer buffer
10789 (beginning-of-line 1)
10790 (unless (org-looking-at-p re)
10791 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
10793 (defun org-refile-new-child (parent-target child)
10794 "Use refile target PARENT-TARGET to add new CHILD below it."
10795 (unless parent-target
10796 (error "Cannot find parent for new node"))
10797 (let ((file (nth 1 parent-target))
10798 (pos (nth 3 parent-target))
10800 (with-current-buffer (or (find-buffer-visiting file)
10801 (find-file-noselect file))
10807 (goto-char (point-max))
10808 (if (not (bolp)) (newline)))
10809 (when (looking-at org-outline-regexp)
10810 (setq level (funcall outline-level))
10811 (org-end-of-subtree t t))
10812 (org-back-over-empty-lines)
10813 (insert "\n" (make-string
10814 (if pos (org-get-valid-level level 1) 1) ?*)
10816 (beginning-of-line 0)
10817 (list (concat (car parent-target) "/" child) file "" (point)))))))
10819 (defun org-olpath-completing-read (prompt collection &rest args)
10820 "Read an outline path like a file name."
10821 (let ((thetable collection)
10822 (org-completion-use-ido nil) ; does not work with ido.
10823 (org-completion-use-iswitchb nil)) ; or iswitchb
10825 'org-icompleting-read prompt
10826 (lambda (string predicate &optional flag)
10827 (let (rtn r f (l (length string)))
10831 (try-completion string thetable))
10834 (setq rtn (all-completions string thetable predicate))
10837 (setq r (substring x l))
10838 (if (string-match " ([^)]*)$" x)
10839 (setq f (match-string 0 x))
10841 (if (string-match "/" r)
10842 (concat string (substring r 0 (match-end 0)) f)
10847 (assoc string thetable)))
10851 ;;;; Dynamic blocks
10853 (defun org-find-dblock (name)
10854 "Find the first dynamic block with name NAME in the buffer.
10855 If not found, stay at current position and return nil."
10858 (goto-char (point-min))
10859 (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
10861 (match-beginning 0))))
10862 (if pos (goto-char pos))
10865 (defconst org-dblock-start-re
10866 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
10867 "Matches the start line of a dynamic block, with parameters.")
10869 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
10870 "Matches the end of a dynamic block.")
10872 (defun org-create-dblock (plist)
10873 "Create a dynamic block section, with parameters taken from PLIST.
10874 PLIST must contain a :name entry which is used as name of the block."
10875 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
10878 (let ((col (current-column))
10879 (name (plist-get plist :name)))
10880 (insert "#+BEGIN: " name)
10882 (if (eq (car plist) :name)
10883 (setq plist (cddr plist))
10884 (insert " " (prin1-to-string (pop plist)))))
10885 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
10886 (beginning-of-line -2)))
10888 (defun org-prepare-dblock ()
10889 "Prepare dynamic block for refresh.
10890 This empties the block, puts the cursor at the insert position and returns
10891 the property list including an extra property :name with the block name."
10892 (unless (looking-at org-dblock-start-re)
10893 (error "Not at a dynamic block"))
10894 (let* ((begdel (1+ (match-end 0)))
10895 (name (org-no-properties (match-string 1)))
10896 (params (append (list :name name)
10897 (read (concat "(" (match-string 3) ")")))))
10899 (beginning-of-line 1)
10900 (skip-chars-forward " \t")
10901 (setq params (plist-put params :indentation-column (current-column))))
10902 (unless (re-search-forward org-dblock-end-re nil t)
10903 (error "Dynamic block not terminated"))
10906 (list :content (buffer-substring
10907 begdel (match-beginning 0)))))
10908 (delete-region begdel (match-beginning 0))
10913 (defun org-map-dblocks (&optional command)
10914 "Apply COMMAND to all dynamic blocks in the current buffer.
10915 If COMMAND is not given, use `org-update-dblock'."
10916 (let ((cmd (or command 'org-update-dblock)))
10918 (goto-char (point-min))
10919 (while (re-search-forward org-dblock-start-re nil t)
10920 (goto-char (match-beginning 0))
10922 (condition-case nil
10924 (error (message "Error during update of dynamic block"))))
10925 (unless (re-search-forward org-dblock-end-re nil t)
10926 (error "Dynamic block not terminated"))))))
10928 (defun org-dblock-update (&optional arg)
10929 "User command for updating dynamic blocks.
10930 Update the dynamic block at point. With prefix ARG, update all dynamic
10931 blocks in the buffer."
10934 (org-update-all-dblocks)
10935 (or (looking-at org-dblock-start-re)
10936 (org-beginning-of-dblock))
10937 (org-update-dblock)))
10939 (defun org-update-dblock ()
10940 "Update the dynamic block at point.
10941 This means to empty the block, parse for parameters and then call
10942 the correct writing function."
10944 (save-window-excursion
10945 (let* ((pos (point))
10946 (line (org-current-line))
10947 (params (org-prepare-dblock))
10948 (name (plist-get params :name))
10949 (indent (plist-get params :indentation-column))
10950 (cmd (intern (concat "org-dblock-write:" name))))
10951 (message "Updating dynamic block `%s' at line %d..." name line)
10952 (funcall cmd params)
10953 (message "Updating dynamic block `%s' at line %d...done" name line)
10955 (when (and indent (> indent 0))
10956 (setq indent (make-string indent ?\ ))
10958 (org-beginning-of-dblock)
10960 (while (not (looking-at org-dblock-end-re))
10962 (beginning-of-line 2))
10963 (when (looking-at org-dblock-end-re)
10964 (and (looking-at "[ \t]+")
10965 (replace-match ""))
10966 (insert indent)))))))
10968 (defun org-beginning-of-dblock ()
10969 "Find the beginning of the dynamic block at point.
10970 Error if there is no such block at point."
10971 (let ((pos (point))
10974 (if (and (re-search-backward org-dblock-start-re nil t)
10975 (setq beg (match-beginning 0))
10976 (re-search-forward org-dblock-end-re nil t)
10977 (> (match-end 0) pos))
10980 (error "Not in a dynamic block"))))
10982 (defun org-update-all-dblocks ()
10983 "Update all dynamic blocks in the buffer.
10984 This function can be used in a hook."
10986 (when (eq major-mode 'org-mode)
10987 (org-map-dblocks 'org-update-dblock)))
10992 (defconst org-additional-option-like-keywords
10993 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
10994 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
10995 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
10996 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
10998 "ORGTBL" "TBLFM:" "TBLNAME:"
10999 "BEGIN_EXAMPLE" "END_EXAMPLE"
11000 "BEGIN_QUOTE" "END_QUOTE"
11001 "BEGIN_VERSE" "END_VERSE"
11002 "BEGIN_CENTER" "END_CENTER"
11003 "BEGIN_SRC" "END_SRC"
11004 "BEGIN_RESULT" "END_RESULT"
11006 "HEADER:" "HEADERS:"
11007 "CATEGORY:" "COLUMNS:" "PROPERTY:"
11008 "CAPTION:" "LABEL:"
11014 (defcustom org-structure-template-alist
11016 ("s" "#+begin_src ?\n\n#+end_src"
11017 "<src lang=\"?\">\n\n</src>")
11018 ("e" "#+begin_example\n?\n#+end_example"
11019 "<example>\n?\n</example>")
11020 ("q" "#+begin_quote\n?\n#+end_quote"
11021 "<quote>\n?\n</quote>")
11022 ("v" "#+begin_verse\n?\n#+end_verse"
11023 "<verse>\n?\n/verse>")
11024 ("c" "#+begin_center\n?\n#+end_center"
11025 "<center>\n?\n/center>")
11026 ("l" "#+begin_latex\n?\n#+end_latex"
11027 "<literal style=\"latex\">\n?\n</literal>")
11029 "<literal style=\"latex\">?</literal>")
11030 ("h" "#+begin_html\n?\n#+end_html"
11031 "<literal style=\"html\">\n?\n</literal>")
11033 "<literal style=\"html\">?</literal>")
11034 ("a" "#+begin_ascii\n?\n#+end_ascii")
11038 ("I" "#+include %file ?"
11039 "<include file=%file markup=\"?\">")
11041 "Structure completion elements.
11042 This is a list of abbreviation keys and values. The value gets inserted
11043 if you type `<' followed by the key and then press the completion key,
11044 usually `M-TAB'. %file will be replaced by a file name after prompting
11045 for the file using completion. The cursor will be placed at the position
11046 of the `?` in the template.
11047 There are two templates for each key, the first uses the original Org syntax,
11048 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11049 the default when the /org-mtags.el/ module has been loaded. See also the
11050 variable `org-mtags-prefer-muse-templates'.
11051 This is an experimental feature, it is undecided if it is going to stay in."
11052 :group 'org-completion
11054 (string :tag "Key")
11055 (string :tag "Template")
11056 (string :tag "Muse Template")))
11058 (defun org-try-structure-completion ()
11059 "Try to complete a structure template before point.
11060 This looks for strings like \"<e\" on an otherwise empty line and
11062 (let ((l (buffer-substring (point-at-bol) (point)))
11064 (when (and (looking-at "[ \t]*$")
11065 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11066 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11067 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11068 (match-beginning 1)) a)
11071 (defun org-complete-expand-structure-template (start cell)
11072 "Expand a structure template."
11073 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11074 (rpl (nth (if musep 2 1) cell))
11076 (delete-region start (point))
11077 (when (string-match "\\`#\\+" rpl)
11080 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11081 (setq ind (buffer-substring (point-at-bol) (point))))
11083 (setq start (point))
11084 (if (string-match "%file" rpl)
11085 (setq rpl (replace-match
11089 (abbreviate-file-name (read-file-name "Include file: ")))
11092 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11093 (concat "\n" ind)))
11095 (if (re-search-backward "\\?" start t) (delete-char 1))))
11097 ;;;; TODO, DEADLINE, Comments
11099 (defun org-toggle-comment ()
11100 "Change the COMMENT state of an entry."
11103 (org-back-to-heading)
11104 (let (case-fold-search)
11106 ((looking-at (format org-heading-keyword-regexp-format
11107 org-comment-string))
11108 (goto-char (match-end 1))
11109 (looking-at (concat " +" org-comment-string))
11110 (replace-match "" t t)
11111 (when (eolp) (insert " ")))
11112 ((looking-at org-outline-regexp)
11113 (goto-char (match-end 0))
11114 (insert org-comment-string " "))))))
11116 (defvar org-last-todo-state-is-todo nil
11117 "This is non-nil when the last TODO state change led to a TODO state.
11118 If the last change removed the TODO tag or switched to DONE, then
11121 (defvar org-setting-tags nil) ; dynamically skipped
11123 (defvar org-todo-setup-filter-hook nil
11124 "Hook for functions that pre-filter todo specs.
11125 Each function takes a todo spec and returns either nil or the spec
11126 transformed into canonical form." )
11128 (defvar org-todo-get-default-hook nil
11129 "Hook for functions that get a default item for todo.
11130 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11131 nil or a string to be used for the todo mark." )
11133 (defvar org-agenda-headline-snapshot-before-repeat)
11135 (defun org-current-effective-time ()
11136 "Return current time adjusted for `org-extend-today-until' variable"
11137 (let* ((ct (org-current-time))
11138 (dct (decode-time ct))
11140 (if (and org-use-effective-time
11141 (< (nth 2 dct) org-extend-today-until))
11142 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11146 (defun org-todo-yesterday (&optional arg)
11147 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11149 (if (eq major-mode 'org-agenda-mode)
11150 (apply 'org-agenda-todo-yesterday arg)
11151 (let* ((hour (third (decode-time
11152 (org-current-time))))
11153 (org-extend-today-until (1+ hour)))
11156 (defun org-todo (&optional arg)
11157 "Change the TODO state of an item.
11158 The state of an item is given by a keyword at the start of the heading,
11160 *** TODO Write paper
11163 The different keywords are specified in the variable `org-todo-keywords'.
11164 By default the available states are \"TODO\" and \"DONE\".
11165 So for this example: when the item starts with TODO, it is changed to DONE.
11166 When it starts with DONE, the DONE is removed. And when neither TODO nor
11167 DONE are present, add TODO at the beginning of the heading.
11169 With \\[universal-argument] prefix arg, use completion to determine the new \
11171 With numeric prefix arg, switch to that state.
11172 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11173 keywords (nextset).
11174 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11175 With a numeric prefix arg of 0, inhibit note taking for the change.
11177 For calling through lisp, arg is also interpreted in the following way:
11178 'none -> empty state
11179 \"\"(empty string) -> switch to empty state
11180 'done -> switch to DONE
11181 'nextset -> switch to the next set of keywords
11182 'previousset -> switch to the previous set of keywords
11183 \"WAITING\" -> switch to the specified keyword, but only if it
11184 really is a member of `org-todo-keywords'."
11186 (if (equal arg '(16)) (setq arg 'nextset))
11187 (let ((org-blocker-hook org-blocker-hook)
11188 (case-fold-search nil))
11189 (when (equal arg '(64))
11190 (setq arg nil org-blocker-hook nil))
11191 (when (and org-blocker-hook
11192 (or org-inhibit-blocking
11193 (org-entry-get nil "NOBLOCKING")))
11194 (setq org-blocker-hook nil))
11197 (org-back-to-heading t)
11198 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11199 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11200 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11201 (let* ((match-data (match-data))
11202 (startpos (point-at-bol))
11203 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11204 (org-log-done org-log-done)
11205 (org-log-repeat org-log-repeat)
11206 (org-todo-log-states org-todo-log-states)
11207 (org-inhibit-logging
11209 (progn (setq arg nil) 'note) org-inhibit-logging))
11210 (this (match-string 1))
11211 (hl-pos (match-beginning 0))
11212 (head (org-get-todo-sequence-head this))
11213 (ass (assoc head org-todo-kwd-alist))
11214 (interpret (nth 1 ass))
11215 (done-word (nth 3 ass))
11216 (final-done-word (nth 4 ass))
11217 (last-state (or this ""))
11218 (completion-ignore-case t)
11219 (member (member this org-todo-keywords-1))
11220 (tail (cdr member))
11222 ((and org-todo-key-trigger
11223 (or (and (equal arg '(4))
11224 (eq org-use-fast-todo-selection 'prefix))
11225 (and (not arg) org-use-fast-todo-selection
11226 (not (eq org-use-fast-todo-selection
11228 ;; Use fast selection
11229 (org-fast-todo-selection))
11230 ((and (equal arg '(4))
11231 (or (not org-use-fast-todo-selection)
11232 (not org-todo-key-trigger)))
11233 ;; Read a state with completion
11234 (org-icompleting-read
11235 "State: " (mapcar (lambda(x) (list x))
11236 org-todo-keywords-1)
11240 (if tail (car tail) nil)
11241 (car org-todo-keywords-1)))
11243 (if (equal member org-todo-keywords-1)
11246 (nth (- (length org-todo-keywords-1)
11248 org-todo-keywords-1)
11249 (org-last org-todo-keywords-1))))
11250 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11251 (setq arg nil))) ; hack to fall back to cycling
11253 ;; user or caller requests a specific state
11255 ((equal arg "") nil)
11256 ((eq arg 'none) nil)
11257 ((eq arg 'done) (or done-word (car org-done-keywords)))
11259 (or (car (cdr (member head org-todo-heads)))
11260 (car org-todo-heads)))
11261 ((eq arg 'previousset)
11262 (let ((org-todo-heads (reverse org-todo-heads)))
11263 (or (car (cdr (member head org-todo-heads)))
11264 (car org-todo-heads))))
11265 ((car (member arg org-todo-keywords-1)))
11267 (error "State `%s' not valid in this file" arg))
11268 ((nth (1- (prefix-numeric-value arg))
11269 org-todo-keywords-1))))
11270 ((null member) (or head (car org-todo-keywords-1)))
11271 ((equal this final-done-word) nil) ;; -> make empty
11272 ((null tail) nil) ;; -> first entry
11273 ((memq interpret '(type priority))
11274 (if (eq this-command last-command)
11276 (if (> (length tail) 0)
11277 (or done-word (car org-done-keywords))
11282 (run-hook-with-args-until-success
11283 'org-todo-get-default-hook state last-state)
11285 (next (if state (concat " " state " ") " "))
11286 (change-plist (list :type 'todo-state-change :from this :to state
11287 :position startpos))
11289 (when org-blocker-hook
11290 (setq org-last-todo-state-is-todo
11291 (not (member this org-done-keywords)))
11292 (unless (save-excursion
11294 (org-with-wide-buffer
11295 (run-hook-with-args-until-failure
11296 'org-blocker-hook change-plist))))
11297 (if (org-called-interactively-p 'interactive)
11298 (error "TODO state change from %s to %s blocked" this state)
11300 (message "TODO state change from %s to %s blocked" this state)
11301 (throw 'exit nil))))
11302 (store-match-data match-data)
11303 (replace-match next t t)
11304 (unless (pos-visible-in-window-p hl-pos)
11305 (message "TODO state changed to %s" (org-trim next)))
11307 (setq head (org-get-todo-sequence-head state)
11308 ass (assoc head org-todo-kwd-alist)
11309 interpret (nth 1 ass)
11310 done-word (nth 3 ass)
11311 final-done-word (nth 4 ass)))
11312 (when (memq arg '(nextset previousset))
11313 (message "Keyword-Set %d/%d: %s"
11314 (- (length org-todo-sets) -1
11315 (length (memq (assoc state org-todo-sets) org-todo-sets)))
11316 (length org-todo-sets)
11317 (mapconcat 'identity (assoc state org-todo-sets) " ")))
11318 (setq org-last-todo-state-is-todo
11319 (not (member state org-done-keywords)))
11320 (setq now-done-p (and (member state org-done-keywords)
11321 (not (member this org-done-keywords))))
11322 (and logging (org-local-logging logging))
11323 (when (and (or org-todo-log-states org-log-done)
11324 (not (eq org-inhibit-logging t))
11325 (not (memq arg '(nextset previousset))))
11326 ;; we need to look at recording a time and note
11327 (setq dolog (or (nth 1 (assoc state org-todo-log-states))
11328 (nth 2 (assoc this org-todo-log-states))))
11329 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11330 (setq dolog 'time))
11332 (member state org-not-done-keywords)
11333 (not (member this org-not-done-keywords)))
11334 ;; This is now a todo state and was not one before
11335 ;; If there was a CLOSED time stamp, get rid of it.
11336 (org-add-planning-info nil nil 'closed))
11337 (when (and now-done-p org-log-done)
11338 ;; It is now done, and it was not done before
11339 (org-add-planning-info 'closed (org-current-effective-time))
11340 (if (and (not dolog) (eq 'note org-log-done))
11341 (org-add-log-setup 'done state this 'findpos 'note)))
11342 (when (and state dolog)
11343 ;; This is a non-nil state, and we need to log it
11344 (org-add-log-setup 'state state this 'findpos dolog)))
11345 ;; Fixup tag positioning
11346 (org-todo-trigger-tag-changes state)
11347 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11348 (when org-provide-todo-statistics
11349 (org-update-parent-todo-statistics))
11350 (run-hooks 'org-after-todo-state-change-hook)
11351 (if (and arg (not (member state org-done-keywords)))
11352 (setq head (org-get-todo-sequence-head state)))
11353 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11354 ;; Do we need to trigger a repeat?
11356 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11357 ;; This is for the agenda, take a snapshot of the headline.
11359 (setq org-agenda-headline-snapshot-before-repeat
11360 (org-get-heading))))
11361 (org-auto-repeat-maybe state))
11362 ;; Fixup cursor location if close to the keyword
11363 (if (and (outline-on-heading-p)
11365 (save-excursion (beginning-of-line 1)
11366 (looking-at org-todo-line-regexp))
11367 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11369 (goto-char (or (match-end 2) (match-end 1)))
11370 (and (looking-at " ") (just-one-space))))
11371 (when org-trigger-hook
11373 (run-hook-with-args 'org-trigger-hook change-plist))))))))
11375 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11376 "Block turning an entry into a TODO, using the hierarchy.
11377 This checks whether the current task should be blocked from state
11378 changes. Such blocking occurs when:
11380 1. The task has children which are not all in a completed state.
11382 2. A task has a parent with the property :ORDERED:, and there
11383 are siblings prior to the current task with incomplete
11386 3. The parent of the task is blocked because it has siblings that should
11387 be done first, or is child of a block grandparent TODO entry."
11389 (if (not org-enforce-todo-dependencies)
11390 t ; if locally turned off don't block
11392 ;; If this is not a todo state change, or if this entry is already DONE,
11394 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11395 (member (plist-get change-plist :from)
11396 (cons 'done org-done-keywords))
11397 (member (plist-get change-plist :to)
11398 (cons 'todo org-not-done-keywords))
11399 (not (plist-get change-plist :to)))
11400 (throw 'dont-block t))
11401 ;; If this task has children, and any are undone, it's blocked
11403 (org-back-to-heading t)
11404 (let ((this-level (funcall outline-level)))
11405 (outline-next-heading)
11406 (let ((child-level (funcall outline-level)))
11407 (while (and (not (eobp))
11408 (> child-level this-level))
11409 ;; this todo has children, check whether they are all
11411 (if (and (not (org-entry-is-done-p))
11412 (org-entry-is-todo-p))
11413 (throw 'dont-block nil))
11414 (outline-next-heading)
11415 (setq child-level (funcall outline-level))))))
11416 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11417 ;; any previous siblings are undone, it's blocked
11419 (org-back-to-heading t)
11420 (let* ((pos (point))
11421 (parent-pos (and (org-up-heading-safe) (point))))
11422 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11423 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11425 (re-search-forward org-not-done-heading-regexp pos t))
11426 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11427 ;; Search further up the hierarchy, to see if an anchestor is blocked
11429 (goto-char parent-pos)
11430 (if (not (looking-at org-not-done-heading-regexp))
11431 (throw 'dont-block t)) ; do not block, parent is not a TODO
11433 (setq parent-pos (and (org-up-heading-safe) (point)))
11434 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11435 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11437 (re-search-forward org-not-done-heading-regexp pos t))
11438 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11440 (defcustom org-track-ordered-property-with-tag nil
11441 "Should the ORDERED property also be shown as a tag?
11442 The ORDERED property decides if an entry should require subtasks to be
11443 completed in sequence. Since a property is not very visible, setting
11444 this option means that toggling the ORDERED property with the command
11445 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11446 not relevant for the behavior, but it makes things more visible.
11448 Note that toggling the tag with tags commands will not change the property
11449 and therefore not influence behavior!
11451 This can be t, meaning the tag ORDERED should be used, It can also be a
11452 string to select a different tag for this task."
11455 (const :tag "No tracking" nil)
11456 (const :tag "Track with ORDERED tag" t)
11457 (string :tag "Use other tag")))
11459 (defun org-toggle-ordered-property ()
11460 "Toggle the ORDERED property of the current entry.
11461 For better visibility, you can track the value of this property with a tag.
11462 See variable `org-track-ordered-property-with-tag'."
11464 (let* ((t1 org-track-ordered-property-with-tag)
11465 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11467 (org-back-to-heading)
11468 (if (org-entry-get nil "ORDERED")
11470 (org-delete-property "ORDERED")
11471 (and tag (org-toggle-tag tag 'off))
11472 (message "Subtasks can be completed in arbitrary order"))
11473 (org-entry-put nil "ORDERED" "t")
11474 (and tag (org-toggle-tag tag 'on))
11475 (message "Subtasks must be completed in sequence")))))
11477 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11478 (defun org-block-todo-from-checkboxes (change-plist)
11479 "Block turning an entry into a TODO, using checkboxes.
11480 This checks whether the current task should be blocked from state
11481 changes because there are unchecked boxes in this entry."
11482 (if (not org-enforce-todo-checkbox-dependencies)
11483 t ; if locally turned off don't block
11485 ;; If this is not a todo state change, or if this entry is already DONE,
11487 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11488 (member (plist-get change-plist :from)
11489 (cons 'done org-done-keywords))
11490 (member (plist-get change-plist :to)
11491 (cons 'todo org-not-done-keywords))
11492 (not (plist-get change-plist :to)))
11493 (throw 'dont-block t))
11494 ;; If this task has checkboxes that are not checked, it's blocked
11496 (org-back-to-heading t)
11497 (let ((beg (point)) end)
11498 (outline-next-heading)
11501 (if (org-list-search-forward
11502 (concat (org-item-beginning-re)
11503 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11507 (if (boundp 'org-blocked-by-checkboxes)
11508 (setq org-blocked-by-checkboxes t))
11509 (throw 'dont-block nil)))))
11510 t))) ; do not block
11512 (defun org-entry-blocked-p ()
11513 "Is the current entry blocked?"
11514 (if (org-entry-get nil "NOBLOCKING")
11515 nil ;; Never block this entry
11517 (run-hook-with-args-until-failure
11519 (list :type 'todo-state-change
11524 (defun org-update-statistics-cookies (all)
11525 "Update the statistics cookie, either from TODO or from checkboxes.
11526 This should be called with the cursor in a line with a statistics cookie."
11530 (org-update-checkbox-count 'all)
11531 (org-map-entries 'org-update-parent-todo-statistics))
11532 (if (not (org-on-heading-p))
11533 (org-update-checkbox-count)
11534 (let ((pos (move-marker (make-marker) (point)))
11536 (ignore-errors (org-back-to-heading t))
11537 (if (not (org-on-heading-p))
11538 (org-update-checkbox-count)
11539 (setq l1 (org-outline-level))
11540 (setq end (save-excursion
11541 (outline-next-heading)
11542 (if (org-on-heading-p) (setq l2 (org-outline-level)))
11544 (if (and (save-excursion
11546 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11547 (not (save-excursion (re-search-forward
11548 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11549 (org-update-checkbox-count)
11550 (if (and l2 (> l2 l1))
11553 (org-update-parent-todo-statistics))
11555 (beginning-of-line 1)
11556 (while (re-search-forward
11557 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11559 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11561 (move-marker pos nil)))))
11563 (defvar org-entry-property-inherited-from) ;; defined below
11564 (defun org-update-parent-todo-statistics ()
11565 "Update any statistics cookie in the parent of the current headline.
11566 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11567 the entire subtree and this will travel up the hierarchy and update
11568 statistics everywhere."
11569 (let* ((prop (save-excursion (org-up-heading-safe)
11570 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11571 (recursive (or (not org-hierarchical-todo-statistics)
11572 (and prop (string-match "\\<recursive\\>" prop))))
11573 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11576 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11577 level ltoggle l1 new ndel
11578 (cnt-all 0) (cnt-done 0) is-percent kwd
11579 checkbox-beg ov ovs ove cookie-present)
11582 (beginning-of-line 1)
11583 (setq ltoggle (funcall outline-level))
11584 ;; Three situations are to consider:
11586 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11587 ;; to the top-level ancestor on the headline;
11589 ;; 2. If parent has "recursive" property, repeat up to the
11590 ;; headline setting that property, taking inheritance into
11593 ;; 3. Else, move up to direct parent and proceed only once.
11594 (while (and (setq level (org-up-heading-safe))
11595 (or recursive first)
11597 (setq first nil cookie-present nil)
11601 (downcase (or (org-entry-get nil "COOKIE_DATA")
11604 (while (re-search-forward box-re (point-at-eol) t)
11605 (setq cnt-all 0 cnt-done 0 cookie-present t)
11606 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11608 (unless (outline-next-heading) (throw 'exit nil))
11609 (while (and (looking-at org-complex-heading-regexp)
11610 (> (setq l1 (length (match-string 1))) level))
11611 (setq kwd (and (or recursive (= l1 ltoggle))
11613 (if (or (eq org-provide-todo-statistics 'all-headlines)
11614 (and (listp org-provide-todo-statistics)
11615 (or (member kwd org-provide-todo-statistics)
11616 (member kwd org-done-keywords))))
11617 (setq cnt-all (1+ cnt-all))
11618 (if (eq org-provide-todo-statistics t)
11619 (and kwd (setq cnt-all (1+ cnt-all)))))
11620 (and (member kwd org-done-keywords)
11621 (setq cnt-done (1+ cnt-done)))
11622 (outline-next-heading)))
11625 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11626 (format "[%d/%d]" cnt-done cnt-all))
11627 ndel (- (match-end 0) checkbox-beg))
11628 ;; handle overlays when updating cookie from column view
11629 (when (setq ov (car (overlays-at checkbox-beg)))
11630 (setq ovs (overlay-start ov) ove (overlay-end ov))
11631 (delete-overlay ov))
11632 (goto-char checkbox-beg)
11634 (delete-region (point) (+ (point) ndel))
11635 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11636 (when ov (move-overlay ov ovs ove)))
11637 (when cookie-present
11638 (run-hook-with-args 'org-after-todo-statistics-hook
11639 cnt-done (- cnt-all cnt-done))))))
11640 (run-hooks 'org-todo-statistics-hook)))
11642 (defvar org-after-todo-statistics-hook nil
11643 "Hook that is called after a TODO statistics cookie has been updated.
11644 Each function is called with two arguments: the number of not-done entries
11645 and the number of done entries.
11647 For example, the following function, when added to this hook, will switch
11648 an entry to DONE when all children are done, and back to TODO when new
11649 entries are set to a TODO status. Note that this hook is only called
11650 when there is a statistics cookie in the headline!
11652 (defun org-summary-todo (n-done n-not-done)
11653 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11654 (let (org-log-done org-log-states) ; turn off logging
11655 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11658 (defvar org-todo-statistics-hook nil
11659 "Hook that is run whenever Org thinks TODO statistics should be updated.
11660 This hook runs even if there is no statistics cookie present, in which case
11661 `org-after-todo-statistics-hook' would not run.")
11663 (defun org-todo-trigger-tag-changes (state)
11664 "Apply the changes defined in `org-todo-state-tags-triggers'."
11665 (let ((l org-todo-state-tags-triggers)
11667 (when (or (not state) (equal state ""))
11668 (setq changes (append changes (cdr (assoc "" l)))))
11669 (when (and (stringp state) (> (length state) 0))
11670 (setq changes (append changes (cdr (assoc state l)))))
11671 (when (member state org-not-done-keywords)
11672 (setq changes (append changes (cdr (assoc 'todo l)))))
11673 (when (member state org-done-keywords)
11674 (setq changes (append changes (cdr (assoc 'done l)))))
11675 (dolist (c changes)
11676 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11678 (defun org-local-logging (value)
11679 "Get logging settings from a property VALUE."
11681 ;; directly set the variables, they are already local.
11682 (setq org-log-done nil
11684 org-todo-log-states nil)
11685 (setq words (org-split-string value))
11686 (while (setq w (pop words))
11688 ((setq a (assoc w org-startup-options))
11689 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11690 (set (nth 1 a) (nth 2 a))))
11691 ((setq a (org-extract-log-state-settings w))
11692 (and (member (car a) org-todo-keywords-1)
11693 (push a org-todo-log-states)))))))
11695 (defun org-get-todo-sequence-head (kwd)
11696 "Return the head of the TODO sequence to which KWD belongs.
11697 If KWD is not set, check if there is a text property remembering the
11702 (or (get-text-property (point-at-bol) 'org-todo-head)
11704 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11705 nil (point-at-eol)))
11706 (get-text-property p 'org-todo-head))))
11707 ((not (member kwd org-todo-keywords-1))
11708 (car org-todo-keywords-1))
11709 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
11711 (defun org-fast-todo-selection ()
11712 "Fast TODO keyword selection with single keys.
11713 Returns the new TODO keyword, or nil if no state change should occur."
11714 (let* ((fulltable org-todo-key-alist)
11715 (done-keywords org-done-keywords) ;; needed for the faces.
11716 (maxlen (apply 'max (mapcar
11718 (if (stringp (car x)) (string-width (car x)) 0))
11721 (fwidth (+ maxlen 3 1 3))
11722 (ncol (/ (- (window-width) 4) fwidth))
11726 (save-window-excursion
11728 (set-buffer (get-buffer-create " *Org todo*"))
11729 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
11731 (org-set-local 'org-done-keywords done-keywords)
11732 (setq tbl fulltable cnt 0)
11733 (while (setq e (pop tbl))
11735 ((equal e '(:startgroup))
11736 (push '() groups) (setq ingroup t)
11737 (when (not (= cnt 0))
11741 ((equal e '(:endgroup))
11742 (setq ingroup nil cnt 0)
11744 ((equal e '(:newline))
11745 (when (not (= cnt 0))
11749 (while (equal (car tbl) '(:newline))
11751 (setq tbl (cdr tbl)))))
11753 (setq tg (car e) c (cdr e))
11754 (if ingroup (push tg (car groups)))
11755 (setq tg (org-add-props tg nil 'face
11756 (org-get-todo-face tg)))
11757 (if (and (= cnt 0) (not ingroup)) (insert " "))
11758 (insert "[" c "] " tg (make-string
11759 (- fwidth 4 (length tg)) ?\ ))
11760 (when (= (setq cnt (1+ cnt)) ncol)
11762 (if ingroup (insert " "))
11765 (goto-char (point-min))
11766 (if (not expert) (org-fit-window-to-buffer))
11767 (message "[a-z..]:Set [SPC]:clear")
11768 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11771 (and (= c ?q) (not (rassoc c fulltable))))
11772 (setq quit-flag t))
11774 ((setq e (rassoc c fulltable) tg (car e))
11776 (t (setq quit-flag t)))))))
11778 (defun org-entry-is-todo-p ()
11779 (member (org-get-todo-state) org-not-done-keywords))
11781 (defun org-entry-is-done-p ()
11782 (member (org-get-todo-state) org-done-keywords))
11784 (defun org-get-todo-state ()
11786 (org-back-to-heading t)
11787 (and (looking-at org-todo-line-regexp)
11789 (match-string 2))))
11791 (defun org-at-date-range-p (&optional inactive-ok)
11792 "Is the cursor inside a date range?"
11796 (let ((pos (point)))
11797 (skip-chars-backward "^[<\r\n")
11798 (skip-chars-backward "<[")
11799 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11800 (>= (match-end 0) pos)
11802 (skip-chars-backward "^<[\r\n")
11803 (skip-chars-backward "<[")
11804 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11805 (>= (match-end 0) pos)
11809 (defun org-get-repeat (&optional tagline)
11810 "Check if there is a deadline/schedule with repeater in this entry."
11813 (org-back-to-heading t)
11814 (and (re-search-forward (if tagline
11815 (concat tagline "\\s-*" org-repeat-re)
11817 (org-entry-end-position) t)
11818 (match-string-no-properties 1)))))
11820 (defvar org-last-changed-timestamp)
11821 (defvar org-last-inserted-timestamp)
11822 (defvar org-log-post-message)
11823 (defvar org-log-note-purpose)
11824 (defvar org-log-note-how)
11825 (defvar org-log-note-extra)
11826 (defun org-auto-repeat-maybe (done-word)
11827 "Check if the current headline contains a repeated deadline/schedule.
11828 If yes, set TODO state back to what it was and change the base date
11829 of repeating deadline/scheduled time stamps to new date.
11830 This function is run automatically after each state change to a DONE state."
11831 ;; last-state is dynamically scoped into this function
11832 (let* ((repeat (org-get-repeat))
11833 (aa (assoc last-state org-todo-kwd-alist))
11834 (interpret (nth 1 aa))
11836 (whata '(("d" . day) ("m" . month) ("y" . year)))
11837 (msg "Entry repeats: ")
11839 (org-todo-log-states nil)
11840 re type n what ts time to-state)
11842 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
11843 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
11844 org-todo-repeat-to-state))
11845 (unless (and to-state (member to-state org-todo-keywords-1))
11846 (setq to-state (if (eq interpret 'type) last-state head)))
11847 (org-todo to-state)
11848 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
11849 (org-entry-put nil "LAST_REPEAT" (format-time-string
11850 (org-time-stamp-format t t))))
11851 (when org-log-repeat
11852 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
11853 (memq 'org-add-log-note post-command-hook))
11854 ;; OK, we are already setup for some record
11855 (if (eq org-log-repeat 'note)
11856 ;; make sure we take a note, not only a time stamp
11857 (setq org-log-note-how 'note))
11858 ;; Set up for taking a record
11859 (org-add-log-setup 'state (or done-word (car org-done-keywords))
11861 'findpos org-log-repeat)))
11862 (org-back-to-heading t)
11863 (org-add-planning-info nil nil 'closed)
11864 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
11865 org-deadline-time-regexp "\\)\\|\\("
11866 org-ts-regexp "\\)"))
11867 (while (re-search-forward
11868 re (save-excursion (outline-next-heading) (point)) t)
11869 (setq type (if (match-end 1) org-scheduled-string
11870 (if (match-end 3) org-deadline-string "Plain:"))
11871 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
11872 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
11873 (setq n (string-to-number (match-string 2 ts))
11874 what (match-string 3 ts))
11875 (if (equal what "w") (setq n (* n 7) what "d"))
11876 ;; Preparation, see if we need to modify the start date for the change
11877 (when (match-end 1)
11878 (setq time (save-match-data (org-time-string-to-time ts)))
11880 ((equal (match-string 1 ts) ".")
11881 ;; Shift starting date to today
11882 (org-timestamp-change
11883 (- (org-today) (time-to-days time))
11885 ((equal (match-string 1 ts) "+")
11886 (let ((nshiftmax 10) (nshift 0))
11887 (while (or (= nshift 0)
11888 (<= (time-to-days time)
11889 (time-to-days (current-time))))
11890 (when (= (incf nshift) nshiftmax)
11891 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
11893 (org-timestamp-change n (cdr (assoc what whata)))
11894 (org-at-timestamp-p t)
11895 (setq ts (match-string 1))
11896 (setq time (save-match-data (org-time-string-to-time ts)))))
11897 (org-timestamp-change (- n) (cdr (assoc what whata)))
11898 ;; rematch, so that we have everything in place for the real shift
11899 (org-at-timestamp-p t)
11900 (setq ts (match-string 1))
11901 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
11902 (org-timestamp-change n (cdr (assoc what whata)))
11903 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
11904 (setq org-log-post-message msg)
11905 (message "%s" msg))))
11907 (defun org-show-todo-tree (arg)
11908 "Make a compact tree which shows all headlines marked with TODO.
11909 The tree will show the lines where the regexp matches, and all higher
11910 headlines above the match.
11911 With a \\[universal-argument] prefix, prompt for a regexp to match.
11912 With a numeric prefix N, construct a sparse tree for the Nth element
11913 of `org-todo-keywords-1'."
11915 (let ((case-fold-search nil)
11917 (cond ((null arg) org-not-done-regexp)
11919 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
11920 (mapcar 'list org-todo-keywords-1))))
11922 (mapconcat 'identity (org-split-string kwd "|") "\\|")
11924 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
11925 (regexp-quote (nth (1- (prefix-numeric-value arg))
11926 org-todo-keywords-1)))
11927 (t (error "Invalid prefix argument: %s" arg)))))
11928 (message "%d TODO entries found"
11929 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
11931 (defun org-deadline (&optional remove time)
11932 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
11933 With argument REMOVE, remove any deadline from the item.
11934 With argument TIME, set the deadline at the corresponding date. TIME
11935 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
11937 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11938 (let (org-loop-over-headlines-in-active-region)
11940 `(org-deadline ',remove ,time) org-loop-over-headlines-in-active-region 'region (if (outline-invisible-p) (org-end-of-subtree nil t))))
11941 (let* ((old-date (org-entry-get nil "DEADLINE"))
11942 (repeater (and old-date
11944 "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
11946 (match-string 1 old-date))))
11949 (when (and old-date org-log-redeadline)
11950 (org-add-log-setup 'deldeadline nil old-date 'findpos
11951 org-log-redeadline))
11952 (org-remove-timestamp-with-keyword org-deadline-string)
11953 (message "Item no longer has a deadline."))
11954 (org-add-planning-info 'deadline time 'closed)
11955 (when (and old-date org-log-redeadline
11956 (not (equal old-date
11957 (substring org-last-inserted-timestamp 1 -1))))
11958 (org-add-log-setup 'redeadline nil old-date 'findpos
11959 org-log-redeadline))
11962 (org-back-to-heading t)
11963 (when (re-search-forward (concat org-deadline-string " "
11964 org-last-inserted-timestamp)
11966 (outline-next-heading) (point)) t)
11967 (goto-char (1- (match-end 0)))
11968 (insert " " repeater)
11969 (setq org-last-inserted-timestamp
11970 (concat (substring org-last-inserted-timestamp 0 -1)
11972 (substring org-last-inserted-timestamp -1))))))
11973 (message "Deadline on %s" org-last-inserted-timestamp)))))
11975 (defun org-schedule (&optional remove time)
11976 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
11977 With argument REMOVE, remove any scheduling date from the item.
11978 With argument TIME, scheduled at the corresponding date. TIME can
11979 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
11981 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11982 (let (org-loop-over-headlines-in-active-region)
11984 `(org-schedule ',remove ,time) org-loop-over-headlines-in-active-region 'region (if (outline-invisible-p) (org-end-of-subtree nil t))))
11985 (let* ((old-date (org-entry-get nil "SCHEDULED"))
11986 (repeater (and old-date
11988 "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
11990 (match-string 1 old-date))))
11993 (when (and old-date org-log-reschedule)
11994 (org-add-log-setup 'delschedule nil old-date 'findpos
11995 org-log-reschedule))
11996 (org-remove-timestamp-with-keyword org-scheduled-string)
11997 (message "Item is no longer scheduled."))
11998 (org-add-planning-info 'scheduled time 'closed)
11999 (when (and old-date org-log-reschedule
12000 (not (equal old-date
12001 (substring org-last-inserted-timestamp 1 -1))))
12002 (org-add-log-setup 'reschedule nil old-date 'findpos
12003 org-log-reschedule))
12006 (org-back-to-heading t)
12007 (when (re-search-forward (concat org-scheduled-string " "
12008 org-last-inserted-timestamp)
12010 (outline-next-heading) (point)) t)
12011 (goto-char (1- (match-end 0)))
12012 (insert " " repeater)
12013 (setq org-last-inserted-timestamp
12014 (concat (substring org-last-inserted-timestamp 0 -1)
12016 (substring org-last-inserted-timestamp -1))))))
12017 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12019 (defun org-get-scheduled-time (pom &optional inherit)
12020 "Get the scheduled time as a time tuple, of a format suitable
12021 for calling org-schedule with, or if there is no scheduling,
12023 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12025 (apply 'encode-time (org-parse-time-string time)))))
12027 (defun org-get-deadline-time (pom &optional inherit)
12028 "Get the deadline as a time tuple, of a format suitable for
12029 calling org-deadline with, or if there is no scheduling, returns
12031 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12033 (apply 'encode-time (org-parse-time-string time)))))
12035 (defun org-remove-timestamp-with-keyword (keyword)
12036 "Remove all time stamps with KEYWORD in the current entry."
12037 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12040 (org-back-to-heading t)
12042 (outline-next-heading)
12043 (while (re-search-backward re beg t)
12045 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12046 (equal (char-before) ?\ ))
12047 (backward-delete-char 1)
12048 (if (string-match "^[ \t]*$" (buffer-substring
12049 (point-at-bol) (point-at-eol)))
12050 (delete-region (point-at-bol)
12051 (min (point-max) (1+ (point-at-eol))))))))))
12053 (defun org-add-planning-info (what &optional time &rest remove)
12054 "Insert new timestamp with keyword in the line directly after the headline.
12055 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12056 If non is given, the user is prompted for a date.
12057 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12060 (let (org-time-was-given org-end-time-was-given ts
12061 end default-time default-input)
12064 (when (and (memq what '(scheduled deadline))
12066 (and (stringp time)
12067 (string-match "^[-+]+[0-9]" time))))
12068 ;; Try to get a default date/time from existing timestamp
12070 (org-back-to-heading t)
12071 (setq end (save-excursion (outline-next-heading) (point)))
12072 (when (re-search-forward (if (eq what 'scheduled)
12073 org-scheduled-time-regexp
12074 org-deadline-time-regexp)
12076 (setq ts (match-string 1)
12078 (apply 'encode-time (org-parse-time-string ts))
12079 default-input (and ts (org-get-compact-tod ts))))))
12083 ;; This is a string (relative or absolute), set proper date
12084 (apply 'encode-time
12085 (org-read-date-analyze
12086 time default-time (decode-time default-time)))
12087 ;; If necessary, get the time from the user
12088 (or time (org-read-date nil 'to-time nil nil
12089 default-time default-input)))))
12091 (when (and org-insert-labeled-timestamps-at-point
12092 (member what '(scheduled deadline)))
12094 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12095 (org-insert-time-stamp time org-time-was-given
12096 nil nil nil (list org-end-time-was-given))
12100 (let (col list elt ts buffer-invisibility-spec)
12101 (org-back-to-heading t)
12102 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12103 (goto-char (match-end 1))
12104 (setq col (current-column))
12105 (goto-char (match-end 0))
12106 (if (eobp) (insert "\n") (forward-char 1))
12107 (when (and (not what)
12110 org-keyword-time-not-clock-regexp))))
12111 ;; Nothing to add, nothing to remove...... :-)
12113 (if (and (not (looking-at org-outline-regexp))
12114 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12116 (not (equal (match-string 1) org-clock-string)))
12117 (narrow-to-region (match-beginning 0) (match-end 0))
12118 (insert-before-markers "\n")
12120 (narrow-to-region (point) (point))
12121 (and org-adapt-indentation (org-indent-to-column col)))
12122 ;; Check if we have to remove something.
12123 (setq list (cons what remove))
12125 (setq elt (pop list))
12126 (when (or (and (eq elt 'scheduled)
12127 (re-search-forward org-scheduled-time-regexp nil t))
12128 (and (eq elt 'deadline)
12129 (re-search-forward org-deadline-time-regexp nil t))
12130 (and (eq elt 'closed)
12131 (re-search-forward org-closed-time-regexp nil t)))
12133 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12134 (and (looking-at "[ \t]+") (replace-match ""))
12135 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12138 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12139 (cond ((eq what 'scheduled) org-scheduled-string)
12140 ((eq what 'deadline) org-deadline-string)
12141 ((eq what 'closed) org-closed-string))
12143 (setq ts (org-insert-time-stamp
12145 (or org-time-was-given
12146 (and (eq what 'closed) org-log-done-with-time))
12148 nil nil (list org-end-time-was-given)))
12150 (if (not (or (bolp) (eq (char-before) ?\ )
12151 (memq (char-after) '(32 10))
12154 (goto-char (point-min))
12156 (if (and (looking-at "[ \t]*\n")
12157 (equal (char-before) ?\n))
12158 (delete-region (1- (point)) (point-at-eol)))
12161 (defvar org-log-note-marker (make-marker))
12162 (defvar org-log-note-purpose nil)
12163 (defvar org-log-note-state nil)
12164 (defvar org-log-note-previous-state nil)
12165 (defvar org-log-note-how nil)
12166 (defvar org-log-note-extra nil)
12167 (defvar org-log-note-window-configuration nil)
12168 (defvar org-log-note-return-to (make-marker))
12169 (defvar org-log-note-effective-time nil
12170 "Remembered current time so that dynamically scoped
12171 `org-extend-today-until' affects tha timestamps in state change
12174 (defvar org-log-post-message nil
12175 "Message to be displayed after a log note has been stored.
12176 The auto-repeater uses this.")
12178 (defun org-add-note ()
12179 "Add a note to the current entry.
12180 This is done in the same way as adding a state change note."
12182 (org-add-log-setup 'note nil nil 'findpos nil))
12184 (defvar org-property-end-re)
12185 (defun org-add-log-setup (&optional purpose state prev-state
12187 "Set up the post command hook to take a note.
12188 If this is about to TODO state change, the new state is expected in STATE.
12189 When FINDPOS is non-nil, find the correct position for the note in
12190 the current entry. If not, assume that it can be inserted at point.
12191 HOW is an indicator what kind of note should be created.
12192 EXTRA is additional text that will be inserted into the notes buffer."
12193 (let* ((org-log-into-drawer (org-log-into-drawer))
12194 (drawer (cond ((stringp org-log-into-drawer)
12195 org-log-into-drawer)
12196 (org-log-into-drawer "LOGBOOK")
12201 (org-back-to-heading t)
12202 (narrow-to-region (point) (save-excursion
12203 (outline-next-heading) (point)))
12204 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12205 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12207 (goto-char (match-end 0))
12210 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12213 (goto-char (match-end 0))
12214 (or org-log-states-order-reversed
12215 (and (re-search-forward org-property-end-re nil t)
12216 (goto-char (1- (match-beginning 0))))))
12217 (insert "\n:" drawer ":\n:END:")
12218 (beginning-of-line 0)
12219 (org-indent-line-function)
12220 (beginning-of-line 2)
12221 (org-indent-line-function)
12223 ((and org-log-state-notes-insert-after-drawers
12225 (forward-line) (looking-at org-drawer-regexp)))
12227 (while (looking-at org-drawer-regexp)
12228 (goto-char (match-end 0))
12229 (re-search-forward org-property-end-re (point-max) t)
12231 (forward-line -1)))
12232 (unless org-log-states-order-reversed
12233 (and (= (char-after) ?\n) (forward-char 1))
12234 (org-skip-over-state-notes)
12235 (skip-chars-backward " \t\n\r")))
12236 (move-marker org-log-note-marker (point))
12237 (setq org-log-note-purpose purpose
12238 org-log-note-state state
12239 org-log-note-previous-state prev-state
12240 org-log-note-how how
12241 org-log-note-extra extra
12242 org-log-note-effective-time (org-current-effective-time))
12243 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12245 (defun org-skip-over-state-notes ()
12246 "Skip past the list of State notes in an entry."
12247 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12248 (when (ignore-errors (goto-char (org-in-item-p)))
12249 (let* ((struct (org-list-struct))
12250 (prevs (org-list-prevs-alist struct)))
12251 (while (looking-at "[ \t]*- State")
12252 (goto-char (or (org-list-get-next-item (point) struct prevs)
12253 (org-list-get-item-end (point) struct)))))))
12255 (defun org-add-log-note (&optional purpose)
12256 "Pop up a window for taking a note, and add this note later at point."
12257 (remove-hook 'post-command-hook 'org-add-log-note)
12258 (setq org-log-note-window-configuration (current-window-configuration))
12259 (delete-other-windows)
12260 (move-marker org-log-note-return-to (point))
12261 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12262 (goto-char org-log-note-marker)
12263 (org-switch-to-buffer-other-window "*Org Note*")
12265 (if (memq org-log-note-how '(time state))
12266 (let (current-prefix-arg) (org-store-log-note))
12267 (let ((org-inhibit-startup t)) (org-mode))
12268 (insert (format "# Insert note for %s.
12269 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12271 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12272 ((eq org-log-note-purpose 'done) "closed todo item")
12273 ((eq org-log-note-purpose 'state)
12274 (format "state change from \"%s\" to \"%s\""
12275 (or org-log-note-previous-state "")
12276 (or org-log-note-state "")))
12277 ((eq org-log-note-purpose 'reschedule)
12279 ((eq org-log-note-purpose 'delschedule)
12280 "no longer scheduled")
12281 ((eq org-log-note-purpose 'redeadline)
12282 "changing deadline")
12283 ((eq org-log-note-purpose 'deldeadline)
12284 "removing deadline")
12285 ((eq org-log-note-purpose 'refile)
12287 ((eq org-log-note-purpose 'note)
12289 (t (error "This should not happen")))))
12290 (if org-log-note-extra (insert org-log-note-extra))
12291 (org-set-local 'org-finish-function 'org-store-log-note)
12292 (run-hooks 'org-log-buffer-setup-hook)))
12294 (defvar org-note-abort nil) ; dynamically scoped
12295 (defun org-store-log-note ()
12296 "Finish taking a log note, and insert it to where it belongs."
12297 (let ((txt (buffer-string))
12298 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12300 (kill-buffer (current-buffer))
12301 (while (string-match "\\`#.*\n[ \t\n]*" txt)
12302 (setq txt (replace-match "" t t txt)))
12303 (if (string-match "\\s-+\\'" txt)
12304 (setq txt (replace-match "" t t txt)))
12305 (setq lines (org-split-string txt "\n"))
12306 (when (and note (string-match "\\S-" note))
12308 (org-replace-escapes
12310 (list (cons "%u" (user-login-name))
12311 (cons "%U" user-full-name)
12312 (cons "%t" (format-time-string
12313 (org-time-stamp-format 'long 'inactive)
12314 org-log-note-effective-time))
12315 (cons "%T" (format-time-string
12316 (org-time-stamp-format 'long nil)
12317 org-log-note-effective-time))
12318 (cons "%d" (format-time-string
12319 (org-time-stamp-format nil 'inactive)
12320 org-log-note-effective-time))
12321 (cons "%D" (format-time-string
12322 (org-time-stamp-format nil nil)
12323 org-log-note-effective-time))
12324 (cons "%s" (if org-log-note-state
12325 (concat "\"" org-log-note-state "\"")
12327 (cons "%S" (if org-log-note-previous-state
12328 (concat "\"" org-log-note-previous-state "\"")
12330 (if lines (setq note (concat note " \\\\")))
12332 (when (or current-prefix-arg org-note-abort)
12333 (when org-log-into-drawer
12334 (org-remove-empty-drawer-at
12335 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12336 org-log-note-marker))
12339 (with-current-buffer (marker-buffer org-log-note-marker)
12341 (goto-char org-log-note-marker)
12342 (move-marker org-log-note-marker nil)
12344 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12345 (setq ind (save-excursion
12346 (if (ignore-errors (goto-char (org-in-item-p)))
12347 (let ((struct (org-list-struct)))
12349 (org-list-get-top-point struct) struct))
12350 (skip-chars-backward " \r\t\n")
12352 ((and (org-at-heading-p)
12353 org-adapt-indentation)
12354 (1+ (org-current-level)))
12355 ((org-at-heading-p) 0)
12356 (t (org-get-indentation))))))
12357 (setq bul (org-list-bullet-string "-"))
12358 (org-indent-line-to ind)
12359 (insert bul (pop lines))
12360 (let ((ind-body (+ (length bul) ind)))
12363 (org-indent-line-to ind-body)
12364 (insert (pop lines))))
12365 (message "Note stored")
12366 (org-back-to-heading t)
12367 (org-cycle-hide-drawers 'children)))))
12368 (set-window-configuration org-log-note-window-configuration)
12369 (with-current-buffer (marker-buffer org-log-note-return-to)
12370 (goto-char org-log-note-return-to))
12371 (move-marker org-log-note-return-to nil)
12372 (and org-log-post-message (message "%s" org-log-post-message)))
12374 (defun org-remove-empty-drawer-at (drawer pos)
12375 "Remove an empty drawer DRAWER at position POS.
12376 POS may also be a marker."
12377 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12383 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12384 (replace-match ""))))))
12386 (defun org-sparse-tree (&optional arg)
12387 "Create a sparse tree, prompt for the details.
12388 This command can create sparse trees. You first need to select the type
12389 of match used to create the tree:
12391 t Show all TODO entries.
12392 T Show entries with a specific TODO keyword.
12393 m Show entries selected by a tags/property match.
12394 p Enter a property name and its value (both with completion on existing
12395 names/values) and show entries with that property.
12396 r Show entries matching a regular expression (`/' can be used as well)
12397 d Show deadlines due within `org-deadline-warning-days'.
12398 b Show deadlines and scheduled items before a date.
12399 a Show deadlines and scheduled items after a date."
12401 (let (ans kwd value)
12402 (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
12403 (setq ans (read-char-exclusive))
12406 (call-interactively 'org-check-deadlines))
12408 (call-interactively 'org-check-before-date))
12410 (call-interactively 'org-check-after-date))
12412 (org-show-todo-tree nil))
12414 (org-show-todo-tree '(4)))
12415 ((member ans '(?T ?m))
12416 (call-interactively 'org-match-sparse-tree))
12417 ((member ans '(?p ?P))
12418 (setq kwd (org-icompleting-read "Property: "
12419 (mapcar 'list (org-buffer-property-keys))))
12420 (setq value (org-icompleting-read "Value: "
12421 (mapcar 'list (org-property-values kwd))))
12422 (unless (string-match "\\`{.*}\\'" value)
12423 (setq value (concat "\"" value "\"")))
12424 (org-match-sparse-tree arg (concat kwd "=" value)))
12425 ((member ans '(?r ?R ?/))
12426 (call-interactively 'org-occur))
12427 (t (error "No such sparse tree command \"%c\"" ans)))))
12429 (defvar org-occur-highlights nil
12430 "List of overlays used for occur matches.")
12431 (make-variable-buffer-local 'org-occur-highlights)
12432 (defvar org-occur-parameters nil
12433 "Parameters of the active org-occur calls.
12434 This is a list, each call to org-occur pushes as cons cell,
12435 containing the regular expression and the callback, onto the list.
12436 The list can contain several entries if `org-occur' has been called
12437 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12438 will only contain one set of parameters. When the highlights are
12439 removed (for example with `C-c C-c', or with the next edit (depending
12440 on `org-remove-highlights-with-change'), this variable is emptied
12442 (make-variable-buffer-local 'org-occur-parameters)
12444 (defun org-occur (regexp &optional keep-previous callback)
12445 "Make a compact tree which shows all matches of REGEXP.
12446 The tree will show the lines where the regexp matches, and all higher
12447 headlines above the match. It will also show the heading after the match,
12448 to make sure editing the matching entry is easy.
12449 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12450 call to `org-occur' will be kept, to allow stacking of calls to this
12452 If CALLBACK is non-nil, it is a function which is called to confirm
12453 that the match should indeed be shown."
12454 (interactive "sRegexp: \nP")
12455 (when (equal regexp "")
12456 (error "Regexp cannot be empty"))
12457 (unless keep-previous
12458 (org-remove-occur-highlights nil nil t))
12459 (push (cons regexp callback) org-occur-parameters)
12462 (goto-char (point-min))
12463 (if (or (not keep-previous) ; do not want to keep
12464 (not org-occur-highlights)) ; no previous matches
12467 (while (re-search-forward regexp nil t)
12468 (when (or (not callback)
12469 (save-match-data (funcall callback)))
12470 (setq cnt (1+ cnt))
12471 (when org-highlight-sparse-tree-matches
12472 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12473 (org-show-context 'occur-tree))))
12474 (when org-remove-highlights-with-change
12475 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12477 (unless org-sparse-tree-open-archived-trees
12478 (org-hide-archived-subtrees (point-min) (point-max)))
12479 (run-hooks 'org-occur-hook)
12480 (if (org-called-interactively-p 'interactive)
12481 (message "%d match(es) for regexp %s" cnt regexp))
12484 (defun org-occur-next-match (&optional n reset)
12485 "Function for `next-error-function' to find sparse tree matches.
12486 N is the number of matches to move, when negative move backwards.
12487 RESET is entirely ignored - this function always goes back to the
12488 starting point when no match is found."
12489 (let* ((limit (if (< n 0) (point-min) (point-max)))
12490 (search-func (if (< n 0)
12491 'previous-single-char-property-change
12492 'next-single-char-property-change))
12497 (while (setq p1 (funcall search-func (point) 'org-type))
12498 (when (equal p1 limit)
12500 (error "No more matches"))
12501 (when (equal (get-char-property p1 'org-type) 'org-occur)
12505 (throw 'exit (point))))
12508 (error "No more matches"))))
12510 (defun org-show-context (&optional key)
12511 "Make sure point and context are visible.
12512 How much context is shown depends upon the variables
12513 `org-show-hierarchy-above', `org-show-following-heading'. and
12514 `org-show-siblings'."
12515 (let ((heading-p (org-on-heading-p t))
12516 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12517 (following-p (org-get-alist-option org-show-following-heading key))
12518 (entry-p (org-get-alist-option org-show-entry-below key))
12519 (siblings-p (org-get-alist-option org-show-siblings key)))
12521 ;; Show heading or entry text
12522 (if (and heading-p (not entry-p))
12523 (org-flag-heading nil) ; only show the heading
12524 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12525 (org-show-hidden-entry))) ; show entire entry
12527 ;; Show next sibling, or heading below text
12529 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12530 (org-flag-heading nil))))
12531 (when siblings-p (org-show-siblings))
12533 ;; show all higher headings, possibly with siblings
12535 (while (and (condition-case nil
12536 (progn (org-up-heading-all 1) t)
12539 (org-flag-heading nil)
12540 (when siblings-p (org-show-siblings))))))))
12542 (defvar org-reveal-start-hook nil
12543 "Hook run before revealing a location.")
12545 (defun org-reveal (&optional siblings)
12546 "Show current entry, hierarchy above it, and the following headline.
12547 This can be used to show a consistent set of context around locations
12548 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12549 not t for the search context.
12551 With optional argument SIBLINGS, on each level of the hierarchy all
12552 siblings are shown. This repairs the tree structure to what it would
12553 look like when opened with hierarchical calls to `org-cycle'.
12554 With double optional argument \\[universal-argument] \\[universal-argument], \
12555 go to the parent and show the
12558 (run-hooks 'org-reveal-start-hook)
12559 (let ((org-show-hierarchy-above t)
12560 (org-show-following-heading t)
12561 (org-show-siblings (if siblings t org-show-siblings)))
12562 (org-show-context nil))
12563 (when (equal siblings '(16))
12565 (when (org-up-heading-safe)
12567 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12569 (defun org-highlight-new-match (beg end)
12570 "Highlight from BEG to END and mark the highlight is an occur headline."
12571 (let ((ov (make-overlay beg end)))
12572 (overlay-put ov 'face 'secondary-selection)
12573 (overlay-put ov 'org-type 'org-occur)
12574 (push ov org-occur-highlights)))
12576 (defun org-remove-occur-highlights (&optional beg end noremove)
12577 "Remove the occur highlights from the buffer.
12578 BEG and END are ignored. If NOREMOVE is nil, remove this function
12579 from the `before-change-functions' in the current buffer."
12581 (unless org-inhibit-highlight-removal
12582 (mapc 'delete-overlay org-occur-highlights)
12583 (setq org-occur-highlights nil)
12584 (setq org-occur-parameters nil)
12586 (remove-hook 'before-change-functions
12587 'org-remove-occur-highlights 'local))))
12591 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12592 "Regular expression matching the priority indicator.")
12594 (defvar org-remove-priority-next-time nil)
12596 (defun org-priority-up ()
12597 "Increase the priority of the current item."
12599 (org-priority 'up))
12601 (defun org-priority-down ()
12602 "Decrease the priority of the current item."
12604 (org-priority 'down))
12606 (defun org-priority (&optional action)
12607 "Change the priority of an item by ARG.
12608 ACTION can be `set', `up', `down', or a character."
12610 (unless org-enable-priority-commands
12611 (error "Priority commands are disabled"))
12612 (setq action (or action 'set))
12613 (let (current new news have remove)
12615 (org-back-to-heading t)
12616 (if (looking-at org-priority-regexp)
12617 (setq current (string-to-char (match-string 2))
12620 ((eq action 'remove)
12621 (setq remove t new ?\ ))
12622 ((or (eq action 'set)
12623 (if (featurep 'xemacs) (characterp action) (integerp action)))
12624 (if (not (eq action 'set))
12626 (message "Priority %c-%c, SPC to remove: "
12627 org-highest-priority org-lowest-priority)
12629 (setq new (read-char-exclusive))))
12630 (if (and (= (upcase org-highest-priority) org-highest-priority)
12631 (= (upcase org-lowest-priority) org-lowest-priority))
12632 (setq new (upcase new)))
12633 (cond ((equal new ?\ ) (setq remove t))
12634 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12635 (error "Priority must be between `%c' and `%c'"
12636 org-highest-priority org-lowest-priority))))
12639 (1- current) ; normal cycling
12640 ;; last priority was empty
12641 (if (eq last-command this-command)
12642 org-lowest-priority ; wrap around empty to lowest
12644 (if org-priority-start-cycle-with-default
12645 org-default-priority
12646 (1- org-default-priority))))))
12649 (1+ current) ; normal cycling
12650 ;; last priority was empty
12651 (if (eq last-command this-command)
12652 org-highest-priority ; wrap around empty to highest
12654 (if org-priority-start-cycle-with-default
12655 org-default-priority
12656 (1+ org-default-priority))))))
12657 (t (error "Invalid action")))
12658 (if (or (< (upcase new) org-highest-priority)
12659 (> (upcase new) org-lowest-priority))
12660 (if (and (memq action '(up down))
12661 (not have) (not (eq last-command this-command)))
12662 ;; `new' is from default priority
12664 "The default can not be set, see `org-default-priority' why")
12665 ;; normal cycling: `new' is beyond highest/lowest priority
12666 ;; and is wrapped around to the empty priority
12668 (setq news (format "%c" new))
12671 (replace-match "" t t nil 1)
12672 (replace-match news t t nil 2))
12674 (error "No priority cookie found in line")
12675 (let ((case-fold-search nil))
12676 (looking-at org-todo-line-regexp))
12679 (goto-char (match-end 2))
12680 (insert " [#" news "]"))
12681 (goto-char (match-beginning 3))
12682 (insert "[#" news "] "))))
12683 (org-preserve-lc (org-set-tags nil 'align)))
12685 (message "Priority removed")
12686 (message "Priority of current item set to %s" news))))
12688 (defun org-get-priority (s)
12689 "Find priority cookie and return priority."
12690 (if (functionp org-get-priority-function)
12691 (funcall org-get-priority-function)
12693 (if (not (string-match org-priority-regexp s))
12694 (* 1000 (- org-lowest-priority org-default-priority))
12695 (* 1000 (- org-lowest-priority
12696 (string-to-char (match-string 2 s))))))))
12700 (defvar org-agenda-archives-mode)
12701 (defvar org-map-continue-from nil
12702 "Position from where mapping should continue.
12703 Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
12705 (defvar org-scanner-tags nil
12706 "The current tag list while the tags scanner is running.")
12707 (defvar org-trust-scanner-tags nil
12708 "Should `org-get-tags-at' use the tags for the scanner.
12709 This is for internal dynamical scoping only.
12710 When this is non-nil, the function `org-get-tags-at' will return the value
12711 of `org-scanner-tags' instead of building the list by itself. This
12712 can lead to large speed-ups when the tags scanner is used in a file with
12713 many entries, and when the list of tags is retrieved, for example to
12714 obtain a list of properties. Building the tags list for each entry in such
12715 a file becomes an N^2 operation - but with this variable set, it scales
12718 (defun org-scan-tags (action matcher &optional todo-only)
12719 "Scan headline tags with inheritance and produce output ACTION.
12721 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
12722 or `agenda' to produce an entry list for an agenda view. It can also be
12723 a Lisp form or a function that should be called at each matched headline, in
12724 this case the return value is a list of all return values from these calls.
12726 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
12727 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
12728 only lines with a TODO keyword are included in the output."
12729 (require 'org-agenda)
12730 (let* ((re (concat "^" org-outline-regexp " *\\(\\<\\("
12731 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
12733 "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
12734 (props (list 'face 'default
12735 'done-face 'org-agenda-done
12736 'undone-face 'default
12737 'mouse-face 'highlight
12738 'org-not-done-regexp org-not-done-regexp
12739 'org-todo-regexp org-todo-regexp
12740 'org-complex-heading-regexp org-complex-heading-regexp
12742 (format "mouse-2 or RET jump to org file %s"
12743 (abbreviate-file-name
12744 (or (buffer-file-name (buffer-base-buffer))
12745 (buffer-name (buffer-base-buffer)))))))
12746 (case-fold-search nil)
12747 (org-map-continue-from nil)
12748 lspos tags tags-list
12749 (tags-alist (list (cons 0 org-file-tags)))
12750 (llast 0) rtn rtn1 level category i txt
12751 todo marker entry priority)
12752 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
12753 (setq action (list 'lambda nil action)))
12755 (goto-char (point-min))
12756 (when (eq action 'sparse-tree)
12758 (org-remove-occur-highlights))
12759 (while (re-search-forward re nil t)
12761 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
12762 tags (if (match-end 4) (org-match-string-no-properties 4)))
12763 (goto-char (setq lspos (match-beginning 0)))
12764 (setq level (org-reduced-level (funcall outline-level))
12765 category (org-get-category))
12766 (setq i llast llast level)
12767 ;; remove tag lists from same and sublevels
12768 (while (>= i level)
12769 (when (setq entry (assoc i tags-alist))
12770 (setq tags-alist (delete entry tags-alist)))
12772 ;; add the next tags
12774 (setq tags (org-split-string tags ":")
12776 (cons (cons level tags) tags-alist)))
12777 ;; compile tags for current headline
12779 (if org-use-tag-inheritance
12780 (apply 'append (mapcar 'cdr (reverse tags-alist)))
12782 org-scanner-tags tags-list)
12783 (when org-use-tag-inheritance
12784 (setcdr (car tags-alist)
12785 (mapcar (lambda (x)
12786 (setq x (copy-sequence x))
12787 (org-add-prop-inherited x))
12788 (cdar tags-alist))))
12789 (when (and tags org-use-tag-inheritance
12790 (or (not (eq t org-use-tag-inheritance))
12791 org-tags-exclude-from-inheritance))
12792 ;; selective inheritance, remove uninherited ones
12793 (setcdr (car tags-alist)
12794 (org-remove-uninherited-tags (cdar tags-alist))))
12797 ;; eval matcher only when the todo condition is OK
12798 (and (or (not todo-only) (member todo org-not-done-keywords))
12799 (let ((case-fold-search t)) (eval matcher)))
12801 ;; Call the skipper, but return t if it does not skip,
12802 ;; so that the `and' form continues evaluating
12804 (unless (eq action 'sparse-tree) (org-agenda-skip))
12807 ;; Check if timestamps are deselecting this entry
12808 (or (not todo-only)
12809 (and (member todo org-not-done-keywords)
12810 (or (not org-agenda-tags-todo-honor-ignore-options)
12811 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
12813 ;; Extra check for the archive tag
12814 ;; FIXME: Does the skipper already do this????
12816 (not (member org-archive-tag tags-list))
12817 ;; we have an archive tag, should we use this anyway?
12818 (or (not org-agenda-skip-archived-trees)
12819 (and (eq action 'agenda) org-agenda-archives-mode))))
12821 ;; select this headline
12824 ((eq action 'sparse-tree)
12825 (and org-highlight-sparse-tree-matches
12826 (org-get-heading) (match-end 0)
12827 (org-highlight-new-match
12828 (match-beginning 0) (match-beginning 1)))
12829 (org-show-context 'tags-tree))
12830 ((eq action 'agenda)
12831 (setq txt (org-agenda-format-item
12834 (if (eq org-tags-match-list-sublevels 'indented)
12835 (make-string (1- level) ?.) "")
12840 priority (org-get-priority txt))
12842 (setq marker (org-agenda-new-marker))
12843 (org-add-props txt props
12844 'org-marker marker 'org-hd-marker marker 'org-category category
12846 'priority priority 'type "tagsmatch")
12848 ((functionp action)
12849 (setq org-map-continue-from nil)
12851 (setq rtn1 (funcall action))
12853 (t (error "Invalid action")))
12855 ;; if we are to skip sublevels, jump to end of subtree
12856 (unless org-tags-match-list-sublevels
12857 (org-end-of-subtree t)
12858 (backward-char 1))))
12859 ;; Get the correct position from where to continue
12860 (if org-map-continue-from
12861 (goto-char org-map-continue-from)
12862 (and (= (point) lspos) (end-of-line 1)))))
12863 (when (and (eq action 'sparse-tree)
12864 (not org-sparse-tree-open-archived-trees))
12865 (org-hide-archived-subtrees (point-min) (point-max)))
12868 (defun org-remove-uninherited-tags (tags)
12869 "Remove all tags that are not inherited from the list TAGS."
12871 ((eq org-use-tag-inheritance t)
12872 (if org-tags-exclude-from-inheritance
12873 (org-delete-all org-tags-exclude-from-inheritance tags)
12875 ((not org-use-tag-inheritance) nil)
12876 ((stringp org-use-tag-inheritance)
12879 (if (and (string-match org-use-tag-inheritance x)
12880 (not (member x org-tags-exclude-from-inheritance)))
12883 ((listp org-use-tag-inheritance)
12886 (if (member x org-use-tag-inheritance) x nil))
12889 (defvar todo-only) ;; dynamically scoped
12891 (defun org-match-sparse-tree (&optional todo-only match)
12892 "Create a sparse tree according to tags string MATCH.
12893 MATCH can contain positive and negative selection of tags, like
12894 \"+WORK+URGENT-WITHBOSS\".
12895 If optional argument TODO-ONLY is non-nil, only select lines that are
12898 (org-prepare-agenda-buffers (list (current-buffer)))
12899 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
12901 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
12903 (defvar org-cached-props nil)
12904 (defun org-cached-entry-get (pom property)
12905 (if (or (eq t org-use-property-inheritance)
12906 (and (stringp org-use-property-inheritance)
12907 (string-match org-use-property-inheritance property))
12908 (and (listp org-use-property-inheritance)
12909 (member property org-use-property-inheritance)))
12910 ;; Caching is not possible, check it directly
12911 (org-entry-get pom property 'inherit)
12912 ;; Get all properties, so that we can do complicated checks easily
12913 (cdr (assoc property (or org-cached-props
12914 (setq org-cached-props
12915 (org-entry-properties pom)))))))
12917 (defun org-global-tags-completion-table (&optional files)
12918 "Return the list of all tags in all agenda buffer/files.
12919 Optional FILES argument is a list of files to which can be used
12920 instead of the agenda files."
12927 (set-buffer (find-file-noselect file))
12928 (append (org-get-buffer-tags)
12929 (mapcar (lambda (x) (if (stringp (car-safe x))
12930 (list (car-safe x)) nil))
12932 (if (and files (car files))
12934 (org-agenda-files))))))))
12936 (defun org-make-tags-matcher (match)
12937 "Create the TAGS/TODO matcher form for the selection string MATCH."
12938 ;; todo-only is scoped dynamically into this function, and the function
12939 ;; may change it if the matcher asks for it.
12941 ;; Get a new match request, with completion
12942 (let ((org-last-tags-completion-table
12943 (org-global-tags-completion-table)))
12944 (setq match (org-completing-read-no-i
12945 "Match: " 'org-tags-completion-function nil nil nil
12946 'org-tags-history))))
12948 ;; Parse the string and create a lisp form
12949 (let ((match0 match)
12950 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
12952 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
12953 orterms term orlist re-p str-p level-p level-op time-p
12954 prop-p pn pv po gv rest)
12955 (if (string-match "/+" match)
12956 ;; match contains also a todo-matching request
12958 (setq tagsmatch (substring match 0 (match-beginning 0))
12959 todomatch (substring match (match-end 0)))
12960 (if (string-match "^!" todomatch)
12961 (setq todo-only t todomatch (substring todomatch 1)))
12962 (if (string-match "^\\s-*$" todomatch)
12963 (setq todomatch nil)))
12964 ;; only matching tags
12965 (setq tagsmatch match todomatch nil))
12967 ;; Make the tags matcher
12968 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
12969 (setq tagsmatcher t)
12970 (setq orterms (org-split-string tagsmatch "|") orlist nil)
12971 (while (setq term (pop orterms))
12972 (while (and (equal (substring term -1) "\\") orterms)
12973 (setq term (concat term "|" (pop orterms)))) ; repair bad split
12974 (while (string-match re term)
12975 (setq rest (substring term (match-end 0))
12976 minus (and (match-end 1)
12977 (equal (match-string 1 term) "-"))
12978 tag (save-match-data (replace-regexp-in-string
12980 (match-string 2 term)))
12981 re-p (equal (string-to-char tag) ?{)
12982 level-p (match-end 4)
12983 prop-p (match-end 5)
12985 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
12987 (setq level-op (org-op-to-function (match-string 3 term)))
12988 `(,level-op level ,(string-to-number
12989 (match-string 4 term))))
12991 (setq pn (match-string 5 term)
12992 po (match-string 6 term)
12993 pv (match-string 7 term)
12994 re-p (equal (string-to-char pv) ?{)
12995 str-p (equal (string-to-char pv) ?\")
12996 time-p (save-match-data
12997 (string-match "^\"[[<].*[]>]\"$" pv))
12998 pv (if (or re-p str-p) (substring pv 1 -1) pv))
12999 (if time-p (setq pv (org-matcher-time pv)))
13000 (setq po (org-op-to-function po (if time-p 'time str-p)))
13002 ((equal pn "CATEGORY")
13003 (setq gv '(get-text-property (point) 'org-category)))
13007 (setq gv `(org-cached-entry-get nil ,pn))))
13010 `(not (string-match ,pv (or ,gv "")))
13011 `(string-match ,pv (or ,gv "")))
13013 `(,po (or ,gv "") ,pv)
13014 `(,po (string-to-number (or ,gv ""))
13015 ,(string-to-number pv) ))))
13016 (t `(member ,tag tags-list)))
13017 mm (if minus (list 'not mm) mm)
13019 (push mm tagsmatcher))
13020 (push (if (> (length tagsmatcher) 1)
13021 (cons 'and tagsmatcher)
13024 (setq tagsmatcher nil))
13025 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13027 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13028 ;; Make the todo matcher
13029 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13030 (setq todomatcher t)
13031 (setq orterms (org-split-string todomatch "|") orlist nil)
13032 (while (setq term (pop orterms))
13033 (while (string-match re term)
13034 (setq minus (and (match-end 1)
13035 (equal (match-string 1 term) "-"))
13036 kwd (match-string 2 term)
13037 re-p (equal (string-to-char kwd) ?{)
13038 term (substring term (match-end 0))
13040 `(string-match ,(substring kwd 1 -1) todo)
13041 (list 'equal 'todo kwd))
13042 mm (if minus (list 'not mm) mm))
13043 (push mm todomatcher))
13044 (push (if (> (length todomatcher) 1)
13045 (cons 'and todomatcher)
13048 (setq todomatcher nil))
13049 (setq todomatcher (if (> (length orlist) 1)
13050 (cons 'or orlist) (car orlist))))
13052 ;; Return the string and lisp forms of the matcher
13053 (setq matcher (if todomatcher
13054 (list 'and tagsmatcher todomatcher)
13056 (cons match0 matcher)))
13058 (defun org-op-to-function (op &optional stringp)
13059 "Turn an operator into the appropriate function."
13062 ((equal op "<" ) '(< string< org-time<))
13063 ((equal op ">" ) '(> org-string> org-time>))
13064 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13065 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13066 ((member op '("=" "==")) '(= string= org-time=))
13067 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13068 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13070 (defun org<> (a b) (not (= a b)))
13071 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13072 (defun org-string>= (a b) (not (string< a b)))
13073 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13074 (defun org-string<> (a b) (not (string= a b)))
13075 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13076 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13077 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13078 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13079 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13080 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13082 "Convert S to a floating point time.
13083 If S is already a number, just return it. If it is a string, parse
13084 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13088 (condition-case nil
13089 (float-time (apply 'encode-time (org-parse-time-string s)))
13093 (defun org-time-today ()
13094 "Time in seconds today at 0:00.
13095 Returns the float number of seconds since the beginning of the
13096 epoch to the beginning of today (00:00)."
13097 (float-time (apply 'encode-time
13098 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13100 (defun org-matcher-time (s)
13101 "Interpret a time comparison value."
13104 ((string= s "<now>") (float-time))
13105 ((string= s "<today>") (org-time-today))
13106 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13107 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13108 ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
13109 (+ (org-time-today)
13110 (* (string-to-number (match-string 1 s))
13111 (cdr (assoc (match-string 2 s)
13112 '(("d" . 86400.0) ("w" . 604800.0)
13113 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13116 (defun org-match-any-p (re list)
13117 "Does re match any element of list?"
13118 (setq list (mapcar (lambda (x) (string-match re x)) list))
13121 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13122 (defvar org-tags-overlay (make-overlay 1 1))
13123 (org-detach-overlay org-tags-overlay)
13125 (defun org-get-local-tags-at (&optional pos)
13126 "Get a list of tags defined in the current headline."
13127 (org-get-tags-at pos 'local))
13129 (defun org-get-local-tags ()
13130 "Get a list of tags defined in the current headline."
13131 (org-get-tags-at nil 'local))
13133 (defun org-get-tags-at (&optional pos local)
13134 "Get a list of all headline tags applicable at POS.
13135 POS defaults to point. If tags are inherited, the list contains
13136 the targets in the same sequence as the headlines appear, i.e.
13137 the tags of the current headline come last.
13138 When LOCAL is non-nil, only return tags from the current headline,
13139 ignore inherited ones."
13141 (if (and org-trust-scanner-tags
13142 (or (not pos) (equal pos (point)))
13145 (let (tags ltags lastpos parent)
13149 (goto-char (or pos (point)))
13152 (condition-case nil
13154 (org-back-to-heading t)
13155 (while (not (equal lastpos (point)))
13156 (setq lastpos (point))
13158 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13159 (setq ltags (org-split-string
13160 (org-match-string-no-properties 1) ":"))
13162 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13165 (org-remove-uninherited-tags ltags)
13168 (or org-use-tag-inheritance (throw 'done t))
13169 (if local (throw 'done t))
13170 (or (org-up-heading-safe) (error nil))
13175 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13177 (defun org-add-prop-inherited (s)
13178 (add-text-properties 0 (length s) '(inherited t) s)
13181 (defun org-toggle-tag (tag &optional onoff)
13182 "Toggle the tag TAG for the current line.
13183 If ONOFF is `on' or `off', don't toggle but set to this state."
13186 (org-back-to-heading t)
13187 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13190 (setq current (match-string 1))
13191 (replace-match ""))
13193 (setq current (nreverse (org-split-string current ":")))
13197 (or (member tag current) (push tag current)))
13199 (or (not (member tag current)) (setq current (delete tag current))))
13200 (t (if (member tag current)
13201 (setq current (delete tag current))
13203 (push tag current))))
13207 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13208 (org-set-tags nil t))
13209 (delete-horizontal-space))
13210 (run-hooks 'org-after-tags-change-hook))
13213 (defun org-align-tags-here (to-col)
13214 ;; Assumes that this is a headline
13215 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13216 (beginning-of-line 1)
13217 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13218 (< pos (match-beginning 2)))
13220 (setq tags-l (- (match-end 2) (match-beginning 2)))
13221 (goto-char (match-beginning 1))
13223 (delete-region (point) (1+ (match-beginning 2)))
13224 (setq ncol (max (current-column)
13228 (- (abs to-col) tags-l))))
13230 (insert (make-string (- ncol (current-column)) ?\ ))
13231 (setq ncol (current-column))
13232 (when indent-tabs-mode (tabify p (point-at-eol)))
13233 (org-move-to-column (min ncol col) t))
13236 (defun org-set-tags-command (&optional arg just-align)
13237 "Call the set-tags command for the current entry."
13239 (if (org-on-heading-p)
13240 (org-set-tags arg just-align)
13242 (org-back-to-heading t)
13243 (org-set-tags arg just-align))))
13245 (defun org-set-tags-to (data)
13246 "Set the tags of the current entry to DATA, replacing the current tags.
13247 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13248 If DATA is nil or the empty string, any tags will be removed."
13249 (interactive "sTags: ")
13253 ((equal data "") "")
13255 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13258 (concat ":" (mapconcat 'identity data ":") ":"))
13262 (org-back-to-heading t)
13263 (when (looking-at org-complex-heading-regexp)
13266 (goto-char (match-beginning 5))
13268 (delete-region (point) (point-at-eol))
13269 (org-set-tags nil 'align))
13270 (goto-char (point-at-eol))
13272 (org-set-tags nil 'align)))
13273 (beginning-of-line 1)
13274 (if (looking-at ".*?\\([ \t]+\\)$")
13275 (delete-region (match-beginning 1) (match-end 1))))))
13277 (defun org-align-all-tags ()
13278 "Align the tags i all headings."
13281 (or (ignore-errors (org-back-to-heading t))
13282 (outline-next-heading))
13283 (if (org-on-heading-p)
13285 (message "No headings"))))
13287 (defvar org-indent-indentation-per-level)
13288 (defun org-set-tags (&optional arg just-align)
13289 "Set the tags for the current headline.
13290 With prefix ARG, realign all tags in headings in the current buffer."
13292 (let* ((re org-outline-regexp-bol)
13293 (current (org-get-tags-string))
13294 (col (current-column))
13295 (org-setting-tags t)
13296 table current-tags inherited-tags ; computed below when needed
13297 tags p0 c0 c1 rpl di tc level)
13300 (goto-char (point-min))
13301 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13302 (while (re-search-forward re nil t)
13303 (org-set-tags nil t)
13305 (message "All tags realigned to column %d" org-tags-column))
13307 (setq tags current)
13308 ;; Get a new set of tags from the user
13310 (setq table (append org-tag-persistent-alist
13311 (or org-tag-alist (org-get-buffer-tags))
13313 org-complete-tags-always-offer-all-agenda-tags
13314 (org-global-tags-completion-table
13315 (org-agenda-files))))
13316 org-last-tags-completion-table table
13317 current-tags (org-split-string current ":")
13318 inherited-tags (nreverse
13319 (nthcdr (length current-tags)
13320 (nreverse (org-get-tags-at))))
13322 (if (or (eq t org-use-fast-tag-selection)
13323 (and org-use-fast-tag-selection
13324 (delq nil (mapcar 'cdr table))))
13325 (org-fast-tag-selection
13326 current-tags inherited-tags table
13327 (if org-fast-tag-selection-include-todo
13328 org-todo-key-alist))
13329 (let ((org-add-colon-after-tag-completion t))
13331 (org-icompleting-read "Tags: "
13332 'org-tags-completion-function
13333 nil nil current 'org-tags-history))))))
13334 (while (string-match "[-+&]+" tags)
13335 ;; No boolean logic, just a list
13336 (setq tags (replace-match ":" t t tags))))
13338 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13340 (if org-tags-sort-function
13341 (setq tags (mapconcat 'identity
13342 (sort (org-split-string
13343 tags (org-re "[^[:alnum:]_@#%]+"))
13344 org-tags-sort-function) ":")))
13346 (if (string-match "\\`[\t ]*\\'" tags)
13348 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13349 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13351 ;; Insert new tags at the correct column
13352 (beginning-of-line 1)
13353 (setq level (or (and (looking-at org-outline-regexp)
13354 (- (match-end 0) (point) 1))
13357 ((and (equal current "") (equal tags "")))
13358 ((re-search-forward
13359 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13361 (if (equal tags "")
13363 (goto-char (match-beginning 0))
13364 (setq c0 (current-column)
13365 ;; compute offset for the case of org-indent-mode active
13366 di (if org-indent-mode
13367 (* (1- org-indent-indentation-per-level) (1- level))
13369 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13370 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13371 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13372 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13373 (replace-match rpl t t)
13374 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13376 (t (error "Tags alignment failed")))
13377 (org-move-to-column col)
13379 (run-hooks 'org-after-tags-change-hook)))))
13381 (defun org-change-tag-in-region (beg end tag off)
13382 "Add or remove TAG for each entry in the region.
13383 This works in the agenda, and also in an org-mode buffer."
13385 (list (region-beginning) (region-end)
13386 (let ((org-last-tags-completion-table
13387 (if (eq major-mode 'org-mode)
13388 (org-get-buffer-tags)
13389 (org-global-tags-completion-table))))
13390 (org-icompleting-read
13391 "Tag: " 'org-tags-completion-function nil nil nil
13392 'org-tags-history))
13394 (message "[s]et or
[r]emove? ")
13395 (equal (read-char-exclusive) ?r))))
13396 (if (fboundp 'deactivate-mark) (deactivate-mark))
13397 (let ((agendap (equal major-mode 'org-agenda-mode))
13398 l1 l2 m buf pos newhead (cnt 0))
13400 (setq l2 (1- (org-current-line)))
13402 (setq l1 (org-current-line))
13403 (loop for l from l1 to l2 do
13405 (setq m (get-text-property (point) 'org-hd-marker))
13406 (when (or (and (eq major-mode 'org-mode) (org-on-heading-p))
13408 (setq buf (if agendap (marker-buffer m) (current-buffer))
13409 pos (if agendap m (point)))
13410 (with-current-buffer buf
13414 (setq cnt (1+ cnt))
13415 (org-toggle-tag tag (if off 'off 'on))
13416 (setq newhead (org-get-heading)))))
13417 (and agendap (org-agenda-change-all-lines newhead m))))
13418 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13420 (defun org-tags-completion-function (string predicate &optional flag)
13421 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13422 (confirm (lambda (x) (stringp (car x)))))
13423 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13424 (setq s1 (match-string 1 string)
13425 s2 (match-string 2 string))
13426 (setq s1 "" s2 string))
13430 (setq rtn (try-completion s2 ctable confirm))
13433 (concat s1 s2 (substring rtn (length s2))
13434 (if (and org-add-colon-after-tag-completion
13435 (assoc rtn ctable))
13440 (all-completions s2 ctable confirm)
13444 (assoc s2 ctable)))
13447 (defun org-fast-tag-insert (kwd tags face &optional end)
13448 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13449 (insert (format "%-12s" (concat kwd ":"))
13450 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13453 (defun org-fast-tag-show-exit (flag)
13456 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13457 (replace-match ""))
13460 (org-move-to-column (- (window-width) 19) t)
13461 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13463 (defun org-set-current-tags-overlay (current prefix)
13464 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13465 (if (featurep 'xemacs)
13466 (org-overlay-display org-tags-overlay (concat prefix s)
13467 'secondary-selection)
13468 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13469 (org-overlay-display org-tags-overlay (concat prefix s)))))
13471 (defvar org-last-tag-selection-key nil)
13472 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13473 "Fast tag selection with single keys.
13474 CURRENT is the current list of tags in the headline, INHERITED is the
13475 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13476 possibly with grouping information. TODO-TABLE is a similar table with
13477 TODO keywords, should these have keys assigned to them.
13478 If the keys are nil, a-z are automatically assigned.
13479 Returns the new tags string, or nil to not change the current settings."
13480 (let* ((fulltable (append table todo-table))
13481 (maxlen (apply 'max (mapcar
13483 (if (stringp (car x)) (string-width (car x)) 0))
13485 (buf (current-buffer))
13486 (expert (eq org-fast-tag-selection-single-key 'expert))
13488 (fwidth (+ maxlen 3 1 3))
13489 (ncol (/ (- (window-width) 4) fwidth))
13492 tg cnt e c char c1 c2 ntable tbl rtn
13493 ov-start ov-end ov-prefix
13494 (exit-after-next org-fast-tag-selection-single-key)
13495 (done-keywords org-done-keywords)
13498 (beginning-of-line 1)
13500 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13501 (setq ov-start (match-beginning 1)
13502 ov-end (match-end 1)
13504 (setq ov-start (1- (point-at-eol))
13505 ov-end (1+ ov-start))
13506 (skip-chars-forward "^\n\r")
13509 (buffer-substring (1- (point)) (point))
13510 (if (> (current-column) org-tags-column)
13512 (make-string (- org-tags-column (current-column)) ?\ ))))))
13513 (move-overlay org-tags-overlay ov-start ov-end)
13514 (save-window-excursion
13516 (set-buffer (get-buffer-create " *Org tags*"))
13517 (delete-other-windows)
13518 (split-window-vertically)
13519 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13521 (org-set-local 'org-done-keywords done-keywords)
13522 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13523 (org-fast-tag-insert "Current" current c-face "\n\n")
13524 (org-fast-tag-show-exit exit-after-next)
13525 (org-set-current-tags-overlay current ov-prefix)
13526 (setq tbl fulltable char ?a cnt 0)
13527 (while (setq e (pop tbl))
13529 ((equal (car e) :startgroup)
13530 (push '() groups) (setq ingroup t)
13531 (when (not (= cnt 0))
13534 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13535 ((equal (car e) :endgroup)
13536 (setq ingroup nil cnt 0)
13537 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13538 ((equal e '(:newline))
13539 (when (not (= cnt 0))
13543 (while (equal (car tbl) '(:newline))
13545 (setq tbl (cdr tbl)))))
13547 (setq tg (copy-sequence (car e)) c2 nil)
13550 ;; automatically assign a character.
13551 (setq c1 (string-to-char
13552 (downcase (substring
13553 tg (if (= (string-to-char tg) ?@) 1 0)))))
13554 (if (or (rassoc c1 ntable) (rassoc c1 table))
13555 (while (or (rassoc char ntable) (rassoc char table))
13556 (setq char (1+ char)))
13558 (setq c (or c2 char)))
13559 (if ingroup (push tg (car groups)))
13560 (setq tg (org-add-props tg nil 'face
13562 ((not (assoc tg table))
13563 (org-get-todo-face tg))
13564 ((member tg current) c-face)
13565 ((member tg inherited) i-face)
13567 (if (and (= cnt 0) (not ingroup)) (insert " "))
13568 (insert "[" c "] " tg (make-string
13569 (- fwidth 4 (length tg)) ?\ ))
13570 (push (cons tg c) ntable)
13571 (when (= (setq cnt (1+ cnt)) ncol)
13573 (if ingroup (insert " "))
13575 (setq ntable (nreverse ntable))
13577 (goto-char (point-min))
13578 (if (not expert) (org-fit-window-to-buffer))
13582 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13583 (if (not groups) "no " "")
13584 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13585 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13586 (setq org-last-tag-selection-key c)
13588 ((= c ?\r) (throw 'exit t))
13590 (setq groups (not groups))
13591 (goto-char (point-min))
13592 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13595 (org-fast-tag-show-exit
13596 (setq exit-after-next (not exit-after-next)))
13598 (delete-other-windows)
13599 (set-window-buffer (split-window-vertically) " *Org tags*")
13600 (org-switch-to-buffer-other-window " *Org tags*")
13601 (org-fit-window-to-buffer)))
13603 (and (= c ?q) (not (rassoc c ntable))))
13604 (org-detach-overlay org-tags-overlay)
13605 (setq quit-flag t))
13608 (if exit-after-next (setq exit-after-next 'now)))
13610 (condition-case nil
13611 (setq tg (org-icompleting-read
13614 (with-current-buffer buf
13615 (org-get-buffer-tags)))))
13616 (quit (setq tg "")))
13617 (when (string-match "\\S-" tg)
13618 (add-to-list 'buffer-tags (list tg))
13619 (if (member tg current)
13620 (setq current (delete tg current))
13621 (push tg current)))
13622 (if exit-after-next (setq exit-after-next 'now)))
13623 ((setq e (rassoc c todo-table) tg (car e))
13624 (with-current-buffer buf
13625 (save-excursion (org-todo tg)))
13626 (if exit-after-next (setq exit-after-next 'now)))
13627 ((setq e (rassoc c ntable) tg (car e))
13628 (if (member tg current)
13629 (setq current (delete tg current))
13630 (loop for g in groups do
13633 (setq current (delete x current)))
13636 (if exit-after-next (setq exit-after-next 'now))))
13638 ;; Create a sorted list
13642 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13643 (if (eq exit-after-next 'now) (throw 'exit t))
13644 (goto-char (point-min))
13645 (beginning-of-line 2)
13646 (delete-region (point) (point-at-eol))
13647 (org-fast-tag-insert "Current" current c-face)
13648 (org-set-current-tags-overlay current ov-prefix)
13649 (while (re-search-forward
13650 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
13651 (setq tg (match-string 1))
13652 (add-text-properties
13653 (match-beginning 1) (match-end 1)
13656 ((member tg current) c-face)
13657 ((member tg inherited) i-face)
13658 (t (get-text-property (match-beginning 1) 'face))))))
13659 (goto-char (point-min)))))
13660 (org-detach-overlay org-tags-overlay)
13662 (mapconcat 'identity current ":")
13665 (defun org-get-tags-string ()
13666 "Get the TAGS string in the current headline."
13667 (unless (org-on-heading-p t)
13668 (error "Not on a heading"))
13670 (beginning-of-line 1)
13671 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13672 (org-match-string-no-properties 1)
13675 (defun org-get-tags ()
13676 "Get the list of tags specified in the current headline."
13677 (org-split-string (org-get-tags-string) ":"))
13679 (defun org-get-buffer-tags ()
13680 "Get a table of all tags used in the buffer, for completion."
13683 (goto-char (point-min))
13684 (while (re-search-forward
13685 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
13686 (when (equal (char-after (point-at-bol 0)) ?*)
13687 (mapc (lambda (x) (add-to-list 'tags x))
13688 (org-split-string (org-match-string-no-properties 1) ":")))))
13689 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
13690 (mapcar 'list tags)))
13692 ;;;; The mapping API
13695 (defun org-map-entries (func &optional match scope &rest skip)
13696 "Call FUNC at each headline selected by MATCH in SCOPE.
13698 FUNC is a function or a lisp form. The function will be called without
13699 arguments, with the cursor positioned at the beginning of the headline.
13700 The return values of all calls to the function will be collected and
13701 returned as a list.
13703 The call to FUNC will be wrapped into a save-excursion form, so FUNC
13704 does not need to preserve point. After evaluation, the cursor will be
13705 moved to the end of the line (presumably of the headline of the
13706 processed entry) and search continues from there. Under some
13707 circumstances, this may not produce the wanted results. For example,
13708 if you have removed (e.g. archived) the current (sub)tree it could
13709 mean that the next entry will be skipped entirely. In such cases, you
13710 can specify the position from where search should continue by making
13711 FUNC set the variable `org-map-continue-from' to the desired buffer
13714 MATCH is a tags/property/todo match as it is used in the agenda tags view.
13715 Only headlines that are matched by this query will be considered during
13716 the iteration. When MATCH is nil or t, all headlines will be
13717 visited by the iteration.
13719 SCOPE determines the scope of this command. It can be any of:
13721 nil The current buffer, respecting the restriction if any
13722 tree The subtree started with the entry at point
13723 region The entries within the active region, if any
13724 file The current buffer, without restriction
13726 The current buffer, and any archives associated with it
13727 agenda All agenda files
13728 agenda-with-archives
13729 All agenda files with any archive files associated with them
13731 If this is a list, all files in the list will be scanned
13733 The remaining args are treated as settings for the skipping facilities of
13734 the scanner. The following items can be given here:
13736 archive skip trees with the archive tag.
13737 comment skip trees with the COMMENT keyword
13738 function or Emacs Lisp form:
13739 will be used as value for `org-agenda-skip-function', so whenever
13740 the function returns t, FUNC will not be called for that
13741 entry and search will continue from the point where the
13742 function leaves it.
13744 If your function needs to retrieve the tags including inherited tags
13745 at the *current* entry, you can use the value of the variable
13746 `org-scanner-tags' which will be much faster than getting the value
13747 with `org-get-tags-at'. If your function gets properties with
13748 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
13749 to t around the call to `org-entry-properties' to get the same speedup.
13750 Note that if your function moves around to retrieve tags and properties at
13751 a *different* entry, you cannot use these techniques."
13752 (unless (and (eq scope 'region) (not (org-region-active-p)))
13753 (let* ((org-agenda-archives-mode nil) ; just to make sure
13754 (org-agenda-skip-archived-trees (memq 'archive skip))
13755 (org-agenda-skip-comment-trees (memq 'comment skip))
13756 (org-agenda-skip-function
13757 (car (org-delete-all '(comment archive) skip)))
13758 (org-tags-match-list-sublevels t)
13760 org-todo-keywords-for-agenda
13761 org-done-keywords-for-agenda
13762 org-todo-keyword-alist-for-agenda
13763 org-drawers-for-agenda
13764 org-tag-alist-for-agenda)
13767 ((eq match t) (setq matcher t))
13768 ((eq match nil) (setq matcher t))
13769 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
13773 (cond ((eq scope 'tree)
13774 (org-back-to-heading t)
13775 (org-narrow-to-subtree)
13777 ((and (eq scope 'region) (org-region-active-p))
13778 (narrow-to-region (region-beginning)
13780 (goto-char (region-end))
13781 (unless (and (bolp) (org-at-heading-p))
13782 (outline-next-heading))
13788 (org-prepare-agenda-buffers
13789 (list (buffer-file-name (current-buffer))))
13790 (setq res (org-scan-tags func matcher)))
13791 ;; Get the right scope
13793 ((and scope (listp scope) (symbolp (car scope)))
13794 (setq scope (eval scope)))
13795 ((eq scope 'agenda)
13796 (setq scope (org-agenda-files t)))
13797 ((eq scope 'agenda-with-archives)
13798 (setq scope (org-agenda-files t))
13799 (setq scope (org-add-archive-files scope)))
13801 (setq scope (list (buffer-file-name))))
13802 ((eq scope 'file-with-archives)
13803 (setq scope (org-add-archive-files (list (buffer-file-name))))))
13804 (org-prepare-agenda-buffers scope)
13805 (while (setq file (pop scope))
13806 (with-current-buffer (org-find-base-buffer-visiting file)
13810 (goto-char (point-min))
13811 (setq res (append res (org-scan-tags func matcher))))))))))
13816 ;;; Setting and retrieving properties
13818 (defconst org-special-properties
13819 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
13820 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
13821 "The special properties valid in Org-mode.
13823 These are properties that are not defined in the property drawer,
13824 but in some other way.")
13826 (defconst org-default-properties
13827 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
13828 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
13829 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
13830 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
13831 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
13832 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
13833 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
13834 "Some properties that are used by Org-mode for various purposes.
13835 Being in this list makes sure that they are offered for completion.")
13837 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
13838 "Regular expression matching the first line of a property drawer.")
13840 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
13841 "Regular expression matching the last line of a property drawer.")
13843 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
13844 "Regular expression matching the first line of a property drawer.")
13846 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
13847 "Regular expression matching the first line of a property drawer.")
13849 (defconst org-property-drawer-re
13850 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
13851 org-property-end-re "\\)\n?")
13852 "Matches an entire property drawer.")
13854 (defconst org-clock-drawer-re
13855 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
13856 org-property-end-re "\\)\n?")
13857 "Matches an entire clock drawer.")
13859 (defsubst org-re-property (property)
13860 "Return a regexp matching PROPERTY.
13861 Match group 1 will be set to the value "
13862 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
13864 (defun org-property-action ()
13865 "Do an action on properties."
13868 (org-at-property-p)
13869 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
13870 (setq c (read-char-exclusive))
13873 (call-interactively 'org-set-property))
13875 (call-interactively 'org-delete-property))
13877 (call-interactively 'org-delete-property-globally))
13879 (call-interactively 'org-compute-property-at-point))
13880 (t (error "No such property action %c" c)))))
13882 (defun org-set-effort (&optional value)
13883 "Set the effort property of the current entry.
13884 With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
13887 (if (equal value 0) (setq value 10))
13888 (let* ((completion-ignore-case t)
13889 (prop org-effort-property)
13890 (cur (org-entry-get nil prop))
13891 (allowed (org-property-get-allowed-values nil prop 'table))
13892 (existing (mapcar 'list (org-property-values prop)))
13895 ((stringp value) value)
13896 ((and allowed (integerp value))
13897 (or (car (nth (1- value) allowed))
13898 (car (org-last allowed))))
13900 (message "Select 1-9,0, [RET%s]: %s"
13901 (if cur (concat "=" cur) "")
13902 (mapconcat 'car allowed " "))
13903 (setq rpl (read-char-exclusive))
13904 (if (equal rpl ?\r)
13906 (setq rpl (- rpl ?0))
13907 (if (equal rpl 0) (setq rpl 10))
13908 (if (and (> rpl 0) (<= rpl (length allowed)))
13909 (car (nth (1- rpl) allowed))
13910 (org-completing-read "Effort: " allowed nil))))
13912 (let (org-completion-use-ido org-completion-use-iswitchb)
13913 (org-completing-read
13914 (concat "Effort " (if (and cur (string-match "\\S-" cur))
13915 (concat "[" cur "]") "")
13917 existing nil nil "" nil cur))))))
13918 (unless (equal (org-entry-get nil prop) val)
13919 (org-entry-put nil prop val))
13920 (message "%s is now %s" prop val)))
13922 (defun org-at-property-p ()
13923 "Is cursor inside a property drawer?"
13925 (beginning-of-line 1)
13926 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
13927 (save-match-data ;; Used by calling procedures
13929 (range (unless (org-before-first-heading-p)
13930 (org-get-property-block))))
13931 (and range (<= (car range) p) (< p (cdr range))))))))
13933 (defun org-get-property-block (&optional beg end force)
13934 "Return the (beg . end) range of the body of the property drawer.
13935 BEG and END can be beginning and end of subtree, if not given
13936 they will be found.
13937 If the drawer does not exist and FORCE is non-nil, create the drawer."
13940 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
13941 (end (or end (progn (outline-next-heading) (point)))))
13943 (if (re-search-forward org-property-start-re end t)
13944 (setq beg (1+ (match-end 0)))
13947 (org-insert-property-drawer)
13948 (setq end (progn (outline-next-heading) (point))))
13951 (if (re-search-forward org-property-start-re end t)
13952 (setq beg (1+ (match-end 0)))))
13953 (if (re-search-forward org-property-end-re end t)
13954 (setq end (match-beginning 0))
13955 (or force (throw 'exit nil))
13958 (org-indent-line-function)
13959 (insert ":END:\n"))
13962 (defun org-entry-properties (&optional pom which specific)
13963 "Get all properties of the entry at point-or-marker POM.
13964 This includes the TODO keyword, the tags, time strings for deadline,
13965 scheduled, and clocking, and any additional properties defined in the
13966 entry. The return value is an alist, keys may occur multiple times
13967 if the property key was used several times.
13968 POM may also be nil, in which case the current entry is used.
13969 If WHICH is nil or `all', get all properties. If WHICH is
13970 `special' or `standard', only get that subclass. If WHICH
13971 is a string only get exactly this property. SPECIFIC can be a string, the
13972 specific property we are interested in. Specifying it can speed
13973 things up because then unnecessary parsing is avoided."
13974 (setq which (or which 'all))
13975 (org-with-point-at pom
13976 (let ((clockstr (substring org-clock-string 0 -1))
13977 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
13978 (case-fold-search nil)
13979 beg end range props sum-props key key1 value string clocksum)
13981 (when (condition-case nil
13982 (and (eq major-mode 'org-mode) (org-back-to-heading t))
13985 (setq sum-props (get-text-property (point) 'org-summaries))
13986 (setq clocksum (get-text-property (point) :org-clock-minutes))
13987 (outline-next-heading)
13989 (when (memq which '(all special))
13990 ;; Get the special properties, like TODO and tags
13992 (when (and (or (not specific) (string= specific "TODO"))
13993 (looking-at org-todo-line-regexp) (match-end 2))
13994 (push (cons "TODO" (org-match-string-no-properties 2)) props))
13995 (when (and (or (not specific) (string= specific "PRIORITY"))
13996 (looking-at org-priority-regexp))
13997 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
13998 (when (or (not specific) (string= specific "FILE"))
13999 (push (cons "FILE" buffer-file-name) props))
14000 (when (and (or (not specific) (string= specific "TAGS"))
14001 (setq value (org-get-tags-string))
14002 (string-match "\\S-" value))
14003 (push (cons "TAGS" value) props))
14004 (when (and (or (not specific) (string= specific "ALLTAGS"))
14005 (setq value (org-get-tags-at)))
14006 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14009 (when (or (not specific) (string= specific "BLOCKED"))
14010 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14011 (when (or (not specific)
14013 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14014 "TIMESTAMP" "TIMESTAMP_IA")))
14016 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14017 (setq key (if (match-end 1)
14018 (substring (org-match-string-no-properties 1)
14020 string (if (equal key clockstr)
14024 (match-beginning 3) (goto-char
14026 (substring (org-match-string-no-properties 3)
14028 ;; Get the correct property name from the key. This is
14029 ;; necessary if the user has configured time keywords.
14030 (setq key1 (concat key ":"))
14034 (if (= (char-after (match-beginning 3)) ?\[)
14035 "TIMESTAMP_IA" "TIMESTAMP")))
14036 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14037 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14038 ((equal key1 org-closed-string) (setq key "CLOSED"))
14039 ((equal key1 org-clock-string) (setq key "CLOCK")))
14040 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14042 (push (cons key string) props)
14043 ;; no need to search further if match is found
14045 (when (or (equal key "CLOCK") (not (assoc key props)))
14046 (push (cons key string) props))))))
14049 (when (memq which '(all standard))
14050 ;; Get the standard properties, like :PROP: ...
14051 (setq range (org-get-property-block beg end))
14053 (goto-char (car range))
14054 (while (re-search-forward
14055 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14057 (setq key (org-match-string-no-properties 1)
14058 value (org-trim (or (org-match-string-no-properties 2) "")))
14059 (unless (member key excluded)
14060 (push (cons key (or value "")) props)))))
14062 (push (cons "CLOCKSUM"
14063 (org-columns-number-to-string (/ (float clocksum) 60.)
14066 (unless (assoc "CATEGORY" props)
14067 (push (cons "CATEGORY" (org-get-category)) props))
14068 (append sum-props (nreverse props)))))))
14070 (defun org-entry-get (pom property &optional inherit literal-nil)
14071 "Get value of PROPERTY for entry at point-or-marker POM.
14072 If INHERIT is non-nil and the entry does not have the property,
14073 then also check higher levels of the hierarchy.
14074 If INHERIT is the symbol `selective', use inheritance only if the setting
14075 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14076 If the property is present but empty, the return value is the empty string.
14077 If the property is not present at all, nil is returned.
14079 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14080 do not interpret it as the list atom nil. This is used for inheritance
14081 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14082 (org-with-point-at pom
14083 (if (and inherit (if (eq inherit 'selective)
14084 (org-property-inherit-p property)
14086 (org-entry-get-with-inheritance property literal-nil)
14087 (if (member property org-special-properties)
14088 ;; We need a special property. Use `org-entry-properties' to
14089 ;; retrieve it, but specify the wanted property
14090 (cdr (assoc property (org-entry-properties nil 'special property)))
14091 (let ((range (unless (org-before-first-heading-p)
14092 (org-get-property-block))))
14093 (when (and range (goto-char (car range)))
14094 ((lambda (val) (when val (if literal-nil val (org-not-nil val))))
14096 ((re-search-forward
14097 (org-re-property property) (cdr range) t)
14098 (if (match-end 1) (org-match-string-no-properties 1) ""))
14099 ((re-search-forward
14100 (org-re-property (concat property "+")) (cdr range) t)
14103 (org-update-property-plist
14104 (concat property "+")
14105 (if (match-end 1) (org-match-string-no-properties 1) "")
14106 (list (or (assoc property org-file-properties)
14107 (assoc property org-global-properties)
14108 (assoc property org-global-properties-fixed)
14111 (defun org-property-or-variable-value (var &optional inherit)
14112 "Check if there is a property fixing the value of VAR.
14113 If yes, return this value. If not, return the current value of the variable."
14114 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14115 (if (and prop (stringp prop) (string-match "\\S-" prop))
14117 (symbol-value var))))
14119 (defun org-entry-delete (pom property)
14120 "Delete the property PROPERTY from entry at point-or-marker POM."
14121 (org-with-point-at pom
14122 (if (member property org-special-properties)
14123 nil ; cannot delete these properties.
14124 (let ((range (org-get-property-block)))
14126 (goto-char (car range))
14128 (org-re-property property)
14131 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14135 ;; Multi-values properties are properties that contain multiple values
14136 ;; These values are assumed to be single words, separated by whitespace.
14137 (defun org-entry-add-to-multivalued-property (pom property value)
14138 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14139 (let* ((old (org-entry-get pom property))
14140 (values (and old (org-split-string old "[ \t]"))))
14141 (setq value (org-entry-protect-space value))
14142 (unless (member value values)
14143 (setq values (cons value values))
14144 (org-entry-put pom property
14145 (mapconcat 'identity values " ")))))
14147 (defun org-entry-remove-from-multivalued-property (pom property value)
14148 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14149 (let* ((old (org-entry-get pom property))
14150 (values (and old (org-split-string old "[ \t]"))))
14151 (setq value (org-entry-protect-space value))
14152 (when (member value values)
14153 (setq values (delete value values))
14154 (org-entry-put pom property
14155 (mapconcat 'identity values " ")))))
14157 (defun org-entry-member-in-multivalued-property (pom property value)
14158 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14159 (let* ((old (org-entry-get pom property))
14160 (values (and old (org-split-string old "[ \t]"))))
14161 (setq value (org-entry-protect-space value))
14162 (member value values)))
14164 (defun org-entry-get-multivalued-property (pom property)
14165 "Return a list of values in a multivalued property."
14166 (let* ((value (org-entry-get pom property))
14167 (values (and value (org-split-string value "[ \t]"))))
14168 (mapcar 'org-entry-restore-space values)))
14170 (defun org-entry-put-multivalued-property (pom property &rest values)
14171 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14172 VALUES should be a list of strings. Spaces will be protected."
14173 (org-entry-put pom property
14174 (mapconcat 'org-entry-protect-space values " "))
14175 (let* ((value (org-entry-get pom property))
14176 (values (and value (org-split-string value "[ \t]"))))
14177 (mapcar 'org-entry-restore-space values)))
14179 (defun org-entry-protect-space (s)
14180 "Protect spaces and newline in string S."
14181 (while (string-match " " s)
14182 (setq s (replace-match "%20" t t s)))
14183 (while (string-match "\n" s)
14184 (setq s (replace-match "%0A" t t s)))
14187 (defun org-entry-restore-space (s)
14188 "Restore spaces and newline in string S."
14189 (while (string-match "%20" s)
14190 (setq s (replace-match " " t t s)))
14191 (while (string-match "%0A" s)
14192 (setq s (replace-match "\n" t t s)))
14195 (defvar org-entry-property-inherited-from (make-marker)
14196 "Marker pointing to the entry from where a property was inherited.
14197 Each call to `org-entry-get-with-inheritance' will set this marker to the
14198 location of the entry where the inheritance search matched. If there was
14199 no match, the marker will point nowhere.
14200 Note that also `org-entry-get' calls this function, if the INHERIT flag
14203 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14204 "Get entry property, and search higher levels if not present.
14205 The search will stop at the first ancestor which has the property defined.
14206 If the value found is \"nil\", return nil to show that the property
14207 should be considered as undefined (this is the meaning of nil here).
14208 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14209 (move-marker org-entry-property-inherited-from nil)
14211 (unless (org-before-first-heading-p)
14217 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14218 (org-back-to-heading t)
14219 (move-marker org-entry-property-inherited-from (point))
14221 (or (org-up-heading-safe) (throw 'ex nil)))))))
14223 (cdr (assoc property org-file-properties))
14224 (cdr (assoc property org-global-properties))
14225 (cdr (assoc property org-global-properties-fixed))))
14226 (if literal-nil tmp (org-not-nil tmp))))
14228 (defvar org-property-changed-functions nil
14229 "Hook called when the value of a property has changed.
14230 Each hook function should accept two arguments, the name of the property
14231 and the new value.")
14233 (defun org-entry-put (pom property value)
14234 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14235 (org-with-point-at pom
14236 (org-back-to-heading t)
14237 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14240 ((equal property "TODO")
14241 (when (and (stringp value) (string-match "\\S-" value)
14242 (not (member value org-todo-keywords-1)))
14243 (error "\"%s\" is not a valid TODO state" value))
14244 (if (or (not value)
14245 (not (string-match "\\S-" value)))
14246 (setq value 'none))
14248 (org-set-tags nil 'align))
14249 ((equal property "PRIORITY")
14250 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14251 (string-to-char value) ?\ ))
14252 (org-set-tags nil 'align))
14253 ((equal property "SCHEDULED")
14254 (if (re-search-forward org-scheduled-time-regexp end t)
14256 ((eq value 'earlier) (org-timestamp-change -1 'day))
14257 ((eq value 'later) (org-timestamp-change 1 'day))
14258 (t (call-interactively 'org-schedule)))
14259 (call-interactively 'org-schedule)))
14260 ((equal property "DEADLINE")
14261 (if (re-search-forward org-deadline-time-regexp end t)
14263 ((eq value 'earlier) (org-timestamp-change -1 'day))
14264 ((eq value 'later) (org-timestamp-change 1 'day))
14265 (t (call-interactively 'org-deadline)))
14266 (call-interactively 'org-deadline)))
14267 ((member property org-special-properties)
14268 (error "The %s property can not yet be set with `org-entry-put'"
14270 (t ; a non-special property
14271 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14272 (setq range (org-get-property-block beg end 'force))
14273 (goto-char (car range))
14274 (if (re-search-forward
14275 (org-re-property property) (cdr range) t)
14277 (delete-region (match-beginning 0) (match-end 0))
14278 (goto-char (match-beginning 0)))
14279 (goto-char (cdr range))
14282 (org-indent-line-function))
14283 (insert ":" property ":")
14284 (and value (insert " " value))
14285 (org-indent-line-function)))))
14286 (run-hook-with-args 'org-property-changed-functions property value)))
14288 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14289 "Get all property keys in the current buffer.
14290 With INCLUDE-SPECIALS, also list the special properties that reflect things
14291 like tags and TODO state.
14292 With INCLUDE-DEFAULTS, also include properties that has special meaning
14293 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14295 With INCLUDE-COLUMNS, also include property names given in COLUMN
14296 formats in the current buffer."
14297 (let (rtn range cfmt s p)
14301 (goto-char (point-min))
14302 (while (re-search-forward org-property-start-re nil t)
14303 (setq range (org-get-property-block))
14304 (goto-char (car range))
14305 (while (re-search-forward
14306 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14308 (add-to-list 'rtn (org-match-string-no-properties 1)))
14309 (outline-next-heading))))
14311 (when include-specials
14312 (setq rtn (append org-special-properties rtn)))
14314 (when include-defaults
14315 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14316 (add-to-list 'rtn org-effort-property))
14318 (when include-columns
14322 (goto-char (point-min))
14323 (while (re-search-forward
14324 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14326 (setq cfmt (match-string 2) s 0)
14327 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14329 (setq s (match-end 0)
14330 p (match-string 1 cfmt))
14331 (unless (or (equal p "ITEM")
14332 (member p org-special-properties))
14333 (add-to-list 'rtn (match-string 1 cfmt))))))))
14335 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14337 (defun org-property-values (key)
14338 "Return a list of all values of property KEY in the current buffer."
14342 (goto-char (point-min))
14343 (let ((re (org-re-property key))
14345 (while (re-search-forward re nil t)
14346 (add-to-list 'values (org-trim (match-string 1))))
14347 (delete "" values)))))
14349 (defun org-insert-property-drawer ()
14350 "Insert a property drawer into the current entry."
14352 (org-back-to-heading t)
14353 (looking-at org-outline-regexp)
14354 (let ((indent (if org-adapt-indentation
14355 (- (match-end 0)(match-beginning 0))
14358 (re (concat "^[ \t]*" org-keyword-time-regexp))
14360 (outline-next-heading)
14363 (while (re-search-forward re end t))
14364 (setq hiddenp (outline-invisible-p))
14366 (and (equal (char-after) ?\n) (forward-char 1))
14367 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14368 (if (member (match-string 1) '("CLOCK:" ":END:"))
14369 ;; just skip this line
14370 (beginning-of-line 2)
14371 ;; Drawer start, find the end
14372 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14373 (beginning-of-line 1)))
14374 (org-skip-over-state-notes)
14375 (skip-chars-backward " \t\n\r")
14376 (if (eq (char-before) ?*) (forward-char 1))
14377 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14378 (beginning-of-line 0)
14379 (org-indent-to-column indent)
14380 (beginning-of-line 2)
14381 (org-indent-to-column indent)
14382 (beginning-of-line 0)
14385 (org-back-to-heading t)
14387 (org-flag-drawer t))))
14389 (defvar org-property-set-functions-alist nil
14390 "Property set function alist.
14391 Each entry should have the following format:
14393 (PROPERTY . READ-FUNCTION)
14395 The read function will be called with the same argument as
14396 `org-completing-read'.")
14398 (defun org-set-property-function (property)
14399 "Get the function that should be used to set PROPERTY.
14400 This is computed according to `org-property-set-functions-alist'."
14401 (or (cdr (assoc property org-property-set-functions-alist))
14402 'org-completing-read))
14404 (defun org-read-property-value (property)
14405 "Read PROPERTY value from user."
14406 (let* ((completion-ignore-case t)
14407 (allowed (org-property-get-allowed-values nil property 'table))
14408 (cur (org-entry-get nil property))
14409 (prompt (concat property " value"
14410 (if (and cur (string-match "\\S-" cur))
14411 (concat " [" cur "]") "") ": "))
14412 (set-function (org-set-property-function property))
14414 (funcall set-function prompt allowed nil
14415 (not (get-text-property 0 'org-unrestricted
14417 (let (org-completion-use-ido org-completion-use-iswitchb)
14418 (funcall set-function prompt
14419 (mapcar 'list (org-property-values property))
14420 nil nil "" nil cur)))))
14425 (defvar org-last-set-property nil)
14426 (defun org-read-property-name ()
14427 "Read a property name."
14428 (let* ((completion-ignore-case t)
14429 (keys (org-buffer-property-keys nil t t))
14430 (default-prop (or (save-excursion
14432 (beginning-of-line)
14433 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14434 (null (string= (match-string 1) "END"))
14435 (match-string 1))))
14436 org-last-set-property))
14437 (property (org-icompleting-read
14439 (if default-prop (concat " [" default-prop "]") "")
14441 (mapcar 'list keys)
14445 (if (member property keys)
14447 (or (cdr (assoc (downcase property)
14448 (mapcar (lambda (x) (cons (downcase x) x))
14452 (defun org-set-property (property value)
14453 "In the current entry, set PROPERTY to VALUE.
14454 When called interactively, this will prompt for a property name, offering
14455 completion on existing and default properties. And then it will prompt
14456 for a value, offering completion either on allowed values (via an inherited
14457 xxx_ALL property) or on existing values in other instances of this property
14458 in the current file."
14459 (interactive (list nil nil))
14460 (let* ((property (or property (org-read-property-name)))
14461 (value (or value (org-read-property-value property)))
14462 (fn (assoc property org-properties-postprocess-alist)))
14463 (setq org-last-set-property property)
14464 ;; Possibly postprocess the inserted value:
14465 (when fn (setq value (funcall (cadr fn) value)))
14466 (unless (equal (org-entry-get nil property) value)
14467 (org-entry-put nil property value))))
14469 (defun org-delete-property (property)
14470 "In the current entry, delete PROPERTY."
14472 (let* ((completion-ignore-case t)
14473 (prop (org-icompleting-read "Property: "
14474 (org-entry-properties nil 'standard))))
14476 (message "Property %s %s" property
14477 (if (org-entry-delete nil property)
14479 "was not present in the entry")))
14481 (defun org-delete-property-globally (property)
14482 "Remove PROPERTY globally, from all entries."
14484 (let* ((completion-ignore-case t)
14485 (prop (org-icompleting-read
14486 "Globally remove property: "
14487 (mapcar 'list (org-buffer-property-keys)))))
14492 (goto-char (point-min))
14494 (while (re-search-forward
14495 (org-re-property property)
14497 (setq cnt (1+ cnt))
14498 (replace-match ""))
14499 (message "Property \"%s\" removed from %d entries" property cnt)))))
14501 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14503 (defun org-compute-property-at-point ()
14504 "Compute the property at point.
14505 This looks for an enclosing column format, extracts the operator and
14506 then applies it to the property in the column format's scope."
14508 (unless (org-at-property-p)
14509 (error "Not at a property"))
14510 (let ((prop (org-match-string-no-properties 2)))
14511 (org-columns-get-format-and-top-level)
14512 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14513 (error "No operator defined for property %s" prop))
14514 (org-columns-compute prop)))
14516 (defvar org-property-allowed-value-functions nil
14517 "Hook for functions supplying allowed values for a specific property.
14518 The functions must take a single argument, the name of the property, and
14519 return a flat list of allowed values. If \":ETC\" is one of
14520 the values, this means that these values are intended as defaults for
14521 completion, but that other values should be allowed too.
14522 The functions must return nil if they are not responsible for this
14525 (defun org-property-get-allowed-values (pom property &optional table)
14526 "Get allowed values for the property PROPERTY.
14527 When TABLE is non-nil, return an alist that can directly be used for
14531 ((equal property "TODO")
14532 (setq vals (org-with-point-at pom
14533 (append org-todo-keywords-1 '("")))))
14534 ((equal property "PRIORITY")
14535 (let ((n org-lowest-priority))
14536 (while (>= n org-highest-priority)
14537 (push (char-to-string n) vals)
14539 ((member property org-special-properties))
14540 ((setq vals (run-hook-with-args-until-success
14541 'org-property-allowed-value-functions property)))
14543 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
14544 (when (and vals (string-match "\\S-" vals))
14545 (setq vals (car (read-from-string (concat "(" vals ")"))))
14546 (setq vals (mapcar (lambda (x)
14547 (cond ((stringp x) x)
14548 ((numberp x) (number-to-string x))
14549 ((symbolp x) (symbol-name x))
14552 (when (member ":ETC" vals)
14553 (setq vals (remove ":ETC" vals))
14554 (org-add-props (car vals) '(org-unrestricted t)))
14555 (if table (mapcar 'list vals) vals)))
14557 (defun org-property-previous-allowed-value (&optional previous)
14558 "Switch to the next allowed value for this property."
14560 (org-property-next-allowed-value t))
14562 (defun org-property-next-allowed-value (&optional previous)
14563 "Switch to the next allowed value for this property."
14565 (unless (org-at-property-p)
14566 (error "Not at a property"))
14567 (let* ((key (match-string 2))
14568 (value (match-string 3))
14569 (allowed (or (org-property-get-allowed-values (point) key)
14570 (and (member value '("[ ]" "[-]" "[X]"))
14574 (error "Allowed values for this property have not been defined"))
14575 (if previous (setq allowed (reverse allowed)))
14576 (if (member value allowed)
14577 (setq nval (car (cdr (member value allowed)))))
14578 (setq nval (or nval (car allowed)))
14579 (if (equal nval value)
14580 (error "Only one allowed value for this property"))
14581 (org-at-property-p)
14582 (replace-match (concat " :" key ": " nval) t t)
14583 (org-indent-line-function)
14584 (beginning-of-line 1)
14585 (skip-chars-forward " \t")
14586 (run-hook-with-args 'org-property-changed-functions key nval)))
14588 (defun org-find-olp (path &optional this-buffer)
14589 "Return a marker pointing to the entry at outline path OLP.
14590 If anything goes wrong, throw an error.
14591 You can wrap this call to catch the error like this:
14593 (condition-case msg
14594 (org-mobile-locate-entry (match-string 4))
14595 (error (nth 1 msg)))
14597 The return value will then be either a string with the error message,
14598 or a marker if everything is OK.
14600 If THIS-BUFFER is set, the outline path does not contain a file,
14602 (let* ((file (if this-buffer buffer-file-name (pop path)))
14603 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
14607 limit re end found pos heading cnt flevel)
14608 (unless buffer (error "File not found :%s" file))
14609 (with-current-buffer buffer
14613 (setq limit (point-max))
14614 (goto-char (point-min))
14615 (while (setq heading (pop path))
14616 (setq re (format org-complex-heading-regexp-format
14617 (regexp-quote heading)))
14618 (setq cnt 0 pos (point))
14619 (while (re-search-forward re end t)
14620 (setq level (- (match-end 1) (match-beginning 1)))
14621 (if (and (>= level lmin) (<= level lmax))
14622 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
14623 (when (= cnt 0) (error "Heading not found on level %d: %s"
14625 (when (> cnt 1) (error "Heading not unique on level %d: %s"
14628 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
14629 (setq end (save-excursion (org-end-of-subtree t t))))
14630 (when (org-on-heading-p)
14631 (move-marker (make-marker) (point))))))))
14633 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
14634 "Find node HEADING in BUFFER.
14635 Return a marker to the heading if it was found, or nil if not.
14636 If POS-ONLY is set, return just the position instead of a marker.
14638 The heading text must match exact, but it may have a TODO keyword,
14639 a priority cookie and tags in the standard locations."
14640 (with-current-buffer (or buffer (current-buffer))
14644 (goto-char (point-min))
14645 (let (case-fold-search)
14646 (if (re-search-forward
14647 (format org-complex-heading-regexp-format
14648 (regexp-quote heading)) nil t)
14650 (match-beginning 0)
14651 (move-marker (make-marker) (match-beginning 0)))))))))
14653 (defun org-find-exact-heading-in-directory (heading &optional dir)
14654 "Find Org node headline HEADING in all .org files in directory DIR.
14655 When the target headline is found, return a marker to this location."
14656 (let ((files (directory-files (or dir default-directory)
14657 nil "\\`[^.#].*\\.org\\'"))
14658 file visiting m buffer)
14660 (while (setq file (pop files))
14661 (message "trying %s" file)
14662 (setq visiting (org-find-base-buffer-visiting file))
14663 (setq buffer (or visiting (find-file-noselect file)))
14664 (setq m (org-find-exact-headline-in-buffer
14666 (when (and (not m) (not visiting)) (kill-buffer buffer))
14667 (and m (throw 'found m))))))
14669 (defun org-find-entry-with-id (ident)
14670 "Locate the entry that contains the ID property with exact value IDENT.
14671 IDENT can be a string, a symbol or a number, this function will search for
14672 the string representation of it.
14673 Return the position where this entry starts, or nil if there is no such entry."
14674 (interactive "sID: ")
14676 ((stringp ident) ident)
14677 ((symbol-name ident) (symbol-name ident))
14678 ((numberp ident) (number-to-string ident))
14679 (t (error "IDENT %s must be a string, symbol or number" ident))))
14680 (case-fold-search nil))
14684 (goto-char (point-min))
14685 (when (re-search-forward
14686 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
14688 (org-back-to-heading t)
14693 (defvar org-last-changed-timestamp nil)
14694 (defvar org-last-inserted-timestamp nil
14695 "The last time stamp inserted with `org-insert-time-stamp'.")
14696 (defvar org-time-was-given) ; dynamically scoped parameter
14697 (defvar org-end-time-was-given) ; dynamically scoped parameter
14698 (defvar org-ts-what) ; dynamically scoped parameter
14700 (defun org-time-stamp (arg &optional inactive)
14701 "Prompt for a date/time and insert a time stamp.
14702 If the user specifies a time like HH:MM, or if this command is called
14703 with a prefix argument, the time stamp will contain date and time.
14704 Otherwise, only the date will be included. All parts of a date not
14705 specified by the user will be filled in from the current date/time.
14706 So if you press just return without typing anything, the time stamp
14707 will represent the current date/time. If there is already a timestamp
14708 at the cursor, it will be modified."
14712 ;; Default time is either today, or, when entering a range,
14713 ;; the range start.
14714 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
14716 (re-search-backward
14717 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
14718 (- (point) 20) t)))
14719 (apply 'encode-time (org-parse-time-string (match-string 1)))
14721 (default-input (and ts (org-get-compact-tod ts)))
14722 (repeater (save-excursion
14724 (beginning-of-line)
14725 (when (re-search-forward
14726 "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
14727 (save-excursion (progn (end-of-line) (point))) t)
14728 (match-string 0)))))
14729 org-time-was-given org-end-time-was-given time)
14731 ((and (org-at-timestamp-p t)
14732 (memq last-command '(org-time-stamp org-time-stamp-inactive))
14733 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
14735 (setq time (let ((this-command this-command))
14736 (org-read-date arg 'totime nil nil
14737 default-time default-input)))
14738 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
14739 ((org-at-timestamp-p t)
14740 (setq time (let ((this-command this-command))
14741 (org-read-date arg 'totime nil nil default-time default-input)))
14742 (when (org-at-timestamp-p t) ; just to get the match data
14743 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
14745 (setq org-last-changed-timestamp
14746 (org-insert-time-stamp
14747 time (or org-time-was-given arg)
14748 inactive nil nil (list org-end-time-was-given)))
14749 (when repeater (goto-char (1- (point))) (insert " " repeater)
14750 (setq org-last-changed-timestamp
14751 (concat (substring org-last-inserted-timestamp 0 -1)
14752 " " repeater ">"))))
14753 (message "Timestamp updated"))
14755 (setq time (let ((this-command this-command))
14756 (org-read-date arg 'totime nil nil default-time default-input)))
14757 (org-insert-time-stamp time (or org-time-was-given arg) inactive
14758 nil nil (list org-end-time-was-given))))))
14760 ;; FIXME: can we use this for something else, like computing time differences?
14761 (defun org-get-compact-tod (s)
14762 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
14763 (let* ((t1 (match-string 1 s))
14764 (h1 (string-to-number (match-string 2 s)))
14765 (m1 (string-to-number (match-string 3 s)))
14766 (t2 (and (match-end 4) (match-string 5 s)))
14767 (h2 (and t2 (string-to-number (match-string 6 s))))
14768 (m2 (and t2 (string-to-number (match-string 7 s))))
14772 (setq dh (- h2 h1) dm (- m2 m1))
14773 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
14774 (concat t1 "+" (number-to-string dh)
14775 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
14777 (defun org-time-stamp-inactive (&optional arg)
14778 "Insert an inactive time stamp.
14779 An inactive time stamp is enclosed in square brackets instead of angle
14780 brackets. It is inactive in the sense that it does not trigger agenda entries,
14781 does not link to the calendar and cannot be changed with the S-cursor keys.
14782 So these are more for recording a certain time/date."
14784 (org-time-stamp arg 'inactive))
14786 (defvar org-date-ovl (make-overlay 1 1))
14787 (overlay-put org-date-ovl 'face 'org-warning)
14788 (org-detach-overlay org-date-ovl)
14790 (defvar org-ans1) ; dynamically scoped parameter
14791 (defvar org-ans2) ; dynamically scoped parameter
14793 (defvar org-plain-time-of-day-regexp) ; defined below
14795 (defvar org-overriding-default-time nil) ; dynamically scoped
14796 (defvar org-read-date-overlay nil)
14797 (defvar org-dcst nil) ; dynamically scoped
14798 (defvar org-read-date-history nil)
14799 (defvar org-read-date-final-answer nil)
14800 (defvar org-read-date-analyze-futurep nil)
14801 (defvar org-read-date-analyze-forced-year nil)
14803 (defun org-read-date (&optional with-time to-time from-string prompt
14804 default-time default-input)
14805 "Read a date, possibly a time, and make things smooth for the user.
14806 The prompt will suggest to enter an ISO date, but you can also enter anything
14807 which will at least partially be understood by `parse-time-string'.
14808 Unrecognized parts of the date will default to the current day, month, year,
14809 hour and minute. If this command is called to replace a timestamp at point,
14810 or to enter the second timestamp of a range, the default time is taken
14811 from the existing stamp. Furthermore, the command prefers the future,
14812 so if you are giving a date where the year is not given, and the day-month
14813 combination is already past in the current year, it will assume you
14814 mean next year. For details, see the manual. A few examples:
14816 3-2-5 --> 2003-02-05
14817 feb 15 --> currentyear-02-15
14818 2/15 --> currentyear-02-15
14819 sep 12 9 --> 2009-09-12
14820 12:45 --> today 12:45
14821 22 sept 0:34 --> currentyear-09-22 0:34
14822 12 --> currentyear-currentmonth-12
14823 Fri --> nearest Friday (today or later)
14826 Furthermore you can specify a relative date by giving, as the *first* thing
14827 in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
14828 change in days weeks, months, years.
14829 With a single plus or minus, the date is relative to today. With a double
14830 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
14831 +4d --> four days from today
14832 +4 --> same as above
14833 +2w --> two weeks from today
14834 ++5 --> five days from default date
14836 The function understands only English month and weekday abbreviations,
14837 but this can be configured with the variables `parse-time-months' and
14838 `parse-time-weekdays'.
14840 While prompting, a calendar is popped up - you can also select the
14841 date with the mouse (button 1). The calendar shows a period of three
14842 months. To scroll it to other months, use the keys `>' and `<'.
14843 If you don't like the calendar, turn it off with
14844 \(setq org-read-date-popup-calendar nil)
14846 With optional argument TO-TIME, the date will immediately be converted
14847 to an internal time.
14848 With an optional argument WITH-TIME, the prompt will suggest to also
14849 insert a time. Note that when WITH-TIME is not set, you can still
14850 enter a time, and this function will inform the calling routine about
14851 this change. The calling routine may then choose to change the format
14852 used to insert the time stamp into the buffer to include the time.
14853 With optional argument FROM-STRING, read from this string instead from
14854 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
14855 the time/date that is used for everything that is not specified by the
14857 (require 'parse-time)
14858 (let* ((org-time-stamp-rounding-minutes
14859 (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
14860 (org-dcst org-display-custom-times)
14861 (ct (org-current-time))
14862 (def (or org-overriding-default-time default-time ct))
14863 (defdecode (decode-time def))
14865 (when (< (nth 2 defdecode) org-extend-today-until)
14866 (setcar (nthcdr 2 defdecode) -1)
14867 (setcar (nthcdr 1 defdecode) 59)
14868 (setq def (apply 'encode-time defdecode)
14869 defdecode (decode-time def)))))
14870 (calendar-frame-setup nil)
14871 (calendar-setup nil)
14872 (calendar-move-hook nil)
14873 (calendar-view-diary-initially-flag nil)
14874 (calendar-view-holidays-initially-flag nil)
14875 (timestr (format-time-string
14876 (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
14877 (prompt (concat (if prompt (concat prompt " ") "")
14878 (format "Date+time [%s]: " timestr)))
14879 ans (org-ans0 "") org-ans1 org-ans2 final)
14882 (from-string (setq ans from-string))
14883 (org-read-date-popup-calendar
14885 (save-window-excursion
14889 (calendar-forward-day (- (time-to-days def)
14890 (calendar-absolute-from-gregorian
14891 (calendar-current-date))))
14892 (org-eval-in-calendar nil t)
14893 (let* ((old-map (current-local-map))
14894 (map (copy-keymap calendar-mode-map))
14895 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
14896 (org-defkey map (kbd "RET") 'org-calendar-select)
14897 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
14898 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
14899 (org-defkey minibuffer-local-map [(meta shift left)]
14900 (lambda () (interactive)
14901 (org-eval-in-calendar '(calendar-backward-month 1))))
14902 (org-defkey minibuffer-local-map [(meta shift right)]
14903 (lambda () (interactive)
14904 (org-eval-in-calendar '(calendar-forward-month 1))))
14905 (org-defkey minibuffer-local-map [(meta shift up)]
14906 (lambda () (interactive)
14907 (org-eval-in-calendar '(calendar-backward-year 1))))
14908 (org-defkey minibuffer-local-map [(meta shift down)]
14909 (lambda () (interactive)
14910 (org-eval-in-calendar '(calendar-forward-year 1))))
14911 (org-defkey minibuffer-local-map [?\e (shift left)]
14912 (lambda () (interactive)
14913 (org-eval-in-calendar '(calendar-backward-month 1))))
14914 (org-defkey minibuffer-local-map [?\e (shift right)]
14915 (lambda () (interactive)
14916 (org-eval-in-calendar '(calendar-forward-month 1))))
14917 (org-defkey minibuffer-local-map [?\e (shift up)]
14918 (lambda () (interactive)
14919 (org-eval-in-calendar '(calendar-backward-year 1))))
14920 (org-defkey minibuffer-local-map [?\e (shift down)]
14921 (lambda () (interactive)
14922 (org-eval-in-calendar '(calendar-forward-year 1))))
14923 (org-defkey minibuffer-local-map [(shift up)]
14924 (lambda () (interactive)
14925 (org-eval-in-calendar '(calendar-backward-week 1))))
14926 (org-defkey minibuffer-local-map [(shift down)]
14927 (lambda () (interactive)
14928 (org-eval-in-calendar '(calendar-forward-week 1))))
14929 (org-defkey minibuffer-local-map [(shift left)]
14930 (lambda () (interactive)
14931 (org-eval-in-calendar '(calendar-backward-day 1))))
14932 (org-defkey minibuffer-local-map [(shift right)]
14933 (lambda () (interactive)
14934 (org-eval-in-calendar '(calendar-forward-day 1))))
14935 (org-defkey minibuffer-local-map ">"
14936 (lambda () (interactive)
14937 (org-eval-in-calendar '(scroll-calendar-left 1))))
14938 (org-defkey minibuffer-local-map "<"
14939 (lambda () (interactive)
14940 (org-eval-in-calendar '(scroll-calendar-right 1))))
14941 (org-defkey minibuffer-local-map "\C-v"
14942 (lambda () (interactive)
14943 (org-eval-in-calendar
14944 '(calendar-scroll-left-three-months 1))))
14945 (org-defkey minibuffer-local-map "\M-v"
14946 (lambda () (interactive)
14947 (org-eval-in-calendar
14948 '(calendar-scroll-right-three-months 1))))
14949 (run-hooks 'org-read-date-minibuffer-setup-hook)
14952 (use-local-map map)
14953 (add-hook 'post-command-hook 'org-read-date-display)
14954 (setq org-ans0 (read-string prompt default-input
14955 'org-read-date-history nil))
14956 ;; org-ans0: from prompt
14957 ;; org-ans1: from mouse click
14958 ;; org-ans2: from calendar motion
14959 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
14960 (remove-hook 'post-command-hook 'org-read-date-display)
14961 (use-local-map old-map)
14962 (when org-read-date-overlay
14963 (delete-overlay org-read-date-overlay)
14964 (setq org-read-date-overlay nil)))))
14965 (bury-buffer "*Calendar*")))))
14967 (t ; Naked prompt only
14969 (setq ans (read-string prompt default-input
14970 'org-read-date-history timestr))
14971 (when org-read-date-overlay
14972 (delete-overlay org-read-date-overlay)
14973 (setq org-read-date-overlay nil)))))
14975 (setq final (org-read-date-analyze ans def defdecode))
14977 (when org-read-date-analyze-forced-year
14978 (message "Year was forced into %s"
14979 (if org-read-date-force-compatible-dates
14980 "compatible range (1970-2037)"
14981 "range representable on this machine"))
14984 ;; One round trip to get rid of 34th of August and stuff like that....
14985 (setq final (decode-time (apply 'encode-time final)))
14987 (setq org-read-date-final-answer ans)
14990 (apply 'encode-time final)
14991 (if (and (boundp 'org-time-was-given) org-time-was-given)
14992 (format "%04d-%02d-%02d %02d:%02d"
14993 (nth 5 final) (nth 4 final) (nth 3 final)
14994 (nth 2 final) (nth 1 final))
14995 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15000 (defun org-read-date-display ()
15001 "Display the current date prompt interpretation in the minibuffer."
15002 (when org-read-date-display-live
15003 (when org-read-date-overlay
15004 (delete-overlay org-read-date-overlay))
15007 (while (not (equal (buffer-substring
15008 (max (point-min) (- (point) 4)) (point))
15012 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15013 " " (or org-ans1 org-ans2)))
15014 (org-end-time-was-given nil)
15015 (f (org-read-date-analyze ans def defdecode))
15017 org-time-stamp-custom-formats
15018 org-time-stamp-formats))
15019 (fmt (if (or with-time
15020 (and (boundp 'org-time-was-given) org-time-was-given))
15023 (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
15024 (when (and org-end-time-was-given
15025 (string-match org-plain-time-of-day-regexp txt))
15026 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15027 org-end-time-was-given
15028 (substring txt (match-end 0)))))
15029 (when org-read-date-analyze-futurep
15030 (setq txt (concat txt " (=>F)")))
15031 (setq org-read-date-overlay
15032 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15033 (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
15035 (defun org-read-date-analyze (ans def defdecode)
15036 "Analyze the combined answer of the date prompt."
15037 ;; FIXME: cleanup and comment
15038 (let ((nowdecode (decode-time (current-time)))
15039 delta deltan deltaw deltadef year month day
15040 hour minute second wday pm h2 m2 tl wday1
15041 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15042 (setq org-read-date-analyze-futurep nil
15043 org-read-date-analyze-forced-year nil)
15044 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15047 (when (setq delta (org-read-date-get-relative ans (current-time) def))
15048 (setq ans (replace-match "" t t ans)
15050 deltaw (nth 1 delta)
15051 deltadef (nth 2 delta)))
15053 ;; Check if there is an iso week date in there
15054 ;; If yes, store the info and postpone interpreting it until the rest
15055 ;; of the parsing is done
15056 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15057 (setq iso-year (if (match-end 1)
15058 (org-small-year-to-year
15059 (string-to-number (match-string 1 ans))))
15060 iso-weekday (if (match-end 3)
15061 (string-to-number (match-string 3 ans)))
15062 iso-week (string-to-number (match-string 2 ans)))
15063 (setq ans (replace-match "" t t ans)))
15065 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15066 (when (string-match
15067 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15068 (setq year (if (match-end 2)
15069 (string-to-number (match-string 2 ans))
15070 (progn (setq kill-year t)
15071 (string-to-number (format-time-string "%Y"))))
15072 month (string-to-number (match-string 3 ans))
15073 day (string-to-number (match-string 4 ans)))
15074 (if (< year 100) (setq year (+ 2000 year)))
15075 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15078 ;; Help matching dottet european dates
15079 (when (string-match
15080 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
15081 (setq year (if (match-end 3)
15082 (string-to-number (match-string 3 ans))
15083 (progn (setq kill-year t)
15084 (string-to-number (format-time-string "%Y"))))
15085 day (string-to-number (match-string 1 ans))
15086 month (string-to-number (match-string 2 ans))
15087 ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15090 ;; Help matching american dates, like 5/30 or 5/30/7
15091 (when (string-match
15092 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15093 (setq year (if (match-end 4)
15094 (string-to-number (match-string 4 ans))
15095 (progn (setq kill-year t)
15096 (string-to-number (format-time-string "%Y"))))
15097 month (string-to-number (match-string 1 ans))
15098 day (string-to-number (match-string 2 ans)))
15099 (if (< year 100) (setq year (+ 2000 year)))
15100 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15102 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15103 ;; If there is a time with am/pm, and *no* time without it, we convert
15104 ;; so that matching will be successful.
15105 (loop for i from 1 to 2 do ; twice, for end time as well
15106 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15107 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15108 (setq hour (string-to-number (match-string 1 ans))
15109 minute (if (match-end 3)
15110 (string-to-number (match-string 3 ans))
15113 (string-to-char (downcase (match-string 4 ans)))))
15114 (if (and (= hour 12) (not pm))
15116 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15117 (setq ans (replace-match (format "%02d:%02d" hour minute)
15120 ;; Check if a time range is given as a duration
15121 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15122 (setq hour (string-to-number (match-string 1 ans))
15123 h2 (+ hour (string-to-number (match-string 3 ans)))
15124 minute (string-to-number (match-string 2 ans))
15125 m2 (+ minute (if (match-end 5) (string-to-number
15126 (match-string 5 ans))0)))
15127 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15128 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15131 ;; Check if there is a time range
15132 (when (boundp 'org-end-time-was-given)
15133 (setq org-time-was-given nil)
15134 (when (and (string-match org-plain-time-of-day-regexp ans)
15136 (setq org-end-time-was-given (match-string 8 ans))
15137 (setq ans (concat (substring ans 0 (match-beginning 7))
15138 (substring ans (match-end 7))))))
15140 (setq tl (parse-time-string ans)
15141 day (or (nth 3 tl) (nth 3 defdecode))
15142 month (or (nth 4 tl)
15143 (if (and org-read-date-prefer-future
15144 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15145 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15146 (nth 4 defdecode)))
15147 year (or (and (not kill-year) (nth 5 tl))
15148 (if (and org-read-date-prefer-future
15149 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15150 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15151 (nth 5 defdecode)))
15152 hour (or (nth 2 tl) (nth 2 defdecode))
15153 minute (or (nth 1 tl) (nth 1 defdecode))
15154 second (or (nth 0 tl) 0)
15157 (when (and (eq org-read-date-prefer-future 'time)
15158 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15159 (equal day (nth 3 nowdecode))
15160 (equal month (nth 4 nowdecode))
15161 (equal year (nth 5 nowdecode))
15163 (or (< (nth 2 tl) (nth 2 nowdecode))
15164 (and (= (nth 2 tl) (nth 2 nowdecode))
15166 (< (nth 1 tl) (nth 1 nowdecode)))))
15170 ;; Special date definitions below
15173 ;; There was an iso week
15176 (setq year (or iso-year year)
15177 day (or iso-weekday wday 1)
15178 wday nil ; to make sure that the trigger below does not match
15179 iso-date (calendar-gregorian-from-absolute
15180 (calendar-absolute-from-iso
15181 (list iso-week day year))))
15182 ; FIXME: Should we also push ISO weeks into the future?
15183 ; (when (and org-read-date-prefer-future
15185 ; (< (calendar-absolute-from-gregorian iso-date)
15186 ; (time-to-days (current-time))))
15187 ; (setq year (1+ year)
15188 ; iso-date (calendar-gregorian-from-absolute
15189 ; (calendar-absolute-from-iso
15190 ; (list iso-week day year)))))
15191 (setq month (car iso-date)
15192 year (nth 2 iso-date)
15193 day (nth 1 iso-date)))
15197 (let ((now (decode-time (current-time))))
15198 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15199 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15200 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15201 ((equal deltaw "m") (setq month (+ month deltan)))
15202 ((equal deltaw "y") (setq year (+ year deltan)))))
15203 ((and wday (not (nth 3 tl)))
15205 ;; Weekday was given, but no day, so pick that day in the week
15206 ;; on or after the derived date.
15207 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15208 (unless (equal wday wday1)
15209 (setq day (+ day (% (- wday wday1 -7) 7))))))
15210 (if (and (boundp 'org-time-was-given)
15212 (setq org-time-was-given t))
15213 (if (< year 100) (setq year (+ 2000 year)))
15214 ;; Check of the date is representable
15215 (if org-read-date-force-compatible-dates
15218 (setq year 1970 org-read-date-analyze-forced-year t))
15220 (setq year 2037 org-read-date-analyze-forced-year t)))
15221 (condition-case nil
15222 (ignore (encode-time second minute hour day month year))
15224 (setq year (nth 5 defdecode))
15225 (setq org-read-date-analyze-forced-year t))))
15226 (setq org-read-date-analyze-futurep futurep)
15227 (list second minute hour day month year)))
15229 (defvar parse-time-weekdays)
15231 (defun org-read-date-get-relative (s today default)
15232 "Check string S for special relative date string.
15233 TODAY and DEFAULT are internal times, for today and for a default.
15234 Return shift list (N what def-flag)
15235 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15236 N is the number of WHATs to shift.
15237 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15238 the DEFAULT date rather than TODAY."
15239 (require 'parse-time)
15243 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15245 "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15246 "\\([ \t]\\|$\\)") s)
15247 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15248 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15249 (string-to-char (substring (match-string 1 s) -1))
15251 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15252 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15253 (what (if (match-end 3) (match-string 3 s) "d"))
15254 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15255 (date (if rel default today))
15256 (wday (nth 6 (decode-time date)))
15260 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15261 (if (= dir ?-) (setq delta (- delta 7)))
15262 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15263 (list delta "d" rel))
15264 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15266 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15267 "Turn a user-specified date into the internal representation.
15268 The internal representation needed by the calendar is (month day year).
15269 This is a wrapper to handle the brain-dead convention in calendar that
15270 user function argument order change dependent on argument order."
15271 (if (boundp 'calendar-date-style)
15273 ((eq calendar-date-style 'american)
15274 (list arg1 arg2 arg3))
15275 ((eq calendar-date-style 'european)
15276 (list arg2 arg1 arg3))
15277 ((eq calendar-date-style 'iso)
15278 (list arg2 arg3 arg1)))
15279 (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15280 (if (org-bound-and-true-p european-calendar-style)
15281 (list arg2 arg1 arg3)
15282 (list arg1 arg2 arg3)))))
15284 (defun org-eval-in-calendar (form &optional keepdate)
15285 "Eval FORM in the calendar window and return to current window.
15286 Also, store the cursor date in variable org-ans2."
15287 (let ((sf (selected-frame))
15288 (sw (selected-window)))
15289 (select-window (get-buffer-window "*Calendar*" t))
15291 (when (and (not keepdate) (calendar-cursor-to-date))
15292 (let* ((date (calendar-cursor-to-date))
15293 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15294 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15295 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15297 (org-select-frame-set-input-focus sf)))
15299 (defun org-calendar-select ()
15300 "Return to `org-read-date' with the date currently selected.
15301 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15303 (when (calendar-cursor-to-date)
15304 (let* ((date (calendar-cursor-to-date))
15305 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15306 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15307 (if (active-minibuffer-window) (exit-minibuffer))))
15309 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15310 "Insert a date stamp for the date given by the internal TIME.
15311 WITH-HM means use the stamp format that includes the time of the day.
15312 INACTIVE means use square brackets instead of angular ones, so that the
15313 stamp will not contribute to the agenda.
15314 PRE and POST are optional strings to be inserted before and after the
15316 The command returns the inserted time stamp."
15317 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15319 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15320 (insert-before-markers (or pre ""))
15321 (when (listp extra)
15322 (setq extra (car extra))
15323 (if (and (stringp extra)
15324 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15325 (setq extra (format "-%02d:%02d"
15326 (string-to-number (match-string 1 extra))
15327 (string-to-number (match-string 2 extra))))
15330 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15331 (insert-before-markers (setq stamp (format-time-string fmt time)))
15332 (insert-before-markers (or post ""))
15333 (setq org-last-inserted-timestamp stamp)))
15335 (defun org-toggle-time-stamp-overlays ()
15336 "Toggle the use of custom time stamp formats."
15338 (setq org-display-custom-times (not org-display-custom-times))
15339 (unless org-display-custom-times
15340 (let ((p (point-min)) (bmp (buffer-modified-p)))
15341 (while (setq p (next-single-property-change p 'display))
15342 (if (and (get-text-property p 'display)
15343 (eq (get-text-property p 'face) 'org-date))
15344 (remove-text-properties
15345 p (setq p (next-single-property-change p 'display))
15347 (set-buffer-modified-p bmp)))
15348 (if (featurep 'xemacs)
15349 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15350 (org-restart-font-lock)
15351 (setq org-table-may-need-update t)
15352 (if org-display-custom-times
15353 (message "Time stamps are overlaid with custom format")
15354 (message "Time stamp overlays removed")))
15356 (defun org-display-custom-time (beg end)
15357 "Overlay modified time stamp format over timestamp between BEG and END."
15358 (let* ((ts (buffer-substring beg end))
15359 t1 w1 with-hm tf time str w2 (off 0))
15361 (setq t1 (org-parse-time-string ts t))
15362 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
15363 (setq off (- (match-end 0) (match-beginning 0)))))
15364 (setq end (- end off))
15365 (setq w1 (- end beg)
15366 with-hm (and (nth 1 t1) (nth 2 t1))
15367 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15368 time (org-fix-decoded-time t1)
15370 (format-time-string
15371 (substring tf 1 -1) (apply 'encode-time time))
15372 nil 'mouse-face 'highlight)
15374 (if (not (= w2 w1))
15375 (add-text-properties (1+ beg) (+ 2 beg)
15376 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15377 (if (featurep 'xemacs)
15379 (put-text-property beg end 'invisible t)
15380 (put-text-property beg end 'end-glyph (make-glyph str)))
15381 (put-text-property beg end 'display str))))
15383 (defun org-translate-time (string)
15384 "Translate all timestamps in STRING to custom format.
15385 But do this only if the variable `org-display-custom-times' is set."
15386 (when org-display-custom-times
15389 (re org-ts-regexp-both)
15390 t1 with-hm inactive tf time str beg end)
15391 (while (setq start (string-match re string start))
15392 (setq beg (match-beginning 0)
15394 t1 (save-match-data
15395 (org-parse-time-string (substring string beg end) t))
15396 with-hm (and (nth 1 t1) (nth 2 t1))
15397 inactive (equal (substring string beg (1+ beg)) "[")
15398 tf (funcall (if with-hm 'cdr 'car)
15399 org-time-stamp-custom-formats)
15400 time (org-fix-decoded-time t1)
15401 str (format-time-string
15403 (if inactive "[" "<") (substring tf 1 -1)
15404 (if inactive "]" ">"))
15405 (apply 'encode-time time))
15406 string (replace-match str t t string)
15407 start (+ start (length str)))))))
15410 (defun org-fix-decoded-time (time)
15411 "Set 0 instead of nil for the first 6 elements of time.
15412 Don't touch the rest."
15414 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15416 (defun org-days-to-time (timestamp-string)
15417 "Difference between TIMESTAMP-STRING and now in days."
15418 (- (time-to-days (org-time-string-to-time timestamp-string))
15419 (time-to-days (current-time))))
15421 (defun org-deadline-close (timestamp-string &optional ndays)
15422 "Is the time in TIMESTAMP-STRING close to the current date?"
15423 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15424 (and (< (org-days-to-time timestamp-string) ndays)
15425 (not (org-entry-is-done-p))))
15427 (defun org-get-wdays (ts)
15428 "Get the deadline lead time appropriate for timestring TS."
15430 ((<= org-deadline-warning-days 0)
15431 ;; 0 or negative, enforce this value no matter what
15432 (- org-deadline-warning-days))
15433 ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
15434 ;; lead time is specified.
15435 (floor (* (string-to-number (match-string 1 ts))
15436 (cdr (assoc (match-string 2 ts)
15437 '(("d" . 1) ("w" . 7)
15438 ("m" . 30.4) ("y" . 365.25)))))))
15439 ;; go for the default.
15440 (t org-deadline-warning-days)))
15442 (defun org-calendar-select-mouse (ev)
15443 "Return to `org-read-date' with the date currently selected.
15444 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15446 (mouse-set-point ev)
15447 (when (calendar-cursor-to-date)
15448 (let* ((date (calendar-cursor-to-date))
15449 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15450 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15451 (if (active-minibuffer-window) (exit-minibuffer))))
15453 (defun org-check-deadlines (ndays)
15454 "Check if there are any deadlines due or past due.
15455 A deadline is considered due if it happens within `org-deadline-warning-days'
15456 days from today's date. If the deadline appears in an entry marked DONE,
15457 it is not shown. The prefix arg NDAYS can be used to test that many
15458 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15460 (let* ((org-warn-days
15462 ((equal ndays '(4)) 100000)
15463 (ndays (prefix-numeric-value ndays))
15464 (t (abs org-deadline-warning-days))))
15465 (case-fold-search nil)
15466 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15468 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15470 (message "%d deadlines past-due or due within %d days"
15471 (org-occur regexp nil callback)
15474 (defun org-check-before-date (date)
15475 "Check if there are deadlines or scheduled entries before DATE."
15476 (interactive (list (org-read-date)))
15477 (let ((case-fold-search nil)
15478 (regexp (concat "\\<\\(" org-deadline-string
15479 "\\|" org-scheduled-string
15480 "\\) *<\\([^>]+\\)>"))
15482 (lambda () (time-less-p
15483 (org-time-string-to-time (match-string 2))
15484 (org-time-string-to-time date)))))
15485 (message "%d entries before %s"
15486 (org-occur regexp nil callback) date)))
15488 (defun org-check-after-date (date)
15489 "Check if there are deadlines or scheduled entries after DATE."
15490 (interactive (list (org-read-date)))
15491 (let ((case-fold-search nil)
15492 (regexp (concat "\\<\\(" org-deadline-string
15493 "\\|" org-scheduled-string
15494 "\\) *<\\([^>]+\\)>"))
15498 (org-time-string-to-time (match-string 2))
15499 (org-time-string-to-time date))))))
15500 (message "%d entries after %s"
15501 (org-occur regexp nil callback) date)))
15503 (defun org-evaluate-time-range (&optional to-buffer)
15504 "Evaluate a time range by computing the difference between start and end.
15505 Normally the result is just printed in the echo area, but with prefix arg
15506 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
15507 If the time range is actually in a table, the result is inserted into the
15509 For time difference computation, a year is assumed to be exactly 365
15510 days in order to avoid rounding problems."
15513 (org-clock-update-time-maybe)
15515 (unless (org-at-date-range-p t)
15516 (goto-char (point-at-bol))
15517 (re-search-forward org-tr-regexp-both (point-at-eol) t))
15518 (if (not (org-at-date-range-p t))
15519 (error "Not at a time-stamp range, and none found in current line")))
15520 (let* ((ts1 (match-string 1))
15521 (ts2 (match-string 2))
15522 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
15523 (match-end (match-end 0))
15524 (time1 (org-time-string-to-time ts1))
15525 (time2 (org-time-string-to-time ts2))
15526 (t1 (org-float-time time1))
15527 (t2 (org-float-time time2))
15528 (diff (abs (- t2 t1)))
15529 (negative (< (- t2 t1) 0))
15530 ;; (ys (floor (* 365 24 60 60)))
15533 (fy "%dy %dd %02d:%02d")
15535 (fd "%dd %02d:%02d")
15540 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15542 d (floor (/ diff ds)) diff (mod diff ds)
15543 h (floor (/ diff hs)) diff (mod diff hs)
15544 m (floor (/ diff 60)))
15545 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15547 d (floor (+ (/ diff ds) 0.5))
15549 (if (not to-buffer)
15550 (message "%s" (org-make-tdiff-string y d h m))
15551 (if (org-at-table-p)
15553 (goto-char match-end)
15555 (and (looking-at " *|") (goto-char (match-end 0))))
15556 (goto-char match-end))
15558 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
15559 (replace-match ""))
15560 (if negative (insert " -"))
15561 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
15562 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
15563 (insert " " (format fh h m))))
15564 (if align (org-table-align))
15565 (message "Time difference inserted")))))
15567 (defun org-make-tdiff-string (y d h m)
15570 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
15572 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
15574 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
15576 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
15578 (apply 'format fmt (nreverse l))))
15580 (defun org-time-string-to-time (s &optional buffer pos)
15581 (condition-case errdata
15582 (apply 'encode-time (org-parse-time-string s))
15583 (error (error "Bad timestamp `%s'%s\nError was: %s"
15584 s (if (not (and buffer pos))
15586 (format " at %d in buffer `%s'" pos buffer))
15589 (defun org-time-string-to-seconds (s)
15590 (org-float-time (org-time-string-to-time s)))
15592 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
15593 "Convert a time stamp to an absolute day number.
15594 If there is a specifier for a cyclic time stamp, get the closest date to
15596 PREFER and SHOW-ALL are passed through to `org-closest-date'.
15597 The variable date is bound by the calendar when this is called."
15599 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
15600 (if (org-diary-sexp-entry (match-string 1 s) "" date)
15603 ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
15604 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
15605 (time-to-days (current-time))) (match-string 0 s)
15608 (condition-case errdata
15609 (apply 'encode-time (org-parse-time-string s))
15610 (error (error "Bad timestamp `%s'%s\nError was: %s"
15611 s (if (not (and buffer pos))
15613 (format " at %d in buffer `%s'" pos buffer))
15614 (cdr errdata))))))))
15616 (defun org-days-to-iso-week (days)
15617 "Return the iso week number."
15619 (car (calendar-iso-from-absolute days)))
15621 (defun org-small-year-to-year (year)
15622 "Convert 2-digit years into 4-digit years.
15623 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
15624 The year 2000 cannot be abbreviated. Any year larger than 99
15625 is returned unchanged."
15627 (setq year (+ 2000 year))
15629 (setq year (+ 1900 year))))
15632 (defun org-time-from-absolute (d)
15633 "Return the time corresponding to date D.
15634 D may be an absolute day number, or a calendar-type list (month day year)."
15635 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
15636 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
15638 (defun org-calendar-holiday ()
15639 "List of holidays, for Diary display in Org-mode."
15640 (require 'holidays)
15642 (if (fboundp 'calendar-check-holidays)
15643 'calendar-check-holidays 'check-calendar-holidays) date)))
15644 (if hl (mapconcat 'identity hl "; "))))
15646 (defun org-diary-sexp-entry (sexp entry date)
15647 "Process a SEXP diary ENTRY for DATE."
15648 (require 'diary-lib)
15649 (let ((result (if calendar-debug-sexp
15650 (let ((stack-trace-on-error t))
15651 (eval (car (read-from-string sexp))))
15652 (condition-case nil
15653 (eval (car (read-from-string sexp)))
15656 (message "Bad sexp at line %d in %s: %s"
15658 (buffer-file-name) sexp)
15660 (cond ((stringp result) (split-string result "; "))
15661 ((and (consp result)
15662 (not (consp (cdr result)))
15663 (stringp (cdr result))) (cdr result))
15664 ((and (consp result)
15665 (stringp (car result))) result)
15669 (defun org-diary-to-ical-string (frombuf)
15670 "Get iCalendar entries from diary entries in buffer FROMBUF.
15671 This uses the icalendar.el library."
15672 (let* ((tmpdir (if (featurep 'xemacs)
15674 temporary-file-directory))
15675 (tmpfile (make-temp-name
15676 (expand-file-name "orgics" tmpdir)))
15678 (with-current-buffer frombuf
15679 (icalendar-export-region (point-min) (point-max) tmpfile)
15680 (setq buf (find-buffer-visiting tmpfile))
15682 (goto-char (point-min))
15683 (if (re-search-forward "^BEGIN:VEVENT" nil t)
15684 (setq b (match-beginning 0)))
15685 (goto-char (point-max))
15686 (if (re-search-backward "^END:VEVENT" nil t)
15687 (setq e (match-end 0)))
15688 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
15690 (delete-file tmpfile)
15693 (defun org-closest-date (start current change prefer show-all)
15694 "Find the date closest to CURRENT that is consistent with START and CHANGE.
15695 When PREFER is `past', return a date that is either CURRENT or past.
15696 When PREFER is `future', return a date that is either CURRENT or future.
15697 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
15698 ;; Make the proper lists from the dates
15700 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
15701 dn dw sday cday n1 n2 n0
15702 d m y y1 y2 date1 date2 nmonths nm ny m2)
15704 (setq start (org-date-to-gregorian start)
15705 current (org-date-to-gregorian
15708 (time-to-days (current-time))))
15709 sday (calendar-absolute-from-gregorian start)
15710 cday (calendar-absolute-from-gregorian current))
15712 (if (<= cday sday) (throw 'exit sday))
15714 (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
15715 (setq dn (string-to-number (match-string 1 change))
15716 dw (cdr (assoc (match-string 2 change) a1)))
15717 (error "Invalid change specifier: %s" change))
15718 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
15721 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
15724 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
15725 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
15726 (setq date1 (list m d y1)
15727 n1 (calendar-absolute-from-gregorian date1)
15728 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
15729 n2 (calendar-absolute-from-gregorian date2)))
15731 ;; approx number of month between the two dates
15732 (setq nmonths (floor (/ (- cday sday) 30.436875)))
15733 ;; How often does dn fit in there?
15734 (setq d (nth 1 start) m (car start) y (nth 2 start)
15735 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
15737 ny (floor (/ m 12))
15740 (while (> m 12) (setq m (- m 12) y (1+ y)))
15741 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
15742 (setq m2 (+ m dn) y2 y)
15743 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15744 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
15745 (while (<= n2 cday)
15746 (setq n1 n2 m m2 y y2)
15747 (setq m2 (+ m dn) y2 y)
15748 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
15749 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
15750 ;; Make sure n1 is the earlier date
15751 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
15754 ((eq prefer 'past) (if (= cday n2) n2 n1))
15755 ((eq prefer 'future) (if (= cday n1) n1 n2))
15756 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
15758 ((eq prefer 'past) (if (= cday n2) n2 n1))
15759 ((eq prefer 'future) (if (= cday n1) n1 n2))
15760 (t (if (= cday n1) n1 n2)))))))
15762 (defun org-date-to-gregorian (date)
15763 "Turn any specification of DATE into a Gregorian date for the calendar."
15764 (cond ((integerp date) (calendar-gregorian-from-absolute date))
15765 ((and (listp date) (= (length date) 3)) date)
15767 (setq date (org-parse-time-string date))
15768 (list (nth 4 date) (nth 3 date) (nth 5 date)))
15770 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
15772 (defun org-parse-time-string (s &optional nodefault)
15773 "Parse the standard Org-mode time string.
15774 This should be a lot faster than the normal `parse-time-string'.
15775 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
15776 hour and minute fields will be nil if not given."
15777 (if (string-match org-ts-regexp0 s)
15779 (if (or (match-beginning 8) (not nodefault))
15780 (string-to-number (or (match-string 8 s) "0")))
15781 (if (or (match-beginning 7) (not nodefault))
15782 (string-to-number (or (match-string 7 s) "0")))
15783 (string-to-number (match-string 4 s))
15784 (string-to-number (match-string 3 s))
15785 (string-to-number (match-string 2 s))
15787 (error "Not a standard Org-mode time string: %s" s)))
15789 (defun org-timestamp-up (&optional arg)
15790 "Increase the date item at the cursor by one.
15791 If the cursor is on the year, change the year. If it is on the month,
15792 the day or the time, change that.
15793 With prefix ARG, change by that many units."
15795 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
15797 (defun org-timestamp-down (&optional arg)
15798 "Decrease the date item at the cursor by one.
15799 If the cursor is on the year, change the year. If it is on the month,
15800 the day or the time, change that.
15801 With prefix ARG, change by that many units."
15803 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
15805 (defun org-timestamp-up-day (&optional arg)
15806 "Increase the date in the time stamp by one day.
15807 With prefix ARG, change that many days."
15809 (if (and (not (org-at-timestamp-p t))
15810 (org-on-heading-p))
15812 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
15814 (defun org-timestamp-down-day (&optional arg)
15815 "Decrease the date in the time stamp by one day.
15816 With prefix ARG, change that many days."
15818 (if (and (not (org-at-timestamp-p t))
15819 (org-on-heading-p))
15821 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
15823 (defun org-at-timestamp-p (&optional inactive-ok)
15824 "Determine if the cursor is in or at a timestamp."
15826 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
15828 (ans (or (looking-at tsr)
15830 (skip-chars-backward "^[<\n\r\t")
15831 (if (> (point) (point-min)) (backward-char 1))
15832 (and (looking-at tsr)
15833 (> (- (match-end 0) pos) -1))))))
15835 (boundp 'org-ts-what)
15838 ((= pos (match-beginning 0)) 'bracket)
15839 ;; Point is considered to be "on the bracket" whether
15840 ;; it's really on it or right after it.
15841 ((or (= pos (1- (match-end 0)))
15842 (= pos (match-end 0))) 'bracket)
15843 ((org-pos-in-match-range pos 2) 'year)
15844 ((org-pos-in-match-range pos 3) 'month)
15845 ((org-pos-in-match-range pos 7) 'hour)
15846 ((org-pos-in-match-range pos 8) 'minute)
15847 ((or (org-pos-in-match-range pos 4)
15848 (org-pos-in-match-range pos 5)) 'day)
15849 ((and (> pos (or (match-end 8) (match-end 5)))
15850 (< pos (match-end 0)))
15851 (- pos (or (match-end 8) (match-end 5))))
15855 (defun org-toggle-timestamp-type ()
15856 "Toggle the type (<active> or [inactive]) of a time stamp."
15858 (when (org-at-timestamp-p t)
15859 (let ((beg (match-beginning 0)) (end (match-end 0))
15860 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
15863 (while (re-search-forward "[][<>]" end t)
15864 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
15866 (message "Timestamp is now %sactive"
15867 (if (equal (char-after beg) ?<) "" "in")))))
15869 (defun org-timestamp-change (n &optional what updown)
15870 "Change the date in the time stamp at point.
15871 The date will be changed by N times WHAT. WHAT can be `day', `month',
15872 `year', `minute', `second'. If WHAT is not given, the cursor position
15873 in the timestamp determines what will be changed."
15874 (let ((origin (point)) origin-cat
15876 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
15880 (if (not (org-at-timestamp-p t))
15881 (error "Not at a timestamp"))
15882 (if (and (not what) (eq org-ts-what 'bracket))
15883 (org-toggle-timestamp-type)
15884 ;; Point isn't on brackets. Remember the part of the time-stamp
15885 ;; the point was in. Indeed, size of time-stamps may change,
15886 ;; but point must be kept in the same category nonetheless.
15887 (setq origin-cat org-ts-what)
15888 (if (and (not what) (not (eq org-ts-what 'day))
15889 org-display-custom-times
15890 (get-text-property (point) 'display)
15891 (not (get-text-property (1- (point)) 'display)))
15892 (setq org-ts-what 'day))
15893 (setq org-ts-what (or what org-ts-what)
15894 inactive (= (char-after (match-beginning 0)) ?\[)
15895 ts (match-string 0))
15898 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
15900 (setq extra (match-string 1 ts)))
15901 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
15903 (setq time0 (org-parse-time-string ts))
15905 (eq org-ts-what 'minute)
15906 (not current-prefix-arg))
15907 ;; This looks like s-up and s-down. Change by one rounding step.
15908 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
15909 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
15910 (setcar (cdr time0) (+ (nth 1 time0)
15911 (if (> n 0) (- rem) (- dm rem))))))
15913 (encode-time (or (car time0) 0)
15914 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
15915 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
15916 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
15917 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
15918 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
15920 (when (and (member org-ts-what '(hour minute))
15922 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
15923 (setq extra (org-modify-ts-extra
15925 (if (eq org-ts-what 'hour) 2 5)
15927 (when (integerp org-ts-what)
15928 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
15929 (if (eq what 'calendar)
15930 (let ((cal-date (org-get-date-from-calendar)))
15931 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
15932 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
15933 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
15934 (setcar time0 (or (car time0) 0))
15935 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
15936 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
15937 (setq time (apply 'encode-time time0))))
15938 ;; Insert the new time-stamp, and ensure point stays in the same
15939 ;; category as before (i.e. not after the last position in that
15941 (let ((pos (point)))
15942 ;; Stay before inserted string. `save-excursion' is of no use.
15943 (setq org-last-changed-timestamp
15944 (org-insert-time-stamp time with-hm inactive nil nil extra))
15947 (looking-at org-ts-regexp3)
15949 ;; `day' category ends before `hour' if any, or at
15950 ;; the end of the day name.
15951 ((eq origin-cat 'day)
15952 (min (or (match-beginning 7) (1- (match-end 5))) origin))
15953 ((eq origin-cat 'hour) (min (match-end 7) origin))
15954 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
15955 ((integerp origin-cat) (min (1- (match-end 0)) origin))
15956 ;; `year' and `month' have both fixed size: point
15957 ;; couldn't have moved into another part.
15959 ;; Update clock if on a CLOCK line.
15960 (org-clock-update-time-maybe)
15961 ;; Try to recenter the calendar window, if any.
15962 (if (and org-calendar-follow-timestamp-change
15963 (get-buffer-window "*Calendar*" t)
15964 (memq org-ts-what '(day month year)))
15965 (org-recenter-calendar (time-to-days time))))))
15967 (defun org-modify-ts-extra (s pos n dm)
15968 "Change the different parts of the lead-time and repeat fields in timestamp."
15969 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
15971 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
15973 ((or (org-pos-in-match-range pos 2)
15974 (org-pos-in-match-range pos 3))
15975 (setq m (string-to-number (match-string 3 s))
15976 h (string-to-number (match-string 2 s)))
15977 (if (org-pos-in-match-range pos 2)
15979 (setq n (* dm (org-no-warnings (signum n))))
15980 (when (not (= 0 (setq rem (% m dm))))
15981 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
15983 (if (< m 0) (setq m (+ m 60) h (1- h)))
15984 (if (> m 59) (setq m (- m 60) h (1+ h)))
15985 (setq h (min 24 (max 0 h)))
15986 (setq ng 1 new (format "-%02d:%02d" h m)))
15987 ((org-pos-in-match-range pos 6)
15988 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
15989 ((org-pos-in-match-range pos 5)
15990 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
15992 ((org-pos-in-match-range pos 9)
15993 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
15994 ((org-pos-in-match-range pos 8)
15995 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
15999 (substring s 0 (match-beginning ng))
16001 (substring s (match-end ng))))))
16004 (defun org-recenter-calendar (date)
16005 "If the calendar is visible, recenter it to DATE."
16006 (let* ((win (selected-window))
16007 (cwin (get-buffer-window "*Calendar*" t))
16008 (calendar-move-hook nil))
16010 (select-window cwin)
16011 (calendar-goto-date (if (listp date) date
16012 (calendar-gregorian-from-absolute date)))
16013 (select-window win))))
16015 (defun org-goto-calendar (&optional arg)
16016 "Go to the Emacs calendar at the current date.
16017 If there is a time stamp in the current line, go to that date.
16018 A prefix ARG can be used to force the current date."
16020 (let ((tsr org-ts-regexp) diff
16021 (calendar-move-hook nil)
16022 (calendar-view-holidays-initially-flag nil)
16023 (calendar-view-diary-initially-flag nil))
16024 (if (or (org-at-timestamp-p)
16026 (beginning-of-line 1)
16027 (looking-at (concat ".*" tsr))))
16028 (let ((d1 (time-to-days (current-time)))
16030 (org-time-string-to-time (match-string 1)))))
16031 (setq diff (- d2 d1))))
16033 (calendar-goto-today)
16034 (if (and diff (not arg)) (calendar-forward-day diff))))
16036 (defun org-get-date-from-calendar ()
16037 "Return a list (month day year) of date at point in calendar."
16038 (with-current-buffer "*Calendar*"
16040 (calendar-cursor-to-date))))
16042 (defun org-date-from-calendar ()
16043 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16044 If there is already a time stamp at the cursor position, update it."
16046 (if (org-at-timestamp-p t)
16047 (org-timestamp-change 0 'calendar)
16048 (let ((cal-date (org-get-date-from-calendar)))
16049 (org-insert-time-stamp
16050 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16052 (defun org-minutes-to-hh:mm-string (m)
16053 "Compute H:MM from a number of minutes."
16054 (let ((h (/ m 60)))
16055 (setq m (- m (* 60 h)))
16056 (format org-time-clocksum-format h m)))
16058 (defun org-hh:mm-string-to-minutes (s)
16059 "Convert a string H:MM to a number of minutes.
16060 If the string is just a number, interpret it as minutes.
16061 In fact, the first hh:mm or number in the string will be taken,
16062 there can be extra stuff in the string.
16063 If no number is found, the return value is 0."
16066 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16067 (+ (* (string-to-number (match-string 1 s)) 60)
16068 (string-to-number (match-string 2 s))))
16069 ((string-match "\\([0-9]+\\)" s)
16070 (string-to-number (match-string 1 s)))
16073 (defcustom org-effort-durations
16076 ("w" . ,(* 60 8 5))
16077 ("m" . ,(* 60 8 5 4))
16078 ("y" . ,(* 60 8 5 40)))
16079 "Conversion factor to minutes for an effort modifier.
16081 Each entry has the form (MODIFIER . MINUTES).
16083 In an effort string, a number followed by MODIFIER is multiplied
16084 by the specified number of MINUTES to obtain an effort in
16087 For example, if the value of this variable is ((\"hours\" . 60)), then an
16088 effort string \"2hours\" is equivalent to 120 minutes."
16090 :type '(alist :key-type (string :tag "Modifier")
16091 :value-type (number :tag "Minutes")))
16093 (defun org-duration-string-to-minutes (s)
16094 "Convert a duration string S to minutes.
16096 A bare number is interpreted as minutes, modifiers can be set by
16097 customizing `org-effort-durations' (which see).
16099 Entries containing a colon are interpreted as H:MM by
16100 `org-hh:mm-string-to-minutes'."
16102 (re (concat "\\([0-9]+\\) *\\("
16103 (regexp-opt (mapcar 'car org-effort-durations))
16105 (while (string-match re s)
16106 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16107 (string-to-number (match-string 1 s))))
16108 (setq s (replace-match "" nil t s)))
16109 (incf result (org-hh:mm-string-to-minutes s))
16114 (defun org-save-all-org-buffers ()
16115 "Save all Org-mode buffers without user confirmation."
16117 (message "Saving all Org-mode buffers...")
16118 (save-some-buffers t (lambda () (eq major-mode 'org-mode)))
16119 (when (featurep 'org-id) (org-id-locations-save))
16120 (message "Saving all Org-mode buffers... done"))
16122 (defun org-revert-all-org-buffers ()
16123 "Revert all Org-mode buffers.
16124 Prompt for confirmation when there are unsaved changes.
16125 Be sure you know what you are doing before letting this function
16126 overwrite your changes.
16128 This function is useful in a setup where one tracks org files
16129 with a version control system, to revert on one machine after pulling
16130 changes from another. I believe the procedure must be like this:
16132 1. M-x org-save-all-org-buffers
16133 2. Pull changes from the other machine, resolve conflicts
16134 3. M-x org-revert-all-org-buffers"
16136 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16139 (save-window-excursion
16142 (when (and (with-current-buffer b (eq major-mode 'org-mode))
16143 (with-current-buffer b buffer-file-name))
16144 (org-pop-to-buffer-same-window b)
16145 (revert-buffer t 'no-confirm)))
16147 (when (and (featurep 'org-id) org-id-track-globally)
16148 (org-id-locations-load)))))
16153 (defun org-switchb (&optional arg)
16154 "Switch between Org buffers.
16155 With one prefix argument, restrict available buffers to files.
16156 With two prefix arguments, restrict available buffers to agenda files.
16158 Defaults to `iswitchb' for buffer name completion.
16159 Set `org-completion-use-ido' to make it use ido instead."
16161 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16162 ((equal arg '(16)) (org-buffer-list 'agenda))
16163 (t (org-buffer-list))))
16164 (org-completion-use-iswitchb org-completion-use-iswitchb)
16165 (org-completion-use-ido org-completion-use-ido))
16166 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16167 (setq org-completion-use-iswitchb t))
16168 (org-pop-to-buffer-same-window
16169 (org-icompleting-read "Org buffer: "
16170 (mapcar 'list (mapcar 'buffer-name blist))
16173 ;;; Define some older names previously used for this functionality
16175 (defalias 'org-ido-switchb 'org-switchb)
16177 (defalias 'org-iswitchb 'org-switchb)
16179 (defun org-buffer-list (&optional predicate exclude-tmp)
16180 "Return a list of Org buffers.
16181 PREDICATE can be `export', `files' or `agenda'.
16183 export restrict the list to Export buffers.
16184 files restrict the list to buffers visiting Org files.
16185 agenda restrict the list to buffers visiting agenda files.
16187 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16189 (agenda-files (and (eq predicate 'agenda)
16190 (mapcar 'file-truename (org-agenda-files t))))
16193 ((eq predicate 'files)
16194 (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
16195 ((eq predicate 'export)
16196 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16197 ((eq predicate 'agenda)
16199 (with-current-buffer b
16200 (and (eq major-mode 'org-mode)
16201 (setq bfn (buffer-file-name b))
16202 (member (file-truename bfn) agenda-files)))))
16203 (t (lambda (b) (with-current-buffer b
16204 (or (eq major-mode 'org-mode)
16205 (string-match "\*Org .*Export"
16206 (buffer-name b)))))))))
16210 (if (and (funcall filter b)
16211 (or (not exclude-tmp)
16212 (not (string-match "tmp" (buffer-name b)))))
16217 (defun org-agenda-files (&optional unrestricted archives)
16218 "Get the list of agenda files.
16219 Optional UNRESTRICTED means return the full list even if a restriction
16220 is currently in place.
16221 When ARCHIVES is t, include all archive files that are really being
16222 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16223 `org-agenda-archives-mode' is t."
16226 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16227 ((stringp org-agenda-files) (org-read-agenda-file-list))
16228 ((listp org-agenda-files) org-agenda-files)
16229 (t (error "Invalid value of `org-agenda-files'")))))
16230 (setq files (apply 'append
16231 (mapcar (lambda (f)
16232 (if (file-directory-p f)
16234 f t org-agenda-file-regexp)
16237 (when org-agenda-skip-unavailable-files
16238 (setq files (delq nil
16241 (and (file-readable-p file) file)))
16243 (when (or (eq archives t)
16244 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16245 (setq files (org-add-archive-files files)))
16248 (defun org-agenda-file-p (&optional file)
16249 "Return non-nil, if FILE is an agenda file.
16250 If FILE is omitted, use the file associated with the current
16252 (member (or file (buffer-file-name))
16253 (org-agenda-files t)))
16255 (defun org-edit-agenda-file-list ()
16256 "Edit the list of agenda files.
16257 Depending on setup, this either uses customize to edit the variable
16258 `org-agenda-files', or it visits the file that is holding the list. In the
16259 latter case, the buffer is set up in a way that saving it automatically kills
16260 the buffer and restores the previous window configuration."
16262 (if (stringp org-agenda-files)
16263 (let ((cw (current-window-configuration)))
16264 (find-file org-agenda-files)
16265 (org-set-local 'org-window-configuration cw)
16266 (org-add-hook 'after-save-hook
16268 (set-window-configuration
16269 (prog1 org-window-configuration
16270 (kill-buffer (current-buffer))))
16271 (org-install-agenda-files-menu)
16272 (message "New agenda file list installed"))
16274 (message "%s" (substitute-command-keys
16275 "Edit list and finish with \\[save-buffer]")))
16276 (customize-variable 'org-agenda-files)))
16278 (defun org-store-new-agenda-file-list (list)
16279 "Set new value for the agenda file list and save it correctly."
16280 (if (stringp org-agenda-files)
16281 (let ((fe (org-read-agenda-file-list t)) b u)
16282 (while (setq b (find-buffer-visiting org-agenda-files))
16284 (with-temp-file org-agenda-files
16287 (lambda (f) ;; Keep un-expanded entries.
16288 (if (setq u (assoc f fe))
16293 (let ((org-mode-hook nil) (org-inhibit-startup t)
16294 (org-insert-mode-line-in-empty-file nil))
16295 (setq org-agenda-files list)
16296 (customize-save-variable 'org-agenda-files org-agenda-files))))
16298 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16299 "Read the list of agenda files from a file.
16300 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16301 filenames, used by `org-store-new-agenda-file-list' to write back
16302 un-expanded file names."
16303 (when (file-directory-p org-agenda-files)
16304 (error "`org-agenda-files' cannot be a single directory"))
16305 (when (stringp org-agenda-files)
16307 (insert-file-contents org-agenda-files)
16310 (let ((e (expand-file-name (substitute-in-file-name f)
16312 (if pair-with-expansion
16315 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16318 (defun org-cycle-agenda-files ()
16319 "Cycle through the files in `org-agenda-files'.
16320 If the current buffer visits an agenda file, find the next one in the list.
16321 If the current buffer does not, find the first agenda file."
16323 (let* ((fs (org-agenda-files t))
16324 (files (append fs (list (car fs))))
16325 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16327 (unless files (error "No agenda files"))
16329 (while (setq file (pop files))
16330 (if (equal (file-truename file) tcf)
16332 (find-file (car files))
16334 (find-file (car fs)))
16335 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16337 (defun org-agenda-file-to-front (&optional to-end)
16338 "Move/add the current file to the top of the agenda file list.
16339 If the file is not present in the list, it is added to the front. If it is
16340 present, it is moved there. With optional argument TO-END, add/move to the
16343 (let ((org-agenda-skip-unavailable-files nil)
16344 (file-alist (mapcar (lambda (x)
16345 (cons (file-truename x) x))
16346 (org-agenda-files t)))
16347 (ctf (file-truename buffer-file-name))
16349 (setq x (assoc ctf file-alist) had x)
16351 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16353 (setq file-alist (append (delq x file-alist) (list x)))
16354 (setq file-alist (cons x (delq x file-alist))))
16355 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16356 (org-install-agenda-files-menu)
16357 (message "File %s to %s of agenda file list"
16358 (if had "moved" "added") (if to-end "end" "front"))))
16360 (defun org-remove-file (&optional file)
16361 "Remove current file from the list of files in variable `org-agenda-files'.
16362 These are the files which are being checked for agenda entries.
16363 Optional argument FILE means use this file instead of the current."
16365 (let* ((org-agenda-skip-unavailable-files nil)
16366 (file (or file buffer-file-name))
16367 (true-file (file-truename file))
16368 (afile (abbreviate-file-name file))
16369 (files (delq nil (mapcar
16371 (if (equal true-file
16374 (org-agenda-files t)))))
16375 (if (not (= (length files) (length (org-agenda-files t))))
16377 (org-store-new-agenda-file-list files)
16378 (org-install-agenda-files-menu)
16379 (message "Removed file: %s" afile))
16380 (message "File was not in list: %s (not removed)" afile))))
16382 (defun org-file-menu-entry (file)
16383 (vector file (list 'find-file file) t))
16385 (defun org-check-agenda-file (file)
16386 "Make sure FILE exists. If not, ask user what to do."
16387 (when (not (file-exists-p file))
16388 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
16389 (abbreviate-file-name file))
16390 (let ((r (downcase (read-char-exclusive))))
16393 (org-remove-file file)
16394 (throw 'nextfile t))
16395 (t (error "Abort"))))))
16397 (defun org-get-agenda-file-buffer (file)
16398 "Get a buffer visiting FILE. If the buffer needs to be created, add
16399 it to the list of buffers which might be released later."
16400 (let ((buf (org-find-base-buffer-visiting file)))
16402 buf ; just return it
16403 ;; Make a new buffer and remember it
16404 (setq buf (find-file-noselect file))
16405 (if buf (push buf org-agenda-new-buffers))
16408 (defun org-release-buffers (blist)
16409 "Release all buffers in list, asking the user for confirmation when needed.
16410 When a buffer is unmodified, it is just killed. When modified, it is saved
16411 \(if the user agrees) and then killed."
16413 (while (setq buf (pop blist))
16414 (setq file (buffer-file-name buf))
16415 (when (and (buffer-modified-p buf)
16417 (y-or-n-p (format "Save file %s? " file)))
16418 (with-current-buffer buf (save-buffer)))
16419 (kill-buffer buf))))
16421 (defun org-prepare-agenda-buffers (files)
16422 "Create buffers for all agenda files, protect archived trees and comments."
16424 (let ((pa '(:org-archived t))
16425 (pc '(:org-comment t))
16426 (pall '(:org-archived t :org-comment t))
16427 (inhibit-read-only t)
16428 (rea (concat ":" org-archive-tag ":"))
16432 (while (setq file (pop files))
16436 (org-check-agenda-file file)
16437 (set-buffer (org-get-agenda-file-buffer file)))
16439 (setq bmp (buffer-modified-p))
16440 (org-refresh-category-properties)
16441 (setq org-todo-keywords-for-agenda
16442 (append org-todo-keywords-for-agenda org-todo-keywords-1))
16443 (setq org-done-keywords-for-agenda
16444 (append org-done-keywords-for-agenda org-done-keywords))
16445 (setq org-todo-keyword-alist-for-agenda
16446 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
16447 (setq org-drawers-for-agenda
16448 (append org-drawers-for-agenda org-drawers))
16449 (setq org-tag-alist-for-agenda
16450 (append org-tag-alist-for-agenda org-tag-alist))
16453 (remove-text-properties (point-min) (point-max) pall)
16454 (when org-agenda-skip-archived-trees
16455 (goto-char (point-min))
16456 (while (re-search-forward rea nil t)
16457 (if (org-on-heading-p t)
16458 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
16459 (goto-char (point-min))
16460 (setq re (format org-heading-keyword-regexp-format
16461 org-comment-string))
16462 (while (re-search-forward re nil t)
16463 (add-text-properties
16464 (match-beginning 0) (org-end-of-subtree t) pc)))
16465 (set-buffer-modified-p bmp)))))
16466 (setq org-todo-keywords-for-agenda
16467 (org-uniquify org-todo-keywords-for-agenda))
16468 (setq org-todo-keyword-alist-for-agenda
16469 (org-uniquify org-todo-keyword-alist-for-agenda)
16470 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
16472 ;;;; Embedded LaTeX
16474 (defvar org-cdlatex-mode-map (make-sparse-keymap)
16475 "Keymap for the minor `org-cdlatex-mode'.")
16477 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
16478 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
16479 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
16480 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
16481 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
16483 (defvar org-cdlatex-texmathp-advice-is-done nil
16484 "Flag remembering if we have applied the advice to texmathp already.")
16486 (define-minor-mode org-cdlatex-mode
16487 "Toggle the minor `org-cdlatex-mode'.
16488 This mode supports entering LaTeX environment and math in LaTeX fragments
16490 \\{org-cdlatex-mode-map}"
16492 (when org-cdlatex-mode
16494 (run-hooks 'cdlatex-mode-hook)
16495 (cdlatex-compute-tables))
16496 (unless org-cdlatex-texmathp-advice-is-done
16497 (setq org-cdlatex-texmathp-advice-is-done t)
16498 (defadvice texmathp (around org-math-always-on activate)
16499 "Always return t in org-mode buffers.
16500 This is because we want to insert math symbols without dollars even outside
16501 the LaTeX math segments. If Orgmode thinks that point is actually inside
16502 an embedded LaTeX fragment, let texmathp do its job.
16503 \\[org-cdlatex-mode-map]"
16507 ((not (eq major-mode 'org-mode)) ad-do-it)
16508 ((eq this-command 'cdlatex-math-symbol)
16509 (setq ad-return-value t
16510 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
16512 (let ((p (org-inside-LaTeX-fragment-p)))
16513 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
16514 (setq ad-return-value t
16515 texmathp-why '("Org-mode embedded math" . 0))
16516 (if p ad-do-it)))))))))
16518 (defun turn-on-org-cdlatex ()
16519 "Unconditionally turn on `org-cdlatex-mode'."
16520 (org-cdlatex-mode 1))
16522 (defun org-inside-LaTeX-fragment-p ()
16523 "Test if point is inside a LaTeX fragment.
16524 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
16525 sequence appearing also before point.
16526 Even though the matchers for math are configurable, this function assumes
16527 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
16528 delimiters are skipped when they have been removed by customization.
16529 The return value is nil, or a cons cell with the delimiter and the
16530 position of this delimiter.
16532 This function does a reasonably good job, but can locally be fooled by
16533 for example currency specifications. For example it will assume being in
16534 inline math after \"$22.34\". The LaTeX fragment formatter will only format
16535 fragments that are properly closed, but during editing, we have to live
16536 with the uncertainty caused by missing closing delimiters. This function
16537 looks only before point, not after."
16539 (let ((pos (point))
16540 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
16542 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
16544 dd-on str (start 0) m re)
16547 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
16548 re (nth 1 (assoc "$" org-latex-regexps)))
16549 (while (string-match re str start)
16551 ((= (match-end 0) (length str))
16552 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
16553 ((= (match-end 0) (- (length str) 5))
16555 (t (setq start (match-end 0))))))
16556 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
16558 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
16559 (and (match-beginning 2) (throw 'exit nil))
16561 (while (re-search-backward "\\$\\$" lim t)
16562 (setq dd-on (not dd-on)))
16564 (if dd-on (cons "$$" m))))))
16566 (defun org-inside-latex-macro-p ()
16567 "Is point inside a LaTeX macro or its arguments?"
16570 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
16572 (defun org-try-cdlatex-tab ()
16573 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
16574 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
16575 - inside a LaTeX fragment, or
16576 - after the first word in a line, where an abbreviation expansion could
16577 insert a LaTeX environment."
16578 (when org-cdlatex-mode
16580 ;; Before any word on the line: No expansion possible.
16581 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
16582 ;; Just after first word on the line: Expand it. Make sure it
16583 ;; cannot happen on headlines, though.
16585 (skip-chars-backward "a-zA-Z0-9*")
16586 (skip-chars-backward " \t")
16587 (and (bolp) (not (org-at-heading-p))))
16589 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
16591 (defun org-cdlatex-underscore-caret (&optional arg)
16592 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
16593 Revert to the normal definition outside of these fragments."
16595 (if (org-inside-LaTeX-fragment-p)
16596 (call-interactively 'cdlatex-sub-superscript)
16597 (let (org-cdlatex-mode)
16598 (call-interactively (key-binding (vector last-input-event))))))
16600 (defun org-cdlatex-math-modify (&optional arg)
16601 "Execute `cdlatex-math-modify' in LaTeX fragments.
16602 Revert to the normal definition outside of these fragments."
16604 (if (org-inside-LaTeX-fragment-p)
16605 (call-interactively 'cdlatex-math-modify)
16606 (let (org-cdlatex-mode)
16607 (call-interactively (key-binding (vector last-input-event))))))
16609 (defvar org-latex-fragment-image-overlays nil
16610 "List of overlays carrying the images of latex fragments.")
16611 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
16613 (defun org-remove-latex-fragment-image-overlays ()
16614 "Remove all overlays with LaTeX fragment images in current buffer."
16615 (mapc 'delete-overlay org-latex-fragment-image-overlays)
16616 (setq org-latex-fragment-image-overlays nil))
16618 (defun org-preview-latex-fragment (&optional subtree)
16619 "Preview the LaTeX fragment at point, or all locally or globally.
16620 If the cursor is in a LaTeX fragment, create the image and overlay
16621 it over the source code. If there is no fragment at point, display
16622 all fragments in the current text, from one headline to the next. With
16623 prefix SUBTREE, display all fragments in the current subtree. With a
16624 double prefix arg \\[universal-argument] \\[universal-argument], or when \
16625 the cursor is before the first headline,
16626 display all fragments in the buffer.
16627 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
16629 (org-remove-latex-fragment-image-overlays)
16632 (let (beg end at msg)
16634 ((or (equal subtree '(16))
16635 (not (save-excursion
16636 (re-search-backward org-outline-regexp-bol nil t))))
16637 (setq beg (point-min) end (point-max)
16638 msg "Creating images for buffer...%s"))
16639 ((equal subtree '(4))
16640 (org-back-to-heading)
16641 (setq beg (point) end (org-end-of-subtree t)
16642 msg "Creating images for subtree...%s"))
16644 (if (setq at (org-inside-LaTeX-fragment-p))
16645 (goto-char (max (point-min) (- (cdr at) 2)))
16646 (org-back-to-heading))
16647 (setq beg (point) end (progn (outline-next-heading) (point))
16648 msg (if at "Creating image...%s"
16649 "Creating images for entry...%s"))))
16651 (narrow-to-region beg end)
16654 (concat "ltxpng/" (file-name-sans-extension
16655 (file-name-nondirectory
16656 buffer-file-name)))
16657 default-directory 'overlays msg at 'forbuffer 'dvipng)
16658 (message msg "done. Use `C-c C-c' to remove images.")))))
16660 (defvar org-latex-regexps
16661 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
16662 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
16663 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
16664 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
16665 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
16666 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
16667 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
16668 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
16669 "Regular expressions for matching embedded LaTeX.")
16671 (defvar org-export-have-math nil) ;; dynamic scoping
16672 (defun org-format-latex (prefix &optional dir overlays msg at
16673 forbuffer processing-type)
16674 "Replace LaTeX fragments with links to an image, and produce images.
16675 Some of the options can be changed using the variable
16676 `org-format-latex-options'."
16677 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
16678 (let* ((prefixnodir (file-name-nondirectory prefix))
16679 (absprefix (expand-file-name prefix dir))
16680 (todir (file-name-directory absprefix))
16681 (opt org-format-latex-options)
16682 (matchers (plist-get opt :matchers))
16683 (re-list org-latex-regexps)
16684 (org-format-latex-header-extra
16685 (plist-get (org-infile-export-plist) :latex-header-extra))
16686 (cnt 0) txt hash link beg end re e checkdir
16687 executables-checked string
16688 m n block-type block linkfile movefile ov)
16689 ;; Check the different regular expressions
16690 (while (setq e (pop re-list))
16691 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
16692 block (if block-type "\n\n" ""))
16693 (when (member m matchers)
16694 (goto-char (point-min))
16695 (while (re-search-forward re nil t)
16696 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
16697 (not (get-text-property (match-beginning n)
16700 (not (eq (get-char-property (match-beginning n)
16702 'org-latex-overlay))))
16703 (setq org-export-have-math t)
16705 ((eq processing-type 'verbatim)
16706 ;; Leave the text verbatim, just protect it
16707 (add-text-properties (match-beginning n) (match-end n)
16708 '(org-protected t)))
16709 ((eq processing-type 'mathjax)
16710 ;; Prepare for MathJax processing
16711 (setq string (match-string n))
16712 (if (member m '("$" "$1"))
16714 (delete-region (match-beginning n) (match-end n))
16715 (goto-char (match-beginning n))
16716 (insert (org-add-props (concat "\\(" (substring string 1 -1)
16718 '(org-protected t))))
16719 (add-text-properties (match-beginning n) (match-end n)
16720 '(org-protected t))))
16721 ((eq processing-type 'dvipng)
16722 ;; Process to an image
16723 (setq txt (match-string n)
16724 beg (match-beginning n) end (match-end n)
16726 (let (print-length print-level) ; make sure full list is printed
16727 (setq hash (sha1 (prin1-to-string
16728 (list org-format-latex-header
16729 org-format-latex-header-extra
16730 org-export-latex-default-packages-alist
16731 org-export-latex-packages-alist
16732 org-format-latex-options
16734 linkfile (format "%s_%s.png" prefix hash)
16735 movefile (format "%s_%s.png" absprefix hash)))
16736 (setq link (concat block "[[file:" linkfile "]]" block))
16737 (if msg (message msg cnt))
16739 (unless checkdir ; make sure the directory exists
16741 (or (file-directory-p todir) (make-directory todir t)))
16743 (unless executables-checked
16744 (org-check-external-command
16745 "latex" "needed to convert LaTeX fragments to images")
16746 (org-check-external-command
16747 "dvipng" "needed to convert LaTeX fragments to images")
16748 (setq executables-checked t))
16750 (unless (file-exists-p movefile)
16751 (org-create-formula-image
16752 txt movefile opt forbuffer))
16756 (if (eq (overlay-get o 'org-overlay-type)
16757 'org-latex-overlay)
16758 (delete-overlay o)))
16759 (overlays-in beg end))
16760 (setq ov (make-overlay beg end))
16761 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
16762 (if (featurep 'xemacs)
16764 (overlay-put ov 'invisible t)
16767 (make-glyph (vector 'png :file movefile))))
16770 (list 'image :type 'png :file movefile :ascent 'center)))
16771 (push ov org-latex-fragment-image-overlays)
16773 (delete-region beg end)
16774 (insert (org-add-props link
16775 (list 'org-latex-src
16776 (replace-regexp-in-string
16778 'org-latex-src-embed-type
16779 (if block-type 'paragraph 'character))))))
16780 ((eq processing-type 'mathml)
16781 ;; Process to MathML
16782 (unless executables-checked
16783 (unless (save-match-data (org-format-latex-mathml-available-p))
16784 (error "LaTeX to MathML converter not configured"))
16785 (setq executables-checked t))
16786 (setq txt (match-string n)
16787 beg (match-beginning n) end (match-end n)
16789 (if msg (message msg cnt))
16791 (delete-region beg end)
16792 (insert (org-format-latex-as-mathml
16793 txt block-type prefix dir)))
16795 (error "Unknown conversion type %s for latex fragments"
16796 processing-type)))))))))
16798 (defun org-create-math-formula (latex-frag &optional mathml-file)
16799 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
16800 Use `org-latex-to-mathml-convert-command'. If the conversion is
16801 sucessful, return the portion between \"<math...> </math>\"
16802 elements otherwise return nil. When MATHML-FILE is specified,
16803 write the results in to that file. When invoked as an
16804 interactive command, prompt for LATEX-FRAG, with initial value
16805 set to the current active region and echo the results for user
16807 (interactive (list (let ((frag (when (region-active-p)
16808 (buffer-substring-no-properties
16809 (region-beginning) (region-end)))))
16810 (read-string "LaTeX Fragment: " frag nil frag))))
16811 (unless latex-frag (error "Invalid latex-frag"))
16812 (let* ((tmp-in-file (file-relative-name
16813 (make-temp-name (expand-file-name "ltxmathml-in"))))
16814 (ignore (write-region latex-frag nil tmp-in-file))
16815 (tmp-out-file (file-relative-name
16816 (make-temp-name (expand-file-name "ltxmathml-out"))))
16818 org-latex-to-mathml-convert-command
16819 `((?j . ,(shell-quote-argument
16820 (expand-file-name org-latex-to-mathml-jar-file)))
16821 (?I . ,(shell-quote-argument tmp-in-file))
16822 (?o . ,(shell-quote-argument tmp-out-file)))))
16823 mathml shell-command-output)
16824 (when (org-called-interactively-p 'any)
16825 (unless (org-format-latex-mathml-available-p)
16826 (error "LaTeX to MathML converter not configured")))
16827 (message "Running %s" cmd)
16828 (setq shell-command-output (shell-command-to-string cmd))
16830 (when (file-readable-p tmp-out-file)
16831 (with-current-buffer (find-file-noselect tmp-out-file t)
16832 (goto-char (point-min))
16833 (when (re-search-forward
16836 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
16838 (regexp-quote "</math>")) nil t)
16839 (prog1 (match-string 0) (kill-buffer))))))
16843 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
16845 (write-region mathml nil mathml-file))
16846 (when (org-called-interactively-p 'any)
16848 ((message "LaTeX to MathML conversion failed")
16849 (message shell-command-output)))
16850 (delete-file tmp-in-file)
16851 (when (file-exists-p tmp-out-file)
16852 (delete-file tmp-out-file))
16855 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
16856 prefix &optional dir)
16857 "Use `org-create-math-formula' but check local cache first."
16858 (let* ((absprefix (expand-file-name prefix dir))
16859 (print-length nil) (print-level nil)
16860 (formula-id (concat
16865 org-latex-to-mathml-convert-command)))))
16866 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
16867 (formula-cache-dir (file-name-directory formula-cache)))
16869 (unless (file-directory-p formula-cache-dir)
16870 (make-directory formula-cache-dir t))
16872 (unless (file-exists-p formula-cache)
16873 (org-create-math-formula latex-frag formula-cache))
16875 (if (file-exists-p formula-cache)
16876 ;; Successful conversion. Return the link to MathML file.
16878 (format "[[file:%s]]" (file-relative-name formula-cache dir))
16879 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
16880 'org-latex-src-embed-type (if latex-frag-type
16881 'paragraph 'character)))
16882 ;; Failed conversion. Return the LaTeX fragment verbatim
16883 (add-text-properties
16884 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
16887 ;; This function borrows from Ganesh Swami's latex2png.el
16888 (defun org-create-formula-image (string tofile options buffer)
16889 "This calls dvipng."
16890 (require 'org-latex)
16891 (let* ((tmpdir (if (featurep 'xemacs)
16893 temporary-file-directory))
16894 (texfilebase (make-temp-name
16895 (expand-file-name "orgtex" tmpdir)))
16896 (texfile (concat texfilebase ".tex"))
16897 (dvifile (concat texfilebase ".dvi"))
16898 (pngfile (concat texfilebase ".png"))
16899 (fnh (if (featurep 'xemacs)
16900 (font-height (get-face-font 'default))
16901 (face-attribute 'default :height nil)))
16902 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
16903 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
16904 (fg (or (plist-get options (if buffer :foreground :html-foreground))
16906 (bg (or (plist-get options (if buffer :background :html-background))
16908 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
16909 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
16910 (with-temp-file texfile
16911 (insert (org-splice-latex-header
16912 org-format-latex-header
16913 org-export-latex-default-packages-alist
16914 org-export-latex-packages-alist t
16915 org-format-latex-header-extra))
16916 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
16917 (require 'org-latex)
16918 (org-export-latex-fix-inputenc))
16919 (let ((dir default-directory))
16920 (condition-case nil
16923 (call-process "latex" nil nil nil texfile))
16926 (if (not (file-exists-p dvifile))
16927 (progn (message "Failed to create dvi file from %s" texfile) nil)
16928 (condition-case nil
16929 (call-process "dvipng" nil nil nil
16932 ;;"-x" scale "-y" scale
16937 (if (not (file-exists-p pngfile))
16938 (if org-format-latex-signal-error
16939 (error "Failed to create png file from %s" texfile)
16940 (message "Failed to create png file from %s" texfile)
16942 ;; Use the requested file name and clean up
16943 (copy-file pngfile tofile 'replace)
16944 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
16945 (delete-file (concat texfilebase e)))
16948 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
16949 "Fill a LaTeX header template TPL.
16950 In the template, the following place holders will be recognized:
16952 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
16953 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
16954 [PACKAGES] \\usepackage statements for PKG
16955 [NO-PACKAGES] do not include PKG
16956 [EXTRA] the string EXTRA
16957 [NO-EXTRA] do not include EXTRA
16959 For backward compatibility, if both the positive and the negative place
16960 holder is missing, the positive one (without the \"NO-\") will be
16961 assumed to be present at the end of the template.
16962 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
16964 SNIPPETS-P indicates if this is run to create snippet images for HTML."
16965 (let (rpl (end ""))
16966 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
16967 (setq rpl (if (or (match-end 1) (not def-pkg))
16968 "" (org-latex-packages-to-string def-pkg snippets-p t))
16969 tpl (replace-match rpl t t tpl))
16970 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
16972 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
16973 (setq rpl (if (or (match-end 1) (not pkg))
16974 "" (org-latex-packages-to-string pkg snippets-p t))
16975 tpl (replace-match rpl t t tpl))
16978 (org-latex-packages-to-string pkg snippets-p)))))
16980 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
16981 (setq rpl (if (or (match-end 1) (not extra))
16982 "" (concat extra "\n"))
16983 tpl (replace-match rpl t t tpl))
16984 (if (and extra (string-match "\\S-" extra))
16985 (setq end (concat end "\n" extra))))
16987 (if (string-match "\\S-" end)
16988 (concat tpl "\n" end)
16991 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
16992 "Turn an alist of packages into a string with the \\usepackage macros."
16993 (setq pkg (mapconcat (lambda(p)
16996 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
16997 (format "%% Package %s omitted" (cadr p)))
16998 ((equal "" (car p))
16999 (format "\\usepackage{%s}" (cadr p)))
17001 (format "\\usepackage[%s]{%s}"
17002 (car p) (cadr p)))))
17005 (if newline (concat pkg "\n") pkg))
17007 (defun org-dvipng-color (attr)
17008 "Return an rgb color specification for dvipng."
17009 (apply 'format "rgb %s %s %s"
17010 (mapcar 'org-normalize-color
17011 (color-values (face-attribute 'default attr nil)))))
17013 (defun org-normalize-color (value)
17014 "Return string to be used as color value for an RGB component."
17015 (format "%g" (/ value 65535.0)))
17020 (defvar org-inline-image-overlays nil)
17021 (make-variable-buffer-local 'org-inline-image-overlays)
17023 (defun org-toggle-inline-images (&optional include-linked)
17024 "Toggle the display of inline images.
17025 INCLUDE-LINKED is passed to `org-display-inline-images'."
17027 (if org-inline-image-overlays
17029 (org-remove-inline-images)
17030 (message "Inline image display turned off"))
17031 (org-display-inline-images include-linked)
17032 (if org-inline-image-overlays
17033 (message "%d images displayed inline"
17034 (length org-inline-image-overlays))
17035 (message "No images to display inline"))))
17037 (defun org-display-inline-images (&optional include-linked refresh beg end)
17038 "Display inline images.
17039 Normally only links without a description part are inlined, because this
17040 is how it will work for export. When INCLUDE-LINKED is set, also links
17041 with a description part will be inlined. This can be nice for a quick
17042 look at those images, but it does not reflect what exported files will look
17044 When REFRESH is set, refresh existing images between BEG and END.
17045 This will create new image displays only if necessary.
17046 BEG and END default to the buffer boundaries."
17049 (org-remove-inline-images)
17050 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17054 (setq beg (or beg (point-min)) end (or end (point-max)))
17055 (goto-char (point-min))
17056 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17057 (substring (org-image-file-name-regexp) 0 -2)
17058 "\\)\\]" (if include-linked "" "\\]")))
17060 (while (re-search-forward re end t)
17061 (setq old (get-char-property-and-overlay (match-beginning 1)
17062 'org-image-overlay))
17063 (setq file (expand-file-name
17064 (concat (or (match-string 3) "") (match-string 4))))
17065 (when (file-exists-p file)
17066 (if (and (car-safe old) refresh)
17067 (image-refresh (overlay-get (cdr old) 'display))
17068 (setq img (save-match-data (create-image file)))
17070 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17071 (overlay-put ov 'display img)
17072 (overlay-put ov 'face 'default)
17073 (overlay-put ov 'org-image-overlay t)
17074 (overlay-put ov 'modification-hooks
17075 (list 'org-display-inline-modification-hook))
17076 (push ov org-inline-image-overlays)))))))))
17078 (defun org-display-inline-modification-hook (ov after beg end &optional len)
17079 "Remove inline-display overlay if a corresponding region is modified."
17080 (let ((inhibit-modification-hooks t))
17081 (when (and ov after)
17082 (delete ov org-inline-image-overlays)
17083 (delete-overlay ov))))
17085 (defun org-remove-inline-images ()
17086 "Remove inline display of images."
17088 (mapc 'delete-overlay org-inline-image-overlays)
17089 (setq org-inline-image-overlays nil))
17093 ;; Outline functions from `outline-mode-prefix-map'
17094 ;; that can be remapped in Org:
17095 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17096 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17097 (define-key org-mode-map [remap outline-forward-same-level]
17098 'org-forward-same-level)
17099 (define-key org-mode-map [remap outline-backward-same-level]
17100 'org-backward-same-level)
17101 (define-key org-mode-map [remap show-branches]
17102 'org-kill-note-or-show-branches)
17103 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17104 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17105 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17107 ;; Outline functions from `outline-mode-prefix-map'
17108 ;; that can not be remapped in Org:
17109 ;; - the column "key binding" shows whether the Outline function is still
17110 ;; available in Org mode on the same key that it has been bound to in
17112 ;; - "overridden": key used for a different functionality in Org mode
17113 ;; - else: key still bound to the same Outline function in Org mode
17114 ;; | Outline function | key binding | Org replacement |
17115 ;; |------------------------------------+-------------+-----------------------|
17116 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17117 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17118 ;; | `show-children' | `C-c C-i' | visibility cycling |
17119 ;; | `hide-subtree' | overridden | visibility cycling |
17120 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17121 ;; | `hide-body' | overridden | no replacement |
17122 ;; | `show-all' | overridden | no replacement |
17123 ;; | `hide-entry' | overridden | visibility cycling |
17124 ;; | `show-entry' | overridden | no replacement |
17125 ;; | `hide-leaves' | overridden | no replacement |
17126 ;; | `hide-sublevels' | overridden | no replacement |
17127 ;; | `hide-other' | overridden | no replacement |
17128 ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
17129 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17131 ;; Make `C-c C-x' a prefix key
17132 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17134 ;; TAB key with modifiers
17135 (org-defkey org-mode-map "\C-i" 'org-cycle)
17136 (org-defkey org-mode-map [(tab)] 'org-cycle)
17137 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17138 (org-defkey org-mode-map [(meta tab)] 'pcomplete)
17139 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17140 (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
17141 ;; The following line is necessary under Suse GNU/Linux
17142 (unless (featurep 'xemacs)
17143 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17144 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17145 (define-key org-mode-map [backtab] 'org-shifttab)
17147 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17148 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17149 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17151 ;; Cursor keys with modifiers
17152 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17153 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17154 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17155 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17157 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17158 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17159 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17160 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17162 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17163 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17164 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17165 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17167 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17168 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17169 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17170 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17173 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17174 (mapc (lambda (pair)
17175 (define-key org-babel-map (car pair) (cdr pair)))
17176 org-babel-key-bindings)
17178 ;;; Extra keys for tty access.
17179 ;; We only set them when really needed because otherwise the
17180 ;; menus don't show the simple keys
17182 (when (or org-use-extra-keys
17183 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17184 (not window-system))
17185 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17186 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17187 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17188 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17189 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17190 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17191 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17192 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17193 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17194 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17195 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17196 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17197 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17198 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17199 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17200 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17201 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17202 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17203 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17204 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17205 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17206 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17207 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17208 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17209 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17210 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17211 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17212 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17214 ;; All the other keys
17216 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17217 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17218 (if (boundp 'narrow-map)
17219 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17220 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17221 (if (boundp 'narrow-map)
17222 (org-defkey narrow-map "b" 'org-narrow-to-block)
17223 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17224 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
17225 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
17226 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17227 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17228 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17229 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17230 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17231 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17232 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17233 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17234 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17235 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
17236 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
17237 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
17238 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
17239 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
17240 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
17241 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
17242 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
17243 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
17244 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
17245 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
17246 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
17247 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
17248 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
17249 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
17250 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
17251 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
17252 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
17253 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
17254 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
17255 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
17256 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
17257 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
17258 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
17259 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
17260 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
17261 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
17262 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
17263 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
17264 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
17265 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17266 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
17267 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
17268 (org-defkey org-mode-map "\C-c^" 'org-sort)
17269 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
17270 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
17271 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
17272 (org-defkey org-mode-map "\C-m" 'org-return)
17273 (org-defkey org-mode-map "\C-j" 'org-return-indent)
17274 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
17275 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
17276 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
17277 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
17278 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
17279 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
17280 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
17281 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
17282 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
17283 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
17284 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
17285 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
17286 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
17287 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
17288 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
17289 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
17290 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
17291 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
17292 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
17293 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
17294 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
17296 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
17297 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
17298 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
17299 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
17301 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
17302 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
17303 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
17304 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
17305 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
17306 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
17307 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
17308 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
17309 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
17310 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
17311 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
17312 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
17313 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
17314 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
17315 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
17316 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
17317 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
17318 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
17320 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
17321 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
17322 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
17323 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
17324 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
17326 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
17328 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
17330 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
17331 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
17333 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
17336 (when (featurep 'xemacs)
17337 (org-defkey org-mode-map 'button3 'popup-mode-menu))
17340 (defconst org-speed-commands-default
17342 ("Outline Navigation")
17343 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
17344 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
17345 ("f" . (org-speed-move-safe 'org-forward-same-level))
17346 ("b" . (org-speed-move-safe 'org-backward-same-level))
17347 ("u" . (org-speed-move-safe 'outline-up-heading))
17349 ("g" . (org-refile t))
17350 ("Outline Visibility")
17352 ("C" . org-shifttab)
17353 (" " . org-display-outline-path)
17354 ("Outline Structure Editing")
17355 ("U" . org-shiftmetaup)
17356 ("D" . org-shiftmetadown)
17357 ("r" . org-metaright)
17358 ("l" . org-metaleft)
17359 ("R" . org-shiftmetaright)
17360 ("L" . org-shiftmetaleft)
17361 ("i" . (progn (forward-char 1) (call-interactively
17362 'org-insert-heading-respect-content)))
17365 ("a" . org-archive-subtree-default-with-confirmation)
17366 ("." . org-mark-subtree)
17368 ("I" . org-clock-in)
17369 ("O" . org-clock-out)
17370 ("Meta Data Editing")
17372 ("0" . (org-priority ?\ ))
17373 ("1" . (org-priority ?A))
17374 ("2" . (org-priority ?B))
17375 ("3" . (org-priority ?C))
17376 (";" . org-set-tags-command)
17377 ("e" . org-set-effort)
17378 ("Agenda Views etc")
17380 ("/" . org-sparse-tree)
17382 ("o" . org-open-at-point)
17383 ("?" . org-speed-command-help)
17384 ("<" . (org-agenda-set-restriction-lock 'subtree))
17385 (">" . (org-agenda-remove-restriction-lock))
17387 "The default speed commands.")
17389 (defun org-print-speed-command (e)
17390 (if (> (length (car e)) 1)
17395 (princ (make-string (length (car e)) ?-))
17399 (if (symbolp (cdr e))
17400 (princ (symbol-name (cdr e)))
17404 (defun org-speed-command-help ()
17405 "Show the available speed commands."
17407 (if (not org-use-speed-commands)
17408 (error "Speed commands are not activated, customize `org-use-speed-commands'")
17409 (with-output-to-temp-buffer "*Help*"
17410 (princ "User-defined Speed commands\n===========================\n")
17411 (mapc 'org-print-speed-command org-speed-commands-user)
17413 (princ "Built-in Speed commands\n=======================\n")
17414 (mapc 'org-print-speed-command org-speed-commands-default))
17415 (with-current-buffer "*Help*"
17416 (setq truncate-lines t))))
17418 (defun org-speed-move-safe (cmd)
17419 "Execute CMD, but make sure that the cursor always ends up in a headline.
17420 If not, return to the original position and throw an error."
17422 (let ((pos (point)))
17423 (call-interactively cmd)
17424 (unless (and (bolp) (org-on-heading-p))
17426 (error "Boundary reached while executing %s" cmd))))
17428 (defvar org-self-insert-command-undo-counter 0)
17430 (defvar org-table-auto-blank-field) ; defined in org-table.el
17431 (defvar org-speed-command nil)
17433 (defun org-speed-command-default-hook (keys)
17434 "Hook for activating single-letter speed commands.
17435 `org-speed-commands-default' specifies a minimal command set.
17436 Use `org-speed-commands-user' for further customization."
17437 (when (or (and (bolp) (looking-at org-outline-regexp))
17438 (and (functionp org-use-speed-commands)
17439 (funcall org-use-speed-commands)))
17440 (cdr (assoc keys (append org-speed-commands-user
17441 org-speed-commands-default)))))
17443 (defun org-babel-speed-command-hook (keys)
17444 "Hook for activating single-letter code block commands."
17445 (when (and (bolp) (looking-at org-babel-src-block-regexp))
17446 (cdr (assoc keys org-babel-key-bindings))))
17448 (defcustom org-speed-command-hook
17449 '(org-speed-command-default-hook org-babel-speed-command-hook)
17450 "Hook for activating speed commands at strategic locations.
17451 Hook functions are called in sequence until a valid handler is
17454 Each hook takes a single argument, a user-pressed command key
17455 which is also a `self-insert-command' from the global map.
17457 Within the hook, examine the cursor position and the command key
17458 and return nil or a valid handler as appropriate. Handler could
17459 be one of an interactive command, a function, or a form.
17461 Set `org-use-speed-commands' to non-nil value to enable this
17462 hook. The default setting is `org-speed-command-default-hook'."
17463 :group 'org-structure
17466 (defun org-self-insert-command (N)
17467 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
17468 If the cursor is in a table looking at whitespace, the whitespace is
17469 overwritten, and the table is not marked as requiring realignment."
17471 (org-check-before-invisible-edit 'insert)
17473 ((and org-use-speed-commands
17474 (setq org-speed-command
17475 (run-hook-with-args-until-success
17476 'org-speed-command-hook (this-command-keys))))
17478 ((commandp org-speed-command)
17479 (setq this-command org-speed-command)
17480 (call-interactively org-speed-command))
17481 ((functionp org-speed-command)
17482 (funcall org-speed-command))
17483 ((and org-speed-command (listp org-speed-command))
17484 (eval org-speed-command))
17485 (t (let (org-use-speed-commands)
17486 (call-interactively 'org-self-insert-command)))))
17490 ;; check if we blank the field, and if that triggers align
17491 (and (featurep 'org-table) org-table-auto-blank-field
17492 (member last-command
17493 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
17494 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
17495 ;; got extra space, this field does not determine column width
17496 (let (org-table-may-need-update) (org-table-blank-field))
17497 ;; no extra space, this field may determine column width
17498 (org-table-blank-field)))
17501 (looking-at "[^|\n]* |"))
17502 (let (org-table-may-need-update)
17503 (goto-char (1- (match-end 0)))
17505 (goto-char (match-beginning 0))
17506 (self-insert-command N)))
17508 (setq org-table-may-need-update t)
17509 (self-insert-command N)
17510 (org-fix-tags-on-the-fly)
17511 (if org-self-insert-cluster-for-undo
17512 (if (not (eq last-command 'org-self-insert-command))
17513 (setq org-self-insert-command-undo-counter 1)
17514 (if (>= org-self-insert-command-undo-counter 20)
17515 (setq org-self-insert-command-undo-counter 1)
17516 (and (> org-self-insert-command-undo-counter 0)
17517 buffer-undo-list (listp buffer-undo-list)
17518 (not (cadr buffer-undo-list)) ; remove nil entry
17519 (setcdr buffer-undo-list (cddr buffer-undo-list)))
17520 (setq org-self-insert-command-undo-counter
17521 (1+ org-self-insert-command-undo-counter))))))))
17523 (defun org-check-before-invisible-edit (kind)
17524 "Check is editing if kind KIND would be dangerous with invisible text around.
17525 The detailed reaction depends on the user option `org-catch-invisible-edits'."
17526 ;; First, try to get out of here as quickly as possible, to reduce overhead
17527 (if (and org-catch-invisible-edits
17528 (or (not (boundp 'visible-mode)) (not visible-mode))
17529 (or (get-char-property (point) 'invisible)
17530 (get-char-property (max (point-min) (1- (point))) 'invisible)))
17531 ;; OK, we need to take a closer look
17532 (let* ((invisible-at-point (get-char-property (point) 'invisible))
17533 (invisible-before-point (if (bobp) nil (get-char-property
17534 (1- (point)) 'invisible)))
17535 (border-and-ok-direction
17537 ;; Check if we are acting predictably before invisible text
17538 (and invisible-at-point (not invisible-before-point)
17539 (memq kind '(insert delete-backward)))
17540 ;; Check if we are acting predictably after invisible text
17541 ;; This works not well, and I have turned it off. It seems
17542 ;; better to always show and stop after invisible text.
17543 ;; (and (not invisible-at-point) invisible-before-point
17544 ;; (memq kind '(insert delete)))
17547 (when (or (memq invisible-at-point '(outline org-hide-block))
17548 (memq invisible-before-point '(outline org-hide-block)))
17549 (if (eq org-catch-invisible-edits 'error)
17550 (error "Editing in invisible areas is prohibited - make visible first"))
17551 ;; Make the area visible
17553 (if invisible-before-point
17554 (goto-char (previous-single-char-property-change
17555 (point) 'invisible)))
17558 ((eq org-catch-invisible-edits 'show)
17559 ;; That's it, we do the edit after showing
17561 "Unfolding invisible region around point before editing")
17563 ((and (eq org-catch-invisible-edits 'smart)
17564 border-and-ok-direction)
17565 (message "Unfolding invisible region around point before editing"))
17567 ;; Don't do the edit, make the user repeat it in full visibility
17568 (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
17570 (defun org-fix-tags-on-the-fly ()
17571 (when (and (equal (char-after (point-at-bol)) ?*)
17572 (org-on-heading-p))
17573 (org-align-tags-here org-tags-column)))
17575 (defun org-delete-backward-char (N)
17576 "Like `delete-backward-char', insert whitespace at field end in tables.
17577 When deleting backwards, in tables this function will insert whitespace in
17578 front of the next \"|\" separator, to keep the table aligned. The table will
17579 still be marked for re-alignment if the field did fill the entire column,
17580 because, in this case the deletion might narrow the column."
17582 (org-check-before-invisible-edit 'delete-backward)
17583 (if (and (org-table-p)
17585 (string-match "|" (buffer-substring (point-at-bol) (point)))
17586 (looking-at ".*?|"))
17587 (let ((pos (point))
17588 (noalign (looking-at "[^|\n\r]* |"))
17589 (c org-table-may-need-update))
17590 (backward-delete-char N)
17591 (if (not overwrite-mode)
17593 (skip-chars-forward "^|")
17595 (goto-char (1- pos))))
17596 ;; noalign: if there were two spaces at the end, this field
17597 ;; does not determine the width of the column.
17598 (if noalign (setq org-table-may-need-update c)))
17599 (backward-delete-char N)
17600 (org-fix-tags-on-the-fly)))
17602 (defun org-delete-char (N)
17603 "Like `delete-char', but insert whitespace at field end in tables.
17604 When deleting characters, in tables this function will insert whitespace in
17605 front of the next \"|\" separator, to keep the table aligned. The table will
17606 still be marked for re-alignment if the field did fill the entire column,
17607 because, in this case the deletion might narrow the column."
17609 (org-check-before-invisible-edit 'delete)
17610 (if (and (org-table-p)
17612 (not (= (char-after) ?|))
17614 (if (looking-at ".*?|")
17615 (let ((pos (point))
17616 (noalign (looking-at "[^|\n\r]* |"))
17617 (c org-table-may-need-update))
17618 (replace-match (concat
17619 (substring (match-string 0) 1 -1)
17622 ;; noalign: if there were two spaces at the end, this field
17623 ;; does not determine the width of the column.
17624 (if noalign (setq org-table-may-need-update c)))
17627 (org-fix-tags-on-the-fly)))
17629 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
17630 (put 'org-self-insert-command 'delete-selection t)
17631 (put 'orgtbl-self-insert-command 'delete-selection t)
17632 (put 'org-delete-char 'delete-selection 'supersede)
17633 (put 'org-delete-backward-char 'delete-selection 'supersede)
17634 (put 'org-yank 'delete-selection 'yank)
17636 ;; Make `flyspell-mode' delay after some commands
17637 (put 'org-self-insert-command 'flyspell-delayed t)
17638 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
17639 (put 'org-delete-char 'flyspell-delayed t)
17640 (put 'org-delete-backward-char 'flyspell-delayed t)
17642 ;; Make pabbrev-mode expand after org-mode commands
17643 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
17644 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
17646 ;; How to do this: Measure non-white length of current string
17647 ;; If equal to column width, we should realign.
17649 (defun org-remap (map &rest commands)
17650 "In MAP, remap the functions given in COMMANDS.
17651 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
17654 (setq old (pop commands) new (pop commands))
17655 (if (fboundp 'command-remapping)
17656 (org-defkey map (vector 'remap old) new)
17657 (substitute-key-definition old new map global-map)))))
17659 (when (eq org-enable-table-editor 'optimized)
17660 ;; If the user wants maximum table support, we need to hijack
17661 ;; some standard editing functions
17662 (org-remap org-mode-map
17663 'self-insert-command 'org-self-insert-command
17664 'delete-char 'org-delete-char
17665 'delete-backward-char 'org-delete-backward-char)
17666 (org-defkey org-mode-map "|" 'org-force-self-insert))
17668 (defvar org-ctrl-c-ctrl-c-hook nil
17669 "Hook for functions attaching themselves to `C-c C-c'.
17671 This can be used to add additional functionality to the C-c C-c
17672 key which executes context-dependent commands. This hook is run
17673 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
17674 run after the last test.
17676 Each function will be called with no arguments. The function
17677 must check if the context is appropriate for it to act. If yes,
17678 it should do its thing and then return a non-nil value. If the
17679 context is wrong, just do nothing and return nil.")
17681 (defvar org-ctrl-c-ctrl-c-final-hook nil
17682 "Hook for functions attaching themselves to `C-c C-c'.
17684 This can be used to add additional functionality to the C-c C-c
17685 key which executes context-dependent commands. This hook is run
17686 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
17687 before the first test.
17689 Each function will be called with no arguments. The function
17690 must check if the context is appropriate for it to act. If yes,
17691 it should do its thing and then return a non-nil value. If the
17692 context is wrong, just do nothing and return nil.")
17694 (defvar org-tab-first-hook nil
17695 "Hook for functions to attach themselves to TAB.
17696 See `org-ctrl-c-ctrl-c-hook' for more information.
17697 This hook runs as the first action when TAB is pressed, even before
17698 `org-cycle' messes around with the `outline-regexp' to cater for
17699 inline tasks and plain list item folding.
17700 If any function in this hook returns t, any other actions that
17701 would have been caused by TAB (such as table field motion or visibility
17702 cycling) will not occur.")
17704 (defvar org-tab-after-check-for-table-hook nil
17705 "Hook for functions to attach themselves to TAB.
17706 See `org-ctrl-c-ctrl-c-hook' for more information.
17707 This hook runs after it has been established that the cursor is not in a
17708 table, but before checking if the cursor is in a headline or if global cycling
17710 If any function in this hook returns t, not other actions like visibility
17711 cycling will be done.")
17713 (defvar org-tab-after-check-for-cycling-hook nil
17714 "Hook for functions to attach themselves to TAB.
17715 See `org-ctrl-c-ctrl-c-hook' for more information.
17716 This hook runs after it has been established that not table field motion and
17717 not visibility should be done because of current context. This is probably
17718 the place where a package like yasnippets can hook in.")
17720 (defvar org-tab-before-tab-emulation-hook nil
17721 "Hook for functions to attach themselves to TAB.
17722 See `org-ctrl-c-ctrl-c-hook' for more information.
17723 This hook runs after every other options for TAB have been exhausted, but
17724 before indentation and \t insertion takes place.")
17726 (defvar org-metaleft-hook nil
17727 "Hook for functions attaching themselves to `M-left'.
17728 See `org-ctrl-c-ctrl-c-hook' for more information.")
17729 (defvar org-metaright-hook nil
17730 "Hook for functions attaching themselves to `M-right'.
17731 See `org-ctrl-c-ctrl-c-hook' for more information.")
17732 (defvar org-metaup-hook nil
17733 "Hook for functions attaching themselves to `M-up'.
17734 See `org-ctrl-c-ctrl-c-hook' for more information.")
17735 (defvar org-metadown-hook nil
17736 "Hook for functions attaching themselves to `M-down'.
17737 See `org-ctrl-c-ctrl-c-hook' for more information.")
17738 (defvar org-shiftmetaleft-hook nil
17739 "Hook for functions attaching themselves to `M-S-left'.
17740 See `org-ctrl-c-ctrl-c-hook' for more information.")
17741 (defvar org-shiftmetaright-hook nil
17742 "Hook for functions attaching themselves to `M-S-right'.
17743 See `org-ctrl-c-ctrl-c-hook' for more information.")
17744 (defvar org-shiftmetaup-hook nil
17745 "Hook for functions attaching themselves to `M-S-up'.
17746 See `org-ctrl-c-ctrl-c-hook' for more information.")
17747 (defvar org-shiftmetadown-hook nil
17748 "Hook for functions attaching themselves to `M-S-down'.
17749 See `org-ctrl-c-ctrl-c-hook' for more information.")
17750 (defvar org-metareturn-hook nil
17751 "Hook for functions attaching themselves to `M-RET'.
17752 See `org-ctrl-c-ctrl-c-hook' for more information.")
17753 (defvar org-shiftup-hook nil
17754 "Hook for functions attaching themselves to `S-up'.
17755 See `org-ctrl-c-ctrl-c-hook' for more information.")
17756 (defvar org-shiftup-final-hook nil
17757 "Hook for functions attaching themselves to `S-up'.
17758 This one runs after all other options except shift-select have been excluded.
17759 See `org-ctrl-c-ctrl-c-hook' for more information.")
17760 (defvar org-shiftdown-hook nil
17761 "Hook for functions attaching themselves to `S-down'.
17762 See `org-ctrl-c-ctrl-c-hook' for more information.")
17763 (defvar org-shiftdown-final-hook nil
17764 "Hook for functions attaching themselves to `S-down'.
17765 This one runs after all other options except shift-select have been excluded.
17766 See `org-ctrl-c-ctrl-c-hook' for more information.")
17767 (defvar org-shiftleft-hook nil
17768 "Hook for functions attaching themselves to `S-left'.
17769 See `org-ctrl-c-ctrl-c-hook' for more information.")
17770 (defvar org-shiftleft-final-hook nil
17771 "Hook for functions attaching themselves to `S-left'.
17772 This one runs after all other options except shift-select have been excluded.
17773 See `org-ctrl-c-ctrl-c-hook' for more information.")
17774 (defvar org-shiftright-hook nil
17775 "Hook for functions attaching themselves to `S-right'.
17776 See `org-ctrl-c-ctrl-c-hook' for more information.")
17777 (defvar org-shiftright-final-hook nil
17778 "Hook for functions attaching themselves to `S-right'.
17779 This one runs after all other options except shift-select have been excluded.
17780 See `org-ctrl-c-ctrl-c-hook' for more information.")
17782 (defun org-modifier-cursor-error ()
17783 "Throw an error, a modified cursor command was applied in wrong context."
17784 (error "This command is active in special context like tables, headlines or items"))
17786 (defun org-shiftselect-error ()
17787 "Throw an error because Shift-Cursor command was applied in wrong context."
17788 (if (and (boundp 'shift-select-mode) shift-select-mode)
17789 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
17790 (error "This command works only in special context like headlines or timestamps")))
17792 (defun org-call-for-shift-select (cmd)
17793 (let ((this-command-keys-shift-translated t))
17794 (call-interactively cmd)))
17796 (defun org-shifttab (&optional arg)
17797 "Global visibility cycling or move to previous table field.
17798 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
17800 See the individual commands for more information."
17803 ((org-at-table-p) (call-interactively 'org-table-previous-field))
17805 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
17806 (message "Content view to level: %d" arg)
17807 (org-content (prefix-numeric-value arg2))
17808 (setq org-cycle-global-status 'overview)))
17809 (t (call-interactively 'org-global-cycle))))
17811 (defun org-shiftmetaleft ()
17812 "Promote subtree or delete table column.
17813 Calls `org-promote-subtree', `org-outdent-item',
17814 or `org-table-delete-column', depending on context.
17815 See the individual commands for more information."
17818 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
17819 ((org-at-table-p) (call-interactively 'org-table-delete-column))
17820 ((org-on-heading-p) (call-interactively 'org-promote-subtree))
17821 ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
17822 (t (org-modifier-cursor-error))))
17824 (defun org-shiftmetaright ()
17825 "Demote subtree or insert table column.
17826 Calls `org-demote-subtree', `org-indent-item',
17827 or `org-table-insert-column', depending on context.
17828 See the individual commands for more information."
17831 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
17832 ((org-at-table-p) (call-interactively 'org-table-insert-column))
17833 ((org-on-heading-p) (call-interactively 'org-demote-subtree))
17834 ((org-at-item-p) (call-interactively 'org-indent-item-tree))
17835 (t (org-modifier-cursor-error))))
17837 (defun org-shiftmetaup (&optional arg)
17838 "Move subtree up or kill table row.
17839 Calls `org-move-subtree-up' or `org-table-kill-row' or
17840 `org-move-item-up' depending on context. See the individual commands
17841 for more information."
17844 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
17845 ((org-at-table-p) (call-interactively 'org-table-kill-row))
17846 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
17847 ((org-at-item-p) (call-interactively 'org-move-item-up))
17848 (t (org-modifier-cursor-error))))
17850 (defun org-shiftmetadown (&optional arg)
17851 "Move subtree down or insert table row.
17852 Calls `org-move-subtree-down' or `org-table-insert-row' or
17853 `org-move-item-down', depending on context. See the individual
17854 commands for more information."
17857 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
17858 ((org-at-table-p) (call-interactively 'org-table-insert-row))
17859 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
17860 ((org-at-item-p) (call-interactively 'org-move-item-down))
17861 (t (org-modifier-cursor-error))))
17863 (defsubst org-hidden-tree-error ()
17865 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
17867 (defun org-metaleft (&optional arg)
17868 "Promote heading or move table column to left.
17869 Calls `org-do-promote' or `org-table-move-column', depending on context.
17870 With no specific context, calls the Emacs default `backward-word'.
17871 See the individual commands for more information."
17874 ((run-hook-with-args-until-success 'org-metaleft-hook))
17875 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
17876 ((org-with-limited-levels
17877 (or (org-on-heading-p)
17878 (and (org-region-active-p)
17880 (goto-char (region-beginning))
17881 (org-on-heading-p)))))
17882 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
17883 (call-interactively 'org-do-promote))
17884 ;; At an inline task.
17885 ((org-on-heading-p)
17886 (call-interactively 'org-inlinetask-promote))
17887 ((or (org-at-item-p)
17888 (and (org-region-active-p)
17890 (goto-char (region-beginning))
17892 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
17893 (call-interactively 'org-outdent-item))
17894 (t (call-interactively 'backward-word))))
17896 (defun org-metaright (&optional arg)
17897 "Demote subtree or move table column to right.
17898 Calls `org-do-demote' or `org-table-move-column', depending on context.
17899 With no specific context, calls the Emacs default `forward-word'.
17900 See the individual commands for more information."
17903 ((run-hook-with-args-until-success 'org-metaright-hook))
17904 ((org-at-table-p) (call-interactively 'org-table-move-column))
17905 ((org-with-limited-levels
17906 (or (org-on-heading-p)
17907 (and (org-region-active-p)
17909 (goto-char (region-beginning))
17910 (org-on-heading-p)))))
17911 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
17912 (call-interactively 'org-do-demote))
17913 ;; At an inline task.
17914 ((org-on-heading-p)
17915 (call-interactively 'org-inlinetask-demote))
17916 ((or (org-at-item-p)
17917 (and (org-region-active-p)
17919 (goto-char (region-beginning))
17921 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
17922 (call-interactively 'org-indent-item))
17923 (t (call-interactively 'forward-word))))
17925 (defun org-check-for-hidden (what)
17926 "Check if there are hidden headlines/items in the current visual line.
17927 WHAT can be either `headlines' or `items'. If the current line is
17928 an outline or item heading and it has a folded subtree below it,
17929 this function returns t, nil otherwise."
17931 ((eq what 'headlines) org-outline-regexp-bol)
17932 ((eq what 'items) (org-item-beginning-re))
17933 (t (error "This should not happen"))))
17937 (unless (org-region-active-p)
17938 (setq beg (point-at-bol))
17939 (beginning-of-line 2)
17940 (while (and (not (eobp)) ;; this is like `next-line'
17941 (get-char-property (1- (point)) 'invisible))
17942 (beginning-of-line 2))
17945 (goto-char (point-at-eol))
17946 (setq end (max end (point)))
17947 (while (re-search-forward re end t)
17948 (if (get-char-property (match-beginning 0) 'invisible)
17952 (defun org-metaup (&optional arg)
17953 "Move subtree up or move table row up.
17954 Calls `org-move-subtree-up' or `org-table-move-row' or
17955 `org-move-item-up', depending on context. See the individual commands
17956 for more information."
17959 ((run-hook-with-args-until-success 'org-metaup-hook))
17960 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
17961 ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
17962 ((org-at-item-p) (call-interactively 'org-move-item-up))
17963 (t (transpose-lines 1) (beginning-of-line -1))))
17965 (defun org-metadown (&optional arg)
17966 "Move subtree down or move table row down.
17967 Calls `org-move-subtree-down' or `org-table-move-row' or
17968 `org-move-item-down', depending on context. See the individual
17969 commands for more information."
17972 ((run-hook-with-args-until-success 'org-metadown-hook))
17973 ((org-at-table-p) (call-interactively 'org-table-move-row))
17974 ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
17975 ((org-at-item-p) (call-interactively 'org-move-item-down))
17976 (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
17978 (defun org-shiftup (&optional arg)
17979 "Increase item in timestamp or increase priority of current headline.
17980 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
17981 depending on context. See the individual commands for more information."
17984 ((run-hook-with-args-until-success 'org-shiftup-hook))
17985 ((and org-support-shift-select (org-region-active-p))
17986 (org-call-for-shift-select 'previous-line))
17987 ((org-at-timestamp-p t)
17988 (call-interactively (if org-edit-timestamp-down-means-later
17989 'org-timestamp-down 'org-timestamp-up)))
17990 ((and (not (eq org-support-shift-select 'always))
17991 org-enable-priority-commands
17992 (org-on-heading-p))
17993 (call-interactively 'org-priority-up))
17994 ((and (not org-support-shift-select) (org-at-item-p))
17995 (call-interactively 'org-previous-item))
17996 ((org-clocktable-try-shift 'up arg))
17997 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
17998 (org-support-shift-select
17999 (org-call-for-shift-select 'previous-line))
18000 (t (org-shiftselect-error))))
18002 (defun org-shiftdown (&optional arg)
18003 "Decrease item in timestamp or decrease priority of current headline.
18004 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18005 depending on context. See the individual commands for more information."
18008 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18009 ((and org-support-shift-select (org-region-active-p))
18010 (org-call-for-shift-select 'next-line))
18011 ((org-at-timestamp-p t)
18012 (call-interactively (if org-edit-timestamp-down-means-later
18013 'org-timestamp-up 'org-timestamp-down)))
18014 ((and (not (eq org-support-shift-select 'always))
18015 org-enable-priority-commands
18016 (org-on-heading-p))
18017 (call-interactively 'org-priority-down))
18018 ((and (not org-support-shift-select) (org-at-item-p))
18019 (call-interactively 'org-next-item))
18020 ((org-clocktable-try-shift 'down arg))
18021 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18022 (org-support-shift-select
18023 (org-call-for-shift-select 'next-line))
18024 (t (org-shiftselect-error))))
18026 (defun org-shiftright (&optional arg)
18027 "Cycle the thing at point or in the current line, depending on context.
18028 Depending on context, this does one of the following:
18030 - switch a timestamp at point one day into the future
18031 - on a headline, switch to the next TODO keyword.
18032 - on an item, switch entire list to the next bullet type
18033 - on a property line, switch to the next allowed value
18034 - on a clocktable definition line, move time block into the future"
18037 ((run-hook-with-args-until-success 'org-shiftright-hook))
18038 ((and org-support-shift-select (org-region-active-p))
18039 (org-call-for-shift-select 'forward-char))
18040 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18041 ((and (not (eq org-support-shift-select 'always))
18042 (org-on-heading-p))
18043 (let ((org-inhibit-logging
18044 (not org-treat-S-cursor-todo-selection-as-state-change))
18045 (org-inhibit-blocking
18046 (not org-treat-S-cursor-todo-selection-as-state-change)))
18047 (org-call-with-arg 'org-todo 'right)))
18048 ((or (and org-support-shift-select
18049 (not (eq org-support-shift-select 'always))
18050 (org-at-item-bullet-p))
18051 (and (not org-support-shift-select) (org-at-item-p)))
18052 (org-call-with-arg 'org-cycle-list-bullet nil))
18053 ((and (not (eq org-support-shift-select 'always))
18054 (org-at-property-p))
18055 (call-interactively 'org-property-next-allowed-value))
18056 ((org-clocktable-try-shift 'right arg))
18057 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18058 (org-support-shift-select
18059 (org-call-for-shift-select 'forward-char))
18060 (t (org-shiftselect-error))))
18062 (defun org-shiftleft (&optional arg)
18063 "Cycle the thing at point or in the current line, depending on context.
18064 Depending on context, this does one of the following:
18066 - switch a timestamp at point one day into the past
18067 - on a headline, switch to the previous TODO keyword.
18068 - on an item, switch entire list to the previous bullet type
18069 - on a property line, switch to the previous allowed value
18070 - on a clocktable definition line, move time block into the past"
18073 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18074 ((and org-support-shift-select (org-region-active-p))
18075 (org-call-for-shift-select 'backward-char))
18076 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18077 ((and (not (eq org-support-shift-select 'always))
18078 (org-on-heading-p))
18079 (let ((org-inhibit-logging
18080 (not org-treat-S-cursor-todo-selection-as-state-change))
18081 (org-inhibit-blocking
18082 (not org-treat-S-cursor-todo-selection-as-state-change)))
18083 (org-call-with-arg 'org-todo 'left)))
18084 ((or (and org-support-shift-select
18085 (not (eq org-support-shift-select 'always))
18086 (org-at-item-bullet-p))
18087 (and (not org-support-shift-select) (org-at-item-p)))
18088 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18089 ((and (not (eq org-support-shift-select 'always))
18090 (org-at-property-p))
18091 (call-interactively 'org-property-previous-allowed-value))
18092 ((org-clocktable-try-shift 'left arg))
18093 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18094 (org-support-shift-select
18095 (org-call-for-shift-select 'backward-char))
18096 (t (org-shiftselect-error))))
18098 (defun org-shiftcontrolright ()
18099 "Switch to next TODO set."
18102 ((and org-support-shift-select (org-region-active-p))
18103 (org-call-for-shift-select 'forward-word))
18104 ((and (not (eq org-support-shift-select 'always))
18105 (org-on-heading-p))
18106 (org-call-with-arg 'org-todo 'nextset))
18107 (org-support-shift-select
18108 (org-call-for-shift-select 'forward-word))
18109 (t (org-shiftselect-error))))
18111 (defun org-shiftcontrolleft ()
18112 "Switch to previous TODO set."
18115 ((and org-support-shift-select (org-region-active-p))
18116 (org-call-for-shift-select 'backward-word))
18117 ((and (not (eq org-support-shift-select 'always))
18118 (org-on-heading-p))
18119 (org-call-with-arg 'org-todo 'previousset))
18120 (org-support-shift-select
18121 (org-call-for-shift-select 'backward-word))
18122 (t (org-shiftselect-error))))
18124 (defun org-shiftcontrolup ()
18125 "Change timestamps synchronously up in CLOCK log lines."
18127 (cond ((and (not org-support-shift-select)
18128 (org-at-clock-log-p)
18129 (org-at-timestamp-p t))
18130 (org-clock-timestamps-up))
18131 (t (org-shiftselect-error))))
18133 (defun org-shiftcontroldown ()
18134 "Change timestamps synchronously down in CLOCK log lines."
18136 (cond ((and (not org-support-shift-select)
18137 (org-at-clock-log-p)
18138 (org-at-timestamp-p t))
18139 (org-clock-timestamps-down))
18140 (t (org-shiftselect-error))))
18142 (defun org-ctrl-c-ret ()
18143 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18146 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18147 (t (call-interactively 'org-insert-heading))))
18149 (defun org-find-visible ()
18151 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18152 (get-char-property s 'invisible)))
18154 (defun org-find-invisible ()
18156 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18157 (not (get-char-property s 'invisible))))
18160 (defun org-copy-visible (beg end)
18161 "Copy the visible parts of the region."
18166 (narrow-to-region beg end)
18167 (setq s (goto-char (point-min)))
18168 (while (not (= (point) (point-max)))
18169 (goto-char (org-find-invisible))
18170 (push (buffer-substring s (point)) snippets)
18171 (setq s (goto-char (org-find-visible))))))
18172 (kill-new (apply 'concat (nreverse snippets)))))
18174 (defun org-copy-special ()
18175 "Copy region in table or copy current subtree.
18176 Calls `org-table-copy' or `org-copy-subtree', depending on context.
18177 See the individual commands for more information."
18179 (call-interactively
18180 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
18182 (defun org-cut-special ()
18183 "Cut region in table or cut current subtree.
18184 Calls `org-table-copy' or `org-cut-subtree', depending on context.
18185 See the individual commands for more information."
18187 (call-interactively
18188 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
18190 (defun org-paste-special (arg)
18191 "Paste rectangular region into table, or past subtree relative to level.
18192 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
18193 See the individual commands for more information."
18195 (if (org-at-table-p)
18196 (org-table-paste-rectangle)
18197 (org-paste-subtree arg)))
18199 (defun org-edit-special (&optional arg)
18200 "Call a special editor for the stuff at point.
18201 When at a table, call the formula editor with `org-table-edit-formulas'.
18202 When at the first line of an src example, call `org-edit-src-code'.
18203 When in an #+include line, visit the include file. Otherwise call
18204 `ffap' to visit the file at point."
18206 ;; possibly prep session before editing source
18208 (let* ((info (org-babel-get-src-block-info))
18209 (lang (nth 0 info))
18210 (params (nth 2 info))
18211 (session (cdr (assoc :session params))))
18212 (when (and info session) ;; we are in a source-code block with a session
18214 (intern (concat "org-babel-prep-session:" lang)) session params))))
18215 (cond ;; proceed with `org-edit-special'
18217 (beginning-of-line 1)
18218 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
18219 (find-file (org-trim (match-string 1))))
18220 ((org-edit-src-code))
18221 ((org-edit-fixed-width-region))
18222 ((org-at-table.el-p)
18223 (org-edit-src-code))
18224 ((or (org-at-table-p)
18226 (beginning-of-line 1)
18227 (looking-at "[ \t]*#\\+TBLFM:")))
18228 (call-interactively 'org-table-edit-formulas))
18229 (t (call-interactively 'ffap))))
18231 (defvar org-table-coordinate-overlays) ; defined in org-table.el
18232 (defun org-ctrl-c-ctrl-c (&optional arg)
18233 "Set tags in headline, or update according to changed information at point.
18235 This command does many different things, depending on context:
18237 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
18238 this is what we do.
18240 - If the cursor is on a statistics cookie, update it.
18242 - If the cursor is in a headline, prompt for tags and insert them
18243 into the current line, aligned to `org-tags-column'. When called
18244 with prefix arg, realign all tags in the current buffer.
18246 - If the cursor is in one of the special #+KEYWORD lines, this
18247 triggers scanning the buffer for these lines and updating the
18250 - If the cursor is inside a table, realign the table. This command
18251 works even if the automatic table editor has been turned off.
18253 - If the cursor is on a #+TBLFM line, re-apply the formulas to
18256 - If the cursor is at a footnote reference or definition, jump to
18257 the corresponding definition or references, respectively.
18259 - If the cursor is a the beginning of a dynamic block, update it.
18261 - If the current buffer is a capture buffer, close note and file it.
18263 - If the cursor is on a <<<target>>>, update radio targets and
18264 corresponding links in this buffer.
18266 - If the cursor is on a numbered item in a plain list, renumber the
18269 - If the cursor is on a checkbox, toggle it.
18271 - If the cursor is on a code block, evaluate it. The variable
18272 `org-confirm-babel-evaluate' can be used to control prompting
18273 before code block evaluation, by default every code block
18274 evaluation requires confirmation. Code block evaluation can be
18275 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
18277 (let ((org-enable-table-editor t))
18279 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
18280 org-occur-highlights
18281 org-latex-fragment-image-overlays)
18282 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
18283 (org-remove-occur-highlights)
18284 (org-remove-latex-fragment-image-overlays)
18285 (message "Temporary highlights/overlays removed from current buffer"))
18286 ((and (local-variable-p 'org-finish-function (current-buffer))
18287 (fboundp org-finish-function))
18288 (funcall org-finish-function))
18289 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
18290 ((org-in-regexp org-ts-regexp-both)
18291 (org-timestamp-change 0 'day))
18292 ((or (looking-at org-property-start-re)
18293 (org-at-property-p))
18294 (call-interactively 'org-property-action))
18295 ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
18296 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
18297 (or (org-on-heading-p) (org-at-item-p)))
18298 (call-interactively 'org-update-statistics-cookies))
18299 ((org-on-heading-p) (call-interactively 'org-set-tags))
18300 ((org-at-table.el-p)
18301 (message "Use C-c ' to edit table.el tables"))
18303 (org-table-maybe-eval-formula)
18305 (call-interactively 'org-table-recalculate)
18306 (org-table-maybe-recalculate-line))
18307 (call-interactively 'org-table-align)
18308 (orgtbl-send-table 'maybe))
18309 ((or (org-footnote-at-reference-p)
18310 (org-footnote-at-definition-p))
18311 (call-interactively 'org-footnote-action))
18312 ((org-at-item-checkbox-p)
18313 ;; Cursor at a checkbox: repair list and update checkboxes. Send
18314 ;; list only if at top item.
18315 (let* ((cbox (match-string 1))
18316 (struct (org-list-struct))
18317 (old-struct (copy-tree struct))
18318 (parents (org-list-parents-alist struct))
18319 (orderedp (org-entry-get nil "ORDERED"))
18320 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
18322 ;; Use a light version of `org-toggle-checkbox' to avoid
18323 ;; computing list structure twice.
18324 (org-list-set-checkbox (point-at-bol) struct
18326 ((equal arg '(16)) "[-]")
18327 ((equal arg '(4)) nil)
18328 ((equal "[X]" cbox) "[ ]")
18330 ;; Replicate `org-list-write-struct', while grabbing a return
18331 ;; value from `org-list-struct-fix-box'.
18332 (org-list-struct-fix-ind struct parents 2)
18333 (org-list-struct-fix-item-end struct)
18334 (let ((prevs (org-list-prevs-alist struct)))
18335 (org-list-struct-fix-bul struct prevs)
18336 (org-list-struct-fix-ind struct parents)
18338 (org-list-struct-fix-box struct parents prevs orderedp)))
18339 (org-list-struct-apply-struct struct old-struct)
18340 (org-update-checkbox-count-maybe)
18343 "Checkboxes were removed due to unchecked box at line %d"
18344 (org-current-line block-item)))
18345 (when firstp (org-list-send-list 'maybe))))
18347 ;; Cursor at an item: repair list. Do checkbox related actions
18348 ;; only if function was called with an argument. Send list only
18350 (let* ((struct (org-list-struct))
18351 (firstp (= (org-list-get-top-point struct) (point-at-bol))))
18352 (when arg (org-list-set-checkbox (point-at-bol) struct "[ ]"))
18353 (org-list-write-struct struct (org-list-parents-alist struct))
18354 (when arg (org-update-checkbox-count-maybe))
18355 (when firstp (org-list-send-list 'maybe))))
18356 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
18358 (beginning-of-line 1)
18359 (save-excursion (org-update-dblock)))
18361 (beginning-of-line 1)
18362 (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
18364 ((equal (match-string 1) "TBLFM")
18365 ;; Recalculate the table before this line
18367 (beginning-of-line 1)
18368 (skip-chars-backward " \r\n\t")
18369 (if (org-at-table-p)
18370 (org-call-with-arg 'org-table-recalculate (or arg t)))))
18372 (let ((org-inhibit-startup-visibility-stuff t)
18373 (org-startup-align-all-tables nil))
18374 (when (boundp 'org-table-coordinate-overlays)
18375 (mapc 'delete-overlay org-table-coordinate-overlays)
18376 (setq org-table-coordinate-overlays nil))
18377 (org-save-outline-visibility 'use-markers (org-mode-restart)))
18378 (message "Local setup has been refreshed"))))
18379 ((org-clock-update-time-maybe))
18381 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
18382 (error "C-c C-c can do nothing useful at this location"))))))
18384 (defun org-mode-restart ()
18385 "Restart Org-mode, to scan again for special lines.
18386 Also updates the keyword regular expressions."
18389 (message "Org-mode restarted"))
18391 (defun org-kill-note-or-show-branches ()
18392 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
18394 (if (not org-finish-function)
18397 (call-interactively 'show-branches))
18398 (let ((org-note-abort t))
18399 (funcall org-finish-function))))
18401 (defun org-return (&optional indent)
18402 "Goto next table row or insert a newline.
18403 Calls `org-table-next-row' or `newline', depending on context.
18404 See the individual commands for more information."
18407 ((bobp) (if indent (newline-and-indent) (newline)))
18409 (org-table-justify-field-maybe)
18410 (call-interactively 'org-table-next-row))
18411 ;; when `newline-and-indent' is called within a list, make sure
18412 ;; text moved stays inside the item.
18413 ((and (org-in-item-p) indent)
18414 (if (and (org-at-item-p) (>= (point) (match-end 0)))
18416 (save-match-data (newline))
18417 (org-indent-line-to (length (match-string 0))))
18418 (let ((ind (org-get-indentation)))
18420 (if (org-looking-back org-list-end-re)
18421 (org-indent-line-function)
18422 (org-indent-line-to ind)))))
18423 ((and org-return-follows-link
18424 (let ((tprop (get-text-property (point) 'face)))
18425 (or (eq tprop 'org-link)
18426 (and (listp tprop) (memq 'org-link tprop)))))
18427 (call-interactively 'org-open-at-point))
18428 ((and (org-at-heading-p)
18430 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
18434 (t (if indent (newline-and-indent) (newline)))))
18436 (defun org-return-indent ()
18437 "Goto next table row or insert a newline and indent.
18438 Calls `org-table-next-row' or `newline-and-indent', depending on
18439 context. See the individual commands for more information."
18443 (defun org-ctrl-c-star ()
18444 "Compute table, or change heading status of lines.
18445 Calls `org-table-recalculate' or `org-toggle-heading',
18446 depending on context."
18450 (call-interactively 'org-table-recalculate))
18452 ;; Convert all lines in region to list items
18453 (call-interactively 'org-toggle-heading))))
18455 (defun org-ctrl-c-minus ()
18456 "Insert separator line in table or modify bullet status of line.
18457 Also turns a plain line or a region of lines into list items.
18458 Calls `org-table-insert-hline', `org-toggle-item', or
18459 `org-cycle-list-bullet', depending on context."
18463 (call-interactively 'org-table-insert-hline))
18464 ((org-region-active-p)
18465 (call-interactively 'org-toggle-item))
18467 (call-interactively 'org-cycle-list-bullet))
18469 (call-interactively 'org-toggle-item))))
18471 (defun org-toggle-item (arg)
18472 "Convert headings or normal lines to items, items to normal lines.
18473 If there is no active region, only the current line is considered.
18475 If the first non blank line in the region is an headline, convert
18476 all headlines to items, shifting text accordingly.
18478 If it is an item, convert all items to normal lines.
18480 If it is normal text, change region into an item. With a prefix
18481 argument ARG, change each line in region into an item."
18485 ;; Shift text in current section to IND, from point to END.
18486 ;; The function leaves point to END line.
18488 (let ((min-i 1000) (end (copy-marker end)))
18489 ;; First determine the minimum indentation (MIN-I) of
18493 (while (< (point) end)
18494 (let ((i (org-get-indentation)))
18496 ;; Skip blank lines and inline tasks.
18497 ((looking-at "^[ \t]*$"))
18498 ((looking-at org-outline-regexp-bol))
18499 ;; We can't find less than 0 indentation.
18500 ((zerop i) (throw 'exit (setq min-i 0)))
18501 ((< i min-i) (setq min-i i))))
18503 ;; Then indent each line so that a line indented to
18504 ;; MIN-I becomes indented to IND. Ignore blank lines
18505 ;; and inline tasks in the process.
18506 (let ((delta (- ind min-i)))
18507 (while (< (point) end)
18508 (unless (or (looking-at "^[ \t]*$")
18509 (looking-at org-outline-regexp-bol))
18510 (org-indent-line-to (+ (org-get-indentation) delta)))
18511 (forward-line)))))))
18514 ;; Return beginning of first non-blank line, starting from
18519 (skip-chars-forward " \r\t\n")
18522 ;; Determine boundaries of changes.
18523 (if (org-region-active-p)
18524 (setq beg (funcall skip-blanks (region-beginning))
18525 end (copy-marker (region-end)))
18526 (setq beg (funcall skip-blanks (point-at-bol))
18527 end (copy-marker (point-at-eol))))
18528 ;; Depending on the starting line, choose an action on the text
18529 ;; between BEG and END.
18530 (org-with-limited-levels
18534 ;; Case 1. Start at an item: de-itemize. Note that it only
18535 ;; happens when a region is active: `org-ctrl-c-minus'
18536 ;; would call `org-cycle-list-bullet' otherwise.
18538 (while (< (point) end)
18539 (when (org-at-item-p)
18540 (skip-chars-forward " \t")
18541 (delete-region (point) (match-end 0)))
18543 ;; Case 2. Start at an heading: convert to items.
18544 ((org-on-heading-p)
18545 (let* ((bul (org-list-bullet-string "-"))
18546 (bul-len (length bul))
18547 ;; Indentation of the first heading. It should be
18548 ;; relative to the indentation of its parent, if any.
18549 (start-ind (save-excursion
18551 ((not org-adapt-indentation) 0)
18552 ((not (outline-previous-heading)) 0)
18553 (t (length (match-string 0))))))
18554 ;; Level of first heading. Further headings will be
18555 ;; compared to it to determine hierarchy in the list.
18556 (ref-level (org-reduced-level (org-outline-level))))
18557 (while (< (point) end)
18558 (let* ((level (org-reduced-level (org-outline-level)))
18559 (delta (max 0 (- level ref-level))))
18560 ;; If current headline is less indented than the first
18561 ;; one, set it as reference, in order to preserve
18563 (when (< level ref-level) (setq ref-level level))
18564 (replace-match bul t t)
18565 (org-indent-line-to (+ start-ind (* delta bul-len)))
18566 ;; Ensure all text down to END (or SECTION-END) belongs
18567 ;; to the newly created item.
18568 (let ((section-end (save-excursion
18569 (or (outline-next-heading) (point)))))
18571 (funcall shift-text
18572 (+ start-ind (* (1+ delta) bul-len))
18573 (min end section-end)))))))
18574 ;; Case 3. Normal line with ARG: turn each non-item line into
18577 (while (< (point) end)
18578 (unless (or (org-on-heading-p) (org-at-item-p))
18579 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
18581 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
18583 ;; Case 4. Normal line without ARG: make the first line of
18584 ;; region an item, and shift indentation of others
18585 ;; lines to set them as item's body.
18586 (t (let* ((bul (org-list-bullet-string "-"))
18587 (bul-len (length bul))
18588 (ref-ind (org-get-indentation)))
18589 (skip-chars-forward " \t")
18592 (while (< (point) end)
18593 ;; Ensure that lines less indented than first one
18594 ;; still get included in item body.
18595 (funcall shift-text
18596 (+ ref-ind bul-len)
18597 (min end (save-excursion (or (outline-next-heading)
18599 (forward-line)))))))))
18601 (defun org-toggle-heading (&optional nstars)
18602 "Convert headings to normal text, or items or text to headings.
18603 If there is no active region, only the current line is considered.
18605 If the first non blank line is an headline, remove the stars from
18606 all headlines in the region.
18608 If it is a plain list item, turn all plain list items into headings.
18610 If it is a normal line, turn each and every normal line (i.e. not
18611 an heading or an item) in the region into a heading.
18613 When converting a line into a heading, the number of stars is chosen
18614 such that the lines become children of the current entry. However,
18615 when a prefix argument is given, its value determines the number of
18620 ;; Return beginning of first non-blank line, starting from
18625 (skip-chars-forward " \r\t\n")
18628 ;; Determine boundaries of changes. If region ends at a bol, do
18629 ;; not consider the last line to be in the region.
18630 (if (org-region-active-p)
18631 (setq beg (funcall skip-blanks (region-beginning))
18632 end (copy-marker (save-excursion
18633 (goto-char (region-end))
18634 (if (bolp) (point) (point-at-eol)))))
18635 (setq beg (funcall skip-blanks (point-at-bol))
18636 end (copy-marker (point-at-eol))))
18637 ;; Ensure inline tasks don't count as headings.
18638 (org-with-limited-levels
18642 ;; Case 1. Started at an heading: de-star headings.
18643 ((org-on-heading-p)
18644 (while (< (point) end)
18645 (when (org-on-heading-p t)
18646 (looking-at org-outline-regexp) (replace-match ""))
18648 ;; Case 2. Started at an item: change items into headlines.
18649 ;; One star will be added by `org-list-to-subtree'.
18651 (let* ((stars (make-string
18653 ;; subtract the star that will be added again by
18654 ;; `org-list-to-subtree'
18655 (1- (prefix-numeric-value current-prefix-arg))
18656 (or (org-current-level) 0))
18659 (cond (nstars "") ; stars from prefix only
18660 ((equal stars "") "") ; before first heading
18661 (org-odd-levels-only "*") ; inside heading, odd
18662 (t "")))) ; inside heading, oddeven
18663 (while (< (point) end)
18664 (when (org-at-item-p)
18665 ;; Pay attention to cases when region ends before list.
18666 (let* ((struct (org-list-struct))
18667 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
18669 (narrow-to-region (point) list-end)
18671 (org-list-to-subtree
18672 (org-list-parse-list t)
18673 '(:istart (concat stars add-stars (funcall get-stars depth))
18674 :icount (concat stars add-stars (funcall get-stars depth))))))))
18676 ;; Case 3. Started at normal text: make every line an heading,
18677 ;; skipping headlines and items.
18678 (t (let* ((stars (make-string
18680 (prefix-numeric-value current-prefix-arg)
18681 (or (org-current-level) 0))
18684 (cond (nstars "") ; stars from prefix only
18685 ((equal stars "") "*") ; before first heading
18686 (org-odd-levels-only "**") ; inside heading, odd
18687 (t "*"))) ; inside heading, oddeven
18688 (rpl (concat stars add-stars " ")))
18689 (while (< (point) end)
18690 (when (and (not (org-on-heading-p)) (not (org-at-item-p))
18691 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
18692 (replace-match (concat rpl (match-string 2))))
18693 (forward-line)))))))))
18695 (defun org-meta-return (&optional arg)
18696 "Insert a new heading or wrap a region in a table.
18697 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
18698 See the individual commands for more information."
18701 ((run-hook-with-args-until-success 'org-metareturn-hook))
18703 (call-interactively 'org-table-wrap-region))
18704 (t (call-interactively 'org-insert-heading))))
18708 ;; Define the Org-mode menus
18709 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
18711 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
18712 ["Next Field" org-cycle (org-at-table-p)]
18713 ["Previous Field" org-shifttab (org-at-table-p)]
18714 ["Next Row" org-return (org-at-table-p)]
18716 ["Blank Field" org-table-blank-field (org-at-table-p)]
18717 ["Edit Field" org-table-edit-field (org-at-table-p)]
18718 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
18721 ["Move Column Left" org-metaleft (org-at-table-p)]
18722 ["Move Column Right" org-metaright (org-at-table-p)]
18723 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
18724 ["Insert Column" org-shiftmetaright (org-at-table-p)])
18726 ["Move Row Up" org-metaup (org-at-table-p)]
18727 ["Move Row Down" org-metadown (org-at-table-p)]
18728 ["Delete Row" org-shiftmetaup (org-at-table-p)]
18729 ["Insert Row" org-shiftmetadown (org-at-table-p)]
18730 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
18732 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
18734 ["Copy Rectangle" org-copy-special (org-at-table-p)]
18735 ["Cut Rectangle" org-cut-special (org-at-table-p)]
18736 ["Paste Rectangle" org-paste-special (org-at-table-p)]
18737 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
18740 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
18741 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
18742 ["Edit Formulas" org-edit-special (org-at-table-p)]
18744 ["Recalculate line" org-table-recalculate (org-at-table-p)]
18745 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
18746 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
18748 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
18750 ["Sum Column/Rectangle" org-table-sum
18751 (or (org-at-table-p) (org-region-active-p))]
18752 ["Which Column?" org-table-current-column (org-at-table-p)])
18754 org-table-toggle-formula-debugger
18755 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
18756 ["Show Col/Row Numbers"
18757 org-table-toggle-coordinate-overlays
18759 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
18761 ["Create" org-table-create (and (not (org-at-table-p))
18762 org-enable-table-editor)]
18763 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
18764 ["Import from File" org-table-import (not (org-at-table-p))]
18765 ["Export to File" org-table-export (org-at-table-p)]
18767 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
18769 (easy-menu-define org-org-menu org-mode-map "Org menu"
18772 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
18773 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
18774 ["Sparse Tree..." org-sparse-tree t]
18775 ["Reveal Context" org-reveal t]
18776 ["Show All" show-all t]
18778 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
18780 ["New Heading" org-insert-heading t]
18781 ("Navigate Headings"
18782 ["Up" outline-up-heading t]
18783 ["Next" outline-next-visible-heading t]
18784 ["Previous" outline-previous-visible-heading t]
18785 ["Next Same Level" outline-forward-same-level t]
18786 ["Previous Same Level" outline-backward-same-level t]
18788 ["Jump" org-goto t])
18790 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
18791 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
18793 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
18794 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
18795 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
18797 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
18799 ["Copy visible text" org-copy-visible t]
18801 ["Promote Heading" org-metaleft (not (org-at-table-p))]
18802 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
18803 ["Demote Heading" org-metaright (not (org-at-table-p))]
18804 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
18806 ["Sort Region/Children" org-sort (not (org-at-table-p))]
18808 ["Convert to odd levels" org-convert-to-odd-levels t]
18809 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
18811 ["Emphasis..." org-emphasize t]
18812 ["Edit Source Example" org-edit-special t]
18814 ["Footnote new/jump" org-footnote-action t]
18815 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
18817 ["Archive (default method)" org-archive-subtree-default t]
18819 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
18820 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
18821 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
18825 ["Store Link (Global)" org-store-link t]
18826 ["Find existing link to here" org-occur-link-in-agenda-files t]
18827 ["Insert Link" org-insert-link t]
18828 ["Follow Link" org-open-at-point t]
18830 ["Next link" org-next-link t]
18831 ["Previous link" org-previous-link t]
18833 ["Descriptive Links"
18834 org-toggle-link-display
18836 :selected org-descriptive-links
18839 org-toggle-link-display
18841 :selected (not org-descriptive-links)])
18844 ["TODO/DONE/-" org-todo t]
18846 ["Next keyword" org-shiftright (org-on-heading-p)]
18847 ["Previous keyword" org-shiftleft (org-on-heading-p)]
18848 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
18849 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
18850 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
18851 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
18852 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
18854 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
18855 :selected org-enforce-todo-dependencies :style toggle :active t]
18856 "Settings for tree at point"
18857 ["Do Children sequentially" org-toggle-ordered-property :style radio
18858 :selected (org-entry-get nil "ORDERED")
18859 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
18860 ["Do Children parallel" org-toggle-ordered-property :style radio
18861 :selected (not (org-entry-get nil "ORDERED"))
18862 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
18864 ["Set Priority" org-priority t]
18865 ["Priority Up" org-shiftup t]
18866 ["Priority Down" org-shiftdown t]
18868 ["Get news from all feeds" org-feed-update-all t]
18869 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
18870 ["Customize feeds" (customize-variable 'org-feed-alist) t])
18871 ("TAGS and Properties"
18872 ["Set Tags" org-set-tags-command t]
18873 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
18875 ["Set property" org-set-property t]
18876 ["Column view of properties" org-columns t]
18877 ["Insert Column View DBlock" org-insert-columns-dblock t])
18878 ("Dates and Scheduling"
18879 ["Timestamp" org-time-stamp t]
18880 ["Timestamp (inactive)" org-time-stamp-inactive t]
18882 ["1 Day Later" org-shiftright t]
18883 ["1 Day Earlier" org-shiftleft t]
18884 ["1 ... Later" org-shiftup t]
18885 ["1 ... Earlier" org-shiftdown t])
18886 ["Compute Time Range" org-evaluate-time-range t]
18887 ["Schedule Item" org-schedule t]
18888 ["Deadline" org-deadline t]
18890 ["Custom time format" org-toggle-time-stamp-overlays
18891 :style radio :selected org-display-custom-times]
18893 ["Goto Calendar" org-goto-calendar t]
18894 ["Date from Calendar" org-date-from-calendar t]
18896 ["Start/Restart Timer" org-timer-start t]
18897 ["Pause/Continue Timer" org-timer-pause-or-continue t]
18898 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
18899 ["Insert Timer String" org-timer t]
18900 ["Insert Timer Item" org-timer-item t])
18902 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
18903 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
18904 ["Clock out" org-clock-out t]
18905 ["Clock cancel" org-clock-cancel t]
18907 ["Mark as default task" org-clock-mark-default-task t]
18908 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
18909 ["Goto running clock" org-clock-goto t]
18911 ["Display times" org-clock-display t]
18912 ["Create clock table" org-clock-report t]
18914 ["Record DONE time"
18915 (progn (setq org-log-done (not org-log-done))
18916 (message "Switching to %s will %s record a timestamp"
18917 (car org-done-keywords)
18918 (if org-log-done "automatically" "not")))
18919 :style toggle :selected org-log-done])
18921 ["Agenda Command..." org-agenda t]
18922 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
18923 ("File List for Agenda")
18924 ("Special views current file"
18925 ["TODO Tree" org-show-todo-tree t]
18926 ["Check Deadlines" org-check-deadlines t]
18927 ["Timeline" org-timeline t]
18928 ["Tags/Property tree" org-match-sparse-tree t])
18930 ["Export/Publish..." org-export t]
18932 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
18933 :selected org-cdlatex-mode]
18934 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
18935 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
18936 ["Modify math symbol" org-cdlatex-math-modify
18937 (org-inside-LaTeX-fragment-p)]
18938 ["Insert citation" org-reftex-citation t]
18940 ["Template for BEAMER" org-insert-beamer-options-template t])
18943 ["Push Files and Views" org-mobile-push t]
18944 ["Get Captured and Flagged" org-mobile-pull t]
18945 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
18947 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
18950 ["Show Version" org-version t]
18951 ["Info Documentation" org-info t])
18953 ["Browse Org Group" org-customize t]
18955 ["Expand This Menu" org-create-customize-menu
18956 (fboundp 'customize-menu-create)])
18957 ["Send bug report" org-submit-bug-report t]
18960 ["Refresh setup current buffer" org-mode-restart t]
18961 ["Reload Org (after update)" org-reload t]
18962 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
18965 (defun org-info (&optional node)
18966 "Read documentation for Org-mode in the info system.
18967 With optional NODE, go directly to that node."
18969 (info (format "(org)%s" (or node ""))))
18972 (defun org-submit-bug-report ()
18973 "Submit a bug report on Org-mode via mail.
18975 Don't hesitate to report any problems or inaccurate documentation.
18977 If you don't have setup sending mail from (X)Emacs, please copy the
18978 output buffer into your mail program, as it gives us important
18979 information about your Org-mode version and configuration."
18981 (require 'reporter)
18982 (org-load-modules-maybe)
18983 (org-require-autoloaded-modules)
18984 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
18985 (reporter-submit-bug-report
18986 "emacs-orgmode@gnu.org"
18989 (save-window-excursion
18990 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
18991 (delete-other-windows)
18993 (insert "You are about to submit a bug report to the Org-mode mailing list.
18995 We would like to add your full Org-mode and Outline configuration to the
18996 bug report. This greatly simplifies the work of the maintainer and
18997 other experts on the mailing list.
18999 HOWEVER, some variables you have customized may contain private
19000 information. The names of customers, colleagues, or friends, might
19001 appear in the form of file names, tags, todo states, or search strings.
19002 If you answer yes to the prompt, you might want to check and remove
19003 such private information before sending the email.")
19004 (add-text-properties (point-min) (point-max) '(face org-warning))
19005 (when (yes-or-no-p "Include your Org-mode configuration ")
19009 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19010 (or (and (symbol-value v)
19011 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19013 (get v 'custom-type) (get v 'standard-value)
19014 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19016 (kill-buffer (get-buffer "*Warn about privacy*"))
19019 "Remember to cover the basics, that is, what you expected to happen and
19020 what in fact did happen. You don't know how to make a good report? See
19022 http://orgmode.org/manual/Feedback.html#Feedback
19024 Your bug report will be posted to the Org-mode mailing list.
19025 ------------------------------------------------------------------------")
19027 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19028 (replace-match "\\1Bug: \\3 [\\2]")))))
19031 (defun org-install-agenda-files-menu ()
19032 (let ((bl (buffer-list)))
19035 (set-buffer (pop bl))
19036 (if (eq major-mode 'org-mode) (setq bl nil)))
19037 (when (eq major-mode 'org-mode)
19039 '("Org") "File List for Agenda"
19042 ["Edit File List" (org-edit-agenda-file-list) t]
19043 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19044 ["Remove Current File from List" org-remove-file t]
19045 ["Cycle through agenda files" org-cycle-agenda-files t]
19046 ["Occur in all agenda files" org-occur-in-agenda-files t]
19048 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19053 (defun org-require-autoloaded-modules ()
19056 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19057 org-docbook org-exp org-html org-icalendar
19059 org-publish org-remember org-table
19060 org-timer org-xoxo)))
19063 (defun org-reload (&optional uncompiled)
19064 "Reload all org lisp files.
19065 With prefix arg UNCOMPILED, load the uncompiled versions."
19067 (require 'find-func)
19068 (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
19069 (dir-org (file-name-directory (org-find-library-name "org")))
19070 (dir-org-contrib (ignore-errors
19071 (file-name-directory
19072 (org-find-library-name "org-contribdir"))))
19074 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19075 (append (list nil "comint" "eval" "exp" "keys"
19076 "lob" "ref" "table" "tangle")
19080 (when (cdr lang) (symbol-name (car lang))))
19081 org-babel-load-languages)))))
19083 (append (directory-files dir-org t file-re)
19085 (and dir-org-contrib
19086 (directory-files dir-org-contrib t file-re))))
19087 (remove-re (concat (if (featurep 'xemacs)
19088 "org-colview" "org-colview-xemacs")
19090 (setq files (mapcar 'file-name-sans-extension files))
19091 (setq files (mapcar
19092 (lambda (x) (if (string-match remove-re x) nil x))
19094 (setq files (delq nil files))
19097 (when (featurep (intern (file-name-nondirectory f)))
19098 (if (and (not uncompiled)
19099 (file-exists-p (concat f ".elc")))
19100 (load (concat f ".elc") nil nil t)
19101 (load (concat f ".el") nil nil t))))
19106 (defun org-customize ()
19107 "Call the customize function with org as argument."
19109 (org-load-modules-maybe)
19110 (org-require-autoloaded-modules)
19111 (customize-browse 'org))
19113 (defun org-create-customize-menu ()
19114 "Create a full customization menu for Org-mode, insert it into the menu."
19116 (org-load-modules-maybe)
19117 (org-require-autoloaded-modules)
19118 (if (fboundp 'customize-menu-create)
19121 '("Org") "Customize"
19122 `(["Browse Org group" org-customize t]
19124 ,(customize-menu-create 'org)
19125 ["Set" Custom-set t]
19126 ["Save" Custom-save t]
19127 ["Reset to Current" Custom-reset-current t]
19128 ["Reset to Saved" Custom-reset-saved t]
19129 ["Reset to Standard Settings" Custom-reset-standard t]))
19130 (message "\"Org\"-menu now contains full customization menu"))
19131 (error "Cannot expand menu (outdated version of cus-edit.el)")))
19133 ;;;; Miscellaneous stuff
19135 ;;; Generally useful functions
19137 (defun org-get-at-bol (property)
19138 "Get text property PROPERTY at beginning of line."
19139 (get-text-property (point-at-bol) property))
19141 (defun org-find-text-property-in-string (prop s)
19142 "Return the first non-nil value of property PROP in string S."
19143 (or (get-text-property 0 prop s)
19144 (get-text-property (or (next-single-property-change 0 prop s) 0)
19147 (defun org-display-warning (message) ;; Copied from Emacs-Muse
19148 "Display the given MESSAGE as a warning."
19149 (if (fboundp 'display-warning)
19150 (display-warning 'org message
19151 (if (featurep 'xemacs) 'warning :warning))
19152 (let ((buf (get-buffer-create "*Org warnings*")))
19153 (with-current-buffer buf
19154 (goto-char (point-max))
19155 (insert "Warning (Org): " message)
19158 (display-buffer buf)
19161 (defun org-eval (form)
19162 "Eval FORM and return result."
19163 (condition-case error
19165 (error (format "%%![Error: %s]" error))))
19167 (defun org-in-commented-line ()
19168 "Is point in a line starting with `#'?"
19169 (equal (char-after (point-at-bol)) ?#))
19171 (defun org-in-indented-comment-line ()
19172 "Is point in a line starting with `#' after some white space?"
19175 (goto-char (point-at-bol))
19176 (looking-at "[ \t]*#"))))
19178 (defun org-in-verbatim-emphasis ()
19180 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
19182 (defun org-goto-marker-or-bmk (marker &optional bookmark)
19183 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
19184 (if (and marker (marker-buffer marker)
19185 (buffer-live-p (marker-buffer marker)))
19187 (org-pop-to-buffer-same-window (marker-buffer marker))
19188 (if (or (> marker (point-max)) (< marker (point-min)))
19191 (org-show-context 'org-goto))
19193 (bookmark-jump bookmark)
19194 (error "Cannot find location"))))
19196 (defun org-quote-csv-field (s)
19197 "Quote field for inclusion in CSV material."
19198 (if (string-match "[\",]" s)
19199 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
19202 (defun org-force-self-insert (N)
19203 "Needed to enforce self-insert under remapping."
19205 (self-insert-command N))
19207 (defun org-string-width (s)
19208 "Compute width of string, ignoring invisible characters.
19209 This ignores character with invisibility property `org-link', and also
19210 characters with property `org-cwidth', because these will become invisible
19211 upon the next fontification round."
19213 (when (or (eq t buffer-invisibility-spec)
19214 (assq 'org-link buffer-invisibility-spec))
19215 (while (setq b (text-property-any 0 (length s)
19216 'invisible 'org-link s))
19217 (setq s (concat (substring s 0 b)
19218 (substring s (or (next-single-property-change
19219 b 'invisible s) (length s)))))))
19220 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
19221 (setq s (concat (substring s 0 b)
19222 (substring s (or (next-single-property-change
19223 b 'org-cwidth s) (length s))))))
19224 (setq l (string-width s) b -1)
19225 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
19226 (setq l (- l (get-text-property b 'org-dwidth-n s))))
19229 (defun org-shorten-string (s maxlength)
19230 "Shorten string S so tht it is no longer than MAXLENGTH characters.
19231 If the string is shorter or has length MAXLENGTH, just return the
19232 original string. If it is longer, the functions finds a space in the
19233 string, breaks this string off at that locations and adds three dots
19234 as ellipsis. Including the ellipsis, the string will not be longer
19235 than MAXLENGTH. If finding a good breaking point in the string does
19236 not work, the string is just chopped off in the middle of a word
19238 (if (<= (length s) maxlength)
19240 (let* ((n (max (- maxlength 4) 1))
19241 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
19242 (if (string-match re s)
19243 (concat (match-string 1 s) "...")
19244 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
19246 (defun org-get-indentation (&optional line)
19247 "Get the indentation of the current line, interpreting tabs.
19248 When LINE is given, assume it represents a line and compute its indentation."
19250 (if (string-match "^ *" (org-remove-tabs line))
19253 (beginning-of-line 1)
19254 (skip-chars-forward " \t")
19255 (current-column))))
19257 (defun org-get-string-indentation (s)
19258 "What indentation has S due to SPACE and TAB at the beginning of the string?"
19259 (let ((n -1) (i 0) (w tab-width) c)
19261 (while (< (setq n (1+ n)) (length s))
19262 (setq c (aref s n))
19263 (cond ((= c ?\ ) (setq i (1+ i)))
19264 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
19265 (t (throw 'exit t)))))
19268 (defun org-remove-tabs (s &optional width)
19269 "Replace tabulators in S with spaces.
19270 Assumes that s is a single line, starting in column 0."
19271 (setq width (or width tab-width))
19272 (while (string-match "\t" s)
19273 (setq s (replace-match
19275 (- (* width (/ (+ (match-beginning 0) width) width))
19276 (match-beginning 0)) ?\ )
19280 (defun org-fix-indentation (line ind)
19281 "Fix indentation in LINE.
19282 IND is a cons cell with target and minimum indentation.
19283 If the current indentation in LINE is smaller than the minimum,
19284 leave it alone. If it is larger than ind, set it to the target."
19285 (let* ((l (org-remove-tabs line))
19286 (i (org-get-indentation l))
19287 (i1 (car ind)) (i2 (cdr ind)))
19288 (if (>= i i2) (setq l (substring line i2)))
19290 (concat (make-string i1 ?\ ) l)
19293 (defun org-remove-indentation (code &optional n)
19294 "Remove the maximum common indentation from the lines in CODE.
19295 N may optionally be the number of spaces to remove."
19298 (org-do-remove-indentation n)
19301 (defun org-do-remove-indentation (&optional n)
19302 "Remove the maximum common indentation from the buffer."
19303 (untabify (point-min) (point-max))
19304 (let ((min 10000) re)
19307 (goto-char (point-min))
19308 (while (re-search-forward "^ *[^ \n]" nil t)
19309 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
19310 (unless (or (= min 0) (= min 10000))
19311 (setq re (format "^ \\{%d\\}" min))
19312 (goto-char (point-min))
19313 (while (re-search-forward re nil t)
19318 (defun org-fill-template (template alist)
19319 "Find each %key of ALIST in TEMPLATE and replace it."
19320 (let ((case-fold-search nil)
19322 (setq alist (sort (copy-sequence alist)
19323 (lambda (a b) (< (length (car a)) (length (car b))))))
19324 (while (setq entry (pop alist))
19326 (replace-regexp-in-string
19327 (concat "%" (regexp-quote (car entry)))
19328 (cdr entry) template t t)))
19331 (defun org-base-buffer (buffer)
19332 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
19335 (or (buffer-base-buffer buffer)
19338 (defun org-trim (s)
19339 "Remove whitespace at beginning and end of string."
19340 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
19341 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
19344 (defun org-wrap (string &optional width lines)
19345 "Wrap string to either a number of lines, or a width in characters.
19346 If WIDTH is non-nil, the string is wrapped to that width, however many lines
19347 that costs. If there is a word longer than WIDTH, the text is actually
19348 wrapped to the length of that word.
19349 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
19350 many lines, whatever width that takes.
19351 The return value is a list of lines, without newlines at the end."
19352 (let* ((words (org-split-string string "[ \t\n]+"))
19353 (maxword (apply 'max (mapcar 'org-string-width words)))
19356 (org-do-wrap words (max maxword width)))
19359 (setq ll (org-do-wrap words maxword))
19360 (if (<= (length ll) lines)
19363 (while (> (length ll) lines)
19365 (setq ll (org-do-wrap words w)))
19367 (t (error "Cannot wrap this")))))
19369 (defun org-do-wrap (words width)
19370 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
19373 (setq line (pop words))
19374 (while (and words (< (+ (length line) (length (car words))) width))
19375 (setq line (concat line " " (pop words))))
19376 (setq lines (push line lines)))
19379 (defun org-split-string (string &optional separators)
19380 "Splits STRING into substrings at SEPARATORS.
19381 No empty strings are returned if there are matches at the beginning
19382 and end of string."
19383 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
19387 (while (and (string-match rexp string
19389 (= start (match-beginning 0))
19390 (< start (length string)))
19392 (< (match-beginning 0) (length string)))
19394 (or (eq (match-beginning 0) 0)
19395 (and (eq (match-beginning 0) (match-end 0))
19396 (eq (match-beginning 0) start))
19398 (cons (substring string start (match-beginning 0))
19400 (setq start (match-end 0)))
19401 (or (eq start (length string))
19403 (cons (substring string start)
19407 (defun org-quote-vert (s)
19408 "Replace \"|\" with \"\\vert\"."
19409 (while (string-match "|" s)
19410 (setq s (replace-match "\\vert" t t s)))
19413 (defun org-uuidgen-p (s)
19414 "Is S an ID created by UUIDGEN?"
19415 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
19417 (defun org-context ()
19418 "Return a list of contexts of the current cursor position.
19419 If several contexts apply, all are returned.
19420 Each context entry is a list with a symbol naming the context, and
19421 two positions indicating start and end of the context. Possible
19424 :headline anywhere in a headline
19425 :headline-stars on the leading stars in a headline
19426 :todo-keyword on a TODO keyword (including DONE) in a headline
19427 :tags on the TAGS in a headline
19428 :priority on the priority cookie in a headline
19429 :item on the first line of a plain list item
19430 :item-bullet on the bullet/number of a plain list item
19431 :checkbox on the checkbox in a plain list item
19432 :table in an org-mode table
19433 :table-special on a special filed in a table
19434 :table-table in a table.el table
19435 :link on a hyperlink
19436 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
19437 :target on a <<target>>
19438 :radio-target on a <<<radio-target>>>
19439 :latex-fragment on a LaTeX fragment
19440 :latex-preview on a LaTeX fragment with overlaid preview image
19442 This function expects the position to be visible because it uses font-lock
19443 faces as a help to recognize the following contexts: :table-special, :link,
19445 (let* ((f (get-text-property (point) 'face))
19446 (faces (if (listp f) f (list f)))
19447 (p (point)) clist o)
19448 ;; First the large context
19450 ((org-on-heading-p t)
19451 (push (list :headline (point-at-bol) (point-at-eol)) clist)
19453 (beginning-of-line 1)
19454 (looking-at org-todo-line-tags-regexp))
19455 (push (org-point-in-group p 1 :headline-stars) clist)
19456 (push (org-point-in-group p 2 :todo-keyword) clist)
19457 (push (org-point-in-group p 4 :tags) clist))
19459 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
19460 (if (looking-at "\\[#[A-Z0-9]\\]")
19461 (push (org-point-in-group p 0 :priority) clist)))
19464 (push (org-point-in-group p 2 :item-bullet) clist)
19465 (push (list :item (point-at-bol)
19466 (save-excursion (org-end-of-item) (point)))
19468 (and (org-at-item-checkbox-p)
19469 (push (org-point-in-group p 0 :checkbox) clist)))
19472 (push (list :table (org-table-begin) (org-table-end)) clist)
19473 (if (memq 'org-formula faces)
19474 (push (list :table-special
19475 (previous-single-property-change p 'face)
19476 (next-single-property-change p 'face)) clist)))
19477 ((org-at-table-p 'any)
19478 (push (list :table-table) clist)))
19481 ;; Now the small context
19483 ((org-at-timestamp-p)
19484 (push (org-point-in-group p 0 :timestamp) clist))
19485 ((memq 'org-link faces)
19487 (previous-single-property-change p 'face)
19488 (next-single-property-change p 'face)) clist))
19489 ((memq 'org-special-keyword faces)
19490 (push (list :keyword
19491 (previous-single-property-change p 'face)
19492 (next-single-property-change p 'face)) clist))
19494 (push (org-point-in-group p 0 :target) clist)
19495 (goto-char (1- (match-beginning 0)))
19496 (if (looking-at org-radio-target-regexp)
19497 (push (org-point-in-group p 0 :radio-target) clist))
19499 ((setq o (car (delq nil
19502 (if (memq x org-latex-fragment-image-overlays) x))
19503 (overlays-at (point))))))
19504 (push (list :latex-fragment
19505 (overlay-start o) (overlay-end o)) clist)
19506 (push (list :latex-preview
19507 (overlay-start o) (overlay-end o)) clist))
19508 ((org-inside-LaTeX-fragment-p)
19509 ;; FIXME: positions wrong.
19510 (push (list :latex-fragment (point) (point)) clist)))
19512 (setq clist (nreverse (delq nil clist)))
19515 ;; FIXME: Compare with at-regexp-p Do we need both?
19516 (defun org-in-regexp (re &optional nlines visually)
19517 "Check if point is inside a match of regexp.
19518 Normally only the current line is checked, but you can include NLINES extra
19519 lines both before and after point into the search.
19520 If VISUALLY is set, require that the cursor is not after the match but
19521 really on, so that the block visually is on the match."
19523 (let ((pos (point))
19524 (eol (point-at-eol (+ 1 (or nlines 0))))
19525 (inc (if visually 1 0)))
19527 (beginning-of-line (- 1 (or nlines 0)))
19528 (while (re-search-forward re eol t)
19529 (if (and (<= (match-beginning 0) pos)
19530 (>= (+ inc (match-end 0)) pos))
19531 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
19533 (defun org-at-regexp-p (regexp)
19534 "Is point inside a match of REGEXP in the current line?"
19537 (let ((pos (point)) (end (point-at-eol)))
19538 (beginning-of-line 1)
19539 (while (re-search-forward regexp end t)
19540 (if (and (<= (match-beginning 0) pos)
19541 (>= (match-end 0) pos))
19545 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
19546 "Non-nil when point is between matches of START-RE and END-RE.
19548 Also return a non-nil value when point is on one of the matches.
19550 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
19551 buffer positions. Default values are the positions of headlines
19552 surrounding the point.
19554 The functions returns a cons cell whose car (resp. cdr) is the
19555 position before START-RE (resp. after END-RE)."
19557 (let ((pos (point))
19558 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
19559 (limit-down (or lim-down (save-excursion (outline-next-heading))))
19562 ;; Point is on a block when on START-RE or if START-RE can be
19563 ;; found before it...
19564 (and (or (org-at-regexp-p start-re)
19565 (re-search-backward start-re limit-up t))
19566 (setq beg (match-beginning 0))
19567 ;; ... and END-RE after it...
19568 (goto-char (match-end 0))
19569 (re-search-forward end-re limit-down t)
19570 (> (setq end (match-end 0)) pos)
19571 ;; ... without another START-RE in-between.
19572 (goto-char (match-beginning 0))
19573 (not (re-search-backward start-re (1+ beg) t))
19575 (cons beg end))))))
19577 (defun org-in-block-p (names)
19578 "Non-nil when point belongs to a block whose name belongs to NAMES.
19580 NAMES is a list of strings containing names of blocks.
19582 Return first block name matched, or nil. Beware that in case of
19583 nested blocks, the returned name may not belong to the closest
19587 (let ((case-fold-search t)
19588 (lim-up (save-excursion (outline-previous-heading)))
19589 (lim-down (save-excursion (outline-next-heading))))
19590 (mapc (lambda (name)
19591 (let ((n (regexp-quote name)))
19592 (when (org-between-regexps-p
19593 (concat "^[ \t]*#\\+begin_" n)
19594 (concat "^[ \t]*#\\+end_" n)
19600 (defun org-occur-in-agenda-files (regexp &optional nlines)
19601 "Call `multi-occur' with buffers for all agenda files."
19602 (interactive "sOrg-files matching: \np")
19603 (let* ((files (org-agenda-files))
19604 (tnames (mapcar 'file-truename files))
19605 (extra org-agenda-text-search-extra-files)
19607 (when (eq (car extra) 'agenda-archives)
19608 (setq extra (cdr extra))
19609 (setq files (org-add-archive-files files)))
19610 (while (setq f (pop extra))
19611 (unless (member (file-truename f) tnames)
19612 (add-to-list 'files f 'append)
19613 (add-to-list 'tnames (file-truename f) 'append)))
19615 (mapcar (lambda (x)
19616 (with-current-buffer
19617 (or (get-file-buffer x) (find-file-noselect x))
19623 (if (boundp 'occur-mode-find-occurrence-hook)
19625 (add-hook 'occur-mode-find-occurrence-hook
19627 (when (eq major-mode 'org-mode)
19630 (defadvice occur-mode-goto-occurrence
19631 (after org-occur-reveal activate)
19632 (and (eq major-mode 'org-mode) (org-reveal)))
19633 (defadvice occur-mode-goto-occurrence-other-window
19634 (after org-occur-reveal activate)
19635 (and (eq major-mode 'org-mode) (org-reveal)))
19636 (defadvice occur-mode-display-occurrence
19637 (after org-occur-reveal activate)
19638 (when (eq major-mode 'org-mode)
19639 (let ((pos (occur-mode-find-occurrence)))
19640 (with-current-buffer (marker-buffer pos)
19645 (defun org-occur-link-in-agenda-files ()
19646 "Create a link and search for it in the agendas.
19647 The link is not stored in `org-stored-links', it is just created
19648 for the search purpose."
19650 (let ((link (condition-case nil
19651 (org-store-link nil)
19652 (error "Unable to create a link to here"))))
19653 (org-occur-in-agenda-files (regexp-quote link))))
19655 (defun org-uniquify (list)
19656 "Remove duplicate elements from LIST."
19658 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
19661 (defun org-delete-all (elts list)
19662 "Remove all elements in ELTS from LIST."
19664 (setq list (delete (pop elts) list)))
19667 (defun org-count (cl-item cl-seq)
19668 "Count the number of occurrences of ITEM in SEQ.
19669 Taken from `count' in cl-seq.el with all keyword arguments removed."
19670 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
19671 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
19672 (while (< cl-start cl-end)
19673 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
19674 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
19675 (setq cl-start (1+ cl-start)))
19678 (defun org-remove-if (predicate seq)
19679 "Remove everything from SEQ that fulfills PREDICATE."
19683 (if (not (funcall predicate e)) (push e res)))
19686 (defun org-remove-if-not (predicate seq)
19687 "Remove everything from SEQ that does not fulfill PREDICATE."
19691 (if (funcall predicate e) (push e res)))
19694 (defun org-reduce (cl-func cl-seq &rest cl-keys)
19695 "Reduce two-argument FUNCTION across SEQ.
19696 Taken from `reduce' in cl-seq.el with all keyword arguments but
19697 \":initial-value\" removed."
19698 (let ((cl-accum (cond ((memq :initial-value cl-keys)
19699 (cadr (memq :initial-value cl-keys)))
19700 (cl-seq (pop cl-seq))
19701 (t (funcall cl-func)))))
19703 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
19706 (defun org-back-over-empty-lines ()
19707 "Move backwards over whitespace, to the beginning of the first empty line.
19708 Returns the number of empty lines passed."
19709 (let ((pos (point)))
19710 (if (cdr (assoc 'heading org-blank-before-new-entry))
19711 (skip-chars-backward " \t\n\r")
19713 (forward-line -1)))
19714 (beginning-of-line 2)
19715 (goto-char (min (point) pos))
19716 (count-lines (point) pos)))
19718 (defun org-skip-whitespace ()
19719 (skip-chars-forward " \t\n\r"))
19721 (defun org-point-in-group (point group &optional context)
19722 "Check if POINT is in match-group GROUP.
19723 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
19724 match. If the match group does not exist or point is not inside it,
19726 (and (match-beginning group)
19727 (>= point (match-beginning group))
19728 (<= point (match-end group))
19730 (list context (match-beginning group) (match-end group))
19733 (defun org-switch-to-buffer-other-window (&rest args)
19734 "Switch to buffer in a second window on the current frame.
19735 In particular, do not allow pop-up frames.
19736 Returns the newly created buffer."
19737 (let (pop-up-frames special-display-buffer-names special-display-regexps
19738 special-display-function)
19739 (apply 'switch-to-buffer-other-window args)))
19741 (defun org-combine-plists (&rest plists)
19742 "Create a single property list from all plists in PLISTS.
19743 The process starts by copying the first list, and then setting properties
19744 from the other lists. Settings in the last list are the most significant
19745 ones and overrule settings in the other lists."
19746 (let ((rtn (copy-sequence (pop plists)))
19749 (setq ls (pop plists))
19751 (setq p (pop ls) v (pop ls))
19752 (setq rtn (plist-put rtn p v))))
19755 (defun org-move-line-down (arg)
19756 "Move the current line down. With prefix argument, move it past ARG lines."
19758 (let ((col (current-column))
19760 (beginning-of-line 1) (setq beg (point))
19761 (beginning-of-line 2) (setq end (point))
19762 (beginning-of-line (+ 1 arg))
19763 (setq pos (move-marker (make-marker) (point)))
19764 (insert (delete-and-extract-region beg end))
19766 (org-move-to-column col)))
19768 (defun org-move-line-up (arg)
19769 "Move the current line up. With prefix argument, move it past ARG lines."
19771 (let ((col (current-column))
19773 (beginning-of-line 1) (setq beg (point))
19774 (beginning-of-line 2) (setq end (point))
19775 (beginning-of-line (- arg))
19776 (setq pos (move-marker (make-marker) (point)))
19777 (insert (delete-and-extract-region beg end))
19779 (org-move-to-column col)))
19781 (defun org-replace-escapes (string table)
19782 "Replace %-escapes in STRING with values in TABLE.
19783 TABLE is an association list with keys like \"%a\" and string values.
19784 The sequences in STRING may contain normal field width and padding information,
19785 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
19786 so values can contain further %-escapes if they are define later in TABLE."
19787 (let ((tbl (copy-alist table))
19788 (case-fold-search nil)
19791 (while (setq e (pop tbl))
19792 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
19793 (when (and (cdr e) (string-match re (cdr e)))
19794 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
19796 (add-text-properties 0 3 (list 'sref sref) safe)
19797 (setcdr e (replace-match safe t t (cdr e)))))
19798 (while (string-match re string)
19799 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
19801 (setq string (replace-match rpl t t string))))
19802 (while (setq pchg (next-property-change pchg string))
19803 (let ((sref (get-text-property pchg 'sref string)))
19804 (when (and sref (string-match "SREF" string pchg))
19805 (setq string (replace-match sref t t string)))))
19808 (defun org-sublist (list start end)
19809 "Return a section of LIST, from START to END.
19810 Counting starts at 1."
19811 (let (rtn (c start))
19812 (setq list (nthcdr (1- start) list))
19813 (while (and list (<= c end))
19814 (push (pop list) rtn)
19818 (defun org-find-base-buffer-visiting (file)
19819 "Like `find-buffer-visiting' but always return the base buffer and
19820 not an indirect buffer."
19821 (let ((buf (or (get-file-buffer file)
19822 (find-buffer-visiting file))))
19824 (or (buffer-base-buffer buf) buf)
19827 (defun org-image-file-name-regexp (&optional extensions)
19828 "Return regexp matching the file names of images.
19829 If EXTENSIONS is given, only match these."
19830 (if (and (not extensions) (fboundp 'image-file-name-regexp))
19831 (image-file-name-regexp)
19832 (let ((image-file-name-extensions
19834 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
19835 "xbm" "xpm" "pbm" "pgm" "ppm"))))
19837 (regexp-opt (nconc (mapcar 'upcase
19838 image-file-name-extensions)
19839 image-file-name-extensions)
19843 (defun org-file-image-p (file &optional extensions)
19844 "Return non-nil if FILE is an image."
19846 (string-match (org-image-file-name-regexp extensions) file)))
19848 (defun org-get-cursor-date ()
19849 "Return the date at cursor in as a time.
19850 This works in the calendar and in the agenda, anywhere else it just
19851 returns the current time."
19852 (let (date day defd)
19854 ((eq major-mode 'calendar-mode)
19855 (setq date (calendar-cursor-to-date)
19856 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
19857 ((eq major-mode 'org-agenda-mode)
19858 (setq day (get-text-property (point) 'day))
19860 (setq date (calendar-gregorian-from-absolute day)
19861 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
19863 (or defd (current-time))))
19865 (defvar org-agenda-action-marker (make-marker)
19866 "Marker pointing to the entry for the next agenda action.")
19868 (defun org-mark-entry-for-agenda-action ()
19869 "Mark the current entry as target of an agenda action.
19870 Agenda actions are actions executed from the agenda with the key `k',
19871 which make use of the date at the cursor."
19873 (move-marker org-agenda-action-marker
19874 (save-excursion (org-back-to-heading t) (point))
19877 "Entry marked for action; press `k' at desired date in agenda or calendar"))
19879 (defun org-mark-subtree ()
19880 "Mark the current subtree.
19881 This puts point at the start of the current subtree, and mark at the end.
19883 If point is in an inline task, mark that task instead."
19885 (let ((inline-task-p
19886 (and (featurep 'org-inlinetask)
19887 (org-inlinetask-in-task-p)))
19889 ;; Get beginning of subtree
19891 (inline-task-p (org-inlinetask-goto-beginning))
19892 ((org-at-heading-p) (beginning-of-line))
19893 (t (org-with-limited-levels (outline-previous-visible-heading 1))))
19897 (org-inlinetask-goto-end)
19898 (org-end-of-subtree))
19900 (push-mark (point) nil t)
19903 ;;; Paragraph filling stuff.
19904 ;; We want this to be just right, so use the full arsenal.
19906 (defun org-indent-line-function ()
19907 "Indent line depending on context."
19909 (let* ((pos (point))
19910 (itemp (org-at-item-p))
19911 (case-fold-search t)
19912 (org-drawer-regexp (or org-drawer-regexp "\000"))
19913 (inline-task-p (and (featurep 'org-inlinetask)
19914 (org-inlinetask-in-task-p)))
19915 (inline-re (and inline-task-p
19916 (org-inlinetask-outline-regexp)))
19918 (beginning-of-line 1)
19921 ((looking-at "# ") (setq column 0))
19923 ((looking-at org-outline-regexp) (setq column 0))
19925 ((looking-at "#\\+include:") (setq column 0))
19926 ;; Footnote definition
19927 ((looking-at org-footnote-definition-re) (setq column 0))
19928 ;; Literal examples
19929 ((looking-at "[ \t]*:\\( \\|$\\)")
19930 (setq column (org-get-indentation))) ; do nothing
19932 ((ignore-errors (goto-char (org-in-item-p)))
19933 (setq column (if itemp
19934 (org-get-indentation)
19935 (org-list-item-body-column (point))))
19938 ((and (looking-at "[ \t]*:END:")
19939 (save-excursion (re-search-backward org-drawer-regexp nil t)))
19941 (goto-char (1- (match-beginning 1)))
19942 (setq column (current-column))))
19944 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
19946 (re-search-backward
19947 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
19948 (setq column (org-get-indentation (match-string 0))))
19949 ((and (not (looking-at "[ \t]*#\\+begin_"))
19950 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
19952 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
19954 (cond ((equal (downcase (match-string 1)) "src")
19955 ;; src blocks: let `org-edit-src-exit' handle them
19956 (org-get-indentation))
19957 ((equal (downcase (match-string 1)) "example")
19958 (max (org-get-indentation)
19959 (org-get-indentation (match-string 0))))
19961 (org-get-indentation (match-string 0))))))
19962 ;; This line has nothing special, look at the previous relevant
19963 ;; line to compute indentation
19965 (beginning-of-line 0)
19966 (while (and (not (bobp))
19967 (not (looking-at org-drawer-regexp))
19968 ;; When point started in an inline task, do not move
19969 ;; above task starting line.
19970 (not (and inline-task-p (looking-at inline-re)))
19971 ;; Skip drawers, blocks, empty lines, verbatim,
19972 ;; comments, tables, footnotes definitions, lists,
19974 (or (and (looking-at "[ \t]*:END:")
19975 (re-search-backward org-drawer-regexp nil t))
19976 (and (looking-at "[ \t]*#\\+end_")
19977 (re-search-backward "[ \t]*#\\+begin_"nil t))
19978 (looking-at "[ \t]*[\n:#|]")
19979 (looking-at org-footnote-definition-re)
19980 (and (ignore-errors (goto-char (org-in-item-p)))
19982 (org-list-get-top-point (org-list-struct))))
19983 (and (not inline-task-p)
19984 (featurep 'org-inlinetask)
19985 (org-inlinetask-in-task-p)
19986 (or (org-inlinetask-goto-beginning) t))))
19987 (beginning-of-line 0))
19989 ;; There was an heading above.
19990 ((looking-at "\\*+[ \t]+")
19991 (if (not org-adapt-indentation)
19993 (goto-char (match-end 0))
19994 (setq column (current-column))))
19995 ;; A drawer had started and is unfinished
19996 ((looking-at org-drawer-regexp)
19997 (goto-char (1- (match-beginning 1)))
19998 (setq column (current-column)))
19999 ;; Else, nothing noticeable found: get indentation and go on.
20000 (t (setq column (org-get-indentation))))))
20001 ;; Now apply indentation and move cursor accordingly
20003 (if (<= (current-column) (current-indentation))
20004 (org-indent-line-to column)
20005 (save-excursion (org-indent-line-to column)))
20006 ;; Special polishing for properties, see `org-property-format'
20007 (setq column (current-column))
20008 (beginning-of-line 1)
20010 "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20011 (replace-match (concat (match-string 1)
20012 (format org-property-format
20013 (match-string 2) (match-string 3)))
20015 (org-move-to-column column)))
20017 (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
20018 "Variable to store copy of `adaptive-fill-regexp'.
20019 Since `adaptive-fill-regexp' is set to never match, we need to
20020 store a backup of its value before entering `org-mode' so that
20021 the functionality can be provided as a fall-back.")
20023 (defun org-set-autofill-regexps ()
20025 ;; In the paragraph separator we include headlines, because filling
20026 ;; text in a line directly attached to a headline would otherwise
20027 ;; fill the headline as well.
20028 (org-set-local 'comment-start-skip "^#+[ \t]*")
20029 (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
20030 ;; The paragraph starter includes hand-formatted lists.
20036 org-outline-regexp "\\|"
20038 (org-item-re) "\\|"
20041 "\\\\\\(begin\\|end\\|[][]\\)"))
20042 ;; Inhibit auto-fill for headers, tables and fixed-width lines.
20043 ;; But only if the user has not turned off tables or fixed-width regions
20045 'auto-fill-inhibit-regexp
20046 (concat org-outline-regexp
20048 "\\|[ \t]*" org-keyword-time-regexp
20049 (if (or org-enable-table-editor org-enable-fixed-width-editor)
20052 (if org-enable-table-editor "|" "")
20053 (if org-enable-fixed-width-editor ":" "")
20055 ;; We use our own fill-paragraph function, to make sure that tables
20056 ;; and fixed-width regions are not wrapped. That function will pass
20057 ;; through to `fill-paragraph' when appropriate.
20058 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20059 ;; Prevent auto-fill from inserting unwanted new items.
20060 (if (boundp 'fill-nobreak-predicate)
20061 (org-set-local 'fill-nobreak-predicate
20062 (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
20063 fill-nobreak-predicate
20064 (cons 'org-fill-item-nobreak-p fill-nobreak-predicate))))
20065 ;; Adaptive filling: To get full control, first make sure that
20066 ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
20067 (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
20068 (org-set-local 'org-adaptive-fill-regexp-backup
20069 adaptive-fill-regexp))
20070 (org-set-local 'adaptive-fill-regexp "\000")
20071 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20072 (org-set-local 'adaptive-fill-function
20073 'org-adaptive-fill-function)
20075 'align-mode-rules-list
20076 '((org-in-buffer-settings
20077 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
20078 (modes . '(org-mode))))))
20080 (defun org-fill-item-nobreak-p ()
20081 "Non-nil when a line break at point would insert a new item."
20082 (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
20084 (defun org-fill-paragraph (&optional justify)
20085 "Re-align a table, pass through to fill-paragraph if no table."
20086 (let ((table-p (org-at-table-p))
20087 (table.el-p (org-at-table.el-p))
20088 (itemp (org-in-item-p)))
20089 (cond ((and (equal (char-after (point-at-bol)) ?*)
20090 (save-excursion (goto-char (point-at-bol))
20091 (looking-at org-outline-regexp)))
20092 t) ; skip headlines
20093 (table.el-p t) ; skip table.el tables
20094 (table-p (org-table-align) t) ; align Org tables
20095 (itemp ; align text in items
20096 (let* ((struct (save-excursion (goto-char itemp)
20097 (org-list-struct)))
20098 (parents (org-list-parents-alist struct))
20099 (children (org-list-get-children itemp struct parents))
20100 beg end prev next prefix)
20101 ;; Determine in which part of item point is: before
20102 ;; first child, after last child, between two
20103 ;; sub-lists, or simply in item if there's no child.
20106 (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
20108 end (org-list-get-item-end itemp struct)))
20109 ((< (point) (setq next (car children)))
20110 (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
20113 ((> (point) (setq prev (car (last children))))
20114 (setq beg (org-list-get-item-end prev struct)
20115 end (org-list-get-item-end itemp struct)
20116 prefix (save-excursion
20118 (skip-chars-forward " \t")
20119 (make-string (current-column) ?\ ))))
20121 (while (setq next (pop children))
20122 (if (> (point) next)
20124 (setq beg (org-list-get-item-end prev struct)
20126 prefix (save-excursion
20128 (skip-chars-forward " \t")
20129 (make-string (current-column) ?\ )))
20130 (throw 'exit nil))))))
20131 ;; Use `fill-paragraph' with buffer narrowed to item
20132 ;; without any child, and with our computed PREFIX.
20133 (flet ((fill-context-prefix (from to &optional flr) prefix))
20135 (narrow-to-region beg end)
20136 (save-excursion (fill-paragraph justify)))) t))
20137 ;; Special case where point is not in a list but is on
20138 ;; a paragraph adjacent to a list: make sure this paragraph
20139 ;; doesn't get merged with the end of the list by narrowing
20141 ((save-excursion (forward-paragraph -1)
20142 (setq itemp (org-in-item-p)))
20143 (let ((struct (save-excursion (goto-char itemp)
20144 (org-list-struct))))
20146 (narrow-to-region (org-list-get-bottom-point struct)
20147 (save-excursion (forward-paragraph 1)
20149 (fill-paragraph justify) t)))
20150 ;; Else simply call `fill-paragraph'.
20153 ;; For reference, this is the default value of adaptive-fill-regexp
20154 ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
20156 (defun org-adaptive-fill-function ()
20157 "Return a fill prefix for org-mode files."
20162 ((looking-at "#[ \t]+")
20163 (match-string-no-properties 0))
20166 (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
20167 ;; Point is in a list after `backward-paragraph': original
20168 ;; point wasn't in the list, or filling would have been taken
20169 ;; care of by `org-auto-fill-function', but the list and the
20170 ;; real paragraph are not separated by a blank line. Thus, move
20171 ;; point after the list to go back to real paragraph and
20172 ;; determine fill-prefix.
20173 ((setq itemp (org-in-item-p))
20175 (let* ((struct (org-list-struct))
20176 (bottom (org-list-get-bottom-point struct)))
20178 (make-string (org-get-indentation) ?\ )))
20180 ((looking-at org-adaptive-fill-regexp-backup)
20181 (match-string-no-properties 0))))))
20183 (defun org-auto-fill-function ()
20184 "Auto-fill function."
20185 (let (itemp prefix)
20186 ;; When in a list, compute an appropriate fill-prefix and make
20187 ;; sure it will be used by `do-auto-fill'.
20188 (if (setq itemp (org-in-item-p))
20190 (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
20191 (flet ((fill-context-prefix (from to &optional flr) prefix))
20193 ;; Else just use `do-auto-fill'.
20198 (defun org-toggle-fixed-width-section (arg)
20199 "Toggle the fixed-width export.
20200 If there is no active region, the QUOTE keyword at the current headline is
20201 inserted or removed. When present, it causes the text between this headline
20202 and the next to be exported as fixed-width text, and unmodified.
20203 If there is an active region, this command adds or removes a colon as the
20204 first character of this line. If the first character of a line is a colon,
20205 this line is also exported in fixed-width font."
20208 (regionp (org-region-active-p))
20209 (beg (if regionp (region-beginning) (point)))
20210 (end (if regionp (region-end)))
20211 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
20212 (case-fold-search nil)
20213 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
20218 (setq cc (current-column))
20219 (beginning-of-line 1)
20220 (setq off (looking-at re))
20221 (while (> nlines 0)
20222 (setq nlines (1- nlines))
20223 (beginning-of-line 1)
20226 (org-move-to-column cc t)
20229 ((and off (looking-at re))
20230 (replace-match "" t t nil 1))
20231 ((not off) (org-move-to-column cc t) (insert ": ")))
20234 (org-back-to-heading)
20236 ((looking-at (format org-heading-keyword-regexp-format
20238 (goto-char (match-end 1))
20239 (looking-at (concat " +" org-quote-string))
20240 (replace-match "" t t)
20241 (when (eolp) (insert " ")))
20242 ((looking-at org-outline-regexp)
20243 (goto-char (match-end 0))
20244 (insert org-quote-string " ")))))))
20246 (defun org-reftex-citation ()
20247 "Use reftex-citation to insert a citation into the buffer.
20248 This looks for a line like
20250 #+BIBLIOGRAPHY: foo plain option:-d
20252 and derives from it that foo.bib is the bibliography file relevant
20253 for this document. It then installs the necessary environment for RefTeX
20254 to work in this buffer and calls `reftex-citation' to insert a citation
20257 Export of such citations to both LaTeX and HTML is handled by the contributed
20258 package org-exp-bibtex by Taru Karttunen."
20260 (let ((reftex-docstruct-symbol 'rds)
20261 (reftex-cite-format "\\cite{%l}")
20266 (let ((case-fold-search t)
20267 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
20268 (if (not (save-excursion
20269 (or (re-search-forward re nil t)
20270 (re-search-backward re nil t))))
20271 (error "No bibliography defined in file")
20272 (setq bib (concat (match-string 1) ".bib")
20273 rds (list (list 'bib bib)))))))
20274 (call-interactively 'reftex-citation)))
20276 ;;;; Functions extending outline functionality
20278 (defun org-beginning-of-line (&optional arg)
20279 "Go to the beginning of the current line. If that is invisible, continue
20280 to a visible line beginning. This makes the function of C-a more intuitive.
20281 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
20282 first attempt, and only move to after the tags when the cursor is already
20283 beyond the end of the headline."
20285 (let ((pos (point))
20286 (special (if (consp org-special-ctrl-a/e)
20287 (car org-special-ctrl-a/e)
20288 org-special-ctrl-a/e))
20290 (if (org-bound-and-true-p line-move-visual)
20291 (beginning-of-visual-line 1)
20292 (beginning-of-line 1))
20293 (if (and arg (fboundp 'move-beginning-of-line))
20294 (call-interactively 'move-beginning-of-line)
20298 (if (org-truely-invisible-p)
20299 (while (and (not (bobp)) (org-truely-invisible-p))
20301 (beginning-of-line 1))
20302 (forward-char 1))))
20305 ((and (looking-at org-complex-heading-regexp)
20306 (= (char-after (match-end 1)) ?\ ))
20307 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
20311 (cond ((> pos refpos) refpos)
20312 ((= pos (point)) refpos)
20314 (cond ((> pos (point)) (point))
20315 ((not (eq last-command this-command)) (point))
20320 (cond ((> pos (match-end 0)) (match-end 0))
20321 ((= pos (point)) (match-end 0))
20323 (cond ((> pos (point)) (point))
20324 ((not (eq last-command this-command)) (point))
20325 (t (match-end 0))))))))
20327 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
20329 (defun org-end-of-line (&optional arg)
20330 "Go to the end of the line.
20331 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
20332 first attempt, and only move to after the tags when the cursor is already
20333 beyond the end of the headline."
20335 (let ((special (if (consp org-special-ctrl-a/e)
20336 (cdr org-special-ctrl-a/e)
20337 org-special-ctrl-a/e)))
20339 ((or (not special) arg
20340 (not (or (org-on-heading-p) (org-at-item-p))))
20341 (call-interactively
20342 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
20343 ((fboundp 'move-end-of-line) 'move-end-of-line)
20344 (t 'end-of-line))))
20345 ((org-on-heading-p)
20346 (let ((pos (point)))
20347 (beginning-of-line 1)
20348 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
20350 (if (or (< pos (match-beginning 1))
20351 (= pos (match-end 0)))
20352 (goto-char (match-beginning 1))
20353 (goto-char (match-end 0)))
20354 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
20355 (goto-char (match-end 0))
20356 (goto-char (match-beginning 1))))
20357 (call-interactively (if (fboundp 'move-end-of-line)
20360 ;; At an item: Move before any hidden text.
20361 (t (call-interactively 'end-of-line)))
20363 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
20365 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
20366 (define-key org-mode-map "\C-e" 'org-end-of-line)
20368 (defun org-backward-sentence (&optional arg)
20369 "Go to beginning of sentence, or beginning of table field.
20370 This will call `backward-sentence' or `org-table-beginning-of-field',
20371 depending on context."
20374 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
20375 (t (call-interactively 'backward-sentence))))
20377 (defun org-forward-sentence (&optional arg)
20378 "Go to end of sentence, or end of table field.
20379 This will call `forward-sentence' or `org-table-end-of-field',
20380 depending on context."
20383 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
20384 (t (call-interactively 'forward-sentence))))
20386 (define-key org-mode-map "\M-a" 'org-backward-sentence)
20387 (define-key org-mode-map "\M-e" 'org-forward-sentence)
20389 (defun org-kill-line (&optional arg)
20390 "Kill line, to tags or end of line."
20393 ((or (not org-special-ctrl-k)
20395 (not (org-on-heading-p)))
20396 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
20397 org-ctrl-k-protect-subtree)
20398 (if (or (eq org-ctrl-k-protect-subtree 'error)
20399 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
20400 (error "C-k aborted - would kill hidden subtree")))
20401 (call-interactively 'kill-line))
20402 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
20403 (kill-region (point) (match-beginning 1))
20404 (org-set-tags nil t))
20405 (t (kill-region (point) (point-at-eol)))))
20407 (define-key org-mode-map "\C-k" 'org-kill-line)
20409 (defun org-yank (&optional arg)
20410 "Yank. If the kill is a subtree, treat it specially.
20411 This command will look at the current kill and check if is a single
20412 subtree, or a series of subtrees[1]. If it passes the test, and if the
20413 cursor is at the beginning of a line or after the stars of a currently
20414 empty headline, then the yank is handled specially. How exactly depends
20415 on the value of the following variables, both set by default.
20417 org-yank-folded-subtrees
20418 When set, the subtree(s) will be folded after insertion, but only
20419 if doing so would now swallow text after the yanked text.
20421 org-yank-adjusted-subtrees
20422 When set, the subtree will be promoted or demoted in order to
20423 fit into the local outline tree structure, which means that the level
20424 will be adjusted so that it becomes the smaller one of the two
20425 *visible* surrounding headings.
20427 Any prefix to this command will cause `yank' to be called directly with
20428 no special treatment. In particular, a simple \\[universal-argument] prefix \
20430 plainly yank the text as it is.
20432 \[1] The test checks if the first non-white line is a heading
20433 and if there are no other headings with fewer stars."
20435 (org-yank-generic 'yank arg))
20437 (defun org-yank-generic (command arg)
20438 "Perform some yank-like command.
20440 This function implements the behavior described in the `org-yank'
20441 documentation. However, it has been generalized to work for any
20442 interactive command with similar behavior."
20444 ;; pretend to be command COMMAND
20445 (setq this-command command)
20448 (call-interactively command)
20450 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
20451 (and (org-kill-is-subtree-p)
20453 (and (looking-at "[ \t]*$")
20456 (buffer-substring (point-at-bol) (point)))))))
20459 ((and subtreep org-yank-folded-subtrees)
20460 (let ((beg (point))
20462 (if (and subtreep org-yank-adjusted-subtrees)
20463 (org-paste-subtree nil nil 'for-yank)
20464 (call-interactively command))
20468 (when (and (bolp) subtreep
20469 (not (setq swallowp
20470 (org-yank-folding-would-swallow-text beg end))))
20471 (org-with-limited-levels
20472 (or (looking-at org-outline-regexp)
20473 (re-search-forward org-outline-regexp-bol end t))
20474 (while (and (< (point) end) (looking-at org-outline-regexp))
20476 (org-cycle-show-empty-lines 'folded)
20477 (condition-case nil
20478 (outline-forward-same-level 1)
20479 (error (goto-char end))))))
20482 "Inserted text not folded because that would swallow text"))
20485 (skip-chars-forward " \t\n\r")
20486 (beginning-of-line 1)
20487 (push-mark beg 'nomsg)))
20488 ((and subtreep org-yank-adjusted-subtrees)
20489 (let ((beg (point-at-bol)))
20490 (org-paste-subtree nil nil 'for-yank)
20491 (push-mark beg 'nomsg)))
20493 (call-interactively command))))))
20495 (defun org-yank-folding-would-swallow-text (beg end)
20496 "Would hide-subtree at BEG swallow any text after END?"
20498 (org-with-limited-levels
20501 (when (or (looking-at org-outline-regexp)
20502 (re-search-forward org-outline-regexp-bol end t))
20503 (setq level (org-outline-level)))
20505 (skip-chars-forward " \t\r\n\v\f")
20507 (and (bolp) (looking-at org-outline-regexp)
20508 (<= (org-outline-level) level)))
20509 nil ; Nothing would be swallowed
20510 t))))) ; something would swallow
20512 (define-key org-mode-map "\C-y" 'org-yank)
20514 (defun org-truely-invisible-p ()
20515 "Check if point is at a character currently not visible.
20516 This version does not only check the character property, but also
20518 ;; Early versions of noutline don't have `outline-invisible-p'.
20519 (if (org-bound-and-true-p visible-mode)
20521 (outline-invisible-p)))
20523 (defun org-invisible-p2 ()
20524 "Check if point is at a character currently not visible."
20526 (if (and (eolp) (not (bobp))) (backward-char 1))
20527 ;; Early versions of noutline don't have `outline-invisible-p'.
20528 (outline-invisible-p)))
20530 (defun org-back-to-heading (&optional invisible-ok)
20531 "Call `outline-back-to-heading', but provide a better error message."
20532 (condition-case nil
20533 (outline-back-to-heading invisible-ok)
20534 (error (error "Before first headline at position %d in buffer %s"
20535 (point) (current-buffer)))))
20537 (defun org-beginning-of-defun ()
20538 "Go to the beginning of the subtree, i.e. back to the heading."
20539 (org-back-to-heading))
20540 (defun org-end-of-defun ()
20541 "Go to the end of the subtree."
20542 (org-end-of-subtree nil t))
20544 (defun org-before-first-heading-p ()
20545 "Before first heading?"
20548 (null (re-search-backward org-outline-regexp-bol nil t))))
20550 (defun org-on-heading-p (&optional ignored)
20551 (outline-on-heading-p t))
20552 (defun org-at-heading-p (&optional ignored)
20553 (outline-on-heading-p t))
20555 (defun org-point-at-end-of-empty-headline ()
20556 "If point is at the end of an empty headline, return t, else nil.
20557 If the heading only contains a TODO keyword, it is still still considered
20559 (and (looking-at "[ \t]*$")
20561 (beginning-of-line 1)
20562 (let ((case-fold-search nil))
20563 (looking-at org-todo-line-regexp)))
20564 (string= (match-string 3) "")))
20566 (defun org-at-heading-or-item-p ()
20567 (or (org-on-heading-p) (org-at-item-p)))
20569 (defun org-on-target-p ()
20570 (or (org-in-regexp org-radio-target-regexp)
20571 (org-in-regexp org-target-regexp)))
20573 (defun org-up-heading-all (arg)
20574 "Move to the heading line of which the present line is a subheading.
20575 This function considers both visible and invisible heading lines.
20576 With argument, move up ARG levels."
20577 (if (fboundp 'outline-up-heading-all)
20578 (outline-up-heading-all arg) ; emacs 21 version of outline.el
20579 (outline-up-heading arg t))) ; emacs 22 version of outline.el
20581 (defun org-up-heading-safe ()
20582 "Move to the heading line of which the present line is a subheading.
20583 This version will not throw an error. It will return the level of the
20584 headline found, or nil if no higher level is found.
20586 Also, this function will be a lot faster than `outline-up-heading',
20587 because it relies on stars being the outline starters. This can really
20588 make a significant difference in outlines with very many siblings."
20589 (let (start-level re)
20590 (org-back-to-heading t)
20591 (setq start-level (funcall outline-level))
20592 (if (equal start-level 1)
20594 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
20595 (if (re-search-backward re nil t)
20596 (funcall outline-level)))))
20598 (defun org-first-sibling-p ()
20599 "Is this heading the first child of its parents?"
20601 (let ((re org-outline-regexp-bol)
20603 (unless (org-at-heading-p t)
20604 (error "Not at a heading"))
20605 (setq level (funcall outline-level))
20607 (if (not (re-search-backward re nil t))
20609 (setq l (funcall outline-level))
20612 (defun org-goto-sibling (&optional previous)
20613 "Goto the next sibling, even if it is invisible.
20614 When PREVIOUS is set, go to the previous sibling instead. Returns t
20615 when a sibling was found. When none is found, return nil and don't
20617 (let ((fun (if previous 're-search-backward 're-search-forward))
20619 (re org-outline-regexp-bol)
20621 (when (condition-case nil (org-back-to-heading t) (error nil))
20622 (setq level (funcall outline-level))
20624 (or previous (forward-char 1))
20625 (while (funcall fun re nil t)
20626 (setq l (funcall outline-level))
20627 (when (< l level) (goto-char pos) (throw 'exit nil))
20628 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
20632 (defun org-show-siblings ()
20633 "Show all siblings of the current headline."
20635 (while (org-goto-sibling) (org-flag-heading nil)))
20637 (while (org-goto-sibling 'previous)
20638 (org-flag-heading nil))))
20640 (defun org-goto-first-child ()
20641 "Goto the first child, even if it is invisible.
20642 Return t when a child was found. Otherwise don't move point and
20644 (let (level (pos (point)) (re org-outline-regexp-bol))
20645 (when (condition-case nil (org-back-to-heading t) (error nil))
20646 (setq level (outline-level))
20648 (if (and (re-search-forward re nil t) (> (outline-level) level))
20649 (progn (goto-char (match-beginning 0)) t)
20650 (goto-char pos) nil))))
20652 (defun org-show-hidden-entry ()
20653 "Show an entry where even the heading is hidden."
20657 (defun org-flag-heading (flag &optional entry)
20658 "Flag the current heading. FLAG non-nil means make invisible.
20659 When ENTRY is non-nil, show the entire entry."
20661 (org-back-to-heading t)
20662 ;; Check if we should show the entire entry
20667 (and (outline-next-heading)
20668 (org-flag-heading nil))))
20669 (outline-flag-region (max (point-min) (1- (point)))
20670 (save-excursion (outline-end-of-heading) (point))
20673 (defun org-get-next-sibling ()
20674 "Move to next heading of the same level, and return point.
20675 If there is no such heading, return nil.
20676 This is like outline-next-sibling, but invisible headings are ok."
20677 (let ((level (funcall outline-level)))
20678 (outline-next-heading)
20679 (while (and (not (eobp)) (> (funcall outline-level) level))
20680 (outline-next-heading))
20681 (if (or (eobp) (< (funcall outline-level) level))
20685 (defun org-get-last-sibling ()
20686 "Move to previous heading of the same level, and return point.
20687 If there is no such heading, return nil."
20688 (let ((opoint (point))
20689 (level (funcall outline-level)))
20690 (outline-previous-heading)
20691 (when (and (/= (point) opoint) (outline-on-heading-p t))
20692 (while (and (> (funcall outline-level) level)
20694 (outline-previous-heading))
20695 (if (< (funcall outline-level) level)
20699 (defun org-end-of-subtree (&optional invisible-OK to-heading)
20700 ;; This contains an exact copy of the original function, but it uses
20701 ;; `org-back-to-heading', to make it work also in invisible
20702 ;; trees. And is uses an invisible-OK argument.
20703 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
20704 ;; Furthermore, when used inside Org, finding the end of a large subtree
20705 ;; with many children and grandchildren etc, this can be much faster
20706 ;; than the outline version.
20707 (org-back-to-heading invisible-OK)
20709 (level (funcall outline-level)))
20710 (if (and (eq major-mode 'org-mode) (< level 1000))
20711 ;; A true heading (not a plain list item), in Org-mode
20712 ;; This means we can easily find the end by looking
20713 ;; only for the right number of stars. Using a regexp to do
20714 ;; this is so much faster than using a Lisp loop.
20715 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
20717 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
20718 ;; something else, do it the slow way
20719 (while (and (not (eobp))
20720 (or first (> (funcall outline-level) level)))
20722 (outline-next-heading)))
20724 (if (memq (preceding-char) '(?\n ?\^M))
20726 ;; Go to end of line before heading
20728 (if (memq (preceding-char) '(?\n ?\^M))
20729 ;; leave blank line before heading
20730 (forward-char -1))))))
20733 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
20734 "Use Org version in org-mode, for dramatic speed-up."
20735 (if (eq major-mode 'org-mode)
20737 (org-end-of-subtree nil t)
20738 (unless (eobp) (backward-char 1)))
20741 (defun org-end-of-meta-data-and-drawers ()
20742 "Jump to the first text after meta data and drawers in the current entry.
20743 This will move over empty lines, lines with planning time stamps,
20744 clocking lines, and drawers."
20745 (org-back-to-heading t)
20746 (let ((end (save-excursion (outline-next-heading) (point)))
20747 (re (concat "\\(" org-drawer-regexp "\\)"
20748 "\\|" "[ \t]*" org-keyword-time-regexp)))
20750 (while (re-search-forward re end t)
20751 (if (not (match-end 1))
20752 ;; empty or planning line
20754 ;; a drawer, find the end
20755 (re-search-forward "^[ \t]*:END:" end 'move)
20757 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
20760 (defun org-forward-same-level (arg &optional invisible-ok)
20761 "Move forward to the arg'th subheading at same level as this one.
20762 Stop at the first and last subheadings of a superior heading.
20763 Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
20764 it wil also look at invisible ones."
20766 (org-back-to-heading invisible-ok)
20768 (let* ((level (- (match-end 0) (match-beginning 0) 1))
20769 (re (format "^\\*\\{1,%d\\} " level))
20773 (while (and (re-search-forward re nil 'move)
20774 (setq l (- (match-end 0) (match-beginning 0) 1))
20777 (progn (backward-char 1) (outline-invisible-p)))
20778 (if (< l level) (setq arg 1)))
20779 (setq arg (1- arg)))
20780 (beginning-of-line 1)))
20782 (defun org-backward-same-level (arg &optional invisible-ok)
20783 "Move backward to the arg'th subheading at same level as this one.
20784 Stop at the first and last subheadings of a superior heading."
20786 (org-back-to-heading)
20788 (let* ((level (- (match-end 0) (match-beginning 0) 1))
20789 (re (format "^\\*\\{1,%d\\} " level))
20792 (while (and (re-search-backward re nil 'move)
20793 (setq l (- (match-end 0) (match-beginning 0) 1))
20796 (outline-invisible-p))
20797 (if (< l level) (setq arg 1)))
20798 (setq arg (1- arg)))))
20800 (defun org-show-subtree ()
20801 "Show everything after this heading at deeper levels."
20802 (outline-flag-region
20805 (org-end-of-subtree t t))
20808 (defun org-show-entry ()
20809 "Show the body directly following this heading.
20810 Show the heading too, if it is currently invisible."
20813 (condition-case nil
20815 (org-back-to-heading t)
20816 (outline-flag-region
20817 (max (point-min) (1- (point)))
20819 (if (re-search-forward
20820 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
20821 (match-beginning 1)
20824 (org-cycle-hide-drawers 'children))
20827 (defun org-make-options-regexp (kwds &optional extra)
20828 "Make a regular expression for keyword lines."
20832 (mapconcat 'regexp-quote kwds "\\|")
20833 (if extra (concat "\\|" extra))
20837 ;; Make isearch reveal the necessary context
20838 (defun org-isearch-end ()
20839 "Reveal context after isearch exits."
20840 (when isearch-success ; only if search was successful
20841 (if (featurep 'xemacs)
20842 ;; Under XEmacs, the hook is run in the correct place,
20843 ;; we directly show the context.
20844 (org-show-context 'isearch)
20845 ;; In Emacs the hook runs *before* restoring the overlays.
20846 ;; So we have to use a one-time post-command-hook to do this.
20847 ;; (Emacs 22 has a special variable, see function `org-mode')
20848 (unless (and (boundp 'isearch-mode-end-hook-quit)
20849 isearch-mode-end-hook-quit)
20850 ;; Only when the isearch was not quitted.
20851 (org-add-hook 'post-command-hook 'org-isearch-post-command
20852 'append 'local)))))
20854 (defun org-isearch-post-command ()
20855 "Remove self from hook, and show context."
20856 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
20857 (org-show-context 'isearch))
20860 ;;;; Integration with and fixes for other packages
20864 (defvar org-imenu-markers nil
20865 "All markers currently used by Imenu.")
20866 (make-variable-buffer-local 'org-imenu-markers)
20868 (defun org-imenu-new-marker (&optional pos)
20869 "Return a new marker for use by Imenu, and remember the marker."
20870 (let ((m (make-marker)))
20871 (move-marker m (or pos (point)))
20872 (push m org-imenu-markers)
20875 (defun org-imenu-get-tree ()
20876 "Produce the index for Imenu."
20877 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
20878 (setq org-imenu-markers nil)
20879 (let* ((n org-imenu-depth)
20880 (re (concat "^" (org-get-limited-outline-regexp)))
20881 (subs (make-vector (1+ n) nil))
20887 (goto-char (point-max))
20888 (while (re-search-backward re nil t)
20889 (setq level (org-reduced-level (funcall outline-level)))
20891 (looking-at org-complex-heading-regexp)
20892 (setq head (org-link-display-format
20893 (org-match-string-no-properties 4))
20894 m (org-imenu-new-marker))
20895 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
20896 (if (>= level last-level)
20897 (push (cons head m) (aref subs level))
20898 (push (cons head (aref subs (1+ level))) (aref subs level))
20899 (loop for i from (1+ level) to n do (aset subs i nil)))
20900 (setq last-level level)))))
20903 (eval-after-load "imenu"
20905 (add-hook 'imenu-after-jump-hook
20907 (if (eq major-mode 'org-mode)
20908 (org-show-context 'org-goto))))))
20910 (defun org-link-display-format (link)
20911 "Replace a link with either the description, or the link target
20912 if no description is present"
20914 (if (string-match org-bracket-link-analytic-regexp link)
20915 (replace-match (if (match-end 5)
20916 (match-string 5 link)
20917 (concat (match-string 1 link)
20918 (match-string 3 link)))
20922 (defun org-toggle-link-display ()
20923 "Toggle the literal or descriptive display of links."
20925 (if org-descriptive-links
20926 (progn (org-remove-from-invisibility-spec '(org-link))
20927 (org-restart-font-lock)
20928 (setq org-descriptive-links nil))
20929 (progn (add-to-invisibility-spec '(org-link))
20930 (org-restart-font-lock)
20931 (setq org-descriptive-links t))))
20933 ;; Speedbar support
20935 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
20936 "Overlay marking the agenda restriction line in speedbar.")
20937 (overlay-put org-speedbar-restriction-lock-overlay
20938 'face 'org-agenda-restriction-lock)
20939 (overlay-put org-speedbar-restriction-lock-overlay
20940 'help-echo "Agendas are currently limited to this item.")
20941 (org-detach-overlay org-speedbar-restriction-lock-overlay)
20943 (defun org-speedbar-set-agenda-restriction ()
20944 "Restrict future agenda commands to the location at point in speedbar.
20945 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
20947 (require 'org-agenda)
20948 (let (p m tp np dir txt)
20950 ((setq p (text-property-any (point-at-bol) (point-at-eol)
20952 (setq m (get-text-property p 'org-imenu-marker))
20953 (with-current-buffer (marker-buffer m)
20955 (org-agenda-set-restriction-lock 'subtree)))
20956 ((setq p (text-property-any (point-at-bol) (point-at-eol)
20957 'speedbar-function 'speedbar-find-file))
20958 (setq tp (previous-single-property-change
20959 (1+ p) 'speedbar-function)
20960 np (next-single-property-change
20961 tp 'speedbar-function)
20962 dir (speedbar-line-directory)
20963 txt (buffer-substring-no-properties (or tp (point-min))
20964 (or np (point-max))))
20965 (with-current-buffer (find-file-noselect
20966 (let ((default-directory dir))
20967 (expand-file-name txt)))
20968 (unless (eq major-mode 'org-mode)
20969 (error "Cannot restrict to non-Org-mode file"))
20970 (org-agenda-set-restriction-lock 'file)))
20971 (t (error "Don't know how to restrict Org-mode's agenda")))
20972 (move-overlay org-speedbar-restriction-lock-overlay
20973 (point-at-bol) (point-at-eol))
20974 (setq current-prefix-arg nil)
20975 (org-agenda-maybe-redo)))
20977 (eval-after-load "speedbar"
20979 (speedbar-add-supported-extension ".org")
20980 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
20981 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
20982 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
20983 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
20984 (add-hook 'speedbar-visiting-tag-hook
20985 (lambda () (and (eq major-mode 'org-mode) (org-show-context 'org-goto))))))
20987 ;;; Fixes and Hacks for problems with other packages
20989 ;; Make flyspell not check words in links, to not mess up our keymap
20990 (defun org-mode-flyspell-verify ()
20991 "Don't let flyspell put overlays at active buttons, or on
20992 {todo,all-time,additional-option-like}-keywords."
20993 (let ((pos (max (1- (point)) (point-min)))
20994 (word (thing-at-point 'word)))
20995 (and (not (get-text-property pos 'keymap))
20996 (not (get-text-property pos 'org-no-flyspell))
20997 (not (member word org-todo-keywords-1))
20998 (not (member word org-all-time-keywords))
20999 (not (member word org-additional-option-like-keywords)))))
21001 (defun org-remove-flyspell-overlays-in (beg end)
21002 "Remove flyspell overlays in region."
21003 (and (org-bound-and-true-p flyspell-mode)
21004 (fboundp 'flyspell-delete-region-overlays)
21005 (flyspell-delete-region-overlays beg end))
21006 (add-text-properties beg end '(org-no-flyspell t)))
21008 ;; Make `bookmark-jump' shows the jump location if it was hidden.
21009 (eval-after-load "bookmark"
21010 '(if (boundp 'bookmark-after-jump-hook)
21011 ;; We can use the hook
21012 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
21013 ;; Hook not available, use advice
21014 (defadvice bookmark-jump (after org-make-visible activate)
21015 "Make the position visible."
21016 (org-bookmark-jump-unhide))))
21018 ;; Make sure saveplace shows the location if it was hidden
21019 (eval-after-load "saveplace"
21020 '(defadvice save-place-find-file-hook (after org-make-visible activate)
21021 "Make the position visible."
21022 (org-bookmark-jump-unhide)))
21024 ;; Make sure ecb shows the location if it was hidden
21025 (eval-after-load "ecb"
21026 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
21027 "Make hierarchy visible when jumping into location from ECB tree buffer."
21028 (if (eq major-mode 'org-mode)
21029 (org-show-context))))
21031 (defun org-bookmark-jump-unhide ()
21032 "Unhide the current position, to show the bookmark location."
21033 (and (eq major-mode 'org-mode)
21034 (or (outline-invisible-p)
21035 (save-excursion (goto-char (max (point-min) (1- (point))))
21036 (outline-invisible-p)))
21037 (org-show-context 'bookmark-jump)))
21039 ;; Make session.el ignore our circular variable
21040 (eval-after-load "session"
21041 '(add-to-list 'session-globals-exclude 'org-mark-ring))
21043 ;;;; Experimental code
21045 (defun org-closed-in-range ()
21046 "Sparse tree of items closed in a certain time range.
21047 Still experimental, may disappear in the future."
21049 ;; Get the time interval from the user.
21050 (let* ((time1 (org-float-time
21051 (org-read-date nil 'to-time nil "Starting date: ")))
21052 (time2 (org-float-time
21053 (org-read-date nil 'to-time nil "End date:")))
21054 ;; callback function
21055 (callback (lambda ()
21058 (apply 'encode-time
21059 (org-parse-time-string
21060 (match-string 1))))))
21061 ;; check if time in interval
21062 (and (>= time time1) (<= time time2))))))
21063 ;; make tree, check each match with the callback
21064 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
21070 (run-hooks 'org-load-hook)
21072 ;;; org.el ends here