1 ;;; org.el --- Outline-based notes management and organizer
3 ;; Carstens outline-mode for keeping track of everything.
4 ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
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 (require 'format-spec
)
81 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
82 ;; some places -- e.g. see the macro org-with-limited-levels.
84 ;; In Org buffers, the value of `outline-regexp' is that of
85 ;; `org-outline-regexp'. The only function still directly relying on
86 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
87 ;; job when `orgstruct-mode' is active.
88 (defvar org-outline-regexp
"\\*+ "
89 "Regexp to match Org headlines.")
90 (defconst org-outline-regexp-bol
"^\\*+ "
91 "Regexp to match Org headlines.
92 This is similar to `org-outline-regexp' but additionally makes
93 sure that we are at the beginning of the line.")
95 (defconst org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
96 "Matches an headline, putting stars and text into groups.
97 Stars are put in group 1 and the trimmed body in group 2.")
99 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
100 (when (fboundp 'defvaralias
)
101 (unless (boundp 'calendar-view-holidays-initially-flag
)
102 (defvaralias 'calendar-view-holidays-initially-flag
103 'view-calendar-holidays-initially
))
104 (unless (boundp 'calendar-view-diary-initially-flag
)
105 (defvaralias 'calendar-view-diary-initially-flag
106 'view-diary-entries-initially
))
107 (unless (boundp 'diary-fancy-buffer
)
108 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
110 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
111 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
112 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
113 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
114 (declare-function org-at-clock-log-p
"org-clock" ())
115 (declare-function org-clock-timestamps-up
"org-clock" ())
116 (declare-function org-clock-timestamps-down
"org-clock" ())
118 ;; load languages based on value of `org-babel-load-languages'
119 (defvar org-babel-load-languages
)
121 (defun org-babel-do-load-languages (sym value
)
122 "Load the languages defined in `org-babel-load-languages'."
123 (set-default sym value
)
125 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
128 (require (intern (concat "ob-" lang
))))
130 (funcall 'fmakunbound
131 (intern (concat "org-babel-execute:" lang
)))
132 (funcall 'fmakunbound
133 (intern (concat "org-babel-expand-body:" lang
)))))))
134 org-babel-load-languages
))
136 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
137 "Languages which can be evaluated in Org-mode buffers.
138 This list can be used to load support for any of the languages
139 below, note that each language will depend on a different set of
140 system executables and/or Emacs modes. When a language is
141 \"loaded\", then code blocks in that language can be evaluated
142 with `org-babel-execute-src-block' bound by default to C-c
143 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
144 be set to remove code block evaluation from the C-c C-c
145 keybinding. By default only Emacs Lisp (which has no
146 requirements) is loaded."
148 :set
'org-babel-do-load-languages
150 :type
'(alist :tag
"Babel Languages"
153 (const :tag
"Awk" awk
)
156 (const :tag
"Asymptote" asymptote
)
157 (const :tag
"Calc" calc
)
158 (const :tag
"Clojure" clojure
)
159 (const :tag
"CSS" css
)
160 (const :tag
"Ditaa" ditaa
)
161 (const :tag
"Dot" dot
)
162 (const :tag
"Emacs Lisp" emacs-lisp
)
163 (const :tag
"Fortran" fortran
)
164 (const :tag
"Gnuplot" gnuplot
)
165 (const :tag
"Haskell" haskell
)
167 (const :tag
"Java" java
)
168 (const :tag
"Javascript" js
)
169 (const :tag
"LaTeX" latex
)
170 (const :tag
"Ledger" ledger
)
171 (const :tag
"Lilypond" lilypond
)
172 (const :tag
"Maxima" maxima
)
173 (const :tag
"Matlab" matlab
)
174 (const :tag
"Mscgen" mscgen
)
175 (const :tag
"Ocaml" ocaml
)
176 (const :tag
"Octave" octave
)
177 (const :tag
"Org" org
)
178 (const :tag
"Perl" perl
)
179 (const :tag
"Pico Lisp" picolisp
)
180 (const :tag
"PlantUML" plantuml
)
181 (const :tag
"Python" python
)
182 (const :tag
"Ruby" ruby
)
183 (const :tag
"Sass" sass
)
184 (const :tag
"Scala" scala
)
185 (const :tag
"Scheme" scheme
)
186 (const :tag
"Screen" screen
)
187 (const :tag
"Shell Script" sh
)
188 (const :tag
"Shen" shen
)
189 (const :tag
"Sql" sql
)
190 (const :tag
"Sqlite" sqlite
))
191 :value-type
(boolean :tag
"Activate" :value t
)))
193 ;;;; Customization variables
194 (defcustom org-clone-delete-id nil
195 "Remove ID property of clones of a subtree.
196 When non-nil, clones of a subtree don't inherit the ID property.
197 Otherwise they inherit the ID property with a new unique
206 (defun org-version (&optional here full message
)
207 "Show the org-mode version in the echo area.
208 With prefix arg HERE, insert it at point."
210 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
211 (org-install-dir (ignore-errors (org-find-library-dir "org-install.el")))
213 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
214 (load (concat org-dir
"org-version.el")
215 'noerror
'nomessage
'nosuffix
)))
216 (org-version (org-release))
217 (git-version (org-git-version))
218 (version (format "Org-mode version %s (%s @ %s)"
222 (if (string= org-dir org-install-dir
)
224 (concat "mixed installation! " org-install-dir
" and " org-dir
))
225 "org-install.el can not be found!")))
226 (_version (if full version org-version
)))
227 (if (org-called-interactively-p 'interactive
)
231 (if message
(message _version
))
234 ;;; Compatibility constants
236 ;;; The custom variables
239 "Outline-based notes management and organizer."
244 (defcustom org-mode-hook nil
245 "Mode hook for Org-mode, run after the mode was turned on."
249 (defcustom org-load-hook nil
250 "Hook that is run after org.el has been loaded."
254 (defcustom org-log-buffer-setup-hook nil
255 "Hook that is run after an Org log buffer is created."
260 (defvar org-modules
) ; defined below
261 (defvar org-modules-loaded nil
262 "Have the modules been loaded already?")
264 (defun org-load-modules-maybe (&optional force
)
265 "Load all extensions listed in `org-modules'."
266 (when (or force
(not org-modules-loaded
))
268 (condition-case nil
(require ext
)
269 (error (message "Problems while trying to load feature `%s'" ext
))))
271 (setq org-modules-loaded t
)))
273 (defun org-set-modules (var value
)
274 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
276 (when (featurep 'org
)
277 (org-load-modules-maybe 'force
)))
279 (when (org-bound-and-true-p org-modules
)
280 (let ((a (member 'org-infojs org-modules
)))
281 (and a
(setcar a
'org-jsinfo
))))
283 (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
)
284 "Modules that should always be loaded together with org.el.
285 If a description starts with <C>, the file is not part of Emacs
286 and loading it will require that you have downloaded and properly installed
287 the org-mode distribution.
289 You can also use this system to load external packages (i.e. neither Org
290 core modules, nor modules from the CONTRIB directory). Just add symbols
291 to the end of the list. If the package is called org-xyz.el, then you need
292 to add the symbol `xyz', and the package must have a call to
296 :set
'org-set-modules
299 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
300 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
301 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
302 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
303 (const :tag
" docview: Links to doc-view buffers" org-docview
)
304 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
305 (const :tag
" id: Global IDs for identifying entries" org-id
)
306 (const :tag
" info: Links to Info nodes" org-info
)
307 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
308 (const :tag
" habit: Track your consistency with habits" org-habit
)
309 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
310 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
311 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
312 (const :tag
" mew Links to Mew folders/messages" org-mew
)
313 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
314 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
315 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
316 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
317 (const :tag
" vm: Links to VM folders/messages" org-vm
)
318 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
319 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
320 (const :tag
" mouse: Additional mouse support" org-mouse
)
321 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
323 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
324 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
325 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
326 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
327 (const :tag
"C collector: Collect properties into tables" org-collector
)
328 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
329 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
330 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
331 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
332 (const :tag
"C eval: Include command output as text" org-eval
)
333 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
334 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
335 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
336 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
337 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
339 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
341 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
342 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
343 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
344 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
345 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
346 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
347 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
348 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
349 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
350 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
351 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
352 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
353 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
354 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
355 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
356 (const :tag
"C track: Keep up with Org-mode development" org-track
)
357 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
358 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
359 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
361 (defcustom org-support-shift-select nil
362 "Non-nil means make shift-cursor commands select text when possible.
364 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
365 start selecting a region, or enlarge regions started in this way.
366 In Org-mode, in special contexts, these same keys are used for
367 other purposes, important enough to compete with shift selection.
368 Org tries to balance these needs by supporting `shift-select-mode'
369 outside these special contexts, under control of this variable.
371 The default of this variable is nil, to avoid confusing behavior. Shifted
372 cursor keys will then execute Org commands in the following contexts:
373 - on a headline, changing TODO state (left/right) and priority (up/down)
374 - on a time stamp, changing the time
375 - in a plain list item, changing the bullet type
376 - in a property definition line, switching between allowed values
377 - in the BEGIN line of a clock table (changing the time block).
378 Outside these contexts, the commands will throw an error.
380 When this variable is t and the cursor is not in a special
381 context, Org-mode will support shift-selection for making and
382 enlarging regions. To make this more effective, the bullet
383 cycling will no longer happen anywhere in an item line, but only
384 if the cursor is exactly on the bullet.
386 If you set this variable to the symbol `always', then the keys
387 will not be special in headlines, property lines, and item lines,
388 to make shift selection work there as well. If this is what you
389 want, you can use the following alternative commands: `C-c C-t'
390 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
391 can be used to switch TODO sets, `C-c -' to cycle item bullet
392 types, and properties can be edited by hand or in column view.
394 However, when the cursor is on a timestamp, shift-cursor commands
395 will still edit the time stamp - this is just too good to give up.
397 XEmacs user should have this variable set to nil, because
398 `shift-select-mode' is in Emacs 23 or later only."
401 (const :tag
"Never" nil
)
402 (const :tag
"When outside special context" t
)
403 (const :tag
"Everywhere except timestamps" always
)))
405 (defcustom org-loop-over-headlines-in-active-region nil
406 "Shall some commands act upon headlines in the active region?
408 When set to `t', some commands will be performed in all headlines
409 within the active region.
411 When set to `start-level', some commands will be performed in all
412 headlines within the active region, provided that these headlines
413 are of the same level than the first one.
415 When set to a string, those commands will be performed on the
416 matching headlines within the active region. Such string must be
417 a tags/property/todo match as it is used in the agenda tags view.
419 The list of commands is: `org-schedule', `org-deadline',
420 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
421 `org-archive-to-archive-sibling'. The archiving commands skip
422 already archived entries."
423 :type
'(choice (const :tag
"Don't loop" nil
)
424 (const :tag
"All headlines in active region" t
)
425 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
426 (string :tag
"Tags/Property/Todo matcher"))
431 (defgroup org-startup nil
432 "Options concerning startup of Org-mode."
436 (defcustom org-startup-folded t
437 "Non-nil means entering Org-mode will switch to OVERVIEW.
438 This can also be configured on a per-file basis by adding one of
439 the following lines anywhere in the buffer:
441 #+STARTUP: fold (or `overview', this is equivalent)
442 #+STARTUP: nofold (or `showall', this is equivalent)
444 #+STARTUP: showeverything"
447 (const :tag
"nofold: show all" nil
)
448 (const :tag
"fold: overview" t
)
449 (const :tag
"content: all headlines" content
)
450 (const :tag
"show everything, even drawers" showeverything
)))
452 (defcustom org-startup-truncated t
453 "Non-nil means entering Org-mode will set `truncate-lines'.
454 This is useful since some lines containing links can be very long and
455 uninteresting. Also tables look terrible when wrapped."
459 (defcustom org-startup-indented nil
460 "Non-nil means turn on `org-indent-mode' on startup.
461 This can also be configured on a per-file basis by adding one of
462 the following lines anywhere in the buffer:
466 :group
'org-structure
468 (const :tag
"Not" nil
)
469 (const :tag
"Globally (slow on startup in large files)" t
)))
471 (defcustom org-use-sub-superscripts t
472 "Non-nil means interpret \"_\" and \"^\" for export.
473 When this option is turned on, you can use TeX-like syntax for sub- and
474 superscripts. Several characters after \"_\" or \"^\" will be
475 considered as a single item - so grouping with {} is normally not
476 needed. For example, the following things will be parsed as single
477 sub- or superscripts.
479 10^24 or 10^tau several digits will be considered 1 item.
480 10^-12 or 10^-tau a leading sign with digits or a word
481 x^2-y^3 will be read as x^2 - y^3, because items are
482 terminated by almost any nonword/nondigit char.
483 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
485 Still, ambiguity is possible - so when in doubt use {} to enclose the
486 sub/superscript. If you set this variable to the symbol `{}',
487 the braces are *required* in order to trigger interpretations as
488 sub/superscript. This can be helpful in documents that need \"_\"
489 frequently in plain text.
491 Not all export backends support this, but HTML does.
493 This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
495 :group
'org-export-translation
498 (const :tag
"Always interpret" t
)
499 (const :tag
"Only with braces" {})
500 (const :tag
"Never interpret" nil
)))
502 (if (fboundp 'defvaralias
)
503 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
506 (defcustom org-startup-with-beamer-mode nil
507 "Non-nil means turn on `org-beamer-mode' on startup.
508 This can also be configured on a per-file basis by adding one of
509 the following lines anywhere in the buffer:
516 (defcustom org-startup-align-all-tables nil
517 "Non-nil means align all tables when visiting a file.
518 This is useful when the column width in tables is forced with <N> cookies
519 in table fields. Such tables will look correct only after the first re-align.
520 This can also be configured on a per-file basis by adding one of
521 the following lines anywhere in the buffer:
527 (defcustom org-startup-with-inline-images nil
528 "Non-nil means show inline images when loading a new Org file.
529 This can also be configured on a per-file basis by adding one of
530 the following lines anywhere in the buffer:
531 #+STARTUP: inlineimages
532 #+STARTUP: noinlineimages"
537 (defcustom org-insert-mode-line-in-empty-file nil
538 "Non-nil means insert the first line setting Org-mode in empty files.
539 When the function `org-mode' is called interactively in an empty file, this
540 normally means that the file name does not automatically trigger Org-mode.
541 To ensure that the file will always be in Org-mode in the future, a
542 line enforcing Org-mode will be inserted into the buffer, if this option
547 (defcustom org-replace-disputed-keys nil
548 "Non-nil means use alternative key bindings for some keys.
549 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
550 These keys are also used by other packages like shift-selection-mode'
551 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
552 If you want to use Org-mode together with one of these other modes,
553 or more generally if you would like to move some Org-mode commands to
554 other keys, set this variable and configure the keys with the variable
557 This option is only relevant at load-time of Org-mode, and must be set
558 *before* org.el is loaded. Changing it requires a restart of Emacs to
563 (defcustom org-use-extra-keys nil
564 "Non-nil means use extra key sequence definitions for certain commands.
565 This happens automatically if you run XEmacs or if `window-system'
566 is nil. This variable lets you do the same manually. You must
567 set it before loading org.
569 Example: on Carbon Emacs 22 running graphically, with an external
570 keyboard on a Powerbook, the default way of setting M-left might
571 not work for either Alt or ESC. Setting this variable will make
576 (if (fboundp 'defvaralias
)
577 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
579 (defcustom org-disputed-keys
580 '(([(shift up
)] .
[(meta p
)])
581 ([(shift down
)] .
[(meta n
)])
582 ([(shift left
)] .
[(meta -
)])
583 ([(shift right
)] .
[(meta +)])
584 ([(control shift right
)] .
[(meta shift
+)])
585 ([(control shift left
)] .
[(meta shift -
)]))
586 "Keys for which Org-mode and other modes compete.
587 This is an alist, cars are the default keys, second element specifies
588 the alternative to use when `org-replace-disputed-keys' is t.
590 Keys can be specified in any syntax supported by `define-key'.
591 The value of this option takes effect only at Org-mode's startup,
592 therefore you'll have to restart Emacs to apply it after changing."
597 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
598 Or return the original if not disputed.
599 Also apply the translations defined in `org-xemacs-key-equivalents'."
600 (when org-replace-disputed-keys
601 (let* ((nkey (key-description key
))
602 (x (org-find-if (lambda (x)
603 (equal (key-description (car x
)) nkey
))
605 (setq key
(if x
(cdr x
) key
))))
606 (when (featurep 'xemacs
)
607 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
610 (defun org-find-if (predicate seq
)
613 (if (funcall predicate
(car seq
))
614 (throw 'exit
(car seq
))
617 (defun org-defkey (keymap key def
)
618 "Define a key, possibly translated, as returned by `org-key'."
619 (define-key keymap
(org-key key
) def
))
621 (defcustom org-ellipsis nil
622 "The ellipsis to use in the Org-mode outline.
623 When nil, just use the standard three dots. When a string, use that instead,
624 When a face, use the standard 3 dots, but with the specified face.
625 The change affects only Org-mode (which will then use its own display table).
626 Changing this requires executing `M-x org-mode' in a buffer to become
629 :type
'(choice (const :tag
"Default" nil
)
630 (face :tag
"Face" :value org-warning
)
631 (string :tag
"String" :value
"...#")))
633 (defvar org-display-table nil
634 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
636 (defgroup org-keywords nil
637 "Keywords in Org-mode."
641 (defcustom org-deadline-string
"DEADLINE:"
642 "String to mark deadline entries.
643 A deadline is this string, followed by a time stamp. Should be a word,
644 terminated by a colon. You can insert a schedule keyword and
645 a timestamp with \\[org-deadline].
646 Changes become only effective after restarting Emacs."
650 (defcustom org-scheduled-string
"SCHEDULED:"
651 "String to mark scheduled TODO entries.
652 A schedule is this string, followed by a time stamp. Should be a word,
653 terminated by a colon. You can insert a schedule keyword and
654 a timestamp with \\[org-schedule].
655 Changes become only effective after restarting Emacs."
659 (defcustom org-closed-string
"CLOSED:"
660 "String used as the prefix for timestamps logging closing a TODO entry."
664 (defcustom org-clock-string
"CLOCK:"
665 "String used as prefix for timestamps clocking work hours on an item."
669 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
670 org-scheduled-string
"\\|"
671 org-deadline-string
"\\|"
672 org-closed-string
"\\|"
673 org-clock-string
"\\)")
674 "Matches a line with planning or clock info.")
676 (defcustom org-comment-string
"COMMENT"
677 "Entries starting with this keyword will never be exported.
678 An entry can be toggled between COMMENT and normal with
679 \\[org-toggle-comment].
680 Changes become only effective after restarting Emacs."
684 (defcustom org-quote-string
"QUOTE"
685 "Entries starting with this keyword will be exported in fixed-width font.
686 Quoting applies only to the text in the entry following the headline, and does
687 not extend beyond the next headline, even if that is lower level.
688 An entry can be toggled between QUOTE and normal with
689 \\[org-toggle-fixed-width-section]."
693 (defconst org-repeat-re
694 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
695 "Regular expression for specifying repeated events.
696 After a match, group 1 contains the repeat expression.")
698 (defgroup org-structure nil
699 "Options concerning the general structure of Org-mode files."
703 (defgroup org-reveal-location nil
704 "Options about how to make context of a location visible."
705 :tag
"Org Reveal Location"
706 :group
'org-structure
)
708 (defconst org-context-choice
710 (const :tag
"Always" t
)
711 (const :tag
"Never" nil
)
712 (repeat :greedy t
:tag
"Individual contexts"
714 (choice :tag
"Context"
721 (const bookmark-jump
)
725 "Contexts for the reveal options.")
727 (defcustom org-show-hierarchy-above
'((default . t
))
728 "Non-nil means show full hierarchy when revealing a location.
729 Org-mode often shows locations in an org-mode file which might have
730 been invisible before. When this is set, the hierarchy of headings
731 above the exposed location is shown.
732 Turning this off for example for sparse trees makes them very compact.
733 Instead of t, this can also be an alist specifying this option for different
734 contexts. Valid contexts are
735 agenda when exposing an entry from the agenda
736 org-goto when using the command `org-goto' on key C-c C-j
737 occur-tree when using the command `org-occur' on key C-c /
738 tags-tree when constructing a sparse tree based on tags matches
739 link-search when exposing search matches associated with a link
740 mark-goto when exposing the jump goal of a mark
741 bookmark-jump when exposing a bookmark location
742 isearch when exiting from an incremental search
743 default default for all contexts not set explicitly"
744 :group
'org-reveal-location
745 :type org-context-choice
)
747 (defcustom org-show-following-heading
'((default . nil
))
748 "Non-nil means show following heading when revealing a location.
749 Org-mode often shows locations in an org-mode file which might have
750 been invisible before. When this is set, the heading following the
752 Turning this off for example for sparse trees makes them very compact,
753 but makes it harder to edit the location of the match. In such a case,
754 use the command \\[org-reveal] to show more context.
755 Instead of t, this can also be an alist specifying this option for different
756 contexts. See `org-show-hierarchy-above' for valid contexts."
757 :group
'org-reveal-location
758 :type org-context-choice
)
760 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
761 "Non-nil means show all sibling heading when revealing a location.
762 Org-mode often shows locations in an org-mode file which might have
763 been invisible before. When this is set, the sibling of the current entry
764 heading are all made visible. If `org-show-hierarchy-above' is t,
765 the same happens on each level of the hierarchy above the current entry.
767 By default this is on for the isearch context, off for all other contexts.
768 Turning this off for example for sparse trees makes them very compact,
769 but makes it harder to edit the location of the match. In such a case,
770 use the command \\[org-reveal] to show more context.
771 Instead of t, this can also be an alist specifying this option for different
772 contexts. See `org-show-hierarchy-above' for valid contexts."
773 :group
'org-reveal-location
774 :type org-context-choice
)
776 (defcustom org-show-entry-below
'((default . nil
))
777 "Non-nil means show the entry below a headline when revealing a location.
778 Org-mode often shows locations in an org-mode file which might have
779 been invisible before. When this is set, the text below the headline that is
780 exposed is also shown.
782 By default this is off for all contexts.
783 Instead of t, this can also be an alist specifying this option for different
784 contexts. See `org-show-hierarchy-above' for valid contexts."
785 :group
'org-reveal-location
786 :type org-context-choice
)
788 (defcustom org-indirect-buffer-display
'other-window
789 "How should indirect tree buffers be displayed?
790 This applies to indirect buffers created with the commands
791 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
793 current-window Display in the current window
794 other-window Just display in another window.
795 dedicated-frame Create one new frame, and re-use it each time.
796 new-frame Make a new frame each time. Note that in this case
797 previously-made indirect buffers are kept, and you need to
798 kill these buffers yourself."
799 :group
'org-structure
800 :group
'org-agenda-windows
802 (const :tag
"In current window" current-window
)
803 (const :tag
"In current frame, other window" other-window
)
804 (const :tag
"Each time a new frame" new-frame
)
805 (const :tag
"One dedicated frame" dedicated-frame
)))
807 (defcustom org-use-speed-commands nil
808 "Non-nil means activate single letter commands at beginning of a headline.
809 This may also be a function to test for appropriate locations where speed
810 commands should be active."
811 :group
'org-structure
813 (const :tag
"Never" nil
)
814 (const :tag
"At beginning of headline stars" t
)
817 (defcustom org-speed-commands-user nil
818 "Alist of additional speed commands.
819 This list will be checked before `org-speed-commands-default'
820 when the variable `org-use-speed-commands' is non-nil
821 and when the cursor is at the beginning of a headline.
822 The car if each entry is a string with a single letter, which must
823 be assigned to `self-insert-command' in the global map.
824 The cdr is either a command to be called interactively, a function
825 to be called, or a form to be evaluated.
826 An entry that is just a list with a single string will be interpreted
827 as a descriptive headline that will be added when listing the speed
828 commands in the Help buffer using the `?' speed command."
829 :group
'org-structure
830 :type
'(repeat :value
("k" . ignore
)
831 (choice :value
("k" . ignore
)
832 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
833 (cons :tag
"Letter and Command"
834 (string :tag
"Command letter")
839 (defgroup org-cycle nil
840 "Options concerning visibility cycling in Org-mode."
842 :group
'org-structure
)
844 (defcustom org-cycle-skip-children-state-if-no-children t
845 "Non-nil means skip CHILDREN state in entries that don't have any."
849 (defcustom org-cycle-max-level nil
850 "Maximum level which should still be subject to visibility cycling.
851 Levels higher than this will, for cycling, be treated as text, not a headline.
852 When `org-odd-levels-only' is set, a value of N in this variable actually
853 means 2N-1 stars as the limiting headline.
854 When nil, cycle all levels.
855 Note that the limiting level of cycling is also influenced by
856 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
857 `org-inlinetask-min-level' is, cycling will be limited to levels one less
861 (const :tag
"No limit" nil
)
862 (integer :tag
"Maximum level")))
864 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
865 "Names of drawers. Drawers are not opened by cycling on the headline above.
866 Drawers only open with a TAB on the drawer line itself. A drawer looks like
871 The drawer \"PROPERTIES\" is special for capturing properties through
874 Drawers can be defined on the per-file basis with a line like:
876 #+DRAWERS: HIDDEN STATE PROPERTIES"
877 :group
'org-structure
879 :type
'(repeat (string :tag
"Drawer Name")))
881 (defcustom org-hide-block-startup nil
882 "Non-nil means entering Org-mode will fold all blocks.
883 This can also be set in on a per-file basis with
885 #+STARTUP: hideblocks
886 #+STARTUP: showblocks"
891 (defcustom org-cycle-global-at-bob nil
892 "Cycle globally if cursor is at beginning of buffer and not at a headline.
893 This makes it possible to do global cycling without having to use S-TAB or
894 \\[universal-argument] TAB. For this special case to work, the first line \
896 must not be a headline - it may be empty or some other text. When used in
897 this way, `org-cycle-hook' is disables temporarily, to make sure the
898 cursor stays at the beginning of the buffer.
899 When this option is nil, don't do anything special at the beginning
904 (defcustom org-cycle-level-after-item
/entry-creation t
905 "Non-nil means cycle entry level or item indentation in new empty entries.
907 When the cursor is at the end of an empty headline, i.e with only stars
908 and maybe a TODO keyword, TAB will then switch the entry to become a child,
909 and then all possible ancestor states, before returning to the original state.
910 This makes data entry extremely fast: M-RET to create a new headline,
911 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
913 When the cursor is at the end of an empty plain list item, one TAB will
914 make it a subitem, two or more tabs will back up to make this an item
915 higher up in the item hierarchy."
919 (defcustom org-cycle-emulate-tab t
920 "Where should `org-cycle' emulate TAB.
922 white Only in completely white lines
923 whitestart Only at the beginning of lines, before the first non-white char
924 t Everywhere except in headlines
925 exc-hl-bol Everywhere except at the start of a headline
926 If TAB is used in a place where it does not emulate TAB, the current subtree
927 visibility is cycled."
929 :type
'(choice (const :tag
"Never" nil
)
930 (const :tag
"Only in completely white lines" white
)
931 (const :tag
"Before first char in a line" whitestart
)
932 (const :tag
"Everywhere except in headlines" t
)
933 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
936 (defcustom org-cycle-separator-lines
2
937 "Number of empty lines needed to keep an empty line between collapsed trees.
938 If you leave an empty line between the end of a subtree and the following
939 headline, this empty line is hidden when the subtree is folded.
940 Org-mode will leave (exactly) one empty line visible if the number of
941 empty lines is equal or larger to the number given in this variable.
942 So the default 2 means at least 2 empty lines after the end of a subtree
943 are needed to produce free space between a collapsed subtree and the
946 If the number is negative, and the number of empty lines is at least -N,
947 all empty lines are shown.
949 Special case: when 0, never leave empty lines in collapsed view."
952 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
954 (defcustom org-pre-cycle-hook nil
955 "Hook that is run before visibility cycling is happening.
956 The function(s) in this hook must accept a single argument which indicates
957 the new state that will be set right after running this hook. The
958 argument is a symbol. Before a global state change, it can have the values
959 `overview', `content', or `all'. Before a local state change, it can have
960 the values `folded', `children', or `subtree'."
964 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
965 org-cycle-hide-drawers
966 org-cycle-show-empty-lines
967 org-optimize-window-after-visibility-change
)
968 "Hook that is run after `org-cycle' has changed the buffer visibility.
969 The function(s) in this hook must accept a single argument which indicates
970 the new state that was set by the most recent `org-cycle' command. The
971 argument is a symbol. After a global state change, it can have the values
972 `overview', `content', or `all'. After a local state change, it can have
973 the values `folded', `children', or `subtree'."
977 (defgroup org-edit-structure nil
978 "Options concerning structure editing in Org-mode."
979 :tag
"Org Edit Structure"
980 :group
'org-structure
)
982 (defcustom org-odd-levels-only nil
983 "Non-nil means skip even levels and only use odd levels for the outline.
984 This has the effect that two stars are being added/taken away in
985 promotion/demotion commands. It also influences how levels are
986 handled by the exporters.
987 Changing it requires restart of `font-lock-mode' to become effective
988 for fontification also in regions already fontified.
989 You may also set this on a per-file basis by adding one of the following
994 :group
'org-edit-structure
995 :group
'org-appearance
998 (defcustom org-adapt-indentation t
999 "Non-nil means adapt indentation to outline node level.
1001 When this variable is set, Org assumes that you write outlines by
1002 indenting text in each node to align with the headline (after the stars).
1003 The following issues are influenced by this variable:
1005 - When this is set and the *entire* text in an entry is indented, the
1006 indentation is increased by one space in a demotion command, and
1007 decreased by one in a promotion command. If any line in the entry
1008 body starts with text at column 0, indentation is not changed at all.
1010 - Property drawers and planning information is inserted indented when
1011 this variable s set. When nil, they will not be indented.
1013 - TAB indents a line relative to context. The lines below a headline
1014 will be indented when this variable is set.
1016 Note that this is all about true indentation, by adding and removing
1017 space characters. See also `org-indent.el' which does level-dependent
1018 indentation in a virtual way, i.e. at display time in Emacs."
1019 :group
'org-edit-structure
1022 (defcustom org-special-ctrl-a
/e nil
1023 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1025 When t, `C-a' will bring back the cursor to the beginning of the
1026 headline text, i.e. after the stars and after a possible TODO
1027 keyword. In an item, this will be the position after bullet and
1028 check-box, if any. When the cursor is already at that position,
1029 another `C-a' will bring it to the beginning of the line.
1031 `C-e' will jump to the end of the headline, ignoring the presence
1032 of tags in the headline. A second `C-e' will then jump to the
1033 true end of the line, after any tags. This also means that, when
1034 this variable is non-nil, `C-e' also will never jump beyond the
1035 end of the heading of a folded section, i.e. not after the
1038 When set to the symbol `reversed', the first `C-a' or `C-e' works
1039 normally, going to the true line boundary first. Only a directly
1040 following, identical keypress will bring the cursor to the
1043 This may also be a cons cell where the behavior for `C-a' and
1044 `C-e' is set separately."
1045 :group
'org-edit-structure
1047 (const :tag
"off" nil
)
1048 (const :tag
"on: after stars/bullet and before tags first" t
)
1049 (const :tag
"reversed: true line boundary first" reversed
)
1050 (cons :tag
"Set C-a and C-e separately"
1051 (choice :tag
"Special C-a"
1052 (const :tag
"off" nil
)
1053 (const :tag
"on: after stars/bullet first" t
)
1054 (const :tag
"reversed: before stars/bullet first" reversed
))
1055 (choice :tag
"Special C-e"
1056 (const :tag
"off" nil
)
1057 (const :tag
"on: before tags first" t
)
1058 (const :tag
"reversed: after tags first" reversed
)))))
1059 (if (fboundp 'defvaralias
)
1060 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1062 (defcustom org-special-ctrl-k nil
1063 "Non-nil means `C-k' will behave specially in headlines.
1064 When nil, `C-k' will call the default `kill-line' command.
1065 When t, the following will happen while the cursor is in the headline:
1067 - When the cursor is at the beginning of a headline, kill the entire
1068 line and possible the folded subtree below the line.
1069 - When in the middle of the headline text, kill the headline up to the tags.
1070 - When after the headline text, kill the tags."
1071 :group
'org-edit-structure
1074 (defcustom org-ctrl-k-protect-subtree nil
1075 "Non-nil means, do not delete a hidden subtree with C-k.
1076 When set to the symbol `error', simply throw an error when C-k is
1077 used to kill (part-of) a headline that has hidden text behind it.
1078 Any other non-nil value will result in a query to the user, if it is
1079 OK to kill that hidden subtree. When nil, kill without remorse."
1080 :group
'org-edit-structure
1083 (const :tag
"Do not protect hidden subtrees" nil
)
1084 (const :tag
"Protect hidden subtrees with a security query" t
)
1085 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1087 (defcustom org-catch-invisible-edits nil
1088 "Check if in invisible region before inserting or deleting a character.
1091 nil Do not check, so just do invisible edits.
1092 error Throw an error and do nothing.
1093 show Make point visible, and do the requested edit.
1094 show-and-error Make point visible, then throw an error and abort the edit.
1095 smart Make point visible, and do insertion/deletion if it is
1096 adjacent to visible text and the change feels predictable.
1097 Never delete a previously invisible character or add in the
1098 middle or right after an invisible region. Basically, this
1099 allows insertion and backward-delete right before ellipses.
1100 FIXME: maybe in this case we should not even show?"
1101 :group
'org-edit-structure
1104 (const :tag
"Do not check" nil
)
1105 (const :tag
"Throw error when trying to edit" error
)
1106 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1107 (const :tag
"Show invisible part and do the edit" show
)
1108 (const :tag
"Be smart and do the right thing" smart
)))
1110 (defcustom org-yank-folded-subtrees t
1111 "Non-nil means when yanking subtrees, fold them.
1112 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1113 it starts with a heading and all other headings in it are either children
1114 or siblings, then fold all the subtrees. However, do this only if no
1115 text after the yank would be swallowed into a folded tree by this action."
1116 :group
'org-edit-structure
1119 (defcustom org-yank-adjusted-subtrees nil
1120 "Non-nil means when yanking subtrees, adjust the level.
1121 With this setting, `org-paste-subtree' is used to insert the subtree, see
1122 this function for details."
1123 :group
'org-edit-structure
1126 (defcustom org-M-RET-may-split-line
'((default . t
))
1127 "Non-nil means M-RET will split the line at the cursor position.
1128 When nil, it will go to the end of the line before making a
1130 You may also set this option in a different way for different
1131 contexts. Valid contexts are:
1133 headline when creating a new headline
1134 item when creating a new item
1135 table in a table field
1136 default the value to be used for all contexts not explicitly
1138 :group
'org-structure
1141 (const :tag
"Always" t
)
1142 (const :tag
"Never" nil
)
1143 (repeat :greedy t
:tag
"Individual contexts"
1145 (choice :tag
"Context"
1153 (defcustom org-insert-heading-respect-content nil
1154 "Non-nil means insert new headings after the current subtree.
1155 When nil, the new heading is created directly after the current line.
1156 The commands \\[org-insert-heading-respect-content] and
1157 \\[org-insert-todo-heading-respect-content] turn this variable on
1158 for the duration of the command."
1159 :group
'org-structure
1162 (defcustom org-blank-before-new-entry
'((heading . auto
)
1163 (plain-list-item . auto
))
1164 "Should `org-insert-heading' leave a blank line before new heading/item?
1165 The value is an alist, with `heading' and `plain-list-item' as CAR,
1166 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1167 which case Org will look at the surrounding headings/items and try to
1168 make an intelligent decision whether to insert a blank line or not.
1170 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1171 set, the setting here is ignored and no empty line is inserted, to avoid
1172 breaking the list structure."
1173 :group
'org-edit-structure
1175 (cons (const heading
)
1176 (choice (const :tag
"Never" nil
)
1177 (const :tag
"Always" t
)
1178 (const :tag
"Auto" auto
)))
1179 (cons (const plain-list-item
)
1180 (choice (const :tag
"Never" nil
)
1181 (const :tag
"Always" t
)
1182 (const :tag
"Auto" auto
)))))
1184 (defcustom org-insert-heading-hook nil
1185 "Hook being run after inserting a new heading."
1186 :group
'org-edit-structure
1189 (defcustom org-enable-fixed-width-editor t
1190 "Non-nil means lines starting with \":\" are treated as fixed-width.
1191 This currently only means they are never auto-wrapped.
1192 When nil, such lines will be treated like ordinary lines.
1193 See also the QUOTE keyword."
1194 :group
'org-edit-structure
1197 (defcustom org-goto-auto-isearch t
1198 "Non-nil means typing characters in `org-goto' starts incremental search."
1199 :group
'org-edit-structure
1202 (defgroup org-sparse-trees nil
1203 "Options concerning sparse trees in Org-mode."
1204 :tag
"Org Sparse Trees"
1205 :group
'org-structure
)
1207 (defcustom org-highlight-sparse-tree-matches t
1208 "Non-nil means highlight all matches that define a sparse tree.
1209 The highlights will automatically disappear the next time the buffer is
1210 changed by an edit command."
1211 :group
'org-sparse-trees
1214 (defcustom org-remove-highlights-with-change t
1215 "Non-nil means any change to the buffer will remove temporary highlights.
1216 Such highlights are created by `org-occur' and `org-clock-display'.
1217 When nil, `C-c C-c needs to be used to get rid of the highlights.
1218 The highlights created by `org-preview-latex-fragment' always need
1219 `C-c C-c' to be removed."
1220 :group
'org-sparse-trees
1225 (defcustom org-occur-hook
'(org-first-headline-recenter)
1226 "Hook that is run after `org-occur' has constructed a sparse tree.
1227 This can be used to recenter the window to show as much of the structure
1229 :group
'org-sparse-trees
1232 (defgroup org-imenu-and-speedbar nil
1233 "Options concerning imenu and speedbar in Org-mode."
1234 :tag
"Org Imenu and Speedbar"
1235 :group
'org-structure
)
1237 (defcustom org-imenu-depth
2
1238 "The maximum level for Imenu access to Org-mode headlines.
1239 This also applied for speedbar access."
1240 :group
'org-imenu-and-speedbar
1243 (defgroup org-table nil
1244 "Options concerning tables in Org-mode."
1248 (defcustom org-enable-table-editor
'optimized
1249 "Non-nil means lines starting with \"|\" are handled by the table editor.
1250 When nil, such lines will be treated like ordinary lines.
1252 When equal to the symbol `optimized', the table editor will be optimized to
1254 - Automatic overwrite mode in front of whitespace in table fields.
1255 This makes the structure of the table stay in tact as long as the edited
1256 field does not exceed the column width.
1257 - Minimize the number of realigns. Normally, the table is aligned each time
1258 TAB or RET are pressed to move to another field. With optimization this
1259 happens only if changes to a field might have changed the column width.
1260 Optimization requires replacing the functions `self-insert-command',
1261 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1262 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1263 very good at guessing when a re-align will be necessary, but you can always
1264 force one with \\[org-ctrl-c-ctrl-c].
1266 If you would like to use the optimized version in Org-mode, but the
1267 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1269 This variable can be used to turn on and off the table editor during a session,
1270 but in order to toggle optimization, a restart is required.
1272 See also the variable `org-table-auto-blank-field'."
1275 (const :tag
"off" nil
)
1277 (const :tag
"on, optimized" optimized
)))
1279 (defcustom org-self-insert-cluster-for-undo t
1280 "Non-nil means cluster self-insert commands for undo when possible.
1281 If this is set, then, like in the Emacs command loop, 20 consecutive
1282 characters will be undone together.
1283 This is configurable, because there is some impact on typing performance."
1287 (defcustom org-table-tab-recognizes-table.el t
1288 "Non-nil means TAB will automatically notice a table.el table.
1289 When it sees such a table, it moves point into it and - if necessary -
1290 calls `table-recognize-table'."
1291 :group
'org-table-editing
1294 (defgroup org-link nil
1295 "Options concerning links in Org-mode."
1299 (defvar org-link-abbrev-alist-local nil
1300 "Buffer-local version of `org-link-abbrev-alist', which see.
1301 The value of this is taken from the #+LINK lines.")
1302 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1304 (defcustom org-link-abbrev-alist nil
1305 "Alist of link abbreviations.
1306 The car of each element is a string, to be replaced at the start of a link.
1307 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1308 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1310 [[linkkey:tag][description]]
1312 The 'linkkey' must be a word word, starting with a letter, followed
1313 by letters, numbers, '-' or '_'.
1315 If REPLACE is a string, the tag will simply be appended to create the link.
1316 If the string contains \"%s\", the tag will be inserted there. If the string
1317 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1318 at that point (see the function `url-hexify-string'). If the string contains
1319 the specifier \"%(my-function)\", then the custom function `my-function' will
1320 be invoked: this function takes the tag as its only argument and must return
1323 REPLACE may also be a function that will be called with the tag as the
1324 only argument to create the link, which should be returned as a string.
1326 See the manual for examples."
1330 (string :tag
"Protocol")
1332 (string :tag
"Format")
1335 (defcustom org-descriptive-links t
1336 "Non-nil means Org will display descriptive links.
1337 E.g. [[http://orgmode.org][Org website]] will be displayed as
1338 \"Org Website\", hiding the link itself and just displaying its
1339 description. When set to `nil', Org will display the full links
1342 You can interactively set the value of this variable by calling
1343 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1347 (defcustom org-link-file-path-type
'adaptive
1348 "How the path name in file links should be stored.
1351 relative Relative to the current directory, i.e. the directory of the file
1352 into which the link is being inserted.
1353 absolute Absolute path, if possible with ~ for home directory.
1354 noabbrev Absolute path, no abbreviation of home directory.
1355 adaptive Use relative path for files in the current directory and sub-
1356 directories of it. For other files, use an absolute path."
1364 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1365 "Types of links that should be activated in Org-mode files.
1366 This is a list of symbols, each leading to the activation of a certain link
1367 type. In principle, it does not hurt to turn on most link types - there may
1368 be a small gain when turning off unused link types. The types are:
1370 bracket The recommended [[link][description]] or [[link]] links with hiding.
1371 angle Links in angular brackets that may contain whitespace like
1372 <bbdb:Carsten Dominik>.
1373 plain Plain links in normal text, no whitespace, like http://google.com.
1374 radio Text that is matched by a radio target, see manual for details.
1375 tag Tag settings in a headline (link to tag search).
1376 date Time stamps (link to calendar).
1377 footnote Footnote labels.
1379 Changing this variable requires a restart of Emacs to become effective."
1381 :type
'(set :greedy t
1382 (const :tag
"Double bracket links" bracket
)
1383 (const :tag
"Angular bracket links" angle
)
1384 (const :tag
"Plain text links" plain
)
1385 (const :tag
"Radio target matches" radio
)
1386 (const :tag
"Tags" tag
)
1387 (const :tag
"Timestamps" date
)
1388 (const :tag
"Footnotes" footnote
)))
1390 (defcustom org-make-link-description-function nil
1391 "Function to use to generate link descriptions from links.
1392 If nil the link location will be used. This function must take
1393 two parameters; the first is the link and the second the
1394 description `org-insert-link' has generated, and should return the
1395 description to use."
1399 (defgroup org-link-store nil
1400 "Options concerning storing links in Org-mode."
1401 :tag
"Org Store Link"
1404 (defcustom org-email-link-description-format
"Email %c: %.30s"
1405 "Format of the description part of a link to an email or usenet message.
1406 The following %-escapes will be replaced by corresponding information:
1408 %F full \"From\" field
1409 %f name, taken from \"From\" field, address if no name
1410 %T full \"To\" field
1411 %t first name in \"To\" field, address if no name
1412 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1413 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1418 You may use normal field width specification between the % and the letter.
1419 This is for example useful to limit the length of the subject.
1421 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1422 :group
'org-link-store
1425 (defcustom org-from-is-user-regexp
1427 (when (and user-mail-address
(not (string= user-mail-address
"")))
1428 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1429 (when (and user-full-name
(not (string= user-full-name
"")))
1430 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1431 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1432 "Regexp matched against the \"From:\" header of an email or usenet message.
1433 It should match if the message is from the user him/herself."
1434 :group
'org-link-store
1437 (defcustom org-link-to-org-use-id
'create-if-interactive-and-no-custom-id
1438 "Non-nil means storing a link to an Org file will use entry IDs.
1440 Note that before this variable is even considered, org-id must be loaded,
1441 so please customize `org-modules' and turn it on.
1443 The variable can have the following values:
1445 t Create an ID if needed to make a link to the current entry.
1447 create-if-interactive
1448 If `org-store-link' is called directly (interactively, as a user
1449 command), do create an ID to support the link. But when doing the
1450 job for remember, only use the ID if it already exists. The
1451 purpose of this setting is to avoid proliferation of unwanted
1452 IDs, just because you happen to be in an Org file when you
1453 call `org-remember' that automatically and preemptively
1454 creates a link. If you do want to get an ID link in a remember
1455 template to an entry not having an ID, create it first by
1456 explicitly creating a link to it, using `C-c C-l' first.
1458 create-if-interactive-and-no-custom-id
1459 Like create-if-interactive, but do not create an ID if there is
1460 a CUSTOM_ID property defined in the entry. This is the default.
1463 Use existing ID, do not create one.
1465 nil Never use an ID to make a link, instead link using a text search for
1467 :group
'org-link-store
1469 (const :tag
"Create ID to make link" t
)
1470 (const :tag
"Create if storing link interactively"
1471 create-if-interactive
)
1472 (const :tag
"Create if storing link interactively and no CUSTOM_ID is present"
1473 create-if-interactive-and-no-custom-id
)
1474 (const :tag
"Only use existing" use-existing
)
1475 (const :tag
"Do not use ID to create link" nil
)))
1477 (defcustom org-context-in-file-links t
1478 "Non-nil means file links from `org-store-link' contain context.
1479 A search string will be added to the file name with :: as separator and
1480 used to find the context when the link is activated by the command
1481 `org-open-at-point'. When this option is t, the entire active region
1482 will be placed in the search string of the file link. If set to a
1483 positive integer, only the first n lines of context will be stored.
1485 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1486 negates this setting for the duration of the command."
1487 :group
'org-link-store
1488 :type
'(choice boolean integer
))
1490 (defcustom org-keep-stored-link-after-insertion nil
1491 "Non-nil means keep link in list for entire session.
1493 The command `org-store-link' adds a link pointing to the current
1494 location to an internal list. These links accumulate during a session.
1495 The command `org-insert-link' can be used to insert links into any
1496 Org-mode file (offering completion for all stored links). When this
1497 option is nil, every link which has been inserted once using \\[org-insert-link]
1498 will be removed from the list, to make completing the unused links
1500 :group
'org-link-store
1503 (defgroup org-link-follow nil
1504 "Options concerning following links in Org-mode."
1505 :tag
"Org Follow Link"
1508 (defcustom org-link-translation-function nil
1509 "Function to translate links with different syntax to Org syntax.
1510 This can be used to translate links created for example by the Planner
1511 or emacs-wiki packages to Org syntax.
1512 The function must accept two parameters, a TYPE containing the link
1513 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1514 which is everything after the link protocol. It should return a cons
1515 with possibly modified values of type and path.
1516 Org contains a function for this, so if you set this variable to
1517 `org-translate-link-from-planner', you should be able follow many
1518 links created by planner."
1519 :group
'org-link-follow
1522 (defcustom org-follow-link-hook nil
1523 "Hook that is run after a link has been followed."
1524 :group
'org-link-follow
1527 (defcustom org-tab-follows-link nil
1528 "Non-nil means on links TAB will follow the link.
1529 Needs to be set before org.el is loaded.
1530 This really should not be used, it does not make sense, and the
1531 implementation is bad."
1532 :group
'org-link-follow
1535 (defcustom org-return-follows-link nil
1536 "Non-nil means on links RET will follow the link."
1537 :group
'org-link-follow
1540 (defcustom org-mouse-1-follows-link
1541 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1542 "Non-nil means mouse-1 on a link will follow the link.
1543 A longer mouse click will still set point. Does not work on XEmacs.
1544 Needs to be set before org.el is loaded."
1545 :group
'org-link-follow
1548 (defcustom org-mark-ring-length
4
1549 "Number of different positions to be recorded in the ring.
1550 Changing this requires a restart of Emacs to work correctly."
1551 :group
'org-link-follow
1554 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1555 "Non-nil means internal links in Org files must exactly match a headline.
1556 When nil, the link search tries to match a phrase with all words
1557 in the search text."
1558 :group
'org-link-follow
1561 (const :tag
"Use fuzzy text search" nil
)
1562 (const :tag
"Match only exact headline" t
)
1563 (const :tag
"Match exact headline or query to create it"
1566 (defcustom org-link-frame-setup
1567 '((vm . vm-visit-folder-other-frame
)
1568 (vm-imap . vm-visit-imap-folder-other-frame
)
1569 (gnus . org-gnus-no-new-news
)
1570 (file . find-file-other-window
)
1571 (wl . wl-other-frame
))
1572 "Setup the frame configuration for following links.
1573 When following a link with Emacs, it may often be useful to display
1574 this link in another window or frame. This variable can be used to
1575 set this up for the different types of links.
1578 `vm-visit-folder-other-window'
1579 `vm-visit-folder-other-frame'
1580 For Gnus, use any of
1583 `org-gnus-no-new-news'
1584 For FILE, use any of
1586 `find-file-other-window'
1587 `find-file-other-frame'
1588 For Wanderlust use any of
1591 For the calendar, use the variable `calendar-setup'.
1592 For BBDB, it is currently only possible to display the matches in
1594 :group
'org-link-follow
1598 (const vm-visit-folder
)
1599 (const vm-visit-folder-other-window
)
1600 (const vm-visit-folder-other-frame
)))
1604 (const gnus-other-frame
)
1605 (const org-gnus-no-new-news
)))
1609 (const find-file-other-window
)
1610 (const find-file-other-frame
)))
1614 (const wl-other-frame
)))))
1616 (defcustom org-display-internal-link-with-indirect-buffer nil
1617 "Non-nil means use indirect buffer to display infile links.
1618 Activating internal links (from one location in a file to another location
1619 in the same file) normally just jumps to the location. When the link is
1620 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1622 another window. When this option is set, the other window actually displays
1623 an indirect buffer clone of the current buffer, to avoid any visibility
1624 changes to the current buffer."
1625 :group
'org-link-follow
1628 (defcustom org-open-non-existing-files nil
1629 "Non-nil means `org-open-file' will open non-existing files.
1630 When nil, an error will be generated.
1631 This variable applies only to external applications because they
1632 might choke on non-existing files. If the link is to a file that
1633 will be opened in Emacs, the variable is ignored."
1634 :group
'org-link-follow
1637 (defcustom org-open-directory-means-index-dot-org nil
1638 "Non-nil means a link to a directory really means to index.org.
1639 When nil, following a directory link will run dired or open a finder/explorer
1640 window on that directory."
1641 :group
'org-link-follow
1644 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1645 "Function and arguments to call for following mailto links.
1646 This is a list with the first element being a Lisp function, and the
1647 remaining elements being arguments to the function. In string arguments,
1648 %a will be replaced by the address, and %s will be replaced by the subject
1649 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1650 :group
'org-link-follow
1652 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1653 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1654 (const :tag
"message-mail" (message-mail "%a" "%s"))
1655 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1657 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1658 "Non-nil means ask for confirmation before executing shell links.
1659 Shell links can be dangerous: just think about a link
1661 [[shell:rm -rf ~/*][Google Search]]
1663 This link would show up in your Org-mode document as \"Google Search\",
1664 but really it would remove your entire home directory.
1665 Therefore we advise against setting this variable to nil.
1666 Just change it to `y-or-n-p' if you want to confirm with a
1667 single keystroke rather than having to type \"yes\"."
1668 :group
'org-link-follow
1670 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1671 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1672 (const :tag
"no confirmation (dangerous)" nil
)))
1673 (put 'org-confirm-shell-link-function
1674 'safe-local-variable
1675 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1677 (defcustom org-confirm-shell-link-not-regexp
""
1678 "A regexp to skip confirmation for shell links."
1679 :group
'org-link-follow
1683 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1684 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1685 Elisp links can be dangerous: just think about a link
1687 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1689 This link would show up in your Org-mode document as \"Google Search\",
1690 but really it would remove your entire home directory.
1691 Therefore we advise against setting this variable to nil.
1692 Just change it to `y-or-n-p' if you want to confirm with a
1693 single keystroke rather than having to type \"yes\"."
1694 :group
'org-link-follow
1696 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1697 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1698 (const :tag
"no confirmation (dangerous)" nil
)))
1699 (put 'org-confirm-shell-link-function
1700 'safe-local-variable
1701 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1703 (defcustom org-confirm-elisp-link-not-regexp
""
1704 "A regexp to skip confirmation for Elisp links."
1705 :group
'org-link-follow
1709 (defconst org-file-apps-defaults-gnu
1713 "Default file applications on a UNIX or GNU/Linux system.
1714 See `org-file-apps'.")
1716 (defconst org-file-apps-defaults-macosx
1719 (system .
"open %s")
1721 ("eps.gz" .
"gv %s")
1723 ("fig" .
"xfig %s"))
1724 "Default file applications on a MacOS X system.
1725 The system \"open\" is known as a default, but we use X11 applications
1726 for some files for which the OS does not have a good default.
1727 See `org-file-apps'.")
1729 (defconst org-file-apps-defaults-windowsnt
1733 (list (if (featurep 'xemacs
)
1734 'mswindows-shell-execute
1738 (list (if (featurep 'xemacs
)
1739 'mswindows-shell-execute
1742 "Default file applications on a Windows NT system.
1743 The system \"open\" is used for most files.
1744 See `org-file-apps'.")
1746 (defcustom org-file-apps
1749 ("\\.mm\\'" . default
)
1750 ("\\.x?html?\\'" . default
)
1751 ("\\.pdf\\'" . default
)
1753 "External applications for opening `file:path' items in a document.
1754 Org-mode uses system defaults for different file types, but
1755 you can use this variable to set the application for a given file
1756 extension. The entries in this list are cons cells where the car identifies
1757 files and the cdr the corresponding command. Possible values for the
1759 \"string\" A string as a file identifier can be interpreted in different
1760 ways, depending on its contents:
1762 - Alphanumeric characters only:
1763 Match links with this file extension.
1764 Example: (\"pdf\" . \"evince %s\")
1765 to open PDFs with evince.
1767 - Regular expression: Match links where the
1768 filename matches the regexp. If you want to
1769 use groups here, use shy groups.
1771 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1772 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1773 to open *.html and *.xhtml with firefox.
1775 - Regular expression which contains (non-shy) groups:
1776 Match links where the whole link, including \"::\", and
1777 anything after that, matches the regexp.
1778 In a custom command string, %1, %2, etc. are replaced with
1779 the parts of the link that were matched by the groups.
1780 For backwards compatibility, if a command string is given
1781 that does not use any of the group matches, this case is
1782 handled identically to the second one (i.e. match against
1784 In a custom lisp form, you can access the group matches with
1785 (match-string n link).
1787 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1788 to open [[file:document.pdf::5]] with evince at page 5.
1790 `directory' Matches a directory
1791 `remote' Matches a remote file, accessible through tramp or efs.
1792 Remote files most likely should be visited through Emacs
1793 because external applications cannot handle such paths.
1794 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1795 so all files Emacs knows how to handle. Using this with
1796 command `emacs' will open most files in Emacs. Beware that this
1797 will also open html files inside Emacs, unless you add
1798 (\"html\" . default) to the list as well.
1799 t Default for files not matched by any of the other options.
1800 `system' The system command to open files, like `open' on Windows
1801 and Mac OS X, and mailcap under GNU/Linux. This is the command
1802 that will be selected if you call `C-c C-o' with a double
1803 \\[universal-argument] \\[universal-argument] prefix.
1805 Possible values for the command are:
1806 `emacs' The file will be visited by the current Emacs process.
1807 `default' Use the default application for this file type, which is the
1808 association for t in the list, most likely in the system-specific
1810 This can be used to overrule an unwanted setting in the
1811 system-specific variable.
1812 `system' Use the system command for opening files, like \"open\".
1813 This command is specified by the entry whose car is `system'.
1814 Most likely, the system-specific version of this variable
1815 does define this command, but you can overrule/replace it
1817 string A command to be executed by a shell; %s will be replaced
1818 by the path to the file.
1819 sexp A Lisp form which will be evaluated. The file path will
1820 be available in the Lisp variable `file'.
1821 For more examples, see the system specific constants
1822 `org-file-apps-defaults-macosx'
1823 `org-file-apps-defaults-windowsnt'
1824 `org-file-apps-defaults-gnu'."
1825 :group
'org-link-follow
1827 (cons (choice :value
""
1828 (string :tag
"Extension")
1829 (const :tag
"System command to open files" system
)
1830 (const :tag
"Default for unrecognized files" t
)
1831 (const :tag
"Remote file" remote
)
1832 (const :tag
"Links to a directory" directory
)
1833 (const :tag
"Any files that have Emacs modes"
1836 (const :tag
"Visit with Emacs" emacs
)
1837 (const :tag
"Use default" default
)
1838 (const :tag
"Use the system command" system
)
1839 (string :tag
"Command")
1840 (sexp :tag
"Lisp form")))))
1842 (defcustom org-doi-server-url
"http://dx.doi.org/"
1843 "The URL of the DOI server."
1846 :group
'org-link-follow
)
1848 (defgroup org-refile nil
1849 "Options concerning refiling entries in Org-mode."
1853 (defcustom org-directory
"~/org"
1854 "Directory with org files.
1855 This is just a default location to look for Org files. There is no need
1856 at all to put your files into this directory. It is only used in the
1857 following situations:
1859 1. When a remember template specifies a target file that is not an
1860 absolute path. The path will then be interpreted relative to
1862 2. When a remember note is filed away in an interactive way (when exiting the
1863 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1864 with `org-directory' as the default path."
1866 :group
'org-remember
1869 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1870 "Default target for storing notes.
1871 Used as a fall back file for org-remember.el and org-capture.el, for
1872 templates that do not specify a target file."
1874 :group
'org-remember
1876 (const :tag
"Default from remember-data-file" nil
)
1879 (defcustom org-goto-interface
'outline
1880 "The default interface to be used for `org-goto'.
1882 outline The interface shows an outline of the relevant file
1883 and the correct heading is found by moving through
1884 the outline or by searching with incremental search.
1885 outline-path-completion Headlines in the current buffer are offered via
1886 completion. This is the interface also used by
1887 the refile command."
1890 (const :tag
"Outline" outline
)
1891 (const :tag
"Outline-path-completion" outline-path-completion
)))
1893 (defcustom org-goto-max-level
5
1894 "Maximum target level when running `org-goto' with refile interface."
1898 (defcustom org-reverse-note-order nil
1899 "Non-nil means store new notes at the beginning of a file or entry.
1900 When nil, new notes will be filed to the end of a file or entry.
1901 This can also be a list with cons cells of regular expressions that
1902 are matched against file names, and values."
1903 :group
'org-remember
1906 (const :tag
"Reverse always" t
)
1907 (const :tag
"Reverse never" nil
)
1908 (repeat :tag
"By file name regexp"
1909 (cons regexp boolean
))))
1911 (defcustom org-log-refile nil
1912 "Information to record when a task is refiled.
1914 Possible values are:
1916 nil Don't add anything
1917 time Add a time stamp to the task
1918 note Prompt for a note and add it with template `org-log-note-headings'
1920 This option can also be set with on a per-file-basis with
1922 #+STARTUP: nologrefile
1923 #+STARTUP: logrefile
1924 #+STARTUP: lognoterefile
1926 You can have local logging settings for a subtree by setting the LOGGING
1927 property to one or more of these keywords.
1929 When bulk-refiling from the agenda, the value `note' is forbidden and
1930 will temporarily be changed to `time'."
1932 :group
'org-progress
1935 (const :tag
"No logging" nil
)
1936 (const :tag
"Record timestamp" time
)
1937 (const :tag
"Record timestamp with note." note
)))
1939 (defcustom org-refile-targets nil
1940 "Targets for refiling entries with \\[org-refile].
1941 This is a list of cons cells. Each cell contains:
1942 - a specification of the files to be considered, either a list of files,
1943 or a symbol whose function or variable value will be used to retrieve
1944 a file name or a list of file names. If you use `org-agenda-files' for
1945 that, all agenda files will be scanned for targets. Nil means consider
1946 headings in the current buffer.
1947 - A specification of how to find candidate refile targets. This may be
1949 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1950 This tag has to be present in all target headlines, inheritance will
1952 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1954 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1955 headlines that are refiling targets.
1956 - a cons cell (:level . N). Any headline of level N is considered a target.
1957 Note that, when `org-odd-levels-only' is set, level corresponds to
1958 order in hierarchy, not to the number of stars.
1959 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1960 Note that, when `org-odd-levels-only' is set, level corresponds to
1961 order in hierarchy, not to the number of stars.
1963 Each element of this list generates a set of possible targets.
1964 The union of these sets is presented (with completion) to
1965 the user by `org-refile'.
1967 You can set the variable `org-refile-target-verify-function' to a function
1968 to verify each headline found by the simple criteria above.
1970 When this variable is nil, all top-level headlines in the current buffer
1971 are used, equivalent to the value `((nil . (:level . 1))'."
1975 (choice :value org-agenda-files
1976 (const :tag
"All agenda files" org-agenda-files
)
1977 (const :tag
"Current buffer" nil
)
1978 (function) (variable) (file))
1979 (choice :tag
"Identify target headline by"
1980 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1981 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1982 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1983 (cons :tag
"Level number" (const :value
:level
) (integer))
1984 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1986 (defcustom org-refile-target-verify-function nil
1987 "Function to verify if the headline at point should be a refile target.
1988 The function will be called without arguments, with point at the
1989 beginning of the headline. It should return t and leave point
1990 where it is if the headline is a valid target for refiling.
1992 If the target should not be selected, the function must return nil.
1993 In addition to this, it may move point to a place from where the search
1994 should be continued. For example, the function may decide that the entire
1995 subtree of the current entry should be excluded and move point to the end
2000 (defcustom org-refile-use-cache nil
2001 "Non-nil means cache refile targets to speed up the process.
2002 The cache for a particular file will be updated automatically when
2003 the buffer has been killed, or when any of the marker used for flagging
2004 refile targets no longer points at a live buffer.
2005 If you have added new entries to a buffer that might themselves be targets,
2006 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
2007 find that easier, `C-u C-u C-u C-c C-w'."
2012 (defcustom org-refile-use-outline-path nil
2013 "Non-nil means provide refile targets as paths.
2014 So a level 3 headline will be available as level1/level2/level3.
2016 When the value is `file', also include the file name (without directory)
2017 into the path. In this case, you can also stop the completion after
2018 the file name, to get entries inserted as top level in the file.
2020 When `full-file-path', include the full file path."
2023 (const :tag
"Not" nil
)
2024 (const :tag
"Yes" t
)
2025 (const :tag
"Start with file name" file
)
2026 (const :tag
"Start with full file path" full-file-path
)))
2028 (defcustom org-outline-path-complete-in-steps t
2029 "Non-nil means complete the outline path in hierarchical steps.
2030 When Org-mode uses the refile interface to select an outline path
2031 \(see variable `org-refile-use-outline-path'), the completion of
2032 the path can be done is a single go, or if can be done in steps down
2033 the headline hierarchy. Going in steps is probably the best if you
2034 do not use a special completion package like `ido' or `icicles'.
2035 However, when using these packages, going in one step can be very
2036 fast, while still showing the whole path to the entry."
2040 (defcustom org-refile-allow-creating-parent-nodes nil
2041 "Non-nil means allow to create new nodes as refile targets.
2042 New nodes are then created by adding \"/new node name\" to the completion
2043 of an existing node. When the value of this variable is `confirm',
2044 new node creation must be confirmed by the user (recommended)
2045 When nil, the completion must match an existing entry.
2047 Note that, if the new heading is not seen by the criteria
2048 listed in `org-refile-targets', multiple instances of the same
2049 heading would be created by trying again to file under the new
2053 (const :tag
"Never" nil
)
2054 (const :tag
"Always" t
)
2055 (const :tag
"Prompt for confirmation" confirm
)))
2057 (defcustom org-refile-active-region-within-subtree nil
2058 "Non-nil means also refile active region within a subtree.
2060 By default `org-refile' doesn't allow refiling regions if they
2061 don't contain a set of subtrees, but it might be convenient to
2062 do so sometimes: in that case, the first line of the region is
2063 converted to a headline before refiling."
2068 (defgroup org-todo nil
2069 "Options concerning TODO items in Org-mode."
2073 (defgroup org-progress nil
2074 "Options concerning Progress logging in Org-mode."
2078 (defvar org-todo-interpretation-widgets
2079 '((:tag
"Sequence (cycling hits every state)" sequence
)
2080 (:tag
"Type (cycling directly to DONE)" type
))
2081 "The available interpretation symbols for customizing `org-todo-keywords'.
2082 Interested libraries should add to this list.")
2084 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2085 "List of TODO entry keyword sequences and their interpretation.
2086 \\<org-mode-map>This is a list of sequences.
2088 Each sequence starts with a symbol, either `sequence' or `type',
2089 indicating if the keywords should be interpreted as a sequence of
2090 action steps, or as different types of TODO items. The first
2091 keywords are states requiring action - these states will select a headline
2092 for inclusion into the global TODO list Org-mode produces. If one of
2093 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2094 signify that no further action is necessary. If \"|\" is not found,
2095 the last keyword is treated as the only DONE state of the sequence.
2097 The command \\[org-todo] cycles an entry through these states, and one
2098 additional state where no keyword is present. For details about this
2099 cycling, see the manual.
2101 TODO keywords and interpretation can also be set on a per-file basis with
2102 the special #+SEQ_TODO and #+TYP_TODO lines.
2104 Each keyword can optionally specify a letter for fast state selection
2105 \(in combination with the variable `org-use-fast-todo-selection')
2106 and specifiers for state change logging, using the same syntax that
2107 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2108 the WAIT state can be selected with the \"w\" letter. \"WAIT(w!)\"
2109 indicates to record a time stamp each time this state is selected.
2111 Each keyword may also specify if a timestamp or a note should be
2112 recorded when entering or leaving the state, by adding additional
2113 characters in the parenthesis after the keyword. This looks like this:
2114 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2115 record only the time of the state change. With X and Y being either
2116 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2117 Y when leaving the state if and only if the *target* state does not
2118 define X. You may omit any of the fast-selection key or X or /Y,
2119 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2121 For backward compatibility, this variable may also be just a list
2122 of keywords. In this case the interpretation (sequence or type) will be
2123 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2125 :group
'org-keywords
2127 (repeat :tag
"Old syntax, just keywords"
2128 (string :tag
"Keyword"))
2129 (repeat :tag
"New syntax"
2132 :tag
"Interpretation"
2133 ;;Quick and dirty way to see
2134 ;;`org-todo-interpretations'. This takes the
2135 ;;place of item arguments
2143 org-todo-interpretation-widgets
))
2146 (string :tag
"Keyword"))))))
2148 (defvar org-todo-keywords-1 nil
2149 "All TODO and DONE keywords active in a buffer.")
2150 (make-variable-buffer-local 'org-todo-keywords-1
)
2151 (defvar org-todo-keywords-for-agenda nil
)
2152 (defvar org-done-keywords-for-agenda nil
)
2153 (defvar org-drawers-for-agenda nil
)
2154 (defvar org-todo-keyword-alist-for-agenda nil
)
2155 (defvar org-tag-alist-for-agenda nil
)
2156 (defvar org-agenda-contributing-files nil
)
2157 (defvar org-not-done-keywords nil
)
2158 (make-variable-buffer-local 'org-not-done-keywords
)
2159 (defvar org-done-keywords nil
)
2160 (make-variable-buffer-local 'org-done-keywords
)
2161 (defvar org-todo-heads nil
)
2162 (make-variable-buffer-local 'org-todo-heads
)
2163 (defvar org-todo-sets nil
)
2164 (make-variable-buffer-local 'org-todo-sets
)
2165 (defvar org-todo-log-states nil
)
2166 (make-variable-buffer-local 'org-todo-log-states
)
2167 (defvar org-todo-kwd-alist nil
)
2168 (make-variable-buffer-local 'org-todo-kwd-alist
)
2169 (defvar org-todo-key-alist nil
)
2170 (make-variable-buffer-local 'org-todo-key-alist
)
2171 (defvar org-todo-key-trigger nil
)
2172 (make-variable-buffer-local 'org-todo-key-trigger
)
2174 (defcustom org-todo-interpretation
'sequence
2175 "Controls how TODO keywords are interpreted.
2176 This variable is in principle obsolete and is only used for
2177 backward compatibility, if the interpretation of todo keywords is
2178 not given already in `org-todo-keywords'. See that variable for
2181 :group
'org-keywords
2182 :type
'(choice (const sequence
)
2185 (defcustom org-use-fast-todo-selection t
2186 "Non-nil means use the fast todo selection scheme with C-c C-t.
2187 This variable describes if and under what circumstances the cycling
2188 mechanism for TODO keywords will be replaced by a single-key, direct
2191 When nil, fast selection is never used.
2193 When the symbol `prefix', it will be used when `org-todo' is called
2194 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2195 `C-u t' in an agenda buffer.
2197 When t, fast selection is used by default. In this case, the prefix
2198 argument forces cycling instead.
2200 In all cases, the special interface is only used if access keys have
2201 actually been assigned by the user, i.e. if keywords in the configuration
2202 are followed by a letter in parenthesis, like TODO(t)."
2205 (const :tag
"Never" nil
)
2206 (const :tag
"By default" t
)
2207 (const :tag
"Only with C-u C-c C-t" prefix
)))
2209 (defcustom org-provide-todo-statistics t
2210 "Non-nil means update todo statistics after insert and toggle.
2211 ALL-HEADLINES means update todo statistics by including headlines
2212 with no TODO keyword as well, counting them as not done.
2213 A list of TODO keywords means the same, but skip keywords that are
2216 When this is set, todo statistics is updated in the parent of the
2217 current entry each time a todo state is changed."
2220 (const :tag
"Yes, only for TODO entries" t
)
2221 (const :tag
"Yes, including all entries" 'all-headlines
)
2222 (repeat :tag
"Yes, for TODOs in this list"
2223 (string :tag
"TODO keyword"))
2224 (other :tag
"No TODO statistics" nil
)))
2226 (defcustom org-hierarchical-todo-statistics t
2227 "Non-nil means TODO statistics covers just direct children.
2228 When nil, all entries in the subtree are considered.
2229 This has only an effect if `org-provide-todo-statistics' is set.
2230 To set this to nil for only a single subtree, use a COOKIE_DATA
2231 property and include the word \"recursive\" into the value."
2235 (defcustom org-after-todo-state-change-hook nil
2236 "Hook which is run after the state of a TODO item was changed.
2237 The new state (a string with a TODO keyword, or nil) is available in the
2238 Lisp variable `org-state'."
2242 (defvar org-blocker-hook nil
2243 "Hook for functions that are allowed to block a state change.
2245 Each function gets as its single argument a property list, see
2246 `org-trigger-hook' for more information about this list.
2248 If any of the functions in this hook returns nil, the state change
2251 (defvar org-trigger-hook nil
2252 "Hook for functions that are triggered by a state change.
2254 Each function gets as its single argument a property list with at least
2255 the following elements:
2257 (:type type-of-change :position pos-at-entry-start
2258 :from old-state :to new-state)
2260 Depending on the type, more properties may be present.
2262 This mechanism is currently implemented for:
2266 :type todo-state-change
2267 :from previous state (keyword as a string), or nil, or a symbol
2268 'todo' or 'done', to indicate the general type of state.
2269 :to new state, like in :from")
2271 (defcustom org-enforce-todo-dependencies nil
2272 "Non-nil means undone TODO entries will block switching the parent to DONE.
2273 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2274 be blocked if any prior sibling is not yet done.
2275 Finally, if the parent is blocked because of ordered siblings of its own,
2276 the child will also be blocked."
2277 :set
(lambda (var val
)
2280 (add-hook 'org-blocker-hook
2281 'org-block-todo-from-children-or-siblings-or-parent
)
2282 (remove-hook 'org-blocker-hook
2283 'org-block-todo-from-children-or-siblings-or-parent
)))
2287 (defcustom org-enforce-todo-checkbox-dependencies nil
2288 "Non-nil means unchecked boxes will block switching the parent to DONE.
2289 When this is nil, checkboxes have no influence on switching TODO states.
2290 When non-nil, you first need to check off all check boxes before the TODO
2291 entry can be switched to DONE.
2292 This variable needs to be set before org.el is loaded, and you need to
2293 restart Emacs after a change to make the change effective. The only way
2294 to change is while Emacs is running is through the customize interface."
2295 :set
(lambda (var val
)
2298 (add-hook 'org-blocker-hook
2299 'org-block-todo-from-checkboxes
)
2300 (remove-hook 'org-blocker-hook
2301 'org-block-todo-from-checkboxes
)))
2305 (defcustom org-treat-insert-todo-heading-as-state-change nil
2306 "Non-nil means inserting a TODO heading is treated as state change.
2307 So when the command \\[org-insert-todo-heading] is used, state change
2308 logging will apply if appropriate. When nil, the new TODO item will
2309 be inserted directly, and no logging will take place."
2313 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2314 "Non-nil means switching TODO states with S-cursor counts as state change.
2315 This is the default behavior. However, setting this to nil allows a
2316 convenient way to select a TODO state and bypass any logging associated
2321 (defcustom org-todo-state-tags-triggers nil
2322 "Tag changes that should be triggered by TODO state changes.
2323 This is a list. Each entry is
2325 (state-change (tag . flag) .......)
2327 State-change can be a string with a state, and empty string to indicate the
2328 state that has no TODO keyword, or it can be one of the symbols `todo'
2329 or `done', meaning any not-done or done state, respectively."
2333 (cons (choice :tag
"When changing to"
2334 (const :tag
"Not-done state" todo
)
2335 (const :tag
"Done state" done
)
2336 (string :tag
"State"))
2338 (cons :tag
"Tag action"
2340 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2342 (defcustom org-log-done nil
2343 "Information to record when a task moves to the DONE state.
2345 Possible values are:
2347 nil Don't add anything, just change the keyword
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: nologdone
2355 #+STARTUP: lognotedone
2357 You can have local logging settings for a subtree by setting the LOGGING
2358 property to one or more of these keywords."
2360 :group
'org-progress
2362 (const :tag
"No logging" nil
)
2363 (const :tag
"Record CLOSED timestamp" time
)
2364 (const :tag
"Record CLOSED timestamp with note." note
)))
2366 ;; Normalize old uses of org-log-done.
2368 ((eq org-log-done t
) (setq org-log-done
'time
))
2369 ((and (listp org-log-done
) (memq 'done org-log-done
))
2370 (setq org-log-done
'note
)))
2372 (defcustom org-log-reschedule nil
2373 "Information to record when the scheduling date of a tasks is modified.
2375 Possible values are:
2377 nil Don't add anything, just change the date
2378 time Add a time stamp to the task
2379 note Prompt for a note and add it with template `org-log-note-headings'
2381 This option can also be set with on a per-file-basis with
2383 #+STARTUP: nologreschedule
2384 #+STARTUP: logreschedule
2385 #+STARTUP: lognotereschedule"
2387 :group
'org-progress
2389 (const :tag
"No logging" nil
)
2390 (const :tag
"Record timestamp" time
)
2391 (const :tag
"Record timestamp with note." note
)))
2393 (defcustom org-log-redeadline nil
2394 "Information to record when the deadline date of a tasks is modified.
2396 Possible values are:
2398 nil Don't add anything, just change the date
2399 time Add a time stamp to the task
2400 note Prompt for a note and add it with template `org-log-note-headings'
2402 This option can also be set with on a per-file-basis with
2404 #+STARTUP: nologredeadline
2405 #+STARTUP: logredeadline
2406 #+STARTUP: lognoteredeadline
2408 You can have local logging settings for a subtree by setting the LOGGING
2409 property to one or more of these keywords."
2411 :group
'org-progress
2413 (const :tag
"No logging" nil
)
2414 (const :tag
"Record timestamp" time
)
2415 (const :tag
"Record timestamp with note." note
)))
2417 (defcustom org-log-note-clock-out nil
2418 "Non-nil means record a note when clocking out of an item.
2419 This can also be configured on a per-file basis by adding one of
2420 the following lines anywhere in the buffer:
2422 #+STARTUP: lognoteclock-out
2423 #+STARTUP: nolognoteclock-out"
2425 :group
'org-progress
2428 (defcustom org-log-done-with-time t
2429 "Non-nil means the CLOSED time stamp will contain date and time.
2430 When nil, only the date will be recorded."
2431 :group
'org-progress
2434 (defcustom org-log-note-headings
2435 '((done .
"CLOSING NOTE %t")
2436 (state .
"State %-12s from %-12S %t")
2437 (note .
"Note taken on %t")
2438 (reschedule .
"Rescheduled from %S on %t")
2439 (delschedule .
"Not scheduled, was %S on %t")
2440 (redeadline .
"New deadline from %S on %t")
2441 (deldeadline .
"Removed deadline, was %S on %t")
2442 (refile .
"Refiled on %t")
2444 "Headings for notes added to entries.
2445 The value is an alist, with the car being a symbol indicating the note
2446 context, and the cdr is the heading to be used. The heading may also be the
2448 %t in the heading will be replaced by a time stamp.
2449 %T will be an active time stamp instead the default inactive one
2450 %d will be replaced by a short-format time stamp.
2451 %D will be replaced by an active short-format time stamp.
2452 %s will be replaced by the new TODO state, in double quotes.
2453 %S will be replaced by the old TODO state, in double quotes.
2454 %u will be replaced by the user name.
2455 %U will be replaced by the full user name.
2457 In fact, it is not a good idea to change the `state' entry, because
2458 agenda log mode depends on the format of these entries."
2460 :group
'org-progress
2461 :type
'(list :greedy t
2462 (cons (const :tag
"Heading when closing an item" done
) string
)
2464 "Heading when changing todo state (todo sequence only)"
2466 (cons (const :tag
"Heading when just taking a note" note
) string
)
2467 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2468 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2469 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2470 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2471 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2472 (cons (const :tag
"Heading when refiling" refile
) string
)))
2474 (unless (assq 'note org-log-note-headings
)
2475 (push '(note .
"%t") org-log-note-headings
))
2477 (defcustom org-log-into-drawer nil
2478 "Non-nil means insert state change notes and time stamps into a drawer.
2479 When nil, state changes notes will be inserted after the headline and
2480 any scheduling and clock lines, but not inside a drawer.
2482 The value of this variable should be the name of the drawer to use.
2483 LOGBOOK is proposed as the default drawer for this purpose, you can
2484 also set this to a string to define the drawer of your choice.
2486 A value of t is also allowed, representing \"LOGBOOK\".
2488 If this variable is set, `org-log-state-notes-insert-after-drawers'
2491 You can set the property LOG_INTO_DRAWER to overrule this setting for
2494 :group
'org-progress
2496 (const :tag
"Not into a drawer" nil
)
2497 (const :tag
"LOGBOOK" t
)
2498 (string :tag
"Other")))
2500 (if (fboundp 'defvaralias
)
2501 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2503 (defun org-log-into-drawer ()
2504 "Return the value of `org-log-into-drawer', but let properties overrule.
2505 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2506 used instead of the default value."
2507 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
)))
2509 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2510 ((equal p
"t") "LOGBOOK")
2513 (defcustom org-log-state-notes-insert-after-drawers nil
2514 "Non-nil means insert state change notes after any drawers in entry.
2515 Only the drawers that *immediately* follow the headline and the
2516 deadline/scheduled line are skipped.
2517 When nil, insert notes right after the heading and perhaps the line
2518 with deadline/scheduling if present.
2520 This variable will have no effect if `org-log-into-drawer' is
2523 :group
'org-progress
2526 (defcustom org-log-states-order-reversed t
2527 "Non-nil means the latest state note will be directly after heading.
2528 When nil, the state change notes will be ordered according to time."
2530 :group
'org-progress
2533 (defcustom org-todo-repeat-to-state nil
2534 "The TODO state to which a repeater should return the repeating task.
2535 By default this is the first task in a TODO sequence, or the previous state
2536 in a TODO_TYP set. But you can specify another task here.
2537 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2540 :type
'(choice (const :tag
"Head of sequence" nil
)
2541 (string :tag
"Specific state")))
2543 (defcustom org-log-repeat
'time
2544 "Non-nil means record moving through the DONE state when triggering repeat.
2545 An auto-repeating task is immediately switched back to TODO when
2546 marked DONE. If you are not logging state changes (by adding \"@\"
2547 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2548 record a closing note, there will be no record of the task moving
2549 through DONE. This variable forces taking a note anyway.
2551 nil Don't force a record
2552 time Record a time stamp
2555 This option can also be set with on a per-file-basis with
2557 #+STARTUP: logrepeat
2558 #+STARTUP: lognoterepeat
2559 #+STARTUP: nologrepeat
2561 You can have local logging settings for a subtree by setting the LOGGING
2562 property to one or more of these keywords."
2564 :group
'org-progress
2566 (const :tag
"Don't force a record" nil
)
2567 (const :tag
"Force recording the DONE state" time
)
2568 (const :tag
"Force recording a note with the DONE state" note
)))
2571 (defgroup org-priorities nil
2572 "Priorities in Org-mode."
2573 :tag
"Org Priorities"
2576 (defcustom org-enable-priority-commands t
2577 "Non-nil means priority commands are active.
2578 When nil, these commands will be disabled, so that you never accidentally
2580 :group
'org-priorities
2583 (defcustom org-highest-priority ?A
2584 "The highest priority of TODO items. A character like ?A, ?B etc.
2585 Must have a smaller ASCII number than `org-lowest-priority'."
2586 :group
'org-priorities
2589 (defcustom org-lowest-priority ?C
2590 "The lowest priority of TODO items. A character like ?A, ?B etc.
2591 Must have a larger ASCII number than `org-highest-priority'."
2592 :group
'org-priorities
2595 (defcustom org-default-priority ?B
2596 "The default priority of TODO items.
2597 This is the priority an item gets if no explicit priority is given.
2598 When starting to cycle on an empty priority the first step in the cycle
2599 depends on `org-priority-start-cycle-with-default'. The resulting first
2600 step priority must not exceed the range from `org-highest-priority' to
2601 `org-lowest-priority' which means that `org-default-priority' has to be
2602 in this range exclusive or inclusive the range boundaries. Else the
2603 first step refuses to set the default and the second will fall back
2604 to (depending on the command used) the highest or lowest priority."
2605 :group
'org-priorities
2608 (defcustom org-priority-start-cycle-with-default t
2609 "Non-nil means start with default priority when starting to cycle.
2610 When this is nil, the first step in the cycle will be (depending on the
2611 command used) one higher or lower than the default priority.
2612 See also `org-default-priority'."
2613 :group
'org-priorities
2616 (defcustom org-get-priority-function nil
2617 "Function to extract the priority from a string.
2618 The string is normally the headline. If this is nil Org computes the
2619 priority from the priority cookie like [#A] in the headline. It returns
2620 an integer, increasing by 1000 for each priority level.
2621 The user can set a different function here, which should take a string
2622 as an argument and return the numeric priority."
2623 :group
'org-priorities
2627 (defgroup org-time nil
2628 "Options concerning time stamps and deadlines in Org-mode."
2632 (defcustom org-insert-labeled-timestamps-at-point nil
2633 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2634 When nil, these labeled time stamps are forces into the second line of an
2635 entry, just after the headline. When scheduling from the global TODO list,
2636 the time stamp will always be forced into the second line."
2640 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2641 "Formats for `format-time-string' which are used for time stamps.
2642 It is not recommended to change this constant.")
2644 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2645 "Number of minutes to round time stamps to.
2646 These are two values, the first applies when first creating a time stamp.
2647 The second applies when changing it with the commands `S-up' and `S-down'.
2648 When changing the time stamp, this means that it will change in steps
2649 of N minutes, as given by the second value.
2651 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2652 numbers should be factors of 60, so for example 5, 10, 15.
2654 When this is larger than 1, you can still force an exact time stamp by using
2655 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2656 and by using a prefix arg to `S-up/down' to specify the exact number
2657 of minutes to shift."
2659 :get
#'(lambda (var) ; Make sure both elements are there
2660 (if (integerp (default-value var
))
2661 (list (default-value var
) 5)
2662 (default-value var
)))
2664 (integer :tag
"when inserting times")
2665 (integer :tag
"when modifying times")))
2667 ;; Normalize old customizations of this variable.
2668 (when (integerp org-time-stamp-rounding-minutes
)
2669 (setq org-time-stamp-rounding-minutes
2670 (list org-time-stamp-rounding-minutes
2671 org-time-stamp-rounding-minutes
)))
2673 (defcustom org-display-custom-times nil
2674 "Non-nil means overlay custom formats over all time stamps.
2675 The formats are defined through the variable `org-time-stamp-custom-formats'.
2676 To turn this on on a per-file basis, insert anywhere in the file:
2677 #+STARTUP: customtime"
2681 (make-variable-buffer-local 'org-display-custom-times
)
2683 (defcustom org-time-stamp-custom-formats
2684 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2685 "Custom formats for time stamps. See `format-time-string' for the syntax.
2686 These are overlaid over the default ISO format if the variable
2687 `org-display-custom-times' is set. Time like %H:%M should be at the
2688 end of the second format. The custom formats are also honored by export
2689 commands, if custom time display is turned on at the time of export."
2693 (defun org-time-stamp-format (&optional long inactive
)
2694 "Get the right format for a time string."
2695 (let ((f (if long
(cdr org-time-stamp-formats
)
2696 (car org-time-stamp-formats
))))
2698 (concat "[" (substring f
1 -
1) "]")
2701 (defcustom org-time-clocksum-format
"%d:%02d"
2702 "The format string used when creating CLOCKSUM lines.
2703 This is also used when org-mode generates a time duration."
2707 (defcustom org-time-clocksum-use-fractional nil
2708 "If non-nil, \\[org-clock-display] uses fractional times.
2709 org-mode generates a time duration."
2713 (defcustom org-time-clocksum-fractional-format
"%.2f"
2714 "The format string used when creating CLOCKSUM lines, or when
2715 org-mode generates a time duration."
2719 (defcustom org-deadline-warning-days
14
2720 "No. of days before expiration during which a deadline becomes active.
2721 This variable governs the display in sparse trees and in the agenda.
2722 When 0 or negative, it means use this number (the absolute value of it)
2723 even if a deadline has a different individual lead time specified.
2725 Custom commands can set this variable in the options section."
2727 :group
'org-agenda-daily
/weekly
2730 (defcustom org-read-date-prefer-future t
2731 "Non-nil means assume future for incomplete date input from user.
2732 This affects the following situations:
2733 1. The user gives a month but not a year.
2734 For example, if it is April and you enter \"feb 2\", this will be read
2735 as Feb 2, *next* year. \"May 5\", however, will be this year.
2736 2. The user gives a day, but no month.
2737 For example, if today is the 15th, and you enter \"3\", Org-mode will
2738 read this as the third of *next* month. However, if you enter \"17\",
2739 it will be considered as *this* month.
2741 If you set this variable to the symbol `time', then also the following
2744 3. If the user gives a time, but no day. If the time is before now,
2745 to will be interpreted as tomorrow.
2747 Currently none of this works for ISO week specifications.
2749 When this option is nil, the current day, month and year will always be
2752 See also `org-agenda-jump-prefer-future'."
2755 (const :tag
"Never" nil
)
2756 (const :tag
"Check month and day" t
)
2757 (const :tag
"Check month, day, and time" time
)))
2759 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2760 "Should the agenda jump command prefer the future for incomplete dates?
2761 The default is to do the same as configured in `org-read-date-prefer-future'.
2762 But you can also set a deviating value here.
2763 This may t or nil, or the symbol `org-read-date-prefer-future'."
2768 (const :tag
"Use org-read-date-prefer-future"
2769 org-read-date-prefer-future
)
2770 (const :tag
"Never" nil
)
2771 (const :tag
"Always" t
)))
2773 (defcustom org-read-date-force-compatible-dates t
2774 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2776 Depending on the system Emacs is running on, certain dates cannot
2777 be represented with the type used internally to represent time.
2778 Dates between 1970-1-1 and 2038-1-1 can always be represented
2779 correctly. Some systems allow for earlier dates, some for later,
2780 some for both. One way to find out it to insert any date into an
2781 Org buffer, putting the cursor on the year and hitting S-up and
2782 S-down to test the range.
2784 When this variable is set to t, the date/time prompt will not let
2785 you specify dates outside the 1970-2037 range, so it is certain that
2786 these dates will work in whatever version of Emacs you are
2787 running, and also that you can move a file from one Emacs implementation
2788 to another. WHenever Org is forcing the year for you, it will display
2791 When this variable is nil, Org will check if the date is
2792 representable in the specific Emacs implementation you are using.
2793 If not, it will force a year, usually the current year, and beep
2794 to remind you. Currently this setting is not recommended because
2795 the likelihood that you will open your Org files in an Emacs that
2796 has limited date range is not negligible.
2798 A workaround for this problem is to use diary sexp dates for time
2799 stamps outside of this range."
2804 (defcustom org-read-date-display-live t
2805 "Non-nil means display current interpretation of date prompt live.
2806 This display will be in an overlay, in the minibuffer."
2810 (defcustom org-read-date-popup-calendar t
2811 "Non-nil means pop up a calendar when prompting for a date.
2812 In the calendar, the date can be selected with mouse-1. However, the
2813 minibuffer will also be active, and you can simply enter the date as well.
2814 When nil, only the minibuffer will be available."
2817 (if (fboundp 'defvaralias
)
2818 (defvaralias 'org-popup-calendar-for-date-prompt
2819 'org-read-date-popup-calendar
))
2821 (defcustom org-read-date-minibuffer-setup-hook nil
2822 "Hook to be used to set up keys for the date/time interface.
2823 Add key definitions to `minibuffer-local-map', which will be a temporary
2828 (defcustom org-extend-today-until
0
2829 "The hour when your day really ends. Must be an integer.
2830 This has influence for the following applications:
2831 - When switching the agenda to \"today\". It it is still earlier than
2832 the time given here, the day recognized as TODAY is actually yesterday.
2833 - When a date is read from the user and it is still before the time given
2834 here, the current date and time will be assumed to be yesterday, 23:59.
2835 Also, timestamps inserted in remember templates follow this rule.
2837 IMPORTANT: This is a feature whose implementation is and likely will
2838 remain incomplete. Really, it is only here because past midnight seems to
2839 be the favorite working time of John Wiegley :-)"
2843 (defcustom org-use-effective-time nil
2844 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2845 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2846 \"effective time\" of any timestamps between midnight and 8am will be
2847 23:59 of the previous day."
2852 (defcustom org-edit-timestamp-down-means-later nil
2853 "Non-nil means S-down will increase the time in a time stamp.
2854 When nil, S-up will increase."
2858 (defcustom org-calendar-follow-timestamp-change t
2859 "Non-nil means make the calendar window follow timestamp changes.
2860 When a timestamp is modified and the calendar window is visible, it will be
2861 moved to the new date."
2865 (defgroup org-tags nil
2866 "Options concerning tags in Org-mode."
2870 (defcustom org-tag-alist nil
2871 "List of tags allowed in Org-mode files.
2872 When this list is nil, Org-mode will base TAG input on what is already in the
2874 The value of this variable is an alist, the car of each entry must be a
2875 keyword as a string, the cdr may be a character that is used to select
2876 that tag through the fast-tag-selection interface.
2877 See the manual for details."
2881 (cons (string :tag
"Tag name")
2882 (character :tag
"Access char"))
2883 (list :tag
"Start radio group"
2885 (option (string :tag
"Group description")))
2886 (list :tag
"End radio group"
2888 (option (string :tag
"Group description")))
2889 (const :tag
"New line" (:newline
)))))
2891 (defcustom org-tag-persistent-alist nil
2892 "List of tags that will always appear in all Org-mode files.
2893 This is in addition to any in buffer settings or customizations
2895 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2896 The value of this variable is an alist, the car of each entry must be a
2897 keyword as a string, the cdr may be a character that is used to select
2898 that tag through the fast-tag-selection interface.
2899 See the manual for details.
2900 To disable these tags on a per-file basis, insert anywhere in the file:
2905 (cons (string :tag
"Tag name")
2906 (character :tag
"Access char"))
2907 (const :tag
"Start radio group" (:startgroup
))
2908 (const :tag
"End radio group" (:endgroup
))
2909 (const :tag
"New line" (:newline
)))))
2911 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2912 "If non-nil, always offer completion for all tags of all agenda files.
2913 Instead of customizing this variable directly, you might want to
2914 set it locally for capture buffers, because there no list of
2915 tags in that file can be created dynamically (there are none).
2917 (add-hook 'org-capture-mode-hook
2919 (set (make-local-variable
2920 'org-complete-tags-always-offer-all-agenda-tags)
2926 (defvar org-file-tags nil
2927 "List of tags that can be inherited by all entries in the file.
2928 The tags will be inherited if the variable `org-use-tag-inheritance'
2929 says they should be.
2930 This variable is populated from #+FILETAGS lines.")
2932 (defcustom org-use-fast-tag-selection
'auto
2933 "Non-nil means use fast tag selection scheme.
2934 This is a special interface to select and deselect tags with single keys.
2935 When nil, fast selection is never used.
2936 When the symbol `auto', fast selection is used if and only if selection
2937 characters for tags have been configured, either through the variable
2938 `org-tag-alist' or through a #+TAGS line in the buffer.
2939 When t, fast selection is always used and selection keys are assigned
2940 automatically if necessary."
2943 (const :tag
"Always" t
)
2944 (const :tag
"Never" nil
)
2945 (const :tag
"When selection characters are configured" 'auto
)))
2947 (defcustom org-fast-tag-selection-single-key nil
2948 "Non-nil means fast tag selection exits after first change.
2949 When nil, you have to press RET to exit it.
2950 During fast tag selection, you can toggle this flag with `C-c'.
2951 This variable can also have the value `expert'. In this case, the window
2952 displaying the tags menu is not even shown, until you press C-c again."
2955 (const :tag
"No" nil
)
2956 (const :tag
"Yes" t
)
2957 (const :tag
"Expert" expert
)))
2959 (defvar org-fast-tag-selection-include-todo nil
2960 "Non-nil means fast tags selection interface will also offer TODO states.
2961 This is an undocumented feature, you should not rely on it.")
2963 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2964 "The column to which tags should be indented in a headline.
2965 If this number is positive, it specifies the column. If it is negative,
2966 it means that the tags should be flushright to that column. For example,
2967 -80 works well for a normal 80 character screen.
2968 When 0, place tags directly after headline text, with only one space in
2973 (defcustom org-auto-align-tags t
2974 "Non-nil keeps tags aligned when modifying headlines.
2975 Some operations (i.e. demoting) change the length of a headline and
2976 therefore shift the tags around. With this option turned on, after
2977 each such operation the tags are again aligned to `org-tags-column'."
2981 (defcustom org-use-tag-inheritance t
2982 "Non-nil means tags in levels apply also for sublevels.
2983 When nil, only the tags directly given in a specific line apply there.
2984 This may also be a list of tags that should be inherited, or a regexp that
2985 matches tags that should be inherited. Additional control is possible
2986 with the variable `org-tags-exclude-from-inheritance' which gives an
2987 explicit list of tags to be excluded from inheritance., even if the value of
2988 `org-use-tag-inheritance' would select it for inheritance.
2990 If this option is t, a match early-on in a tree can lead to a large
2991 number of matches in the subtree when constructing the agenda or creating
2992 a sparse tree. If you only want to see the first match in a tree during
2993 a search, check out the variable `org-tags-match-list-sublevels'."
2996 (const :tag
"Not" nil
)
2997 (const :tag
"Always" t
)
2998 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2999 (regexp :tag
"Tags matched by regexp")))
3001 (defcustom org-tags-exclude-from-inheritance nil
3002 "List of tags that should never be inherited.
3003 This is a way to exclude a few tags from inheritance. For way to do
3004 the opposite, to actively allow inheritance for selected tags,
3005 see the variable `org-use-tag-inheritance'."
3007 :type
'(repeat (string :tag
"Tag")))
3009 (defun org-tag-inherit-p (tag)
3010 "Check if TAG is one that should be inherited."
3012 ((member tag org-tags-exclude-from-inheritance
) nil
)
3013 ((eq org-use-tag-inheritance t
) t
)
3014 ((not org-use-tag-inheritance
) nil
)
3015 ((stringp org-use-tag-inheritance
)
3016 (string-match org-use-tag-inheritance tag
))
3017 ((listp org-use-tag-inheritance
)
3018 (member tag org-use-tag-inheritance
))
3019 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3021 (defcustom org-tags-match-list-sublevels t
3022 "Non-nil means list also sublevels of headlines matching a search.
3023 This variable applies to tags/property searches, and also to stuck
3024 projects because this search is based on a tags match as well.
3026 When set to the symbol `indented', sublevels are indented with
3029 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3030 the sublevels of a headline matching a tag search often also match
3031 the same search. Listing all of them can create very long lists.
3032 Setting this variable to nil causes subtrees of a match to be skipped.
3034 This variable is semi-obsolete and probably should always be true. It
3035 is better to limit inheritance to certain tags using the variables
3036 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3039 (const :tag
"No, don't list them" nil
)
3040 (const :tag
"Yes, do list them" t
)
3041 (const :tag
"List them, indented with leading dots" indented
)))
3043 (defcustom org-tags-sort-function nil
3044 "When set, tags are sorted using this function as a comparator."
3047 (const :tag
"No sorting" nil
)
3048 (const :tag
"Alphabetical" string
<)
3049 (const :tag
"Reverse alphabetical" string
>)
3050 (function :tag
"Custom function" nil
)))
3052 (defvar org-tags-history nil
3053 "History of minibuffer reads for tags.")
3054 (defvar org-last-tags-completion-table nil
3055 "The last used completion table for tags.")
3056 (defvar org-after-tags-change-hook nil
3057 "Hook that is run after the tags in a line have changed.")
3059 (defgroup org-properties nil
3060 "Options concerning properties in Org-mode."
3061 :tag
"Org Properties"
3064 (defcustom org-property-format
"%-10s %s"
3065 "How property key/value pairs should be formatted by `indent-line'.
3066 When `indent-line' hits a property definition, it will format the line
3067 according to this format, mainly to make sure that the values are
3068 lined-up with respect to each other."
3069 :group
'org-properties
3072 (defcustom org-properties-postprocess-alist nil
3073 "Alist of properties and functions to adjust inserted values.
3074 Elements of this alist must be of the form
3076 ([string] [function])
3078 where [string] must be a property name and [function] must be a
3079 lambda expression: this lambda expression must take one argument,
3080 the value to adjust, and return the new value as a string.
3082 For example, this element will allow the property \"Remaining\"
3083 to be updated wrt the relation between the \"Effort\" property
3084 and the clock summary:
3086 ((\"Remaining\" (lambda(value)
3087 (let ((clocksum (org-clock-sum-current-item))
3088 (effort (org-duration-string-to-minutes
3089 (org-entry-get (point) \"Effort\"))))
3090 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3091 :group
'org-properties
3093 :type
'(alist :key-type
(string :tag
"Property")
3094 :value-type
(function :tag
"Function")))
3096 (defcustom org-use-property-inheritance nil
3097 "Non-nil means properties apply also for sublevels.
3099 This setting is chiefly used during property searches. Turning it on can
3100 cause significant overhead when doing a search, which is why it is not
3103 When nil, only the properties directly given in the current entry count.
3104 When t, every property is inherited. The value may also be a list of
3105 properties that should have inheritance, or a regular expression matching
3106 properties that should be inherited.
3108 However, note that some special properties use inheritance under special
3109 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3110 and the properties ending in \"_ALL\" when they are used as descriptor
3111 for valid values of a property.
3113 Note for programmers:
3114 When querying an entry with `org-entry-get', you can control if inheritance
3115 should be used. By default, `org-entry-get' looks only at the local
3116 properties. You can request inheritance by setting the inherit argument
3117 to t (to force inheritance) or to `selective' (to respect the setting
3119 :group
'org-properties
3121 (const :tag
"Not" nil
)
3122 (const :tag
"Always" t
)
3123 (repeat :tag
"Specific properties" (string :tag
"Property"))
3124 (regexp :tag
"Properties matched by regexp")))
3126 (defun org-property-inherit-p (property)
3127 "Check if PROPERTY is one that should be inherited."
3129 ((eq org-use-property-inheritance t
) t
)
3130 ((not org-use-property-inheritance
) nil
)
3131 ((stringp org-use-property-inheritance
)
3132 (string-match org-use-property-inheritance property
))
3133 ((listp org-use-property-inheritance
)
3134 (member property org-use-property-inheritance
))
3135 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3137 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3138 "The default column format, if no other format has been defined.
3139 This variable can be set on the per-file basis by inserting a line
3141 #+COLUMNS: %25ITEM ....."
3142 :group
'org-properties
3145 (defcustom org-columns-ellipses
".."
3146 "The ellipses to be used when a field in column view is truncated.
3147 When this is the empty string, as many characters as possible are shown,
3148 but then there will be no visual indication that the field has been truncated.
3149 When this is a string of length N, the last N characters of a truncated
3150 field are replaced by this string. If the column is narrower than the
3151 ellipses string, only part of the ellipses string will be shown."
3152 :group
'org-properties
3155 (defcustom org-columns-modify-value-for-display-function nil
3156 "Function that modifies values for display in column view.
3157 For example, it can be used to cut out a certain part from a time stamp.
3158 The function must take 2 arguments:
3160 column-title The title of the column (*not* the property name)
3161 value The value that should be modified.
3163 The function should return the value that should be displayed,
3164 or nil if the normal value should be used."
3165 :group
'org-properties
3168 (defcustom org-effort-property
"Effort"
3169 "The property that is being used to keep track of effort estimates.
3170 Effort estimates given in this property need to have the format H:MM."
3171 :group
'org-properties
3172 :group
'org-progress
3173 :type
'(string :tag
"Property"))
3175 (defconst org-global-properties-fixed
3176 '(("VISIBILITY_ALL" .
"folded children content all")
3177 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3178 "List of property/value pairs that can be inherited by any entry.
3180 These are fixed values, for the preset properties. The user variable
3181 that can be used to add to this list is `org-global-properties'.
3183 The entries in this list are cons cells where the car is a property
3184 name and cdr is a string with the value. If the value represents
3185 multiple items like an \"_ALL\" property, separate the items by
3188 (defcustom org-global-properties nil
3189 "List of property/value pairs that can be inherited by any entry.
3191 This list will be combined with the constant `org-global-properties-fixed'.
3193 The entries in this list are cons cells where the car is a property
3194 name and cdr is a string with the value.
3196 You can set buffer-local values for the same purpose in the variable
3197 `org-file-properties' this by adding lines like
3199 #+PROPERTY: NAME VALUE"
3200 :group
'org-properties
3202 (cons (string :tag
"Property")
3203 (string :tag
"Value"))))
3205 (defvar org-file-properties nil
3206 "List of property/value pairs that can be inherited by any entry.
3207 Valid for the current buffer.
3208 This variable is populated from #+PROPERTY lines.")
3209 (make-variable-buffer-local 'org-file-properties
)
3211 (defgroup org-agenda nil
3212 "Options concerning agenda views in Org-mode."
3216 (defvar org-category nil
3217 "Variable used by org files to set a category for agenda display.
3218 Such files should use a file variable to set it, for example
3220 # -*- mode: org; org-category: \"ELisp\"
3222 or contain a special line
3226 If the file does not specify a category, then file's base name
3228 (make-variable-buffer-local 'org-category
)
3229 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3231 (defcustom org-agenda-files nil
3232 "The files to be used for agenda display.
3233 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3234 \\[org-remove-file]. You can also use customize to edit the list.
3236 If an entry is a directory, all files in that directory that are matched by
3237 `org-agenda-file-regexp' will be part of the file list.
3239 If the value of the variable is not a list but a single file name, then
3240 the list of agenda files is actually stored and maintained in that file, one
3241 agenda file per line. In this file paths can be given relative to
3242 `org-directory'. Tilde expansion and environment variable substitution
3246 (repeat :tag
"List of files and directories" file
)
3247 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3249 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3250 "Regular expression to match files for `org-agenda-files'.
3251 If any element in the list in that variable contains a directory instead
3252 of a normal file, all files in that directory that are matched by this
3253 regular expression will be included."
3257 (defcustom org-agenda-text-search-extra-files nil
3258 "List of extra files to be searched by text search commands.
3259 These files will be search in addition to the agenda files by the
3260 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3261 Note that these files will only be searched for text search commands,
3262 not for the other agenda views like todo lists, tag searches or the weekly
3263 agenda. This variable is intended to list notes and possibly archive files
3264 that should also be searched by these two commands.
3265 In fact, if the first element in the list is the symbol `agenda-archives',
3266 than all archive files of all agenda files will be added to the search
3269 :type
'(set :greedy t
3270 (const :tag
"Agenda Archives" agenda-archives
)
3271 (repeat :inline t
(file))))
3273 (if (fboundp 'defvaralias
)
3274 (defvaralias 'org-agenda-multi-occur-extra-files
3275 'org-agenda-text-search-extra-files
))
3277 (defcustom org-agenda-skip-unavailable-files nil
3278 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3279 A nil value means to remove them, after a query, from the list."
3283 (defcustom org-calendar-to-agenda-key
[?c
]
3284 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3285 The command `org-calendar-goto-agenda' will be bound to this key. The
3286 default is the character `c' because then `c' can be used to switch back and
3287 forth between agenda and calendar."
3291 (defcustom org-calendar-agenda-action-key
[?k
]
3292 "The key to be installed in `calendar-mode-map' for agenda-action.
3293 The command `org-agenda-action' will be bound to this key. The
3294 default is the character `k' because we use the same key in the agenda."
3298 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3299 "The key to be installed in `calendar-mode-map' for adding diary entries.
3300 This option is irrelevant until `org-agenda-diary-file' has been configured
3301 to point to an Org-mode file. When that is the case, the command
3302 `org-agenda-diary-entry' will be bound to the key given here, by default
3303 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3304 if you want to continue doing this, you need to change this to a different
3309 (defcustom org-agenda-diary-file
'diary-file
3310 "File to which to add new entries with the `i' key in agenda and calendar.
3311 When this is the symbol `diary-file', the functionality in the Emacs
3312 calendar will be used to add entries to the `diary-file'. But when this
3313 points to a file, `org-agenda-diary-entry' will be used instead."
3316 (const :tag
"The standard Emacs diary file" diary-file
)
3317 (file :tag
"Special Org file diary entries")))
3319 (eval-after-load "calendar"
3321 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3322 'org-calendar-goto-agenda
)
3323 (org-defkey calendar-mode-map org-calendar-agenda-action-key
3325 (add-hook 'calendar-mode-hook
3327 (unless (eq org-agenda-diary-file
'diary-file
)
3328 (define-key calendar-mode-map
3329 org-calendar-insert-diary-entry-key
3330 'org-agenda-diary-entry
))))))
3332 (defgroup org-latex nil
3333 "Options for embedding LaTeX code into Org-mode."
3337 (defcustom org-format-latex-options
3338 '(:foreground default
:background default
:scale
1.0
3339 :html-foreground
"Black" :html-background
"Transparent"
3340 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3341 "Options for creating images from LaTeX fragments.
3342 This is a property list with the following properties:
3343 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3344 `default' means use the foreground of the default face.
3345 :background the background color, or \"Transparent\".
3346 `default' means use the background of the default face.
3347 :scale a scaling factor for the size of the images, to get more pixels
3348 :html-foreground, :html-background, :html-scale
3349 the same numbers for HTML export.
3350 :matchers a list indicating which matchers should be used to
3351 find LaTeX fragments. Valid members of this list are:
3352 \"begin\" find environments
3353 \"$1\" find single characters surrounded by $.$
3354 \"$\" find math expressions surrounded by $...$
3355 \"$$\" find math expressions surrounded by $$....$$
3356 \"\\(\" find math expressions surrounded by \\(...\\)
3357 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3361 (defcustom org-format-latex-signal-error t
3362 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3363 When nil, just push out a message."
3368 (defcustom org-latex-to-mathml-jar-file nil
3369 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3370 Use this to specify additional executable file say a jar file.
3372 When using MathToWeb as the converter, specify the full-path to
3373 your mathtoweb.jar file."
3377 (const :tag
"None" nil
)
3378 (file :tag
"JAR file" :must-match t
)))
3380 (defcustom org-latex-to-mathml-convert-command nil
3381 "Command to convert LaTeX fragments to MathML.
3382 Replace format-specifiers in the command as noted below and use
3383 `shell-command' to convert LaTeX to MathML.
3384 %j: Executable file in fully expanded form as specified by
3385 `org-latex-to-mathml-jar-file'.
3386 %I: Input LaTeX file in fully expanded form
3387 %o: Output MathML file
3388 This command is used by `org-create-math-formula'.
3390 When using MathToWeb as the converter, set this to
3391 \"java -jar %j -unicode -force -df %o %I\"."
3395 (const :tag
"None" nil
)
3396 (string :tag
"\nShell command")))
3398 (defcustom org-latex-create-formula-image-program
'dvipng
3399 "Program to convert LaTeX fragments with.
3401 dvipng Process the LaTeX fragments to dvi file, then convert
3402 dvi files to png files using dvipng.
3403 This will also include processing of non-math environments.
3404 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3405 to convert pdf files to png files"
3409 (const :tag
"dvipng" dvipng
)
3410 (const :tag
"imagemagick" imagemagick
)))
3412 (defun org-format-latex-mathml-available-p ()
3413 "Return t if `org-latex-to-mathml-convert-command' is usable."
3415 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3416 org-latex-to-mathml-convert-command
)
3417 (let ((executable (car (split-string
3418 org-latex-to-mathml-convert-command
))))
3419 (when (executable-find executable
)
3421 "%j" org-latex-to-mathml-convert-command
)
3422 (file-readable-p org-latex-to-mathml-jar-file
)
3425 (defcustom org-format-latex-header
"\\documentclass{article}
3426 \\usepackage[usenames]{color}
3427 \\usepackage{amsmath}
3428 \\usepackage[mathscr]{eucal}
3429 \\pagestyle{empty} % do not remove
3432 % The settings below are copied from fullpage.sty
3433 \\setlength{\\textwidth}{\\paperwidth}
3434 \\addtolength{\\textwidth}{-3cm}
3435 \\setlength{\\oddsidemargin}{1.5cm}
3436 \\addtolength{\\oddsidemargin}{-2.54cm}
3437 \\setlength{\\evensidemargin}{\\oddsidemargin}
3438 \\setlength{\\textheight}{\\paperheight}
3439 \\addtolength{\\textheight}{-\\headheight}
3440 \\addtolength{\\textheight}{-\\headsep}
3441 \\addtolength{\\textheight}{-\\footskip}
3442 \\addtolength{\\textheight}{-3cm}
3443 \\setlength{\\topmargin}{1.5cm}
3444 \\addtolength{\\topmargin}{-2.54cm}"
3445 "The document header used for processing LaTeX fragments.
3446 It is imperative that this header make sure that no page number
3447 appears on the page. The package defined in the variables
3448 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3449 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3454 (defvar org-format-latex-header-extra nil
)
3456 (defun org-set-packages-alist (var val
)
3457 "Set the packages alist and make sure it has 3 elements per entry."
3458 (set var
(mapcar (lambda (x)
3459 (if (and (consp x
) (= (length x
) 2))
3460 (list (car x
) (nth 1 x
) t
)
3464 (defun org-get-packages-alist (var)
3466 "Get the packages alist and make sure it has 3 elements per entry."
3468 (if (and (consp x
) (= (length x
) 2))
3469 (list (car x
) (nth 1 x
) t
)
3471 (default-value var
)))
3473 ;; The following variables are defined here because is it also used
3474 ;; when formatting latex fragments. Originally it was part of the
3475 ;; LaTeX exporter, which is why the name includes "export".
3476 (defcustom org-export-latex-default-packages-alist
3477 '(("AUTO" "inputenc" t
)
3481 ("" "longtable" nil
)
3493 "Alist of default packages to be inserted in the header.
3494 Change this only if one of the packages here causes an incompatibility
3495 with another package you are using.
3496 The packages in this list are needed by one part or another of Org-mode
3497 to function properly.
3499 - inputenc, fontenc: for basic font and character selection
3500 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3501 for interpreting the entities in `org-entities'. You can skip some of these
3502 packages if you don't use any of the symbols in it.
3503 - graphicx: for including images
3504 - float, wrapfig: for figure placement
3505 - longtable: for long tables
3506 - hyperref: for cross references
3508 Therefore you should not modify this variable unless you know what you
3509 are doing. The one reason to change it anyway is that you might be loading
3510 some other package that conflicts with one of the default packages.
3511 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3512 If SNIPPET-FLAG is t, the package also needs to be included when
3513 compiling LaTeX snippets into images for inclusion into HTML."
3514 :group
'org-export-latex
3515 :set
'org-set-packages-alist
3516 :get
'org-get-packages-alist
3520 (list :tag
"options/package pair"
3521 (string :tag
"options")
3522 (string :tag
"package")
3523 (boolean :tag
"Snippet"))
3524 (string :tag
"A line of LaTeX"))))
3526 (defcustom org-export-latex-packages-alist nil
3527 "Alist of packages to be inserted in every LaTeX header.
3528 These will be inserted after `org-export-latex-default-packages-alist'.
3529 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3530 SNIPPET-FLAG, when t, indicates that this package is also needed when
3531 turning LaTeX snippets into images for inclusion into HTML.
3532 Make sure that you only list packages here which:
3533 - you want in every file
3534 - do not conflict with the default packages in
3535 `org-export-latex-default-packages-alist'
3536 - do not conflict with the setup in `org-format-latex-header'."
3537 :group
'org-export-latex
3538 :set
'org-set-packages-alist
3539 :get
'org-get-packages-alist
3542 (list :tag
"options/package pair"
3543 (string :tag
"options")
3544 (string :tag
"package")
3545 (boolean :tag
"Snippet"))
3546 (string :tag
"A line of LaTeX"))))
3549 (defgroup org-appearance nil
3550 "Settings for Org-mode appearance."
3551 :tag
"Org Appearance"
3554 (defcustom org-level-color-stars-only nil
3555 "Non-nil means fontify only the stars in each headline.
3556 When nil, the entire headline is fontified.
3557 Changing it requires restart of `font-lock-mode' to become effective
3558 also in regions already fontified."
3559 :group
'org-appearance
3562 (defcustom org-hide-leading-stars nil
3563 "Non-nil means hide the first N-1 stars in a headline.
3564 This works by using the face `org-hide' for these stars. This
3565 face is white for a light background, and black for a dark
3566 background. You may have to customize the face `org-hide' to
3568 Changing it requires restart of `font-lock-mode' to become effective
3569 also in regions already fontified.
3570 You may also set this on a per-file basis by adding one of the following
3571 lines to the buffer:
3573 #+STARTUP: hidestars
3574 #+STARTUP: showstars"
3575 :group
'org-appearance
3578 (defcustom org-hidden-keywords nil
3579 "List of symbols corresponding to keywords to be hidden the org buffer.
3580 For example, a value '(title) for this list will make the document's title
3581 appear in the buffer without the initial #+TITLE: keyword."
3582 :group
'org-appearance
3584 :type
'(set (const :tag
"#+AUTHOR" author
)
3585 (const :tag
"#+DATE" date
)
3586 (const :tag
"#+EMAIL" email
)
3587 (const :tag
"#+TITLE" title
)))
3589 (defcustom org-fontify-done-headline nil
3590 "Non-nil means change the face of a headline if it is marked DONE.
3591 Normally, only the TODO/DONE keyword indicates the state of a headline.
3592 When this is non-nil, the headline after the keyword is set to the
3593 `org-headline-done' as an additional indication."
3594 :group
'org-appearance
3597 (defcustom org-fontify-emphasized-text t
3598 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3599 Changing this variable requires a restart of Emacs to take effect."
3600 :group
'org-appearance
3603 (defcustom org-fontify-whole-heading-line nil
3604 "Non-nil means fontify the whole line for headings.
3605 This is useful when setting a background color for the
3607 :group
'org-appearance
3610 (defcustom org-highlight-latex-fragments-and-specials nil
3611 "Non-nil means fontify what is treated specially by the exporters."
3612 :group
'org-appearance
3615 (defcustom org-hide-emphasis-markers nil
3616 "Non-nil mean font-lock should hide the emphasis marker characters."
3617 :group
'org-appearance
3620 (defcustom org-pretty-entities nil
3621 "Non-nil means show entities as UTF8 characters.
3622 When nil, the \\name form remains in the buffer."
3623 :group
'org-appearance
3627 (defcustom org-pretty-entities-include-sub-superscripts t
3628 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3629 :group
'org-appearance
3633 (defvar org-emph-re nil
3634 "Regular expression for matching emphasis.
3635 After a match, the match groups contain these elements:
3636 0 The match of the full regular expression, including the characters
3637 before and after the proper match
3638 1 The character before the proper match, or empty at beginning of line
3639 2 The proper match, including the leading and trailing markers
3640 3 The leading marker like * or /, indicating the type of highlighting
3641 4 The text between the emphasis markers, not including the markers
3642 5 The character after the match, empty at the end of a line")
3643 (defvar org-verbatim-re nil
3644 "Regular expression for matching verbatim text.")
3645 (defvar org-emphasis-regexp-components
) ; defined just below
3646 (defvar org-emphasis-alist
) ; defined just below
3647 (defun org-set-emph-re (var val
)
3648 "Set variable and compute the emphasis regular expression."
3650 (when (and (boundp 'org-emphasis-alist
)
3651 (boundp 'org-emphasis-regexp-components
)
3652 org-emphasis-alist org-emphasis-regexp-components
)
3653 (let* ((e org-emphasis-regexp-components
)
3659 (body1 (concat body
"*?"))
3660 (markers (mapconcat 'car org-emphasis-alist
""))
3661 (vmarkers (mapconcat
3662 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3663 org-emphasis-alist
"")))
3664 ;; make sure special characters appear at the right position in the class
3665 (if (string-match "\\^" markers
)
3666 (setq markers
(concat (replace-match "" t t markers
) "^")))
3667 (if (string-match "-" markers
)
3668 (setq markers
(concat (replace-match "" t t markers
) "-")))
3669 (if (string-match "\\^" vmarkers
)
3670 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3671 (if (string-match "-" vmarkers
)
3672 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3674 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3675 (int-to-string nl
) "\\}")))
3678 (concat "\\([" pre
"]\\|^\\)"
3680 "\\([" markers
"]\\)"
3688 "\\([" post
"]\\|$\\)"))
3689 (setq org-verbatim-re
3690 (concat "\\([" pre
"]\\|^\\)"
3692 "\\([" vmarkers
"]\\)"
3700 "\\([" post
"]\\|$\\)")))))
3702 (defcustom org-emphasis-regexp-components
3703 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3704 "Components used to build the regular expression for emphasis.
3705 This is a list with five entries. Terminology: In an emphasis string
3706 like \" *strong word* \", we call the initial space PREMATCH, the final
3707 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3708 and \"trong wor\" is the body. The different components in this variable
3709 specify what is allowed/forbidden in each part:
3711 pre Chars allowed as prematch. Beginning of line will be allowed too.
3712 post Chars allowed as postmatch. End of line will be allowed too.
3713 border The chars *forbidden* as border characters.
3714 body-regexp A regexp like \".\" to match a body character. Don't use
3715 non-shy groups here, and don't allow newline here.
3716 newline The maximum number of newlines allowed in an emphasis exp.
3718 Use customize to modify this, or restart Emacs after changing it."
3719 :group
'org-appearance
3720 :set
'org-set-emph-re
3722 (sexp :tag
"Allowed chars in pre ")
3723 (sexp :tag
"Allowed chars in post ")
3724 (sexp :tag
"Forbidden chars in border ")
3725 (sexp :tag
"Regexp for body ")
3726 (integer :tag
"number of newlines allowed")
3727 (option (boolean :tag
"Please ignore this button"))))
3729 (defcustom org-emphasis-alist
3730 `(("*" bold
"<b>" "</b>")
3731 ("/" italic
"<i>" "</i>")
3732 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3733 ("=" org-code
"<code>" "</code>" verbatim
)
3734 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3735 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3738 "Special syntax for emphasized text.
3739 Text starting and ending with a special character will be emphasized, for
3740 example *bold*, _underlined_ and /italic/. This variable sets the marker
3741 characters, the face to be used by font-lock for highlighting in Org-mode
3742 Emacs buffers, and the HTML tags to be used for this.
3743 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3744 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3745 Use customize to modify this, or restart Emacs after changing it."
3746 :group
'org-appearance
3747 :set
'org-set-emph-re
3750 (string :tag
"Marker character")
3752 (face :tag
"Font-lock-face")
3753 (plist :tag
"Face property list"))
3754 (string :tag
"HTML start tag")
3755 (string :tag
"HTML end tag")
3756 (option (const verbatim
)))))
3758 (defvar org-protecting-blocks
3759 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3760 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3761 This is needed for font-lock setup.")
3763 ;;; Miscellaneous options
3765 (defgroup org-completion nil
3766 "Completion in Org-mode."
3767 :tag
"Org Completion"
3770 (defcustom org-completion-use-ido nil
3771 "Non-nil means use ido completion wherever possible.
3772 Note that `ido-mode' must be active for this variable to be relevant.
3773 If you decide to turn this variable on, you might well want to turn off
3774 `org-outline-path-complete-in-steps'.
3775 See also `org-completion-use-iswitchb'."
3776 :group
'org-completion
3779 (defcustom org-completion-use-iswitchb nil
3780 "Non-nil means use iswitchb completion wherever possible.
3781 Note that `iswitchb-mode' must be active for this variable to be relevant.
3782 If you decide to turn this variable on, you might well want to turn off
3783 `org-outline-path-complete-in-steps'.
3784 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3785 :group
'org-completion
3788 (defcustom org-completion-fallback-command
'hippie-expand
3789 "The expansion command called by \\[pcomplete] in normal context.
3790 Normal means, no org-mode-specific context."
3791 :group
'org-completion
3794 ;;; Functions and variables from their packages
3795 ;; Declared here to avoid compiler warnings
3798 (defvar outline-mode-menu-heading
)
3799 (defvar outline-mode-menu-show
)
3800 (defvar outline-mode-menu-hide
)
3801 (defvar zmacs-regions
) ; XEmacs regions
3804 (defvar mark-active
)
3807 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3808 (declare-function calendar-forward-day
"cal-move" (arg))
3809 (declare-function calendar-goto-date
"cal-move" (date))
3810 (declare-function calendar-goto-today
"cal-move" ())
3811 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3812 (defvar calc-embedded-close-formula
)
3813 (defvar calc-embedded-open-formula
)
3814 (declare-function cdlatex-tab
"ext:cdlatex" ())
3815 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3816 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3817 (defvar font-lock-unfontify-region-function
)
3818 (declare-function iswitchb-read-buffer
"iswitchb"
3819 (prompt &optional default require-match start matches-set
))
3820 (defvar iswitchb-temp-buflist
)
3821 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3822 (defvar org-agenda-tags-todo-honor-ignore-options
)
3823 (declare-function org-agenda-skip
"org-agenda" ())
3825 org-agenda-format-item
"org-agenda"
3826 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3827 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3828 (declare-function org-agenda-change-all-lines
"org-agenda"
3829 (newhead hdmarker
&optional fixface just-this
))
3830 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3831 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3832 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3834 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3835 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3836 "org-agenda" (&optional end
))
3837 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3838 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3839 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3840 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3841 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3842 (declare-function parse-time-string
"parse-time" (string))
3843 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3844 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3845 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3846 (defvar remember-data-file
)
3847 (defvar texmathp-why
)
3848 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3849 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3851 (defvar w3m-current-url
)
3852 (defvar w3m-current-title
)
3854 (defvar org-latex-regexps
)
3856 ;;; Autoload and prepare some org modules
3858 ;; Some table stuff that needs to be defined here, because it is used
3859 ;; by the functions setting up org-mode or checking for table context.
3861 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3862 "Detect an org-type or table-type table.")
3863 (defconst org-table-line-regexp
"^[ \t]*|"
3864 "Detect an org-type table line.")
3865 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3866 "Detect an org-type table line.")
3867 (defconst org-table-hline-regexp
"^[ \t]*|-"
3868 "Detect an org-type table hline.")
3869 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3870 "Detect a table-type table hline.")
3871 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3872 "Detect the first line outside a table when searching from within it.
3873 This works for both table types.")
3875 ;; Autoload the functions in org-table.el that are needed by functions here.
3878 (org-autoload "org-table"
3879 '(org-table-align org-table-begin org-table-blank-field
3880 org-table-convert org-table-convert-region org-table-copy-down
3881 org-table-copy-region org-table-create
3882 org-table-create-or-convert-from-region
3883 org-table-create-with-table.el org-table-current-dline
3884 org-table-cut-region org-table-delete-column org-table-edit-field
3885 org-table-edit-formulas org-table-end org-table-eval-formula
3886 org-table-export org-table-field-info
3887 org-table-get-stored-formulas org-table-goto-column
3888 org-table-hline-and-move org-table-import org-table-insert-column
3889 org-table-insert-hline org-table-insert-row org-table-iterate
3890 org-table-justify-field-maybe org-table-kill-row
3891 org-table-maybe-eval-formula org-table-maybe-recalculate-line
3892 org-table-move-column org-table-move-column-left
3893 org-table-move-column-right org-table-move-row
3894 org-table-move-row-down org-table-move-row-up
3895 org-table-next-field org-table-next-row org-table-paste-rectangle
3896 org-table-previous-field org-table-recalculate
3897 org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
3898 org-table-toggle-coordinate-overlays
3899 org-table-toggle-formula-debugger org-table-wrap-region
3900 orgtbl-mode turn-on-orgtbl org-table-to-lisp
3901 orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
3902 orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo
)))
3904 (defun org-at-table-p (&optional table-type
)
3905 "Return t if the cursor is inside an org-type table.
3906 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3907 (if org-enable-table-editor
3909 (beginning-of-line 1)
3910 (looking-at (if table-type org-table-any-line-regexp
3911 org-table-line-regexp
)))
3913 (defsubst org-table-p
() (org-at-table-p))
3915 (defun org-at-table.el-p
()
3916 "Return t if and only if we are at a table.el table."
3917 (and (org-at-table-p 'any
)
3919 (goto-char (org-table-begin 'any
))
3920 (looking-at org-table1-hline-regexp
))))
3921 (defun org-table-recognize-table.el
()
3922 "If there is a table.el table nearby, recognize it and move into it."
3923 (if org-table-tab-recognizes-table.el
3924 (if (org-at-table.el-p
)
3926 (beginning-of-line 1)
3927 (if (looking-at org-table-dataline-regexp
)
3929 (if (looking-at org-table1-hline-regexp
)
3931 (beginning-of-line 2)
3932 (if (looking-at org-table-any-border-regexp
)
3933 (beginning-of-line -
1)))))
3934 (if (re-search-forward "|" (org-table-end t
) t
)
3937 (if (table--at-cell-p (point))
3939 (message "recognizing table.el table...")
3940 (table-recognize-table)
3941 (message "recognizing table.el table...done")))
3942 (error "This should not happen"))
3947 (defun org-at-table-hline-p ()
3948 "Return t if the cursor is inside a hline in a table."
3949 (if org-enable-table-editor
3951 (beginning-of-line 1)
3952 (looking-at org-table-hline-regexp
))
3955 (defvar org-table-clean-did-remove-column nil
)
3957 (defun org-table-map-tables (function &optional quietly
)
3958 "Apply FUNCTION to the start of all tables in the buffer."
3962 (goto-char (point-min))
3963 (while (re-search-forward org-table-any-line-regexp nil t
)
3965 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3966 (beginning-of-line 1)
3967 (when (and (looking-at org-table-line-regexp
)
3968 ;; Exclude tables in src/example/verbatim/clocktable blocks
3969 (not (org-in-block-p '("src" "example"))))
3970 (save-excursion (funcall function
))
3971 (or (looking-at org-table-line-regexp
)
3973 (re-search-forward org-table-any-border-regexp nil
1))))
3974 (unless quietly
(message "Mapping tables: done")))
3976 ;; Declare and autoload functions from org-exp.el & Co
3978 (declare-function org-default-export-plist
"org-exp")
3979 (declare-function org-infile-export-plist
"org-exp")
3980 (declare-function org-get-current-options
"org-exp")
3982 (org-autoload "org-exp"
3983 '(org-export org-export-visible
3984 org-insert-export-options-template
3985 org-table-clean-before-export
))
3986 (org-autoload "org-ascii"
3987 '(org-export-as-ascii org-export-ascii-preprocess
3988 org-export-as-ascii-to-buffer org-replace-region-by-ascii
3989 org-export-region-as-ascii
))
3990 (org-autoload "org-latex"
3991 '(org-export-as-latex-batch org-export-as-latex-to-buffer
3992 org-replace-region-by-latex org-export-region-as-latex
3993 org-export-as-latex org-export-as-pdf
3994 org-export-as-pdf-and-open
))
3995 (org-autoload "org-html"
3996 '(org-export-as-html-and-open
3997 org-export-as-html-batch org-export-as-html-to-buffer
3998 org-replace-region-by-html org-export-region-as-html
3999 org-export-as-html
))
4000 (org-autoload "org-docbook"
4001 '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
4002 org-replace-region-by-docbook org-export-region-as-docbook
4003 org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
4004 org-export-as-docbook
))
4005 (org-autoload "org-icalendar"
4006 '(org-export-icalendar-this-file
4007 org-export-icalendar-all-agenda-files
4008 org-export-icalendar-combine-agenda-files
))
4009 (org-autoload "org-xoxo" '(org-export-as-xoxo))
4010 (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning
)))
4012 ;; Declare and autoload functions from org-agenda.el
4015 (org-autoload "org-agenda"
4016 '(org-agenda org-agenda-list org-search-view
4017 org-todo-list org-tags-view org-agenda-list-stuck-projects
4018 org-diary org-agenda-to-appt
4019 org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
)))
4021 ;; Autoload org-remember
4024 (org-autoload "org-remember"
4025 '(org-remember-insinuate org-remember-annotation
4026 org-remember-apply-template org-remember org-remember-handler
)))
4029 (org-autoload "org-capture"
4030 '(org-capture org-capture-insert-template-here
4031 org-capture-import-remember-templates
)))
4033 ;; Autoload org-clock.el
4035 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock"
4037 (declare-function org-clock-update-mode-line
"org-clock" ())
4038 (declare-function org-resolve-clocks
"org-clock"
4039 (&optional also-non-dangling-p prompt last-valid
))
4040 (defvar org-clock-start-time
)
4041 (defvar org-clock-marker
(make-marker)
4042 "Marker recording the last clock-in.")
4043 (defvar org-clock-hd-marker
(make-marker)
4044 "Marker recording the last clock-in, but the headline position.")
4045 (defvar org-clock-heading
""
4046 "The heading of the current clock entry.")
4047 (defun org-clock-is-active ()
4048 "Return non-nil if clock is currently running.
4049 The return value is actually the clock marker."
4050 (marker-buffer org-clock-marker
))
4055 '(org-clock-in org-clock-out org-clock-cancel
4056 org-clock-goto org-clock-sum org-clock-display
4057 org-clock-remove-overlays org-clock-report
4058 org-clocktable-shift org-dblock-write
:clocktable
4059 org-get-clocktable org-resolve-clocks
)))
4061 (defun org-clock-update-time-maybe ()
4062 "If this is a CLOCK line, update it and return t.
4063 Otherwise, return nil."
4066 (beginning-of-line 1)
4067 (skip-chars-forward " \t")
4068 (when (looking-at org-clock-string
)
4069 (let ((re (concat "[ \t]*" org-clock-string
4070 " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
4071 "\\([ \t]*=>.*\\)?\\)?"))
4074 ((not (looking-at re
))
4076 ((not (match-end 2))
4077 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4078 (> org-clock-marker
(point))
4079 (<= org-clock-marker
(point-at-eol)))
4080 ;; The clock is running here
4081 (setq org-clock-start-time
4083 (org-parse-time-string (match-string 1))))
4084 (org-clock-update-mode-line)))
4086 (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
4088 (setq ts
(match-string 1)
4089 te
(match-string 3))
4090 (setq s
(- (org-float-time
4091 (apply 'encode-time
(org-parse-time-string te
)))
4093 (apply 'encode-time
(org-parse-time-string ts
))))
4096 h
(floor (/ s
3600))
4100 (insert " => " (format (if neg
"-%d:%02d" "%2d:%02d") h m
))
4103 (defun org-check-running-clock ()
4104 "Check if the current buffer contains the running clock.
4105 If yes, offer to stop it and to save the buffer with the changes."
4106 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
4107 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
4110 (when (y-or-n-p "Save changed buffer?")
4113 (defun org-clocktable-try-shift (dir n
)
4114 "Check if this line starts a clock table, if yes, shift the time block."
4115 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
4116 (org-clocktable-shift dir n
)))
4118 ;; Autoload org-timer.el
4123 '(org-timer-start org-timer org-timer-item
4124 org-timer-change-times-in-region
4126 org-timer-reset-timers
4127 org-timer-show-remaining-time
)))
4129 ;; Autoload org-feed.el
4134 '(org-feed-update org-feed-update-all org-feed-goto-inbox
)))
4137 ;; Autoload org-indent.el
4139 ;; Define the variable already here, to make sure we have it.
4140 (defvar org-indent-mode nil
4141 "Non-nil if Org-Indent mode is enabled.
4142 Use the command `org-indent-mode' to change this variable.")
4147 '(org-indent-mode)))
4149 ;; Autoload org-mobile.el
4154 '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda
)))
4156 ;; Autoload archiving code
4157 ;; The stuff that is needed for cycling and tags has to be defined here.
4159 (defgroup org-archive nil
4160 "Options concerning archiving in Org-mode."
4162 :group
'org-structure
)
4164 (defcustom org-archive-location
"%s_archive::"
4165 "The location where subtrees should be archived.
4167 The value of this variable is a string, consisting of two parts,
4168 separated by a double-colon. The first part is a filename and
4169 the second part is a headline.
4171 When the filename is omitted, archiving happens in the same file.
4172 %s in the filename will be replaced by the current file
4173 name (without the directory part). Archiving to a different file
4174 is useful to keep archived entries from contributing to the
4177 The archived entries will be filed as subtrees of the specified
4178 headline. When the headline is omitted, the subtrees are simply
4179 filed away at the end of the file, as top-level entries. Also in
4180 the heading you can use %s to represent the file name, this can be
4181 useful when using the same archive for a number of different files.
4183 Here are a few examples:
4185 If the current file is Projects.org, archive in file
4186 Projects.org_archive, as top-level trees. This is the default.
4188 \"::* Archived Tasks\"
4189 Archive in the current file, under the top-level headline
4190 \"* Archived Tasks\".
4192 \"~/org/archive.org::\"
4193 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4195 \"~/org/archive.org::* From %s\"
4196 Archive in file ~/org/archive.org (absolute path), under headlines
4197 \"From FILENAME\" where file name is the current file name.
4199 \"~/org/datetree.org::datetree/* Finished Tasks\"
4200 The \"datetree/\" string is special, signifying to archive
4201 items to the datetree. Items are placed in either the CLOSED
4202 date of the item, or the current date if there is no CLOSED date.
4203 The heading will be a subentry to the current date. There doesn't
4204 need to be a heading, but there always needs to be a slash after
4205 datetree. For example, to store archived items directly in the
4206 datetree, use \"~/org/datetree.org::datetree/\".
4208 \"basement::** Finished Tasks\"
4209 Archive in file ./basement (relative path), as level 3 trees
4210 below the level 2 heading \"** Finished Tasks\".
4212 You may set this option on a per-file basis by adding to the buffer a
4215 #+ARCHIVE: basement::** Finished Tasks
4217 You may also define it locally for a subtree by setting an ARCHIVE property
4218 in the entry. If such a property is found in an entry, or anywhere up
4219 the hierarchy, it will be used."
4223 (defcustom org-archive-tag
"ARCHIVE"
4224 "The tag that marks a subtree as archived.
4225 An archived subtree does not open during visibility cycling, and does
4226 not contribute to the agenda listings.
4227 After changing this, font-lock must be restarted in the relevant buffers to
4228 get the proper fontification."
4230 :group
'org-keywords
4233 (defcustom org-agenda-skip-archived-trees t
4234 "Non-nil means the agenda will skip any items located in archived trees.
4235 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4236 variable is no longer recommended, you should leave it at the value t.
4237 Instead, use the key `v' to cycle the archives-mode in the agenda."
4239 :group
'org-agenda-skip
4242 (defcustom org-columns-skip-archived-trees t
4243 "Non-nil means ignore archived trees when creating column view."
4245 :group
'org-properties
4248 (defcustom org-cycle-open-archived-trees nil
4249 "Non-nil means `org-cycle' will open archived trees.
4250 An archived tree is a tree marked with the tag ARCHIVE.
4251 When nil, archived trees will stay folded. You can still open them with
4252 normal outline commands like `show-all', but not with the cycling commands."
4257 (defcustom org-sparse-tree-open-archived-trees nil
4258 "Non-nil means sparse tree construction shows matches in archived trees.
4259 When nil, matches in these trees are highlighted, but the trees are kept in
4262 :group
'org-sparse-trees
4265 (defun org-cycle-hide-archived-subtrees (state)
4266 "Re-hide all archived subtrees after a visibility state change."
4267 (when (and (not org-cycle-open-archived-trees
)
4268 (not (memq state
'(overview folded
))))
4270 (let* ((globalp (memq state
'(contents all
)))
4271 (beg (if globalp
(point-min) (point)))
4272 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4273 (org-hide-archived-subtrees beg end
)
4275 (if (looking-at (concat ".*:" org-archive-tag
":"))
4276 (message "%s" (substitute-command-keys
4277 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4279 (defun org-force-cycle-archived ()
4280 "Cycle subtree even if it is archived."
4282 (setq this-command
'org-cycle
)
4283 (let ((org-cycle-open-archived-trees t
))
4284 (call-interactively 'org-cycle
)))
4286 (defun org-hide-archived-subtrees (beg end
)
4287 "Re-hide all archived subtrees after a visibility state change."
4289 (let* ((re (concat ":" org-archive-tag
":")))
4291 (while (re-search-forward re end t
)
4292 (when (org-at-heading-p)
4293 (org-flag-subtree t
)
4294 (org-end-of-subtree t
))))))
4296 (defun org-flag-subtree (flag)
4298 (org-back-to-heading t
)
4299 (outline-end-of-heading)
4300 (outline-flag-region (point)
4301 (progn (org-end-of-subtree t
) (point))
4304 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4307 (org-autoload "org-archive"
4308 '(org-add-archive-files org-archive-subtree
4309 org-archive-to-archive-sibling org-toggle-archive-tag
4310 org-archive-subtree-default
4311 org-archive-subtree-default-with-confirmation
)))
4313 ;; Autoload Column View Code
4315 (declare-function org-columns-number-to-string
"org-colview")
4316 (declare-function org-columns-get-format-and-top-level
"org-colview")
4317 (declare-function org-columns-compute
"org-colview")
4319 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4320 '(org-columns-number-to-string org-columns-get-format-and-top-level
4321 org-columns-compute org-agenda-columns org-columns-remove-overlays
4322 org-columns org-insert-columns-dblock org-dblock-write
:columnview
))
4326 (declare-function org-id-store-link
"org-id")
4327 (declare-function org-id-locations-load
"org-id")
4328 (declare-function org-id-locations-save
"org-id")
4329 (defvar org-id-track-globally
)
4330 (org-autoload "org-id"
4331 '(org-id-get-create org-id-new org-id-copy org-id-get
4332 org-id-get-with-outline-path-completion
4333 org-id-get-with-outline-drilling org-id-store-link
4334 org-id-goto org-id-find org-id-store-link
))
4336 ;; Autoload Plotting Code
4338 (org-autoload "org-plot"
4339 '(org-plot/gnuplot
))
4341 ;;; Variables for pre-computed regular expressions, all buffer local
4343 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4344 "Matches first line of a hidden block.")
4345 (make-variable-buffer-local 'org-drawer-regexp
)
4346 (defvar org-todo-regexp nil
4347 "Matches any of the TODO state keywords.")
4348 (make-variable-buffer-local 'org-todo-regexp
)
4349 (defvar org-not-done-regexp nil
4350 "Matches any of the TODO state keywords except the last one.")
4351 (make-variable-buffer-local 'org-not-done-regexp
)
4352 (defvar org-not-done-heading-regexp nil
4353 "Matches a TODO headline that is not done.")
4354 (make-variable-buffer-local 'org-not-done-regexp
)
4355 (defvar org-todo-line-regexp nil
4356 "Matches a headline and puts TODO state into group 2 if present.")
4357 (make-variable-buffer-local 'org-todo-line-regexp
)
4358 (defvar org-complex-heading-regexp nil
4359 "Matches a headline and puts everything into groups:
4361 group 2: The todo keyword, maybe
4362 group 3: Priority cookie
4363 group 4: True headline
4365 (make-variable-buffer-local 'org-complex-heading-regexp
)
4366 (defvar org-complex-heading-regexp-format nil
4367 "Printf format to make regexp to match an exact headline.
4368 This regexp will match the headline of any node which has the
4369 exact headline text that is put into the format, but may have any
4370 TODO state, priority and tags.")
4371 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4372 (defvar org-todo-line-tags-regexp nil
4373 "Matches a headline and puts TODO state into group 2 if present.
4374 Also put tags into group 4 if tags are present.")
4375 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4376 (defvar org-ds-keyword-length
12
4377 "Maximum length of the Deadline and SCHEDULED keywords.")
4378 (make-variable-buffer-local 'org-ds-keyword-length
)
4379 (defvar org-deadline-regexp nil
4380 "Matches the DEADLINE keyword.")
4381 (make-variable-buffer-local 'org-deadline-regexp
)
4382 (defvar org-deadline-time-regexp nil
4383 "Matches the DEADLINE keyword together with a time stamp.")
4384 (make-variable-buffer-local 'org-deadline-time-regexp
)
4385 (defvar org-deadline-line-regexp nil
4386 "Matches the DEADLINE keyword and the rest of the line.")
4387 (make-variable-buffer-local 'org-deadline-line-regexp
)
4388 (defvar org-scheduled-regexp nil
4389 "Matches the SCHEDULED keyword.")
4390 (make-variable-buffer-local 'org-scheduled-regexp
)
4391 (defvar org-scheduled-time-regexp nil
4392 "Matches the SCHEDULED keyword together with a time stamp.")
4393 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4394 (defvar org-closed-time-regexp nil
4395 "Matches the CLOSED keyword together with a time stamp.")
4396 (make-variable-buffer-local 'org-closed-time-regexp
)
4398 (defvar org-keyword-time-regexp nil
4399 "Matches any of the 4 keywords, together with the time stamp.")
4400 (make-variable-buffer-local 'org-keyword-time-regexp
)
4401 (defvar org-keyword-time-not-clock-regexp nil
4402 "Matches any of the 3 keywords, together with the time stamp.")
4403 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4404 (defvar org-maybe-keyword-time-regexp nil
4405 "Matches a timestamp, possibly preceded by a keyword.")
4406 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4407 (defvar org-all-time-keywords nil
4408 "List of time keywords.")
4409 (make-variable-buffer-local 'org-all-time-keywords
)
4411 (defconst org-plain-time-of-day-regexp
4414 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4417 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4419 "Regular expression to match a plain time or time range.
4420 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4421 groups carry important information:
4423 1 the first time, range or not
4424 8 the second time, if it is a range.")
4426 (defconst org-plain-time-extension-regexp
4429 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4430 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4431 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4432 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4433 groups carry important information:
4436 9 minutes of duration")
4438 (defconst org-stamp-time-of-day-regexp
4440 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4441 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4443 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4444 "Regular expression to match a timestamp time or time range.
4445 After a match, the following groups carry important information:
4447 1 date plus weekday, for back referencing to make sure both times are on the same day
4448 2 the first time, range or not
4449 4 the second time, if it is a range.")
4451 (defconst org-startup-options
4452 '(("fold" org-startup-folded t
)
4453 ("overview" org-startup-folded t
)
4454 ("nofold" org-startup-folded nil
)
4455 ("showall" org-startup-folded nil
)
4456 ("showeverything" org-startup-folded showeverything
)
4457 ("content" org-startup-folded content
)
4458 ("indent" org-startup-indented t
)
4459 ("noindent" org-startup-indented nil
)
4460 ("hidestars" org-hide-leading-stars t
)
4461 ("showstars" org-hide-leading-stars nil
)
4462 ("odd" org-odd-levels-only t
)
4463 ("oddeven" org-odd-levels-only nil
)
4464 ("align" org-startup-align-all-tables t
)
4465 ("noalign" org-startup-align-all-tables nil
)
4466 ("inlineimages" org-startup-with-inline-images t
)
4467 ("noinlineimages" org-startup-with-inline-images nil
)
4468 ("customtime" org-display-custom-times t
)
4469 ("logdone" org-log-done time
)
4470 ("lognotedone" org-log-done note
)
4471 ("nologdone" org-log-done nil
)
4472 ("lognoteclock-out" org-log-note-clock-out t
)
4473 ("nolognoteclock-out" org-log-note-clock-out nil
)
4474 ("logrepeat" org-log-repeat state
)
4475 ("lognoterepeat" org-log-repeat note
)
4476 ("nologrepeat" org-log-repeat nil
)
4477 ("logreschedule" org-log-reschedule time
)
4478 ("lognotereschedule" org-log-reschedule note
)
4479 ("nologreschedule" org-log-reschedule nil
)
4480 ("logredeadline" org-log-redeadline time
)
4481 ("lognoteredeadline" org-log-redeadline note
)
4482 ("nologredeadline" org-log-redeadline nil
)
4483 ("logrefile" org-log-refile time
)
4484 ("lognoterefile" org-log-refile note
)
4485 ("nologrefile" org-log-refile nil
)
4486 ("fninline" org-footnote-define-inline t
)
4487 ("nofninline" org-footnote-define-inline nil
)
4488 ("fnlocal" org-footnote-section nil
)
4489 ("fnauto" org-footnote-auto-label t
)
4490 ("fnprompt" org-footnote-auto-label nil
)
4491 ("fnconfirm" org-footnote-auto-label confirm
)
4492 ("fnplain" org-footnote-auto-label plain
)
4493 ("fnadjust" org-footnote-auto-adjust t
)
4494 ("nofnadjust" org-footnote-auto-adjust nil
)
4495 ("constcgs" constants-unit-system cgs
)
4496 ("constSI" constants-unit-system SI
)
4497 ("noptag" org-tag-persistent-alist nil
)
4498 ("hideblocks" org-hide-block-startup t
)
4499 ("nohideblocks" org-hide-block-startup nil
)
4500 ("beamer" org-startup-with-beamer-mode t
)
4501 ("entitiespretty" org-pretty-entities t
)
4502 ("entitiesplain" org-pretty-entities nil
))
4503 "Variable associated with STARTUP options for org-mode.
4504 Each element is a list of three items: the startup options (as written
4505 in the #+STARTUP line), the corresponding variable, and the value to set
4506 this variable to if the option is found. An optional forth element PUSH
4507 means to push this value onto the list in the variable.")
4509 (defun org-update-property-plist (key val props
)
4510 "Update PROPS with KEY and VAL."
4511 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4512 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4513 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4514 (previous (cdr (assoc key props
))))
4516 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4517 (cons (cons key val
) remainder
))))
4519 (defconst org-block-regexp
4520 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4521 "Regular expression for hiding blocks.")
4522 (defconst org-heading-keyword-regexp-format
4523 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4524 "Printf format for a regexp matching an headline with some keyword.
4525 This regexp will match the headline of any node which has the
4526 exact keyword that is put into the format. The keyword isn't in
4527 any group by default, but the stars and the body are.")
4528 (defconst org-heading-keyword-maybe-regexp-format
4529 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4530 "Printf format for a regexp matching an headline, possibly with some keyword.
4531 This regexp can match any headline with the specified keyword, or
4532 without a keyword. The keyword isn't in any group by default,
4533 but the stars and the body are.")
4535 (defun org-set-regexps-and-options ()
4536 "Precompute regular expressions for current buffer."
4537 (when (derived-mode-p 'org-mode
)
4538 (org-set-local 'org-todo-kwd-alist nil
)
4539 (org-set-local 'org-todo-key-alist nil
)
4540 (org-set-local 'org-todo-key-trigger nil
)
4541 (org-set-local 'org-todo-keywords-1 nil
)
4542 (org-set-local 'org-done-keywords nil
)
4543 (org-set-local 'org-todo-heads nil
)
4544 (org-set-local 'org-todo-sets nil
)
4545 (org-set-local 'org-todo-log-states nil
)
4546 (org-set-local 'org-file-properties nil
)
4547 (org-set-local 'org-file-tags nil
)
4548 (let ((re (org-make-options-regexp
4549 '("CATEGORY" "TODO" "COLUMNS"
4550 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4551 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4553 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4555 (scripts org-use-sub-superscripts
)
4556 kwds kws0 kwsa key log value cat arch tags const links hw dws
4557 tail sep kws1 prio props ftags drawers beamer-p
4558 ext-setup-or-nil setup-contents
(start 0))
4562 (goto-char (point-min))
4563 (while (or (and ext-setup-or-nil
4564 (string-match re ext-setup-or-nil start
)
4565 (setq start
(match-end 0)))
4566 (and (setq ext-setup-or-nil nil start
0)
4567 (re-search-forward re nil t
)))
4568 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4569 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4570 (if (stringp value
) (setq value
(org-trim value
)))
4572 ((equal key
"CATEGORY")
4574 ((member key
'("SEQ_TODO" "TODO"))
4575 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4576 ((equal key
"TYP_TODO")
4577 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4578 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4579 ;; general TODO-like setup
4580 (push (cons (intern (downcase (match-string 1 key
)))
4581 (org-split-string value splitre
)) kwds
))
4583 (setq tags
(append tags
(if tags
'("\\n") nil
)
4584 (org-split-string value splitre
))))
4585 ((equal key
"COLUMNS")
4586 (org-set-local 'org-columns-default-format value
))
4588 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4589 (push (cons (match-string 1 value
)
4590 (org-trim (match-string 2 value
)))
4592 ((equal key
"PRIORITIES")
4593 (setq prio
(org-split-string value
" +")))
4594 ((equal key
"PROPERTY")
4595 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4596 (setq props
(org-update-property-plist (match-string 1 value
)
4597 (match-string 2 value
)
4599 ((equal key
"FILETAGS")
4600 (when (string-match "\\S-" value
)
4605 (mapcar (lambda (x) (org-split-string x
":"))
4606 (org-split-string value
)))))))
4607 ((equal key
"DRAWERS")
4608 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4609 ((equal key
"CONSTANTS")
4610 (setq const
(append const
(org-split-string value splitre
))))
4611 ((equal key
"STARTUP")
4612 (let ((opts (org-split-string value splitre
))
4614 (while (setq l
(pop opts
))
4615 (when (setq l
(assoc l org-startup-options
))
4616 (setq var
(nth 1 l
) val
(nth 2 l
))
4618 (set (make-local-variable var
) val
)
4619 (if (not (listp (symbol-value var
)))
4620 (set (make-local-variable var
) nil
))
4621 (set (make-local-variable var
) (symbol-value var
))
4622 (add-to-list var val
))))))
4623 ((equal key
"ARCHIVE")
4625 (remove-text-properties 0 (length arch
)
4626 '(face t fontified t
) arch
))
4627 ((equal key
"LATEX_CLASS")
4628 (setq beamer-p
(equal value
"beamer")))
4629 ((equal key
"OPTIONS")
4630 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4631 (setq scripts
(read (match-string 2 value
)))))
4632 ((equal key
"SETUPFILE")
4633 (setq setup-contents
(org-file-contents
4635 (org-remove-double-quotes value
))
4637 (if (not ext-setup-or-nil
)
4638 (setq ext-setup-or-nil setup-contents start
0)
4639 (setq ext-setup-or-nil
4640 (concat (substring ext-setup-or-nil
0 start
)
4641 "\n" setup-contents
"\n"
4642 (substring ext-setup-or-nil start
)))))))
4643 ;; search for property blocks
4644 (goto-char (point-min))
4645 (while (re-search-forward org-block-regexp nil t
)
4646 (when (equal "PROPERTY" (upcase (match-string 1)))
4647 (setq value
(replace-regexp-in-string
4648 "[\n\r]" " " (match-string 4)))
4649 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4650 (setq props
(org-update-property-plist (match-string 1 value
)
4651 (match-string 2 value
)
4653 (org-set-local 'org-use-sub-superscripts scripts
)
4655 (org-set-local 'org-category
(intern cat
))
4656 (push (cons "CATEGORY" cat
) props
))
4658 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4659 (setq prio
(mapcar 'string-to-char prio
))
4660 (org-set-local 'org-highest-priority
(nth 0 prio
))
4661 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4662 (org-set-local 'org-default-priority
(nth 2 prio
)))
4663 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4664 (and ftags
(org-set-local 'org-file-tags
4665 (mapcar 'org-add-prop-inherited ftags
)))
4666 (and drawers
(org-set-local 'org-drawers drawers
))
4667 (and arch
(org-set-local 'org-archive-location arch
))
4668 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4669 ;; Process the TODO keywords
4671 ;; Use the global values as if they had been given locally.
4672 (setq kwds
(default-value 'org-todo-keywords
))
4673 (if (stringp (car kwds
))
4674 (setq kwds
(list (cons org-todo-interpretation
4675 (default-value 'org-todo-keywords
)))))
4676 (setq kwds
(reverse kwds
)))
4677 (setq kwds
(nreverse kwds
))
4679 (while (setq kws
(pop kwds
))
4681 (run-hook-with-args-until-success
4682 'org-todo-setup-filter-hook kws
)
4684 (setq inter
(pop kws
) sep
(member "|" kws
)
4685 kws0
(delete "|" (copy-sequence kws
))
4690 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4692 (setq kw
(match-string 1 x
)
4693 key
(and (match-end 2) (match-string 2 x
))
4694 log
(org-extract-log-state-settings x
))
4695 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4696 (and log
(push log org-todo-log-states
))
4698 (error "Invalid TODO keyword %s" x
)))
4700 kwsa
(if kwsa
(append '((:startgroup
))
4704 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4705 tail
(list inter hw
(car dws
) (org-last dws
))))
4706 (add-to-list 'org-todo-heads hw
'append
)
4707 (push kws1 org-todo-sets
)
4708 (setq org-done-keywords
(append org-done-keywords dws nil
))
4709 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4710 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4711 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4712 (setq org-todo-sets
(nreverse org-todo-sets
)
4713 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4714 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4715 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4716 ;; Process the constants
4719 (while (setq e
(pop const
))
4720 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4721 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4722 (setq org-table-formula-constants-local cst
)))
4724 ;; Process the tags.
4727 (while (setq e
(pop tags
))
4729 ((equal e
"{") (push '(:startgroup
) tgs
))
4730 ((equal e
"}") (push '(:endgroup
) tgs
))
4731 ((equal e
"\\n") (push '(:newline
) tgs
))
4732 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4733 (push (cons (match-string 1 e
)
4734 (string-to-char (match-string 2 e
)))
4736 (t (push (list e
) tgs
))))
4737 (org-set-local 'org-tag-alist nil
)
4738 (while (setq e
(pop tgs
))
4739 (or (and (stringp (car e
))
4740 (assoc (car e
) org-tag-alist
))
4741 (push e org-tag-alist
)))))
4743 ;; Compute the regular expressions and other local variables.
4744 ;; Using `org-outline-regexp-bol' would complicate them much,
4745 ;; because of the fixed white space at the end of that string.
4746 (if (not org-done-keywords
)
4747 (setq org-done-keywords
(and org-todo-keywords-1
4748 (list (org-last org-todo-keywords-1
)))))
4749 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4750 (length org-scheduled-string
)
4751 (length org-clock-string
)
4752 (length org-closed-string
)))
4754 (concat "^[ \t]*:\\("
4755 (mapconcat 'regexp-quote org-drawers
"\\|")
4757 org-not-done-keywords
4758 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4761 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4765 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4767 org-not-done-heading-regexp
4768 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4769 org-todo-line-regexp
4770 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4771 org-complex-heading-regexp
4772 (concat "^\\(\\*+\\)"
4773 "\\(?: +" org-todo-regexp
"\\)?"
4774 "\\(?: +\\(\\[#.\\]\\)\\)?"
4775 "\\(?: +\\(.*?\\)\\)?"
4776 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4778 org-complex-heading-regexp-format
4779 (concat "^\\(\\*+\\)"
4780 "\\(?: +" org-todo-regexp
"\\)?"
4781 "\\(?: +\\(\\[#.\\]\\)\\)?"
4783 ;; Stats cookies can be stuck to body.
4784 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4786 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4788 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4790 org-todo-line-tags-regexp
4791 (concat "^\\(\\*+\\)"
4792 "\\(?: +" org-todo-regexp
"\\)?"
4793 "\\(?: +\\(.*?\\)\\)?"
4794 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4796 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4797 org-deadline-time-regexp
4798 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4799 org-deadline-line-regexp
4800 (concat "\\<\\(" org-deadline-string
"\\).*")
4801 org-scheduled-regexp
4802 (concat "\\<" org-scheduled-string
)
4803 org-scheduled-time-regexp
4804 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4805 org-closed-time-regexp
4806 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4807 org-keyword-time-regexp
4808 (concat "\\<\\(" org-scheduled-string
4809 "\\|" org-deadline-string
4810 "\\|" org-closed-string
4811 "\\|" org-clock-string
"\\)"
4812 " *[[<]\\([^]>]+\\)[]>]")
4813 org-keyword-time-not-clock-regexp
4814 (concat "\\<\\(" org-scheduled-string
4815 "\\|" org-deadline-string
4816 "\\|" org-closed-string
4818 " *[[<]\\([^]>]+\\)[]>]")
4819 org-maybe-keyword-time-regexp
4820 (concat "\\(\\<\\(" org-scheduled-string
4821 "\\|" org-deadline-string
4822 "\\|" org-closed-string
4823 "\\|" org-clock-string
"\\)\\)?"
4824 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4825 org-all-time-keywords
4826 (mapcar (lambda (w) (substring w
0 -
1))
4827 (list org-scheduled-string org-deadline-string
4828 org-clock-string org-closed-string
))
4830 (org-compute-latex-and-specials-regexp)
4831 (org-set-font-lock-defaults))))
4833 (defun org-file-contents (file &optional noerror
)
4834 "Return the contents of FILE, as a string."
4836 (not (file-readable-p file
)))
4839 (message "Cannot read file \"%s\"" file
)
4842 (error "Cannot read file \"%s\"" file
))
4844 (insert-file-contents file
)
4847 (defun org-extract-log-state-settings (x)
4848 "Extract the log state setting from a TODO keyword string.
4849 This will extract info from a string like \"WAIT(w@/!)\"."
4850 (let (kw key log1 log2
)
4851 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4852 (setq kw
(match-string 1 x
)
4853 key
(and (match-end 2) (match-string 2 x
))
4854 log1
(and (match-end 3) (match-string 3 x
))
4855 log2
(and (match-end 4) (match-string 4 x
)))
4858 (and log1
(if (equal log1
"!") 'time
'note
))
4859 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4861 (defun org-remove-keyword-keys (list)
4862 "Remove a pair of parenthesis at the end of each string in LIST."
4864 (if (string-match "(.*)$" x
)
4865 (substring x
0 (match-beginning 0))
4869 (defun org-assign-fast-keys (alist)
4870 "Assign fast keys to a keyword-key alist.
4871 Respect keys that are already there."
4872 (let (new e
(alt ?
0))
4873 (while (setq e
(pop alist
))
4874 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4875 (cdr e
)) ;; Key already assigned.
4877 (let ((clist (string-to-list (downcase (car e
))))
4878 (used (append new alist
)))
4879 (when (= (car clist
) ?
@)
4881 (while (and clist
(rassoc (car clist
) used
))
4884 (while (rassoc alt used
)
4886 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4889 ;;; Some variables used in various places
4891 (defvar org-window-configuration nil
4892 "Used in various places to store a window configuration.")
4893 (defvar org-selected-window nil
4894 "Used in various places to store a window configuration.")
4895 (defvar org-finish-function nil
4896 "Function to be called when `C-c C-c' is used.
4897 This is for getting out of special buffers like remember.")
4900 ;; FIXME: Occasionally check by commenting these, to make sure
4901 ;; no other functions uses these, forgetting to let-bind them.
4902 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
4903 (defvar org-last-state
)
4904 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
4906 ;; Defined somewhere in this file, but used before definition.
4907 (defvar org-entities
) ;; defined in org-entities.el
4908 (defvar org-struct-menu
)
4909 (defvar org-org-menu
)
4910 (defvar org-tbl-menu
)
4912 ;;;; Define the Org-mode
4914 ;; We use a before-change function to check if a table might need
4916 (defvar org-table-may-need-update t
4917 "Indicates that a table might need an update.
4918 This variable is set by `org-before-change-function'.
4919 `org-table-align' sets it back to nil.")
4920 (defun org-before-change-function (beg end
)
4921 "Every change indicates that a table might need an update."
4922 (setq org-table-may-need-update t
))
4923 (defvar org-mode-map
)
4924 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4925 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4926 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4927 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4928 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4929 (defvar org-table-buffer-is-an nil
)
4931 (defvar bidi-paragraph-direction
)
4932 (defvar buffer-face-mode-face
)
4935 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4936 (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"))
4937 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
4939 ;; Other stuff we need.
4940 (require 'time-date
)
4941 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
4946 (require 'org-entities
)
4947 (require 'org-compat
)
4948 (require 'org-faces
)
4950 (require 'org-pcomplete
)
4952 (require 'org-footnote
)
4953 (require 'org-element
)
4960 (require 'ob-tangle
)
4961 (require 'ob-comint
)
4965 (define-derived-mode org-mode outline-mode
"Org"
4966 "Outline-based notes management and organizer, alias
4967 \"Carsten's outline-mode for keeping track of everything.\"
4969 Org-mode develops organizational tasks around a NOTES file which
4970 contains information about projects as plain text. Org-mode is
4971 implemented on top of outline-mode, which is ideal to keep the content
4972 of large files well structured. It supports ToDo items, deadlines and
4973 time stamps, which magically appear in the diary listing of the Emacs
4974 calendar. Tables are easily created with a built-in table editor.
4975 Plain text URL-like links connect to websites, emails (VM), Usenet
4976 messages (Gnus), BBDB entries, and any files related to the project.
4977 For printing and sharing of notes, an Org-mode file (or a part of it)
4978 can be exported as a structured ASCII or HTML file.
4980 The following commands are available:
4984 ;; Get rid of Outline menus, they are not needed
4985 ;; Need to do this here because define-derived-mode sets up
4986 ;; the keymap so late. Still, it is a waste to call this each time
4987 ;; we switch another buffer into org-mode.
4988 (if (featurep 'xemacs
)
4989 (when (boundp 'outline-mode-menu-heading
)
4990 ;; Assume this is Greg's port, it uses easymenu
4991 (easy-menu-remove outline-mode-menu-heading
)
4992 (easy-menu-remove outline-mode-menu-show
)
4993 (easy-menu-remove outline-mode-menu-hide
))
4994 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4995 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4996 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4998 (org-load-modules-maybe)
4999 (easy-menu-add org-org-menu
)
5000 (easy-menu-add org-tbl-menu
)
5001 (org-install-agenda-files-menu)
5002 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
5003 (add-to-invisibility-spec '(org-cwidth))
5004 (add-to-invisibility-spec '(org-hide-block . t
))
5005 (when (featurep 'xemacs
)
5006 (org-set-local 'line-move-ignore-invisible t
))
5007 (org-set-local 'outline-regexp org-outline-regexp
)
5008 (org-set-local 'outline-level
'org-outline-level
)
5009 (setq bidi-paragraph-direction
'left-to-right
)
5010 (when (and org-ellipsis
5011 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
5012 (fboundp 'make-glyph-code
))
5013 (unless org-display-table
5014 (setq org-display-table
(make-display-table)))
5015 (set-display-table-slot
5018 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
5020 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
5021 (setq buffer-display-table org-display-table
))
5022 (org-set-regexps-and-options)
5023 (when (and org-tag-faces
(not org-tags-special-faces-re
))
5024 ;; tag faces set outside customize.... force initialization.
5025 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
5027 (org-set-local 'calc-embedded-open-mode
"# ")
5028 (modify-syntax-entry ?
@ "w")
5029 (if org-startup-truncated
(setq truncate-lines t
))
5030 (org-set-local 'font-lock-unfontify-region-function
5031 'org-unfontify-region
)
5032 ;; Activate before-change-function
5033 (org-set-local 'org-table-may-need-update t
)
5034 (org-add-hook 'before-change-functions
'org-before-change-function nil
5036 ;; Check for running clock before killing a buffer
5037 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
5038 ;; Paragraphs and auto-filling
5039 (org-set-autofill-regexps)
5040 (org-set-local 'indent-line-function
'org-indent-line
)
5041 (org-set-local 'indent-region-function
'org-indent-region
)
5042 (org-update-radio-target-regexp)
5043 ;; Beginning/end of defun
5044 (org-set-local 'beginning-of-defun-function
'org-beginning-of-defun
)
5045 (org-set-local 'end-of-defun-function
'org-end-of-defun
)
5046 ;; Next error for sparse trees
5047 (org-set-local 'next-error-function
'org-occur-next-match
)
5048 ;; Make sure dependence stuff works reliably, even for users who set it
5050 (if org-enforce-todo-dependencies
5051 (add-hook 'org-blocker-hook
5052 'org-block-todo-from-children-or-siblings-or-parent
)
5053 (remove-hook 'org-blocker-hook
5054 'org-block-todo-from-children-or-siblings-or-parent
))
5055 (if org-enforce-todo-checkbox-dependencies
5056 (add-hook 'org-blocker-hook
5057 'org-block-todo-from-checkboxes
)
5058 (remove-hook 'org-blocker-hook
5059 'org-block-todo-from-checkboxes
))
5061 ;; Align options lines
5063 'align-mode-rules-list
5064 '((org-in-buffer-settings
5065 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
5066 (modes .
'(org-mode)))))
5069 (org-set-local 'imenu-create-index-function
5070 'org-imenu-get-tree
)
5072 ;; Make isearch reveal context
5073 (if (or (featurep 'xemacs
)
5074 (not (boundp 'outline-isearch-open-invisible-function
)))
5075 ;; Emacs 21 and XEmacs make use of the hook
5076 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
5077 ;; Emacs 22 deals with this through a special variable
5078 (org-set-local 'outline-isearch-open-invisible-function
5079 (lambda (&rest ignore
) (org-show-context 'isearch
))))
5081 ;; Turn on org-beamer-mode?
5082 (and org-startup-with-beamer-mode
(org-beamer-mode 1))
5084 ;; Setup the pcomplete hooks
5085 (set (make-local-variable 'pcomplete-command-completion-function
)
5086 'org-pcomplete-initial
)
5087 (set (make-local-variable 'pcomplete-command-name-function
)
5088 'org-command-at-point
)
5089 (set (make-local-variable 'pcomplete-default-completion-function
)
5091 (set (make-local-variable 'pcomplete-parse-arguments-function
)
5092 'org-parse-arguments
)
5093 (set (make-local-variable 'pcomplete-termination-string
) "")
5094 (when (>= emacs-major-version
23)
5095 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
5097 ;; If empty file that did not turn on org-mode automatically, make it to.
5098 (if (and org-insert-mode-line-in-empty-file
5099 (org-called-interactively-p 'any
)
5100 (= (point-min) (point-max)))
5101 (insert "# -*- mode: org -*-\n\n"))
5102 (unless org-inhibit-startup
5103 (when org-startup-align-all-tables
5104 (let ((bmp (buffer-modified-p)))
5105 (org-table-map-tables 'org-table-align
'quietly
)
5106 (set-buffer-modified-p bmp
)))
5107 (when org-startup-with-inline-images
5108 (org-display-inline-images))
5109 (when org-startup-indented
5110 (require 'org-indent
)
5111 (org-indent-mode 1))
5112 (unless org-inhibit-startup-visibility-stuff
5113 (org-set-startup-visibility))))
5115 (when (fboundp 'abbrev-table-put
)
5116 (abbrev-table-put org-mode-abbrev-table
5117 :parents
(list text-mode-abbrev-table
)))
5119 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
5121 (defun org-current-time ()
5122 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5123 (if (> (car org-time-stamp-rounding-minutes
) 1)
5124 (let ((r (car org-time-stamp-rounding-minutes
))
5125 (time (decode-time)))
5127 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5132 "Return today date, considering `org-extend-today-until'."
5134 (time-subtract (current-time)
5135 (list 0 (* 3600 org-extend-today-until
) 0))))
5137 ;;;; Font-Lock stuff, including the activators
5139 (defvar org-mouse-map
(make-sparse-keymap))
5140 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5141 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5142 (when org-mouse-1-follows-link
5143 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5144 (when org-tab-follows-link
5145 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5146 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5148 (require 'font-lock
)
5150 (defconst org-non-link-chars
"]\t\n\r<>")
5151 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5152 "shell" "elisp" "doi" "message"))
5153 (defvar org-link-types-re nil
5154 "Matches a link that has a url-like prefix like \"http:\"")
5155 (defvar org-link-re-with-space nil
5156 "Matches a link with spaces, optional angular brackets around it.")
5157 (defvar org-link-re-with-space2 nil
5158 "Matches a link with spaces, optional angular brackets around it.")
5159 (defvar org-link-re-with-space3 nil
5160 "Matches a link with spaces, only for internal part in bracket links.")
5161 (defvar org-angle-link-re nil
5162 "Matches link with angular brackets, spaces are allowed.")
5163 (defvar org-plain-link-re nil
5164 "Matches plain link, without spaces.")
5165 (defvar org-bracket-link-regexp nil
5166 "Matches a link in double brackets.")
5167 (defvar org-bracket-link-analytic-regexp nil
5168 "Regular expression used to analyze links.
5169 Here is what the match groups contain after a match:
5175 (defvar org-bracket-link-analytic-regexp
++ nil
5176 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5177 (defvar org-any-link-re nil
5178 "Regular expression matching any link.")
5180 (defcustom org-match-sexp-depth
3
5181 "Number of stacked braces for sub/superscript matching.
5182 This has to be set before loading org.el to be effective."
5183 :group
'org-export-translation
; ??????????????????????????/
5186 (defun org-create-multibrace-regexp (left right n
)
5187 "Create a regular expression which will match a balanced sexp.
5188 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5189 as single character strings.
5190 The regexp returned will match the entire expression including the
5191 delimiters. It will also define a single group which contains the
5192 match except for the outermost delimiters. The maximum depth of
5193 stacked delimiters is N. Escaping delimiters is not possible."
5194 (let* ((nothing (concat "[^" left right
"]*?"))
5197 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5200 re
(concat re or next
)
5201 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5202 (concat left
"\\(" re
"\\)" right
)))
5204 (defvar org-match-substring-regexp
5206 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5207 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5209 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5211 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5212 "The regular expression matching a sub- or superscript.")
5214 (defvar org-match-substring-with-braces-regexp
5216 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5217 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5219 "The regular expression matching a sub- or superscript, forcing braces.")
5221 (defun org-make-link-regexps ()
5222 "Update the link regular expressions.
5223 This should be called after the variable `org-link-types' has changed."
5224 (setq org-link-types-re
5226 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5227 org-link-re-with-space
5229 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5230 "\\([^" org-non-link-chars
" ]"
5231 "[^" org-non-link-chars
"]*"
5232 "[^" org-non-link-chars
" ]\\)>?")
5233 org-link-re-with-space2
5235 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5236 "\\([^" org-non-link-chars
" ]"
5238 "[^" org-non-link-chars
" ]\\)>?")
5239 org-link-re-with-space3
5241 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5242 "\\([^" org-non-link-chars
" ]"
5246 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5247 "\\([^" org-non-link-chars
" ]"
5248 "[^" org-non-link-chars
"]*"
5252 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5253 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5254 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5255 org-bracket-link-regexp
5256 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5257 org-bracket-link-analytic-regexp
5260 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5263 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5265 org-bracket-link-analytic-regexp
++
5268 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5271 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5274 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5275 org-angle-link-re
"\\)\\|\\("
5276 org-plain-link-re
"\\)")))
5278 (org-make-link-regexps)
5280 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5281 "Regular expression for fast time stamp matching.")
5282 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5283 "Regular expression for fast time stamp matching.")
5284 (defconst org-ts-regexp0
5285 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5286 "Regular expression matching time strings for analysis.
5287 This one does not require the space after the date, so it can be used
5288 on a string that terminates immediately after the date.")
5289 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5290 "Regular expression matching time strings for analysis.")
5291 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5292 "Regular expression matching time stamps, with groups.")
5293 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5294 "Regular expression matching time stamps (also [..]), with groups.")
5295 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5296 "Regular expression matching a time stamp range.")
5297 (defconst org-tr-regexp-both
5298 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5299 "Regular expression matching a time stamp range.")
5300 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5301 org-ts-regexp
"\\)?")
5302 "Regular expression matching a time stamp or time stamp range.")
5303 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5304 org-ts-regexp-both
"\\)?")
5305 "Regular expression matching a time stamp or time stamp range.
5306 The time stamps may be either active or inactive.")
5308 (defvar org-emph-face nil
)
5310 (defun org-do-emphasis-faces (limit)
5311 "Run through the buffer and add overlays to emphasized strings."
5313 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5314 (if (not (= (char-after (match-beginning 3))
5315 (char-after (match-beginning 4))))
5318 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5319 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5323 (org-remove-flyspell-overlays-in
5324 (match-beginning 0) (match-end 0)))
5325 (add-text-properties (match-beginning 2) (match-end 2)
5326 '(font-lock-multiline t org-emphasis t
))
5327 (when org-hide-emphasis-markers
5328 (add-text-properties (match-end 4) (match-beginning 5)
5329 '(invisible org-link
))
5330 (add-text-properties (match-beginning 3) (match-end 3)
5331 '(invisible org-link
)))))
5335 (defun org-emphasize (&optional char
)
5336 "Insert or change an emphasis, i.e. a font like bold or italic.
5337 If there is an active region, change that region to a new emphasis.
5338 If there is no region, just insert the marker characters and position
5339 the cursor between them.
5340 CHAR should be either the marker character, or the first character of the
5341 HTML tag associated with that emphasis. If CHAR is a space, the means
5342 to remove the emphasis of the selected region.
5343 If char is not given (for example in an interactive call) it
5344 will be prompted for."
5346 (let ((eal org-emphasis-alist
) e det
5347 (erc org-emphasis-regexp-components
)
5349 (string "") beg end move tag c s
)
5350 (if (org-region-active-p)
5351 (setq beg
(region-beginning) end
(region-end)
5352 string
(buffer-substring beg end
))
5355 (while (setq e
(pop eal
))
5356 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5358 (push (cons c
(string-to-char (car e
))) det
)
5359 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5360 (substring tag
1)))))
5361 (setq det
(nreverse det
))
5363 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5364 (setq char
(read-char-exclusive)))
5365 (setq char
(or (cdr (assoc char det
)) char
))
5366 (if (equal char ?\
)
5367 (setq s
"" move nil
)
5368 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5369 (error "No such emphasis marker: \"%c\"" char
))
5370 (setq s
(char-to-string char
)))
5371 (while (and (> (length string
) 1)
5372 (equal (substring string
0 1) (substring string -
1))
5373 (assoc (substring string
0 1) org-emphasis-alist
))
5374 (setq string
(substring string
1 -
1)))
5375 (setq string
(concat s string s
))
5376 (if beg
(delete-region beg end
))
5378 (string-match (concat "[" (nth 0 erc
) "\n]")
5379 (char-to-string (char-before (point)))))
5382 (string-match (concat "[" (nth 1 erc
) "\n]")
5383 (char-to-string (char-after (point)))))
5384 (insert " ") (backward-char 1))
5386 (and move
(backward-char 1))))
5388 (defconst org-nonsticky-props
5389 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
5391 (defsubst org-rear-nonsticky-at
(pos)
5392 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5394 (defun org-activate-plain-links (limit)
5395 "Run through the buffer and add overlays to links."
5398 (if (re-search-forward org-plain-link-re limit t
)
5400 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5401 (setq f
(get-text-property (match-beginning 0) 'face
))
5402 (if (or (eq f
'org-tag
)
5403 (and (listp f
) (memq 'org-tag f
)))
5405 (add-text-properties (match-beginning 0) (match-end 0)
5406 (list 'mouse-face
'highlight
5408 'keymap org-mouse-map
))
5409 (org-rear-nonsticky-at (match-end 0)))
5412 (defun org-activate-code (limit)
5413 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5415 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5416 (remove-text-properties (match-beginning 0) (match-end 0)
5417 '(display t invisible t intangible t
))
5420 (defcustom org-src-fontify-natively nil
5421 "When non-nil, fontify code in code blocks."
5424 :group
'org-appearance
5427 (defcustom org-src-prevent-auto-filling nil
5428 "When non-nil, prevent auto-filling in src blocks."
5431 :group
'org-appearance
5434 (defcustom org-allow-promoting-top-level-subtree nil
5435 "When non-nil, allow promoting a top level subtree.
5436 The leading star of the top level headline will be replaced
5440 :group
'org-appearance
)
5442 (defun org-fontify-meta-lines-and-blocks (limit)
5444 (org-fontify-meta-lines-and-blocks-1 limit
)
5445 (error (message "org-mode fontification error"))))
5447 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5448 "Fontify #+ lines and blocks, in the correct ways."
5449 (let ((case-fold-search t
))
5450 (if (re-search-forward
5451 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5453 (let ((beg (match-beginning 0))
5454 (block-start (match-end 0))
5456 (lang (match-string 7))
5457 (beg1 (line-beginning-position 2))
5458 (dc1 (downcase (match-string 2)))
5459 (dc3 (downcase (match-string 3)))
5460 end end1 quoting block-type ovl
)
5462 ((member dc1
'("html:" "ascii:" "latex:" "docbook:"))
5463 ;; a single line of backend-specific content
5464 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5465 (remove-text-properties (match-beginning 0) (match-end 0)
5466 '(display t invisible t intangible t
))
5467 (add-text-properties (match-beginning 1) (match-end 3)
5468 '(font-lock-fontified t face org-meta-line
))
5469 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5470 '(font-lock-fontified t face org-block
))
5471 ; for backend-specific code
5473 ((and (match-end 4) (equal dc3
"begin"))
5475 (setq block-type
(downcase (match-string 5))
5476 quoting
(member block-type org-protecting-blocks
))
5477 (when (re-search-forward
5478 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5479 nil t
) ;; on purpose, we look further than LIMIT
5480 (setq end
(min (point-max) (match-end 0))
5481 end1
(min (point-max) (1- (match-beginning 0))))
5482 (setq block-end
(match-beginning 0))
5484 (remove-text-properties beg end
5485 '(display t invisible t intangible t
)))
5486 (add-text-properties
5488 '(font-lock-fontified t font-lock-multiline t
))
5489 (add-text-properties beg beg1
'(face org-meta-line
))
5490 (add-text-properties end1
(min (point-max) (1+ end
))
5491 '(face org-meta-line
)) ; for end_src
5493 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5494 (org-src-font-lock-fontify-block lang block-start block-end
)
5495 ;; remove old background overlays
5497 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5498 (delete-overlay ov
)))
5499 (overlays-at (/ (+ beg1 block-end
) 2)))
5500 ;; add a background overlay
5501 (setq ovl
(make-overlay beg1 block-end
))
5502 (overlay-put ovl
'face
'org-block-background
)
5503 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5505 (add-text-properties beg1
(min (point-max) (1+ end1
))
5506 '(face org-block
))) ; end of source block
5507 ((not org-fontify-quote-and-verse-blocks
))
5508 ((string= block-type
"quote")
5509 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5510 ((string= block-type
"verse")
5511 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5512 (add-text-properties beg beg1
'(face org-block-begin-line
))
5513 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5514 '(face org-block-end-line
))
5516 ((member dc1
'("title:" "author:" "email:" "date:"))
5517 (add-text-properties
5519 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5520 '(font-lock-fontified t invisible t
)
5521 '(font-lock-fontified t face org-document-info-keyword
)))
5522 (add-text-properties
5523 (match-beginning 6) (match-end 6)
5524 (if (string-equal dc1
"title:")
5525 '(font-lock-fontified t face org-document-title
)
5526 '(font-lock-fontified t face org-document-info
))))
5527 ((not (member (char-after beg
) '(?\ ?
\t)))
5528 ;; just any other in-buffer setting, but not indented
5529 (add-text-properties
5531 '(font-lock-fontified t face org-meta-line
))
5533 ((or (member dc1
'("begin:" "end:" "caption:" "label:"
5534 "orgtbl:" "tblfm:" "tblname:" "results:"
5535 "call:" "header:" "headers:" "name:"))
5536 (and (match-end 4) (equal dc3
"attr")))
5537 (add-text-properties
5539 '(font-lock-fontified t face org-meta-line
))
5541 ((member dc3
'(" " ""))
5542 (add-text-properties
5544 '(font-lock-fontified t face font-lock-comment-face
)))
5547 (defun org-strip-protective-commas (beg end
)
5548 "Strip protective commas between BEG and END in the current buffer."
5553 (let ((front-line (save-excursion
5555 "[^[:space:]]" end t
)
5556 (goto-char (match-beginning 0))
5558 (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t
)
5559 (goto-char (match-beginning 1))
5560 (when (= (current-column) front-line
)
5561 (replace-match "" nil nil nil
1)))))))
5563 (defun org-activate-angle-links (limit)
5564 "Run through the buffer and add overlays to links."
5565 (if (re-search-forward org-angle-link-re limit t
)
5567 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5568 (add-text-properties (match-beginning 0) (match-end 0)
5569 (list 'mouse-face
'highlight
5570 'keymap org-mouse-map
))
5571 (org-rear-nonsticky-at (match-end 0))
5574 (defun org-activate-footnote-links (limit)
5575 "Run through the buffer and add overlays to footnotes."
5576 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5578 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5579 (org-remove-flyspell-overlays-in beg end
)
5580 (add-text-properties beg end
5581 (list 'mouse-face
'highlight
5582 'keymap org-mouse-map
5584 (if (= (point-at-bol) beg
)
5585 "Footnote definition"
5586 "Footnote reference")
5587 'font-lock-fontified t
5588 'font-lock-multiline t
5589 'face
'org-footnote
))))))
5591 (defun org-activate-bracket-links (limit)
5592 "Run through the buffer and add overlays to bracketed links."
5593 (if (re-search-forward org-bracket-link-regexp limit t
)
5594 (let* ((help (concat "LINK: "
5595 (org-match-string-no-properties 1)))
5596 ;; FIXME: above we should remove the escapes.
5597 ;; but that requires another match, protecting match data,
5598 ;; a lot of overhead for font-lock.
5599 (ip (org-maybe-intangible
5600 (list 'invisible
'org-link
5601 'keymap org-mouse-map
'mouse-face
'highlight
5602 'font-lock-multiline t
'help-echo help
)))
5603 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5604 'font-lock-multiline t
'help-echo help
)))
5605 ;; We need to remove the invisible property here. Table narrowing
5606 ;; may have made some of this invisible.
5607 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5608 (remove-text-properties (match-beginning 0) (match-end 0)
5612 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5613 (org-rear-nonsticky-at (match-beginning 3))
5614 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5615 (org-rear-nonsticky-at (match-end 3))
5616 (add-text-properties (match-end 3) (match-end 0) ip
)
5617 (org-rear-nonsticky-at (match-end 0)))
5618 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5619 (org-rear-nonsticky-at (match-beginning 1))
5620 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5621 (org-rear-nonsticky-at (match-end 1))
5622 (add-text-properties (match-end 1) (match-end 0) ip
)
5623 (org-rear-nonsticky-at (match-end 0)))
5626 (defun org-activate-dates (limit)
5627 "Run through the buffer and add overlays to dates."
5628 (if (re-search-forward org-tsr-regexp-both limit t
)
5630 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5631 (add-text-properties (match-beginning 0) (match-end 0)
5632 (list 'mouse-face
'highlight
5633 'keymap org-mouse-map
))
5634 (org-rear-nonsticky-at (match-end 0))
5635 (when org-display-custom-times
5637 (org-display-custom-time (match-beginning 3) (match-end 3)))
5638 (org-display-custom-time (match-beginning 1) (match-end 1)))
5641 (defvar org-target-link-regexp nil
5642 "Regular expression matching radio targets in plain text.")
5643 (make-variable-buffer-local 'org-target-link-regexp
)
5644 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5645 "Regular expression matching a link target.")
5646 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5647 "Regular expression matching a radio target.")
5648 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5649 "Regular expression matching any target.")
5651 (defun org-activate-target-links (limit)
5652 "Run through the buffer and add overlays to target matches."
5653 (when org-target-link-regexp
5654 (let ((case-fold-search t
))
5655 (if (re-search-forward org-target-link-regexp limit t
)
5657 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5658 (add-text-properties (match-beginning 0) (match-end 0)
5659 (list 'mouse-face
'highlight
5660 'keymap org-mouse-map
5661 'help-echo
"Radio target link"
5662 'org-linked-text t
))
5663 (org-rear-nonsticky-at (match-end 0))
5666 (defun org-update-radio-target-regexp ()
5667 "Find all radio targets in this file and update the regular expression."
5669 (when (memq 'radio org-activate-links
)
5670 (setq org-target-link-regexp
5671 (org-make-target-link-regexp (org-all-targets 'radio
)))
5672 (org-restart-font-lock)))
5674 (defun org-hide-wide-columns (limit)
5676 (setq s
(text-property-any (point) (or limit
(point-max))
5679 (setq e
(next-single-property-change s
'org-cwidth
))
5680 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5684 (defvar org-latex-and-specials-regexp nil
5685 "Regular expression for highlighting export special stuff.")
5686 (defvar org-match-substring-regexp
)
5687 (defvar org-match-substring-with-braces-regexp
)
5689 ;; This should be with the exporter code, but we also use if for font-locking
5690 (defconst org-export-html-special-string-regexps
5691 '(("\\\\-" .
"­")
5692 ("---\\([^-]\\)" .
"—\\1")
5693 ("--\\([^-]\\)" .
"–\\1")
5694 ("\\.\\.\\." .
"…"))
5695 "Regular expressions for special string conversion.")
5698 (defun org-compute-latex-and-specials-regexp ()
5699 "Compute regular expression for stuff treated specially by exporters."
5700 (if (not org-highlight-latex-fragments-and-specials
)
5701 (org-set-local 'org-latex-and-specials-regexp nil
)
5704 ((matchers (plist-get org-format-latex-options
:matchers
))
5705 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5706 org-latex-regexps
)))
5707 (org-export-allow-BIND nil
)
5708 (options (org-combine-plists (org-default-export-plist)
5709 (org-infile-export-plist)))
5710 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5711 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5712 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5713 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5714 (org-export-with-special-strings (plist-get options
:special-strings
))
5717 ((equal org-export-with-sub-superscripts
'{})
5718 (list org-match-substring-with-braces-regexp
))
5719 (org-export-with-sub-superscripts
5720 (list org-match-substring-regexp
))
5723 (if org-export-with-LaTeX-fragments
5724 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5726 (if org-export-with-TeX-macros
5727 (list (concat "\\\\"
5733 (append org-entities-user
5735 (if (boundp 'org-latex-entities
)
5737 (or (car-safe x
) x
))
5742 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5743 (re-special (if org-export-with-special-strings
5744 (mapcar (lambda (x) (car x
))
5745 org-export-html-special-string-regexps
)))
5749 (if org-export-html-expand
"@<[^>\n]+>")
5752 'org-latex-and-specials-regexp
5753 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5756 (defun org-do-latex-and-special-faces (limit)
5757 "Run through the buffer and add overlays to links."
5758 (when org-latex-and-specials-regexp
5760 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5762 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5764 '(org-code org-verbatim underline
)))
5767 d
(cond ((member (char-after (1+ (match-beginning 0)))
5770 (font-lock-prepend-text-property
5771 (+ d
(match-beginning 0)) (match-end 0)
5772 'face
'org-latex-and-export-specials
)
5773 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5774 '(font-lock-multiline t
)))))
5777 (defun org-restart-font-lock ()
5778 "Restart `font-lock-mode', to force refontification."
5779 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5781 (font-lock-mode 1)))
5783 (defun org-all-targets (&optional radio
)
5784 "Return a list of all targets in this file.
5785 With optional argument RADIO, only find radio targets."
5786 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5789 (goto-char (point-min))
5790 (while (re-search-forward re nil t
)
5791 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5794 (defun org-make-target-link-regexp (targets)
5795 "Make regular expression matching all strings in TARGETS.
5796 The regular expression finds the targets also if there is a line break
5803 (setq x
(regexp-quote x
))
5804 (while (string-match " +" x
)
5805 (setq x
(replace-match "\\s-+" t t x
)))
5811 (defun org-activate-tags (limit)
5812 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5814 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5815 (add-text-properties (match-beginning 1) (match-end 1)
5816 (list 'mouse-face
'highlight
5817 'keymap org-mouse-map
))
5818 (org-rear-nonsticky-at (match-end 1))
5821 (defun org-outline-level ()
5822 "Compute the outline level of the heading at point.
5823 This function assumes that the cursor is at the beginning of a line matched
5824 by `outline-regexp'. Otherwise it returns garbage.
5825 If this is called at a normal headline, the level is the number of stars.
5826 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5828 (looking-at org-outline-regexp
)
5829 (1- (- (match-end 0) (match-beginning 0)))))
5831 (defvar org-font-lock-keywords nil
)
5833 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5834 "Regular expression matching a property line.")
5836 (defvar org-font-lock-hook nil
5837 "Functions to be called for special font lock stuff.")
5839 (defvar org-font-lock-set-keywords-hook nil
5840 "Functions that can manipulate `org-font-lock-extra-keywords'.
5841 This is called after `org-font-lock-extra-keywords' is defined, but before
5842 it is installed to be used by font lock. This can be useful if something
5843 needs to be inserted at a specific position in the font-lock sequence.")
5845 (defun org-font-lock-hook (limit)
5846 (run-hook-with-args 'org-font-lock-hook limit
))
5848 (defun org-set-font-lock-defaults ()
5849 (let* ((em org-fontify-emphasized-text
)
5850 (lk org-activate-links
)
5851 (org-font-lock-extra-keywords
5854 '(org-font-lock-hook)
5856 `(,(if org-fontify-whole-heading-line
5857 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5858 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5859 (1 (org-get-level-face 1))
5860 (2 (org-get-level-face 2))
5861 (3 (org-get-level-face 3)))
5863 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5866 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5867 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5868 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5869 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5871 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5872 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5874 (list org-property-re
5875 '(1 'org-special-keyword t
)
5876 '(3 'org-property-value t
))
5878 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5879 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5880 (if (memq 'plain lk
) '(org-activate-plain-links))
5881 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5882 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5883 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5884 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5885 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5886 '(org-hide-wide-columns (0 nil append
))
5888 (list (format org-heading-keyword-regexp-format
5890 '(2 (org-get-todo-face 2) t
))
5892 (if org-fontify-done-headline
5893 (list (format org-heading-keyword-regexp-format
5896 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5898 '(2 'org-headline-done t
))
5901 '(org-font-lock-add-priority-faces)
5903 '(org-font-lock-add-tag-faces)
5905 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5906 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5907 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5908 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5911 (if (featurep 'xemacs
)
5912 '(org-do-emphasis-faces (0 nil append
))
5913 '(org-do-emphasis-faces)))
5915 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5916 1 'org-checkbox prepend
)
5917 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5918 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5919 (0 (org-get-checkbox-statistics-face) t
)))
5920 ;; Description list items
5921 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5922 1 'org-list-dt prepend
)
5923 ;; ARCHIVEd headings
5925 org-outline-regexp-bol
5926 "\\(.*:" org-archive-tag
":.*\\)")
5927 '(1 'org-archived prepend
))
5929 '(org-do-latex-and-special-faces)
5930 '(org-fontify-entities)
5931 '(org-raise-scripts)
5933 '(org-activate-code (1 'org-code t
))
5935 (list (format org-heading-keyword-regexp-format
5937 org-comment-string
"\\|" org-quote-string
5939 '(2 'org-special-keyword t
))
5940 '("^#.*" (0 'font-lock-comment-face t
))
5941 ;; Blocks and meta lines
5942 '(org-fontify-meta-lines-and-blocks)
5944 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5945 (run-hooks 'org-font-lock-set-keywords-hook
)
5946 ;; Now set the full font-lock-keywords
5947 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5948 (org-set-local 'font-lock-defaults
5949 '(org-font-lock-keywords t nil nil backward-paragraph
))
5950 (kill-local-variable 'font-lock-keywords
) nil
))
5952 (defun org-toggle-pretty-entities ()
5953 "Toggle the composition display of entities as UTF8 characters."
5955 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5956 (org-restart-font-lock)
5957 (if org-pretty-entities
5958 (message "Entities are displayed as UTF8 characters")
5961 (org-decompose-region (point-min) (point-max))
5962 (message "Entities are displayed plain"))))
5964 (defun org-fontify-entities (limit)
5965 "Find an entity to fontify."
5967 (when org-pretty-entities
5969 (while (re-search-forward
5970 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
5972 (if (and (not (org-in-indented-comment-line))
5973 (setq ee
(org-entity-get (match-string 1)))
5974 (= (length (nth 6 ee
)) 1))
5976 ((end (if (equal (match-string 2) "{}")
5979 (add-text-properties
5980 (match-beginning 0) end
5981 (list 'font-lock-fontified t
))
5982 (compose-region (match-beginning 0) end
5988 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5989 "Fontify string S like in Org-mode."
5992 (let ((org-odd-levels-only odd-levels
))
5994 (font-lock-fontify-buffer)
6000 (defun org-get-level-face (n)
6001 "Get the right face for match N in font-lock matching of headlines."
6002 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
6003 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
6004 (if org-cycle-level-faces
6005 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
6006 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
6008 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
6010 (t (if org-level-color-stars-only nil org-f
))))
6013 (defun org-get-todo-face (kwd)
6014 "Get the right face for a TODO keyword KWD.
6015 If KWD is a number, get the corresponding match group."
6016 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6017 (or (org-face-from-face-or-color
6018 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
6019 (and (member kwd org-done-keywords
) 'org-done
)
6022 (defun org-face-from-face-or-color (context inherit face-or-color
)
6023 "Create a face list that inherits INHERIT, but sets the foreground color.
6024 When FACE-OR-COLOR is not a string, just return it."
6025 (if (stringp face-or-color
)
6026 (list :inherit inherit
6027 (cdr (assoc context org-faces-easy-properties
))
6031 (defun org-font-lock-add-tag-faces (limit)
6032 "Add the special tag faces."
6033 (when (and org-tag-faces org-tags-special-faces-re
)
6034 (while (re-search-forward org-tags-special-faces-re limit t
)
6035 (add-text-properties (match-beginning 1) (match-end 1)
6036 (list 'face
(org-get-tag-face 1)
6037 'font-lock-fontified t
))
6038 (backward-char 1))))
6040 (defun org-font-lock-add-priority-faces (limit)
6041 "Add the special priority faces."
6042 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
6043 (when (save-match-data (org-at-heading-p))
6044 (add-text-properties
6045 (match-beginning 0) (match-end 0)
6046 (list 'face
(or (org-face-from-face-or-color
6047 'priority
'org-special-keyword
6048 (cdr (assoc (char-after (match-beginning 1))
6049 org-priority-faces
)))
6050 'org-special-keyword
)
6051 'font-lock-fontified t
)))))
6053 (defun org-get-tag-face (kwd)
6054 "Get the right face for a TODO keyword KWD.
6055 If KWD is a number, get the corresponding match group."
6056 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
6057 (or (org-face-from-face-or-color
6058 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
6061 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
6062 "Remove fontification and activation overlays from links."
6063 (font-lock-default-unfontify-region beg end
)
6064 (let* ((buffer-undo-list t
)
6065 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
6066 (inhibit-modification-hooks t
)
6067 deactivate-mark buffer-file-name buffer-file-truename
)
6068 (org-decompose-region beg end
)
6069 (remove-text-properties beg end
6070 '(mouse-face t keymap t org-linked-text t
6071 invisible t intangible t
6072 org-no-flyspell t org-emphasis t
))
6073 (org-remove-font-lock-display-properties beg end
)))
6075 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
6076 ((raise 0.3) (height 0.7))
6079 "Display properties for showing superscripts and subscripts.")
6081 (defun org-remove-font-lock-display-properties (beg end
)
6082 "Remove specific display properties that have been added by font lock.
6083 The will remove the raise properties that are used to show superscripts
6087 (setq next
(next-single-property-change beg
'display nil end
)
6088 prop
(get-text-property beg
'display
))
6089 (if (member prop org-script-display
)
6090 (put-text-property beg next
'display nil
))
6093 (defun org-raise-scripts (limit)
6094 "Add raise properties to sub/superscripts."
6095 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
6096 (if (re-search-forward
6097 (if (eq org-use-sub-superscripts t
)
6098 org-match-substring-regexp
6099 org-match-substring-with-braces-regexp
)
6101 (let* ((pos (point)) table-p comment-p
6102 (mpos (match-beginning 3))
6103 (emph-p (get-text-property mpos
'org-emphasis
))
6104 (link-p (get-text-property mpos
'mouse-face
))
6105 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
6106 (goto-char (point-at-bol))
6107 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
6108 comment-p
(org-looking-at-p "[ \t]*#"))
6110 ;; FIXME: Should we go back one character here, for a_b^c
6111 ;; (goto-char (1- pos)) ;????????????????????
6112 (if (or comment-p emph-p link-p keyw-p
)
6114 (put-text-property (match-beginning 3) (match-end 0)
6116 (if (equal (char-after (match-beginning 2)) ?^
)
6117 (nth (if table-p
3 1) org-script-display
)
6118 (nth (if table-p
2 0) org-script-display
)))
6119 (add-text-properties (match-beginning 2) (match-end 2)
6121 'org-dwidth t
'org-dwidth-n
1))
6122 (if (and (eq (char-after (match-beginning 3)) ?
{)
6123 (eq (char-before (match-end 3)) ?
}))
6125 (add-text-properties
6126 (match-beginning 3) (1+ (match-beginning 3))
6127 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6128 (add-text-properties
6129 (1- (match-end 3)) (match-end 3)
6130 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6133 ;;;; Visibility cycling, including org-goto and indirect buffer
6137 (defvar org-cycle-global-status nil
)
6138 (make-variable-buffer-local 'org-cycle-global-status
)
6139 (defvar org-cycle-subtree-status nil
)
6140 (make-variable-buffer-local 'org-cycle-subtree-status
)
6144 (defvar org-inlinetask-min-level
)
6146 (defun org-cycle (&optional arg
)
6147 "TAB-action and visibility cycling for Org-mode.
6149 This is the command invoked in Org-mode by the TAB key. Its main purpose
6150 is outline visibility cycling, but it also invokes other actions
6151 in special contexts.
6153 - When this function is called with a prefix argument, rotate the entire
6154 buffer through 3 states (global cycling)
6155 1. OVERVIEW: Show only top-level headlines.
6156 2. CONTENTS: Show all headlines of all levels, but no body text.
6157 3. SHOW ALL: Show everything.
6158 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6159 determined by the variable `org-startup-folded', and by any VISIBILITY
6160 properties in the buffer.
6161 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6162 including any drawers.
6164 - When inside a table, re-align the table and move to the next field.
6166 - When point is at the beginning of a headline, rotate the subtree started
6167 by this line through 3 different states (local cycling)
6168 1. FOLDED: Only the main headline is shown.
6169 2. CHILDREN: The main headline and the direct children are shown.
6170 From this state, you can move to one of the children
6171 and zoom in further.
6172 3. SUBTREE: Show the entire subtree, including body text.
6173 If there is no subtree, switch directly from CHILDREN to FOLDED.
6175 - When point is at the beginning of an empty headline and the variable
6176 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6177 of the headline by demoting and promoting it to likely levels. This
6178 speeds up creation document structure by pressing TAB once or several
6179 times right after creating a new headline.
6181 - When there is a numeric prefix, go up to a heading with level ARG, do
6182 a `show-subtree' and return to the previous cursor position. If ARG
6183 is negative, go up that many levels.
6185 - When point is not at the beginning of a headline, execute the global
6186 binding for TAB, which is re-indenting the line. See the option
6187 `org-cycle-emulate-tab' for details.
6189 - Special case: if point is at the beginning of the buffer and there is
6190 no headline in line 1, this function will act as if called with prefix arg
6191 (C-u TAB, same as S-TAB) also when called without prefix arg.
6192 But only if also the variable `org-cycle-global-at-bob' is t."
6194 (org-load-modules-maybe)
6195 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6196 (and org-cycle-level-after-item
/entry-creation
6197 (or (org-cycle-level)
6198 (org-cycle-item-indentation))))
6200 (or org-cycle-max-level
6201 (and (boundp 'org-inlinetask-min-level
)
6202 org-inlinetask-min-level
6203 (1- org-inlinetask-min-level
))))
6204 (nstars (and limit-level
6205 (if org-odd-levels-only
6206 (and limit-level
(1- (* limit-level
2)))
6209 (if (not (derived-mode-p 'org-mode
))
6211 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6212 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6213 (not (looking-at org-outline-regexp
))))
6216 (delq 'org-optimize-window-after-visibility-change
6217 (copy-sequence org-cycle-hook
))
6221 (if (or bob-special
(equal arg
'(4)))
6222 ;; special case: use global cycling
6228 (setq last-command
'dummy
)
6229 (org-set-startup-visibility)
6230 (message "Startup visibility, plus VISIBILITY properties"))
6234 (message "Entire buffer visible, including drawers"))
6236 ;; Table: enter it or move to the next field.
6237 ((org-at-table-p 'any
)
6238 (if (org-at-table.el-p
)
6239 (message "Use C-c ' to edit table.el tables")
6240 (if arg
(org-table-edit-field t
)
6241 (org-table-justify-field-maybe)
6242 (call-interactively 'org-table-next-field
))))
6244 ((run-hook-with-args-until-success
6245 'org-tab-after-check-for-table-hook
))
6247 ;; Global cycling: delegate to `org-cycle-internal-global'.
6248 ((eq arg t
) (org-cycle-internal-global))
6250 ;; Drawers: delegate to `org-flag-drawer'.
6251 ((and org-drawers org-drawer-regexp
6253 (beginning-of-line 1)
6254 (looking-at org-drawer-regexp
)))
6255 (org-flag-drawer ; toggle block visibility
6256 (not (get-char-property (match-end 0) 'invisible
))))
6258 ;; Show-subtree, ARG levels up from here.
6261 (org-back-to-heading)
6262 (outline-up-heading (if (< arg
0) (- arg
)
6263 (- (funcall outline-level
) arg
)))
6264 (org-show-subtree)))
6266 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6267 ((and (featurep 'org-inlinetask
)
6268 (org-inlinetask-at-task-p)
6269 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6270 (org-inlinetask-toggle-visibility))
6272 ((org-try-cdlatex-tab))
6274 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6275 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6276 (save-excursion (beginning-of-line 1)
6277 (looking-at org-outline-regexp
)))
6278 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6279 (org-cycle-internal-local))
6281 ;; From there: TAB emulation and template completion.
6282 (buffer-read-only (org-back-to-heading))
6284 ((run-hook-with-args-until-success
6285 'org-tab-after-check-for-cycling-hook
))
6287 ((org-try-structure-completion))
6289 ((run-hook-with-args-until-success
6290 'org-tab-before-tab-emulation-hook
))
6292 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6294 (not (looking-at org-outline-regexp
))))
6295 (call-interactively (global-key-binding "\t")))
6297 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6298 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6299 (or (and (eq org-cycle-emulate-tab
'white
)
6300 (= (match-end 0) (point-at-eol)))
6301 (and (eq org-cycle-emulate-tab
'whitestart
)
6302 (>= (match-end 0) pos
))))
6304 (eq org-cycle-emulate-tab t
))
6305 (call-interactively (global-key-binding "\t")))
6308 (org-back-to-heading)
6311 (defun org-cycle-internal-global ()
6312 "Do the global cycling action."
6313 ;; Hack to avoid display of messages for .org attachments in Gnus
6314 (let ((ga (string-match "\\*fontification" (buffer-name))))
6316 ((and (eq last-command this-command
)
6317 (eq org-cycle-global-status
'overview
))
6318 ;; We just created the overview - now do table of contents
6319 ;; This can be slow in very large buffers, so indicate action
6320 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6321 (unless ga
(message "CONTENTS..."))
6323 (unless ga
(message "CONTENTS...done"))
6324 (setq org-cycle-global-status
'contents
)
6325 (run-hook-with-args 'org-cycle-hook
'contents
))
6327 ((and (eq last-command this-command
)
6328 (eq org-cycle-global-status
'contents
))
6329 ;; We just showed the table of contents - now show everything
6330 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6332 (unless ga
(message "SHOW ALL"))
6333 (setq org-cycle-global-status
'all
)
6334 (run-hook-with-args 'org-cycle-hook
'all
))
6337 ;; Default action: go to overview
6338 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6340 (unless ga
(message "OVERVIEW"))
6341 (setq org-cycle-global-status
'overview
)
6342 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6344 (defun org-cycle-internal-local ()
6345 "Do the local cycling action."
6346 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6347 ;; First, determine end of headline (EOH), end of subtree or item
6348 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6353 (setq struct
(org-list-struct))
6354 (setq eoh
(point-at-eol))
6355 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6356 (setq has-children
(org-list-has-child-p (point) struct
)))
6357 (org-back-to-heading)
6358 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6359 (setq eos
(save-excursion
6360 (org-end-of-subtree t
)
6362 (skip-chars-forward " \t\n"))
6363 (if (eobp) (point) (1- (point)))))
6366 (let ((level (funcall outline-level
)))
6367 (outline-next-heading)
6368 (and (org-at-heading-p t
)
6369 (> (funcall outline-level
) level
))))
6371 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6372 ;; Determine end invisible part of buffer (EOL)
6373 (beginning-of-line 2)
6374 ;; XEmacs doesn't have `next-single-char-property-change'
6375 (if (featurep 'xemacs
)
6376 (while (and (not (eobp)) ;; this is like `next-line'
6377 (get-char-property (1- (point)) 'invisible
))
6378 (beginning-of-line 2))
6379 (while (and (not (eobp)) ;; this is like `next-line'
6380 (get-char-property (1- (point)) 'invisible
))
6381 (goto-char (next-single-char-property-change (point) 'invisible
))
6382 (and (eolp) (beginning-of-line 2))))
6384 ;; Find out what to do next and set `this-command'
6387 ;; Nothing is hidden behind this heading
6388 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
6389 (message "EMPTY ENTRY")
6390 (setq org-cycle-subtree-status nil
)
6393 (outline-next-heading)
6394 (if (outline-invisible-p) (org-flag-heading nil
))))
6395 ((and (or (>= eol eos
)
6396 (not (string-match "\\S-" (buffer-substring eol eos
))))
6398 (not (setq children-skipped
6399 org-cycle-skip-children-state-if-no-children
))))
6400 ;; Entire subtree is hidden in one line: children view
6401 (run-hook-with-args 'org-pre-cycle-hook
'children
)
6403 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6405 (org-with-limited-levels (show-children))
6406 ;; FIXME: This slows down the func way too much.
6407 ;; How keep drawers hidden in subtree anyway?
6408 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6409 ;; (org-cycle-hide-drawers 'subtree))
6411 ;; Fold every list in subtree to top-level items.
6412 (when (eq org-cycle-include-plain-lists
'integrate
)
6414 (org-back-to-heading)
6415 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6416 (beginning-of-line 1)
6417 (let* ((struct (org-list-struct))
6418 (prevs (org-list-prevs-alist struct
))
6419 (end (org-list-get-bottom-point struct
)))
6420 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6421 (org-list-get-all-items (point) struct prevs
))
6422 (goto-char end
))))))
6423 (message "CHILDREN")
6426 (outline-next-heading)
6427 (if (outline-invisible-p) (org-flag-heading nil
)))
6428 (setq org-cycle-subtree-status
'children
)
6429 (run-hook-with-args 'org-cycle-hook
'children
))
6430 ((or children-skipped
6431 (and (eq last-command this-command
)
6432 (eq org-cycle-subtree-status
'children
)))
6433 ;; We just showed the children, or no children are there,
6434 ;; now show everything.
6435 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
6436 (outline-flag-region eoh eos nil
)
6437 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6438 (setq org-cycle-subtree-status
'subtree
)
6439 (run-hook-with-args 'org-cycle-hook
'subtree
))
6441 ;; Default action: hide the subtree.
6442 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6443 (outline-flag-region eoh eos t
)
6445 (setq org-cycle-subtree-status
'folded
)
6446 (run-hook-with-args 'org-cycle-hook
'folded
)))))
6449 (defun org-global-cycle (&optional arg
)
6450 "Cycle the global visibility. For details see `org-cycle'.
6451 With \\[universal-argument] prefix arg, switch to startup visibility.
6452 With a numeric prefix, show all headlines up to that level."
6454 (let ((org-cycle-include-plain-lists
6455 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6459 (hide-sublevels arg
)
6460 (setq org-cycle-global-status
'contents
))
6462 (org-set-startup-visibility)
6463 (message "Startup visibility, plus VISIBILITY properties."))
6465 (org-cycle '(4))))))
6467 (defun org-set-startup-visibility ()
6468 "Set the visibility required by startup options and properties."
6470 ((eq org-startup-folded t
)
6472 ((eq org-startup-folded
'content
)
6473 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6474 (org-cycle '(4)) (org-cycle '(4)))))
6475 (unless (eq org-startup-folded
'showeverything
)
6476 (if org-hide-block-startup
(org-hide-block-all))
6477 (org-set-visibility-according-to-property 'no-cleanup
)
6478 (org-cycle-hide-archived-subtrees 'all
)
6479 (org-cycle-hide-drawers 'all
)
6480 (org-cycle-show-empty-lines t
)))
6482 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6483 "Switch subtree visibilities according to :VISIBILITY: property."
6485 (let (org-show-entry-below state
)
6487 (goto-char (point-min))
6488 (while (re-search-forward
6489 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6491 (setq state
(match-string 1))
6493 (org-back-to-heading t
)
6497 ((equal state
'("fold" "folded"))
6499 ((equal state
"children")
6500 (org-show-hidden-entry)
6502 ((equal state
"content")
6505 (org-narrow-to-subtree)
6507 ((member state
'("all" "showall"))
6510 (org-cycle-hide-archived-subtrees 'all
)
6511 (org-cycle-hide-drawers 'all
)
6512 (org-cycle-show-empty-lines 'all
)))))
6514 ;; This function uses outline-regexp instead of the more fundamental
6515 ;; org-outline-regexp so that org-cycle-global works outside of Org
6516 ;; buffers, where outline-regexp is needed.
6517 (defun org-overview ()
6518 "Switch to overview mode, showing only top-level headlines.
6519 Really, this shows all headlines with level equal or greater than the level
6520 of the first headline in the buffer. This is important, because if the
6521 first headline is not level one, then (hide-sublevels 1) gives confusing
6524 (let ((level (save-excursion
6525 (goto-char (point-min))
6526 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6528 (goto-char (match-beginning 0))
6529 (funcall outline-level
))))))
6530 (and level
(hide-sublevels level
))))
6532 (defun org-content (&optional arg
)
6533 "Show all headlines in the buffer, like a table of contents.
6534 With numerical argument N, show content up to level N."
6537 ;; Visit all headings and show their offspring
6538 (and (integerp arg
) (org-overview))
6539 (goto-char (point-max))
6541 (while (and (progn (condition-case nil
6542 (outline-previous-visible-heading 1)
6543 (error (goto-char (point-min))))
6545 (looking-at org-outline-regexp
))
6547 (show-children (1- arg
))
6549 (if (bobp) (throw 'exit nil
))))))
6552 (defun org-optimize-window-after-visibility-change (state)
6553 "Adjust the window after a change in outline visibility.
6554 This function is the default value of the hook `org-cycle-hook'."
6555 (when (get-buffer-window (current-buffer))
6557 ((eq state
'content
) nil
)
6558 ((eq state
'all
) nil
)
6559 ((eq state
'folded
) nil
)
6560 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6561 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6563 (defun org-remove-empty-overlays-at (pos)
6564 "Remove outline overlays that do not contain non-white stuff."
6567 (and (eq 'outline
(overlay-get o
'invisible
))
6568 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6570 (delete-overlay o
)))
6573 (defun org-clean-visibility-after-subtree-move ()
6574 "Fix visibility issues after moving a subtree."
6575 ;; First, find a reasonable region to look at:
6576 ;; Start two siblings above, end three below
6577 (let* ((beg (save-excursion
6578 (and (org-get-last-sibling)
6579 (org-get-last-sibling))
6581 (end (save-excursion
6582 (and (org-get-next-sibling)
6583 (org-get-next-sibling)
6584 (org-get-next-sibling))
6585 (if (org-at-heading-p)
6588 (level (looking-at "\\*+"))
6589 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6592 (narrow-to-region beg end
)
6594 ;; Properly fold already folded siblings
6595 (goto-char (point-min))
6596 (while (re-search-forward re nil t
)
6597 (if (and (not (outline-invisible-p))
6599 (goto-char (point-at-eol)) (outline-invisible-p)))
6601 (org-cycle-show-empty-lines 'overview
)
6602 (org-cycle-hide-drawers 'overview
)))))
6604 (defun org-cycle-show-empty-lines (state)
6605 "Show empty lines above all visible headlines.
6606 The region to be covered depends on STATE when called through
6607 `org-cycle-hook'. Lisp program can use t for STATE to get the
6608 entire buffer covered. Note that an empty line is only shown if there
6609 are at least `org-cycle-separator-lines' empty lines before the headline."
6610 (when (not (= org-cycle-separator-lines
0))
6612 (let* ((n (abs org-cycle-separator-lines
))
6614 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6615 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6616 (t (let ((ns (number-to-string (- n
2))))
6617 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6618 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6621 ((memq state
'(overview contents t
))
6622 (setq beg
(point-min) end
(point-max)))
6623 ((memq state
'(children folded
))
6624 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6625 (beginning-of-line 2)
6629 (while (re-search-forward re end t
)
6630 (unless (get-char-property (match-end 1) 'invisible
)
6631 (setq e
(match-end 1))
6632 (if (< org-cycle-separator-lines
0)
6633 (setq b
(save-excursion
6634 (goto-char (match-beginning 0))
6635 (org-back-over-empty-lines)
6637 (goto-char (max (point-min) (1- (point))))
6641 (setq b
(match-beginning 1)))
6642 (outline-flag-region b e nil
)))))))
6643 ;; Never hide empty lines at the end of the file.
6645 (goto-char (point-max))
6646 (outline-previous-heading)
6647 (outline-end-of-heading)
6648 (if (and (looking-at "[ \t\n]+")
6649 (= (match-end 0) (point-max)))
6650 (outline-flag-region (point) (match-end 0) nil
))))
6652 (defun org-show-empty-lines-in-parent ()
6653 "Move to the parent and re-show empty lines before visible headlines."
6655 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6656 (org-cycle-show-empty-lines context
))))
6658 (defun org-files-list ()
6659 "Return `org-agenda-files' list, plus all open org-mode files.
6660 This is useful for operations that need to scan all of a user's
6661 open and agenda-wise Org files."
6662 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6663 (dolist (buf (buffer-list))
6664 (with-current-buffer buf
6665 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6666 (let ((file (expand-file-name (buffer-file-name))))
6667 (unless (member file files
)
6668 (push file files
))))))
6671 (defsubst org-entry-beginning-position
()
6672 "Return the beginning position of the current entry."
6673 (save-excursion (outline-back-to-heading t
) (point)))
6675 (defsubst org-entry-end-position
()
6676 "Return the end position of the current entry."
6677 (save-excursion (outline-next-heading) (point)))
6679 (defun org-cycle-hide-drawers (state)
6680 "Re-hide all drawers after a visibility state change."
6681 (when (and (derived-mode-p 'org-mode
)
6682 (not (memq state
'(overview folded contents
))))
6684 (let* ((globalp (memq state
'(contents all
)))
6685 (beg (if globalp
(point-min) (point)))
6686 (end (if globalp
(point-max)
6687 (if (eq state
'children
)
6688 (save-excursion (outline-next-heading) (point))
6689 (org-end-of-subtree t
)))))
6691 (while (re-search-forward org-drawer-regexp end t
)
6692 (org-flag-drawer t
))))))
6694 (defun org-flag-drawer (flag)
6695 "When FLAG is non-nil, hide the drawer we are within.
6696 Otherwise make it visible."
6698 (beginning-of-line 1)
6699 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6700 (let ((b (match-end 0)))
6701 (if (re-search-forward
6703 (save-excursion (outline-next-heading) (point)) t
)
6704 (outline-flag-region b
(point-at-eol) flag
)
6705 (error ":END: line missing at position %s" b
))))))
6707 (defun org-subtree-end-visible-p ()
6708 "Is the end of the current subtree visible?"
6709 (pos-visible-in-window-p
6710 (save-excursion (org-end-of-subtree t
) (point))))
6712 (defun org-first-headline-recenter (&optional N
)
6713 "Move cursor to the first headline and recenter the headline.
6714 Optional argument N means put the headline into the Nth line of the window."
6715 (goto-char (point-min))
6716 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6718 (recenter (prefix-numeric-value N
))))
6720 ;;; Saving and restoring visibility
6722 (defun org-outline-overlay-data (&optional use-markers
)
6723 "Return a list of the locations of all outline overlays.
6724 These are overlays with the `invisible' property value `outline'.
6725 The return value is a list of cons cells, with start and stop
6726 positions for each overlay.
6727 If USE-MARKERS is set, return the positions as markers."
6734 (when (eq (overlay-get o
'invisible
) 'outline
)
6735 (setq beg
(overlay-start o
)
6736 end
(overlay-end o
))
6737 (and beg end
(> end beg
)
6739 (cons (move-marker (make-marker) beg
)
6740 (move-marker (make-marker) end
))
6742 (overlays-in (point-min) (point-max))))))))
6744 (defun org-set-outline-overlay-data (data)
6745 "Create visibility overlays for all positions in DATA.
6746 DATA should have been made by `org-outline-overlay-data'."
6753 (outline-flag-region (car c
) (cdr c
) t
))
6756 ;;; Folding of blocks
6758 (defvar org-hide-block-overlays nil
6759 "Overlays hiding blocks.")
6760 (make-variable-buffer-local 'org-hide-block-overlays
)
6762 (defun org-block-map (function &optional start end
)
6763 "Call FUNCTION at the head of all source blocks in the current buffer.
6764 Optional arguments START and END can be used to limit the range."
6765 (let ((start (or start
(point-min)))
6766 (end (or end
(point-max))))
6769 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6772 (goto-char (match-beginning 0))
6773 (funcall function
)))))))
6775 (defun org-hide-block-toggle-all ()
6776 "Toggle the visibility of all blocks in the current buffer."
6777 (org-block-map #'org-hide-block-toggle
))
6779 (defun org-hide-block-all ()
6780 "Fold all blocks in the current buffer."
6782 (org-show-block-all)
6783 (org-block-map #'org-hide-block-toggle-maybe
))
6785 (defun org-show-block-all ()
6786 "Unfold all blocks in the current buffer."
6788 (mapc 'delete-overlay org-hide-block-overlays
)
6789 (setq org-hide-block-overlays nil
))
6791 (defun org-hide-block-toggle-maybe ()
6792 "Toggle visibility of block at point."
6794 (let ((case-fold-search t
))
6796 (beginning-of-line 1)
6797 (looking-at org-block-regexp
))
6798 (progn (org-hide-block-toggle)
6799 t
) ;; to signal that we took action
6800 nil
))) ;; to signal that we did not
6802 (defun org-hide-block-toggle (&optional force
)
6803 "Toggle the visibility of the current block."
6807 (if (re-search-forward org-block-regexp nil t
)
6808 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6809 (end (match-end 0)) ;; end of entire body
6811 (if (memq t
(mapcar (lambda (overlay)
6812 (eq (overlay-get overlay
'invisible
)
6814 (overlays-at start
)))
6815 (if (or (not force
) (eq force
'off
))
6817 (when (member ov org-hide-block-overlays
)
6818 (setq org-hide-block-overlays
6819 (delq ov org-hide-block-overlays
)))
6820 (when (eq (overlay-get ov
'invisible
)
6822 (delete-overlay ov
)))
6823 (overlays-at start
)))
6824 (setq ov
(make-overlay start end
))
6825 (overlay-put ov
'invisible
'org-hide-block
)
6826 ;; make the block accessible to isearch
6828 ov
'isearch-open-invisible
6830 (when (member ov org-hide-block-overlays
)
6831 (setq org-hide-block-overlays
6832 (delq ov org-hide-block-overlays
)))
6833 (when (eq (overlay-get ov
'invisible
)
6835 (delete-overlay ov
))))
6836 (push ov org-hide-block-overlays
)))
6837 (error "Not looking at a source block"))))
6839 ;; org-tab-after-check-for-cycling-hook
6840 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6841 ;; Remove overlays when changing major mode
6842 (add-hook 'org-mode-hook
6843 (lambda () (org-add-hook 'change-major-mode-hook
6844 'org-show-block-all
'append
'local
)))
6848 (defvar org-goto-window-configuration nil
)
6849 (defvar org-goto-marker nil
)
6850 (defvar org-goto-map
6851 (let ((map (make-sparse-keymap)))
6852 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6853 (while (setq cmd
(pop cmds
))
6854 (substitute-key-definition cmd cmd map global-map
)))
6855 (suppress-keymap map
)
6856 (org-defkey map
"\C-m" 'org-goto-ret
)
6857 (org-defkey map
[(return)] 'org-goto-ret
)
6858 (org-defkey map
[(left)] 'org-goto-left
)
6859 (org-defkey map
[(right)] 'org-goto-right
)
6860 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6861 (org-defkey map
"\C-i" 'org-cycle
)
6862 (org-defkey map
[(tab)] 'org-cycle
)
6863 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6864 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6865 (if org-goto-auto-isearch
6866 (if (fboundp 'define-key-after
)
6867 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6869 (org-defkey map "q" 'org-goto-quit)
6870 (org-defkey map "n" 'outline-next-visible-heading)
6871 (org-defkey map "p" 'outline-previous-visible-heading)
6872 (org-defkey map "f" 'outline-forward-same-level)
6873 (org-defkey map "b" 'outline-backward-same-level)
6874 (org-defkey map "u" 'outline-up-heading))
6875 (org-defkey map "/" 'org-occur)
6876 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6877 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6878 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6879 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6880 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6883 (defconst org-goto-help
6884 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6885 RET=jump to location [Q]uit and return to previous location
6886 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6888 (defvar org-goto-start-pos) ; dynamically scoped parameter
6890 ;; FIXME: Docstring does not mention both interfaces
6891 (defun org-goto (&optional alternative-interface)
6892 "Look up a different location in the current file, keeping current visibility.
6894 When you want look-up or go to a different location in a
6895 document, the fastest way is often to fold the entire buffer and
6896 then dive into the tree. This method has the disadvantage, that
6897 the previous location will be folded, which may not be what you
6900 This command works around this by showing a copy of the current
6901 buffer in an indirect buffer, in overview mode. You can dive
6902 into the tree in that copy, use org-occur and incremental search
6903 to find a location. When pressing RET or `Q', the command
6904 returns to the original buffer in which the visibility is still
6905 unchanged. After RET it will also jump to the location selected
6906 in the indirect buffer and expose the headline hierarchy above.
6908 With a prefix argument, use the alternative interface: e.g. if
6909 `org-goto-interface' is 'outline use 'outline-path-completion."
6911 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6912 (org-refile-use-outline-path t)
6913 (org-refile-target-verify-function nil)
6915 (if (not alternative-interface)
6917 (if (eq org-goto-interface 'outline)
6918 'outline-path-completion
6920 (org-goto-start-pos (point))
6922 (if (eq interface 'outline)
6923 (car (org-get-location (current-buffer) org-goto-help))
6924 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6925 (org-refile-check-position pa)
6929 (org-mark-ring-push org-goto-start-pos)
6930 (goto-char selected-point)
6931 (if (or (outline-invisible-p) (org-invisible-p2))
6932 (org-show-context 'org-goto)))
6935 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6936 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6937 (defvar org-goto-local-auto-isearch-map) ; defined below
6939 (defun org-get-location (buf help)
6940 "Let the user select a location in the Org-mode buffer BUF.
6941 This function uses a recursive edit. It returns the selected position
6943 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6944 (isearch-hide-immediately nil)
6945 (isearch-search-fun-function
6946 (lambda () 'org-goto-local-search-headings))
6947 (org-goto-selected-point org-goto-exit-command)
6949 (special-display-buffer-names nil)
6950 (special-display-regexps nil)
6951 (special-display-function nil))
6953 (save-window-excursion
6954 (delete-other-windows)
6955 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6956 (org-pop-to-buffer-same-window
6958 (make-indirect-buffer (current-buffer) "*org-goto*")
6959 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6960 (with-output-to-temp-buffer "*Help*"
6962 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6963 (setq buffer-read-only nil)
6964 (let ((org-startup-truncated t)
6965 (org-startup-folded nil)
6966 (org-startup-align-all-tables nil))
6969 (setq buffer-read-only t)
6970 (if (and (boundp 'org-goto-start-pos)
6971 (integer-or-marker-p org-goto-start-pos))
6972 (let ((org-show-hierarchy-above t)
6973 (org-show-siblings t)
6974 (org-show-following-heading t))
6975 (goto-char org-goto-start-pos)
6976 (and (outline-invisible-p) (org-show-context)))
6977 (goto-char (point-min)))
6978 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6979 (message "Select location and press RET")
6980 (use-local-map org-goto-map)
6983 (kill-buffer "*org-goto*")
6984 (cons org-goto-selected-point org-goto-exit-command)))
6986 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6987 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6988 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6989 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6991 (defun org-goto-local-search-headings (string bound noerror)
6992 "Search and make sure that any matches are in headlines."
6994 (while (if isearch-forward
6995 (search-forward string bound noerror)
6996 (search-backward string bound noerror))
6997 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6998 (and (member :headline context)
6999 (not (member :tags context))))
7000 (throw 'return (point))))))
7002 (defun org-goto-local-auto-isearch ()
7005 (goto-char (point-min))
7006 (let ((keys (this-command-keys)))
7007 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
7009 (isearch-process-search-char (string-to-char keys)))))
7011 (defun org-goto-ret (&optional arg)
7012 "Finish `org-goto' by going to the new location."
7014 (setq org-goto-selected-point (point)
7015 org-goto-exit-command 'return)
7018 (defun org-goto-left ()
7019 "Finish `org-goto' by going to the new location."
7021 (if (org-at-heading-p)
7023 (beginning-of-line 1)
7024 (setq org-goto-selected-point (point)
7025 org-goto-exit-command 'left)
7027 (error "Not on a heading")))
7029 (defun org-goto-right ()
7030 "Finish `org-goto' by going to the new location."
7032 (if (org-at-heading-p)
7034 (setq org-goto-selected-point (point)
7035 org-goto-exit-command 'right)
7037 (error "Not on a heading")))
7039 (defun org-goto-quit ()
7040 "Finish `org-goto' without cursor motion."
7042 (setq org-goto-selected-point nil)
7043 (setq org-goto-exit-command 'quit)
7046 ;;; Indirect buffer display of subtrees
7048 (defvar org-indirect-dedicated-frame nil
7049 "This is the frame being used for indirect tree display.")
7050 (defvar org-last-indirect-buffer nil)
7052 (defun org-tree-to-indirect-buffer (&optional arg)
7053 "Create indirect buffer and narrow it to current subtree.
7054 With numerical prefix ARG, go up to this level and then take that tree.
7055 If ARG is negative, go up that many levels.
7056 If `org-indirect-buffer-display' is not `new-frame', the command removes the
7057 indirect buffer previously made with this command, to avoid proliferation of
7058 indirect buffers. However, when you call the command with a \
7059 \\[universal-argument] prefix, or
7060 when `org-indirect-buffer-display' is `new-frame', the last buffer
7061 is kept so that you can work with several indirect buffers at the same time.
7062 If `org-indirect-buffer-display' is `dedicated-frame', the \
7063 \\[universal-argument] prefix also
7064 requests that a new frame be made for the new buffer, so that the dedicated
7065 frame is not changed."
7067 (let ((cbuf (current-buffer))
7068 (cwin (selected-window))
7070 beg end level heading ibuf)
7072 (org-back-to-heading t)
7074 (setq level (org-outline-level))
7075 (if (< arg 0) (setq arg (+ level arg)))
7076 (while (> (setq level (org-outline-level)) arg)
7077 (outline-up-heading 1 t)))
7079 heading (org-get-heading))
7080 (org-end-of-subtree t t)
7081 (if (org-at-heading-p) (backward-char 1))
7083 (if (and (buffer-live-p org-last-indirect-buffer)
7084 (not (eq org-indirect-buffer-display 'new-frame))
7086 (kill-buffer org-last-indirect-buffer))
7087 (setq ibuf (org-get-indirect-buffer cbuf)
7088 org-last-indirect-buffer ibuf)
7090 ((or (eq org-indirect-buffer-display 'new-frame)
7091 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
7092 (select-frame (make-frame))
7093 (delete-other-windows)
7094 (org-pop-to-buffer-same-window ibuf)
7095 (org-set-frame-title heading))
7096 ((eq org-indirect-buffer-display 'dedicated-frame)
7098 (select-frame (or (and org-indirect-dedicated-frame
7099 (frame-live-p org-indirect-dedicated-frame)
7100 org-indirect-dedicated-frame)
7101 (setq org-indirect-dedicated-frame (make-frame)))))
7102 (delete-other-windows)
7103 (org-pop-to-buffer-same-window ibuf)
7104 (org-set-frame-title (concat "Indirect: " heading)))
7105 ((eq org-indirect-buffer-display 'current-window)
7106 (org-pop-to-buffer-same-window ibuf))
7107 ((eq org-indirect-buffer-display 'other-window)
7108 (pop-to-buffer ibuf))
7109 (t (error "Invalid value")))
7110 (if (featurep 'xemacs)
7111 (save-excursion (org-mode) (turn-on-font-lock)))
7112 (narrow-to-region beg end)
7115 (run-hook-with-args 'org-cycle-hook 'all)
7116 (and (window-live-p cwin) (select-window cwin))))
7118 (defun org-get-indirect-buffer (&optional buffer)
7119 (setq buffer (or buffer (current-buffer)))
7120 (let ((n 1) (base (buffer-name buffer)) bname)
7121 (while (buffer-live-p
7122 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7125 (make-indirect-buffer buffer bname 'clone)
7126 (error (make-indirect-buffer buffer bname)))))
7128 (defun org-set-frame-title (title)
7129 "Set the title of the current frame to the string TITLE."
7130 ;; FIXME: how to name a single frame in XEmacs???
7131 (unless (featurep 'xemacs)
7132 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7134 ;;;; Structure editing
7136 ;;; Inserting headlines
7138 (defun org-previous-line-empty-p ()
7141 (or (beginning-of-line 0) t)
7143 (looking-at "[ \t]*$")))))
7145 (defun org-insert-heading (&optional force-heading invisible-ok)
7146 "Insert a new heading or item with same depth at point.
7147 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7148 If point is at the beginning of a headline, insert a sibling before the
7149 current headline. If point is not at the beginning, split the line,
7150 create the new headline with the text in the current line after point
7151 \(but see also the variable `org-M-RET-may-split-line').
7153 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7154 This is important for non-interactive uses of the command."
7156 (if (or (= (buffer-size) 0)
7157 (and (not (save-excursion
7158 (and (ignore-errors (org-back-to-heading invisible-ok))
7159 (org-at-heading-p))))
7160 (or force-heading (not (org-in-item-p)))))
7163 (run-hooks 'org-insert-heading-hook))
7164 (when (or force-heading (not (org-insert-item)))
7165 (let* ((empty-line-p nil)
7167 (on-heading (org-at-heading-p))
7168 (head (save-excursion
7171 (org-back-to-heading invisible-ok)
7172 (when (and (not on-heading)
7173 (featurep 'org-inlinetask)
7174 (integerp org-inlinetask-min-level)
7175 (>= (length (match-string 0))
7176 org-inlinetask-min-level))
7177 ;; Find a heading level before the inline task
7178 (while (and (setq level (org-up-heading-safe))
7179 (>= level org-inlinetask-min-level)))
7180 (if (org-at-heading-p)
7181 (org-back-to-heading invisible-ok)
7182 (error "This should not happen")))
7183 (setq empty-line-p (org-previous-line-empty-p))
7186 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7187 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7188 pos hide-previous previous-pos)
7190 ((and (org-at-heading-p) (bolp)
7192 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7193 ;; insert before the current line
7194 (open-line (if blank 2 1)))
7196 (not org-insert-heading-respect-content)
7199 (backward-char 1) (not (outline-invisible-p)))))
7200 ;; insert right here
7203 ;; somewhere in the line
7205 (setq previous-pos (point-at-bol))
7207 (setq hide-previous (outline-invisible-p)))
7208 (and org-insert-heading-respect-content (org-show-subtree))
7210 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7213 (goto-char (point-at-bol))
7214 (and (looking-at org-complex-heading-regexp)
7216 (> p (match-beginning 4)))))))
7219 (org-insert-heading-respect-content
7220 (org-end-of-subtree nil t)
7221 (when (featurep 'org-inlinetask)
7222 (while (and (not (eobp))
7223 (looking-at "\\(\\*+\\)[ \t]+")
7224 (>= (length (match-string 1))
7225 org-inlinetask-min-level))
7226 (org-end-of-subtree nil t)))
7227 (or (bolp) (newline))
7228 (or (org-previous-line-empty-p)
7229 (and blank (newline)))
7235 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7236 (setq tags (and (match-end 2) (match-string 2)))
7238 (delete-region (match-beginning 1) (match-end 1)))
7239 (setq pos (point-at-bol))
7240 (or split (end-of-line 1))
7241 (delete-horizontal-space)
7242 (if (string-match "\\`\\*+\\'"
7243 (buffer-substring (point-at-bol) (point)))
7245 (newline (if blank 2 1))
7251 (org-set-tags nil 'align))))
7253 (or split (end-of-line 1))
7254 (newline (if blank 2 1)))))))
7255 (insert head) (just-one-space)
7258 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7259 (when (and org-insert-heading-respect-content hide-previous)
7261 (goto-char previous-pos)
7263 (run-hooks 'org-insert-heading-hook)))))
7265 (defun org-get-heading (&optional no-tags no-todo)
7266 "Return the heading of the current entry, without the stars.
7267 When NO-TAGS is non-nil, don't include tags.
7268 When NO-TODO is non-nil, don't include TODO keywords."
7270 (org-back-to-heading t)
7272 ((and no-tags no-todo)
7273 (looking-at org-complex-heading-regexp)
7276 (looking-at (concat org-outline-regexp
7278 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7281 (looking-at org-todo-line-regexp)
7283 (t (looking-at org-heading-regexp)
7284 (match-string 2)))))
7286 (defun org-heading-components ()
7287 "Return the components of the current heading.
7288 This is a list with the following elements:
7289 - the level as an integer
7290 - the reduced level, different if `org-odd-levels-only' is set.
7291 - the TODO keyword, or nil
7292 - the priority character, like ?A, or nil if no priority is given
7293 - the headline text itself, or the tags string if no headline text
7294 - the tags string, or nil."
7296 (org-back-to-heading t)
7297 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7298 (list (length (match-string 1))
7299 (org-reduced-level (length (match-string 1)))
7300 (org-match-string-no-properties 2)
7301 (and (match-end 3) (aref (match-string 3) 2))
7302 (org-match-string-no-properties 4)
7303 (org-match-string-no-properties 5)))))
7305 (defun org-get-entry ()
7306 "Get the entry text, after heading, entire subtree."
7308 (org-back-to-heading t)
7309 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7311 (defun org-insert-heading-after-current ()
7312 "Insert a new heading with same level as current, after current subtree."
7314 (org-back-to-heading)
7315 (org-insert-heading)
7316 (org-move-subtree-down)
7319 (defun org-insert-heading-respect-content ()
7321 (let ((org-insert-heading-respect-content t))
7322 (org-insert-heading t)))
7324 (defun org-insert-todo-heading-respect-content (&optional force-state)
7326 (let ((org-insert-heading-respect-content t))
7327 (org-insert-todo-heading force-state t)))
7329 (defun org-insert-todo-heading (arg &optional force-heading)
7330 "Insert a new heading with the same level and TODO state as current heading.
7331 If the heading has no TODO state, or if the state is DONE, use the first
7332 state (TODO by default). Also with prefix arg, force first state."
7334 (when (or force-heading (not (org-insert-item 'checkbox)))
7335 (org-insert-heading force-heading)
7337 (org-back-to-heading)
7338 (outline-previous-heading)
7339 (looking-at org-todo-line-regexp))
7343 (not (match-beginning 2))
7344 (member (match-string 2) org-done-keywords))
7345 (car org-todo-keywords-1)
7349 (run-hook-with-args-until-success
7350 'org-todo-get-default-hook new-mark-x nil)
7352 (beginning-of-line 1)
7353 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7354 (if org-treat-insert-todo-heading-as-state-change
7356 (insert new-mark " "))))
7357 (when org-provide-todo-statistics
7358 (org-update-parent-todo-statistics))))
7360 (defun org-insert-subheading (arg)
7361 "Insert a new subheading and demote it.
7362 Works for outline headings and for plain lists alike."
7364 (org-insert-heading arg)
7366 ((org-at-heading-p) (org-do-demote))
7367 ((org-at-item-p) (org-indent-item))))
7369 (defun org-insert-todo-subheading (arg)
7370 "Insert a new subheading with TODO keyword or checkbox and demote it.
7371 Works for outline headings and for plain lists alike."
7373 (org-insert-todo-heading arg)
7375 ((org-at-heading-p) (org-do-demote))
7376 ((org-at-item-p) (org-indent-item))))
7378 ;;; Promotion and Demotion
7380 (defvar org-after-demote-entry-hook nil
7381 "Hook run after an entry has been demoted.
7382 The cursor will be at the beginning of the entry.
7383 When a subtree is being demoted, the hook will be called for each node.")
7385 (defvar org-after-promote-entry-hook nil
7386 "Hook run after an entry has been promoted.
7387 The cursor will be at the beginning of the entry.
7388 When a subtree is being promoted, the hook will be called for each node.")
7390 (defun org-promote-subtree ()
7391 "Promote the entire subtree.
7392 See also `org-promote'."
7395 (org-with-limited-levels (org-map-tree 'org-promote)))
7396 (org-fix-position-after-promote))
7398 (defun org-demote-subtree ()
7399 "Demote the entire subtree. See `org-demote'.
7400 See also `org-promote'."
7403 (org-with-limited-levels (org-map-tree 'org-demote)))
7404 (org-fix-position-after-promote))
7407 (defun org-do-promote ()
7408 "Promote the current heading higher up the tree.
7409 If the region is active in `transient-mark-mode', promote all headings
7413 (if (org-region-active-p)
7414 (org-map-region 'org-promote (region-beginning) (region-end))
7416 (org-fix-position-after-promote))
7418 (defun org-do-demote ()
7419 "Demote the current heading lower down the tree.
7420 If the region is active in `transient-mark-mode', demote all headings
7424 (if (org-region-active-p)
7425 (org-map-region 'org-demote (region-beginning) (region-end))
7427 (org-fix-position-after-promote))
7429 (defun org-fix-position-after-promote ()
7430 "Make sure that after pro/demotion cursor position is right."
7431 (let ((pos (point)))
7432 (when (save-excursion
7433 (beginning-of-line 1)
7434 (looking-at org-todo-line-regexp)
7435 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7436 (cond ((eobp) (insert " "))
7437 ((eolp) (insert " "))
7438 ((equal (char-after) ?\ ) (forward-char 1))))))
7440 (defun org-current-level ()
7441 "Return the level of the current entry, or nil if before the first headline.
7442 The level is the number of stars at the beginning of the headline."
7444 (org-with-limited-levels
7445 (if (ignore-errors (org-back-to-heading t))
7446 (funcall outline-level)))))
7448 (defun org-get-previous-line-level ()
7449 "Return the outline depth of the last headline before the current line.
7450 Returns 0 for the first headline in the buffer, and nil if before the
7452 (let ((current-level (org-current-level))
7453 (prev-level (when (> (line-number-at-pos) 1)
7455 (beginning-of-line 0)
7456 (org-current-level)))))
7457 (cond ((null current-level) nil) ; Before first headline
7458 ((null prev-level) 0) ; At first headline
7461 (defun org-reduced-level (l)
7462 "Compute the effective level of a heading.
7463 This takes into account the setting of `org-odd-levels-only'."
7466 (org-odd-levels-only (1+ (floor (/ l 2))))
7469 (defun org-level-increment ()
7470 "Return the number of stars that will be added or removed at a
7471 time to headlines when structure editing, based on the value of
7472 `org-odd-levels-only'."
7473 (if org-odd-levels-only 2 1))
7475 (defun org-get-valid-level (level &optional change)
7476 "Rectify a level change under the influence of `org-odd-levels-only'
7477 LEVEL is a current level, CHANGE is by how much the level should be
7478 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7479 even level numbers will become the next higher odd number."
7480 (if org-odd-levels-only
7481 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7482 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7483 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7484 (max 1 (+ level (or change 0)))))
7486 (if (boundp 'define-obsolete-function-alias)
7487 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7488 (define-obsolete-function-alias 'org-get-legal-level
7489 'org-get-valid-level)
7490 (define-obsolete-function-alias 'org-get-legal-level
7491 'org-get-valid-level "23.1")))
7493 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7494 ;; ̀org-with-limited-levels'
7495 (defun org-promote ()
7496 "Promote the current heading higher up the tree.
7497 If the region is active in `transient-mark-mode', promote all headings
7499 (org-back-to-heading t)
7500 (let* ((level (save-match-data (funcall outline-level)))
7501 (after-change-functions (remove 'flyspell-after-change-function
7502 after-change-functions))
7503 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7504 (diff (abs (- level (length up-head) -1))))
7505 (cond ((and (= level 1) org-called-with-limited-levels
7506 org-allow-promoting-top-level-subtree)
7507 (replace-match "# " nil t))
7509 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7510 (t (replace-match up-head nil t)))
7511 ;; Fixup tag positioning
7513 (and org-auto-align-tags (org-set-tags nil t))
7514 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7515 (run-hooks 'org-after-promote-entry-hook)))
7517 (defun org-demote ()
7518 "Demote the current heading lower down the tree.
7519 If the region is active in `transient-mark-mode', demote all headings
7521 (org-back-to-heading t)
7522 (let* ((level (save-match-data (funcall outline-level)))
7523 (after-change-functions (remove 'flyspell-after-change-function
7524 after-change-functions))
7525 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7526 (diff (abs (- level (length down-head) -1))))
7527 (replace-match down-head nil t)
7528 ;; Fixup tag positioning
7529 (and org-auto-align-tags (org-set-tags nil t))
7530 (if org-adapt-indentation (org-fixup-indentation diff))
7531 (run-hooks 'org-after-demote-entry-hook)))
7533 (defun org-cycle-level ()
7534 "Cycle the level of an empty headline through possible states.
7535 This goes first to child, then to parent, level, then up the hierarchy.
7536 After top level, it switches back to sibling level."
7538 (let ((org-adapt-indentation nil))
7539 (when (org-point-at-end-of-empty-headline)
7540 (setq this-command 'org-cycle-level) ; Only needed for caching
7541 (let ((cur-level (org-current-level))
7542 (prev-level (org-get-previous-line-level)))
7544 ;; If first headline in file, promote to top-level.
7546 (loop repeat (/ (- cur-level 1) (org-level-increment))
7547 do (org-do-promote)))
7548 ;; If same level as prev, demote one.
7549 ((= prev-level cur-level)
7551 ;; If parent is top-level, promote to top level if not already.
7553 (loop repeat (/ (- cur-level 1) (org-level-increment))
7554 do (org-do-promote)))
7555 ;; If top-level, return to prev-level.
7557 (loop repeat (/ (- prev-level 1) (org-level-increment))
7558 do (org-do-demote)))
7559 ;; If less than prev-level, promote one.
7560 ((< cur-level prev-level)
7562 ;; If deeper than prev-level, promote until higher than
7564 ((> cur-level prev-level)
7565 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7566 do (org-do-promote))))
7569 (defun org-map-tree (fun)
7570 "Call FUN for every heading underneath the current one."
7571 (org-back-to-heading)
7572 (let ((level (funcall outline-level)))
7576 (outline-next-heading)
7577 (> (funcall outline-level) level))
7581 (defun org-map-region (fun beg end)
7582 "Call FUN for every heading between BEG and END."
7583 (let ((org-ignore-region t))
7585 (setq end (copy-marker end))
7587 (if (and (re-search-forward org-outline-regexp-bol nil t)
7591 (outline-next-heading)
7596 (defvar org-property-end-re) ; silence byte-compiler
7597 (defun org-fixup-indentation (diff)
7598 "Change the indentation in the current entry by DIFF.
7599 However, if any line in the current entry has no indentation, or if it
7600 would end up with no indentation after the change, nothing at all is done."
7602 (let ((end (save-excursion (outline-next-heading)
7604 (prohibit (if (> diff 0)
7606 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7608 (unless (save-excursion (end-of-line 1)
7609 (re-search-forward prohibit end t))
7610 (while (and (< (point) end)
7611 (re-search-forward "^[ \t]+" end t))
7612 (goto-char (match-end 0))
7613 (setq col (current-column))
7614 (if (< diff 0) (replace-match ""))
7615 (org-indent-to-column (+ diff col))))
7616 (move-marker end nil))))
7618 (defun org-convert-to-odd-levels ()
7619 "Convert an org-mode file with all levels allowed to one with odd levels.
7620 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7623 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7624 (let ((outline-level 'org-outline-level)
7625 (org-odd-levels-only nil) n)
7627 (goto-char (point-min))
7628 (while (re-search-forward "^\\*\\*+ " nil t)
7629 (setq n (- (length (match-string 0)) 2))
7630 (while (>= (setq n (1- n)) 0)
7632 (end-of-line 1))))))
7634 (defun org-convert-to-oddeven-levels ()
7635 "Convert an org-mode file with only odd levels to one with odd/even levels.
7636 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7637 file contains a section with an even level, conversion would
7638 destroy the structure of the file. An error is signaled in this
7641 (goto-char (point-min))
7642 ;; First check if there are no even levels
7643 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7644 (org-show-context t)
7645 (error "Not all levels are odd in this file. Conversion not possible"))
7646 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7647 (let ((outline-regexp org-outline-regexp)
7648 (outline-level 'org-outline-level)
7649 (org-odd-levels-only nil) n)
7651 (goto-char (point-min))
7652 (while (re-search-forward "^\\*\\*+ " nil t)
7653 (setq n (/ (1- (length (match-string 0))) 2))
7654 (while (>= (setq n (1- n)) 0)
7656 (end-of-line 1))))))
7658 (defun org-tr-level (n)
7659 "Make N odd if required."
7660 (if org-odd-levels-only (1+ (/ n 2)) n))
7662 ;;; Vertical tree motion, cutting and pasting of subtrees
7664 (defun org-move-subtree-up (&optional arg)
7665 "Move the current subtree up past ARG headlines of the same level."
7667 (org-move-subtree-down (- (prefix-numeric-value arg))))
7669 (defun org-move-subtree-down (&optional arg)
7670 "Move the current subtree down past ARG headlines of the same level."
7672 (setq arg (prefix-numeric-value arg))
7673 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7674 'org-get-last-sibling))
7675 (ins-point (make-marker))
7677 (col (current-column))
7678 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7680 (org-back-to-heading)
7683 (setq ne-beg (org-back-over-empty-lines))
7686 (save-excursion (outline-end-of-heading)
7687 (setq folded (outline-invisible-p)))
7688 (outline-end-of-subtree))
7689 (outline-next-heading)
7690 (setq ne-end (org-back-over-empty-lines))
7693 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7694 ;; include less whitespace
7697 (forward-line (- ne-beg ne-end))
7698 (setq beg (point))))
7699 ;; Find insertion point, with error handling
7701 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7702 (progn (goto-char beg0)
7703 (error "Cannot move past superior level or buffer limit")))
7704 (setq cnt (1- cnt)))
7706 ;; Moving forward - still need to move over subtree
7707 (progn (org-end-of-subtree t t)
7709 (org-back-over-empty-lines)
7710 (or (bolp) (newline)))))
7711 (setq ne-ins (org-back-over-empty-lines))
7712 (move-marker ins-point (point))
7713 (setq txt (buffer-substring beg end))
7714 (org-save-markers-in-region beg end)
7715 (delete-region beg end)
7716 (org-remove-empty-overlays-at beg)
7717 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7718 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7719 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7720 (let ((bbb (point)))
7721 (insert-before-markers txt)
7722 (org-reinstall-markers-in-region bbb)
7723 (move-marker ins-point bbb))
7724 (or (bolp) (insert "\n"))
7725 (setq ins-end (point))
7726 (goto-char ins-point)
7727 (org-skip-whitespace)
7728 (when (and (< arg 0)
7729 (org-first-sibling-p)
7731 ;; Move whitespace back to beginning
7734 (let ((kill-whole-line t))
7735 (kill-line (- ne-ins ne-beg)) (point)))
7736 (insert (make-string (- ne-ins ne-beg) ?\n)))
7737 (move-marker ins-point nil)
7742 (org-cycle-hide-drawers 'children))
7743 (org-clean-visibility-after-subtree-move)
7744 ;; move back to the initial column we were at
7745 (move-to-column col)))
7747 (defvar org-subtree-clip ""
7748 "Clipboard for cut and paste of subtrees.
7749 This is actually only a copy of the kill, because we use the normal kill
7750 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7752 (defvar org-subtree-clip-folded nil
7753 "Was the last copied subtree folded?
7754 This is used to fold the tree back after pasting.")
7756 (defun org-cut-subtree (&optional n)
7757 "Cut the current subtree into the clipboard.
7758 With prefix arg N, cut this many sequential subtrees.
7759 This is a short-hand for marking the subtree and then cutting it."
7761 (org-copy-subtree n 'cut))
7763 (defun org-copy-subtree (&optional n cut force-store-markers)
7764 "Cut the current subtree into the clipboard.
7765 With prefix arg N, cut this many sequential subtrees.
7766 This is a short-hand for marking the subtree and then copying it.
7767 If CUT is non-nil, actually cut the subtree.
7768 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7769 of some markers in the region, even if CUT is non-nil. This is
7770 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7772 (let (beg end folded (beg0 (point)))
7773 (if (org-called-interactively-p 'any)
7774 (org-back-to-heading nil) ; take what looks like a subtree
7775 (org-back-to-heading t)) ; take what is really there
7776 (org-back-over-empty-lines)
7778 (skip-chars-forward " \t\r\n")
7780 (save-excursion (outline-end-of-heading)
7781 (setq folded (outline-invisible-p)))
7783 (org-forward-same-level (1- n) t)
7785 (org-end-of-subtree t t))
7786 (org-back-over-empty-lines)
7790 (setq org-subtree-clip-folded folded)
7791 (when (or cut force-store-markers)
7792 (org-save-markers-in-region beg end))
7793 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7794 (setq org-subtree-clip (current-kill 0))
7795 (message "%s: Subtree(s) with %d characters"
7796 (if cut "Cut" "Copied")
7797 (length org-subtree-clip)))))
7799 (defun org-paste-subtree (&optional level tree for-yank)
7800 "Paste the clipboard as a subtree, with modification of headline level.
7801 The entire subtree is promoted or demoted in order to match a new headline
7804 If the cursor is at the beginning of a headline, the same level as
7805 that headline is used to paste the tree
7807 If not, the new level is derived from the *visible* headings
7808 before and after the insertion point, and taken to be the inferior headline
7809 level of the two. So if the previous visible heading is level 3 and the
7810 next is level 4 (or vice versa), level 4 will be used for insertion.
7811 This makes sure that the subtree remains an independent subtree and does
7812 not swallow low level entries.
7814 You can also force a different level, either by using a numeric prefix
7815 argument, or by inserting the heading marker by hand. For example, if the
7816 cursor is after \"*****\", then the tree will be shifted to level 5.
7818 If optional TREE is given, use this text instead of the kill ring.
7820 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7821 move back over whitespace before inserting, and move point to the end of
7822 the inserted text when done."
7824 (setq tree (or tree (and kill-ring (current-kill 0))))
7825 (unless (org-kill-is-subtree-p tree)
7827 (substitute-command-keys
7828 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7829 (org-with-limited-levels
7830 (let* ((visp (not (outline-invisible-p)))
7832 (^re_ "\\(\\*+\\)[ \t]*")
7833 (old-level (if (string-match org-outline-regexp-bol txt)
7834 (- (match-end 0) (match-beginning 0) 1)
7836 (force-level (cond (level (prefix-numeric-value level))
7837 ((and (looking-at "[ \t]*$")
7839 "^\\*+$" (buffer-substring
7840 (point-at-bol) (point))))
7841 (- (match-end 1) (match-beginning 1)))
7843 (looking-at org-outline-regexp))
7844 (- (match-end 0) (point) 1))
7846 (previous-level (save-excursion
7849 (outline-previous-visible-heading 1)
7850 (if (looking-at ^re_)
7851 (- (match-end 0) (match-beginning 0) 1)
7854 (next-level (save-excursion
7857 (or (looking-at org-outline-regexp)
7858 (outline-next-visible-heading 1))
7859 (if (looking-at ^re_)
7860 (- (match-end 0) (match-beginning 0) 1)
7863 (new-level (or force-level (max previous-level next-level)))
7864 (shift (if (or (= old-level -1)
7866 (= old-level new-level))
7868 (- new-level old-level)))
7869 (delta (if (> shift 0) -1 1))
7870 (func (if (> shift 0) 'org-demote 'org-promote))
7871 (org-odd-levels-only nil)
7873 ;; Remove the forced level indicator
7875 (delete-region (point-at-bol) (point)))
7877 (beginning-of-line (if (bolp) 1 2))
7878 (unless for-yank (org-back-over-empty-lines))
7880 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7881 (insert-before-markers txt)
7882 (unless (string-match "\n\\'" txt) (insert "\n"))
7883 (setq newend (point))
7884 (org-reinstall-markers-in-region beg)
7887 (skip-chars-forward " \t\n\r")
7889 (if (and (outline-invisible-p) visp)
7890 (save-excursion (outline-show-heading)))
7891 ;; Shift if necessary
7894 (narrow-to-region beg end)
7895 (while (not (= shift 0))
7896 (org-map-region func (point-min) (point-max))
7897 (setq shift (+ delta shift)))
7898 (goto-char (point-min))
7899 (setq newend (point-max))))
7900 (when (or (org-called-interactively-p 'interactive) for-yank)
7901 (message "Clipboard pasted as level %d subtree" new-level))
7902 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7904 (eq org-subtree-clip (current-kill 0))
7905 org-subtree-clip-folded)
7906 ;; The tree was folded before it was killed/copied
7908 (and for-yank (goto-char newend)))))
7910 (defun org-kill-is-subtree-p (&optional txt)
7911 "Check if the current kill is an outline subtree, or a set of trees.
7912 Returns nil if kill does not start with a headline, or if the first
7913 headline level is not the largest headline level in the tree.
7914 So this will actually accept several entries of equal levels as well,
7915 which is OK for `org-paste-subtree'.
7916 If optional TXT is given, check this string instead of the current kill."
7917 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7918 (re (org-get-limited-outline-regexp))
7919 (^re (concat "^" re))
7920 (start-level (and kill
7922 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7924 (- (match-end 2) (match-beginning 2) 1)))
7925 (start (1+ (or (match-beginning 2) -1))))
7926 (if (not start-level)
7928 nil) ;; does not even start with a heading
7930 (while (setq start (string-match ^re kill (1+ start)))
7931 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7935 (defvar org-markers-to-move nil
7936 "Markers that should be moved with a cut-and-paste operation.
7937 Those markers are stored together with their positions relative to
7938 the start of the region.")
7940 (defun org-save-markers-in-region (beg end)
7941 "Check markers in region.
7942 If these markers are between BEG and END, record their position relative
7943 to BEG, so that after moving the block of text, we can put the markers back
7945 This function gets called just before an entry or tree gets cut from the
7946 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7947 called immediately, to move the markers with the entries."
7948 (setq org-markers-to-move nil)
7949 (when (featurep 'org-clock)
7950 (org-clock-save-markers-for-cut-and-paste beg end))
7951 (when (featurep 'org-agenda)
7952 (org-agenda-save-markers-for-cut-and-paste beg end)))
7954 (defun org-check-and-save-marker (marker beg end)
7955 "Check if MARKER is between BEG and END.
7956 If yes, remember the marker and the distance to BEG."
7957 (when (and (marker-buffer marker)
7958 (equal (marker-buffer marker) (current-buffer)))
7959 (if (and (>= marker beg) (< marker end))
7960 (push (cons marker (- marker beg)) org-markers-to-move))))
7962 (defun org-reinstall-markers-in-region (beg)
7963 "Move all remembered markers to their position relative to BEG."
7965 (move-marker (car x) (+ beg (cdr x))))
7966 org-markers-to-move)
7967 (setq org-markers-to-move nil))
7969 (defun org-narrow-to-subtree ()
7970 "Narrow buffer to the current subtree."
7974 (org-with-limited-levels
7976 (progn (org-back-to-heading t) (point))
7977 (progn (org-end-of-subtree t t)
7978 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
7981 (defun org-narrow-to-block ()
7982 "Narrow buffer to the current block."
7984 (let* ((case-fold-search t)
7985 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
7986 "^[ \t]*#\\+end_.*")))
7988 (narrow-to-region (car blockp) (cdr blockp))
7989 (error "Not in a block"))))
7992 (defvar org-property-drawer-re))
7994 (defvar org-property-start-re) ;; defined below
7995 (defun org-clone-subtree-with-time-shift (n &optional shift)
7996 "Clone the task (subtree) at point N times.
7997 The clones will be inserted as siblings.
7999 In interactive use, the user will be prompted for the number of
8000 clones to be produced, and for a time SHIFT, which may be a
8001 repeater as used in time stamps, for example `+3d'.
8003 When a valid repeater is given and the entry contains any time
8004 stamps, the clones will become a sequence in time, with time
8005 stamps in the subtree shifted for each clone produced. If SHIFT
8006 is nil or the empty string, time stamps will be left alone. The
8007 ID property of the original subtree is removed.
8009 If the original subtree did contain time stamps with a repeater,
8010 the following will happen:
8011 - the repeater will be removed in each clone
8012 - an additional clone will be produced, with the current, unshifted
8013 date(s) in the entry.
8014 - the original entry will be placed *after* all the clones, with
8016 - the start days in the repeater in the original entry will be shifted
8017 to past the last clone.
8018 In this way you can spell out a number of instances of a repeating task,
8019 and still retain the repeater to cover future instances of the task."
8020 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
8021 (let (beg end template task idprop
8022 shift-n shift-what doshift nmin nmax (n-no-remove -1)
8023 (drawer-re org-drawer-regexp))
8024 (if (not (and (integerp n) (> n 0)))
8025 (error "Invalid number of replications %s" n))
8026 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
8027 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
8029 (error "Invalid shift specification %s" shift))
8031 (setq shift-n (string-to-number (match-string 1 shift))
8032 shift-what (cdr (assoc (match-string 2 shift)
8033 '(("d" . day) ("w" . week)
8034 ("m" . month) ("y" . year))))))
8035 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
8036 (setq nmin 1 nmax n)
8037 (org-back-to-heading t)
8039 (setq idprop (org-entry-get nil "ID"))
8040 (org-end-of-subtree t t)
8041 (or (bolp) (insert "\n"))
8043 (setq template (buffer-substring beg end))
8045 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
8046 (delete-region beg end)
8048 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
8050 (loop for n from nmin to nmax do
8055 (goto-char (point-min))
8057 (and idprop (if org-clone-delete-id
8058 (org-entry-delete nil "ID")
8059 (org-id-get-create t)))
8061 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
8063 (goto-char (point-min))
8064 (while (re-search-forward drawer-re nil t)
8066 (org-remove-empty-drawer-at d (point))) org-drawers)))
8067 (goto-char (point-min))
8069 (while (re-search-forward org-ts-regexp-both nil t)
8070 (org-timestamp-change (* n shift-n) shift-what))
8071 (unless (= n n-no-remove)
8072 (goto-char (point-min))
8073 (while (re-search-forward org-ts-regexp nil t)
8075 (goto-char (match-beginning 0))
8076 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
8077 (delete-region (match-beginning 1) (match-end 1)))))))
8078 (setq task (buffer-string)))
8084 (defun org-sort (with-case)
8085 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
8086 Optional argument WITH-CASE means sort case-sensitively."
8089 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
8090 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
8092 (org-call-with-arg 'org-sort-entries with-case))))
8094 (defun org-sort-remove-invisible (s)
8095 (remove-text-properties 0 (length s) org-rm-props s)
8096 (while (string-match org-bracket-link-regexp s)
8097 (setq s (replace-match (if (match-end 2)
8099 (match-string 1 s)) t t s)))
8102 (defvar org-priority-regexp) ; defined later in the file
8104 (defvar org-after-sorting-entries-or-items-hook nil
8105 "Hook that is run after a bunch of entries or items have been sorted.
8106 When children are sorted, the cursor is in the parent line when this
8107 hook gets called. When a region or a plain list is sorted, the cursor
8108 will be in the first entry of the sorted region/list.")
8110 (defun org-sort-entries
8111 (&optional with-case sorting-type getkey-func compare-func property)
8112 "Sort entries on a certain level of an outline tree.
8113 If there is an active region, the entries in the region are sorted.
8114 Else, if the cursor is before the first entry, sort the top-level items.
8115 Else, the children of the entry at point are sorted.
8117 Sorting can be alphabetically, numerically, by date/time as given by
8118 a time stamp, by a property or by priority.
8120 The command prompts for the sorting type unless it has been given to the
8121 function through the SORTING-TYPE argument, which needs to be a character,
8122 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
8123 precise meaning of each character:
8125 n Numerically, by converting the beginning of the entry/item to a number.
8126 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8127 t By date/time, either the first active time stamp in the entry, or, if
8128 none exist, by the first inactive one.
8129 s By the scheduled date/time.
8130 d By deadline date/time.
8131 c By creation time, which is assumed to be the first inactive time stamp
8132 at the beginning of a line.
8133 p By priority according to the cookie.
8134 r By the value of a property.
8136 Capital letters will reverse the sort order.
8138 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8139 called with point at the beginning of the record. It must return either
8140 a string or a number that should serve as the sorting key for that record.
8142 Comparing entries ignores case by default. However, with an optional argument
8143 WITH-CASE, the sorting considers case as well."
8145 (let ((case-func (if with-case 'identity 'downcase))
8146 start beg end stars re re2
8148 ;; Find beginning and end of region to sort
8150 ((org-region-active-p)
8151 ;; we will sort the region
8152 (setq end (region-end)
8154 (goto-char (region-beginning))
8155 (if (not (org-at-heading-p)) (outline-next-heading))
8156 (setq start (point)))
8157 ((or (org-at-heading-p)
8158 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8159 ;; we will sort the children of the current headline
8160 (org-back-to-heading)
8162 end (progn (org-end-of-subtree t t)
8163 (or (bolp) (insert "\n"))
8164 (org-back-over-empty-lines)
8169 (outline-next-heading))
8171 ;; we will sort the top-level entries in this file
8172 (goto-char (point-min))
8173 (or (org-at-heading-p) (outline-next-heading))
8174 (setq start (point))
8175 (goto-char (point-max))
8176 (beginning-of-line 1)
8177 (when (looking-at ".*?\\S-")
8178 ;; File ends in a non-white line
8181 (setq end (point-max))
8182 (setq what "top-level")
8187 (if (>= beg end) (error "Nothing to sort"))
8189 (looking-at "\\(\\*+\\)")
8190 (setq stars (match-string 1)
8191 re (concat "^" (regexp-quote stars) " +")
8192 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8193 txt (buffer-substring beg end))
8194 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8195 (if (and (not (equal stars "*")) (string-match re2 txt))
8196 (error "Region to sort contains a level above the first entry"))
8198 (unless sorting-type
8200 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8201 [t]ime
[s]cheduled [d]eadline [c]reated
8202 A/N/T/S/D/C/P/O/F means reversed:"
8204 (setq sorting-type (read-char-exclusive))
8206 (and (= (downcase sorting-type) ?f)
8208 (org-icompleting-read "Sort using function: "
8209 obarray 'fboundp t nil nil))
8210 (setq getkey-func (intern getkey-func)))
8212 (and (= (downcase sorting-type) ?r)
8214 (org-icompleting-read "Property: "
8215 (mapcar 'list (org-buffer-property-keys t))
8218 (message "Sorting entries...")
8221 (narrow-to-region start end)
8222 (let ((dcst (downcase sorting-type))
8223 (case-fold-search nil)
8224 (now (current-time)))
8226 (/= dcst sorting-type)
8227 ;; This function moves to the beginning character of the "record" to
8230 (if (re-search-forward re nil t)
8231 (goto-char (match-beginning 0))
8232 (goto-char (point-max))))
8233 ;; This function moves to the last character of the "record" being
8238 (outline-forward-same-level 1)
8240 (goto-char (point-max))))))
8241 ;; This function returns the value that gets sorted against.
8245 (if (looking-at org-complex-heading-regexp)
8246 (string-to-number (match-string 4))
8249 (if (looking-at org-complex-heading-regexp)
8250 (funcall case-func (match-string 4))
8253 (let ((end (save-excursion (outline-next-heading) (point))))
8254 (if (or (re-search-forward org-ts-regexp end t)
8255 (re-search-forward org-ts-regexp-both end t))
8256 (org-time-string-to-seconds (match-string 0))
8257 (org-float-time now))))
8259 (let ((end (save-excursion (outline-next-heading) (point))))
8260 (if (re-search-forward
8261 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8263 (org-time-string-to-seconds (match-string 0))
8264 (org-float-time now))))
8266 (let ((end (save-excursion (outline-next-heading) (point))))
8267 (if (re-search-forward org-scheduled-time-regexp end t)
8268 (org-time-string-to-seconds (match-string 1))
8269 (org-float-time now))))
8271 (let ((end (save-excursion (outline-next-heading) (point))))
8272 (if (re-search-forward org-deadline-time-regexp end t)
8273 (org-time-string-to-seconds (match-string 1))
8274 (org-float-time now))))
8276 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8277 (string-to-char (match-string 2))
8278 org-default-priority))
8280 (or (org-entry-get nil property) ""))
8282 (if (looking-at org-complex-heading-regexp)
8283 (- 9999 (length (member (match-string 2)
8284 org-todo-keywords-1)))))
8288 (setq tmp (funcall getkey-func))
8289 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8291 (error "Invalid key function `%s'" getkey-func)))
8292 (t (error "Invalid sorting type `%c'" sorting-type))))
8295 ((= dcst ?a) 'string<)
8296 ((= dcst ?f) compare-func)
8297 ((member dcst '(?p ?t ?s ?d ?c)) '<)
8299 (run-hooks 'org-after-sorting-entries-or-items-hook)
8300 (message "Sorting entries...done")))
8302 (defun org-do-sort (table what &optional with-case sorting-type)
8303 "Sort TABLE of WHAT according to SORTING-TYPE.
8304 The user will be prompted for the SORTING-TYPE if the call to this
8305 function does not specify it. WHAT is only for the prompt, to indicate
8306 what is being sorted. The sorting key will be extracted from
8307 the car of the elements of the table.
8308 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8309 (unless sorting-type
8311 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
8313 (setq sorting-type (read-char-exclusive)))
8314 (let ((dcst (downcase sorting-type))
8315 extractfun comparefun)
8316 ;; Define the appropriate functions
8319 (setq extractfun 'string-to-number
8320 comparefun (if (= dcst sorting-type) '< '>)))
8322 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8323 (lambda(x) (downcase (org-sort-remove-invisible x))))
8324 comparefun (if (= dcst sorting-type)
8326 (lambda (a b) (and (not (string< a b))
8327 (not (string= a b)))))))
8331 (if (or (string-match org-ts-regexp x)
8332 (string-match org-ts-regexp-both x))
8334 (org-time-string-to-time (match-string 0 x)))
8336 comparefun (if (= dcst sorting-type) '< '>)))
8337 (t (error "Invalid sorting type `%c'" sorting-type)))
8339 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8341 (lambda (a b) (funcall comparefun (car a) (car b))))))
8344 ;;; The orgstruct minor mode
8346 ;; Define a minor mode which can be used in other modes in order to
8347 ;; integrate the org-mode structure editing commands.
8349 ;; This is really a hack, because the org-mode structure commands use
8350 ;; keys which normally belong to the major mode. Here is how it
8351 ;; works: The minor mode defines all the keys necessary to operate the
8352 ;; structure commands, but wraps the commands into a function which
8353 ;; tests if the cursor is currently at a headline or a plain list
8354 ;; item. If that is the case, the structure command is used,
8355 ;; temporarily setting many Org-mode variables like regular
8356 ;; expressions for filling etc. However, when any of those keys is
8357 ;; used at a different location, function uses `key-binding' to look
8358 ;; up if the key has an associated command in another currently active
8359 ;; keymap (minor modes, major mode, global), and executes that
8360 ;; command. There might be problems if any of the keys is otherwise
8361 ;; used as a prefix key.
8363 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8364 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8365 ;; addresses this by checking explicitly for both bindings.
8367 (defvar orgstruct-mode-map (make-sparse-keymap)
8368 "Keymap for the minor `orgstruct-mode'.")
8370 (defvar org-local-vars nil
8371 "List of local variables, for use by `orgstruct-mode'.")
8374 (define-minor-mode orgstruct-mode
8375 "Toggle the minor mode `orgstruct-mode'.
8376 This mode is for using Org-mode structure commands in other
8377 modes. The following keys behave as if Org-mode were active, if
8378 the cursor is on a headline, or on a plain list item (both as
8379 defined by Org-mode).
8381 M-up Move entry/item up
8382 M-down Move entry/item down
8385 M-S-up Move entry/item up
8386 M-S-down Move entry/item down
8387 M-S-left Promote subtree
8388 M-S-right Demote subtree
8389 M-q Fill paragraph and items like in Org-mode
8391 C-c - Cycle list bullet
8392 TAB Cycle item visibility
8393 M-RET Insert new heading/item
8394 S-M-RET Insert new TODO heading / Checkbox item
8395 C-c C-c Set tags / toggle checkbox"
8396 nil " OrgStruct" nil
8397 (org-load-modules-maybe)
8398 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8401 (defun turn-on-orgstruct ()
8402 "Unconditionally turn on `orgstruct-mode'."
8405 (defvar org-fb-vars nil)
8406 (make-variable-buffer-local 'org-fb-vars)
8407 (defun orgstruct++-mode (&optional arg)
8408 "Toggle `orgstruct-mode', the enhanced version of it.
8409 In addition to setting orgstruct-mode, this also exports all
8410 indentation and autofilling variables from org-mode into the
8411 buffer. It will also recognize item context in multiline items."
8413 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8415 (progn (orgstruct-mode -1)
8417 (org-set-local (car v)
8418 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8421 (setq org-fb-vars nil)
8426 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8427 (symbol-name (car x)))
8428 (setq var (car x) val (nth 1 x))
8429 (push (list var `(quote ,(eval var))) org-fb-vars)
8430 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8432 (org-set-local 'orgstruct-is-++ t))))
8434 (defvar orgstruct-is-++ nil
8435 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8436 (make-variable-buffer-local 'orgstruct-is-++)
8439 (defun turn-on-orgstruct++ ()
8440 "Unconditionally turn on `orgstruct++-mode'."
8441 (orgstruct++-mode 1))
8443 (defun orgstruct-error ()
8444 "Error when there is no default binding for a structure key."
8446 (error "This key has no function outside structure elements"))
8448 (defun orgstruct-setup ()
8449 "Setup orgstruct keymaps."
8453 '([(meta up)] org-metaup)
8454 '([(meta down)] org-metadown)
8455 '([(meta left)] org-metaleft)
8456 '([(meta right)] org-metaright)
8457 '([(meta shift up)] org-shiftmetaup)
8458 '([(meta shift down)] org-shiftmetadown)
8459 '([(meta shift left)] org-shiftmetaleft)
8460 '([(meta shift right)] org-shiftmetaright)
8461 '([?\e (up)] org-metaup)
8462 '([?\e (down)] org-metadown)
8463 '([?\e (left)] org-metaleft)
8464 '([?\e (right)] org-metaright)
8465 '([?\e (shift up)] org-shiftmetaup)
8466 '([?\e (shift down)] org-shiftmetadown)
8467 '([?\e (shift left)] org-shiftmetaleft)
8468 '([?\e (shift right)] org-shiftmetaright)
8469 '([(shift up)] org-shiftup)
8470 '([(shift down)] org-shiftdown)
8471 '([(shift left)] org-shiftleft)
8472 '([(shift right)] org-shiftright)
8473 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8474 '("\M-q" fill-paragraph)
8476 '("\C-c-" org-cycle-list-bullet)))
8478 (while (setq elt (pop bindings))
8479 (setq nfunc (1+ nfunc))
8480 (setq key (org-key (car elt))
8482 cmd (orgstruct-make-binding fun nfunc key))
8483 (org-defkey orgstruct-mode-map key cmd))
8485 ;; Special treatment needed for TAB and RET
8486 (org-defkey orgstruct-mode-map [(tab)]
8487 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8488 (org-defkey orgstruct-mode-map "\C-i"
8489 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8491 (org-defkey orgstruct-mode-map "\M-\C-m"
8492 (orgstruct-make-binding 'org-insert-heading 105
8493 "\M-\C-m" [(meta return)]))
8494 (org-defkey orgstruct-mode-map [(meta return)]
8495 (orgstruct-make-binding 'org-insert-heading 106
8496 [(meta return)] "\M-\C-m"))
8498 (org-defkey orgstruct-mode-map [(shift meta return)]
8499 (orgstruct-make-binding 'org-insert-todo-heading 107
8500 [(meta return)] "\M-\C-m"))
8502 (org-defkey orgstruct-mode-map "\e\C-m"
8503 (orgstruct-make-binding 'org-insert-heading 108
8504 "\e\C-m" [?\e (return)]))
8505 (org-defkey orgstruct-mode-map [?\e (return)]
8506 (orgstruct-make-binding 'org-insert-heading 109
8507 [?\e (return)] "\e\C-m"))
8508 (org-defkey orgstruct-mode-map [?\e (shift return)]
8509 (orgstruct-make-binding 'org-insert-todo-heading 110
8510 [?\e (return)] "\e\C-m"))
8512 (unless org-local-vars
8513 (setq org-local-vars (org-get-local-variables)))
8517 (defun orgstruct-make-binding (fun n &rest keys)
8518 "Create a function for binding in the structure minor mode.
8519 FUN is the command to call inside a table. N is used to create a unique
8520 command name. KEYS are keys that should be checked in for a command
8521 to execute outside of tables."
8524 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8526 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8527 "Outside of structure, run the binding of `"
8528 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8532 `(org-context-p 'headline 'item
8533 (and orgstruct-is-++
8534 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8536 (list 'org-run-like-in-org-mode (list 'quote fun))
8537 (list 'let '(orgstruct-mode)
8538 (list 'call-interactively
8541 (list 'key-binding k))
8543 '('orgstruct-error))))))))
8545 (defun org-context-p (&rest contexts)
8546 "Check if local context is any of CONTEXTS.
8547 Possible values in the list of contexts are `table', `headline', and `item'."
8548 (let ((pos (point)))
8549 (goto-char (point-at-bol))
8550 (prog1 (or (and (memq 'table contexts)
8551 (looking-at "[ \t]*|"))
8552 (and (memq 'headline contexts)
8553 (looking-at org-outline-regexp))
8554 (and (memq 'item contexts)
8555 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8556 (and (memq 'item-body contexts)
8560 (defun org-get-local-variables ()
8561 "Return a list of all local variables in an Org mode buffer."
8563 (with-current-buffer (get-buffer-create "*Org tmp*")
8566 (setq varlist (buffer-local-variables)))
8567 (kill-buffer "*Org tmp*")
8574 (list (car x) (list 'quote (cdr x)))))
8576 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8577 (symbol-name (car x)))
8581 (defun org-clone-local-variables (from-buffer &optional regexp)
8582 "Clone local variables from FROM-BUFFER.
8583 Optional argument REGEXP selects variables to clone."
8586 (and (symbolp (car pair))
8588 (string-match regexp (symbol-name (car pair))))
8589 (set (make-local-variable (car pair))
8591 (buffer-local-variables from-buffer)))
8594 (defun org-run-like-in-org-mode (cmd)
8595 "Run a command, pretending that the current buffer is in Org-mode.
8596 This will temporarily bind local variables that are typically bound in
8597 Org-mode to the values they have in Org-mode, and then interactively
8599 (org-load-modules-maybe)
8600 (unless org-local-vars
8601 (setq org-local-vars (org-get-local-variables)))
8602 (eval (list 'let org-local-vars
8603 (list 'call-interactively (list 'quote cmd)))))
8607 (defun org-get-category (&optional pos force-refresh)
8608 "Get the category applying to position POS."
8610 (if force-refresh (org-refresh-category-properties))
8611 (let ((pos (or pos (point))))
8612 (or (get-text-property pos 'org-category)
8613 (progn (org-refresh-category-properties)
8614 (get-text-property pos 'org-category))))))
8616 (defun org-refresh-category-properties ()
8617 "Refresh category text properties in the buffer."
8618 (let ((inhibit-read-only t)
8620 ((null org-category)
8621 (if buffer-file-name
8622 (file-name-sans-extension
8623 (file-name-nondirectory buffer-file-name))
8625 ((symbolp org-category) (symbol-name org-category))
8627 beg end cat pos optionp)
8632 (goto-char (point-min))
8633 (put-text-property (point) (point-max) 'org-category def-cat)
8634 (while (re-search-forward
8635 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8636 (setq pos (match-end 0)
8637 optionp (equal (char-after (match-beginning 0)) ?#)
8638 cat (org-trim (match-string 2)))
8640 (setq beg (point-at-bol) end (point-max))
8641 (org-back-to-heading t)
8642 (setq beg (point) end (org-end-of-subtree t t)))
8643 (put-text-property beg end 'org-category cat)
8644 (put-text-property beg end 'org-category-position beg)
8645 (goto-char pos)))))))
8650 ;;; Link abbreviations
8652 (defun org-link-expand-abbrev (link)
8653 "Apply replacements as defined in `org-link-abbrev-alist'."
8654 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8655 (let* ((key (match-string 1 link))
8656 (as (or (assoc key org-link-abbrev-alist-local)
8657 (assoc key org-link-abbrev-alist)))
8658 (tag (and (match-end 2) (match-string 3 link)))
8664 ((symbolp rpl) (funcall rpl tag))
8665 ((string-match "%(\\([^)]+\\))" rpl)
8666 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
8667 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8668 ((string-match "%h" rpl)
8669 (replace-match (url-hexify-string (or tag "")) t t rpl))
8670 (t (concat rpl tag)))))
8673 ;;; Storing and inserting links
8675 (defvar org-insert-link-history nil
8676 "Minibuffer history for links inserted with `org-insert-link'.")
8678 (defvar org-stored-links nil
8679 "Contains the links stored with `org-store-link'.")
8681 (defvar org-store-link-plist nil
8682 "Plist with info about the most recently link created with `org-store-link'.")
8684 (defvar org-link-protocols nil
8685 "Link protocols added to Org-mode using `org-add-link-type'.")
8687 (defvar org-store-link-functions nil
8688 "List of functions that are called to create and store a link.
8689 Each function will be called in turn until one returns a non-nil
8690 value. Each function should check if it is responsible for creating
8691 this link (for example by looking at the major mode).
8692 If not, it must exit and return nil.
8693 If yes, it should return a non-nil value after a calling
8694 `org-store-link-props' with a list of properties and values.
8695 Special properties are:
8697 :type The link prefix, like \"http\". This must be given.
8698 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8699 This is obligatory as well.
8700 :description Optional default description for the second pair
8701 of brackets in an Org-mode link. The user can still change
8702 this when inserting this link into an Org-mode buffer.
8704 In addition to these, any additional properties can be specified
8705 and then used in remember templates.")
8707 (defun org-add-link-type (type &optional follow export)
8708 "Add TYPE to the list of `org-link-types'.
8709 Re-compute all regular expressions depending on `org-link-types'
8711 FOLLOW and EXPORT are two functions.
8713 FOLLOW should take the link path as the single argument and do whatever
8714 is necessary to follow the link, for example find a file or display
8717 EXPORT should format the link path for export to one of the export formats.
8718 It should be a function accepting three arguments:
8720 path the path of the link, the text after the prefix (like \"http:\")
8721 desc the description of the link, if any, or a description added by
8722 org-export-normalize-links if there is none
8723 format the export format, a symbol like `html' or `latex' or `ascii'..
8725 The function may use the FORMAT information to return different values
8726 depending on the format. The return value will be put literally into
8727 the exported file. If the return value is nil, this means Org should
8728 do what it normally does with links which do not have EXPORT defined.
8730 Org-mode has a built-in default for exporting links. If you are happy with
8731 this default, there is no need to define an export function for the link
8732 type. For a simple example of an export function, see `org-bbdb.el'."
8733 (add-to-list 'org-link-types type t)
8734 (org-make-link-regexps)
8735 (if (assoc type org-link-protocols)
8736 (setcdr (assoc type org-link-protocols) (list follow export))
8737 (push (list type follow export) org-link-protocols)))
8739 (defvar org-agenda-buffer-name)
8742 (defun org-store-link (arg)
8743 "\\<org-mode-map>Store an org-link to the current location.
8744 This link is added to `org-stored-links' and can later be inserted
8745 into an org-buffer with \\[org-insert-link].
8747 For some link types, a prefix arg is interpreted:
8748 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8749 For file links, arg negates `org-context-in-file-links'."
8751 (org-load-modules-maybe)
8752 (setq org-store-link-plist nil) ; reset
8753 (org-with-limited-levels
8754 (let (link cpltxt desc description search txt custom-id agenda-link)
8757 ((run-hook-with-args-until-success 'org-store-link-functions)
8758 (setq link (plist-get org-store-link-plist :link)
8759 desc (or (plist-get org-store-link-plist :description) link)))
8761 ((org-src-edit-buffer-p)
8763 (while (or (not label)
8767 (goto-char (point-min))
8769 (regexp-quote (format org-coderef-label-format label))
8771 (when label (message "Label exists already") (sit-for 2))
8772 (setq label (read-string "Code line label: " label)))
8774 (setq link (format org-coderef-label-format label))
8775 (setq gc (- 79 (length link)))
8776 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8778 (setq link (concat "(" label ")") desc nil)))
8780 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8781 ;; We are in the agenda, link to referenced location
8782 (let ((m (or (get-text-property (point) 'org-hd-marker)
8783 (get-text-property (point) 'org-marker))))
8785 (org-with-point-at m
8787 (if (org-called-interactively-p 'any)
8788 (call-interactively 'org-store-link)
8789 (org-store-link nil)))))))
8791 ((eq major-mode 'calendar-mode)
8792 (let ((cd (calendar-cursor-to-date)))
8795 (car org-time-stamp-formats)
8797 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8799 (org-store-link-props :type "calendar" :date cd)))
8801 ((eq major-mode 'w3-mode)
8802 (setq cpltxt (if (and (buffer-name)
8803 (not (string-match "Untitled" (buffer-name))))
8806 link (org-make-link (url-view-url t)))
8807 (org-store-link-props :type "w3" :url (url-view-url t)))
8809 ((eq major-mode 'w3m-mode)
8810 (setq cpltxt (or w3m-current-title w3m-current-url)
8811 link (org-make-link w3m-current-url))
8812 (org-store-link-props :type "w3m" :url (url-view-url t)))
8814 ((setq search (run-hook-with-args-until-success
8815 'org-create-file-search-functions))
8816 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8818 (setq cpltxt (or description link)))
8820 ((eq major-mode 'image-mode)
8821 (setq cpltxt (concat "file:"
8822 (abbreviate-file-name buffer-file-name))
8823 link (org-make-link cpltxt))
8824 (org-store-link-props :type "image" :file buffer-file-name))
8826 ((eq major-mode 'dired-mode)
8827 ;; link to the file in the current line
8828 (let ((file (dired-get-filename nil t)))
8830 (abbreviate-file-name
8831 (expand-file-name (dired-get-filename nil t)))
8832 ;; otherwise, no file so use current directory.
8834 (setq cpltxt (concat "file:" file)
8835 link (org-make-link cpltxt))))
8837 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
8838 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8840 ((org-in-regexp "<<\\(.*?\\)>>")
8843 (abbreviate-file-name
8844 (buffer-file-name (buffer-base-buffer)))
8845 "::" (match-string 1))
8846 link (org-make-link cpltxt)))
8847 ((and (featurep 'org-id)
8848 (or (eq org-link-to-org-use-id t)
8849 (and (eq org-link-to-org-use-id 'create-if-interactive)
8850 (org-called-interactively-p 'any))
8851 (and (eq org-link-to-org-use-id
8852 'create-if-interactive-and-no-custom-id)
8853 (org-called-interactively-p 'any)
8855 (and org-link-to-org-use-id
8856 (org-entry-get nil "ID"))))
8857 ;; We can make a link using the ID.
8858 (setq link (condition-case nil
8859 (prog1 (org-id-store-link)
8860 (setq desc (plist-get org-store-link-plist
8863 ;; probably before first headline, link to file only
8865 (abbreviate-file-name
8866 (buffer-file-name (buffer-base-buffer))))))))
8868 ;; Just link to current headline
8869 (setq cpltxt (concat "file:"
8870 (abbreviate-file-name
8871 (buffer-file-name (buffer-base-buffer)))))
8872 ;; Add a context search string
8873 (when (org-xor org-context-in-file-links arg)
8875 ((org-at-heading-p) nil)
8876 ((org-region-active-p)
8877 (buffer-substring (region-beginning) (region-end)))
8879 (when (or (null txt) (string-match "\\S-" txt))
8883 (org-make-org-heading-search-string txt)
8885 desc (or (nth 4 (ignore-errors
8886 (org-heading-components))) "NONE"))))
8887 (if (string-match "::\\'" cpltxt)
8888 (setq cpltxt (substring cpltxt 0 -2)))
8889 (setq link (org-make-link cpltxt)))))
8891 ((buffer-file-name (buffer-base-buffer))
8892 ;; Just link to this file here.
8893 (setq cpltxt (concat "file:"
8894 (abbreviate-file-name
8895 (buffer-file-name (buffer-base-buffer)))))
8896 ;; Add a context string
8897 (when (org-xor org-context-in-file-links arg)
8898 (setq txt (if (org-region-active-p)
8899 (buffer-substring (region-beginning) (region-end))
8900 (buffer-substring (point-at-bol) (point-at-eol))))
8901 ;; Only use search option if there is some text.
8902 (when (string-match "\\S-" txt)
8904 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8906 (setq link (org-make-link cpltxt)))
8908 ((org-called-interactively-p 'interactive)
8909 (error "Cannot link to a buffer which is not visiting a file"))
8911 (t (setq link nil)))
8913 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8914 (setq link (or link cpltxt)
8915 desc (or desc cpltxt))
8916 (if (equal desc "NONE") (setq desc nil))
8918 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8920 (setq org-stored-links
8921 (cons (list link desc) org-stored-links))
8922 (message "Stored: %s" (or desc link))
8924 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8926 (setq org-stored-links
8927 (cons (list link desc) org-stored-links))))
8928 (or agenda-link (and link (org-make-link-string link desc)))))))
8930 (defun org-store-link-props (&rest plist)
8931 "Store link properties, extract names and addresses."
8933 (when (setq x (plist-get plist :from))
8934 (setq adr (mail-extract-address-components x))
8935 (setq plist (plist-put plist :fromname (car adr)))
8936 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8937 (when (setq x (plist-get plist :to))
8938 (setq adr (mail-extract-address-components x))
8939 (setq plist (plist-put plist :toname (car adr)))
8940 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8941 (let ((from (plist-get plist :from))
8942 (to (plist-get plist :to)))
8943 (when (and from to org-from-is-user-regexp)
8945 (plist-put plist :fromto
8946 (if (string-match org-from-is-user-regexp from)
8948 (concat "from %f"))))))
8949 (setq org-store-link-plist plist))
8951 (defun org-add-link-props (&rest plist)
8952 "Add these properties to the link property list."
8955 (setq key (pop plist) value (pop plist))
8956 (setq org-store-link-plist
8957 (plist-put org-store-link-plist key value)))))
8959 (defun org-email-link-description (&optional fmt)
8960 "Return the description part of an email link.
8961 This takes information from `org-store-link-plist' and formats it
8962 according to FMT (default from `org-email-link-description-format')."
8963 (setq fmt (or fmt org-email-link-description-format))
8964 (let* ((p org-store-link-plist)
8965 (to (plist-get p :toaddress))
8966 (from (plist-get p :fromaddress))
8969 (cons "%c" (plist-get p :fromto))
8970 (cons "%F" (plist-get p :from))
8971 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8972 (cons "%T" (plist-get p :to))
8973 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8974 (cons "%s" (plist-get p :subject))
8975 (cons "%d" (plist-get p :date))
8976 (cons "%m" (plist-get p :message-id)))))
8977 (when (string-match "%c" fmt)
8978 ;; Check if the user wrote this message
8979 (if (and org-from-is-user-regexp from to
8980 (save-match-data (string-match org-from-is-user-regexp from)))
8981 (setq fmt (replace-match "to %t" t t fmt))
8982 (setq fmt (replace-match "from %f" t t fmt))))
8983 (org-replace-escapes fmt table)))
8985 (defun org-make-org-heading-search-string (&optional string heading)
8986 "Make search string for STRING or current headline."
8988 (let ((s (or string (org-get-heading)))
8989 (lines org-context-in-file-links))
8990 (unless (and string (not heading))
8991 ;; We are using a headline, clean up garbage in there.
8992 (if (string-match org-todo-regexp s)
8993 (setq s (replace-match "" t t s)))
8994 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
8995 (setq s (replace-match "" t t s)))
8996 (setq s (org-trim s))
8997 (if (string-match (concat "^\\(" org-quote-string "\\|"
8998 org-comment-string "\\)") s)
8999 (setq s (replace-match "" t t s)))
9000 (while (string-match org-ts-regexp s)
9001 (setq s (replace-match "" t t s))))
9002 (or string (setq s (concat "*" s))) ; Add * for headlines
9003 (when (and string (integerp lines) (> lines 0))
9004 (let ((slines (org-split-string s "\n")))
9005 (when (< lines (length slines))
9008 (reverse (nthcdr (- (length slines) lines)
9009 (reverse slines))) "\n")))))
9010 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
9012 (defun org-make-link (&rest strings)
9013 "Concatenate STRINGS."
9014 (apply 'concat strings))
9016 (defun org-make-link-string (link &optional description)
9017 "Make a link with brackets, consisting of LINK and DESCRIPTION."
9018 (unless (string-match "\\S-" link)
9019 (error "Empty link"))
9020 (when (and description
9021 (stringp description)
9022 (not (string-match "\\S-" description)))
9023 (setq description nil))
9024 (when (stringp description)
9025 ;; Remove brackets from the description, they are fatal.
9026 (while (string-match "\\[" description)
9027 (setq description (replace-match "{" t t description)))
9028 (while (string-match "\\]" description)
9029 (setq description (replace-match "}" t t description))))
9030 (when (equal link description)
9031 ;; No description needed, it is identical
9032 (setq description nil))
9033 (when (and (not description)
9034 (not (string-match (org-image-file-name-regexp) link))
9035 (not (equal link (org-link-escape link))))
9036 (setq description (org-extract-attributes link)))
9038 (cond ((string-match (org-image-file-name-regexp) link) link)
9039 ((string-match org-link-types-re link)
9040 (concat (match-string 1 link)
9041 (org-link-escape (substring link (match-end 1)))))
9042 (t (org-link-escape link))))
9043 (concat "[[" link "]"
9044 (if description (concat "[" description "]") "")
9047 (defconst org-link-escape-chars
9048 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9049 "List of characters that should be escaped in link.
9050 This is the list that is used for internal purposes.")
9052 (defvar org-url-encoding-use-url-hexify nil)
9054 (defconst org-link-escape-chars-browser
9056 "List of escapes for characters that are problematic in links.
9057 This is the list that is used before handing over to the browser.")
9059 (defun org-link-escape (text &optional table merge)
9060 "Return percent escaped representation of TEXT.
9061 TEXT is a string with the text to escape.
9062 Optional argument TABLE is a list with characters that should be
9063 escaped. When nil, `org-link-escape-chars' is used.
9064 If optional argument MERGE is set, merge TABLE into
9065 `org-link-escape-chars'."
9066 (if (and org-url-encoding-use-url-hexify (not table))
9067 (url-hexify-string text)
9070 (mapc (lambda (defchr)
9071 (unless (member defchr table)
9072 (setq table (cons defchr table)))) org-link-escape-chars))
9074 (setq table org-link-escape-chars)))
9077 (if (or (member char table)
9078 (< char 32) (= char 37) (> char 126))
9079 (mapconcat (lambda (sequence-element)
9080 (format "%%%.2X" sequence-element))
9081 (or (encode-coding-char char 'utf-8)
9082 (error "Unable to percent escape character: %s"
9083 (char-to-string char))) "")
9084 (char-to-string char))) text "")))
9086 (defun org-link-unescape (str)
9087 "Unhex hexified Unicode strings as returned from the JavaScript function
9088 encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
9089 (unless (and (null str) (string= "" str))
9090 (let ((pos 0) (case-fold-search t) unhexed)
9091 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9092 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9093 (setq str (replace-match unhexed t t str))
9094 (setq pos (+ pos (length unhexed))))))
9097 (defun org-link-unescape-compound (hex)
9098 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
9099 Note: this function also decodes single byte encodings like
9100 `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
9102 (let* ((bytes (cdr (split-string hex "%")))
9107 (let* ((val (string-to-number (pop bytes) 16))
9111 ((>= val 252) (cons 6 252))
9112 ((>= val 248) (cons 5 248))
9113 ((>= val 240) (cons 4 240))
9114 ((>= val 224) (cons 3 224))
9115 ((>= val 192) (cons 2 192))
9118 (if (>= val 192) (setq eat (car shift-xor)))
9119 (setq val (logxor val (cdr shift-xor)))
9120 (setq sum (+ (lsh sum (car shift-xor)) val))
9121 (if (> eat 0) (setq eat (- eat 1)))
9123 ((= 0 eat) ;multi byte
9124 (setq ret (concat ret (org-char-to-string sum)))
9126 ((not bytes) ; single byte(s)
9127 (setq ret (org-link-unescape-single-byte-sequence hex))))
9128 )) ;; end (while bytes
9131 (defun org-link-unescape-single-byte-sequence (hex)
9132 "Unhexify hex-encoded single byte character sequences."
9133 (mapconcat (lambda (byte)
9134 (char-to-string (string-to-number byte 16)))
9135 (cdr (split-string hex "%")) ""))
9137 (defun org-xor (a b)
9141 (defun org-fixup-message-id-for-http (s)
9142 "Replace special characters in a message id, so it can be used in an http query."
9143 (when (string-match "%" s)
9144 (setq s (mapconcat (lambda (c)
9147 (char-to-string c)))
9149 (while (string-match "<" s)
9150 (setq s (replace-match "%3C" t t s)))
9151 (while (string-match ">" s)
9152 (setq s (replace-match "%3E" t t s)))
9153 (while (string-match "@" s)
9154 (setq s (replace-match "%40" t t s)))
9157 (defun org-link-prettify (link)
9158 "Return a human-readable representation of LINK.
9159 The car of LINK must be a raw link the cdr of LINK must be either
9160 a link description or nil."
9161 (let ((desc (or (cadr link) "<no description>")))
9162 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9163 "[[" (car link) "]]")))
9166 (defun org-insert-link-global ()
9167 "Insert a link like Org-mode does.
9168 This command can be called in any mode to insert a link in Org-mode syntax."
9170 (org-load-modules-maybe)
9171 (org-run-like-in-org-mode 'org-insert-link))
9173 (defun org-insert-all-links (&optional keep)
9174 "Insert all links in `org-stored-links'."
9176 (let ((links (copy-sequence org-stored-links)) l)
9177 (while (setq l (if keep (pop links) (pop org-stored-links)))
9179 (org-insert-link nil (car l) (cadr l))
9182 (defun org-insert-link (&optional complete-file link-location default-description)
9183 "Insert a link. At the prompt, enter the link.
9185 Completion can be used to insert any of the link protocol prefixes like
9188 The history can be used to select a link previously stored with
9189 `org-store-link'. When the empty string is entered (i.e. if you just
9190 press RET at the prompt), the link defaults to the most recently
9191 stored link. As SPC triggers completion in the minibuffer, you need to
9192 use M-SPC or C-q SPC to force the insertion of a space character.
9194 You will also be prompted for a description, and if one is given, it will
9195 be displayed in the buffer instead of the link.
9197 If there is already a link at point, this command will allow you to edit link
9198 and description parts.
9200 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9201 be selected using completion. The path to the file will be relative to the
9202 current directory if the file is in the current directory or a subdirectory.
9203 Otherwise, the link will be the absolute path as completed in the minibuffer
9204 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9205 option `org-link-file-path-type'.
9207 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9208 the current directory or below.
9210 With three \\[universal-argument] prefixes, negate the meaning of
9211 `org-keep-stored-link-after-insertion'.
9213 If `org-make-link-description-function' is non-nil, this function will be
9214 called with the link target, and the result will be the default
9217 If the LINK-LOCATION parameter is non-nil, this value will be
9218 used as the link location instead of reading one interactively.
9220 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9221 be used as the default description."
9223 (let* ((wcf (current-window-configuration))
9224 (region (if (org-region-active-p)
9225 (buffer-substring (region-beginning) (region-end))))
9226 (remove (and region (list (region-beginning) (region-end))))
9228 tmphist ; byte-compile incorrectly complains about this
9229 (link link-location)
9230 entry file all-prefixes)
9232 (link-location) ; specified by arg, just use it.
9233 ((org-in-regexp org-bracket-link-regexp 1)
9234 ;; We do have a link at point, and we are going to edit it.
9235 (setq remove (list (match-beginning 0) (match-end 0)))
9236 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9237 (setq link (read-string "Link: "
9239 (org-match-string-no-properties 1)))))
9240 ((or (org-in-regexp org-angle-link-re)
9241 (org-in-regexp org-plain-link-re))
9242 ;; Convert to bracket link
9243 (setq remove (list (match-beginning 0) (match-end 0))
9244 link (read-string "Link: "
9245 (org-remove-angle-brackets (match-string 0)))))
9246 ((member complete-file '((4) (16)))
9247 ;; Completing read for file names.
9248 (setq link (org-file-complete-link complete-file)))
9250 ;; Read link, with completion for stored links.
9251 (with-output-to-temp-buffer "*Org Links*"
9252 (princ "Insert a link.
9253 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9254 (when org-stored-links
9255 (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9256 (princ (mapconcat 'org-link-prettify
9257 (reverse org-stored-links) "\n"))))
9258 (let ((cw (selected-window)))
9259 (select-window (get-buffer-window "*Org Links*" 'visible))
9260 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9261 (unless (pos-visible-in-window-p (point-max))
9262 (org-fit-window-to-buffer))
9263 (and (window-live-p cw) (select-window cw)))
9264 ;; Fake a link history, containing the stored links.
9265 (setq tmphist (append (mapcar 'car org-stored-links)
9266 org-insert-link-history))
9267 (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
9268 (mapcar 'car org-link-abbrev-alist)
9273 (let ((org-completion-use-ido nil)
9274 (org-completion-use-iswitchb nil))
9275 (org-completing-read
9278 (mapcar (lambda (x) (list (concat x ":")))
9280 (mapcar 'car org-stored-links))
9283 (car (car org-stored-links)))))
9284 (if (not (string-match "\\S-" link))
9285 (error "No link selected"))
9286 (if (or (member link all-prefixes)
9287 (and (equal ":" (substring link -1))
9288 (member (substring link 0 -1) all-prefixes)
9289 (setq link (substring link 0 -1))))
9290 (setq link (org-link-try-special-completion link))))
9291 (set-window-configuration wcf)
9292 (kill-buffer "*Org Links*"))
9293 (setq entry (assoc link org-stored-links))
9294 (or entry (push link org-insert-link-history))
9295 (setq desc (or desc (nth 1 entry)))))
9297 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9298 (not org-keep-stored-link-after-insertion))
9299 (setq org-stored-links (delq (assoc link org-stored-links)
9302 (if (string-match org-plain-link-re link)
9303 ;; URL-like link, normalize the use of angular brackets.
9304 (setq link (org-make-link (org-remove-angle-brackets link))))
9306 ;; Check if we are linking to the current file with a search option
9307 ;; If yes, simplify the link by using only the search option.
9308 (when (and buffer-file-name
9309 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
9310 (let* ((path (match-string 1 link))
9311 (case-fold-search nil)
9312 (search (match-string 2 link)))
9314 (if (equal (file-truename buffer-file-name) (file-truename path))
9315 ;; We are linking to this same file, with a search option
9316 (setq link search)))))
9318 ;; Check if we can/should use a relative path. If yes, simplify the link
9319 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9320 (let* ((type (match-string 1 link))
9321 (path (match-string 2 link))
9323 (case-fold-search nil))
9325 ((or (eq org-link-file-path-type 'absolute)
9326 (equal complete-file '(16)))
9327 (setq path (abbreviate-file-name (expand-file-name path))))
9328 ((eq org-link-file-path-type 'noabbrev)
9329 (setq path (expand-file-name path)))
9330 ((eq org-link-file-path-type 'relative)
9331 (setq path (file-relative-name path)))
9334 (if (string-match (concat "^" (regexp-quote
9336 (file-name-as-directory
9337 default-directory))))
9338 (expand-file-name path))
9339 ;; We are linking a file with relative path name.
9340 (setq path (substring (expand-file-name path)
9342 (setq path (abbreviate-file-name (expand-file-name path)))))))
9343 (setq link (concat type path))
9344 (if (equal desc origpath)
9347 (if org-make-link-description-function
9348 (setq desc (funcall org-make-link-description-function link desc))
9349 (if default-description (setq desc default-description)
9350 (setq desc (read-string "Description: " desc))))
9352 (unless (string-match "\\S-" desc) (setq desc nil))
9353 (if remove (apply 'delete-region remove))
9354 (insert (org-make-link-string link desc))))
9356 (defun org-link-try-special-completion (type)
9357 "If there is completion support for link type TYPE, offer it."
9358 (let ((fun (intern (concat "org-" type "-complete-link"))))
9361 (read-string "Link (no completion support): " (concat type ":")))))
9363 (defun org-file-complete-link (&optional arg)
9364 "Create a file link using completion."
9366 (setq file (read-file-name "File: "))
9367 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9368 (pwd1 (file-name-as-directory (abbreviate-file-name
9369 (expand-file-name ".")))))
9372 (setq link (org-make-link
9374 (abbreviate-file-name (expand-file-name file)))))
9375 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9376 (setq link (org-make-link "file:" (match-string 1 file))))
9377 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9378 (expand-file-name file))
9379 (setq link (org-make-link
9380 "file:" (match-string 1 (expand-file-name file)))))
9381 (t (setq link (org-make-link "file:" file)))))
9384 (defun org-completing-read (&rest args)
9385 "Completing-read with SPACE being a normal character."
9386 (let ((enable-recursive-minibuffers t)
9387 (minibuffer-local-completion-map
9388 (copy-keymap minibuffer-local-completion-map)))
9389 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9390 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9391 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9392 (apply 'org-icompleting-read args)))
9394 (defun org-completing-read-no-i (&rest args)
9395 (let (org-completion-use-ido org-completion-use-iswitchb)
9396 (apply 'org-completing-read args)))
9398 (defun org-iswitchb-completing-read (prompt choices &rest args)
9399 "Use iswitch as a completing-read replacement to choose from choices.
9400 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9402 (let* ((iswitchb-use-virtual-buffers nil)
9403 (iswitchb-make-buflist-hook
9405 (setq iswitchb-temp-buflist choices))))
9406 (iswitchb-read-buffer prompt)))
9408 (defun org-icompleting-read (&rest args)
9409 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9410 (org-without-partial-completion
9411 (if (and org-completion-use-ido
9412 (fboundp 'ido-completing-read)
9413 (boundp 'ido-mode) ido-mode
9414 (listp (second args)))
9415 (let ((ido-enter-matching-directory nil))
9416 (apply 'ido-completing-read (concat (car args))
9417 (if (consp (car (nth 1 args)))
9418 (mapcar 'car (nth 1 args))
9421 (if (and org-completion-use-iswitchb
9422 (boundp 'iswitchb-mode) iswitchb-mode
9423 (listp (second args)))
9424 (apply 'org-iswitchb-completing-read (concat (car args))
9425 (if (consp (car (nth 1 args)))
9426 (mapcar 'car (nth 1 args))
9429 (apply 'completing-read args)))))
9431 (defun org-extract-attributes (s)
9432 "Extract the attributes cookie from a string and set as text property."
9433 (let (a attr (start 0) key value)
9435 (when (string-match "{{\\([^}]+\\)}}$" s)
9436 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9437 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9438 (setq key (match-string 1 a) value (match-string 2 a)
9440 attr (plist-put attr (intern key) value))))
9441 (org-add-props s nil 'org-attr attr))
9444 (defun org-extract-attributes-from-string (tag)
9445 (let (key value attr)
9446 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9447 (setq key (match-string 1 tag) value (match-string 2 tag)
9448 tag (replace-match "" t t tag)
9449 attr (plist-put attr (intern key) value)))
9452 (defun org-attributes-to-string (plist)
9453 "Format a property list into an HTML attribute list."
9454 (let ((s "") key value)
9456 (setq key (pop plist) value (pop plist))
9458 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9461 ;;; Opening/following a link
9463 (defvar org-link-search-failed nil)
9465 (defvar org-open-link-functions nil
9466 "Hook for functions finding a plain text link.
9467 These functions must take a single argument, the link content.
9468 They will be called for links that look like [[link text][description]]
9469 when LINK TEXT does not have a protocol like \"http:\" and does not look
9470 like a filename (e.g. \"./blue.png\").
9472 These functions will be called *before* Org attempts to resolve the
9473 link by doing text searches in the current buffer - so if you want a
9474 link \"[[target]]\" to still find \"<<target>>\", your function should
9475 handle this as a special case.
9477 When the function does handle the link, it must return a non-nil value.
9478 If it decides that it is not responsible for this link, it must return
9479 nil to indicate that that Org-mode can continue with other options
9480 like exact and fuzzy text search.")
9482 (defun org-next-link ()
9483 "Move forward to the next link.
9484 If the link is in hidden text, expose it."
9486 (when (and org-link-search-failed (eq this-command last-command))
9487 (goto-char (point-min))
9488 (message "Link search wrapped back to beginning of buffer"))
9489 (setq org-link-search-failed nil)
9490 (let* ((pos (point))
9492 (a (assoc :link ct)))
9493 (if a (goto-char (nth 2 a)))
9494 (if (re-search-forward org-any-link-re nil t)
9496 (goto-char (match-beginning 0))
9497 (if (outline-invisible-p) (org-show-context)))
9499 (setq org-link-search-failed t)
9500 (error "No further link found"))))
9502 (defun org-previous-link ()
9503 "Move backward to the previous link.
9504 If the link is in hidden text, expose it."
9506 (when (and org-link-search-failed (eq this-command last-command))
9507 (goto-char (point-max))
9508 (message "Link search wrapped back to end of buffer"))
9509 (setq org-link-search-failed nil)
9510 (let* ((pos (point))
9512 (a (assoc :link ct)))
9513 (if a (goto-char (nth 1 a)))
9514 (if (re-search-backward org-any-link-re nil t)
9516 (goto-char (match-beginning 0))
9517 (if (outline-invisible-p) (org-show-context)))
9519 (setq org-link-search-failed t)
9520 (error "No further link found"))))
9522 (defun org-translate-link (s)
9523 "Translate a link string if a translation function has been defined."
9524 (if (and org-link-translation-function
9525 (fboundp org-link-translation-function)
9526 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9528 (setq s (funcall org-link-translation-function
9529 (match-string 1 s) (match-string 2 s)))
9530 (concat (car s) ":" (cdr s)))
9533 (defun org-translate-link-from-planner (type path)
9534 "Translate a link from Emacs Planner syntax so that Org can follow it.
9535 This is still an experimental function, your mileage may vary."
9537 ((member type '("http" "https" "news" "ftp"))
9538 ;; standard Internet links are the same.
9540 ((and (equal type "irc") (string-match "^//" path))
9541 ;; Planner has two / at the beginning of an irc link, we have 1.
9542 ;; We should have zero, actually....
9543 (setq path (substring path 1)))
9544 ((and (equal type "lisp") (string-match "^/" path))
9545 ;; Planner has a slash, we do not.
9546 (setq type "elisp" path (substring path 1)))
9547 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9548 ;; A typical message link. Planner has the id after the final slash,
9549 ;; we separate it with a hash mark
9550 (setq path (concat (match-string 1 path) "#"
9551 (org-remove-angle-brackets (match-string 2 path)))))
9555 (defun org-find-file-at-mouse (ev)
9556 "Open file link or URL at mouse."
9558 (mouse-set-point ev)
9559 (org-open-at-point 'in-emacs))
9561 (defun org-open-at-mouse (ev)
9562 "Open file link or URL at mouse.
9563 See the docstring of `org-open-file' for details."
9565 (mouse-set-point ev)
9566 (if (eq major-mode 'org-agenda-mode)
9567 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9568 (org-open-at-point))
9570 (defvar org-window-config-before-follow-link nil
9571 "The window configuration before following a link.
9572 This is saved in case the need arises to restore it.")
9574 (defvar org-open-link-marker (make-marker)
9575 "Marker pointing to the location where `org-open-at-point; was called.")
9578 (defun org-open-at-point-global ()
9579 "Follow a link like Org-mode does.
9580 This command can be called in any mode to follow a link that has
9583 (org-run-like-in-org-mode 'org-open-at-point))
9586 (defun org-open-link-from-string (s &optional arg reference-buffer)
9587 "Open a link in the string S, as if it was in Org-mode."
9588 (interactive "sLink: \nP")
9589 (let ((reference-buffer (or reference-buffer (current-buffer))))
9591 (let ((org-inhibit-startup (not reference-buffer)))
9594 (goto-char (point-min))
9595 (when reference-buffer
9596 (setq org-link-abbrev-alist-local
9597 (with-current-buffer reference-buffer
9598 org-link-abbrev-alist-local)))
9599 (org-open-at-point arg reference-buffer)))))
9601 (defvar org-open-at-point-functions nil
9602 "Hook that is run when following a link at point.
9604 Functions in this hook must return t if they identify and follow
9605 a link at point. If they don't find anything interesting at point,
9606 they must return nil.")
9608 (defun org-open-at-point (&optional arg reference-buffer)
9609 "Open link at or after point.
9610 If there is no link at point, this function will search forward up to
9611 the end of the current line.
9612 Normally, files will be opened by an appropriate application. If the
9613 optional prefix argument ARG is non-nil, Emacs will visit the file.
9614 With a double prefix argument, try to open outside of Emacs, in the
9615 application the system uses for this file type."
9617 ;; if in a code block, then open the block's results
9618 (unless (call-interactively #'org-babel-open-src-block-result)
9619 (org-load-modules-maybe)
9620 (move-marker org-open-link-marker (point))
9621 (setq org-window-config-before-follow-link (current-window-configuration))
9622 (org-remove-occur-highlights nil nil t)
9624 ((and (org-at-heading-p)
9625 (not (org-at-timestamp-p t))
9627 (concat org-plain-link-re "\\|"
9628 org-bracket-link-regexp "\\|"
9629 org-angle-link-re "\\|"
9630 "[ \t]:[^ \t\n]+:[ \t]*$")))
9631 (not (get-text-property (point) 'org-linked-text)))
9632 (or (org-offer-links-in-entry arg)
9633 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9634 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9635 ((org-at-timestamp-p t) (org-follow-timestamp-link))
9636 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9637 (not (org-in-regexp org-bracket-link-regexp)))
9638 (org-footnote-action))
9640 (let (type path link line search (pos (point)))
9643 (skip-chars-forward "^]\n\r")
9644 (when (org-in-regexp org-bracket-link-regexp 1)
9645 (setq link (org-extract-attributes
9646 (org-link-unescape (org-match-string-no-properties 1))))
9647 (while (string-match " *\n *" link)
9648 (setq link (replace-match " " t t link)))
9649 (setq link (org-link-expand-abbrev link))
9651 ((or (file-name-absolute-p link)
9652 (string-match "^\\.\\.?/" link))
9653 (setq type "file" path link))
9654 ((string-match org-link-re-with-space3 link)
9655 (setq type (match-string 1 link) path (match-string 2 link)))
9656 (t (setq type "thisfile" path link)))
9659 (when (get-text-property (point) 'org-linked-text)
9660 (setq type "thisfile"
9661 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9662 (1+ (point)) (point))
9663 path (buffer-substring
9664 (or (previous-single-property-change pos 'org-linked-text)
9666 (or (next-single-property-change pos 'org-linked-text)
9671 (when (or (org-in-regexp org-angle-link-re)
9672 (org-in-regexp org-plain-link-re))
9673 (setq type (match-string 1)
9674 path (org-link-unescape (match-string 2)))
9677 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9679 path (match-string 1))
9680 (while (string-match ":" path)
9681 (setq path (replace-match "+" t t path)))
9683 (when (org-in-regexp "<\\([^><\n]+\\)>")
9684 (setq type "tree-match"
9685 path (match-string 1))
9688 (error "No link found"))
9690 ;; switch back to reference buffer
9691 ;; needed when if called in a temporary buffer through
9692 ;; org-open-link-from-string
9693 (with-current-buffer (or reference-buffer (current-buffer))
9695 ;; Remove any trailing spaces in path
9696 (if (string-match " +\\'" path)
9697 (setq path (replace-match "" t t path)))
9698 (if (and org-link-translation-function
9699 (fboundp org-link-translation-function))
9700 ;; Check if we need to translate the link
9701 (let ((tmp (funcall org-link-translation-function type path)))
9702 (setq type (car tmp) path (cdr tmp))))
9706 ((assoc type org-link-protocols)
9707 (funcall (nth 1 (assoc type org-link-protocols)) path))
9709 ((equal type "mailto")
9710 (let ((cmd (car org-link-mailto-program))
9711 (args (cdr org-link-mailto-program)) args1
9712 (address path) (subject "") a)
9713 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9714 (setq address (match-string 1 path)
9715 subject (org-link-escape (match-string 2 path))))
9718 ((not (stringp (car args))) (push (pop args) args1))
9719 (t (setq a (pop args))
9720 (if (string-match "%a" a)
9721 (setq a (replace-match address t t a)))
9722 (if (string-match "%s" a)
9723 (setq a (replace-match subject t t a)))
9725 (apply cmd (nreverse args1))))
9727 ((member type '("http" "https" "ftp" "news"))
9728 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9730 path org-link-escape-chars-browser)
9733 ((string= type "doi")
9734 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
9736 path org-link-escape-chars-browser)
9739 ((member type '("message"))
9740 (browse-url (concat type ":" path)))
9742 ((string= type "tags")
9743 (org-tags-view arg path))
9745 ((string= type "tree-match")
9746 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9748 ((string= type "file")
9749 (if (string-match "::\\([0-9]+\\)\\'" path)
9750 (setq line (string-to-number (match-string 1 path))
9751 path (substring path 0 (match-beginning 0)))
9752 (if (string-match "::\\(.+\\)\\'" path)
9753 (setq search (match-string 1 path)
9754 path (substring path 0 (match-beginning 0)))))
9755 (if (string-match "[*?{]" (file-name-nondirectory path))
9757 (org-open-file path arg line search)))
9759 ((string= type "shell")
9761 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9762 (string-match org-confirm-shell-link-not-regexp cmd))
9763 (not org-confirm-shell-link-function)
9764 (funcall org-confirm-shell-link-function
9765 (format "Execute \"%s\" in shell? "
9766 (org-add-props cmd nil
9767 'face 'org-warning))))
9769 (message "Executing %s" cmd)
9770 (shell-command cmd))
9773 ((string= type "elisp")
9775 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9776 (string-match org-confirm-elisp-link-not-regexp cmd))
9777 (not org-confirm-elisp-link-function)
9778 (funcall org-confirm-elisp-link-function
9779 (format "Execute \"%s\" as elisp? "
9780 (org-add-props cmd nil
9781 'face 'org-warning))))
9782 (message "%s => %s" cmd
9783 (if (equal (string-to-char cmd) ?\()
9785 (call-interactively (read cmd))))
9788 ((and (string= type "thisfile")
9789 (run-hook-with-args-until-success
9790 'org-open-link-functions path)))
9792 ((string= type "thisfile")
9794 (switch-to-buffer-other-window
9795 (org-get-buffer-for-internal-link (current-buffer)))
9796 (org-mark-ring-push))
9797 (let ((cmd `(org-link-search
9799 ,(cond ((equal arg '(4)) ''occur)
9800 ((equal arg '(16)) ''org-occur)
9803 (condition-case nil (let ((org-link-search-inhibit-query t))
9805 (error (progn (widen) (eval cmd))))))
9808 (browse-url-at-point)))))))
9809 (move-marker org-open-link-marker nil)
9810 (run-hook-with-args 'org-follow-link-hook)))
9812 (defun org-offer-links-in-entry (&optional nth zero)
9813 "Offer links in the current entry and follow the selected link.
9814 If there is only one link, follow it immediately as well.
9815 If NTH is an integer, immediately pick the NTH link found.
9816 If ZERO is a string, check also this string for a link, and if
9817 there is one, offer it as link number zero."
9818 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9819 "\\(" org-angle-link-re "\\)\\|"
9820 "\\(" org-plain-link-re "\\)"))
9822 (in-emacs (if (integerp nth) nil nth))
9823 have-zero end links link c)
9824 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9825 (push (match-string 0 zero) links)
9826 (setq cnt (1- cnt) have-zero t))
9828 (org-back-to-heading t)
9829 (setq end (save-excursion (outline-next-heading) (point)))
9830 (while (re-search-forward re end t)
9831 (push (match-string 0) links))
9832 (setq links (org-uniquify (reverse links))))
9836 (message "No links"))
9837 ((equal (length links) 1)
9838 (setq link (list (car links))))
9839 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9840 (setq link (list (nth (if have-zero nth (1- nth)) links))))
9841 (t ; we have to select a link
9843 (save-window-excursion
9844 (delete-other-windows)
9845 (with-output-to-temp-buffer "*Select Link*"
9847 (if (not (string-match org-bracket-link-regexp l))
9848 (princ (format "[%c] %s\n" (incf cnt)
9849 (org-remove-angle-brackets l)))
9851 (princ (format "[%c] %s (%s)\n" (incf cnt)
9852 (match-string 3 l) (match-string 1 l)))
9853 (princ (format "[%c] %s\n" (incf cnt)
9854 (match-string 1 l))))))
9856 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9857 (message "Select link to open, RET to open all:")
9858 (setq c (read-char-exclusive))
9859 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9860 (when (equal c ?q) (error "Abort"))
9864 (if have-zero (setq nth (1+ nth)))
9865 (unless (and (integerp nth) (>= (length links) nth))
9866 (error "Invalid link selection"))
9867 (setq link (list (nth (1- nth) links))))))
9869 (let ((buf (current-buffer)))
9871 (org-open-link-from-string l in-emacs buf))
9875 ;; Add special file links that specify the way of opening
9877 (org-add-link-type "file+sys" 'org-open-file-with-system)
9878 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9879 (defun org-open-file-with-system (path)
9880 "Open file at PATH using the system way of opening it."
9881 (org-open-file path 'system))
9882 (defun org-open-file-with-emacs (path)
9883 "Open file at PATH in Emacs."
9884 (org-open-file path 'emacs))
9885 (defun org-remove-file-link-modifiers ()
9886 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9887 (goto-char (point-min))
9888 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9890 (replace-match "file:" t t))))
9891 (eval-after-load "org-exp"
9892 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9893 'org-remove-file-link-modifiers))
9897 (defun org-get-effort (&optional pom)
9898 "Get the effort estimate for the current entry."
9899 (org-entry-get pom org-effort-property))
9903 (defvar org-create-file-search-functions nil
9904 "List of functions to construct the right search string for a file link.
9905 These functions are called in turn with point at the location to
9906 which the link should point.
9908 A function in the hook should first test if it would like to
9909 handle this file type, for example by checking the `major-mode'
9910 or the file extension. If it decides not to handle this file, it
9911 should just return nil to give other functions a chance. If it
9912 does handle the file, it must return the search string to be used
9913 when following the link. The search string will be part of the
9914 file link, given after a double colon, and `org-open-at-point'
9915 will automatically search for it. If special measures must be
9916 taken to make the search successful, another function should be
9917 added to the companion hook `org-execute-file-search-functions',
9920 A function in this hook may also use `setq' to set the variable
9921 `description' to provide a suggestion for the descriptive text to
9922 be used for this link when it gets inserted into an Org-mode
9923 buffer with \\[org-insert-link].")
9925 (defvar org-execute-file-search-functions nil
9926 "List of functions to execute a file search triggered by a link.
9928 Functions added to this hook must accept a single argument, the
9929 search string that was part of the file link, the part after the
9930 double colon. The function must first check if it would like to
9931 handle this search, for example by checking the `major-mode' or
9932 the file extension. If it decides not to handle this search, it
9933 should just return nil to give other functions a chance. If it
9934 does handle the search, it must return a non-nil value to keep
9935 other functions from trying.
9937 Each function can access the current prefix argument through the
9938 variable `current-prefix-argument'. Note that a single prefix is
9939 used to force opening a link in Emacs, so it may be good to only
9940 use a numeric or double prefix to guide the search function.
9942 In case this is needed, a function in this hook can also restore
9943 the window configuration before `org-open-at-point' was called using:
9945 (set-window-configuration org-window-config-before-follow-link)")
9947 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
9948 (defun org-link-search (s &optional type avoid-pos stealth)
9949 "Search for a link search option.
9950 If S is surrounded by forward slashes, it is interpreted as a
9951 regular expression. In org-mode files, this will create an `org-occur'
9952 sparse tree. In ordinary files, `occur' will be used to list matches.
9953 If the current buffer is in `dired-mode', grep will be used to search
9954 in all files. If AVOID-POS is given, ignore matches near that position.
9956 When optional argument STEALTH is non-nil, do not modify
9957 visibility around point, thus ignoring
9958 `org-show-hierarchy-above', `org-show-following-heading' and
9959 `org-show-siblings' variables."
9960 (let ((case-fold-search t)
9961 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
9962 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
9963 (append '(("") (" ") ("\t") ("\n"))
9967 (pre nil) (post nil)
9968 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
9970 ;; First check if there are any special search functions
9971 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
9972 ;; Now try the builtin stuff
9973 ((and (equal (string-to-char s0) ?#)
9976 (goto-char (point-min))
9979 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
9980 (setq type 'dedicated
9981 pos (match-beginning 0))))
9982 ;; There is an exact target for this
9984 (org-back-to-heading t)))
9986 (goto-char (point-min))
9989 (concat "<<" (regexp-quote s0) ">>") nil t)
9990 (setq type 'dedicated
9991 pos (match-beginning 0))))
9992 ;; There is an exact target for this
9995 (goto-char (point-min))
9998 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
9999 (setq type 'dedicated pos (match-beginning 0))))
10000 ;; Found an invisible target.
10003 (goto-char (point-min))
10006 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10007 (setq type 'dedicated pos (match-beginning 0))))
10008 ;; Found an element with a matching #+name affiliated keyword.
10010 ((and (string-match "^(\\(.*\\))$" s0)
10012 (goto-char (point-min))
10015 (concat "[^[]" (regexp-quote
10016 (format org-coderef-label-format
10017 (match-string 1 s0))))
10019 (setq type 'dedicated
10020 pos (1+ (match-beginning 0))))))
10021 ;; There is a coderef target for this
10023 ((string-match "^/\\(.*\\)/$" s)
10024 ;; A regular expression
10026 ((derived-mode-p 'org-mode)
10027 (org-occur (match-string 1 s)))
10028 ;;((eq major-mode 'dired-mode)
10029 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
10030 (t (org-do-occur (match-string 1 s)))))
10031 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10032 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10033 (goto-char (point-min))
10035 ((let (case-fold-search)
10036 (re-search-forward (format org-complex-heading-regexp-format
10039 ;; OK, found a match
10040 (setq type 'dedicated)
10041 (goto-char (match-beginning 0)))
10042 ((and (not org-link-search-inhibit-query)
10043 (eq org-link-search-must-match-exact-headline 'query-to-create)
10044 (y-or-n-p "No match - create this as a new heading? "))
10045 (goto-char (point-max))
10046 (or (bolp) (newline))
10047 (insert "* " s "\n")
10048 (beginning-of-line 0))
10051 (error "No match"))))
10053 ;; A normal search string
10054 (when (equal (string-to-char s) ?*)
10055 ;; Anchor on headlines, post may include tags.
10056 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10057 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10058 s (substring s 1)))
10059 (remove-text-properties
10061 '(face nil mouse-face nil keymap nil fontified nil) s)
10062 ;; Make a series of regular expressions to find a match
10063 (setq words (org-split-string s "[ \n\r\t]+")
10065 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10066 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10068 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10069 re2a (concat "[ \t\r\n]" re2a_)
10070 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10071 re4 (concat "[^a-zA-Z_]" re4_)
10073 re1 (concat pre re2 post)
10074 re3 (concat pre (if pre re4_ re4) post)
10075 re5 (concat pre ".*" re4)
10076 re2 (concat pre re2)
10077 re2a (concat pre (if pre re2a_ re2a))
10078 re4 (concat pre (if pre re4_ re4))
10079 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10080 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10084 ((eq type 'org-occur) (org-occur reall))
10085 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10086 (t (goto-char (point-min))
10088 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10089 (org-search-not-self 1 re1 nil t)
10090 (org-search-not-self 1 re2 nil t)
10091 (org-search-not-self 1 re2a nil t)
10092 (org-search-not-self 1 re3 nil t)
10093 (org-search-not-self 1 re4 nil t)
10094 (org-search-not-self 1 re5 nil t)
10096 (goto-char (match-beginning 1))
10098 (error "No match"))))))
10099 (and (derived-mode-p 'org-mode)
10101 (org-show-context 'link-search))
10104 (defun org-search-not-self (group &rest args)
10105 "Execute `re-search-forward', but only accept matches that do not
10106 enclose the position of `org-open-link-marker'."
10107 (let ((m org-open-link-marker))
10109 (while (apply 're-search-forward args)
10110 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10111 (goto-char (match-end group))
10112 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10113 (> (match-beginning 0) (marker-position m))
10114 (< (match-end 0) (marker-position m)))
10116 (or (not (org-in-regexp
10117 org-bracket-link-analytic-regexp 1))
10118 (not (match-end 4)) ; no description
10119 (and (<= (match-beginning 4) (point))
10120 (>= (match-end 4) (point))))))
10121 (throw 'exit (point))))))))
10123 (defun org-get-buffer-for-internal-link (buffer)
10124 "Return a buffer to be used for displaying the link target of internal links."
10126 ((not org-display-internal-link-with-indirect-buffer)
10128 ((string-match "(Clone)$" (buffer-name buffer))
10129 (message "Buffer is already a clone, not making another one")
10130 ;; we also do not modify visibility in this case
10132 (t ; make a new indirect buffer for displaying the link
10133 (let* ((bn (buffer-name buffer))
10134 (ibn (concat bn "(Clone)"))
10135 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10136 (with-current-buffer ib (org-overview))
10139 (defun org-do-occur (regexp &optional cleanup)
10140 "Call the Emacs command `occur'.
10141 If CLEANUP is non-nil, remove the printout of the regular expression
10142 in the *Occur* buffer. This is useful if the regex is long and not useful
10146 (let ((cwin (selected-window)) win beg end)
10147 (when (setq win (get-buffer-window "*Occur*"))
10148 (select-window win))
10149 (goto-char (point-min))
10150 (when (re-search-forward "match[a-z]+" nil t)
10151 (setq beg (match-end 0))
10152 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10153 (setq end (1- (match-beginning 0)))))
10154 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10155 (goto-char (point-min))
10156 (select-window cwin))))
10158 ;;; The mark ring for links jumps
10160 (defvar org-mark-ring nil
10161 "Mark ring for positions before jumps in Org-mode.")
10162 (defvar org-mark-ring-last-goto nil
10163 "Last position in the mark ring used to go back.")
10164 ;; Fill and close the ring
10165 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10166 (loop for i from 1 to org-mark-ring-length do
10167 (push (make-marker) org-mark-ring))
10168 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10171 (defun org-mark-ring-push (&optional pos buffer)
10172 "Put the current position or POS into the mark ring and rotate it."
10174 (setq pos (or pos (point)))
10175 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10176 (move-marker (car org-mark-ring)
10178 (or buffer (current-buffer)))
10180 (substitute-command-keys
10181 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10183 (defun org-mark-ring-goto (&optional n)
10184 "Jump to the previous position in the mark ring.
10185 With prefix arg N, jump back that many stored positions. When
10186 called several times in succession, walk through the entire ring.
10187 Org-mode commands jumping to a different position in the current file,
10188 or to another Org-mode file, automatically push the old position
10192 (if (eq last-command this-command)
10193 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10194 (setq p org-mark-ring))
10195 (setq org-mark-ring-last-goto p)
10197 (org-pop-to-buffer-same-window (marker-buffer m))
10199 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10201 (defun org-remove-angle-brackets (s)
10202 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10203 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10205 (defun org-add-angle-brackets (s)
10206 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10207 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10209 (defun org-remove-double-quotes (s)
10210 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10211 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10214 ;;; Following specific links
10216 (defun org-follow-timestamp-link ()
10218 ((org-at-date-range-p t)
10219 (let ((org-agenda-start-on-weekday)
10220 (t1 (match-string 1))
10221 (t2 (match-string 2)))
10222 (setq t1 (time-to-days (org-time-string-to-time t1))
10223 t2 (time-to-days (org-time-string-to-time t2)))
10224 (org-agenda-list nil t1 (1+ (- t2 t1)))))
10225 ((org-at-timestamp-p t)
10226 (org-agenda-list nil (time-to-days (org-time-string-to-time
10227 (substring (match-string 1) 0 10)))
10229 (t (error "This should not happen"))))
10232 ;;; Following file links
10233 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10234 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10235 (declare-function mailcap-mime-info
10236 "mailcap" (string &optional request no-decode))
10237 (defvar org-wait nil)
10238 (defun org-open-file (path &optional in-emacs line search)
10239 "Open the file at PATH.
10240 First, this expands any special file name abbreviations. Then the
10241 configuration variable `org-file-apps' is checked if it contains an
10242 entry for this file type, and if yes, the corresponding command is launched.
10244 If no application is found, Emacs simply visits the file.
10246 With optional prefix argument IN-EMACS, Emacs will visit the file.
10247 With a double \\[universal-argument] \\[universal-argument] \
10248 prefix arg, Org tries to avoid opening in Emacs
10249 and to use an external application to visit the file.
10251 Optional LINE specifies a line to go to, optional SEARCH a string
10252 to search for. If LINE or SEARCH is given, the file will be
10253 opened in Emacs, unless an entry from org-file-apps that makes
10254 use of groups in a regexp matches.
10256 If you want to change the way frames are used when following a
10257 link, please customize `org-link-frame-setup'.
10259 If the file does not exist, an error is thrown."
10260 (let* ((file (if (equal path "")
10262 (substitute-in-file-name (expand-file-name path))))
10263 (file-apps (append org-file-apps (org-default-apps)))
10264 (apps (org-remove-if
10265 'org-file-apps-entry-match-against-dlink-p file-apps))
10266 (apps-dlink (org-remove-if-not
10267 'org-file-apps-entry-match-against-dlink-p file-apps))
10268 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10269 (dirp (if remp nil (file-directory-p file)))
10270 (file (if (and dirp org-open-directory-means-index-dot-org)
10271 (concat (file-name-as-directory file) "index.org")
10273 (a-m-a-p (assq 'auto-mode apps))
10274 (dfile (downcase file))
10275 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10276 (link (cond ((and (eq line nil)
10280 (concat file "::" (number-to-string line)))
10282 (concat file "::" search))))
10283 (dlink (downcase link))
10284 (old-buffer (current-buffer))
10286 (old-mode major-mode)
10287 ext cmd link-match-data)
10288 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10289 (setq ext (match-string 1 dfile))
10290 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10291 (setq ext (match-string 1 dfile))))
10293 ((member in-emacs '((16) system))
10294 (setq cmd (cdr (assoc 'system apps))))
10295 (in-emacs (setq cmd 'emacs))
10297 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10298 (and dirp (cdr (assoc 'directory apps)))
10299 ; first, try matching against apps-dlink
10300 ; if we get a match here, store the match data for later
10301 (let ((match (assoc-default dlink apps-dlink
10304 (progn (setq link-match-data (match-data))
10306 (progn (setq in-emacs (or in-emacs line search))
10307 nil))) ; if we have no match in apps-dlink,
10308 ; always open the file in emacs if line or search
10309 ; is given (for backwards compatibility)
10310 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10312 (cdr (assoc ext apps))
10313 (cdr (assoc t apps))))))
10314 (when (eq cmd 'system)
10315 (setq cmd (cdr (assoc 'system apps))))
10316 (when (eq cmd 'default)
10317 (setq cmd (cdr (assoc t apps))))
10318 (when (eq cmd 'mailcap)
10320 (mailcap-parse-mailcaps)
10321 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10322 (command (mailcap-mime-info mime-type)))
10323 (if (stringp command)
10325 (setq cmd 'emacs))))
10326 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10327 (not (file-exists-p file))
10328 (not org-open-non-existing-files))
10329 (error "No such file: %s" file))
10331 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10332 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10333 (while (string-match "['\"]%s['\"]" cmd)
10334 (setq cmd (replace-match "%s" t t cmd)))
10335 (while (string-match "%s" cmd)
10336 (setq cmd (replace-match
10338 (shell-quote-argument
10339 (convert-standard-filename file)))
10342 ;; Replace "%1", "%2" etc. in command with group matches from regex
10344 (let ((match-index 1)
10345 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10346 (set-match-data link-match-data)
10347 (while (<= match-index number-of-groups)
10348 (let ((regex (concat "%" (number-to-string match-index)))
10349 (replace-with (match-string match-index dlink)))
10350 (while (string-match regex cmd)
10351 (setq cmd (replace-match replace-with t t cmd))))
10352 (setq match-index (+ match-index 1)))))
10354 (save-window-excursion
10355 (start-process-shell-command cmd nil cmd)
10356 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10360 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10362 (if line (org-goto-line line)
10363 (if search (org-link-search search))))
10365 (let ((file (convert-standard-filename file)))
10367 (set-match-data link-match-data)
10369 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10370 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10371 (or (not (equal old-buffer (current-buffer)))
10372 (not (equal old-pos (point))))
10373 (org-mark-ring-push old-pos old-buffer))))
10375 (defun org-file-apps-entry-match-against-dlink-p (entry)
10376 "This function returns non-nil if `entry' uses a regular
10377 expression which should be matched against the whole link by
10380 It assumes that is the case when the entry uses a regular
10381 expression which has at least one grouping construct and the
10382 action is either a lisp form or a command string containing
10383 '%1', i.e. using at least one subexpression match as a
10385 (let ((selector (car entry))
10386 (action (cdr entry)))
10387 (if (stringp selector)
10388 (and (> (regexp-opt-depth selector) 0)
10389 (or (and (stringp action)
10390 (string-match "%[0-9]" action))
10394 (defun org-default-apps ()
10395 "Return the default applications for this operating system."
10397 ((eq system-type 'darwin)
10398 org-file-apps-defaults-macosx)
10399 ((eq system-type 'windows-nt)
10400 org-file-apps-defaults-windowsnt)
10401 (t org-file-apps-defaults-gnu)))
10403 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10404 "Convert extensions to regular expressions in the cars of LIST.
10405 Also, weed out any non-string entries, because the return value is used
10406 only for regexp matching.
10407 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10408 point to the symbol `emacs', indicating that the file should
10409 be opened in Emacs."
10412 (mapcar (lambda (x)
10413 (if (not (stringp (car x)))
10415 (if (string-match "\\W" (car x))
10417 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10420 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10422 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10423 (defun org-file-remote-p (file)
10424 "Test whether FILE specifies a location on a remote system.
10425 Return non-nil if the location is indeed remote.
10427 For example, the filename \"/user@host:/foo\" specifies a location
10428 on the system \"/user@host:\"."
10429 (cond ((fboundp 'file-remote-p)
10430 (file-remote-p file))
10431 ((fboundp 'tramp-handle-file-remote-p)
10432 (tramp-handle-file-remote-p file))
10433 ((and (boundp 'ange-ftp-name-format)
10434 (string-match (car ange-ftp-name-format) file))
10441 (defun org-get-org-file ()
10442 "Read a filename, with default directory `org-directory'."
10443 (let ((default (or org-default-notes-file remember-data-file)))
10444 (read-file-name (format "File name [%s]: " default)
10445 (file-name-as-directory org-directory)
10448 (defun org-notes-order-reversed-p ()
10449 "Check if the current file should receive notes in reversed order."
10451 ((not org-reverse-note-order) nil)
10452 ((eq t org-reverse-note-order) t)
10453 ((not (listp org-reverse-note-order)) nil)
10455 (let ((all org-reverse-note-order)
10457 (while (setq entry (pop all))
10458 (if (string-match (car entry) buffer-file-name)
10459 (throw 'exit (cdr entry))))
10462 (defvar org-refile-target-table nil
10463 "The list of refile targets, created by `org-refile'.")
10465 (defvar org-agenda-new-buffers nil
10466 "Buffers created to visit agenda files.")
10468 (defvar org-refile-cache nil
10469 "Cache for refile targets.")
10471 (defvar org-refile-markers nil
10472 "All the markers used for caching refile locations.")
10474 (defun org-refile-marker (pos)
10475 "Get a new refile marker, but only if caching is in use."
10476 (if (not org-refile-use-cache)
10478 (let ((m (make-marker)))
10479 (move-marker m pos)
10480 (push m org-refile-markers)
10483 (defun org-refile-cache-clear ()
10484 "Clear the refile cache and disable all the markers."
10485 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10486 (setq org-refile-markers nil)
10487 (setq org-refile-cache nil)
10488 (message "Refile cache has been cleared"))
10490 (defun org-refile-cache-check-set (set)
10491 "Check if all the markers in the cache still have live buffers."
10494 (while (and set (setq marker (nth 3 (pop set))))
10495 ;; if org-refile-use-outline-path is 'file, marker may be nil
10496 (when (and marker (null (marker-buffer marker)))
10497 (message "not found") (sit-for 3)
10498 (throw 'exit nil)))
10501 (defun org-refile-cache-put (set &rest identifiers)
10502 "Push the refile targets SET into the cache, under IDENTIFIERS."
10503 (let* ((key (sha1 (prin1-to-string identifiers)))
10504 (entry (assoc key org-refile-cache)))
10507 (push (cons key set) org-refile-cache))))
10509 (defun org-refile-cache-get (&rest identifiers)
10510 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10512 ((not org-refile-cache) nil)
10513 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10515 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10516 org-refile-cache))))
10517 (and set (org-refile-cache-check-set set) set)))))
10519 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10520 "Produce a table with refile targets."
10521 (let ((case-fold-search nil)
10522 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10523 (entries (or org-refile-targets '((nil . (:level . 1)))))
10524 targets tgs txt re files f desc descre fast-path-p level pos0)
10525 (message "Getting targets...")
10526 (with-current-buffer (or default-buffer (current-buffer))
10527 (while (setq entry (pop entries))
10528 (setq files (car entry) desc (cdr entry))
10529 (setq fast-path-p nil)
10531 ((null files) (setq files (list (current-buffer))))
10532 ((eq files 'org-agenda-files)
10533 (setq files (org-agenda-files 'unrestricted)))
10534 ((and (symbolp files) (fboundp files))
10535 (setq files (funcall files)))
10536 ((and (symbolp files) (boundp files))
10537 (setq files (symbol-value files))))
10538 (if (stringp files) (setq files (list files)))
10540 ((eq (car desc) :tag)
10541 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10542 ((eq (car desc) :todo)
10543 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10544 ((eq (car desc) :regexp)
10545 (setq descre (cdr desc)))
10546 ((eq (car desc) :level)
10547 (setq descre (concat "^\\*\\{" (number-to-string
10548 (if org-odd-levels-only
10549 (1- (* 2 (cdr desc)))
10552 ((eq (car desc) :maxlevel)
10553 (setq fast-path-p t)
10554 (setq descre (concat "^\\*\\{1," (number-to-string
10555 (if org-odd-levels-only
10556 (1- (* 2 (cdr desc)))
10559 (t (error "Bad refiling target description %s" desc)))
10560 (while (setq f (pop files))
10561 (with-current-buffer
10562 (if (bufferp f) f (org-get-agenda-file-buffer f))
10564 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10566 (if (bufferp f) (setq f (buffer-file-name
10567 (buffer-base-buffer f))))
10568 (setq f (and f (expand-file-name f)))
10569 (if (eq org-refile-use-outline-path 'file)
10570 (push (list (file-name-nondirectory f) f nil nil) tgs))
10574 (goto-char (point-min))
10575 (while (re-search-forward descre nil t)
10576 (goto-char (setq pos0 (point-at-bol)))
10578 (when org-refile-target-verify-function
10580 (or (funcall org-refile-target-verify-function)
10582 (when (and (looking-at org-complex-heading-regexp)
10583 (not (member (match-string 4) excluded-entries))
10585 (setq level (org-reduced-level
10586 (- (match-end 1) (match-beginning 1)))
10587 txt (org-link-display-format (match-string 4))
10588 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10589 re (format org-complex-heading-regexp-format
10590 (regexp-quote (match-string 4))))
10591 (when org-refile-use-outline-path
10592 (setq txt (mapconcat
10595 (if (eq org-refile-use-outline-path
10597 (list (file-name-nondirectory
10599 (buffer-base-buffer))))
10600 (if (eq org-refile-use-outline-path
10602 (list (buffer-file-name
10603 (buffer-base-buffer)))))
10604 (org-get-outline-path fast-path-p
10608 (push (list txt f re (org-refile-marker (point)))
10610 (when (= (point) pos0)
10611 ;; verification function has not moved point
10612 (goto-char (point-at-eol))))))))
10613 (when org-refile-use-cache
10614 (org-refile-cache-put tgs (buffer-file-name) descre))
10615 (setq targets (append tgs targets))
10617 (message "Getting targets...done")
10618 (nreverse targets)))
10620 (defun org-protect-slash (s)
10621 (while (string-match "/" s)
10622 (setq s (replace-match "\\" t t s)))
10625 (defvar org-olpa (make-vector 20 nil))
10627 (defun org-get-outline-path (&optional fastp level heading)
10628 "Return the outline path to the current entry, as a list.
10630 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10631 routine which makes outline path derivations for an entire file,
10632 avoiding backtracing. Refile target collection makes use of that."
10636 (error "Outline path failure, more than 19 levels"))
10637 (loop for i from level upto 19 do
10638 (aset org-olpa i nil))
10640 (delq nil (append org-olpa nil))
10641 (aset org-olpa level heading)))
10642 (let (rtn case-fold-search)
10646 (while (org-up-heading-safe)
10647 (when (looking-at org-complex-heading-regexp)
10648 (push (org-match-string-no-properties 4) rtn)))
10651 (defun org-format-outline-path (path &optional width prefix)
10652 "Format the outline path PATH for display.
10653 Width is the maximum number of characters that is available.
10654 Prefix is a prefix to be included in the returned string,
10655 such as the file name."
10656 (setq width (or width 79))
10657 (if prefix (setq width (- width (length prefix))))
10660 (let* ((nsteps (length path))
10661 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10662 (maxwidth (if (<= total-width width)
10663 10000 ;; everything fits
10664 ;; we need to shorten the level headings
10665 (/ (- width nsteps) nsteps)))
10666 (org-odd-levels-only nil)
10668 (total (1+ (length prefix))))
10669 (setq maxwidth (max maxwidth 10))
10674 (if (and (= n nsteps) (< maxwidth 10000))
10675 (setq maxwidth (- total-width total)))
10676 (if (< (length h) maxwidth)
10677 (progn (setq total (+ total (length h) 1)) h)
10678 (setq h (substring h 0 (- maxwidth 2))
10679 total (+ total maxwidth 1))
10680 (if (string-match "[ \t]+\\'" h)
10681 (setq h (substring h 0 (match-beginning 0))))
10682 (setq h (concat h "..")))
10683 (org-add-props h nil 'face
10684 (nth (% (1- n) org-n-level-faces)
10689 (defun org-display-outline-path (&optional file current)
10690 "Display the current outline path in the echo area."
10692 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10693 (case-fold-search nil)
10694 (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
10695 (if current (setq path (append path
10697 (org-back-to-heading t)
10698 (if (looking-at org-complex-heading-regexp)
10699 (list (match-string 4)))))))
10701 (org-format-outline-path
10704 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10706 (defvar org-refile-history nil
10707 "History for refiling operations.")
10709 (defvar org-after-refile-insert-hook nil
10710 "Hook run after `org-refile' has inserted its stuff at the new location.
10711 Note that this is still *before* the stuff will be removed from
10712 the *old* location.")
10714 (defvar org-capture-last-stored-marker)
10715 (defun org-refile (&optional goto default-buffer rfloc)
10716 "Move the entry or entries at point to another heading.
10717 The list of target headings is compiled using the information in
10718 `org-refile-targets', which see.
10720 At the target location, the entry is filed as a subitem of the target
10721 heading. Depending on `org-reverse-note-order', the new subitem will
10722 either be the first or the last subitem.
10724 If there is an active region, all entries in that region will be moved.
10725 However, the region must fulfill the requirement that the first heading
10726 is the first one sets the top-level of the moved text - at most siblings
10727 below it are allowed.
10729 With prefix arg GOTO, the command will only visit the target location
10730 and not actually move anything.
10732 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10733 go to the location where the last refiling operation has put the subtree.
10734 With a prefix argument of `2', refile to the running clock.
10736 RFLOC can be a refile location obtained in a different way.
10738 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10740 If you are using target caching (see `org-refile-use-cache'),
10741 you have to clear the target cache in order to find new targets.
10742 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10743 prefix argument (`C-u C-u C-u C-c C-w')."
10746 (if (member goto '(0 (64)))
10747 (org-refile-cache-clear)
10748 (let* ((cbuf (current-buffer))
10749 (regionp (org-region-active-p))
10750 (region-start (and regionp (region-beginning)))
10751 (region-end (and regionp (region-end)))
10752 (region-length (and regionp (- region-end region-start)))
10753 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10754 pos it nbuf file re level reversed)
10755 (setq last-command nil)
10757 (goto-char region-start)
10758 (or (bolp) (goto-char (point-at-bol)))
10759 (setq region-start (point))
10760 (unless (or (org-kill-is-subtree-p
10761 (buffer-substring region-start region-end))
10762 (prog1 org-refile-active-region-within-subtree
10763 (org-toggle-heading)))
10764 (error "The region is not a (sequence of) subtree(s)")))
10765 (if (equal goto '(16))
10766 (org-refile-goto-last-stored)
10768 (and (equal goto 2)
10769 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10771 (setq it (list (or org-clock-heading "running clock")
10773 (marker-buffer org-clock-hd-marker))
10775 (marker-position org-clock-hd-marker)))
10778 (let (heading-text)
10781 (org-back-to-heading t)
10783 (nth 4 (org-heading-components))))
10784 (org-refile-get-location
10785 (cond (goto "Goto")
10786 (regionp "Refile region to")
10787 (t (concat "Refile subtree \""
10788 heading-text "\" to")))
10790 (and (not (equal '(4) goto))
10791 org-refile-allow-creating-parent-nodes)
10793 (setq file (nth 1 it)
10796 (if (and (not goto)
10798 (equal (buffer-file-name) file)
10800 (and (>= pos region-start)
10801 (<= pos region-end))
10802 (and (>= pos (point))
10803 (< pos (save-excursion
10804 (org-end-of-subtree t t))))))
10805 (error "Cannot refile to position inside the tree or region"))
10807 (setq nbuf (or (find-buffer-visiting file)
10808 (find-file-noselect file)))
10811 (org-pop-to-buffer-same-window nbuf)
10813 (org-show-context 'org-goto))
10816 (org-kill-new (buffer-substring region-start region-end))
10817 (org-save-markers-in-region region-start region-end))
10818 (org-copy-subtree 1 nil t))
10819 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10820 (find-file-noselect file)))
10821 (setq reversed (org-notes-order-reversed-p))
10828 (looking-at org-outline-regexp)
10829 (setq level (org-get-valid-level (funcall outline-level) 1))
10832 (or (outline-next-heading) (point-max))
10833 (or (save-excursion (org-get-next-sibling))
10834 (org-end-of-subtree t t)
10838 (goto-char (point-max))
10839 (goto-char (point-min))
10840 (or (outline-next-heading) (goto-char (point-max)))))
10841 (if (not (bolp)) (newline))
10842 (org-paste-subtree level)
10843 (when org-log-refile
10844 (org-add-log-setup 'refile nil nil 'findpos
10846 (unless (eq org-log-refile 'note)
10847 (save-excursion (org-add-log-note))))
10848 (and org-auto-align-tags
10849 (let ((org-loop-over-headlines-in-active-region nil))
10850 (org-set-tags nil t)))
10851 (bookmark-set "org-refile-last-stored")
10852 ;; If we are refiling for capture, make sure that the
10853 ;; last-capture pointers point here
10854 (when (org-bound-and-true-p org-refile-for-capture)
10855 (bookmark-set "org-capture-last-stored-marker")
10856 (move-marker org-capture-last-stored-marker (point)))
10857 (if (fboundp 'deactivate-mark) (deactivate-mark))
10858 (run-hooks 'org-after-refile-insert-hook))))
10860 (delete-region (point) (+ (point) region-length))
10862 (when (featurep 'org-inlinetask)
10863 (org-inlinetask-remove-END-maybe))
10864 (setq org-markers-to-move nil)
10865 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10867 (defun org-refile-goto-last-stored ()
10868 "Go to the location where the last refile was stored."
10870 (bookmark-jump "org-refile-last-stored")
10871 (message "This is the location of the last refile"))
10873 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10875 "Prompt the user for a refile location, using PROMPT.
10876 PROMPT should not be suffixed with a colon and a space, because
10877 this function appends the default value from
10878 `org-refile-history' automatically, if that is not empty.
10879 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10880 this is used for the GOTO interface."
10881 (let ((org-refile-targets org-refile-targets)
10882 (org-refile-use-outline-path org-refile-use-outline-path)
10884 (when (and (derived-mode-p 'org-mode)
10885 (not org-refile-use-cache)
10889 (setq excluded-entries
10890 (append excluded-entries (list (org-get-heading t t)))))))
10891 (setq org-refile-target-table
10892 (org-refile-get-targets default-buffer excluded-entries)))
10893 (unless org-refile-target-table
10894 (error "No refile targets"))
10895 (let* ((prompt (concat prompt
10896 (and (car org-refile-history)
10897 (concat " (default " (car org-refile-history) ")"))
10899 (cbuf (current-buffer))
10900 (partial-completion-mode nil)
10901 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10902 (cfunc (if (and org-refile-use-outline-path
10903 org-outline-path-complete-in-steps)
10904 'org-olpath-completing-read
10905 'org-icompleting-read))
10906 (extra (if org-refile-use-outline-path "/" ""))
10907 (filename (and cfn (expand-file-name cfn)))
10910 (if (and (not (member org-refile-use-outline-path
10911 '(file full-file-path)))
10912 (not (equal filename (nth 1 x))))
10913 (cons (concat (car x) extra " ("
10914 (file-name-nondirectory (nth 1 x)) ")")
10916 (cons (concat (car x) extra) (cdr x))))
10917 org-refile-target-table))
10918 (completion-ignore-case t)
10919 pa answ parent-target child parent old-hist)
10920 (setq old-hist org-refile-history)
10921 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10922 nil 'org-refile-history (car org-refile-history)))
10923 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
10924 (org-refile-check-position pa)
10927 (when (or (not org-refile-history)
10928 (not (eq old-hist org-refile-history))
10929 (not (equal (car pa) (car org-refile-history))))
10930 (setq org-refile-history
10931 (cons (car pa) (if (assoc (car org-refile-history) tbl)
10933 (cdr org-refile-history))))
10934 (if (equal (car org-refile-history) (nth 1 org-refile-history))
10935 (pop org-refile-history)))
10937 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
10939 (setq parent (match-string 1 answ)
10940 child (match-string 2 answ))
10941 (setq parent-target (or (assoc parent tbl)
10942 (assoc (concat parent "/") tbl)))
10943 (when (and parent-target
10944 (or (eq new-nodes t)
10945 (and (eq new-nodes 'confirm)
10946 (y-or-n-p (format "Create new node \"%s\"? "
10948 (org-refile-new-child parent-target child)))
10949 (error "Invalid target location")))))
10951 (declare-function org-string-nw-p "org-macs.el" (s))
10952 (defun org-refile-check-position (refile-pointer)
10953 "Check if the refile pointer matches the readline to which it points."
10954 (let* ((file (nth 1 refile-pointer))
10955 (re (nth 2 refile-pointer))
10956 (pos (nth 3 refile-pointer))
10958 (when (org-string-nw-p re)
10959 (setq buffer (if (markerp pos)
10960 (marker-buffer pos)
10961 (or (find-buffer-visiting file)
10962 (find-file-noselect file))))
10963 (with-current-buffer buffer
10968 (beginning-of-line 1)
10969 (unless (org-looking-at-p re)
10970 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
10972 (defun org-refile-new-child (parent-target child)
10973 "Use refile target PARENT-TARGET to add new CHILD below it."
10974 (unless parent-target
10975 (error "Cannot find parent for new node"))
10976 (let ((file (nth 1 parent-target))
10977 (pos (nth 3 parent-target))
10979 (with-current-buffer (or (find-buffer-visiting file)
10980 (find-file-noselect file))
10986 (goto-char (point-max))
10987 (if (not (bolp)) (newline)))
10988 (when (looking-at org-outline-regexp)
10989 (setq level (funcall outline-level))
10990 (org-end-of-subtree t t))
10991 (org-back-over-empty-lines)
10992 (insert "\n" (make-string
10993 (if pos (org-get-valid-level level 1) 1) ?*)
10995 (beginning-of-line 0)
10996 (list (concat (car parent-target) "/" child) file "" (point)))))))
10998 (defun org-olpath-completing-read (prompt collection &rest args)
10999 "Read an outline path like a file name."
11000 (let ((thetable collection)
11001 (org-completion-use-ido nil) ; does not work with ido.
11002 (org-completion-use-iswitchb nil)) ; or iswitchb
11004 'org-icompleting-read prompt
11005 (lambda (string predicate &optional flag)
11006 (let (rtn r f (l (length string)))
11010 (try-completion string thetable))
11013 (setq rtn (all-completions string thetable predicate))
11016 (setq r (substring x l))
11017 (if (string-match " ([^)]*)$" x)
11018 (setq f (match-string 0 x))
11020 (if (string-match "/" r)
11021 (concat string (substring r 0 (match-end 0)) f)
11026 (assoc string thetable)))))
11029 ;;;; Dynamic blocks
11031 (defun org-find-dblock (name)
11032 "Find the first dynamic block with name NAME in the buffer.
11033 If not found, stay at current position and return nil."
11036 (goto-char (point-min))
11037 (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
11039 (match-beginning 0))))
11040 (if pos (goto-char pos))
11043 (defconst org-dblock-start-re
11044 "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11045 "Matches the start line of a dynamic block, with parameters.")
11047 (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
11048 "Matches the end of a dynamic block.")
11050 (defun org-create-dblock (plist)
11051 "Create a dynamic block section, with parameters taken from PLIST.
11052 PLIST must contain a :name entry which is used as name of the block."
11053 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11056 (let ((col (current-column))
11057 (name (plist-get plist :name)))
11058 (insert "#+BEGIN: " name)
11060 (if (eq (car plist) :name)
11061 (setq plist (cddr plist))
11062 (insert " " (prin1-to-string (pop plist)))))
11063 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11064 (beginning-of-line -2)))
11066 (defun org-prepare-dblock ()
11067 "Prepare dynamic block for refresh.
11068 This empties the block, puts the cursor at the insert position and returns
11069 the property list including an extra property :name with the block name."
11070 (unless (looking-at org-dblock-start-re)
11071 (error "Not at a dynamic block"))
11072 (let* ((begdel (1+ (match-end 0)))
11073 (name (org-no-properties (match-string 1)))
11074 (params (append (list :name name)
11075 (read (concat "(" (match-string 3) ")")))))
11077 (beginning-of-line 1)
11078 (skip-chars-forward " \t")
11079 (setq params (plist-put params :indentation-column (current-column))))
11080 (unless (re-search-forward org-dblock-end-re nil t)
11081 (error "Dynamic block not terminated"))
11084 (list :content (buffer-substring
11085 begdel (match-beginning 0)))))
11086 (delete-region begdel (match-beginning 0))
11091 (defun org-map-dblocks (&optional command)
11092 "Apply COMMAND to all dynamic blocks in the current buffer.
11093 If COMMAND is not given, use `org-update-dblock'."
11094 (let ((cmd (or command 'org-update-dblock)))
11096 (goto-char (point-min))
11097 (while (re-search-forward org-dblock-start-re nil t)
11098 (goto-char (match-beginning 0))
11100 (condition-case nil
11102 (error (message "Error during update of dynamic block"))))
11103 (unless (re-search-forward org-dblock-end-re nil t)
11104 (error "Dynamic block not terminated"))))))
11106 (defun org-dblock-update (&optional arg)
11107 "User command for updating dynamic blocks.
11108 Update the dynamic block at point. With prefix ARG, update all dynamic
11109 blocks in the buffer."
11112 (org-update-all-dblocks)
11113 (or (looking-at org-dblock-start-re)
11114 (org-beginning-of-dblock))
11115 (org-update-dblock)))
11117 (defun org-update-dblock ()
11118 "Update the dynamic block at point.
11119 This means to empty the block, parse for parameters and then call
11120 the correct writing function."
11122 (save-window-excursion
11123 (let* ((pos (point))
11124 (line (org-current-line))
11125 (params (org-prepare-dblock))
11126 (name (plist-get params :name))
11127 (indent (plist-get params :indentation-column))
11128 (cmd (intern (concat "org-dblock-write:" name))))
11129 (message "Updating dynamic block `%s' at line %d..." name line)
11130 (funcall cmd params)
11131 (message "Updating dynamic block `%s' at line %d...done" name line)
11133 (when (and indent (> indent 0))
11134 (setq indent (make-string indent ?\ ))
11136 (org-beginning-of-dblock)
11138 (while (not (looking-at org-dblock-end-re))
11140 (beginning-of-line 2))
11141 (when (looking-at org-dblock-end-re)
11142 (and (looking-at "[ \t]+")
11143 (replace-match ""))
11144 (insert indent)))))))
11146 (defun org-beginning-of-dblock ()
11147 "Find the beginning of the dynamic block at point.
11148 Error if there is no such block at point."
11149 (let ((pos (point))
11152 (if (and (re-search-backward org-dblock-start-re nil t)
11153 (setq beg (match-beginning 0))
11154 (re-search-forward org-dblock-end-re nil t)
11155 (> (match-end 0) pos))
11158 (error "Not in a dynamic block"))))
11161 (defun org-update-all-dblocks ()
11162 "Update all dynamic blocks in the buffer.
11163 This function can be used in a hook."
11165 (when (derived-mode-p 'org-mode)
11166 (org-map-dblocks 'org-update-dblock)))
11171 (defconst org-additional-option-like-keywords
11172 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11173 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11174 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11175 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11177 "ORGTBL" "TBLFM:" "TBLNAME:"
11178 "BEGIN_EXAMPLE" "END_EXAMPLE"
11179 "BEGIN_QUOTE" "END_QUOTE"
11180 "BEGIN_VERSE" "END_VERSE"
11181 "BEGIN_CENTER" "END_CENTER"
11182 "BEGIN_SRC" "END_SRC"
11183 "BEGIN_RESULT" "END_RESULT"
11185 "HEADER:" "HEADERS:"
11186 "CATEGORY:" "COLUMNS:" "PROPERTY:"
11187 "CAPTION:" "LABEL:"
11193 (defcustom org-structure-template-alist
11195 ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11196 "<src lang=\"?\">\n\n</src>")
11197 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11198 "<example>\n?\n</example>")
11199 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11200 "<quote>\n?\n</quote>")
11201 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11202 "<verse>\n?\n</verse>")
11203 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11204 "<center>\n?\n</center>")
11205 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11206 "<literal style=\"latex\">\n?\n</literal>")
11208 "<literal style=\"latex\">?</literal>")
11209 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11210 "<literal style=\"html\">\n?\n</literal>")
11212 "<literal style=\"html\">?</literal>")
11213 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11217 ("I" "#+INCLUDE %file ?"
11218 "<include file=%file markup=\"?\">")
11220 "Structure completion elements.
11221 This is a list of abbreviation keys and values. The value gets inserted
11222 if you type `<' followed by the key and then press the completion key,
11223 usually `M-TAB'. %file will be replaced by a file name after prompting
11224 for the file using completion. The cursor will be placed at the position
11225 of the `?` in the template.
11226 There are two templates for each key, the first uses the original Org syntax,
11227 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11228 the default when the /org-mtags.el/ module has been loaded. See also the
11229 variable `org-mtags-prefer-muse-templates'."
11230 :group 'org-completion
11232 (string :tag "Key")
11233 (string :tag "Template")
11234 (string :tag "Muse Template")))
11236 (defun org-try-structure-completion ()
11237 "Try to complete a structure template before point.
11238 This looks for strings like \"<e\" on an otherwise empty line and
11240 (let ((l (buffer-substring (point-at-bol) (point)))
11242 (when (and (looking-at "[ \t]*$")
11243 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11244 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11245 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11246 (match-beginning 1)) a)
11249 (defun org-complete-expand-structure-template (start cell)
11250 "Expand a structure template."
11251 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11252 (rpl (nth (if musep 2 1) cell))
11254 (delete-region start (point))
11255 (when (string-match "\\`#\\+" rpl)
11258 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11259 (setq ind (buffer-substring (point-at-bol) (point))))
11261 (setq start (point))
11262 (if (string-match "%file" rpl)
11263 (setq rpl (replace-match
11267 (abbreviate-file-name (read-file-name "Include file: ")))
11270 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11271 (concat "\n" ind)))
11273 (if (re-search-backward "\\?" start t) (delete-char 1))))
11275 ;;;; TODO, DEADLINE, Comments
11277 (defun org-toggle-comment ()
11278 "Change the COMMENT state of an entry."
11281 (org-back-to-heading)
11282 (let (case-fold-search)
11284 ((looking-at (format org-heading-keyword-regexp-format
11285 org-comment-string))
11286 (goto-char (match-end 1))
11287 (looking-at (concat " +" org-comment-string))
11288 (replace-match "" t t)
11289 (when (eolp) (insert " ")))
11290 ((looking-at org-outline-regexp)
11291 (goto-char (match-end 0))
11292 (insert org-comment-string " "))))))
11294 (defvar org-last-todo-state-is-todo nil
11295 "This is non-nil when the last TODO state change led to a TODO state.
11296 If the last change removed the TODO tag or switched to DONE, then
11299 (defvar org-setting-tags nil) ; dynamically skipped
11301 (defvar org-todo-setup-filter-hook nil
11302 "Hook for functions that pre-filter todo specs.
11303 Each function takes a todo spec and returns either nil or the spec
11304 transformed into canonical form." )
11306 (defvar org-todo-get-default-hook nil
11307 "Hook for functions that get a default item for todo.
11308 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11309 nil or a string to be used for the todo mark." )
11311 (defvar org-agenda-headline-snapshot-before-repeat)
11313 (defun org-current-effective-time ()
11314 "Return current time adjusted for `org-extend-today-until' variable"
11315 (let* ((ct (org-current-time))
11316 (dct (decode-time ct))
11318 (if (and org-use-effective-time
11319 (< (nth 2 dct) org-extend-today-until))
11320 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11324 (defun org-todo-yesterday (&optional arg)
11325 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11327 (if (eq major-mode 'org-agenda-mode)
11328 (apply 'org-agenda-todo-yesterday arg)
11329 (let* ((hour (third (decode-time
11330 (org-current-time))))
11331 (org-extend-today-until (1+ hour)))
11334 (defun org-todo (&optional arg)
11335 "Change the TODO state of an item.
11336 The state of an item is given by a keyword at the start of the heading,
11338 *** TODO Write paper
11341 The different keywords are specified in the variable `org-todo-keywords'.
11342 By default the available states are \"TODO\" and \"DONE\".
11343 So for this example: when the item starts with TODO, it is changed to DONE.
11344 When it starts with DONE, the DONE is removed. And when neither TODO nor
11345 DONE are present, add TODO at the beginning of the heading.
11347 With \\[universal-argument] prefix arg, use completion to determine the new \
11349 With numeric prefix arg, switch to that state.
11350 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11351 keywords (nextset).
11352 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11353 With a numeric prefix arg of 0, inhibit note taking for the change.
11355 For calling through lisp, arg is also interpreted in the following way:
11356 'none -> empty state
11357 \"\"(empty string) -> switch to empty state
11358 'done -> switch to DONE
11359 'nextset -> switch to the next set of keywords
11360 'previousset -> switch to the previous set of keywords
11361 \"WAITING\" -> switch to the specified keyword, but only if it
11362 really is a member of `org-todo-keywords'."
11364 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11365 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11366 'region-start-level 'region))
11367 org-loop-over-headlines-in-active-region)
11370 org-loop-over-headlines-in-active-region
11371 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11372 (if (equal arg '(16)) (setq arg 'nextset))
11373 (let ((org-blocker-hook org-blocker-hook)
11374 (case-fold-search nil))
11375 (when (equal arg '(64))
11376 (setq arg nil org-blocker-hook nil))
11377 (when (and org-blocker-hook
11378 (or org-inhibit-blocking
11379 (org-entry-get nil "NOBLOCKING")))
11380 (setq org-blocker-hook nil))
11383 (org-back-to-heading t)
11384 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11385 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11386 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11387 (let* ((match-data (match-data))
11388 (startpos (point-at-bol))
11389 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11390 (org-log-done org-log-done)
11391 (org-log-repeat org-log-repeat)
11392 (org-todo-log-states org-todo-log-states)
11393 (org-inhibit-logging
11395 (progn (setq arg nil) 'note) org-inhibit-logging))
11396 (this (match-string 1))
11397 (hl-pos (match-beginning 0))
11398 (head (org-get-todo-sequence-head this))
11399 (ass (assoc head org-todo-kwd-alist))
11400 (interpret (nth 1 ass))
11401 (done-word (nth 3 ass))
11402 (final-done-word (nth 4 ass))
11403 (org-last-state (or this ""))
11404 (completion-ignore-case t)
11405 (member (member this org-todo-keywords-1))
11406 (tail (cdr member))
11408 ((and org-todo-key-trigger
11409 (or (and (equal arg '(4))
11410 (eq org-use-fast-todo-selection 'prefix))
11411 (and (not arg) org-use-fast-todo-selection
11412 (not (eq org-use-fast-todo-selection
11414 ;; Use fast selection
11415 (org-fast-todo-selection))
11416 ((and (equal arg '(4))
11417 (or (not org-use-fast-todo-selection)
11418 (not org-todo-key-trigger)))
11419 ;; Read a state with completion
11420 (org-icompleting-read
11421 "State: " (mapcar (lambda(x) (list x))
11422 org-todo-keywords-1)
11426 (if tail (car tail) nil)
11427 (car org-todo-keywords-1)))
11429 (if (equal member org-todo-keywords-1)
11432 (nth (- (length org-todo-keywords-1)
11434 org-todo-keywords-1)
11435 (org-last org-todo-keywords-1))))
11436 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11437 (setq arg nil))) ; hack to fall back to cycling
11439 ;; user or caller requests a specific state
11441 ((equal arg "") nil)
11442 ((eq arg 'none) nil)
11443 ((eq arg 'done) (or done-word (car org-done-keywords)))
11445 (or (car (cdr (member head org-todo-heads)))
11446 (car org-todo-heads)))
11447 ((eq arg 'previousset)
11448 (let ((org-todo-heads (reverse org-todo-heads)))
11449 (or (car (cdr (member head org-todo-heads)))
11450 (car org-todo-heads))))
11451 ((car (member arg org-todo-keywords-1)))
11453 (error "State `%s' not valid in this file" arg))
11454 ((nth (1- (prefix-numeric-value arg))
11455 org-todo-keywords-1))))
11456 ((null member) (or head (car org-todo-keywords-1)))
11457 ((equal this final-done-word) nil) ;; -> make empty
11458 ((null tail) nil) ;; -> first entry
11459 ((memq interpret '(type priority))
11460 (if (eq this-command last-command)
11462 (if (> (length tail) 0)
11463 (or done-word (car org-done-keywords))
11468 (run-hook-with-args-until-success
11469 'org-todo-get-default-hook org-state org-last-state)
11471 (next (if org-state (concat " " org-state " ") " "))
11472 (change-plist (list :type 'todo-state-change :from this :to org-state
11473 :position startpos))
11475 (when org-blocker-hook
11476 (setq org-last-todo-state-is-todo
11477 (not (member this org-done-keywords)))
11478 (unless (save-excursion
11480 (org-with-wide-buffer
11481 (run-hook-with-args-until-failure
11482 'org-blocker-hook change-plist))))
11483 (if (org-called-interactively-p 'interactive)
11484 (error "TODO state change from %s to %s blocked" this org-state)
11486 (message "TODO state change from %s to %s blocked" this org-state)
11487 (throw 'exit nil))))
11488 (store-match-data match-data)
11489 (replace-match next t t)
11490 (unless (pos-visible-in-window-p hl-pos)
11491 (message "TODO state changed to %s" (org-trim next)))
11493 (setq head (org-get-todo-sequence-head org-state)
11494 ass (assoc head org-todo-kwd-alist)
11495 interpret (nth 1 ass)
11496 done-word (nth 3 ass)
11497 final-done-word (nth 4 ass)))
11498 (when (memq arg '(nextset previousset))
11499 (message "Keyword-Set %d/%d: %s"
11500 (- (length org-todo-sets) -1
11501 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11502 (length org-todo-sets)
11503 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11504 (setq org-last-todo-state-is-todo
11505 (not (member org-state org-done-keywords)))
11506 (setq now-done-p (and (member org-state org-done-keywords)
11507 (not (member this org-done-keywords))))
11508 (and logging (org-local-logging logging))
11509 (when (and (or org-todo-log-states org-log-done)
11510 (not (eq org-inhibit-logging t))
11511 (not (memq arg '(nextset previousset))))
11512 ;; we need to look at recording a time and note
11513 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11514 (nth 2 (assoc this org-todo-log-states))))
11515 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11516 (setq dolog 'time))
11517 (when (and org-state
11518 (member org-state org-not-done-keywords)
11519 (not (member this org-not-done-keywords)))
11520 ;; This is now a todo state and was not one before
11521 ;; If there was a CLOSED time stamp, get rid of it.
11522 (org-add-planning-info nil nil 'closed))
11523 (when (and now-done-p org-log-done)
11524 ;; It is now done, and it was not done before
11525 (org-add-planning-info 'closed (org-current-effective-time))
11526 (if (and (not dolog) (eq 'note org-log-done))
11527 (org-add-log-setup 'done org-state this 'findpos 'note)))
11528 (when (and org-state dolog)
11529 ;; This is a non-nil state, and we need to log it
11530 (org-add-log-setup 'state org-state this 'findpos dolog)))
11531 ;; Fixup tag positioning
11532 (org-todo-trigger-tag-changes org-state)
11533 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11534 (when org-provide-todo-statistics
11535 (org-update-parent-todo-statistics))
11536 (run-hooks 'org-after-todo-state-change-hook)
11537 (if (and arg (not (member org-state org-done-keywords)))
11538 (setq head (org-get-todo-sequence-head org-state)))
11539 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11540 ;; Do we need to trigger a repeat?
11542 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11543 ;; This is for the agenda, take a snapshot of the headline.
11545 (setq org-agenda-headline-snapshot-before-repeat
11546 (org-get-heading))))
11547 (org-auto-repeat-maybe org-state))
11548 ;; Fixup cursor location if close to the keyword
11549 (if (and (outline-on-heading-p)
11551 (save-excursion (beginning-of-line 1)
11552 (looking-at org-todo-line-regexp))
11553 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11555 (goto-char (or (match-end 2) (match-end 1)))
11556 (and (looking-at " ") (just-one-space))))
11557 (when org-trigger-hook
11559 (run-hook-with-args 'org-trigger-hook change-plist)))))))))
11561 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11562 "Block turning an entry into a TODO, using the hierarchy.
11563 This checks whether the current task should be blocked from state
11564 changes. Such blocking occurs when:
11566 1. The task has children which are not all in a completed state.
11568 2. A task has a parent with the property :ORDERED:, and there
11569 are siblings prior to the current task with incomplete
11572 3. The parent of the task is blocked because it has siblings that should
11573 be done first, or is child of a block grandparent TODO entry."
11575 (if (not org-enforce-todo-dependencies)
11576 t ; if locally turned off don't block
11578 ;; If this is not a todo state change, or if this entry is already DONE,
11580 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11581 (member (plist-get change-plist :from)
11582 (cons 'done org-done-keywords))
11583 (member (plist-get change-plist :to)
11584 (cons 'todo org-not-done-keywords))
11585 (not (plist-get change-plist :to)))
11586 (throw 'dont-block t))
11587 ;; If this task has children, and any are undone, it's blocked
11589 (org-back-to-heading t)
11590 (let ((this-level (funcall outline-level)))
11591 (outline-next-heading)
11592 (let ((child-level (funcall outline-level)))
11593 (while (and (not (eobp))
11594 (> child-level this-level))
11595 ;; this todo has children, check whether they are all
11597 (if (and (not (org-entry-is-done-p))
11598 (org-entry-is-todo-p))
11599 (throw 'dont-block nil))
11600 (outline-next-heading)
11601 (setq child-level (funcall outline-level))))))
11602 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11603 ;; any previous siblings are undone, it's blocked
11605 (org-back-to-heading t)
11606 (let* ((pos (point))
11607 (parent-pos (and (org-up-heading-safe) (point))))
11608 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11609 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11611 (re-search-forward org-not-done-heading-regexp pos t))
11612 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11613 ;; Search further up the hierarchy, to see if an ancestor is blocked
11615 (goto-char parent-pos)
11616 (if (not (looking-at org-not-done-heading-regexp))
11617 (throw 'dont-block t)) ; do not block, parent is not a TODO
11619 (setq parent-pos (and (org-up-heading-safe) (point)))
11620 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11621 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11623 (re-search-forward org-not-done-heading-regexp pos t))
11624 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11626 (defcustom org-track-ordered-property-with-tag nil
11627 "Should the ORDERED property also be shown as a tag?
11628 The ORDERED property decides if an entry should require subtasks to be
11629 completed in sequence. Since a property is not very visible, setting
11630 this option means that toggling the ORDERED property with the command
11631 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11632 not relevant for the behavior, but it makes things more visible.
11634 Note that toggling the tag with tags commands will not change the property
11635 and therefore not influence behavior!
11637 This can be t, meaning the tag ORDERED should be used, It can also be a
11638 string to select a different tag for this task."
11641 (const :tag "No tracking" nil)
11642 (const :tag "Track with ORDERED tag" t)
11643 (string :tag "Use other tag")))
11645 (defun org-toggle-ordered-property ()
11646 "Toggle the ORDERED property of the current entry.
11647 For better visibility, you can track the value of this property with a tag.
11648 See variable `org-track-ordered-property-with-tag'."
11650 (let* ((t1 org-track-ordered-property-with-tag)
11651 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11653 (org-back-to-heading)
11654 (if (org-entry-get nil "ORDERED")
11656 (org-delete-property "ORDERED")
11657 (and tag (org-toggle-tag tag 'off))
11658 (message "Subtasks can be completed in arbitrary order"))
11659 (org-entry-put nil "ORDERED" "t")
11660 (and tag (org-toggle-tag tag 'on))
11661 (message "Subtasks must be completed in sequence")))))
11663 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11664 (defun org-block-todo-from-checkboxes (change-plist)
11665 "Block turning an entry into a TODO, using checkboxes.
11666 This checks whether the current task should be blocked from state
11667 changes because there are unchecked boxes in this entry."
11668 (if (not org-enforce-todo-checkbox-dependencies)
11669 t ; if locally turned off don't block
11671 ;; If this is not a todo state change, or if this entry is already DONE,
11673 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11674 (member (plist-get change-plist :from)
11675 (cons 'done org-done-keywords))
11676 (member (plist-get change-plist :to)
11677 (cons 'todo org-not-done-keywords))
11678 (not (plist-get change-plist :to)))
11679 (throw 'dont-block t))
11680 ;; If this task has checkboxes that are not checked, it's blocked
11682 (org-back-to-heading t)
11683 (let ((beg (point)) end)
11684 (outline-next-heading)
11687 (if (org-list-search-forward
11688 (concat (org-item-beginning-re)
11689 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11693 (if (boundp 'org-blocked-by-checkboxes)
11694 (setq org-blocked-by-checkboxes t))
11695 (throw 'dont-block nil)))))
11696 t))) ; do not block
11698 (defun org-entry-blocked-p ()
11699 "Is the current entry blocked?"
11700 (if (org-entry-get nil "NOBLOCKING")
11701 nil ;; Never block this entry
11703 (run-hook-with-args-until-failure
11705 (list :type 'todo-state-change
11710 (defun org-update-statistics-cookies (all)
11711 "Update the statistics cookie, either from TODO or from checkboxes.
11712 This should be called with the cursor in a line with a statistics cookie."
11716 (org-update-checkbox-count 'all)
11717 (org-map-entries 'org-update-parent-todo-statistics))
11718 (if (not (org-at-heading-p))
11719 (org-update-checkbox-count)
11720 (let ((pos (move-marker (make-marker) (point)))
11722 (ignore-errors (org-back-to-heading t))
11723 (if (not (org-at-heading-p))
11724 (org-update-checkbox-count)
11725 (setq l1 (org-outline-level))
11726 (setq end (save-excursion
11727 (outline-next-heading)
11728 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11730 (if (and (save-excursion
11732 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11733 (not (save-excursion (re-search-forward
11734 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11735 (org-update-checkbox-count)
11736 (if (and l2 (> l2 l1))
11739 (org-update-parent-todo-statistics))
11741 (beginning-of-line 1)
11742 (while (re-search-forward
11743 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11745 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11747 (move-marker pos nil)))))
11749 (defvar org-entry-property-inherited-from) ;; defined below
11750 (defun org-update-parent-todo-statistics ()
11751 "Update any statistics cookie in the parent of the current headline.
11752 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11753 the entire subtree and this will travel up the hierarchy and update
11754 statistics everywhere."
11755 (let* ((prop (save-excursion (org-up-heading-safe)
11756 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11757 (recursive (or (not org-hierarchical-todo-statistics)
11758 (and prop (string-match "\\<recursive\\>" prop))))
11759 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11762 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11763 level ltoggle l1 new ndel
11764 (cnt-all 0) (cnt-done 0) is-percent kwd
11765 checkbox-beg ov ovs ove cookie-present)
11768 (beginning-of-line 1)
11769 (setq ltoggle (funcall outline-level))
11770 ;; Three situations are to consider:
11772 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11773 ;; to the top-level ancestor on the headline;
11775 ;; 2. If parent has "recursive" property, repeat up to the
11776 ;; headline setting that property, taking inheritance into
11779 ;; 3. Else, move up to direct parent and proceed only once.
11780 (while (and (setq level (org-up-heading-safe))
11781 (or recursive first)
11783 (setq first nil cookie-present nil)
11787 (downcase (or (org-entry-get nil "COOKIE_DATA")
11790 (while (re-search-forward box-re (point-at-eol) t)
11791 (setq cnt-all 0 cnt-done 0 cookie-present t)
11792 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11794 (unless (outline-next-heading) (throw 'exit nil))
11795 (while (and (looking-at org-complex-heading-regexp)
11796 (> (setq l1 (length (match-string 1))) level))
11797 (setq kwd (and (or recursive (= l1 ltoggle))
11799 (if (or (eq org-provide-todo-statistics 'all-headlines)
11800 (and (listp org-provide-todo-statistics)
11801 (or (member kwd org-provide-todo-statistics)
11802 (member kwd org-done-keywords))))
11803 (setq cnt-all (1+ cnt-all))
11804 (if (eq org-provide-todo-statistics t)
11805 (and kwd (setq cnt-all (1+ cnt-all)))))
11806 (and (member kwd org-done-keywords)
11807 (setq cnt-done (1+ cnt-done)))
11808 (outline-next-heading)))
11811 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11812 (format "[%d/%d]" cnt-done cnt-all))
11813 ndel (- (match-end 0) checkbox-beg))
11814 ;; handle overlays when updating cookie from column view
11815 (when (setq ov (car (overlays-at checkbox-beg)))
11816 (setq ovs (overlay-start ov) ove (overlay-end ov))
11817 (delete-overlay ov))
11818 (goto-char checkbox-beg)
11820 (delete-region (point) (+ (point) ndel))
11821 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11822 (when ov (move-overlay ov ovs ove)))
11823 (when cookie-present
11824 (run-hook-with-args 'org-after-todo-statistics-hook
11825 cnt-done (- cnt-all cnt-done))))))
11826 (run-hooks 'org-todo-statistics-hook)))
11828 (defvar org-after-todo-statistics-hook nil
11829 "Hook that is called after a TODO statistics cookie has been updated.
11830 Each function is called with two arguments: the number of not-done entries
11831 and the number of done entries.
11833 For example, the following function, when added to this hook, will switch
11834 an entry to DONE when all children are done, and back to TODO when new
11835 entries are set to a TODO status. Note that this hook is only called
11836 when there is a statistics cookie in the headline!
11838 (defun org-summary-todo (n-done n-not-done)
11839 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11840 (let (org-log-done org-log-states) ; turn off logging
11841 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11844 (defvar org-todo-statistics-hook nil
11845 "Hook that is run whenever Org thinks TODO statistics should be updated.
11846 This hook runs even if there is no statistics cookie present, in which case
11847 `org-after-todo-statistics-hook' would not run.")
11849 (defun org-todo-trigger-tag-changes (state)
11850 "Apply the changes defined in `org-todo-state-tags-triggers'."
11851 (let ((l org-todo-state-tags-triggers)
11853 (when (or (not state) (equal state ""))
11854 (setq changes (append changes (cdr (assoc "" l)))))
11855 (when (and (stringp state) (> (length state) 0))
11856 (setq changes (append changes (cdr (assoc state l)))))
11857 (when (member state org-not-done-keywords)
11858 (setq changes (append changes (cdr (assoc 'todo l)))))
11859 (when (member state org-done-keywords)
11860 (setq changes (append changes (cdr (assoc 'done l)))))
11861 (dolist (c changes)
11862 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11864 (defun org-local-logging (value)
11865 "Get logging settings from a property VALUE."
11867 ;; directly set the variables, they are already local.
11868 (setq org-log-done nil
11870 org-todo-log-states nil)
11871 (setq words (org-split-string value))
11872 (while (setq w (pop words))
11874 ((setq a (assoc w org-startup-options))
11875 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11876 (set (nth 1 a) (nth 2 a))))
11877 ((setq a (org-extract-log-state-settings w))
11878 (and (member (car a) org-todo-keywords-1)
11879 (push a org-todo-log-states)))))))
11881 (defun org-get-todo-sequence-head (kwd)
11882 "Return the head of the TODO sequence to which KWD belongs.
11883 If KWD is not set, check if there is a text property remembering the
11888 (or (get-text-property (point-at-bol) 'org-todo-head)
11890 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11891 nil (point-at-eol)))
11892 (get-text-property p 'org-todo-head))))
11893 ((not (member kwd org-todo-keywords-1))
11894 (car org-todo-keywords-1))
11895 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
11897 (defun org-fast-todo-selection ()
11898 "Fast TODO keyword selection with single keys.
11899 Returns the new TODO keyword, or nil if no state change should occur."
11900 (let* ((fulltable org-todo-key-alist)
11901 (done-keywords org-done-keywords) ;; needed for the faces.
11902 (maxlen (apply 'max (mapcar
11904 (if (stringp (car x)) (string-width (car x)) 0))
11907 (fwidth (+ maxlen 3 1 3))
11908 (ncol (/ (- (window-width) 4) fwidth))
11912 (save-window-excursion
11914 (set-buffer (get-buffer-create " *Org todo*"))
11915 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
11917 (org-set-local 'org-done-keywords done-keywords)
11918 (setq tbl fulltable cnt 0)
11919 (while (setq e (pop tbl))
11921 ((equal e '(:startgroup))
11922 (push '() groups) (setq ingroup t)
11923 (when (not (= cnt 0))
11927 ((equal e '(:endgroup))
11928 (setq ingroup nil cnt 0)
11930 ((equal e '(:newline))
11931 (when (not (= cnt 0))
11935 (while (equal (car tbl) '(:newline))
11937 (setq tbl (cdr tbl)))))
11939 (setq tg (car e) c (cdr e))
11940 (if ingroup (push tg (car groups)))
11941 (setq tg (org-add-props tg nil 'face
11942 (org-get-todo-face tg)))
11943 (if (and (= cnt 0) (not ingroup)) (insert " "))
11944 (insert "[" c "] " tg (make-string
11945 (- fwidth 4 (length tg)) ?\ ))
11946 (when (= (setq cnt (1+ cnt)) ncol)
11948 (if ingroup (insert " "))
11951 (goto-char (point-min))
11952 (if (not expert) (org-fit-window-to-buffer))
11953 (message "[a-z..]:Set [SPC]:clear")
11954 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11957 (and (= c ?q) (not (rassoc c fulltable))))
11958 (setq quit-flag t))
11960 ((setq e (rassoc c fulltable) tg (car e))
11962 (t (setq quit-flag t)))))))
11964 (defun org-entry-is-todo-p ()
11965 (member (org-get-todo-state) org-not-done-keywords))
11967 (defun org-entry-is-done-p ()
11968 (member (org-get-todo-state) org-done-keywords))
11970 (defun org-get-todo-state ()
11972 (org-back-to-heading t)
11973 (and (looking-at org-todo-line-regexp)
11975 (match-string 2))))
11977 (defun org-at-date-range-p (&optional inactive-ok)
11978 "Is the cursor inside a date range?"
11982 (let ((pos (point)))
11983 (skip-chars-backward "^[<\r\n")
11984 (skip-chars-backward "<[")
11985 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11986 (>= (match-end 0) pos)
11988 (skip-chars-backward "^<[\r\n")
11989 (skip-chars-backward "<[")
11990 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
11991 (>= (match-end 0) pos)
11995 (defun org-get-repeat (&optional tagline)
11996 "Check if there is a deadline/schedule with repeater in this entry."
11999 (org-back-to-heading t)
12000 (and (re-search-forward (if tagline
12001 (concat tagline "\\s-*" org-repeat-re)
12003 (org-entry-end-position) t)
12004 (match-string-no-properties 1)))))
12006 (defvar org-last-changed-timestamp)
12007 (defvar org-last-inserted-timestamp)
12008 (defvar org-log-post-message)
12009 (defvar org-log-note-purpose)
12010 (defvar org-log-note-how)
12011 (defvar org-log-note-extra)
12012 (defun org-auto-repeat-maybe (done-word)
12013 "Check if the current headline contains a repeated deadline/schedule.
12014 If yes, set TODO state back to what it was and change the base date
12015 of repeating deadline/scheduled time stamps to new date.
12016 This function is run automatically after each state change to a DONE state."
12017 ;; last-state is dynamically scoped into this function
12018 (let* ((repeat (org-get-repeat))
12019 (aa (assoc org-last-state org-todo-kwd-alist))
12020 (interpret (nth 1 aa))
12022 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12023 (msg "Entry repeats: ")
12025 (org-todo-log-states nil)
12026 re type n what ts time to-state)
12028 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12029 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12030 org-todo-repeat-to-state))
12031 (unless (and to-state (member to-state org-todo-keywords-1))
12032 (setq to-state (if (eq interpret 'type) org-last-state head)))
12033 (org-todo to-state)
12034 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12035 (org-entry-put nil "LAST_REPEAT" (format-time-string
12036 (org-time-stamp-format t t))))
12037 (when org-log-repeat
12038 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12039 (memq 'org-add-log-note post-command-hook))
12040 ;; OK, we are already setup for some record
12041 (if (eq org-log-repeat 'note)
12042 ;; make sure we take a note, not only a time stamp
12043 (setq org-log-note-how 'note))
12044 ;; Set up for taking a record
12045 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12047 'findpos org-log-repeat)))
12048 (org-back-to-heading t)
12049 (org-add-planning-info nil nil 'closed)
12050 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12051 org-deadline-time-regexp "\\)\\|\\("
12052 org-ts-regexp "\\)"))
12053 (while (re-search-forward
12054 re (save-excursion (outline-next-heading) (point)) t)
12055 (setq type (if (match-end 1) org-scheduled-string
12056 (if (match-end 3) org-deadline-string "Plain:"))
12057 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12058 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12059 (setq n (string-to-number (match-string 2 ts))
12060 what (match-string 3 ts))
12061 (if (equal what "w") (setq n (* n 7) what "d"))
12062 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12063 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12064 ;; Preparation, see if we need to modify the start date for the change
12065 (when (match-end 1)
12066 (setq time (save-match-data (org-time-string-to-time ts)))
12068 ((equal (match-string 1 ts) ".")
12069 ;; Shift starting date to today
12070 (org-timestamp-change
12071 (- (org-today) (time-to-days time))
12073 ((equal (match-string 1 ts) "+")
12074 (let ((nshiftmax 10) (nshift 0))
12075 (while (or (= nshift 0)
12076 (<= (time-to-days time)
12077 (time-to-days (current-time))))
12078 (when (= (incf nshift) nshiftmax)
12079 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12081 (org-timestamp-change n (cdr (assoc what whata)))
12082 (org-at-timestamp-p t)
12083 (setq ts (match-string 1))
12084 (setq time (save-match-data (org-time-string-to-time ts)))))
12085 (org-timestamp-change (- n) (cdr (assoc what whata)))
12086 ;; rematch, so that we have everything in place for the real shift
12087 (org-at-timestamp-p t)
12088 (setq ts (match-string 1))
12089 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12090 (org-timestamp-change n (cdr (assoc what whata)))
12091 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12092 (setq org-log-post-message msg)
12093 (message "%s" msg))))
12095 (defun org-show-todo-tree (arg)
12096 "Make a compact tree which shows all headlines marked with TODO.
12097 The tree will show the lines where the regexp matches, and all higher
12098 headlines above the match.
12099 With a \\[universal-argument] prefix, prompt for a regexp to match.
12100 With a numeric prefix N, construct a sparse tree for the Nth element
12101 of `org-todo-keywords-1'."
12103 (let ((case-fold-search nil)
12105 (cond ((null arg) org-not-done-regexp)
12107 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12108 (mapcar 'list org-todo-keywords-1))))
12110 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12112 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12113 (regexp-quote (nth (1- (prefix-numeric-value arg))
12114 org-todo-keywords-1)))
12115 (t (error "Invalid prefix argument: %s" arg)))))
12116 (message "%d TODO entries found"
12117 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12119 (defun org-deadline (&optional remove time)
12120 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12121 With argument REMOVE, remove any deadline from the item.
12122 With argument TIME, set the deadline at the corresponding date. TIME
12123 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12125 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12126 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12127 'region-start-level 'region))
12128 org-loop-over-headlines-in-active-region)
12130 `(org-deadline ',remove ,time)
12131 org-loop-over-headlines-in-active-region
12132 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12133 (let* ((old-date (org-entry-get nil "DEADLINE"))
12134 (repeater (and old-date
12136 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12138 (match-string 1 old-date))))
12141 (when (and old-date org-log-redeadline)
12142 (org-add-log-setup 'deldeadline nil old-date 'findpos
12143 org-log-redeadline))
12144 (org-remove-timestamp-with-keyword org-deadline-string)
12145 (message "Item no longer has a deadline."))
12146 (org-add-planning-info 'deadline time 'closed)
12147 (when (and old-date org-log-redeadline
12148 (not (equal old-date
12149 (substring org-last-inserted-timestamp 1 -1))))
12150 (org-add-log-setup 'redeadline nil old-date 'findpos
12151 org-log-redeadline))
12154 (org-back-to-heading t)
12155 (when (re-search-forward (concat org-deadline-string " "
12156 org-last-inserted-timestamp)
12158 (outline-next-heading) (point)) t)
12159 (goto-char (1- (match-end 0)))
12160 (insert " " repeater)
12161 (setq org-last-inserted-timestamp
12162 (concat (substring org-last-inserted-timestamp 0 -1)
12164 (substring org-last-inserted-timestamp -1))))))
12165 (message "Deadline on %s" org-last-inserted-timestamp)))))
12167 (defun org-schedule (&optional remove time)
12168 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12169 With argument REMOVE, remove any scheduling date from the item.
12170 With argument TIME, scheduled at the corresponding date. TIME can
12171 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12173 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12174 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12175 'region-start-level 'region))
12176 org-loop-over-headlines-in-active-region)
12178 `(org-schedule ',remove ,time)
12179 org-loop-over-headlines-in-active-region
12180 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12181 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12182 (repeater (and old-date
12184 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12186 (match-string 1 old-date))))
12189 (when (and old-date org-log-reschedule)
12190 (org-add-log-setup 'delschedule nil old-date 'findpos
12191 org-log-reschedule))
12192 (org-remove-timestamp-with-keyword org-scheduled-string)
12193 (message "Item is no longer scheduled."))
12194 (org-add-planning-info 'scheduled time 'closed)
12195 (when (and old-date org-log-reschedule
12196 (not (equal old-date
12197 (substring org-last-inserted-timestamp 1 -1))))
12198 (org-add-log-setup 'reschedule nil old-date 'findpos
12199 org-log-reschedule))
12202 (org-back-to-heading t)
12203 (when (re-search-forward (concat org-scheduled-string " "
12204 org-last-inserted-timestamp)
12206 (outline-next-heading) (point)) t)
12207 (goto-char (1- (match-end 0)))
12208 (insert " " repeater)
12209 (setq org-last-inserted-timestamp
12210 (concat (substring org-last-inserted-timestamp 0 -1)
12212 (substring org-last-inserted-timestamp -1))))))
12213 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12215 (defun org-get-scheduled-time (pom &optional inherit)
12216 "Get the scheduled time as a time tuple, of a format suitable
12217 for calling org-schedule with, or if there is no scheduling,
12219 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12221 (apply 'encode-time (org-parse-time-string time)))))
12223 (defun org-get-deadline-time (pom &optional inherit)
12224 "Get the deadline as a time tuple, of a format suitable for
12225 calling org-deadline with, or if there is no scheduling, returns
12227 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12229 (apply 'encode-time (org-parse-time-string time)))))
12231 (defun org-remove-timestamp-with-keyword (keyword)
12232 "Remove all time stamps with KEYWORD in the current entry."
12233 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12236 (org-back-to-heading t)
12238 (outline-next-heading)
12239 (while (re-search-backward re beg t)
12241 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12242 (equal (char-before) ?\ ))
12243 (backward-delete-char 1)
12244 (if (string-match "^[ \t]*$" (buffer-substring
12245 (point-at-bol) (point-at-eol)))
12246 (delete-region (point-at-bol)
12247 (min (point-max) (1+ (point-at-eol))))))))))
12249 (defun org-add-planning-info (what &optional time &rest remove)
12250 "Insert new timestamp with keyword in the line directly after the headline.
12251 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12252 If non is given, the user is prompted for a date.
12253 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12256 (let (org-time-was-given org-end-time-was-given ts
12257 end default-time default-input)
12260 (when (and (memq what '(scheduled deadline))
12262 (and (stringp time)
12263 (string-match "^[-+]+[0-9]" time))))
12264 ;; Try to get a default date/time from existing timestamp
12266 (org-back-to-heading t)
12267 (setq end (save-excursion (outline-next-heading) (point)))
12268 (when (re-search-forward (if (eq what 'scheduled)
12269 org-scheduled-time-regexp
12270 org-deadline-time-regexp)
12272 (setq ts (match-string 1)
12274 (apply 'encode-time (org-parse-time-string ts))
12275 default-input (and ts (org-get-compact-tod ts))))))
12279 ;; This is a string (relative or absolute), set proper date
12280 (apply 'encode-time
12281 (org-read-date-analyze
12282 time default-time (decode-time default-time)))
12283 ;; If necessary, get the time from the user
12284 (or time (org-read-date nil 'to-time nil nil
12285 default-time default-input)))))
12287 (when (and org-insert-labeled-timestamps-at-point
12288 (member what '(scheduled deadline)))
12290 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12291 (org-insert-time-stamp time org-time-was-given
12292 nil nil nil (list org-end-time-was-given))
12296 (let (col list elt ts buffer-invisibility-spec)
12297 (org-back-to-heading t)
12298 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12299 (goto-char (match-end 1))
12300 (setq col (current-column))
12301 (goto-char (match-end 0))
12302 (if (eobp) (insert "\n") (forward-char 1))
12303 (when (and (not what)
12306 org-keyword-time-not-clock-regexp))))
12307 ;; Nothing to add, nothing to remove...... :-)
12309 (if (and (not (looking-at org-outline-regexp))
12310 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12312 (not (equal (match-string 1) org-clock-string)))
12313 (narrow-to-region (match-beginning 0) (match-end 0))
12314 (insert-before-markers "\n")
12316 (narrow-to-region (point) (point))
12317 (and org-adapt-indentation (org-indent-to-column col)))
12318 ;; Check if we have to remove something.
12319 (setq list (cons what remove))
12321 (setq elt (pop list))
12322 (when (or (and (eq elt 'scheduled)
12323 (re-search-forward org-scheduled-time-regexp nil t))
12324 (and (eq elt 'deadline)
12325 (re-search-forward org-deadline-time-regexp nil t))
12326 (and (eq elt 'closed)
12327 (re-search-forward org-closed-time-regexp nil t)))
12329 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12330 (and (looking-at "[ \t]+") (replace-match ""))
12331 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12334 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12335 (cond ((eq what 'scheduled) org-scheduled-string)
12336 ((eq what 'deadline) org-deadline-string)
12337 ((eq what 'closed) org-closed-string))
12339 (setq ts (org-insert-time-stamp
12341 (or org-time-was-given
12342 (and (eq what 'closed) org-log-done-with-time))
12344 nil nil (list org-end-time-was-given)))
12346 (if (not (or (bolp) (eq (char-before) ?\ )
12347 (memq (char-after) '(32 10))
12350 (goto-char (point-min))
12352 (if (and (looking-at "[ \t]*\n")
12353 (equal (char-before) ?\n))
12354 (delete-region (1- (point)) (point-at-eol)))
12357 (defvar org-log-note-marker (make-marker))
12358 (defvar org-log-note-purpose nil)
12359 (defvar org-log-note-state nil)
12360 (defvar org-log-note-previous-state nil)
12361 (defvar org-log-note-how nil)
12362 (defvar org-log-note-extra nil)
12363 (defvar org-log-note-window-configuration nil)
12364 (defvar org-log-note-return-to (make-marker))
12365 (defvar org-log-note-effective-time nil
12366 "Remembered current time so that dynamically scoped
12367 `org-extend-today-until' affects tha timestamps in state change
12370 (defvar org-log-post-message nil
12371 "Message to be displayed after a log note has been stored.
12372 The auto-repeater uses this.")
12374 (defun org-add-note ()
12375 "Add a note to the current entry.
12376 This is done in the same way as adding a state change note."
12378 (org-add-log-setup 'note nil nil 'findpos nil))
12380 (defvar org-property-end-re)
12381 (defun org-add-log-setup (&optional purpose state prev-state
12383 "Set up the post command hook to take a note.
12384 If this is about to TODO state change, the new state is expected in STATE.
12385 When FINDPOS is non-nil, find the correct position for the note in
12386 the current entry. If not, assume that it can be inserted at point.
12387 HOW is an indicator what kind of note should be created.
12388 EXTRA is additional text that will be inserted into the notes buffer."
12389 (let* ((org-log-into-drawer (org-log-into-drawer))
12390 (drawer (cond ((stringp org-log-into-drawer)
12391 org-log-into-drawer)
12392 (org-log-into-drawer "LOGBOOK")
12397 (org-back-to-heading t)
12398 (narrow-to-region (point) (save-excursion
12399 (outline-next-heading) (point)))
12400 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12401 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12403 (goto-char (match-end 0))
12406 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12409 (goto-char (match-end 0))
12410 (or org-log-states-order-reversed
12411 (and (re-search-forward org-property-end-re nil t)
12412 (goto-char (1- (match-beginning 0))))))
12413 (insert "\n:" drawer ":\n:END:")
12414 (beginning-of-line 0)
12416 (beginning-of-line 2)
12419 ((and org-log-state-notes-insert-after-drawers
12421 (forward-line) (looking-at org-drawer-regexp)))
12423 (while (looking-at org-drawer-regexp)
12424 (goto-char (match-end 0))
12425 (re-search-forward org-property-end-re (point-max) t)
12427 (forward-line -1)))
12428 (unless org-log-states-order-reversed
12429 (and (= (char-after) ?\n) (forward-char 1))
12430 (org-skip-over-state-notes)
12431 (skip-chars-backward " \t\n\r")))
12432 (move-marker org-log-note-marker (point))
12433 (setq org-log-note-purpose purpose
12434 org-log-note-state state
12435 org-log-note-previous-state prev-state
12436 org-log-note-how how
12437 org-log-note-extra extra
12438 org-log-note-effective-time (org-current-effective-time))
12439 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12441 (defun org-skip-over-state-notes ()
12442 "Skip past the list of State notes in an entry."
12443 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12444 (when (ignore-errors (goto-char (org-in-item-p)))
12445 (let* ((struct (org-list-struct))
12446 (prevs (org-list-prevs-alist struct)))
12447 (while (looking-at "[ \t]*- State")
12448 (goto-char (or (org-list-get-next-item (point) struct prevs)
12449 (org-list-get-item-end (point) struct)))))))
12451 (defun org-add-log-note (&optional purpose)
12452 "Pop up a window for taking a note, and add this note later at point."
12453 (remove-hook 'post-command-hook 'org-add-log-note)
12454 (setq org-log-note-window-configuration (current-window-configuration))
12455 (delete-other-windows)
12456 (move-marker org-log-note-return-to (point))
12457 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12458 (goto-char org-log-note-marker)
12459 (org-switch-to-buffer-other-window "*Org Note*")
12461 (if (memq org-log-note-how '(time state))
12462 (let (current-prefix-arg) (org-store-log-note))
12463 (let ((org-inhibit-startup t)) (org-mode))
12464 (insert (format "# Insert note for %s.
12465 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12467 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12468 ((eq org-log-note-purpose 'done) "closed todo item")
12469 ((eq org-log-note-purpose 'state)
12470 (format "state change from \"%s\" to \"%s\""
12471 (or org-log-note-previous-state "")
12472 (or org-log-note-state "")))
12473 ((eq org-log-note-purpose 'reschedule)
12475 ((eq org-log-note-purpose 'delschedule)
12476 "no longer scheduled")
12477 ((eq org-log-note-purpose 'redeadline)
12478 "changing deadline")
12479 ((eq org-log-note-purpose 'deldeadline)
12480 "removing deadline")
12481 ((eq org-log-note-purpose 'refile)
12483 ((eq org-log-note-purpose 'note)
12485 (t (error "This should not happen")))))
12486 (if org-log-note-extra (insert org-log-note-extra))
12487 (org-set-local 'org-finish-function 'org-store-log-note)
12488 (run-hooks 'org-log-buffer-setup-hook)))
12490 (defvar org-note-abort nil) ; dynamically scoped
12491 (defun org-store-log-note ()
12492 "Finish taking a log note, and insert it to where it belongs."
12493 (let ((txt (buffer-string))
12494 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12496 (kill-buffer (current-buffer))
12497 (while (string-match "\\`#.*\n[ \t\n]*" txt)
12498 (setq txt (replace-match "" t t txt)))
12499 (if (string-match "\\s-+\\'" txt)
12500 (setq txt (replace-match "" t t txt)))
12501 (setq lines (org-split-string txt "\n"))
12502 (when (and note (string-match "\\S-" note))
12504 (org-replace-escapes
12506 (list (cons "%u" (user-login-name))
12507 (cons "%U" user-full-name)
12508 (cons "%t" (format-time-string
12509 (org-time-stamp-format 'long 'inactive)
12510 org-log-note-effective-time))
12511 (cons "%T" (format-time-string
12512 (org-time-stamp-format 'long nil)
12513 org-log-note-effective-time))
12514 (cons "%d" (format-time-string
12515 (org-time-stamp-format nil 'inactive)
12516 org-log-note-effective-time))
12517 (cons "%D" (format-time-string
12518 (org-time-stamp-format nil nil)
12519 org-log-note-effective-time))
12520 (cons "%s" (if org-log-note-state
12521 (concat "\"" org-log-note-state "\"")
12523 (cons "%S" (if org-log-note-previous-state
12524 (concat "\"" org-log-note-previous-state "\"")
12526 (if lines (setq note (concat note " \\\\")))
12528 (when (or current-prefix-arg org-note-abort)
12529 (when org-log-into-drawer
12530 (org-remove-empty-drawer-at
12531 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12532 org-log-note-marker))
12535 (with-current-buffer (marker-buffer org-log-note-marker)
12537 (goto-char org-log-note-marker)
12538 (move-marker org-log-note-marker nil)
12540 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12541 (setq ind (save-excursion
12542 (if (ignore-errors (goto-char (org-in-item-p)))
12543 (let ((struct (org-list-struct)))
12545 (org-list-get-top-point struct) struct))
12546 (skip-chars-backward " \r\t\n")
12548 ((and (org-at-heading-p)
12549 org-adapt-indentation)
12550 (1+ (org-current-level)))
12551 ((org-at-heading-p) 0)
12552 (t (org-get-indentation))))))
12553 (setq bul (org-list-bullet-string "-"))
12554 (org-indent-line-to ind)
12555 (insert bul (pop lines))
12556 (let ((ind-body (+ (length bul) ind)))
12559 (org-indent-line-to ind-body)
12560 (insert (pop lines))))
12561 (message "Note stored")
12562 (org-back-to-heading t)
12563 (org-cycle-hide-drawers 'children)))))
12564 (set-window-configuration org-log-note-window-configuration)
12565 (with-current-buffer (marker-buffer org-log-note-return-to)
12566 (goto-char org-log-note-return-to))
12567 (move-marker org-log-note-return-to nil)
12568 (and org-log-post-message (message "%s" org-log-post-message)))
12570 (defun org-remove-empty-drawer-at (drawer pos)
12571 "Remove an empty drawer DRAWER at position POS.
12572 POS may also be a marker."
12573 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12579 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12580 (replace-match ""))))))
12582 (defun org-sparse-tree (&optional arg)
12583 "Create a sparse tree, prompt for the details.
12584 This command can create sparse trees. You first need to select the type
12585 of match used to create the tree:
12587 t Show all TODO entries.
12588 T Show entries with a specific TODO keyword.
12589 m Show entries selected by a tags/property match.
12590 p Enter a property name and its value (both with completion on existing
12591 names/values) and show entries with that property.
12592 r Show entries matching a regular expression (`/' can be used as well)
12593 d Show deadlines due within `org-deadline-warning-days'.
12594 b Show deadlines and scheduled items before a date.
12595 a Show deadlines and scheduled items after a date."
12597 (let (ans kwd value)
12598 (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range")
12599 (setq ans (read-char-exclusive))
12602 (call-interactively 'org-check-deadlines))
12604 (call-interactively 'org-check-before-date))
12606 (call-interactively 'org-check-after-date))
12608 (call-interactively 'org-check-dates-range))
12610 (org-show-todo-tree nil))
12612 (org-show-todo-tree '(4)))
12613 ((member ans '(?T ?m))
12614 (call-interactively 'org-match-sparse-tree))
12615 ((member ans '(?p ?P))
12616 (setq kwd (org-icompleting-read "Property: "
12617 (mapcar 'list (org-buffer-property-keys))))
12618 (setq value (org-icompleting-read "Value: "
12619 (mapcar 'list (org-property-values kwd))))
12620 (unless (string-match "\\`{.*}\\'" value)
12621 (setq value (concat "\"" value "\"")))
12622 (org-match-sparse-tree arg (concat kwd "=" value)))
12623 ((member ans '(?r ?R ?/))
12624 (call-interactively 'org-occur))
12625 (t (error "No such sparse tree command \"%c\"" ans)))))
12627 (defvar org-occur-highlights nil
12628 "List of overlays used for occur matches.")
12629 (make-variable-buffer-local 'org-occur-highlights)
12630 (defvar org-occur-parameters nil
12631 "Parameters of the active org-occur calls.
12632 This is a list, each call to org-occur pushes as cons cell,
12633 containing the regular expression and the callback, onto the list.
12634 The list can contain several entries if `org-occur' has been called
12635 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12636 will only contain one set of parameters. When the highlights are
12637 removed (for example with `C-c C-c', or with the next edit (depending
12638 on `org-remove-highlights-with-change'), this variable is emptied
12640 (make-variable-buffer-local 'org-occur-parameters)
12642 (defun org-occur (regexp &optional keep-previous callback)
12643 "Make a compact tree which shows all matches of REGEXP.
12644 The tree will show the lines where the regexp matches, and all higher
12645 headlines above the match. It will also show the heading after the match,
12646 to make sure editing the matching entry is easy.
12647 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12648 call to `org-occur' will be kept, to allow stacking of calls to this
12650 If CALLBACK is non-nil, it is a function which is called to confirm
12651 that the match should indeed be shown."
12652 (interactive "sRegexp: \nP")
12653 (when (equal regexp "")
12654 (error "Regexp cannot be empty"))
12655 (unless keep-previous
12656 (org-remove-occur-highlights nil nil t))
12657 (push (cons regexp callback) org-occur-parameters)
12660 (goto-char (point-min))
12661 (if (or (not keep-previous) ; do not want to keep
12662 (not org-occur-highlights)) ; no previous matches
12665 (while (re-search-forward regexp nil t)
12666 (when (or (not callback)
12667 (save-match-data (funcall callback)))
12668 (setq cnt (1+ cnt))
12669 (when org-highlight-sparse-tree-matches
12670 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12671 (org-show-context 'occur-tree))))
12672 (when org-remove-highlights-with-change
12673 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12675 (unless org-sparse-tree-open-archived-trees
12676 (org-hide-archived-subtrees (point-min) (point-max)))
12677 (run-hooks 'org-occur-hook)
12678 (if (org-called-interactively-p 'interactive)
12679 (message "%d match(es) for regexp %s" cnt regexp))
12682 (defun org-occur-next-match (&optional n reset)
12683 "Function for `next-error-function' to find sparse tree matches.
12684 N is the number of matches to move, when negative move backwards.
12685 RESET is entirely ignored - this function always goes back to the
12686 starting point when no match is found."
12687 (let* ((limit (if (< n 0) (point-min) (point-max)))
12688 (search-func (if (< n 0)
12689 'previous-single-char-property-change
12690 'next-single-char-property-change))
12695 (while (setq p1 (funcall search-func (point) 'org-type))
12696 (when (equal p1 limit)
12698 (error "No more matches"))
12699 (when (equal (get-char-property p1 'org-type) 'org-occur)
12703 (throw 'exit (point))))
12706 (error "No more matches"))))
12708 (defun org-show-context (&optional key)
12709 "Make sure point and context are visible.
12710 How much context is shown depends upon the variables
12711 `org-show-hierarchy-above', `org-show-following-heading',
12712 `org-show-entry-below' and `org-show-siblings'."
12713 (let ((heading-p (org-at-heading-p t))
12714 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12715 (following-p (org-get-alist-option org-show-following-heading key))
12716 (entry-p (org-get-alist-option org-show-entry-below key))
12717 (siblings-p (org-get-alist-option org-show-siblings key)))
12719 ;; Show heading or entry text
12720 (if (and heading-p (not entry-p))
12721 (org-flag-heading nil) ; only show the heading
12722 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12723 (org-show-hidden-entry))) ; show entire entry
12725 ;; Show next sibling, or heading below text
12727 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12728 (org-flag-heading nil))))
12729 (when siblings-p (org-show-siblings))
12731 ;; show all higher headings, possibly with siblings
12733 (while (and (condition-case nil
12734 (progn (org-up-heading-all 1) t)
12737 (org-flag-heading nil)
12738 (when siblings-p (org-show-siblings))))))))
12740 (defvar org-reveal-start-hook nil
12741 "Hook run before revealing a location.")
12743 (defun org-reveal (&optional siblings)
12744 "Show current entry, hierarchy above it, and the following headline.
12745 This can be used to show a consistent set of context around locations
12746 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12747 not t for the search context.
12749 With optional argument SIBLINGS, on each level of the hierarchy all
12750 siblings are shown. This repairs the tree structure to what it would
12751 look like when opened with hierarchical calls to `org-cycle'.
12752 With double optional argument \\[universal-argument] \\[universal-argument], \
12753 go to the parent and show the
12756 (run-hooks 'org-reveal-start-hook)
12757 (let ((org-show-hierarchy-above t)
12758 (org-show-following-heading t)
12759 (org-show-siblings (if siblings t org-show-siblings)))
12760 (org-show-context nil))
12761 (when (equal siblings '(16))
12763 (when (org-up-heading-safe)
12765 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12767 (defun org-highlight-new-match (beg end)
12768 "Highlight from BEG to END and mark the highlight is an occur headline."
12769 (let ((ov (make-overlay beg end)))
12770 (overlay-put ov 'face 'secondary-selection)
12771 (overlay-put ov 'org-type 'org-occur)
12772 (push ov org-occur-highlights)))
12774 (defun org-remove-occur-highlights (&optional beg end noremove)
12775 "Remove the occur highlights from the buffer.
12776 BEG and END are ignored. If NOREMOVE is nil, remove this function
12777 from the `before-change-functions' in the current buffer."
12779 (unless org-inhibit-highlight-removal
12780 (mapc 'delete-overlay org-occur-highlights)
12781 (setq org-occur-highlights nil)
12782 (setq org-occur-parameters nil)
12784 (remove-hook 'before-change-functions
12785 'org-remove-occur-highlights 'local))))
12789 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12790 "Regular expression matching the priority indicator.")
12792 (defvar org-remove-priority-next-time nil)
12794 (defun org-priority-up ()
12795 "Increase the priority of the current item."
12797 (org-priority 'up))
12799 (defun org-priority-down ()
12800 "Decrease the priority of the current item."
12802 (org-priority 'down))
12804 (defun org-priority (&optional action)
12805 "Change the priority of an item by ARG.
12806 ACTION can be `set', `up', `down', or a character."
12808 (unless org-enable-priority-commands
12809 (error "Priority commands are disabled"))
12810 (setq action (or action 'set))
12811 (let (current new news have remove)
12813 (org-back-to-heading t)
12814 (if (looking-at org-priority-regexp)
12815 (setq current (string-to-char (match-string 2))
12818 ((eq action 'remove)
12819 (setq remove t new ?\ ))
12820 ((or (eq action 'set)
12821 (if (featurep 'xemacs) (characterp action) (integerp action)))
12822 (if (not (eq action 'set))
12824 (message "Priority %c-%c, SPC to remove: "
12825 org-highest-priority org-lowest-priority)
12827 (setq new (read-char-exclusive))))
12828 (if (and (= (upcase org-highest-priority) org-highest-priority)
12829 (= (upcase org-lowest-priority) org-lowest-priority))
12830 (setq new (upcase new)))
12831 (cond ((equal new ?\ ) (setq remove t))
12832 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12833 (error "Priority must be between `%c' and `%c'"
12834 org-highest-priority org-lowest-priority))))
12837 (1- current) ; normal cycling
12838 ;; last priority was empty
12839 (if (eq last-command this-command)
12840 org-lowest-priority ; wrap around empty to lowest
12842 (if org-priority-start-cycle-with-default
12843 org-default-priority
12844 (1- org-default-priority))))))
12847 (1+ current) ; normal cycling
12848 ;; last priority was empty
12849 (if (eq last-command this-command)
12850 org-highest-priority ; wrap around empty to highest
12852 (if org-priority-start-cycle-with-default
12853 org-default-priority
12854 (1+ org-default-priority))))))
12855 (t (error "Invalid action")))
12856 (if (or (< (upcase new) org-highest-priority)
12857 (> (upcase new) org-lowest-priority))
12858 (if (and (memq action '(up down))
12859 (not have) (not (eq last-command this-command)))
12860 ;; `new' is from default priority
12862 "The default can not be set, see `org-default-priority' why")
12863 ;; normal cycling: `new' is beyond highest/lowest priority
12864 ;; and is wrapped around to the empty priority
12866 (setq news (format "%c" new))
12869 (replace-match "" t t nil 1)
12870 (replace-match news t t nil 2))
12872 (error "No priority cookie found in line")
12873 (let ((case-fold-search nil))
12874 (looking-at org-todo-line-regexp))
12877 (goto-char (match-end 2))
12878 (insert " [#" news "]"))
12879 (goto-char (match-beginning 3))
12880 (insert "[#" news "] "))))
12881 (org-preserve-lc (org-set-tags nil 'align)))
12883 (message "Priority removed")
12884 (message "Priority of current item set to %s" news))))
12886 (defun org-get-priority (s)
12887 "Find priority cookie and return priority."
12888 (if (functionp org-get-priority-function)
12889 (funcall org-get-priority-function)
12891 (if (not (string-match org-priority-regexp s))
12892 (* 1000 (- org-lowest-priority org-default-priority))
12893 (* 1000 (- org-lowest-priority
12894 (string-to-char (match-string 2 s))))))))
12898 (defvar org-agenda-archives-mode)
12899 (defvar org-map-continue-from nil
12900 "Position from where mapping should continue.
12901 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
12903 (defvar org-scanner-tags nil
12904 "The current tag list while the tags scanner is running.")
12905 (defvar org-trust-scanner-tags nil
12906 "Should `org-get-tags-at' use the tags for the scanner.
12907 This is for internal dynamical scoping only.
12908 When this is non-nil, the function `org-get-tags-at' will return the value
12909 of `org-scanner-tags' instead of building the list by itself. This
12910 can lead to large speed-ups when the tags scanner is used in a file with
12911 many entries, and when the list of tags is retrieved, for example to
12912 obtain a list of properties. Building the tags list for each entry in such
12913 a file becomes an N^2 operation - but with this variable set, it scales
12916 (defun org-scan-tags (action matcher todo-only &optional start-level)
12917 "Scan headline tags with inheritance and produce output ACTION.
12919 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
12920 or `agenda' to produce an entry list for an agenda view. It can also be
12921 a Lisp form or a function that should be called at each matched headline, in
12922 this case the return value is a list of all return values from these calls.
12924 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
12925 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
12926 only lines with a not-done TODO keyword are included in the output.
12927 This should be the same variable that was scoped into
12928 and set by `org-make-tags-matcher' when it constructed MATCHER.
12930 START-LEVEL can be a string with asterisks, reducing the scope to
12931 headlines matching this string."
12932 (require 'org-agenda)
12933 (let* ((re (concat "^"
12935 ;; Get the correct level to match
12936 (concat "\\*\\{" (number-to-string start-level) "\\} ")
12937 org-outline-regexp)
12939 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
12940 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
12941 (props (list 'face 'default
12942 'done-face 'org-agenda-done
12943 'undone-face 'default
12944 'mouse-face 'highlight
12945 'org-not-done-regexp org-not-done-regexp
12946 'org-todo-regexp org-todo-regexp
12947 'org-complex-heading-regexp org-complex-heading-regexp
12949 (format "mouse-2 or RET jump to org file %s"
12950 (abbreviate-file-name
12951 (or (buffer-file-name (buffer-base-buffer))
12952 (buffer-name (buffer-base-buffer)))))))
12953 (case-fold-search nil)
12954 (org-map-continue-from nil)
12955 lspos tags tags-list
12956 (tags-alist (list (cons 0 org-file-tags)))
12957 (llast 0) rtn rtn1 level category i txt
12958 todo marker entry priority)
12959 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
12960 (setq action (list 'lambda nil action)))
12962 (goto-char (point-min))
12963 (when (eq action 'sparse-tree)
12965 (org-remove-occur-highlights))
12966 (while (re-search-forward re nil t)
12967 (setq org-map-continue-from nil)
12969 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
12970 tags (if (match-end 4) (org-match-string-no-properties 4)))
12971 (goto-char (setq lspos (match-beginning 0)))
12972 (setq level (org-reduced-level (funcall outline-level))
12973 category (org-get-category))
12974 (setq i llast llast level)
12975 ;; remove tag lists from same and sublevels
12976 (while (>= i level)
12977 (when (setq entry (assoc i tags-alist))
12978 (setq tags-alist (delete entry tags-alist)))
12980 ;; add the next tags
12982 (setq tags (org-split-string tags ":")
12984 (cons (cons level tags) tags-alist)))
12985 ;; compile tags for current headline
12987 (if org-use-tag-inheritance
12988 (apply 'append (mapcar 'cdr (reverse tags-alist)))
12990 org-scanner-tags tags-list)
12991 (when org-use-tag-inheritance
12992 (setcdr (car tags-alist)
12993 (mapcar (lambda (x)
12994 (setq x (copy-sequence x))
12995 (org-add-prop-inherited x))
12996 (cdar tags-alist))))
12997 (when (and tags org-use-tag-inheritance
12998 (or (not (eq t org-use-tag-inheritance))
12999 org-tags-exclude-from-inheritance))
13000 ;; selective inheritance, remove uninherited ones
13001 (setcdr (car tags-alist)
13002 (org-remove-uninherited-tags (cdar tags-alist))))
13005 ;; eval matcher only when the todo condition is OK
13006 (and (or (not todo-only) (member todo org-not-done-keywords))
13007 (let ((case-fold-search t) (org-trust-scanner-tags t))
13010 ;; Call the skipper, but return t if it does not skip,
13011 ;; so that the `and' form continues evaluating
13013 (unless (eq action 'sparse-tree) (org-agenda-skip))
13016 ;; Check if timestamps are deselecting this entry
13017 (or (not todo-only)
13018 (and (member todo org-not-done-keywords)
13019 (or (not org-agenda-tags-todo-honor-ignore-options)
13020 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
13022 ;; Extra check for the archive tag
13023 ;; FIXME: Does the skipper already do this????
13025 (not (member org-archive-tag tags-list))
13026 ;; we have an archive tag, should we use this anyway?
13027 (or (not org-agenda-skip-archived-trees)
13028 (and (eq action 'agenda) org-agenda-archives-mode))))
13030 ;; select this headline
13033 ((eq action 'sparse-tree)
13034 (and org-highlight-sparse-tree-matches
13035 (org-get-heading) (match-end 0)
13036 (org-highlight-new-match
13037 (match-beginning 1) (match-end 1)))
13038 (org-show-context 'tags-tree))
13039 ((eq action 'agenda)
13040 (setq txt (org-agenda-format-item
13043 (if (eq org-tags-match-list-sublevels 'indented)
13044 (make-string (1- level) ?.) "")
13048 priority (org-get-priority txt))
13050 (setq marker (org-agenda-new-marker))
13051 (org-add-props txt props
13052 'org-marker marker 'org-hd-marker marker 'org-category category
13054 'priority priority 'type "tagsmatch")
13056 ((functionp action)
13057 (setq org-map-continue-from nil)
13059 (setq rtn1 (funcall action))
13061 (t (error "Invalid action")))
13063 ;; if we are to skip sublevels, jump to end of subtree
13064 (unless org-tags-match-list-sublevels
13065 (org-end-of-subtree t)
13066 (backward-char 1))))
13067 ;; Get the correct position from where to continue
13068 (if org-map-continue-from
13069 (goto-char org-map-continue-from)
13070 (and (= (point) lspos) (end-of-line 1)))))
13071 (when (and (eq action 'sparse-tree)
13072 (not org-sparse-tree-open-archived-trees))
13073 (org-hide-archived-subtrees (point-min) (point-max)))
13076 (defun org-remove-uninherited-tags (tags)
13077 "Remove all tags that are not inherited from the list TAGS."
13079 ((eq org-use-tag-inheritance t)
13080 (if org-tags-exclude-from-inheritance
13081 (org-delete-all org-tags-exclude-from-inheritance tags)
13083 ((not org-use-tag-inheritance) nil)
13084 ((stringp org-use-tag-inheritance)
13087 (if (and (string-match org-use-tag-inheritance x)
13088 (not (member x org-tags-exclude-from-inheritance)))
13091 ((listp org-use-tag-inheritance)
13094 (if (member x org-use-tag-inheritance) x nil))
13097 (defun org-match-sparse-tree (&optional todo-only match)
13098 "Create a sparse tree according to tags string MATCH.
13099 MATCH can contain positive and negative selection of tags, like
13100 \"+WORK+URGENT-WITHBOSS\".
13101 If optional argument TODO-ONLY is non-nil, only select lines that are
13104 (org-prepare-agenda-buffers (list (current-buffer)))
13105 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13107 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13109 (defvar org-cached-props nil)
13110 (defun org-cached-entry-get (pom property)
13111 (if (or (eq t org-use-property-inheritance)
13112 (and (stringp org-use-property-inheritance)
13113 (string-match org-use-property-inheritance property))
13114 (and (listp org-use-property-inheritance)
13115 (member property org-use-property-inheritance)))
13116 ;; Caching is not possible, check it directly
13117 (org-entry-get pom property 'inherit)
13118 ;; Get all properties, so that we can do complicated checks easily
13119 (cdr (assoc property (or org-cached-props
13120 (setq org-cached-props
13121 (org-entry-properties pom)))))))
13123 (defun org-global-tags-completion-table (&optional files)
13124 "Return the list of all tags in all agenda buffer/files.
13125 Optional FILES argument is a list of files to which can be used
13126 instead of the agenda files."
13133 (set-buffer (find-file-noselect file))
13134 (append (org-get-buffer-tags)
13135 (mapcar (lambda (x) (if (stringp (car-safe x))
13136 (list (car-safe x)) nil))
13138 (if (and files (car files))
13140 (org-agenda-files))))))))
13142 (defun org-make-tags-matcher (match)
13143 "Create the TAGS/TODO matcher form for the selection string MATCH.
13145 The variable `todo-only' is scoped dynamically into this function; it will be
13146 set to t if the matcher restricts matching to TODO entries,
13147 otherwise will not be touched.
13149 Returns a cons of the selection string MATCH and the constructed
13150 lisp form implementing the matcher. The matcher is to be
13151 evaluated at an Org entry, with point on the headline,
13152 and returns t if the entry matches the
13153 selection string MATCH. The returned lisp form references
13154 two variables with information about the entry, which must be
13155 bound around the form's evaluation: todo, the TODO keyword at the
13156 entry (or nil of none); and tags-list, the list of all tags at the
13157 entry including inherited ones. Additionally, the category
13158 of the entry (if any) must be specified as the text property
13159 'org-category on the headline.
13161 See also `org-scan-tags'.
13163 (declare (special todo-only))
13164 (unless (boundp 'todo-only)
13165 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13167 ;; Get a new match request, with completion
13168 (let ((org-last-tags-completion-table
13169 (org-global-tags-completion-table)))
13170 (setq match (org-completing-read-no-i
13171 "Match: " 'org-tags-completion-function nil nil nil
13172 'org-tags-history))))
13174 ;; Parse the string and create a lisp form
13175 (let ((match0 match)
13176 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13178 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13179 orterms term orlist re-p str-p level-p level-op time-p
13180 prop-p pn pv po gv rest)
13181 (if (string-match "/+" match)
13182 ;; match contains also a todo-matching request
13184 (setq tagsmatch (substring match 0 (match-beginning 0))
13185 todomatch (substring match (match-end 0)))
13186 (if (string-match "^!" todomatch)
13187 (setq todo-only t todomatch (substring todomatch 1)))
13188 (if (string-match "^\\s-*$" todomatch)
13189 (setq todomatch nil)))
13190 ;; only matching tags
13191 (setq tagsmatch match todomatch nil))
13193 ;; Make the tags matcher
13194 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13195 (setq tagsmatcher t)
13196 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13197 (while (setq term (pop orterms))
13198 (while (and (equal (substring term -1) "\\") orterms)
13199 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13200 (while (string-match re term)
13201 (setq rest (substring term (match-end 0))
13202 minus (and (match-end 1)
13203 (equal (match-string 1 term) "-"))
13204 tag (save-match-data (replace-regexp-in-string
13206 (match-string 2 term)))
13207 re-p (equal (string-to-char tag) ?{)
13208 level-p (match-end 4)
13209 prop-p (match-end 5)
13211 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13213 (setq level-op (org-op-to-function (match-string 3 term)))
13214 `(,level-op level ,(string-to-number
13215 (match-string 4 term))))
13217 (setq pn (match-string 5 term)
13218 po (match-string 6 term)
13219 pv (match-string 7 term)
13220 re-p (equal (string-to-char pv) ?{)
13221 str-p (equal (string-to-char pv) ?\")
13222 time-p (save-match-data
13223 (string-match "^\"[[<].*[]>]\"$" pv))
13224 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13225 (if time-p (setq pv (org-matcher-time pv)))
13226 (setq po (org-op-to-function po (if time-p 'time str-p)))
13228 ((equal pn "CATEGORY")
13229 (setq gv '(get-text-property (point) 'org-category)))
13233 (setq gv `(org-cached-entry-get nil ,pn))))
13236 `(not (string-match ,pv (or ,gv "")))
13237 `(string-match ,pv (or ,gv "")))
13239 `(,po (or ,gv "") ,pv)
13240 `(,po (string-to-number (or ,gv ""))
13241 ,(string-to-number pv) ))))
13242 (t `(member ,tag tags-list)))
13243 mm (if minus (list 'not mm) mm)
13245 (push mm tagsmatcher))
13246 (push (if (> (length tagsmatcher) 1)
13247 (cons 'and tagsmatcher)
13250 (setq tagsmatcher nil))
13251 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13253 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13254 ;; Make the todo matcher
13255 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13256 (setq todomatcher t)
13257 (setq orterms (org-split-string todomatch "|") orlist nil)
13258 (while (setq term (pop orterms))
13259 (while (string-match re term)
13260 (setq minus (and (match-end 1)
13261 (equal (match-string 1 term) "-"))
13262 kwd (match-string 2 term)
13263 re-p (equal (string-to-char kwd) ?{)
13264 term (substring term (match-end 0))
13266 `(string-match ,(substring kwd 1 -1) todo)
13267 (list 'equal 'todo kwd))
13268 mm (if minus (list 'not mm) mm))
13269 (push mm todomatcher))
13270 (push (if (> (length todomatcher) 1)
13271 (cons 'and todomatcher)
13274 (setq todomatcher nil))
13275 (setq todomatcher (if (> (length orlist) 1)
13276 (cons 'or orlist) (car orlist))))
13278 ;; Return the string and lisp forms of the matcher
13279 (setq matcher (if todomatcher
13280 (list 'and tagsmatcher todomatcher)
13283 (setq matcher (list 'and '(member todo org-not-done-keywords)
13285 (cons match0 matcher)))
13287 (defun org-op-to-function (op &optional stringp)
13288 "Turn an operator into the appropriate function."
13291 ((equal op "<" ) '(< string< org-time<))
13292 ((equal op ">" ) '(> org-string> org-time>))
13293 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13294 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13295 ((member op '("=" "==")) '(= string= org-time=))
13296 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13297 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13299 (defun org<> (a b) (not (= a b)))
13300 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13301 (defun org-string>= (a b) (not (string< a b)))
13302 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13303 (defun org-string<> (a b) (not (string= a b)))
13304 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13305 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13306 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13307 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13308 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13309 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13311 "Convert S to a floating point time.
13312 If S is already a number, just return it. If it is a string, parse
13313 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13317 (condition-case nil
13318 (float-time (apply 'encode-time (org-parse-time-string s)))
13322 (defun org-time-today ()
13323 "Time in seconds today at 0:00.
13324 Returns the float number of seconds since the beginning of the
13325 epoch to the beginning of today (00:00)."
13326 (float-time (apply 'encode-time
13327 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13329 (defun org-matcher-time (s)
13330 "Interpret a time comparison value."
13333 ((string= s "<now>") (float-time))
13334 ((string= s "<today>") (org-time-today))
13335 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13336 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13337 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13338 (+ (org-time-today)
13339 (* (string-to-number (match-string 1 s))
13340 (cdr (assoc (match-string 2 s)
13341 '(("d" . 86400.0) ("w" . 604800.0)
13342 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13345 (defun org-match-any-p (re list)
13346 "Does re match any element of list?"
13347 (setq list (mapcar (lambda (x) (string-match re x)) list))
13350 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13351 (defvar org-tags-overlay (make-overlay 1 1))
13352 (org-detach-overlay org-tags-overlay)
13354 (defun org-get-local-tags-at (&optional pos)
13355 "Get a list of tags defined in the current headline."
13356 (org-get-tags-at pos 'local))
13358 (defun org-get-local-tags ()
13359 "Get a list of tags defined in the current headline."
13360 (org-get-tags-at nil 'local))
13362 (defun org-get-tags-at (&optional pos local)
13363 "Get a list of all headline tags applicable at POS.
13364 POS defaults to point. If tags are inherited, the list contains
13365 the targets in the same sequence as the headlines appear, i.e.
13366 the tags of the current headline come last.
13367 When LOCAL is non-nil, only return tags from the current headline,
13368 ignore inherited ones."
13370 (if (and org-trust-scanner-tags
13371 (or (not pos) (equal pos (point)))
13374 (let (tags ltags lastpos parent)
13378 (goto-char (or pos (point)))
13381 (condition-case nil
13383 (org-back-to-heading t)
13384 (while (not (equal lastpos (point)))
13385 (setq lastpos (point))
13387 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13388 (setq ltags (org-split-string
13389 (org-match-string-no-properties 1) ":"))
13391 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13394 (org-remove-uninherited-tags ltags)
13397 (or org-use-tag-inheritance (throw 'done t))
13398 (if local (throw 'done t))
13399 (or (org-up-heading-safe) (error nil))
13404 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13406 (defun org-add-prop-inherited (s)
13407 (add-text-properties 0 (length s) '(inherited t) s)
13410 (defun org-toggle-tag (tag &optional onoff)
13411 "Toggle the tag TAG for the current line.
13412 If ONOFF is `on' or `off', don't toggle but set to this state."
13415 (org-back-to-heading t)
13416 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13419 (setq current (match-string 1))
13420 (replace-match ""))
13422 (setq current (nreverse (org-split-string current ":")))
13426 (or (member tag current) (push tag current)))
13428 (or (not (member tag current)) (setq current (delete tag current))))
13429 (t (if (member tag current)
13430 (setq current (delete tag current))
13432 (push tag current))))
13436 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13437 (org-set-tags nil t))
13438 (delete-horizontal-space))
13439 (run-hooks 'org-after-tags-change-hook))
13442 (defun org-align-tags-here (to-col)
13443 ;; Assumes that this is a headline
13444 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13445 (beginning-of-line 1)
13446 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13447 (< pos (match-beginning 2)))
13449 (setq tags-l (- (match-end 2) (match-beginning 2)))
13450 (goto-char (match-beginning 1))
13452 (delete-region (point) (1+ (match-beginning 2)))
13453 (setq ncol (max (current-column)
13457 (- (abs to-col) tags-l))))
13459 (insert (make-string (- ncol (current-column)) ?\ ))
13460 (setq ncol (current-column))
13461 (when indent-tabs-mode (tabify p (point-at-eol)))
13462 (org-move-to-column (min ncol col) t))
13465 (defun org-set-tags-command (&optional arg just-align)
13466 "Call the set-tags command for the current entry."
13468 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13469 (org-set-tags arg just-align)
13471 (org-back-to-heading t)
13472 (org-set-tags arg just-align))))
13474 (defun org-set-tags-to (data)
13475 "Set the tags of the current entry to DATA, replacing the current tags.
13476 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13477 If DATA is nil or the empty string, any tags will be removed."
13478 (interactive "sTags: ")
13482 ((equal data "") "")
13484 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13487 (concat ":" (mapconcat 'identity data ":") ":"))
13491 (org-back-to-heading t)
13492 (when (looking-at org-complex-heading-regexp)
13495 (goto-char (match-beginning 5))
13497 (delete-region (point) (point-at-eol))
13498 (org-set-tags nil 'align))
13499 (goto-char (point-at-eol))
13501 (org-set-tags nil 'align)))
13502 (beginning-of-line 1)
13503 (if (looking-at ".*?\\([ \t]+\\)$")
13504 (delete-region (match-beginning 1) (match-end 1))))))
13506 (defun org-align-all-tags ()
13507 "Align the tags i all headings."
13510 (or (ignore-errors (org-back-to-heading t))
13511 (outline-next-heading))
13512 (if (org-at-heading-p)
13514 (message "No headings"))))
13516 (defvar org-indent-indentation-per-level)
13517 (defun org-set-tags (&optional arg just-align)
13518 "Set the tags for the current headline.
13519 With prefix ARG, realign all tags in headings in the current buffer."
13521 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13522 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13523 'region-start-level 'region))
13524 org-loop-over-headlines-in-active-region)
13526 ;; We don't use ARG and JUST-ALIGN here these args are not
13527 ;; useful when looping over headlines
13529 org-loop-over-headlines-in-active-region
13530 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13531 (let* ((re org-outline-regexp-bol)
13532 (current (unless arg (org-get-tags-string)))
13533 (col (current-column))
13534 (org-setting-tags t)
13535 table current-tags inherited-tags ; computed below when needed
13536 tags p0 c0 c1 rpl di tc level)
13539 (goto-char (point-min))
13540 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13541 (while (re-search-forward re nil t)
13542 (org-set-tags nil t)
13544 (message "All tags realigned to column %d" org-tags-column))
13546 (setq tags current)
13547 ;; Get a new set of tags from the user
13549 (setq table (append org-tag-persistent-alist
13550 (or org-tag-alist (org-get-buffer-tags))
13552 org-complete-tags-always-offer-all-agenda-tags
13553 (org-global-tags-completion-table
13554 (org-agenda-files))))
13555 org-last-tags-completion-table table
13556 current-tags (org-split-string current ":")
13557 inherited-tags (nreverse
13558 (nthcdr (length current-tags)
13559 (nreverse (org-get-tags-at))))
13561 (if (or (eq t org-use-fast-tag-selection)
13562 (and org-use-fast-tag-selection
13563 (delq nil (mapcar 'cdr table))))
13564 (org-fast-tag-selection
13565 current-tags inherited-tags table
13566 (if org-fast-tag-selection-include-todo
13567 org-todo-key-alist))
13568 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13570 (org-icompleting-read "Tags: "
13571 'org-tags-completion-function
13572 nil nil current 'org-tags-history))))))
13573 (while (string-match "[-+&]+" tags)
13574 ;; No boolean logic, just a list
13575 (setq tags (replace-match ":" t t tags))))
13577 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13579 (if org-tags-sort-function
13580 (setq tags (mapconcat 'identity
13581 (sort (org-split-string
13582 tags (org-re "[^[:alnum:]_@#%]+"))
13583 org-tags-sort-function) ":")))
13585 (if (string-match "\\`[\t ]*\\'" tags)
13587 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13588 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13590 ;; Insert new tags at the correct column
13591 (beginning-of-line 1)
13592 (setq level (or (and (looking-at org-outline-regexp)
13593 (- (match-end 0) (point) 1))
13596 ((and (equal current "") (equal tags "")))
13597 ((re-search-forward
13598 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13600 (if (equal tags "")
13602 (goto-char (match-beginning 0))
13603 (setq c0 (current-column)
13604 ;; compute offset for the case of org-indent-mode active
13605 di (if org-indent-mode
13606 (* (1- org-indent-indentation-per-level) (1- level))
13608 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13609 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13610 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13611 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13612 (replace-match rpl t t)
13613 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13615 (t (error "Tags alignment failed")))
13616 (org-move-to-column col)
13618 (run-hooks 'org-after-tags-change-hook))))))
13620 (defun org-change-tag-in-region (beg end tag off)
13621 "Add or remove TAG for each entry in the region.
13622 This works in the agenda, and also in an org-mode buffer."
13624 (list (region-beginning) (region-end)
13625 (let ((org-last-tags-completion-table
13626 (if (derived-mode-p 'org-mode)
13627 (org-get-buffer-tags)
13628 (org-global-tags-completion-table))))
13629 (org-icompleting-read
13630 "Tag: " 'org-tags-completion-function nil nil nil
13631 'org-tags-history))
13633 (message "[s]et or
[r]emove? ")
13634 (equal (read-char-exclusive) ?r))))
13635 (if (fboundp 'deactivate-mark) (deactivate-mark))
13636 (let ((agendap (equal major-mode 'org-agenda-mode))
13637 l1 l2 m buf pos newhead (cnt 0))
13639 (setq l2 (1- (org-current-line)))
13641 (setq l1 (org-current-line))
13642 (loop for l from l1 to l2 do
13644 (setq m (get-text-property (point) 'org-hd-marker))
13645 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
13647 (setq buf (if agendap (marker-buffer m) (current-buffer))
13648 pos (if agendap m (point)))
13649 (with-current-buffer buf
13653 (setq cnt (1+ cnt))
13654 (org-toggle-tag tag (if off 'off 'on))
13655 (setq newhead (org-get-heading)))))
13656 (and agendap (org-agenda-change-all-lines newhead m))))
13657 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13659 (defun org-tags-completion-function (string predicate &optional flag)
13660 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13661 (confirm (lambda (x) (stringp (car x)))))
13662 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13663 (setq s1 (match-string 1 string)
13664 s2 (match-string 2 string))
13665 (setq s1 "" s2 string))
13669 (setq rtn (try-completion s2 ctable confirm))
13672 (concat s1 s2 (substring rtn (length s2))
13673 (if (and org-add-colon-after-tag-completion
13674 (assoc rtn ctable))
13679 (all-completions s2 ctable confirm)
13683 (assoc s2 ctable)))
13686 (defun org-fast-tag-insert (kwd tags face &optional end)
13687 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13688 (insert (format "%-12s" (concat kwd ":"))
13689 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13692 (defun org-fast-tag-show-exit (flag)
13695 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13696 (replace-match ""))
13699 (org-move-to-column (- (window-width) 19) t)
13700 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13702 (defun org-set-current-tags-overlay (current prefix)
13703 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13704 (if (featurep 'xemacs)
13705 (org-overlay-display org-tags-overlay (concat prefix s)
13706 'secondary-selection)
13707 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13708 (org-overlay-display org-tags-overlay (concat prefix s)))))
13710 (defvar org-last-tag-selection-key nil)
13711 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13712 "Fast tag selection with single keys.
13713 CURRENT is the current list of tags in the headline, INHERITED is the
13714 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13715 possibly with grouping information. TODO-TABLE is a similar table with
13716 TODO keywords, should these have keys assigned to them.
13717 If the keys are nil, a-z are automatically assigned.
13718 Returns the new tags string, or nil to not change the current settings."
13719 (let* ((fulltable (append table todo-table))
13720 (maxlen (apply 'max (mapcar
13722 (if (stringp (car x)) (string-width (car x)) 0))
13724 (buf (current-buffer))
13725 (expert (eq org-fast-tag-selection-single-key 'expert))
13727 (fwidth (+ maxlen 3 1 3))
13728 (ncol (/ (- (window-width) 4) fwidth))
13731 tg cnt e c char c1 c2 ntable tbl rtn
13732 ov-start ov-end ov-prefix
13733 (exit-after-next org-fast-tag-selection-single-key)
13734 (done-keywords org-done-keywords)
13737 (beginning-of-line 1)
13739 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13740 (setq ov-start (match-beginning 1)
13741 ov-end (match-end 1)
13743 (setq ov-start (1- (point-at-eol))
13744 ov-end (1+ ov-start))
13745 (skip-chars-forward "^\n\r")
13748 (buffer-substring (1- (point)) (point))
13749 (if (> (current-column) org-tags-column)
13751 (make-string (- org-tags-column (current-column)) ?\ ))))))
13752 (move-overlay org-tags-overlay ov-start ov-end)
13753 (save-window-excursion
13755 (set-buffer (get-buffer-create " *Org tags*"))
13756 (delete-other-windows)
13757 (split-window-vertically)
13758 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13760 (org-set-local 'org-done-keywords done-keywords)
13761 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13762 (org-fast-tag-insert "Current" current c-face "\n\n")
13763 (org-fast-tag-show-exit exit-after-next)
13764 (org-set-current-tags-overlay current ov-prefix)
13765 (setq tbl fulltable char ?a cnt 0)
13766 (while (setq e (pop tbl))
13768 ((equal (car e) :startgroup)
13769 (push '() groups) (setq ingroup t)
13770 (when (not (= cnt 0))
13773 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13774 ((equal (car e) :endgroup)
13775 (setq ingroup nil cnt 0)
13776 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13777 ((equal e '(:newline))
13778 (when (not (= cnt 0))
13782 (while (equal (car tbl) '(:newline))
13784 (setq tbl (cdr tbl)))))
13786 (setq tg (copy-sequence (car e)) c2 nil)
13789 ;; automatically assign a character.
13790 (setq c1 (string-to-char
13791 (downcase (substring
13792 tg (if (= (string-to-char tg) ?@) 1 0)))))
13793 (if (or (rassoc c1 ntable) (rassoc c1 table))
13794 (while (or (rassoc char ntable) (rassoc char table))
13795 (setq char (1+ char)))
13797 (setq c (or c2 char)))
13798 (if ingroup (push tg (car groups)))
13799 (setq tg (org-add-props tg nil 'face
13801 ((not (assoc tg table))
13802 (org-get-todo-face tg))
13803 ((member tg current) c-face)
13804 ((member tg inherited) i-face)
13806 (if (and (= cnt 0) (not ingroup)) (insert " "))
13807 (insert "[" c "] " tg (make-string
13808 (- fwidth 4 (length tg)) ?\ ))
13809 (push (cons tg c) ntable)
13810 (when (= (setq cnt (1+ cnt)) ncol)
13812 (if ingroup (insert " "))
13814 (setq ntable (nreverse ntable))
13816 (goto-char (point-min))
13817 (if (not expert) (org-fit-window-to-buffer))
13821 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13822 (if (not groups) "no " "")
13823 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13824 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13825 (setq org-last-tag-selection-key c)
13827 ((= c ?\r) (throw 'exit t))
13829 (setq groups (not groups))
13830 (goto-char (point-min))
13831 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13834 (org-fast-tag-show-exit
13835 (setq exit-after-next (not exit-after-next)))
13837 (delete-other-windows)
13838 (set-window-buffer (split-window-vertically) " *Org tags*")
13839 (org-switch-to-buffer-other-window " *Org tags*")
13840 (org-fit-window-to-buffer)))
13842 (and (= c ?q) (not (rassoc c ntable))))
13843 (org-detach-overlay org-tags-overlay)
13844 (setq quit-flag t))
13847 (if exit-after-next (setq exit-after-next 'now)))
13849 (condition-case nil
13850 (setq tg (org-icompleting-read
13853 (with-current-buffer buf
13854 (org-get-buffer-tags)))))
13855 (quit (setq tg "")))
13856 (when (string-match "\\S-" tg)
13857 (add-to-list 'buffer-tags (list tg))
13858 (if (member tg current)
13859 (setq current (delete tg current))
13860 (push tg current)))
13861 (if exit-after-next (setq exit-after-next 'now)))
13862 ((setq e (rassoc c todo-table) tg (car e))
13863 (with-current-buffer buf
13864 (save-excursion (org-todo tg)))
13865 (if exit-after-next (setq exit-after-next 'now)))
13866 ((setq e (rassoc c ntable) tg (car e))
13867 (if (member tg current)
13868 (setq current (delete tg current))
13869 (loop for g in groups do
13872 (setq current (delete x current)))
13875 (if exit-after-next (setq exit-after-next 'now))))
13877 ;; Create a sorted list
13881 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13882 (if (eq exit-after-next 'now) (throw 'exit t))
13883 (goto-char (point-min))
13884 (beginning-of-line 2)
13885 (delete-region (point) (point-at-eol))
13886 (org-fast-tag-insert "Current" current c-face)
13887 (org-set-current-tags-overlay current ov-prefix)
13888 (while (re-search-forward
13889 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
13890 (setq tg (match-string 1))
13891 (add-text-properties
13892 (match-beginning 1) (match-end 1)
13895 ((member tg current) c-face)
13896 ((member tg inherited) i-face)
13897 (t (get-text-property (match-beginning 1) 'face))))))
13898 (goto-char (point-min)))))
13899 (org-detach-overlay org-tags-overlay)
13901 (mapconcat 'identity current ":")
13904 (defun org-get-tags-string ()
13905 "Get the TAGS string in the current headline."
13906 (unless (org-at-heading-p t)
13907 (error "Not on a heading"))
13909 (beginning-of-line 1)
13910 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13911 (org-match-string-no-properties 1)
13914 (defun org-get-tags ()
13915 "Get the list of tags specified in the current headline."
13916 (org-split-string (org-get-tags-string) ":"))
13918 (defun org-get-buffer-tags ()
13919 "Get a table of all tags used in the buffer, for completion."
13922 (goto-char (point-min))
13923 (while (re-search-forward
13924 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
13925 (when (equal (char-after (point-at-bol 0)) ?*)
13926 (mapc (lambda (x) (add-to-list 'tags x))
13927 (org-split-string (org-match-string-no-properties 1) ":")))))
13928 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
13929 (mapcar 'list tags)))
13931 ;;;; The mapping API
13934 (defun org-map-entries (func &optional match scope &rest skip)
13935 "Call FUNC at each headline selected by MATCH in SCOPE.
13937 FUNC is a function or a lisp form. The function will be called without
13938 arguments, with the cursor positioned at the beginning of the headline.
13939 The return values of all calls to the function will be collected and
13940 returned as a list.
13942 The call to FUNC will be wrapped into a save-excursion form, so FUNC
13943 does not need to preserve point. After evaluation, the cursor will be
13944 moved to the end of the line (presumably of the headline of the
13945 processed entry) and search continues from there. Under some
13946 circumstances, this may not produce the wanted results. For example,
13947 if you have removed (e.g. archived) the current (sub)tree it could
13948 mean that the next entry will be skipped entirely. In such cases, you
13949 can specify the position from where search should continue by making
13950 FUNC set the variable `org-map-continue-from' to the desired buffer
13953 MATCH is a tags/property/todo match as it is used in the agenda tags view.
13954 Only headlines that are matched by this query will be considered during
13955 the iteration. When MATCH is nil or t, all headlines will be
13956 visited by the iteration.
13958 SCOPE determines the scope of this command. It can be any of:
13960 nil The current buffer, respecting the restriction if any
13961 tree The subtree started with the entry at point
13962 region The entries within the active region, if any
13964 The entries within the active region, but only those at
13965 the same level than the first one.
13966 file The current buffer, without restriction
13968 The current buffer, and any archives associated with it
13969 agenda All agenda files
13970 agenda-with-archives
13971 All agenda files with any archive files associated with them
13973 If this is a list, all files in the list will be scanned
13975 The remaining args are treated as settings for the skipping facilities of
13976 the scanner. The following items can be given here:
13978 archive skip trees with the archive tag.
13979 comment skip trees with the COMMENT keyword
13980 function or Emacs Lisp form:
13981 will be used as value for `org-agenda-skip-function', so whenever
13982 the function returns t, FUNC will not be called for that
13983 entry and search will continue from the point where the
13984 function leaves it.
13986 If your function needs to retrieve the tags including inherited tags
13987 at the *current* entry, you can use the value of the variable
13988 `org-scanner-tags' which will be much faster than getting the value
13989 with `org-get-tags-at'. If your function gets properties with
13990 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
13991 to t around the call to `org-entry-properties' to get the same speedup.
13992 Note that if your function moves around to retrieve tags and properties at
13993 a *different* entry, you cannot use these techniques."
13994 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
13995 (not (org-region-active-p)))
13996 (let* ((org-agenda-archives-mode nil) ; just to make sure
13997 (org-agenda-skip-archived-trees (memq 'archive skip))
13998 (org-agenda-skip-comment-trees (memq 'comment skip))
13999 (org-agenda-skip-function
14000 (car (org-delete-all '(comment archive) skip)))
14001 (org-tags-match-list-sublevels t)
14002 (start-level (eq scope 'region-start-level))
14004 org-todo-keywords-for-agenda
14005 org-done-keywords-for-agenda
14006 org-todo-keyword-alist-for-agenda
14007 org-drawers-for-agenda
14008 org-tag-alist-for-agenda
14012 ((eq match t) (setq matcher t))
14013 ((eq match nil) (setq matcher t))
14014 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14018 (cond ((eq scope 'tree)
14019 (org-back-to-heading t)
14020 (org-narrow-to-subtree)
14022 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14023 (org-region-active-p))
14024 ;; If needed, set start-level to a string like "2"
14027 (goto-char (region-beginning))
14028 (unless (org-at-heading-p) (outline-next-heading))
14029 (setq start-level (org-current-level))))
14030 (narrow-to-region (region-beginning)
14032 (goto-char (region-end))
14033 (unless (and (bolp) (org-at-heading-p))
14034 (outline-next-heading))
14040 (org-prepare-agenda-buffers
14041 (list (buffer-file-name (current-buffer))))
14042 (setq res (org-scan-tags func matcher todo-only start-level)))
14043 ;; Get the right scope
14045 ((and scope (listp scope) (symbolp (car scope)))
14046 (setq scope (eval scope)))
14047 ((eq scope 'agenda)
14048 (setq scope (org-agenda-files t)))
14049 ((eq scope 'agenda-with-archives)
14050 (setq scope (org-agenda-files t))
14051 (setq scope (org-add-archive-files scope)))
14053 (setq scope (list (buffer-file-name))))
14054 ((eq scope 'file-with-archives)
14055 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14056 (org-prepare-agenda-buffers scope)
14057 (while (setq file (pop scope))
14058 (with-current-buffer (org-find-base-buffer-visiting file)
14062 (goto-char (point-min))
14063 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14068 ;;; Setting and retrieving properties
14070 (defconst org-special-properties
14071 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14072 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
14073 "The special properties valid in Org-mode.
14075 These are properties that are not defined in the property drawer,
14076 but in some other way.")
14078 (defconst org-default-properties
14079 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14080 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14081 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14082 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14083 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14084 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14085 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14086 "Some properties that are used by Org-mode for various purposes.
14087 Being in this list makes sure that they are offered for completion.")
14089 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14090 "Regular expression matching the first line of a property drawer.")
14092 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14093 "Regular expression matching the last line of a property drawer.")
14095 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14096 "Regular expression matching the first line of a property drawer.")
14098 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14099 "Regular expression matching the first line of a property drawer.")
14101 (defconst org-property-drawer-re
14102 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14103 org-property-end-re "\\)\n?")
14104 "Matches an entire property drawer.")
14106 (defconst org-clock-drawer-re
14107 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14108 org-property-end-re "\\)\n?")
14109 "Matches an entire clock drawer.")
14111 (defsubst org-re-property (property)
14112 "Return a regexp matching a PROPERTY line.
14113 Match group 1 will be set to the value."
14114 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14116 (defsubst org-re-property-keyword (property)
14117 "Return a regexp matching a PROPERTY line, possibly with no
14118 value for the property."
14119 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14121 (defun org-property-action ()
14122 "Do an action on properties."
14125 (org-at-property-p)
14126 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14127 (setq c (read-char-exclusive))
14130 (call-interactively 'org-set-property))
14132 (call-interactively 'org-delete-property))
14134 (call-interactively 'org-delete-property-globally))
14136 (call-interactively 'org-compute-property-at-point))
14137 (t (error "No such property action %c" c)))))
14139 (defun org-inc-effort ()
14140 "Increment the value of the effort property in the current entry."
14142 (org-set-effort nil t))
14144 (defun org-set-effort (&optional value increment)
14145 "Set the effort property of the current entry.
14146 With numerical prefix arg, use the nth allowed value, 0 stands for the
14147 10th allowed value.
14149 When INCREMENT is non-nil, set the property to the next allowed value."
14151 (if (equal value 0) (setq value 10))
14152 (let* ((completion-ignore-case t)
14153 (prop org-effort-property)
14154 (cur (org-entry-get nil prop))
14155 (allowed (org-property-get-allowed-values nil prop 'table))
14156 (existing (mapcar 'list (org-property-values prop)))
14159 ((stringp value) value)
14160 ((and allowed (integerp value))
14161 (or (car (nth (1- value) allowed))
14162 (car (org-last allowed))))
14163 ((and allowed increment)
14164 (or (caadr (member (list cur) allowed))
14165 (error "Allowed effort values are not set")))
14167 (message "Select 1-9,0, [RET%s]: %s"
14168 (if cur (concat "=" cur) "")
14169 (mapconcat 'car allowed " "))
14170 (setq rpl (read-char-exclusive))
14171 (if (equal rpl ?\r)
14173 (setq rpl (- rpl ?0))
14174 (if (equal rpl 0) (setq rpl 10))
14175 (if (and (> rpl 0) (<= rpl (length allowed)))
14176 (car (nth (1- rpl) allowed))
14177 (org-completing-read "Effort: " allowed nil))))
14179 (let (org-completion-use-ido org-completion-use-iswitchb)
14180 (org-completing-read
14181 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14182 (concat "[" cur "]") "")
14184 existing nil nil "" nil cur))))))
14185 (unless (equal (org-entry-get nil prop) val)
14186 (org-entry-put nil prop val))
14187 (message "%s is now %s" prop val)))
14189 (defun org-at-property-p ()
14190 "Is cursor inside a property drawer?"
14192 (beginning-of-line 1)
14193 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14194 (save-match-data ;; Used by calling procedures
14196 (range (unless (org-before-first-heading-p)
14197 (org-get-property-block))))
14198 (and range (<= (car range) p) (< p (cdr range))))))))
14200 (defun org-get-property-block (&optional beg end force)
14201 "Return the (beg . end) range of the body of the property drawer.
14202 BEG and END can be beginning and end of subtree, if not given
14203 they will be found.
14204 If the drawer does not exist and FORCE is non-nil, create the drawer."
14207 (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
14208 (end (or end (progn (outline-next-heading) (point)))))
14210 (if (re-search-forward org-property-start-re end t)
14211 (setq beg (1+ (match-end 0)))
14214 (org-insert-property-drawer)
14215 (setq end (progn (outline-next-heading) (point))))
14218 (if (re-search-forward org-property-start-re end t)
14219 (setq beg (1+ (match-end 0)))))
14220 (if (re-search-forward org-property-end-re end t)
14221 (setq end (match-beginning 0))
14222 (or force (throw 'exit nil))
14226 (insert ":END:\n"))
14229 (defun org-entry-properties (&optional pom which specific)
14230 "Get all properties of the entry at point-or-marker POM.
14231 This includes the TODO keyword, the tags, time strings for deadline,
14232 scheduled, and clocking, and any additional properties defined in the
14233 entry. The return value is an alist, keys may occur multiple times
14234 if the property key was used several times.
14235 POM may also be nil, in which case the current entry is used.
14236 If WHICH is nil or `all', get all properties. If WHICH is
14237 `special' or `standard', only get that subclass. If WHICH
14238 is a string only get exactly this property. SPECIFIC can be a string, the
14239 specific property we are interested in. Specifying it can speed
14240 things up because then unnecessary parsing is avoided."
14241 (setq which (or which 'all))
14242 (org-with-point-at pom
14243 (let ((clockstr (substring org-clock-string 0 -1))
14244 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14245 (case-fold-search nil)
14246 beg end range props sum-props key key1 value string clocksum)
14248 (when (condition-case nil
14249 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14252 (setq sum-props (get-text-property (point) 'org-summaries))
14253 (setq clocksum (get-text-property (point) :org-clock-minutes))
14254 (outline-next-heading)
14256 (when (memq which '(all special))
14257 ;; Get the special properties, like TODO and tags
14259 (when (and (or (not specific) (string= specific "TODO"))
14260 (looking-at org-todo-line-regexp) (match-end 2))
14261 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14262 (when (and (or (not specific) (string= specific "PRIORITY"))
14263 (looking-at org-priority-regexp))
14264 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14265 (when (or (not specific) (string= specific "FILE"))
14266 (push (cons "FILE" buffer-file-name) props))
14267 (when (and (or (not specific) (string= specific "TAGS"))
14268 (setq value (org-get-tags-string))
14269 (string-match "\\S-" value))
14270 (push (cons "TAGS" value) props))
14271 (when (and (or (not specific) (string= specific "ALLTAGS"))
14272 (setq value (org-get-tags-at)))
14273 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14276 (when (or (not specific) (string= specific "BLOCKED"))
14277 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14278 (when (or (not specific)
14280 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14281 "TIMESTAMP" "TIMESTAMP_IA")))
14283 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14284 (setq key (if (match-end 1)
14285 (substring (org-match-string-no-properties 1)
14287 string (if (equal key clockstr)
14291 (match-beginning 3) (goto-char
14293 (substring (org-match-string-no-properties 3)
14295 ;; Get the correct property name from the key. This is
14296 ;; necessary if the user has configured time keywords.
14297 (setq key1 (concat key ":"))
14301 (if (= (char-after (match-beginning 3)) ?\[)
14302 "TIMESTAMP_IA" "TIMESTAMP")))
14303 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14304 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14305 ((equal key1 org-closed-string) (setq key "CLOSED"))
14306 ((equal key1 org-clock-string) (setq key "CLOCK")))
14307 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14309 (push (cons key string) props)
14310 ;; no need to search further if match is found
14312 (when (or (equal key "CLOCK") (not (assoc key props)))
14313 (push (cons key string) props))))))
14316 (when (memq which '(all standard))
14317 ;; Get the standard properties, like :PROP: ...
14318 (setq range (org-get-property-block beg end))
14320 (goto-char (car range))
14321 (while (re-search-forward
14322 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14324 (setq key (org-match-string-no-properties 1)
14325 value (org-trim (or (org-match-string-no-properties 2) "")))
14326 (unless (member key excluded)
14327 (push (cons key (or value "")) props)))))
14329 (push (cons "CLOCKSUM"
14330 (org-columns-number-to-string (/ (float clocksum) 60.)
14333 (unless (assoc "CATEGORY" props)
14334 (push (cons "CATEGORY" (org-get-category)) props))
14335 (append sum-props (nreverse props)))))))
14337 (defun org-entry-get (pom property &optional inherit literal-nil)
14338 "Get value of PROPERTY for entry at point-or-marker POM.
14339 If INHERIT is non-nil and the entry does not have the property,
14340 then also check higher levels of the hierarchy.
14341 If INHERIT is the symbol `selective', use inheritance only if the setting
14342 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14343 If the property is present but empty, the return value is the empty string.
14344 If the property is not present at all, nil is returned.
14346 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14347 do not interpret it as the list atom nil. This is used for inheritance
14348 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14349 (org-with-point-at pom
14350 (if (and inherit (if (eq inherit 'selective)
14351 (org-property-inherit-p property)
14353 (org-entry-get-with-inheritance property literal-nil)
14354 (if (member property org-special-properties)
14355 ;; We need a special property. Use `org-entry-properties' to
14356 ;; retrieve it, but specify the wanted property
14357 (cdr (assoc property (org-entry-properties nil 'special property)))
14358 (let ((range (unless (org-before-first-heading-p)
14359 (org-get-property-block)))
14360 (props (list (or (assoc property org-file-properties)
14361 (assoc property org-global-properties)
14362 (assoc property org-global-properties-fixed))))
14364 (org-flet ((ap (key)
14365 (when (re-search-forward
14366 (org-re-property key) (cdr range) t)
14368 (org-update-property-plist
14371 (org-match-string-no-properties 1) "")
14373 (when (and range (goto-char (car range)))
14375 (goto-char (car range))
14376 (while (ap (concat property "+")))
14377 (setq val (cdr (assoc property props)))
14378 (when val (if literal-nil val (org-not-nil val))))))))))
14380 (defun org-property-or-variable-value (var &optional inherit)
14381 "Check if there is a property fixing the value of VAR.
14382 If yes, return this value. If not, return the current value of the variable."
14383 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14384 (if (and prop (stringp prop) (string-match "\\S-" prop))
14386 (symbol-value var))))
14388 (defun org-entry-delete (pom property)
14389 "Delete the property PROPERTY from entry at point-or-marker POM."
14390 (org-with-point-at pom
14391 (if (member property org-special-properties)
14392 nil ; cannot delete these properties.
14393 (let ((range (org-get-property-block)))
14395 (goto-char (car range))
14397 (org-re-property property)
14400 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14404 ;; Multi-values properties are properties that contain multiple values
14405 ;; These values are assumed to be single words, separated by whitespace.
14406 (defun org-entry-add-to-multivalued-property (pom property value)
14407 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14408 (let* ((old (org-entry-get pom property))
14409 (values (and old (org-split-string old "[ \t]"))))
14410 (setq value (org-entry-protect-space value))
14411 (unless (member value values)
14412 (setq values (cons value values))
14413 (org-entry-put pom property
14414 (mapconcat 'identity values " ")))))
14416 (defun org-entry-remove-from-multivalued-property (pom property value)
14417 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14418 (let* ((old (org-entry-get pom property))
14419 (values (and old (org-split-string old "[ \t]"))))
14420 (setq value (org-entry-protect-space value))
14421 (when (member value values)
14422 (setq values (delete value values))
14423 (org-entry-put pom property
14424 (mapconcat 'identity values " ")))))
14426 (defun org-entry-member-in-multivalued-property (pom property value)
14427 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14428 (let* ((old (org-entry-get pom property))
14429 (values (and old (org-split-string old "[ \t]"))))
14430 (setq value (org-entry-protect-space value))
14431 (member value values)))
14433 (defun org-entry-get-multivalued-property (pom property)
14434 "Return a list of values in a multivalued property."
14435 (let* ((value (org-entry-get pom property))
14436 (values (and value (org-split-string value "[ \t]"))))
14437 (mapcar 'org-entry-restore-space values)))
14439 (defun org-entry-put-multivalued-property (pom property &rest values)
14440 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14441 VALUES should be a list of strings. Spaces will be protected."
14442 (org-entry-put pom property
14443 (mapconcat 'org-entry-protect-space values " "))
14444 (let* ((value (org-entry-get pom property))
14445 (values (and value (org-split-string value "[ \t]"))))
14446 (mapcar 'org-entry-restore-space values)))
14448 (defun org-entry-protect-space (s)
14449 "Protect spaces and newline in string S."
14450 (while (string-match " " s)
14451 (setq s (replace-match "%20" t t s)))
14452 (while (string-match "\n" s)
14453 (setq s (replace-match "%0A" t t s)))
14456 (defun org-entry-restore-space (s)
14457 "Restore spaces and newline in string S."
14458 (while (string-match "%20" s)
14459 (setq s (replace-match " " t t s)))
14460 (while (string-match "%0A" s)
14461 (setq s (replace-match "\n" t t s)))
14464 (defvar org-entry-property-inherited-from (make-marker)
14465 "Marker pointing to the entry from where a property was inherited.
14466 Each call to `org-entry-get-with-inheritance' will set this marker to the
14467 location of the entry where the inheritance search matched. If there was
14468 no match, the marker will point nowhere.
14469 Note that also `org-entry-get' calls this function, if the INHERIT flag
14472 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14473 "Get entry property, and search higher levels if not present.
14474 The search will stop at the first ancestor which has the property defined.
14475 If the value found is \"nil\", return nil to show that the property
14476 should be considered as undefined (this is the meaning of nil here).
14477 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14478 (move-marker org-entry-property-inherited-from nil)
14480 (unless (org-before-first-heading-p)
14486 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14487 (org-back-to-heading t)
14488 (move-marker org-entry-property-inherited-from (point))
14490 (or (org-up-heading-safe) (throw 'ex nil)))))))
14492 (cdr (assoc property org-file-properties))
14493 (cdr (assoc property org-global-properties))
14494 (cdr (assoc property org-global-properties-fixed))))
14495 (if literal-nil tmp (org-not-nil tmp))))
14497 (defvar org-property-changed-functions nil
14498 "Hook called when the value of a property has changed.
14499 Each hook function should accept two arguments, the name of the property
14500 and the new value.")
14502 (defun org-entry-put (pom property value)
14503 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14504 (org-with-point-at pom
14505 (org-back-to-heading t)
14506 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14509 ((equal property "TODO")
14510 (when (and (stringp value) (string-match "\\S-" value)
14511 (not (member value org-todo-keywords-1)))
14512 (error "\"%s\" is not a valid TODO state" value))
14513 (if (or (not value)
14514 (not (string-match "\\S-" value)))
14515 (setq value 'none))
14517 (org-set-tags nil 'align))
14518 ((equal property "PRIORITY")
14519 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14520 (string-to-char value) ?\ ))
14521 (org-set-tags nil 'align))
14522 ((equal property "SCHEDULED")
14523 (if (re-search-forward org-scheduled-time-regexp end t)
14525 ((eq value 'earlier) (org-timestamp-change -1 'day))
14526 ((eq value 'later) (org-timestamp-change 1 'day))
14527 (t (call-interactively 'org-schedule)))
14528 (call-interactively 'org-schedule)))
14529 ((equal property "DEADLINE")
14530 (if (re-search-forward org-deadline-time-regexp end t)
14532 ((eq value 'earlier) (org-timestamp-change -1 'day))
14533 ((eq value 'later) (org-timestamp-change 1 'day))
14534 (t (call-interactively 'org-deadline)))
14535 (call-interactively 'org-deadline)))
14536 ((member property org-special-properties)
14537 (error "The %s property can not yet be set with `org-entry-put'"
14539 (t ; a non-special property
14540 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14541 (setq range (org-get-property-block beg end 'force))
14542 (goto-char (car range))
14543 (if (re-search-forward
14544 (org-re-property-keyword property) (cdr range) t)
14546 (delete-region (match-beginning 0) (match-end 0))
14547 (goto-char (match-beginning 0)))
14548 (goto-char (cdr range))
14552 (insert ":" property ":")
14553 (and value (insert " " value))
14554 (org-indent-line)))))
14555 (run-hook-with-args 'org-property-changed-functions property value)))
14557 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14558 "Get all property keys in the current buffer.
14559 With INCLUDE-SPECIALS, also list the special properties that reflect things
14560 like tags and TODO state.
14561 With INCLUDE-DEFAULTS, also include properties that has special meaning
14562 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14564 With INCLUDE-COLUMNS, also include property names given in COLUMN
14565 formats in the current buffer."
14566 (let (rtn range cfmt s p)
14570 (goto-char (point-min))
14571 (while (re-search-forward org-property-start-re nil t)
14572 (setq range (org-get-property-block))
14573 (goto-char (car range))
14574 (while (re-search-forward
14575 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14577 (add-to-list 'rtn (org-match-string-no-properties 1)))
14578 (outline-next-heading))))
14580 (when include-specials
14581 (setq rtn (append org-special-properties rtn)))
14583 (when include-defaults
14584 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14585 (add-to-list 'rtn org-effort-property))
14587 (when include-columns
14591 (goto-char (point-min))
14592 (while (re-search-forward
14593 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14595 (setq cfmt (match-string 2) s 0)
14596 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14598 (setq s (match-end 0)
14599 p (match-string 1 cfmt))
14600 (unless (or (equal p "ITEM")
14601 (member p org-special-properties))
14602 (add-to-list 'rtn (match-string 1 cfmt))))))))
14604 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14606 (defun org-property-values (key)
14607 "Return a list of all values of property KEY in the current buffer."
14611 (goto-char (point-min))
14612 (let ((re (org-re-property key))
14614 (while (re-search-forward re nil t)
14615 (add-to-list 'values (org-trim (match-string 1))))
14616 (delete "" values)))))
14618 (defun org-insert-property-drawer ()
14619 "Insert a property drawer into the current entry."
14620 (org-back-to-heading t)
14621 (looking-at org-outline-regexp)
14622 (let ((indent (if org-adapt-indentation
14623 (- (match-end 0) (match-beginning 0))
14626 (re (concat "^[ \t]*" org-keyword-time-regexp))
14628 (outline-next-heading)
14631 (while (re-search-forward re end t))
14632 (setq hiddenp (outline-invisible-p))
14634 (and (equal (char-after) ?\n) (forward-char 1))
14635 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14636 (if (member (match-string 1) '("CLOCK:" ":END:"))
14637 ;; just skip this line
14638 (beginning-of-line 2)
14639 ;; Drawer start, find the end
14640 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14641 (beginning-of-line 1)))
14642 (org-skip-over-state-notes)
14643 (skip-chars-backward " \t\n\r")
14644 (if (eq (char-before) ?*) (forward-char 1))
14645 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14646 (beginning-of-line 0)
14647 (org-indent-to-column indent)
14648 (beginning-of-line 2)
14649 (org-indent-to-column indent)
14650 (beginning-of-line 0)
14653 (org-back-to-heading t)
14655 (org-flag-drawer t))))
14657 (defun org-insert-drawer (&optional arg drawer)
14658 "Insert a drawer at point.
14660 Optional argument DRAWER, when non-nil, is a string representing
14661 drawer's name. Otherwise, the user is prompted for a name.
14663 If a region is active, insert the drawer around that region
14666 Point is left between drawer's boundaries."
14668 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14670 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14671 ;; internally by Org. They are meant to be inserted
14673 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14674 ;; Remove system drawers from list. Note: For some reason,
14675 ;; `org-completing-read' ignores the predicate while
14676 ;; `completing-read' handles it fine.
14677 (drawer (if arg "PROPERTIES"
14680 "Drawer: " org-drawers
14681 (lambda (d) (not (member d system-drawers))))))))
14683 ;; With C-u, fall back on `org-insert-property-drawer'
14684 (arg (org-insert-property-drawer))
14685 ;; With an active region, insert a drawer at point.
14686 ((not (org-region-active-p))
14688 (unless (bolp) (insert "\n"))
14689 (insert (format ":%s:\n\n:END:\n" drawer))
14690 (forward-line -2)))
14691 ;; Otherwise, insert the drawer at point
14693 (let ((rbeg (region-beginning))
14694 (rend (copy-marker (region-end))))
14698 (beginning-of-line)
14699 (when (save-excursion
14700 (re-search-forward org-outline-regexp-bol rend t))
14701 (error "Drawers cannot contain headlines"))
14702 ;; Position point at the beginning of the first
14703 ;; non-blank line in region. Insert drawer's opening
14704 ;; there, then indent it.
14705 (org-skip-whitespace)
14706 (beginning-of-line)
14707 (insert ":" drawer ":\n")
14709 (indent-for-tab-command)
14710 ;; Move point to the beginning of the first blank line
14711 ;; after the last non-blank line in region. Insert
14712 ;; drawer's closing, then indent it.
14714 (skip-chars-backward " \r\t\n")
14716 (indent-for-tab-command)
14717 (unless (eolp) (insert "\n")))
14718 ;; Clear marker, whatever the outcome of insertion is.
14719 (set-marker rend nil)))))))
14721 (defvar org-property-set-functions-alist nil
14722 "Property set function alist.
14723 Each entry should have the following format:
14725 (PROPERTY . READ-FUNCTION)
14727 The read function will be called with the same argument as
14728 `org-completing-read'.")
14730 (defun org-set-property-function (property)
14731 "Get the function that should be used to set PROPERTY.
14732 This is computed according to `org-property-set-functions-alist'."
14733 (or (cdr (assoc property org-property-set-functions-alist))
14734 'org-completing-read))
14736 (defun org-read-property-value (property)
14737 "Read PROPERTY value from user."
14738 (let* ((completion-ignore-case t)
14739 (allowed (org-property-get-allowed-values nil property 'table))
14740 (cur (org-entry-get nil property))
14741 (prompt (concat property " value"
14742 (if (and cur (string-match "\\S-" cur))
14743 (concat " [" cur "]") "") ": "))
14744 (set-function (org-set-property-function property))
14746 (funcall set-function prompt allowed nil
14747 (not (get-text-property 0 'org-unrestricted
14749 (let (org-completion-use-ido org-completion-use-iswitchb)
14750 (funcall set-function prompt
14751 (mapcar 'list (org-property-values property))
14752 nil nil "" nil cur)))))
14757 (defvar org-last-set-property nil)
14758 (defun org-read-property-name ()
14759 "Read a property name."
14760 (let* ((completion-ignore-case t)
14761 (keys (org-buffer-property-keys nil t t))
14762 (default-prop (or (save-excursion
14764 (beginning-of-line)
14765 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14766 (null (string= (match-string 1) "END"))
14767 (match-string 1))))
14768 org-last-set-property))
14769 (property (org-icompleting-read
14771 (if default-prop (concat " [" default-prop "]") "")
14773 (mapcar 'list keys)
14777 (if (member property keys)
14779 (or (cdr (assoc (downcase property)
14780 (mapcar (lambda (x) (cons (downcase x) x))
14784 (defun org-set-property (property value)
14785 "In the current entry, set PROPERTY to VALUE.
14786 When called interactively, this will prompt for a property name, offering
14787 completion on existing and default properties. And then it will prompt
14788 for a value, offering completion either on allowed values (via an inherited
14789 xxx_ALL property) or on existing values in other instances of this property
14790 in the current file."
14791 (interactive (list nil nil))
14792 (let* ((property (or property (org-read-property-name)))
14793 (value (or value (org-read-property-value property)))
14794 (fn (cdr (assoc property org-properties-postprocess-alist))))
14795 (setq org-last-set-property property)
14796 ;; Possibly postprocess the inserted value:
14797 (when fn (setq value (funcall fn value)))
14798 (unless (equal (org-entry-get nil property) value)
14799 (org-entry-put nil property value))))
14801 (defun org-delete-property (property)
14802 "In the current entry, delete PROPERTY."
14804 (let* ((completion-ignore-case t)
14805 (prop (org-icompleting-read "Property: "
14806 (org-entry-properties nil 'standard))))
14808 (message "Property %s %s" property
14809 (if (org-entry-delete nil property)
14811 "was not present in the entry")))
14813 (defun org-delete-property-globally (property)
14814 "Remove PROPERTY globally, from all entries."
14816 (let* ((completion-ignore-case t)
14817 (prop (org-icompleting-read
14818 "Globally remove property: "
14819 (mapcar 'list (org-buffer-property-keys)))))
14824 (goto-char (point-min))
14826 (while (re-search-forward
14827 (org-re-property property)
14829 (setq cnt (1+ cnt))
14830 (delete-region (match-beginning 0) (1+ (point-at-eol))))
14831 (message "Property \"%s\" removed from %d entries" property cnt)))))
14833 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14835 (defun org-compute-property-at-point ()
14836 "Compute the property at point.
14837 This looks for an enclosing column format, extracts the operator and
14838 then applies it to the property in the column format's scope."
14840 (unless (org-at-property-p)
14841 (error "Not at a property"))
14842 (let ((prop (org-match-string-no-properties 2)))
14843 (org-columns-get-format-and-top-level)
14844 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14845 (error "No operator defined for property %s" prop))
14846 (org-columns-compute prop)))
14848 (defvar org-property-allowed-value-functions nil
14849 "Hook for functions supplying allowed values for a specific property.
14850 The functions must take a single argument, the name of the property, and
14851 return a flat list of allowed values. If \":ETC\" is one of
14852 the values, this means that these values are intended as defaults for
14853 completion, but that other values should be allowed too.
14854 The functions must return nil if they are not responsible for this
14857 (defun org-property-get-allowed-values (pom property &optional table)
14858 "Get allowed values for the property PROPERTY.
14859 When TABLE is non-nil, return an alist that can directly be used for
14863 ((equal property "TODO")
14864 (setq vals (org-with-point-at pom
14865 (append org-todo-keywords-1 '("")))))
14866 ((equal property "PRIORITY")
14867 (let ((n org-lowest-priority))
14868 (while (>= n org-highest-priority)
14869 (push (char-to-string n) vals)
14871 ((member property org-special-properties))
14872 ((setq vals (run-hook-with-args-until-success
14873 'org-property-allowed-value-functions property)))
14875 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
14876 (when (and vals (string-match "\\S-" vals))
14877 (setq vals (car (read-from-string (concat "(" vals ")"))))
14878 (setq vals (mapcar (lambda (x)
14879 (cond ((stringp x) x)
14880 ((numberp x) (number-to-string x))
14881 ((symbolp x) (symbol-name x))
14884 (when (member ":ETC" vals)
14885 (setq vals (remove ":ETC" vals))
14886 (org-add-props (car vals) '(org-unrestricted t)))
14887 (if table (mapcar 'list vals) vals)))
14889 (defun org-property-previous-allowed-value (&optional previous)
14890 "Switch to the next allowed value for this property."
14892 (org-property-next-allowed-value t))
14894 (defun org-property-next-allowed-value (&optional previous)
14895 "Switch to the next allowed value for this property."
14897 (unless (org-at-property-p)
14898 (error "Not at a property"))
14899 (let* ((key (match-string 2))
14900 (value (match-string 3))
14901 (allowed (or (org-property-get-allowed-values (point) key)
14902 (and (member value '("[ ]" "[-]" "[X]"))
14906 (error "Allowed values for this property have not been defined"))
14907 (if previous (setq allowed (reverse allowed)))
14908 (if (member value allowed)
14909 (setq nval (car (cdr (member value allowed)))))
14910 (setq nval (or nval (car allowed)))
14911 (if (equal nval value)
14912 (error "Only one allowed value for this property"))
14913 (org-at-property-p)
14914 (replace-match (concat " :" key ": " nval) t t)
14916 (beginning-of-line 1)
14917 (skip-chars-forward " \t")
14918 (run-hook-with-args 'org-property-changed-functions key nval)))
14920 (defun org-find-olp (path &optional this-buffer)
14921 "Return a marker pointing to the entry at outline path OLP.
14922 If anything goes wrong, throw an error.
14923 You can wrap this call to catch the error like this:
14925 (condition-case msg
14926 (org-mobile-locate-entry (match-string 4))
14927 (error (nth 1 msg)))
14929 The return value will then be either a string with the error message,
14930 or a marker if everything is OK.
14932 If THIS-BUFFER is set, the outline path does not contain a file,
14934 (let* ((file (if this-buffer buffer-file-name (pop path)))
14935 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
14939 limit re end found pos heading cnt flevel)
14940 (unless buffer (error "File not found :%s" file))
14941 (with-current-buffer buffer
14945 (setq limit (point-max))
14946 (goto-char (point-min))
14947 (while (setq heading (pop path))
14948 (setq re (format org-complex-heading-regexp-format
14949 (regexp-quote heading)))
14950 (setq cnt 0 pos (point))
14951 (while (re-search-forward re end t)
14952 (setq level (- (match-end 1) (match-beginning 1)))
14953 (if (and (>= level lmin) (<= level lmax))
14954 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
14955 (when (= cnt 0) (error "Heading not found on level %d: %s"
14957 (when (> cnt 1) (error "Heading not unique on level %d: %s"
14960 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
14961 (setq end (save-excursion (org-end-of-subtree t t))))
14962 (when (org-at-heading-p)
14963 (move-marker (make-marker) (point))))))))
14965 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
14966 "Find node HEADING in BUFFER.
14967 Return a marker to the heading if it was found, or nil if not.
14968 If POS-ONLY is set, return just the position instead of a marker.
14970 The heading text must match exact, but it may have a TODO keyword,
14971 a priority cookie and tags in the standard locations."
14972 (with-current-buffer (or buffer (current-buffer))
14976 (goto-char (point-min))
14977 (let (case-fold-search)
14978 (if (re-search-forward
14979 (format org-complex-heading-regexp-format
14980 (regexp-quote heading)) nil t)
14982 (match-beginning 0)
14983 (move-marker (make-marker) (match-beginning 0)))))))))
14985 (defun org-find-exact-heading-in-directory (heading &optional dir)
14986 "Find Org node headline HEADING in all .org files in directory DIR.
14987 When the target headline is found, return a marker to this location."
14988 (let ((files (directory-files (or dir default-directory)
14989 nil "\\`[^.#].*\\.org\\'"))
14990 file visiting m buffer)
14992 (while (setq file (pop files))
14993 (message "trying %s" file)
14994 (setq visiting (org-find-base-buffer-visiting file))
14995 (setq buffer (or visiting (find-file-noselect file)))
14996 (setq m (org-find-exact-headline-in-buffer
14998 (when (and (not m) (not visiting)) (kill-buffer buffer))
14999 (and m (throw 'found m))))))
15001 (defun org-find-entry-with-id (ident)
15002 "Locate the entry that contains the ID property with exact value IDENT.
15003 IDENT can be a string, a symbol or a number, this function will search for
15004 the string representation of it.
15005 Return the position where this entry starts, or nil if there is no such entry."
15006 (interactive "sID: ")
15008 ((stringp ident) ident)
15009 ((symbol-name ident) (symbol-name ident))
15010 ((numberp ident) (number-to-string ident))
15011 (t (error "IDENT %s must be a string, symbol or number" ident))))
15012 (case-fold-search nil))
15016 (goto-char (point-min))
15017 (when (re-search-forward
15018 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15020 (org-back-to-heading t)
15025 (defvar org-last-changed-timestamp nil)
15026 (defvar org-last-inserted-timestamp nil
15027 "The last time stamp inserted with `org-insert-time-stamp'.")
15028 (defvar org-time-was-given) ; dynamically scoped parameter
15029 (defvar org-end-time-was-given) ; dynamically scoped parameter
15030 (defvar org-ts-what) ; dynamically scoped parameter
15032 (defun org-time-stamp (arg &optional inactive)
15033 "Prompt for a date/time and insert a time stamp.
15034 If the user specifies a time like HH:MM or if this command is
15035 called with at least one prefix argument, the time stamp contains
15036 the date and the time. Otherwise, only the date is be included.
15038 All parts of a date not specified by the user is filled in from
15039 the current date/time. So if you just press return without
15040 typing anything, the time stamp will represent the current
15043 If there is already a timestamp at the cursor, it will be
15046 With two universal prefix arguments, insert an active timestamp
15047 with the current time without prompting the user."
15051 ;; Default time is either today, or, when entering a range,
15052 ;; the range start.
15053 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15055 (re-search-backward
15056 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15057 (- (point) 20) t)))
15058 (apply 'encode-time (org-parse-time-string (match-string 1)))
15060 (default-input (and ts (org-get-compact-tod ts)))
15061 (repeater (save-excursion
15063 (beginning-of-line)
15064 (when (re-search-forward
15065 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15066 (save-excursion (progn (end-of-line) (point))) t)
15067 (match-string 0)))))
15068 org-time-was-given org-end-time-was-given time)
15070 ((and (org-at-timestamp-p t)
15071 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15072 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15074 (setq time (let ((this-command this-command))
15075 (org-read-date arg 'totime nil nil
15076 default-time default-input inactive)))
15077 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15078 ((org-at-timestamp-p t)
15079 (setq time (let ((this-command this-command))
15080 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15081 (when (org-at-timestamp-p t) ; just to get the match data
15082 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15084 (setq org-last-changed-timestamp
15085 (org-insert-time-stamp
15086 time (or org-time-was-given arg)
15087 inactive nil nil (list org-end-time-was-given)))
15088 (when repeater (goto-char (1- (point))) (insert " " repeater)
15089 (setq org-last-changed-timestamp
15090 (concat (substring org-last-inserted-timestamp 0 -1)
15091 " " repeater ">"))))
15092 (message "Timestamp updated"))
15094 (org-insert-time-stamp (current-time) t))
15096 (setq time (let ((this-command this-command))
15097 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15098 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15099 nil nil (list org-end-time-was-given))))))
15101 ;; FIXME: can we use this for something else, like computing time differences?
15102 (defun org-get-compact-tod (s)
15103 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15104 (let* ((t1 (match-string 1 s))
15105 (h1 (string-to-number (match-string 2 s)))
15106 (m1 (string-to-number (match-string 3 s)))
15107 (t2 (and (match-end 4) (match-string 5 s)))
15108 (h2 (and t2 (string-to-number (match-string 6 s))))
15109 (m2 (and t2 (string-to-number (match-string 7 s))))
15113 (setq dh (- h2 h1) dm (- m2 m1))
15114 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15115 (concat t1 "+" (number-to-string dh)
15116 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15118 (defun org-time-stamp-inactive (&optional arg)
15119 "Insert an inactive time stamp.
15120 An inactive time stamp is enclosed in square brackets instead of angle
15121 brackets. It is inactive in the sense that it does not trigger agenda entries,
15122 does not link to the calendar and cannot be changed with the S-cursor keys.
15123 So these are more for recording a certain time/date."
15125 (org-time-stamp arg 'inactive))
15127 (defvar org-date-ovl (make-overlay 1 1))
15128 (overlay-put org-date-ovl 'face 'org-date-selected)
15129 (org-detach-overlay org-date-ovl)
15131 (defvar org-ans1) ; dynamically scoped parameter
15132 (defvar org-ans2) ; dynamically scoped parameter
15134 (defvar org-plain-time-of-day-regexp) ; defined below
15136 (defvar org-overriding-default-time nil) ; dynamically scoped
15137 (defvar org-read-date-overlay nil)
15138 (defvar org-dcst nil) ; dynamically scoped
15139 (defvar org-read-date-history nil)
15140 (defvar org-read-date-final-answer nil)
15141 (defvar org-read-date-analyze-futurep nil)
15142 (defvar org-read-date-analyze-forced-year nil)
15143 (defvar org-read-date-inactive)
15145 (defun org-read-date (&optional org-with-time to-time from-string prompt
15146 default-time default-input inactive)
15147 "Read a date, possibly a time, and make things smooth for the user.
15148 The prompt will suggest to enter an ISO date, but you can also enter anything
15149 which will at least partially be understood by `parse-time-string'.
15150 Unrecognized parts of the date will default to the current day, month, year,
15151 hour and minute. If this command is called to replace a timestamp at point,
15152 or to enter the second timestamp of a range, the default time is taken
15153 from the existing stamp. Furthermore, the command prefers the future,
15154 so if you are giving a date where the year is not given, and the day-month
15155 combination is already past in the current year, it will assume you
15156 mean next year. For details, see the manual. A few examples:
15158 3-2-5 --> 2003-02-05
15159 feb 15 --> currentyear-02-15
15160 2/15 --> currentyear-02-15
15161 sep 12 9 --> 2009-09-12
15162 12:45 --> today 12:45
15163 22 sept 0:34 --> currentyear-09-22 0:34
15164 12 --> currentyear-currentmonth-12
15165 Fri --> nearest Friday (today or later)
15168 Furthermore you can specify a relative date by giving, as the *first* thing
15169 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15170 change in days weeks, months, years.
15171 With a single plus or minus, the date is relative to today. With a double
15172 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15173 +4d --> four days from today
15174 +4 --> same as above
15175 +2w --> two weeks from today
15176 ++5 --> five days from default date
15178 The function understands only English month and weekday abbreviations.
15180 While prompting, a calendar is popped up - you can also select the
15181 date with the mouse (button 1). The calendar shows a period of three
15182 months. To scroll it to other months, use the keys `>' and `<'.
15183 If you don't like the calendar, turn it off with
15184 \(setq org-read-date-popup-calendar nil)
15186 With optional argument TO-TIME, the date will immediately be converted
15187 to an internal time.
15188 With an optional argument WITH-TIME, the prompt will suggest to also
15189 insert a time. Note that when WITH-TIME is not set, you can still
15190 enter a time, and this function will inform the calling routine about
15191 this change. The calling routine may then choose to change the format
15192 used to insert the time stamp into the buffer to include the time.
15193 With optional argument FROM-STRING, read from this string instead from
15194 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15195 the time/date that is used for everything that is not specified by the
15197 (require 'parse-time)
15198 (let* ((org-time-stamp-rounding-minutes
15199 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15200 (org-dcst org-display-custom-times)
15201 (ct (org-current-time))
15202 (org-def (or org-overriding-default-time default-time ct))
15203 (org-defdecode (decode-time org-def))
15205 (when (< (nth 2 org-defdecode) org-extend-today-until)
15206 (setcar (nthcdr 2 org-defdecode) -1)
15207 (setcar (nthcdr 1 org-defdecode) 59)
15208 (setq org-def (apply 'encode-time org-defdecode)
15209 org-defdecode (decode-time org-def)))))
15210 (calendar-frame-setup nil)
15211 (calendar-setup nil)
15212 (calendar-move-hook nil)
15213 (calendar-view-diary-initially-flag nil)
15214 (calendar-view-holidays-initially-flag nil)
15215 (timestr (format-time-string
15216 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15217 (prompt (concat (if prompt (concat prompt " ") "")
15218 (format "Date+time [%s]: " timestr)))
15219 ans (org-ans0 "") org-ans1 org-ans2 final)
15222 (from-string (setq ans from-string))
15223 (org-read-date-popup-calendar
15225 (save-window-excursion
15227 (org-eval-in-calendar '(setq cursor-type nil) t)
15230 (calendar-forward-day (- (time-to-days org-def)
15231 (calendar-absolute-from-gregorian
15232 (calendar-current-date))))
15233 (org-eval-in-calendar nil t)
15234 (let* ((old-map (current-local-map))
15235 (map (copy-keymap calendar-mode-map))
15236 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15237 (org-defkey map (kbd "RET") 'org-calendar-select)
15238 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15239 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15240 (org-defkey minibuffer-local-map [(meta shift left)]
15241 (lambda () (interactive)
15242 (org-eval-in-calendar '(calendar-backward-month 1))))
15243 (org-defkey minibuffer-local-map [(meta shift right)]
15244 (lambda () (interactive)
15245 (org-eval-in-calendar '(calendar-forward-month 1))))
15246 (org-defkey minibuffer-local-map [(meta shift up)]
15247 (lambda () (interactive)
15248 (org-eval-in-calendar '(calendar-backward-year 1))))
15249 (org-defkey minibuffer-local-map [(meta shift down)]
15250 (lambda () (interactive)
15251 (org-eval-in-calendar '(calendar-forward-year 1))))
15252 (org-defkey minibuffer-local-map [?\e (shift left)]
15253 (lambda () (interactive)
15254 (org-eval-in-calendar '(calendar-backward-month 1))))
15255 (org-defkey minibuffer-local-map [?\e (shift right)]
15256 (lambda () (interactive)
15257 (org-eval-in-calendar '(calendar-forward-month 1))))
15258 (org-defkey minibuffer-local-map [?\e (shift up)]
15259 (lambda () (interactive)
15260 (org-eval-in-calendar '(calendar-backward-year 1))))
15261 (org-defkey minibuffer-local-map [?\e (shift down)]
15262 (lambda () (interactive)
15263 (org-eval-in-calendar '(calendar-forward-year 1))))
15264 (org-defkey minibuffer-local-map [(shift up)]
15265 (lambda () (interactive)
15266 (org-eval-in-calendar '(calendar-backward-week 1))))
15267 (org-defkey minibuffer-local-map [(shift down)]
15268 (lambda () (interactive)
15269 (org-eval-in-calendar '(calendar-forward-week 1))))
15270 (org-defkey minibuffer-local-map [(shift left)]
15271 (lambda () (interactive)
15272 (org-eval-in-calendar '(calendar-backward-day 1))))
15273 (org-defkey minibuffer-local-map [(shift right)]
15274 (lambda () (interactive)
15275 (org-eval-in-calendar '(calendar-forward-day 1))))
15276 (org-defkey minibuffer-local-map ">"
15277 (lambda () (interactive)
15278 (org-eval-in-calendar '(scroll-calendar-left 1))))
15279 (org-defkey minibuffer-local-map "<"
15280 (lambda () (interactive)
15281 (org-eval-in-calendar '(scroll-calendar-right 1))))
15282 (org-defkey minibuffer-local-map "\C-v"
15283 (lambda () (interactive)
15284 (org-eval-in-calendar
15285 '(calendar-scroll-left-three-months 1))))
15286 (org-defkey minibuffer-local-map "\M-v"
15287 (lambda () (interactive)
15288 (org-eval-in-calendar
15289 '(calendar-scroll-right-three-months 1))))
15290 (run-hooks 'org-read-date-minibuffer-setup-hook)
15293 (use-local-map map)
15294 (setq org-read-date-inactive inactive)
15295 (add-hook 'post-command-hook 'org-read-date-display)
15296 (setq org-ans0 (read-string prompt default-input
15297 'org-read-date-history nil))
15298 ;; org-ans0: from prompt
15299 ;; org-ans1: from mouse click
15300 ;; org-ans2: from calendar motion
15301 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15302 (remove-hook 'post-command-hook 'org-read-date-display)
15303 (use-local-map old-map)
15304 (when org-read-date-overlay
15305 (delete-overlay org-read-date-overlay)
15306 (setq org-read-date-overlay nil)))))
15307 (bury-buffer "*Calendar*")))))
15309 (t ; Naked prompt only
15311 (setq ans (read-string prompt default-input
15312 'org-read-date-history timestr))
15313 (when org-read-date-overlay
15314 (delete-overlay org-read-date-overlay)
15315 (setq org-read-date-overlay nil)))))
15317 (setq final (org-read-date-analyze ans org-def org-defdecode))
15319 (when org-read-date-analyze-forced-year
15320 (message "Year was forced into %s"
15321 (if org-read-date-force-compatible-dates
15322 "compatible range (1970-2037)"
15323 "range representable on this machine"))
15326 ;; One round trip to get rid of 34th of August and stuff like that....
15327 (setq final (decode-time (apply 'encode-time final)))
15329 (setq org-read-date-final-answer ans)
15332 (apply 'encode-time final)
15333 (if (and (boundp 'org-time-was-given) org-time-was-given)
15334 (format "%04d-%02d-%02d %02d:%02d"
15335 (nth 5 final) (nth 4 final) (nth 3 final)
15336 (nth 2 final) (nth 1 final))
15337 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15340 (defvar org-defdecode)
15341 (defvar org-with-time)
15342 (defun org-read-date-display ()
15343 "Display the current date prompt interpretation in the minibuffer."
15344 (when org-read-date-display-live
15345 (when org-read-date-overlay
15346 (delete-overlay org-read-date-overlay))
15347 (when (minibufferp (current-buffer))
15350 (while (not (equal (buffer-substring
15351 (max (point-min) (- (point) 4)) (point))
15354 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15355 " " (or org-ans1 org-ans2)))
15356 (org-end-time-was-given nil)
15357 (f (org-read-date-analyze ans org-def org-defdecode))
15359 org-time-stamp-custom-formats
15360 org-time-stamp-formats))
15361 (fmt (if (or org-with-time
15362 (and (boundp 'org-time-was-given) org-time-was-given))
15365 (txt (format-time-string fmt (apply 'encode-time f)))
15366 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15367 (txt (concat "=> " txt)))
15368 (when (and org-end-time-was-given
15369 (string-match org-plain-time-of-day-regexp txt))
15370 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15371 org-end-time-was-given
15372 (substring txt (match-end 0)))))
15373 (when org-read-date-analyze-futurep
15374 (setq txt (concat txt " (=>F)")))
15375 (setq org-read-date-overlay
15376 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15377 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15379 (defun org-read-date-analyze (ans org-def org-defdecode)
15380 "Analyze the combined answer of the date prompt."
15381 ;; FIXME: cleanup and comment
15382 (let ((nowdecode (decode-time (current-time)))
15383 delta deltan deltaw deltadef year month day
15384 hour minute second wday pm h2 m2 tl wday1
15385 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15386 (setq org-read-date-analyze-futurep nil
15387 org-read-date-analyze-forced-year nil)
15388 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15391 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15392 (setq ans (replace-match "" t t ans)
15394 deltaw (nth 1 delta)
15395 deltadef (nth 2 delta)))
15397 ;; Check if there is an iso week date in there
15398 ;; If yes, store the info and postpone interpreting it until the rest
15399 ;; of the parsing is done
15400 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15401 (setq iso-year (if (match-end 1)
15402 (org-small-year-to-year
15403 (string-to-number (match-string 1 ans))))
15404 iso-weekday (if (match-end 3)
15405 (string-to-number (match-string 3 ans)))
15406 iso-week (string-to-number (match-string 2 ans)))
15407 (setq ans (replace-match "" t t ans)))
15409 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15410 (when (string-match
15411 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15412 (setq year (if (match-end 2)
15413 (string-to-number (match-string 2 ans))
15414 (progn (setq kill-year t)
15415 (string-to-number (format-time-string "%Y"))))
15416 month (string-to-number (match-string 3 ans))
15417 day (string-to-number (match-string 4 ans)))
15418 (if (< year 100) (setq year (+ 2000 year)))
15419 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15422 ;; Help matching dotted european dates
15423 (when (string-match
15424 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
15425 (setq year (if (match-end 3)
15426 (string-to-number (match-string 3 ans))
15427 (progn (setq kill-year t)
15428 (string-to-number (format-time-string "%Y"))))
15429 day (string-to-number (match-string 1 ans))
15430 month (string-to-number (match-string 2 ans))
15431 ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15434 ;; Help matching american dates, like 5/30 or 5/30/7
15435 (when (string-match
15436 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15437 (setq year (if (match-end 4)
15438 (string-to-number (match-string 4 ans))
15439 (progn (setq kill-year t)
15440 (string-to-number (format-time-string "%Y"))))
15441 month (string-to-number (match-string 1 ans))
15442 day (string-to-number (match-string 2 ans)))
15443 (if (< year 100) (setq year (+ 2000 year)))
15444 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15446 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15447 ;; If there is a time with am/pm, and *no* time without it, we convert
15448 ;; so that matching will be successful.
15449 (loop for i from 1 to 2 do ; twice, for end time as well
15450 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15451 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15452 (setq hour (string-to-number (match-string 1 ans))
15453 minute (if (match-end 3)
15454 (string-to-number (match-string 3 ans))
15457 (string-to-char (downcase (match-string 4 ans)))))
15458 (if (and (= hour 12) (not pm))
15460 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15461 (setq ans (replace-match (format "%02d:%02d" hour minute)
15464 ;; Check if a time range is given as a duration
15465 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15466 (setq hour (string-to-number (match-string 1 ans))
15467 h2 (+ hour (string-to-number (match-string 3 ans)))
15468 minute (string-to-number (match-string 2 ans))
15469 m2 (+ minute (if (match-end 5) (string-to-number
15470 (match-string 5 ans))0)))
15471 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15472 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15475 ;; Check if there is a time range
15476 (when (boundp 'org-end-time-was-given)
15477 (setq org-time-was-given nil)
15478 (when (and (string-match org-plain-time-of-day-regexp ans)
15480 (setq org-end-time-was-given (match-string 8 ans))
15481 (setq ans (concat (substring ans 0 (match-beginning 7))
15482 (substring ans (match-end 7))))))
15484 (setq tl (parse-time-string ans)
15485 day (or (nth 3 tl) (nth 3 org-defdecode))
15486 month (or (nth 4 tl)
15487 (if (and org-read-date-prefer-future
15488 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15489 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15490 (nth 4 org-defdecode)))
15491 year (or (and (not kill-year) (nth 5 tl))
15492 (if (and org-read-date-prefer-future
15493 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15494 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15495 (nth 5 org-defdecode)))
15496 hour (or (nth 2 tl) (nth 2 org-defdecode))
15497 minute (or (nth 1 tl) (nth 1 org-defdecode))
15498 second (or (nth 0 tl) 0)
15501 (when (and (eq org-read-date-prefer-future 'time)
15502 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15503 (equal day (nth 3 nowdecode))
15504 (equal month (nth 4 nowdecode))
15505 (equal year (nth 5 nowdecode))
15507 (or (< (nth 2 tl) (nth 2 nowdecode))
15508 (and (= (nth 2 tl) (nth 2 nowdecode))
15510 (< (nth 1 tl) (nth 1 nowdecode)))))
15514 ;; Special date definitions below
15517 ;; There was an iso week
15520 (setq year (or iso-year year)
15521 day (or iso-weekday wday 1)
15522 wday nil ; to make sure that the trigger below does not match
15523 iso-date (calendar-gregorian-from-absolute
15524 (calendar-absolute-from-iso
15525 (list iso-week day year))))
15526 ; FIXME: Should we also push ISO weeks into the future?
15527 ; (when (and org-read-date-prefer-future
15529 ; (< (calendar-absolute-from-gregorian iso-date)
15530 ; (time-to-days (current-time))))
15531 ; (setq year (1+ year)
15532 ; iso-date (calendar-gregorian-from-absolute
15533 ; (calendar-absolute-from-iso
15534 ; (list iso-week day year)))))
15535 (setq month (car iso-date)
15536 year (nth 2 iso-date)
15537 day (nth 1 iso-date)))
15541 (let ((now (decode-time (current-time))))
15542 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15543 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15544 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15545 ((equal deltaw "m") (setq month (+ month deltan)))
15546 ((equal deltaw "y") (setq year (+ year deltan)))))
15547 ((and wday (not (nth 3 tl)))
15549 ;; Weekday was given, but no day, so pick that day in the week
15550 ;; on or after the derived date.
15551 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15552 (unless (equal wday wday1)
15553 (setq day (+ day (% (- wday wday1 -7) 7))))))
15554 (if (and (boundp 'org-time-was-given)
15556 (setq org-time-was-given t))
15557 (if (< year 100) (setq year (+ 2000 year)))
15558 ;; Check of the date is representable
15559 (if org-read-date-force-compatible-dates
15562 (setq year 1970 org-read-date-analyze-forced-year t))
15564 (setq year 2037 org-read-date-analyze-forced-year t)))
15565 (condition-case nil
15566 (ignore (encode-time second minute hour day month year))
15568 (setq year (nth 5 org-defdecode))
15569 (setq org-read-date-analyze-forced-year t))))
15570 (setq org-read-date-analyze-futurep futurep)
15571 (list second minute hour day month year)))
15573 (defvar parse-time-weekdays)
15574 (defun org-read-date-get-relative (s today default)
15575 "Check string S for special relative date string.
15576 TODAY and DEFAULT are internal times, for today and for a default.
15577 Return shift list (N what def-flag)
15578 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15579 N is the number of WHATs to shift.
15580 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15581 the DEFAULT date rather than TODAY."
15582 (require 'parse-time)
15586 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15588 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15589 "\\([ \t]\\|$\\)") s)
15590 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15591 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15592 (string-to-char (substring (match-string 1 s) -1))
15594 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15595 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15596 (what (if (match-end 3) (match-string 3 s) "d"))
15597 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15598 (date (if rel default today))
15599 (wday (nth 6 (decode-time date)))
15603 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15604 (if (= dir ?-) (setq delta (- delta 7)))
15605 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15606 (list delta "d" rel))
15607 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15609 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15610 "Turn a user-specified date into the internal representation.
15611 The internal representation needed by the calendar is (month day year).
15612 This is a wrapper to handle the brain-dead convention in calendar that
15613 user function argument order change dependent on argument order."
15614 (if (boundp 'calendar-date-style)
15616 ((eq calendar-date-style 'american)
15617 (list arg1 arg2 arg3))
15618 ((eq calendar-date-style 'european)
15619 (list arg2 arg1 arg3))
15620 ((eq calendar-date-style 'iso)
15621 (list arg2 arg3 arg1)))
15622 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15623 (if (org-bound-and-true-p european-calendar-style)
15624 (list arg2 arg1 arg3)
15625 (list arg1 arg2 arg3)))))
15627 (defun org-eval-in-calendar (form &optional keepdate)
15628 "Eval FORM in the calendar window and return to current window.
15629 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
15630 otherwise stick to the current value of `org-ans2'."
15631 (let ((sf (selected-frame))
15632 (sw (selected-window)))
15633 (select-window (get-buffer-window "*Calendar*" t))
15635 (when (and (not keepdate) (calendar-cursor-to-date))
15636 (let* ((date (calendar-cursor-to-date))
15637 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15638 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15639 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15641 (org-select-frame-set-input-focus sf)))
15643 (defun org-calendar-select ()
15644 "Return to `org-read-date' with the date currently selected.
15645 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15647 (when (calendar-cursor-to-date)
15648 (let* ((date (calendar-cursor-to-date))
15649 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15650 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15651 (if (active-minibuffer-window) (exit-minibuffer))))
15653 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15654 "Insert a date stamp for the date given by the internal TIME.
15655 WITH-HM means use the stamp format that includes the time of the day.
15656 INACTIVE means use square brackets instead of angular ones, so that the
15657 stamp will not contribute to the agenda.
15658 PRE and POST are optional strings to be inserted before and after the
15660 The command returns the inserted time stamp."
15661 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15663 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15664 (insert-before-markers (or pre ""))
15665 (when (listp extra)
15666 (setq extra (car extra))
15667 (if (and (stringp extra)
15668 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15669 (setq extra (format "-%02d:%02d"
15670 (string-to-number (match-string 1 extra))
15671 (string-to-number (match-string 2 extra))))
15674 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15675 (insert-before-markers (setq stamp (format-time-string fmt time)))
15676 (insert-before-markers (or post ""))
15677 (setq org-last-inserted-timestamp stamp)))
15679 (defun org-toggle-time-stamp-overlays ()
15680 "Toggle the use of custom time stamp formats."
15682 (setq org-display-custom-times (not org-display-custom-times))
15683 (unless org-display-custom-times
15684 (let ((p (point-min)) (bmp (buffer-modified-p)))
15685 (while (setq p (next-single-property-change p 'display))
15686 (if (and (get-text-property p 'display)
15687 (eq (get-text-property p 'face) 'org-date))
15688 (remove-text-properties
15689 p (setq p (next-single-property-change p 'display))
15691 (set-buffer-modified-p bmp)))
15692 (if (featurep 'xemacs)
15693 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15694 (org-restart-font-lock)
15695 (setq org-table-may-need-update t)
15696 (if org-display-custom-times
15697 (message "Time stamps are overlaid with custom format")
15698 (message "Time stamp overlays removed")))
15700 (defun org-display-custom-time (beg end)
15701 "Overlay modified time stamp format over timestamp between BEG and END."
15702 (let* ((ts (buffer-substring beg end))
15703 t1 w1 with-hm tf time str w2 (off 0))
15705 (setq t1 (org-parse-time-string ts t))
15706 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
15707 (setq off (- (match-end 0) (match-beginning 0)))))
15708 (setq end (- end off))
15709 (setq w1 (- end beg)
15710 with-hm (and (nth 1 t1) (nth 2 t1))
15711 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15712 time (org-fix-decoded-time t1)
15714 (format-time-string
15715 (substring tf 1 -1) (apply 'encode-time time))
15716 nil 'mouse-face 'highlight)
15718 (if (not (= w2 w1))
15719 (add-text-properties (1+ beg) (+ 2 beg)
15720 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15721 (if (featurep 'xemacs)
15723 (put-text-property beg end 'invisible t)
15724 (put-text-property beg end 'end-glyph (make-glyph str)))
15725 (put-text-property beg end 'display str))))
15727 (defun org-translate-time (string)
15728 "Translate all timestamps in STRING to custom format.
15729 But do this only if the variable `org-display-custom-times' is set."
15730 (when org-display-custom-times
15733 (re org-ts-regexp-both)
15734 t1 with-hm inactive tf time str beg end)
15735 (while (setq start (string-match re string start))
15736 (setq beg (match-beginning 0)
15738 t1 (save-match-data
15739 (org-parse-time-string (substring string beg end) t))
15740 with-hm (and (nth 1 t1) (nth 2 t1))
15741 inactive (equal (substring string beg (1+ beg)) "[")
15742 tf (funcall (if with-hm 'cdr 'car)
15743 org-time-stamp-custom-formats)
15744 time (org-fix-decoded-time t1)
15745 str (format-time-string
15747 (if inactive "[" "<") (substring tf 1 -1)
15748 (if inactive "]" ">"))
15749 (apply 'encode-time time))
15750 string (replace-match str t t string)
15751 start (+ start (length str)))))))
15754 (defun org-fix-decoded-time (time)
15755 "Set 0 instead of nil for the first 6 elements of time.
15756 Don't touch the rest."
15758 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15760 (defun org-days-to-time (timestamp-string)
15761 "Difference between TIMESTAMP-STRING and now in days."
15762 (- (time-to-days (org-time-string-to-time timestamp-string))
15763 (time-to-days (current-time))))
15765 (defun org-deadline-close (timestamp-string &optional ndays)
15766 "Is the time in TIMESTAMP-STRING close to the current date?"
15767 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15768 (and (< (org-days-to-time timestamp-string) ndays)
15769 (not (org-entry-is-done-p))))
15771 (defun org-get-wdays (ts)
15772 "Get the deadline lead time appropriate for timestring TS."
15774 ((<= org-deadline-warning-days 0)
15775 ;; 0 or negative, enforce this value no matter what
15776 (- org-deadline-warning-days))
15777 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
15778 ;; lead time is specified.
15779 (floor (* (string-to-number (match-string 1 ts))
15780 (cdr (assoc (match-string 2 ts)
15781 '(("d" . 1) ("w" . 7)
15782 ("m" . 30.4) ("y" . 365.25)))))))
15783 ;; go for the default.
15784 (t org-deadline-warning-days)))
15786 (defun org-calendar-select-mouse (ev)
15787 "Return to `org-read-date' with the date currently selected.
15788 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15790 (mouse-set-point ev)
15791 (when (calendar-cursor-to-date)
15792 (let* ((date (calendar-cursor-to-date))
15793 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15794 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15795 (if (active-minibuffer-window) (exit-minibuffer))))
15797 (defun org-check-deadlines (ndays)
15798 "Check if there are any deadlines due or past due.
15799 A deadline is considered due if it happens within `org-deadline-warning-days'
15800 days from today's date. If the deadline appears in an entry marked DONE,
15801 it is not shown. The prefix arg NDAYS can be used to test that many
15802 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15804 (let* ((org-warn-days
15806 ((equal ndays '(4)) 100000)
15807 (ndays (prefix-numeric-value ndays))
15808 (t (abs org-deadline-warning-days))))
15809 (case-fold-search nil)
15810 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15812 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15814 (message "%d deadlines past-due or due within %d days"
15815 (org-occur regexp nil callback)
15818 (defun org-check-before-date (date)
15819 "Check if there are deadlines or scheduled entries before DATE."
15820 (interactive (list (org-read-date)))
15821 (let ((case-fold-search nil)
15822 (regexp (concat "\\<\\(" org-deadline-string
15823 "\\|" org-scheduled-string
15824 "\\) *<\\([^>]+\\)>"))
15826 (lambda () (time-less-p
15827 (org-time-string-to-time (match-string 2))
15828 (org-time-string-to-time date)))))
15829 (message "%d entries before %s"
15830 (org-occur regexp nil callback) date)))
15832 (defun org-check-after-date (date)
15833 "Check if there are deadlines or scheduled entries after DATE."
15834 (interactive (list (org-read-date)))
15835 (let ((case-fold-search nil)
15836 (regexp (concat "\\<\\(" org-deadline-string
15837 "\\|" org-scheduled-string
15838 "\\) *<\\([^>]+\\)>"))
15842 (org-time-string-to-time (match-string 2))
15843 (org-time-string-to-time date))))))
15844 (message "%d entries after %s"
15845 (org-occur regexp nil callback) date)))
15847 (defun org-check-dates-range (start-date end-date)
15848 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
15849 (interactive (list (org-read-date nil nil nil "Range starts")
15850 (org-read-date nil nil nil "Range end")))
15851 (let ((case-fold-search nil)
15852 (regexp (concat "\\<\\(" org-deadline-string
15853 "\\|" org-scheduled-string
15854 "\\) *<\\([^>]+\\)>"))
15857 (let ((match (match-string 2)))
15860 (org-time-string-to-time match)
15861 (org-time-string-to-time start-date)))
15863 (org-time-string-to-time match)
15864 (org-time-string-to-time end-date)))))))
15865 (message "%d entries between %s and %s"
15866 (org-occur regexp nil callback) start-date end-date)))
15868 (defun org-evaluate-time-range (&optional to-buffer)
15869 "Evaluate a time range by computing the difference between start and end.
15870 Normally the result is just printed in the echo area, but with prefix arg
15871 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
15872 If the time range is actually in a table, the result is inserted into the
15874 For time difference computation, a year is assumed to be exactly 365
15875 days in order to avoid rounding problems."
15878 (org-clock-update-time-maybe)
15880 (unless (org-at-date-range-p t)
15881 (goto-char (point-at-bol))
15882 (re-search-forward org-tr-regexp-both (point-at-eol) t))
15883 (if (not (org-at-date-range-p t))
15884 (error "Not at a time-stamp range, and none found in current line")))
15885 (let* ((ts1 (match-string 1))
15886 (ts2 (match-string 2))
15887 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
15888 (match-end (match-end 0))
15889 (time1 (org-time-string-to-time ts1))
15890 (time2 (org-time-string-to-time ts2))
15891 (t1 (org-float-time time1))
15892 (t2 (org-float-time time2))
15893 (diff (abs (- t2 t1)))
15894 (negative (< (- t2 t1) 0))
15895 ;; (ys (floor (* 365 24 60 60)))
15898 (fy "%dy %dd %02d:%02d")
15900 (fd "%dd %02d:%02d")
15905 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15907 d (floor (/ diff ds)) diff (mod diff ds)
15908 h (floor (/ diff hs)) diff (mod diff hs)
15909 m (floor (/ diff 60)))
15910 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15912 d (floor (+ (/ diff ds) 0.5))
15914 (if (not to-buffer)
15915 (message "%s" (org-make-tdiff-string y d h m))
15916 (if (org-at-table-p)
15918 (goto-char match-end)
15920 (and (looking-at " *|") (goto-char (match-end 0))))
15921 (goto-char match-end))
15923 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
15924 (replace-match ""))
15925 (if negative (insert " -"))
15926 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
15927 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
15928 (insert " " (format fh h m))))
15929 (if align (org-table-align))
15930 (message "Time difference inserted")))))
15932 (defun org-make-tdiff-string (y d h m)
15935 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
15937 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
15939 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
15941 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
15943 (apply 'format fmt (nreverse l))))
15945 (defun org-time-string-to-time (s &optional buffer pos)
15946 (condition-case errdata
15947 (apply 'encode-time (org-parse-time-string s))
15948 (error (error "Bad timestamp `%s'%s\nError was: %s"
15949 s (if (not (and buffer pos))
15951 (format " at %d in buffer `%s'" pos buffer))
15954 (defun org-time-string-to-seconds (s)
15955 (org-float-time (org-time-string-to-time s)))
15957 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
15958 "Convert a time stamp to an absolute day number.
15959 If there is a specifier for a cyclic time stamp, get the closest date to
15961 PREFER and SHOW-ALL are passed through to `org-closest-date'.
15962 The variable date is bound by the calendar when this is called."
15964 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
15965 (if (org-diary-sexp-entry (match-string 1 s) "" date)
15968 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
15969 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
15970 (time-to-days (current-time))) (match-string 0 s)
15973 (condition-case errdata
15974 (apply 'encode-time (org-parse-time-string s))
15975 (error (error "Bad timestamp `%s'%s\nError was: %s"
15976 s (if (not (and buffer pos))
15978 (format " at %d in buffer `%s'" pos buffer))
15979 (cdr errdata))))))))
15981 (defun org-days-to-iso-week (days)
15982 "Return the iso week number."
15984 (car (calendar-iso-from-absolute days)))
15986 (defun org-small-year-to-year (year)
15987 "Convert 2-digit years into 4-digit years.
15988 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
15989 The year 2000 cannot be abbreviated. Any year larger than 99
15990 is returned unchanged."
15992 (setq year (+ 2000 year))
15994 (setq year (+ 1900 year))))
15997 (defun org-time-from-absolute (d)
15998 "Return the time corresponding to date D.
15999 D may be an absolute day number, or a calendar-type list (month day year)."
16000 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16001 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16003 (defun org-calendar-holiday ()
16004 "List of holidays, for Diary display in Org-mode."
16005 (require 'holidays)
16007 (if (fboundp 'calendar-check-holidays)
16008 'calendar-check-holidays 'check-calendar-holidays) date)))
16009 (if hl (mapconcat 'identity hl "; "))))
16011 (defun org-diary-sexp-entry (sexp entry date)
16012 "Process a SEXP diary ENTRY for DATE."
16013 (require 'diary-lib)
16014 (let ((result (if calendar-debug-sexp
16015 (let ((stack-trace-on-error t))
16016 (eval (car (read-from-string sexp))))
16017 (condition-case nil
16018 (eval (car (read-from-string sexp)))
16021 (message "Bad sexp at line %d in %s: %s"
16023 (buffer-file-name) sexp)
16025 (cond ((stringp result) (split-string result "; "))
16026 ((and (consp result)
16027 (not (consp (cdr result)))
16028 (stringp (cdr result))) (cdr result))
16029 ((and (consp result)
16030 (stringp (car result))) result)
16034 (defun org-diary-to-ical-string (frombuf)
16035 "Get iCalendar entries from diary entries in buffer FROMBUF.
16036 This uses the icalendar.el library."
16037 (let* ((tmpdir (if (featurep 'xemacs)
16039 temporary-file-directory))
16040 (tmpfile (make-temp-name
16041 (expand-file-name "orgics" tmpdir)))
16043 (with-current-buffer frombuf
16044 (icalendar-export-region (point-min) (point-max) tmpfile)
16045 (setq buf (find-buffer-visiting tmpfile))
16047 (goto-char (point-min))
16048 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16049 (setq b (match-beginning 0)))
16050 (goto-char (point-max))
16051 (if (re-search-backward "^END:VEVENT" nil t)
16052 (setq e (match-end 0)))
16053 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16055 (delete-file tmpfile)
16058 (defun org-closest-date (start current change prefer show-all)
16059 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16060 When PREFER is `past', return a date that is either CURRENT or past.
16061 When PREFER is `future', return a date that is either CURRENT or future.
16062 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16063 ;; Make the proper lists from the dates
16065 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
16066 dn dw sday cday n1 n2 n0
16067 d m y y1 y2 date1 date2 nmonths nm ny m2)
16069 (setq start (org-date-to-gregorian start)
16070 current (org-date-to-gregorian
16073 (time-to-days (current-time))))
16074 sday (calendar-absolute-from-gregorian start)
16075 cday (calendar-absolute-from-gregorian current))
16077 (if (<= cday sday) (throw 'exit sday))
16079 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16080 (setq dn (string-to-number (match-string 1 change))
16081 dw (cdr (assoc (match-string 2 change) a1)))
16082 (error "Invalid change specifier: %s" change))
16083 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16086 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16089 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16090 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16091 (setq date1 (list m d y1)
16092 n1 (calendar-absolute-from-gregorian date1)
16093 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16094 n2 (calendar-absolute-from-gregorian date2)))
16096 ;; approx number of month between the two dates
16097 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16098 ;; How often does dn fit in there?
16099 (setq d (nth 1 start) m (car start) y (nth 2 start)
16100 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16102 ny (floor (/ m 12))
16105 (while (> m 12) (setq m (- m 12) y (1+ y)))
16106 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16107 (setq m2 (+ m dn) y2 y)
16108 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16109 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16110 (while (<= n2 cday)
16111 (setq n1 n2 m m2 y y2)
16112 (setq m2 (+ m dn) y2 y)
16113 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16114 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16115 ;; Make sure n1 is the earlier date
16116 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16119 ((eq prefer 'past) (if (= cday n2) n2 n1))
16120 ((eq prefer 'future) (if (= cday n1) n1 n2))
16121 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16123 ((eq prefer 'past) (if (= cday n2) n2 n1))
16124 ((eq prefer 'future) (if (= cday n1) n1 n2))
16125 (t (if (= cday n1) n1 n2)))))))
16127 (defun org-date-to-gregorian (date)
16128 "Turn any specification of DATE into a Gregorian date for the calendar."
16129 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16130 ((and (listp date) (= (length date) 3)) date)
16132 (setq date (org-parse-time-string date))
16133 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16135 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16137 (defun org-parse-time-string (s &optional nodefault)
16138 "Parse the standard Org-mode time string.
16139 This should be a lot faster than the normal `parse-time-string'.
16140 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16141 hour and minute fields will be nil if not given."
16142 (if (string-match org-ts-regexp0 s)
16144 (if (or (match-beginning 8) (not nodefault))
16145 (string-to-number (or (match-string 8 s) "0")))
16146 (if (or (match-beginning 7) (not nodefault))
16147 (string-to-number (or (match-string 7 s) "0")))
16148 (string-to-number (match-string 4 s))
16149 (string-to-number (match-string 3 s))
16150 (string-to-number (match-string 2 s))
16152 (error "Not a standard Org-mode time string: %s" s)))
16154 (defun org-timestamp-up (&optional arg)
16155 "Increase the date item at the cursor by one.
16156 If the cursor is on the year, change the year. If it is on the month,
16157 the day or the time, change that.
16158 With prefix ARG, change by that many units."
16160 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16162 (defun org-timestamp-down (&optional arg)
16163 "Decrease the date item at the cursor by one.
16164 If the cursor is on the year, change the year. If it is on the month,
16165 the day or the time, change that.
16166 With prefix ARG, change by that many units."
16168 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16170 (defun org-timestamp-up-day (&optional arg)
16171 "Increase the date in the time stamp by one day.
16172 With prefix ARG, change that many days."
16174 (if (and (not (org-at-timestamp-p t))
16175 (org-at-heading-p))
16177 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16179 (defun org-timestamp-down-day (&optional arg)
16180 "Decrease the date in the time stamp by one day.
16181 With prefix ARG, change that many days."
16183 (if (and (not (org-at-timestamp-p t))
16184 (org-at-heading-p))
16186 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16188 (defun org-at-timestamp-p (&optional inactive-ok)
16189 "Determine if the cursor is in or at a timestamp."
16191 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16193 (ans (or (looking-at tsr)
16195 (skip-chars-backward "^[<\n\r\t")
16196 (if (> (point) (point-min)) (backward-char 1))
16197 (and (looking-at tsr)
16198 (> (- (match-end 0) pos) -1))))))
16200 (boundp 'org-ts-what)
16203 ((= pos (match-beginning 0)) 'bracket)
16204 ;; Point is considered to be "on the bracket" whether
16205 ;; it's really on it or right after it.
16206 ((= pos (1- (match-end 0))) 'bracket)
16207 ((= pos (match-end 0)) 'after)
16208 ((org-pos-in-match-range pos 2) 'year)
16209 ((org-pos-in-match-range pos 3) 'month)
16210 ((org-pos-in-match-range pos 7) 'hour)
16211 ((org-pos-in-match-range pos 8) 'minute)
16212 ((or (org-pos-in-match-range pos 4)
16213 (org-pos-in-match-range pos 5)) 'day)
16214 ((and (> pos (or (match-end 8) (match-end 5)))
16215 (< pos (match-end 0)))
16216 (- pos (or (match-end 8) (match-end 5))))
16220 (defun org-toggle-timestamp-type ()
16221 "Toggle the type (<active> or [inactive]) of a time stamp."
16223 (when (org-at-timestamp-p t)
16224 (let ((beg (match-beginning 0)) (end (match-end 0))
16225 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16228 (while (re-search-forward "[][<>]" end t)
16229 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16231 (message "Timestamp is now %sactive"
16232 (if (equal (char-after beg) ?<) "" "in")))))
16234 (defvar org-clock-history) ; defined in org-clock.el
16235 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16236 (defun org-timestamp-change (n &optional what updown)
16237 "Change the date in the time stamp at point.
16238 The date will be changed by N times WHAT. WHAT can be `day', `month',
16239 `year', `minute', `second'. If WHAT is not given, the cursor position
16240 in the timestamp determines what will be changed."
16241 (let ((origin (point)) origin-cat
16243 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16246 ts time time0 fixnext clrgx)
16247 (if (not (org-at-timestamp-p t))
16248 (error "Not at a timestamp"))
16249 (if (and (not what) (eq org-ts-what 'bracket))
16250 (org-toggle-timestamp-type)
16251 ;; Point isn't on brackets. Remember the part of the time-stamp
16252 ;; the point was in. Indeed, size of time-stamps may change,
16253 ;; but point must be kept in the same category nonetheless.
16254 (setq origin-cat org-ts-what)
16255 (if (and (not what) (not (eq org-ts-what 'day))
16256 org-display-custom-times
16257 (get-text-property (point) 'display)
16258 (not (get-text-property (1- (point)) 'display)))
16259 (setq org-ts-what 'day))
16260 (setq org-ts-what (or what org-ts-what)
16261 inactive (= (char-after (match-beginning 0)) ?\[)
16262 ts (match-string 0))
16265 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16267 (setq extra (match-string 1 ts)))
16268 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16270 (setq time0 (org-parse-time-string ts))
16272 (eq org-ts-what 'minute)
16273 (not current-prefix-arg))
16274 ;; This looks like s-up and s-down. Change by one rounding step.
16275 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16276 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16277 (setcar (cdr time0) (+ (nth 1 time0)
16278 (if (> n 0) (- rem) (- dm rem))))))
16280 (encode-time (or (car time0) 0)
16281 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16282 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16283 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16284 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16285 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16287 (when (and (member org-ts-what '(hour minute))
16289 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16290 (setq extra (org-modify-ts-extra
16292 (if (eq org-ts-what 'hour) 2 5)
16294 (when (integerp org-ts-what)
16295 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16296 (if (eq what 'calendar)
16297 (let ((cal-date (org-get-date-from-calendar)))
16298 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16299 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16300 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16301 (setcar time0 (or (car time0) 0))
16302 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16303 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16304 (setq time (apply 'encode-time time0))))
16305 ;; Insert the new time-stamp, and ensure point stays in the same
16306 ;; category as before (i.e. not after the last position in that
16308 (let ((pos (point)))
16309 ;; Stay before inserted string. `save-excursion' is of no use.
16310 (setq org-last-changed-timestamp
16311 (org-insert-time-stamp time with-hm inactive nil nil extra))
16314 (looking-at org-ts-regexp3)
16316 ;; `day' category ends before `hour' if any, or at
16317 ;; the end of the day name.
16318 ((eq origin-cat 'day)
16319 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16320 ((eq origin-cat 'hour) (min (match-end 7) origin))
16321 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16322 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16323 ;; `year' and `month' have both fixed size: point
16324 ;; couldn't have moved into another part.
16326 ;; Update clock if on a CLOCK line.
16327 (org-clock-update-time-maybe)
16328 ;; Maybe adjust the closest clock in `org-clock-history'
16329 (when org-clock-adjust-closest
16330 (if (not (and (org-at-clock-log-p)
16331 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16332 org-clock-history))))))
16333 (message "No clock to adjust")
16334 (cond ((save-excursion ; fix previous clock?
16335 (re-search-backward org-ts-regexp0 nil t)
16336 (looking-back (concat org-clock-string " \\[")))
16337 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16338 ((save-excursion ; fix next clock?
16339 (re-search-backward org-ts-regexp0 nil t)
16340 (looking-at (concat org-ts-regexp0 "\\] =>")))
16341 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16342 (save-window-excursion
16343 ;; Find closest clock to point, adjust the previous/next one in history
16344 (let* ((p (save-excursion (org-back-to-heading t)))
16345 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16347 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16348 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16350 (message "No clock to adjust")
16351 (org-goto-marker-or-bmk clfixpos)
16353 (when (re-search-forward clrgx nil t)
16354 (goto-char (match-beginning 1))
16355 (let (org-clock-adjust-closest)
16356 (org-timestamp-change n org-ts-what updown))
16357 (message "Clock adjusted in %s for heading: %s"
16358 (file-name-nondirectory (buffer-file-name))
16359 (org-get-heading t t))))))))
16360 ;; Try to recenter the calendar window, if any.
16361 (if (and org-calendar-follow-timestamp-change
16362 (get-buffer-window "*Calendar*" t)
16363 (memq org-ts-what '(day month year)))
16364 (org-recenter-calendar (time-to-days time))))))
16366 (defun org-modify-ts-extra (s pos n dm)
16367 "Change the different parts of the lead-time and repeat fields in timestamp."
16368 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16370 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16372 ((or (org-pos-in-match-range pos 2)
16373 (org-pos-in-match-range pos 3))
16374 (setq m (string-to-number (match-string 3 s))
16375 h (string-to-number (match-string 2 s)))
16376 (if (org-pos-in-match-range pos 2)
16378 (setq n (* dm (org-no-warnings (signum n))))
16379 (when (not (= 0 (setq rem (% m dm))))
16380 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16382 (if (< m 0) (setq m (+ m 60) h (1- h)))
16383 (if (> m 59) (setq m (- m 60) h (1+ h)))
16384 (setq h (min 24 (max 0 h)))
16385 (setq ng 1 new (format "-%02d:%02d" h m)))
16386 ((org-pos-in-match-range pos 6)
16387 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16388 ((org-pos-in-match-range pos 5)
16389 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16391 ((org-pos-in-match-range pos 9)
16392 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16393 ((org-pos-in-match-range pos 8)
16394 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16398 (substring s 0 (match-beginning ng))
16400 (substring s (match-end ng))))))
16403 (defun org-recenter-calendar (date)
16404 "If the calendar is visible, recenter it to DATE."
16405 (let ((cwin (get-buffer-window "*Calendar*" t)))
16407 (let ((calendar-move-hook nil))
16408 (with-selected-window cwin
16409 (calendar-goto-date (if (listp date) date
16410 (calendar-gregorian-from-absolute date))))))))
16412 (defun org-goto-calendar (&optional arg)
16413 "Go to the Emacs calendar at the current date.
16414 If there is a time stamp in the current line, go to that date.
16415 A prefix ARG can be used to force the current date."
16417 (let ((tsr org-ts-regexp) diff
16418 (calendar-move-hook nil)
16419 (calendar-view-holidays-initially-flag nil)
16420 (calendar-view-diary-initially-flag nil))
16421 (if (or (org-at-timestamp-p)
16423 (beginning-of-line 1)
16424 (looking-at (concat ".*" tsr))))
16425 (let ((d1 (time-to-days (current-time)))
16427 (org-time-string-to-time (match-string 1)))))
16428 (setq diff (- d2 d1))))
16430 (calendar-goto-today)
16431 (if (and diff (not arg)) (calendar-forward-day diff))))
16433 (defun org-get-date-from-calendar ()
16434 "Return a list (month day year) of date at point in calendar."
16435 (with-current-buffer "*Calendar*"
16437 (calendar-cursor-to-date))))
16439 (defun org-date-from-calendar ()
16440 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16441 If there is already a time stamp at the cursor position, update it."
16443 (if (org-at-timestamp-p t)
16444 (org-timestamp-change 0 'calendar)
16445 (let ((cal-date (org-get-date-from-calendar)))
16446 (org-insert-time-stamp
16447 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16449 (defun org-minutes-to-hh:mm-string (m)
16450 "Compute H:MM from a number of minutes."
16451 (let ((h (/ m 60)))
16452 (setq m (- m (* 60 h)))
16453 (format org-time-clocksum-format h m)))
16455 (defun org-hh:mm-string-to-minutes (s)
16456 "Convert a string H:MM to a number of minutes.
16457 If the string is just a number, interpret it as minutes.
16458 In fact, the first hh:mm or number in the string will be taken,
16459 there can be extra stuff in the string.
16460 If no number is found, the return value is 0."
16463 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16464 (+ (* (string-to-number (match-string 1 s)) 60)
16465 (string-to-number (match-string 2 s))))
16466 ((string-match "\\([0-9]+\\)" s)
16467 (string-to-number (match-string 1 s)))
16470 (defcustom org-effort-durations
16473 ("w" . ,(* 60 8 5))
16474 ("m" . ,(* 60 8 5 4))
16475 ("y" . ,(* 60 8 5 40)))
16476 "Conversion factor to minutes for an effort modifier.
16478 Each entry has the form (MODIFIER . MINUTES).
16480 In an effort string, a number followed by MODIFIER is multiplied
16481 by the specified number of MINUTES to obtain an effort in
16484 For example, if the value of this variable is ((\"hours\" . 60)), then an
16485 effort string \"2hours\" is equivalent to 120 minutes."
16488 :type '(alist :key-type (string :tag "Modifier")
16489 :value-type (number :tag "Minutes")))
16491 (defun org-duration-string-to-minutes (s &optional output-to-string)
16492 "Convert a duration string S to minutes.
16494 A bare number is interpreted as minutes, modifiers can be set by
16495 customizing `org-effort-durations' (which see).
16497 Entries containing a colon are interpreted as H:MM by
16498 `org-hh:mm-string-to-minutes'."
16500 (re (concat "\\([0-9.]+\\) *\\("
16501 (regexp-opt (mapcar 'car org-effort-durations))
16503 (while (string-match re s)
16504 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16505 (string-to-number (match-string 1 s))))
16506 (setq s (replace-match "" nil t s)))
16507 (setq result (floor result))
16508 (incf result (org-hh:mm-string-to-minutes s))
16509 (if output-to-string (number-to-string result) result)))
16513 (defun org-save-all-org-buffers ()
16514 "Save all Org-mode buffers without user confirmation."
16516 (message "Saving all Org-mode buffers...")
16517 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
16518 (when (featurep 'org-id) (org-id-locations-save))
16519 (message "Saving all Org-mode buffers... done"))
16521 (defun org-revert-all-org-buffers ()
16522 "Revert all Org-mode buffers.
16523 Prompt for confirmation when there are unsaved changes.
16524 Be sure you know what you are doing before letting this function
16525 overwrite your changes.
16527 This function is useful in a setup where one tracks org files
16528 with a version control system, to revert on one machine after pulling
16529 changes from another. I believe the procedure must be like this:
16531 1. M-x org-save-all-org-buffers
16532 2. Pull changes from the other machine, resolve conflicts
16533 3. M-x org-revert-all-org-buffers"
16535 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16538 (save-window-excursion
16541 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
16542 (with-current-buffer b buffer-file-name))
16543 (org-pop-to-buffer-same-window b)
16544 (revert-buffer t 'no-confirm)))
16546 (when (and (featurep 'org-id) org-id-track-globally)
16547 (org-id-locations-load)))))
16552 (defun org-switchb (&optional arg)
16553 "Switch between Org buffers.
16554 With one prefix argument, restrict available buffers to files.
16555 With two prefix arguments, restrict available buffers to agenda files.
16557 Defaults to `iswitchb' for buffer name completion.
16558 Set `org-completion-use-ido' to make it use ido instead."
16560 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16561 ((equal arg '(16)) (org-buffer-list 'agenda))
16562 (t (org-buffer-list))))
16563 (org-completion-use-iswitchb org-completion-use-iswitchb)
16564 (org-completion-use-ido org-completion-use-ido))
16565 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16566 (setq org-completion-use-iswitchb t))
16567 (org-pop-to-buffer-same-window
16568 (org-icompleting-read "Org buffer: "
16569 (mapcar 'list (mapcar 'buffer-name blist))
16572 ;;; Define some older names previously used for this functionality
16574 (defalias 'org-ido-switchb 'org-switchb)
16576 (defalias 'org-iswitchb 'org-switchb)
16578 (defun org-buffer-list (&optional predicate exclude-tmp)
16579 "Return a list of Org buffers.
16580 PREDICATE can be `export', `files' or `agenda'.
16582 export restrict the list to Export buffers.
16583 files restrict the list to buffers visiting Org files.
16584 agenda restrict the list to buffers visiting agenda files.
16586 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16588 (agenda-files (and (eq predicate 'agenda)
16589 (mapcar 'file-truename (org-agenda-files t))))
16592 ((eq predicate 'files)
16593 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
16594 ((eq predicate 'export)
16595 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16596 ((eq predicate 'agenda)
16598 (with-current-buffer b
16599 (and (derived-mode-p 'org-mode)
16600 (setq bfn (buffer-file-name b))
16601 (member (file-truename bfn) agenda-files)))))
16602 (t (lambda (b) (with-current-buffer b
16603 (or (derived-mode-p 'org-mode)
16604 (string-match "\*Org .*Export"
16605 (buffer-name b)))))))))
16609 (if (and (funcall filter b)
16610 (or (not exclude-tmp)
16611 (not (string-match "tmp" (buffer-name b)))))
16616 (defun org-agenda-files (&optional unrestricted archives)
16617 "Get the list of agenda files.
16618 Optional UNRESTRICTED means return the full list even if a restriction
16619 is currently in place.
16620 When ARCHIVES is t, include all archive files that are really being
16621 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16622 `org-agenda-archives-mode' is t."
16625 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16626 ((stringp org-agenda-files) (org-read-agenda-file-list))
16627 ((listp org-agenda-files) org-agenda-files)
16628 (t (error "Invalid value of `org-agenda-files'")))))
16629 (setq files (apply 'append
16630 (mapcar (lambda (f)
16631 (if (file-directory-p f)
16633 f t org-agenda-file-regexp)
16636 (when org-agenda-skip-unavailable-files
16637 (setq files (delq nil
16640 (and (file-readable-p file) file)))
16642 (when (or (eq archives t)
16643 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16644 (setq files (org-add-archive-files files)))
16647 (defun org-agenda-file-p (&optional file)
16648 "Return non-nil, if FILE is an agenda file.
16649 If FILE is omitted, use the file associated with the current
16651 (member (or file (buffer-file-name))
16652 (org-agenda-files t)))
16654 (defun org-edit-agenda-file-list ()
16655 "Edit the list of agenda files.
16656 Depending on setup, this either uses customize to edit the variable
16657 `org-agenda-files', or it visits the file that is holding the list. In the
16658 latter case, the buffer is set up in a way that saving it automatically kills
16659 the buffer and restores the previous window configuration."
16661 (if (stringp org-agenda-files)
16662 (let ((cw (current-window-configuration)))
16663 (find-file org-agenda-files)
16664 (org-set-local 'org-window-configuration cw)
16665 (org-add-hook 'after-save-hook
16667 (set-window-configuration
16668 (prog1 org-window-configuration
16669 (kill-buffer (current-buffer))))
16670 (org-install-agenda-files-menu)
16671 (message "New agenda file list installed"))
16673 (message "%s" (substitute-command-keys
16674 "Edit list and finish with \\[save-buffer]")))
16675 (customize-variable 'org-agenda-files)))
16677 (defun org-store-new-agenda-file-list (list)
16678 "Set new value for the agenda file list and save it correctly."
16679 (if (stringp org-agenda-files)
16680 (let ((fe (org-read-agenda-file-list t)) b u)
16681 (while (setq b (find-buffer-visiting org-agenda-files))
16683 (with-temp-file org-agenda-files
16686 (lambda (f) ;; Keep un-expanded entries.
16687 (if (setq u (assoc f fe))
16692 (let ((org-mode-hook nil) (org-inhibit-startup t)
16693 (org-insert-mode-line-in-empty-file nil))
16694 (setq org-agenda-files list)
16695 (customize-save-variable 'org-agenda-files org-agenda-files))))
16697 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16698 "Read the list of agenda files from a file.
16699 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16700 filenames, used by `org-store-new-agenda-file-list' to write back
16701 un-expanded file names."
16702 (when (file-directory-p org-agenda-files)
16703 (error "`org-agenda-files' cannot be a single directory"))
16704 (when (stringp org-agenda-files)
16706 (insert-file-contents org-agenda-files)
16709 (let ((e (expand-file-name (substitute-in-file-name f)
16711 (if pair-with-expansion
16714 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16717 (defun org-cycle-agenda-files ()
16718 "Cycle through the files in `org-agenda-files'.
16719 If the current buffer visits an agenda file, find the next one in the list.
16720 If the current buffer does not, find the first agenda file."
16722 (let* ((fs (org-agenda-files t))
16723 (files (append fs (list (car fs))))
16724 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16726 (unless files (error "No agenda files"))
16728 (while (setq file (pop files))
16729 (if (equal (file-truename file) tcf)
16731 (find-file (car files))
16733 (find-file (car fs)))
16734 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16736 (defun org-agenda-file-to-front (&optional to-end)
16737 "Move/add the current file to the top of the agenda file list.
16738 If the file is not present in the list, it is added to the front. If it is
16739 present, it is moved there. With optional argument TO-END, add/move to the
16742 (let ((org-agenda-skip-unavailable-files nil)
16743 (file-alist (mapcar (lambda (x)
16744 (cons (file-truename x) x))
16745 (org-agenda-files t)))
16746 (ctf (file-truename buffer-file-name))
16748 (setq x (assoc ctf file-alist) had x)
16750 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16752 (setq file-alist (append (delq x file-alist) (list x)))
16753 (setq file-alist (cons x (delq x file-alist))))
16754 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16755 (org-install-agenda-files-menu)
16756 (message "File %s to %s of agenda file list"
16757 (if had "moved" "added") (if to-end "end" "front"))))
16759 (defun org-remove-file (&optional file)
16760 "Remove current file from the list of files in variable `org-agenda-files'.
16761 These are the files which are being checked for agenda entries.
16762 Optional argument FILE means use this file instead of the current."
16764 (let* ((org-agenda-skip-unavailable-files nil)
16765 (file (or file buffer-file-name))
16766 (true-file (file-truename file))
16767 (afile (abbreviate-file-name file))
16768 (files (delq nil (mapcar
16770 (if (equal true-file
16773 (org-agenda-files t)))))
16774 (if (not (= (length files) (length (org-agenda-files t))))
16776 (org-store-new-agenda-file-list files)
16777 (org-install-agenda-files-menu)
16778 (message "Removed file: %s" afile))
16779 (message "File was not in list: %s (not removed)" afile))))
16781 (defun org-file-menu-entry (file)
16782 (vector file (list 'find-file file) t))
16784 (defun org-check-agenda-file (file)
16785 "Make sure FILE exists. If not, ask user what to do."
16786 (when (not (file-exists-p file))
16787 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
16788 (abbreviate-file-name file))
16789 (let ((r (downcase (read-char-exclusive))))
16792 (org-remove-file file)
16793 (throw 'nextfile t))
16794 (t (error "Abort"))))))
16796 (defun org-get-agenda-file-buffer (file)
16797 "Get a buffer visiting FILE. If the buffer needs to be created, add
16798 it to the list of buffers which might be released later."
16799 (let ((buf (org-find-base-buffer-visiting file)))
16801 buf ; just return it
16802 ;; Make a new buffer and remember it
16803 (setq buf (find-file-noselect file))
16804 (if buf (push buf org-agenda-new-buffers))
16807 (defun org-release-buffers (blist)
16808 "Release all buffers in list, asking the user for confirmation when needed.
16809 When a buffer is unmodified, it is just killed. When modified, it is saved
16810 \(if the user agrees) and then killed."
16812 (while (setq buf (pop blist))
16813 (setq file (buffer-file-name buf))
16814 (when (and (buffer-modified-p buf)
16816 (y-or-n-p (format "Save file %s? " file)))
16817 (with-current-buffer buf (save-buffer)))
16818 (kill-buffer buf))))
16820 (defun org-prepare-agenda-buffers (files)
16821 "Create buffers for all agenda files, protect archived trees and comments."
16823 (let ((pa '(:org-archived t))
16824 (pc '(:org-comment t))
16825 (pall '(:org-archived t :org-comment t))
16826 (inhibit-read-only t)
16827 (rea (concat ":" org-archive-tag ":"))
16831 (while (setq file (pop files))
16835 (org-check-agenda-file file)
16836 (set-buffer (org-get-agenda-file-buffer file)))
16838 (setq bmp (buffer-modified-p))
16839 (org-refresh-category-properties)
16840 (setq org-todo-keywords-for-agenda
16841 (append org-todo-keywords-for-agenda org-todo-keywords-1))
16842 (setq org-done-keywords-for-agenda
16843 (append org-done-keywords-for-agenda org-done-keywords))
16844 (setq org-todo-keyword-alist-for-agenda
16845 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
16846 (setq org-drawers-for-agenda
16847 (append org-drawers-for-agenda org-drawers))
16848 (setq org-tag-alist-for-agenda
16849 (append org-tag-alist-for-agenda org-tag-alist))
16852 (remove-text-properties (point-min) (point-max) pall)
16853 (when org-agenda-skip-archived-trees
16854 (goto-char (point-min))
16855 (while (re-search-forward rea nil t)
16856 (if (org-at-heading-p t)
16857 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
16858 (goto-char (point-min))
16859 (setq re (format org-heading-keyword-regexp-format
16860 org-comment-string))
16861 (while (re-search-forward re nil t)
16862 (add-text-properties
16863 (match-beginning 0) (org-end-of-subtree t) pc)))
16864 (set-buffer-modified-p bmp)))))
16865 (setq org-todo-keywords-for-agenda
16866 (org-uniquify org-todo-keywords-for-agenda))
16867 (setq org-todo-keyword-alist-for-agenda
16868 (org-uniquify org-todo-keyword-alist-for-agenda)
16869 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
16871 ;;;; Embedded LaTeX
16873 (defvar org-cdlatex-mode-map (make-sparse-keymap)
16874 "Keymap for the minor `org-cdlatex-mode'.")
16876 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
16877 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
16878 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
16879 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
16880 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
16882 (defvar org-cdlatex-texmathp-advice-is-done nil
16883 "Flag remembering if we have applied the advice to texmathp already.")
16885 (define-minor-mode org-cdlatex-mode
16886 "Toggle the minor `org-cdlatex-mode'.
16887 This mode supports entering LaTeX environment and math in LaTeX fragments
16889 \\{org-cdlatex-mode-map}"
16891 (when org-cdlatex-mode
16893 (run-hooks 'cdlatex-mode-hook)
16894 (cdlatex-compute-tables))
16895 (unless org-cdlatex-texmathp-advice-is-done
16896 (setq org-cdlatex-texmathp-advice-is-done t)
16897 (defadvice texmathp (around org-math-always-on activate)
16898 "Always return t in org-mode buffers.
16899 This is because we want to insert math symbols without dollars even outside
16900 the LaTeX math segments. If Orgmode thinks that point is actually inside
16901 an embedded LaTeX fragment, let texmathp do its job.
16902 \\[org-cdlatex-mode-map]"
16906 ((not (derived-mode-p 'org-mode)) ad-do-it)
16907 ((eq this-command 'cdlatex-math-symbol)
16908 (setq ad-return-value t
16909 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
16911 (let ((p (org-inside-LaTeX-fragment-p)))
16912 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
16913 (setq ad-return-value t
16914 texmathp-why '("Org-mode embedded math" . 0))
16915 (if p ad-do-it)))))))))
16917 (defun turn-on-org-cdlatex ()
16918 "Unconditionally turn on `org-cdlatex-mode'."
16919 (org-cdlatex-mode 1))
16921 (defun org-inside-LaTeX-fragment-p ()
16922 "Test if point is inside a LaTeX fragment.
16923 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
16924 sequence appearing also before point.
16925 Even though the matchers for math are configurable, this function assumes
16926 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
16927 delimiters are skipped when they have been removed by customization.
16928 The return value is nil, or a cons cell with the delimiter and the
16929 position of this delimiter.
16931 This function does a reasonably good job, but can locally be fooled by
16932 for example currency specifications. For example it will assume being in
16933 inline math after \"$22.34\". The LaTeX fragment formatter will only format
16934 fragments that are properly closed, but during editing, we have to live
16935 with the uncertainty caused by missing closing delimiters. This function
16936 looks only before point, not after."
16938 (let ((pos (point))
16939 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
16941 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
16943 dd-on str (start 0) m re)
16946 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
16947 re (nth 1 (assoc "$" org-latex-regexps)))
16948 (while (string-match re str start)
16950 ((= (match-end 0) (length str))
16951 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
16952 ((= (match-end 0) (- (length str) 5))
16954 (t (setq start (match-end 0))))))
16955 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
16957 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
16958 (and (match-beginning 2) (throw 'exit nil))
16960 (while (re-search-backward "\\$\\$" lim t)
16961 (setq dd-on (not dd-on)))
16963 (if dd-on (cons "$$" m))))))
16965 (defun org-inside-latex-macro-p ()
16966 "Is point inside a LaTeX macro or its arguments?"
16969 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
16971 (defun org-try-cdlatex-tab ()
16972 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
16973 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
16974 - inside a LaTeX fragment, or
16975 - after the first word in a line, where an abbreviation expansion could
16976 insert a LaTeX environment."
16977 (when org-cdlatex-mode
16979 ;; Before any word on the line: No expansion possible.
16980 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
16981 ;; Just after first word on the line: Expand it. Make sure it
16982 ;; cannot happen on headlines, though.
16984 (skip-chars-backward "a-zA-Z0-9*")
16985 (skip-chars-backward " \t")
16986 (and (bolp) (not (org-at-heading-p))))
16988 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
16990 (defun org-cdlatex-underscore-caret (&optional arg)
16991 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
16992 Revert to the normal definition outside of these fragments."
16994 (if (org-inside-LaTeX-fragment-p)
16995 (call-interactively 'cdlatex-sub-superscript)
16996 (let (org-cdlatex-mode)
16997 (call-interactively (key-binding (vector last-input-event))))))
16999 (defun org-cdlatex-math-modify (&optional arg)
17000 "Execute `cdlatex-math-modify' in LaTeX fragments.
17001 Revert to the normal definition outside of these fragments."
17003 (if (org-inside-LaTeX-fragment-p)
17004 (call-interactively 'cdlatex-math-modify)
17005 (let (org-cdlatex-mode)
17006 (call-interactively (key-binding (vector last-input-event))))))
17008 (defvar org-latex-fragment-image-overlays nil
17009 "List of overlays carrying the images of latex fragments.")
17010 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17012 (defun org-remove-latex-fragment-image-overlays ()
17013 "Remove all overlays with LaTeX fragment images in current buffer."
17014 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17015 (setq org-latex-fragment-image-overlays nil))
17017 (defun org-preview-latex-fragment (&optional subtree)
17018 "Preview the LaTeX fragment at point, or all locally or globally.
17019 If the cursor is in a LaTeX fragment, create the image and overlay
17020 it over the source code. If there is no fragment at point, display
17021 all fragments in the current text, from one headline to the next. With
17022 prefix SUBTREE, display all fragments in the current subtree. With a
17023 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17024 the cursor is before the first headline,
17025 display all fragments in the buffer.
17026 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17028 (unless buffer-file-name
17029 (error "Can't preview LaTeX fragment in a non-file buffer"))
17030 (org-remove-latex-fragment-image-overlays)
17033 (let (beg end at msg)
17035 ((or (equal subtree '(16))
17036 (not (save-excursion
17037 (re-search-backward org-outline-regexp-bol nil t))))
17038 (setq beg (point-min) end (point-max)
17039 msg "Creating images for buffer...%s"))
17040 ((equal subtree '(4))
17041 (org-back-to-heading)
17042 (setq beg (point) end (org-end-of-subtree t)
17043 msg "Creating images for subtree...%s"))
17045 (if (setq at (org-inside-LaTeX-fragment-p))
17046 (goto-char (max (point-min) (- (cdr at) 2)))
17047 (org-back-to-heading))
17048 (setq beg (point) end (progn (outline-next-heading) (point))
17049 msg (if at "Creating image...%s"
17050 "Creating images for entry...%s"))))
17052 (narrow-to-region beg end)
17055 (concat "ltxpng/" (file-name-sans-extension
17056 (file-name-nondirectory
17057 buffer-file-name)))
17058 default-directory 'overlays msg at 'forbuffer
17059 org-latex-create-formula-image-program)
17060 (message msg "done. Use `C-c C-c' to remove images.")))))
17062 (defvar org-latex-regexps
17063 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17064 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17065 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17066 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17067 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17068 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17069 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17070 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17071 "Regular expressions for matching embedded LaTeX.")
17073 (defvar org-export-have-math nil) ;; dynamic scoping
17074 (defun org-format-latex (prefix &optional dir overlays msg at
17075 forbuffer processing-type)
17076 "Replace LaTeX fragments with links to an image, and produce images.
17077 Some of the options can be changed using the variable
17078 `org-format-latex-options'."
17079 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17080 (let* ((prefixnodir (file-name-nondirectory prefix))
17081 (absprefix (expand-file-name prefix dir))
17082 (todir (file-name-directory absprefix))
17083 (opt org-format-latex-options)
17084 (matchers (plist-get opt :matchers))
17085 (re-list org-latex-regexps)
17086 (org-format-latex-header-extra
17087 (plist-get (org-infile-export-plist) :latex-header-extra))
17088 (cnt 0) txt hash link beg end re e checkdir
17089 executables-checked string
17090 m n block-type block linkfile movefile ov)
17091 ;; Check the different regular expressions
17092 (while (setq e (pop re-list))
17093 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17094 block (if block-type "\n\n" ""))
17095 (when (member m matchers)
17096 (goto-char (point-min))
17097 (while (re-search-forward re nil t)
17098 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17099 (not (get-text-property (match-beginning n)
17102 (not (eq (get-char-property (match-beginning n)
17104 'org-latex-overlay))))
17105 (setq org-export-have-math t)
17107 ((eq processing-type 'verbatim)
17108 ;; Leave the text verbatim, just protect it
17109 (add-text-properties (match-beginning n) (match-end n)
17110 '(org-protected t)))
17111 ((eq processing-type 'mathjax)
17112 ;; Prepare for MathJax processing
17113 (setq string (match-string n))
17114 (if (member m '("$" "$1"))
17116 (delete-region (match-beginning n) (match-end n))
17117 (goto-char (match-beginning n))
17118 (insert (org-add-props (concat "\\(" (substring string 1 -1)
17120 '(org-protected t))))
17121 (add-text-properties (match-beginning n) (match-end n)
17122 '(org-protected t))))
17123 ((or (eq processing-type 'dvipng)
17124 (eq processing-type 'imagemagick))
17125 ;; Process to an image
17126 (setq txt (match-string n)
17127 beg (match-beginning n) end (match-end n)
17129 (let (print-length print-level) ; make sure full list is printed
17130 (setq hash (sha1 (prin1-to-string
17131 (list org-format-latex-header
17132 org-format-latex-header-extra
17133 org-export-latex-default-packages-alist
17134 org-export-latex-packages-alist
17135 org-format-latex-options
17137 linkfile (format "%s_%s.png" prefix hash)
17138 movefile (format "%s_%s.png" absprefix hash)))
17139 (setq link (concat block "[[file:" linkfile "]]" block))
17140 (if msg (message msg cnt))
17142 (unless checkdir ; make sure the directory exists
17144 (or (file-directory-p todir) (make-directory todir t)))
17146 ((eq processing-type 'dvipng)
17147 (unless executables-checked
17148 (org-check-external-command
17149 "latex" "needed to convert LaTeX fragments to images")
17150 (org-check-external-command
17151 "dvipng" "needed to convert LaTeX fragments to images")
17152 (setq executables-checked t))
17153 (unless (file-exists-p movefile)
17154 (org-create-formula-image-with-dvipng
17155 txt movefile opt forbuffer)))
17156 ((eq processing-type 'imagemagick)
17157 (unless executables-checked
17158 (org-check-external-command
17159 "convert" "you need to install imagemagick")
17160 (setq executables-checked t))
17161 (unless (file-exists-p movefile)
17162 (org-create-formula-image-with-imagemagick
17163 txt movefile opt forbuffer))))
17167 (if (eq (overlay-get o 'org-overlay-type)
17168 'org-latex-overlay)
17169 (delete-overlay o)))
17170 (overlays-in beg end))
17171 (setq ov (make-overlay beg end))
17172 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17173 (if (featurep 'xemacs)
17175 (overlay-put ov 'invisible t)
17178 (make-glyph (vector 'png :file movefile))))
17181 (list 'image :type 'png :file movefile :ascent 'center)))
17182 (push ov org-latex-fragment-image-overlays)
17184 (delete-region beg end)
17185 (insert (org-add-props link
17186 (list 'org-latex-src
17187 (replace-regexp-in-string
17189 'org-latex-src-embed-type
17190 (if block-type 'paragraph 'character))))))
17191 ((eq processing-type 'mathml)
17192 ;; Process to MathML
17193 (unless executables-checked
17194 (unless (save-match-data (org-format-latex-mathml-available-p))
17195 (error "LaTeX to MathML converter not configured"))
17196 (setq executables-checked t))
17197 (setq txt (match-string n)
17198 beg (match-beginning n) end (match-end n)
17200 (if msg (message msg cnt))
17202 (delete-region beg end)
17203 (insert (org-format-latex-as-mathml
17204 txt block-type prefix dir)))
17206 (error "Unknown conversion type %s for latex fragments"
17207 processing-type)))))))))
17209 (defun org-create-math-formula (latex-frag &optional mathml-file)
17210 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17211 Use `org-latex-to-mathml-convert-command'. If the conversion is
17212 sucessful, return the portion between \"<math...> </math>\"
17213 elements otherwise return nil. When MATHML-FILE is specified,
17214 write the results in to that file. When invoked as an
17215 interactive command, prompt for LATEX-FRAG, with initial value
17216 set to the current active region and echo the results for user
17218 (interactive (list (let ((frag (when (region-active-p)
17219 (buffer-substring-no-properties
17220 (region-beginning) (region-end)))))
17221 (read-string "LaTeX Fragment: " frag nil frag))))
17222 (unless latex-frag (error "Invalid latex-frag"))
17223 (let* ((tmp-in-file (file-relative-name
17224 (make-temp-name (expand-file-name "ltxmathml-in"))))
17225 (ignore (write-region latex-frag nil tmp-in-file))
17226 (tmp-out-file (file-relative-name
17227 (make-temp-name (expand-file-name "ltxmathml-out"))))
17229 org-latex-to-mathml-convert-command
17230 `((?j . ,(shell-quote-argument
17231 (expand-file-name org-latex-to-mathml-jar-file)))
17232 (?I . ,(shell-quote-argument tmp-in-file))
17233 (?o . ,(shell-quote-argument tmp-out-file)))))
17234 mathml shell-command-output)
17235 (when (org-called-interactively-p 'any)
17236 (unless (org-format-latex-mathml-available-p)
17237 (error "LaTeX to MathML converter not configured")))
17238 (message "Running %s" cmd)
17239 (setq shell-command-output (shell-command-to-string cmd))
17241 (when (file-readable-p tmp-out-file)
17242 (with-current-buffer (find-file-noselect tmp-out-file t)
17243 (goto-char (point-min))
17244 (when (re-search-forward
17247 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17249 (regexp-quote "</math>")) nil t)
17250 (prog1 (match-string 0) (kill-buffer))))))
17254 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17256 (write-region mathml nil mathml-file))
17257 (when (org-called-interactively-p 'any)
17259 ((message "LaTeX to MathML conversion failed")
17260 (message shell-command-output)))
17261 (delete-file tmp-in-file)
17262 (when (file-exists-p tmp-out-file)
17263 (delete-file tmp-out-file))
17266 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17267 prefix &optional dir)
17268 "Use `org-create-math-formula' but check local cache first."
17269 (let* ((absprefix (expand-file-name prefix dir))
17270 (print-length nil) (print-level nil)
17271 (formula-id (concat
17276 org-latex-to-mathml-convert-command)))))
17277 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17278 (formula-cache-dir (file-name-directory formula-cache)))
17280 (unless (file-directory-p formula-cache-dir)
17281 (make-directory formula-cache-dir t))
17283 (unless (file-exists-p formula-cache)
17284 (org-create-math-formula latex-frag formula-cache))
17286 (if (file-exists-p formula-cache)
17287 ;; Successful conversion. Return the link to MathML file.
17289 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17290 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17291 'org-latex-src-embed-type (if latex-frag-type
17292 'paragraph 'character)))
17293 ;; Failed conversion. Return the LaTeX fragment verbatim
17294 (add-text-properties
17295 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17298 ;; This function borrows from Ganesh Swami's latex2png.el
17299 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
17300 "This calls dvipng."
17301 (require 'org-latex)
17302 (let* ((tmpdir (if (featurep 'xemacs)
17304 temporary-file-directory))
17305 (texfilebase (make-temp-name
17306 (expand-file-name "orgtex" tmpdir)))
17307 (texfile (concat texfilebase ".tex"))
17308 (dvifile (concat texfilebase ".dvi"))
17309 (pngfile (concat texfilebase ".png"))
17310 (fnh (if (featurep 'xemacs)
17311 (font-height (face-font 'default))
17312 (face-attribute 'default :height nil)))
17313 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17314 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17315 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17317 (bg (or (plist-get options (if buffer :background :html-background))
17319 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17320 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17321 (with-temp-file texfile
17322 (insert (org-splice-latex-header
17323 org-format-latex-header
17324 org-export-latex-default-packages-alist
17325 org-export-latex-packages-alist t
17326 org-format-latex-header-extra))
17327 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17328 (require 'org-latex)
17329 (org-export-latex-fix-inputenc))
17330 (let ((dir default-directory))
17331 (condition-case nil
17334 (call-process "latex" nil nil nil texfile))
17337 (if (not (file-exists-p dvifile))
17338 (progn (message "Failed to create dvi file from %s" texfile) nil)
17339 (condition-case nil
17340 (if (featurep 'xemacs)
17341 (call-process "dvipng" nil nil nil
17346 (call-process "dvipng" nil nil nil
17349 ;;"-x" scale "-y" scale
17354 (if (not (file-exists-p pngfile))
17355 (if org-format-latex-signal-error
17356 (error "Failed to create png file from %s" texfile)
17357 (message "Failed to create png file from %s" texfile)
17359 ;; Use the requested file name and clean up
17360 (copy-file pngfile tofile 'replace)
17361 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
17362 (if (file-exists-p (concat texfilebase e))
17363 (delete-file (concat texfilebase e))))
17366 (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
17367 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
17368 "This calls convert, which is included into imagemagick."
17369 (require 'org-latex)
17370 (let* ((tmpdir (if (featurep 'xemacs)
17372 temporary-file-directory))
17373 (texfilebase (make-temp-name
17374 (expand-file-name "orgtex" tmpdir)))
17375 (texfile (concat texfilebase ".tex"))
17376 (pdffile (concat texfilebase ".pdf"))
17377 (pngfile (concat texfilebase ".png"))
17378 (fnh (if (featurep 'xemacs)
17379 (font-height (face-font 'default))
17380 (face-attribute 'default :height nil)))
17381 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17382 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17383 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17385 (bg (or (plist-get options (if buffer :background :html-background))
17387 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
17388 (setq fg (org-latex-color-format fg)))
17389 (if (eq bg 'default) (setq bg (org-latex-color :background))
17390 (setq bg (org-latex-color-format
17391 (if (string= bg "Transparent")(setq bg "white")))))
17392 (with-temp-file texfile
17393 (insert (org-splice-latex-header
17394 org-format-latex-header
17395 org-export-latex-default-packages-alist
17396 org-export-latex-packages-alist t
17397 org-format-latex-header-extra))
17398 (insert "\n\\begin{document}\n"
17399 "\\definecolor{fg}{rgb}{" fg "}\n"
17400 "\\definecolor{bg}{rgb}{" bg "}\n"
17401 "\n\\pagecolor{bg}\n"
17405 "\n\\end{document}\n" )
17406 (require 'org-latex)
17407 (org-export-latex-fix-inputenc))
17408 (let ((dir default-directory) cmd cmds latex-frags-cmds)
17409 (condition-case nil
17412 (setq cmds org-latex-to-pdf-process)
17414 (setq latex-frags-cmds (pop cmds))
17415 (if (listp latex-frags-cmds)
17417 (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
17418 (while latex-frags-cmds
17419 (setq cmd (pop latex-frags-cmds))
17420 (while (string-match "%b" cmd)
17421 (setq cmd (replace-match
17423 (shell-quote-argument texfile))
17425 (while (string-match "%f" cmd)
17426 (setq cmd (replace-match
17428 (shell-quote-argument (file-name-nondirectory texfile)))
17430 (while (string-match "%o" cmd)
17431 (setq cmd (replace-match
17433 (shell-quote-argument (file-name-directory texfile)))
17435 (setq cmd (split-string cmd))
17436 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
17439 (if (not (file-exists-p pdffile))
17440 (progn (message "Failed to create pdf file from %s" texfile) nil)
17441 (condition-case nil
17442 (if (featurep 'xemacs)
17443 (call-process "convert" nil nil nil
17449 ;; "-sharpen" "0x1.0"
17451 (call-process "convert" nil nil nil
17457 ; "-sharpen" "0x1.0"
17460 (if (not (file-exists-p pngfile))
17461 (if org-format-latex-signal-error
17462 (error "Failed to create png file from %s" texfile)
17463 (message "Failed to create png file from %s" texfile)
17465 ;; Use the requested file name and clean up
17466 (copy-file pngfile tofile 'replace)
17467 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
17468 (if (file-exists-p (concat texfilebase e))
17469 (delete-file (concat texfilebase e))))
17472 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17473 "Fill a LaTeX header template TPL.
17474 In the template, the following place holders will be recognized:
17476 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17477 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17478 [PACKAGES] \\usepackage statements for PKG
17479 [NO-PACKAGES] do not include PKG
17480 [EXTRA] the string EXTRA
17481 [NO-EXTRA] do not include EXTRA
17483 For backward compatibility, if both the positive and the negative place
17484 holder is missing, the positive one (without the \"NO-\") will be
17485 assumed to be present at the end of the template.
17486 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17488 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17489 (let (rpl (end ""))
17490 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17491 (setq rpl (if (or (match-end 1) (not def-pkg))
17492 "" (org-latex-packages-to-string def-pkg snippets-p t))
17493 tpl (replace-match rpl t t tpl))
17494 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17496 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17497 (setq rpl (if (or (match-end 1) (not pkg))
17498 "" (org-latex-packages-to-string pkg snippets-p t))
17499 tpl (replace-match rpl t t tpl))
17502 (org-latex-packages-to-string pkg snippets-p)))))
17504 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17505 (setq rpl (if (or (match-end 1) (not extra))
17506 "" (concat extra "\n"))
17507 tpl (replace-match rpl t t tpl))
17508 (if (and extra (string-match "\\S-" extra))
17509 (setq end (concat end "\n" extra))))
17511 (if (string-match "\\S-" end)
17512 (concat tpl "\n" end)
17515 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17516 "Turn an alist of packages into a string with the \\usepackage macros."
17517 (setq pkg (mapconcat (lambda(p)
17520 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17521 (format "%% Package %s omitted" (cadr p)))
17522 ((equal "" (car p))
17523 (format "\\usepackage{%s}" (cadr p)))
17525 (format "\\usepackage[%s]{%s}"
17526 (car p) (cadr p)))))
17529 (if newline (concat pkg "\n") pkg))
17531 (defun org-dvipng-color (attr)
17532 "Return a RGB color specification for dvipng."
17533 (apply 'format "rgb %s %s %s"
17534 (mapcar 'org-normalize-color
17535 (if (featurep 'xemacs)
17536 (color-rgb-components
17537 (face-property 'default
17538 (cond ((eq attr :foreground) 'foreground)
17539 ((eq attr :background) 'background))))
17540 (color-values (face-attribute 'default attr nil))))))
17542 (defun org-latex-color (attr)
17543 "Return a RGB color for the LaTeX color package."
17544 (apply 'format "%s,%s,%s"
17545 (mapcar 'org-normalize-color
17546 (if (featurep 'xemacs)
17547 (color-rgb-components
17548 (face-property 'default
17549 (cond ((eq attr :foreground) 'foreground)
17550 ((eq attr :background) 'background))))
17551 (color-values (face-attribute 'default attr nil))))))
17553 (defun org-latex-color-format (color-name)
17554 "Convert COLOR-NAME to a RGB color value."
17555 (apply 'format "%s,%s,%s"
17556 (mapcar 'org-normalize-color
17557 (color-values color-name))))
17559 (defun org-normalize-color (value)
17560 "Return string to be used as color value for an RGB component."
17561 (format "%g" (/ value 65535.0)))
17566 (defvar org-inline-image-overlays nil)
17567 (make-variable-buffer-local 'org-inline-image-overlays)
17569 (defun org-toggle-inline-images (&optional include-linked)
17570 "Toggle the display of inline images.
17571 INCLUDE-LINKED is passed to `org-display-inline-images'."
17573 (if org-inline-image-overlays
17575 (org-remove-inline-images)
17576 (message "Inline image display turned off"))
17577 (org-display-inline-images include-linked)
17578 (if org-inline-image-overlays
17579 (message "%d images displayed inline"
17580 (length org-inline-image-overlays))
17581 (message "No images to display inline"))))
17583 (defun org-display-inline-images (&optional include-linked refresh beg end)
17584 "Display inline images.
17585 Normally only links without a description part are inlined, because this
17586 is how it will work for export. When INCLUDE-LINKED is set, also links
17587 with a description part will be inlined. This can be nice for a quick
17588 look at those images, but it does not reflect what exported files will look
17590 When REFRESH is set, refresh existing images between BEG and END.
17591 This will create new image displays only if necessary.
17592 BEG and END default to the buffer boundaries."
17595 (org-remove-inline-images)
17596 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17600 (setq beg (or beg (point-min)) end (or end (point-max)))
17602 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17603 (substring (org-image-file-name-regexp) 0 -2)
17604 "\\)\\]" (if include-linked "" "\\]")))
17606 (while (re-search-forward re end t)
17607 (setq old (get-char-property-and-overlay (match-beginning 1)
17608 'org-image-overlay))
17609 (setq file (expand-file-name
17610 (concat (or (match-string 3) "") (match-string 4))))
17611 (when (file-exists-p file)
17612 (if (and (car-safe old) refresh)
17613 (image-refresh (overlay-get (cdr old) 'display))
17614 (setq img (save-match-data (create-image file)))
17616 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17617 (overlay-put ov 'display img)
17618 (overlay-put ov 'face 'default)
17619 (overlay-put ov 'org-image-overlay t)
17620 (overlay-put ov 'modification-hooks
17621 (list 'org-display-inline-modification-hook))
17622 (push ov org-inline-image-overlays)))))))))
17624 (defun org-display-inline-modification-hook (ov after beg end &optional len)
17625 "Remove inline-display overlay if a corresponding region is modified."
17626 (let ((inhibit-modification-hooks t))
17627 (when (and ov after)
17628 (delete ov org-inline-image-overlays)
17629 (delete-overlay ov))))
17631 (defun org-remove-inline-images ()
17632 "Remove inline display of images."
17634 (mapc 'delete-overlay org-inline-image-overlays)
17635 (setq org-inline-image-overlays nil))
17639 ;; Outline functions from `outline-mode-prefix-map'
17640 ;; that can be remapped in Org:
17641 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17642 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17643 (define-key org-mode-map [remap outline-forward-same-level]
17644 'org-forward-same-level)
17645 (define-key org-mode-map [remap outline-backward-same-level]
17646 'org-backward-same-level)
17647 (define-key org-mode-map [remap show-branches]
17648 'org-kill-note-or-show-branches)
17649 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17650 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17651 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17653 ;; Outline functions from `outline-mode-prefix-map' that can not
17654 ;; be remapped in Org:
17656 ;; - the column "key binding" shows whether the Outline function is still
17657 ;; available in Org mode on the same key that it has been bound to in
17659 ;; - "overridden": key used for a different functionality in Org mode
17660 ;; - else: key still bound to the same Outline function in Org mode
17662 ;; | Outline function | key binding | Org replacement |
17663 ;; |------------------------------------+-------------+-----------------------|
17664 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17665 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17666 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17667 ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
17668 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17669 ;; | `show-entry' | overridden | no replacement |
17670 ;; | `show-children' | `C-c C-i' | visibility cycling |
17671 ;; | `show-branches' | `C-c C-k' | still same function |
17672 ;; | `show-subtree' | overridden | visibility cycling |
17673 ;; | `show-all' | overridden | no replacement |
17674 ;; | `hide-subtree' | overridden | visibility cycling |
17675 ;; | `hide-body' | overridden | no replacement |
17676 ;; | `hide-entry' | overridden | visibility cycling |
17677 ;; | `hide-leaves' | overridden | no replacement |
17678 ;; | `hide-sublevels' | overridden | no replacement |
17679 ;; | `hide-other' | overridden | no replacement |
17681 ;; Make `C-c C-x' a prefix key
17682 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17684 ;; TAB key with modifiers
17685 (org-defkey org-mode-map "\C-i" 'org-cycle)
17686 (org-defkey org-mode-map [(tab)] 'org-cycle)
17687 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17688 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17689 ;; The following line is necessary under Suse GNU/Linux
17690 (unless (featurep 'xemacs)
17691 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17692 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17693 (define-key org-mode-map [backtab] 'org-shifttab)
17695 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17696 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17697 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17699 ;; Cursor keys with modifiers
17700 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17701 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17702 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17703 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17705 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17706 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17707 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17708 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17710 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17711 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17712 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17713 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17715 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17716 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17717 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17718 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17721 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17722 (mapc (lambda (pair)
17723 (define-key org-babel-map (car pair) (cdr pair)))
17724 org-babel-key-bindings)
17726 ;;; Extra keys for tty access.
17727 ;; We only set them when really needed because otherwise the
17728 ;; menus don't show the simple keys
17730 (when (or org-use-extra-keys
17731 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17732 (not window-system))
17733 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17734 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17735 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17736 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17737 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17738 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17739 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17740 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17741 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17742 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17743 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17744 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17745 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17746 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17747 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17748 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17749 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17750 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17751 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17752 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17753 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17754 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17755 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17756 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17757 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17758 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17759 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17760 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17762 ;; All the other keys
17764 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17765 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17766 (if (boundp 'narrow-map)
17767 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17768 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17769 (if (boundp 'narrow-map)
17770 (org-defkey narrow-map "b" 'org-narrow-to-block)
17771 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17772 (if (boundp 'narrow-map)
17773 (org-defkey narrow-map "e" 'org-narrow-to-element)
17774 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
17775 (org-defkey org-mode-map "\C-\M-t" 'org-element-transpose)
17776 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
17777 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
17778 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17779 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17780 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17781 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
17782 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17783 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17784 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17785 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17786 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17787 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17788 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
17789 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
17790 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
17791 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
17792 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
17793 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
17794 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
17795 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
17796 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
17797 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
17798 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
17799 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
17800 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
17801 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
17802 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
17803 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
17804 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
17805 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
17806 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
17807 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
17808 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
17809 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
17810 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
17811 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
17812 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
17813 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
17814 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
17815 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
17816 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
17817 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
17818 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
17819 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17820 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
17821 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
17822 (org-defkey org-mode-map "\C-c^" 'org-sort)
17823 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
17824 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
17825 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
17826 (org-defkey org-mode-map "\C-m" 'org-return)
17827 (org-defkey org-mode-map "\C-j" 'org-return-indent)
17828 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
17829 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
17830 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
17831 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
17832 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
17833 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
17834 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
17835 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
17836 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
17837 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
17838 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
17839 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
17840 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
17841 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
17842 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
17843 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
17844 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
17845 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
17846 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
17847 (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
17848 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
17849 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
17851 (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
17852 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
17853 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
17854 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
17856 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
17857 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
17858 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
17859 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
17860 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
17861 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
17862 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
17863 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
17864 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
17865 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
17866 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
17867 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
17868 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
17869 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
17870 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
17871 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
17872 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
17873 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
17874 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
17875 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
17876 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
17878 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
17879 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
17880 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
17881 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
17882 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
17884 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
17886 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
17888 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
17889 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
17891 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
17894 (when (featurep 'xemacs)
17895 (org-defkey org-mode-map 'button3 'popup-mode-menu))
17898 (defconst org-speed-commands-default
17900 ("Outline Navigation")
17901 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
17902 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
17903 ("f" . (org-speed-move-safe 'org-forward-same-level))
17904 ("b" . (org-speed-move-safe 'org-backward-same-level))
17905 ("u" . (org-speed-move-safe 'outline-up-heading))
17907 ("g" . (org-refile t))
17908 ("Outline Visibility")
17910 ("C" . org-shifttab)
17911 (" " . org-display-outline-path)
17912 (":" . org-columns)
17913 ("Outline Structure Editing")
17914 ("U" . org-shiftmetaup)
17915 ("D" . org-shiftmetadown)
17916 ("r" . org-metaright)
17917 ("l" . org-metaleft)
17918 ("R" . org-shiftmetaright)
17919 ("L" . org-shiftmetaleft)
17920 ("i" . (progn (forward-char 1) (call-interactively
17921 'org-insert-heading-respect-content)))
17924 ("a" . org-archive-subtree-default-with-confirmation)
17925 ("." . org-mark-subtree)
17926 ("#" . org-toggle-comment)
17928 ("I" . org-clock-in)
17929 ("O" . org-clock-out)
17930 ("Meta Data Editing")
17932 ("0" . (org-priority ?\ ))
17933 ("1" . (org-priority ?A))
17934 ("2" . (org-priority ?B))
17935 ("3" . (org-priority ?C))
17936 (";" . org-set-tags-command)
17937 ("e" . org-set-effort)
17938 ("E" . org-inc-effort)
17939 ("Agenda Views etc")
17941 ("/" . org-sparse-tree)
17943 ("o" . org-open-at-point)
17944 ("?" . org-speed-command-help)
17945 ("<" . (org-agenda-set-restriction-lock 'subtree))
17946 (">" . (org-agenda-remove-restriction-lock))
17948 "The default speed commands.")
17950 (defun org-print-speed-command (e)
17951 (if (> (length (car e)) 1)
17956 (princ (make-string (length (car e)) ?-))
17960 (if (symbolp (cdr e))
17961 (princ (symbol-name (cdr e)))
17965 (defun org-speed-command-help ()
17966 "Show the available speed commands."
17968 (if (not org-use-speed-commands)
17969 (error "Speed commands are not activated, customize `org-use-speed-commands'")
17970 (with-output-to-temp-buffer "*Help*"
17971 (princ "User-defined Speed commands\n===========================\n")
17972 (mapc 'org-print-speed-command org-speed-commands-user)
17974 (princ "Built-in Speed commands\n=======================\n")
17975 (mapc 'org-print-speed-command org-speed-commands-default))
17976 (with-current-buffer "*Help*"
17977 (setq truncate-lines t))))
17979 (defun org-speed-move-safe (cmd)
17980 "Execute CMD, but make sure that the cursor always ends up in a headline.
17981 If not, return to the original position and throw an error."
17983 (let ((pos (point)))
17984 (call-interactively cmd)
17985 (unless (and (bolp) (org-at-heading-p))
17987 (error "Boundary reached while executing %s" cmd))))
17989 (defvar org-self-insert-command-undo-counter 0)
17991 (defvar org-table-auto-blank-field) ; defined in org-table.el
17992 (defvar org-speed-command nil)
17994 (defun org-speed-command-default-hook (keys)
17995 "Hook for activating single-letter speed commands.
17996 `org-speed-commands-default' specifies a minimal command set.
17997 Use `org-speed-commands-user' for further customization."
17998 (when (or (and (bolp) (looking-at org-outline-regexp))
17999 (and (functionp org-use-speed-commands)
18000 (funcall org-use-speed-commands)))
18001 (cdr (assoc keys (append org-speed-commands-user
18002 org-speed-commands-default)))))
18004 (defun org-babel-speed-command-hook (keys)
18005 "Hook for activating single-letter code block commands."
18006 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18007 (cdr (assoc keys org-babel-key-bindings))))
18009 (defcustom org-speed-command-hook
18010 '(org-speed-command-default-hook org-babel-speed-command-hook)
18011 "Hook for activating speed commands at strategic locations.
18012 Hook functions are called in sequence until a valid handler is
18015 Each hook takes a single argument, a user-pressed command key
18016 which is also a `self-insert-command' from the global map.
18018 Within the hook, examine the cursor position and the command key
18019 and return nil or a valid handler as appropriate. Handler could
18020 be one of an interactive command, a function, or a form.
18022 Set `org-use-speed-commands' to non-nil value to enable this
18023 hook. The default setting is `org-speed-command-default-hook'."
18024 :group 'org-structure
18028 (defun org-self-insert-command (N)
18029 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18030 If the cursor is in a table looking at whitespace, the whitespace is
18031 overwritten, and the table is not marked as requiring realignment."
18033 (org-check-before-invisible-edit 'insert)
18035 ((and org-use-speed-commands
18036 (setq org-speed-command
18037 (run-hook-with-args-until-success
18038 'org-speed-command-hook (this-command-keys))))
18040 ((commandp org-speed-command)
18041 (setq this-command org-speed-command)
18042 (call-interactively org-speed-command))
18043 ((functionp org-speed-command)
18044 (funcall org-speed-command))
18045 ((and org-speed-command (listp org-speed-command))
18046 (eval org-speed-command))
18047 (t (let (org-use-speed-commands)
18048 (call-interactively 'org-self-insert-command)))))
18052 ;; check if we blank the field, and if that triggers align
18053 (and (featurep 'org-table) org-table-auto-blank-field
18054 (member last-command
18055 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18056 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18057 ;; got extra space, this field does not determine column width
18058 (let (org-table-may-need-update) (org-table-blank-field))
18059 ;; no extra space, this field may determine column width
18060 (org-table-blank-field)))
18063 (looking-at "[^|\n]* |"))
18064 (let (org-table-may-need-update)
18065 (goto-char (1- (match-end 0)))
18066 (backward-delete-char 1)
18067 (goto-char (match-beginning 0))
18068 (self-insert-command N)))
18070 (setq org-table-may-need-update t)
18071 (self-insert-command N)
18072 (org-fix-tags-on-the-fly)
18073 (if org-self-insert-cluster-for-undo
18074 (if (not (eq last-command 'org-self-insert-command))
18075 (setq org-self-insert-command-undo-counter 1)
18076 (if (>= org-self-insert-command-undo-counter 20)
18077 (setq org-self-insert-command-undo-counter 1)
18078 (and (> org-self-insert-command-undo-counter 0)
18079 buffer-undo-list (listp buffer-undo-list)
18080 (not (cadr buffer-undo-list)) ; remove nil entry
18081 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18082 (setq org-self-insert-command-undo-counter
18083 (1+ org-self-insert-command-undo-counter))))))))
18085 (defun org-check-before-invisible-edit (kind)
18086 "Check is editing if kind KIND would be dangerous with invisible text around.
18087 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18088 ;; First, try to get out of here as quickly as possible, to reduce overhead
18089 (if (and org-catch-invisible-edits
18090 (or (not (boundp 'visible-mode)) (not visible-mode))
18091 (or (get-char-property (point) 'invisible)
18092 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18093 ;; OK, we need to take a closer look
18094 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18095 (invisible-before-point (if (bobp) nil (get-char-property
18096 (1- (point)) 'invisible)))
18097 (border-and-ok-direction
18099 ;; Check if we are acting predictably before invisible text
18100 (and invisible-at-point (not invisible-before-point)
18101 (memq kind '(insert delete-backward)))
18102 ;; Check if we are acting predictably after invisible text
18103 ;; This works not well, and I have turned it off. It seems
18104 ;; better to always show and stop after invisible text.
18105 ;; (and (not invisible-at-point) invisible-before-point
18106 ;; (memq kind '(insert delete)))
18109 (when (or (memq invisible-at-point '(outline org-hide-block))
18110 (memq invisible-before-point '(outline org-hide-block)))
18111 (if (eq org-catch-invisible-edits 'error)
18112 (error "Editing in invisible areas is prohibited - make visible first"))
18113 ;; Make the area visible
18115 (if invisible-before-point
18116 (goto-char (previous-single-char-property-change
18117 (point) 'invisible)))
18120 ((eq org-catch-invisible-edits 'show)
18121 ;; That's it, we do the edit after showing
18123 "Unfolding invisible region around point before editing")
18125 ((and (eq org-catch-invisible-edits 'smart)
18126 border-and-ok-direction)
18127 (message "Unfolding invisible region around point before editing"))
18129 ;; Don't do the edit, make the user repeat it in full visibility
18130 (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
18132 (defun org-fix-tags-on-the-fly ()
18133 (when (and (equal (char-after (point-at-bol)) ?*)
18134 (org-at-heading-p))
18135 (org-align-tags-here org-tags-column)))
18137 (defun org-delete-backward-char (N)
18138 "Like `delete-backward-char', insert whitespace at field end in tables.
18139 When deleting backwards, in tables this function will insert whitespace in
18140 front of the next \"|\" separator, to keep the table aligned. The table will
18141 still be marked for re-alignment if the field did fill the entire column,
18142 because, in this case the deletion might narrow the column."
18144 (org-check-before-invisible-edit 'delete-backward)
18145 (if (and (org-table-p)
18147 (string-match "|" (buffer-substring (point-at-bol) (point)))
18148 (looking-at ".*?|"))
18149 (let ((pos (point))
18150 (noalign (looking-at "[^|\n\r]* |"))
18151 (c org-table-may-need-update))
18152 (backward-delete-char N)
18153 (if (not overwrite-mode)
18155 (skip-chars-forward "^|")
18157 (goto-char (1- pos))))
18158 ;; noalign: if there were two spaces at the end, this field
18159 ;; does not determine the width of the column.
18160 (if noalign (setq org-table-may-need-update c)))
18161 (backward-delete-char N)
18162 (org-fix-tags-on-the-fly)))
18164 (defun org-delete-char (N)
18165 "Like `delete-char', but insert whitespace at field end in tables.
18166 When deleting characters, in tables this function will insert whitespace in
18167 front of the next \"|\" separator, to keep the table aligned. The table will
18168 still be marked for re-alignment if the field did fill the entire column,
18169 because, in this case the deletion might narrow the column."
18171 (org-check-before-invisible-edit 'delete)
18172 (if (and (org-table-p)
18174 (not (= (char-after) ?|))
18176 (if (looking-at ".*?|")
18177 (let ((pos (point))
18178 (noalign (looking-at "[^|\n\r]* |"))
18179 (c org-table-may-need-update))
18180 (replace-match (concat
18181 (substring (match-string 0) 1 -1)
18184 ;; noalign: if there were two spaces at the end, this field
18185 ;; does not determine the width of the column.
18186 (if noalign (setq org-table-may-need-update c)))
18189 (org-fix-tags-on-the-fly)))
18191 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
18192 (put 'org-self-insert-command 'delete-selection t)
18193 (put 'orgtbl-self-insert-command 'delete-selection t)
18194 (put 'org-delete-char 'delete-selection 'supersede)
18195 (put 'org-delete-backward-char 'delete-selection 'supersede)
18196 (put 'org-yank 'delete-selection 'yank)
18198 ;; Make `flyspell-mode' delay after some commands
18199 (put 'org-self-insert-command 'flyspell-delayed t)
18200 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
18201 (put 'org-delete-char 'flyspell-delayed t)
18202 (put 'org-delete-backward-char 'flyspell-delayed t)
18204 ;; Make pabbrev-mode expand after org-mode commands
18205 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
18206 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
18208 ;; How to do this: Measure non-white length of current string
18209 ;; If equal to column width, we should realign.
18211 (defun org-remap (map &rest commands)
18212 "In MAP, remap the functions given in COMMANDS.
18213 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18216 (setq old (pop commands) new (pop commands))
18217 (if (fboundp 'command-remapping)
18218 (org-defkey map (vector 'remap old) new)
18219 (substitute-key-definition old new map global-map)))))
18221 (when (eq org-enable-table-editor 'optimized)
18222 ;; If the user wants maximum table support, we need to hijack
18223 ;; some standard editing functions
18224 (org-remap org-mode-map
18225 'self-insert-command 'org-self-insert-command
18226 'delete-char 'org-delete-char
18227 'delete-backward-char 'org-delete-backward-char)
18228 (org-defkey org-mode-map "|" 'org-force-self-insert))
18230 (defvar org-ctrl-c-ctrl-c-hook nil
18231 "Hook for functions attaching themselves to `C-c C-c'.
18233 This can be used to add additional functionality to the C-c C-c
18234 key which executes context-dependent commands. This hook is run
18235 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
18236 run after the last test.
18238 Each function will be called with no arguments. The function
18239 must check if the context is appropriate for it to act. If yes,
18240 it should do its thing and then return a non-nil value. If the
18241 context is wrong, just do nothing and return nil.")
18243 (defvar org-ctrl-c-ctrl-c-final-hook nil
18244 "Hook for functions attaching themselves to `C-c C-c'.
18246 This can be used to add additional functionality to the C-c C-c
18247 key which executes context-dependent commands. This hook is run
18248 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
18249 before the first test.
18251 Each function will be called with no arguments. The function
18252 must check if the context is appropriate for it to act. If yes,
18253 it should do its thing and then return a non-nil value. If the
18254 context is wrong, just do nothing and return nil.")
18256 (defvar org-tab-first-hook nil
18257 "Hook for functions to attach themselves to TAB.
18258 See `org-ctrl-c-ctrl-c-hook' for more information.
18259 This hook runs as the first action when TAB is pressed, even before
18260 `org-cycle' messes around with the `outline-regexp' to cater for
18261 inline tasks and plain list item folding.
18262 If any function in this hook returns t, any other actions that
18263 would have been caused by TAB (such as table field motion or visibility
18264 cycling) will not occur.")
18266 (defvar org-tab-after-check-for-table-hook nil
18267 "Hook for functions to attach themselves to TAB.
18268 See `org-ctrl-c-ctrl-c-hook' for more information.
18269 This hook runs after it has been established that the cursor is not in a
18270 table, but before checking if the cursor is in a headline or if global cycling
18272 If any function in this hook returns t, not other actions like visibility
18273 cycling will be done.")
18275 (defvar org-tab-after-check-for-cycling-hook nil
18276 "Hook for functions to attach themselves to TAB.
18277 See `org-ctrl-c-ctrl-c-hook' for more information.
18278 This hook runs after it has been established that not table field motion and
18279 not visibility should be done because of current context. This is probably
18280 the place where a package like yasnippets can hook in.")
18282 (defvar org-tab-before-tab-emulation-hook nil
18283 "Hook for functions to attach themselves to TAB.
18284 See `org-ctrl-c-ctrl-c-hook' for more information.
18285 This hook runs after every other options for TAB have been exhausted, but
18286 before indentation and \t insertion takes place.")
18288 (defvar org-metaleft-hook nil
18289 "Hook for functions attaching themselves to `M-left'.
18290 See `org-ctrl-c-ctrl-c-hook' for more information.")
18291 (defvar org-metaright-hook nil
18292 "Hook for functions attaching themselves to `M-right'.
18293 See `org-ctrl-c-ctrl-c-hook' for more information.")
18294 (defvar org-metaup-hook nil
18295 "Hook for functions attaching themselves to `M-up'.
18296 See `org-ctrl-c-ctrl-c-hook' for more information.")
18297 (defvar org-metadown-hook nil
18298 "Hook for functions attaching themselves to `M-down'.
18299 See `org-ctrl-c-ctrl-c-hook' for more information.")
18300 (defvar org-shiftmetaleft-hook nil
18301 "Hook for functions attaching themselves to `M-S-left'.
18302 See `org-ctrl-c-ctrl-c-hook' for more information.")
18303 (defvar org-shiftmetaright-hook nil
18304 "Hook for functions attaching themselves to `M-S-right'.
18305 See `org-ctrl-c-ctrl-c-hook' for more information.")
18306 (defvar org-shiftmetaup-hook nil
18307 "Hook for functions attaching themselves to `M-S-up'.
18308 See `org-ctrl-c-ctrl-c-hook' for more information.")
18309 (defvar org-shiftmetadown-hook nil
18310 "Hook for functions attaching themselves to `M-S-down'.
18311 See `org-ctrl-c-ctrl-c-hook' for more information.")
18312 (defvar org-metareturn-hook nil
18313 "Hook for functions attaching themselves to `M-RET'.
18314 See `org-ctrl-c-ctrl-c-hook' for more information.")
18315 (defvar org-shiftup-hook nil
18316 "Hook for functions attaching themselves to `S-up'.
18317 See `org-ctrl-c-ctrl-c-hook' for more information.")
18318 (defvar org-shiftup-final-hook nil
18319 "Hook for functions attaching themselves to `S-up'.
18320 This one runs after all other options except shift-select have been excluded.
18321 See `org-ctrl-c-ctrl-c-hook' for more information.")
18322 (defvar org-shiftdown-hook nil
18323 "Hook for functions attaching themselves to `S-down'.
18324 See `org-ctrl-c-ctrl-c-hook' for more information.")
18325 (defvar org-shiftdown-final-hook nil
18326 "Hook for functions attaching themselves to `S-down'.
18327 This one runs after all other options except shift-select have been excluded.
18328 See `org-ctrl-c-ctrl-c-hook' for more information.")
18329 (defvar org-shiftleft-hook nil
18330 "Hook for functions attaching themselves to `S-left'.
18331 See `org-ctrl-c-ctrl-c-hook' for more information.")
18332 (defvar org-shiftleft-final-hook nil
18333 "Hook for functions attaching themselves to `S-left'.
18334 This one runs after all other options except shift-select have been excluded.
18335 See `org-ctrl-c-ctrl-c-hook' for more information.")
18336 (defvar org-shiftright-hook nil
18337 "Hook for functions attaching themselves to `S-right'.
18338 See `org-ctrl-c-ctrl-c-hook' for more information.")
18339 (defvar org-shiftright-final-hook nil
18340 "Hook for functions attaching themselves to `S-right'.
18341 This one runs after all other options except shift-select have been excluded.
18342 See `org-ctrl-c-ctrl-c-hook' for more information.")
18344 (defun org-modifier-cursor-error ()
18345 "Throw an error, a modified cursor command was applied in wrong context."
18346 (error "This command is active in special context like tables, headlines or items"))
18348 (defun org-shiftselect-error ()
18349 "Throw an error because Shift-Cursor command was applied in wrong context."
18350 (if (and (boundp 'shift-select-mode) shift-select-mode)
18351 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18352 (error "This command works only in special context like headlines or timestamps")))
18354 (defun org-call-for-shift-select (cmd)
18355 (let ((this-command-keys-shift-translated t))
18356 (call-interactively cmd)))
18358 (defun org-shifttab (&optional arg)
18359 "Global visibility cycling or move to previous table field.
18360 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18362 See the individual commands for more information."
18365 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18367 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18368 (message "Content view to level: %d" arg)
18369 (org-content (prefix-numeric-value arg2))
18370 (setq org-cycle-global-status 'overview)))
18371 (t (call-interactively 'org-global-cycle))))
18373 (defun org-shiftmetaleft ()
18374 "Promote subtree or delete table column.
18375 Calls `org-promote-subtree', `org-outdent-item-tree', or
18376 `org-table-delete-column', depending on context. See the
18377 individual commands for more information."
18380 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18381 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18382 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18383 ((if (not (org-region-active-p)) (org-at-item-p)
18384 (save-excursion (goto-char (region-beginning))
18386 (call-interactively 'org-outdent-item-tree))
18387 (t (org-modifier-cursor-error))))
18389 (defun org-shiftmetaright ()
18390 "Demote subtree or insert table column.
18391 Calls `org-demote-subtree', `org-indent-item-tree', or
18392 `org-table-insert-column', depending on context. See the
18393 individual commands for more information."
18396 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18397 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18398 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18399 ((if (not (org-region-active-p)) (org-at-item-p)
18400 (save-excursion (goto-char (region-beginning))
18402 (call-interactively 'org-indent-item-tree))
18403 (t (org-modifier-cursor-error))))
18405 (defun org-shiftmetaup (&optional arg)
18406 "Move subtree up or kill table row.
18407 Calls `org-move-subtree-up' or `org-table-kill-row' or
18408 `org-move-item-up' or `org-timestamp-up', depending on context.
18409 See the individual commands for more information."
18412 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18413 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18414 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18415 ((org-at-item-p) (call-interactively 'org-move-item-up))
18416 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18417 (call-interactively 'org-timestamp-up)))
18418 (t (org-modifier-cursor-error))))
18420 (defun org-shiftmetadown (&optional arg)
18421 "Move subtree down or insert table row.
18422 Calls `org-move-subtree-down' or `org-table-insert-row' or
18423 `org-move-item-down' or `org-timestamp-up', depending on context.
18424 See the individual commands for more information."
18427 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18428 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18429 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18430 ((org-at-item-p) (call-interactively 'org-move-item-down))
18431 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18432 (call-interactively 'org-timestamp-down)))
18433 (t (org-modifier-cursor-error))))
18435 (defsubst org-hidden-tree-error ()
18437 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18439 (defun org-metaleft (&optional arg)
18440 "Promote heading or move table column to left.
18441 Calls `org-do-promote' or `org-table-move-column', depending on context.
18442 With no specific context, calls the Emacs default `backward-word'.
18443 See the individual commands for more information."
18446 ((run-hook-with-args-until-success 'org-metaleft-hook))
18447 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18448 ((org-with-limited-levels
18449 (or (org-at-heading-p)
18450 (and (org-region-active-p)
18452 (goto-char (region-beginning))
18453 (org-at-heading-p)))))
18454 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18455 (call-interactively 'org-do-promote))
18456 ;; At an inline task.
18457 ((org-at-heading-p)
18458 (call-interactively 'org-inlinetask-promote))
18459 ((or (org-at-item-p)
18460 (and (org-region-active-p)
18462 (goto-char (region-beginning))
18464 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18465 (call-interactively 'org-outdent-item))
18466 (t (call-interactively 'backward-word))))
18468 (defun org-metaright (&optional arg)
18469 "Demote a subtree, a list item or move table column to right.
18470 In front of a drawer or a block keyword, indent it correctly.
18471 With no specific context, calls the Emacs default `forward-word'.
18472 See the individual commands for more information."
18475 ((run-hook-with-args-until-success 'org-metaright-hook))
18476 ((org-at-table-p) (call-interactively 'org-table-move-column))
18477 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18478 ((org-at-block-p) (call-interactively 'org-indent-block))
18479 ((org-with-limited-levels
18480 (or (org-at-heading-p)
18481 (and (org-region-active-p)
18483 (goto-char (region-beginning))
18484 (org-at-heading-p)))))
18485 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18486 (call-interactively 'org-do-demote))
18487 ;; At an inline task.
18488 ((org-at-heading-p)
18489 (call-interactively 'org-inlinetask-demote))
18490 ((or (org-at-item-p)
18491 (and (org-region-active-p)
18493 (goto-char (region-beginning))
18495 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18496 (call-interactively 'org-indent-item))
18497 (t (call-interactively 'forward-word))))
18499 (defun org-check-for-hidden (what)
18500 "Check if there are hidden headlines/items in the current visual line.
18501 WHAT can be either `headlines' or `items'. If the current line is
18502 an outline or item heading and it has a folded subtree below it,
18503 this function returns t, nil otherwise."
18505 ((eq what 'headlines) org-outline-regexp-bol)
18506 ((eq what 'items) (org-item-beginning-re))
18507 (t (error "This should not happen"))))
18511 (unless (org-region-active-p)
18512 (setq beg (point-at-bol))
18513 (beginning-of-line 2)
18514 (while (and (not (eobp)) ;; this is like `next-line'
18515 (get-char-property (1- (point)) 'invisible))
18516 (beginning-of-line 2))
18519 (goto-char (point-at-eol))
18520 (setq end (max end (point)))
18521 (while (re-search-forward re end t)
18522 (if (get-char-property (match-beginning 0) 'invisible)
18526 (defun org-metaup (&optional arg)
18527 "Move subtree up or move table row up.
18528 Calls `org-move-subtree-up' or `org-table-move-row' or
18529 `org-move-item-up', depending on context. See the individual commands
18530 for more information."
18533 ((run-hook-with-args-until-success 'org-metaup-hook))
18534 ((org-region-active-p)
18535 (let* ((a (min (region-beginning) (region-end)))
18536 (b (1- (max (region-beginning) (region-end))))
18537 (c (save-excursion (goto-char a)
18538 (move-beginning-of-line 0)))
18539 (d (save-excursion (goto-char a)
18540 (move-end-of-line 0) (point))))
18541 (transpose-regions a b c d)
18543 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18544 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18545 ((org-at-item-p) (call-interactively 'org-move-item-up))
18546 (t (org-element-drag-backward))))
18548 (defun org-metadown (&optional arg)
18549 "Move subtree down or move table row down.
18550 Calls `org-move-subtree-down' or `org-table-move-row' or
18551 `org-move-item-down', depending on context. See the individual
18552 commands for more information."
18555 ((run-hook-with-args-until-success 'org-metadown-hook))
18556 ((org-region-active-p)
18557 (let* ((a (min (region-beginning) (region-end)))
18558 (b (max (region-beginning) (region-end)))
18559 (c (save-excursion (goto-char b)
18560 (move-beginning-of-line 1)))
18561 (d (save-excursion (goto-char b)
18562 (move-end-of-line 1) (1+ (point)))))
18563 (transpose-regions a b c d)
18565 ((org-at-table-p) (call-interactively 'org-table-move-row))
18566 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18567 ((org-at-item-p) (call-interactively 'org-move-item-down))
18568 (t (org-element-drag-forward))))
18570 (defun org-shiftup (&optional arg)
18571 "Increase item in timestamp or increase priority of current headline.
18572 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18573 depending on context. See the individual commands for more information."
18576 ((run-hook-with-args-until-success 'org-shiftup-hook))
18577 ((and org-support-shift-select (org-region-active-p))
18578 (org-call-for-shift-select 'previous-line))
18579 ((org-at-timestamp-p t)
18580 (call-interactively (if org-edit-timestamp-down-means-later
18581 'org-timestamp-down 'org-timestamp-up)))
18582 ((and (not (eq org-support-shift-select 'always))
18583 org-enable-priority-commands
18584 (org-at-heading-p))
18585 (call-interactively 'org-priority-up))
18586 ((and (not org-support-shift-select) (org-at-item-p))
18587 (call-interactively 'org-previous-item))
18588 ((org-clocktable-try-shift 'up arg))
18589 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18590 (org-support-shift-select
18591 (org-call-for-shift-select 'previous-line))
18592 (t (org-shiftselect-error))))
18594 (defun org-shiftdown (&optional arg)
18595 "Decrease item in timestamp or decrease priority of current headline.
18596 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18597 depending on context. See the individual commands for more information."
18600 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18601 ((and org-support-shift-select (org-region-active-p))
18602 (org-call-for-shift-select 'next-line))
18603 ((org-at-timestamp-p t)
18604 (call-interactively (if org-edit-timestamp-down-means-later
18605 'org-timestamp-up 'org-timestamp-down)))
18606 ((and (not (eq org-support-shift-select 'always))
18607 org-enable-priority-commands
18608 (org-at-heading-p))
18609 (call-interactively 'org-priority-down))
18610 ((and (not org-support-shift-select) (org-at-item-p))
18611 (call-interactively 'org-next-item))
18612 ((org-clocktable-try-shift 'down arg))
18613 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18614 (org-support-shift-select
18615 (org-call-for-shift-select 'next-line))
18616 (t (org-shiftselect-error))))
18618 (defun org-shiftright (&optional arg)
18619 "Cycle the thing at point or in the current line, depending on context.
18620 Depending on context, this does one of the following:
18622 - switch a timestamp at point one day into the future
18623 - on a headline, switch to the next TODO keyword.
18624 - on an item, switch entire list to the next bullet type
18625 - on a property line, switch to the next allowed value
18626 - on a clocktable definition line, move time block into the future"
18629 ((run-hook-with-args-until-success 'org-shiftright-hook))
18630 ((and org-support-shift-select (org-region-active-p))
18631 (org-call-for-shift-select 'forward-char))
18632 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18633 ((and (not (eq org-support-shift-select 'always))
18634 (org-at-heading-p))
18635 (let ((org-inhibit-logging
18636 (not org-treat-S-cursor-todo-selection-as-state-change))
18637 (org-inhibit-blocking
18638 (not org-treat-S-cursor-todo-selection-as-state-change)))
18639 (org-call-with-arg 'org-todo 'right)))
18640 ((or (and org-support-shift-select
18641 (not (eq org-support-shift-select 'always))
18642 (org-at-item-bullet-p))
18643 (and (not org-support-shift-select) (org-at-item-p)))
18644 (org-call-with-arg 'org-cycle-list-bullet nil))
18645 ((and (not (eq org-support-shift-select 'always))
18646 (org-at-property-p))
18647 (call-interactively 'org-property-next-allowed-value))
18648 ((org-clocktable-try-shift 'right arg))
18649 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18650 (org-support-shift-select
18651 (org-call-for-shift-select 'forward-char))
18652 (t (org-shiftselect-error))))
18654 (defun org-shiftleft (&optional arg)
18655 "Cycle the thing at point or in the current line, depending on context.
18656 Depending on context, this does one of the following:
18658 - switch a timestamp at point one day into the past
18659 - on a headline, switch to the previous TODO keyword.
18660 - on an item, switch entire list to the previous bullet type
18661 - on a property line, switch to the previous allowed value
18662 - on a clocktable definition line, move time block into the past"
18665 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18666 ((and org-support-shift-select (org-region-active-p))
18667 (org-call-for-shift-select 'backward-char))
18668 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18669 ((and (not (eq org-support-shift-select 'always))
18670 (org-at-heading-p))
18671 (let ((org-inhibit-logging
18672 (not org-treat-S-cursor-todo-selection-as-state-change))
18673 (org-inhibit-blocking
18674 (not org-treat-S-cursor-todo-selection-as-state-change)))
18675 (org-call-with-arg 'org-todo 'left)))
18676 ((or (and org-support-shift-select
18677 (not (eq org-support-shift-select 'always))
18678 (org-at-item-bullet-p))
18679 (and (not org-support-shift-select) (org-at-item-p)))
18680 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18681 ((and (not (eq org-support-shift-select 'always))
18682 (org-at-property-p))
18683 (call-interactively 'org-property-previous-allowed-value))
18684 ((org-clocktable-try-shift 'left arg))
18685 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18686 (org-support-shift-select
18687 (org-call-for-shift-select 'backward-char))
18688 (t (org-shiftselect-error))))
18690 (defun org-shiftcontrolright ()
18691 "Switch to next TODO set."
18694 ((and org-support-shift-select (org-region-active-p))
18695 (org-call-for-shift-select 'forward-word))
18696 ((and (not (eq org-support-shift-select 'always))
18697 (org-at-heading-p))
18698 (org-call-with-arg 'org-todo 'nextset))
18699 (org-support-shift-select
18700 (org-call-for-shift-select 'forward-word))
18701 (t (org-shiftselect-error))))
18703 (defun org-shiftcontrolleft ()
18704 "Switch to previous TODO set."
18707 ((and org-support-shift-select (org-region-active-p))
18708 (org-call-for-shift-select 'backward-word))
18709 ((and (not (eq org-support-shift-select 'always))
18710 (org-at-heading-p))
18711 (org-call-with-arg 'org-todo 'previousset))
18712 (org-support-shift-select
18713 (org-call-for-shift-select 'backward-word))
18714 (t (org-shiftselect-error))))
18716 (defun org-shiftcontrolup ()
18717 "Change timestamps synchronously up in CLOCK log lines."
18719 (cond ((and (not org-support-shift-select)
18720 (org-at-clock-log-p)
18721 (org-at-timestamp-p t))
18722 (org-clock-timestamps-up))
18723 (t (org-shiftselect-error))))
18725 (defun org-shiftcontroldown ()
18726 "Change timestamps synchronously down in CLOCK log lines."
18728 (cond ((and (not org-support-shift-select)
18729 (org-at-clock-log-p)
18730 (org-at-timestamp-p t))
18731 (org-clock-timestamps-down))
18732 (t (org-shiftselect-error))))
18734 (defun org-ctrl-c-ret ()
18735 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18738 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18739 (t (call-interactively 'org-insert-heading))))
18741 (defun org-find-visible ()
18743 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18744 (get-char-property s 'invisible)))
18746 (defun org-find-invisible ()
18748 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18749 (not (get-char-property s 'invisible))))
18752 (defun org-copy-visible (beg end)
18753 "Copy the visible parts of the region."
18758 (narrow-to-region beg end)
18759 (setq s (goto-char (point-min)))
18760 (while (not (= (point) (point-max)))
18761 (goto-char (org-find-invisible))
18762 (push (buffer-substring s (point)) snippets)
18763 (setq s (goto-char (org-find-visible))))))
18764 (kill-new (apply 'concat (nreverse snippets)))))
18766 (defun org-copy-special ()
18767 "Copy region in table or copy current subtree.
18768 Calls `org-table-copy' or `org-copy-subtree', depending on context.
18769 See the individual commands for more information."
18771 (call-interactively
18772 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
18774 (defun org-cut-special ()
18775 "Cut region in table or cut current subtree.
18776 Calls `org-table-copy' or `org-cut-subtree', depending on context.
18777 See the individual commands for more information."
18779 (call-interactively
18780 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
18782 (defun org-paste-special (arg)
18783 "Paste rectangular region into table, or past subtree relative to level.
18784 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
18785 See the individual commands for more information."
18787 (if (org-at-table-p)
18788 (org-table-paste-rectangle)
18789 (org-paste-subtree arg)))
18791 (defun org-edit-special (&optional arg)
18792 "Call a special editor for the stuff at point.
18793 When at a table, call the formula editor with `org-table-edit-formulas'.
18794 When at the first line of an src example, call `org-edit-src-code'.
18795 When in an #+include line, visit the include file. Otherwise call
18796 `ffap' to visit the file at point."
18798 ;; possibly prep session before editing source
18800 (let* ((info (org-babel-get-src-block-info))
18801 (lang (nth 0 info))
18802 (params (nth 2 info))
18803 (session (cdr (assoc :session params))))
18804 (when (and info session) ;; we are in a source-code block with a session
18806 (intern (concat "org-babel-prep-session:" lang)) session params))))
18807 (cond ;; proceed with `org-edit-special'
18809 (beginning-of-line 1)
18810 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
18811 (find-file (org-trim (match-string 1))))
18812 ((org-edit-src-code))
18813 ((org-edit-fixed-width-region))
18814 ((org-at-table.el-p)
18815 (org-edit-src-code))
18816 ((or (org-at-table-p)
18818 (beginning-of-line 1)
18819 (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
18820 (call-interactively 'org-table-edit-formulas))
18821 (t (call-interactively 'ffap))))
18823 (defvar org-table-coordinate-overlays) ; defined in org-table.el
18824 (defun org-ctrl-c-ctrl-c (&optional arg)
18825 "Set tags in headline, or update according to changed information at point.
18827 This command does many different things, depending on context:
18829 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
18830 this is what we do.
18832 - If the cursor is on a statistics cookie, update it.
18834 - If the cursor is in a headline, prompt for tags and insert them
18835 into the current line, aligned to `org-tags-column'. When called
18836 with prefix arg, realign all tags in the current buffer.
18838 - If the cursor is in one of the special #+KEYWORD lines, this
18839 triggers scanning the buffer for these lines and updating the
18842 - If the cursor is inside a table, realign the table. This command
18843 works even if the automatic table editor has been turned off.
18845 - If the cursor is on a #+TBLFM line, re-apply the formulas to
18848 - If the cursor is at a footnote reference or definition, jump to
18849 the corresponding definition or references, respectively.
18851 - If the cursor is a the beginning of a dynamic block, update it.
18853 - If the current buffer is a capture buffer, close note and file it.
18855 - If the cursor is on a <<<target>>>, update radio targets and
18856 corresponding links in this buffer.
18858 - If the cursor is on a numbered item in a plain list, renumber the
18861 - If the cursor is on a checkbox, toggle it.
18863 - If the cursor is on a code block, evaluate it. The variable
18864 `org-confirm-babel-evaluate' can be used to control prompting
18865 before code block evaluation, by default every code block
18866 evaluation requires confirmation. Code block evaluation can be
18867 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
18869 (let ((org-enable-table-editor t))
18871 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
18872 org-occur-highlights
18873 org-latex-fragment-image-overlays)
18874 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
18875 (org-remove-occur-highlights)
18876 (org-remove-latex-fragment-image-overlays)
18877 (message "Temporary highlights/overlays removed from current buffer"))
18878 ((and (local-variable-p 'org-finish-function (current-buffer))
18879 (fboundp org-finish-function))
18880 (funcall org-finish-function))
18881 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
18882 ((org-in-regexp org-ts-regexp-both)
18883 (org-timestamp-change 0 'day))
18884 ((or (looking-at org-property-start-re)
18885 (org-at-property-p))
18886 (call-interactively 'org-property-action))
18887 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
18888 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
18889 (or (org-at-heading-p) (org-at-item-p)))
18890 (call-interactively 'org-update-statistics-cookies))
18891 ((org-at-heading-p) (call-interactively 'org-set-tags))
18892 ((org-at-table.el-p)
18893 (message "Use C-c ' to edit table.el tables"))
18895 (org-table-maybe-eval-formula)
18897 (call-interactively 'org-table-recalculate)
18898 (org-table-maybe-recalculate-line))
18899 (call-interactively 'org-table-align)
18900 (orgtbl-send-table 'maybe))
18901 ((or (org-footnote-at-reference-p)
18902 (org-footnote-at-definition-p))
18903 (call-interactively 'org-footnote-action))
18904 ((org-at-item-checkbox-p)
18905 ;; Cursor at a checkbox: repair list and update checkboxes. Send
18906 ;; list only if at top item.
18907 (let* ((cbox (match-string 1))
18908 (struct (org-list-struct))
18909 (old-struct (copy-tree struct))
18910 (parents (org-list-parents-alist struct))
18911 (orderedp (org-entry-get nil "ORDERED"))
18912 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
18914 ;; Use a light version of `org-toggle-checkbox' to avoid
18915 ;; computing list structure twice.
18916 (let ((new-box (cond
18917 ((equal arg '(16)) "[-]")
18918 ((equal arg '(4)) nil)
18919 ((equal "[X]" cbox) "[ ]")
18921 (if (and firstp arg)
18922 ;; If at first item of sub-list, remove check-box from
18923 ;; every item at the same level.
18925 (lambda (pos) (org-list-set-checkbox pos struct new-box))
18926 (org-list-get-all-items
18927 (point-at-bol) struct (org-list-prevs-alist struct)))
18928 (org-list-set-checkbox (point-at-bol) struct new-box)))
18929 ;; Replicate `org-list-write-struct', while grabbing a return
18930 ;; value from `org-list-struct-fix-box'.
18931 (org-list-struct-fix-ind struct parents 2)
18932 (org-list-struct-fix-item-end struct)
18933 (let ((prevs (org-list-prevs-alist struct)))
18934 (org-list-struct-fix-bul struct prevs)
18935 (org-list-struct-fix-ind struct parents)
18937 (org-list-struct-fix-box struct parents prevs orderedp)))
18938 (org-list-struct-apply-struct struct old-struct)
18939 (org-update-checkbox-count-maybe)
18942 "Checkboxes were removed due to unchecked box at line %d"
18943 (org-current-line block-item)))
18944 (when firstp (org-list-send-list 'maybe))))
18946 ;; Cursor at an item: repair list. Do checkbox related actions
18947 ;; only if function was called with an argument. Send list only
18949 (let* ((struct (org-list-struct))
18950 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
18953 (setq old-struct (copy-tree struct))
18955 ;; If at first item of sub-list, add check-box to every
18956 ;; item at the same level.
18959 (unless (org-list-get-checkbox pos struct)
18960 (org-list-set-checkbox pos struct "[ ]")))
18961 (org-list-get-all-items
18962 (point-at-bol) struct (org-list-prevs-alist struct)))
18963 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
18964 (org-list-write-struct
18965 struct (org-list-parents-alist struct) old-struct)
18966 (when arg (org-update-checkbox-count-maybe))
18967 (when firstp (org-list-send-list 'maybe))))
18968 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
18970 (beginning-of-line 1)
18971 (save-excursion (org-update-dblock)))
18973 (let ((case-fold-search t))
18974 (beginning-of-line 1)
18975 (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
18977 ((or (equal (match-string 1) "TBLFM")
18978 (equal (match-string 1) "tblfm"))
18979 ;; Recalculate the table before this line
18981 (beginning-of-line 1)
18982 (skip-chars-backward " \r\n\t")
18983 (if (org-at-table-p)
18984 (org-call-with-arg 'org-table-recalculate (or arg t)))))
18986 (let ((org-inhibit-startup-visibility-stuff t)
18987 (org-startup-align-all-tables nil))
18988 (when (boundp 'org-table-coordinate-overlays)
18989 (mapc 'delete-overlay org-table-coordinate-overlays)
18990 (setq org-table-coordinate-overlays nil))
18991 (org-save-outline-visibility 'use-markers (org-mode-restart)))
18992 (message "Local setup has been refreshed"))))
18993 ((org-clock-update-time-maybe))
18995 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
18996 (error "C-c C-c can do nothing useful at this location"))))))
18998 (defun org-mode-restart ()
18999 "Restart Org-mode, to scan again for special lines.
19000 Also updates the keyword regular expressions."
19003 (message "Org-mode restarted"))
19005 (defun org-kill-note-or-show-branches ()
19006 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19008 (if (not org-finish-function)
19011 (call-interactively 'show-branches))
19012 (let ((org-note-abort t))
19013 (funcall org-finish-function))))
19015 (defun org-return (&optional indent)
19016 "Goto next table row or insert a newline.
19017 Calls `org-table-next-row' or `newline', depending on context.
19018 See the individual commands for more information."
19022 ((or (bobp) (org-in-src-block-p))
19023 (if indent (newline-and-indent) (newline)))
19025 (org-table-justify-field-maybe)
19026 (call-interactively 'org-table-next-row))
19027 ;; when `newline-and-indent' is called within a list, make sure
19028 ;; text moved stays inside the item.
19029 ((and (org-in-item-p) indent)
19030 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19032 (save-match-data (newline))
19033 (org-indent-line-to (length (match-string 0))))
19034 (let ((ind (org-get-indentation)))
19036 (if (org-looking-back org-list-end-re)
19038 (org-indent-line-to ind)))))
19039 ((and org-return-follows-link
19040 (org-at-timestamp-p t)
19041 (not (eq org-ts-what 'after)))
19042 (org-follow-timestamp-link))
19043 ((and org-return-follows-link
19044 (let ((tprop (get-text-property (point) 'face)))
19045 (or (eq tprop 'org-link)
19046 (and (listp tprop) (memq 'org-link tprop)))))
19047 (call-interactively 'org-open-at-point))
19048 ((and (org-at-heading-p)
19050 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19054 (t (if indent (newline-and-indent) (newline))))))
19056 (defun org-return-indent ()
19057 "Goto next table row or insert a newline and indent.
19058 Calls `org-table-next-row' or `newline-and-indent', depending on
19059 context. See the individual commands for more information."
19063 (defun org-ctrl-c-star ()
19064 "Compute table, or change heading status of lines.
19065 Calls `org-table-recalculate' or `org-toggle-heading',
19066 depending on context."
19070 (call-interactively 'org-table-recalculate))
19072 ;; Convert all lines in region to list items
19073 (call-interactively 'org-toggle-heading))))
19075 (defun org-ctrl-c-minus ()
19076 "Insert separator line in table or modify bullet status of line.
19077 Also turns a plain line or a region of lines into list items.
19078 Calls `org-table-insert-hline', `org-toggle-item', or
19079 `org-cycle-list-bullet', depending on context."
19083 (call-interactively 'org-table-insert-hline))
19084 ((org-region-active-p)
19085 (call-interactively 'org-toggle-item))
19087 (call-interactively 'org-cycle-list-bullet))
19089 (call-interactively 'org-toggle-item))))
19091 (defun org-toggle-item (arg)
19092 "Convert headings or normal lines to items, items to normal lines.
19093 If there is no active region, only the current line is considered.
19095 If the first non blank line in the region is an headline, convert
19096 all headlines to items, shifting text accordingly.
19098 If it is an item, convert all items to normal lines.
19100 If it is normal text, change region into an item. With a prefix
19101 argument ARG, change each line in region into an item."
19105 ;; Shift text in current section to IND, from point to END.
19106 ;; The function leaves point to END line.
19108 (let ((min-i 1000) (end (copy-marker end)))
19109 ;; First determine the minimum indentation (MIN-I) of
19113 (while (< (point) end)
19114 (let ((i (org-get-indentation)))
19116 ;; Skip blank lines and inline tasks.
19117 ((looking-at "^[ \t]*$"))
19118 ((looking-at org-outline-regexp-bol))
19119 ;; We can't find less than 0 indentation.
19120 ((zerop i) (throw 'exit (setq min-i 0)))
19121 ((< i min-i) (setq min-i i))))
19123 ;; Then indent each line so that a line indented to
19124 ;; MIN-I becomes indented to IND. Ignore blank lines
19125 ;; and inline tasks in the process.
19126 (let ((delta (- ind min-i)))
19127 (while (< (point) end)
19128 (unless (or (looking-at "^[ \t]*$")
19129 (looking-at org-outline-regexp-bol))
19130 (org-indent-line-to (+ (org-get-indentation) delta)))
19131 (forward-line)))))))
19134 ;; Return beginning of first non-blank line, starting from
19139 (skip-chars-forward " \r\t\n")
19142 ;; Determine boundaries of changes.
19143 (if (org-region-active-p)
19144 (setq beg (funcall skip-blanks (region-beginning))
19145 end (copy-marker (region-end)))
19146 (setq beg (funcall skip-blanks (point-at-bol))
19147 end (copy-marker (point-at-eol))))
19148 ;; Depending on the starting line, choose an action on the text
19149 ;; between BEG and END.
19150 (org-with-limited-levels
19154 ;; Case 1. Start at an item: de-itemize. Note that it only
19155 ;; happens when a region is active: `org-ctrl-c-minus'
19156 ;; would call `org-cycle-list-bullet' otherwise.
19158 (while (< (point) end)
19159 (when (org-at-item-p)
19160 (skip-chars-forward " \t")
19161 (delete-region (point) (match-end 0)))
19163 ;; Case 2. Start at an heading: convert to items.
19164 ((org-at-heading-p)
19165 (let* ((bul (org-list-bullet-string "-"))
19166 (bul-len (length bul))
19167 ;; Indentation of the first heading. It should be
19168 ;; relative to the indentation of its parent, if any.
19169 (start-ind (save-excursion
19171 ((not org-adapt-indentation) 0)
19172 ((not (outline-previous-heading)) 0)
19173 (t (length (match-string 0))))))
19174 ;; Level of first heading. Further headings will be
19175 ;; compared to it to determine hierarchy in the list.
19176 (ref-level (org-reduced-level (org-outline-level))))
19177 (while (< (point) end)
19178 (let* ((level (org-reduced-level (org-outline-level)))
19179 (delta (max 0 (- level ref-level))))
19180 ;; If current headline is less indented than the first
19181 ;; one, set it as reference, in order to preserve
19183 (when (< level ref-level) (setq ref-level level))
19184 (replace-match bul t t)
19185 (org-indent-line-to (+ start-ind (* delta bul-len)))
19186 ;; Ensure all text down to END (or SECTION-END) belongs
19187 ;; to the newly created item.
19188 (let ((section-end (save-excursion
19189 (or (outline-next-heading) (point)))))
19191 (funcall shift-text
19192 (+ start-ind (* (1+ delta) bul-len))
19193 (min end section-end)))))))
19194 ;; Case 3. Normal line with ARG: turn each non-item line into
19197 (while (< (point) end)
19198 (unless (or (org-at-heading-p) (org-at-item-p))
19199 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
19201 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
19203 ;; Case 4. Normal line without ARG: make the first line of
19204 ;; region an item, and shift indentation of others
19205 ;; lines to set them as item's body.
19206 (t (let* ((bul (org-list-bullet-string "-"))
19207 (bul-len (length bul))
19208 (ref-ind (org-get-indentation)))
19209 (skip-chars-forward " \t")
19212 (while (< (point) end)
19213 ;; Ensure that lines less indented than first one
19214 ;; still get included in item body.
19215 (funcall shift-text
19216 (+ ref-ind bul-len)
19217 (min end (save-excursion (or (outline-next-heading)
19219 (forward-line)))))))))
19221 (defun org-toggle-heading (&optional nstars)
19222 "Convert headings to normal text, or items or text to headings.
19223 If there is no active region, only the current line is considered.
19225 With a \\[universal-argument] prefix, convert the whole list at
19226 point into heading.
19230 - If the first non blank line is an headline, remove the stars
19231 from all headlines in the region.
19233 - If it is a normal line turn each and every normal line (i.e. not an
19234 heading or an item) in the region into a heading.
19236 - If it is a plain list item, turn all plain list items into headings.
19238 When converting a line into a heading, the number of stars is chosen
19239 such that the lines become children of the current entry. However,
19240 when a prefix argument is given, its value determines the number of
19245 ;; Return beginning of first non-blank line, starting from
19250 (while (org-at-comment-p) (forward-line))
19251 (skip-chars-forward " \r\t\n")
19254 ;; Determine boundaries of changes. If a universal prefix has
19255 ;; been given, put the list in a region. If region ends at a bol,
19256 ;; do not consider the last line to be in the region.
19258 (when (and current-prefix-arg (org-at-item-p))
19259 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
19262 (if (org-region-active-p)
19263 (setq beg (funcall skip-blanks (region-beginning))
19264 end (copy-marker (save-excursion
19265 (goto-char (region-end))
19266 (if (bolp) (point) (point-at-eol)))))
19267 (setq beg (funcall skip-blanks (point-at-bol))
19268 end (copy-marker (point-at-eol))))
19269 ;; Ensure inline tasks don't count as headings.
19270 (org-with-limited-levels
19274 ;; Case 1. Started at an heading: de-star headings.
19275 ((org-at-heading-p)
19276 (while (< (point) end)
19277 (when (org-at-heading-p t)
19278 (looking-at org-outline-regexp) (replace-match "")
19281 ;; Case 2. Started at an item: change items into headlines.
19282 ;; One star will be added by `org-list-to-subtree'.
19284 (let* ((stars (make-string
19286 ;; subtract the star that will be added again by
19287 ;; `org-list-to-subtree'
19288 (1- (prefix-numeric-value current-prefix-arg))
19289 (or (org-current-level) 0))
19292 (cond (nstars "") ; stars from prefix only
19293 ((equal stars "") "") ; before first heading
19294 (org-odd-levels-only "*") ; inside heading, odd
19295 (t "")))) ; inside heading, oddeven
19296 (while (< (point) end)
19297 (when (org-at-item-p)
19298 ;; Pay attention to cases when region ends before list.
19299 (let* ((struct (org-list-struct))
19300 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
19302 (narrow-to-region (point) list-end)
19304 (org-list-to-subtree
19305 (org-list-parse-list t)
19306 '(:istart (concat stars add-stars (funcall get-stars depth))
19307 :icount (concat stars add-stars (funcall get-stars depth)))))))
19310 ;; Case 3. Started at normal text: make every line an heading,
19311 ;; skipping headlines and items.
19312 (t (let* ((stars (make-string
19314 (prefix-numeric-value current-prefix-arg)
19315 (or (org-current-level) 0))
19318 (cond (nstars "") ; stars from prefix only
19319 ((equal stars "") "*") ; before first heading
19320 (org-odd-levels-only "**") ; inside heading, odd
19321 (t "*"))) ; inside heading, oddeven
19322 (rpl (concat stars add-stars " ")))
19323 (while (< (point) end)
19324 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
19325 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
19326 (replace-match (concat rpl (match-string 2))) (setq toggled t))
19327 (forward-line)))))))
19328 (unless toggled (message "Cannot toggle heading from here"))))
19330 (defun org-meta-return (&optional arg)
19331 "Insert a new heading or wrap a region in a table.
19332 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
19333 See the individual commands for more information."
19336 ((run-hook-with-args-until-success 'org-metareturn-hook))
19337 ((or (org-at-drawer-p) (org-at-property-p))
19338 (newline-and-indent))
19340 (call-interactively 'org-table-wrap-region))
19341 (t (call-interactively 'org-insert-heading))))
19345 ;; Define the Org-mode menus
19346 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
19348 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
19349 ["Next Field" org-cycle (org-at-table-p)]
19350 ["Previous Field" org-shifttab (org-at-table-p)]
19351 ["Next Row" org-return (org-at-table-p)]
19353 ["Blank Field" org-table-blank-field (org-at-table-p)]
19354 ["Edit Field" org-table-edit-field (org-at-table-p)]
19355 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19358 ["Move Column Left" org-metaleft (org-at-table-p)]
19359 ["Move Column Right" org-metaright (org-at-table-p)]
19360 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19361 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19363 ["Move Row Up" org-metaup (org-at-table-p)]
19364 ["Move Row Down" org-metadown (org-at-table-p)]
19365 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19366 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19367 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19369 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19371 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19372 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19373 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19374 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19377 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19378 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19379 ["Edit Formulas" org-edit-special (org-at-table-p)]
19381 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19382 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19383 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19385 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19387 ["Sum Column/Rectangle" org-table-sum
19388 (or (org-at-table-p) (org-region-active-p))]
19389 ["Which Column?" org-table-current-column (org-at-table-p)])
19391 org-table-toggle-formula-debugger
19392 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19393 ["Show Col/Row Numbers"
19394 org-table-toggle-coordinate-overlays
19396 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19398 ["Create" org-table-create (and (not (org-at-table-p))
19399 org-enable-table-editor)]
19400 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19401 ["Import from File" org-table-import (not (org-at-table-p))]
19402 ["Export to File" org-table-export (org-at-table-p)]
19404 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19406 (easy-menu-define org-org-menu org-mode-map "Org menu"
19409 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19410 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19411 ["Sparse Tree..." org-sparse-tree t]
19412 ["Reveal Context" org-reveal t]
19413 ["Show All" show-all t]
19415 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19417 ["New Heading" org-insert-heading t]
19418 ("Navigate Headings"
19419 ["Up" outline-up-heading t]
19420 ["Next" outline-next-visible-heading t]
19421 ["Previous" outline-previous-visible-heading t]
19422 ["Next Same Level" outline-forward-same-level t]
19423 ["Previous Same Level" outline-backward-same-level t]
19425 ["Jump" org-goto t])
19427 ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
19428 ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
19430 ["Copy Subtree" org-copy-special (not (org-at-table-p))]
19431 ["Cut Subtree" org-cut-special (not (org-at-table-p))]
19432 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19434 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19436 ["Copy visible text" org-copy-visible t]
19438 ["Promote Heading" org-metaleft (not (org-at-table-p))]
19439 ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
19440 ["Demote Heading" org-metaright (not (org-at-table-p))]
19441 ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
19443 ["Sort Region/Children" org-sort (not (org-at-table-p))]
19445 ["Convert to odd levels" org-convert-to-odd-levels t]
19446 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19448 ["Emphasis..." org-emphasize t]
19449 ["Edit Source Example" org-edit-special t]
19451 ["Footnote new/jump" org-footnote-action t]
19452 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19454 ["Archive (default method)" org-archive-subtree-default t]
19456 ["Move Subtree to Archive file" org-advertized-archive-subtree t]
19457 ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
19458 ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
19462 ["Store Link (Global)" org-store-link t]
19463 ["Find existing link to here" org-occur-link-in-agenda-files t]
19464 ["Insert Link" org-insert-link t]
19465 ["Follow Link" org-open-at-point t]
19467 ["Next link" org-next-link t]
19468 ["Previous link" org-previous-link t]
19470 ["Descriptive Links"
19471 org-toggle-link-display
19473 :selected org-descriptive-links
19476 org-toggle-link-display
19478 :selected (not org-descriptive-links)])
19481 ["TODO/DONE/-" org-todo t]
19483 ["Next keyword" org-shiftright (org-at-heading-p)]
19484 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19485 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19486 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19487 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19488 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19489 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19491 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19492 :selected org-enforce-todo-dependencies :style toggle :active t]
19493 "Settings for tree at point"
19494 ["Do Children sequentially" org-toggle-ordered-property :style radio
19495 :selected (org-entry-get nil "ORDERED")
19496 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19497 ["Do Children parallel" org-toggle-ordered-property :style radio
19498 :selected (not (org-entry-get nil "ORDERED"))
19499 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19501 ["Set Priority" org-priority t]
19502 ["Priority Up" org-shiftup t]
19503 ["Priority Down" org-shiftdown t]
19505 ["Get news from all feeds" org-feed-update-all t]
19506 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19507 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19508 ("TAGS and Properties"
19509 ["Set Tags" org-set-tags-command t]
19510 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19512 ["Set property" org-set-property t]
19513 ["Column view of properties" org-columns t]
19514 ["Insert Column View DBlock" org-insert-columns-dblock t])
19515 ("Dates and Scheduling"
19516 ["Timestamp" org-time-stamp t]
19517 ["Timestamp (inactive)" org-time-stamp-inactive t]
19519 ["1 Day Later" org-shiftright t]
19520 ["1 Day Earlier" org-shiftleft t]
19521 ["1 ... Later" org-shiftup t]
19522 ["1 ... Earlier" org-shiftdown t])
19523 ["Compute Time Range" org-evaluate-time-range t]
19524 ["Schedule Item" org-schedule t]
19525 ["Deadline" org-deadline t]
19527 ["Custom time format" org-toggle-time-stamp-overlays
19528 :style radio :selected org-display-custom-times]
19530 ["Goto Calendar" org-goto-calendar t]
19531 ["Date from Calendar" org-date-from-calendar t]
19533 ["Start/Restart Timer" org-timer-start t]
19534 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19535 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19536 ["Insert Timer String" org-timer t]
19537 ["Insert Timer Item" org-timer-item t])
19539 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19540 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19541 ["Clock out" org-clock-out t]
19542 ["Clock cancel" org-clock-cancel t]
19544 ["Mark as default task" org-clock-mark-default-task t]
19545 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19546 ["Goto running clock" org-clock-goto t]
19548 ["Display times" org-clock-display t]
19549 ["Create clock table" org-clock-report t]
19551 ["Record DONE time"
19552 (progn (setq org-log-done (not org-log-done))
19553 (message "Switching to %s will %s record a timestamp"
19554 (car org-done-keywords)
19555 (if org-log-done "automatically" "not")))
19556 :style toggle :selected org-log-done])
19558 ["Agenda Command..." org-agenda t]
19559 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19560 ("File List for Agenda")
19561 ("Special views current file"
19562 ["TODO Tree" org-show-todo-tree t]
19563 ["Check Deadlines" org-check-deadlines t]
19564 ["Timeline" org-timeline t]
19565 ["Tags/Property tree" org-match-sparse-tree t])
19567 ["Export/Publish..." org-export t]
19569 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19570 :selected org-cdlatex-mode]
19571 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19572 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19573 ["Modify math symbol" org-cdlatex-math-modify
19574 (org-inside-LaTeX-fragment-p)]
19575 ["Insert citation" org-reftex-citation t]
19577 ["Template for BEAMER" (progn (require 'org-beamer)
19578 (org-insert-beamer-options-template)) t])
19581 ["Push Files and Views" org-mobile-push t]
19582 ["Get Captured and Flagged" org-mobile-pull t]
19583 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19585 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19588 ["Show Version" org-version t]
19589 ["Info Documentation" org-info t])
19591 ["Browse Org Group" org-customize t]
19593 ["Expand This Menu" org-create-customize-menu
19594 (fboundp 'customize-menu-create)])
19595 ["Send bug report" org-submit-bug-report t]
19598 ["Refresh setup current buffer" org-mode-restart t]
19599 ["Reload Org (after update)" org-reload t]
19600 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
19603 (defun org-info (&optional node)
19604 "Read documentation for Org-mode in the info system.
19605 With optional NODE, go directly to that node."
19607 (info (format "(org)%s" (or node ""))))
19610 (defun org-submit-bug-report ()
19611 "Submit a bug report on Org-mode via mail.
19613 Don't hesitate to report any problems or inaccurate documentation.
19615 If you don't have setup sending mail from (X)Emacs, please copy the
19616 output buffer into your mail program, as it gives us important
19617 information about your Org-mode version and configuration."
19619 (require 'reporter)
19620 (org-load-modules-maybe)
19621 (org-require-autoloaded-modules)
19622 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19623 (reporter-submit-bug-report
19624 "emacs-orgmode@gnu.org"
19625 (org-version nil 'full)
19627 (save-window-excursion
19628 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19629 (delete-other-windows)
19631 (insert "You are about to submit a bug report to the Org-mode mailing list.
19633 We would like to add your full Org-mode and Outline configuration to the
19634 bug report. This greatly simplifies the work of the maintainer and
19635 other experts on the mailing list.
19637 HOWEVER, some variables you have customized may contain private
19638 information. The names of customers, colleagues, or friends, might
19639 appear in the form of file names, tags, todo states, or search strings.
19640 If you answer yes to the prompt, you might want to check and remove
19641 such private information before sending the email.")
19642 (add-text-properties (point-min) (point-max) '(face org-warning))
19643 (when (yes-or-no-p "Include your Org-mode configuration ")
19647 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19648 (or (and (symbol-value v)
19649 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19651 (get v 'custom-type) (get v 'standard-value)
19652 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19654 (kill-buffer (get-buffer "*Warn about privacy*"))
19657 "Remember to cover the basics, that is, what you expected to happen and
19658 what in fact did happen. You don't know how to make a good report? See
19660 http://orgmode.org/manual/Feedback.html#Feedback
19662 Your bug report will be posted to the Org-mode mailing list.
19663 ------------------------------------------------------------------------")
19665 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19666 (replace-match "\\1Bug: \\3 [\\2]")))))
19669 (defun org-install-agenda-files-menu ()
19670 (let ((bl (buffer-list)))
19673 (set-buffer (pop bl))
19674 (if (derived-mode-p 'org-mode) (setq bl nil)))
19675 (when (derived-mode-p 'org-mode)
19677 '("Org") "File List for Agenda"
19680 ["Edit File List" (org-edit-agenda-file-list) t]
19681 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19682 ["Remove Current File from List" org-remove-file t]
19683 ["Cycle through agenda files" org-cycle-agenda-files t]
19684 ["Occur in all agenda files" org-occur-in-agenda-files t]
19686 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19691 (defun org-require-autoloaded-modules ()
19694 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19695 org-docbook org-exp org-html org-icalendar
19697 org-publish org-remember org-table
19698 org-timer org-xoxo)))
19701 (defun org-reload (&optional uncompiled)
19702 "Reload all org lisp files.
19703 With prefix arg UNCOMPILED, load the uncompiled versions."
19705 (require 'find-func)
19706 (let* ((file-re "^org\\(-.*\\)?\\.el")
19707 (dir-org (file-name-directory (org-find-library-dir "org")))
19708 (dir-org-contrib (ignore-errors
19709 (file-name-directory
19710 (org-find-library-dir "org-contribdir"))))
19712 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19713 (append (list nil "comint" "eval" "exp" "keys"
19714 "lob" "ref" "table" "tangle")
19718 (when (cdr lang) (symbol-name (car lang))))
19719 org-babel-load-languages)))))
19721 (append babel-files
19722 (and dir-org-contrib
19723 (directory-files dir-org-contrib t file-re))
19724 (directory-files dir-org t file-re)))
19725 (remove-re (concat (if (featurep 'xemacs)
19726 "org-colview" "org-colview-xemacs")
19728 (setq files (mapcar 'file-name-sans-extension files))
19729 (setq files (mapcar
19730 (lambda (x) (if (string-match remove-re x) nil x))
19732 (setq files (delq nil files))
19735 (when (featurep (intern (file-name-nondirectory f)))
19736 (if (and (not uncompiled)
19737 (file-exists-p (concat f ".elc")))
19738 (load (concat f ".elc") nil nil 'nosuffix)
19739 (load (concat f ".el") nil nil 'nosuffix))))
19741 (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
19742 (org-version nil 'full 'message))
19745 (defun org-customize ()
19746 "Call the customize function with org as argument."
19748 (org-load-modules-maybe)
19749 (org-require-autoloaded-modules)
19750 (customize-browse 'org))
19752 (defun org-create-customize-menu ()
19753 "Create a full customization menu for Org-mode, insert it into the menu."
19755 (org-load-modules-maybe)
19756 (org-require-autoloaded-modules)
19757 (if (fboundp 'customize-menu-create)
19760 '("Org") "Customize"
19761 `(["Browse Org group" org-customize t]
19763 ,(customize-menu-create 'org)
19764 ["Set" Custom-set t]
19765 ["Save" Custom-save t]
19766 ["Reset to Current" Custom-reset-current t]
19767 ["Reset to Saved" Custom-reset-saved t]
19768 ["Reset to Standard Settings" Custom-reset-standard t]))
19769 (message "\"Org\"-menu now contains full customization menu"))
19770 (error "Cannot expand menu (outdated version of cus-edit.el)")))
19772 ;;;; Miscellaneous stuff
19774 ;;; Generally useful functions
19776 (defun org-get-at-bol (property)
19777 "Get text property PROPERTY at beginning of line."
19778 (get-text-property (point-at-bol) property))
19780 (defun org-find-text-property-in-string (prop s)
19781 "Return the first non-nil value of property PROP in string S."
19782 (or (get-text-property 0 prop s)
19783 (get-text-property (or (next-single-property-change 0 prop s) 0)
19786 (defun org-display-warning (message) ;; Copied from Emacs-Muse
19787 "Display the given MESSAGE as a warning."
19788 (if (fboundp 'display-warning)
19789 (display-warning 'org message
19790 (if (featurep 'xemacs) 'warning :warning))
19791 (let ((buf (get-buffer-create "*Org warnings*")))
19792 (with-current-buffer buf
19793 (goto-char (point-max))
19794 (insert "Warning (Org): " message)
19797 (display-buffer buf)
19800 (defun org-eval (form)
19801 "Eval FORM and return result."
19802 (condition-case error
19804 (error (format "%%![Error: %s]" error))))
19806 (defun org-in-clocktable-p ()
19807 "Check if the cursor is in a clocktable."
19808 (let ((pos (point)) start)
19811 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
19812 (setq start (match-beginning 0))
19813 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
19814 (>= (match-end 0) pos)
19817 (defun org-in-commented-line ()
19818 "Is point in a line starting with `#'?"
19819 (equal (char-after (point-at-bol)) ?#))
19821 (defun org-in-indented-comment-line ()
19822 "Is point in a line starting with `#' after some white space?"
19825 (goto-char (point-at-bol))
19826 (looking-at "[ \t]*#"))))
19828 (defun org-in-verbatim-emphasis ()
19830 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
19832 (defun org-goto-marker-or-bmk (marker &optional bookmark)
19833 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
19834 (if (and marker (marker-buffer marker)
19835 (buffer-live-p (marker-buffer marker)))
19837 (org-pop-to-buffer-same-window (marker-buffer marker))
19838 (if (or (> marker (point-max)) (< marker (point-min)))
19841 (org-show-context 'org-goto))
19843 (bookmark-jump bookmark)
19844 (error "Cannot find location"))))
19846 (defun org-quote-csv-field (s)
19847 "Quote field for inclusion in CSV material."
19848 (if (string-match "[\",]" s)
19849 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
19852 (defun org-force-self-insert (N)
19853 "Needed to enforce self-insert under remapping."
19855 (self-insert-command N))
19857 (defun org-string-width (s)
19858 "Compute width of string, ignoring invisible characters.
19859 This ignores character with invisibility property `org-link', and also
19860 characters with property `org-cwidth', because these will become invisible
19861 upon the next fontification round."
19863 (when (or (eq t buffer-invisibility-spec)
19864 (assq 'org-link buffer-invisibility-spec))
19865 (while (setq b (text-property-any 0 (length s)
19866 'invisible 'org-link s))
19867 (setq s (concat (substring s 0 b)
19868 (substring s (or (next-single-property-change
19869 b 'invisible s) (length s)))))))
19870 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
19871 (setq s (concat (substring s 0 b)
19872 (substring s (or (next-single-property-change
19873 b 'org-cwidth s) (length s))))))
19874 (setq l (string-width s) b -1)
19875 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
19876 (setq l (- l (get-text-property b 'org-dwidth-n s))))
19879 (defun org-shorten-string (s maxlength)
19880 "Shorten string S so tht it is no longer than MAXLENGTH characters.
19881 If the string is shorter or has length MAXLENGTH, just return the
19882 original string. If it is longer, the functions finds a space in the
19883 string, breaks this string off at that locations and adds three dots
19884 as ellipsis. Including the ellipsis, the string will not be longer
19885 than MAXLENGTH. If finding a good breaking point in the string does
19886 not work, the string is just chopped off in the middle of a word
19888 (if (<= (length s) maxlength)
19890 (let* ((n (max (- maxlength 4) 1))
19891 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
19892 (if (string-match re s)
19893 (concat (match-string 1 s) "...")
19894 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
19896 (defun org-get-indentation (&optional line)
19897 "Get the indentation of the current line, interpreting tabs.
19898 When LINE is given, assume it represents a line and compute its indentation."
19900 (if (string-match "^ *" (org-remove-tabs line))
19903 (beginning-of-line 1)
19904 (skip-chars-forward " \t")
19905 (current-column))))
19907 (defun org-get-string-indentation (s)
19908 "What indentation has S due to SPACE and TAB at the beginning of the string?"
19909 (let ((n -1) (i 0) (w tab-width) c)
19911 (while (< (setq n (1+ n)) (length s))
19912 (setq c (aref s n))
19913 (cond ((= c ?\ ) (setq i (1+ i)))
19914 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
19915 (t (throw 'exit t)))))
19918 (defun org-remove-tabs (s &optional width)
19919 "Replace tabulators in S with spaces.
19920 Assumes that s is a single line, starting in column 0."
19921 (setq width (or width tab-width))
19922 (while (string-match "\t" s)
19923 (setq s (replace-match
19925 (- (* width (/ (+ (match-beginning 0) width) width))
19926 (match-beginning 0)) ?\ )
19930 (defun org-fix-indentation (line ind)
19931 "Fix indentation in LINE.
19932 IND is a cons cell with target and minimum indentation.
19933 If the current indentation in LINE is smaller than the minimum,
19934 leave it alone. If it is larger than ind, set it to the target."
19935 (let* ((l (org-remove-tabs line))
19936 (i (org-get-indentation l))
19937 (i1 (car ind)) (i2 (cdr ind)))
19938 (if (>= i i2) (setq l (substring line i2)))
19940 (concat (make-string i1 ?\ ) l)
19943 (defun org-remove-indentation (code &optional n)
19944 "Remove the maximum common indentation from the lines in CODE.
19945 N may optionally be the number of spaces to remove."
19948 (org-do-remove-indentation n)
19951 (defun org-do-remove-indentation (&optional n)
19952 "Remove the maximum common indentation from the buffer."
19953 (untabify (point-min) (point-max))
19954 (let ((min 10000) re)
19957 (goto-char (point-min))
19958 (while (re-search-forward "^ *[^ \n]" nil t)
19959 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
19960 (unless (or (= min 0) (= min 10000))
19961 (setq re (format "^ \\{%d\\}" min))
19962 (goto-char (point-min))
19963 (while (re-search-forward re nil t)
19968 (defun org-fill-template (template alist)
19969 "Find each %key of ALIST in TEMPLATE and replace it."
19970 (let ((case-fold-search nil)
19972 (setq alist (sort (copy-sequence alist)
19973 (lambda (a b) (< (length (car a)) (length (car b))))))
19974 (while (setq entry (pop alist))
19976 (replace-regexp-in-string
19977 (concat "%" (regexp-quote (car entry)))
19978 (cdr entry) template t t)))
19981 (defun org-base-buffer (buffer)
19982 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
19985 (or (buffer-base-buffer buffer)
19988 (defun org-trim (s)
19989 "Remove whitespace at beginning and end of string."
19990 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
19991 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
19994 (defun org-wrap (string &optional width lines)
19995 "Wrap string to either a number of lines, or a width in characters.
19996 If WIDTH is non-nil, the string is wrapped to that width, however many lines
19997 that costs. If there is a word longer than WIDTH, the text is actually
19998 wrapped to the length of that word.
19999 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20000 many lines, whatever width that takes.
20001 The return value is a list of lines, without newlines at the end."
20002 (let* ((words (org-split-string string "[ \t\n]+"))
20003 (maxword (apply 'max (mapcar 'org-string-width words)))
20006 (org-do-wrap words (max maxword width)))
20009 (setq ll (org-do-wrap words maxword))
20010 (if (<= (length ll) lines)
20013 (while (> (length ll) lines)
20015 (setq ll (org-do-wrap words w)))
20017 (t (error "Cannot wrap this")))))
20019 (defun org-do-wrap (words width)
20020 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20023 (setq line (pop words))
20024 (while (and words (< (+ (length line) (length (car words))) width))
20025 (setq line (concat line " " (pop words))))
20026 (setq lines (push line lines)))
20029 (defun org-split-string (string &optional separators)
20030 "Splits STRING into substrings at SEPARATORS.
20031 No empty strings are returned if there are matches at the beginning
20032 and end of string."
20033 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20037 (while (and (string-match rexp string
20039 (= start (match-beginning 0))
20040 (< start (length string)))
20042 (< (match-beginning 0) (length string)))
20044 (or (eq (match-beginning 0) 0)
20045 (and (eq (match-beginning 0) (match-end 0))
20046 (eq (match-beginning 0) start))
20048 (cons (substring string start (match-beginning 0))
20050 (setq start (match-end 0)))
20051 (or (eq start (length string))
20053 (cons (substring string start)
20057 (defun org-quote-vert (s)
20058 "Replace \"|\" with \"\\vert\"."
20059 (while (string-match "|" s)
20060 (setq s (replace-match "\\vert" t t s)))
20063 (defun org-uuidgen-p (s)
20064 "Is S an ID created by UUIDGEN?"
20065 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20067 (defun org-in-src-block-p nil
20068 "Whether point is in a code source block."
20070 (when (setq ov (overlays-at (point)))
20071 (memq 'org-block-background
20072 (overlay-properties
20075 (defun org-context ()
20076 "Return a list of contexts of the current cursor position.
20077 If several contexts apply, all are returned.
20078 Each context entry is a list with a symbol naming the context, and
20079 two positions indicating start and end of the context. Possible
20082 :headline anywhere in a headline
20083 :headline-stars on the leading stars in a headline
20084 :todo-keyword on a TODO keyword (including DONE) in a headline
20085 :tags on the TAGS in a headline
20086 :priority on the priority cookie in a headline
20087 :item on the first line of a plain list item
20088 :item-bullet on the bullet/number of a plain list item
20089 :checkbox on the checkbox in a plain list item
20090 :table in an org-mode table
20091 :table-special on a special filed in a table
20092 :table-table in a table.el table
20093 :clocktable in a clocktable
20094 :src-block in a source block
20095 :link on a hyperlink
20096 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
20097 :target on a <<target>>
20098 :radio-target on a <<<radio-target>>>
20099 :latex-fragment on a LaTeX fragment
20100 :latex-preview on a LaTeX fragment with overlaid preview image
20102 This function expects the position to be visible because it uses font-lock
20103 faces as a help to recognize the following contexts: :table-special, :link,
20105 (let* ((f (get-text-property (point) 'face))
20106 (faces (if (listp f) f (list f)))
20107 (case-fold-search t)
20108 (p (point)) clist o)
20109 ;; First the large context
20111 ((org-at-heading-p t)
20112 (push (list :headline (point-at-bol) (point-at-eol)) clist)
20114 (beginning-of-line 1)
20115 (looking-at org-todo-line-tags-regexp))
20116 (push (org-point-in-group p 1 :headline-stars) clist)
20117 (push (org-point-in-group p 2 :todo-keyword) clist)
20118 (push (org-point-in-group p 4 :tags) clist))
20120 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
20121 (if (looking-at "\\[#[A-Z0-9]\\]")
20122 (push (org-point-in-group p 0 :priority) clist)))
20125 (push (org-point-in-group p 2 :item-bullet) clist)
20126 (push (list :item (point-at-bol)
20127 (save-excursion (org-end-of-item) (point)))
20129 (and (org-at-item-checkbox-p)
20130 (push (org-point-in-group p 0 :checkbox) clist)))
20133 (push (list :table (org-table-begin) (org-table-end)) clist)
20134 (if (memq 'org-formula faces)
20135 (push (list :table-special
20136 (previous-single-property-change p 'face)
20137 (next-single-property-change p 'face)) clist)))
20138 ((org-at-table-p 'any)
20139 (push (list :table-table) clist)))
20142 ;; New the "medium" contexts: clocktables, source blocks
20143 (cond ((org-in-clocktable-p)
20144 (push (list :clocktable
20145 (and (or (looking-at "#\\+BEGIN: clocktable")
20146 (search-backward "#+BEGIN: clocktable" nil t))
20147 (match-beginning 0))
20148 (and (re-search-forward "#\\+END:?" nil t)
20149 (match-end 0))) clist))
20150 ((org-in-src-block-p)
20151 (push (list :src-block
20152 (and (or (looking-at "#\\+BEGIN_SRC")
20153 (search-backward "#+BEGIN_SRC" nil t))
20154 (match-beginning 0))
20155 (and (search-forward "#+END_SRC" nil t)
20156 (match-beginning 0))) clist)))
20159 ;; Now the small context
20161 ((org-at-timestamp-p)
20162 (push (org-point-in-group p 0 :timestamp) clist))
20163 ((memq 'org-link faces)
20165 (previous-single-property-change p 'face)
20166 (next-single-property-change p 'face)) clist))
20167 ((memq 'org-special-keyword faces)
20168 (push (list :keyword
20169 (previous-single-property-change p 'face)
20170 (next-single-property-change p 'face)) clist))
20172 (push (org-point-in-group p 0 :target) clist)
20173 (goto-char (1- (match-beginning 0)))
20174 (if (looking-at org-radio-target-regexp)
20175 (push (org-point-in-group p 0 :radio-target) clist))
20177 ((setq o (car (delq nil
20180 (if (memq x org-latex-fragment-image-overlays) x))
20181 (overlays-at (point))))))
20182 (push (list :latex-fragment
20183 (overlay-start o) (overlay-end o)) clist)
20184 (push (list :latex-preview
20185 (overlay-start o) (overlay-end o)) clist))
20186 ((org-inside-LaTeX-fragment-p)
20187 ;; FIXME: positions wrong.
20188 (push (list :latex-fragment (point) (point)) clist)))
20190 (setq clist (nreverse (delq nil clist)))
20193 ;; FIXME: Compare with at-regexp-p Do we need both?
20194 (defun org-in-regexp (re &optional nlines visually)
20195 "Check if point is inside a match of regexp.
20196 Normally only the current line is checked, but you can include NLINES extra
20197 lines both before and after point into the search.
20198 If VISUALLY is set, require that the cursor is not after the match but
20199 really on, so that the block visually is on the match."
20201 (let ((pos (point))
20202 (eol (point-at-eol (+ 1 (or nlines 0))))
20203 (inc (if visually 1 0)))
20205 (beginning-of-line (- 1 (or nlines 0)))
20206 (while (re-search-forward re eol t)
20207 (if (and (<= (match-beginning 0) pos)
20208 (>= (+ inc (match-end 0)) pos))
20209 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
20211 (defun org-at-regexp-p (regexp)
20212 "Is point inside a match of REGEXP in the current line?"
20215 (let ((pos (point)) (end (point-at-eol)))
20216 (beginning-of-line 1)
20217 (while (re-search-forward regexp end t)
20218 (if (and (<= (match-beginning 0) pos)
20219 (>= (match-end 0) pos))
20223 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
20224 "Non-nil when point is between matches of START-RE and END-RE.
20226 Also return a non-nil value when point is on one of the matches.
20228 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
20229 buffer positions. Default values are the positions of headlines
20230 surrounding the point.
20232 The functions returns a cons cell whose car (resp. cdr) is the
20233 position before START-RE (resp. after END-RE)."
20235 (let ((pos (point))
20236 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
20237 (limit-down (or lim-down (save-excursion (outline-next-heading))))
20240 ;; Point is on a block when on START-RE or if START-RE can be
20241 ;; found before it...
20242 (and (or (org-at-regexp-p start-re)
20243 (re-search-backward start-re limit-up t))
20244 (setq beg (match-beginning 0))
20245 ;; ... and END-RE after it...
20246 (goto-char (match-end 0))
20247 (re-search-forward end-re limit-down t)
20248 (> (setq end (match-end 0)) pos)
20249 ;; ... without another START-RE in-between.
20250 (goto-char (match-beginning 0))
20251 (not (re-search-backward start-re (1+ beg) t))
20253 (cons beg end))))))
20255 (defun org-in-block-p (names)
20256 "Non-nil when point belongs to a block whose name belongs to NAMES.
20258 NAMES is a list of strings containing names of blocks.
20260 Return first block name matched, or nil. Beware that in case of
20261 nested blocks, the returned name may not belong to the closest
20265 (let ((case-fold-search t)
20266 (lim-up (save-excursion (outline-previous-heading)))
20267 (lim-down (save-excursion (outline-next-heading))))
20268 (mapc (lambda (name)
20269 (let ((n (regexp-quote name)))
20270 (when (org-between-regexps-p
20271 (concat "^[ \t]*#\\+begin_" n)
20272 (concat "^[ \t]*#\\+end_" n)
20278 (defun org-occur-in-agenda-files (regexp &optional nlines)
20279 "Call `multi-occur' with buffers for all agenda files."
20280 (interactive "sOrg-files matching: \np")
20281 (let* ((files (org-agenda-files))
20282 (tnames (mapcar 'file-truename files))
20283 (extra org-agenda-text-search-extra-files)
20285 (when (eq (car extra) 'agenda-archives)
20286 (setq extra (cdr extra))
20287 (setq files (org-add-archive-files files)))
20288 (while (setq f (pop extra))
20289 (unless (member (file-truename f) tnames)
20290 (add-to-list 'files f 'append)
20291 (add-to-list 'tnames (file-truename f) 'append)))
20293 (mapcar (lambda (x)
20294 (with-current-buffer
20295 (or (get-file-buffer x) (find-file-noselect x))
20301 (if (boundp 'occur-mode-find-occurrence-hook)
20303 (add-hook 'occur-mode-find-occurrence-hook
20305 (when (derived-mode-p 'org-mode)
20308 (defadvice occur-mode-goto-occurrence
20309 (after org-occur-reveal activate)
20310 (and (derived-mode-p 'org-mode) (org-reveal)))
20311 (defadvice occur-mode-goto-occurrence-other-window
20312 (after org-occur-reveal activate)
20313 (and (derived-mode-p 'org-mode) (org-reveal)))
20314 (defadvice occur-mode-display-occurrence
20315 (after org-occur-reveal activate)
20316 (when (derived-mode-p 'org-mode)
20317 (let ((pos (occur-mode-find-occurrence)))
20318 (with-current-buffer (marker-buffer pos)
20323 (defun org-occur-link-in-agenda-files ()
20324 "Create a link and search for it in the agendas.
20325 The link is not stored in `org-stored-links', it is just created
20326 for the search purpose."
20328 (let ((link (condition-case nil
20329 (org-store-link nil)
20330 (error "Unable to create a link to here"))))
20331 (org-occur-in-agenda-files (regexp-quote link))))
20333 (defun org-uniquify (list)
20334 "Remove duplicate elements from LIST."
20336 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
20339 (defun org-delete-all (elts list)
20340 "Remove all elements in ELTS from LIST."
20342 (setq list (delete (pop elts) list)))
20345 (defun org-count (cl-item cl-seq)
20346 "Count the number of occurrences of ITEM in SEQ.
20347 Taken from `count' in cl-seq.el with all keyword arguments removed."
20348 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
20349 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
20350 (while (< cl-start cl-end)
20351 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
20352 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
20353 (setq cl-start (1+ cl-start)))
20356 (defun org-remove-if (predicate seq)
20357 "Remove everything from SEQ that fulfills PREDICATE."
20361 (if (not (funcall predicate e)) (push e res)))
20364 (defun org-remove-if-not (predicate seq)
20365 "Remove everything from SEQ that does not fulfill PREDICATE."
20369 (if (funcall predicate e) (push e res)))
20372 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20373 "Reduce two-argument FUNCTION across SEQ.
20374 Taken from `reduce' in cl-seq.el with all keyword arguments but
20375 \":initial-value\" removed."
20376 (let ((cl-accum (cond ((memq :initial-value cl-keys)
20377 (cadr (memq :initial-value cl-keys)))
20378 (cl-seq (pop cl-seq))
20379 (t (funcall cl-func)))))
20381 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20384 (defun org-back-over-empty-lines ()
20385 "Move backwards over whitespace, to the beginning of the first empty line.
20386 Returns the number of empty lines passed."
20387 (let ((pos (point)))
20388 (if (cdr (assoc 'heading org-blank-before-new-entry))
20389 (skip-chars-backward " \t\n\r")
20391 (forward-line -1)))
20392 (beginning-of-line 2)
20393 (goto-char (min (point) pos))
20394 (count-lines (point) pos)))
20396 (defun org-skip-whitespace ()
20397 (skip-chars-forward " \t\n\r"))
20399 (defun org-point-in-group (point group &optional context)
20400 "Check if POINT is in match-group GROUP.
20401 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20402 match. If the match group does not exist or point is not inside it,
20404 (and (match-beginning group)
20405 (>= point (match-beginning group))
20406 (<= point (match-end group))
20408 (list context (match-beginning group) (match-end group))
20411 (defun org-switch-to-buffer-other-window (&rest args)
20412 "Switch to buffer in a second window on the current frame.
20413 In particular, do not allow pop-up frames.
20414 Returns the newly created buffer."
20415 (let (pop-up-frames special-display-buffer-names special-display-regexps
20416 special-display-function)
20417 (apply 'switch-to-buffer-other-window args)))
20419 (defun org-combine-plists (&rest plists)
20420 "Create a single property list from all plists in PLISTS.
20421 The process starts by copying the first list, and then setting properties
20422 from the other lists. Settings in the last list are the most significant
20423 ones and overrule settings in the other lists."
20424 (let ((rtn (copy-sequence (pop plists)))
20427 (setq ls (pop plists))
20429 (setq p (pop ls) v (pop ls))
20430 (setq rtn (plist-put rtn p v))))
20433 (defun org-replace-escapes (string table)
20434 "Replace %-escapes in STRING with values in TABLE.
20435 TABLE is an association list with keys like \"%a\" and string values.
20436 The sequences in STRING may contain normal field width and padding information,
20437 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20438 so values can contain further %-escapes if they are define later in TABLE."
20439 (let ((tbl (copy-alist table))
20440 (case-fold-search nil)
20443 (while (setq e (pop tbl))
20444 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20445 (when (and (cdr e) (string-match re (cdr e)))
20446 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20448 (add-text-properties 0 3 (list 'sref sref) safe)
20449 (setcdr e (replace-match safe t t (cdr e)))))
20450 (while (string-match re string)
20451 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20453 (setq string (replace-match rpl t t string))))
20454 (while (setq pchg (next-property-change pchg string))
20455 (let ((sref (get-text-property pchg 'sref string)))
20456 (when (and sref (string-match "SREF" string pchg))
20457 (setq string (replace-match sref t t string)))))
20460 (defun org-sublist (list start end)
20461 "Return a section of LIST, from START to END.
20462 Counting starts at 1."
20463 (let (rtn (c start))
20464 (setq list (nthcdr (1- start) list))
20465 (while (and list (<= c end))
20466 (push (pop list) rtn)
20470 (defun org-find-base-buffer-visiting (file)
20471 "Like `find-buffer-visiting' but always return the base buffer and
20472 not an indirect buffer."
20473 (let ((buf (or (get-file-buffer file)
20474 (find-buffer-visiting file))))
20476 (or (buffer-base-buffer buf) buf)
20479 (defun org-image-file-name-regexp (&optional extensions)
20480 "Return regexp matching the file names of images.
20481 If EXTENSIONS is given, only match these."
20482 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20483 (image-file-name-regexp)
20484 (let ((image-file-name-extensions
20486 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20487 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20489 (regexp-opt (nconc (mapcar 'upcase
20490 image-file-name-extensions)
20491 image-file-name-extensions)
20495 (defun org-file-image-p (file &optional extensions)
20496 "Return non-nil if FILE is an image."
20498 (string-match (org-image-file-name-regexp extensions) file)))
20500 (defun org-get-cursor-date ()
20501 "Return the date at cursor in as a time.
20502 This works in the calendar and in the agenda, anywhere else it just
20503 returns the current time."
20504 (let (date day defd)
20506 ((eq major-mode 'calendar-mode)
20507 (setq date (calendar-cursor-to-date)
20508 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20509 ((eq major-mode 'org-agenda-mode)
20510 (setq day (get-text-property (point) 'day))
20512 (setq date (calendar-gregorian-from-absolute day)
20513 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20515 (or defd (current-time))))
20517 (defvar org-agenda-action-marker (make-marker)
20518 "Marker pointing to the entry for the next agenda action.")
20520 (defun org-mark-entry-for-agenda-action ()
20521 "Mark the current entry as target of an agenda action.
20522 Agenda actions are actions executed from the agenda with the key `k',
20523 which make use of the date at the cursor."
20525 (move-marker org-agenda-action-marker
20526 (save-excursion (org-back-to-heading t) (point))
20529 "Entry marked for action; press `k' at desired date in agenda or calendar"))
20531 (defun org-mark-subtree ()
20532 "Mark the current subtree.
20533 This puts point at the start of the current subtree, and mark at the end.
20535 If point is in an inline task, mark that task instead."
20537 (let ((inline-task-p
20538 (and (featurep 'org-inlinetask)
20539 (org-inlinetask-in-task-p)))
20541 ;; Get beginning of subtree
20543 (inline-task-p (org-inlinetask-goto-beginning))
20544 ((org-at-heading-p) (beginning-of-line))
20545 (t (org-with-limited-levels (outline-previous-visible-heading 1))))
20549 (org-inlinetask-goto-end)
20550 (org-end-of-subtree))
20552 (push-mark (point) nil t)
20555 ;;; Paragraph filling stuff.
20556 ;; We want this to be just right, so use the full arsenal.
20558 (defun org-indent-line ()
20559 "Indent line depending on context."
20561 (let* ((pos (point))
20562 (itemp (org-at-item-p))
20563 (case-fold-search t)
20564 (org-drawer-regexp (or org-drawer-regexp "\000"))
20565 (inline-task-p (and (featurep 'org-inlinetask)
20566 (org-inlinetask-in-task-p)))
20567 (inline-re (and inline-task-p
20568 (org-inlinetask-outline-regexp)))
20570 (beginning-of-line 1)
20573 ((looking-at "# ") (setq column 0))
20575 ((looking-at org-outline-regexp) (setq column 0))
20577 ((looking-at "#\\+include:") (setq column 0))
20578 ;; Footnote definition
20579 ((looking-at org-footnote-definition-re) (setq column 0))
20580 ;; Literal examples
20581 ((looking-at "[ \t]*:\\( \\|$\\)")
20582 (setq column (org-get-indentation))) ; do nothing
20584 ((ignore-errors (goto-char (org-in-item-p)))
20585 (setq column (if itemp
20586 (org-get-indentation)
20587 (org-list-item-body-column (point))))
20590 ((and (looking-at "[ \t]*:END:")
20591 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20593 (goto-char (1- (match-beginning 1)))
20594 (setq column (current-column))))
20596 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20598 (re-search-backward
20599 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20600 (setq column (org-get-indentation (match-string 0))))
20601 ((and (not (looking-at "[ \t]*#\\+begin_"))
20602 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20604 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20606 (cond ((equal (downcase (match-string 1)) "src")
20607 ;; src blocks: let `org-edit-src-exit' handle them
20608 (org-get-indentation))
20609 ((equal (downcase (match-string 1)) "example")
20610 (max (org-get-indentation)
20611 (org-get-indentation (match-string 0))))
20613 (org-get-indentation (match-string 0))))))
20614 ;; This line has nothing special, look at the previous relevant
20615 ;; line to compute indentation
20617 (beginning-of-line 0)
20618 (while (and (not (bobp))
20619 (not (looking-at org-drawer-regexp))
20620 ;; When point started in an inline task, do not move
20621 ;; above task starting line.
20622 (not (and inline-task-p (looking-at inline-re)))
20623 ;; Skip drawers, blocks, empty lines, verbatim,
20624 ;; comments, tables, footnotes definitions, lists,
20626 (or (and (looking-at "[ \t]*:END:")
20627 (re-search-backward org-drawer-regexp nil t))
20628 (and (looking-at "[ \t]*#\\+end_")
20629 (re-search-backward "[ \t]*#\\+begin_"nil t))
20630 (looking-at "[ \t]*[\n:#|]")
20631 (looking-at org-footnote-definition-re)
20632 (and (ignore-errors (goto-char (org-in-item-p)))
20634 (org-list-get-top-point (org-list-struct))))
20635 (and (not inline-task-p)
20636 (featurep 'org-inlinetask)
20637 (org-inlinetask-in-task-p)
20638 (or (org-inlinetask-goto-beginning) t))))
20639 (beginning-of-line 0))
20641 ;; There was an heading above.
20642 ((looking-at "\\*+[ \t]+")
20643 (if (not org-adapt-indentation)
20645 (goto-char (match-end 0))
20646 (setq column (current-column))))
20647 ;; A drawer had started and is unfinished
20648 ((looking-at org-drawer-regexp)
20649 (goto-char (1- (match-beginning 1)))
20650 (setq column (current-column)))
20651 ;; Else, nothing noticeable found: get indentation and go on.
20652 (t (setq column (org-get-indentation))))))
20653 ;; Now apply indentation and move cursor accordingly
20655 (if (<= (current-column) (current-indentation))
20656 (org-indent-line-to column)
20657 (save-excursion (org-indent-line-to column)))
20658 ;; Special polishing for properties, see `org-property-format'
20659 (setq column (current-column))
20660 (beginning-of-line 1)
20662 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20663 (replace-match (concat (match-string 1)
20664 (format org-property-format
20665 (match-string 2) (match-string 3)))
20667 (org-move-to-column column)
20668 (when (and orgstruct-is-++ (eq pos (point)))
20669 (org-let org-fb-vars
20670 '(indent-according-to-mode)))))
20672 (defun org-indent-drawer ()
20673 "Indent the drawer at point."
20676 (e (and (save-excursion (re-search-forward ":END:" nil t))
20681 (when (overlays-at (point))
20682 (member 'invisible (overlay-properties
20683 (car (overlays-at (point)))))))))
20684 (when folded (org-cycle))
20685 (indent-for-tab-command)
20686 (while (and (move-beginning-of-line 2) (< (point) e))
20687 (indent-for-tab-command))
20689 (when folded (org-cycle)))
20690 (message "Drawer at point indented"))
20692 (defun org-indent-block ()
20693 "Indent the block at point."
20696 (case-fold-search t)
20697 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20702 (when (overlays-at (point))
20703 (member 'invisible (overlay-properties
20704 (car (overlays-at (point)))))))))
20705 (when folded (org-cycle))
20706 (indent-for-tab-command)
20707 (while (and (move-beginning-of-line 2) (< (point) e))
20708 (indent-for-tab-command))
20710 (when folded (org-cycle)))
20711 (message "Block at point indented"))
20713 (defun org-indent-region (start end)
20717 (let ((line-end (org-current-line end)))
20719 (while (< (org-current-line) line-end)
20720 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
20721 (t (call-interactively 'org-indent-line)))
20722 (move-beginning-of-line 2)))))
20724 (defun org-set-autofill-regexps ()
20726 ;; We use our own fill-paragraph and auto-fill functions. These
20727 ;; functions will shadow `fill-prefix' (computed internally with
20728 ;; `org-fill-context-prefix') and pass through to
20729 ;; `fill-region-as-paragraph' and `do-auto-fill' as appropriate.
20730 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20731 ;; Prevent auto-fill from inserting unwanted new items.
20732 (when (boundp 'fill-nobreak-predicate)
20734 'fill-nobreak-predicate
20736 (append fill-nobreak-predicate
20737 '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
20738 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20739 (org-set-local 'align-mode-rules-list
20740 '((org-in-buffer-settings
20741 (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
20742 (modes . '(org-mode))))))
20744 (defun org-fill-item-nobreak-p ()
20745 "Non-nil when a line break at point would insert a new item."
20746 (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
20748 (defun org-fill-line-break-nobreak-p ()
20749 "Non-nil when a line break at point would create an Org line break."
20751 (skip-chars-backward "[ \t]")
20752 (skip-chars-backward "\\\\")
20753 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
20755 (defun org-fill-context-prefix (p)
20756 "Compute a fill prefix for the line at point P.
20757 Return fill prefix, as a string, or nil if current line isn't
20758 meant to be filled."
20760 (let* ((elements (org-element-at-point t))
20761 (element (car elements))
20762 (type (org-element-type element))
20765 (goto-char (org-element-property :begin element))
20766 (while (looking-at org-element--affiliated-re) (forward-line))
20768 (unless (< p post-affiliated)
20770 (beginning-of-line)
20772 (comment (looking-at "[ \t]*#\\+? ?") (match-string 0))
20774 (make-string (org-list-item-body-column
20775 (org-element-property :begin element))
20778 ;; Fill prefix is usually the same as the current line,
20779 ;; except if the paragraph is at the beginning of an item.
20780 (let ((parent (cadr elements)))
20781 (if (eq (org-element-type parent) 'item)
20782 (make-string (org-list-item-body-column
20783 (org-element-property :begin parent))
20785 (if (looking-at "\\s-+") (match-string 0) ""))))
20786 ((comment-block verse-block)
20787 ;; Only fill contents if P is within block boundaries.
20788 (let* ((cbeg (save-excursion (goto-char post-affiliated)
20791 (cend (save-excursion
20792 (goto-char (org-element-property :end element))
20793 (skip-chars-backward " \r\t\n")
20794 (line-beginning-position))))
20795 (when (and (>= p cbeg) (< p cend))
20796 (if (looking-at "\\s-+") (match-string 0) "")))))))))
20798 (defun org-fill-paragraph (&optional justify)
20799 "Fill element at point, when applicable.
20801 This function only applies to comment blocks, comments, example
20802 blocks, paragraphs and verse blocks. Also, as a special case,
20803 re-align table when point is at one.
20805 If JUSTIFY is non-nil (interactively, with prefix argument),
20806 justify as well. If `sentence-end-double-space' is non-nil, then
20807 period followed by one space does not end a sentence, so don't
20808 break a line there. The variable `fill-column' controls the
20809 width for filling."
20810 (let ((element (org-element-at-point)))
20811 ;; First check if point is in a blank line at the beginning of the
20812 ;; buffer. In that case, ignore filling.
20813 (if (< (point) (org-element-property :begin element)) t
20814 (case (org-element-type element)
20815 ;; Align Org tables, leave table.el tables as-is.
20816 (table-row (org-table-align) t)
20818 (when (eq (org-element-property :type element) 'org) (org-table-align))
20820 ;; Elements that may contain `line-break' type objects.
20821 ((paragraph verse-block)
20822 (let ((beg (org-element-property :contents-begin element))
20823 (end (org-element-property :contents-end element)))
20824 ;; Do nothing if point is at an affiliated keyword or at
20825 ;; verse block markers.
20826 (if (or (< (point) beg) (>= (point) end)) t
20827 ;; At a verse block, first narrow to current "paragraph"
20828 ;; and set current element to that paragraph.
20830 (when (eq (org-element-type element) 'verse-block)
20831 (narrow-to-region beg end)
20834 (let ((bol-pos (point-at-bol)))
20835 (re-search-backward org-element-paragraph-separate nil 'm)
20836 (unless (or (bobp) (= (point-at-bol) bol-pos))
20838 (setq element (org-element-paragraph-parser end)
20839 beg (org-element-property :contents-begin element)
20840 end (org-element-property :contents-end element)))))
20841 ;; Fill paragraph, taking line breaks into consideration.
20842 ;; For that, slice the paragraph using line breaks as
20843 ;; separators, and fill the parts in reverse order to
20844 ;; avoid messing with markers.
20849 (let ((fill-prefix (org-fill-context-prefix pos)))
20850 (fill-region-as-paragraph pos (point) justify))
20852 ;; Find the list of ending positions for line breaks
20853 ;; in the current paragraph. Add paragraph beginning
20854 ;; to include first slice.
20858 (org-element--parse-objects
20859 beg end nil org-element-all-objects)
20861 (lambda (lb) (org-element-property :end lb))))))))
20863 ;; Contents of `comment-block' type elements should be filled as
20866 (let ((fill-prefix (org-fill-context-prefix (point))))
20868 (fill-region-as-paragraph
20870 (goto-char (org-element-property :begin element))
20871 (while (looking-at org-element--affiliated-re) (forward-line))
20875 (goto-char (org-element-property :end element))
20876 (skip-chars-backward " \r\t\n")
20877 (line-beginning-position))
20880 (let ((fill-prefix (org-fill-context-prefix (point))))
20882 (fill-region-as-paragraph
20884 (goto-char (org-element-property :begin element))
20885 (while (looking-at org-element--affiliated-re) (forward-line))
20888 (goto-char (org-element-property :end element))
20889 (skip-chars-backward " \r\t\n")
20890 (line-end-position))))))
20891 ;; Ignore every other element.
20894 (defun org-auto-fill-function ()
20895 "Auto-fill function."
20896 ;; Check if auto-filling is meaningful before computing fill prefix.
20897 (let ((fc (current-fill-column)))
20898 (when (and fc (> (current-column) fc))
20899 (let ((fill-prefix (org-fill-context-prefix (point))))
20901 (if orgstruct-is-++ (org-let org-fb-vars '(do-auto-fill))
20902 (do-auto-fill)))))))
20906 (defun org-toggle-fixed-width-section (arg)
20907 "Toggle the fixed-width export.
20908 If there is no active region, the QUOTE keyword at the current headline is
20909 inserted or removed. When present, it causes the text between this headline
20910 and the next to be exported as fixed-width text, and unmodified.
20911 If there is an active region, this command adds or removes a colon as the
20912 first character of this line. If the first character of a line is a colon,
20913 this line is also exported in fixed-width font."
20916 (regionp (org-region-active-p))
20917 (beg (if regionp (region-beginning) (point)))
20918 (end (if regionp (region-end)))
20919 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
20920 (case-fold-search nil)
20921 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
20926 (setq cc (current-column))
20927 (beginning-of-line 1)
20928 (setq off (looking-at re))
20929 (while (> nlines 0)
20930 (setq nlines (1- nlines))
20931 (beginning-of-line 1)
20934 (org-move-to-column cc t)
20937 ((and off (looking-at re))
20938 (replace-match "" t t nil 1))
20939 ((not off) (org-move-to-column cc t) (insert ": ")))
20942 (org-back-to-heading)
20944 ((looking-at (format org-heading-keyword-regexp-format
20946 (goto-char (match-end 1))
20947 (looking-at (concat " +" org-quote-string))
20948 (replace-match "" t t)
20949 (when (eolp) (insert " ")))
20950 ((looking-at org-outline-regexp)
20951 (goto-char (match-end 0))
20952 (insert org-quote-string " ")))))))
20954 (defun org-reftex-citation ()
20955 "Use reftex-citation to insert a citation into the buffer.
20956 This looks for a line like
20958 #+BIBLIOGRAPHY: foo plain option:-d
20960 and derives from it that foo.bib is the bibliography file relevant
20961 for this document. It then installs the necessary environment for RefTeX
20962 to work in this buffer and calls `reftex-citation' to insert a citation
20965 Export of such citations to both LaTeX and HTML is handled by the contributed
20966 package org-exp-bibtex by Taru Karttunen."
20968 (let ((reftex-docstruct-symbol 'rds)
20969 (reftex-cite-format "\\cite{%l}")
20974 (let ((case-fold-search t)
20975 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
20976 (if (not (save-excursion
20977 (or (re-search-forward re nil t)
20978 (re-search-backward re nil t))))
20979 (error "No bibliography defined in file")
20980 (setq bib (concat (match-string 1) ".bib")
20981 rds (list (list 'bib bib)))))))
20982 (call-interactively 'reftex-citation)))
20984 ;;;; Functions extending outline functionality
20986 (defun org-beginning-of-line (&optional arg)
20987 "Go to the beginning of the current line. If that is invisible, continue
20988 to a visible line beginning. This makes the function of C-a more intuitive.
20989 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
20990 first attempt, and only move to after the tags when the cursor is already
20991 beyond the end of the headline."
20993 (let ((pos (point))
20994 (special (if (consp org-special-ctrl-a/e)
20995 (car org-special-ctrl-a/e)
20996 org-special-ctrl-a/e))
20998 (if (org-bound-and-true-p line-move-visual)
20999 (beginning-of-visual-line 1)
21000 (beginning-of-line 1))
21001 (if (and arg (fboundp 'move-beginning-of-line))
21002 (call-interactively 'move-beginning-of-line)
21006 (if (org-truely-invisible-p)
21007 (while (and (not (bobp)) (org-truely-invisible-p))
21009 (beginning-of-line 1))
21010 (forward-char 1))))
21013 ((and (looking-at org-complex-heading-regexp)
21014 (= (char-after (match-end 1)) ?\ ))
21015 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
21019 (cond ((> pos refpos) refpos)
21020 ((= pos (point)) refpos)
21022 (cond ((> pos (point)) (point))
21023 ((not (eq last-command this-command)) (point))
21026 ;; Being at an item and not looking at an the item means point
21027 ;; was previously moved to beginning of a visual line, which
21028 ;; doesn't contain the item. Therefore, do nothing special,
21030 (when (looking-at org-list-full-item-re)
21031 ;; Set special position at first white space character after
21032 ;; bullet, and check-box, if any.
21033 (let ((after-bullet
21034 (let ((box (match-end 3)))
21035 (if (not box) (match-end 1)
21036 (let ((after (char-after box)))
21037 (if (and after (= after ? )) (1+ box) box))))))
21038 ;; Special case: Move point to special position when
21039 ;; currently after it or at beginning of line.
21041 (when (or (> pos after-bullet) (= (point) pos))
21042 (goto-char after-bullet))
21043 ;; Reversed case: Move point to special position when
21044 ;; point was already at beginning of line and command is
21046 (when (and (= (point) pos) (eq last-command this-command))
21047 (goto-char after-bullet))))))))
21049 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21051 (defun org-end-of-line (&optional arg)
21052 "Go to the end of the line.
21053 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21054 first attempt, and only move to after the tags when the cursor is already
21055 beyond the end of the headline."
21057 (let ((special (if (consp org-special-ctrl-a/e)
21058 (cdr org-special-ctrl-a/e)
21059 org-special-ctrl-a/e)))
21061 ((or (not special) arg
21062 (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
21063 (call-interactively
21064 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
21065 ((fboundp 'move-end-of-line) 'move-end-of-line)
21066 (t 'end-of-line))))
21067 ((org-at-heading-p)
21068 (let ((pos (point)))
21069 (beginning-of-line 1)
21070 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
21072 (if (or (< pos (match-beginning 1))
21073 (= pos (match-end 0)))
21074 (goto-char (match-beginning 1))
21075 (goto-char (match-end 0)))
21076 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
21077 (goto-char (match-end 0))
21078 (goto-char (match-beginning 1))))
21079 (call-interactively (if (fboundp 'move-end-of-line)
21083 (move-end-of-line 1)
21084 (when (overlays-at (1- (point))) (backward-char 1)))
21085 ;; At an item: Move before any hidden text.
21086 (t (call-interactively
21087 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
21088 ((fboundp 'move-end-of-line) 'move-end-of-line)
21089 (t 'end-of-line)))))
21091 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21093 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
21094 (define-key org-mode-map "\C-e" 'org-end-of-line)
21096 (defun org-backward-sentence (&optional arg)
21097 "Go to beginning of sentence, or beginning of table field.
21098 This will call `backward-sentence' or `org-table-beginning-of-field',
21099 depending on context."
21102 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
21103 (t (call-interactively 'backward-sentence))))
21105 (defun org-forward-sentence (&optional arg)
21106 "Go to end of sentence, or end of table field.
21107 This will call `forward-sentence' or `org-table-end-of-field',
21108 depending on context."
21111 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
21112 (t (call-interactively 'forward-sentence))))
21114 (define-key org-mode-map "\M-a" 'org-backward-sentence)
21115 (define-key org-mode-map "\M-e" 'org-forward-sentence)
21117 (defun org-kill-line (&optional arg)
21118 "Kill line, to tags or end of line."
21121 ((or (not org-special-ctrl-k)
21123 (not (org-at-heading-p)))
21124 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
21125 org-ctrl-k-protect-subtree)
21126 (if (or (eq org-ctrl-k-protect-subtree 'error)
21127 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
21128 (error "C-k aborted - would kill hidden subtree")))
21129 (call-interactively
21130 (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
21131 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
21132 (kill-region (point) (match-beginning 1))
21133 (org-set-tags nil t))
21134 (t (kill-region (point) (point-at-eol)))))
21136 (define-key org-mode-map "\C-k" 'org-kill-line)
21138 (defun org-yank (&optional arg)
21139 "Yank. If the kill is a subtree, treat it specially.
21140 This command will look at the current kill and check if is a single
21141 subtree, or a series of subtrees[1]. If it passes the test, and if the
21142 cursor is at the beginning of a line or after the stars of a currently
21143 empty headline, then the yank is handled specially. How exactly depends
21144 on the value of the following variables, both set by default.
21146 org-yank-folded-subtrees
21147 When set, the subtree(s) will be folded after insertion, but only
21148 if doing so would now swallow text after the yanked text.
21150 org-yank-adjusted-subtrees
21151 When set, the subtree will be promoted or demoted in order to
21152 fit into the local outline tree structure, which means that the level
21153 will be adjusted so that it becomes the smaller one of the two
21154 *visible* surrounding headings.
21156 Any prefix to this command will cause `yank' to be called directly with
21157 no special treatment. In particular, a simple \\[universal-argument] prefix \
21159 plainly yank the text as it is.
21161 \[1] The test checks if the first non-white line is a heading
21162 and if there are no other headings with fewer stars."
21164 (org-yank-generic 'yank arg))
21166 (defun org-yank-generic (command arg)
21167 "Perform some yank-like command.
21169 This function implements the behavior described in the `org-yank'
21170 documentation. However, it has been generalized to work for any
21171 interactive command with similar behavior."
21173 ;; pretend to be command COMMAND
21174 (setq this-command command)
21177 (call-interactively command)
21179 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
21180 (and (org-kill-is-subtree-p)
21182 (and (looking-at "[ \t]*$")
21185 (buffer-substring (point-at-bol) (point)))))))
21188 ((and subtreep org-yank-folded-subtrees)
21189 (let ((beg (point))
21191 (if (and subtreep org-yank-adjusted-subtrees)
21192 (org-paste-subtree nil nil 'for-yank)
21193 (call-interactively command))
21197 (when (and (bolp) subtreep
21198 (not (setq swallowp
21199 (org-yank-folding-would-swallow-text beg end))))
21200 (org-with-limited-levels
21201 (or (looking-at org-outline-regexp)
21202 (re-search-forward org-outline-regexp-bol end t))
21203 (while (and (< (point) end) (looking-at org-outline-regexp))
21205 (org-cycle-show-empty-lines 'folded)
21206 (condition-case nil
21207 (outline-forward-same-level 1)
21208 (error (goto-char end))))))
21211 "Inserted text not folded because that would swallow text"))
21214 (skip-chars-forward " \t\n\r")
21215 (beginning-of-line 1)
21216 (push-mark beg 'nomsg)))
21217 ((and subtreep org-yank-adjusted-subtrees)
21218 (let ((beg (point-at-bol)))
21219 (org-paste-subtree nil nil 'for-yank)
21220 (push-mark beg 'nomsg)))
21222 (call-interactively command))))))
21224 (defun org-yank-folding-would-swallow-text (beg end)
21225 "Would hide-subtree at BEG swallow any text after END?"
21227 (org-with-limited-levels
21230 (when (or (looking-at org-outline-regexp)
21231 (re-search-forward org-outline-regexp-bol end t))
21232 (setq level (org-outline-level)))
21234 (skip-chars-forward " \t\r\n\v\f")
21236 (and (bolp) (looking-at org-outline-regexp)
21237 (<= (org-outline-level) level)))
21238 nil ; Nothing would be swallowed
21239 t))))) ; something would swallow
21241 (define-key org-mode-map "\C-y" 'org-yank)
21243 (defun org-truely-invisible-p ()
21244 "Check if point is at a character currently not visible.
21245 This version does not only check the character property, but also
21247 ;; Early versions of noutline don't have `outline-invisible-p'.
21248 (if (org-bound-and-true-p visible-mode)
21250 (outline-invisible-p)))
21252 (defun org-invisible-p2 ()
21253 "Check if point is at a character currently not visible."
21255 (if (and (eolp) (not (bobp))) (backward-char 1))
21256 ;; Early versions of noutline don't have `outline-invisible-p'.
21257 (outline-invisible-p)))
21259 (defun org-back-to-heading (&optional invisible-ok)
21260 "Call `outline-back-to-heading', but provide a better error message."
21261 (condition-case nil
21262 (outline-back-to-heading invisible-ok)
21263 (error (error "Before first headline at position %d in buffer %s"
21264 (point) (current-buffer)))))
21266 (defun org-beginning-of-defun ()
21267 "Go to the beginning of the subtree, i.e. back to the heading."
21268 (org-back-to-heading))
21269 (defun org-end-of-defun ()
21270 "Go to the end of the subtree."
21271 (org-end-of-subtree nil t))
21273 (defun org-before-first-heading-p ()
21274 "Before first heading?"
21277 (null (re-search-backward org-outline-regexp-bol nil t))))
21279 (defun org-at-heading-p (&optional ignored)
21280 (outline-on-heading-p t))
21281 ;; Compatibility alias with Org versions < 7.8.03
21282 (defalias 'org-on-heading-p 'org-at-heading-p)
21284 (defun org-at-comment-p nil
21285 "Is cursor in a line starting with a # character?"
21287 (beginning-of-line)
21288 (looking-at "^#")))
21290 (defun org-at-drawer-p nil
21291 "Is cursor at a drawer keyword?"
21293 (move-beginning-of-line 1)
21294 (looking-at org-drawer-regexp)))
21296 (defun org-at-block-p nil
21297 "Is cursor at a block keyword?"
21299 (move-beginning-of-line 1)
21300 (looking-at org-block-regexp)))
21302 (defun org-point-at-end-of-empty-headline ()
21303 "If point is at the end of an empty headline, return t, else nil.
21304 If the heading only contains a TODO keyword, it is still still considered
21306 (and (looking-at "[ \t]*$")
21307 (when org-todo-line-regexp
21309 (beginning-of-line 1)
21310 (let ((case-fold-search nil))
21311 (looking-at org-todo-line-regexp)
21312 (string= (match-string 3) ""))))))
21314 (defun org-at-heading-or-item-p ()
21315 (or (org-at-heading-p) (org-at-item-p)))
21317 (defun org-at-target-p ()
21318 (or (org-in-regexp org-radio-target-regexp)
21319 (org-in-regexp org-target-regexp)))
21320 ;; Compatibility alias with Org versions < 7.8.03
21321 (defalias 'org-on-target-p 'org-at-target-p)
21323 (defun org-up-heading-all (arg)
21324 "Move to the heading line of which the present line is a subheading.
21325 This function considers both visible and invisible heading lines.
21326 With argument, move up ARG levels."
21327 (if (fboundp 'outline-up-heading-all)
21328 (outline-up-heading-all arg) ; emacs 21 version of outline.el
21329 (outline-up-heading arg t))) ; emacs 22 version of outline.el
21331 (defun org-up-heading-safe ()
21332 "Move to the heading line of which the present line is a subheading.
21333 This version will not throw an error. It will return the level of the
21334 headline found, or nil if no higher level is found.
21336 Also, this function will be a lot faster than `outline-up-heading',
21337 because it relies on stars being the outline starters. This can really
21338 make a significant difference in outlines with very many siblings."
21339 (let (start-level re)
21340 (org-back-to-heading t)
21341 (setq start-level (funcall outline-level))
21342 (if (equal start-level 1)
21344 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
21345 (if (re-search-backward re nil t)
21346 (funcall outline-level)))))
21348 (defun org-first-sibling-p ()
21349 "Is this heading the first child of its parents?"
21351 (let ((re org-outline-regexp-bol)
21353 (unless (org-at-heading-p t)
21354 (error "Not at a heading"))
21355 (setq level (funcall outline-level))
21357 (if (not (re-search-backward re nil t))
21359 (setq l (funcall outline-level))
21362 (defun org-goto-sibling (&optional previous)
21363 "Goto the next sibling, even if it is invisible.
21364 When PREVIOUS is set, go to the previous sibling instead. Returns t
21365 when a sibling was found. When none is found, return nil and don't
21367 (let ((fun (if previous 're-search-backward 're-search-forward))
21369 (re org-outline-regexp-bol)
21371 (when (condition-case nil (org-back-to-heading t) (error nil))
21372 (setq level (funcall outline-level))
21374 (or previous (forward-char 1))
21375 (while (funcall fun re nil t)
21376 (setq l (funcall outline-level))
21377 (when (< l level) (goto-char pos) (throw 'exit nil))
21378 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21382 (defun org-show-siblings ()
21383 "Show all siblings of the current headline."
21385 (while (org-goto-sibling) (org-flag-heading nil)))
21387 (while (org-goto-sibling 'previous)
21388 (org-flag-heading nil))))
21390 (defun org-goto-first-child ()
21391 "Goto the first child, even if it is invisible.
21392 Return t when a child was found. Otherwise don't move point and
21394 (let (level (pos (point)) (re org-outline-regexp-bol))
21395 (when (condition-case nil (org-back-to-heading t) (error nil))
21396 (setq level (outline-level))
21398 (if (and (re-search-forward re nil t) (> (outline-level) level))
21399 (progn (goto-char (match-beginning 0)) t)
21400 (goto-char pos) nil))))
21402 (defun org-show-hidden-entry ()
21403 "Show an entry where even the heading is hidden."
21407 (defun org-flag-heading (flag &optional entry)
21408 "Flag the current heading. FLAG non-nil means make invisible.
21409 When ENTRY is non-nil, show the entire entry."
21411 (org-back-to-heading t)
21412 ;; Check if we should show the entire entry
21417 (and (outline-next-heading)
21418 (org-flag-heading nil))))
21419 (outline-flag-region (max (point-min) (1- (point)))
21420 (save-excursion (outline-end-of-heading) (point))
21423 (defun org-get-next-sibling ()
21424 "Move to next heading of the same level, and return point.
21425 If there is no such heading, return nil.
21426 This is like outline-next-sibling, but invisible headings are ok."
21427 (let ((level (funcall outline-level)))
21428 (outline-next-heading)
21429 (while (and (not (eobp)) (> (funcall outline-level) level))
21430 (outline-next-heading))
21431 (if (or (eobp) (< (funcall outline-level) level))
21435 (defun org-get-last-sibling ()
21436 "Move to previous heading of the same level, and return point.
21437 If there is no such heading, return nil."
21438 (let ((opoint (point))
21439 (level (funcall outline-level)))
21440 (outline-previous-heading)
21441 (when (and (/= (point) opoint) (outline-on-heading-p t))
21442 (while (and (> (funcall outline-level) level)
21444 (outline-previous-heading))
21445 (if (< (funcall outline-level) level)
21449 (defun org-end-of-subtree (&optional invisible-OK to-heading)
21450 ;; This contains an exact copy of the original function, but it uses
21451 ;; `org-back-to-heading', to make it work also in invisible
21452 ;; trees. And is uses an invisible-OK argument.
21453 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21454 ;; Furthermore, when used inside Org, finding the end of a large subtree
21455 ;; with many children and grandchildren etc, this can be much faster
21456 ;; than the outline version.
21457 (org-back-to-heading invisible-OK)
21459 (level (funcall outline-level)))
21460 (if (and (derived-mode-p 'org-mode) (< level 1000))
21461 ;; A true heading (not a plain list item), in Org-mode
21462 ;; This means we can easily find the end by looking
21463 ;; only for the right number of stars. Using a regexp to do
21464 ;; this is so much faster than using a Lisp loop.
21465 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21467 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21468 ;; something else, do it the slow way
21469 (while (and (not (eobp))
21470 (or first (> (funcall outline-level) level)))
21472 (outline-next-heading)))
21474 (if (memq (preceding-char) '(?\n ?\^M))
21476 ;; Go to end of line before heading
21478 (if (memq (preceding-char) '(?\n ?\^M))
21479 ;; leave blank line before heading
21480 (forward-char -1))))))
21483 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21484 "Use Org version in org-mode, for dramatic speed-up."
21485 (if (derived-mode-p 'org-mode)
21487 (org-end-of-subtree nil t)
21488 (unless (eobp) (backward-char 1)))
21491 (defun org-end-of-meta-data-and-drawers ()
21492 "Jump to the first text after meta data and drawers in the current entry.
21493 This will move over empty lines, lines with planning time stamps,
21494 clocking lines, and drawers."
21495 (org-back-to-heading t)
21496 (let ((end (save-excursion (outline-next-heading) (point)))
21497 (re (concat "\\(" org-drawer-regexp "\\)"
21498 "\\|" "[ \t]*" org-keyword-time-regexp)))
21500 (while (re-search-forward re end t)
21501 (if (not (match-end 1))
21502 ;; empty or planning line
21504 ;; a drawer, find the end
21505 (re-search-forward "^[ \t]*:END:" end 'move)
21507 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21510 (defun org-forward-same-level (arg &optional invisible-ok)
21511 "Move forward to the arg'th subheading at same level as this one.
21512 Stop at the first and last subheadings of a superior heading.
21513 Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
21514 it wil also look at invisible ones."
21516 (org-back-to-heading invisible-ok)
21518 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21519 (re (format "^\\*\\{1,%d\\} " level))
21523 (while (and (re-search-forward re nil 'move)
21524 (setq l (- (match-end 0) (match-beginning 0) 1))
21527 (progn (backward-char 1) (outline-invisible-p)))
21528 (if (< l level) (setq arg 1)))
21529 (setq arg (1- arg)))
21530 (beginning-of-line 1)))
21532 (defun org-backward-same-level (arg &optional invisible-ok)
21533 "Move backward to the arg'th subheading at same level as this one.
21534 Stop at the first and last subheadings of a superior heading."
21536 (org-back-to-heading)
21538 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21539 (re (format "^\\*\\{1,%d\\} " level))
21542 (while (and (re-search-backward re nil 'move)
21543 (setq l (- (match-end 0) (match-beginning 0) 1))
21546 (outline-invisible-p))
21547 (if (< l level) (setq arg 1)))
21548 (setq arg (1- arg)))))
21550 (defun org-show-subtree ()
21551 "Show everything after this heading at deeper levels."
21553 (outline-flag-region
21556 (org-end-of-subtree t t))
21559 (defun org-show-entry ()
21560 "Show the body directly following this heading.
21561 Show the heading too, if it is currently invisible."
21564 (condition-case nil
21566 (org-back-to-heading t)
21567 (outline-flag-region
21568 (max (point-min) (1- (point)))
21570 (if (re-search-forward
21571 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
21572 (match-beginning 1)
21575 (org-cycle-hide-drawers 'children))
21578 (defun org-make-options-regexp (kwds &optional extra)
21579 "Make a regular expression for keyword lines."
21583 (mapconcat 'regexp-quote kwds "\\|")
21584 (if extra (concat "\\|" extra))
21588 ;; Make isearch reveal the necessary context
21589 (defun org-isearch-end ()
21590 "Reveal context after isearch exits."
21591 (when isearch-success ; only if search was successful
21592 (if (featurep 'xemacs)
21593 ;; Under XEmacs, the hook is run in the correct place,
21594 ;; we directly show the context.
21595 (org-show-context 'isearch)
21596 ;; In Emacs the hook runs *before* restoring the overlays.
21597 ;; So we have to use a one-time post-command-hook to do this.
21598 ;; (Emacs 22 has a special variable, see function `org-mode')
21599 (unless (and (boundp 'isearch-mode-end-hook-quit)
21600 isearch-mode-end-hook-quit)
21601 ;; Only when the isearch was not quitted.
21602 (org-add-hook 'post-command-hook 'org-isearch-post-command
21603 'append 'local)))))
21605 (defun org-isearch-post-command ()
21606 "Remove self from hook, and show context."
21607 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
21608 (org-show-context 'isearch))
21611 ;;;; Integration with and fixes for other packages
21615 (defvar org-imenu-markers nil
21616 "All markers currently used by Imenu.")
21617 (make-variable-buffer-local 'org-imenu-markers)
21619 (defun org-imenu-new-marker (&optional pos)
21620 "Return a new marker for use by Imenu, and remember the marker."
21621 (let ((m (make-marker)))
21622 (move-marker m (or pos (point)))
21623 (push m org-imenu-markers)
21626 (defun org-imenu-get-tree ()
21627 "Produce the index for Imenu."
21628 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
21629 (setq org-imenu-markers nil)
21630 (let* ((n org-imenu-depth)
21631 (re (concat "^" (org-get-limited-outline-regexp)))
21632 (subs (make-vector (1+ n) nil))
21638 (goto-char (point-max))
21639 (while (re-search-backward re nil t)
21640 (setq level (org-reduced-level (funcall outline-level)))
21641 (when (and (<= level n)
21642 (looking-at org-complex-heading-regexp))
21643 (setq head (org-link-display-format
21644 (org-match-string-no-properties 4))
21645 m (org-imenu-new-marker))
21646 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
21647 (if (>= level last-level)
21648 (push (cons head m) (aref subs level))
21649 (push (cons head (aref subs (1+ level))) (aref subs level))
21650 (loop for i from (1+ level) to n do (aset subs i nil)))
21651 (setq last-level level)))))
21654 (eval-after-load "imenu"
21656 (add-hook 'imenu-after-jump-hook
21658 (if (derived-mode-p 'org-mode)
21659 (org-show-context 'org-goto))))))
21661 (defun org-link-display-format (link)
21662 "Replace a link with either the description, or the link target
21663 if no description is present"
21665 (if (string-match org-bracket-link-analytic-regexp link)
21666 (replace-match (if (match-end 5)
21667 (match-string 5 link)
21668 (concat (match-string 1 link)
21669 (match-string 3 link)))
21673 (defun org-toggle-link-display ()
21674 "Toggle the literal or descriptive display of links."
21676 (if org-descriptive-links
21677 (progn (org-remove-from-invisibility-spec '(org-link))
21678 (org-restart-font-lock)
21679 (setq org-descriptive-links nil))
21680 (progn (add-to-invisibility-spec '(org-link))
21681 (org-restart-font-lock)
21682 (setq org-descriptive-links t))))
21684 ;; Speedbar support
21686 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
21687 "Overlay marking the agenda restriction line in speedbar.")
21688 (overlay-put org-speedbar-restriction-lock-overlay
21689 'face 'org-agenda-restriction-lock)
21690 (overlay-put org-speedbar-restriction-lock-overlay
21691 'help-echo "Agendas are currently limited to this item.")
21692 (org-detach-overlay org-speedbar-restriction-lock-overlay)
21694 (defun org-speedbar-set-agenda-restriction ()
21695 "Restrict future agenda commands to the location at point in speedbar.
21696 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
21698 (require 'org-agenda)
21699 (let (p m tp np dir txt)
21701 ((setq p (text-property-any (point-at-bol) (point-at-eol)
21703 (setq m (get-text-property p 'org-imenu-marker))
21704 (with-current-buffer (marker-buffer m)
21706 (org-agenda-set-restriction-lock 'subtree)))
21707 ((setq p (text-property-any (point-at-bol) (point-at-eol)
21708 'speedbar-function 'speedbar-find-file))
21709 (setq tp (previous-single-property-change
21710 (1+ p) 'speedbar-function)
21711 np (next-single-property-change
21712 tp 'speedbar-function)
21713 dir (speedbar-line-directory)
21714 txt (buffer-substring-no-properties (or tp (point-min))
21715 (or np (point-max))))
21716 (with-current-buffer (find-file-noselect
21717 (let ((default-directory dir))
21718 (expand-file-name txt)))
21719 (unless (derived-mode-p 'org-mode)
21720 (error "Cannot restrict to non-Org-mode file"))
21721 (org-agenda-set-restriction-lock 'file)))
21722 (t (error "Don't know how to restrict Org-mode's agenda")))
21723 (move-overlay org-speedbar-restriction-lock-overlay
21724 (point-at-bol) (point-at-eol))
21725 (setq current-prefix-arg nil)
21726 (org-agenda-maybe-redo)))
21728 (eval-after-load "speedbar"
21730 (speedbar-add-supported-extension ".org")
21731 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
21732 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
21733 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
21734 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
21735 (add-hook 'speedbar-visiting-tag-hook
21736 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
21738 ;;; Fixes and Hacks for problems with other packages
21740 ;; Make flyspell not check words in links, to not mess up our keymap
21741 (defun org-mode-flyspell-verify ()
21742 "Don't let flyspell put overlays at active buttons, or on
21743 {todo,all-time,additional-option-like}-keywords."
21744 (let ((pos (max (1- (point)) (point-min)))
21745 (word (thing-at-point 'word)))
21746 (and (not (get-text-property pos 'keymap))
21747 (not (get-text-property pos 'org-no-flyspell))
21748 (not (member word org-todo-keywords-1))
21749 (not (member word org-all-time-keywords))
21750 (not (member word org-additional-option-like-keywords)))))
21752 (defun org-remove-flyspell-overlays-in (beg end)
21753 "Remove flyspell overlays in region."
21754 (and (org-bound-and-true-p flyspell-mode)
21755 (fboundp 'flyspell-delete-region-overlays)
21756 (flyspell-delete-region-overlays beg end))
21757 (add-text-properties beg end '(org-no-flyspell t)))
21759 ;; Make `bookmark-jump' shows the jump location if it was hidden.
21760 (eval-after-load "bookmark"
21761 '(if (boundp 'bookmark-after-jump-hook)
21762 ;; We can use the hook
21763 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
21764 ;; Hook not available, use advice
21765 (defadvice bookmark-jump (after org-make-visible activate)
21766 "Make the position visible."
21767 (org-bookmark-jump-unhide))))
21769 ;; Make sure saveplace shows the location if it was hidden
21770 (eval-after-load "saveplace"
21771 '(defadvice save-place-find-file-hook (after org-make-visible activate)
21772 "Make the position visible."
21773 (org-bookmark-jump-unhide)))
21775 ;; Make sure ecb shows the location if it was hidden
21776 (eval-after-load "ecb"
21777 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
21778 "Make hierarchy visible when jumping into location from ECB tree buffer."
21779 (if (derived-mode-p 'org-mode)
21780 (org-show-context))))
21782 (defun org-bookmark-jump-unhide ()
21783 "Unhide the current position, to show the bookmark location."
21784 (and (derived-mode-p 'org-mode)
21785 (or (outline-invisible-p)
21786 (save-excursion (goto-char (max (point-min) (1- (point))))
21787 (outline-invisible-p)))
21788 (org-show-context 'bookmark-jump)))
21790 ;; Make session.el ignore our circular variable
21791 (eval-after-load "session"
21792 '(add-to-list 'session-globals-exclude 'org-mark-ring))
21794 ;;;; Experimental code
21796 (defun org-closed-in-range ()
21797 "Sparse tree of items closed in a certain time range.
21798 Still experimental, may disappear in the future."
21800 ;; Get the time interval from the user.
21801 (let* ((time1 (org-float-time
21802 (org-read-date nil 'to-time nil "Starting date: ")))
21803 (time2 (org-float-time
21804 (org-read-date nil 'to-time nil "End date:")))
21805 ;; callback function
21806 (callback (lambda ()
21809 (apply 'encode-time
21810 (org-parse-time-string
21811 (match-string 1))))))
21812 ;; check if time in interval
21813 (and (>= time time1) (<= time time2))))))
21814 ;; make tree, check each match with the callback
21815 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
21821 (run-hooks 'org-load-hook)
21823 ;;; org.el ends here