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 (load "org-loaddefs.el" nil t
)
83 ;; `org-outline-regexp' ought to be a defconst but is let-binding in
84 ;; some places -- e.g. see the macro org-with-limited-levels.
86 ;; In Org buffers, the value of `outline-regexp' is that of
87 ;; `org-outline-regexp'. The only function still directly relying on
88 ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
89 ;; job when `orgstruct-mode' is active.
90 (defvar org-outline-regexp
"\\*+ "
91 "Regexp to match Org headlines.")
93 (defvar org-outline-regexp-bol
"^\\*+ "
94 "Regexp to match Org headlines.
95 This is similar to `org-outline-regexp' but additionally makes
96 sure that we are at the beginning of the line.")
98 (defvar org-heading-regexp
"^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
99 "Matches an headline, putting stars and text into groups.
100 Stars are put in group 1 and the trimmed body in group 2.")
102 ;; Emacs 22 calendar compatibility: Make sure the new variables are available
103 (when (fboundp 'defvaralias
)
104 (unless (boundp 'calendar-view-holidays-initially-flag
)
105 (defvaralias 'calendar-view-holidays-initially-flag
106 'view-calendar-holidays-initially
))
107 (unless (boundp 'calendar-view-diary-initially-flag
)
108 (defvaralias 'calendar-view-diary-initially-flag
109 'view-diary-entries-initially
))
110 (unless (boundp 'diary-fancy-buffer
)
111 (defvaralias 'diary-fancy-buffer
'fancy-diary-buffer
)))
113 (declare-function org-inlinetask-at-task-p
"org-inlinetask" ())
114 (declare-function org-inlinetask-outline-regexp
"org-inlinetask" ())
115 (declare-function org-inlinetask-toggle-visibility
"org-inlinetask" ())
116 (declare-function org-pop-to-buffer-same-window
"org-compat" (&optional buffer-or-name norecord label
))
117 (declare-function org-at-clock-log-p
"org-clock" ())
118 (declare-function org-clock-timestamps-up
"org-clock" ())
119 (declare-function org-clock-timestamps-down
"org-clock" ())
120 (declare-function org-clock-sum-current-item
"org-clock" (&optional tstart
))
122 ;; load languages based on value of `org-babel-load-languages'
123 (defvar org-babel-load-languages
)
126 (defun org-babel-do-load-languages (sym value
)
127 "Load the languages defined in `org-babel-load-languages'."
128 (set-default sym value
)
130 (let ((active (cdr pair
)) (lang (symbol-name (car pair
))))
133 (require (intern (concat "ob-" lang
))))
135 (funcall 'fmakunbound
136 (intern (concat "org-babel-execute:" lang
)))
137 (funcall 'fmakunbound
138 (intern (concat "org-babel-expand-body:" lang
)))))))
139 org-babel-load-languages
))
141 (defcustom org-babel-load-languages
'((emacs-lisp . t
))
142 "Languages which can be evaluated in Org-mode buffers.
143 This list can be used to load support for any of the languages
144 below, note that each language will depend on a different set of
145 system executables and/or Emacs modes. When a language is
146 \"loaded\", then code blocks in that language can be evaluated
147 with `org-babel-execute-src-block' bound by default to C-c
148 C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
149 be set to remove code block evaluation from the C-c C-c
150 keybinding. By default only Emacs Lisp (which has no
151 requirements) is loaded."
153 :set
'org-babel-do-load-languages
155 :type
'(alist :tag
"Babel Languages"
158 (const :tag
"Awk" awk
)
161 (const :tag
"Asymptote" asymptote
)
162 (const :tag
"Calc" calc
)
163 (const :tag
"Clojure" clojure
)
164 (const :tag
"CSS" css
)
165 (const :tag
"Ditaa" ditaa
)
166 (const :tag
"Dot" dot
)
167 (const :tag
"Emacs Lisp" emacs-lisp
)
168 (const :tag
"Fortran" fortran
)
169 (const :tag
"Gnuplot" gnuplot
)
170 (const :tag
"Haskell" haskell
)
172 (const :tag
"Java" java
)
173 (const :tag
"Javascript" js
)
174 (const :tag
"LaTeX" latex
)
175 (const :tag
"Ledger" ledger
)
176 (const :tag
"Lilypond" lilypond
)
177 (const :tag
"Lisp" lisp
)
178 (const :tag
"Maxima" maxima
)
179 (const :tag
"Matlab" matlab
)
180 (const :tag
"Mscgen" mscgen
)
181 (const :tag
"Ocaml" ocaml
)
182 (const :tag
"Octave" octave
)
183 (const :tag
"Org" org
)
184 (const :tag
"Perl" perl
)
185 (const :tag
"Pico Lisp" picolisp
)
186 (const :tag
"PlantUML" plantuml
)
187 (const :tag
"Python" python
)
188 (const :tag
"Ruby" ruby
)
189 (const :tag
"Sass" sass
)
190 (const :tag
"Scala" scala
)
191 (const :tag
"Scheme" scheme
)
192 (const :tag
"Screen" screen
)
193 (const :tag
"Shell Script" sh
)
194 (const :tag
"Shen" shen
)
195 (const :tag
"Sql" sql
)
196 (const :tag
"Sqlite" sqlite
))
197 :value-type
(boolean :tag
"Activate" :value t
)))
199 ;;;; Customization variables
200 (defcustom org-clone-delete-id nil
201 "Remove ID property of clones of a subtree.
202 When non-nil, clones of a subtree don't inherit the ID property.
203 Otherwise they inherit the ID property with a new unique
210 (require 'org-compat
)
214 (defun org-version (&optional here full message
)
215 "Show the org-mode version in the echo area.
216 With prefix argument HERE, insert it at point.
217 When FULL is non-nil, use a verbose version string.
218 When MESSAGE is non-nil, display a message with the version."
220 (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
221 (org-install-dir (ignore-errors (org-find-library-dir "org-install.el")))
223 (and (fboundp 'org-release
) (fboundp 'org-git-version
))
224 (load (concat org-dir
"org-version.el")
225 'noerror
'nomessage
'nosuffix
)))
226 (org-version (org-release))
227 (git-version (org-git-version))
228 (version (format "Org-mode version %s (%s @ %s)"
232 (if (string= org-dir org-install-dir
)
234 (concat "mixed installation! " org-install-dir
" and " org-dir
))
235 "org-install.el can not be found!")))
236 (_version (if full version org-version
)))
237 (if (org-called-interactively-p 'interactive
)
241 (if message
(message _version
))
244 (defconst org-version
(org-version))
246 ;;; Compatibility constants
248 ;;; The custom variables
251 "Outline-based notes management and organizer."
256 (defcustom org-mode-hook nil
257 "Mode hook for Org-mode, run after the mode was turned on."
261 (defcustom org-load-hook nil
262 "Hook that is run after org.el has been loaded."
266 (defcustom org-log-buffer-setup-hook nil
267 "Hook that is run after an Org log buffer is created."
272 (defvar org-modules
) ; defined below
273 (defvar org-modules-loaded nil
274 "Have the modules been loaded already?")
276 (defun org-load-modules-maybe (&optional force
)
277 "Load all extensions listed in `org-modules'."
278 (when (or force
(not org-modules-loaded
))
280 (condition-case nil
(require ext
)
281 (error (message "Problems while trying to load feature `%s'" ext
))))
283 (setq org-modules-loaded t
)))
285 (defun org-set-modules (var value
)
286 "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
288 (when (featurep 'org
)
289 (org-load-modules-maybe 'force
)))
291 (when (org-bound-and-true-p org-modules
)
292 (let ((a (member 'org-infojs org-modules
)))
293 (and a
(setcar a
'org-jsinfo
))))
295 (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
)
296 "Modules that should always be loaded together with org.el.
297 If a description starts with <C>, the file is not part of Emacs
298 and loading it will require that you have downloaded and properly installed
299 the org-mode distribution.
301 You can also use this system to load external packages (i.e. neither Org
302 core modules, nor modules from the CONTRIB directory). Just add symbols
303 to the end of the list. If the package is called org-xyz.el, then you need
304 to add the symbol `xyz', and the package must have a call to
308 :set
'org-set-modules
311 (const :tag
" bbdb: Links to BBDB entries" org-bbdb
)
312 (const :tag
" bibtex: Links to BibTeX entries" org-bibtex
)
313 (const :tag
" crypt: Encryption of subtrees" org-crypt
)
314 (const :tag
" ctags: Access to Emacs tags with links" org-ctags
)
315 (const :tag
" docview: Links to doc-view buffers" org-docview
)
316 (const :tag
" gnus: Links to GNUS folders/messages" org-gnus
)
317 (const :tag
" id: Global IDs for identifying entries" org-id
)
318 (const :tag
" info: Links to Info nodes" org-info
)
319 (const :tag
" jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo
)
320 (const :tag
" habit: Track your consistency with habits" org-habit
)
321 (const :tag
" inlinetask: Tasks independent of outline hierarchy" org-inlinetask
)
322 (const :tag
" irc: Links to IRC/ERC chat sessions" org-irc
)
323 (const :tag
" mac-message: Links to messages in Apple Mail" org-mac-message
)
324 (const :tag
" mew Links to Mew folders/messages" org-mew
)
325 (const :tag
" mhe: Links to MHE folders/messages" org-mhe
)
326 (const :tag
" protocol: Intercept calls from emacsclient" org-protocol
)
327 (const :tag
" rmail: Links to RMAIL folders/messages" org-rmail
)
328 (const :tag
" special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks
)
329 (const :tag
" vm: Links to VM folders/messages" org-vm
)
330 (const :tag
" wl: Links to Wanderlust folders/messages" org-wl
)
331 (const :tag
" w3m: Special cut/paste from w3m to Org-mode." org-w3m
)
332 (const :tag
" mouse: Additional mouse support" org-mouse
)
333 (const :tag
" TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler
)
335 (const :tag
"C annotate-file: Annotate a file with org syntax" org-annotate-file
)
336 (const :tag
"C bookmark: Org-mode links to bookmarks" org-bookmark
)
337 (const :tag
"C checklist: Extra functions for checklists in repeated tasks" org-checklist
)
338 (const :tag
"C choose: Use TODO keywords to mark decisions states" org-choose
)
339 (const :tag
"C collector: Collect properties into tables" org-collector
)
340 (const :tag
"C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend
)
341 (const :tag
"C drill: Flashcards and spaced repetition for Org-mode" org-drill
)
342 (const :tag
"C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol
)
343 (const :tag
"C eshell Support for links to working directories in eshell" org-eshell
)
344 (const :tag
"C eval: Include command output as text" org-eval
)
345 (const :tag
"C eval-light: Evaluate inbuffer-code on demand" org-eval-light
)
346 (const :tag
"C expiry: Expiry mechanism for Org-mode entries" org-expiry
)
347 (const :tag
"C exp-bibtex: Export citations using BibTeX" org-exp-bibtex
)
348 (const :tag
"C git-link: Provide org links to specific file version" org-git-link
)
349 (const :tag
"C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query
)
351 (const :tag
"C invoice: Help manage client invoices in Org-mode" org-invoice
)
353 (const :tag
"C jira: Add a jira:ticket protocol to Org-mode" org-jira
)
354 (const :tag
"C learn: SuperMemo's incremental learning algorithm" org-learn
)
355 (const :tag
"C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix
)
356 (const :tag
"C notmuch: Provide org links to notmuch searches or messages" org-notmuch
)
357 (const :tag
"C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal
)
358 (const :tag
"C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber
)
359 (const :tag
"C man: Support for links to manpages in Org-mode" org-man
)
360 (const :tag
"C mtags: Support for muse-like tags" org-mtags
)
361 (const :tag
"C panel: Simple routines for us with bad memory" org-panel
)
362 (const :tag
"C registry: A registry for Org-mode links" org-registry
)
363 (const :tag
"C org2rem: Convert org appointments into reminders" org2rem
)
364 (const :tag
"C screen: Visit screen sessions through Org-mode links" org-screen
)
365 (const :tag
"C secretary: Team management with org-mode" org-secretary
)
366 (const :tag
"C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert
)
367 (const :tag
"C toc: Table of contents for Org-mode buffer" org-toc
)
368 (const :tag
"C track: Keep up with Org-mode development" org-track
)
369 (const :tag
"C velocity Something like Notational Velocity for Org" org-velocity
)
370 (const :tag
"C wikinodes: CamelCase wiki-like links" org-wikinodes
)
371 (repeat :tag
"External packages" :inline t
(symbol :tag
"Package"))))
373 (defcustom org-support-shift-select nil
374 "Non-nil means make shift-cursor commands select text when possible.
376 In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
377 start selecting a region, or enlarge regions started in this way.
378 In Org-mode, in special contexts, these same keys are used for
379 other purposes, important enough to compete with shift selection.
380 Org tries to balance these needs by supporting `shift-select-mode'
381 outside these special contexts, under control of this variable.
383 The default of this variable is nil, to avoid confusing behavior. Shifted
384 cursor keys will then execute Org commands in the following contexts:
385 - on a headline, changing TODO state (left/right) and priority (up/down)
386 - on a time stamp, changing the time
387 - in a plain list item, changing the bullet type
388 - in a property definition line, switching between allowed values
389 - in the BEGIN line of a clock table (changing the time block).
390 Outside these contexts, the commands will throw an error.
392 When this variable is t and the cursor is not in a special
393 context, Org-mode will support shift-selection for making and
394 enlarging regions. To make this more effective, the bullet
395 cycling will no longer happen anywhere in an item line, but only
396 if the cursor is exactly on the bullet.
398 If you set this variable to the symbol `always', then the keys
399 will not be special in headlines, property lines, and item lines,
400 to make shift selection work there as well. If this is what you
401 want, you can use the following alternative commands: `C-c C-t'
402 and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
403 can be used to switch TODO sets, `C-c -' to cycle item bullet
404 types, and properties can be edited by hand or in column view.
406 However, when the cursor is on a timestamp, shift-cursor commands
407 will still edit the time stamp - this is just too good to give up.
409 XEmacs user should have this variable set to nil, because
410 `shift-select-mode' is in Emacs 23 or later only."
413 (const :tag
"Never" nil
)
414 (const :tag
"When outside special context" t
)
415 (const :tag
"Everywhere except timestamps" always
)))
417 (defcustom org-loop-over-headlines-in-active-region nil
418 "Shall some commands act upon headlines in the active region?
420 When set to `t', some commands will be performed in all headlines
421 within the active region.
423 When set to `start-level', some commands will be performed in all
424 headlines within the active region, provided that these headlines
425 are of the same level than the first one.
427 When set to a string, those commands will be performed on the
428 matching headlines within the active region. Such string must be
429 a tags/property/todo match as it is used in the agenda tags view.
431 The list of commands is: `org-schedule', `org-deadline',
432 `org-todo', `org-archive-subtree', `org-archive-set-tag' and
433 `org-archive-to-archive-sibling'. The archiving commands skip
434 already archived entries."
435 :type
'(choice (const :tag
"Don't loop" nil
)
436 (const :tag
"All headlines in active region" t
)
437 (const :tag
"In active region, headlines at the same level than the first one" 'start-level
)
438 (string :tag
"Tags/Property/Todo matcher"))
443 (defgroup org-startup nil
444 "Options concerning startup of Org-mode."
448 (defcustom org-startup-folded t
449 "Non-nil means entering Org-mode will switch to OVERVIEW.
450 This can also be configured on a per-file basis by adding one of
451 the following lines anywhere in the buffer:
453 #+STARTUP: fold (or `overview', this is equivalent)
454 #+STARTUP: nofold (or `showall', this is equivalent)
456 #+STARTUP: showeverything"
459 (const :tag
"nofold: show all" nil
)
460 (const :tag
"fold: overview" t
)
461 (const :tag
"content: all headlines" content
)
462 (const :tag
"show everything, even drawers" showeverything
)))
464 (defcustom org-startup-truncated t
465 "Non-nil means entering Org-mode will set `truncate-lines'.
466 This is useful since some lines containing links can be very long and
467 uninteresting. Also tables look terrible when wrapped."
471 (defcustom org-startup-indented nil
472 "Non-nil means turn on `org-indent-mode' on startup.
473 This can also be configured on a per-file basis by adding one of
474 the following lines anywhere in the buffer:
478 :group
'org-structure
480 (const :tag
"Not" nil
)
481 (const :tag
"Globally (slow on startup in large files)" t
)))
483 (defcustom org-use-sub-superscripts t
484 "Non-nil means interpret \"_\" and \"^\" for export.
485 When this option is turned on, you can use TeX-like syntax for sub- and
486 superscripts. Several characters after \"_\" or \"^\" will be
487 considered as a single item - so grouping with {} is normally not
488 needed. For example, the following things will be parsed as single
489 sub- or superscripts.
491 10^24 or 10^tau several digits will be considered 1 item.
492 10^-12 or 10^-tau a leading sign with digits or a word
493 x^2-y^3 will be read as x^2 - y^3, because items are
494 terminated by almost any nonword/nondigit char.
495 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
497 Still, ambiguity is possible - so when in doubt use {} to enclose the
498 sub/superscript. If you set this variable to the symbol `{}',
499 the braces are *required* in order to trigger interpretations as
500 sub/superscript. This can be helpful in documents that need \"_\"
501 frequently in plain text.
503 Not all export backends support this, but HTML does.
505 This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
507 :group
'org-export-translation
510 (const :tag
"Always interpret" t
)
511 (const :tag
"Only with braces" {})
512 (const :tag
"Never interpret" nil
)))
514 (if (fboundp 'defvaralias
)
515 (defvaralias 'org-export-with-sub-superscripts
'org-use-sub-superscripts
))
518 (defcustom org-startup-with-beamer-mode nil
519 "Non-nil means turn on `org-beamer-mode' on startup.
520 This can also be configured on a per-file basis by adding one of
521 the following lines anywhere in the buffer:
528 (defcustom org-startup-align-all-tables nil
529 "Non-nil means align all tables when visiting a file.
530 This is useful when the column width in tables is forced with <N> cookies
531 in table fields. Such tables will look correct only after the first re-align.
532 This can also be configured on a per-file basis by adding one of
533 the following lines anywhere in the buffer:
539 (defcustom org-startup-with-inline-images nil
540 "Non-nil means show inline images when loading a new Org file.
541 This can also be configured on a per-file basis by adding one of
542 the following lines anywhere in the buffer:
543 #+STARTUP: inlineimages
544 #+STARTUP: noinlineimages"
549 (defcustom org-insert-mode-line-in-empty-file nil
550 "Non-nil means insert the first line setting Org-mode in empty files.
551 When the function `org-mode' is called interactively in an empty file, this
552 normally means that the file name does not automatically trigger Org-mode.
553 To ensure that the file will always be in Org-mode in the future, a
554 line enforcing Org-mode will be inserted into the buffer, if this option
559 (defcustom org-replace-disputed-keys nil
560 "Non-nil means use alternative key bindings for some keys.
561 Org-mode uses S-<cursor> keys for changing timestamps and priorities.
562 These keys are also used by other packages like shift-selection-mode'
563 \(built into Emacs 23), `CUA-mode' or `windmove.el'.
564 If you want to use Org-mode together with one of these other modes,
565 or more generally if you would like to move some Org-mode commands to
566 other keys, set this variable and configure the keys with the variable
569 This option is only relevant at load-time of Org-mode, and must be set
570 *before* org.el is loaded. Changing it requires a restart of Emacs to
575 (defcustom org-use-extra-keys nil
576 "Non-nil means use extra key sequence definitions for certain commands.
577 This happens automatically if you run XEmacs or if `window-system'
578 is nil. This variable lets you do the same manually. You must
579 set it before loading org.
581 Example: on Carbon Emacs 22 running graphically, with an external
582 keyboard on a Powerbook, the default way of setting M-left might
583 not work for either Alt or ESC. Setting this variable will make
588 (if (fboundp 'defvaralias
)
589 (defvaralias 'org-CUA-compatible
'org-replace-disputed-keys
))
591 (defcustom org-disputed-keys
592 '(([(shift up
)] .
[(meta p
)])
593 ([(shift down
)] .
[(meta n
)])
594 ([(shift left
)] .
[(meta -
)])
595 ([(shift right
)] .
[(meta +)])
596 ([(control shift right
)] .
[(meta shift
+)])
597 ([(control shift left
)] .
[(meta shift -
)]))
598 "Keys for which Org-mode and other modes compete.
599 This is an alist, cars are the default keys, second element specifies
600 the alternative to use when `org-replace-disputed-keys' is t.
602 Keys can be specified in any syntax supported by `define-key'.
603 The value of this option takes effect only at Org-mode's startup,
604 therefore you'll have to restart Emacs to apply it after changing."
609 "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
610 Or return the original if not disputed.
611 Also apply the translations defined in `org-xemacs-key-equivalents'."
612 (when org-replace-disputed-keys
613 (let* ((nkey (key-description key
))
614 (x (org-find-if (lambda (x)
615 (equal (key-description (car x
)) nkey
))
617 (setq key
(if x
(cdr x
) key
))))
618 (when (featurep 'xemacs
)
619 (setq key
(or (cdr (assoc key org-xemacs-key-equivalents
)) key
)))
622 (defun org-find-if (predicate seq
)
625 (if (funcall predicate
(car seq
))
626 (throw 'exit
(car seq
))
629 (defun org-defkey (keymap key def
)
630 "Define a key, possibly translated, as returned by `org-key'."
631 (define-key keymap
(org-key key
) def
))
633 (defcustom org-ellipsis nil
634 "The ellipsis to use in the Org-mode outline.
635 When nil, just use the standard three dots. When a string, use that instead,
636 When a face, use the standard 3 dots, but with the specified face.
637 The change affects only Org-mode (which will then use its own display table).
638 Changing this requires executing `M-x org-mode' in a buffer to become
641 :type
'(choice (const :tag
"Default" nil
)
642 (face :tag
"Face" :value org-warning
)
643 (string :tag
"String" :value
"...#")))
645 (defvar org-display-table nil
646 "The display table for org-mode, in case `org-ellipsis' is non-nil.")
648 (defgroup org-keywords nil
649 "Keywords in Org-mode."
653 (defcustom org-deadline-string
"DEADLINE:"
654 "String to mark deadline entries.
655 A deadline is this string, followed by a time stamp. Should be a word,
656 terminated by a colon. You can insert a schedule keyword and
657 a timestamp with \\[org-deadline].
658 Changes become only effective after restarting Emacs."
662 (defcustom org-scheduled-string
"SCHEDULED:"
663 "String to mark scheduled TODO entries.
664 A schedule is this string, followed by a time stamp. Should be a word,
665 terminated by a colon. You can insert a schedule keyword and
666 a timestamp with \\[org-schedule].
667 Changes become only effective after restarting Emacs."
671 (defcustom org-closed-string
"CLOSED:"
672 "String used as the prefix for timestamps logging closing a TODO entry."
676 (defcustom org-clock-string
"CLOCK:"
677 "String used as prefix for timestamps clocking work hours on an item."
681 (defconst org-planning-or-clock-line-re
(concat "^[ \t]*\\("
682 org-scheduled-string
"\\|"
683 org-deadline-string
"\\|"
684 org-closed-string
"\\|"
685 org-clock-string
"\\)")
686 "Matches a line with planning or clock info.")
688 (defcustom org-comment-string
"COMMENT"
689 "Entries starting with this keyword will never be exported.
690 An entry can be toggled between COMMENT and normal with
691 \\[org-toggle-comment].
692 Changes become only effective after restarting Emacs."
696 (defcustom org-quote-string
"QUOTE"
697 "Entries starting with this keyword will be exported in fixed-width font.
698 Quoting applies only to the text in the entry following the headline, and does
699 not extend beyond the next headline, even if that is lower level.
700 An entry can be toggled between QUOTE and normal with
701 \\[org-toggle-fixed-width-section]."
705 (defconst org-repeat-re
706 "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
707 "Regular expression for specifying repeated events.
708 After a match, group 1 contains the repeat expression.")
710 (defgroup org-structure nil
711 "Options concerning the general structure of Org-mode files."
715 (defgroup org-reveal-location nil
716 "Options about how to make context of a location visible."
717 :tag
"Org Reveal Location"
718 :group
'org-structure
)
720 (defconst org-context-choice
722 (const :tag
"Always" t
)
723 (const :tag
"Never" nil
)
724 (repeat :greedy t
:tag
"Individual contexts"
726 (choice :tag
"Context"
733 (const bookmark-jump
)
737 "Contexts for the reveal options.")
739 (defcustom org-show-hierarchy-above
'((default . t
))
740 "Non-nil means show full hierarchy when revealing a location.
741 Org-mode often shows locations in an org-mode file which might have
742 been invisible before. When this is set, the hierarchy of headings
743 above the exposed location is shown.
744 Turning this off for example for sparse trees makes them very compact.
745 Instead of t, this can also be an alist specifying this option for different
746 contexts. Valid contexts are
747 agenda when exposing an entry from the agenda
748 org-goto when using the command `org-goto' on key C-c C-j
749 occur-tree when using the command `org-occur' on key C-c /
750 tags-tree when constructing a sparse tree based on tags matches
751 link-search when exposing search matches associated with a link
752 mark-goto when exposing the jump goal of a mark
753 bookmark-jump when exposing a bookmark location
754 isearch when exiting from an incremental search
755 default default for all contexts not set explicitly"
756 :group
'org-reveal-location
757 :type org-context-choice
)
759 (defcustom org-show-following-heading
'((default . nil
))
760 "Non-nil means show following heading when revealing a location.
761 Org-mode often shows locations in an org-mode file which might have
762 been invisible before. When this is set, the heading following the
764 Turning this off for example for sparse trees makes them very compact,
765 but makes it harder to edit the location of the match. In such a case,
766 use the command \\[org-reveal] to show more context.
767 Instead of t, this can also be an alist specifying this option for different
768 contexts. See `org-show-hierarchy-above' for valid contexts."
769 :group
'org-reveal-location
770 :type org-context-choice
)
772 (defcustom org-show-siblings
'((default . nil
) (isearch t
))
773 "Non-nil means show all sibling heading when revealing a location.
774 Org-mode often shows locations in an org-mode file which might have
775 been invisible before. When this is set, the sibling of the current entry
776 heading are all made visible. If `org-show-hierarchy-above' is t,
777 the same happens on each level of the hierarchy above the current entry.
779 By default this is on for the isearch context, off for all other contexts.
780 Turning this off for example for sparse trees makes them very compact,
781 but makes it harder to edit the location of the match. In such a case,
782 use the command \\[org-reveal] to show more context.
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-show-entry-below
'((default . nil
))
789 "Non-nil means show the entry below a headline when revealing a location.
790 Org-mode often shows locations in an org-mode file which might have
791 been invisible before. When this is set, the text below the headline that is
792 exposed is also shown.
794 By default this is off for all contexts.
795 Instead of t, this can also be an alist specifying this option for different
796 contexts. See `org-show-hierarchy-above' for valid contexts."
797 :group
'org-reveal-location
798 :type org-context-choice
)
800 (defcustom org-indirect-buffer-display
'other-window
801 "How should indirect tree buffers be displayed?
802 This applies to indirect buffers created with the commands
803 \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
805 current-window Display in the current window
806 other-window Just display in another window.
807 dedicated-frame Create one new frame, and re-use it each time.
808 new-frame Make a new frame each time. Note that in this case
809 previously-made indirect buffers are kept, and you need to
810 kill these buffers yourself."
811 :group
'org-structure
812 :group
'org-agenda-windows
814 (const :tag
"In current window" current-window
)
815 (const :tag
"In current frame, other window" other-window
)
816 (const :tag
"Each time a new frame" new-frame
)
817 (const :tag
"One dedicated frame" dedicated-frame
)))
819 (defcustom org-use-speed-commands nil
820 "Non-nil means activate single letter commands at beginning of a headline.
821 This may also be a function to test for appropriate locations where speed
822 commands should be active."
823 :group
'org-structure
825 (const :tag
"Never" nil
)
826 (const :tag
"At beginning of headline stars" t
)
829 (defcustom org-speed-commands-user nil
830 "Alist of additional speed commands.
831 This list will be checked before `org-speed-commands-default'
832 when the variable `org-use-speed-commands' is non-nil
833 and when the cursor is at the beginning of a headline.
834 The car if each entry is a string with a single letter, which must
835 be assigned to `self-insert-command' in the global map.
836 The cdr is either a command to be called interactively, a function
837 to be called, or a form to be evaluated.
838 An entry that is just a list with a single string will be interpreted
839 as a descriptive headline that will be added when listing the speed
840 commands in the Help buffer using the `?' speed command."
841 :group
'org-structure
842 :type
'(repeat :value
("k" . ignore
)
843 (choice :value
("k" . ignore
)
844 (list :tag
"Descriptive Headline" (string :tag
"Headline"))
845 (cons :tag
"Letter and Command"
846 (string :tag
"Command letter")
851 (defgroup org-cycle nil
852 "Options concerning visibility cycling in Org-mode."
854 :group
'org-structure
)
856 (defcustom org-cycle-skip-children-state-if-no-children t
857 "Non-nil means skip CHILDREN state in entries that don't have any."
861 (defcustom org-cycle-max-level nil
862 "Maximum level which should still be subject to visibility cycling.
863 Levels higher than this will, for cycling, be treated as text, not a headline.
864 When `org-odd-levels-only' is set, a value of N in this variable actually
865 means 2N-1 stars as the limiting headline.
866 When nil, cycle all levels.
867 Note that the limiting level of cycling is also influenced by
868 `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
869 `org-inlinetask-min-level' is, cycling will be limited to levels one less
873 (const :tag
"No limit" nil
)
874 (integer :tag
"Maximum level")))
876 (defcustom org-drawers
'("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
877 "Names of drawers. Drawers are not opened by cycling on the headline above.
878 Drawers only open with a TAB on the drawer line itself. A drawer looks like
883 The drawer \"PROPERTIES\" is special for capturing properties through
886 Drawers can be defined on the per-file basis with a line like:
888 #+DRAWERS: HIDDEN STATE PROPERTIES"
889 :group
'org-structure
891 :type
'(repeat (string :tag
"Drawer Name")))
893 (defcustom org-hide-block-startup nil
894 "Non-nil means entering Org-mode will fold all blocks.
895 This can also be set in on a per-file basis with
897 #+STARTUP: hideblocks
898 #+STARTUP: showblocks"
903 (defcustom org-cycle-global-at-bob nil
904 "Cycle globally if cursor is at beginning of buffer and not at a headline.
905 This makes it possible to do global cycling without having to use S-TAB or
906 \\[universal-argument] TAB. For this special case to work, the first line
907 of the buffer must not be a headline -- it may be empty or some other text.
908 When used in this way, `org-cycle-hook' is disabled temporarily to make
909 sure the cursor stays at the beginning of the buffer. When this option is
910 nil, don't do anything special at the beginning of the buffer."
914 (defcustom org-cycle-level-after-item
/entry-creation t
915 "Non-nil means cycle entry level or item indentation in new empty entries.
917 When the cursor is at the end of an empty headline, i.e with only stars
918 and maybe a TODO keyword, TAB will then switch the entry to become a child,
919 and then all possible ancestor states, before returning to the original state.
920 This makes data entry extremely fast: M-RET to create a new headline,
921 on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
923 When the cursor is at the end of an empty plain list item, one TAB will
924 make it a subitem, two or more tabs will back up to make this an item
925 higher up in the item hierarchy."
929 (defcustom org-cycle-emulate-tab t
930 "Where should `org-cycle' emulate TAB.
932 white Only in completely white lines
933 whitestart Only at the beginning of lines, before the first non-white char
934 t Everywhere except in headlines
935 exc-hl-bol Everywhere except at the start of a headline
936 If TAB is used in a place where it does not emulate TAB, the current subtree
937 visibility is cycled."
939 :type
'(choice (const :tag
"Never" nil
)
940 (const :tag
"Only in completely white lines" white
)
941 (const :tag
"Before first char in a line" whitestart
)
942 (const :tag
"Everywhere except in headlines" t
)
943 (const :tag
"Everywhere except at bol in headlines" exc-hl-bol
)
946 (defcustom org-cycle-separator-lines
2
947 "Number of empty lines needed to keep an empty line between collapsed trees.
948 If you leave an empty line between the end of a subtree and the following
949 headline, this empty line is hidden when the subtree is folded.
950 Org-mode will leave (exactly) one empty line visible if the number of
951 empty lines is equal or larger to the number given in this variable.
952 So the default 2 means at least 2 empty lines after the end of a subtree
953 are needed to produce free space between a collapsed subtree and the
956 If the number is negative, and the number of empty lines is at least -N,
957 all empty lines are shown.
959 Special case: when 0, never leave empty lines in collapsed view."
962 (put 'org-cycle-separator-lines
'safe-local-variable
'integerp
)
964 (defcustom org-pre-cycle-hook nil
965 "Hook that is run before visibility cycling is happening.
966 The function(s) in this hook must accept a single argument which indicates
967 the new state that will be set right after running this hook. The
968 argument is a symbol. Before a global state change, it can have the values
969 `overview', `content', or `all'. Before a local state change, it can have
970 the values `folded', `children', or `subtree'."
974 (defcustom org-cycle-hook
'(org-cycle-hide-archived-subtrees
975 org-cycle-hide-drawers
976 org-cycle-show-empty-lines
977 org-optimize-window-after-visibility-change
)
978 "Hook that is run after `org-cycle' has changed the buffer visibility.
979 The function(s) in this hook must accept a single argument which indicates
980 the new state that was set by the most recent `org-cycle' command. The
981 argument is a symbol. After a global state change, it can have the values
982 `overview', `contents', or `all'. After a local state change, it can have
983 the values `folded', `children', or `subtree'."
987 (defgroup org-edit-structure nil
988 "Options concerning structure editing in Org-mode."
989 :tag
"Org Edit Structure"
990 :group
'org-structure
)
992 (defcustom org-odd-levels-only nil
993 "Non-nil means skip even levels and only use odd levels for the outline.
994 This has the effect that two stars are being added/taken away in
995 promotion/demotion commands. It also influences how levels are
996 handled by the exporters.
997 Changing it requires restart of `font-lock-mode' to become effective
998 for fontification also in regions already fontified.
999 You may also set this on a per-file basis by adding one of the following
1000 lines to the buffer:
1004 :group
'org-edit-structure
1005 :group
'org-appearance
1008 (defcustom org-adapt-indentation t
1009 "Non-nil means adapt indentation to outline node level.
1011 When this variable is set, Org assumes that you write outlines by
1012 indenting text in each node to align with the headline (after the stars).
1013 The following issues are influenced by this variable:
1015 - When this is set and the *entire* text in an entry is indented, the
1016 indentation is increased by one space in a demotion command, and
1017 decreased by one in a promotion command. If any line in the entry
1018 body starts with text at column 0, indentation is not changed at all.
1020 - Property drawers and planning information is inserted indented when
1021 this variable s set. When nil, they will not be indented.
1023 - TAB indents a line relative to context. The lines below a headline
1024 will be indented when this variable is set.
1026 Note that this is all about true indentation, by adding and removing
1027 space characters. See also `org-indent.el' which does level-dependent
1028 indentation in a virtual way, i.e. at display time in Emacs."
1029 :group
'org-edit-structure
1032 (defcustom org-special-ctrl-a
/e nil
1033 "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
1035 When t, `C-a' will bring back the cursor to the beginning of the
1036 headline text, i.e. after the stars and after a possible TODO
1037 keyword. In an item, this will be the position after bullet and
1038 check-box, if any. When the cursor is already at that position,
1039 another `C-a' will bring it to the beginning of the line.
1041 `C-e' will jump to the end of the headline, ignoring the presence
1042 of tags in the headline. A second `C-e' will then jump to the
1043 true end of the line, after any tags. This also means that, when
1044 this variable is non-nil, `C-e' also will never jump beyond the
1045 end of the heading of a folded section, i.e. not after the
1048 When set to the symbol `reversed', the first `C-a' or `C-e' works
1049 normally, going to the true line boundary first. Only a directly
1050 following, identical keypress will bring the cursor to the
1053 This may also be a cons cell where the behavior for `C-a' and
1054 `C-e' is set separately."
1055 :group
'org-edit-structure
1057 (const :tag
"off" nil
)
1058 (const :tag
"on: after stars/bullet and before tags first" t
)
1059 (const :tag
"reversed: true line boundary first" reversed
)
1060 (cons :tag
"Set C-a and C-e separately"
1061 (choice :tag
"Special C-a"
1062 (const :tag
"off" nil
)
1063 (const :tag
"on: after stars/bullet first" t
)
1064 (const :tag
"reversed: before stars/bullet first" reversed
))
1065 (choice :tag
"Special C-e"
1066 (const :tag
"off" nil
)
1067 (const :tag
"on: before tags first" t
)
1068 (const :tag
"reversed: after tags first" reversed
)))))
1069 (if (fboundp 'defvaralias
)
1070 (defvaralias 'org-special-ctrl-a
'org-special-ctrl-a
/e
))
1072 (defcustom org-special-ctrl-k nil
1073 "Non-nil means `C-k' will behave specially in headlines.
1074 When nil, `C-k' will call the default `kill-line' command.
1075 When t, the following will happen while the cursor is in the headline:
1077 - When the cursor is at the beginning of a headline, kill the entire
1078 line and possible the folded subtree below the line.
1079 - When in the middle of the headline text, kill the headline up to the tags.
1080 - When after the headline text, kill the tags."
1081 :group
'org-edit-structure
1084 (defcustom org-ctrl-k-protect-subtree nil
1085 "Non-nil means, do not delete a hidden subtree with C-k.
1086 When set to the symbol `error', simply throw an error when C-k is
1087 used to kill (part-of) a headline that has hidden text behind it.
1088 Any other non-nil value will result in a query to the user, if it is
1089 OK to kill that hidden subtree. When nil, kill without remorse."
1090 :group
'org-edit-structure
1093 (const :tag
"Do not protect hidden subtrees" nil
)
1094 (const :tag
"Protect hidden subtrees with a security query" t
)
1095 (const :tag
"Never kill a hidden subtree with C-k" error
)))
1097 (defcustom org-catch-invisible-edits nil
1098 "Check if in invisible region before inserting or deleting a character.
1101 nil Do not check, so just do invisible edits.
1102 error Throw an error and do nothing.
1103 show Make point visible, and do the requested edit.
1104 show-and-error Make point visible, then throw an error and abort the edit.
1105 smart Make point visible, and do insertion/deletion if it is
1106 adjacent to visible text and the change feels predictable.
1107 Never delete a previously invisible character or add in the
1108 middle or right after an invisible region. Basically, this
1109 allows insertion and backward-delete right before ellipses.
1110 FIXME: maybe in this case we should not even show?"
1111 :group
'org-edit-structure
1114 (const :tag
"Do not check" nil
)
1115 (const :tag
"Throw error when trying to edit" error
)
1116 (const :tag
"Unhide, but do not do the edit" show-and-error
)
1117 (const :tag
"Show invisible part and do the edit" show
)
1118 (const :tag
"Be smart and do the right thing" smart
)))
1120 (defcustom org-yank-folded-subtrees t
1121 "Non-nil means when yanking subtrees, fold them.
1122 If the kill is a single subtree, or a sequence of subtrees, i.e. if
1123 it starts with a heading and all other headings in it are either children
1124 or siblings, then fold all the subtrees. However, do this only if no
1125 text after the yank would be swallowed into a folded tree by this action."
1126 :group
'org-edit-structure
1129 (defcustom org-yank-adjusted-subtrees nil
1130 "Non-nil means when yanking subtrees, adjust the level.
1131 With this setting, `org-paste-subtree' is used to insert the subtree, see
1132 this function for details."
1133 :group
'org-edit-structure
1136 (defcustom org-M-RET-may-split-line
'((default . t
))
1137 "Non-nil means M-RET will split the line at the cursor position.
1138 When nil, it will go to the end of the line before making a
1140 You may also set this option in a different way for different
1141 contexts. Valid contexts are:
1143 headline when creating a new headline
1144 item when creating a new item
1145 table in a table field
1146 default the value to be used for all contexts not explicitly
1148 :group
'org-structure
1151 (const :tag
"Always" t
)
1152 (const :tag
"Never" nil
)
1153 (repeat :greedy t
:tag
"Individual contexts"
1155 (choice :tag
"Context"
1163 (defcustom org-insert-heading-respect-content nil
1164 "Non-nil means insert new headings after the current subtree.
1165 When nil, the new heading is created directly after the current line.
1166 The commands \\[org-insert-heading-respect-content] and
1167 \\[org-insert-todo-heading-respect-content] turn this variable on
1168 for the duration of the command."
1169 :group
'org-structure
1172 (defcustom org-blank-before-new-entry
'((heading . auto
)
1173 (plain-list-item . auto
))
1174 "Should `org-insert-heading' leave a blank line before new heading/item?
1175 The value is an alist, with `heading' and `plain-list-item' as CAR,
1176 and a boolean flag as CDR. The cdr may also be the symbol `auto', in
1177 which case Org will look at the surrounding headings/items and try to
1178 make an intelligent decision whether to insert a blank line or not.
1180 For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
1181 set, the setting here is ignored and no empty line is inserted, to avoid
1182 breaking the list structure."
1183 :group
'org-edit-structure
1185 (cons (const heading
)
1186 (choice (const :tag
"Never" nil
)
1187 (const :tag
"Always" t
)
1188 (const :tag
"Auto" auto
)))
1189 (cons (const plain-list-item
)
1190 (choice (const :tag
"Never" nil
)
1191 (const :tag
"Always" t
)
1192 (const :tag
"Auto" auto
)))))
1194 (defcustom org-insert-heading-hook nil
1195 "Hook being run after inserting a new heading."
1196 :group
'org-edit-structure
1199 (defcustom org-enable-fixed-width-editor t
1200 "Non-nil means lines starting with \":\" are treated as fixed-width.
1201 This currently only means they are never auto-wrapped.
1202 When nil, such lines will be treated like ordinary lines.
1203 See also the QUOTE keyword."
1204 :group
'org-edit-structure
1207 (defcustom org-goto-auto-isearch t
1208 "Non-nil means typing characters in `org-goto' starts incremental search."
1209 :group
'org-edit-structure
1212 (defgroup org-sparse-trees nil
1213 "Options concerning sparse trees in Org-mode."
1214 :tag
"Org Sparse Trees"
1215 :group
'org-structure
)
1217 (defcustom org-highlight-sparse-tree-matches t
1218 "Non-nil means highlight all matches that define a sparse tree.
1219 The highlights will automatically disappear the next time the buffer is
1220 changed by an edit command."
1221 :group
'org-sparse-trees
1224 (defcustom org-remove-highlights-with-change t
1225 "Non-nil means any change to the buffer will remove temporary highlights.
1226 Such highlights are created by `org-occur' and `org-clock-display'.
1227 When nil, `C-c C-c needs to be used to get rid of the highlights.
1228 The highlights created by `org-preview-latex-fragment' always need
1229 `C-c C-c' to be removed."
1230 :group
'org-sparse-trees
1235 (defcustom org-occur-hook
'(org-first-headline-recenter)
1236 "Hook that is run after `org-occur' has constructed a sparse tree.
1237 This can be used to recenter the window to show as much of the structure
1239 :group
'org-sparse-trees
1242 (defgroup org-imenu-and-speedbar nil
1243 "Options concerning imenu and speedbar in Org-mode."
1244 :tag
"Org Imenu and Speedbar"
1245 :group
'org-structure
)
1247 (defcustom org-imenu-depth
2
1248 "The maximum level for Imenu access to Org-mode headlines.
1249 This also applied for speedbar access."
1250 :group
'org-imenu-and-speedbar
1253 (defgroup org-table nil
1254 "Options concerning tables in Org-mode."
1258 (defcustom org-enable-table-editor
'optimized
1259 "Non-nil means lines starting with \"|\" are handled by the table editor.
1260 When nil, such lines will be treated like ordinary lines.
1262 When equal to the symbol `optimized', the table editor will be optimized to
1264 - Automatic overwrite mode in front of whitespace in table fields.
1265 This makes the structure of the table stay in tact as long as the edited
1266 field does not exceed the column width.
1267 - Minimize the number of realigns. Normally, the table is aligned each time
1268 TAB or RET are pressed to move to another field. With optimization this
1269 happens only if changes to a field might have changed the column width.
1270 Optimization requires replacing the functions `self-insert-command',
1271 `delete-char', and `backward-delete-char' in Org-mode buffers, with a
1272 slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
1273 very good at guessing when a re-align will be necessary, but you can always
1274 force one with \\[org-ctrl-c-ctrl-c].
1276 If you would like to use the optimized version in Org-mode, but the
1277 un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
1279 This variable can be used to turn on and off the table editor during a session,
1280 but in order to toggle optimization, a restart is required.
1282 See also the variable `org-table-auto-blank-field'."
1285 (const :tag
"off" nil
)
1287 (const :tag
"on, optimized" optimized
)))
1289 (defcustom org-self-insert-cluster-for-undo
(or (featurep 'xemacs
)
1290 (version<= emacs-version
"24.1"))
1291 "Non-nil means cluster self-insert commands for undo when possible.
1292 If this is set, then, like in the Emacs command loop, 20 consecutive
1293 characters will be undone together.
1294 This is configurable, because there is some impact on typing performance."
1298 (defcustom org-table-tab-recognizes-table.el t
1299 "Non-nil means TAB will automatically notice a table.el table.
1300 When it sees such a table, it moves point into it and - if necessary -
1301 calls `table-recognize-table'."
1302 :group
'org-table-editing
1305 (defgroup org-link nil
1306 "Options concerning links in Org-mode."
1310 (defvar org-link-abbrev-alist-local nil
1311 "Buffer-local version of `org-link-abbrev-alist', which see.
1312 The value of this is taken from the #+LINK lines.")
1313 (make-variable-buffer-local 'org-link-abbrev-alist-local
)
1315 (defcustom org-link-abbrev-alist nil
1316 "Alist of link abbreviations.
1317 The car of each element is a string, to be replaced at the start of a link.
1318 The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
1319 links in Org-mode buffers can have an optional tag after a double colon, e.g.
1321 [[linkkey:tag][description]]
1323 The 'linkkey' must be a word word, starting with a letter, followed
1324 by letters, numbers, '-' or '_'.
1326 If REPLACE is a string, the tag will simply be appended to create the link.
1327 If the string contains \"%s\", the tag will be inserted there. If the string
1328 contains \"%h\", it will cause a url-encoded version of the tag to be inserted
1329 at that point (see the function `url-hexify-string'). If the string contains
1330 the specifier \"%(my-function)\", then the custom function `my-function' will
1331 be invoked: this function takes the tag as its only argument and must return
1334 REPLACE may also be a function that will be called with the tag as the
1335 only argument to create the link, which should be returned as a string.
1337 See the manual for examples."
1341 (string :tag
"Protocol")
1343 (string :tag
"Format")
1346 (defcustom org-descriptive-links t
1347 "Non-nil means Org will display descriptive links.
1348 E.g. [[http://orgmode.org][Org website]] will be displayed as
1349 \"Org Website\", hiding the link itself and just displaying its
1350 description. When set to `nil', Org will display the full links
1353 You can interactively set the value of this variable by calling
1354 `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
1358 (defcustom org-link-file-path-type
'adaptive
1359 "How the path name in file links should be stored.
1362 relative Relative to the current directory, i.e. the directory of the file
1363 into which the link is being inserted.
1364 absolute Absolute path, if possible with ~ for home directory.
1365 noabbrev Absolute path, no abbreviation of home directory.
1366 adaptive Use relative path for files in the current directory and sub-
1367 directories of it. For other files, use an absolute path."
1375 (defcustom org-activate-links
'(bracket angle plain radio tag date footnote
)
1376 "Types of links that should be activated in Org-mode files.
1377 This is a list of symbols, each leading to the activation of a certain link
1378 type. In principle, it does not hurt to turn on most link types - there may
1379 be a small gain when turning off unused link types. The types are:
1381 bracket The recommended [[link][description]] or [[link]] links with hiding.
1382 angle Links in angular brackets that may contain whitespace like
1383 <bbdb:Carsten Dominik>.
1384 plain Plain links in normal text, no whitespace, like http://google.com.
1385 radio Text that is matched by a radio target, see manual for details.
1386 tag Tag settings in a headline (link to tag search).
1387 date Time stamps (link to calendar).
1388 footnote Footnote labels.
1390 Changing this variable requires a restart of Emacs to become effective."
1392 :type
'(set :greedy t
1393 (const :tag
"Double bracket links" bracket
)
1394 (const :tag
"Angular bracket links" angle
)
1395 (const :tag
"Plain text links" plain
)
1396 (const :tag
"Radio target matches" radio
)
1397 (const :tag
"Tags" tag
)
1398 (const :tag
"Timestamps" date
)
1399 (const :tag
"Footnotes" footnote
)))
1401 (defcustom org-make-link-description-function nil
1402 "Function to use for generating link descriptions from links.
1403 When nil, the link location will be used. This function must take
1404 two parameters: the first one is the link, the second one is the
1405 description generated by `org-insert-link'. The function should
1406 return the description to use."
1410 (defgroup org-link-store nil
1411 "Options concerning storing links in Org-mode."
1412 :tag
"Org Store Link"
1415 (defcustom org-url-hexify-p t
1416 "When non-nil, hexify URL when creating a link."
1419 :group
'org-link-store
)
1421 (defcustom org-email-link-description-format
"Email %c: %.30s"
1422 "Format of the description part of a link to an email or usenet message.
1423 The following %-escapes will be replaced by corresponding information:
1425 %F full \"From\" field
1426 %f name, taken from \"From\" field, address if no name
1427 %T full \"To\" field
1428 %t first name in \"To\" field, address if no name
1429 %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
1430 will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
1435 You may use normal field width specification between the % and the letter.
1436 This is for example useful to limit the length of the subject.
1438 Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
1439 :group
'org-link-store
1442 (defcustom org-from-is-user-regexp
1444 (when (and user-mail-address
(not (string= user-mail-address
"")))
1445 (setq r1
(concat "\\<" (regexp-quote user-mail-address
) "\\>")))
1446 (when (and user-full-name
(not (string= user-full-name
"")))
1447 (setq r2
(concat "\\<" (regexp-quote user-full-name
) "\\>")))
1448 (if (and r1 r2
) (concat r1
"\\|" r2
) (or r1 r2
)))
1449 "Regexp matched against the \"From:\" header of an email or usenet message.
1450 It should match if the message is from the user him/herself."
1451 :group
'org-link-store
1454 (defcustom org-context-in-file-links t
1455 "Non-nil means file links from `org-store-link' contain context.
1456 A search string will be added to the file name with :: as separator and
1457 used to find the context when the link is activated by the command
1458 `org-open-at-point'. When this option is t, the entire active region
1459 will be placed in the search string of the file link. If set to a
1460 positive integer, only the first n lines of context will be stored.
1462 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
1463 negates this setting for the duration of the command."
1464 :group
'org-link-store
1465 :type
'(choice boolean integer
))
1467 (defcustom org-keep-stored-link-after-insertion nil
1468 "Non-nil means keep link in list for entire session.
1470 The command `org-store-link' adds a link pointing to the current
1471 location to an internal list. These links accumulate during a session.
1472 The command `org-insert-link' can be used to insert links into any
1473 Org-mode file (offering completion for all stored links). When this
1474 option is nil, every link which has been inserted once using \\[org-insert-link]
1475 will be removed from the list, to make completing the unused links
1477 :group
'org-link-store
1480 (defgroup org-link-follow nil
1481 "Options concerning following links in Org-mode."
1482 :tag
"Org Follow Link"
1485 (defcustom org-link-translation-function nil
1486 "Function to translate links with different syntax to Org syntax.
1487 This can be used to translate links created for example by the Planner
1488 or emacs-wiki packages to Org syntax.
1489 The function must accept two parameters, a TYPE containing the link
1490 protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
1491 which is everything after the link protocol. It should return a cons
1492 with possibly modified values of type and path.
1493 Org contains a function for this, so if you set this variable to
1494 `org-translate-link-from-planner', you should be able follow many
1495 links created by planner."
1496 :group
'org-link-follow
1499 (defcustom org-follow-link-hook nil
1500 "Hook that is run after a link has been followed."
1501 :group
'org-link-follow
1504 (defcustom org-tab-follows-link nil
1505 "Non-nil means on links TAB will follow the link.
1506 Needs to be set before org.el is loaded.
1507 This really should not be used, it does not make sense, and the
1508 implementation is bad."
1509 :group
'org-link-follow
1512 (defcustom org-return-follows-link nil
1513 "Non-nil means on links RET will follow the link."
1514 :group
'org-link-follow
1517 (defcustom org-mouse-1-follows-link
1518 (if (boundp 'mouse-1-click-follows-link
) mouse-1-click-follows-link t
)
1519 "Non-nil means mouse-1 on a link will follow the link.
1520 A longer mouse click will still set point. Does not work on XEmacs.
1521 Needs to be set before org.el is loaded."
1522 :group
'org-link-follow
1525 (defcustom org-mark-ring-length
4
1526 "Number of different positions to be recorded in the ring.
1527 Changing this requires a restart of Emacs to work correctly."
1528 :group
'org-link-follow
1531 (defcustom org-link-search-must-match-exact-headline
'query-to-create
1532 "Non-nil means internal links in Org files must exactly match a headline.
1533 When nil, the link search tries to match a phrase with all words
1534 in the search text."
1535 :group
'org-link-follow
1538 (const :tag
"Use fuzzy text search" nil
)
1539 (const :tag
"Match only exact headline" t
)
1540 (const :tag
"Match exact headline or query to create it"
1543 (defcustom org-link-frame-setup
1544 '((vm . vm-visit-folder-other-frame
)
1545 (vm-imap . vm-visit-imap-folder-other-frame
)
1546 (gnus . org-gnus-no-new-news
)
1547 (file . find-file-other-window
)
1548 (wl . wl-other-frame
))
1549 "Setup the frame configuration for following links.
1550 When following a link with Emacs, it may often be useful to display
1551 this link in another window or frame. This variable can be used to
1552 set this up for the different types of links.
1555 `vm-visit-folder-other-window'
1556 `vm-visit-folder-other-frame'
1557 For Gnus, use any of
1560 `org-gnus-no-new-news'
1561 For FILE, use any of
1563 `find-file-other-window'
1564 `find-file-other-frame'
1565 For Wanderlust use any of
1568 For the calendar, use the variable `calendar-setup'.
1569 For BBDB, it is currently only possible to display the matches in
1571 :group
'org-link-follow
1575 (const vm-visit-folder
)
1576 (const vm-visit-folder-other-window
)
1577 (const vm-visit-folder-other-frame
)))
1581 (const gnus-other-frame
)
1582 (const org-gnus-no-new-news
)))
1586 (const find-file-other-window
)
1587 (const find-file-other-frame
)))
1591 (const wl-other-frame
)))))
1593 (defcustom org-display-internal-link-with-indirect-buffer nil
1594 "Non-nil means use indirect buffer to display infile links.
1595 Activating internal links (from one location in a file to another location
1596 in the same file) normally just jumps to the location. When the link is
1597 activated with a \\[universal-argument] prefix (or with mouse-3), the link \
1599 another window. When this option is set, the other window actually displays
1600 an indirect buffer clone of the current buffer, to avoid any visibility
1601 changes to the current buffer."
1602 :group
'org-link-follow
1605 (defcustom org-open-non-existing-files nil
1606 "Non-nil means `org-open-file' will open non-existing files.
1607 When nil, an error will be generated.
1608 This variable applies only to external applications because they
1609 might choke on non-existing files. If the link is to a file that
1610 will be opened in Emacs, the variable is ignored."
1611 :group
'org-link-follow
1614 (defcustom org-open-directory-means-index-dot-org nil
1615 "Non-nil means a link to a directory really means to index.org.
1616 When nil, following a directory link will run dired or open a finder/explorer
1617 window on that directory."
1618 :group
'org-link-follow
1621 (defcustom org-link-mailto-program
'(browse-url "mailto:%a?subject=%s")
1622 "Function and arguments to call for following mailto links.
1623 This is a list with the first element being a Lisp function, and the
1624 remaining elements being arguments to the function. In string arguments,
1625 %a will be replaced by the address, and %s will be replaced by the subject
1626 if one was given like in <mailto:arthur@galaxy.org::this subject>."
1627 :group
'org-link-follow
1629 (const :tag
"browse-url" (browse-url-mail "mailto:%a?subject=%s"))
1630 (const :tag
"compose-mail" (compose-mail "%a" "%s"))
1631 (const :tag
"message-mail" (message-mail "%a" "%s"))
1632 (cons :tag
"other" (function) (repeat :tag
"argument" sexp
))))
1634 (defcustom org-confirm-shell-link-function
'yes-or-no-p
1635 "Non-nil means ask for confirmation before executing shell links.
1636 Shell links can be dangerous: just think about a link
1638 [[shell:rm -rf ~/*][Google Search]]
1640 This link would show up in your Org-mode document as \"Google Search\",
1641 but really it would remove your entire home directory.
1642 Therefore we advise against setting this variable to nil.
1643 Just change it to `y-or-n-p' if you want to confirm with a
1644 single keystroke rather than having to type \"yes\"."
1645 :group
'org-link-follow
1647 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1648 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1649 (const :tag
"no confirmation (dangerous)" nil
)))
1650 (put 'org-confirm-shell-link-function
1651 'safe-local-variable
1652 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1654 (defcustom org-confirm-shell-link-not-regexp
""
1655 "A regexp to skip confirmation for shell links."
1656 :group
'org-link-follow
1660 (defcustom org-confirm-elisp-link-function
'yes-or-no-p
1661 "Non-nil means ask for confirmation before executing Emacs Lisp links.
1662 Elisp links can be dangerous: just think about a link
1664 [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
1666 This link would show up in your Org-mode document as \"Google Search\",
1667 but really it would remove your entire home directory.
1668 Therefore we advise against setting this variable to nil.
1669 Just change it to `y-or-n-p' if you want to confirm with a
1670 single keystroke rather than having to type \"yes\"."
1671 :group
'org-link-follow
1673 (const :tag
"with yes-or-no (safer)" yes-or-no-p
)
1674 (const :tag
"with y-or-n (faster)" y-or-n-p
)
1675 (const :tag
"no confirmation (dangerous)" nil
)))
1676 (put 'org-confirm-shell-link-function
1677 'safe-local-variable
1678 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
1680 (defcustom org-confirm-elisp-link-not-regexp
""
1681 "A regexp to skip confirmation for Elisp links."
1682 :group
'org-link-follow
1686 (defconst org-file-apps-defaults-gnu
1690 "Default file applications on a UNIX or GNU/Linux system.
1691 See `org-file-apps'.")
1693 (defconst org-file-apps-defaults-macosx
1696 (system .
"open %s")
1698 ("eps.gz" .
"gv %s")
1700 ("fig" .
"xfig %s"))
1701 "Default file applications on a MacOS X system.
1702 The system \"open\" is known as a default, but we use X11 applications
1703 for some files for which the OS does not have a good default.
1704 See `org-file-apps'.")
1706 (defconst org-file-apps-defaults-windowsnt
1710 (list (if (featurep 'xemacs
)
1711 'mswindows-shell-execute
1715 (list (if (featurep 'xemacs
)
1716 'mswindows-shell-execute
1719 "Default file applications on a Windows NT system.
1720 The system \"open\" is used for most files.
1721 See `org-file-apps'.")
1723 (defcustom org-file-apps
1726 ("\\.mm\\'" . default
)
1727 ("\\.x?html?\\'" . default
)
1728 ("\\.pdf\\'" . default
)
1730 "External applications for opening `file:path' items in a document.
1731 Org-mode uses system defaults for different file types, but
1732 you can use this variable to set the application for a given file
1733 extension. The entries in this list are cons cells where the car identifies
1734 files and the cdr the corresponding command. Possible values for the
1736 \"string\" A string as a file identifier can be interpreted in different
1737 ways, depending on its contents:
1739 - Alphanumeric characters only:
1740 Match links with this file extension.
1741 Example: (\"pdf\" . \"evince %s\")
1742 to open PDFs with evince.
1744 - Regular expression: Match links where the
1745 filename matches the regexp. If you want to
1746 use groups here, use shy groups.
1748 Example: (\"\\.x?html\\'\" . \"firefox %s\")
1749 (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
1750 to open *.html and *.xhtml with firefox.
1752 - Regular expression which contains (non-shy) groups:
1753 Match links where the whole link, including \"::\", and
1754 anything after that, matches the regexp.
1755 In a custom command string, %1, %2, etc. are replaced with
1756 the parts of the link that were matched by the groups.
1757 For backwards compatibility, if a command string is given
1758 that does not use any of the group matches, this case is
1759 handled identically to the second one (i.e. match against
1761 In a custom lisp form, you can access the group matches with
1762 (match-string n link).
1764 Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
1765 to open [[file:document.pdf::5]] with evince at page 5.
1767 `directory' Matches a directory
1768 `remote' Matches a remote file, accessible through tramp or efs.
1769 Remote files most likely should be visited through Emacs
1770 because external applications cannot handle such paths.
1771 `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
1772 so all files Emacs knows how to handle. Using this with
1773 command `emacs' will open most files in Emacs. Beware that this
1774 will also open html files inside Emacs, unless you add
1775 (\"html\" . default) to the list as well.
1776 t Default for files not matched by any of the other options.
1777 `system' The system command to open files, like `open' on Windows
1778 and Mac OS X, and mailcap under GNU/Linux. This is the command
1779 that will be selected if you call `C-c C-o' with a double
1780 \\[universal-argument] \\[universal-argument] prefix.
1782 Possible values for the command are:
1783 `emacs' The file will be visited by the current Emacs process.
1784 `default' Use the default application for this file type, which is the
1785 association for t in the list, most likely in the system-specific
1787 This can be used to overrule an unwanted setting in the
1788 system-specific variable.
1789 `system' Use the system command for opening files, like \"open\".
1790 This command is specified by the entry whose car is `system'.
1791 Most likely, the system-specific version of this variable
1792 does define this command, but you can overrule/replace it
1794 string A command to be executed by a shell; %s will be replaced
1795 by the path to the file.
1796 sexp A Lisp form which will be evaluated. The file path will
1797 be available in the Lisp variable `file'.
1798 For more examples, see the system specific constants
1799 `org-file-apps-defaults-macosx'
1800 `org-file-apps-defaults-windowsnt'
1801 `org-file-apps-defaults-gnu'."
1802 :group
'org-link-follow
1804 (cons (choice :value
""
1805 (string :tag
"Extension")
1806 (const :tag
"System command to open files" system
)
1807 (const :tag
"Default for unrecognized files" t
)
1808 (const :tag
"Remote file" remote
)
1809 (const :tag
"Links to a directory" directory
)
1810 (const :tag
"Any files that have Emacs modes"
1813 (const :tag
"Visit with Emacs" emacs
)
1814 (const :tag
"Use default" default
)
1815 (const :tag
"Use the system command" system
)
1816 (string :tag
"Command")
1817 (sexp :tag
"Lisp form")))))
1819 (defcustom org-doi-server-url
"http://dx.doi.org/"
1820 "The URL of the DOI server."
1823 :group
'org-link-follow
)
1825 (defgroup org-refile nil
1826 "Options concerning refiling entries in Org-mode."
1830 (defcustom org-directory
"~/org"
1831 "Directory with org files.
1832 This is just a default location to look for Org files. There is no need
1833 at all to put your files into this directory. It is only used in the
1834 following situations:
1836 1. When a capture template specifies a target file that is not an
1837 absolute path. The path will then be interpreted relative to
1839 2. When a capture note is filed away in an interactive way (when exiting the
1840 note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
1841 with `org-directory' as the default path."
1843 :group
'org-remember
1847 (defcustom org-default-notes-file
(convert-standard-filename "~/.notes")
1848 "Default target for storing notes.
1849 Used as a fall back file for org-remember.el and org-capture.el, for
1850 templates that do not specify a target file."
1852 :group
'org-remember
1855 (const :tag
"Default from remember-data-file" nil
)
1858 (defcustom org-goto-interface
'outline
1859 "The default interface to be used for `org-goto'.
1861 outline The interface shows an outline of the relevant file
1862 and the correct heading is found by moving through
1863 the outline or by searching with incremental search.
1864 outline-path-completion Headlines in the current buffer are offered via
1865 completion. This is the interface also used by
1866 the refile command."
1869 (const :tag
"Outline" outline
)
1870 (const :tag
"Outline-path-completion" outline-path-completion
)))
1872 (defcustom org-goto-max-level
5
1873 "Maximum target level when running `org-goto' with refile interface."
1877 (defcustom org-reverse-note-order nil
1878 "Non-nil means store new notes at the beginning of a file or entry.
1879 When nil, new notes will be filed to the end of a file or entry.
1880 This can also be a list with cons cells of regular expressions that
1881 are matched against file names, and values."
1882 :group
'org-remember
1886 (const :tag
"Reverse always" t
)
1887 (const :tag
"Reverse never" nil
)
1888 (repeat :tag
"By file name regexp"
1889 (cons regexp boolean
))))
1891 (defcustom org-log-refile nil
1892 "Information to record when a task is refiled.
1894 Possible values are:
1896 nil Don't add anything
1897 time Add a time stamp to the task
1898 note Prompt for a note and add it with template `org-log-note-headings'
1900 This option can also be set with on a per-file-basis with
1902 #+STARTUP: nologrefile
1903 #+STARTUP: logrefile
1904 #+STARTUP: lognoterefile
1906 You can have local logging settings for a subtree by setting the LOGGING
1907 property to one or more of these keywords.
1909 When bulk-refiling from the agenda, the value `note' is forbidden and
1910 will temporarily be changed to `time'."
1912 :group
'org-progress
1915 (const :tag
"No logging" nil
)
1916 (const :tag
"Record timestamp" time
)
1917 (const :tag
"Record timestamp with note." note
)))
1919 (defcustom org-refile-targets nil
1920 "Targets for refiling entries with \\[org-refile].
1921 This is a list of cons cells. Each cell contains:
1922 - a specification of the files to be considered, either a list of files,
1923 or a symbol whose function or variable value will be used to retrieve
1924 a file name or a list of file names. If you use `org-agenda-files' for
1925 that, all agenda files will be scanned for targets. Nil means consider
1926 headings in the current buffer.
1927 - A specification of how to find candidate refile targets. This may be
1929 - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1930 This tag has to be present in all target headlines, inheritance will
1932 - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
1934 - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
1935 headlines that are refiling targets.
1936 - a cons cell (:level . N). Any headline of level N is considered a target.
1937 Note that, when `org-odd-levels-only' is set, level corresponds to
1938 order in hierarchy, not to the number of stars.
1939 - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1940 Note that, when `org-odd-levels-only' is set, level corresponds to
1941 order in hierarchy, not to the number of stars.
1943 Each element of this list generates a set of possible targets.
1944 The union of these sets is presented (with completion) to
1945 the user by `org-refile'.
1947 You can set the variable `org-refile-target-verify-function' to a function
1948 to verify each headline found by the simple criteria above.
1950 When this variable is nil, all top-level headlines in the current buffer
1951 are used, equivalent to the value `((nil . (:level . 1))'."
1955 (choice :value org-agenda-files
1956 (const :tag
"All agenda files" org-agenda-files
)
1957 (const :tag
"Current buffer" nil
)
1958 (function) (variable) (file))
1959 (choice :tag
"Identify target headline by"
1960 (cons :tag
"Specific tag" (const :value
:tag
) (string))
1961 (cons :tag
"TODO keyword" (const :value
:todo
) (string))
1962 (cons :tag
"Regular expression" (const :value
:regexp
) (regexp))
1963 (cons :tag
"Level number" (const :value
:level
) (integer))
1964 (cons :tag
"Max Level number" (const :value
:maxlevel
) (integer))))))
1966 (defcustom org-refile-target-verify-function nil
1967 "Function to verify if the headline at point should be a refile target.
1968 The function will be called without arguments, with point at the
1969 beginning of the headline. It should return t and leave point
1970 where it is if the headline is a valid target for refiling.
1972 If the target should not be selected, the function must return nil.
1973 In addition to this, it may move point to a place from where the search
1974 should be continued. For example, the function may decide that the entire
1975 subtree of the current entry should be excluded and move point to the end
1980 (defcustom org-refile-use-cache nil
1981 "Non-nil means cache refile targets to speed up the process.
1982 The cache for a particular file will be updated automatically when
1983 the buffer has been killed, or when any of the marker used for flagging
1984 refile targets no longer points at a live buffer.
1985 If you have added new entries to a buffer that might themselves be targets,
1986 you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
1987 find that easier, `C-u C-u C-u C-c C-w'."
1992 (defcustom org-refile-use-outline-path nil
1993 "Non-nil means provide refile targets as paths.
1994 So a level 3 headline will be available as level1/level2/level3.
1996 When the value is `file', also include the file name (without directory)
1997 into the path. In this case, you can also stop the completion after
1998 the file name, to get entries inserted as top level in the file.
2000 When `full-file-path', include the full file path."
2003 (const :tag
"Not" nil
)
2004 (const :tag
"Yes" t
)
2005 (const :tag
"Start with file name" file
)
2006 (const :tag
"Start with full file path" full-file-path
)))
2008 (defcustom org-outline-path-complete-in-steps t
2009 "Non-nil means complete the outline path in hierarchical steps.
2010 When Org-mode uses the refile interface to select an outline path
2011 \(see variable `org-refile-use-outline-path'), the completion of
2012 the path can be done is a single go, or if can be done in steps down
2013 the headline hierarchy. Going in steps is probably the best if you
2014 do not use a special completion package like `ido' or `icicles'.
2015 However, when using these packages, going in one step can be very
2016 fast, while still showing the whole path to the entry."
2020 (defcustom org-refile-allow-creating-parent-nodes nil
2021 "Non-nil means allow to create new nodes as refile targets.
2022 New nodes are then created by adding \"/new node name\" to the completion
2023 of an existing node. When the value of this variable is `confirm',
2024 new node creation must be confirmed by the user (recommended)
2025 When nil, the completion must match an existing entry.
2027 Note that, if the new heading is not seen by the criteria
2028 listed in `org-refile-targets', multiple instances of the same
2029 heading would be created by trying again to file under the new
2033 (const :tag
"Never" nil
)
2034 (const :tag
"Always" t
)
2035 (const :tag
"Prompt for confirmation" confirm
)))
2037 (defcustom org-refile-active-region-within-subtree nil
2038 "Non-nil means also refile active region within a subtree.
2040 By default `org-refile' doesn't allow refiling regions if they
2041 don't contain a set of subtrees, but it might be convenient to
2042 do so sometimes: in that case, the first line of the region is
2043 converted to a headline before refiling."
2048 (defgroup org-todo nil
2049 "Options concerning TODO items in Org-mode."
2053 (defgroup org-progress nil
2054 "Options concerning Progress logging in Org-mode."
2058 (defvar org-todo-interpretation-widgets
2059 '((:tag
"Sequence (cycling hits every state)" sequence
)
2060 (:tag
"Type (cycling directly to DONE)" type
))
2061 "The available interpretation symbols for customizing `org-todo-keywords'.
2062 Interested libraries should add to this list.")
2064 (defcustom org-todo-keywords
'((sequence "TODO" "DONE"))
2065 "List of TODO entry keyword sequences and their interpretation.
2066 \\<org-mode-map>This is a list of sequences.
2068 Each sequence starts with a symbol, either `sequence' or `type',
2069 indicating if the keywords should be interpreted as a sequence of
2070 action steps, or as different types of TODO items. The first
2071 keywords are states requiring action - these states will select a headline
2072 for inclusion into the global TODO list Org-mode produces. If one of
2073 the \"keywords\" is the vertical bar, \"|\", the remaining keywords
2074 signify that no further action is necessary. If \"|\" is not found,
2075 the last keyword is treated as the only DONE state of the sequence.
2077 The command \\[org-todo] cycles an entry through these states, and one
2078 additional state where no keyword is present. For details about this
2079 cycling, see the manual.
2081 TODO keywords and interpretation can also be set on a per-file basis with
2082 the special #+SEQ_TODO and #+TYP_TODO lines.
2084 Each keyword can optionally specify a character for fast state selection
2085 \(in combination with the variable `org-use-fast-todo-selection')
2086 and specifiers for state change logging, using the same syntax that
2087 is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
2088 the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
2089 indicates to record a time stamp each time this state is selected.
2091 Each keyword may also specify if a timestamp or a note should be
2092 recorded when entering or leaving the state, by adding additional
2093 characters in the parenthesis after the keyword. This looks like this:
2094 \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
2095 record only the time of the state change. With X and Y being either
2096 \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
2097 Y when leaving the state if and only if the *target* state does not
2098 define X. You may omit any of the fast-selection key or X or /Y,
2099 so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
2101 For backward compatibility, this variable may also be just a list
2102 of keywords. In this case the interpretation (sequence or type) will be
2103 taken from the (otherwise obsolete) variable `org-todo-interpretation'."
2105 :group
'org-keywords
2107 (repeat :tag
"Old syntax, just keywords"
2108 (string :tag
"Keyword"))
2109 (repeat :tag
"New syntax"
2112 :tag
"Interpretation"
2113 ;;Quick and dirty way to see
2114 ;;`org-todo-interpretations'. This takes the
2115 ;;place of item arguments
2123 org-todo-interpretation-widgets
))
2126 (string :tag
"Keyword"))))))
2128 (defvar org-todo-keywords-1 nil
2129 "All TODO and DONE keywords active in a buffer.")
2130 (make-variable-buffer-local 'org-todo-keywords-1
)
2131 (defvar org-todo-keywords-for-agenda nil
)
2132 (defvar org-done-keywords-for-agenda nil
)
2133 (defvar org-drawers-for-agenda nil
)
2134 (defvar org-todo-keyword-alist-for-agenda nil
)
2135 (defvar org-tag-alist-for-agenda nil
)
2136 (defvar org-agenda-contributing-files nil
)
2137 (defvar org-not-done-keywords nil
)
2138 (make-variable-buffer-local 'org-not-done-keywords
)
2139 (defvar org-done-keywords nil
)
2140 (make-variable-buffer-local 'org-done-keywords
)
2141 (defvar org-todo-heads nil
)
2142 (make-variable-buffer-local 'org-todo-heads
)
2143 (defvar org-todo-sets nil
)
2144 (make-variable-buffer-local 'org-todo-sets
)
2145 (defvar org-todo-log-states nil
)
2146 (make-variable-buffer-local 'org-todo-log-states
)
2147 (defvar org-todo-kwd-alist nil
)
2148 (make-variable-buffer-local 'org-todo-kwd-alist
)
2149 (defvar org-todo-key-alist nil
)
2150 (make-variable-buffer-local 'org-todo-key-alist
)
2151 (defvar org-todo-key-trigger nil
)
2152 (make-variable-buffer-local 'org-todo-key-trigger
)
2154 (defcustom org-todo-interpretation
'sequence
2155 "Controls how TODO keywords are interpreted.
2156 This variable is in principle obsolete and is only used for
2157 backward compatibility, if the interpretation of todo keywords is
2158 not given already in `org-todo-keywords'. See that variable for
2161 :group
'org-keywords
2162 :type
'(choice (const sequence
)
2165 (defcustom org-use-fast-todo-selection t
2166 "Non-nil means use the fast todo selection scheme with C-c C-t.
2167 This variable describes if and under what circumstances the cycling
2168 mechanism for TODO keywords will be replaced by a single-key, direct
2171 When nil, fast selection is never used.
2173 When the symbol `prefix', it will be used when `org-todo' is called
2174 with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
2175 `C-u t' in an agenda buffer.
2177 When t, fast selection is used by default. In this case, the prefix
2178 argument forces cycling instead.
2180 In all cases, the special interface is only used if access keys have
2181 actually been assigned by the user, i.e. if keywords in the configuration
2182 are followed by a letter in parenthesis, like TODO(t)."
2185 (const :tag
"Never" nil
)
2186 (const :tag
"By default" t
)
2187 (const :tag
"Only with C-u C-c C-t" prefix
)))
2189 (defcustom org-provide-todo-statistics t
2190 "Non-nil means update todo statistics after insert and toggle.
2191 ALL-HEADLINES means update todo statistics by including headlines
2192 with no TODO keyword as well, counting them as not done.
2193 A list of TODO keywords means the same, but skip keywords that are
2196 When this is set, todo statistics is updated in the parent of the
2197 current entry each time a todo state is changed."
2200 (const :tag
"Yes, only for TODO entries" t
)
2201 (const :tag
"Yes, including all entries" 'all-headlines
)
2202 (repeat :tag
"Yes, for TODOs in this list"
2203 (string :tag
"TODO keyword"))
2204 (other :tag
"No TODO statistics" nil
)))
2206 (defcustom org-hierarchical-todo-statistics t
2207 "Non-nil means TODO statistics covers just direct children.
2208 When nil, all entries in the subtree are considered.
2209 This has only an effect if `org-provide-todo-statistics' is set.
2210 To set this to nil for only a single subtree, use a COOKIE_DATA
2211 property and include the word \"recursive\" into the value."
2215 (defcustom org-after-todo-state-change-hook nil
2216 "Hook which is run after the state of a TODO item was changed.
2217 The new state (a string with a TODO keyword, or nil) is available in the
2218 Lisp variable `org-state'."
2222 (defvar org-blocker-hook nil
2223 "Hook for functions that are allowed to block a state change.
2225 Each function gets as its single argument a property list, see
2226 `org-trigger-hook' for more information about this list.
2228 If any of the functions in this hook returns nil, the state change
2231 (defvar org-trigger-hook nil
2232 "Hook for functions that are triggered by a state change.
2234 Each function gets as its single argument a property list with at least
2235 the following elements:
2237 (:type type-of-change :position pos-at-entry-start
2238 :from old-state :to new-state)
2240 Depending on the type, more properties may be present.
2242 This mechanism is currently implemented for:
2246 :type todo-state-change
2247 :from previous state (keyword as a string), or nil, or a symbol
2248 'todo' or 'done', to indicate the general type of state.
2249 :to new state, like in :from")
2251 (defcustom org-enforce-todo-dependencies nil
2252 "Non-nil means undone TODO entries will block switching the parent to DONE.
2253 Also, if a parent has an :ORDERED: property, switching an entry to DONE will
2254 be blocked if any prior sibling is not yet done.
2255 Finally, if the parent is blocked because of ordered siblings of its own,
2256 the child will also be blocked."
2257 :set
(lambda (var val
)
2260 (add-hook 'org-blocker-hook
2261 'org-block-todo-from-children-or-siblings-or-parent
)
2262 (remove-hook 'org-blocker-hook
2263 'org-block-todo-from-children-or-siblings-or-parent
)))
2267 (defcustom org-enforce-todo-checkbox-dependencies nil
2268 "Non-nil means unchecked boxes will block switching the parent to DONE.
2269 When this is nil, checkboxes have no influence on switching TODO states.
2270 When non-nil, you first need to check off all check boxes before the TODO
2271 entry can be switched to DONE.
2272 This variable needs to be set before org.el is loaded, and you need to
2273 restart Emacs after a change to make the change effective. The only way
2274 to change is while Emacs is running is through the customize interface."
2275 :set
(lambda (var val
)
2278 (add-hook 'org-blocker-hook
2279 'org-block-todo-from-checkboxes
)
2280 (remove-hook 'org-blocker-hook
2281 'org-block-todo-from-checkboxes
)))
2285 (defcustom org-treat-insert-todo-heading-as-state-change nil
2286 "Non-nil means inserting a TODO heading is treated as state change.
2287 So when the command \\[org-insert-todo-heading] is used, state change
2288 logging will apply if appropriate. When nil, the new TODO item will
2289 be inserted directly, and no logging will take place."
2293 (defcustom org-treat-S-cursor-todo-selection-as-state-change t
2294 "Non-nil means switching TODO states with S-cursor counts as state change.
2295 This is the default behavior. However, setting this to nil allows a
2296 convenient way to select a TODO state and bypass any logging associated
2301 (defcustom org-todo-state-tags-triggers nil
2302 "Tag changes that should be triggered by TODO state changes.
2303 This is a list. Each entry is
2305 (state-change (tag . flag) .......)
2307 State-change can be a string with a state, and empty string to indicate the
2308 state that has no TODO keyword, or it can be one of the symbols `todo'
2309 or `done', meaning any not-done or done state, respectively."
2313 (cons (choice :tag
"When changing to"
2314 (const :tag
"Not-done state" todo
)
2315 (const :tag
"Done state" done
)
2316 (string :tag
"State"))
2318 (cons :tag
"Tag action"
2320 (choice (const :tag
"Add" t
) (const :tag
"Remove" nil
)))))))
2322 (defcustom org-log-done nil
2323 "Information to record when a task moves to the DONE state.
2325 Possible values are:
2327 nil Don't add anything, just change the keyword
2328 time Add a time stamp to the task
2329 note Prompt for a note and add it with template `org-log-note-headings'
2331 This option can also be set with on a per-file-basis with
2333 #+STARTUP: nologdone
2335 #+STARTUP: lognotedone
2337 You can have local logging settings for a subtree by setting the LOGGING
2338 property to one or more of these keywords."
2340 :group
'org-progress
2342 (const :tag
"No logging" nil
)
2343 (const :tag
"Record CLOSED timestamp" time
)
2344 (const :tag
"Record CLOSED timestamp with note." note
)))
2346 ;; Normalize old uses of org-log-done.
2348 ((eq org-log-done t
) (setq org-log-done
'time
))
2349 ((and (listp org-log-done
) (memq 'done org-log-done
))
2350 (setq org-log-done
'note
)))
2352 (defcustom org-log-reschedule nil
2353 "Information to record when the scheduling date of a tasks is modified.
2355 Possible values are:
2357 nil Don't add anything, just change the date
2358 time Add a time stamp to the task
2359 note Prompt for a note and add it with template `org-log-note-headings'
2361 This option can also be set with on a per-file-basis with
2363 #+STARTUP: nologreschedule
2364 #+STARTUP: logreschedule
2365 #+STARTUP: lognotereschedule"
2367 :group
'org-progress
2369 (const :tag
"No logging" nil
)
2370 (const :tag
"Record timestamp" time
)
2371 (const :tag
"Record timestamp with note." note
)))
2373 (defcustom org-log-redeadline nil
2374 "Information to record when the deadline date of a tasks is modified.
2376 Possible values are:
2378 nil Don't add anything, just change the date
2379 time Add a time stamp to the task
2380 note Prompt for a note and add it with template `org-log-note-headings'
2382 This option can also be set with on a per-file-basis with
2384 #+STARTUP: nologredeadline
2385 #+STARTUP: logredeadline
2386 #+STARTUP: lognoteredeadline
2388 You can have local logging settings for a subtree by setting the LOGGING
2389 property to one or more of these keywords."
2391 :group
'org-progress
2393 (const :tag
"No logging" nil
)
2394 (const :tag
"Record timestamp" time
)
2395 (const :tag
"Record timestamp with note." note
)))
2397 (defcustom org-log-note-clock-out nil
2398 "Non-nil means record a note when clocking out of an item.
2399 This can also be configured on a per-file basis by adding one of
2400 the following lines anywhere in the buffer:
2402 #+STARTUP: lognoteclock-out
2403 #+STARTUP: nolognoteclock-out"
2405 :group
'org-progress
2408 (defcustom org-log-done-with-time t
2409 "Non-nil means the CLOSED time stamp will contain date and time.
2410 When nil, only the date will be recorded."
2411 :group
'org-progress
2414 (defcustom org-log-note-headings
2415 '((done .
"CLOSING NOTE %t")
2416 (state .
"State %-12s from %-12S %t")
2417 (note .
"Note taken on %t")
2418 (reschedule .
"Rescheduled from %S on %t")
2419 (delschedule .
"Not scheduled, was %S on %t")
2420 (redeadline .
"New deadline from %S on %t")
2421 (deldeadline .
"Removed deadline, was %S on %t")
2422 (refile .
"Refiled on %t")
2424 "Headings for notes added to entries.
2425 The value is an alist, with the car being a symbol indicating the note
2426 context, and the cdr is the heading to be used. The heading may also be the
2428 %t in the heading will be replaced by a time stamp.
2429 %T will be an active time stamp instead the default inactive one
2430 %d will be replaced by a short-format time stamp.
2431 %D will be replaced by an active short-format time stamp.
2432 %s will be replaced by the new TODO state, in double quotes.
2433 %S will be replaced by the old TODO state, in double quotes.
2434 %u will be replaced by the user name.
2435 %U will be replaced by the full user name.
2437 In fact, it is not a good idea to change the `state' entry, because
2438 agenda log mode depends on the format of these entries."
2440 :group
'org-progress
2441 :type
'(list :greedy t
2442 (cons (const :tag
"Heading when closing an item" done
) string
)
2444 "Heading when changing todo state (todo sequence only)"
2446 (cons (const :tag
"Heading when just taking a note" note
) string
)
2447 (cons (const :tag
"Heading when clocking out" clock-out
) string
)
2448 (cons (const :tag
"Heading when an item is no longer scheduled" delschedule
) string
)
2449 (cons (const :tag
"Heading when rescheduling" reschedule
) string
)
2450 (cons (const :tag
"Heading when changing deadline" redeadline
) string
)
2451 (cons (const :tag
"Heading when deleting a deadline" deldeadline
) string
)
2452 (cons (const :tag
"Heading when refiling" refile
) string
)))
2454 (unless (assq 'note org-log-note-headings
)
2455 (push '(note .
"%t") org-log-note-headings
))
2457 (defcustom org-log-into-drawer nil
2458 "Non-nil means insert state change notes and time stamps into a drawer.
2459 When nil, state changes notes will be inserted after the headline and
2460 any scheduling and clock lines, but not inside a drawer.
2462 The value of this variable should be the name of the drawer to use.
2463 LOGBOOK is proposed as the default drawer for this purpose, you can
2464 also set this to a string to define the drawer of your choice.
2466 A value of t is also allowed, representing \"LOGBOOK\".
2468 If this variable is set, `org-log-state-notes-insert-after-drawers'
2471 You can set the property LOG_INTO_DRAWER to overrule this setting for
2474 :group
'org-progress
2476 (const :tag
"Not into a drawer" nil
)
2477 (const :tag
"LOGBOOK" t
)
2478 (string :tag
"Other")))
2480 (if (fboundp 'defvaralias
)
2481 (defvaralias 'org-log-state-notes-into-drawer
'org-log-into-drawer
))
2483 (defun org-log-into-drawer ()
2484 "Return the value of `org-log-into-drawer', but let properties overrule.
2485 If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
2486 used instead of the default value."
2487 (let ((p (org-entry-get nil
"LOG_INTO_DRAWER" 'inherit
)))
2489 ((or (not p
) (equal p
"nil")) org-log-into-drawer
)
2490 ((equal p
"t") "LOGBOOK")
2493 (defcustom org-log-state-notes-insert-after-drawers nil
2494 "Non-nil means insert state change notes after any drawers in entry.
2495 Only the drawers that *immediately* follow the headline and the
2496 deadline/scheduled line are skipped.
2497 When nil, insert notes right after the heading and perhaps the line
2498 with deadline/scheduling if present.
2500 This variable will have no effect if `org-log-into-drawer' is
2503 :group
'org-progress
2506 (defcustom org-log-states-order-reversed t
2507 "Non-nil means the latest state note will be directly after heading.
2508 When nil, the state change notes will be ordered according to time."
2510 :group
'org-progress
2513 (defcustom org-todo-repeat-to-state nil
2514 "The TODO state to which a repeater should return the repeating task.
2515 By default this is the first task in a TODO sequence, or the previous state
2516 in a TODO_TYP set. But you can specify another task here.
2517 alternatively, set the :REPEAT_TO_STATE: property of the entry."
2520 :type
'(choice (const :tag
"Head of sequence" nil
)
2521 (string :tag
"Specific state")))
2523 (defcustom org-log-repeat
'time
2524 "Non-nil means record moving through the DONE state when triggering repeat.
2525 An auto-repeating task is immediately switched back to TODO when
2526 marked DONE. If you are not logging state changes (by adding \"@\"
2527 or \"!\" to the TODO keyword definition), or set `org-log-done' to
2528 record a closing note, there will be no record of the task moving
2529 through DONE. This variable forces taking a note anyway.
2531 nil Don't force a record
2532 time Record a time stamp
2533 note Prompt for a note and add it with template `org-log-note-headings'
2535 This option can also be set with on a per-file-basis with
2537 #+STARTUP: nologrepeat
2538 #+STARTUP: logrepeat
2539 #+STARTUP: lognoterepeat
2541 You can have local logging settings for a subtree by setting the LOGGING
2542 property to one or more of these keywords."
2544 :group
'org-progress
2546 (const :tag
"Don't force a record" nil
)
2547 (const :tag
"Force recording the DONE state" time
)
2548 (const :tag
"Force recording a note with the DONE state" note
)))
2551 (defgroup org-priorities nil
2552 "Priorities in Org-mode."
2553 :tag
"Org Priorities"
2556 (defcustom org-enable-priority-commands t
2557 "Non-nil means priority commands are active.
2558 When nil, these commands will be disabled, so that you never accidentally
2560 :group
'org-priorities
2563 (defcustom org-highest-priority ?A
2564 "The highest priority of TODO items. A character like ?A, ?B etc.
2565 Must have a smaller ASCII number than `org-lowest-priority'."
2566 :group
'org-priorities
2569 (defcustom org-lowest-priority ?C
2570 "The lowest priority of TODO items. A character like ?A, ?B etc.
2571 Must have a larger ASCII number than `org-highest-priority'."
2572 :group
'org-priorities
2575 (defcustom org-default-priority ?B
2576 "The default priority of TODO items.
2577 This is the priority an item gets if no explicit priority is given.
2578 When starting to cycle on an empty priority the first step in the cycle
2579 depends on `org-priority-start-cycle-with-default'. The resulting first
2580 step priority must not exceed the range from `org-highest-priority' to
2581 `org-lowest-priority' which means that `org-default-priority' has to be
2582 in this range exclusive or inclusive the range boundaries. Else the
2583 first step refuses to set the default and the second will fall back
2584 to (depending on the command used) the highest or lowest priority."
2585 :group
'org-priorities
2588 (defcustom org-priority-start-cycle-with-default t
2589 "Non-nil means start with default priority when starting to cycle.
2590 When this is nil, the first step in the cycle will be (depending on the
2591 command used) one higher or lower than the default priority.
2592 See also `org-default-priority'."
2593 :group
'org-priorities
2596 (defcustom org-get-priority-function nil
2597 "Function to extract the priority from a string.
2598 The string is normally the headline. If this is nil Org computes the
2599 priority from the priority cookie like [#A] in the headline. It returns
2600 an integer, increasing by 1000 for each priority level.
2601 The user can set a different function here, which should take a string
2602 as an argument and return the numeric priority."
2603 :group
'org-priorities
2607 (defgroup org-time nil
2608 "Options concerning time stamps and deadlines in Org-mode."
2612 (defcustom org-insert-labeled-timestamps-at-point nil
2613 "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
2614 When nil, these labeled time stamps are forces into the second line of an
2615 entry, just after the headline. When scheduling from the global TODO list,
2616 the time stamp will always be forced into the second line."
2620 (defconst org-time-stamp-formats
'("<%Y-%m-%d %a>" .
"<%Y-%m-%d %a %H:%M>")
2621 "Formats for `format-time-string' which are used for time stamps.
2622 It is not recommended to change this constant.")
2624 (defcustom org-time-stamp-rounding-minutes
'(0 5)
2625 "Number of minutes to round time stamps to.
2626 These are two values, the first applies when first creating a time stamp.
2627 The second applies when changing it with the commands `S-up' and `S-down'.
2628 When changing the time stamp, this means that it will change in steps
2629 of N minutes, as given by the second value.
2631 When a setting is 0 or 1, insert the time unmodified. Useful rounding
2632 numbers should be factors of 60, so for example 5, 10, 15.
2634 When this is larger than 1, you can still force an exact time stamp by using
2635 a double prefix argument to a time stamp command like `C-c .' or `C-c !',
2636 and by using a prefix arg to `S-up/down' to specify the exact number
2637 of minutes to shift."
2639 :get
#'(lambda (var) ; Make sure both elements are there
2640 (if (integerp (default-value var
))
2641 (list (default-value var
) 5)
2642 (default-value var
)))
2644 (integer :tag
"when inserting times")
2645 (integer :tag
"when modifying times")))
2647 ;; Normalize old customizations of this variable.
2648 (when (integerp org-time-stamp-rounding-minutes
)
2649 (setq org-time-stamp-rounding-minutes
2650 (list org-time-stamp-rounding-minutes
2651 org-time-stamp-rounding-minutes
)))
2653 (defcustom org-display-custom-times nil
2654 "Non-nil means overlay custom formats over all time stamps.
2655 The formats are defined through the variable `org-time-stamp-custom-formats'.
2656 To turn this on on a per-file basis, insert anywhere in the file:
2657 #+STARTUP: customtime"
2661 (make-variable-buffer-local 'org-display-custom-times
)
2663 (defcustom org-time-stamp-custom-formats
2664 '("<%m/%d/%y %a>" .
"<%m/%d/%y %a %H:%M>") ; american
2665 "Custom formats for time stamps. See `format-time-string' for the syntax.
2666 These are overlaid over the default ISO format if the variable
2667 `org-display-custom-times' is set. Time like %H:%M should be at the
2668 end of the second format. The custom formats are also honored by export
2669 commands, if custom time display is turned on at the time of export."
2673 (defun org-time-stamp-format (&optional long inactive
)
2674 "Get the right format for a time string."
2675 (let ((f (if long
(cdr org-time-stamp-formats
)
2676 (car org-time-stamp-formats
))))
2678 (concat "[" (substring f
1 -
1) "]")
2681 (defcustom org-time-clocksum-format
"%d:%02d"
2682 "The format string used when creating CLOCKSUM lines.
2683 This is also used when org-mode generates a time duration."
2687 (defcustom org-time-clocksum-use-fractional nil
2688 "If non-nil, \\[org-clock-display] uses fractional times.
2689 org-mode generates a time duration."
2693 (defcustom org-time-clocksum-fractional-format
"%.2f"
2694 "The format string used when creating CLOCKSUM lines, or when
2695 org-mode generates a time duration."
2699 (defcustom org-deadline-warning-days
14
2700 "No. of days before expiration during which a deadline becomes active.
2701 This variable governs the display in sparse trees and in the agenda.
2702 When 0 or negative, it means use this number (the absolute value of it)
2703 even if a deadline has a different individual lead time specified.
2705 Custom commands can set this variable in the options section."
2707 :group
'org-agenda-daily
/weekly
2710 (defcustom org-read-date-prefer-future t
2711 "Non-nil means assume future for incomplete date input from user.
2712 This affects the following situations:
2713 1. The user gives a month but not a year.
2714 For example, if it is April and you enter \"feb 2\", this will be read
2715 as Feb 2, *next* year. \"May 5\", however, will be this year.
2716 2. The user gives a day, but no month.
2717 For example, if today is the 15th, and you enter \"3\", Org-mode will
2718 read this as the third of *next* month. However, if you enter \"17\",
2719 it will be considered as *this* month.
2721 If you set this variable to the symbol `time', then also the following
2724 3. If the user gives a time.
2725 If the time is before now, it will be interpreted as tomorrow.
2727 Currently none of this works for ISO week specifications.
2729 When this option is nil, the current day, month and year will always be
2732 See also `org-agenda-jump-prefer-future'."
2735 (const :tag
"Never" nil
)
2736 (const :tag
"Check month and day" t
)
2737 (const :tag
"Check month, day, and time" time
)))
2739 (defcustom org-agenda-jump-prefer-future
'org-read-date-prefer-future
2740 "Should the agenda jump command prefer the future for incomplete dates?
2741 The default is to do the same as configured in `org-read-date-prefer-future'.
2742 But you can also set a deviating value here.
2743 This may t or nil, or the symbol `org-read-date-prefer-future'."
2748 (const :tag
"Use org-read-date-prefer-future"
2749 org-read-date-prefer-future
)
2750 (const :tag
"Never" nil
)
2751 (const :tag
"Always" t
)))
2753 (defcustom org-read-date-force-compatible-dates t
2754 "Should date/time prompt force dates that are guaranteed to work in Emacs?
2756 Depending on the system Emacs is running on, certain dates cannot
2757 be represented with the type used internally to represent time.
2758 Dates between 1970-1-1 and 2038-1-1 can always be represented
2759 correctly. Some systems allow for earlier dates, some for later,
2760 some for both. One way to find out it to insert any date into an
2761 Org buffer, putting the cursor on the year and hitting S-up and
2762 S-down to test the range.
2764 When this variable is set to t, the date/time prompt will not let
2765 you specify dates outside the 1970-2037 range, so it is certain that
2766 these dates will work in whatever version of Emacs you are
2767 running, and also that you can move a file from one Emacs implementation
2768 to another. WHenever Org is forcing the year for you, it will display
2771 When this variable is nil, Org will check if the date is
2772 representable in the specific Emacs implementation you are using.
2773 If not, it will force a year, usually the current year, and beep
2774 to remind you. Currently this setting is not recommended because
2775 the likelihood that you will open your Org files in an Emacs that
2776 has limited date range is not negligible.
2778 A workaround for this problem is to use diary sexp dates for time
2779 stamps outside of this range."
2784 (defcustom org-read-date-display-live t
2785 "Non-nil means display current interpretation of date prompt live.
2786 This display will be in an overlay, in the minibuffer."
2790 (defcustom org-read-date-popup-calendar t
2791 "Non-nil means pop up a calendar when prompting for a date.
2792 In the calendar, the date can be selected with mouse-1. However, the
2793 minibuffer will also be active, and you can simply enter the date as well.
2794 When nil, only the minibuffer will be available."
2797 (if (fboundp 'defvaralias
)
2798 (defvaralias 'org-popup-calendar-for-date-prompt
2799 'org-read-date-popup-calendar
))
2801 (defcustom org-read-date-minibuffer-setup-hook nil
2802 "Hook to be used to set up keys for the date/time interface.
2803 Add key definitions to `minibuffer-local-map', which will be a temporary
2808 (defcustom org-extend-today-until
0
2809 "The hour when your day really ends. Must be an integer.
2810 This has influence for the following applications:
2811 - When switching the agenda to \"today\". It it is still earlier than
2812 the time given here, the day recognized as TODAY is actually yesterday.
2813 - When a date is read from the user and it is still before the time given
2814 here, the current date and time will be assumed to be yesterday, 23:59.
2815 Also, timestamps inserted in capture templates follow this rule.
2817 IMPORTANT: This is a feature whose implementation is and likely will
2818 remain incomplete. Really, it is only here because past midnight seems to
2819 be the favorite working time of John Wiegley :-)"
2823 (defcustom org-use-effective-time nil
2824 "If non-nil, consider `org-extend-today-until' when creating timestamps.
2825 For example, if `org-extend-today-until' is 8, and it's 4am, then the
2826 \"effective time\" of any timestamps between midnight and 8am will be
2827 23:59 of the previous day."
2832 (defcustom org-edit-timestamp-down-means-later nil
2833 "Non-nil means S-down will increase the time in a time stamp.
2834 When nil, S-up will increase."
2838 (defcustom org-calendar-follow-timestamp-change t
2839 "Non-nil means make the calendar window follow timestamp changes.
2840 When a timestamp is modified and the calendar window is visible, it will be
2841 moved to the new date."
2845 (defgroup org-tags nil
2846 "Options concerning tags in Org-mode."
2850 (defcustom org-tag-alist nil
2851 "List of tags allowed in Org-mode files.
2852 When this list is nil, Org-mode will base TAG input on what is already in the
2854 The value of this variable is an alist, the car of each entry must be a
2855 keyword as a string, the cdr may be a character that is used to select
2856 that tag through the fast-tag-selection interface.
2857 See the manual for details."
2861 (cons (string :tag
"Tag name")
2862 (character :tag
"Access char"))
2863 (list :tag
"Start radio group"
2865 (option (string :tag
"Group description")))
2866 (list :tag
"End radio group"
2868 (option (string :tag
"Group description")))
2869 (const :tag
"New line" (:newline
)))))
2871 (defcustom org-tag-persistent-alist nil
2872 "List of tags that will always appear in all Org-mode files.
2873 This is in addition to any in buffer settings or customizations
2875 When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
2876 The value of this variable is an alist, the car of each entry must be a
2877 keyword as a string, the cdr may be a character that is used to select
2878 that tag through the fast-tag-selection interface.
2879 See the manual for details.
2880 To disable these tags on a per-file basis, insert anywhere in the file:
2885 (cons (string :tag
"Tag name")
2886 (character :tag
"Access char"))
2887 (const :tag
"Start radio group" (:startgroup
))
2888 (const :tag
"End radio group" (:endgroup
))
2889 (const :tag
"New line" (:newline
)))))
2891 (defcustom org-complete-tags-always-offer-all-agenda-tags nil
2892 "If non-nil, always offer completion for all tags of all agenda files.
2893 Instead of customizing this variable directly, you might want to
2894 set it locally for capture buffers, because there no list of
2895 tags in that file can be created dynamically (there are none).
2897 (add-hook 'org-capture-mode-hook
2899 (set (make-local-variable
2900 'org-complete-tags-always-offer-all-agenda-tags)
2906 (defvar org-file-tags nil
2907 "List of tags that can be inherited by all entries in the file.
2908 The tags will be inherited if the variable `org-use-tag-inheritance'
2909 says they should be.
2910 This variable is populated from #+FILETAGS lines.")
2912 (defcustom org-use-fast-tag-selection
'auto
2913 "Non-nil means use fast tag selection scheme.
2914 This is a special interface to select and deselect tags with single keys.
2915 When nil, fast selection is never used.
2916 When the symbol `auto', fast selection is used if and only if selection
2917 characters for tags have been configured, either through the variable
2918 `org-tag-alist' or through a #+TAGS line in the buffer.
2919 When t, fast selection is always used and selection keys are assigned
2920 automatically if necessary."
2923 (const :tag
"Always" t
)
2924 (const :tag
"Never" nil
)
2925 (const :tag
"When selection characters are configured" 'auto
)))
2927 (defcustom org-fast-tag-selection-single-key nil
2928 "Non-nil means fast tag selection exits after first change.
2929 When nil, you have to press RET to exit it.
2930 During fast tag selection, you can toggle this flag with `C-c'.
2931 This variable can also have the value `expert'. In this case, the window
2932 displaying the tags menu is not even shown, until you press C-c again."
2935 (const :tag
"No" nil
)
2936 (const :tag
"Yes" t
)
2937 (const :tag
"Expert" expert
)))
2939 (defvar org-fast-tag-selection-include-todo nil
2940 "Non-nil means fast tags selection interface will also offer TODO states.
2941 This is an undocumented feature, you should not rely on it.")
2943 (defcustom org-tags-column
(if (featurep 'xemacs
) -
76 -
77)
2944 "The column to which tags should be indented in a headline.
2945 If this number is positive, it specifies the column. If it is negative,
2946 it means that the tags should be flushright to that column. For example,
2947 -80 works well for a normal 80 character screen.
2948 When 0, place tags directly after headline text, with only one space in
2953 (defcustom org-auto-align-tags t
2954 "Non-nil keeps tags aligned when modifying headlines.
2955 Some operations (i.e. demoting) change the length of a headline and
2956 therefore shift the tags around. With this option turned on, after
2957 each such operation the tags are again aligned to `org-tags-column'."
2961 (defcustom org-use-tag-inheritance t
2962 "Non-nil means tags in levels apply also for sublevels.
2963 When nil, only the tags directly given in a specific line apply there.
2964 This may also be a list of tags that should be inherited, or a regexp that
2965 matches tags that should be inherited. Additional control is possible
2966 with the variable `org-tags-exclude-from-inheritance' which gives an
2967 explicit list of tags to be excluded from inheritance., even if the value of
2968 `org-use-tag-inheritance' would select it for inheritance.
2970 If this option is t, a match early-on in a tree can lead to a large
2971 number of matches in the subtree when constructing the agenda or creating
2972 a sparse tree. If you only want to see the first match in a tree during
2973 a search, check out the variable `org-tags-match-list-sublevels'."
2976 (const :tag
"Not" nil
)
2977 (const :tag
"Always" t
)
2978 (repeat :tag
"Specific tags" (string :tag
"Tag"))
2979 (regexp :tag
"Tags matched by regexp")))
2981 (defcustom org-tags-exclude-from-inheritance nil
2982 "List of tags that should never be inherited.
2983 This is a way to exclude a few tags from inheritance. For way to do
2984 the opposite, to actively allow inheritance for selected tags,
2985 see the variable `org-use-tag-inheritance'."
2987 :type
'(repeat (string :tag
"Tag")))
2989 (defun org-tag-inherit-p (tag)
2990 "Check if TAG is one that should be inherited."
2992 ((member tag org-tags-exclude-from-inheritance
) nil
)
2993 ((eq org-use-tag-inheritance t
) t
)
2994 ((not org-use-tag-inheritance
) nil
)
2995 ((stringp org-use-tag-inheritance
)
2996 (string-match org-use-tag-inheritance tag
))
2997 ((listp org-use-tag-inheritance
)
2998 (member tag org-use-tag-inheritance
))
2999 (t (error "Invalid setting of `org-use-tag-inheritance'"))))
3001 (defcustom org-tags-match-list-sublevels t
3002 "Non-nil means list also sublevels of headlines matching a search.
3003 This variable applies to tags/property searches, and also to stuck
3004 projects because this search is based on a tags match as well.
3006 When set to the symbol `indented', sublevels are indented with
3009 Because of tag inheritance (see variable `org-use-tag-inheritance'),
3010 the sublevels of a headline matching a tag search often also match
3011 the same search. Listing all of them can create very long lists.
3012 Setting this variable to nil causes subtrees of a match to be skipped.
3014 This variable is semi-obsolete and probably should always be true. It
3015 is better to limit inheritance to certain tags using the variables
3016 `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
3019 (const :tag
"No, don't list them" nil
)
3020 (const :tag
"Yes, do list them" t
)
3021 (const :tag
"List them, indented with leading dots" indented
)))
3023 (defcustom org-tags-sort-function nil
3024 "When set, tags are sorted using this function as a comparator."
3027 (const :tag
"No sorting" nil
)
3028 (const :tag
"Alphabetical" string
<)
3029 (const :tag
"Reverse alphabetical" string
>)
3030 (function :tag
"Custom function" nil
)))
3032 (defvar org-tags-history nil
3033 "History of minibuffer reads for tags.")
3034 (defvar org-last-tags-completion-table nil
3035 "The last used completion table for tags.")
3036 (defvar org-after-tags-change-hook nil
3037 "Hook that is run after the tags in a line have changed.")
3039 (defgroup org-properties nil
3040 "Options concerning properties in Org-mode."
3041 :tag
"Org Properties"
3044 (defcustom org-property-format
"%-10s %s"
3045 "How property key/value pairs should be formatted by `indent-line'.
3046 When `indent-line' hits a property definition, it will format the line
3047 according to this format, mainly to make sure that the values are
3048 lined-up with respect to each other."
3049 :group
'org-properties
3052 (defcustom org-properties-postprocess-alist nil
3053 "Alist of properties and functions to adjust inserted values.
3054 Elements of this alist must be of the form
3056 ([string] [function])
3058 where [string] must be a property name and [function] must be a
3059 lambda expression: this lambda expression must take one argument,
3060 the value to adjust, and return the new value as a string.
3062 For example, this element will allow the property \"Remaining\"
3063 to be updated wrt the relation between the \"Effort\" property
3064 and the clock summary:
3066 ((\"Remaining\" (lambda(value)
3067 (let ((clocksum (org-clock-sum-current-item))
3068 (effort (org-duration-string-to-minutes
3069 (org-entry-get (point) \"Effort\"))))
3070 (org-minutes-to-hh:mm-string (- effort clocksum))))))"
3071 :group
'org-properties
3073 :type
'(alist :key-type
(string :tag
"Property")
3074 :value-type
(function :tag
"Function")))
3076 (defcustom org-use-property-inheritance nil
3077 "Non-nil means properties apply also for sublevels.
3079 This setting is chiefly used during property searches. Turning it on can
3080 cause significant overhead when doing a search, which is why it is not
3083 When nil, only the properties directly given in the current entry count.
3084 When t, every property is inherited. The value may also be a list of
3085 properties that should have inheritance, or a regular expression matching
3086 properties that should be inherited.
3088 However, note that some special properties use inheritance under special
3089 circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
3090 and the properties ending in \"_ALL\" when they are used as descriptor
3091 for valid values of a property.
3093 Note for programmers:
3094 When querying an entry with `org-entry-get', you can control if inheritance
3095 should be used. By default, `org-entry-get' looks only at the local
3096 properties. You can request inheritance by setting the inherit argument
3097 to t (to force inheritance) or to `selective' (to respect the setting
3099 :group
'org-properties
3101 (const :tag
"Not" nil
)
3102 (const :tag
"Always" t
)
3103 (repeat :tag
"Specific properties" (string :tag
"Property"))
3104 (regexp :tag
"Properties matched by regexp")))
3106 (defun org-property-inherit-p (property)
3107 "Check if PROPERTY is one that should be inherited."
3109 ((eq org-use-property-inheritance t
) t
)
3110 ((not org-use-property-inheritance
) nil
)
3111 ((stringp org-use-property-inheritance
)
3112 (string-match org-use-property-inheritance property
))
3113 ((listp org-use-property-inheritance
)
3114 (member property org-use-property-inheritance
))
3115 (t (error "Invalid setting of `org-use-property-inheritance'"))))
3117 (defcustom org-columns-default-format
"%25ITEM %TODO %3PRIORITY %TAGS"
3118 "The default column format, if no other format has been defined.
3119 This variable can be set on the per-file basis by inserting a line
3121 #+COLUMNS: %25ITEM ....."
3122 :group
'org-properties
3125 (defcustom org-columns-ellipses
".."
3126 "The ellipses to be used when a field in column view is truncated.
3127 When this is the empty string, as many characters as possible are shown,
3128 but then there will be no visual indication that the field has been truncated.
3129 When this is a string of length N, the last N characters of a truncated
3130 field are replaced by this string. If the column is narrower than the
3131 ellipses string, only part of the ellipses string will be shown."
3132 :group
'org-properties
3135 (defcustom org-columns-modify-value-for-display-function nil
3136 "Function that modifies values for display in column view.
3137 For example, it can be used to cut out a certain part from a time stamp.
3138 The function must take 2 arguments:
3140 column-title The title of the column (*not* the property name)
3141 value The value that should be modified.
3143 The function should return the value that should be displayed,
3144 or nil if the normal value should be used."
3145 :group
'org-properties
3148 (defcustom org-effort-property
"Effort"
3149 "The property that is being used to keep track of effort estimates.
3150 Effort estimates given in this property need to have the format H:MM."
3151 :group
'org-properties
3152 :group
'org-progress
3153 :type
'(string :tag
"Property"))
3155 (defconst org-global-properties-fixed
3156 '(("VISIBILITY_ALL" .
"folded children content all")
3157 ("CLOCK_MODELINE_TOTAL_ALL" .
"current today repeat all auto"))
3158 "List of property/value pairs that can be inherited by any entry.
3160 These are fixed values, for the preset properties. The user variable
3161 that can be used to add to this list is `org-global-properties'.
3163 The entries in this list are cons cells where the car is a property
3164 name and cdr is a string with the value. If the value represents
3165 multiple items like an \"_ALL\" property, separate the items by
3168 (defcustom org-global-properties nil
3169 "List of property/value pairs that can be inherited by any entry.
3171 This list will be combined with the constant `org-global-properties-fixed'.
3173 The entries in this list are cons cells where the car is a property
3174 name and cdr is a string with the value.
3176 You can set buffer-local values for the same purpose in the variable
3177 `org-file-properties' this by adding lines like
3179 #+PROPERTY: NAME VALUE"
3180 :group
'org-properties
3182 (cons (string :tag
"Property")
3183 (string :tag
"Value"))))
3185 (defvar org-file-properties nil
3186 "List of property/value pairs that can be inherited by any entry.
3187 Valid for the current buffer.
3188 This variable is populated from #+PROPERTY lines.")
3189 (make-variable-buffer-local 'org-file-properties
)
3191 (defgroup org-agenda nil
3192 "Options concerning agenda views in Org-mode."
3196 (defvar org-category nil
3197 "Variable used by org files to set a category for agenda display.
3198 Such files should use a file variable to set it, for example
3200 # -*- mode: org; org-category: \"ELisp\"
3202 or contain a special line
3206 If the file does not specify a category, then file's base name
3208 (make-variable-buffer-local 'org-category
)
3209 (put 'org-category
'safe-local-variable
#'(lambda (x) (or (symbolp x
) (stringp x
))))
3211 (defcustom org-agenda-files nil
3212 "The files to be used for agenda display.
3213 Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
3214 \\[org-remove-file]. You can also use customize to edit the list.
3216 If an entry is a directory, all files in that directory that are matched by
3217 `org-agenda-file-regexp' will be part of the file list.
3219 If the value of the variable is not a list but a single file name, then
3220 the list of agenda files is actually stored and maintained in that file, one
3221 agenda file per line. In this file paths can be given relative to
3222 `org-directory'. Tilde expansion and environment variable substitution
3226 (repeat :tag
"List of files and directories" file
)
3227 (file :tag
"Store list in a file\n" :value
"~/.agenda_files")))
3229 (defcustom org-agenda-file-regexp
"\\`[^.].*\\.org\\'"
3230 "Regular expression to match files for `org-agenda-files'.
3231 If any element in the list in that variable contains a directory instead
3232 of a normal file, all files in that directory that are matched by this
3233 regular expression will be included."
3237 (defcustom org-agenda-text-search-extra-files nil
3238 "List of extra files to be searched by text search commands.
3239 These files will be search in addition to the agenda files by the
3240 commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
3241 Note that these files will only be searched for text search commands,
3242 not for the other agenda views like todo lists, tag searches or the weekly
3243 agenda. This variable is intended to list notes and possibly archive files
3244 that should also be searched by these two commands.
3245 In fact, if the first element in the list is the symbol `agenda-archives',
3246 than all archive files of all agenda files will be added to the search
3249 :type
'(set :greedy t
3250 (const :tag
"Agenda Archives" agenda-archives
)
3251 (repeat :inline t
(file))))
3253 (if (fboundp 'defvaralias
)
3254 (defvaralias 'org-agenda-multi-occur-extra-files
3255 'org-agenda-text-search-extra-files
))
3257 (defcustom org-agenda-skip-unavailable-files nil
3258 "Non-nil means to just skip non-reachable files in `org-agenda-files'.
3259 A nil value means to remove them, after a query, from the list."
3263 (defcustom org-calendar-to-agenda-key
[?c
]
3264 "The key to be installed in `calendar-mode-map' for switching to the agenda.
3265 The command `org-calendar-goto-agenda' will be bound to this key. The
3266 default is the character `c' because then `c' can be used to switch back and
3267 forth between agenda and calendar."
3271 (defcustom org-calendar-insert-diary-entry-key
[?i
]
3272 "The key to be installed in `calendar-mode-map' for adding diary entries.
3273 This option is irrelevant until `org-agenda-diary-file' has been configured
3274 to point to an Org-mode file. When that is the case, the command
3275 `org-agenda-diary-entry' will be bound to the key given here, by default
3276 `i'. In the calendar, `i' normally adds entries to `diary-file'. So
3277 if you want to continue doing this, you need to change this to a different
3282 (defcustom org-agenda-diary-file
'diary-file
3283 "File to which to add new entries with the `i' key in agenda and calendar.
3284 When this is the symbol `diary-file', the functionality in the Emacs
3285 calendar will be used to add entries to the `diary-file'. But when this
3286 points to a file, `org-agenda-diary-entry' will be used instead."
3289 (const :tag
"The standard Emacs diary file" diary-file
)
3290 (file :tag
"Special Org file diary entries")))
3292 (eval-after-load "calendar"
3294 (org-defkey calendar-mode-map org-calendar-to-agenda-key
3295 'org-calendar-goto-agenda
)
3296 (add-hook 'calendar-mode-hook
3298 (unless (eq org-agenda-diary-file
'diary-file
)
3299 (define-key calendar-mode-map
3300 org-calendar-insert-diary-entry-key
3301 'org-agenda-diary-entry
))))))
3303 (defgroup org-latex nil
3304 "Options for embedding LaTeX code into Org-mode."
3308 (defcustom org-format-latex-options
3309 '(:foreground default
:background default
:scale
1.0
3310 :html-foreground
"Black" :html-background
"Transparent"
3311 :html-scale
1.0 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
3312 "Options for creating images from LaTeX fragments.
3313 This is a property list with the following properties:
3314 :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
3315 `default' means use the foreground of the default face.
3316 :background the background color, or \"Transparent\".
3317 `default' means use the background of the default face.
3318 :scale a scaling factor for the size of the images, to get more pixels
3319 :html-foreground, :html-background, :html-scale
3320 the same numbers for HTML export.
3321 :matchers a list indicating which matchers should be used to
3322 find LaTeX fragments. Valid members of this list are:
3323 \"begin\" find environments
3324 \"$1\" find single characters surrounded by $.$
3325 \"$\" find math expressions surrounded by $...$
3326 \"$$\" find math expressions surrounded by $$....$$
3327 \"\\(\" find math expressions surrounded by \\(...\\)
3328 \"\\ [\" find math expressions surrounded by \\ [...\\]"
3332 (defcustom org-format-latex-signal-error t
3333 "Non-nil means signal an error when image creation of LaTeX snippets fails.
3334 When nil, just push out a message."
3339 (defcustom org-latex-to-mathml-jar-file nil
3340 "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
3341 Use this to specify additional executable file say a jar file.
3343 When using MathToWeb as the converter, specify the full-path to
3344 your mathtoweb.jar file."
3348 (const :tag
"None" nil
)
3349 (file :tag
"JAR file" :must-match t
)))
3351 (defcustom org-latex-to-mathml-convert-command nil
3352 "Command to convert LaTeX fragments to MathML.
3353 Replace format-specifiers in the command as noted below and use
3354 `shell-command' to convert LaTeX to MathML.
3355 %j: Executable file in fully expanded form as specified by
3356 `org-latex-to-mathml-jar-file'.
3357 %I: Input LaTeX file in fully expanded form
3358 %o: Output MathML file
3359 This command is used by `org-create-math-formula'.
3361 When using MathToWeb as the converter, set this to
3362 \"java -jar %j -unicode -force -df %o %I\"."
3366 (const :tag
"None" nil
)
3367 (string :tag
"\nShell command")))
3369 (defcustom org-latex-create-formula-image-program
'dvipng
3370 "Program to convert LaTeX fragments with.
3372 dvipng Process the LaTeX fragments to dvi file, then convert
3373 dvi files to png files using dvipng.
3374 This will also include processing of non-math environments.
3375 imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
3376 to convert pdf files to png files"
3380 (const :tag
"dvipng" dvipng
)
3381 (const :tag
"imagemagick" imagemagick
)))
3383 (defcustom org-latex-preview-ltxpng-directory
"ltxpng/"
3384 "Path to store latex preview images. A relative path here creates many
3385 directories relative to the processed org files paths. An absolute path
3386 puts all preview images at the same place."
3391 (defun org-format-latex-mathml-available-p ()
3392 "Return t if `org-latex-to-mathml-convert-command' is usable."
3394 (when (and (boundp 'org-latex-to-mathml-convert-command
)
3395 org-latex-to-mathml-convert-command
)
3396 (let ((executable (car (split-string
3397 org-latex-to-mathml-convert-command
))))
3398 (when (executable-find executable
)
3400 "%j" org-latex-to-mathml-convert-command
)
3401 (file-readable-p org-latex-to-mathml-jar-file
)
3404 (defcustom org-format-latex-header
"\\documentclass{article}
3405 \\usepackage[usenames]{color}
3406 \\usepackage{amsmath}
3407 \\usepackage[mathscr]{eucal}
3408 \\pagestyle{empty} % do not remove
3411 % The settings below are copied from fullpage.sty
3412 \\setlength{\\textwidth}{\\paperwidth}
3413 \\addtolength{\\textwidth}{-3cm}
3414 \\setlength{\\oddsidemargin}{1.5cm}
3415 \\addtolength{\\oddsidemargin}{-2.54cm}
3416 \\setlength{\\evensidemargin}{\\oddsidemargin}
3417 \\setlength{\\textheight}{\\paperheight}
3418 \\addtolength{\\textheight}{-\\headheight}
3419 \\addtolength{\\textheight}{-\\headsep}
3420 \\addtolength{\\textheight}{-\\footskip}
3421 \\addtolength{\\textheight}{-3cm}
3422 \\setlength{\\topmargin}{1.5cm}
3423 \\addtolength{\\topmargin}{-2.54cm}"
3424 "The document header used for processing LaTeX fragments.
3425 It is imperative that this header make sure that no page number
3426 appears on the page. The package defined in the variables
3427 `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
3428 will either replace the placeholder \"[PACKAGES]\" in this header, or they
3433 (defvar org-format-latex-header-extra nil
)
3435 (defun org-set-packages-alist (var val
)
3436 "Set the packages alist and make sure it has 3 elements per entry."
3437 (set var
(mapcar (lambda (x)
3438 (if (and (consp x
) (= (length x
) 2))
3439 (list (car x
) (nth 1 x
) t
)
3443 (defun org-get-packages-alist (var)
3445 "Get the packages alist and make sure it has 3 elements per entry."
3447 (if (and (consp x
) (= (length x
) 2))
3448 (list (car x
) (nth 1 x
) t
)
3450 (default-value var
)))
3452 ;; The following variables are defined here because is it also used
3453 ;; when formatting latex fragments. Originally it was part of the
3454 ;; LaTeX exporter, which is why the name includes "export".
3455 (defcustom org-export-latex-default-packages-alist
3456 '(("AUTO" "inputenc" t
)
3460 ("" "longtable" nil
)
3472 "Alist of default packages to be inserted in the header.
3473 Change this only if one of the packages here causes an incompatibility
3474 with another package you are using.
3475 The packages in this list are needed by one part or another of Org-mode
3476 to function properly.
3478 - inputenc, fontenc: for basic font and character selection
3479 - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
3480 for interpreting the entities in `org-entities'. You can skip some of these
3481 packages if you don't use any of the symbols in it.
3482 - graphicx: for including images
3483 - float, wrapfig: for figure placement
3484 - longtable: for long tables
3485 - hyperref: for cross references
3487 Therefore you should not modify this variable unless you know what you
3488 are doing. The one reason to change it anyway is that you might be loading
3489 some other package that conflicts with one of the default packages.
3490 Each cell is of the format \( \"options\" \"package\" snippet-flag\).
3491 If SNIPPET-FLAG is t, the package also needs to be included when
3492 compiling LaTeX snippets into images for inclusion into HTML."
3493 :group
'org-export-latex
3494 :set
'org-set-packages-alist
3495 :get
'org-get-packages-alist
3499 (list :tag
"options/package pair"
3500 (string :tag
"options")
3501 (string :tag
"package")
3502 (boolean :tag
"Snippet"))
3503 (string :tag
"A line of LaTeX"))))
3505 (defcustom org-export-latex-packages-alist nil
3506 "Alist of packages to be inserted in every LaTeX header.
3507 These will be inserted after `org-export-latex-default-packages-alist'.
3508 Each cell is of the format \( \"options\" \"package\" snippet-flag \).
3509 SNIPPET-FLAG, when t, indicates that this package is also needed when
3510 turning LaTeX snippets into images for inclusion into HTML.
3511 Make sure that you only list packages here which:
3512 - you want in every file
3513 - do not conflict with the default packages in
3514 `org-export-latex-default-packages-alist'
3515 - do not conflict with the setup in `org-format-latex-header'."
3516 :group
'org-export-latex
3517 :set
'org-set-packages-alist
3518 :get
'org-get-packages-alist
3521 (list :tag
"options/package pair"
3522 (string :tag
"options")
3523 (string :tag
"package")
3524 (boolean :tag
"Snippet"))
3525 (string :tag
"A line of LaTeX"))))
3528 (defgroup org-appearance nil
3529 "Settings for Org-mode appearance."
3530 :tag
"Org Appearance"
3533 (defcustom org-level-color-stars-only nil
3534 "Non-nil means fontify only the stars in each headline.
3535 When nil, the entire headline is fontified.
3536 Changing it requires restart of `font-lock-mode' to become effective
3537 also in regions already fontified."
3538 :group
'org-appearance
3541 (defcustom org-hide-leading-stars nil
3542 "Non-nil means hide the first N-1 stars in a headline.
3543 This works by using the face `org-hide' for these stars. This
3544 face is white for a light background, and black for a dark
3545 background. You may have to customize the face `org-hide' to
3547 Changing it requires restart of `font-lock-mode' to become effective
3548 also in regions already fontified.
3549 You may also set this on a per-file basis by adding one of the following
3550 lines to the buffer:
3552 #+STARTUP: hidestars
3553 #+STARTUP: showstars"
3554 :group
'org-appearance
3557 (defcustom org-hidden-keywords nil
3558 "List of symbols corresponding to keywords to be hidden the org buffer.
3559 For example, a value '(title) for this list will make the document's title
3560 appear in the buffer without the initial #+TITLE: keyword."
3561 :group
'org-appearance
3563 :type
'(set (const :tag
"#+AUTHOR" author
)
3564 (const :tag
"#+DATE" date
)
3565 (const :tag
"#+EMAIL" email
)
3566 (const :tag
"#+TITLE" title
)))
3568 (defcustom org-custom-properties nil
3569 "List of properties (as strings) with a special meaning.
3570 The default use of these custom properties is to let the user
3571 hide them with `org-toggle-custom-properties-visibility'."
3572 :group
'org-properties
3573 :group
'org-appearance
3575 :type
'(repeat (string :tag
"Property Name")))
3577 (defcustom org-fontify-done-headline nil
3578 "Non-nil means change the face of a headline if it is marked DONE.
3579 Normally, only the TODO/DONE keyword indicates the state of a headline.
3580 When this is non-nil, the headline after the keyword is set to the
3581 `org-headline-done' as an additional indication."
3582 :group
'org-appearance
3585 (defcustom org-fontify-emphasized-text t
3586 "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
3587 Changing this variable requires a restart of Emacs to take effect."
3588 :group
'org-appearance
3591 (defcustom org-fontify-whole-heading-line nil
3592 "Non-nil means fontify the whole line for headings.
3593 This is useful when setting a background color for the
3595 :group
'org-appearance
3598 (defcustom org-highlight-latex-fragments-and-specials nil
3599 "Non-nil means fontify what is treated specially by the exporters."
3600 :group
'org-appearance
3603 (defcustom org-hide-emphasis-markers nil
3604 "Non-nil mean font-lock should hide the emphasis marker characters."
3605 :group
'org-appearance
3608 (defcustom org-pretty-entities nil
3609 "Non-nil means show entities as UTF8 characters.
3610 When nil, the \\name form remains in the buffer."
3611 :group
'org-appearance
3615 (defcustom org-pretty-entities-include-sub-superscripts t
3616 "Non-nil means, pretty entity display includes formatting sub/superscripts."
3617 :group
'org-appearance
3621 (defvar org-emph-re nil
3622 "Regular expression for matching emphasis.
3623 After a match, the match groups contain these elements:
3624 0 The match of the full regular expression, including the characters
3625 before and after the proper match
3626 1 The character before the proper match, or empty at beginning of line
3627 2 The proper match, including the leading and trailing markers
3628 3 The leading marker like * or /, indicating the type of highlighting
3629 4 The text between the emphasis markers, not including the markers
3630 5 The character after the match, empty at the end of a line")
3631 (defvar org-verbatim-re nil
3632 "Regular expression for matching verbatim text.")
3633 (defvar org-emphasis-regexp-components
) ; defined just below
3634 (defvar org-emphasis-alist
) ; defined just below
3635 (defun org-set-emph-re (var val
)
3636 "Set variable and compute the emphasis regular expression."
3638 (when (and (boundp 'org-emphasis-alist
)
3639 (boundp 'org-emphasis-regexp-components
)
3640 org-emphasis-alist org-emphasis-regexp-components
)
3641 (let* ((e org-emphasis-regexp-components
)
3647 (body1 (concat body
"*?"))
3648 (markers (mapconcat 'car org-emphasis-alist
""))
3649 (vmarkers (mapconcat
3650 (lambda (x) (if (eq (nth 4 x
) 'verbatim
) (car x
) ""))
3651 org-emphasis-alist
"")))
3652 ;; make sure special characters appear at the right position in the class
3653 (if (string-match "\\^" markers
)
3654 (setq markers
(concat (replace-match "" t t markers
) "^")))
3655 (if (string-match "-" markers
)
3656 (setq markers
(concat (replace-match "" t t markers
) "-")))
3657 (if (string-match "\\^" vmarkers
)
3658 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "^")))
3659 (if (string-match "-" vmarkers
)
3660 (setq vmarkers
(concat (replace-match "" t t vmarkers
) "-")))
3662 (setq body1
(concat body1
"\\(?:\n" body
"*?\\)\\{0,"
3663 (int-to-string nl
) "\\}")))
3666 (concat "\\([" pre
"]\\|^\\)"
3668 "\\([" markers
"]\\)"
3676 "\\([" post
"]\\|$\\)"))
3677 (setq org-verbatim-re
3678 (concat "\\([" pre
"]\\|^\\)"
3680 "\\([" vmarkers
"]\\)"
3688 "\\([" post
"]\\|$\\)")))))
3690 (defcustom org-emphasis-regexp-components
3691 '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
3692 "Components used to build the regular expression for emphasis.
3693 This is a list with five entries. Terminology: In an emphasis string
3694 like \" *strong word* \", we call the initial space PREMATCH, the final
3695 space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
3696 and \"trong wor\" is the body. The different components in this variable
3697 specify what is allowed/forbidden in each part:
3699 pre Chars allowed as prematch. Beginning of line will be allowed too.
3700 post Chars allowed as postmatch. End of line will be allowed too.
3701 border The chars *forbidden* as border characters.
3702 body-regexp A regexp like \".\" to match a body character. Don't use
3703 non-shy groups here, and don't allow newline here.
3704 newline The maximum number of newlines allowed in an emphasis exp.
3706 Use customize to modify this, or restart Emacs after changing it."
3707 :group
'org-appearance
3708 :set
'org-set-emph-re
3710 (sexp :tag
"Allowed chars in pre ")
3711 (sexp :tag
"Allowed chars in post ")
3712 (sexp :tag
"Forbidden chars in border ")
3713 (sexp :tag
"Regexp for body ")
3714 (integer :tag
"number of newlines allowed")
3715 (option (boolean :tag
"Please ignore this button"))))
3717 (defcustom org-emphasis-alist
3718 `(("*" bold
"<b>" "</b>")
3719 ("/" italic
"<i>" "</i>")
3720 ("_" underline
"<span style=\"text-decoration:underline;\">" "</span>")
3721 ("=" org-code
"<code>" "</code>" verbatim
)
3722 ("~" org-verbatim
"<code>" "</code>" verbatim
)
3723 ("+" ,(if (featurep 'xemacs
) 'org-table
'(:strike-through t
))
3726 "Special syntax for emphasized text.
3727 Text starting and ending with a special character will be emphasized, for
3728 example *bold*, _underlined_ and /italic/. This variable sets the marker
3729 characters, the face to be used by font-lock for highlighting in Org-mode
3730 Emacs buffers, and the HTML tags to be used for this.
3731 For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
3732 For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
3733 Use customize to modify this, or restart Emacs after changing it."
3734 :group
'org-appearance
3735 :set
'org-set-emph-re
3738 (string :tag
"Marker character")
3740 (face :tag
"Font-lock-face")
3741 (plist :tag
"Face property list"))
3742 (string :tag
"HTML start tag")
3743 (string :tag
"HTML end tag")
3744 (option (const verbatim
)))))
3746 (defvar org-protecting-blocks
3747 '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
3748 "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
3749 This is needed for font-lock setup.")
3751 ;;; Miscellaneous options
3753 (defgroup org-completion nil
3754 "Completion in Org-mode."
3755 :tag
"Org Completion"
3758 (defcustom org-completion-use-ido nil
3759 "Non-nil means use ido completion wherever possible.
3760 Note that `ido-mode' must be active for this variable to be relevant.
3761 If you decide to turn this variable on, you might well want to turn off
3762 `org-outline-path-complete-in-steps'.
3763 See also `org-completion-use-iswitchb'."
3764 :group
'org-completion
3767 (defcustom org-completion-use-iswitchb nil
3768 "Non-nil means use iswitchb completion wherever possible.
3769 Note that `iswitchb-mode' must be active for this variable to be relevant.
3770 If you decide to turn this variable on, you might well want to turn off
3771 `org-outline-path-complete-in-steps'.
3772 Note that this variable has only an effect if `org-completion-use-ido' is nil."
3773 :group
'org-completion
3776 (defcustom org-completion-fallback-command
'hippie-expand
3777 "The expansion command called by \\[pcomplete] in normal context.
3778 Normal means, no org-mode-specific context."
3779 :group
'org-completion
3782 ;;; Functions and variables from their packages
3783 ;; Declared here to avoid compiler warnings
3786 (defvar outline-mode-menu-heading
)
3787 (defvar outline-mode-menu-show
)
3788 (defvar outline-mode-menu-hide
)
3789 (defvar zmacs-regions
) ; XEmacs regions
3792 (defvar mark-active
)
3795 (declare-function calendar-absolute-from-iso
"cal-iso" (date))
3796 (declare-function calendar-forward-day
"cal-move" (arg))
3797 (declare-function calendar-goto-date
"cal-move" (date))
3798 (declare-function calendar-goto-today
"cal-move" ())
3799 (declare-function calendar-iso-from-absolute
"cal-iso" (date))
3800 (defvar calc-embedded-close-formula
)
3801 (defvar calc-embedded-open-formula
)
3802 (declare-function cdlatex-tab
"ext:cdlatex" ())
3803 (declare-function cdlatex-compute-tables
"ext:cdlatex" ())
3804 (declare-function dired-get-filename
"dired" (&optional localp no-error-if-not-filep
))
3805 (defvar font-lock-unfontify-region-function
)
3806 (declare-function iswitchb-read-buffer
"iswitchb"
3807 (prompt &optional default require-match start matches-set
))
3808 (defvar iswitchb-temp-buflist
)
3809 (declare-function org-gnus-follow-link
"org-gnus" (&optional group article
))
3810 (defvar org-agenda-tags-todo-honor-ignore-options
)
3811 (declare-function org-agenda-skip
"org-agenda" ())
3813 org-agenda-format-item
"org-agenda"
3814 (extra txt
&optional category tags dotime noprefix remove-re habitp
))
3815 (declare-function org-agenda-new-marker
"org-agenda" (&optional pos
))
3816 (declare-function org-agenda-change-all-lines
"org-agenda"
3817 (newhead hdmarker
&optional fixface just-this
))
3818 (declare-function org-agenda-set-restriction-lock
"org-agenda" (&optional type
))
3819 (declare-function org-agenda-maybe-redo
"org-agenda" ())
3820 (declare-function org-agenda-save-markers-for-cut-and-paste
"org-agenda"
3822 (declare-function org-agenda-copy-local-variable
"org-agenda" (var))
3823 (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
3824 "org-agenda" (&optional end
))
3825 (declare-function org-inlinetask-remove-END-maybe
"org-inlinetask" ())
3826 (declare-function org-inlinetask-in-task-p
"org-inlinetask" ())
3827 (declare-function org-inlinetask-goto-beginning
"org-inlinetask" ())
3828 (declare-function org-inlinetask-goto-end
"org-inlinetask" ())
3829 (declare-function org-indent-mode
"org-indent" (&optional arg
))
3830 (declare-function parse-time-string
"parse-time" (string))
3831 (declare-function org-attach-reveal
"org-attach" (&optional if-exists
))
3832 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
3833 (declare-function orgtbl-send-table
"org-table" (&optional maybe
))
3834 (defvar remember-data-file
)
3835 (defvar texmathp-why
)
3836 (declare-function speedbar-line-directory
"speedbar" (&optional depth
))
3837 (declare-function table--at-cell-p
"table" (position &optional object at-column
))
3839 (defvar w3m-current-url
)
3840 (defvar w3m-current-title
)
3842 (defvar org-latex-regexps
)
3844 ;;; Autoload and prepare some org modules
3846 ;; Some table stuff that needs to be defined here, because it is used
3847 ;; by the functions setting up org-mode or checking for table context.
3849 (defconst org-table-any-line-regexp
"^[ \t]*\\(|\\|\\+-[-+]\\)"
3850 "Detect an org-type or table-type table.")
3851 (defconst org-table-line-regexp
"^[ \t]*|"
3852 "Detect an org-type table line.")
3853 (defconst org-table-dataline-regexp
"^[ \t]*|[^-]"
3854 "Detect an org-type table line.")
3855 (defconst org-table-hline-regexp
"^[ \t]*|-"
3856 "Detect an org-type table hline.")
3857 (defconst org-table1-hline-regexp
"^[ \t]*\\+-[-+]"
3858 "Detect a table-type table hline.")
3859 (defconst org-table-any-border-regexp
"^[ \t]*[^|+ \t]"
3860 "Detect the first line outside a table when searching from within it.
3861 This works for both table types.")
3863 ;; Autoload the functions in org-table.el that are needed by functions here.
3866 (org-autoload "org-table"
3867 '(org-table-begin org-table-blank-field org-table-end
)))
3870 (defun turn-on-orgtbl ()
3871 "Unconditionally turn on `orgtbl-mode'."
3872 (require 'org-table
)
3875 (defun org-at-table-p (&optional table-type
)
3876 "Return t if the cursor is inside an org-type table.
3877 If TABLE-TYPE is non-nil, also check for table.el-type tables."
3878 (if org-enable-table-editor
3880 (beginning-of-line 1)
3881 (looking-at (if table-type org-table-any-line-regexp
3882 org-table-line-regexp
)))
3884 (defsubst org-table-p
() (org-at-table-p))
3886 (defun org-at-table.el-p
()
3887 "Return t if and only if we are at a table.el table."
3888 (and (org-at-table-p 'any
)
3890 (goto-char (org-table-begin 'any
))
3891 (looking-at org-table1-hline-regexp
))))
3892 (defun org-table-recognize-table.el
()
3893 "If there is a table.el table nearby, recognize it and move into it."
3894 (if org-table-tab-recognizes-table.el
3895 (if (org-at-table.el-p
)
3897 (beginning-of-line 1)
3898 (if (looking-at org-table-dataline-regexp
)
3900 (if (looking-at org-table1-hline-regexp
)
3902 (beginning-of-line 2)
3903 (if (looking-at org-table-any-border-regexp
)
3904 (beginning-of-line -
1)))))
3905 (if (re-search-forward "|" (org-table-end t
) t
)
3908 (if (table--at-cell-p (point))
3910 (message "recognizing table.el table...")
3911 (table-recognize-table)
3912 (message "recognizing table.el table...done")))
3913 (error "This should not happen"))
3918 (defun org-at-table-hline-p ()
3919 "Return t if the cursor is inside a hline in a table."
3920 (if org-enable-table-editor
3922 (beginning-of-line 1)
3923 (looking-at org-table-hline-regexp
))
3926 (defvar org-table-clean-did-remove-column nil
)
3928 (defun org-table-map-tables (function &optional quietly
)
3929 "Apply FUNCTION to the start of all tables in the buffer."
3933 (goto-char (point-min))
3934 (while (re-search-forward org-table-any-line-regexp nil t
)
3936 (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
3937 (beginning-of-line 1)
3938 (when (and (looking-at org-table-line-regexp
)
3939 ;; Exclude tables in src/example/verbatim/clocktable blocks
3940 (not (org-in-block-p '("src" "example"))))
3941 (save-excursion (funcall function
))
3942 (or (looking-at org-table-line-regexp
)
3944 (re-search-forward org-table-any-border-regexp nil
1))))
3945 (unless quietly
(message "Mapping tables: done")))
3947 ;; Declare and autoload functions from org-exp.el & Co
3949 (declare-function org-default-export-plist
"org-exp")
3950 (declare-function org-infile-export-plist
"org-exp")
3951 (declare-function org-get-current-options
"org-exp")
3953 ;; Declare and autoload functions from org-agenda.el
3956 (org-autoload "org-agenda"
3957 '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
3959 (declare-function org-clock-save-markers-for-cut-and-paste
"org-clock" (beg end
))
3960 (declare-function org-clock-update-mode-line
"org-clock" ())
3961 (declare-function org-resolve-clocks
"org-clock"
3962 (&optional also-non-dangling-p prompt last-valid
))
3963 (defvar org-clock-start-time
)
3964 (defvar org-clock-marker
(make-marker)
3965 "Marker recording the last clock-in.")
3966 (defvar org-clock-hd-marker
(make-marker)
3967 "Marker recording the last clock-in, but the headline position.")
3968 (defvar org-clock-heading
""
3969 "The heading of the current clock entry.")
3970 (defun org-clock-is-active ()
3971 "Return non-nil if clock is currently running.
3972 The return value is actually the clock marker."
3973 (marker-buffer org-clock-marker
))
3976 (org-autoload "org-clock" '(org-clock-remove-overlays
3977 org-clock-update-time-maybe
3978 org-clocktable-shift
)))
3980 (defun org-check-running-clock ()
3981 "Check if the current buffer contains the running clock.
3982 If yes, offer to stop it and to save the buffer with the changes."
3983 (when (and (equal (marker-buffer org-clock-marker
) (current-buffer))
3984 (y-or-n-p (format "Clock-out in buffer %s before killing it? "
3987 (when (y-or-n-p "Save changed buffer?")
3990 (defun org-clocktable-try-shift (dir n
)
3991 "Check if this line starts a clock table, if yes, shift the time block."
3992 (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
3993 (org-clocktable-shift dir n
)))
3996 (defun org-clock-persistence-insinuate ()
3997 "Set up hooks for clock persistence."
3998 (require 'org-clock
)
3999 (add-hook 'org-mode-hook
'org-clock-load
)
4000 (add-hook 'kill-emacs-hook
'org-clock-save
))
4002 ;; Define the variable already here, to make sure we have it.
4003 (defvar org-indent-mode nil
4004 "Non-nil if Org-Indent mode is enabled.
4005 Use the command `org-indent-mode' to change this variable.")
4007 ;; Autoload archiving code
4008 ;; The stuff that is needed for cycling and tags has to be defined here.
4010 (defgroup org-archive nil
4011 "Options concerning archiving in Org-mode."
4013 :group
'org-structure
)
4015 (defcustom org-archive-location
"%s_archive::"
4016 "The location where subtrees should be archived.
4018 The value of this variable is a string, consisting of two parts,
4019 separated by a double-colon. The first part is a filename and
4020 the second part is a headline.
4022 When the filename is omitted, archiving happens in the same file.
4023 %s in the filename will be replaced by the current file
4024 name (without the directory part). Archiving to a different file
4025 is useful to keep archived entries from contributing to the
4028 The archived entries will be filed as subtrees of the specified
4029 headline. When the headline is omitted, the subtrees are simply
4030 filed away at the end of the file, as top-level entries. Also in
4031 the heading you can use %s to represent the file name, this can be
4032 useful when using the same archive for a number of different files.
4034 Here are a few examples:
4036 If the current file is Projects.org, archive in file
4037 Projects.org_archive, as top-level trees. This is the default.
4039 \"::* Archived Tasks\"
4040 Archive in the current file, under the top-level headline
4041 \"* Archived Tasks\".
4043 \"~/org/archive.org::\"
4044 Archive in file ~/org/archive.org (absolute path), as top-level trees.
4046 \"~/org/archive.org::* From %s\"
4047 Archive in file ~/org/archive.org (absolute path), under headlines
4048 \"From FILENAME\" where file name is the current file name.
4050 \"~/org/datetree.org::datetree/* Finished Tasks\"
4051 The \"datetree/\" string is special, signifying to archive
4052 items to the datetree. Items are placed in either the CLOSED
4053 date of the item, or the current date if there is no CLOSED date.
4054 The heading will be a subentry to the current date. There doesn't
4055 need to be a heading, but there always needs to be a slash after
4056 datetree. For example, to store archived items directly in the
4057 datetree, use \"~/org/datetree.org::datetree/\".
4059 \"basement::** Finished Tasks\"
4060 Archive in file ./basement (relative path), as level 3 trees
4061 below the level 2 heading \"** Finished Tasks\".
4063 You may set this option on a per-file basis by adding to the buffer a
4066 #+ARCHIVE: basement::** Finished Tasks
4068 You may also define it locally for a subtree by setting an ARCHIVE property
4069 in the entry. If such a property is found in an entry, or anywhere up
4070 the hierarchy, it will be used."
4074 (defcustom org-archive-tag
"ARCHIVE"
4075 "The tag that marks a subtree as archived.
4076 An archived subtree does not open during visibility cycling, and does
4077 not contribute to the agenda listings.
4078 After changing this, font-lock must be restarted in the relevant buffers to
4079 get the proper fontification."
4081 :group
'org-keywords
4084 (defcustom org-agenda-skip-archived-trees t
4085 "Non-nil means the agenda will skip any items located in archived trees.
4086 An archived tree is a tree marked with the tag ARCHIVE. The use of this
4087 variable is no longer recommended, you should leave it at the value t.
4088 Instead, use the key `v' to cycle the archives-mode in the agenda."
4090 :group
'org-agenda-skip
4093 (defcustom org-columns-skip-archived-trees t
4094 "Non-nil means ignore archived trees when creating column view."
4096 :group
'org-properties
4099 (defcustom org-cycle-open-archived-trees nil
4100 "Non-nil means `org-cycle' will open archived trees.
4101 An archived tree is a tree marked with the tag ARCHIVE.
4102 When nil, archived trees will stay folded. You can still open them with
4103 normal outline commands like `show-all', but not with the cycling commands."
4108 (defcustom org-sparse-tree-open-archived-trees nil
4109 "Non-nil means sparse tree construction shows matches in archived trees.
4110 When nil, matches in these trees are highlighted, but the trees are kept in
4113 :group
'org-sparse-trees
4116 (defcustom org-sparse-tree-default-date-type
'scheduled-or-deadline
4117 "The default date type when building a sparse tree.
4118 When this is nil, a date is a scheduled or a deadline timestamp.
4119 Otherwise, these types are allowed:
4122 active: only active timestamps (<...>)
4123 inactive: only inactive timestamps (<...)
4124 scheduled: only scheduled timestamps
4125 deadline: only deadline timestamps"
4126 :type
'(choice (const :tag
"Scheduled or deadline" 'scheduled-or-deadline
)
4127 (const :tag
"All timestamps" all
)
4128 (const :tag
"Only active timestamps" active
)
4129 (const :tag
"Only inactive timestamps" inactive
)
4130 (const :tag
"Only scheduled timestamps" scheduled
)
4131 (const :tag
"Only deadline timestamps" deadline
))
4133 :group
'org-sparse-trees
)
4135 (defun org-cycle-hide-archived-subtrees (state)
4136 "Re-hide all archived subtrees after a visibility state change."
4137 (when (and (not org-cycle-open-archived-trees
)
4138 (not (memq state
'(overview folded
))))
4140 (let* ((globalp (memq state
'(contents all
)))
4141 (beg (if globalp
(point-min) (point)))
4142 (end (if globalp
(point-max) (org-end-of-subtree t
))))
4143 (org-hide-archived-subtrees beg end
)
4145 (if (looking-at (concat ".*:" org-archive-tag
":"))
4146 (message "%s" (substitute-command-keys
4147 "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
4149 (defun org-force-cycle-archived ()
4150 "Cycle subtree even if it is archived."
4152 (setq this-command
'org-cycle
)
4153 (let ((org-cycle-open-archived-trees t
))
4154 (call-interactively 'org-cycle
)))
4156 (defun org-hide-archived-subtrees (beg end
)
4157 "Re-hide all archived subtrees after a visibility state change."
4159 (let* ((re (concat ":" org-archive-tag
":")))
4161 (while (re-search-forward re end t
)
4162 (when (org-at-heading-p)
4163 (org-flag-subtree t
)
4164 (org-end-of-subtree t
))))))
4166 (declare-function outline-end-of-heading
"outline" ())
4167 (declare-function outline-flag-region
"outline" (from to flag
))
4168 (defun org-flag-subtree (flag)
4170 (org-back-to-heading t
)
4171 (outline-end-of-heading)
4172 (outline-flag-region (point)
4173 (progn (org-end-of-subtree t
) (point))
4176 (defalias 'org-advertized-archive-subtree
'org-archive-subtree
)
4179 (org-autoload "org-archive"
4180 '(org-add-archive-files)))
4182 ;; Autoload Column View Code
4184 (declare-function org-columns-number-to-string
"org-colview" (n fmt
&optional printf
))
4185 (declare-function org-columns-get-format-and-top-level
"org-colview" ())
4186 (declare-function org-columns-compute
"org-colview" (property))
4188 (org-autoload (if (featurep 'xemacs
) "org-colview-xemacs" "org-colview")
4189 '(org-columns-number-to-string
4190 org-columns-get-format-and-top-level
4192 org-columns-remove-overlays
))
4196 (declare-function org-id-store-link
"org-id")
4197 (declare-function org-id-locations-load
"org-id")
4198 (declare-function org-id-locations-save
"org-id")
4199 (defvar org-id-track-globally
)
4200 (org-autoload "org-id"
4203 org-id-get-with-outline-path-completion
4204 org-id-get-with-outline-drilling
))
4206 ;;; Variables for pre-computed regular expressions, all buffer local
4208 (defvar org-drawer-regexp
"^[ \t]*:PROPERTIES:[ \t]*$"
4209 "Matches first line of a hidden block.")
4210 (make-variable-buffer-local 'org-drawer-regexp
)
4211 (defvar org-todo-regexp nil
4212 "Matches any of the TODO state keywords.")
4213 (make-variable-buffer-local 'org-todo-regexp
)
4214 (defvar org-not-done-regexp nil
4215 "Matches any of the TODO state keywords except the last one.")
4216 (make-variable-buffer-local 'org-not-done-regexp
)
4217 (defvar org-not-done-heading-regexp nil
4218 "Matches a TODO headline that is not done.")
4219 (make-variable-buffer-local 'org-not-done-regexp
)
4220 (defvar org-todo-line-regexp nil
4221 "Matches a headline and puts TODO state into group 2 if present.")
4222 (make-variable-buffer-local 'org-todo-line-regexp
)
4223 (defvar org-complex-heading-regexp nil
4224 "Matches a headline and puts everything into groups:
4226 group 2: The todo keyword, maybe
4227 group 3: Priority cookie
4228 group 4: True headline
4230 (make-variable-buffer-local 'org-complex-heading-regexp
)
4231 (defvar org-complex-heading-regexp-format nil
4232 "Printf format to make regexp to match an exact headline.
4233 This regexp will match the headline of any node which has the
4234 exact headline text that is put into the format, but may have any
4235 TODO state, priority and tags.")
4236 (make-variable-buffer-local 'org-complex-heading-regexp-format
)
4237 (defvar org-todo-line-tags-regexp nil
4238 "Matches a headline and puts TODO state into group 2 if present.
4239 Also put tags into group 4 if tags are present.")
4240 (make-variable-buffer-local 'org-todo-line-tags-regexp
)
4241 (defvar org-ds-keyword-length
12
4242 "Maximum length of the DEADLINE and SCHEDULED keywords.")
4243 (make-variable-buffer-local 'org-ds-keyword-length
)
4244 (defvar org-deadline-regexp nil
4245 "Matches the DEADLINE keyword.")
4246 (make-variable-buffer-local 'org-deadline-regexp
)
4247 (defvar org-deadline-time-regexp nil
4248 "Matches the DEADLINE keyword together with a time stamp.")
4249 (make-variable-buffer-local 'org-deadline-time-regexp
)
4250 (defvar org-deadline-line-regexp nil
4251 "Matches the DEADLINE keyword and the rest of the line.")
4252 (make-variable-buffer-local 'org-deadline-line-regexp
)
4253 (defvar org-scheduled-regexp nil
4254 "Matches the SCHEDULED keyword.")
4255 (make-variable-buffer-local 'org-scheduled-regexp
)
4256 (defvar org-scheduled-time-regexp nil
4257 "Matches the SCHEDULED keyword together with a time stamp.")
4258 (make-variable-buffer-local 'org-scheduled-time-regexp
)
4259 (defvar org-closed-time-regexp nil
4260 "Matches the CLOSED keyword together with a time stamp.")
4261 (make-variable-buffer-local 'org-closed-time-regexp
)
4263 (defvar org-keyword-time-regexp nil
4264 "Matches any of the 4 keywords, together with the time stamp.")
4265 (make-variable-buffer-local 'org-keyword-time-regexp
)
4266 (defvar org-keyword-time-not-clock-regexp nil
4267 "Matches any of the 3 keywords, together with the time stamp.")
4268 (make-variable-buffer-local 'org-keyword-time-not-clock-regexp
)
4269 (defvar org-maybe-keyword-time-regexp nil
4270 "Matches a timestamp, possibly preceded by a keyword.")
4271 (make-variable-buffer-local 'org-maybe-keyword-time-regexp
)
4272 (defvar org-all-time-keywords nil
4273 "List of time keywords.")
4274 (make-variable-buffer-local 'org-all-time-keywords
)
4276 (defconst org-plain-time-of-day-regexp
4279 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4282 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4284 "Regular expression to match a plain time or time range.
4285 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4286 groups carry important information:
4288 1 the first time, range or not
4289 8 the second time, if it is a range.")
4291 (defconst org-plain-time-extension-regexp
4294 "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
4295 "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
4296 "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
4297 Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
4298 groups carry important information:
4301 9 minutes of duration")
4303 (defconst org-stamp-time-of-day-regexp
4305 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
4306 "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
4308 "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
4309 "Regular expression to match a timestamp time or time range.
4310 After a match, the following groups carry important information:
4312 1 date plus weekday, for back referencing to make sure both times are on the same day
4313 2 the first time, range or not
4314 4 the second time, if it is a range.")
4316 (defconst org-startup-options
4317 '(("fold" org-startup-folded t
)
4318 ("overview" org-startup-folded t
)
4319 ("nofold" org-startup-folded nil
)
4320 ("showall" org-startup-folded nil
)
4321 ("showeverything" org-startup-folded showeverything
)
4322 ("content" org-startup-folded content
)
4323 ("indent" org-startup-indented t
)
4324 ("noindent" org-startup-indented nil
)
4325 ("hidestars" org-hide-leading-stars t
)
4326 ("showstars" org-hide-leading-stars nil
)
4327 ("odd" org-odd-levels-only t
)
4328 ("oddeven" org-odd-levels-only nil
)
4329 ("align" org-startup-align-all-tables t
)
4330 ("noalign" org-startup-align-all-tables nil
)
4331 ("inlineimages" org-startup-with-inline-images t
)
4332 ("noinlineimages" org-startup-with-inline-images nil
)
4333 ("customtime" org-display-custom-times t
)
4334 ("logdone" org-log-done time
)
4335 ("lognotedone" org-log-done note
)
4336 ("nologdone" org-log-done nil
)
4337 ("lognoteclock-out" org-log-note-clock-out t
)
4338 ("nolognoteclock-out" org-log-note-clock-out nil
)
4339 ("logrepeat" org-log-repeat state
)
4340 ("lognoterepeat" org-log-repeat note
)
4341 ("nologrepeat" org-log-repeat nil
)
4342 ("logreschedule" org-log-reschedule time
)
4343 ("lognotereschedule" org-log-reschedule note
)
4344 ("nologreschedule" org-log-reschedule nil
)
4345 ("logredeadline" org-log-redeadline time
)
4346 ("lognoteredeadline" org-log-redeadline note
)
4347 ("nologredeadline" org-log-redeadline nil
)
4348 ("logrefile" org-log-refile time
)
4349 ("lognoterefile" org-log-refile note
)
4350 ("nologrefile" org-log-refile nil
)
4351 ("fninline" org-footnote-define-inline t
)
4352 ("nofninline" org-footnote-define-inline nil
)
4353 ("fnlocal" org-footnote-section nil
)
4354 ("fnauto" org-footnote-auto-label t
)
4355 ("fnprompt" org-footnote-auto-label nil
)
4356 ("fnconfirm" org-footnote-auto-label confirm
)
4357 ("fnplain" org-footnote-auto-label plain
)
4358 ("fnadjust" org-footnote-auto-adjust t
)
4359 ("nofnadjust" org-footnote-auto-adjust nil
)
4360 ("constcgs" constants-unit-system cgs
)
4361 ("constSI" constants-unit-system SI
)
4362 ("noptag" org-tag-persistent-alist nil
)
4363 ("hideblocks" org-hide-block-startup t
)
4364 ("nohideblocks" org-hide-block-startup nil
)
4365 ("beamer" org-startup-with-beamer-mode t
)
4366 ("entitiespretty" org-pretty-entities t
)
4367 ("entitiesplain" org-pretty-entities nil
))
4368 "Variable associated with STARTUP options for org-mode.
4369 Each element is a list of three items: the startup options (as written
4370 in the #+STARTUP line), the corresponding variable, and the value to set
4371 this variable to if the option is found. An optional forth element PUSH
4372 means to push this value onto the list in the variable.")
4374 (defun org-update-property-plist (key val props
)
4375 "Update PROPS with KEY and VAL."
4376 (let* ((appending (string= "+" (substring key
(- (length key
) 1))))
4377 (key (if appending
(substring key
0 (- (length key
) 1)) key
))
4378 (remainder (org-remove-if (lambda (p) (string= (car p
) key
)) props
))
4379 (previous (cdr (assoc key props
))))
4381 (cons (cons key
(if previous
(concat previous
" " val
) val
)) remainder
)
4382 (cons (cons key val
) remainder
))))
4384 (defconst org-block-regexp
4385 "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
4386 "Regular expression for hiding blocks.")
4387 (defconst org-heading-keyword-regexp-format
4388 "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
4389 "Printf format for a regexp matching an headline with some keyword.
4390 This regexp will match the headline of any node which has the
4391 exact keyword that is put into the format. The keyword isn't in
4392 any group by default, but the stars and the body are.")
4393 (defconst org-heading-keyword-maybe-regexp-format
4394 "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
4395 "Printf format for a regexp matching an headline, possibly with some keyword.
4396 This regexp can match any headline with the specified keyword, or
4397 without a keyword. The keyword isn't in any group by default,
4398 but the stars and the body are.")
4400 (defun org-set-regexps-and-options ()
4401 "Precompute regular expressions for current buffer."
4402 (when (derived-mode-p 'org-mode
)
4403 (org-set-local 'org-todo-kwd-alist nil
)
4404 (org-set-local 'org-todo-key-alist nil
)
4405 (org-set-local 'org-todo-key-trigger nil
)
4406 (org-set-local 'org-todo-keywords-1 nil
)
4407 (org-set-local 'org-done-keywords nil
)
4408 (org-set-local 'org-todo-heads nil
)
4409 (org-set-local 'org-todo-sets nil
)
4410 (org-set-local 'org-todo-log-states nil
)
4411 (org-set-local 'org-file-properties nil
)
4412 (org-set-local 'org-file-tags nil
)
4413 (let ((re (org-make-options-regexp
4414 '("CATEGORY" "TODO" "COLUMNS"
4415 "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
4416 "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
4418 "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
4420 (scripts org-use-sub-superscripts
)
4421 kwds kws0 kwsa key log value cat arch tags const links hw dws
4422 tail sep kws1 prio props ftags drawers beamer-p
4423 ext-setup-or-nil setup-contents
(start 0))
4427 (goto-char (point-min))
4428 (while (or (and ext-setup-or-nil
4429 (string-match re ext-setup-or-nil start
)
4430 (setq start
(match-end 0)))
4431 (and (setq ext-setup-or-nil nil start
0)
4432 (re-search-forward re nil t
)))
4433 (setq key
(upcase (match-string 1 ext-setup-or-nil
))
4434 value
(org-match-string-no-properties 2 ext-setup-or-nil
))
4435 (if (stringp value
) (setq value
(org-trim value
)))
4437 ((equal key
"CATEGORY")
4439 ((member key
'("SEQ_TODO" "TODO"))
4440 (push (cons 'sequence
(org-split-string value splitre
)) kwds
))
4441 ((equal key
"TYP_TODO")
4442 (push (cons 'type
(org-split-string value splitre
)) kwds
))
4443 ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key
)
4444 ;; general TODO-like setup
4445 (push (cons (intern (downcase (match-string 1 key
)))
4446 (org-split-string value splitre
)) kwds
))
4448 (setq tags
(append tags
(if tags
'("\\n") nil
)
4449 (org-split-string value splitre
))))
4450 ((equal key
"COLUMNS")
4451 (org-set-local 'org-columns-default-format value
))
4453 (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value
)
4454 (push (cons (match-string 1 value
)
4455 (org-trim (match-string 2 value
)))
4457 ((equal key
"PRIORITIES")
4458 (setq prio
(org-split-string value
" +")))
4459 ((equal key
"PROPERTY")
4460 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4461 (setq props
(org-update-property-plist (match-string 1 value
)
4462 (match-string 2 value
)
4464 ((equal key
"FILETAGS")
4465 (when (string-match "\\S-" value
)
4470 (mapcar (lambda (x) (org-split-string x
":"))
4471 (org-split-string value
)))))))
4472 ((equal key
"DRAWERS")
4473 (setq drawers
(delete-dups (append org-drawers
(org-split-string value splitre
)))))
4474 ((equal key
"CONSTANTS")
4475 (setq const
(append const
(org-split-string value splitre
))))
4476 ((equal key
"STARTUP")
4477 (let ((opts (org-split-string value splitre
))
4479 (while (setq l
(pop opts
))
4480 (when (setq l
(assoc l org-startup-options
))
4481 (setq var
(nth 1 l
) val
(nth 2 l
))
4483 (set (make-local-variable var
) val
)
4484 (if (not (listp (symbol-value var
)))
4485 (set (make-local-variable var
) nil
))
4486 (set (make-local-variable var
) (symbol-value var
))
4487 (add-to-list var val
))))))
4488 ((equal key
"ARCHIVE")
4490 (remove-text-properties 0 (length arch
)
4491 '(face t fontified t
) arch
))
4492 ((equal key
"LATEX_CLASS")
4493 (setq beamer-p
(equal value
"beamer")))
4494 ((equal key
"OPTIONS")
4495 (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value
)
4496 (setq scripts
(read (match-string 2 value
)))))
4497 ((equal key
"SETUPFILE")
4498 (setq setup-contents
(org-file-contents
4500 (org-remove-double-quotes value
))
4502 (if (not ext-setup-or-nil
)
4503 (setq ext-setup-or-nil setup-contents start
0)
4504 (setq ext-setup-or-nil
4505 (concat (substring ext-setup-or-nil
0 start
)
4506 "\n" setup-contents
"\n"
4507 (substring ext-setup-or-nil start
)))))))
4508 ;; search for property blocks
4509 (goto-char (point-min))
4510 (while (re-search-forward org-block-regexp nil t
)
4511 (when (equal "PROPERTY" (upcase (match-string 1)))
4512 (setq value
(replace-regexp-in-string
4513 "[\n\r]" " " (match-string 4)))
4514 (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value
)
4515 (setq props
(org-update-property-plist (match-string 1 value
)
4516 (match-string 2 value
)
4518 (org-set-local 'org-use-sub-superscripts scripts
)
4520 (org-set-local 'org-category
(intern cat
))
4521 (push (cons "CATEGORY" cat
) props
))
4523 (if (< (length prio
) 3) (setq prio
'("A" "C" "B")))
4524 (setq prio
(mapcar 'string-to-char prio
))
4525 (org-set-local 'org-highest-priority
(nth 0 prio
))
4526 (org-set-local 'org-lowest-priority
(nth 1 prio
))
4527 (org-set-local 'org-default-priority
(nth 2 prio
)))
4528 (and props
(org-set-local 'org-file-properties
(nreverse props
)))
4529 (and ftags
(org-set-local 'org-file-tags
4530 (mapcar 'org-add-prop-inherited ftags
)))
4531 (and drawers
(org-set-local 'org-drawers drawers
))
4532 (and arch
(org-set-local 'org-archive-location arch
))
4533 (and links
(setq org-link-abbrev-alist-local
(nreverse links
)))
4534 ;; Process the TODO keywords
4536 ;; Use the global values as if they had been given locally.
4537 (setq kwds
(default-value 'org-todo-keywords
))
4538 (if (stringp (car kwds
))
4539 (setq kwds
(list (cons org-todo-interpretation
4540 (default-value 'org-todo-keywords
)))))
4541 (setq kwds
(reverse kwds
)))
4542 (setq kwds
(nreverse kwds
))
4544 (while (setq kws
(pop kwds
))
4546 (run-hook-with-args-until-success
4547 'org-todo-setup-filter-hook kws
)
4549 (setq inter
(pop kws
) sep
(member "|" kws
)
4550 kws0
(delete "|" (copy-sequence kws
))
4555 (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x
)
4557 (setq kw
(match-string 1 x
)
4558 key
(and (match-end 2) (match-string 2 x
))
4559 log
(org-extract-log-state-settings x
))
4560 (push (cons kw
(and key
(string-to-char key
))) kwsa
)
4561 (and log
(push log org-todo-log-states
))
4563 (error "Invalid TODO keyword %s" x
)))
4565 kwsa
(if kwsa
(append '((:startgroup
))
4569 dws
(if sep
(org-remove-keyword-keys (cdr sep
)) (last kws1
))
4570 tail
(list inter hw
(car dws
) (org-last dws
))))
4571 (add-to-list 'org-todo-heads hw
'append
)
4572 (push kws1 org-todo-sets
)
4573 (setq org-done-keywords
(append org-done-keywords dws nil
))
4574 (setq org-todo-key-alist
(append org-todo-key-alist kwsa
))
4575 (mapc (lambda (x) (push (cons x tail
) org-todo-kwd-alist
)) kws1
)
4576 (setq org-todo-keywords-1
(append org-todo-keywords-1 kws1 nil
)))
4577 (setq org-todo-sets
(nreverse org-todo-sets
)
4578 org-todo-kwd-alist
(nreverse org-todo-kwd-alist
)
4579 org-todo-key-trigger
(delq nil
(mapcar 'cdr org-todo-key-alist
))
4580 org-todo-key-alist
(org-assign-fast-keys org-todo-key-alist
)))
4581 ;; Process the constants
4584 (while (setq e
(pop const
))
4585 (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e
)
4586 (push (cons (match-string 1 e
) (match-string 2 e
)) cst
)))
4587 (setq org-table-formula-constants-local cst
)))
4589 ;; Process the tags.
4592 (while (setq e
(pop tags
))
4594 ((equal e
"{") (push '(:startgroup
) tgs
))
4595 ((equal e
"}") (push '(:endgroup
) tgs
))
4596 ((equal e
"\\n") (push '(:newline
) tgs
))
4597 ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e
)
4598 (push (cons (match-string 1 e
)
4599 (string-to-char (match-string 2 e
)))
4601 (t (push (list e
) tgs
))))
4602 (org-set-local 'org-tag-alist nil
)
4603 (while (setq e
(pop tgs
))
4604 (or (and (stringp (car e
))
4605 (assoc (car e
) org-tag-alist
))
4606 (push e org-tag-alist
)))))
4608 ;; Compute the regular expressions and other local variables.
4609 ;; Using `org-outline-regexp-bol' would complicate them much,
4610 ;; because of the fixed white space at the end of that string.
4611 (if (not org-done-keywords
)
4612 (setq org-done-keywords
(and org-todo-keywords-1
4613 (list (org-last org-todo-keywords-1
)))))
4614 (setq org-ds-keyword-length
(+ 2 (max (length org-deadline-string
)
4615 (length org-scheduled-string
)
4616 (length org-clock-string
)
4617 (length org-closed-string
)))
4619 (concat "^[ \t]*:\\("
4620 (mapconcat 'regexp-quote org-drawers
"\\|")
4622 org-not-done-keywords
4623 (org-delete-all org-done-keywords
(copy-sequence org-todo-keywords-1
))
4626 (mapconcat 'regexp-quote org-todo-keywords-1
"\\|")
4630 (mapconcat 'regexp-quote org-not-done-keywords
"\\|")
4632 org-not-done-heading-regexp
4633 (format org-heading-keyword-regexp-format org-not-done-regexp
)
4634 org-todo-line-regexp
4635 (format org-heading-keyword-maybe-regexp-format org-todo-regexp
)
4636 org-complex-heading-regexp
4637 (concat "^\\(\\*+\\)"
4638 "\\(?: +" org-todo-regexp
"\\)?"
4639 "\\(?: +\\(\\[#.\\]\\)\\)?"
4640 "\\(?: +\\(.*?\\)\\)??"
4641 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
4643 org-complex-heading-regexp-format
4644 (concat "^\\(\\*+\\)"
4645 "\\(?: +" org-todo-regexp
"\\)?"
4646 "\\(?: +\\(\\[#.\\]\\)\\)?"
4648 ;; Stats cookies can be stuck to body.
4649 "\\(?:\\[[0-9%%/]+\\] *\\)?"
4651 "\\(?: *\\[[0-9%%/]+\\]\\)?"
4653 (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
4655 org-todo-line-tags-regexp
4656 (concat "^\\(\\*+\\)"
4657 "\\(?: +" org-todo-regexp
"\\)?"
4658 "\\(?: +\\(.*?\\)\\)??"
4659 (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
4661 org-deadline-regexp
(concat "\\<" org-deadline-string
)
4662 org-deadline-time-regexp
4663 (concat "\\<" org-deadline-string
" *<\\([^>]+\\)>")
4664 org-deadline-line-regexp
4665 (concat "\\<\\(" org-deadline-string
"\\).*")
4666 org-scheduled-regexp
4667 (concat "\\<" org-scheduled-string
)
4668 org-scheduled-time-regexp
4669 (concat "\\<" org-scheduled-string
" *<\\([^>]+\\)>")
4670 org-closed-time-regexp
4671 (concat "\\<" org-closed-string
" *\\[\\([^]]+\\)\\]")
4672 org-keyword-time-regexp
4673 (concat "\\<\\(" org-scheduled-string
4674 "\\|" org-deadline-string
4675 "\\|" org-closed-string
4676 "\\|" org-clock-string
"\\)"
4677 " *[[<]\\([^]>]+\\)[]>]")
4678 org-keyword-time-not-clock-regexp
4679 (concat "\\<\\(" org-scheduled-string
4680 "\\|" org-deadline-string
4681 "\\|" org-closed-string
4683 " *[[<]\\([^]>]+\\)[]>]")
4684 org-maybe-keyword-time-regexp
4685 (concat "\\(\\<\\(" org-scheduled-string
4686 "\\|" org-deadline-string
4687 "\\|" org-closed-string
4688 "\\|" org-clock-string
"\\)\\)?"
4689 " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
4690 org-all-time-keywords
4691 (mapcar (lambda (w) (substring w
0 -
1))
4692 (list org-scheduled-string org-deadline-string
4693 org-clock-string org-closed-string
))
4695 (org-compute-latex-and-specials-regexp)
4696 (org-set-font-lock-defaults))))
4698 (defun org-file-contents (file &optional noerror
)
4699 "Return the contents of FILE, as a string."
4701 (not (file-readable-p file
)))
4704 (message "Cannot read file \"%s\"" file
)
4707 (error "Cannot read file \"%s\"" file
))
4709 (insert-file-contents file
)
4712 (defun org-extract-log-state-settings (x)
4713 "Extract the log state setting from a TODO keyword string.
4714 This will extract info from a string like \"WAIT(w@/!)\"."
4715 (let (kw key log1 log2
)
4716 (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x
)
4717 (setq kw
(match-string 1 x
)
4718 key
(and (match-end 2) (match-string 2 x
))
4719 log1
(and (match-end 3) (match-string 3 x
))
4720 log2
(and (match-end 4) (match-string 4 x
)))
4723 (and log1
(if (equal log1
"!") 'time
'note
))
4724 (and log2
(if (equal log2
"!") 'time
'note
)))))))
4726 (defun org-remove-keyword-keys (list)
4727 "Remove a pair of parenthesis at the end of each string in LIST."
4729 (if (string-match "(.*)$" x
)
4730 (substring x
0 (match-beginning 0))
4734 (defun org-assign-fast-keys (alist)
4735 "Assign fast keys to a keyword-key alist.
4736 Respect keys that are already there."
4737 (let (new e
(alt ?
0))
4738 (while (setq e
(pop alist
))
4739 (if (or (memq (car e
) '(:newline
:endgroup
:startgroup
))
4740 (cdr e
)) ;; Key already assigned.
4742 (let ((clist (string-to-list (downcase (car e
))))
4743 (used (append new alist
)))
4744 (when (= (car clist
) ?
@)
4746 (while (and clist
(rassoc (car clist
) used
))
4749 (while (rassoc alt used
)
4751 (push (cons (car e
) (or (car clist
) alt
)) new
))))
4754 ;;; Some variables used in various places
4756 (defvar org-window-configuration nil
4757 "Used in various places to store a window configuration.")
4758 (defvar org-selected-window nil
4759 "Used in various places to store a window configuration.")
4760 (defvar org-finish-function nil
4761 "Function to be called when `C-c C-c' is used.
4762 This is for getting out of special buffers like capture.")
4765 ;; FIXME: Occasionally check by commenting these, to make sure
4766 ;; no other functions uses these, forgetting to let-bind them.
4767 (org-no-warnings (defvar entry
)) ;; unprefixed, from calendar.el
4768 (defvar org-last-state
)
4769 (org-no-warnings (defvar date
)) ;; unprefixed, from calendar.el
4771 ;; Defined somewhere in this file, but used before definition.
4772 (defvar org-entities
) ;; defined in org-entities.el
4773 (defvar org-struct-menu
)
4774 (defvar org-org-menu
)
4775 (defvar org-tbl-menu
)
4777 ;;;; Define the Org-mode
4779 ;; We use a before-change function to check if a table might need
4781 (defvar org-table-may-need-update t
4782 "Indicates that a table might need an update.
4783 This variable is set by `org-before-change-function'.
4784 `org-table-align' sets it back to nil.")
4785 (defun org-before-change-function (beg end
)
4786 "Every change indicates that a table might need an update."
4787 (setq org-table-may-need-update t
))
4788 (defvar org-mode-map
)
4789 (defvar org-inhibit-startup nil
) ; Dynamically-scoped param.
4790 (defvar org-inhibit-startup-visibility-stuff nil
) ; Dynamically-scoped param.
4791 (defvar org-agenda-keep-modes nil
) ; Dynamically-scoped param.
4792 (defvar org-inhibit-logging nil
) ; Dynamically-scoped param.
4793 (defvar org-inhibit-blocking nil
) ; Dynamically-scoped param.
4794 (defvar org-table-buffer-is-an nil
)
4796 (defvar bidi-paragraph-direction
)
4797 (defvar buffer-face-mode-face
)
4800 (if (and (not (keymapp outline-mode-map
)) (featurep 'allout
))
4801 (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"))
4802 (require 'noutline
"noutline" 'noerror
) ;; stock XEmacs does not have it
4804 ;; Other stuff we need.
4805 (require 'time-date
)
4806 (unless (fboundp 'time-subtract
) (defalias 'time-subtract
'subtract-time
))
4811 (require 'org-entities
)
4812 ;; (require 'org-compat) moved higher up in the file before it is first used
4813 (require 'org-faces
)
4815 (require 'org-pcomplete
)
4817 (require 'org-footnote
)
4824 (require 'ob-tangle
)
4825 (require 'ob-comint
)
4829 (define-derived-mode org-mode outline-mode
"Org"
4830 "Outline-based notes management and organizer, alias
4831 \"Carsten's outline-mode for keeping track of everything.\"
4833 Org-mode develops organizational tasks around a NOTES file which
4834 contains information about projects as plain text. Org-mode is
4835 implemented on top of outline-mode, which is ideal to keep the content
4836 of large files well structured. It supports ToDo items, deadlines and
4837 time stamps, which magically appear in the diary listing of the Emacs
4838 calendar. Tables are easily created with a built-in table editor.
4839 Plain text URL-like links connect to websites, emails (VM), Usenet
4840 messages (Gnus), BBDB entries, and any files related to the project.
4841 For printing and sharing of notes, an Org-mode file (or a part of it)
4842 can be exported as a structured ASCII or HTML file.
4844 The following commands are available:
4848 ;; Get rid of Outline menus, they are not needed
4849 ;; Need to do this here because define-derived-mode sets up
4850 ;; the keymap so late. Still, it is a waste to call this each time
4851 ;; we switch another buffer into org-mode.
4852 (if (featurep 'xemacs
)
4853 (when (boundp 'outline-mode-menu-heading
)
4854 ;; Assume this is Greg's port, it uses easymenu
4855 (easy-menu-remove outline-mode-menu-heading
)
4856 (easy-menu-remove outline-mode-menu-show
)
4857 (easy-menu-remove outline-mode-menu-hide
))
4858 (define-key org-mode-map
[menu-bar headings
] 'undefined
)
4859 (define-key org-mode-map
[menu-bar hide
] 'undefined
)
4860 (define-key org-mode-map
[menu-bar show
] 'undefined
))
4862 (org-load-modules-maybe)
4863 (easy-menu-add org-org-menu
)
4864 (easy-menu-add org-tbl-menu
)
4865 (org-install-agenda-files-menu)
4866 (if org-descriptive-links
(add-to-invisibility-spec '(org-link)))
4867 (add-to-invisibility-spec '(org-cwidth))
4868 (add-to-invisibility-spec '(org-hide-block . t
))
4869 (when (featurep 'xemacs
)
4870 (org-set-local 'line-move-ignore-invisible t
))
4871 (org-set-local 'outline-regexp org-outline-regexp
)
4872 (org-set-local 'outline-level
'org-outline-level
)
4873 (setq bidi-paragraph-direction
'left-to-right
)
4874 (when (and org-ellipsis
4875 (fboundp 'set-display-table-slot
) (boundp 'buffer-display-table
)
4876 (fboundp 'make-glyph-code
))
4877 (unless org-display-table
4878 (setq org-display-table
(make-display-table)))
4879 (set-display-table-slot
4882 (lambda (c) (make-glyph-code c
(and (not (stringp org-ellipsis
))
4884 (if (stringp org-ellipsis
) org-ellipsis
"..."))))
4885 (setq buffer-display-table org-display-table
))
4886 (org-set-regexps-and-options)
4887 (when (and org-tag-faces
(not org-tags-special-faces-re
))
4888 ;; tag faces set outside customize.... force initialization.
4889 (org-set-tag-faces 'org-tag-faces org-tag-faces
))
4891 (org-set-local 'calc-embedded-open-mode
"# ")
4892 (modify-syntax-entry ?
@ "w")
4893 (if org-startup-truncated
(setq truncate-lines t
))
4894 (org-set-local 'font-lock-unfontify-region-function
4895 'org-unfontify-region
)
4896 ;; Activate before-change-function
4897 (org-set-local 'org-table-may-need-update t
)
4898 (org-add-hook 'before-change-functions
'org-before-change-function nil
4900 ;; Check for running clock before killing a buffer
4901 (org-add-hook 'kill-buffer-hook
'org-check-running-clock nil
'local
)
4903 (org-set-local 'indent-line-function
'org-indent-line
)
4904 (org-set-local 'indent-region-function
'org-indent-region
)
4905 ;; Initialize radio targets.
4906 (org-update-radio-target-regexp)
4907 ;; Filling and auto-filling.
4910 (org-setup-comments-handling)
4911 ;; Beginning/end of defun
4912 (org-set-local 'beginning-of-defun-function
'org-back-to-heading
)
4913 (org-set-local 'end-of-defun-function
(lambda () (interactive) (org-end-of-subtree nil t
)))
4914 ;; Next error for sparse trees
4915 (org-set-local 'next-error-function
'org-occur-next-match
)
4916 ;; Make sure dependence stuff works reliably, even for users who set it
4918 (if org-enforce-todo-dependencies
4919 (add-hook 'org-blocker-hook
4920 'org-block-todo-from-children-or-siblings-or-parent
)
4921 (remove-hook 'org-blocker-hook
4922 'org-block-todo-from-children-or-siblings-or-parent
))
4923 (if org-enforce-todo-checkbox-dependencies
4924 (add-hook 'org-blocker-hook
4925 'org-block-todo-from-checkboxes
)
4926 (remove-hook 'org-blocker-hook
4927 'org-block-todo-from-checkboxes
))
4929 ;; Align options lines
4931 'align-mode-rules-list
4932 '((org-in-buffer-settings
4933 (regexp .
"^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
4934 (modes .
'(org-mode)))))
4937 (org-set-local 'imenu-create-index-function
4938 'org-imenu-get-tree
)
4940 ;; Make isearch reveal context
4941 (if (or (featurep 'xemacs
)
4942 (not (boundp 'outline-isearch-open-invisible-function
)))
4943 ;; Emacs 21 and XEmacs make use of the hook
4944 (org-add-hook 'isearch-mode-end-hook
'org-isearch-end
'append
'local
)
4945 ;; Emacs 22 deals with this through a special variable
4946 (org-set-local 'outline-isearch-open-invisible-function
4947 (lambda (&rest ignore
) (org-show-context 'isearch
))))
4949 ;; Turn on org-beamer-mode?
4950 (and org-startup-with-beamer-mode
(org-beamer-mode 1))
4952 ;; Setup the pcomplete hooks
4953 (set (make-local-variable 'pcomplete-command-completion-function
)
4954 'org-pcomplete-initial
)
4955 (set (make-local-variable 'pcomplete-command-name-function
)
4956 'org-command-at-point
)
4957 (set (make-local-variable 'pcomplete-default-completion-function
)
4959 (set (make-local-variable 'pcomplete-parse-arguments-function
)
4960 'org-parse-arguments
)
4961 (set (make-local-variable 'pcomplete-termination-string
) "")
4962 (when (>= emacs-major-version
23)
4963 (set (make-local-variable 'buffer-face-mode-face
) 'org-default
))
4965 ;; If empty file that did not turn on org-mode automatically, make it to.
4966 (if (and org-insert-mode-line-in-empty-file
4967 (org-called-interactively-p 'any
)
4968 (= (point-min) (point-max)))
4969 (insert "# -*- mode: org -*-\n\n"))
4970 (unless org-inhibit-startup
4971 (when org-startup-align-all-tables
4972 (let ((bmp (buffer-modified-p)))
4973 (org-table-map-tables 'org-table-align
'quietly
)
4974 (set-buffer-modified-p bmp
)))
4975 (when org-startup-with-inline-images
4976 (org-display-inline-images))
4977 (when org-startup-indented
4978 (require 'org-indent
)
4979 (org-indent-mode 1))
4980 (unless org-inhibit-startup-visibility-stuff
4981 (org-set-startup-visibility)))
4982 ;; Try to set org-hide correctly
4983 (set-face-foreground 'org-hide
(org-find-invisible-foreground)))
4985 (when (fboundp 'abbrev-table-put
)
4986 (abbrev-table-put org-mode-abbrev-table
4987 :parents
(list text-mode-abbrev-table
)))
4989 (put 'org-mode
'flyspell-mode-predicate
'org-mode-flyspell-verify
)
4992 (defun org-find-invisible-foreground ()
4993 (let ((candidates (remove
4996 (face-background 'default
)
4997 (face-background 'org-default
)
4998 (cdr (assoc 'background-color default-frame-alist
))
4999 (cdr (assoc 'background-color initial-frame-alist
))
5000 (cdr (assoc 'background-color window-system-default-frame-alist
))
5001 (face-foreground 'org-hide
)))))
5002 (car (remove nil candidates
))))
5004 (defun org-current-time ()
5005 "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
5006 (if (> (car org-time-stamp-rounding-minutes
) 1)
5007 (let ((r (car org-time-stamp-rounding-minutes
))
5008 (time (decode-time)))
5010 (append (list 0 (* r
(floor (+ .5 (/ (float (nth 1 time
)) r
)))))
5015 "Return today date, considering `org-extend-today-until'."
5017 (time-subtract (current-time)
5018 (list 0 (* 3600 org-extend-today-until
) 0))))
5020 ;;;; Font-Lock stuff, including the activators
5022 (defvar org-mouse-map
(make-sparse-keymap))
5023 (org-defkey org-mouse-map
[mouse-2
] 'org-open-at-mouse
)
5024 (org-defkey org-mouse-map
[mouse-3
] 'org-find-file-at-mouse
)
5025 (when org-mouse-1-follows-link
5026 (org-defkey org-mouse-map
[follow-link
] 'mouse-face
))
5027 (when org-tab-follows-link
5028 (org-defkey org-mouse-map
[(tab)] 'org-open-at-point
)
5029 (org-defkey org-mouse-map
"\C-i" 'org-open-at-point
))
5031 (require 'font-lock
)
5033 (defconst org-non-link-chars
"]\t\n\r<>")
5034 (defvar org-link-types
'("http" "https" "ftp" "mailto" "file" "news"
5035 "shell" "elisp" "doi" "message"))
5036 (defvar org-link-types-re nil
5037 "Matches a link that has a url-like prefix like \"http:\"")
5038 (defvar org-link-re-with-space nil
5039 "Matches a link with spaces, optional angular brackets around it.")
5040 (defvar org-link-re-with-space2 nil
5041 "Matches a link with spaces, optional angular brackets around it.")
5042 (defvar org-link-re-with-space3 nil
5043 "Matches a link with spaces, only for internal part in bracket links.")
5044 (defvar org-angle-link-re nil
5045 "Matches link with angular brackets, spaces are allowed.")
5046 (defvar org-plain-link-re nil
5047 "Matches plain link, without spaces.")
5048 (defvar org-bracket-link-regexp nil
5049 "Matches a link in double brackets.")
5050 (defvar org-bracket-link-analytic-regexp nil
5051 "Regular expression used to analyze links.
5052 Here is what the match groups contain after a match:
5058 (defvar org-bracket-link-analytic-regexp
++ nil
5059 "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
5060 (defvar org-any-link-re nil
5061 "Regular expression matching any link.")
5063 (defcustom org-match-sexp-depth
3
5064 "Number of stacked braces for sub/superscript matching.
5065 This has to be set before loading org.el to be effective."
5066 :group
'org-export-translation
; ??????????????????????????/
5069 (defun org-create-multibrace-regexp (left right n
)
5070 "Create a regular expression which will match a balanced sexp.
5071 Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
5072 as single character strings.
5073 The regexp returned will match the entire expression including the
5074 delimiters. It will also define a single group which contains the
5075 match except for the outermost delimiters. The maximum depth of
5076 stacked delimiters is N. Escaping delimiters is not possible."
5077 (let* ((nothing (concat "[^" left right
"]*?"))
5080 (next (concat "\\(?:" nothing left nothing right
"\\)+" nothing
)))
5083 re
(concat re or next
)
5084 next
(concat "\\(?:" nothing left next right
"\\)+" nothing
)))
5085 (concat left
"\\(" re
"\\)" right
)))
5087 (defvar org-match-substring-regexp
5089 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5090 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5092 "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth
) "\\)"
5094 "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
5095 "The regular expression matching a sub- or superscript.")
5097 (defvar org-match-substring-with-braces-regexp
5099 "\\([^\\]\\|^\\)\\([_^]\\)\\("
5100 "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth
) "\\)"
5102 "The regular expression matching a sub- or superscript, forcing braces.")
5104 (defun org-make-link-regexps ()
5105 "Update the link regular expressions.
5106 This should be called after the variable `org-link-types' has changed."
5107 (setq org-link-types-re
5109 "\\`\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):")
5110 org-link-re-with-space
5112 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5113 "\\([^" org-non-link-chars
" ]"
5114 "[^" org-non-link-chars
"]*"
5115 "[^" org-non-link-chars
" ]\\)>?")
5116 org-link-re-with-space2
5118 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5119 "\\([^" org-non-link-chars
" ]"
5121 "[^" org-non-link-chars
" ]\\)>?")
5122 org-link-re-with-space3
5124 "<?\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5125 "\\([^" org-non-link-chars
" ]"
5129 "<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5130 "\\([^" org-non-link-chars
" ]"
5131 "[^" org-non-link-chars
"]*"
5135 "\\<\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):"
5136 (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
5137 ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
5138 org-bracket-link-regexp
5139 "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
5140 org-bracket-link-analytic-regexp
5143 "\\(\\(" (mapconcat 'regexp-quote org-link-types
"\\|") "\\):\\)?"
5146 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5148 org-bracket-link-analytic-regexp
++
5151 "\\(\\(" (mapconcat 'regexp-quote
(cons "coderef" org-link-types
) "\\|") "\\):\\)?"
5154 "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
5157 (concat "\\(" org-bracket-link-regexp
"\\)\\|\\("
5158 org-angle-link-re
"\\)\\|\\("
5159 org-plain-link-re
"\\)")))
5161 (org-make-link-regexps)
5163 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
5164 "Regular expression for fast time stamp matching.")
5165 (defconst org-ts-regexp-both
"[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
5166 "Regular expression for fast time stamp matching.")
5167 (defconst org-ts-regexp0
5168 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5169 "Regular expression matching time strings for analysis.
5170 This one does not require the space after the date, so it can be used
5171 on a string that terminates immediately after the date.")
5172 (defconst org-ts-regexp1
"\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
5173 "Regular expression matching time strings for analysis.")
5174 (defconst org-ts-regexp2
(concat "<" org-ts-regexp1
"[^>\n]\\{0,16\\}>")
5175 "Regular expression matching time stamps, with groups.")
5176 (defconst org-ts-regexp3
(concat "[[<]" org-ts-regexp1
"[^]>\n]\\{0,16\\}[]>]")
5177 "Regular expression matching time stamps (also [..]), with groups.")
5178 (defconst org-tr-regexp
(concat org-ts-regexp
"--?-?" org-ts-regexp
)
5179 "Regular expression matching a time stamp range.")
5180 (defconst org-tr-regexp-both
5181 (concat org-ts-regexp-both
"--?-?" org-ts-regexp-both
)
5182 "Regular expression matching a time stamp range.")
5183 (defconst org-tsr-regexp
(concat org-ts-regexp
"\\(--?-?"
5184 org-ts-regexp
"\\)?")
5185 "Regular expression matching a time stamp or time stamp range.")
5186 (defconst org-tsr-regexp-both
(concat org-ts-regexp-both
"\\(--?-?"
5187 org-ts-regexp-both
"\\)?")
5188 "Regular expression matching a time stamp or time stamp range.
5189 The time stamps may be either active or inactive.")
5191 (defvar org-emph-face nil
)
5193 (defun org-do-emphasis-faces (limit)
5194 "Run through the buffer and add overlays to emphasized strings."
5196 (while (and (not rtn
) (re-search-forward org-emph-re limit t
))
5197 (if (not (= (char-after (match-beginning 3))
5198 (char-after (match-beginning 4))))
5201 (setq a
(assoc (match-string 3) org-emphasis-alist
))
5202 (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
5206 (org-remove-flyspell-overlays-in
5207 (match-beginning 0) (match-end 0)))
5208 (add-text-properties (match-beginning 2) (match-end 2)
5209 '(font-lock-multiline t org-emphasis t
))
5210 (when org-hide-emphasis-markers
5211 (add-text-properties (match-end 4) (match-beginning 5)
5212 '(invisible org-link
))
5213 (add-text-properties (match-beginning 3) (match-end 3)
5214 '(invisible org-link
)))))
5218 (defun org-emphasize (&optional char
)
5219 "Insert or change an emphasis, i.e. a font like bold or italic.
5220 If there is an active region, change that region to a new emphasis.
5221 If there is no region, just insert the marker characters and position
5222 the cursor between them.
5223 CHAR should be either the marker character, or the first character of the
5224 HTML tag associated with that emphasis. If CHAR is a space, the means
5225 to remove the emphasis of the selected region.
5226 If char is not given (for example in an interactive call) it
5227 will be prompted for."
5229 (let ((eal org-emphasis-alist
) e det
5230 (erc org-emphasis-regexp-components
)
5232 (string "") beg end move tag c s
)
5233 (if (org-region-active-p)
5234 (setq beg
(region-beginning) end
(region-end)
5235 string
(buffer-substring beg end
))
5238 (while (setq e
(pop eal
))
5239 (setq tag
(car (org-split-string (nth 2 e
) "[ <>/]+"))
5241 (push (cons c
(string-to-char (car e
))) det
)
5242 (setq prompt
(concat prompt
(format " [%s%c]%s" (car e
) c
5243 (substring tag
1)))))
5244 (setq det
(nreverse det
))
5246 (message "%s" (concat "Emphasis marker or tag:" prompt
))
5247 (setq char
(read-char-exclusive)))
5248 (setq char
(or (cdr (assoc char det
)) char
))
5249 (if (equal char ?\
)
5250 (setq s
"" move nil
)
5251 (unless (assoc (char-to-string char
) org-emphasis-alist
)
5252 (error "No such emphasis marker: \"%c\"" char
))
5253 (setq s
(char-to-string char
)))
5254 (while (and (> (length string
) 1)
5255 (equal (substring string
0 1) (substring string -
1))
5256 (assoc (substring string
0 1) org-emphasis-alist
))
5257 (setq string
(substring string
1 -
1)))
5258 (setq string
(concat s string s
))
5259 (if beg
(delete-region beg end
))
5261 (string-match (concat "[" (nth 0 erc
) "\n]")
5262 (char-to-string (char-before (point)))))
5265 (string-match (concat "[" (nth 1 erc
) "\n]")
5266 (char-to-string (char-after (point)))))
5267 (insert " ") (backward-char 1))
5269 (and move
(backward-char 1))))
5271 (defconst org-nonsticky-props
5272 '(mouse-face highlight keymap invisible intangible help-echo org-linked-text
))
5274 (defsubst org-rear-nonsticky-at
(pos)
5275 (add-text-properties (1- pos
) pos
(list 'rear-nonsticky org-nonsticky-props
)))
5277 (defun org-activate-plain-links (limit)
5278 "Run through the buffer and add overlays to links."
5281 (when (re-search-forward (concat org-plain-link-re
) limit t
)
5282 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5283 (setq f
(get-text-property (match-beginning 0) 'face
))
5284 (if (or (eq f
'org-tag
)
5285 (and (listp f
) (memq 'org-tag f
)))
5287 (add-text-properties (match-beginning 0) (match-end 0)
5288 (list 'mouse-face
'highlight
5290 'keymap org-mouse-map
))
5291 (org-rear-nonsticky-at (match-end 0)))
5294 (defun org-activate-code (limit)
5295 (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t
)
5297 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5298 (remove-text-properties (match-beginning 0) (match-end 0)
5299 '(display t invisible t intangible t
))
5302 (defcustom org-src-fontify-natively nil
5303 "When non-nil, fontify code in code blocks."
5306 :group
'org-appearance
5309 (defcustom org-allow-promoting-top-level-subtree nil
5310 "When non-nil, allow promoting a top level subtree.
5311 The leading star of the top level headline will be replaced
5315 :group
'org-appearance
)
5317 (defun org-fontify-meta-lines-and-blocks (limit)
5319 (org-fontify-meta-lines-and-blocks-1 limit
)
5320 (error (message "org-mode fontification error"))))
5322 (defun org-fontify-meta-lines-and-blocks-1 (limit)
5323 "Fontify #+ lines and blocks, in the correct ways."
5324 (let ((case-fold-search t
))
5325 (if (re-search-forward
5326 "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
5328 (let ((beg (match-beginning 0))
5329 (block-start (match-end 0))
5331 (lang (match-string 7))
5332 (beg1 (line-beginning-position 2))
5333 (dc1 (downcase (match-string 2)))
5334 (dc3 (downcase (match-string 3)))
5335 end end1 quoting block-type ovl
)
5337 ((member dc1
'("+html:" "+ascii:" "+latex:" "+docbook:"))
5338 ;; a single line of backend-specific content
5339 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5340 (remove-text-properties (match-beginning 0) (match-end 0)
5341 '(display t invisible t intangible t
))
5342 (add-text-properties (match-beginning 1) (match-end 3)
5343 '(font-lock-fontified t face org-meta-line
))
5344 (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
5345 '(font-lock-fontified t face org-block
))
5346 ; for backend-specific code
5348 ((and (match-end 4) (equal dc3
"+begin"))
5350 (setq block-type
(downcase (match-string 5))
5351 quoting
(member block-type org-protecting-blocks
))
5352 (when (re-search-forward
5353 (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
5354 nil t
) ;; on purpose, we look further than LIMIT
5355 (setq end
(min (point-max) (match-end 0))
5356 end1
(min (point-max) (1- (match-beginning 0))))
5357 (setq block-end
(match-beginning 0))
5359 (remove-text-properties beg end
5360 '(display t invisible t intangible t
)))
5361 (add-text-properties
5363 '(font-lock-fontified t font-lock-multiline t
))
5364 (add-text-properties beg beg1
'(face org-meta-line
))
5365 (add-text-properties end1
(min (point-max) (1+ end
))
5366 '(face org-meta-line
)) ; for end_src
5368 ((and lang
(not (string= lang
"")) org-src-fontify-natively
)
5369 (org-src-font-lock-fontify-block lang block-start block-end
)
5370 ;; remove old background overlays
5372 (if (eq (overlay-get ov
'face
) 'org-block-background
)
5373 (delete-overlay ov
)))
5374 (overlays-at (/ (+ beg1 block-end
) 2)))
5375 ;; add a background overlay
5376 (setq ovl
(make-overlay beg1 block-end
))
5377 (overlay-put ovl
'face
'org-block-background
)
5378 (overlay-put ovl
'evaporate t
)) ;; make it go away when empty
5380 (add-text-properties beg1
(min (point-max) (1+ end1
))
5381 '(face org-block
))) ; end of source block
5382 ((not org-fontify-quote-and-verse-blocks
))
5383 ((string= block-type
"quote")
5384 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-quote
)))
5385 ((string= block-type
"verse")
5386 (add-text-properties beg1
(min (point-max) (1+ end1
)) '(face org-verse
))))
5387 (add-text-properties beg beg1
'(face org-block-begin-line
))
5388 (add-text-properties (min (point-max) (1+ end
)) (min (point-max) (1+ end1
))
5389 '(face org-block-end-line
))
5391 ((member dc1
'("+title:" "+author:" "+email:" "+date:"))
5392 (add-text-properties
5394 (if (member (intern (substring dc1
0 -
1)) org-hidden-keywords
)
5395 '(font-lock-fontified t invisible t
)
5396 '(font-lock-fontified t face org-document-info-keyword
)))
5397 (add-text-properties
5398 (match-beginning 6) (match-end 6)
5399 (if (string-equal dc1
"+title:")
5400 '(font-lock-fontified t face org-document-title
)
5401 '(font-lock-fontified t face org-document-info
))))
5402 ((or (equal dc1
"+results")
5403 (member dc1
'("+begin:" "+end:" "+caption:" "+label:"
5404 "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
5405 "+call:" "+header:" "+headers:" "+name:"))
5406 (and (match-end 4) (equal dc3
"+attr")))
5407 (add-text-properties
5409 '(font-lock-fontified t face org-meta-line
))
5411 ((member dc3
'(" " ""))
5412 (add-text-properties
5414 '(font-lock-fontified t face font-lock-comment-face
)))
5415 ((not (member (char-after beg
) '(?\ ?
\t)))
5416 ;; just any other in-buffer setting, but not indented
5417 (add-text-properties
5419 '(font-lock-fontified t face org-meta-line
))
5423 (defun org-activate-angle-links (limit)
5424 "Run through the buffer and add overlays to links."
5425 (if (re-search-forward org-angle-link-re limit t
)
5427 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5428 (add-text-properties (match-beginning 0) (match-end 0)
5429 (list 'mouse-face
'highlight
5430 'keymap org-mouse-map
))
5431 (org-rear-nonsticky-at (match-end 0))
5434 (defun org-activate-footnote-links (limit)
5435 "Run through the buffer and add overlays to footnotes."
5436 (let ((fn (org-footnote-next-reference-or-definition limit
)))
5438 (let ((beg (nth 1 fn
)) (end (nth 2 fn
)))
5439 (org-remove-flyspell-overlays-in beg end
)
5440 (add-text-properties beg end
5441 (list 'mouse-face
'highlight
5442 'keymap org-mouse-map
5444 (if (= (point-at-bol) beg
)
5445 "Footnote definition"
5446 "Footnote reference")
5447 'font-lock-fontified t
5448 'font-lock-multiline t
5449 'face
'org-footnote
))))))
5451 (defun org-activate-bracket-links (limit)
5452 "Run through the buffer and add overlays to bracketed links."
5453 (if (re-search-forward org-bracket-link-regexp limit t
)
5454 (let* ((help (concat "LINK: "
5455 (org-match-string-no-properties 1)))
5456 ;; FIXME: above we should remove the escapes.
5457 ;; but that requires another match, protecting match data,
5458 ;; a lot of overhead for font-lock.
5459 (ip (org-maybe-intangible
5460 (list 'invisible
'org-link
5461 'keymap org-mouse-map
'mouse-face
'highlight
5462 'font-lock-multiline t
'help-echo help
)))
5463 (vp (list 'keymap org-mouse-map
'mouse-face
'highlight
5464 'font-lock-multiline t
'help-echo help
)))
5465 ;; We need to remove the invisible property here. Table narrowing
5466 ;; may have made some of this invisible.
5467 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5468 (remove-text-properties (match-beginning 0) (match-end 0)
5472 (add-text-properties (match-beginning 0) (match-beginning 3) ip
)
5473 (org-rear-nonsticky-at (match-beginning 3))
5474 (add-text-properties (match-beginning 3) (match-end 3) vp
)
5475 (org-rear-nonsticky-at (match-end 3))
5476 (add-text-properties (match-end 3) (match-end 0) ip
)
5477 (org-rear-nonsticky-at (match-end 0)))
5478 (add-text-properties (match-beginning 0) (match-beginning 1) ip
)
5479 (org-rear-nonsticky-at (match-beginning 1))
5480 (add-text-properties (match-beginning 1) (match-end 1) vp
)
5481 (org-rear-nonsticky-at (match-end 1))
5482 (add-text-properties (match-end 1) (match-end 0) ip
)
5483 (org-rear-nonsticky-at (match-end 0)))
5486 (defun org-activate-dates (limit)
5487 "Run through the buffer and add overlays to dates."
5488 (if (re-search-forward org-tsr-regexp-both limit t
)
5490 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5491 (add-text-properties (match-beginning 0) (match-end 0)
5492 (list 'mouse-face
'highlight
5493 'keymap org-mouse-map
))
5494 (org-rear-nonsticky-at (match-end 0))
5495 (when org-display-custom-times
5497 (org-display-custom-time (match-beginning 3) (match-end 3)))
5498 (org-display-custom-time (match-beginning 1) (match-end 1)))
5501 (defvar org-target-link-regexp nil
5502 "Regular expression matching radio targets in plain text.")
5503 (make-variable-buffer-local 'org-target-link-regexp
)
5504 (defvar org-target-regexp
"<<\\([^<>\n\r]+\\)>>"
5505 "Regular expression matching a link target.")
5506 (defvar org-radio-target-regexp
"<<<\\([^<>\n\r]+\\)>>>"
5507 "Regular expression matching a radio target.")
5508 (defvar org-any-target-regexp
"<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
5509 "Regular expression matching any target.")
5511 (defun org-activate-target-links (limit)
5512 "Run through the buffer and add overlays to target matches."
5513 (when org-target-link-regexp
5514 (let ((case-fold-search t
))
5515 (if (re-search-forward org-target-link-regexp limit t
)
5517 (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
5518 (add-text-properties (match-beginning 0) (match-end 0)
5519 (list 'mouse-face
'highlight
5520 'keymap org-mouse-map
5521 'help-echo
"Radio target link"
5522 'org-linked-text t
))
5523 (org-rear-nonsticky-at (match-end 0))
5526 (defun org-update-radio-target-regexp ()
5527 "Find all radio targets in this file and update the regular expression."
5529 (when (memq 'radio org-activate-links
)
5530 (setq org-target-link-regexp
5531 (org-make-target-link-regexp (org-all-targets 'radio
)))
5532 (org-restart-font-lock)))
5534 (defun org-hide-wide-columns (limit)
5536 (setq s
(text-property-any (point) (or limit
(point-max))
5539 (setq e
(next-single-property-change s
'org-cwidth
))
5540 (add-text-properties s e
(org-maybe-intangible '(invisible org-cwidth
)))
5544 (defvar org-latex-and-specials-regexp nil
5545 "Regular expression for highlighting export special stuff.")
5546 (defvar org-match-substring-regexp
)
5547 (defvar org-match-substring-with-braces-regexp
)
5549 ;; This should be with the exporter code, but we also use if for font-locking
5550 (defconst org-export-html-special-string-regexps
5551 '(("\\\\-" .
"­")
5552 ("---\\([^-]\\)" .
"—\\1")
5553 ("--\\([^-]\\)" .
"–\\1")
5554 ("\\.\\.\\." .
"…"))
5555 "Regular expressions for special string conversion.")
5558 (defun org-compute-latex-and-specials-regexp ()
5559 "Compute regular expression for stuff treated specially by exporters."
5560 (if (not org-highlight-latex-fragments-and-specials
)
5561 (org-set-local 'org-latex-and-specials-regexp nil
)
5564 ((matchers (plist-get org-format-latex-options
:matchers
))
5565 (latexs (delq nil
(mapcar (lambda (x) (if (member (car x
) matchers
) x
))
5566 org-latex-regexps
)))
5567 (org-export-allow-BIND nil
)
5568 (options (org-combine-plists (org-default-export-plist)
5569 (org-infile-export-plist)))
5570 (org-export-with-sub-superscripts (plist-get options
:sub-superscript
))
5571 (org-export-with-LaTeX-fragments (plist-get options
:LaTeX-fragments
))
5572 (org-export-with-TeX-macros (plist-get options
:TeX-macros
))
5573 (org-export-html-expand (plist-get options
:expand-quoted-html
))
5574 (org-export-with-special-strings (plist-get options
:special-strings
))
5577 ((equal org-export-with-sub-superscripts
'{})
5578 (list org-match-substring-with-braces-regexp
))
5579 (org-export-with-sub-superscripts
5580 (list org-match-substring-regexp
))))
5582 (if org-export-with-LaTeX-fragments
5583 (mapcar (lambda (x) (nth 1 x
)) latexs
)))
5585 (if org-export-with-TeX-macros
5586 (list (concat "\\\\"
5592 (append org-entities-user
5594 (if (boundp 'org-latex-entities
)
5596 (or (car-safe x
) x
))
5601 ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
5602 (re-special (if org-export-with-special-strings
5603 (mapcar (lambda (x) (car x
))
5604 org-export-html-special-string-regexps
)))
5608 (if org-export-html-expand
"@<[^>\n]+>")
5611 'org-latex-and-specials-regexp
5612 (mapconcat 'identity
(append re-latex re-sub re-macros re-special
5615 (defun org-do-latex-and-special-faces (limit)
5616 "Run through the buffer and add overlays to links."
5617 (when org-latex-and-specials-regexp
5619 (while (and (not rtn
) (re-search-forward org-latex-and-specials-regexp
5621 (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
5623 '(org-code org-verbatim underline
)))
5626 d
(cond ((member (char-after (1+ (match-beginning 0)))
5629 (font-lock-prepend-text-property
5630 (+ d
(match-beginning 0)) (match-end 0)
5631 'face
'org-latex-and-export-specials
)
5632 (add-text-properties (+ d
(match-beginning 0)) (match-end 0)
5633 '(font-lock-multiline t
)))))
5636 (defun org-restart-font-lock ()
5637 "Restart `font-lock-mode', to force refontification."
5638 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
5640 (font-lock-mode 1)))
5642 (defun org-all-targets (&optional radio
)
5643 "Return a list of all targets in this file.
5644 With optional argument RADIO, only find radio targets."
5645 (let ((re (if radio org-radio-target-regexp org-target-regexp
))
5648 (goto-char (point-min))
5649 (while (re-search-forward re nil t
)
5650 (add-to-list 'rtn
(downcase (org-match-string-no-properties 1))))
5653 (defun org-make-target-link-regexp (targets)
5654 "Make regular expression matching all strings in TARGETS.
5655 The regular expression finds the targets also if there is a line break
5662 (setq x
(regexp-quote x
))
5663 (while (string-match " +" x
)
5664 (setq x
(replace-match "\\s-+" t t x
)))
5670 (defun org-activate-tags (limit)
5671 (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t
)
5673 (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
5674 (add-text-properties (match-beginning 1) (match-end 1)
5675 (list 'mouse-face
'highlight
5676 'keymap org-mouse-map
))
5677 (org-rear-nonsticky-at (match-end 1))
5680 (defun org-outline-level ()
5681 "Compute the outline level of the heading at point.
5682 This function assumes that the cursor is at the beginning of a line matched
5683 by `outline-regexp'. Otherwise it returns garbage.
5684 If this is called at a normal headline, the level is the number of stars.
5685 Use `org-reduced-level' to remove the effect of `org-odd-levels'."
5687 (looking-at org-outline-regexp
)
5688 (1- (- (match-end 0) (match-beginning 0)))))
5690 (defvar org-font-lock-keywords nil
)
5692 (defconst org-property-re
(org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
5693 "Regular expression matching a property line.")
5695 (defvar org-font-lock-hook nil
5696 "Functions to be called for special font lock stuff.")
5698 (defvar org-font-lock-set-keywords-hook nil
5699 "Functions that can manipulate `org-font-lock-extra-keywords'.
5700 This is called after `org-font-lock-extra-keywords' is defined, but before
5701 it is installed to be used by font lock. This can be useful if something
5702 needs to be inserted at a specific position in the font-lock sequence.")
5704 (defun org-font-lock-hook (limit)
5705 "Run `org-font-lock-hook' within LIMIT."
5706 (run-hook-with-args 'org-font-lock-hook limit
))
5708 (defun org-set-font-lock-defaults ()
5709 "Set font lock defaults for the current buffer."
5710 (let* ((em org-fontify-emphasized-text
)
5711 (lk org-activate-links
)
5712 (org-font-lock-extra-keywords
5715 '(org-font-lock-hook)
5717 `(,(if org-fontify-whole-heading-line
5718 "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
5719 "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
5720 (1 (org-get-level-face 1))
5721 (2 (org-get-level-face 2))
5722 (3 (org-get-level-face 3)))
5724 '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
5727 '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t
))
5728 '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t
))
5729 '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t
))
5730 '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t
))
5732 (list org-drawer-regexp
'(0 'org-special-keyword t
))
5733 (list "^[ \t]*:END:" '(0 'org-special-keyword t
))
5735 (list org-property-re
5736 '(1 'org-special-keyword t
)
5737 '(3 'org-property-value t
))
5739 (if (memq 'tag lk
) '(org-activate-tags (1 'org-tag prepend
)))
5740 (if (memq 'angle lk
) '(org-activate-angle-links (0 'org-link t
)))
5741 (if (memq 'plain lk
) '(org-activate-plain-links))
5742 (if (memq 'bracket lk
) '(org-activate-bracket-links (0 'org-link t
)))
5743 (if (memq 'radio lk
) '(org-activate-target-links (0 'org-link t
)))
5744 (if (memq 'date lk
) '(org-activate-dates (0 'org-date t
)))
5745 (if (memq 'footnote lk
) '(org-activate-footnote-links))
5746 '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t
))
5747 '(org-hide-wide-columns (0 nil append
))
5749 (list (format org-heading-keyword-regexp-format
5751 '(2 (org-get-todo-face 2) t
))
5753 (if org-fontify-done-headline
5754 (list (format org-heading-keyword-regexp-format
5757 (mapconcat 'regexp-quote org-done-keywords
"\\|")
5759 '(2 'org-headline-done t
))
5762 '(org-font-lock-add-priority-faces)
5764 '(org-font-lock-add-tag-faces)
5766 (list (concat "\\<" org-deadline-string
) '(0 'org-special-keyword t
))
5767 (list (concat "\\<" org-scheduled-string
) '(0 'org-special-keyword t
))
5768 (list (concat "\\<" org-closed-string
) '(0 'org-special-keyword t
))
5769 (list (concat "\\<" org-clock-string
) '(0 'org-special-keyword t
))
5772 (if (featurep 'xemacs
)
5773 '(org-do-emphasis-faces (0 nil append
))
5774 '(org-do-emphasis-faces)))
5776 '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
5777 1 'org-checkbox prepend
)
5778 (if (cdr (assq 'checkbox org-list-automatic-rules
))
5779 '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
5780 (0 (org-get-checkbox-statistics-face) t
)))
5781 ;; Description list items
5782 '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
5783 1 'org-list-dt prepend
)
5784 ;; ARCHIVEd headings
5786 org-outline-regexp-bol
5787 "\\(.*:" org-archive-tag
":.*\\)")
5788 '(1 'org-archived prepend
))
5790 '(org-do-latex-and-special-faces)
5791 '(org-fontify-entities)
5792 '(org-raise-scripts)
5794 '(org-activate-code (1 'org-code t
))
5796 (list (format org-heading-keyword-regexp-format
5798 org-comment-string
"\\|" org-quote-string
5800 '(2 'org-special-keyword t
))
5801 ;; Blocks and meta lines
5802 '(org-fontify-meta-lines-and-blocks)
5804 (setq org-font-lock-extra-keywords
(delq nil org-font-lock-extra-keywords
))
5805 (run-hooks 'org-font-lock-set-keywords-hook
)
5806 ;; Now set the full font-lock-keywords
5807 (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords
)
5808 (org-set-local 'font-lock-defaults
5809 '(org-font-lock-keywords t nil nil backward-paragraph
))
5810 (kill-local-variable 'font-lock-keywords
) nil
))
5812 (defun org-toggle-pretty-entities ()
5813 "Toggle the composition display of entities as UTF8 characters."
5815 (org-set-local 'org-pretty-entities
(not org-pretty-entities
))
5816 (org-restart-font-lock)
5817 (if org-pretty-entities
5818 (message "Entities are displayed as UTF8 characters")
5821 (org-decompose-region (point-min) (point-max))
5822 (message "Entities are displayed plain"))))
5824 (defvar org-custom-properties-overlays nil
5825 "List of overlays used for custom properties.")
5826 (make-variable-buffer-local 'org-custom-properties-overlays
)
5828 (defun org-toggle-custom-properties-visibility ()
5829 "Display or hide properties in `org-custom-properties'."
5831 (if org-custom-properties-overlays
5832 (progn (mapc 'delete-overlay org-custom-properties-overlays
)
5833 (setq org-custom-properties-overlays nil
))
5834 (unless (not org-custom-properties
)
5838 (goto-char (point-min))
5839 (while (re-search-forward org-property-re nil t
)
5841 (when (equal p
(substring (match-string 1) 1 -
1))
5842 (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
5843 (overlay-put o
'invisible t
)
5844 (overlay-put o
'org-custom-property t
)
5845 (push o org-custom-properties-overlays
))))
5846 org-custom-properties
)))))))
5848 (defun org-fontify-entities (limit)
5849 "Find an entity to fontify."
5851 (when org-pretty-entities
5853 (while (re-search-forward
5854 "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
5856 (if (and (not (org-in-indented-comment-line))
5857 (setq ee
(org-entity-get (match-string 1)))
5858 (= (length (nth 6 ee
)) 1))
5860 ((end (if (equal (match-string 2) "{}")
5863 (add-text-properties
5864 (match-beginning 0) end
5865 (list 'font-lock-fontified t
))
5866 (compose-region (match-beginning 0) end
5872 (defun org-fontify-like-in-org-mode (s &optional odd-levels
)
5873 "Fontify string S like in Org-mode."
5876 (let ((org-odd-levels-only odd-levels
))
5878 (font-lock-fontify-buffer)
5884 (defun org-get-level-face (n)
5885 "Get the right face for match N in font-lock matching of headlines."
5886 (setq org-l
(- (match-end 2) (match-beginning 1) 1))
5887 (if org-odd-levels-only
(setq org-l
(1+ (/ org-l
2))))
5888 (if org-cycle-level-faces
5889 (setq org-f
(nth (%
(1- org-l
) org-n-level-faces
) org-level-faces
))
5890 (setq org-f
(nth (1- (min org-l org-n-level-faces
)) org-level-faces
)))
5892 ((eq n
1) (if org-hide-leading-stars
'org-hide org-f
))
5894 (t (if org-level-color-stars-only nil org-f
))))
5897 (defun org-get-todo-face (kwd)
5898 "Get the right face for a TODO keyword KWD.
5899 If KWD is a number, get the corresponding match group."
5900 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5901 (or (org-face-from-face-or-color
5902 'todo
'org-todo
(cdr (assoc kwd org-todo-keyword-faces
)))
5903 (and (member kwd org-done-keywords
) 'org-done
)
5906 (defun org-face-from-face-or-color (context inherit face-or-color
)
5907 "Create a face list that inherits INHERIT, but sets the foreground color.
5908 When FACE-OR-COLOR is not a string, just return it."
5909 (if (stringp face-or-color
)
5910 (list :inherit inherit
5911 (cdr (assoc context org-faces-easy-properties
))
5915 (defun org-font-lock-add-tag-faces (limit)
5916 "Add the special tag faces."
5917 (when (and org-tag-faces org-tags-special-faces-re
)
5918 (while (re-search-forward org-tags-special-faces-re limit t
)
5919 (add-text-properties (match-beginning 1) (match-end 1)
5920 (list 'face
(org-get-tag-face 1)
5921 'font-lock-fontified t
))
5922 (backward-char 1))))
5924 (defun org-font-lock-add-priority-faces (limit)
5925 "Add the special priority faces."
5926 (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t
)
5927 (when (save-match-data (org-at-heading-p))
5928 (add-text-properties
5929 (match-beginning 0) (match-end 0)
5930 (list 'face
(or (org-face-from-face-or-color
5931 'priority
'org-special-keyword
5932 (cdr (assoc (char-after (match-beginning 1))
5933 org-priority-faces
)))
5934 'org-special-keyword
)
5935 'font-lock-fontified t
)))))
5937 (defun org-get-tag-face (kwd)
5938 "Get the right face for a TODO keyword KWD.
5939 If KWD is a number, get the corresponding match group."
5940 (if (numberp kwd
) (setq kwd
(match-string kwd
)))
5941 (or (org-face-from-face-or-color
5942 'tag
'org-tag
(cdr (assoc kwd org-tag-faces
)))
5945 (defun org-unfontify-region (beg end
&optional maybe_loudly
)
5946 "Remove fontification and activation overlays from links."
5947 (font-lock-default-unfontify-region beg end
)
5948 (let* ((buffer-undo-list t
)
5949 (inhibit-read-only t
) (inhibit-point-motion-hooks t
)
5950 (inhibit-modification-hooks t
)
5951 deactivate-mark buffer-file-name buffer-file-truename
)
5952 (org-decompose-region beg end
)
5953 (remove-text-properties beg end
5954 '(mouse-face t keymap t org-linked-text t
5955 invisible t intangible t
5956 org-no-flyspell t org-emphasis t
))
5957 (org-remove-font-lock-display-properties beg end
)))
5959 (defconst org-script-display
'(((raise -
0.3) (height 0.7))
5960 ((raise 0.3) (height 0.7))
5963 "Display properties for showing superscripts and subscripts.")
5965 (defun org-remove-font-lock-display-properties (beg end
)
5966 "Remove specific display properties that have been added by font lock.
5967 The will remove the raise properties that are used to show superscripts
5971 (setq next
(next-single-property-change beg
'display nil end
)
5972 prop
(get-text-property beg
'display
))
5973 (if (member prop org-script-display
)
5974 (put-text-property beg next
'display nil
))
5977 (defun org-raise-scripts (limit)
5978 "Add raise properties to sub/superscripts."
5979 (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
)
5980 (if (re-search-forward
5981 (if (eq org-use-sub-superscripts t
)
5982 org-match-substring-regexp
5983 org-match-substring-with-braces-regexp
)
5985 (let* ((pos (point)) table-p comment-p
5986 (mpos (match-beginning 3))
5987 (emph-p (get-text-property mpos
'org-emphasis
))
5988 (link-p (get-text-property mpos
'mouse-face
))
5989 (keyw-p (eq 'org-special-keyword
(get-text-property mpos
'face
))))
5990 (goto-char (point-at-bol))
5991 (setq table-p
(org-looking-at-p org-table-dataline-regexp
)
5992 comment-p
(org-looking-at-p "[ \t]*#"))
5994 ;; FIXME: Should we go back one character here, for a_b^c
5995 ;; (goto-char (1- pos)) ;????????????????????
5996 (if (or comment-p emph-p link-p keyw-p
)
5998 (put-text-property (match-beginning 3) (match-end 0)
6000 (if (equal (char-after (match-beginning 2)) ?^
)
6001 (nth (if table-p
3 1) org-script-display
)
6002 (nth (if table-p
2 0) org-script-display
)))
6003 (add-text-properties (match-beginning 2) (match-end 2)
6005 'org-dwidth t
'org-dwidth-n
1))
6006 (if (and (eq (char-after (match-beginning 3)) ?
{)
6007 (eq (char-before (match-end 3)) ?
}))
6009 (add-text-properties
6010 (match-beginning 3) (1+ (match-beginning 3))
6011 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))
6012 (add-text-properties
6013 (1- (match-end 3)) (match-end 3)
6014 (list 'invisible t
'org-dwidth t
'org-dwidth-n
1))))
6017 ;;;; Visibility cycling, including org-goto and indirect buffer
6021 (defvar org-cycle-global-status nil
)
6022 (make-variable-buffer-local 'org-cycle-global-status
)
6023 (defvar org-cycle-subtree-status nil
)
6024 (make-variable-buffer-local 'org-cycle-subtree-status
)
6026 (defvar org-inlinetask-min-level
)
6029 (defun org-cycle (&optional arg
)
6030 "TAB-action and visibility cycling for Org-mode.
6032 This is the command invoked in Org-mode by the TAB key. Its main purpose
6033 is outline visibility cycling, but it also invokes other actions
6034 in special contexts.
6036 - When this function is called with a prefix argument, rotate the entire
6037 buffer through 3 states (global cycling)
6038 1. OVERVIEW: Show only top-level headlines.
6039 2. CONTENTS: Show all headlines of all levels, but no body text.
6040 3. SHOW ALL: Show everything.
6041 When called with two `C-u C-u' prefixes, switch to the startup visibility,
6042 determined by the variable `org-startup-folded', and by any VISIBILITY
6043 properties in the buffer.
6044 When called with three `C-u C-u C-u' prefixed, show the entire buffer,
6045 including any drawers.
6047 - When inside a table, re-align the table and move to the next field.
6049 - When point is at the beginning of a headline, rotate the subtree started
6050 by this line through 3 different states (local cycling)
6051 1. FOLDED: Only the main headline is shown.
6052 2. CHILDREN: The main headline and the direct children are shown.
6053 From this state, you can move to one of the children
6054 and zoom in further.
6055 3. SUBTREE: Show the entire subtree, including body text.
6056 If there is no subtree, switch directly from CHILDREN to FOLDED.
6058 - When point is at the beginning of an empty headline and the variable
6059 `org-cycle-level-after-item/entry-creation' is set, cycle the level
6060 of the headline by demoting and promoting it to likely levels. This
6061 speeds up creation document structure by pressing TAB once or several
6062 times right after creating a new headline.
6064 - When there is a numeric prefix, go up to a heading with level ARG, do
6065 a `show-subtree' and return to the previous cursor position. If ARG
6066 is negative, go up that many levels.
6068 - When point is not at the beginning of a headline, execute the global
6069 binding for TAB, which is re-indenting the line. See the option
6070 `org-cycle-emulate-tab' for details.
6072 - Special case: if point is at the beginning of the buffer and there is
6073 no headline in line 1, this function will act as if called with prefix arg
6074 (C-u TAB, same as S-TAB) also when called without prefix arg.
6075 But only if also the variable `org-cycle-global-at-bob' is t."
6077 (org-load-modules-maybe)
6078 (unless (or (run-hook-with-args-until-success 'org-tab-first-hook
)
6079 (and org-cycle-level-after-item
/entry-creation
6080 (or (org-cycle-level)
6081 (org-cycle-item-indentation))))
6083 (or org-cycle-max-level
6084 (and (boundp 'org-inlinetask-min-level
)
6085 org-inlinetask-min-level
6086 (1- org-inlinetask-min-level
))))
6087 (nstars (and limit-level
6088 (if org-odd-levels-only
6089 (and limit-level
(1- (* limit-level
2)))
6092 (if (not (derived-mode-p 'org-mode
))
6094 (concat "\\*" (if nstars
(format "\\{1,%d\\} " nstars
) "+ "))))
6095 (bob-special (and org-cycle-global-at-bob
(not arg
) (bobp)
6096 (not (looking-at org-outline-regexp
))))
6099 (delq 'org-optimize-window-after-visibility-change
6100 (copy-sequence org-cycle-hook
))
6104 (if (or bob-special
(equal arg
'(4)))
6105 ;; special case: use global cycling
6111 (setq last-command
'dummy
)
6112 (org-set-startup-visibility)
6113 (message "Startup visibility, plus VISIBILITY properties"))
6117 (message "Entire buffer visible, including drawers"))
6119 ;; Table: enter it or move to the next field.
6120 ((org-at-table-p 'any
)
6121 (if (org-at-table.el-p
)
6122 (message "Use C-c ' to edit table.el tables")
6123 (if arg
(org-table-edit-field t
)
6124 (org-table-justify-field-maybe)
6125 (call-interactively 'org-table-next-field
))))
6127 ((run-hook-with-args-until-success
6128 'org-tab-after-check-for-table-hook
))
6130 ;; Global cycling: delegate to `org-cycle-internal-global'.
6131 ((eq arg t
) (org-cycle-internal-global))
6133 ;; Drawers: delegate to `org-flag-drawer'.
6134 ((and org-drawers org-drawer-regexp
6136 (beginning-of-line 1)
6137 (looking-at org-drawer-regexp
)))
6138 (org-flag-drawer ; toggle block visibility
6139 (not (get-char-property (match-end 0) 'invisible
))))
6141 ;; Show-subtree, ARG levels up from here.
6144 (org-back-to-heading)
6145 (outline-up-heading (if (< arg
0) (- arg
)
6146 (- (funcall outline-level
) arg
)))
6147 (org-show-subtree)))
6149 ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
6150 ((and (featurep 'org-inlinetask
)
6151 (org-inlinetask-at-task-p)
6152 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6153 (org-inlinetask-toggle-visibility))
6155 ((org-try-cdlatex-tab))
6157 ;; At an item/headline: delegate to `org-cycle-internal-local'.
6158 ((and (or (and org-cycle-include-plain-lists
(org-at-item-p))
6159 (save-excursion (beginning-of-line 1)
6160 (looking-at org-outline-regexp
)))
6161 (or (bolp) (not (eq org-cycle-emulate-tab
'exc-hl-bol
))))
6162 (org-cycle-internal-local))
6164 ;; From there: TAB emulation and template completion.
6165 (buffer-read-only (org-back-to-heading))
6167 ((run-hook-with-args-until-success
6168 'org-tab-after-check-for-cycling-hook
))
6170 ((org-try-structure-completion))
6172 ((run-hook-with-args-until-success
6173 'org-tab-before-tab-emulation-hook
))
6175 ((and (eq org-cycle-emulate-tab
'exc-hl-bol
)
6177 (not (looking-at org-outline-regexp
))))
6178 (call-interactively (global-key-binding "\t")))
6180 ((if (and (memq org-cycle-emulate-tab
'(white whitestart
))
6181 (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
6182 (or (and (eq org-cycle-emulate-tab
'white
)
6183 (= (match-end 0) (point-at-eol)))
6184 (and (eq org-cycle-emulate-tab
'whitestart
)
6185 (>= (match-end 0) pos
))))
6187 (eq org-cycle-emulate-tab t
))
6188 (call-interactively (global-key-binding "\t")))
6191 (org-back-to-heading)
6194 (defun org-cycle-internal-global ()
6195 "Do the global cycling action."
6196 ;; Hack to avoid display of messages for .org attachments in Gnus
6197 (let ((ga (string-match "\\*fontification" (buffer-name))))
6199 ((and (eq last-command this-command
)
6200 (eq org-cycle-global-status
'overview
))
6201 ;; We just created the overview - now do table of contents
6202 ;; This can be slow in very large buffers, so indicate action
6203 (run-hook-with-args 'org-pre-cycle-hook
'contents
)
6204 (unless ga
(message "CONTENTS..."))
6206 (unless ga
(message "CONTENTS...done"))
6207 (setq org-cycle-global-status
'contents
)
6208 (run-hook-with-args 'org-cycle-hook
'contents
))
6210 ((and (eq last-command this-command
)
6211 (eq org-cycle-global-status
'contents
))
6212 ;; We just showed the table of contents - now show everything
6213 (run-hook-with-args 'org-pre-cycle-hook
'all
)
6215 (unless ga
(message "SHOW ALL"))
6216 (setq org-cycle-global-status
'all
)
6217 (run-hook-with-args 'org-cycle-hook
'all
))
6220 ;; Default action: go to overview
6221 (run-hook-with-args 'org-pre-cycle-hook
'overview
)
6223 (unless ga
(message "OVERVIEW"))
6224 (setq org-cycle-global-status
'overview
)
6225 (run-hook-with-args 'org-cycle-hook
'overview
)))))
6227 (defun org-cycle-internal-local ()
6228 "Do the local cycling action."
6229 (let ((goal-column 0) eoh eol eos has-children children-skipped struct
)
6230 ;; First, determine end of headline (EOH), end of subtree or item
6231 ;; (EOS), and if item or heading has children (HAS-CHILDREN).
6236 (setq struct
(org-list-struct))
6237 (setq eoh
(point-at-eol))
6238 (setq eos
(org-list-get-item-end-before-blank (point) struct
))
6239 (setq has-children
(org-list-has-child-p (point) struct
)))
6240 (org-back-to-heading)
6241 (setq eoh
(save-excursion (outline-end-of-heading) (point)))
6242 (setq eos
(save-excursion
6243 (org-end-of-subtree t
)
6245 (skip-chars-forward " \t\n"))
6246 (if (eobp) (point) (1- (point)))))
6249 (let ((level (funcall outline-level
)))
6250 (outline-next-heading)
6251 (and (org-at-heading-p t
)
6252 (> (funcall outline-level
) level
))))
6254 (org-list-search-forward (org-item-beginning-re) eos t
)))))
6255 ;; Determine end invisible part of buffer (EOL)
6256 (beginning-of-line 2)
6257 ;; XEmacs doesn't have `next-single-char-property-change'
6258 (if (featurep 'xemacs
)
6259 (while (and (not (eobp)) ;; this is like `next-line'
6260 (get-char-property (1- (point)) 'invisible
))
6261 (beginning-of-line 2))
6262 (while (and (not (eobp)) ;; this is like `next-line'
6263 (get-char-property (1- (point)) 'invisible
))
6264 (goto-char (next-single-char-property-change (point) 'invisible
))
6265 (and (eolp) (beginning-of-line 2))))
6267 ;; Find out what to do next and set `this-command'
6270 ;; Nothing is hidden behind this heading
6271 (run-hook-with-args 'org-pre-cycle-hook
'empty
)
6272 (message "EMPTY ENTRY")
6273 (setq org-cycle-subtree-status nil
)
6276 (outline-next-heading)
6277 (if (outline-invisible-p) (org-flag-heading nil
))))
6278 ((and (or (>= eol eos
)
6279 (not (string-match "\\S-" (buffer-substring eol eos
))))
6281 (not (setq children-skipped
6282 org-cycle-skip-children-state-if-no-children
))))
6283 ;; Entire subtree is hidden in one line: children view
6284 (run-hook-with-args 'org-pre-cycle-hook
'children
)
6286 (org-list-set-item-visibility (point-at-bol) struct
'children
)
6288 (org-with-limited-levels (show-children))
6289 ;; FIXME: This slows down the func way too much.
6290 ;; How keep drawers hidden in subtree anyway?
6291 ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
6292 ;; (org-cycle-hide-drawers 'subtree))
6294 ;; Fold every list in subtree to top-level items.
6295 (when (eq org-cycle-include-plain-lists
'integrate
)
6297 (org-back-to-heading)
6298 (while (org-list-search-forward (org-item-beginning-re) eos t
)
6299 (beginning-of-line 1)
6300 (let* ((struct (org-list-struct))
6301 (prevs (org-list-prevs-alist struct
))
6302 (end (org-list-get-bottom-point struct
)))
6303 (mapc (lambda (e) (org-list-set-item-visibility e struct
'folded
))
6304 (org-list-get-all-items (point) struct prevs
))
6305 (goto-char end
))))))
6306 (message "CHILDREN")
6309 (outline-next-heading)
6310 (if (outline-invisible-p) (org-flag-heading nil
)))
6311 (setq org-cycle-subtree-status
'children
)
6312 (run-hook-with-args 'org-cycle-hook
'children
))
6313 ((or children-skipped
6314 (and (eq last-command this-command
)
6315 (eq org-cycle-subtree-status
'children
)))
6316 ;; We just showed the children, or no children are there,
6317 ;; now show everything.
6318 (run-hook-with-args 'org-pre-cycle-hook
'subtree
)
6319 (outline-flag-region eoh eos nil
)
6320 (message (if children-skipped
"SUBTREE (NO CHILDREN)" "SUBTREE"))
6321 (setq org-cycle-subtree-status
'subtree
)
6322 (run-hook-with-args 'org-cycle-hook
'subtree
))
6324 ;; Default action: hide the subtree.
6325 (run-hook-with-args 'org-pre-cycle-hook
'folded
)
6326 (outline-flag-region eoh eos t
)
6328 (setq org-cycle-subtree-status
'folded
)
6329 (run-hook-with-args 'org-cycle-hook
'folded
)))))
6332 (defun org-global-cycle (&optional arg
)
6333 "Cycle the global visibility. For details see `org-cycle'.
6334 With \\[universal-argument] prefix arg, switch to startup visibility.
6335 With a numeric prefix, show all headlines up to that level."
6337 (let ((org-cycle-include-plain-lists
6338 (if (derived-mode-p 'org-mode
) org-cycle-include-plain-lists nil
)))
6342 (hide-sublevels arg
)
6343 (setq org-cycle-global-status
'contents
))
6345 (org-set-startup-visibility)
6346 (message "Startup visibility, plus VISIBILITY properties."))
6348 (org-cycle '(4))))))
6350 (defun org-set-startup-visibility ()
6351 "Set the visibility required by startup options and properties."
6353 ((eq org-startup-folded t
)
6355 ((eq org-startup-folded
'content
)
6356 (let ((this-command 'org-cycle
) (last-command 'org-cycle
))
6357 (org-cycle '(4)) (org-cycle '(4)))))
6358 (unless (eq org-startup-folded
'showeverything
)
6359 (if org-hide-block-startup
(org-hide-block-all))
6360 (org-set-visibility-according-to-property 'no-cleanup
)
6361 (org-cycle-hide-archived-subtrees 'all
)
6362 (org-cycle-hide-drawers 'all
)
6363 (org-cycle-show-empty-lines t
)))
6365 (defun org-set-visibility-according-to-property (&optional no-cleanup
)
6366 "Switch subtree visibilities according to :VISIBILITY: property."
6368 (let (org-show-entry-below state
)
6370 (goto-char (point-min))
6371 (while (re-search-forward
6372 "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
6374 (setq state
(match-string 1))
6376 (org-back-to-heading t
)
6380 ((equal state
'("fold" "folded"))
6382 ((equal state
"children")
6383 (org-show-hidden-entry)
6385 ((equal state
"content")
6388 (org-narrow-to-subtree)
6390 ((member state
'("all" "showall"))
6393 (org-cycle-hide-archived-subtrees 'all
)
6394 (org-cycle-hide-drawers 'all
)
6395 (org-cycle-show-empty-lines 'all
)))))
6397 ;; This function uses outline-regexp instead of the more fundamental
6398 ;; org-outline-regexp so that org-cycle-global works outside of Org
6399 ;; buffers, where outline-regexp is needed.
6400 (defun org-overview ()
6401 "Switch to overview mode, showing only top-level headlines.
6402 Really, this shows all headlines with level equal or greater than the level
6403 of the first headline in the buffer. This is important, because if the
6404 first headline is not level one, then (hide-sublevels 1) gives confusing
6407 (let ((level (save-excursion
6408 (goto-char (point-min))
6409 (if (re-search-forward (concat "^" outline-regexp
) nil t
)
6411 (goto-char (match-beginning 0))
6412 (funcall outline-level
))))))
6413 (and level
(hide-sublevels level
))))
6415 (defun org-content (&optional arg
)
6416 "Show all headlines in the buffer, like a table of contents.
6417 With numerical argument N, show content up to level N."
6420 ;; Visit all headings and show their offspring
6421 (and (integerp arg
) (org-overview))
6422 (goto-char (point-max))
6424 (while (and (progn (condition-case nil
6425 (outline-previous-visible-heading 1)
6426 (error (goto-char (point-min))))
6428 (looking-at org-outline-regexp
))
6430 (show-children (1- arg
))
6432 (if (bobp) (throw 'exit nil
))))))
6435 (defun org-optimize-window-after-visibility-change (state)
6436 "Adjust the window after a change in outline visibility.
6437 This function is the default value of the hook `org-cycle-hook'."
6438 (when (get-buffer-window (current-buffer))
6440 ((eq state
'content
) nil
)
6441 ((eq state
'all
) nil
)
6442 ((eq state
'folded
) nil
)
6443 ((eq state
'children
) (or (org-subtree-end-visible-p) (recenter 1)))
6444 ((eq state
'subtree
) (or (org-subtree-end-visible-p) (recenter 1))))))
6446 (defun org-remove-empty-overlays-at (pos)
6447 "Remove outline overlays that do not contain non-white stuff."
6450 (and (eq 'outline
(overlay-get o
'invisible
))
6451 (not (string-match "\\S-" (buffer-substring (overlay-start o
)
6453 (delete-overlay o
)))
6456 (defun org-clean-visibility-after-subtree-move ()
6457 "Fix visibility issues after moving a subtree."
6458 ;; First, find a reasonable region to look at:
6459 ;; Start two siblings above, end three below
6460 (let* ((beg (save-excursion
6461 (and (org-get-last-sibling)
6462 (org-get-last-sibling))
6464 (end (save-excursion
6465 (and (org-get-next-sibling)
6466 (org-get-next-sibling)
6467 (org-get-next-sibling))
6468 (if (org-at-heading-p)
6471 (level (looking-at "\\*+"))
6472 (re (if level
(concat "^" (regexp-quote (match-string 0)) " "))))
6475 (narrow-to-region beg end
)
6477 ;; Properly fold already folded siblings
6478 (goto-char (point-min))
6479 (while (re-search-forward re nil t
)
6480 (if (and (not (outline-invisible-p))
6482 (goto-char (point-at-eol)) (outline-invisible-p)))
6484 (org-cycle-show-empty-lines 'overview
)
6485 (org-cycle-hide-drawers 'overview
)))))
6487 (defun org-cycle-show-empty-lines (state)
6488 "Show empty lines above all visible headlines.
6489 The region to be covered depends on STATE when called through
6490 `org-cycle-hook'. Lisp program can use t for STATE to get the
6491 entire buffer covered. Note that an empty line is only shown if there
6492 are at least `org-cycle-separator-lines' empty lines before the headline."
6493 (when (not (= org-cycle-separator-lines
0))
6495 (let* ((n (abs org-cycle-separator-lines
))
6497 ((= n
1) "\\(\n[ \t]*\n\\*+\\) ")
6498 ((= n
2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
6499 (t (let ((ns (number-to-string (- n
2))))
6500 (concat "^\\(?:[ \t]*\n\\)\\{" ns
"," ns
"\\}"
6501 "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
6504 ((memq state
'(overview contents t
))
6505 (setq beg
(point-min) end
(point-max)))
6506 ((memq state
'(children folded
))
6507 (setq beg
(point) end
(progn (org-end-of-subtree t t
)
6508 (beginning-of-line 2)
6512 (while (re-search-forward re end t
)
6513 (unless (get-char-property (match-end 1) 'invisible
)
6514 (setq e
(match-end 1))
6515 (if (< org-cycle-separator-lines
0)
6516 (setq b
(save-excursion
6517 (goto-char (match-beginning 0))
6518 (org-back-over-empty-lines)
6520 (goto-char (max (point-min) (1- (point))))
6524 (setq b
(match-beginning 1)))
6525 (outline-flag-region b e nil
)))))))
6526 ;; Never hide empty lines at the end of the file.
6528 (goto-char (point-max))
6529 (outline-previous-heading)
6530 (outline-end-of-heading)
6531 (if (and (looking-at "[ \t\n]+")
6532 (= (match-end 0) (point-max)))
6533 (outline-flag-region (point) (match-end 0) nil
))))
6535 (defun org-show-empty-lines-in-parent ()
6536 "Move to the parent and re-show empty lines before visible headlines."
6538 (let ((context (if (org-up-heading-safe) 'children
'overview
)))
6539 (org-cycle-show-empty-lines context
))))
6541 (defun org-files-list ()
6542 "Return `org-agenda-files' list, plus all open org-mode files.
6543 This is useful for operations that need to scan all of a user's
6544 open and agenda-wise Org files."
6545 (let ((files (mapcar 'expand-file-name
(org-agenda-files))))
6546 (dolist (buf (buffer-list))
6547 (with-current-buffer buf
6548 (if (and (derived-mode-p 'org-mode
) (buffer-file-name))
6549 (let ((file (expand-file-name (buffer-file-name))))
6550 (unless (member file files
)
6551 (push file files
))))))
6554 (defsubst org-entry-beginning-position
()
6555 "Return the beginning position of the current entry."
6556 (save-excursion (outline-back-to-heading t
) (point)))
6558 (defsubst org-entry-end-position
()
6559 "Return the end position of the current entry."
6560 (save-excursion (outline-next-heading) (point)))
6562 (defun org-cycle-hide-drawers (state)
6563 "Re-hide all drawers after a visibility state change."
6564 (when (and (derived-mode-p 'org-mode
)
6565 (not (memq state
'(overview folded contents
))))
6567 (let* ((globalp (memq state
'(contents all
)))
6568 (beg (if globalp
(point-min) (point)))
6569 (end (if globalp
(point-max)
6570 (if (eq state
'children
)
6571 (save-excursion (outline-next-heading) (point))
6572 (org-end-of-subtree t
)))))
6574 (while (re-search-forward org-drawer-regexp end t
)
6575 (org-flag-drawer t
))))))
6577 (defun org-flag-drawer (flag)
6578 "When FLAG is non-nil, hide the drawer we are within.
6579 Otherwise make it visible."
6581 (beginning-of-line 1)
6582 (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
6583 (let ((b (match-end 0)))
6584 (if (re-search-forward
6586 (save-excursion (outline-next-heading) (point)) t
)
6587 (outline-flag-region b
(point-at-eol) flag
)
6588 (error ":END: line missing at position %s" b
))))))
6590 (defun org-subtree-end-visible-p ()
6591 "Is the end of the current subtree visible?"
6592 (pos-visible-in-window-p
6593 (save-excursion (org-end-of-subtree t
) (point))))
6595 (defun org-first-headline-recenter (&optional N
)
6596 "Move cursor to the first headline and recenter the headline.
6597 Optional argument N means put the headline into the Nth line of the window."
6598 (goto-char (point-min))
6599 (when (re-search-forward (concat "^\\(" org-outline-regexp
"\\)") nil t
)
6601 (recenter (prefix-numeric-value N
))))
6603 ;;; Saving and restoring visibility
6605 (defun org-outline-overlay-data (&optional use-markers
)
6606 "Return a list of the locations of all outline overlays.
6607 These are overlays with the `invisible' property value `outline'.
6608 The return value is a list of cons cells, with start and stop
6609 positions for each overlay.
6610 If USE-MARKERS is set, return the positions as markers."
6617 (when (eq (overlay-get o
'invisible
) 'outline
)
6618 (setq beg
(overlay-start o
)
6619 end
(overlay-end o
))
6620 (and beg end
(> end beg
)
6622 (cons (move-marker (make-marker) beg
)
6623 (move-marker (make-marker) end
))
6625 (overlays-in (point-min) (point-max))))))))
6627 (defun org-set-outline-overlay-data (data)
6628 "Create visibility overlays for all positions in DATA.
6629 DATA should have been made by `org-outline-overlay-data'."
6636 (outline-flag-region (car c
) (cdr c
) t
))
6639 ;;; Folding of blocks
6641 (defvar org-hide-block-overlays nil
6642 "Overlays hiding blocks.")
6643 (make-variable-buffer-local 'org-hide-block-overlays
)
6645 (defun org-block-map (function &optional start end
)
6646 "Call FUNCTION at the head of all source blocks in the current buffer.
6647 Optional arguments START and END can be used to limit the range."
6648 (let ((start (or start
(point-min)))
6649 (end (or end
(point-max))))
6652 (while (and (< (point) end
) (re-search-forward org-block-regexp end t
))
6655 (goto-char (match-beginning 0))
6656 (funcall function
)))))))
6658 (defun org-hide-block-toggle-all ()
6659 "Toggle the visibility of all blocks in the current buffer."
6660 (org-block-map #'org-hide-block-toggle
))
6662 (defun org-hide-block-all ()
6663 "Fold all blocks in the current buffer."
6665 (org-show-block-all)
6666 (org-block-map #'org-hide-block-toggle-maybe
))
6668 (defun org-show-block-all ()
6669 "Unfold all blocks in the current buffer."
6671 (mapc 'delete-overlay org-hide-block-overlays
)
6672 (setq org-hide-block-overlays nil
))
6674 (defun org-hide-block-toggle-maybe ()
6675 "Toggle visibility of block at point."
6677 (let ((case-fold-search t
))
6679 (beginning-of-line 1)
6680 (looking-at org-block-regexp
))
6681 (progn (org-hide-block-toggle)
6682 t
) ;; to signal that we took action
6683 nil
))) ;; to signal that we did not
6685 (defun org-hide-block-toggle (&optional force
)
6686 "Toggle the visibility of the current block."
6690 (if (re-search-forward org-block-regexp nil t
)
6691 (let ((start (- (match-beginning 4) 1)) ;; beginning of body
6692 (end (match-end 0)) ;; end of entire body
6694 (if (memq t
(mapcar (lambda (overlay)
6695 (eq (overlay-get overlay
'invisible
)
6697 (overlays-at start
)))
6698 (if (or (not force
) (eq force
'off
))
6700 (when (member ov org-hide-block-overlays
)
6701 (setq org-hide-block-overlays
6702 (delq ov org-hide-block-overlays
)))
6703 (when (eq (overlay-get ov
'invisible
)
6705 (delete-overlay ov
)))
6706 (overlays-at start
)))
6707 (setq ov
(make-overlay start end
))
6708 (overlay-put ov
'invisible
'org-hide-block
)
6709 ;; make the block accessible to isearch
6711 ov
'isearch-open-invisible
6713 (when (member ov org-hide-block-overlays
)
6714 (setq org-hide-block-overlays
6715 (delq ov org-hide-block-overlays
)))
6716 (when (eq (overlay-get ov
'invisible
)
6718 (delete-overlay ov
))))
6719 (push ov org-hide-block-overlays
)))
6720 (error "Not looking at a source block"))))
6722 ;; org-tab-after-check-for-cycling-hook
6723 (add-hook 'org-tab-first-hook
'org-hide-block-toggle-maybe
)
6724 ;; Remove overlays when changing major mode
6725 (add-hook 'org-mode-hook
6726 (lambda () (org-add-hook 'change-major-mode-hook
6727 'org-show-block-all
'append
'local
)))
6731 (defvar org-goto-window-configuration nil
)
6732 (defvar org-goto-marker nil
)
6733 (defvar org-goto-map
6734 (let ((map (make-sparse-keymap)))
6735 (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur
)) cmd
)
6736 (while (setq cmd
(pop cmds
))
6737 (substitute-key-definition cmd cmd map global-map
)))
6738 (suppress-keymap map
)
6739 (org-defkey map
"\C-m" 'org-goto-ret
)
6740 (org-defkey map
[(return)] 'org-goto-ret
)
6741 (org-defkey map
[(left)] 'org-goto-left
)
6742 (org-defkey map
[(right)] 'org-goto-right
)
6743 (org-defkey map
[(control ?g
)] 'org-goto-quit
)
6744 (org-defkey map
"\C-i" 'org-cycle
)
6745 (org-defkey map
[(tab)] 'org-cycle
)
6746 (org-defkey map
[(down)] 'outline-next-visible-heading
)
6747 (org-defkey map
[(up)] 'outline-previous-visible-heading
)
6748 (if org-goto-auto-isearch
6749 (if (fboundp 'define-key-after
)
6750 (define-key-after map
[t] 'org-goto-local-auto-isearch)
6752 (org-defkey map "q" 'org-goto-quit)
6753 (org-defkey map "n" 'outline-next-visible-heading)
6754 (org-defkey map "p" 'outline-previous-visible-heading)
6755 (org-defkey map "f" 'outline-forward-same-level)
6756 (org-defkey map "b" 'outline-backward-same-level)
6757 (org-defkey map "u" 'outline-up-heading))
6758 (org-defkey map "/" 'org-occur)
6759 (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
6760 (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
6761 (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
6762 (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
6763 (org-defkey map "\C-c\C-u" 'outline-up-heading)
6766 (defconst org-goto-help
6767 "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
6768 RET=jump to location [Q]uit and return to previous location
6769 \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
6771 (defvar org-goto-start-pos) ; dynamically scoped parameter
6773 ;; FIXME: Docstring does not mention both interfaces
6774 (defun org-goto (&optional alternative-interface)
6775 "Look up a different location in the current file, keeping current visibility.
6777 When you want look-up or go to a different location in a
6778 document, the fastest way is often to fold the entire buffer and
6779 then dive into the tree. This method has the disadvantage, that
6780 the previous location will be folded, which may not be what you
6783 This command works around this by showing a copy of the current
6784 buffer in an indirect buffer, in overview mode. You can dive
6785 into the tree in that copy, use org-occur and incremental search
6786 to find a location. When pressing RET or `Q', the command
6787 returns to the original buffer in which the visibility is still
6788 unchanged. After RET it will also jump to the location selected
6789 in the indirect buffer and expose the headline hierarchy above.
6791 With a prefix argument, use the alternative interface: e.g. if
6792 `org-goto-interface' is 'outline use 'outline-path-completion."
6794 (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
6795 (org-refile-use-outline-path t)
6796 (org-refile-target-verify-function nil)
6798 (if (not alternative-interface)
6800 (if (eq org-goto-interface 'outline)
6801 'outline-path-completion
6803 (org-goto-start-pos (point))
6805 (if (eq interface 'outline)
6806 (car (org-get-location (current-buffer) org-goto-help))
6807 (let ((pa (org-refile-get-location "Goto" nil nil t)))
6808 (org-refile-check-position pa)
6812 (org-mark-ring-push org-goto-start-pos)
6813 (goto-char selected-point)
6814 (if (or (outline-invisible-p) (org-invisible-p2))
6815 (org-show-context 'org-goto)))
6818 (defvar org-goto-selected-point nil) ; dynamically scoped parameter
6819 (defvar org-goto-exit-command nil) ; dynamically scoped parameter
6820 (defvar org-goto-local-auto-isearch-map) ; defined below
6822 (defun org-get-location (buf help)
6823 "Let the user select a location in the Org-mode buffer BUF.
6824 This function uses a recursive edit. It returns the selected position
6826 (let ((isearch-mode-map org-goto-local-auto-isearch-map)
6827 (isearch-hide-immediately nil)
6828 (isearch-search-fun-function
6829 (lambda () 'org-goto-local-search-headings))
6830 (org-goto-selected-point org-goto-exit-command)
6832 (special-display-buffer-names nil)
6833 (special-display-regexps nil)
6834 (special-display-function nil))
6836 (save-window-excursion
6837 (delete-other-windows)
6838 (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
6839 (org-pop-to-buffer-same-window
6841 (make-indirect-buffer (current-buffer) "*org-goto*")
6842 (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
6843 (with-output-to-temp-buffer "*Help*"
6845 (org-fit-window-to-buffer (get-buffer-window "*Help*"))
6846 (setq buffer-read-only nil)
6847 (let ((org-startup-truncated t)
6848 (org-startup-folded nil)
6849 (org-startup-align-all-tables nil))
6852 (setq buffer-read-only t)
6853 (if (and (boundp 'org-goto-start-pos)
6854 (integer-or-marker-p org-goto-start-pos))
6855 (let ((org-show-hierarchy-above t)
6856 (org-show-siblings t)
6857 (org-show-following-heading t))
6858 (goto-char org-goto-start-pos)
6859 (and (outline-invisible-p) (org-show-context)))
6860 (goto-char (point-min)))
6861 (let (org-special-ctrl-a/e) (org-beginning-of-line))
6862 (message "Select location and press RET")
6863 (use-local-map org-goto-map)
6866 (kill-buffer "*org-goto*")
6867 (cons org-goto-selected-point org-goto-exit-command)))
6869 (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
6870 (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
6871 (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
6872 (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
6874 (defun org-goto-local-search-headings (string bound noerror)
6875 "Search and make sure that any matches are in headlines."
6877 (while (if isearch-forward
6878 (search-forward string bound noerror)
6879 (search-backward string bound noerror))
6880 (when (let ((context (mapcar 'car (save-match-data (org-context)))))
6881 (and (member :headline context)
6882 (not (member :tags context))))
6883 (throw 'return (point))))))
6885 (defun org-goto-local-auto-isearch ()
6888 (goto-char (point-min))
6889 (let ((keys (this-command-keys)))
6890 (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
6892 (isearch-process-search-char (string-to-char keys)))))
6894 (defun org-goto-ret (&optional arg)
6895 "Finish `org-goto' by going to the new location."
6897 (setq org-goto-selected-point (point)
6898 org-goto-exit-command 'return)
6901 (defun org-goto-left ()
6902 "Finish `org-goto' by going to the new location."
6904 (if (org-at-heading-p)
6906 (beginning-of-line 1)
6907 (setq org-goto-selected-point (point)
6908 org-goto-exit-command 'left)
6910 (error "Not on a heading")))
6912 (defun org-goto-right ()
6913 "Finish `org-goto' by going to the new location."
6915 (if (org-at-heading-p)
6917 (setq org-goto-selected-point (point)
6918 org-goto-exit-command 'right)
6920 (error "Not on a heading")))
6922 (defun org-goto-quit ()
6923 "Finish `org-goto' without cursor motion."
6925 (setq org-goto-selected-point nil)
6926 (setq org-goto-exit-command 'quit)
6929 ;;; Indirect buffer display of subtrees
6931 (defvar org-indirect-dedicated-frame nil
6932 "This is the frame being used for indirect tree display.")
6933 (defvar org-last-indirect-buffer nil)
6935 (defun org-tree-to-indirect-buffer (&optional arg)
6936 "Create indirect buffer and narrow it to current subtree.
6937 With a numerical prefix ARG, go up to this level and then take that tree.
6938 If ARG is negative, go up that many levels.
6940 If `org-indirect-buffer-display' is not `new-frame', the command removes the
6941 indirect buffer previously made with this command, to avoid proliferation of
6942 indirect buffers. However, when you call the command with a \
6943 \\[universal-argument] prefix, or
6944 when `org-indirect-buffer-display' is `new-frame', the last buffer
6945 is kept so that you can work with several indirect buffers at the same time.
6946 If `org-indirect-buffer-display' is `dedicated-frame', the \
6947 \\[universal-argument] prefix also
6948 requests that a new frame be made for the new buffer, so that the dedicated
6949 frame is not changed."
6951 (let ((cbuf (current-buffer))
6952 (cwin (selected-window))
6954 beg end level heading ibuf)
6956 (org-back-to-heading t)
6958 (setq level (org-outline-level))
6959 (if (< arg 0) (setq arg (+ level arg)))
6960 (while (> (setq level (org-outline-level)) arg)
6961 (org-up-heading-safe)))
6963 heading (org-get-heading))
6964 (org-end-of-subtree t t)
6965 (if (org-at-heading-p) (backward-char 1))
6967 (if (and (buffer-live-p org-last-indirect-buffer)
6968 (not (eq org-indirect-buffer-display 'new-frame))
6970 (kill-buffer org-last-indirect-buffer))
6971 (setq ibuf (org-get-indirect-buffer cbuf)
6972 org-last-indirect-buffer ibuf)
6974 ((or (eq org-indirect-buffer-display 'new-frame)
6975 (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
6976 (select-frame (make-frame))
6977 (delete-other-windows)
6978 (org-pop-to-buffer-same-window ibuf)
6979 (org-set-frame-title heading))
6980 ((eq org-indirect-buffer-display 'dedicated-frame)
6982 (select-frame (or (and org-indirect-dedicated-frame
6983 (frame-live-p org-indirect-dedicated-frame)
6984 org-indirect-dedicated-frame)
6985 (setq org-indirect-dedicated-frame (make-frame)))))
6986 (delete-other-windows)
6987 (org-pop-to-buffer-same-window ibuf)
6988 (org-set-frame-title (concat "Indirect: " heading)))
6989 ((eq org-indirect-buffer-display 'current-window)
6990 (org-pop-to-buffer-same-window ibuf))
6991 ((eq org-indirect-buffer-display 'other-window)
6992 (pop-to-buffer ibuf))
6993 (t (error "Invalid value")))
6994 (if (featurep 'xemacs)
6995 (save-excursion (org-mode) (turn-on-font-lock)))
6996 (narrow-to-region beg end)
6999 (run-hook-with-args 'org-cycle-hook 'all)
7000 (and (window-live-p cwin) (select-window cwin))))
7002 (defun org-get-indirect-buffer (&optional buffer)
7003 (setq buffer (or buffer (current-buffer)))
7004 (let ((n 1) (base (buffer-name buffer)) bname)
7005 (while (buffer-live-p
7006 (get-buffer (setq bname (concat base "-" (number-to-string n)))))
7009 (make-indirect-buffer buffer bname 'clone)
7010 (error (make-indirect-buffer buffer bname)))))
7012 (defun org-set-frame-title (title)
7013 "Set the title of the current frame to the string TITLE."
7014 ;; FIXME: how to name a single frame in XEmacs???
7015 (unless (featurep 'xemacs)
7016 (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
7018 ;;;; Structure editing
7020 ;;; Inserting headlines
7022 (defun org-previous-line-empty-p ()
7025 (or (beginning-of-line 0) t)
7027 (looking-at "[ \t]*$")))))
7029 (defun org-insert-heading (&optional force-heading invisible-ok)
7030 "Insert a new heading or item with same depth at point.
7031 If point is in a plain list and FORCE-HEADING is nil, create a new list item.
7032 If point is at the beginning of a headline, insert a sibling before the
7033 current headline. If point is not at the beginning, split the line,
7034 create the new headline with the text in the current line after point
7035 \(but see also the variable `org-M-RET-may-split-line').
7037 When INVISIBLE-OK is set, stop at invisible headlines when going back.
7038 This is important for non-interactive uses of the command."
7040 (if (or (= (buffer-size) 0)
7041 (and (not (save-excursion
7042 (and (ignore-errors (org-back-to-heading invisible-ok))
7043 (org-at-heading-p))))
7044 (or force-heading (not (org-in-item-p)))))
7047 (run-hooks 'org-insert-heading-hook))
7048 (when (or force-heading (not (org-insert-item)))
7049 (let* ((empty-line-p nil)
7051 (on-heading (org-at-heading-p))
7052 (head (save-excursion
7055 (org-back-to-heading invisible-ok)
7056 (when (and (not on-heading)
7057 (featurep 'org-inlinetask)
7058 (integerp org-inlinetask-min-level)
7059 (>= (length (match-string 0))
7060 org-inlinetask-min-level))
7061 ;; Find a heading level before the inline task
7062 (while (and (setq level (org-up-heading-safe))
7063 (>= level org-inlinetask-min-level)))
7064 (if (org-at-heading-p)
7065 (org-back-to-heading invisible-ok)
7066 (error "This should not happen")))
7067 (setq empty-line-p (org-previous-line-empty-p))
7070 (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
7071 (blank (if (eq blank-a 'auto) empty-line-p blank-a))
7072 pos hide-previous previous-pos)
7074 ((and (org-at-heading-p) (bolp)
7076 (save-excursion (backward-char 1) (not (outline-invisible-p)))))
7077 ;; insert before the current line
7078 (open-line (if blank 2 1)))
7080 (not org-insert-heading-respect-content)
7083 (backward-char 1) (not (outline-invisible-p)))))
7084 ;; insert right here
7087 ;; somewhere in the line
7089 (setq previous-pos (point-at-bol))
7091 (setq hide-previous (outline-invisible-p)))
7092 (and org-insert-heading-respect-content (org-show-subtree))
7094 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
7097 (goto-char (point-at-bol))
7098 (and (looking-at org-complex-heading-regexp)
7100 (> p (match-beginning 4)))))))
7103 (org-insert-heading-respect-content
7104 (org-end-of-subtree nil t)
7105 (when (featurep 'org-inlinetask)
7106 (while (and (not (eobp))
7107 (looking-at "\\(\\*+\\)[ \t]+")
7108 (>= (length (match-string 1))
7109 org-inlinetask-min-level))
7110 (org-end-of-subtree nil t)))
7111 (or (bolp) (newline))
7112 (or (org-previous-line-empty-p)
7113 (and blank (newline)))
7119 (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
7120 (setq tags (and (match-end 2) (match-string 2)))
7122 (delete-region (match-beginning 1) (match-end 1)))
7123 (setq pos (point-at-bol))
7124 (or split (end-of-line 1))
7125 (delete-horizontal-space)
7126 (if (string-match "\\`\\*+\\'"
7127 (buffer-substring (point-at-bol) (point)))
7129 (newline (if blank 2 1))
7135 (org-set-tags nil 'align))))
7137 (or split (end-of-line 1))
7138 (newline (if blank 2 1)))))))
7139 (insert head) (just-one-space)
7142 (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
7143 (when (and org-insert-heading-respect-content hide-previous)
7145 (goto-char previous-pos)
7147 (run-hooks 'org-insert-heading-hook)))))
7149 (defun org-get-heading (&optional no-tags no-todo)
7150 "Return the heading of the current entry, without the stars.
7151 When NO-TAGS is non-nil, don't include tags.
7152 When NO-TODO is non-nil, don't include TODO keywords."
7154 (org-back-to-heading t)
7156 ((and no-tags no-todo)
7157 (looking-at org-complex-heading-regexp)
7160 (looking-at (concat org-outline-regexp
7162 "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
7165 (looking-at org-todo-line-regexp)
7167 (t (looking-at org-heading-regexp)
7168 (match-string 2)))))
7170 (defun org-heading-components ()
7171 "Return the components of the current heading.
7172 This is a list with the following elements:
7173 - the level as an integer
7174 - the reduced level, different if `org-odd-levels-only' is set.
7175 - the TODO keyword, or nil
7176 - the priority character, like ?A, or nil if no priority is given
7177 - the headline text itself, or the tags string if no headline text
7178 - the tags string, or nil."
7180 (org-back-to-heading t)
7181 (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
7182 (list (length (match-string 1))
7183 (org-reduced-level (length (match-string 1)))
7184 (org-match-string-no-properties 2)
7185 (and (match-end 3) (aref (match-string 3) 2))
7186 (org-match-string-no-properties 4)
7187 (org-match-string-no-properties 5)))))
7189 (defun org-get-entry ()
7190 "Get the entry text, after heading, entire subtree."
7192 (org-back-to-heading t)
7193 (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
7195 (defun org-insert-heading-after-current ()
7196 "Insert a new heading with same level as current, after current subtree."
7198 (org-back-to-heading)
7199 (org-insert-heading)
7200 (org-move-subtree-down)
7203 (defun org-insert-heading-respect-content ()
7205 (let ((org-insert-heading-respect-content t))
7206 (org-insert-heading t)))
7208 (defun org-insert-todo-heading-respect-content (&optional force-state)
7210 (let ((org-insert-heading-respect-content t))
7211 (org-insert-todo-heading force-state t)))
7213 (defun org-insert-todo-heading (arg &optional force-heading)
7214 "Insert a new heading with the same level and TODO state as current heading.
7215 If the heading has no TODO state, or if the state is DONE, use the first
7216 state (TODO by default). Also with prefix arg, force first state."
7218 (when (or force-heading (not (org-insert-item 'checkbox)))
7219 (org-insert-heading force-heading)
7221 (org-back-to-heading)
7222 (outline-previous-heading)
7223 (looking-at org-todo-line-regexp))
7227 (not (match-beginning 2))
7228 (member (match-string 2) org-done-keywords))
7229 (car org-todo-keywords-1)
7233 (run-hook-with-args-until-success
7234 'org-todo-get-default-hook new-mark-x nil)
7236 (beginning-of-line 1)
7237 (and (looking-at org-outline-regexp) (goto-char (match-end 0))
7238 (if org-treat-insert-todo-heading-as-state-change
7240 (insert new-mark " "))))
7241 (when org-provide-todo-statistics
7242 (org-update-parent-todo-statistics))))
7244 (defun org-insert-subheading (arg)
7245 "Insert a new subheading and demote it.
7246 Works for outline headings and for plain lists alike."
7248 (org-insert-heading arg)
7250 ((org-at-heading-p) (org-do-demote))
7251 ((org-at-item-p) (org-indent-item))))
7253 (defun org-insert-todo-subheading (arg)
7254 "Insert a new subheading with TODO keyword or checkbox and demote it.
7255 Works for outline headings and for plain lists alike."
7257 (org-insert-todo-heading arg)
7259 ((org-at-heading-p) (org-do-demote))
7260 ((org-at-item-p) (org-indent-item))))
7262 ;;; Promotion and Demotion
7264 (defvar org-after-demote-entry-hook nil
7265 "Hook run after an entry has been demoted.
7266 The cursor will be at the beginning of the entry.
7267 When a subtree is being demoted, the hook will be called for each node.")
7269 (defvar org-after-promote-entry-hook nil
7270 "Hook run after an entry has been promoted.
7271 The cursor will be at the beginning of the entry.
7272 When a subtree is being promoted, the hook will be called for each node.")
7274 (defun org-promote-subtree ()
7275 "Promote the entire subtree.
7276 See also `org-promote'."
7279 (org-with-limited-levels (org-map-tree 'org-promote)))
7280 (org-fix-position-after-promote))
7282 (defun org-demote-subtree ()
7283 "Demote the entire subtree. See `org-demote'.
7284 See also `org-promote'."
7287 (org-with-limited-levels (org-map-tree 'org-demote)))
7288 (org-fix-position-after-promote))
7291 (defun org-do-promote ()
7292 "Promote the current heading higher up the tree.
7293 If the region is active in `transient-mark-mode', promote all headings
7297 (if (org-region-active-p)
7298 (org-map-region 'org-promote (region-beginning) (region-end))
7300 (org-fix-position-after-promote))
7302 (defun org-do-demote ()
7303 "Demote the current heading lower down the tree.
7304 If the region is active in `transient-mark-mode', demote all headings
7308 (if (org-region-active-p)
7309 (org-map-region 'org-demote (region-beginning) (region-end))
7311 (org-fix-position-after-promote))
7313 (defun org-fix-position-after-promote ()
7314 "Make sure that after pro/demotion cursor position is right."
7315 (let ((pos (point)))
7316 (when (save-excursion
7317 (beginning-of-line 1)
7318 (looking-at org-todo-line-regexp)
7319 (or (equal pos (match-end 1)) (equal pos (match-end 2))))
7320 (cond ((eobp) (insert " "))
7321 ((eolp) (insert " "))
7322 ((equal (char-after) ?\ ) (forward-char 1))))))
7324 (defun org-current-level ()
7325 "Return the level of the current entry, or nil if before the first headline.
7326 The level is the number of stars at the beginning of the headline."
7328 (org-with-limited-levels
7329 (if (ignore-errors (org-back-to-heading t))
7330 (funcall outline-level)))))
7332 (defun org-get-previous-line-level ()
7333 "Return the outline depth of the last headline before the current line.
7334 Returns 0 for the first headline in the buffer, and nil if before the
7336 (let ((current-level (org-current-level))
7337 (prev-level (when (> (line-number-at-pos) 1)
7339 (beginning-of-line 0)
7340 (org-current-level)))))
7341 (cond ((null current-level) nil) ; Before first headline
7342 ((null prev-level) 0) ; At first headline
7345 (defun org-reduced-level (l)
7346 "Compute the effective level of a heading.
7347 This takes into account the setting of `org-odd-levels-only'."
7350 (org-odd-levels-only (1+ (floor (/ l 2))))
7353 (defun org-level-increment ()
7354 "Return the number of stars that will be added or removed at a
7355 time to headlines when structure editing, based on the value of
7356 `org-odd-levels-only'."
7357 (if org-odd-levels-only 2 1))
7359 (defun org-get-valid-level (level &optional change)
7360 "Rectify a level change under the influence of `org-odd-levels-only'
7361 LEVEL is a current level, CHANGE is by how much the level should be
7362 modified. Even if CHANGE is nil, LEVEL may be returned modified because
7363 even level numbers will become the next higher odd number."
7364 (if org-odd-levels-only
7365 (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
7366 ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
7367 ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
7368 (max 1 (+ level (or change 0)))))
7370 (if (boundp 'define-obsolete-function-alias)
7371 (if (or (featurep 'xemacs) (< emacs-major-version 23))
7372 (define-obsolete-function-alias 'org-get-legal-level
7373 'org-get-valid-level)
7374 (define-obsolete-function-alias 'org-get-legal-level
7375 'org-get-valid-level "23.1")))
7377 (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
7378 ;; ̀org-with-limited-levels'
7379 (defun org-promote ()
7380 "Promote the current heading higher up the tree.
7381 If the region is active in `transient-mark-mode', promote all headings
7383 (org-back-to-heading t)
7384 (let* ((level (save-match-data (funcall outline-level)))
7385 (after-change-functions (remove 'flyspell-after-change-function
7386 after-change-functions))
7387 (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
7388 (diff (abs (- level (length up-head) -1))))
7389 (cond ((and (= level 1) org-called-with-limited-levels
7390 org-allow-promoting-top-level-subtree)
7391 (replace-match "# " nil t))
7393 (error "Cannot promote to level 0. UNDO to recover if necessary"))
7394 (t (replace-match up-head nil t)))
7395 ;; Fixup tag positioning
7397 (and org-auto-align-tags (org-set-tags nil t))
7398 (if org-adapt-indentation (org-fixup-indentation (- diff))))
7399 (run-hooks 'org-after-promote-entry-hook)))
7401 (defun org-demote ()
7402 "Demote the current heading lower down the tree.
7403 If the region is active in `transient-mark-mode', demote all headings
7405 (org-back-to-heading t)
7406 (let* ((level (save-match-data (funcall outline-level)))
7407 (after-change-functions (remove 'flyspell-after-change-function
7408 after-change-functions))
7409 (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
7410 (diff (abs (- level (length down-head) -1))))
7411 (replace-match down-head nil t)
7412 ;; Fixup tag positioning
7413 (and org-auto-align-tags (org-set-tags nil t))
7414 (if org-adapt-indentation (org-fixup-indentation diff))
7415 (run-hooks 'org-after-demote-entry-hook)))
7417 (defun org-cycle-level ()
7418 "Cycle the level of an empty headline through possible states.
7419 This goes first to child, then to parent, level, then up the hierarchy.
7420 After top level, it switches back to sibling level."
7422 (let ((org-adapt-indentation nil))
7423 (when (org-point-at-end-of-empty-headline)
7424 (setq this-command 'org-cycle-level) ; Only needed for caching
7425 (let ((cur-level (org-current-level))
7426 (prev-level (org-get-previous-line-level)))
7428 ;; If first headline in file, promote to top-level.
7430 (loop repeat (/ (- cur-level 1) (org-level-increment))
7431 do (org-do-promote)))
7432 ;; If same level as prev, demote one.
7433 ((= prev-level cur-level)
7435 ;; If parent is top-level, promote to top level if not already.
7437 (loop repeat (/ (- cur-level 1) (org-level-increment))
7438 do (org-do-promote)))
7439 ;; If top-level, return to prev-level.
7441 (loop repeat (/ (- prev-level 1) (org-level-increment))
7442 do (org-do-demote)))
7443 ;; If less than prev-level, promote one.
7444 ((< cur-level prev-level)
7446 ;; If deeper than prev-level, promote until higher than
7448 ((> cur-level prev-level)
7449 (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
7450 do (org-do-promote))))
7453 (defun org-map-tree (fun)
7454 "Call FUN for every heading underneath the current one."
7455 (org-back-to-heading)
7456 (let ((level (funcall outline-level)))
7460 (outline-next-heading)
7461 (> (funcall outline-level) level))
7465 (defun org-map-region (fun beg end)
7466 "Call FUN for every heading between BEG and END."
7467 (let ((org-ignore-region t))
7469 (setq end (copy-marker end))
7471 (if (and (re-search-forward org-outline-regexp-bol nil t)
7475 (outline-next-heading)
7480 (defvar org-property-end-re) ; silence byte-compiler
7481 (defun org-fixup-indentation (diff)
7482 "Change the indentation in the current entry by DIFF.
7483 However, if any line in the current entry has no indentation, or if it
7484 would end up with no indentation after the change, nothing at all is done."
7486 (let ((end (save-excursion (outline-next-heading)
7488 (prohibit (if (> diff 0)
7490 (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
7492 (unless (save-excursion (end-of-line 1)
7493 (re-search-forward prohibit end t))
7494 (while (and (< (point) end)
7495 (re-search-forward "^[ \t]+" end t))
7496 (goto-char (match-end 0))
7497 (setq col (current-column))
7498 (if (< diff 0) (replace-match ""))
7499 (org-indent-to-column (+ diff col))))
7500 (move-marker end nil))))
7502 (defun org-convert-to-odd-levels ()
7503 "Convert an org-mode file with all levels allowed to one with odd levels.
7504 This will leave level 1 alone, convert level 2 to level 3, level 3 to
7507 (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
7508 (let ((outline-level 'org-outline-level)
7509 (org-odd-levels-only nil) n)
7511 (goto-char (point-min))
7512 (while (re-search-forward "^\\*\\*+ " nil t)
7513 (setq n (- (length (match-string 0)) 2))
7514 (while (>= (setq n (1- n)) 0)
7516 (end-of-line 1))))))
7518 (defun org-convert-to-oddeven-levels ()
7519 "Convert an org-mode file with only odd levels to one with odd/even levels.
7520 This promotes level 3 to level 2, level 5 to level 3 etc. If the
7521 file contains a section with an even level, conversion would
7522 destroy the structure of the file. An error is signaled in this
7525 (goto-char (point-min))
7526 ;; First check if there are no even levels
7527 (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
7528 (org-show-context t)
7529 (error "Not all levels are odd in this file. Conversion not possible"))
7530 (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
7531 (let ((outline-regexp org-outline-regexp)
7532 (outline-level 'org-outline-level)
7533 (org-odd-levels-only nil) n)
7535 (goto-char (point-min))
7536 (while (re-search-forward "^\\*\\*+ " nil t)
7537 (setq n (/ (1- (length (match-string 0))) 2))
7538 (while (>= (setq n (1- n)) 0)
7540 (end-of-line 1))))))
7542 (defun org-tr-level (n)
7543 "Make N odd if required."
7544 (if org-odd-levels-only (1+ (/ n 2)) n))
7546 ;;; Vertical tree motion, cutting and pasting of subtrees
7548 (defun org-move-subtree-up (&optional arg)
7549 "Move the current subtree up past ARG headlines of the same level."
7551 (org-move-subtree-down (- (prefix-numeric-value arg))))
7553 (defun org-move-subtree-down (&optional arg)
7554 "Move the current subtree down past ARG headlines of the same level."
7556 (setq arg (prefix-numeric-value arg))
7557 (let ((movfunc (if (> arg 0) 'org-get-next-sibling
7558 'org-get-last-sibling))
7559 (ins-point (make-marker))
7561 (col (current-column))
7562 beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
7564 (org-back-to-heading)
7567 (setq ne-beg (org-back-over-empty-lines))
7570 (save-excursion (outline-end-of-heading)
7571 (setq folded (outline-invisible-p)))
7572 (outline-end-of-subtree))
7573 (outline-next-heading)
7574 (setq ne-end (org-back-over-empty-lines))
7577 (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
7578 ;; include less whitespace
7581 (forward-line (- ne-beg ne-end))
7582 (setq beg (point))))
7583 ;; Find insertion point, with error handling
7585 (or (and (funcall movfunc) (looking-at org-outline-regexp))
7586 (progn (goto-char beg0)
7587 (error "Cannot move past superior level or buffer limit")))
7588 (setq cnt (1- cnt)))
7590 ;; Moving forward - still need to move over subtree
7591 (progn (org-end-of-subtree t t)
7593 (org-back-over-empty-lines)
7594 (or (bolp) (newline)))))
7595 (setq ne-ins (org-back-over-empty-lines))
7596 (move-marker ins-point (point))
7597 (setq txt (buffer-substring beg end))
7598 (org-save-markers-in-region beg end)
7599 (delete-region beg end)
7600 (org-remove-empty-overlays-at beg)
7601 (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
7602 (or (bobp) (outline-flag-region (1- (point)) (point) nil))
7603 (and (not (bolp)) (looking-at "\n") (forward-char 1))
7604 (let ((bbb (point)))
7605 (insert-before-markers txt)
7606 (org-reinstall-markers-in-region bbb)
7607 (move-marker ins-point bbb))
7608 (or (bolp) (insert "\n"))
7609 (setq ins-end (point))
7610 (goto-char ins-point)
7611 (org-skip-whitespace)
7612 (when (and (< arg 0)
7613 (org-first-sibling-p)
7615 ;; Move whitespace back to beginning
7618 (let ((kill-whole-line t))
7619 (kill-line (- ne-ins ne-beg)) (point)))
7620 (insert (make-string (- ne-ins ne-beg) ?\n)))
7621 (move-marker ins-point nil)
7626 (org-cycle-hide-drawers 'children))
7627 (org-clean-visibility-after-subtree-move)
7628 ;; move back to the initial column we were at
7629 (move-to-column col)))
7631 (defvar org-subtree-clip ""
7632 "Clipboard for cut and paste of subtrees.
7633 This is actually only a copy of the kill, because we use the normal kill
7634 ring. We need it to check if the kill was created by `org-copy-subtree'.")
7636 (defvar org-subtree-clip-folded nil
7637 "Was the last copied subtree folded?
7638 This is used to fold the tree back after pasting.")
7640 (defun org-cut-subtree (&optional n)
7641 "Cut the current subtree into the clipboard.
7642 With prefix arg N, cut this many sequential subtrees.
7643 This is a short-hand for marking the subtree and then cutting it."
7645 (org-copy-subtree n 'cut))
7647 (defun org-copy-subtree (&optional n cut force-store-markers)
7648 "Cut the current subtree into the clipboard.
7649 With prefix arg N, cut this many sequential subtrees.
7650 This is a short-hand for marking the subtree and then copying it.
7651 If CUT is non-nil, actually cut the subtree.
7652 If FORCE-STORE-MARKERS is non-nil, store the relative locations
7653 of some markers in the region, even if CUT is non-nil. This is
7654 useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
7656 (let (beg end folded (beg0 (point)))
7657 (if (org-called-interactively-p 'any)
7658 (org-back-to-heading nil) ; take what looks like a subtree
7659 (org-back-to-heading t)) ; take what is really there
7660 (org-back-over-empty-lines)
7662 (skip-chars-forward " \t\r\n")
7664 (save-excursion (outline-end-of-heading)
7665 (setq folded (outline-invisible-p)))
7667 (org-forward-heading-same-level (1- n) t)
7669 (org-end-of-subtree t t))
7670 (org-back-over-empty-lines)
7674 (setq org-subtree-clip-folded folded)
7675 (when (or cut force-store-markers)
7676 (org-save-markers-in-region beg end))
7677 (if cut (kill-region beg end) (copy-region-as-kill beg end))
7678 (setq org-subtree-clip (current-kill 0))
7679 (message "%s: Subtree(s) with %d characters"
7680 (if cut "Cut" "Copied")
7681 (length org-subtree-clip)))))
7683 (defun org-paste-subtree (&optional level tree for-yank)
7684 "Paste the clipboard as a subtree, with modification of headline level.
7685 The entire subtree is promoted or demoted in order to match a new headline
7688 If the cursor is at the beginning of a headline, the same level as
7689 that headline is used to paste the tree
7691 If not, the new level is derived from the *visible* headings
7692 before and after the insertion point, and taken to be the inferior headline
7693 level of the two. So if the previous visible heading is level 3 and the
7694 next is level 4 (or vice versa), level 4 will be used for insertion.
7695 This makes sure that the subtree remains an independent subtree and does
7696 not swallow low level entries.
7698 You can also force a different level, either by using a numeric prefix
7699 argument, or by inserting the heading marker by hand. For example, if the
7700 cursor is after \"*****\", then the tree will be shifted to level 5.
7702 If optional TREE is given, use this text instead of the kill ring.
7704 When FOR-YANK is set, this is called by `org-yank'. In this case, do not
7705 move back over whitespace before inserting, and move point to the end of
7706 the inserted text when done."
7708 (setq tree (or tree (and kill-ring (current-kill 0))))
7709 (unless (org-kill-is-subtree-p tree)
7711 (substitute-command-keys
7712 "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
7713 (org-with-limited-levels
7714 (let* ((visp (not (outline-invisible-p)))
7716 (^re_ "\\(\\*+\\)[ \t]*")
7717 (old-level (if (string-match org-outline-regexp-bol txt)
7718 (- (match-end 0) (match-beginning 0) 1)
7720 (force-level (cond (level (prefix-numeric-value level))
7721 ((and (looking-at "[ \t]*$")
7723 "^\\*+$" (buffer-substring
7724 (point-at-bol) (point))))
7725 (- (match-end 1) (match-beginning 1)))
7727 (looking-at org-outline-regexp))
7728 (- (match-end 0) (point) 1))))
7729 (previous-level (save-excursion
7732 (outline-previous-visible-heading 1)
7733 (if (looking-at ^re_)
7734 (- (match-end 0) (match-beginning 0) 1)
7737 (next-level (save-excursion
7740 (or (looking-at org-outline-regexp)
7741 (outline-next-visible-heading 1))
7742 (if (looking-at ^re_)
7743 (- (match-end 0) (match-beginning 0) 1)
7746 (new-level (or force-level (max previous-level next-level)))
7747 (shift (if (or (= old-level -1)
7749 (= old-level new-level))
7751 (- new-level old-level)))
7752 (delta (if (> shift 0) -1 1))
7753 (func (if (> shift 0) 'org-demote 'org-promote))
7754 (org-odd-levels-only nil)
7756 ;; Remove the forced level indicator
7758 (delete-region (point-at-bol) (point)))
7760 (beginning-of-line (if (bolp) 1 2))
7761 (unless for-yank (org-back-over-empty-lines))
7763 (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
7764 (insert-before-markers txt)
7765 (unless (string-match "\n\\'" txt) (insert "\n"))
7766 (setq newend (point))
7767 (org-reinstall-markers-in-region beg)
7770 (skip-chars-forward " \t\n\r")
7772 (if (and (outline-invisible-p) visp)
7773 (save-excursion (outline-show-heading)))
7774 ;; Shift if necessary
7777 (narrow-to-region beg end)
7778 (while (not (= shift 0))
7779 (org-map-region func (point-min) (point-max))
7780 (setq shift (+ delta shift)))
7781 (goto-char (point-min))
7782 (setq newend (point-max))))
7783 (when (or (org-called-interactively-p 'interactive) for-yank)
7784 (message "Clipboard pasted as level %d subtree" new-level))
7785 (if (and (not for-yank) ; in this case, org-yank will decide about folding
7787 (eq org-subtree-clip (current-kill 0))
7788 org-subtree-clip-folded)
7789 ;; The tree was folded before it was killed/copied
7791 (and for-yank (goto-char newend)))))
7793 (defun org-kill-is-subtree-p (&optional txt)
7794 "Check if the current kill is an outline subtree, or a set of trees.
7795 Returns nil if kill does not start with a headline, or if the first
7796 headline level is not the largest headline level in the tree.
7797 So this will actually accept several entries of equal levels as well,
7798 which is OK for `org-paste-subtree'.
7799 If optional TXT is given, check this string instead of the current kill."
7800 (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
7801 (re (org-get-limited-outline-regexp))
7802 (^re (concat "^" re))
7803 (start-level (and kill
7805 (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
7807 (- (match-end 2) (match-beginning 2) 1)))
7808 (start (1+ (or (match-beginning 2) -1))))
7809 (if (not start-level)
7811 nil) ;; does not even start with a heading
7813 (while (setq start (string-match ^re kill (1+ start)))
7814 (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
7818 (defvar org-markers-to-move nil
7819 "Markers that should be moved with a cut-and-paste operation.
7820 Those markers are stored together with their positions relative to
7821 the start of the region.")
7823 (defun org-save-markers-in-region (beg end)
7824 "Check markers in region.
7825 If these markers are between BEG and END, record their position relative
7826 to BEG, so that after moving the block of text, we can put the markers back
7828 This function gets called just before an entry or tree gets cut from the
7829 buffer. After re-insertion, `org-reinstall-markers-in-region' must be
7830 called immediately, to move the markers with the entries."
7831 (setq org-markers-to-move nil)
7832 (when (featurep 'org-clock)
7833 (org-clock-save-markers-for-cut-and-paste beg end))
7834 (when (featurep 'org-agenda)
7835 (org-agenda-save-markers-for-cut-and-paste beg end)))
7837 (defun org-check-and-save-marker (marker beg end)
7838 "Check if MARKER is between BEG and END.
7839 If yes, remember the marker and the distance to BEG."
7840 (when (and (marker-buffer marker)
7841 (equal (marker-buffer marker) (current-buffer)))
7842 (if (and (>= marker beg) (< marker end))
7843 (push (cons marker (- marker beg)) org-markers-to-move))))
7845 (defun org-reinstall-markers-in-region (beg)
7846 "Move all remembered markers to their position relative to BEG."
7848 (move-marker (car x) (+ beg (cdr x))))
7849 org-markers-to-move)
7850 (setq org-markers-to-move nil))
7852 (defun org-narrow-to-subtree ()
7853 "Narrow buffer to the current subtree."
7857 (org-with-limited-levels
7859 (progn (org-back-to-heading t) (point))
7860 (progn (org-end-of-subtree t t)
7861 (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
7864 (defun org-narrow-to-block ()
7865 "Narrow buffer to the current block."
7867 (let* ((case-fold-search t)
7868 (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
7869 "^[ \t]*#\\+end_.*")))
7871 (narrow-to-region (car blockp) (cdr blockp))
7872 (error "Not in a block"))))
7875 (defvar org-property-drawer-re))
7877 (defvar org-property-start-re) ;; defined below
7878 (defun org-clone-subtree-with-time-shift (n &optional shift)
7879 "Clone the task (subtree) at point N times.
7880 The clones will be inserted as siblings.
7882 In interactive use, the user will be prompted for the number of
7883 clones to be produced, and for a time SHIFT, which may be a
7884 repeater as used in time stamps, for example `+3d'.
7886 When a valid repeater is given and the entry contains any time
7887 stamps, the clones will become a sequence in time, with time
7888 stamps in the subtree shifted for each clone produced. If SHIFT
7889 is nil or the empty string, time stamps will be left alone. The
7890 ID property of the original subtree is removed.
7892 If the original subtree did contain time stamps with a repeater,
7893 the following will happen:
7894 - the repeater will be removed in each clone
7895 - an additional clone will be produced, with the current, unshifted
7896 date(s) in the entry.
7897 - the original entry will be placed *after* all the clones, with
7899 - the start days in the repeater in the original entry will be shifted
7900 to past the last clone.
7901 In this way you can spell out a number of instances of a repeating task,
7902 and still retain the repeater to cover future instances of the task."
7903 (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
7904 (let (beg end template task idprop
7905 shift-n shift-what doshift nmin nmax (n-no-remove -1)
7906 (drawer-re org-drawer-regexp))
7907 (if (not (and (integerp n) (> n 0)))
7908 (error "Invalid number of replications %s" n))
7909 (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
7910 (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
7912 (error "Invalid shift specification %s" shift))
7914 (setq shift-n (string-to-number (match-string 1 shift))
7915 shift-what (cdr (assoc (match-string 2 shift)
7916 '(("d" . day) ("w" . week)
7917 ("m" . month) ("y" . year))))))
7918 (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
7919 (setq nmin 1 nmax n)
7920 (org-back-to-heading t)
7922 (setq idprop (org-entry-get nil "ID"))
7923 (org-end-of-subtree t t)
7924 (or (bolp) (insert "\n"))
7926 (setq template (buffer-substring beg end))
7928 (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
7929 (delete-region beg end)
7931 (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
7933 (loop for n from nmin to nmax do
7938 (goto-char (point-min))
7940 (and idprop (if org-clone-delete-id
7941 (org-entry-delete nil "ID")
7942 (org-id-get-create t)))
7944 (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
7946 (goto-char (point-min))
7947 (while (re-search-forward drawer-re nil t)
7949 (org-remove-empty-drawer-at d (point))) org-drawers)))
7950 (goto-char (point-min))
7952 (while (re-search-forward org-ts-regexp-both nil t)
7953 (org-timestamp-change (* n shift-n) shift-what))
7954 (unless (= n n-no-remove)
7955 (goto-char (point-min))
7956 (while (re-search-forward org-ts-regexp nil t)
7958 (goto-char (match-beginning 0))
7959 (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
7960 (delete-region (match-beginning 1) (match-end 1)))))))
7961 (setq task (buffer-string)))
7967 (defun org-sort (with-case)
7968 "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
7969 Optional argument WITH-CASE means sort case-sensitively."
7972 ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
7973 ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
7975 (org-call-with-arg 'org-sort-entries with-case))))
7977 (defun org-sort-remove-invisible (s)
7978 (remove-text-properties 0 (length s) org-rm-props s)
7979 (while (string-match org-bracket-link-regexp s)
7980 (setq s (replace-match (if (match-end 2)
7982 (match-string 1 s)) t t s)))
7985 (defvar org-priority-regexp) ; defined later in the file
7987 (defvar org-after-sorting-entries-or-items-hook nil
7988 "Hook that is run after a bunch of entries or items have been sorted.
7989 When children are sorted, the cursor is in the parent line when this
7990 hook gets called. When a region or a plain list is sorted, the cursor
7991 will be in the first entry of the sorted region/list.")
7993 (defun org-sort-entries
7994 (&optional with-case sorting-type getkey-func compare-func property)
7995 "Sort entries on a certain level of an outline tree.
7996 If there is an active region, the entries in the region are sorted.
7997 Else, if the cursor is before the first entry, sort the top-level items.
7998 Else, the children of the entry at point are sorted.
8000 Sorting can be alphabetically, numerically, by date/time as given by
8001 a time stamp, by a property or by priority.
8003 The command prompts for the sorting type unless it has been given to the
8004 function through the SORTING-TYPE argument, which needs to be a character,
8005 \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
8006 precise meaning of each character:
8008 n Numerically, by converting the beginning of the entry/item to a number.
8009 a Alphabetically, ignoring the TODO keyword and the priority, if any.
8010 t By date/time, either the first active time stamp in the entry, or, if
8011 none exist, by the first inactive one.
8012 s By the scheduled date/time.
8013 d By deadline date/time.
8014 c By creation time, which is assumed to be the first inactive time stamp
8015 at the beginning of a line.
8016 p By priority according to the cookie.
8017 r By the value of a property.
8019 Capital letters will reverse the sort order.
8021 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
8022 called with point at the beginning of the record. It must return either
8023 a string or a number that should serve as the sorting key for that record.
8025 Comparing entries ignores case by default. However, with an optional argument
8026 WITH-CASE, the sorting considers case as well."
8028 (let ((case-func (if with-case 'identity 'downcase))
8029 start beg end stars re re2
8031 ;; Find beginning and end of region to sort
8033 ((org-region-active-p)
8034 ;; we will sort the region
8035 (setq end (region-end)
8037 (goto-char (region-beginning))
8038 (if (not (org-at-heading-p)) (outline-next-heading))
8039 (setq start (point)))
8040 ((or (org-at-heading-p)
8041 (condition-case nil (progn (org-back-to-heading) t) (error nil)))
8042 ;; we will sort the children of the current headline
8043 (org-back-to-heading)
8045 end (progn (org-end-of-subtree t t)
8046 (or (bolp) (insert "\n"))
8047 (org-back-over-empty-lines)
8052 (outline-next-heading))
8054 ;; we will sort the top-level entries in this file
8055 (goto-char (point-min))
8056 (or (org-at-heading-p) (outline-next-heading))
8057 (setq start (point))
8058 (goto-char (point-max))
8059 (beginning-of-line 1)
8060 (when (looking-at ".*?\\S-")
8061 ;; File ends in a non-white line
8064 (setq end (point-max))
8065 (setq what "top-level")
8070 (if (>= beg end) (error "Nothing to sort"))
8072 (looking-at "\\(\\*+\\)")
8073 (setq stars (match-string 1)
8074 re (concat "^" (regexp-quote stars) " +")
8075 re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
8076 txt (buffer-substring beg end))
8077 (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
8078 (if (and (not (equal stars "*")) (string-match re2 txt))
8079 (error "Region to sort contains a level above the first entry"))
8081 (unless sorting-type
8083 "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
8084 [t]ime
[s]cheduled [d]eadline [c]reated
8085 A/N/T/S/D/C/P/O/F means reversed:"
8087 (setq sorting-type (read-char-exclusive))
8089 (and (= (downcase sorting-type) ?f)
8091 (org-icompleting-read "Sort using function: "
8092 obarray 'fboundp t nil nil))
8093 (setq getkey-func (intern getkey-func)))
8095 (and (= (downcase sorting-type) ?r)
8097 (org-icompleting-read "Property: "
8098 (mapcar 'list (org-buffer-property-keys t))
8101 (message "Sorting entries...")
8104 (narrow-to-region start end)
8105 (let ((dcst (downcase sorting-type))
8106 (case-fold-search nil)
8107 (now (current-time)))
8109 (/= dcst sorting-type)
8110 ;; This function moves to the beginning character of the "record" to
8113 (if (re-search-forward re nil t)
8114 (goto-char (match-beginning 0))
8115 (goto-char (point-max))))
8116 ;; This function moves to the last character of the "record" being
8121 (outline-forward-same-level 1)
8123 (goto-char (point-max))))))
8124 ;; This function returns the value that gets sorted against.
8128 (if (looking-at org-complex-heading-regexp)
8129 (string-to-number (match-string 4))
8132 (if (looking-at org-complex-heading-regexp)
8133 (funcall case-func (match-string 4))
8136 (let ((end (save-excursion (outline-next-heading) (point))))
8137 (if (or (re-search-forward org-ts-regexp end t)
8138 (re-search-forward org-ts-regexp-both end t))
8139 (org-time-string-to-seconds (match-string 0))
8140 (org-float-time now))))
8142 (let ((end (save-excursion (outline-next-heading) (point))))
8143 (if (re-search-forward
8144 (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
8146 (org-time-string-to-seconds (match-string 0))
8147 (org-float-time now))))
8149 (let ((end (save-excursion (outline-next-heading) (point))))
8150 (if (re-search-forward org-scheduled-time-regexp end t)
8151 (org-time-string-to-seconds (match-string 1))
8152 (org-float-time now))))
8154 (let ((end (save-excursion (outline-next-heading) (point))))
8155 (if (re-search-forward org-deadline-time-regexp end t)
8156 (org-time-string-to-seconds (match-string 1))
8157 (org-float-time now))))
8159 (if (re-search-forward org-priority-regexp (point-at-eol) t)
8160 (string-to-char (match-string 2))
8161 org-default-priority))
8163 (or (org-entry-get nil property) ""))
8165 (if (looking-at org-complex-heading-regexp)
8166 (- 9999 (length (member (match-string 2)
8167 org-todo-keywords-1)))))
8171 (setq tmp (funcall getkey-func))
8172 (if (stringp tmp) (setq tmp (funcall case-func tmp)))
8174 (error "Invalid key function `%s'" getkey-func)))
8175 (t (error "Invalid sorting type `%c'" sorting-type))))
8178 ((= dcst ?a) 'string<)
8179 ((= dcst ?f) compare-func)
8180 ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
8181 (run-hooks 'org-after-sorting-entries-or-items-hook)
8182 (message "Sorting entries...done")))
8184 (defun org-do-sort (table what &optional with-case sorting-type)
8185 "Sort TABLE of WHAT according to SORTING-TYPE.
8186 The user will be prompted for the SORTING-TYPE if the call to this
8187 function does not specify it. WHAT is only for the prompt, to indicate
8188 what is being sorted. The sorting key will be extracted from
8189 the car of the elements of the table.
8190 If WITH-CASE is non-nil, the sorting will be case-sensitive."
8191 (unless sorting-type
8193 "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
8195 (setq sorting-type (read-char-exclusive)))
8196 (let ((dcst (downcase sorting-type))
8197 extractfun comparefun)
8198 ;; Define the appropriate functions
8201 (setq extractfun 'string-to-number
8202 comparefun (if (= dcst sorting-type) '< '>)))
8204 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
8205 (lambda(x) (downcase (org-sort-remove-invisible x))))
8206 comparefun (if (= dcst sorting-type)
8208 (lambda (a b) (and (not (string< a b))
8209 (not (string= a b)))))))
8213 (if (or (string-match org-ts-regexp x)
8214 (string-match org-ts-regexp-both x))
8216 (org-time-string-to-time (match-string 0 x)))
8218 comparefun (if (= dcst sorting-type) '< '>)))
8219 (t (error "Invalid sorting type `%c'" sorting-type)))
8221 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
8223 (lambda (a b) (funcall comparefun (car a) (car b))))))
8226 ;;; The orgstruct minor mode
8228 ;; Define a minor mode which can be used in other modes in order to
8229 ;; integrate the org-mode structure editing commands.
8231 ;; This is really a hack, because the org-mode structure commands use
8232 ;; keys which normally belong to the major mode. Here is how it
8233 ;; works: The minor mode defines all the keys necessary to operate the
8234 ;; structure commands, but wraps the commands into a function which
8235 ;; tests if the cursor is currently at a headline or a plain list
8236 ;; item. If that is the case, the structure command is used,
8237 ;; temporarily setting many Org-mode variables like regular
8238 ;; expressions for filling etc. However, when any of those keys is
8239 ;; used at a different location, function uses `key-binding' to look
8240 ;; up if the key has an associated command in another currently active
8241 ;; keymap (minor modes, major mode, global), and executes that
8242 ;; command. There might be problems if any of the keys is otherwise
8243 ;; used as a prefix key.
8245 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
8246 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
8247 ;; addresses this by checking explicitly for both bindings.
8249 (defvar orgstruct-mode-map (make-sparse-keymap)
8250 "Keymap for the minor `orgstruct-mode'.")
8252 (defvar org-local-vars nil
8253 "List of local variables, for use by `orgstruct-mode'.")
8256 (define-minor-mode orgstruct-mode
8257 "Toggle the minor mode `orgstruct-mode'.
8258 This mode is for using Org-mode structure commands in other
8259 modes. The following keys behave as if Org-mode were active, if
8260 the cursor is on a headline, or on a plain list item (both as
8261 defined by Org-mode).
8263 M-up Move entry/item up
8264 M-down Move entry/item down
8267 M-S-up Move entry/item up
8268 M-S-down Move entry/item down
8269 M-S-left Promote subtree
8270 M-S-right Demote subtree
8271 M-q Fill paragraph and items like in Org-mode
8273 C-c - Cycle list bullet
8274 TAB Cycle item visibility
8275 M-RET Insert new heading/item
8276 S-M-RET Insert new TODO heading / Checkbox item
8277 C-c C-c Set tags / toggle checkbox"
8278 nil " OrgStruct" nil
8279 (org-load-modules-maybe)
8280 (and (orgstruct-setup) (defun orgstruct-setup () nil)))
8283 (defun turn-on-orgstruct ()
8284 "Unconditionally turn on `orgstruct-mode'."
8287 (defvar org-fb-vars nil)
8288 (make-variable-buffer-local 'org-fb-vars)
8289 (defun orgstruct++-mode (&optional arg)
8290 "Toggle `orgstruct-mode', the enhanced version of it.
8291 In addition to setting orgstruct-mode, this also exports all
8292 indentation and autofilling variables from org-mode into the
8293 buffer. It will also recognize item context in multiline items."
8295 (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
8297 (progn (orgstruct-mode -1)
8299 (org-set-local (car v)
8300 (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
8303 (setq org-fb-vars nil)
8308 "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
8309 (symbol-name (car x)))
8310 (setq var (car x) val (nth 1 x))
8311 (push (list var `(quote ,(eval var))) org-fb-vars)
8312 (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
8314 (org-set-local 'orgstruct-is-++ t))))
8316 (defvar orgstruct-is-++ nil
8317 "Is `orgstruct-mode' in ++ version in the current-buffer?")
8318 (make-variable-buffer-local 'orgstruct-is-++)
8321 (defun turn-on-orgstruct++ ()
8322 "Unconditionally turn on `orgstruct++-mode'."
8323 (orgstruct++-mode 1))
8325 (defun orgstruct-error ()
8326 "Error when there is no default binding for a structure key."
8328 (error "This key has no function outside structure elements"))
8330 (defun orgstruct-setup ()
8331 "Setup orgstruct keymaps."
8335 '([(meta up)] org-metaup)
8336 '([(meta down)] org-metadown)
8337 '([(meta left)] org-metaleft)
8338 '([(meta right)] org-metaright)
8339 '([(meta shift up)] org-shiftmetaup)
8340 '([(meta shift down)] org-shiftmetadown)
8341 '([(meta shift left)] org-shiftmetaleft)
8342 '([(meta shift right)] org-shiftmetaright)
8343 '([?\e (up)] org-metaup)
8344 '([?\e (down)] org-metadown)
8345 '([?\e (left)] org-metaleft)
8346 '([?\e (right)] org-metaright)
8347 '([?\e (shift up)] org-shiftmetaup)
8348 '([?\e (shift down)] org-shiftmetadown)
8349 '([?\e (shift left)] org-shiftmetaleft)
8350 '([?\e (shift right)] org-shiftmetaright)
8351 '([(shift up)] org-shiftup)
8352 '([(shift down)] org-shiftdown)
8353 '([(shift left)] org-shiftleft)
8354 '([(shift right)] org-shiftright)
8355 '("\C-c\C-c" org-ctrl-c-ctrl-c)
8356 '("\M-q" fill-paragraph)
8358 '("\C-c-" org-cycle-list-bullet)))
8360 (while (setq elt (pop bindings))
8361 (setq nfunc (1+ nfunc))
8362 (setq key (org-key (car elt))
8364 cmd (orgstruct-make-binding fun nfunc key))
8365 (org-defkey orgstruct-mode-map key cmd))
8367 ;; Prevent an error for users who forgot to make autoloads
8368 (require 'org-element)
8370 ;; Special treatment needed for TAB and RET
8371 (org-defkey orgstruct-mode-map [(tab)]
8372 (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
8373 (org-defkey orgstruct-mode-map "\C-i"
8374 (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
8376 (org-defkey orgstruct-mode-map "\M-\C-m"
8377 (orgstruct-make-binding 'org-insert-heading 105
8378 "\M-\C-m" [(meta return)]))
8379 (org-defkey orgstruct-mode-map [(meta return)]
8380 (orgstruct-make-binding 'org-insert-heading 106
8381 [(meta return)] "\M-\C-m"))
8383 (org-defkey orgstruct-mode-map [(shift meta return)]
8384 (orgstruct-make-binding 'org-insert-todo-heading 107
8385 [(meta return)] "\M-\C-m"))
8387 (org-defkey orgstruct-mode-map "\e\C-m"
8388 (orgstruct-make-binding 'org-insert-heading 108
8389 "\e\C-m" [?\e (return)]))
8390 (org-defkey orgstruct-mode-map [?\e (return)]
8391 (orgstruct-make-binding 'org-insert-heading 109
8392 [?\e (return)] "\e\C-m"))
8393 (org-defkey orgstruct-mode-map [?\e (shift return)]
8394 (orgstruct-make-binding 'org-insert-todo-heading 110
8395 [?\e (return)] "\e\C-m"))
8397 (unless org-local-vars
8398 (setq org-local-vars (org-get-local-variables)))
8402 (defun orgstruct-make-binding (fun n &rest keys)
8403 "Create a function for binding in the structure minor mode.
8404 FUN is the command to call inside a table. N is used to create a unique
8405 command name. KEYS are keys that should be checked in for a command
8406 to execute outside of tables."
8409 (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
8411 (concat "In Structure, run `" (symbol-name fun) "'.\n"
8412 "Outside of structure, run the binding of `"
8413 (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
8417 `(org-context-p 'headline 'item
8418 (and orgstruct-is-++
8419 ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
8421 (list 'org-run-like-in-org-mode (list 'quote fun))
8422 (list 'let '(orgstruct-mode)
8423 (list 'call-interactively
8426 (list 'key-binding k))
8428 '('orgstruct-error))))))))
8430 (defun org-contextualize-keys (alist contexts)
8431 "Return valid elements in ALIST depending on CONTEXTS.
8433 `org-agenda-custom-commands' or `org-capture-templates' are the
8434 values used for ALIST, and `org-agenda-custom-commands-contexts'
8435 or `org-capture-templates-contexts' are the associated contexts
8438 ;; normalize contexts
8440 (lambda(c) (cond ((listp (cadr c))
8441 (list (car c) (car c) (cadr c)))
8442 ((string= "" (cadr c))
8443 (list (car c) (car c) (caddr c)))
8446 ;; loop over all commands or templates
8447 (while (setq c (pop a))
8450 ((not (assoc (car c) contexts))
8452 ((and (assoc (car c) contexts)
8453 (setq vrules (org-contextualize-validate-key
8456 (when (not (equal (car vr) (cadr vr)))
8457 (setq repl vr))) vrules)
8458 (if (not repl) (push c r)
8459 (push (cadr repl) s)
8462 (cdr (or (assoc (cadr repl) alist)
8463 (error "Undefined key `%s' as contextual replacement for `%s'"
8464 (cadr repl) (car c)))))
8466 ;; Return limited ALIST, possibly with keys modified, and deduplicated
8471 (let ((tpl (car x)))
8475 (equal y tpl)) s))) x)))
8478 (defun org-contextualize-validate-key (key contexts)
8479 "Check CONTEXTS for agenda or capture KEY."
8481 (while (setq r (pop contexts))
8485 (and (equal key (car r))
8486 (if (functionp rr) (funcall rr)
8487 (or (and (eq (car rr) 'in-file)
8489 (string-match (cdr rr) (buffer-file-name)))
8490 (and (eq (car rr) 'in-mode)
8491 (string-match (cdr rr) (symbol-name major-mode)))
8492 (when (and (eq (car rr) 'not-in-file)
8494 (not (string-match (cdr rr) (buffer-file-name))))
8495 (when (eq (car rr) 'not-in-mode)
8496 (not (string-match (cdr rr) (symbol-name major-mode)))))))
8499 (delete-dups (delq nil res))))
8501 (defun org-context-p (&rest contexts)
8502 "Check if local context is any of CONTEXTS.
8503 Possible values in the list of contexts are `table', `headline', and `item'."
8504 (let ((pos (point)))
8505 (goto-char (point-at-bol))
8506 (prog1 (or (and (memq 'table contexts)
8507 (looking-at "[ \t]*|"))
8508 (and (memq 'headline contexts)
8509 (looking-at org-outline-regexp))
8510 (and (memq 'item contexts)
8511 (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
8512 (and (memq 'item-body contexts)
8516 (defun org-get-local-variables ()
8517 "Return a list of all local variables in an Org mode buffer."
8519 (with-current-buffer (get-buffer-create "*Org tmp*")
8522 (setq varlist (buffer-local-variables)))
8523 (kill-buffer "*Org tmp*")
8530 (list (car x) (list 'quote (cdr x)))))
8532 "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
8533 (symbol-name (car x)))
8537 (defun org-clone-local-variables (from-buffer &optional regexp)
8538 "Clone local variables from FROM-BUFFER.
8539 Optional argument REGEXP selects variables to clone."
8542 (and (symbolp (car pair))
8544 (string-match regexp (symbol-name (car pair))))
8545 (set (make-local-variable (car pair))
8547 (buffer-local-variables from-buffer)))
8550 (defun org-run-like-in-org-mode (cmd)
8551 "Run a command, pretending that the current buffer is in Org-mode.
8552 This will temporarily bind local variables that are typically bound in
8553 Org-mode to the values they have in Org-mode, and then interactively
8555 (org-load-modules-maybe)
8556 (unless org-local-vars
8557 (setq org-local-vars (org-get-local-variables)))
8558 (eval (list 'let org-local-vars
8559 (list 'call-interactively (list 'quote cmd)))))
8563 (defun org-get-category (&optional pos force-refresh)
8564 "Get the category applying to position POS."
8566 (if force-refresh (org-refresh-category-properties))
8567 (let ((pos (or pos (point))))
8568 (or (get-text-property pos 'org-category)
8569 (progn (org-refresh-category-properties)
8570 (get-text-property pos 'org-category))))))
8572 (defun org-refresh-category-properties ()
8573 "Refresh category text properties in the buffer."
8574 (let ((case-fold-search t)
8575 (inhibit-read-only t)
8577 ((null org-category)
8578 (if buffer-file-name
8579 (file-name-sans-extension
8580 (file-name-nondirectory buffer-file-name))
8582 ((symbolp org-category) (symbol-name org-category))
8584 beg end cat pos optionp)
8589 (goto-char (point-min))
8590 (put-text-property (point) (point-max) 'org-category def-cat)
8591 (while (re-search-forward
8592 "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
8593 (setq pos (match-end 0)
8594 optionp (equal (char-after (match-beginning 0)) ?#)
8595 cat (org-trim (match-string 2)))
8597 (setq beg (point-at-bol) end (point-max))
8598 (org-back-to-heading t)
8599 (setq beg (point) end (org-end-of-subtree t t)))
8600 (put-text-property beg end 'org-category cat)
8601 (put-text-property beg end 'org-category-position beg)
8602 (goto-char pos)))))))
8607 ;;; Link abbreviations
8609 (defun org-link-expand-abbrev (link)
8610 "Apply replacements as defined in `org-link-abbrev-alist'."
8611 (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
8612 (let* ((key (match-string 1 link))
8613 (as (or (assoc key org-link-abbrev-alist-local)
8614 (assoc key org-link-abbrev-alist)))
8615 (tag (and (match-end 2) (match-string 3 link)))
8621 ((symbolp rpl) (funcall rpl tag))
8622 ((string-match "%(\\([^)]+\\))" rpl)
8623 (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
8624 ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
8625 ((string-match "%h" rpl)
8626 (replace-match (url-hexify-string (or tag "")) t t rpl))
8627 (t (concat rpl tag)))))
8630 ;;; Storing and inserting links
8632 (defvar org-insert-link-history nil
8633 "Minibuffer history for links inserted with `org-insert-link'.")
8635 (defvar org-stored-links nil
8636 "Contains the links stored with `org-store-link'.")
8638 (defvar org-store-link-plist nil
8639 "Plist with info about the most recently link created with `org-store-link'.")
8641 (defvar org-link-protocols nil
8642 "Link protocols added to Org-mode using `org-add-link-type'.")
8644 (defvar org-store-link-functions nil
8645 "List of functions that are called to create and store a link.
8646 Each function will be called in turn until one returns a non-nil
8647 value. Each function should check if it is responsible for creating
8648 this link (for example by looking at the major mode).
8649 If not, it must exit and return nil.
8650 If yes, it should return a non-nil value after a calling
8651 `org-store-link-props' with a list of properties and values.
8652 Special properties are:
8654 :type The link prefix, like \"http\". This must be given.
8655 :link The link, like \"http://www.astro.uva.nl/~dominik\".
8656 This is obligatory as well.
8657 :description Optional default description for the second pair
8658 of brackets in an Org-mode link. The user can still change
8659 this when inserting this link into an Org-mode buffer.
8661 In addition to these, any additional properties can be specified
8662 and then used in capture templates.")
8664 (defun org-add-link-type (type &optional follow export)
8665 "Add TYPE to the list of `org-link-types'.
8666 Re-compute all regular expressions depending on `org-link-types'
8668 FOLLOW and EXPORT are two functions.
8670 FOLLOW should take the link path as the single argument and do whatever
8671 is necessary to follow the link, for example find a file or display
8674 EXPORT should format the link path for export to one of the export formats.
8675 It should be a function accepting three arguments:
8677 path the path of the link, the text after the prefix (like \"http:\")
8678 desc the description of the link, if any, or a description added by
8679 org-export-normalize-links if there is none
8680 format the export format, a symbol like `html' or `latex' or `ascii'..
8682 The function may use the FORMAT information to return different values
8683 depending on the format. The return value will be put literally into
8684 the exported file. If the return value is nil, this means Org should
8685 do what it normally does with links which do not have EXPORT defined.
8687 Org-mode has a built-in default for exporting links. If you are happy with
8688 this default, there is no need to define an export function for the link
8689 type. For a simple example of an export function, see `org-bbdb.el'."
8690 (add-to-list 'org-link-types type t)
8691 (org-make-link-regexps)
8692 (if (assoc type org-link-protocols)
8693 (setcdr (assoc type org-link-protocols) (list follow export))
8694 (push (list type follow export) org-link-protocols)))
8696 (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
8697 (defvar org-link-to-org-use-id) ; Defined in org-id.el
8700 (defun org-store-link (arg)
8701 "\\<org-mode-map>Store an org-link to the current location.
8702 This link is added to `org-stored-links' and can later be inserted
8703 into an org-buffer with \\[org-insert-link].
8705 For some link types, a prefix arg is interpreted:
8706 For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
8707 For file links, arg negates `org-context-in-file-links'."
8709 (org-load-modules-maybe)
8710 (setq org-store-link-plist nil) ; reset
8711 (org-with-limited-levels
8712 (let (link cpltxt desc description search txt custom-id agenda-link)
8715 ((run-hook-with-args-until-success 'org-store-link-functions)
8716 (setq link (plist-get org-store-link-plist :link)
8717 desc (or (plist-get org-store-link-plist :description) link)))
8719 ((org-src-edit-buffer-p)
8721 (while (or (not label)
8725 (goto-char (point-min))
8727 (regexp-quote (format org-coderef-label-format label))
8729 (when label (message "Label exists already") (sit-for 2))
8730 (setq label (read-string "Code line label: " label)))
8732 (setq link (format org-coderef-label-format label))
8733 (setq gc (- 79 (length link)))
8734 (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
8736 (setq link (concat "(" label ")") desc nil)))
8738 ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
8739 ;; We are in the agenda, link to referenced location
8740 (let ((m (or (get-text-property (point) 'org-hd-marker)
8741 (get-text-property (point) 'org-marker))))
8743 (org-with-point-at m
8745 (if (org-called-interactively-p 'any)
8746 (call-interactively 'org-store-link)
8747 (org-store-link nil)))))))
8749 ((eq major-mode 'calendar-mode)
8750 (let ((cd (calendar-cursor-to-date)))
8753 (car org-time-stamp-formats)
8755 (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
8757 (org-store-link-props :type "calendar" :date cd)))
8759 ((eq major-mode 'help-mode)
8760 (setq link (concat "help:" (save-excursion
8761 (goto-char (point-min))
8762 (looking-at "^[^ ]+")
8764 (org-store-link-props :type "help"))
8766 ((eq major-mode 'w3-mode)
8767 (setq cpltxt (if (and (buffer-name)
8768 (not (string-match "Untitled" (buffer-name))))
8771 link (url-view-url t))
8772 (org-store-link-props :type "w3" :url (url-view-url t)))
8774 ((eq major-mode 'w3m-mode)
8775 (setq cpltxt (or w3m-current-title w3m-current-url)
8776 link w3m-current-url)
8777 (org-store-link-props :type "w3m" :url (url-view-url t)))
8779 ((setq search (run-hook-with-args-until-success
8780 'org-create-file-search-functions))
8781 (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
8783 (setq cpltxt (or description link)))
8785 ((eq major-mode 'image-mode)
8786 (setq cpltxt (concat "file:"
8787 (abbreviate-file-name buffer-file-name))
8789 (org-store-link-props :type "image" :file buffer-file-name))
8791 ((eq major-mode 'dired-mode)
8792 ;; link to the file in the current line
8793 (let ((file (dired-get-filename nil t)))
8795 (abbreviate-file-name
8796 (expand-file-name (dired-get-filename nil t)))
8797 ;; otherwise, no file so use current directory.
8799 (setq cpltxt (concat "file:" file)
8802 ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
8803 (setq custom-id (org-entry-get nil "CUSTOM_ID"))
8805 ((org-in-regexp "<<\\(.*?\\)>>")
8808 (abbreviate-file-name
8809 (buffer-file-name (buffer-base-buffer)))
8810 "::" (match-string 1))
8812 ((and (featurep 'org-id)
8813 (or (eq org-link-to-org-use-id t)
8814 (and (org-called-interactively-p 'any)
8815 (or (eq org-link-to-org-use-id 'create-if-interactive)
8816 (and (eq org-link-to-org-use-id
8817 'create-if-interactive-and-no-custom-id)
8819 (and org-link-to-org-use-id (org-entry-get nil "ID"))))
8820 ;; We can make a link using the ID.
8821 (setq link (condition-case nil
8822 (prog1 (org-id-store-link)
8823 (setq desc (plist-get org-store-link-plist :description)))
8825 ;; probably before first headline, link to file only
8827 (abbreviate-file-name
8828 (buffer-file-name (buffer-base-buffer))))))))
8830 ;; Just link to current headline
8831 (setq cpltxt (concat "file:"
8832 (abbreviate-file-name
8833 (buffer-file-name (buffer-base-buffer)))))
8834 ;; Add a context search string
8835 (when (org-xor org-context-in-file-links arg)
8837 ((org-at-heading-p) nil)
8838 ((org-region-active-p)
8839 (buffer-substring (region-beginning) (region-end)))))
8840 (when (or (null txt) (string-match "\\S-" txt))
8844 (org-make-org-heading-search-string txt)
8846 desc (or (nth 4 (ignore-errors
8847 (org-heading-components))) "NONE"))))
8848 (if (string-match "::\\'" cpltxt)
8849 (setq cpltxt (substring cpltxt 0 -2)))
8850 (setq link cpltxt))))
8852 ((buffer-file-name (buffer-base-buffer))
8853 ;; Just link to this file here.
8854 (setq cpltxt (concat "file:"
8855 (abbreviate-file-name
8856 (buffer-file-name (buffer-base-buffer)))))
8857 ;; Add a context string
8858 (when (org-xor org-context-in-file-links arg)
8859 (setq txt (if (org-region-active-p)
8860 (buffer-substring (region-beginning) (region-end))
8861 (buffer-substring (point-at-bol) (point-at-eol))))
8862 ;; Only use search option if there is some text.
8863 (when (string-match "\\S-" txt)
8865 (concat cpltxt "::" (org-make-org-heading-search-string txt))
8869 ((org-called-interactively-p 'interactive)
8870 (error "Cannot link to a buffer which is not visiting a file"))
8872 (t (setq link nil)))
8874 (if (consp link) (setq cpltxt (car link) link (cdr link)))
8875 (setq link (or link cpltxt)
8876 desc (or desc cpltxt))
8877 (if (equal desc "NONE") (setq desc nil))
8879 (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
8881 (setq org-stored-links
8882 (cons (list link desc) org-stored-links))
8883 (message "Stored: %s" (or desc link))
8885 (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
8887 (setq org-stored-links
8888 (cons (list link desc) org-stored-links))))
8889 (or agenda-link (and link (org-make-link-string link desc)))))))
8891 (defun org-store-link-props (&rest plist)
8892 "Store link properties, extract names and addresses."
8894 (when (setq x (plist-get plist :from))
8895 (setq adr (mail-extract-address-components x))
8896 (setq plist (plist-put plist :fromname (car adr)))
8897 (setq plist (plist-put plist :fromaddress (nth 1 adr))))
8898 (when (setq x (plist-get plist :to))
8899 (setq adr (mail-extract-address-components x))
8900 (setq plist (plist-put plist :toname (car adr)))
8901 (setq plist (plist-put plist :toaddress (nth 1 adr)))))
8902 (let ((from (plist-get plist :from))
8903 (to (plist-get plist :to)))
8904 (when (and from to org-from-is-user-regexp)
8906 (plist-put plist :fromto
8907 (if (string-match org-from-is-user-regexp from)
8909 (concat "from %f"))))))
8910 (setq org-store-link-plist plist))
8912 (defun org-add-link-props (&rest plist)
8913 "Add these properties to the link property list."
8916 (setq key (pop plist) value (pop plist))
8917 (setq org-store-link-plist
8918 (plist-put org-store-link-plist key value)))))
8920 (defun org-email-link-description (&optional fmt)
8921 "Return the description part of an email link.
8922 This takes information from `org-store-link-plist' and formats it
8923 according to FMT (default from `org-email-link-description-format')."
8924 (setq fmt (or fmt org-email-link-description-format))
8925 (let* ((p org-store-link-plist)
8926 (to (plist-get p :toaddress))
8927 (from (plist-get p :fromaddress))
8930 (cons "%c" (plist-get p :fromto))
8931 (cons "%F" (plist-get p :from))
8932 (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
8933 (cons "%T" (plist-get p :to))
8934 (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
8935 (cons "%s" (plist-get p :subject))
8936 (cons "%d" (plist-get p :date))
8937 (cons "%m" (plist-get p :message-id)))))
8938 (when (string-match "%c" fmt)
8939 ;; Check if the user wrote this message
8940 (if (and org-from-is-user-regexp from to
8941 (save-match-data (string-match org-from-is-user-regexp from)))
8942 (setq fmt (replace-match "to %t" t t fmt))
8943 (setq fmt (replace-match "from %f" t t fmt))))
8944 (org-replace-escapes fmt table)))
8946 (defun org-make-org-heading-search-string (&optional string heading)
8947 "Make search string for STRING or current headline."
8949 (let ((s (or string (org-get-heading)))
8950 (lines org-context-in-file-links))
8951 (unless (and string (not heading))
8952 ;; We are using a headline, clean up garbage in there.
8953 (if (string-match org-todo-regexp s)
8954 (setq s (replace-match "" t t s)))
8955 (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
8956 (setq s (replace-match "" t t s)))
8957 (setq s (org-trim s))
8958 (if (string-match (concat "^\\(" org-quote-string "\\|"
8959 org-comment-string "\\)") s)
8960 (setq s (replace-match "" t t s)))
8961 (while (string-match org-ts-regexp s)
8962 (setq s (replace-match "" t t s))))
8963 (or string (setq s (concat "*" s))) ; Add * for headlines
8964 (when (and string (integerp lines) (> lines 0))
8965 (let ((slines (org-split-string s "\n")))
8966 (when (< lines (length slines))
8969 (reverse (nthcdr (- (length slines) lines)
8970 (reverse slines))) "\n")))))
8971 (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
8973 (defun org-make-link-string (link &optional description)
8974 "Make a link with brackets, consisting of LINK and DESCRIPTION."
8975 (unless (string-match "\\S-" link)
8976 (error "Empty link"))
8977 (when (and description
8978 (stringp description)
8979 (not (string-match "\\S-" description)))
8980 (setq description nil))
8981 (when (stringp description)
8982 ;; Remove brackets from the description, they are fatal.
8983 (while (string-match "\\[" description)
8984 (setq description (replace-match "{" t t description)))
8985 (while (string-match "\\]" description)
8986 (setq description (replace-match "}" t t description))))
8987 (when (equal link description)
8988 ;; No description needed, it is identical
8989 (setq description nil))
8990 (when (and (not description)
8991 (not (string-match (org-image-file-name-regexp) link))
8992 (not (equal link (org-link-escape link))))
8993 (setq description (org-extract-attributes link)))
8995 (cond ((string-match (org-image-file-name-regexp) link) link)
8996 ((string-match org-link-types-re link)
8997 (concat (match-string 1 link)
8998 (org-link-escape (substring link (match-end 1)))))
8999 (t (org-link-escape link))))
9000 (concat "[[" link "]"
9001 (if description (concat "[" description "]") "")
9004 (defconst org-link-escape-chars
9005 '(?\ ?\[ ?\] ?\; ?\= ?\+)
9006 "List of characters that should be escaped in link.
9007 This is the list that is used for internal purposes.")
9009 (defconst org-link-escape-chars-browser
9011 "List of escapes for characters that are problematic in links.
9012 This is the list that is used before handing over to the browser.")
9014 (defun org-link-escape (text &optional table merge)
9015 "Return percent escaped representation of TEXT.
9016 TEXT is a string with the text to escape.
9017 Optional argument TABLE is a list with characters that should be
9018 escaped. When nil, `org-link-escape-chars' is used.
9019 If optional argument MERGE is set, merge TABLE into
9020 `org-link-escape-chars'."
9023 (mapc (lambda (defchr)
9024 (unless (member defchr table)
9025 (setq table (cons defchr table)))) org-link-escape-chars))
9027 (setq table org-link-escape-chars)))
9030 (if (or (member char table)
9031 (and (or (< char 32) (= char 37) (> char 126))
9033 (mapconcat (lambda (sequence-element)
9034 (format "%%%.2X" sequence-element))
9035 (or (encode-coding-char char 'utf-8)
9036 (error "Unable to percent escape character: %s"
9037 (char-to-string char))) "")
9038 (char-to-string char))) text ""))
9040 (defun org-link-unescape (str)
9041 "Unhex hexified Unicode strings as returned from the JavaScript function
9042 encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
9043 (unless (and (null str) (string= "" str))
9044 (let ((pos 0) (case-fold-search t) unhexed)
9045 (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
9046 (setq unhexed (org-link-unescape-compound (match-string 0 str)))
9047 (setq str (replace-match unhexed t t str))
9048 (setq pos (+ pos (length unhexed))))))
9051 (defun org-link-unescape-compound (hex)
9052 "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
9053 Note: this function also decodes single byte encodings like
9054 `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
9056 (let* ((bytes (cdr (split-string hex "%")))
9061 (let* ((val (string-to-number (pop bytes) 16))
9065 ((>= val 252) (cons 6 252))
9066 ((>= val 248) (cons 5 248))
9067 ((>= val 240) (cons 4 240))
9068 ((>= val 224) (cons 3 224))
9069 ((>= val 192) (cons 2 192))
9072 (if (>= val 192) (setq eat (car shift-xor)))
9073 (setq val (logxor val (cdr shift-xor)))
9074 (setq sum (+ (lsh sum (car shift-xor)) val))
9075 (if (> eat 0) (setq eat (- eat 1)))
9077 ((= 0 eat) ;multi byte
9078 (setq ret (concat ret (org-char-to-string sum)))
9080 ((not bytes) ; single byte(s)
9081 (setq ret (org-link-unescape-single-byte-sequence hex))))
9082 )) ;; end (while bytes
9085 (defun org-link-unescape-single-byte-sequence (hex)
9086 "Unhexify hex-encoded single byte character sequences."
9087 (mapconcat (lambda (byte)
9088 (char-to-string (string-to-number byte 16)))
9089 (cdr (split-string hex "%")) ""))
9091 (defun org-xor (a b)
9095 (defun org-fixup-message-id-for-http (s)
9096 "Replace special characters in a message id, so it can be used in an http query."
9097 (when (string-match "%" s)
9098 (setq s (mapconcat (lambda (c)
9101 (char-to-string c)))
9103 (while (string-match "<" s)
9104 (setq s (replace-match "%3C" t t s)))
9105 (while (string-match ">" s)
9106 (setq s (replace-match "%3E" t t s)))
9107 (while (string-match "@" s)
9108 (setq s (replace-match "%40" t t s)))
9111 (defun org-link-prettify (link)
9112 "Return a human-readable representation of LINK.
9113 The car of LINK must be a raw link the cdr of LINK must be either
9114 a link description or nil."
9115 (let ((desc (or (cadr link) "<no description>")))
9116 (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
9117 "<" (car link) ">")))
9120 (defun org-insert-link-global ()
9121 "Insert a link like Org-mode does.
9122 This command can be called in any mode to insert a link in Org-mode syntax."
9124 (org-load-modules-maybe)
9125 (org-run-like-in-org-mode 'org-insert-link))
9127 (defun org-insert-all-links (&optional keep)
9128 "Insert all links in `org-stored-links'."
9130 (let ((links (copy-sequence org-stored-links)) l)
9131 (while (setq l (if keep (pop links) (pop org-stored-links)))
9133 (org-insert-link nil (car l) (cadr l))
9136 (defun org-link-fontify-links-to-this-file ()
9137 "Fontify links to the current file in `org-stored-links'."
9138 (let ((f (buffer-file-name)) a b)
9139 (setq a (mapcar (lambda(l)
9141 (when (and (string-match "^file:\\(.+\\)::" ll)
9142 (equal f (expand-file-name (match-string 1 ll))))
9145 (when (featurep 'org-id)
9146 (setq b (mapcar (lambda(l)
9148 (when (and (string-match "^id:\\(.+\\)$" ll)
9149 (equal f (expand-file-name
9150 (or (org-id-find-id-file
9151 (match-string 1 ll)) ""))))
9155 (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
9156 (delq nil (append a b)))))
9158 (defvar org-link-links-in-this-file nil)
9159 (defun org-insert-link (&optional complete-file link-location default-description)
9160 "Insert a link. At the prompt, enter the link.
9162 Completion can be used to insert any of the link protocol prefixes like
9165 The history can be used to select a link previously stored with
9166 `org-store-link'. When the empty string is entered (i.e. if you just
9167 press RET at the prompt), the link defaults to the most recently
9168 stored link. As SPC triggers completion in the minibuffer, you need to
9169 use M-SPC or C-q SPC to force the insertion of a space character.
9171 You will also be prompted for a description, and if one is given, it will
9172 be displayed in the buffer instead of the link.
9174 If there is already a link at point, this command will allow you to edit link
9175 and description parts.
9177 With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
9178 be selected using completion. The path to the file will be relative to the
9179 current directory if the file is in the current directory or a subdirectory.
9180 Otherwise, the link will be the absolute path as completed in the minibuffer
9181 \(i.e. normally ~/path/to/file). You can configure this behavior using the
9182 option `org-link-file-path-type'.
9184 With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
9185 the current directory or below.
9187 With three \\[universal-argument] prefixes, negate the meaning of
9188 `org-keep-stored-link-after-insertion'.
9190 If `org-make-link-description-function' is non-nil, this function will be
9191 called with the link target, and the result will be the default
9194 If the LINK-LOCATION parameter is non-nil, this value will be
9195 used as the link location instead of reading one interactively.
9197 If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
9198 be used as the default description."
9200 (let* ((wcf (current-window-configuration))
9201 (region (if (org-region-active-p)
9202 (buffer-substring (region-beginning) (region-end))))
9203 (remove (and region (list (region-beginning) (region-end))))
9205 tmphist ; byte-compile incorrectly complains about this
9206 (link link-location)
9207 (abbrevs org-link-abbrev-alist-local)
9208 entry file all-prefixes auto-desc)
9210 (link-location) ; specified by arg, just use it.
9211 ((org-in-regexp org-bracket-link-regexp 1)
9212 ;; We do have a link at point, and we are going to edit it.
9213 (setq remove (list (match-beginning 0) (match-end 0)))
9214 (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
9215 (setq link (read-string "Link: "
9217 (org-match-string-no-properties 1)))))
9218 ((or (org-in-regexp org-angle-link-re)
9219 (org-in-regexp org-plain-link-re))
9220 ;; Convert to bracket link
9221 (setq remove (list (match-beginning 0) (match-end 0))
9222 link (read-string "Link: "
9223 (org-remove-angle-brackets (match-string 0)))))
9224 ((member complete-file '((4) (16)))
9225 ;; Completing read for file names.
9226 (setq link (org-file-complete-link complete-file)))
9228 ;; Read link, with completion for stored links.
9229 (org-link-fontify-links-to-this-file)
9230 (org-switch-to-buffer-other-window "*Org Links*")
9231 (with-current-buffer "*Org Links*"
9233 (insert "Insert a link.
9234 Use TAB to complete link prefixes, then RET for type-specific completion support\n")
9235 (when org-stored-links
9236 (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
9237 (insert (mapconcat 'org-link-prettify
9238 (reverse org-stored-links) "\n")))
9239 (goto-char (point-min)))
9240 (let ((cw (selected-window)))
9241 (select-window (get-buffer-window "*Org Links*" 'visible))
9242 (with-current-buffer "*Org Links*" (setq truncate-lines t))
9243 (unless (pos-visible-in-window-p (point-max))
9244 (org-fit-window-to-buffer))
9245 (and (window-live-p cw) (select-window cw)))
9246 ;; Fake a link history, containing the stored links.
9247 (setq tmphist (append (mapcar 'car org-stored-links)
9248 org-insert-link-history))
9249 (setq all-prefixes (append (mapcar 'car abbrevs)
9250 (mapcar 'car org-link-abbrev-alist)
9255 (let ((org-completion-use-ido nil)
9256 (org-completion-use-iswitchb nil))
9257 (org-completing-read
9260 (mapcar (lambda (x) (list (concat x ":")))
9262 (mapcar 'car org-stored-links)
9263 (mapcar 'cadr org-stored-links))
9266 (caar org-stored-links))))
9267 (if (not (string-match "\\S-" link))
9268 (error "No link selected"))
9270 (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
9272 (if (or (member link all-prefixes)
9273 (and (equal ":" (substring link -1))
9274 (member (substring link 0 -1) all-prefixes)
9275 (setq link (substring link 0 -1))))
9276 (setq link (org-link-try-special-completion link))))
9277 (set-window-configuration wcf)
9278 (kill-buffer "*Org Links*"))
9279 (setq entry (assoc link org-stored-links))
9280 (or entry (push link org-insert-link-history))
9281 (setq desc (or desc (nth 1 entry)))))
9283 (if (funcall (if (equal complete-file '(64)) 'not 'identity)
9284 (not org-keep-stored-link-after-insertion))
9285 (setq org-stored-links (delq (assoc link org-stored-links)
9288 (if (string-match org-plain-link-re link)
9289 ;; URL-like link, normalize the use of angular brackets.
9290 (setq link (org-remove-angle-brackets link)))
9292 ;; Check if we are linking to the current file with a search option
9293 ;; If yes, simplify the link by using only the search option.
9294 (when (and buffer-file-name
9295 (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
9296 (let* ((path (match-string 1 link))
9297 (case-fold-search nil)
9298 (search (match-string 2 link)))
9300 (if (equal (file-truename buffer-file-name) (file-truename path))
9301 ;; We are linking to this same file, with a search option
9302 (setq link search)))))
9304 ;; Check if we can/should use a relative path. If yes, simplify the link
9305 (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
9306 (let* ((type (match-string 1 link))
9307 (path (match-string 2 link))
9309 (case-fold-search nil))
9311 ((or (eq org-link-file-path-type 'absolute)
9312 (equal complete-file '(16)))
9313 (setq path (abbreviate-file-name (expand-file-name path))))
9314 ((eq org-link-file-path-type 'noabbrev)
9315 (setq path (expand-file-name path)))
9316 ((eq org-link-file-path-type 'relative)
9317 (setq path (file-relative-name path)))
9320 (if (string-match (concat "^" (regexp-quote
9322 (file-name-as-directory
9323 default-directory))))
9324 (expand-file-name path))
9325 ;; We are linking a file with relative path name.
9326 (setq path (substring (expand-file-name path)
9328 (setq path (abbreviate-file-name (expand-file-name path)))))))
9329 (setq link (concat type path))
9330 (if (equal desc origpath)
9333 (if org-make-link-description-function
9335 (or (condition-case nil
9336 (funcall org-make-link-description-function link desc)
9337 (error (progn (message "Can't get link description from `%s'"
9338 (symbol-name org-make-link-description-function))
9340 (read-string "Description: " default-description)))
9341 (if default-description (setq desc default-description)
9342 (setq desc (or (and auto-desc desc)
9343 (read-string "Description: " desc)))))
9345 (unless (string-match "\\S-" desc) (setq desc nil))
9346 (if remove (apply 'delete-region remove))
9347 (insert (org-make-link-string link desc))))
9349 (defun org-link-try-special-completion (type)
9350 "If there is completion support for link type TYPE, offer it."
9351 (let ((fun (intern (concat "org-" type "-complete-link"))))
9354 (read-string "Link (no completion support): " (concat type ":")))))
9356 (defun org-file-complete-link (&optional arg)
9357 "Create a file link using completion."
9359 (setq file (read-file-name "File: "))
9360 (let ((pwd (file-name-as-directory (expand-file-name ".")))
9361 (pwd1 (file-name-as-directory (abbreviate-file-name
9362 (expand-file-name ".")))))
9367 (abbreviate-file-name (expand-file-name file)))))
9368 ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
9369 (setq link (concat "file:" (match-string 1 file))))
9370 ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
9371 (expand-file-name file))
9373 "file:" (match-string 1 (expand-file-name file)))))
9374 (t (setq link (concat "file:" file)))))
9377 (defun org-completing-read (&rest args)
9378 "Completing-read with SPACE being a normal character."
9379 (let ((enable-recursive-minibuffers t)
9380 (minibuffer-local-completion-map
9381 (copy-keymap minibuffer-local-completion-map)))
9382 (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
9383 (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
9384 (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
9385 (apply 'org-icompleting-read args)))
9387 (defun org-completing-read-no-i (&rest args)
9388 (let (org-completion-use-ido org-completion-use-iswitchb)
9389 (apply 'org-completing-read args)))
9391 (defun org-iswitchb-completing-read (prompt choices &rest args)
9392 "Use iswitch as a completing-read replacement to choose from choices.
9393 PROMPT is a string to prompt with. CHOICES is a list of strings to choose
9395 (let* ((iswitchb-use-virtual-buffers nil)
9396 (iswitchb-make-buflist-hook
9398 (setq iswitchb-temp-buflist choices))))
9399 (iswitchb-read-buffer prompt)))
9401 (defun org-icompleting-read (&rest args)
9402 "Completing-read using `ido-mode' or `iswitchb' speedups if available."
9403 (org-without-partial-completion
9404 (if (and org-completion-use-ido
9405 (fboundp 'ido-completing-read)
9406 (boundp 'ido-mode) ido-mode
9407 (listp (second args)))
9408 (let ((ido-enter-matching-directory nil))
9409 (apply 'ido-completing-read (concat (car args))
9410 (if (consp (car (nth 1 args)))
9411 (mapcar 'car (nth 1 args))
9414 (if (and org-completion-use-iswitchb
9415 (boundp 'iswitchb-mode) iswitchb-mode
9416 (listp (second args)))
9417 (apply 'org-iswitchb-completing-read (concat (car args))
9418 (if (consp (car (nth 1 args)))
9419 (mapcar 'car (nth 1 args))
9422 (apply 'completing-read args)))))
9424 (defun org-extract-attributes (s)
9425 "Extract the attributes cookie from a string and set as text property."
9426 (let (a attr (start 0) key value)
9428 (when (string-match "{{\\([^}]+\\)}}$" s)
9429 (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
9430 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
9431 (setq key (match-string 1 a) value (match-string 2 a)
9433 attr (plist-put attr (intern key) value))))
9434 (org-add-props s nil 'org-attr attr))
9437 (defun org-extract-attributes-from-string (tag)
9438 (let (key value attr)
9439 (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
9440 (setq key (match-string 1 tag) value (match-string 2 tag)
9441 tag (replace-match "" t t tag)
9442 attr (plist-put attr (intern key) value)))
9445 (defun org-attributes-to-string (plist)
9446 "Format a property list into an HTML attribute list."
9447 (let ((s "") key value)
9449 (setq key (pop plist) value (pop plist))
9451 (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
9454 ;;; Opening/following a link
9456 (defvar org-link-search-failed nil)
9458 (defvar org-open-link-functions nil
9459 "Hook for functions finding a plain text link.
9460 These functions must take a single argument, the link content.
9461 They will be called for links that look like [[link text][description]]
9462 when LINK TEXT does not have a protocol like \"http:\" and does not look
9463 like a filename (e.g. \"./blue.png\").
9465 These functions will be called *before* Org attempts to resolve the
9466 link by doing text searches in the current buffer - so if you want a
9467 link \"[[target]]\" to still find \"<<target>>\", your function should
9468 handle this as a special case.
9470 When the function does handle the link, it must return a non-nil value.
9471 If it decides that it is not responsible for this link, it must return
9472 nil to indicate that that Org-mode can continue with other options
9473 like exact and fuzzy text search.")
9475 (defun org-next-link ()
9476 "Move forward to the next link.
9477 If the link is in hidden text, expose it."
9479 (when (and org-link-search-failed (eq this-command last-command))
9480 (goto-char (point-min))
9481 (message "Link search wrapped back to beginning of buffer"))
9482 (setq org-link-search-failed nil)
9483 (let* ((pos (point))
9485 (a (assoc :link ct)))
9486 (if a (goto-char (nth 2 a)))
9487 (if (re-search-forward org-any-link-re nil t)
9489 (goto-char (match-beginning 0))
9490 (if (outline-invisible-p) (org-show-context)))
9492 (setq org-link-search-failed t)
9493 (error "No further link found"))))
9495 (defun org-previous-link ()
9496 "Move backward to the previous link.
9497 If the link is in hidden text, expose it."
9499 (when (and org-link-search-failed (eq this-command last-command))
9500 (goto-char (point-max))
9501 (message "Link search wrapped back to end of buffer"))
9502 (setq org-link-search-failed nil)
9503 (let* ((pos (point))
9505 (a (assoc :link ct)))
9506 (if a (goto-char (nth 1 a)))
9507 (if (re-search-backward org-any-link-re nil t)
9509 (goto-char (match-beginning 0))
9510 (if (outline-invisible-p) (org-show-context)))
9512 (setq org-link-search-failed t)
9513 (error "No further link found"))))
9515 (defun org-translate-link (s)
9516 "Translate a link string if a translation function has been defined."
9517 (if (and org-link-translation-function
9518 (fboundp org-link-translation-function)
9519 (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
9521 (setq s (funcall org-link-translation-function
9522 (match-string 1 s) (match-string 2 s)))
9523 (concat (car s) ":" (cdr s)))
9526 (defun org-translate-link-from-planner (type path)
9527 "Translate a link from Emacs Planner syntax so that Org can follow it.
9528 This is still an experimental function, your mileage may vary."
9530 ((member type '("http" "https" "news" "ftp"))
9531 ;; standard Internet links are the same.
9533 ((and (equal type "irc") (string-match "^//" path))
9534 ;; Planner has two / at the beginning of an irc link, we have 1.
9535 ;; We should have zero, actually....
9536 (setq path (substring path 1)))
9537 ((and (equal type "lisp") (string-match "^/" path))
9538 ;; Planner has a slash, we do not.
9539 (setq type "elisp" path (substring path 1)))
9540 ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
9541 ;; A typical message link. Planner has the id after the final slash,
9542 ;; we separate it with a hash mark
9543 (setq path (concat (match-string 1 path) "#"
9544 (org-remove-angle-brackets (match-string 2 path)))))
9548 (defun org-find-file-at-mouse (ev)
9549 "Open file link or URL at mouse."
9551 (mouse-set-point ev)
9552 (org-open-at-point 'in-emacs))
9554 (defun org-open-at-mouse (ev)
9555 "Open file link or URL at mouse.
9556 See the docstring of `org-open-file' for details."
9558 (mouse-set-point ev)
9559 (if (eq major-mode 'org-agenda-mode)
9560 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
9561 (org-open-at-point))
9563 (defvar org-window-config-before-follow-link nil
9564 "The window configuration before following a link.
9565 This is saved in case the need arises to restore it.")
9567 (defvar org-open-link-marker (make-marker)
9568 "Marker pointing to the location where `org-open-at-point; was called.")
9571 (defun org-open-at-point-global ()
9572 "Follow a link like Org-mode does.
9573 This command can be called in any mode to follow a link that has
9576 (org-run-like-in-org-mode 'org-open-at-point))
9579 (defun org-open-link-from-string (s &optional arg reference-buffer)
9580 "Open a link in the string S, as if it was in Org-mode."
9581 (interactive "sLink: \nP")
9582 (let ((reference-buffer (or reference-buffer (current-buffer))))
9584 (let ((org-inhibit-startup (not reference-buffer)))
9587 (goto-char (point-min))
9588 (when reference-buffer
9589 (setq org-link-abbrev-alist-local
9590 (with-current-buffer reference-buffer
9591 org-link-abbrev-alist-local)))
9592 (org-open-at-point arg reference-buffer)))))
9594 (defvar org-open-at-point-functions nil
9595 "Hook that is run when following a link at point.
9597 Functions in this hook must return t if they identify and follow
9598 a link at point. If they don't find anything interesting at point,
9599 they must return nil.")
9601 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
9602 (defun org-open-at-point (&optional arg reference-buffer)
9603 "Open link at or after point.
9604 If there is no link at point, this function will search forward up to
9605 the end of the current line.
9606 Normally, files will be opened by an appropriate application. If the
9607 optional prefix argument ARG is non-nil, Emacs will visit the file.
9608 With a double prefix argument, try to open outside of Emacs, in the
9609 application the system uses for this file type."
9611 ;; if in a code block, then open the block's results
9612 (unless (call-interactively #'org-babel-open-src-block-result)
9613 (org-load-modules-maybe)
9614 (move-marker org-open-link-marker (point))
9615 (setq org-window-config-before-follow-link (current-window-configuration))
9616 (org-remove-occur-highlights nil nil t)
9618 ((and (org-at-heading-p)
9619 (not (org-at-timestamp-p t))
9621 (concat org-plain-link-re "\\|"
9622 org-bracket-link-regexp "\\|"
9623 org-angle-link-re "\\|"
9624 "[ \t]:[^ \t\n]+:[ \t]*$")))
9625 (not (get-text-property (point) 'org-linked-text)))
9626 (or (org-offer-links-in-entry arg)
9627 (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
9628 ((run-hook-with-args-until-success 'org-open-at-point-functions))
9629 ((and (org-at-timestamp-p t)
9630 (not (org-in-regexp org-bracket-link-regexp)))
9631 (org-follow-timestamp-link))
9632 ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
9633 (not (org-in-regexp org-bracket-link-regexp)))
9634 (org-footnote-action))
9636 (let (type path link line search (pos (point)))
9639 (skip-chars-forward "^]\n\r")
9640 (when (org-in-regexp org-bracket-link-regexp 1)
9641 (setq link (org-extract-attributes
9642 (org-link-unescape (org-match-string-no-properties 1))))
9643 (while (string-match " *\n *" link)
9644 (setq link (replace-match " " t t link)))
9645 (setq link (org-link-expand-abbrev link))
9647 ((or (file-name-absolute-p link)
9648 (string-match "^\\.\\.?/" link))
9649 (setq type "file" path link))
9650 ((string-match org-link-re-with-space3 link)
9651 (setq type (match-string 1 link) path (match-string 2 link)))
9652 ((string-match "^help:+\\(.+\\)" link)
9653 (setq type "help" path (match-string 1 link)))
9654 (t (setq type "thisfile" path link)))
9657 (when (get-text-property (point) 'org-linked-text)
9658 (setq type "thisfile"
9659 pos (if (get-text-property (1+ (point)) 'org-linked-text)
9660 (1+ (point)) (point))
9661 path (buffer-substring
9662 (or (previous-single-property-change pos 'org-linked-text)
9664 (or (next-single-property-change pos 'org-linked-text)
9669 (when (or (org-in-regexp org-angle-link-re)
9670 (and (goto-char (car (org-in-regexp org-plain-link-re)))
9671 (save-match-data (not (looking-back "\\[\\[")))))
9672 (setq type (match-string 1)
9673 path (org-link-unescape (match-string 2)))
9676 (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
9678 path (match-string 1))
9679 (while (string-match ":" path)
9680 (setq path (replace-match "+" t t path)))
9682 (when (org-in-regexp "<\\([^><\n]+\\)>")
9683 (setq type "tree-match"
9684 path (match-string 1))
9687 (error "No link found"))
9689 ;; switch back to reference buffer
9690 ;; needed when if called in a temporary buffer through
9691 ;; org-open-link-from-string
9692 (with-current-buffer (or reference-buffer (current-buffer))
9694 ;; Remove any trailing spaces in path
9695 (if (string-match " +\\'" path)
9696 (setq path (replace-match "" t t path)))
9697 (if (and org-link-translation-function
9698 (fboundp org-link-translation-function))
9699 ;; Check if we need to translate the link
9700 (let ((tmp (funcall org-link-translation-function type path)))
9701 (setq type (car tmp) path (cdr tmp))))
9705 ((assoc type org-link-protocols)
9706 (funcall (nth 1 (assoc type org-link-protocols)) path))
9708 ((equal type "help")
9709 (let ((f-or-v (intern path)))
9710 (cond ((fboundp f-or-v)
9711 (describe-function f-or-v))
9713 (describe-variable f-or-v))
9714 (t (error "Not a known function or variable")))))
9716 ((equal type "mailto")
9717 (let ((cmd (car org-link-mailto-program))
9718 (args (cdr org-link-mailto-program)) args1
9719 (address path) (subject "") a)
9720 (if (string-match "\\(.*\\)::\\(.*\\)" path)
9721 (setq address (match-string 1 path)
9722 subject (org-link-escape (match-string 2 path))))
9725 ((not (stringp (car args))) (push (pop args) args1))
9726 (t (setq a (pop args))
9727 (if (string-match "%a" a)
9728 (setq a (replace-match address t t a)))
9729 (if (string-match "%s" a)
9730 (setq a (replace-match subject t t a)))
9732 (apply cmd (nreverse args1))))
9734 ((member type '("http" "https" "ftp" "news"))
9735 (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
9737 path org-link-escape-chars-browser)
9740 ((string= type "doi")
9741 (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
9743 path org-link-escape-chars-browser)
9746 ((member type '("message"))
9747 (browse-url (concat type ":" path)))
9749 ((string= type "tags")
9750 (org-tags-view arg path))
9752 ((string= type "tree-match")
9753 (org-occur (concat "\\[" (regexp-quote path) "\\]")))
9755 ((string= type "file")
9756 (if (string-match "::\\([0-9]+\\)\\'" path)
9757 (setq line (string-to-number (match-string 1 path))
9758 path (substring path 0 (match-beginning 0)))
9759 (if (string-match "::\\(.+\\)\\'" path)
9760 (setq search (match-string 1 path)
9761 path (substring path 0 (match-beginning 0)))))
9762 (if (string-match "[*?{]" (file-name-nondirectory path))
9764 (org-open-file path arg line search)))
9766 ((string= type "shell")
9767 (let ((buf (generate-new-buffer "*Org Shell Output"))
9769 (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
9770 (string-match org-confirm-shell-link-not-regexp cmd))
9771 (not org-confirm-shell-link-function)
9772 (funcall org-confirm-shell-link-function
9773 (format "Execute \"%s\" in shell? "
9774 (org-add-props cmd nil
9775 'face 'org-warning))))
9777 (message "Executing %s" cmd)
9778 (shell-command cmd buf)
9779 (if (featurep 'midnight)
9780 (setq clean-buffer-list-kill-buffer-names
9781 (cons buf clean-buffer-list-kill-buffer-names))))
9784 ((string= type "elisp")
9786 (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
9787 (string-match org-confirm-elisp-link-not-regexp cmd))
9788 (not org-confirm-elisp-link-function)
9789 (funcall org-confirm-elisp-link-function
9790 (format "Execute \"%s\" as elisp? "
9791 (org-add-props cmd nil
9792 'face 'org-warning))))
9793 (message "%s => %s" cmd
9794 (if (equal (string-to-char cmd) ?\()
9796 (call-interactively (read cmd))))
9799 ((and (string= type "thisfile")
9800 (run-hook-with-args-until-success
9801 'org-open-link-functions path)))
9803 ((string= type "thisfile")
9805 (switch-to-buffer-other-window
9806 (org-get-buffer-for-internal-link (current-buffer)))
9807 (org-mark-ring-push))
9808 (let ((cmd `(org-link-search
9810 ,(cond ((equal arg '(4)) ''occur)
9811 ((equal arg '(16)) ''org-occur))
9813 (condition-case nil (let ((org-link-search-inhibit-query t))
9815 (error (progn (widen) (eval cmd))))))
9817 (t (browse-url-at-point)))))))
9818 (move-marker org-open-link-marker nil)
9819 (run-hook-with-args 'org-follow-link-hook)))
9821 (defun org-offer-links-in-entry (&optional nth zero)
9822 "Offer links in the current entry and follow the selected link.
9823 If there is only one link, follow it immediately as well.
9824 If NTH is an integer, immediately pick the NTH link found.
9825 If ZERO is a string, check also this string for a link, and if
9826 there is one, offer it as link number zero."
9827 (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
9828 "\\(" org-angle-link-re "\\)\\|"
9829 "\\(" org-plain-link-re "\\)"))
9831 (in-emacs (if (integerp nth) nil nth))
9832 have-zero end links link c)
9833 (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
9834 (push (match-string 0 zero) links)
9835 (setq cnt (1- cnt) have-zero t))
9837 (org-back-to-heading t)
9838 (setq end (save-excursion (outline-next-heading) (point)))
9839 (while (re-search-forward re end t)
9840 (push (match-string 0) links))
9841 (setq links (org-uniquify (reverse links))))
9845 (message "No links"))
9846 ((equal (length links) 1)
9847 (setq link (list (car links))))
9848 ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
9849 (setq link (list (nth (if have-zero nth (1- nth)) links))))
9850 (t ; we have to select a link
9852 (save-window-excursion
9853 (delete-other-windows)
9854 (with-output-to-temp-buffer "*Select Link*"
9856 (if (not (string-match org-bracket-link-regexp l))
9857 (princ (format "[%c] %s\n" (incf cnt)
9858 (org-remove-angle-brackets l)))
9860 (princ (format "[%c] %s (%s)\n" (incf cnt)
9861 (match-string 3 l) (match-string 1 l)))
9862 (princ (format "[%c] %s\n" (incf cnt)
9863 (match-string 1 l))))))
9865 (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
9866 (message "Select link to open, RET to open all:")
9867 (setq c (read-char-exclusive))
9868 (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
9869 (when (equal c ?q) (error "Abort"))
9873 (if have-zero (setq nth (1+ nth)))
9874 (unless (and (integerp nth) (>= (length links) nth))
9875 (error "Invalid link selection"))
9876 (setq link (list (nth (1- nth) links))))))
9878 (let ((buf (current-buffer)))
9880 (org-open-link-from-string l in-emacs buf))
9884 ;; Add special file links that specify the way of opening
9886 (org-add-link-type "file+sys" 'org-open-file-with-system)
9887 (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
9888 (defun org-open-file-with-system (path)
9889 "Open file at PATH using the system way of opening it."
9890 (org-open-file path 'system))
9891 (defun org-open-file-with-emacs (path)
9892 "Open file at PATH in Emacs."
9893 (org-open-file path 'emacs))
9894 (defun org-remove-file-link-modifiers ()
9895 "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
9896 (goto-char (point-min))
9897 (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
9899 (replace-match "file:" t t))))
9900 (eval-after-load "org-exp"
9901 '(add-hook 'org-export-preprocess-before-normalizing-links-hook
9902 'org-remove-file-link-modifiers))
9906 (defun org-get-effort (&optional pom)
9907 "Get the effort estimate for the current entry."
9908 (org-entry-get pom org-effort-property))
9912 (defvar org-create-file-search-functions nil
9913 "List of functions to construct the right search string for a file link.
9914 These functions are called in turn with point at the location to
9915 which the link should point.
9917 A function in the hook should first test if it would like to
9918 handle this file type, for example by checking the `major-mode'
9919 or the file extension. If it decides not to handle this file, it
9920 should just return nil to give other functions a chance. If it
9921 does handle the file, it must return the search string to be used
9922 when following the link. The search string will be part of the
9923 file link, given after a double colon, and `org-open-at-point'
9924 will automatically search for it. If special measures must be
9925 taken to make the search successful, another function should be
9926 added to the companion hook `org-execute-file-search-functions',
9929 A function in this hook may also use `setq' to set the variable
9930 `description' to provide a suggestion for the descriptive text to
9931 be used for this link when it gets inserted into an Org-mode
9932 buffer with \\[org-insert-link].")
9934 (defvar org-execute-file-search-functions nil
9935 "List of functions to execute a file search triggered by a link.
9937 Functions added to this hook must accept a single argument, the
9938 search string that was part of the file link, the part after the
9939 double colon. The function must first check if it would like to
9940 handle this search, for example by checking the `major-mode' or
9941 the file extension. If it decides not to handle this search, it
9942 should just return nil to give other functions a chance. If it
9943 does handle the search, it must return a non-nil value to keep
9944 other functions from trying.
9946 Each function can access the current prefix argument through the
9947 variable `current-prefix-argument'. Note that a single prefix is
9948 used to force opening a link in Emacs, so it may be good to only
9949 use a numeric or double prefix to guide the search function.
9951 In case this is needed, a function in this hook can also restore
9952 the window configuration before `org-open-at-point' was called using:
9954 (set-window-configuration org-window-config-before-follow-link)")
9956 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
9957 (defun org-link-search (s &optional type avoid-pos stealth)
9958 "Search for a link search option.
9959 If S is surrounded by forward slashes, it is interpreted as a
9960 regular expression. In org-mode files, this will create an `org-occur'
9961 sparse tree. In ordinary files, `occur' will be used to list matches.
9962 If the current buffer is in `dired-mode', grep will be used to search
9963 in all files. If AVOID-POS is given, ignore matches near that position.
9965 When optional argument STEALTH is non-nil, do not modify
9966 visibility around point, thus ignoring
9967 `org-show-hierarchy-above', `org-show-following-heading' and
9968 `org-show-siblings' variables."
9969 (let ((case-fold-search t)
9970 (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
9971 (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
9972 (append '(("") (" ") ("\t") ("\n"))
9976 (pre nil) (post nil)
9977 words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
9979 ;; First check if there are any special search functions
9980 ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
9981 ;; Now try the builtin stuff
9982 ((and (equal (string-to-char s0) ?#)
9985 (goto-char (point-min))
9988 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
9989 (setq type 'dedicated
9990 pos (match-beginning 0))))
9991 ;; There is an exact target for this
9993 (org-back-to-heading t)))
9995 (goto-char (point-min))
9998 (concat "<<" (regexp-quote s0) ">>") nil t)
9999 (setq type 'dedicated
10000 pos (match-beginning 0))))
10001 ;; There is an exact target for this
10004 (goto-char (point-min))
10007 (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
10008 (setq type 'dedicated pos (match-beginning 0))))
10009 ;; Found an invisible target.
10012 (goto-char (point-min))
10015 (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
10016 (setq type 'dedicated pos (match-beginning 0))))
10017 ;; Found an element with a matching #+name affiliated keyword.
10019 ((and (string-match "^(\\(.*\\))$" s0)
10021 (goto-char (point-min))
10024 (concat "[^[]" (regexp-quote
10025 (format org-coderef-label-format
10026 (match-string 1 s0))))
10028 (setq type 'dedicated
10029 pos (1+ (match-beginning 0))))))
10030 ;; There is a coderef target for this
10032 ((string-match "^/\\(.*\\)/$" s)
10033 ;; A regular expression
10035 ((derived-mode-p 'org-mode)
10036 (org-occur (match-string 1 s)))
10037 ;;((eq major-mode 'dired-mode)
10038 ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
10039 (t (org-do-occur (match-string 1 s)))))
10040 ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
10041 (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
10042 (goto-char (point-min))
10044 ((let (case-fold-search)
10045 (re-search-forward (format org-complex-heading-regexp-format
10048 ;; OK, found a match
10049 (setq type 'dedicated)
10050 (goto-char (match-beginning 0)))
10051 ((and (not org-link-search-inhibit-query)
10052 (eq org-link-search-must-match-exact-headline 'query-to-create)
10053 (y-or-n-p "No match - create this as a new heading? "))
10054 (goto-char (point-max))
10055 (or (bolp) (newline))
10056 (insert "* " s "\n")
10057 (beginning-of-line 0))
10060 (error "No match"))))
10062 ;; A normal search string
10063 (when (equal (string-to-char s) ?*)
10064 ;; Anchor on headlines, post may include tags.
10065 (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
10066 post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
10067 s (substring s 1)))
10068 (remove-text-properties
10070 '(face nil mouse-face nil keymap nil fontified nil) s)
10071 ;; Make a series of regular expressions to find a match
10072 (setq words (org-split-string s "[ \n\r\t]+")
10074 re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
10075 re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
10077 re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
10078 re2a (concat "[ \t\r\n]" re2a_)
10079 re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
10080 re4 (concat "[^a-zA-Z_]" re4_)
10082 re1 (concat pre re2 post)
10083 re3 (concat pre (if pre re4_ re4) post)
10084 re5 (concat pre ".*" re4)
10085 re2 (concat pre re2)
10086 re2a (concat pre (if pre re2a_ re2a))
10087 re4 (concat pre (if pre re4_ re4))
10088 reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
10089 "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
10093 ((eq type 'org-occur) (org-occur reall))
10094 ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
10095 (t (goto-char (point-min))
10097 (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
10098 (org-search-not-self 1 re1 nil t)
10099 (org-search-not-self 1 re2 nil t)
10100 (org-search-not-self 1 re2a nil t)
10101 (org-search-not-self 1 re3 nil t)
10102 (org-search-not-self 1 re4 nil t)
10103 (org-search-not-self 1 re5 nil t)
10105 (goto-char (match-beginning 1))
10107 (error "No match"))))))
10108 (and (derived-mode-p 'org-mode)
10110 (org-show-context 'link-search))
10113 (defun org-search-not-self (group &rest args)
10114 "Execute `re-search-forward', but only accept matches that do not
10115 enclose the position of `org-open-link-marker'."
10116 (let ((m org-open-link-marker))
10118 (while (apply 're-search-forward args)
10119 (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
10120 (goto-char (match-end group))
10121 (if (and (or (not (eq (marker-buffer m) (current-buffer)))
10122 (> (match-beginning 0) (marker-position m))
10123 (< (match-end 0) (marker-position m)))
10125 (or (not (org-in-regexp
10126 org-bracket-link-analytic-regexp 1))
10127 (not (match-end 4)) ; no description
10128 (and (<= (match-beginning 4) (point))
10129 (>= (match-end 4) (point))))))
10130 (throw 'exit (point))))))))
10132 (defun org-get-buffer-for-internal-link (buffer)
10133 "Return a buffer to be used for displaying the link target of internal links."
10135 ((not org-display-internal-link-with-indirect-buffer)
10137 ((string-match "(Clone)$" (buffer-name buffer))
10138 (message "Buffer is already a clone, not making another one")
10139 ;; we also do not modify visibility in this case
10141 (t ; make a new indirect buffer for displaying the link
10142 (let* ((bn (buffer-name buffer))
10143 (ibn (concat bn "(Clone)"))
10144 (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
10145 (with-current-buffer ib (org-overview))
10148 (defun org-do-occur (regexp &optional cleanup)
10149 "Call the Emacs command `occur'.
10150 If CLEANUP is non-nil, remove the printout of the regular expression
10151 in the *Occur* buffer. This is useful if the regex is long and not useful
10155 (let ((cwin (selected-window)) win beg end)
10156 (when (setq win (get-buffer-window "*Occur*"))
10157 (select-window win))
10158 (goto-char (point-min))
10159 (when (re-search-forward "match[a-z]+" nil t)
10160 (setq beg (match-end 0))
10161 (if (re-search-forward "^[ \t]*[0-9]+" nil t)
10162 (setq end (1- (match-beginning 0)))))
10163 (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
10164 (goto-char (point-min))
10165 (select-window cwin))))
10167 ;;; The mark ring for links jumps
10169 (defvar org-mark-ring nil
10170 "Mark ring for positions before jumps in Org-mode.")
10171 (defvar org-mark-ring-last-goto nil
10172 "Last position in the mark ring used to go back.")
10173 ;; Fill and close the ring
10174 (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
10175 (loop for i from 1 to org-mark-ring-length do
10176 (push (make-marker) org-mark-ring))
10177 (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
10180 (defun org-mark-ring-push (&optional pos buffer)
10181 "Put the current position or POS into the mark ring and rotate it."
10183 (setq pos (or pos (point)))
10184 (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
10185 (move-marker (car org-mark-ring)
10187 (or buffer (current-buffer)))
10189 (substitute-command-keys
10190 "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
10192 (defun org-mark-ring-goto (&optional n)
10193 "Jump to the previous position in the mark ring.
10194 With prefix arg N, jump back that many stored positions. When
10195 called several times in succession, walk through the entire ring.
10196 Org-mode commands jumping to a different position in the current file,
10197 or to another Org-mode file, automatically push the old position
10201 (if (eq last-command this-command)
10202 (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
10203 (setq p org-mark-ring))
10204 (setq org-mark-ring-last-goto p)
10206 (org-pop-to-buffer-same-window (marker-buffer m))
10208 (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
10210 (defun org-remove-angle-brackets (s)
10211 (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
10212 (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
10214 (defun org-add-angle-brackets (s)
10215 (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
10216 (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
10218 (defun org-remove-double-quotes (s)
10219 (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
10220 (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
10223 ;;; Following specific links
10225 (defun org-follow-timestamp-link ()
10226 "Open an agenda view for the time-stamp date/range at point."
10228 ((org-at-date-range-p t)
10229 (let ((org-agenda-start-on-weekday)
10230 (t1 (match-string 1))
10231 (t2 (match-string 2)) tt1 tt2)
10232 (setq tt1 (time-to-days (org-time-string-to-time t1))
10233 tt2 (time-to-days (org-time-string-to-time t2)))
10234 (let ((org-agenda-buffer-tmp-name
10235 (format "*Org Agenda(a:%s)"
10236 (concat (substring t1 0 10) "--" (substring t2 0 10)))))
10237 (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
10238 ((org-at-timestamp-p t)
10239 (let ((org-agenda-buffer-tmp-name
10240 (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
10241 (org-agenda-list nil (time-to-days (org-time-string-to-time
10242 (substring (match-string 1) 0 10)))
10244 (t (error "This should not happen"))))
10247 ;;; Following file links
10248 (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
10249 (declare-function mailcap-extension-to-mime "mailcap" (extn))
10250 (declare-function mailcap-mime-info
10251 "mailcap" (string &optional request no-decode))
10252 (defvar org-wait nil)
10253 (defun org-open-file (path &optional in-emacs line search)
10254 "Open the file at PATH.
10255 First, this expands any special file name abbreviations. Then the
10256 configuration variable `org-file-apps' is checked if it contains an
10257 entry for this file type, and if yes, the corresponding command is launched.
10259 If no application is found, Emacs simply visits the file.
10261 With optional prefix argument IN-EMACS, Emacs will visit the file.
10262 With a double \\[universal-argument] \\[universal-argument] \
10263 prefix arg, Org tries to avoid opening in Emacs
10264 and to use an external application to visit the file.
10266 Optional LINE specifies a line to go to, optional SEARCH a string
10267 to search for. If LINE or SEARCH is given, the file will be
10268 opened in Emacs, unless an entry from org-file-apps that makes
10269 use of groups in a regexp matches.
10271 If you want to change the way frames are used when following a
10272 link, please customize `org-link-frame-setup'.
10274 If the file does not exist, an error is thrown."
10275 (let* ((file (if (equal path "")
10277 (substitute-in-file-name (expand-file-name path))))
10278 (file-apps (append org-file-apps (org-default-apps)))
10279 (apps (org-remove-if
10280 'org-file-apps-entry-match-against-dlink-p file-apps))
10281 (apps-dlink (org-remove-if-not
10282 'org-file-apps-entry-match-against-dlink-p file-apps))
10283 (remp (and (assq 'remote apps) (org-file-remote-p file)))
10284 (dirp (if remp nil (file-directory-p file)))
10285 (file (if (and dirp org-open-directory-means-index-dot-org)
10286 (concat (file-name-as-directory file) "index.org")
10288 (a-m-a-p (assq 'auto-mode apps))
10289 (dfile (downcase file))
10290 ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
10291 (link (cond ((and (eq line nil)
10295 (concat file "::" (number-to-string line)))
10297 (concat file "::" search))))
10298 (dlink (downcase link))
10299 (old-buffer (current-buffer))
10301 (old-mode major-mode)
10302 ext cmd link-match-data)
10303 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
10304 (setq ext (match-string 1 dfile))
10305 (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
10306 (setq ext (match-string 1 dfile))))
10308 ((member in-emacs '((16) system))
10309 (setq cmd (cdr (assoc 'system apps))))
10310 (in-emacs (setq cmd 'emacs))
10312 (setq cmd (or (and remp (cdr (assoc 'remote apps)))
10313 (and dirp (cdr (assoc 'directory apps)))
10314 ; first, try matching against apps-dlink
10315 ; if we get a match here, store the match data for later
10316 (let ((match (assoc-default dlink apps-dlink
10319 (progn (setq link-match-data (match-data))
10321 (progn (setq in-emacs (or in-emacs line search))
10322 nil))) ; if we have no match in apps-dlink,
10323 ; always open the file in emacs if line or search
10324 ; is given (for backwards compatibility)
10325 (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
10327 (cdr (assoc ext apps))
10328 (cdr (assoc t apps))))))
10329 (when (eq cmd 'system)
10330 (setq cmd (cdr (assoc 'system apps))))
10331 (when (eq cmd 'default)
10332 (setq cmd (cdr (assoc t apps))))
10333 (when (eq cmd 'mailcap)
10335 (mailcap-parse-mailcaps)
10336 (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
10337 (command (mailcap-mime-info mime-type)))
10338 (if (stringp command)
10340 (setq cmd 'emacs))))
10341 (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
10342 (not (file-exists-p file))
10343 (not org-open-non-existing-files))
10344 (error "No such file: %s" file))
10346 ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
10347 ;; Remove quotes around the file name - we'll use shell-quote-argument.
10348 (while (string-match "['\"]%s['\"]" cmd)
10349 (setq cmd (replace-match "%s" t t cmd)))
10350 (while (string-match "%s" cmd)
10351 (setq cmd (replace-match
10353 (shell-quote-argument
10354 (convert-standard-filename file)))
10357 ;; Replace "%1", "%2" etc. in command with group matches from regex
10359 (let ((match-index 1)
10360 (number-of-groups (- (/ (length link-match-data) 2) 1)))
10361 (set-match-data link-match-data)
10362 (while (<= match-index number-of-groups)
10363 (let ((regex (concat "%" (number-to-string match-index)))
10364 (replace-with (match-string match-index dlink)))
10365 (while (string-match regex cmd)
10366 (setq cmd (replace-match replace-with t t cmd))))
10367 (setq match-index (+ match-index 1)))))
10369 (save-window-excursion
10370 (start-process-shell-command cmd nil cmd)
10371 (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
10375 (funcall (cdr (assq 'file org-link-frame-setup)) file)
10377 (if line (org-goto-line line)
10378 (if search (org-link-search search))))
10380 (let ((file (convert-standard-filename file)))
10382 (set-match-data link-match-data)
10384 (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
10385 (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
10386 (or (not (equal old-buffer (current-buffer)))
10387 (not (equal old-pos (point))))
10388 (org-mark-ring-push old-pos old-buffer))))
10390 (defun org-file-apps-entry-match-against-dlink-p (entry)
10391 "This function returns non-nil if `entry' uses a regular
10392 expression which should be matched against the whole link by
10395 It assumes that is the case when the entry uses a regular
10396 expression which has at least one grouping construct and the
10397 action is either a lisp form or a command string containing
10398 '%1', i.e. using at least one subexpression match as a
10400 (let ((selector (car entry))
10401 (action (cdr entry)))
10402 (if (stringp selector)
10403 (and (> (regexp-opt-depth selector) 0)
10404 (or (and (stringp action)
10405 (string-match "%[0-9]" action))
10409 (defun org-default-apps ()
10410 "Return the default applications for this operating system."
10412 ((eq system-type 'darwin)
10413 org-file-apps-defaults-macosx)
10414 ((eq system-type 'windows-nt)
10415 org-file-apps-defaults-windowsnt)
10416 (t org-file-apps-defaults-gnu)))
10418 (defun org-apps-regexp-alist (list &optional add-auto-mode)
10419 "Convert extensions to regular expressions in the cars of LIST.
10420 Also, weed out any non-string entries, because the return value is used
10421 only for regexp matching.
10422 When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
10423 point to the symbol `emacs', indicating that the file should
10424 be opened in Emacs."
10427 (mapcar (lambda (x)
10428 (if (not (stringp (car x)))
10430 (if (string-match "\\W" (car x))
10432 (cons (concat "\\." (car x) "\\'") (cdr x)))))
10435 (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
10437 (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
10438 (defun org-file-remote-p (file)
10439 "Test whether FILE specifies a location on a remote system.
10440 Return non-nil if the location is indeed remote.
10442 For example, the filename \"/user@host:/foo\" specifies a location
10443 on the system \"/user@host:\"."
10444 (cond ((fboundp 'file-remote-p)
10445 (file-remote-p file))
10446 ((fboundp 'tramp-handle-file-remote-p)
10447 (tramp-handle-file-remote-p file))
10448 ((and (boundp 'ange-ftp-name-format)
10449 (string-match (car ange-ftp-name-format) file))
10455 (defun org-get-org-file ()
10456 "Read a filename, with default directory `org-directory'."
10457 (let ((default (or org-default-notes-file remember-data-file)))
10458 (read-file-name (format "File name [%s]: " default)
10459 (file-name-as-directory org-directory)
10462 (defun org-notes-order-reversed-p ()
10463 "Check if the current file should receive notes in reversed order."
10465 ((not org-reverse-note-order) nil)
10466 ((eq t org-reverse-note-order) t)
10467 ((not (listp org-reverse-note-order)) nil)
10469 (let ((all org-reverse-note-order)
10471 (while (setq entry (pop all))
10472 (if (string-match (car entry) buffer-file-name)
10473 (throw 'exit (cdr entry))))
10476 (defvar org-refile-target-table nil
10477 "The list of refile targets, created by `org-refile'.")
10479 (defvar org-agenda-new-buffers nil
10480 "Buffers created to visit agenda files.")
10482 (defvar org-refile-cache nil
10483 "Cache for refile targets.")
10485 (defvar org-refile-markers nil
10486 "All the markers used for caching refile locations.")
10488 (defun org-refile-marker (pos)
10489 "Get a new refile marker, but only if caching is in use."
10490 (if (not org-refile-use-cache)
10492 (let ((m (make-marker)))
10493 (move-marker m pos)
10494 (push m org-refile-markers)
10497 (defun org-refile-cache-clear ()
10498 "Clear the refile cache and disable all the markers."
10499 (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
10500 (setq org-refile-markers nil)
10501 (setq org-refile-cache nil)
10502 (message "Refile cache has been cleared"))
10504 (defun org-refile-cache-check-set (set)
10505 "Check if all the markers in the cache still have live buffers."
10508 (while (and set (setq marker (nth 3 (pop set))))
10509 ;; if org-refile-use-outline-path is 'file, marker may be nil
10510 (when (and marker (null (marker-buffer marker)))
10511 (message "not found") (sit-for 3)
10512 (throw 'exit nil)))
10515 (defun org-refile-cache-put (set &rest identifiers)
10516 "Push the refile targets SET into the cache, under IDENTIFIERS."
10517 (let* ((key (sha1 (prin1-to-string identifiers)))
10518 (entry (assoc key org-refile-cache)))
10521 (push (cons key set) org-refile-cache))))
10523 (defun org-refile-cache-get (&rest identifiers)
10524 "Retrieve the cached value for refile targets given by IDENTIFIERS."
10526 ((not org-refile-cache) nil)
10527 ((not org-refile-use-cache) (org-refile-cache-clear) nil)
10529 (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
10530 org-refile-cache))))
10531 (and set (org-refile-cache-check-set set) set)))))
10533 (defun org-refile-get-targets (&optional default-buffer excluded-entries)
10534 "Produce a table with refile targets."
10535 (let ((case-fold-search nil)
10536 ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
10537 (entries (or org-refile-targets '((nil . (:level . 1)))))
10538 targets tgs txt re files f desc descre fast-path-p level pos0)
10539 (message "Getting targets...")
10540 (with-current-buffer (or default-buffer (current-buffer))
10541 (while (setq entry (pop entries))
10542 (setq files (car entry) desc (cdr entry))
10543 (setq fast-path-p nil)
10545 ((null files) (setq files (list (current-buffer))))
10546 ((eq files 'org-agenda-files)
10547 (setq files (org-agenda-files 'unrestricted)))
10548 ((and (symbolp files) (fboundp files))
10549 (setq files (funcall files)))
10550 ((and (symbolp files) (boundp files))
10551 (setq files (symbol-value files))))
10552 (if (stringp files) (setq files (list files)))
10554 ((eq (car desc) :tag)
10555 (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
10556 ((eq (car desc) :todo)
10557 (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
10558 ((eq (car desc) :regexp)
10559 (setq descre (cdr desc)))
10560 ((eq (car desc) :level)
10561 (setq descre (concat "^\\*\\{" (number-to-string
10562 (if org-odd-levels-only
10563 (1- (* 2 (cdr desc)))
10566 ((eq (car desc) :maxlevel)
10567 (setq fast-path-p t)
10568 (setq descre (concat "^\\*\\{1," (number-to-string
10569 (if org-odd-levels-only
10570 (1- (* 2 (cdr desc)))
10573 (t (error "Bad refiling target description %s" desc)))
10574 (while (setq f (pop files))
10575 (with-current-buffer
10576 (if (bufferp f) f (org-get-agenda-file-buffer f))
10578 (setq tgs (org-refile-cache-get (buffer-file-name) descre))
10580 (if (bufferp f) (setq f (buffer-file-name
10581 (buffer-base-buffer f))))
10582 (setq f (and f (expand-file-name f)))
10583 (if (eq org-refile-use-outline-path 'file)
10584 (push (list (file-name-nondirectory f) f nil nil) tgs))
10588 (goto-char (point-min))
10589 (while (re-search-forward descre nil t)
10590 (goto-char (setq pos0 (point-at-bol)))
10592 (when org-refile-target-verify-function
10594 (or (funcall org-refile-target-verify-function)
10596 (when (and (looking-at org-complex-heading-regexp)
10597 (not (member (match-string 4) excluded-entries))
10599 (setq level (org-reduced-level
10600 (- (match-end 1) (match-beginning 1)))
10601 txt (org-link-display-format (match-string 4))
10602 txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
10603 re (format org-complex-heading-regexp-format
10604 (regexp-quote (match-string 4))))
10605 (when org-refile-use-outline-path
10606 (setq txt (mapconcat
10609 (if (eq org-refile-use-outline-path
10611 (list (file-name-nondirectory
10613 (buffer-base-buffer))))
10614 (if (eq org-refile-use-outline-path
10616 (list (buffer-file-name
10617 (buffer-base-buffer)))))
10618 (org-get-outline-path fast-path-p
10622 (push (list txt f re (org-refile-marker (point)))
10624 (when (= (point) pos0)
10625 ;; verification function has not moved point
10626 (goto-char (point-at-eol))))))))
10627 (when org-refile-use-cache
10628 (org-refile-cache-put tgs (buffer-file-name) descre))
10629 (setq targets (append tgs targets))
10631 (message "Getting targets...done")
10632 (nreverse targets)))
10634 (defun org-protect-slash (s)
10635 (while (string-match "/" s)
10636 (setq s (replace-match "\\" t t s)))
10639 (defvar org-olpa (make-vector 20 nil))
10641 (defun org-get-outline-path (&optional fastp level heading)
10642 "Return the outline path to the current entry, as a list.
10644 The parameters FASTP, LEVEL, and HEADING are for use by a scanner
10645 routine which makes outline path derivations for an entire file,
10646 avoiding backtracing. Refile target collection makes use of that."
10650 (error "Outline path failure, more than 19 levels"))
10651 (loop for i from level upto 19 do
10652 (aset org-olpa i nil))
10654 (delq nil (append org-olpa nil))
10655 (aset org-olpa level heading)))
10656 (let (rtn case-fold-search)
10660 (while (org-up-heading-safe)
10661 (when (looking-at org-complex-heading-regexp)
10662 (push (org-match-string-no-properties 4) rtn)))
10665 (defun org-format-outline-path (path &optional width prefix)
10666 "Format the outline path PATH for display.
10667 Width is the maximum number of characters that is available.
10668 Prefix is a prefix to be included in the returned string,
10669 such as the file name."
10670 (setq width (or width 79))
10671 (if prefix (setq width (- width (length prefix))))
10674 (let* ((nsteps (length path))
10675 (total-width (+ nsteps (apply '+ (mapcar 'length path))))
10676 (maxwidth (if (<= total-width width)
10677 10000 ;; everything fits
10678 ;; we need to shorten the level headings
10679 (/ (- width nsteps) nsteps)))
10680 (org-odd-levels-only nil)
10682 (total (1+ (length prefix))))
10683 (setq maxwidth (max maxwidth 10))
10688 (if (and (= n nsteps) (< maxwidth 10000))
10689 (setq maxwidth (- total-width total)))
10690 (if (< (length h) maxwidth)
10691 (progn (setq total (+ total (length h) 1)) h)
10692 (setq h (substring h 0 (- maxwidth 2))
10693 total (+ total maxwidth 1))
10694 (if (string-match "[ \t]+\\'" h)
10695 (setq h (substring h 0 (match-beginning 0))))
10696 (setq h (concat h "..")))
10697 (org-add-props h nil 'face
10698 (nth (% (1- n) org-n-level-faces)
10703 (defun org-display-outline-path (&optional file current)
10704 "Display the current outline path in the echo area."
10706 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
10707 (case-fold-search nil)
10708 (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
10709 (if current (setq path (append path
10711 (org-back-to-heading t)
10712 (if (looking-at org-complex-heading-regexp)
10713 (list (match-string 4)))))))
10715 (org-format-outline-path
10718 (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
10720 (defvar org-refile-history nil
10721 "History for refiling operations.")
10723 (defvar org-after-refile-insert-hook nil
10724 "Hook run after `org-refile' has inserted its stuff at the new location.
10725 Note that this is still *before* the stuff will be removed from
10726 the *old* location.")
10728 (defvar org-capture-last-stored-marker)
10729 (defun org-refile (&optional goto default-buffer rfloc)
10730 "Move the entry or entries at point to another heading.
10731 The list of target headings is compiled using the information in
10732 `org-refile-targets', which see.
10734 At the target location, the entry is filed as a subitem of the target
10735 heading. Depending on `org-reverse-note-order', the new subitem will
10736 either be the first or the last subitem.
10738 If there is an active region, all entries in that region will be moved.
10739 However, the region must fulfill the requirement that the first heading
10740 is the first one sets the top-level of the moved text - at most siblings
10741 below it are allowed.
10743 With prefix arg GOTO, the command will only visit the target location
10744 and not actually move anything.
10746 With a double prefix arg \\[universal-argument] \\[universal-argument], \
10747 go to the location where the last refiling operation has put the subtree.
10748 With a prefix argument of `2', refile to the running clock.
10750 RFLOC can be a refile location obtained in a different way.
10752 See also `org-refile-use-outline-path' and `org-completion-use-ido'.
10754 If you are using target caching (see `org-refile-use-cache'),
10755 you have to clear the target cache in order to find new targets.
10756 This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
10757 prefix argument (`C-u C-u C-u C-c C-w')."
10760 (if (member goto '(0 (64)))
10761 (org-refile-cache-clear)
10762 (let* ((cbuf (current-buffer))
10763 (regionp (org-region-active-p))
10764 (region-start (and regionp (region-beginning)))
10765 (region-end (and regionp (region-end)))
10766 (region-length (and regionp (- region-end region-start)))
10767 (filename (buffer-file-name (buffer-base-buffer cbuf)))
10768 pos it nbuf file re level reversed)
10769 (setq last-command nil)
10771 (goto-char region-start)
10772 (or (bolp) (goto-char (point-at-bol)))
10773 (setq region-start (point))
10774 (unless (or (org-kill-is-subtree-p
10775 (buffer-substring region-start region-end))
10776 (prog1 org-refile-active-region-within-subtree
10777 (org-toggle-heading)))
10778 (error "The region is not a (sequence of) subtree(s)")))
10779 (if (equal goto '(16))
10780 (org-refile-goto-last-stored)
10782 (and (equal goto 2)
10783 org-clock-hd-marker (marker-buffer org-clock-hd-marker)
10785 (setq it (list (or org-clock-heading "running clock")
10787 (marker-buffer org-clock-hd-marker))
10789 (marker-position org-clock-hd-marker)))
10792 (let (heading-text)
10795 (org-back-to-heading t)
10797 (nth 4 (org-heading-components))))
10798 (org-refile-get-location
10799 (cond (goto "Goto")
10800 (regionp "Refile region to")
10801 (t (concat "Refile subtree \""
10802 heading-text "\" to")))
10804 (and (not (equal '(4) goto))
10805 org-refile-allow-creating-parent-nodes)
10807 (setq file (nth 1 it)
10810 (if (and (not goto)
10812 (equal (buffer-file-name) file)
10814 (and (>= pos region-start)
10815 (<= pos region-end))
10816 (and (>= pos (point))
10817 (< pos (save-excursion
10818 (org-end-of-subtree t t))))))
10819 (error "Cannot refile to position inside the tree or region"))
10821 (setq nbuf (or (find-buffer-visiting file)
10822 (find-file-noselect file)))
10825 (org-pop-to-buffer-same-window nbuf)
10827 (org-show-context 'org-goto))
10830 (org-kill-new (buffer-substring region-start region-end))
10831 (org-save-markers-in-region region-start region-end))
10832 (org-copy-subtree 1 nil t))
10833 (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
10834 (find-file-noselect file)))
10835 (setq reversed (org-notes-order-reversed-p))
10842 (looking-at org-outline-regexp)
10843 (setq level (org-get-valid-level (funcall outline-level) 1))
10846 (or (outline-next-heading) (point-max))
10847 (or (save-excursion (org-get-next-sibling))
10848 (org-end-of-subtree t t)
10852 (goto-char (point-max))
10853 (goto-char (point-min))
10854 (or (outline-next-heading) (goto-char (point-max)))))
10855 (if (not (bolp)) (newline))
10856 (org-paste-subtree level)
10857 (when org-log-refile
10858 (org-add-log-setup 'refile nil nil 'findpos
10860 (unless (eq org-log-refile 'note)
10861 (save-excursion (org-add-log-note))))
10862 (and org-auto-align-tags
10863 (let ((org-loop-over-headlines-in-active-region nil))
10864 (org-set-tags nil t)))
10865 (bookmark-set "org-refile-last-stored")
10866 ;; If we are refiling for capture, make sure that the
10867 ;; last-capture pointers point here
10868 (when (org-bound-and-true-p org-refile-for-capture)
10869 (bookmark-set "org-capture-last-stored-marker")
10870 (move-marker org-capture-last-stored-marker (point)))
10871 (if (fboundp 'deactivate-mark) (deactivate-mark))
10872 (run-hooks 'org-after-refile-insert-hook))))
10874 (delete-region (point) (+ (point) region-length))
10876 (when (featurep 'org-inlinetask)
10877 (org-inlinetask-remove-END-maybe))
10878 (setq org-markers-to-move nil)
10879 (message "Refiled to \"%s\" in file %s" (car it) file)))))))
10881 (defun org-refile-goto-last-stored ()
10882 "Go to the location where the last refile was stored."
10884 (bookmark-jump "org-refile-last-stored")
10885 (message "This is the location of the last refile"))
10887 (defun org-refile-get-location (&optional prompt default-buffer new-nodes
10889 "Prompt the user for a refile location, using PROMPT.
10890 PROMPT should not be suffixed with a colon and a space, because
10891 this function appends the default value from
10892 `org-refile-history' automatically, if that is not empty.
10893 When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
10894 this is used for the GOTO interface."
10895 (let ((org-refile-targets org-refile-targets)
10896 (org-refile-use-outline-path org-refile-use-outline-path)
10898 (when (and (derived-mode-p 'org-mode)
10899 (not org-refile-use-cache)
10903 (setq excluded-entries
10904 (append excluded-entries (list (org-get-heading t t)))))))
10905 (setq org-refile-target-table
10906 (org-refile-get-targets default-buffer excluded-entries)))
10907 (unless org-refile-target-table
10908 (error "No refile targets"))
10909 (let* ((prompt (concat prompt
10910 (and (car org-refile-history)
10911 (concat " (default " (car org-refile-history) ")"))
10913 (cbuf (current-buffer))
10914 (partial-completion-mode nil)
10915 (cfn (buffer-file-name (buffer-base-buffer cbuf)))
10916 (cfunc (if (and org-refile-use-outline-path
10917 org-outline-path-complete-in-steps)
10918 'org-olpath-completing-read
10919 'org-icompleting-read))
10920 (extra (if org-refile-use-outline-path "/" ""))
10921 (filename (and cfn (expand-file-name cfn)))
10924 (if (and (not (member org-refile-use-outline-path
10925 '(file full-file-path)))
10926 (not (equal filename (nth 1 x))))
10927 (cons (concat (car x) extra " ("
10928 (file-name-nondirectory (nth 1 x)) ")")
10930 (cons (concat (car x) extra) (cdr x))))
10931 org-refile-target-table))
10932 (completion-ignore-case t)
10933 pa answ parent-target child parent old-hist)
10934 (setq old-hist org-refile-history)
10935 (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
10936 nil 'org-refile-history (car org-refile-history)))
10937 (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
10938 (org-refile-check-position pa)
10941 (when (or (not org-refile-history)
10942 (not (eq old-hist org-refile-history))
10943 (not (equal (car pa) (car org-refile-history))))
10944 (setq org-refile-history
10945 (cons (car pa) (if (assoc (car org-refile-history) tbl)
10947 (cdr org-refile-history))))
10948 (if (equal (car org-refile-history) (nth 1 org-refile-history))
10949 (pop org-refile-history)))
10951 (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
10953 (setq parent (match-string 1 answ)
10954 child (match-string 2 answ))
10955 (setq parent-target (or (assoc parent tbl)
10956 (assoc (concat parent "/") tbl)))
10957 (when (and parent-target
10958 (or (eq new-nodes t)
10959 (and (eq new-nodes 'confirm)
10960 (y-or-n-p (format "Create new node \"%s\"? "
10962 (org-refile-new-child parent-target child)))
10963 (error "Invalid target location")))))
10965 (declare-function org-string-nw-p "org-macs.el" (s))
10966 (defun org-refile-check-position (refile-pointer)
10967 "Check if the refile pointer matches the readline to which it points."
10968 (let* ((file (nth 1 refile-pointer))
10969 (re (nth 2 refile-pointer))
10970 (pos (nth 3 refile-pointer))
10972 (when (org-string-nw-p re)
10973 (setq buffer (if (markerp pos)
10974 (marker-buffer pos)
10975 (or (find-buffer-visiting file)
10976 (find-file-noselect file))))
10977 (with-current-buffer buffer
10982 (beginning-of-line 1)
10983 (unless (org-looking-at-p re)
10984 (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
10986 (defun org-refile-new-child (parent-target child)
10987 "Use refile target PARENT-TARGET to add new CHILD below it."
10988 (unless parent-target
10989 (error "Cannot find parent for new node"))
10990 (let ((file (nth 1 parent-target))
10991 (pos (nth 3 parent-target))
10993 (with-current-buffer (or (find-buffer-visiting file)
10994 (find-file-noselect file))
11000 (goto-char (point-max))
11001 (if (not (bolp)) (newline)))
11002 (when (looking-at org-outline-regexp)
11003 (setq level (funcall outline-level))
11004 (org-end-of-subtree t t))
11005 (org-back-over-empty-lines)
11006 (insert "\n" (make-string
11007 (if pos (org-get-valid-level level 1) 1) ?*)
11009 (beginning-of-line 0)
11010 (list (concat (car parent-target) "/" child) file "" (point)))))))
11012 (defun org-olpath-completing-read (prompt collection &rest args)
11013 "Read an outline path like a file name."
11014 (let ((thetable collection)
11015 (org-completion-use-ido nil) ; does not work with ido.
11016 (org-completion-use-iswitchb nil)) ; or iswitchb
11018 'org-icompleting-read prompt
11019 (lambda (string predicate &optional flag)
11020 (let (rtn r f (l (length string)))
11024 (try-completion string thetable))
11027 (setq rtn (all-completions string thetable predicate))
11030 (setq r (substring x l))
11031 (if (string-match " ([^)]*)$" x)
11032 (setq f (match-string 0 x))
11034 (if (string-match "/" r)
11035 (concat string (substring r 0 (match-end 0)) f)
11040 (assoc string thetable)))))
11043 ;;;; Dynamic blocks
11045 (defun org-find-dblock (name)
11046 "Find the first dynamic block with name NAME in the buffer.
11047 If not found, stay at current position and return nil."
11048 (let ((case-fold-search t) pos)
11050 (goto-char (point-min))
11051 (setq pos (and (re-search-forward
11052 (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
11053 (match-beginning 0))))
11054 (if pos (goto-char pos))
11057 (defconst org-dblock-start-re
11058 "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
11059 "Matches the start line of a dynamic block, with parameters.")
11061 (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
11062 "Matches the end of a dynamic block.")
11064 (defun org-create-dblock (plist)
11065 "Create a dynamic block section, with parameters taken from PLIST.
11066 PLIST must contain a :name entry which is used as name of the block."
11067 (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
11070 (let ((col (current-column))
11071 (name (plist-get plist :name)))
11072 (insert "#+BEGIN: " name)
11074 (if (eq (car plist) :name)
11075 (setq plist (cddr plist))
11076 (insert " " (prin1-to-string (pop plist)))))
11077 (insert "\n\n" (make-string col ?\ ) "#+END:\n")
11078 (beginning-of-line -2)))
11080 (defun org-prepare-dblock ()
11081 "Prepare dynamic block for refresh.
11082 This empties the block, puts the cursor at the insert position and returns
11083 the property list including an extra property :name with the block name."
11084 (unless (looking-at org-dblock-start-re)
11085 (error "Not at a dynamic block"))
11086 (let* ((begdel (1+ (match-end 0)))
11087 (name (org-no-properties (match-string 1)))
11088 (params (append (list :name name)
11089 (read (concat "(" (match-string 3) ")")))))
11091 (beginning-of-line 1)
11092 (skip-chars-forward " \t")
11093 (setq params (plist-put params :indentation-column (current-column))))
11094 (unless (re-search-forward org-dblock-end-re nil t)
11095 (error "Dynamic block not terminated"))
11098 (list :content (buffer-substring
11099 begdel (match-beginning 0)))))
11100 (delete-region begdel (match-beginning 0))
11105 (defun org-map-dblocks (&optional command)
11106 "Apply COMMAND to all dynamic blocks in the current buffer.
11107 If COMMAND is not given, use `org-update-dblock'."
11108 (let ((cmd (or command 'org-update-dblock)))
11110 (goto-char (point-min))
11111 (while (re-search-forward org-dblock-start-re nil t)
11112 (goto-char (match-beginning 0))
11114 (condition-case nil
11116 (error (message "Error during update of dynamic block"))))
11117 (unless (re-search-forward org-dblock-end-re nil t)
11118 (error "Dynamic block not terminated"))))))
11120 (defun org-dblock-update (&optional arg)
11121 "User command for updating dynamic blocks.
11122 Update the dynamic block at point. With prefix ARG, update all dynamic
11123 blocks in the buffer."
11126 (org-update-all-dblocks)
11127 (or (looking-at org-dblock-start-re)
11128 (org-beginning-of-dblock))
11129 (org-update-dblock)))
11131 (defun org-update-dblock ()
11132 "Update the dynamic block at point.
11133 This means to empty the block, parse for parameters and then call
11134 the correct writing function."
11136 (save-window-excursion
11137 (let* ((pos (point))
11138 (line (org-current-line))
11139 (params (org-prepare-dblock))
11140 (name (plist-get params :name))
11141 (indent (plist-get params :indentation-column))
11142 (cmd (intern (concat "org-dblock-write:" name))))
11143 (message "Updating dynamic block `%s' at line %d..." name line)
11144 (funcall cmd params)
11145 (message "Updating dynamic block `%s' at line %d...done" name line)
11147 (when (and indent (> indent 0))
11148 (setq indent (make-string indent ?\ ))
11150 (org-beginning-of-dblock)
11152 (while (not (looking-at org-dblock-end-re))
11154 (beginning-of-line 2))
11155 (when (looking-at org-dblock-end-re)
11156 (and (looking-at "[ \t]+")
11157 (replace-match ""))
11158 (insert indent)))))))
11160 (defun org-beginning-of-dblock ()
11161 "Find the beginning of the dynamic block at point.
11162 Error if there is no such block at point."
11163 (let ((pos (point))
11166 (if (and (re-search-backward org-dblock-start-re nil t)
11167 (setq beg (match-beginning 0))
11168 (re-search-forward org-dblock-end-re nil t)
11169 (> (match-end 0) pos))
11172 (error "Not in a dynamic block"))))
11174 (defun org-update-all-dblocks ()
11175 "Update all dynamic blocks in the buffer.
11176 This function can be used in a hook."
11178 (when (derived-mode-p 'org-mode)
11179 (org-map-dblocks 'org-update-dblock)))
11184 (defconst org-additional-option-like-keywords
11185 '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
11186 "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
11187 "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
11188 "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
11190 "ORGTBL" "TBLFM:" "TBLNAME:"
11191 "BEGIN_EXAMPLE" "END_EXAMPLE"
11192 "BEGIN_VERBATIM" "END_VERBATIM"
11193 "BEGIN_QUOTE" "END_QUOTE"
11194 "BEGIN_VERSE" "END_VERSE"
11195 "BEGIN_CENTER" "END_CENTER"
11196 "BEGIN_SRC" "END_SRC"
11197 "BEGIN_RESULT" "END_RESULT"
11198 "BEGIN_lstlisting" "END_lstlisting"
11200 "HEADER:" "HEADERS:"
11201 "COLUMNS:" "PROPERTY:"
11202 "CAPTION:" "LABEL:"
11208 (defconst org-options-keywords
11209 '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
11210 "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
11211 "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
11212 "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
11213 "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
11214 "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
11215 "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
11217 (defconst org-additional-option-like-keywords-for-flyspell
11220 (mapconcat (lambda(k)
11221 (replace-regexp-in-string
11223 (concat k " " (downcase k) " " (upcase k))))
11224 (append org-options-keywords org-additional-option-like-keywords)
11228 (defcustom org-structure-template-alist
11230 ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
11231 "<src lang=\"?\">\n\n</src>")
11232 ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
11233 "<example>\n?\n</example>")
11234 ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
11235 "<quote>\n?\n</quote>")
11236 ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
11237 "<verse>\n?\n</verse>")
11238 ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
11239 "<center>\n?\n</center>")
11240 ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
11241 "<literal style=\"latex\">\n?\n</literal>")
11243 "<literal style=\"latex\">?</literal>")
11244 ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
11245 "<literal style=\"html\">\n?\n</literal>")
11247 "<literal style=\"html\">?</literal>")
11248 ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
11252 ("I" "#+INCLUDE: %file ?"
11253 "<include file=%file markup=\"?\">")
11255 "Structure completion elements.
11256 This is a list of abbreviation keys and values. The value gets inserted
11257 if you type `<' followed by the key and then press the completion key,
11258 usually `M-TAB'. %file will be replaced by a file name after prompting
11259 for the file using completion. The cursor will be placed at the position
11260 of the `?` in the template.
11261 There are two templates for each key, the first uses the original Org syntax,
11262 the second uses Emacs Muse-like syntax tags. These Muse-like tags become
11263 the default when the /org-mtags.el/ module has been loaded. See also the
11264 variable `org-mtags-prefer-muse-templates'."
11265 :group 'org-completion
11267 (string :tag "Key")
11268 (string :tag "Template")
11269 (string :tag "Muse Template")))
11271 (defun org-try-structure-completion ()
11272 "Try to complete a structure template before point.
11273 This looks for strings like \"<e\" on an otherwise empty line and
11275 (let ((l (buffer-substring (point-at-bol) (point)))
11277 (when (and (looking-at "[ \t]*$")
11278 (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
11279 (setq a (assoc (match-string 1 l) org-structure-template-alist)))
11280 (org-complete-expand-structure-template (+ -1 (point-at-bol)
11281 (match-beginning 1)) a)
11284 (defun org-complete-expand-structure-template (start cell)
11285 "Expand a structure template."
11286 (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
11287 (rpl (nth (if musep 2 1) cell))
11289 (delete-region start (point))
11290 (when (string-match "\\`#\\+" rpl)
11293 ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
11294 (setq ind (buffer-substring (point-at-bol) (point))))
11296 (setq start (point))
11297 (if (string-match "%file" rpl)
11298 (setq rpl (replace-match
11302 (abbreviate-file-name (read-file-name "Include file: ")))
11305 (setq rpl (mapconcat 'identity (split-string rpl "\n")
11306 (concat "\n" ind)))
11308 (if (re-search-backward "\\?" start t) (delete-char 1))))
11310 ;;;; TODO, DEADLINE, Comments
11312 (defun org-toggle-comment ()
11313 "Change the COMMENT state of an entry."
11316 (org-back-to-heading)
11317 (let (case-fold-search)
11319 ((looking-at (format org-heading-keyword-regexp-format
11320 org-comment-string))
11321 (goto-char (match-end 1))
11322 (looking-at (concat " +" org-comment-string))
11323 (replace-match "" t t)
11324 (when (eolp) (insert " ")))
11325 ((looking-at org-outline-regexp)
11326 (goto-char (match-end 0))
11327 (insert org-comment-string " "))))))
11329 (defvar org-last-todo-state-is-todo nil
11330 "This is non-nil when the last TODO state change led to a TODO state.
11331 If the last change removed the TODO tag or switched to DONE, then
11334 (defvar org-setting-tags nil) ; dynamically skipped
11336 (defvar org-todo-setup-filter-hook nil
11337 "Hook for functions that pre-filter todo specs.
11338 Each function takes a todo spec and returns either nil or the spec
11339 transformed into canonical form." )
11341 (defvar org-todo-get-default-hook nil
11342 "Hook for functions that get a default item for todo.
11343 Each function takes arguments (NEW-MARK OLD-MARK) and returns either
11344 nil or a string to be used for the todo mark." )
11346 (defvar org-agenda-headline-snapshot-before-repeat)
11348 (defun org-current-effective-time ()
11349 "Return current time adjusted for `org-extend-today-until' variable."
11350 (let* ((ct (org-current-time))
11351 (dct (decode-time ct))
11353 (if (and org-use-effective-time
11354 (< (nth 2 dct) org-extend-today-until))
11355 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
11359 (defun org-todo-yesterday (&optional arg)
11360 "Like `org-todo' but the time of change will be 23:59 of yesterday."
11362 (if (eq major-mode 'org-agenda-mode)
11363 (apply 'org-agenda-todo-yesterday arg)
11364 (let* ((hour (third (decode-time
11365 (org-current-time))))
11366 (org-extend-today-until (1+ hour)))
11369 (defun org-todo (&optional arg)
11370 "Change the TODO state of an item.
11371 The state of an item is given by a keyword at the start of the heading,
11373 *** TODO Write paper
11376 The different keywords are specified in the variable `org-todo-keywords'.
11377 By default the available states are \"TODO\" and \"DONE\".
11378 So for this example: when the item starts with TODO, it is changed to DONE.
11379 When it starts with DONE, the DONE is removed. And when neither TODO nor
11380 DONE are present, add TODO at the beginning of the heading.
11382 With \\[universal-argument] prefix arg, use completion to determine the new \
11384 With numeric prefix arg, switch to that state.
11385 With a double \\[universal-argument] prefix, switch to the next set of TODO \
11386 keywords (nextset).
11387 With a triple \\[universal-argument] prefix, circumvent any state blocking.
11388 With a numeric prefix arg of 0, inhibit note taking for the change.
11390 For calling through lisp, arg is also interpreted in the following way:
11391 'none -> empty state
11392 \"\"(empty string) -> switch to empty state
11393 'done -> switch to DONE
11394 'nextset -> switch to the next set of keywords
11395 'previousset -> switch to the previous set of keywords
11396 \"WAITING\" -> switch to the specified keyword, but only if it
11397 really is a member of `org-todo-keywords'."
11399 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
11400 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
11401 'region-start-level 'region))
11402 org-loop-over-headlines-in-active-region)
11405 org-loop-over-headlines-in-active-region
11406 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
11407 (if (equal arg '(16)) (setq arg 'nextset))
11408 (let ((org-blocker-hook org-blocker-hook)
11409 (case-fold-search nil))
11410 (when (equal arg '(64))
11411 (setq arg nil org-blocker-hook nil))
11412 (when (and org-blocker-hook
11413 (or org-inhibit-blocking
11414 (org-entry-get nil "NOBLOCKING")))
11415 (setq org-blocker-hook nil))
11418 (org-back-to-heading t)
11419 (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
11420 (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
11421 (looking-at "\\(?: *\\|[ \t]*$\\)"))
11422 (let* ((match-data (match-data))
11423 (startpos (point-at-bol))
11424 (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
11425 (org-log-done org-log-done)
11426 (org-log-repeat org-log-repeat)
11427 (org-todo-log-states org-todo-log-states)
11428 (org-inhibit-logging
11430 (progn (setq arg nil) 'note) org-inhibit-logging))
11431 (this (match-string 1))
11432 (hl-pos (match-beginning 0))
11433 (head (org-get-todo-sequence-head this))
11434 (ass (assoc head org-todo-kwd-alist))
11435 (interpret (nth 1 ass))
11436 (done-word (nth 3 ass))
11437 (final-done-word (nth 4 ass))
11438 (org-last-state (or this ""))
11439 (completion-ignore-case t)
11440 (member (member this org-todo-keywords-1))
11441 (tail (cdr member))
11443 ((and org-todo-key-trigger
11444 (or (and (equal arg '(4))
11445 (eq org-use-fast-todo-selection 'prefix))
11446 (and (not arg) org-use-fast-todo-selection
11447 (not (eq org-use-fast-todo-selection
11449 ;; Use fast selection
11450 (org-fast-todo-selection))
11451 ((and (equal arg '(4))
11452 (or (not org-use-fast-todo-selection)
11453 (not org-todo-key-trigger)))
11454 ;; Read a state with completion
11455 (org-icompleting-read
11456 "State: " (mapcar (lambda(x) (list x))
11457 org-todo-keywords-1)
11461 (if tail (car tail) nil)
11462 (car org-todo-keywords-1)))
11464 (if (equal member org-todo-keywords-1)
11467 (nth (- (length org-todo-keywords-1)
11469 org-todo-keywords-1)
11470 (org-last org-todo-keywords-1))))
11471 ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
11472 (setq arg nil))) ; hack to fall back to cycling
11474 ;; user or caller requests a specific state
11476 ((equal arg "") nil)
11477 ((eq arg 'none) nil)
11478 ((eq arg 'done) (or done-word (car org-done-keywords)))
11480 (or (car (cdr (member head org-todo-heads)))
11481 (car org-todo-heads)))
11482 ((eq arg 'previousset)
11483 (let ((org-todo-heads (reverse org-todo-heads)))
11484 (or (car (cdr (member head org-todo-heads)))
11485 (car org-todo-heads))))
11486 ((car (member arg org-todo-keywords-1)))
11488 (error "State `%s' not valid in this file" arg))
11489 ((nth (1- (prefix-numeric-value arg))
11490 org-todo-keywords-1))))
11491 ((null member) (or head (car org-todo-keywords-1)))
11492 ((equal this final-done-word) nil) ;; -> make empty
11493 ((null tail) nil) ;; -> first entry
11494 ((memq interpret '(type priority))
11495 (if (eq this-command last-command)
11497 (if (> (length tail) 0)
11498 (or done-word (car org-done-keywords))
11503 (run-hook-with-args-until-success
11504 'org-todo-get-default-hook org-state org-last-state)
11506 (next (if org-state (concat " " org-state " ") " "))
11507 (change-plist (list :type 'todo-state-change :from this :to org-state
11508 :position startpos))
11510 (when org-blocker-hook
11511 (setq org-last-todo-state-is-todo
11512 (not (member this org-done-keywords)))
11513 (unless (save-excursion
11515 (org-with-wide-buffer
11516 (run-hook-with-args-until-failure
11517 'org-blocker-hook change-plist))))
11518 (if (org-called-interactively-p 'interactive)
11519 (error "TODO state change from %s to %s blocked" this org-state)
11521 (message "TODO state change from %s to %s blocked" this org-state)
11522 (throw 'exit nil))))
11523 (store-match-data match-data)
11524 (replace-match next t t)
11525 (unless (pos-visible-in-window-p hl-pos)
11526 (message "TODO state changed to %s" (org-trim next)))
11528 (setq head (org-get-todo-sequence-head org-state)
11529 ass (assoc head org-todo-kwd-alist)
11530 interpret (nth 1 ass)
11531 done-word (nth 3 ass)
11532 final-done-word (nth 4 ass)))
11533 (when (memq arg '(nextset previousset))
11534 (message "Keyword-Set %d/%d: %s"
11535 (- (length org-todo-sets) -1
11536 (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
11537 (length org-todo-sets)
11538 (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
11539 (setq org-last-todo-state-is-todo
11540 (not (member org-state org-done-keywords)))
11541 (setq now-done-p (and (member org-state org-done-keywords)
11542 (not (member this org-done-keywords))))
11543 (and logging (org-local-logging logging))
11544 (when (and (or org-todo-log-states org-log-done)
11545 (not (eq org-inhibit-logging t))
11546 (not (memq arg '(nextset previousset))))
11547 ;; we need to look at recording a time and note
11548 (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
11549 (nth 2 (assoc this org-todo-log-states))))
11550 (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
11551 (setq dolog 'time))
11552 (when (and org-state
11553 (member org-state org-not-done-keywords)
11554 (not (member this org-not-done-keywords)))
11555 ;; This is now a todo state and was not one before
11556 ;; If there was a CLOSED time stamp, get rid of it.
11557 (org-add-planning-info nil nil 'closed))
11558 (when (and now-done-p org-log-done)
11559 ;; It is now done, and it was not done before
11560 (org-add-planning-info 'closed (org-current-effective-time))
11561 (if (and (not dolog) (eq 'note org-log-done))
11562 (org-add-log-setup 'done org-state this 'findpos 'note)))
11563 (when (and org-state dolog)
11564 ;; This is a non-nil state, and we need to log it
11565 (org-add-log-setup 'state org-state this 'findpos dolog)))
11566 ;; Fixup tag positioning
11567 (org-todo-trigger-tag-changes org-state)
11568 (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
11569 (when org-provide-todo-statistics
11570 (org-update-parent-todo-statistics))
11571 (run-hooks 'org-after-todo-state-change-hook)
11572 (if (and arg (not (member org-state org-done-keywords)))
11573 (setq head (org-get-todo-sequence-head org-state)))
11574 (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
11575 ;; Do we need to trigger a repeat?
11577 (when (boundp 'org-agenda-headline-snapshot-before-repeat)
11578 ;; This is for the agenda, take a snapshot of the headline.
11580 (setq org-agenda-headline-snapshot-before-repeat
11581 (org-get-heading))))
11582 (org-auto-repeat-maybe org-state))
11583 ;; Fixup cursor location if close to the keyword
11584 (if (and (outline-on-heading-p)
11586 (save-excursion (beginning-of-line 1)
11587 (looking-at org-todo-line-regexp))
11588 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
11590 (goto-char (or (match-end 2) (match-end 1)))
11591 (and (looking-at " ") (just-one-space))))
11592 (when org-trigger-hook
11594 (run-hook-with-args 'org-trigger-hook change-plist)))))))))
11596 (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
11597 "Block turning an entry into a TODO, using the hierarchy.
11598 This checks whether the current task should be blocked from state
11599 changes. Such blocking occurs when:
11601 1. The task has children which are not all in a completed state.
11603 2. A task has a parent with the property :ORDERED:, and there
11604 are siblings prior to the current task with incomplete
11607 3. The parent of the task is blocked because it has siblings that should
11608 be done first, or is child of a block grandparent TODO entry."
11610 (if (not org-enforce-todo-dependencies)
11611 t ; if locally turned off don't block
11613 ;; If this is not a todo state change, or if this entry is already DONE,
11615 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11616 (member (plist-get change-plist :from)
11617 (cons 'done org-done-keywords))
11618 (member (plist-get change-plist :to)
11619 (cons 'todo org-not-done-keywords))
11620 (not (plist-get change-plist :to)))
11621 (throw 'dont-block t))
11622 ;; If this task has children, and any are undone, it's blocked
11624 (org-back-to-heading t)
11625 (let ((this-level (funcall outline-level)))
11626 (outline-next-heading)
11627 (let ((child-level (funcall outline-level)))
11628 (while (and (not (eobp))
11629 (> child-level this-level))
11630 ;; this todo has children, check whether they are all
11632 (if (and (not (org-entry-is-done-p))
11633 (org-entry-is-todo-p))
11634 (throw 'dont-block nil))
11635 (outline-next-heading)
11636 (setq child-level (funcall outline-level))))))
11637 ;; Otherwise, if the task's parent has the :ORDERED: property, and
11638 ;; any previous siblings are undone, it's blocked
11640 (org-back-to-heading t)
11641 (let* ((pos (point))
11642 (parent-pos (and (org-up-heading-safe) (point))))
11643 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11644 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11646 (re-search-forward org-not-done-heading-regexp pos t))
11647 (throw 'dont-block nil)) ; block, there is an older sibling not done.
11648 ;; Search further up the hierarchy, to see if an ancestor is blocked
11650 (goto-char parent-pos)
11651 (if (not (looking-at org-not-done-heading-regexp))
11652 (throw 'dont-block t)) ; do not block, parent is not a TODO
11654 (setq parent-pos (and (org-up-heading-safe) (point)))
11655 (if (not parent-pos) (throw 'dont-block t)) ; no parent
11656 (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
11658 (re-search-forward org-not-done-heading-regexp pos t))
11659 (throw 'dont-block nil)))))))) ; block, older sibling not done.
11661 (defcustom org-track-ordered-property-with-tag nil
11662 "Should the ORDERED property also be shown as a tag?
11663 The ORDERED property decides if an entry should require subtasks to be
11664 completed in sequence. Since a property is not very visible, setting
11665 this option means that toggling the ORDERED property with the command
11666 `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
11667 not relevant for the behavior, but it makes things more visible.
11669 Note that toggling the tag with tags commands will not change the property
11670 and therefore not influence behavior!
11672 This can be t, meaning the tag ORDERED should be used, It can also be a
11673 string to select a different tag for this task."
11676 (const :tag "No tracking" nil)
11677 (const :tag "Track with ORDERED tag" t)
11678 (string :tag "Use other tag")))
11680 (defun org-toggle-ordered-property ()
11681 "Toggle the ORDERED property of the current entry.
11682 For better visibility, you can track the value of this property with a tag.
11683 See variable `org-track-ordered-property-with-tag'."
11685 (let* ((t1 org-track-ordered-property-with-tag)
11686 (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
11688 (org-back-to-heading)
11689 (if (org-entry-get nil "ORDERED")
11691 (org-delete-property "ORDERED")
11692 (and tag (org-toggle-tag tag 'off))
11693 (message "Subtasks can be completed in arbitrary order"))
11694 (org-entry-put nil "ORDERED" "t")
11695 (and tag (org-toggle-tag tag 'on))
11696 (message "Subtasks must be completed in sequence")))))
11698 (defvar org-blocked-by-checkboxes) ; dynamically scoped
11699 (defun org-block-todo-from-checkboxes (change-plist)
11700 "Block turning an entry into a TODO, using checkboxes.
11701 This checks whether the current task should be blocked from state
11702 changes because there are unchecked boxes in this entry."
11703 (if (not org-enforce-todo-checkbox-dependencies)
11704 t ; if locally turned off don't block
11706 ;; If this is not a todo state change, or if this entry is already DONE,
11708 (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
11709 (member (plist-get change-plist :from)
11710 (cons 'done org-done-keywords))
11711 (member (plist-get change-plist :to)
11712 (cons 'todo org-not-done-keywords))
11713 (not (plist-get change-plist :to)))
11714 (throw 'dont-block t))
11715 ;; If this task has checkboxes that are not checked, it's blocked
11717 (org-back-to-heading t)
11718 (let ((beg (point)) end)
11719 (outline-next-heading)
11722 (if (org-list-search-forward
11723 (concat (org-item-beginning-re)
11724 "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
11728 (if (boundp 'org-blocked-by-checkboxes)
11729 (setq org-blocked-by-checkboxes t))
11730 (throw 'dont-block nil)))))
11731 t))) ; do not block
11733 (defun org-entry-blocked-p ()
11734 "Is the current entry blocked?"
11735 (if (org-entry-get nil "NOBLOCKING")
11736 nil ;; Never block this entry
11738 (run-hook-with-args-until-failure
11740 (list :type 'todo-state-change
11745 (defun org-update-statistics-cookies (all)
11746 "Update the statistics cookie, either from TODO or from checkboxes.
11747 This should be called with the cursor in a line with a statistics cookie."
11751 (org-update-checkbox-count 'all)
11752 (org-map-entries 'org-update-parent-todo-statistics))
11753 (if (not (org-at-heading-p))
11754 (org-update-checkbox-count)
11755 (let ((pos (move-marker (make-marker) (point)))
11757 (ignore-errors (org-back-to-heading t))
11758 (if (not (org-at-heading-p))
11759 (org-update-checkbox-count)
11760 (setq l1 (org-outline-level))
11761 (setq end (save-excursion
11762 (outline-next-heading)
11763 (if (org-at-heading-p) (setq l2 (org-outline-level)))
11765 (if (and (save-excursion
11767 "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
11768 (not (save-excursion (re-search-forward
11769 ":COOKIE_DATA:.*\\<todo\\>" end t))))
11770 (org-update-checkbox-count)
11771 (if (and l2 (> l2 l1))
11774 (org-update-parent-todo-statistics))
11776 (beginning-of-line 1)
11777 (while (re-search-forward
11778 "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
11780 (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
11782 (move-marker pos nil)))))
11784 (defvar org-entry-property-inherited-from) ;; defined below
11785 (defun org-update-parent-todo-statistics ()
11786 "Update any statistics cookie in the parent of the current headline.
11787 When `org-hierarchical-todo-statistics' is nil, statistics will cover
11788 the entire subtree and this will travel up the hierarchy and update
11789 statistics everywhere."
11790 (let* ((prop (save-excursion (org-up-heading-safe)
11791 (org-entry-get nil "COOKIE_DATA" 'inherit)))
11792 (recursive (or (not org-hierarchical-todo-statistics)
11793 (and prop (string-match "\\<recursive\\>" prop))))
11794 (lim (or (and prop (marker-position org-entry-property-inherited-from))
11797 (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
11798 level ltoggle l1 new ndel
11799 (cnt-all 0) (cnt-done 0) is-percent kwd
11800 checkbox-beg ov ovs ove cookie-present)
11803 (beginning-of-line 1)
11804 (setq ltoggle (funcall outline-level))
11805 ;; Three situations are to consider:
11807 ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
11808 ;; to the top-level ancestor on the headline;
11810 ;; 2. If parent has "recursive" property, repeat up to the
11811 ;; headline setting that property, taking inheritance into
11814 ;; 3. Else, move up to direct parent and proceed only once.
11815 (while (and (setq level (org-up-heading-safe))
11816 (or recursive first)
11818 (setq first nil cookie-present nil)
11822 (downcase (or (org-entry-get nil "COOKIE_DATA")
11825 (while (re-search-forward box-re (point-at-eol) t)
11826 (setq cnt-all 0 cnt-done 0 cookie-present t)
11827 (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
11829 (unless (outline-next-heading) (throw 'exit nil))
11830 (while (and (looking-at org-complex-heading-regexp)
11831 (> (setq l1 (length (match-string 1))) level))
11832 (setq kwd (and (or recursive (= l1 ltoggle))
11834 (if (or (eq org-provide-todo-statistics 'all-headlines)
11835 (and (listp org-provide-todo-statistics)
11836 (or (member kwd org-provide-todo-statistics)
11837 (member kwd org-done-keywords))))
11838 (setq cnt-all (1+ cnt-all))
11839 (if (eq org-provide-todo-statistics t)
11840 (and kwd (setq cnt-all (1+ cnt-all)))))
11841 (and (member kwd org-done-keywords)
11842 (setq cnt-done (1+ cnt-done)))
11843 (outline-next-heading)))
11846 (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
11847 (format "[%d/%d]" cnt-done cnt-all))
11848 ndel (- (match-end 0) checkbox-beg))
11849 ;; handle overlays when updating cookie from column view
11850 (when (setq ov (car (overlays-at checkbox-beg)))
11851 (setq ovs (overlay-start ov) ove (overlay-end ov))
11852 (delete-overlay ov))
11853 (goto-char checkbox-beg)
11855 (delete-region (point) (+ (point) ndel))
11856 (when org-auto-align-tags (org-fix-tags-on-the-fly))
11857 (when ov (move-overlay ov ovs ove)))
11858 (when cookie-present
11859 (run-hook-with-args 'org-after-todo-statistics-hook
11860 cnt-done (- cnt-all cnt-done))))))
11861 (run-hooks 'org-todo-statistics-hook)))
11863 (defvar org-after-todo-statistics-hook nil
11864 "Hook that is called after a TODO statistics cookie has been updated.
11865 Each function is called with two arguments: the number of not-done entries
11866 and the number of done entries.
11868 For example, the following function, when added to this hook, will switch
11869 an entry to DONE when all children are done, and back to TODO when new
11870 entries are set to a TODO status. Note that this hook is only called
11871 when there is a statistics cookie in the headline!
11873 (defun org-summary-todo (n-done n-not-done)
11874 \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
11875 (let (org-log-done org-log-states) ; turn off logging
11876 (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
11879 (defvar org-todo-statistics-hook nil
11880 "Hook that is run whenever Org thinks TODO statistics should be updated.
11881 This hook runs even if there is no statistics cookie present, in which case
11882 `org-after-todo-statistics-hook' would not run.")
11884 (defun org-todo-trigger-tag-changes (state)
11885 "Apply the changes defined in `org-todo-state-tags-triggers'."
11886 (let ((l org-todo-state-tags-triggers)
11888 (when (or (not state) (equal state ""))
11889 (setq changes (append changes (cdr (assoc "" l)))))
11890 (when (and (stringp state) (> (length state) 0))
11891 (setq changes (append changes (cdr (assoc state l)))))
11892 (when (member state org-not-done-keywords)
11893 (setq changes (append changes (cdr (assoc 'todo l)))))
11894 (when (member state org-done-keywords)
11895 (setq changes (append changes (cdr (assoc 'done l)))))
11896 (dolist (c changes)
11897 (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
11899 (defun org-local-logging (value)
11900 "Get logging settings from a property VALUE."
11902 ;; directly set the variables, they are already local.
11903 (setq org-log-done nil
11905 org-todo-log-states nil)
11906 (setq words (org-split-string value))
11907 (while (setq w (pop words))
11909 ((setq a (assoc w org-startup-options))
11910 (and (member (nth 1 a) '(org-log-done org-log-repeat))
11911 (set (nth 1 a) (nth 2 a))))
11912 ((setq a (org-extract-log-state-settings w))
11913 (and (member (car a) org-todo-keywords-1)
11914 (push a org-todo-log-states)))))))
11916 (defun org-get-todo-sequence-head (kwd)
11917 "Return the head of the TODO sequence to which KWD belongs.
11918 If KWD is not set, check if there is a text property remembering the
11923 (or (get-text-property (point-at-bol) 'org-todo-head)
11925 (setq p (next-single-property-change (point-at-bol) 'org-todo-head
11926 nil (point-at-eol)))
11927 (get-text-property p 'org-todo-head))))
11928 ((not (member kwd org-todo-keywords-1))
11929 (car org-todo-keywords-1))
11930 (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
11932 (defun org-fast-todo-selection ()
11933 "Fast TODO keyword selection with single keys.
11934 Returns the new TODO keyword, or nil if no state change should occur."
11935 (let* ((fulltable org-todo-key-alist)
11936 (done-keywords org-done-keywords) ;; needed for the faces.
11937 (maxlen (apply 'max (mapcar
11939 (if (stringp (car x)) (string-width (car x)) 0))
11942 (fwidth (+ maxlen 3 1 3))
11943 (ncol (/ (- (window-width) 4) fwidth))
11947 (save-window-excursion
11949 (set-buffer (get-buffer-create " *Org todo*"))
11950 (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
11952 (org-set-local 'org-done-keywords done-keywords)
11953 (setq tbl fulltable cnt 0)
11954 (while (setq e (pop tbl))
11956 ((equal e '(:startgroup))
11957 (push '() groups) (setq ingroup t)
11958 (when (not (= cnt 0))
11962 ((equal e '(:endgroup))
11963 (setq ingroup nil cnt 0)
11965 ((equal e '(:newline))
11966 (when (not (= cnt 0))
11970 (while (equal (car tbl) '(:newline))
11972 (setq tbl (cdr tbl)))))
11974 (setq tg (car e) c (cdr e))
11975 (if ingroup (push tg (car groups)))
11976 (setq tg (org-add-props tg nil 'face
11977 (org-get-todo-face tg)))
11978 (if (and (= cnt 0) (not ingroup)) (insert " "))
11979 (insert "[" c "] " tg (make-string
11980 (- fwidth 4 (length tg)) ?\ ))
11981 (when (= (setq cnt (1+ cnt)) ncol)
11983 (if ingroup (insert " "))
11986 (goto-char (point-min))
11987 (if (not expert) (org-fit-window-to-buffer))
11988 (message "[a-z..]:Set [SPC]:clear")
11989 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
11992 (and (= c ?q) (not (rassoc c fulltable))))
11993 (setq quit-flag t))
11995 ((setq e (rassoc c fulltable) tg (car e))
11997 (t (setq quit-flag t)))))))
11999 (defun org-entry-is-todo-p ()
12000 (member (org-get-todo-state) org-not-done-keywords))
12002 (defun org-entry-is-done-p ()
12003 (member (org-get-todo-state) org-done-keywords))
12005 (defun org-get-todo-state ()
12007 (org-back-to-heading t)
12008 (and (looking-at org-todo-line-regexp)
12010 (match-string 2))))
12012 (defun org-at-date-range-p (&optional inactive-ok)
12013 "Is the cursor inside a date range?"
12017 (let ((pos (point)))
12018 (skip-chars-backward "^[<\r\n")
12019 (skip-chars-backward "<[")
12020 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12021 (>= (match-end 0) pos)
12023 (skip-chars-backward "^<[\r\n")
12024 (skip-chars-backward "<[")
12025 (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
12026 (>= (match-end 0) pos)
12030 (defun org-get-repeat (&optional tagline)
12031 "Check if there is a deadline/schedule with repeater in this entry."
12034 (org-back-to-heading t)
12035 (and (re-search-forward (if tagline
12036 (concat tagline "\\s-*" org-repeat-re)
12038 (org-entry-end-position) t)
12039 (match-string-no-properties 1)))))
12041 (defvar org-last-changed-timestamp)
12042 (defvar org-last-inserted-timestamp)
12043 (defvar org-log-post-message)
12044 (defvar org-log-note-purpose)
12045 (defvar org-log-note-how)
12046 (defvar org-log-note-extra)
12047 (defun org-auto-repeat-maybe (done-word)
12048 "Check if the current headline contains a repeated deadline/schedule.
12049 If yes, set TODO state back to what it was and change the base date
12050 of repeating deadline/scheduled time stamps to new date.
12051 This function is run automatically after each state change to a DONE state."
12052 ;; last-state is dynamically scoped into this function
12053 (let* ((repeat (org-get-repeat))
12054 (aa (assoc org-last-state org-todo-kwd-alist))
12055 (interpret (nth 1 aa))
12057 (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
12058 (msg "Entry repeats: ")
12060 (org-todo-log-states nil)
12061 re type n what ts time to-state)
12063 (if (eq org-log-repeat t) (setq org-log-repeat 'state))
12064 (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
12065 org-todo-repeat-to-state))
12066 (unless (and to-state (member to-state org-todo-keywords-1))
12067 (setq to-state (if (eq interpret 'type) org-last-state head)))
12068 (org-todo to-state)
12069 (when (or org-log-repeat (org-entry-get nil "CLOCK"))
12070 (org-entry-put nil "LAST_REPEAT" (format-time-string
12071 (org-time-stamp-format t t))))
12072 (when org-log-repeat
12073 (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
12074 (memq 'org-add-log-note post-command-hook))
12075 ;; OK, we are already setup for some record
12076 (if (eq org-log-repeat 'note)
12077 ;; make sure we take a note, not only a time stamp
12078 (setq org-log-note-how 'note))
12079 ;; Set up for taking a record
12080 (org-add-log-setup 'state (or done-word (car org-done-keywords))
12082 'findpos org-log-repeat)))
12083 (org-back-to-heading t)
12084 (org-add-planning-info nil nil 'closed)
12085 (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
12086 org-deadline-time-regexp "\\)\\|\\("
12087 org-ts-regexp "\\)"))
12088 (while (re-search-forward
12089 re (save-excursion (outline-next-heading) (point)) t)
12090 (setq type (if (match-end 1) org-scheduled-string
12091 (if (match-end 3) org-deadline-string "Plain:"))
12092 ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
12093 (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
12094 (setq n (string-to-number (match-string 2 ts))
12095 what (match-string 3 ts))
12096 (if (equal what "w") (setq n (* n 7) what "d"))
12097 (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
12098 (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
12099 ;; Preparation, see if we need to modify the start date for the change
12100 (when (match-end 1)
12101 (setq time (save-match-data (org-time-string-to-time ts)))
12103 ((equal (match-string 1 ts) ".")
12104 ;; Shift starting date to today
12105 (org-timestamp-change
12106 (- (org-today) (time-to-days time))
12108 ((equal (match-string 1 ts) "+")
12109 (let ((nshiftmax 10) (nshift 0))
12110 (while (or (= nshift 0)
12111 (<= (time-to-days time)
12112 (time-to-days (current-time))))
12113 (when (= (incf nshift) nshiftmax)
12114 (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
12116 (org-timestamp-change n (cdr (assoc what whata)))
12117 (org-at-timestamp-p t)
12118 (setq ts (match-string 1))
12119 (setq time (save-match-data (org-time-string-to-time ts)))))
12120 (org-timestamp-change (- n) (cdr (assoc what whata)))
12121 ;; rematch, so that we have everything in place for the real shift
12122 (org-at-timestamp-p t)
12123 (setq ts (match-string 1))
12124 (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
12125 (org-timestamp-change n (cdr (assoc what whata)))
12126 (setq msg (concat msg type " " org-last-changed-timestamp " "))))
12127 (setq org-log-post-message msg)
12128 (message "%s" msg))))
12130 (defun org-show-todo-tree (arg)
12131 "Make a compact tree which shows all headlines marked with TODO.
12132 The tree will show the lines where the regexp matches, and all higher
12133 headlines above the match.
12134 With a \\[universal-argument] prefix, prompt for a regexp to match.
12135 With a numeric prefix N, construct a sparse tree for the Nth element
12136 of `org-todo-keywords-1'."
12138 (let ((case-fold-search nil)
12140 (cond ((null arg) org-not-done-regexp)
12142 (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
12143 (mapcar 'list org-todo-keywords-1))))
12145 (mapconcat 'identity (org-split-string kwd "|") "\\|")
12147 ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
12148 (regexp-quote (nth (1- (prefix-numeric-value arg))
12149 org-todo-keywords-1)))
12150 (t (error "Invalid prefix argument: %s" arg)))))
12151 (message "%d TODO entries found"
12152 (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
12154 (defun org-deadline (&optional remove time)
12155 "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
12156 With argument REMOVE, remove any deadline from the item.
12157 With argument TIME, set the deadline at the corresponding date. TIME
12158 can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12160 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12161 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12162 'region-start-level 'region))
12163 org-loop-over-headlines-in-active-region)
12165 `(org-deadline ',remove ,time)
12166 org-loop-over-headlines-in-active-region
12167 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12168 (let* ((old-date (org-entry-get nil "DEADLINE"))
12169 (repeater (and old-date
12171 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12173 (match-string 1 old-date))))
12176 (when (and old-date org-log-redeadline)
12177 (org-add-log-setup 'deldeadline nil old-date 'findpos
12178 org-log-redeadline))
12179 (org-remove-timestamp-with-keyword org-deadline-string)
12180 (message "Item no longer has a deadline."))
12181 (org-add-planning-info 'deadline time 'closed)
12182 (when (and old-date org-log-redeadline
12183 (not (equal old-date
12184 (substring org-last-inserted-timestamp 1 -1))))
12185 (org-add-log-setup 'redeadline nil old-date 'findpos
12186 org-log-redeadline))
12189 (org-back-to-heading t)
12190 (when (re-search-forward (concat org-deadline-string " "
12191 org-last-inserted-timestamp)
12193 (outline-next-heading) (point)) t)
12194 (goto-char (1- (match-end 0)))
12195 (insert " " repeater)
12196 (setq org-last-inserted-timestamp
12197 (concat (substring org-last-inserted-timestamp 0 -1)
12199 (substring org-last-inserted-timestamp -1))))))
12200 (message "Deadline on %s" org-last-inserted-timestamp)))))
12202 (defun org-schedule (&optional remove time)
12203 "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
12204 With argument REMOVE, remove any scheduling date from the item.
12205 With argument TIME, scheduled at the corresponding date. TIME can
12206 either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
12208 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
12209 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
12210 'region-start-level 'region))
12211 org-loop-over-headlines-in-active-region)
12213 `(org-schedule ',remove ,time)
12214 org-loop-over-headlines-in-active-region
12215 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
12216 (let* ((old-date (org-entry-get nil "SCHEDULED"))
12217 (repeater (and old-date
12219 "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
12221 (match-string 1 old-date))))
12224 (when (and old-date org-log-reschedule)
12225 (org-add-log-setup 'delschedule nil old-date 'findpos
12226 org-log-reschedule))
12227 (org-remove-timestamp-with-keyword org-scheduled-string)
12228 (message "Item is no longer scheduled."))
12229 (org-add-planning-info 'scheduled time 'closed)
12230 (when (and old-date org-log-reschedule
12231 (not (equal old-date
12232 (substring org-last-inserted-timestamp 1 -1))))
12233 (org-add-log-setup 'reschedule nil old-date 'findpos
12234 org-log-reschedule))
12237 (org-back-to-heading t)
12238 (when (re-search-forward (concat org-scheduled-string " "
12239 org-last-inserted-timestamp)
12241 (outline-next-heading) (point)) t)
12242 (goto-char (1- (match-end 0)))
12243 (insert " " repeater)
12244 (setq org-last-inserted-timestamp
12245 (concat (substring org-last-inserted-timestamp 0 -1)
12247 (substring org-last-inserted-timestamp -1))))))
12248 (message "Scheduled to %s" org-last-inserted-timestamp)))))
12250 (defun org-get-scheduled-time (pom &optional inherit)
12251 "Get the scheduled time as a time tuple, of a format suitable
12252 for calling org-schedule with, or if there is no scheduling,
12254 (let ((time (org-entry-get pom "SCHEDULED" inherit)))
12256 (apply 'encode-time (org-parse-time-string time)))))
12258 (defun org-get-deadline-time (pom &optional inherit)
12259 "Get the deadline as a time tuple, of a format suitable for
12260 calling org-deadline with, or if there is no scheduling, returns
12262 (let ((time (org-entry-get pom "DEADLINE" inherit)))
12264 (apply 'encode-time (org-parse-time-string time)))))
12266 (defun org-remove-timestamp-with-keyword (keyword)
12267 "Remove all time stamps with KEYWORD in the current entry."
12268 (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
12271 (org-back-to-heading t)
12273 (outline-next-heading)
12274 (while (re-search-backward re beg t)
12276 (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
12277 (equal (char-before) ?\ ))
12278 (backward-delete-char 1)
12279 (if (string-match "^[ \t]*$" (buffer-substring
12280 (point-at-bol) (point-at-eol)))
12281 (delete-region (point-at-bol)
12282 (min (point-max) (1+ (point-at-eol))))))))))
12284 (defun org-add-planning-info (what &optional time &rest remove)
12285 "Insert new timestamp with keyword in the line directly after the headline.
12286 WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
12287 If non is given, the user is prompted for a date.
12288 REMOVE indicates what kind of entries to remove. An old WHAT entry will also
12291 (let (org-time-was-given org-end-time-was-given ts
12292 end default-time default-input)
12295 (when (and (memq what '(scheduled deadline))
12297 (and (stringp time)
12298 (string-match "^[-+]+[0-9]" time))))
12299 ;; Try to get a default date/time from existing timestamp
12301 (org-back-to-heading t)
12302 (setq end (save-excursion (outline-next-heading) (point)))
12303 (when (re-search-forward (if (eq what 'scheduled)
12304 org-scheduled-time-regexp
12305 org-deadline-time-regexp)
12307 (setq ts (match-string 1)
12309 (apply 'encode-time (org-parse-time-string ts))
12310 default-input (and ts (org-get-compact-tod ts))))))
12314 ;; This is a string (relative or absolute), set proper date
12315 (apply 'encode-time
12316 (org-read-date-analyze
12317 time default-time (decode-time default-time)))
12318 ;; If necessary, get the time from the user
12319 (or time (org-read-date nil 'to-time nil nil
12320 default-time default-input)))))
12322 (when (and org-insert-labeled-timestamps-at-point
12323 (member what '(scheduled deadline)))
12325 (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
12326 (org-insert-time-stamp time org-time-was-given
12327 nil nil nil (list org-end-time-was-given))
12331 (let (col list elt ts buffer-invisibility-spec)
12332 (org-back-to-heading t)
12333 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
12334 (goto-char (match-end 1))
12335 (setq col (current-column))
12336 (goto-char (match-end 0))
12337 (if (eobp) (insert "\n") (forward-char 1))
12338 (when (and (not what)
12341 org-keyword-time-not-clock-regexp))))
12342 ;; Nothing to add, nothing to remove...... :-)
12344 (if (and (not (looking-at org-outline-regexp))
12345 (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
12347 (not (equal (match-string 1) org-clock-string)))
12348 (narrow-to-region (match-beginning 0) (match-end 0))
12349 (insert-before-markers "\n")
12351 (narrow-to-region (point) (point))
12352 (and org-adapt-indentation (org-indent-to-column col)))
12353 ;; Check if we have to remove something.
12354 (setq list (cons what remove))
12356 (setq elt (pop list))
12357 (when (or (and (eq elt 'scheduled)
12358 (re-search-forward org-scheduled-time-regexp nil t))
12359 (and (eq elt 'deadline)
12360 (re-search-forward org-deadline-time-regexp nil t))
12361 (and (eq elt 'closed)
12362 (re-search-forward org-closed-time-regexp nil t)))
12364 (if (looking-at "--+<[^>]+>") (replace-match ""))))
12365 (and (looking-at "[ \t]+") (replace-match ""))
12366 (and org-adapt-indentation (bolp) (org-indent-to-column col))
12369 (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
12370 (cond ((eq what 'scheduled) org-scheduled-string)
12371 ((eq what 'deadline) org-deadline-string)
12372 ((eq what 'closed) org-closed-string))
12374 (setq ts (org-insert-time-stamp
12376 (or org-time-was-given
12377 (and (eq what 'closed) org-log-done-with-time))
12379 nil nil (list org-end-time-was-given)))
12381 (if (not (or (bolp) (eq (char-before) ?\ )
12382 (memq (char-after) '(32 10))
12385 (goto-char (point-min))
12387 (if (and (looking-at "[ \t]*\n")
12388 (equal (char-before) ?\n))
12389 (delete-region (1- (point)) (point-at-eol)))
12392 (defvar org-log-note-marker (make-marker))
12393 (defvar org-log-note-purpose nil)
12394 (defvar org-log-note-state nil)
12395 (defvar org-log-note-previous-state nil)
12396 (defvar org-log-note-how nil)
12397 (defvar org-log-note-extra nil)
12398 (defvar org-log-note-window-configuration nil)
12399 (defvar org-log-note-return-to (make-marker))
12400 (defvar org-log-note-effective-time nil
12401 "Remembered current time so that dynamically scoped
12402 `org-extend-today-until' affects tha timestamps in state change
12405 (defvar org-log-post-message nil
12406 "Message to be displayed after a log note has been stored.
12407 The auto-repeater uses this.")
12409 (defun org-add-note ()
12410 "Add a note to the current entry.
12411 This is done in the same way as adding a state change note."
12413 (org-add-log-setup 'note nil nil 'findpos nil))
12415 (defvar org-property-end-re)
12416 (defun org-add-log-setup (&optional purpose state prev-state
12418 "Set up the post command hook to take a note.
12419 If this is about to TODO state change, the new state is expected in STATE.
12420 When FINDPOS is non-nil, find the correct position for the note in
12421 the current entry. If not, assume that it can be inserted at point.
12422 HOW is an indicator what kind of note should be created.
12423 EXTRA is additional text that will be inserted into the notes buffer."
12424 (let* ((org-log-into-drawer (org-log-into-drawer))
12425 (drawer (cond ((stringp org-log-into-drawer)
12426 org-log-into-drawer)
12427 (org-log-into-drawer "LOGBOOK"))))
12431 (org-back-to-heading t)
12432 (narrow-to-region (point) (save-excursion
12433 (outline-next-heading) (point)))
12434 (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
12435 "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
12437 (goto-char (match-end 0))
12440 (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
12443 (goto-char (match-end 0))
12444 (or org-log-states-order-reversed
12445 (and (re-search-forward org-property-end-re nil t)
12446 (goto-char (1- (match-beginning 0))))))
12447 (insert "\n:" drawer ":\n:END:")
12448 (beginning-of-line 0)
12450 (beginning-of-line 2)
12453 ((and org-log-state-notes-insert-after-drawers
12455 (forward-line) (looking-at org-drawer-regexp)))
12457 (while (looking-at org-drawer-regexp)
12458 (goto-char (match-end 0))
12459 (re-search-forward org-property-end-re (point-max) t)
12461 (forward-line -1)))
12462 (unless org-log-states-order-reversed
12463 (and (= (char-after) ?\n) (forward-char 1))
12464 (org-skip-over-state-notes)
12465 (skip-chars-backward " \t\n\r")))
12466 (move-marker org-log-note-marker (point))
12467 (setq org-log-note-purpose purpose
12468 org-log-note-state state
12469 org-log-note-previous-state prev-state
12470 org-log-note-how how
12471 org-log-note-extra extra
12472 org-log-note-effective-time (org-current-effective-time))
12473 (add-hook 'post-command-hook 'org-add-log-note 'append)))))
12475 (defun org-skip-over-state-notes ()
12476 "Skip past the list of State notes in an entry."
12477 (if (looking-at "\n[ \t]*- State") (forward-char 1))
12478 (when (ignore-errors (goto-char (org-in-item-p)))
12479 (let* ((struct (org-list-struct))
12480 (prevs (org-list-prevs-alist struct)))
12481 (while (looking-at "[ \t]*- State")
12482 (goto-char (or (org-list-get-next-item (point) struct prevs)
12483 (org-list-get-item-end (point) struct)))))))
12485 (defun org-add-log-note (&optional purpose)
12486 "Pop up a window for taking a note, and add this note later at point."
12487 (remove-hook 'post-command-hook 'org-add-log-note)
12488 (setq org-log-note-window-configuration (current-window-configuration))
12489 (delete-other-windows)
12490 (move-marker org-log-note-return-to (point))
12491 (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
12492 (goto-char org-log-note-marker)
12493 (org-switch-to-buffer-other-window "*Org Note*")
12495 (if (memq org-log-note-how '(time state))
12496 (let (current-prefix-arg) (org-store-log-note))
12497 (let ((org-inhibit-startup t)) (org-mode))
12498 (insert (format "# Insert note for %s.
12499 # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
12501 ((eq org-log-note-purpose 'clock-out) "stopped clock")
12502 ((eq org-log-note-purpose 'done) "closed todo item")
12503 ((eq org-log-note-purpose 'state)
12504 (format "state change from \"%s\" to \"%s\""
12505 (or org-log-note-previous-state "")
12506 (or org-log-note-state "")))
12507 ((eq org-log-note-purpose 'reschedule)
12509 ((eq org-log-note-purpose 'delschedule)
12510 "no longer scheduled")
12511 ((eq org-log-note-purpose 'redeadline)
12512 "changing deadline")
12513 ((eq org-log-note-purpose 'deldeadline)
12514 "removing deadline")
12515 ((eq org-log-note-purpose 'refile)
12517 ((eq org-log-note-purpose 'note)
12519 (t (error "This should not happen")))))
12520 (if org-log-note-extra (insert org-log-note-extra))
12521 (org-set-local 'org-finish-function 'org-store-log-note)
12522 (run-hooks 'org-log-buffer-setup-hook)))
12524 (defvar org-note-abort nil) ; dynamically scoped
12525 (defun org-store-log-note ()
12526 "Finish taking a log note, and insert it to where it belongs."
12527 (let ((txt (buffer-string))
12528 (note (cdr (assq org-log-note-purpose org-log-note-headings)))
12530 (kill-buffer (current-buffer))
12531 (while (string-match "\\`# .*\n[ \t\n]*" txt)
12532 (setq txt (replace-match "" t t txt)))
12533 (if (string-match "\\s-+\\'" txt)
12534 (setq txt (replace-match "" t t txt)))
12535 (setq lines (org-split-string txt "\n"))
12536 (when (and note (string-match "\\S-" note))
12538 (org-replace-escapes
12540 (list (cons "%u" (user-login-name))
12541 (cons "%U" user-full-name)
12542 (cons "%t" (format-time-string
12543 (org-time-stamp-format 'long 'inactive)
12544 org-log-note-effective-time))
12545 (cons "%T" (format-time-string
12546 (org-time-stamp-format 'long nil)
12547 org-log-note-effective-time))
12548 (cons "%d" (format-time-string
12549 (org-time-stamp-format nil 'inactive)
12550 org-log-note-effective-time))
12551 (cons "%D" (format-time-string
12552 (org-time-stamp-format nil nil)
12553 org-log-note-effective-time))
12554 (cons "%s" (if org-log-note-state
12555 (concat "\"" org-log-note-state "\"")
12557 (cons "%S" (if org-log-note-previous-state
12558 (concat "\"" org-log-note-previous-state "\"")
12560 (if lines (setq note (concat note " \\\\")))
12562 (when (or current-prefix-arg org-note-abort)
12563 (when org-log-into-drawer
12564 (org-remove-empty-drawer-at
12565 (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
12566 org-log-note-marker))
12569 (with-current-buffer (marker-buffer org-log-note-marker)
12571 (goto-char org-log-note-marker)
12572 (move-marker org-log-note-marker nil)
12574 (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
12575 (setq ind (save-excursion
12576 (if (ignore-errors (goto-char (org-in-item-p)))
12577 (let ((struct (org-list-struct)))
12579 (org-list-get-top-point struct) struct))
12580 (skip-chars-backward " \r\t\n")
12582 ((and (org-at-heading-p)
12583 org-adapt-indentation)
12584 (1+ (org-current-level)))
12585 ((org-at-heading-p) 0)
12586 (t (org-get-indentation))))))
12587 (setq bul (org-list-bullet-string "-"))
12588 (org-indent-line-to ind)
12589 (insert bul (pop lines))
12590 (let ((ind-body (+ (length bul) ind)))
12593 (org-indent-line-to ind-body)
12594 (insert (pop lines))))
12595 (message "Note stored")
12596 (org-back-to-heading t)
12597 (org-cycle-hide-drawers 'children)))))
12598 (set-window-configuration org-log-note-window-configuration)
12599 (with-current-buffer (marker-buffer org-log-note-return-to)
12600 (goto-char org-log-note-return-to))
12601 (move-marker org-log-note-return-to nil)
12602 (and org-log-post-message (message "%s" org-log-post-message)))
12604 (defun org-remove-empty-drawer-at (drawer pos)
12605 "Remove an empty drawer DRAWER at position POS.
12606 POS may also be a marker."
12607 (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
12613 (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
12614 (replace-match ""))))))
12616 (defvar org-ts-type nil)
12617 (defun org-sparse-tree (&optional arg type)
12618 "Create a sparse tree, prompt for the details.
12619 This command can create sparse trees. You first need to select the type
12620 of match used to create the tree:
12622 t Show all TODO entries.
12623 T Show entries with a specific TODO keyword.
12624 m Show entries selected by a tags/property match.
12625 p Enter a property name and its value (both with completion on existing
12626 names/values) and show entries with that property.
12627 r Show entries matching a regular expression (`/' can be used as well).
12628 b Show deadlines and scheduled items before a date.
12629 a Show deadlines and scheduled items after a date.
12630 d Show deadlines due within `org-deadline-warning-days'.
12631 D Show deadlines and scheduled items between a date range."
12633 (let (ans kwd value ts-type)
12634 (setq type (or type org-sparse-tree-default-date-type))
12635 (setq org-ts-type type)
12636 (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\n [c]ycle through date types: %s"
12637 (cond ((eq type 'all) "all timestamps")
12638 ((eq type 'scheduled) "only scheduled")
12639 ((eq type 'deadline) "only deadline")
12640 ((eq type 'active) "only active timestamps")
12641 ((eq type 'inactive) "only inactive timestamps")
12642 ((eq type 'scheduled-or-deadline) "scheduled/deadline")
12643 (t "scheduled/deadline")))
12644 (setq ans (read-char-exclusive))
12647 (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
12649 (call-interactively 'org-check-deadlines))
12651 (call-interactively 'org-check-before-date))
12653 (call-interactively 'org-check-after-date))
12655 (call-interactively 'org-check-dates-range))
12657 (org-show-todo-tree nil))
12659 (org-show-todo-tree '(4)))
12660 ((member ans '(?T ?m))
12661 (call-interactively 'org-match-sparse-tree))
12662 ((member ans '(?p ?P))
12663 (setq kwd (org-icompleting-read "Property: "
12664 (mapcar 'list (org-buffer-property-keys))))
12665 (setq value (org-icompleting-read "Value: "
12666 (mapcar 'list (org-property-values kwd))))
12667 (unless (string-match "\\`{.*}\\'" value)
12668 (setq value (concat "\"" value "\"")))
12669 (org-match-sparse-tree arg (concat kwd "=" value)))
12670 ((member ans '(?r ?R ?/))
12671 (call-interactively 'org-occur))
12672 (t (error "No such sparse tree command \"%c\"" ans)))))
12674 (defvar org-occur-highlights nil
12675 "List of overlays used for occur matches.")
12676 (make-variable-buffer-local 'org-occur-highlights)
12677 (defvar org-occur-parameters nil
12678 "Parameters of the active org-occur calls.
12679 This is a list, each call to org-occur pushes as cons cell,
12680 containing the regular expression and the callback, onto the list.
12681 The list can contain several entries if `org-occur' has been called
12682 several time with the KEEP-PREVIOUS argument. Otherwise, this list
12683 will only contain one set of parameters. When the highlights are
12684 removed (for example with `C-c C-c', or with the next edit (depending
12685 on `org-remove-highlights-with-change'), this variable is emptied
12687 (make-variable-buffer-local 'org-occur-parameters)
12689 (defun org-occur (regexp &optional keep-previous callback)
12690 "Make a compact tree which shows all matches of REGEXP.
12691 The tree will show the lines where the regexp matches, and all higher
12692 headlines above the match. It will also show the heading after the match,
12693 to make sure editing the matching entry is easy.
12694 If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
12695 call to `org-occur' will be kept, to allow stacking of calls to this
12697 If CALLBACK is non-nil, it is a function which is called to confirm
12698 that the match should indeed be shown."
12699 (interactive "sRegexp: \nP")
12700 (when (equal regexp "")
12701 (error "Regexp cannot be empty"))
12702 (unless keep-previous
12703 (org-remove-occur-highlights nil nil t))
12704 (push (cons regexp callback) org-occur-parameters)
12707 (goto-char (point-min))
12708 (if (or (not keep-previous) ; do not want to keep
12709 (not org-occur-highlights)) ; no previous matches
12712 (while (re-search-forward regexp nil t)
12713 (when (or (not callback)
12714 (save-match-data (funcall callback)))
12715 (setq cnt (1+ cnt))
12716 (when org-highlight-sparse-tree-matches
12717 (org-highlight-new-match (match-beginning 0) (match-end 0)))
12718 (org-show-context 'occur-tree))))
12719 (when org-remove-highlights-with-change
12720 (org-add-hook 'before-change-functions 'org-remove-occur-highlights
12722 (unless org-sparse-tree-open-archived-trees
12723 (org-hide-archived-subtrees (point-min) (point-max)))
12724 (run-hooks 'org-occur-hook)
12725 (if (org-called-interactively-p 'interactive)
12726 (message "%d match(es) for regexp %s" cnt regexp))
12729 (defun org-occur-next-match (&optional n reset)
12730 "Function for `next-error-function' to find sparse tree matches.
12731 N is the number of matches to move, when negative move backwards.
12732 RESET is entirely ignored - this function always goes back to the
12733 starting point when no match is found."
12734 (let* ((limit (if (< n 0) (point-min) (point-max)))
12735 (search-func (if (< n 0)
12736 'previous-single-char-property-change
12737 'next-single-char-property-change))
12742 (while (setq p1 (funcall search-func (point) 'org-type))
12743 (when (equal p1 limit)
12745 (error "No more matches"))
12746 (when (equal (get-char-property p1 'org-type) 'org-occur)
12750 (throw 'exit (point))))
12753 (error "No more matches"))))
12755 (defun org-show-context (&optional key)
12756 "Make sure point and context are visible.
12757 How much context is shown depends upon the variables
12758 `org-show-hierarchy-above', `org-show-following-heading',
12759 `org-show-entry-below' and `org-show-siblings'."
12760 (let ((heading-p (org-at-heading-p t))
12761 (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
12762 (following-p (org-get-alist-option org-show-following-heading key))
12763 (entry-p (org-get-alist-option org-show-entry-below key))
12764 (siblings-p (org-get-alist-option org-show-siblings key)))
12766 ;; Show heading or entry text
12767 (if (and heading-p (not entry-p))
12768 (org-flag-heading nil) ; only show the heading
12769 (and (or entry-p (outline-invisible-p) (org-invisible-p2))
12770 (org-show-hidden-entry))) ; show entire entry
12772 ;; Show next sibling, or heading below text
12774 (and (if heading-p (org-goto-sibling) (outline-next-heading))
12775 (org-flag-heading nil))))
12776 (when siblings-p (org-show-siblings))
12778 ;; show all higher headings, possibly with siblings
12780 (while (and (condition-case nil
12781 (progn (org-up-heading-all 1) t)
12784 (org-flag-heading nil)
12785 (when siblings-p (org-show-siblings))))))))
12787 (defvar org-reveal-start-hook nil
12788 "Hook run before revealing a location.")
12790 (defun org-reveal (&optional siblings)
12791 "Show current entry, hierarchy above it, and the following headline.
12792 This can be used to show a consistent set of context around locations
12793 exposed with `org-show-hierarchy-above' or `org-show-following-heading'
12794 not t for the search context.
12796 With optional argument SIBLINGS, on each level of the hierarchy all
12797 siblings are shown. This repairs the tree structure to what it would
12798 look like when opened with hierarchical calls to `org-cycle'.
12799 With double optional argument \\[universal-argument] \\[universal-argument], \
12800 go to the parent and show the
12803 (run-hooks 'org-reveal-start-hook)
12804 (let ((org-show-hierarchy-above t)
12805 (org-show-following-heading t)
12806 (org-show-siblings (if siblings t org-show-siblings)))
12807 (org-show-context nil))
12808 (when (equal siblings '(16))
12810 (when (org-up-heading-safe)
12812 (run-hook-with-args 'org-cycle-hook 'subtree)))))
12814 (defun org-highlight-new-match (beg end)
12815 "Highlight from BEG to END and mark the highlight is an occur headline."
12816 (let ((ov (make-overlay beg end)))
12817 (overlay-put ov 'face 'secondary-selection)
12818 (overlay-put ov 'org-type 'org-occur)
12819 (push ov org-occur-highlights)))
12821 (defun org-remove-occur-highlights (&optional beg end noremove)
12822 "Remove the occur highlights from the buffer.
12823 BEG and END are ignored. If NOREMOVE is nil, remove this function
12824 from the `before-change-functions' in the current buffer."
12826 (unless org-inhibit-highlight-removal
12827 (mapc 'delete-overlay org-occur-highlights)
12828 (setq org-occur-highlights nil)
12829 (setq org-occur-parameters nil)
12831 (remove-hook 'before-change-functions
12832 'org-remove-occur-highlights 'local))))
12836 (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
12837 "Regular expression matching the priority indicator.")
12839 (defvar org-remove-priority-next-time nil)
12841 (defun org-priority-up ()
12842 "Increase the priority of the current item."
12844 (org-priority 'up))
12846 (defun org-priority-down ()
12847 "Decrease the priority of the current item."
12849 (org-priority 'down))
12851 (defun org-priority (&optional action show)
12852 "Change the priority of an item.
12853 ACTION can be `set', `up', `down', or a character."
12855 (if (equal action '(4))
12856 (org-show-priority)
12857 (unless org-enable-priority-commands
12858 (error "Priority commands are disabled"))
12859 (setq action (or action 'set))
12860 (let (current new news have remove)
12862 (org-back-to-heading t)
12863 (if (looking-at org-priority-regexp)
12864 (setq current (string-to-char (match-string 2))
12867 ((eq action 'remove)
12868 (setq remove t new ?\ ))
12869 ((or (eq action 'set)
12870 (if (featurep 'xemacs) (characterp action) (integerp action)))
12871 (if (not (eq action 'set))
12873 (message "Priority %c-%c, SPC to remove: "
12874 org-highest-priority org-lowest-priority)
12876 (setq new (read-char-exclusive))))
12877 (if (and (= (upcase org-highest-priority) org-highest-priority)
12878 (= (upcase org-lowest-priority) org-lowest-priority))
12879 (setq new (upcase new)))
12880 (cond ((equal new ?\ ) (setq remove t))
12881 ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
12882 (error "Priority must be between `%c' and `%c'"
12883 org-highest-priority org-lowest-priority))))
12886 (1- current) ; normal cycling
12887 ;; last priority was empty
12888 (if (eq last-command this-command)
12889 org-lowest-priority ; wrap around empty to lowest
12891 (if org-priority-start-cycle-with-default
12892 org-default-priority
12893 (1- org-default-priority))))))
12896 (1+ current) ; normal cycling
12897 ;; last priority was empty
12898 (if (eq last-command this-command)
12899 org-highest-priority ; wrap around empty to highest
12901 (if org-priority-start-cycle-with-default
12902 org-default-priority
12903 (1+ org-default-priority))))))
12904 (t (error "Invalid action")))
12905 (if (or (< (upcase new) org-highest-priority)
12906 (> (upcase new) org-lowest-priority))
12907 (if (and (memq action '(up down))
12908 (not have) (not (eq last-command this-command)))
12909 ;; `new' is from default priority
12911 "The default can not be set, see `org-default-priority' why")
12912 ;; normal cycling: `new' is beyond highest/lowest priority
12913 ;; and is wrapped around to the empty priority
12915 (setq news (format "%c" new))
12918 (replace-match "" t t nil 1)
12919 (replace-match news t t nil 2))
12921 (error "No priority cookie found in line")
12922 (let ((case-fold-search nil))
12923 (looking-at org-todo-line-regexp))
12926 (goto-char (match-end 2))
12927 (insert " [#" news "]"))
12928 (goto-char (match-beginning 3))
12929 (insert "[#" news "] "))))
12930 (org-preserve-lc (org-set-tags nil 'align)))
12932 (message "Priority removed")
12933 (message "Priority of current item set to %s" news)))))
12935 (defun org-show-priority ()
12936 "Show the priority of the current item.
12937 This priority is composed of the main priority given with the [#A] cookies,
12938 and by additional input from the age of a schedules or deadline entry."
12940 (let ((pri (if (eq major-mode 'org-agenda-mode)
12941 (org-get-at-bol 'priority)
12944 (beginning-of-line)
12945 (and (looking-at org-heading-regexp)
12946 (org-get-priority (match-string 0))))))))
12947 (message "Priority is %d" (if pri pri -1000))))
12949 (defun org-get-priority (s)
12950 "Find priority cookie and return priority."
12951 (if (functionp org-get-priority-function)
12952 (funcall org-get-priority-function)
12954 (if (not (string-match org-priority-regexp s))
12955 (* 1000 (- org-lowest-priority org-default-priority))
12956 (* 1000 (- org-lowest-priority
12957 (string-to-char (match-string 2 s))))))))
12961 (defvar org-agenda-archives-mode)
12962 (defvar org-map-continue-from nil
12963 "Position from where mapping should continue.
12964 Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
12966 (defvar org-scanner-tags nil
12967 "The current tag list while the tags scanner is running.")
12968 (defvar org-trust-scanner-tags nil
12969 "Should `org-get-tags-at' use the tags for the scanner.
12970 This is for internal dynamical scoping only.
12971 When this is non-nil, the function `org-get-tags-at' will return the value
12972 of `org-scanner-tags' instead of building the list by itself. This
12973 can lead to large speed-ups when the tags scanner is used in a file with
12974 many entries, and when the list of tags is retrieved, for example to
12975 obtain a list of properties. Building the tags list for each entry in such
12976 a file becomes an N^2 operation - but with this variable set, it scales
12979 (defun org-scan-tags (action matcher todo-only &optional start-level)
12980 "Scan headline tags with inheritance and produce output ACTION.
12982 ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
12983 or `agenda' to produce an entry list for an agenda view. It can also be
12984 a Lisp form or a function that should be called at each matched headline, in
12985 this case the return value is a list of all return values from these calls.
12987 MATCHER is a Lisp form to be evaluated, testing if a given set of tags
12988 qualifies a headline for inclusion. When TODO-ONLY is non-nil,
12989 only lines with a not-done TODO keyword are included in the output.
12990 This should be the same variable that was scoped into
12991 and set by `org-make-tags-matcher' when it constructed MATCHER.
12993 START-LEVEL can be a string with asterisks, reducing the scope to
12994 headlines matching this string."
12995 (require 'org-agenda)
12996 (let* ((re (concat "^"
12998 ;; Get the correct level to match
12999 (concat "\\*\\{" (number-to-string start-level) "\\} ")
13000 org-outline-regexp)
13002 (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
13003 (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
13004 (props (list 'face 'default
13005 'done-face 'org-agenda-done
13006 'undone-face 'default
13007 'mouse-face 'highlight
13008 'org-not-done-regexp org-not-done-regexp
13009 'org-todo-regexp org-todo-regexp
13010 'org-complex-heading-regexp org-complex-heading-regexp
13012 (format "mouse-2 or RET jump to org file %s"
13013 (abbreviate-file-name
13014 (or (buffer-file-name (buffer-base-buffer))
13015 (buffer-name (buffer-base-buffer)))))))
13016 (case-fold-search nil)
13017 (org-map-continue-from nil)
13018 lspos tags tags-list
13019 (tags-alist (list (cons 0 org-file-tags)))
13020 (llast 0) rtn rtn1 level category i txt
13021 todo marker entry priority)
13022 (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
13023 (setq action (list 'lambda nil action)))
13025 (goto-char (point-min))
13026 (when (eq action 'sparse-tree)
13028 (org-remove-occur-highlights))
13029 (while (re-search-forward re nil t)
13030 (setq org-map-continue-from nil)
13032 (setq todo (if (match-end 1) (org-match-string-no-properties 2))
13033 tags (if (match-end 4) (org-match-string-no-properties 4)))
13034 (goto-char (setq lspos (match-beginning 0)))
13035 (setq level (org-reduced-level (funcall outline-level))
13036 category (org-get-category))
13037 (setq i llast llast level)
13038 ;; remove tag lists from same and sublevels
13039 (while (>= i level)
13040 (when (setq entry (assoc i tags-alist))
13041 (setq tags-alist (delete entry tags-alist)))
13043 ;; add the next tags
13045 (setq tags (org-split-string tags ":")
13047 (cons (cons level tags) tags-alist)))
13048 ;; compile tags for current headline
13050 (if org-use-tag-inheritance
13051 (apply 'append (mapcar 'cdr (reverse tags-alist)))
13053 org-scanner-tags tags-list)
13054 (when org-use-tag-inheritance
13055 (setcdr (car tags-alist)
13056 (mapcar (lambda (x)
13057 (setq x (copy-sequence x))
13058 (org-add-prop-inherited x))
13059 (cdar tags-alist))))
13060 (when (and tags org-use-tag-inheritance
13061 (or (not (eq t org-use-tag-inheritance))
13062 org-tags-exclude-from-inheritance))
13063 ;; selective inheritance, remove uninherited ones
13064 (setcdr (car tags-alist)
13065 (org-remove-uninherited-tags (cdar tags-alist))))
13068 ;; eval matcher only when the todo condition is OK
13069 (and (or (not todo-only) (member todo org-not-done-keywords))
13070 (let ((case-fold-search t) (org-trust-scanner-tags t))
13073 ;; Call the skipper, but return t if it does not skip,
13074 ;; so that the `and' form continues evaluating
13076 (unless (eq action 'sparse-tree) (org-agenda-skip))
13079 ;; Check if timestamps are deselecting this entry
13080 (or (not todo-only)
13081 (and (member todo org-not-done-keywords)
13082 (or (not org-agenda-tags-todo-honor-ignore-options)
13083 (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
13085 ;; Extra check for the archive tag
13086 ;; FIXME: Does the skipper already do this????
13088 (not (member org-archive-tag tags-list))
13089 ;; we have an archive tag, should we use this anyway?
13090 (or (not org-agenda-skip-archived-trees)
13091 (and (eq action 'agenda) org-agenda-archives-mode))))
13093 ;; select this headline
13096 ((eq action 'sparse-tree)
13097 (and org-highlight-sparse-tree-matches
13098 (org-get-heading) (match-end 0)
13099 (org-highlight-new-match
13100 (match-beginning 1) (match-end 1)))
13101 (org-show-context 'tags-tree))
13102 ((eq action 'agenda)
13103 (setq txt (org-agenda-format-item
13106 (if (eq org-tags-match-list-sublevels 'indented)
13107 (make-string (1- level) ?.) "")
13111 priority (org-get-priority txt))
13113 (setq marker (org-agenda-new-marker))
13114 (org-add-props txt props
13115 'org-marker marker 'org-hd-marker marker 'org-category category
13117 'priority priority 'type "tagsmatch")
13119 ((functionp action)
13120 (setq org-map-continue-from nil)
13122 (setq rtn1 (funcall action))
13124 (t (error "Invalid action")))
13126 ;; if we are to skip sublevels, jump to end of subtree
13127 (unless org-tags-match-list-sublevels
13128 (org-end-of-subtree t)
13129 (backward-char 1))))
13130 ;; Get the correct position from where to continue
13131 (if org-map-continue-from
13132 (goto-char org-map-continue-from)
13133 (and (= (point) lspos) (end-of-line 1)))))
13134 (when (and (eq action 'sparse-tree)
13135 (not org-sparse-tree-open-archived-trees))
13136 (org-hide-archived-subtrees (point-min) (point-max)))
13139 (defun org-remove-uninherited-tags (tags)
13140 "Remove all tags that are not inherited from the list TAGS."
13142 ((eq org-use-tag-inheritance t)
13143 (if org-tags-exclude-from-inheritance
13144 (org-delete-all org-tags-exclude-from-inheritance tags)
13146 ((not org-use-tag-inheritance) nil)
13147 ((stringp org-use-tag-inheritance)
13150 (if (and (string-match org-use-tag-inheritance x)
13151 (not (member x org-tags-exclude-from-inheritance)))
13154 ((listp org-use-tag-inheritance)
13157 (if (member x org-use-tag-inheritance) x nil))
13160 (defun org-match-sparse-tree (&optional todo-only match)
13161 "Create a sparse tree according to tags string MATCH.
13162 MATCH can contain positive and negative selection of tags, like
13163 \"+WORK+URGENT-WITHBOSS\".
13164 If optional argument TODO-ONLY is non-nil, only select lines that are
13167 (org-agenda-prepare-buffers (list (current-buffer)))
13168 (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
13170 (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
13172 (defvar org-cached-props nil)
13173 (defun org-cached-entry-get (pom property)
13174 (if (or (eq t org-use-property-inheritance)
13175 (and (stringp org-use-property-inheritance)
13176 (string-match org-use-property-inheritance property))
13177 (and (listp org-use-property-inheritance)
13178 (member property org-use-property-inheritance)))
13179 ;; Caching is not possible, check it directly
13180 (org-entry-get pom property 'inherit)
13181 ;; Get all properties, so that we can do complicated checks easily
13182 (cdr (assoc property (or org-cached-props
13183 (setq org-cached-props
13184 (org-entry-properties pom)))))))
13186 (defun org-global-tags-completion-table (&optional files)
13187 "Return the list of all tags in all agenda buffer/files.
13188 Optional FILES argument is a list of files which can be used
13189 instead of the agenda files."
13196 (set-buffer (find-file-noselect file))
13197 (append (org-get-buffer-tags)
13198 (mapcar (lambda (x) (if (stringp (car-safe x))
13199 (list (car-safe x)) nil))
13201 (if (and files (car files))
13203 (org-agenda-files))))))))
13205 (defun org-make-tags-matcher (match)
13206 "Create the TAGS/TODO matcher form for the selection string MATCH.
13208 The variable `todo-only' is scoped dynamically into this function.
13209 It will be set to t if the matcher restricts matching to TODO entries,
13210 otherwise will not be touched.
13212 Returns a cons of the selection string MATCH and the constructed
13213 lisp form implementing the matcher. The matcher is to be evaluated
13214 at an Org entry, with point on the headline, and returns t if the
13215 entry matches the selection string MATCH. The returned lisp form
13216 references two variables with information about the entry, which
13217 must be bound around the form's evaluation: todo, the TODO keyword
13218 at the entry (or nil of none); and tags-list, the list of all tags
13219 at the entry including inherited ones. Additionally, the category
13220 of the entry (if any) must be specified as the text property
13221 'org-category on the headline.
13223 See also `org-scan-tags'.
13225 (declare (special todo-only))
13226 (unless (boundp 'todo-only)
13227 (error "org-make-tags-matcher expects todo-only to be scoped in"))
13229 ;; Get a new match request, with completion
13230 (let ((org-last-tags-completion-table
13231 (org-global-tags-completion-table)))
13232 (setq match (org-completing-read-no-i
13233 "Match: " 'org-tags-completion-function nil nil nil
13234 'org-tags-history))))
13236 ;; Parse the string and create a lisp form
13237 (let ((match0 match)
13238 (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
13240 tagsmatch todomatch tagsmatcher todomatcher kwd matcher
13241 orterms term orlist re-p str-p level-p level-op time-p
13242 prop-p pn pv po gv rest)
13243 (if (string-match "/+" match)
13244 ;; match contains also a todo-matching request
13246 (setq tagsmatch (substring match 0 (match-beginning 0))
13247 todomatch (substring match (match-end 0)))
13248 (if (string-match "^!" todomatch)
13249 (setq todo-only t todomatch (substring todomatch 1)))
13250 (if (string-match "^\\s-*$" todomatch)
13251 (setq todomatch nil)))
13252 ;; only matching tags
13253 (setq tagsmatch match todomatch nil))
13255 ;; Make the tags matcher
13256 (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
13257 (setq tagsmatcher t)
13258 (setq orterms (org-split-string tagsmatch "|") orlist nil)
13259 (while (setq term (pop orterms))
13260 (while (and (equal (substring term -1) "\\") orterms)
13261 (setq term (concat term "|" (pop orterms)))) ; repair bad split
13262 (while (string-match re term)
13263 (setq rest (substring term (match-end 0))
13264 minus (and (match-end 1)
13265 (equal (match-string 1 term) "-"))
13266 tag (save-match-data (replace-regexp-in-string
13268 (match-string 2 term)))
13269 re-p (equal (string-to-char tag) ?{)
13270 level-p (match-end 4)
13271 prop-p (match-end 5)
13273 (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
13275 (setq level-op (org-op-to-function (match-string 3 term)))
13276 `(,level-op level ,(string-to-number
13277 (match-string 4 term))))
13279 (setq pn (match-string 5 term)
13280 po (match-string 6 term)
13281 pv (match-string 7 term)
13282 re-p (equal (string-to-char pv) ?{)
13283 str-p (equal (string-to-char pv) ?\")
13284 time-p (save-match-data
13285 (string-match "^\"[[<].*[]>]\"$" pv))
13286 pv (if (or re-p str-p) (substring pv 1 -1) pv))
13287 (if time-p (setq pv (org-matcher-time pv)))
13288 (setq po (org-op-to-function po (if time-p 'time str-p)))
13290 ((equal pn "CATEGORY")
13291 (setq gv '(get-text-property (point) 'org-category)))
13295 (setq gv `(org-cached-entry-get nil ,pn))))
13298 `(not (string-match ,pv (or ,gv "")))
13299 `(string-match ,pv (or ,gv "")))
13301 `(,po (or ,gv "") ,pv)
13302 `(,po (string-to-number (or ,gv ""))
13303 ,(string-to-number pv) ))))
13304 (t `(member ,tag tags-list)))
13305 mm (if minus (list 'not mm) mm)
13307 (push mm tagsmatcher))
13308 (push (if (> (length tagsmatcher) 1)
13309 (cons 'and tagsmatcher)
13312 (setq tagsmatcher nil))
13313 (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
13315 (list 'progn '(setq org-cached-props nil) tagsmatcher)))
13316 ;; Make the todo matcher
13317 (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
13318 (setq todomatcher t)
13319 (setq orterms (org-split-string todomatch "|") orlist nil)
13320 (while (setq term (pop orterms))
13321 (while (string-match re term)
13322 (setq minus (and (match-end 1)
13323 (equal (match-string 1 term) "-"))
13324 kwd (match-string 2 term)
13325 re-p (equal (string-to-char kwd) ?{)
13326 term (substring term (match-end 0))
13328 `(string-match ,(substring kwd 1 -1) todo)
13329 (list 'equal 'todo kwd))
13330 mm (if minus (list 'not mm) mm))
13331 (push mm todomatcher))
13332 (push (if (> (length todomatcher) 1)
13333 (cons 'and todomatcher)
13336 (setq todomatcher nil))
13337 (setq todomatcher (if (> (length orlist) 1)
13338 (cons 'or orlist) (car orlist))))
13340 ;; Return the string and lisp forms of the matcher
13341 (setq matcher (if todomatcher
13342 (list 'and tagsmatcher todomatcher)
13345 (setq matcher (list 'and '(member todo org-not-done-keywords)
13347 (cons match0 matcher)))
13349 (defun org-op-to-function (op &optional stringp)
13350 "Turn an operator into the appropriate function."
13353 ((equal op "<" ) '(< string< org-time<))
13354 ((equal op ">" ) '(> org-string> org-time>))
13355 ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
13356 ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
13357 ((member op '("=" "==")) '(= string= org-time=))
13358 ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
13359 (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
13361 (defun org<> (a b) (not (= a b)))
13362 (defun org-string<= (a b) (or (string= a b) (string< a b)))
13363 (defun org-string>= (a b) (not (string< a b)))
13364 (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
13365 (defun org-string<> (a b) (not (string= a b)))
13366 (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
13367 (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
13368 (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
13369 (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
13370 (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
13371 (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
13373 "Convert S to a floating point time.
13374 If S is already a number, just return it. If it is a string, parse
13375 it as a time string and apply `float-time' to it. If S is nil, just return 0."
13379 (condition-case nil
13380 (float-time (apply 'encode-time (org-parse-time-string s)))
13384 (defun org-time-today ()
13385 "Time in seconds today at 0:00.
13386 Returns the float number of seconds since the beginning of the
13387 epoch to the beginning of today (00:00)."
13388 (float-time (apply 'encode-time
13389 (append '(0 0 0) (nthcdr 3 (decode-time))))))
13391 (defun org-matcher-time (s)
13392 "Interpret a time comparison value."
13395 ((string= s "<now>") (float-time))
13396 ((string= s "<today>") (org-time-today))
13397 ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
13398 ((string= s "<yesterday>") (- (org-time-today) 86400.0))
13399 ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
13400 (+ (org-time-today)
13401 (* (string-to-number (match-string 1 s))
13402 (cdr (assoc (match-string 2 s)
13403 '(("d" . 86400.0) ("w" . 604800.0)
13404 ("m" . 2678400.0) ("y" . 31557600.0)))))))
13407 (defun org-match-any-p (re list)
13408 "Does re match any element of list?"
13409 (setq list (mapcar (lambda (x) (string-match re x)) list))
13412 (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
13413 (defvar org-tags-overlay (make-overlay 1 1))
13414 (org-detach-overlay org-tags-overlay)
13416 (defun org-get-local-tags-at (&optional pos)
13417 "Get a list of tags defined in the current headline."
13418 (org-get-tags-at pos 'local))
13420 (defun org-get-local-tags ()
13421 "Get a list of tags defined in the current headline."
13422 (org-get-tags-at nil 'local))
13424 (defun org-get-tags-at (&optional pos local)
13425 "Get a list of all headline tags applicable at POS.
13426 POS defaults to point. If tags are inherited, the list contains
13427 the targets in the same sequence as the headlines appear, i.e.
13428 the tags of the current headline come last.
13429 When LOCAL is non-nil, only return tags from the current headline,
13430 ignore inherited ones."
13432 (if (and org-trust-scanner-tags
13433 (or (not pos) (equal pos (point)))
13436 (let (tags ltags lastpos parent)
13440 (goto-char (or pos (point)))
13443 (condition-case nil
13445 (org-back-to-heading t)
13446 (while (not (equal lastpos (point)))
13447 (setq lastpos (point))
13449 (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
13450 (setq ltags (org-split-string
13451 (org-match-string-no-properties 1) ":"))
13453 (setq ltags (mapcar 'org-add-prop-inherited ltags)))
13456 (org-remove-uninherited-tags ltags)
13459 (or org-use-tag-inheritance (throw 'done t))
13460 (if local (throw 'done t))
13461 (or (org-up-heading-safe) (error nil))
13466 (append (org-remove-uninherited-tags org-file-tags) tags))))))
13468 (defun org-add-prop-inherited (s)
13469 (add-text-properties 0 (length s) '(inherited t) s)
13472 (defun org-toggle-tag (tag &optional onoff)
13473 "Toggle the tag TAG for the current line.
13474 If ONOFF is `on' or `off', don't toggle but set to this state."
13477 (org-back-to-heading t)
13478 (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
13481 (setq current (match-string 1))
13482 (replace-match ""))
13484 (setq current (nreverse (org-split-string current ":")))
13488 (or (member tag current) (push tag current)))
13490 (or (not (member tag current)) (setq current (delete tag current))))
13491 (t (if (member tag current)
13492 (setq current (delete tag current))
13494 (push tag current))))
13498 (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
13499 (org-set-tags nil t))
13500 (delete-horizontal-space))
13501 (run-hooks 'org-after-tags-change-hook))
13504 (defun org-align-tags-here (to-col)
13505 ;; Assumes that this is a headline
13506 (let ((pos (point)) (col (current-column)) ncol tags-l p)
13507 (beginning-of-line 1)
13508 (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13509 (< pos (match-beginning 2)))
13511 (setq tags-l (- (match-end 2) (match-beginning 2)))
13512 (goto-char (match-beginning 1))
13514 (delete-region (point) (1+ (match-beginning 2)))
13515 (setq ncol (max (current-column)
13519 (- (abs to-col) tags-l))))
13521 (insert (make-string (- ncol (current-column)) ?\ ))
13522 (setq ncol (current-column))
13523 (when indent-tabs-mode (tabify p (point-at-eol)))
13524 (org-move-to-column (min ncol col) t))
13527 (defun org-set-tags-command (&optional arg just-align)
13528 "Call the set-tags command for the current entry."
13530 (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
13531 (org-set-tags arg just-align)
13533 (org-back-to-heading t)
13534 (org-set-tags arg just-align))))
13536 (defun org-set-tags-to (data)
13537 "Set the tags of the current entry to DATA, replacing the current tags.
13538 DATA may be a tags string like :aa:bb:cc:, or a list of tags.
13539 If DATA is nil or the empty string, any tags will be removed."
13540 (interactive "sTags: ")
13544 ((equal data "") "")
13546 (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
13549 (concat ":" (mapconcat 'identity data ":") ":"))))
13552 (org-back-to-heading t)
13553 (when (looking-at org-complex-heading-regexp)
13556 (goto-char (match-beginning 5))
13558 (delete-region (point) (point-at-eol))
13559 (org-set-tags nil 'align))
13560 (goto-char (point-at-eol))
13562 (org-set-tags nil 'align)))
13563 (beginning-of-line 1)
13564 (if (looking-at ".*?\\([ \t]+\\)$")
13565 (delete-region (match-beginning 1) (match-end 1))))))
13567 (defun org-align-all-tags ()
13568 "Align the tags i all headings."
13571 (or (ignore-errors (org-back-to-heading t))
13572 (outline-next-heading))
13573 (if (org-at-heading-p)
13575 (message "No headings"))))
13577 (defvar org-indent-indentation-per-level)
13578 (defun org-set-tags (&optional arg just-align)
13579 "Set the tags for the current headline.
13580 With prefix ARG, realign all tags in headings in the current buffer."
13582 (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
13583 (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
13584 'region-start-level 'region))
13585 org-loop-over-headlines-in-active-region)
13587 ;; We don't use ARG and JUST-ALIGN here these args are not
13588 ;; useful when looping over headlines
13590 org-loop-over-headlines-in-active-region
13591 cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
13592 (let* ((re org-outline-regexp-bol)
13593 (current (unless arg (org-get-tags-string)))
13594 (col (current-column))
13595 (org-setting-tags t)
13596 table current-tags inherited-tags ; computed below when needed
13597 tags p0 c0 c1 rpl di tc level)
13600 (goto-char (point-min))
13601 (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
13602 (while (re-search-forward re nil t)
13603 (org-set-tags nil t)
13605 (message "All tags realigned to column %d" org-tags-column))
13607 (setq tags current)
13608 ;; Get a new set of tags from the user
13610 (setq table (append org-tag-persistent-alist
13611 (or org-tag-alist (org-get-buffer-tags))
13613 org-complete-tags-always-offer-all-agenda-tags
13614 (org-global-tags-completion-table
13615 (org-agenda-files))))
13616 org-last-tags-completion-table table
13617 current-tags (org-split-string current ":")
13618 inherited-tags (nreverse
13619 (nthcdr (length current-tags)
13620 (nreverse (org-get-tags-at))))
13622 (if (or (eq t org-use-fast-tag-selection)
13623 (and org-use-fast-tag-selection
13624 (delq nil (mapcar 'cdr table))))
13625 (org-fast-tag-selection
13626 current-tags inherited-tags table
13627 (if org-fast-tag-selection-include-todo
13628 org-todo-key-alist))
13629 (let ((org-add-colon-after-tag-completion (< 1 (length table))))
13631 (org-icompleting-read "Tags: "
13632 'org-tags-completion-function
13633 nil nil current 'org-tags-history))))))
13634 (while (string-match "[-+&]+" tags)
13635 ;; No boolean logic, just a list
13636 (setq tags (replace-match ":" t t tags))))
13638 (setq tags (replace-regexp-in-string "[,]" ":" tags))
13640 (if org-tags-sort-function
13641 (setq tags (mapconcat 'identity
13642 (sort (org-split-string
13643 tags (org-re "[^[:alnum:]_@#%]+"))
13644 org-tags-sort-function) ":")))
13646 (if (string-match "\\`[\t ]*\\'" tags)
13648 (unless (string-match ":$" tags) (setq tags (concat tags ":")))
13649 (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
13651 ;; Insert new tags at the correct column
13652 (beginning-of-line 1)
13653 (setq level (or (and (looking-at org-outline-regexp)
13654 (- (match-end 0) (point) 1))
13657 ((and (equal current "") (equal tags "")))
13658 ((re-search-forward
13659 (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
13661 (if (equal tags "")
13663 (goto-char (match-beginning 0))
13664 (setq c0 (current-column)
13665 ;; compute offset for the case of org-indent-mode active
13666 di (if org-indent-mode
13667 (* (1- org-indent-indentation-per-level) (1- level))
13669 p0 (if (equal (char-before) ?*) (1+ (point)) (point))
13670 tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
13671 c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
13672 rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
13673 (replace-match rpl t t)
13674 (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
13676 (t (error "Tags alignment failed")))
13677 (org-move-to-column col)
13679 (run-hooks 'org-after-tags-change-hook))))))
13681 (defun org-change-tag-in-region (beg end tag off)
13682 "Add or remove TAG for each entry in the region.
13683 This works in the agenda, and also in an org-mode buffer."
13685 (list (region-beginning) (region-end)
13686 (let ((org-last-tags-completion-table
13687 (if (derived-mode-p 'org-mode)
13688 (org-get-buffer-tags)
13689 (org-global-tags-completion-table))))
13690 (org-icompleting-read
13691 "Tag: " 'org-tags-completion-function nil nil nil
13692 'org-tags-history))
13694 (message "[s]et or
[r]emove? ")
13695 (equal (read-char-exclusive) ?r))))
13696 (if (fboundp 'deactivate-mark) (deactivate-mark))
13697 (let ((agendap (equal major-mode 'org-agenda-mode))
13698 l1 l2 m buf pos newhead (cnt 0))
13700 (setq l2 (1- (org-current-line)))
13702 (setq l1 (org-current-line))
13703 (loop for l from l1 to l2 do
13705 (setq m (get-text-property (point) 'org-hd-marker))
13706 (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
13708 (setq buf (if agendap (marker-buffer m) (current-buffer))
13709 pos (if agendap m (point)))
13710 (with-current-buffer buf
13714 (setq cnt (1+ cnt))
13715 (org-toggle-tag tag (if off 'off 'on))
13716 (setq newhead (org-get-heading)))))
13717 (and agendap (org-agenda-change-all-lines newhead m))))
13718 (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
13720 (defun org-tags-completion-function (string predicate &optional flag)
13721 (let (s1 s2 rtn (ctable org-last-tags-completion-table)
13722 (confirm (lambda (x) (stringp (car x)))))
13723 (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
13724 (setq s1 (match-string 1 string)
13725 s2 (match-string 2 string))
13726 (setq s1 "" s2 string))
13730 (setq rtn (try-completion s2 ctable confirm))
13733 (concat s1 s2 (substring rtn (length s2))
13734 (if (and org-add-colon-after-tag-completion
13735 (assoc rtn ctable))
13740 (all-completions s2 ctable confirm)
13744 (assoc s2 ctable)))
13747 (defun org-fast-tag-insert (kwd tags face &optional end)
13748 "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
13749 (insert (format "%-12s" (concat kwd ":"))
13750 (org-add-props (mapconcat 'identity tags " ") nil 'face face)
13753 (defun org-fast-tag-show-exit (flag)
13756 (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
13757 (replace-match ""))
13760 (org-move-to-column (- (window-width) 19) t)
13761 (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
13763 (defun org-set-current-tags-overlay (current prefix)
13764 (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
13765 (if (featurep 'xemacs)
13766 (org-overlay-display org-tags-overlay (concat prefix s)
13767 'secondary-selection)
13768 (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
13769 (org-overlay-display org-tags-overlay (concat prefix s)))))
13771 (defvar org-last-tag-selection-key nil)
13772 (defun org-fast-tag-selection (current inherited table &optional todo-table)
13773 "Fast tag selection with single keys.
13774 CURRENT is the current list of tags in the headline, INHERITED is the
13775 list of inherited tags, and TABLE is an alist of tags and corresponding keys,
13776 possibly with grouping information. TODO-TABLE is a similar table with
13777 TODO keywords, should these have keys assigned to them.
13778 If the keys are nil, a-z are automatically assigned.
13779 Returns the new tags string, or nil to not change the current settings."
13780 (let* ((fulltable (append table todo-table))
13781 (maxlen (apply 'max (mapcar
13783 (if (stringp (car x)) (string-width (car x)) 0))
13785 (buf (current-buffer))
13786 (expert (eq org-fast-tag-selection-single-key 'expert))
13788 (fwidth (+ maxlen 3 1 3))
13789 (ncol (/ (- (window-width) 4) fwidth))
13792 tg cnt e c char c1 c2 ntable tbl rtn
13793 ov-start ov-end ov-prefix
13794 (exit-after-next org-fast-tag-selection-single-key)
13795 (done-keywords org-done-keywords)
13798 (beginning-of-line 1)
13800 (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13801 (setq ov-start (match-beginning 1)
13802 ov-end (match-end 1)
13804 (setq ov-start (1- (point-at-eol))
13805 ov-end (1+ ov-start))
13806 (skip-chars-forward "^\n\r")
13809 (buffer-substring (1- (point)) (point))
13810 (if (> (current-column) org-tags-column)
13812 (make-string (- org-tags-column (current-column)) ?\ ))))))
13813 (move-overlay org-tags-overlay ov-start ov-end)
13814 (save-window-excursion
13816 (set-buffer (get-buffer-create " *Org tags*"))
13817 (delete-other-windows)
13818 (split-window-vertically)
13819 (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
13821 (org-set-local 'org-done-keywords done-keywords)
13822 (org-fast-tag-insert "Inherited" inherited i-face "\n")
13823 (org-fast-tag-insert "Current" current c-face "\n\n")
13824 (org-fast-tag-show-exit exit-after-next)
13825 (org-set-current-tags-overlay current ov-prefix)
13826 (setq tbl fulltable char ?a cnt 0)
13827 (while (setq e (pop tbl))
13829 ((equal (car e) :startgroup)
13830 (push '() groups) (setq ingroup t)
13831 (when (not (= cnt 0))
13834 (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
13835 ((equal (car e) :endgroup)
13836 (setq ingroup nil cnt 0)
13837 (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
13838 ((equal e '(:newline))
13839 (when (not (= cnt 0))
13843 (while (equal (car tbl) '(:newline))
13845 (setq tbl (cdr tbl)))))
13847 (setq tg (copy-sequence (car e)) c2 nil)
13850 ;; automatically assign a character.
13851 (setq c1 (string-to-char
13852 (downcase (substring
13853 tg (if (= (string-to-char tg) ?@) 1 0)))))
13854 (if (or (rassoc c1 ntable) (rassoc c1 table))
13855 (while (or (rassoc char ntable) (rassoc char table))
13856 (setq char (1+ char)))
13858 (setq c (or c2 char)))
13859 (if ingroup (push tg (car groups)))
13860 (setq tg (org-add-props tg nil 'face
13862 ((not (assoc tg table))
13863 (org-get-todo-face tg))
13864 ((member tg current) c-face)
13865 ((member tg inherited) i-face))))
13866 (if (and (= cnt 0) (not ingroup)) (insert " "))
13867 (insert "[" c "] " tg (make-string
13868 (- fwidth 4 (length tg)) ?\ ))
13869 (push (cons tg c) ntable)
13870 (when (= (setq cnt (1+ cnt)) ncol)
13872 (if ingroup (insert " "))
13874 (setq ntable (nreverse ntable))
13876 (goto-char (point-min))
13877 (if (not expert) (org-fit-window-to-buffer))
13881 (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
13882 (if (not groups) "no " "")
13883 (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
13884 (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
13885 (setq org-last-tag-selection-key c)
13887 ((= c ?\r) (throw 'exit t))
13889 (setq groups (not groups))
13890 (goto-char (point-min))
13891 (while (re-search-forward "[{}]" nil t) (replace-match " ")))
13894 (org-fast-tag-show-exit
13895 (setq exit-after-next (not exit-after-next)))
13897 (delete-other-windows)
13898 (set-window-buffer (split-window-vertically) " *Org tags*")
13899 (org-switch-to-buffer-other-window " *Org tags*")
13900 (org-fit-window-to-buffer)))
13902 (and (= c ?q) (not (rassoc c ntable))))
13903 (org-detach-overlay org-tags-overlay)
13904 (setq quit-flag t))
13907 (if exit-after-next (setq exit-after-next 'now)))
13909 (condition-case nil
13910 (setq tg (org-icompleting-read
13913 (with-current-buffer buf
13914 (org-get-buffer-tags)))))
13915 (quit (setq tg "")))
13916 (when (string-match "\\S-" tg)
13917 (add-to-list 'buffer-tags (list tg))
13918 (if (member tg current)
13919 (setq current (delete tg current))
13920 (push tg current)))
13921 (if exit-after-next (setq exit-after-next 'now)))
13922 ((setq e (rassoc c todo-table) tg (car e))
13923 (with-current-buffer buf
13924 (save-excursion (org-todo tg)))
13925 (if exit-after-next (setq exit-after-next 'now)))
13926 ((setq e (rassoc c ntable) tg (car e))
13927 (if (member tg current)
13928 (setq current (delete tg current))
13929 (loop for g in groups do
13932 (setq current (delete x current)))
13935 (if exit-after-next (setq exit-after-next 'now))))
13937 ;; Create a sorted list
13941 (assoc b (cdr (memq (assoc a ntable) ntable))))))
13942 (if (eq exit-after-next 'now) (throw 'exit t))
13943 (goto-char (point-min))
13944 (beginning-of-line 2)
13945 (delete-region (point) (point-at-eol))
13946 (org-fast-tag-insert "Current" current c-face)
13947 (org-set-current-tags-overlay current ov-prefix)
13948 (while (re-search-forward
13949 (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
13950 (setq tg (match-string 1))
13951 (add-text-properties
13952 (match-beginning 1) (match-end 1)
13955 ((member tg current) c-face)
13956 ((member tg inherited) i-face)
13957 (t (get-text-property (match-beginning 1) 'face))))))
13958 (goto-char (point-min)))))
13959 (org-detach-overlay org-tags-overlay)
13961 (mapconcat 'identity current ":")
13964 (defun org-get-tags-string ()
13965 "Get the TAGS string in the current headline."
13966 (unless (org-at-heading-p t)
13967 (error "Not on a heading"))
13969 (beginning-of-line 1)
13970 (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
13971 (org-match-string-no-properties 1)
13974 (defun org-get-tags ()
13975 "Get the list of tags specified in the current headline."
13976 (org-split-string (org-get-tags-string) ":"))
13978 (defun org-get-buffer-tags ()
13979 "Get a table of all tags used in the buffer, for completion."
13982 (goto-char (point-min))
13983 (while (re-search-forward
13984 (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
13985 (when (equal (char-after (point-at-bol 0)) ?*)
13986 (mapc (lambda (x) (add-to-list 'tags x))
13987 (org-split-string (org-match-string-no-properties 1) ":")))))
13988 (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
13989 (mapcar 'list tags)))
13991 ;;;; The mapping API
13993 (defun org-map-entries (func &optional match scope &rest skip)
13994 "Call FUNC at each headline selected by MATCH in SCOPE.
13996 FUNC is a function or a lisp form. The function will be called without
13997 arguments, with the cursor positioned at the beginning of the headline.
13998 The return values of all calls to the function will be collected and
13999 returned as a list.
14001 The call to FUNC will be wrapped into a save-excursion form, so FUNC
14002 does not need to preserve point. After evaluation, the cursor will be
14003 moved to the end of the line (presumably of the headline of the
14004 processed entry) and search continues from there. Under some
14005 circumstances, this may not produce the wanted results. For example,
14006 if you have removed (e.g. archived) the current (sub)tree it could
14007 mean that the next entry will be skipped entirely. In such cases, you
14008 can specify the position from where search should continue by making
14009 FUNC set the variable `org-map-continue-from' to the desired buffer
14012 MATCH is a tags/property/todo match as it is used in the agenda tags view.
14013 Only headlines that are matched by this query will be considered during
14014 the iteration. When MATCH is nil or t, all headlines will be
14015 visited by the iteration.
14017 SCOPE determines the scope of this command. It can be any of:
14019 nil The current buffer, respecting the restriction if any
14020 tree The subtree started with the entry at point
14021 region The entries within the active region, if any
14023 The entries within the active region, but only those at
14024 the same level than the first one.
14025 file The current buffer, without restriction
14027 The current buffer, and any archives associated with it
14028 agenda All agenda files
14029 agenda-with-archives
14030 All agenda files with any archive files associated with them
14032 If this is a list, all files in the list will be scanned
14034 The remaining args are treated as settings for the skipping facilities of
14035 the scanner. The following items can be given here:
14037 archive skip trees with the archive tag.
14038 comment skip trees with the COMMENT keyword
14039 function or Emacs Lisp form:
14040 will be used as value for `org-agenda-skip-function', so whenever
14041 the function returns t, FUNC will not be called for that
14042 entry and search will continue from the point where the
14043 function leaves it.
14045 If your function needs to retrieve the tags including inherited tags
14046 at the *current* entry, you can use the value of the variable
14047 `org-scanner-tags' which will be much faster than getting the value
14048 with `org-get-tags-at'. If your function gets properties with
14049 `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
14050 to t around the call to `org-entry-properties' to get the same speedup.
14051 Note that if your function moves around to retrieve tags and properties at
14052 a *different* entry, you cannot use these techniques."
14053 (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
14054 (not (org-region-active-p)))
14055 (let* ((org-agenda-archives-mode nil) ; just to make sure
14056 (org-agenda-skip-archived-trees (memq 'archive skip))
14057 (org-agenda-skip-comment-trees (memq 'comment skip))
14058 (org-agenda-skip-function
14059 (car (org-delete-all '(comment archive) skip)))
14060 (org-tags-match-list-sublevels t)
14061 (start-level (eq scope 'region-start-level))
14063 org-todo-keywords-for-agenda
14064 org-done-keywords-for-agenda
14065 org-todo-keyword-alist-for-agenda
14066 org-drawers-for-agenda
14067 org-tag-alist-for-agenda
14071 ((eq match t) (setq matcher t))
14072 ((eq match nil) (setq matcher t))
14073 (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
14077 (cond ((eq scope 'tree)
14078 (org-back-to-heading t)
14079 (org-narrow-to-subtree)
14081 ((and (or (eq scope 'region) (eq scope 'region-start-level))
14082 (org-region-active-p))
14083 ;; If needed, set start-level to a string like "2"
14086 (goto-char (region-beginning))
14087 (unless (org-at-heading-p) (outline-next-heading))
14088 (setq start-level (org-current-level))))
14089 (narrow-to-region (region-beginning)
14091 (goto-char (region-end))
14092 (unless (and (bolp) (org-at-heading-p))
14093 (outline-next-heading))
14099 (org-agenda-prepare-buffers
14100 (list (buffer-file-name (current-buffer))))
14101 (setq res (org-scan-tags func matcher todo-only start-level)))
14102 ;; Get the right scope
14104 ((and scope (listp scope) (symbolp (car scope)))
14105 (setq scope (eval scope)))
14106 ((eq scope 'agenda)
14107 (setq scope (org-agenda-files t)))
14108 ((eq scope 'agenda-with-archives)
14109 (setq scope (org-agenda-files t))
14110 (setq scope (org-add-archive-files scope)))
14112 (setq scope (list (buffer-file-name))))
14113 ((eq scope 'file-with-archives)
14114 (setq scope (org-add-archive-files (list (buffer-file-name))))))
14115 (org-agenda-prepare-buffers scope)
14116 (while (setq file (pop scope))
14117 (with-current-buffer (org-find-base-buffer-visiting file)
14121 (goto-char (point-min))
14122 (setq res (append res (org-scan-tags func matcher todo-only))))))))))
14127 ;;; Setting and retrieving properties
14129 (defconst org-special-properties
14130 '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
14131 "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
14132 "The special properties valid in Org-mode.
14134 These are properties that are not defined in the property drawer,
14135 but in some other way.")
14137 (defconst org-default-properties
14138 '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
14139 "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
14140 "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
14141 "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
14142 "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
14143 "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
14144 "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
14145 "Some properties that are used by Org-mode for various purposes.
14146 Being in this list makes sure that they are offered for completion.")
14148 (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
14149 "Regular expression matching the first line of a property drawer.")
14151 (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
14152 "Regular expression matching the last line of a property drawer.")
14154 (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
14155 "Regular expression matching the first line of a property drawer.")
14157 (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
14158 "Regular expression matching the first line of a property drawer.")
14160 (defconst org-property-drawer-re
14161 (concat "\\(" org-property-start-re "\\)[^\000]*\\("
14162 org-property-end-re "\\)\n?")
14163 "Matches an entire property drawer.")
14165 (defconst org-clock-drawer-re
14166 (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
14167 org-property-end-re "\\)\n?")
14168 "Matches an entire clock drawer.")
14170 (defsubst org-re-property (property)
14171 "Return a regexp matching a PROPERTY line.
14172 Match group 1 will be set to the value."
14173 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
14175 (defsubst org-re-property-keyword (property)
14176 "Return a regexp matching a PROPERTY line, possibly with no
14177 value for the property."
14178 (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
14180 (defun org-property-action ()
14181 "Do an action on properties."
14184 (org-at-property-p)
14185 (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
14186 (setq c (read-char-exclusive))
14189 (call-interactively 'org-set-property))
14191 (call-interactively 'org-delete-property))
14193 (call-interactively 'org-delete-property-globally))
14195 (call-interactively 'org-compute-property-at-point))
14196 (t (error "No such property action %c" c)))))
14198 (defun org-inc-effort ()
14199 "Increment the value of the effort property in the current entry."
14201 (org-set-effort nil t))
14203 (defun org-set-effort (&optional value increment)
14204 "Set the effort property of the current entry.
14205 With numerical prefix arg, use the nth allowed value, 0 stands for the
14206 10th allowed value.
14208 When INCREMENT is non-nil, set the property to the next allowed value."
14210 (if (equal value 0) (setq value 10))
14211 (let* ((completion-ignore-case t)
14212 (prop org-effort-property)
14213 (cur (org-entry-get nil prop))
14214 (allowed (org-property-get-allowed-values nil prop 'table))
14215 (existing (mapcar 'list (org-property-values prop)))
14218 ((stringp value) value)
14219 ((and allowed (integerp value))
14220 (or (car (nth (1- value) allowed))
14221 (car (org-last allowed))))
14222 ((and allowed increment)
14223 (or (caadr (member (list cur) allowed))
14224 (error "Allowed effort values are not set")))
14226 (message "Select 1-9,0, [RET%s]: %s"
14227 (if cur (concat "=" cur) "")
14228 (mapconcat 'car allowed " "))
14229 (setq rpl (read-char-exclusive))
14230 (if (equal rpl ?\r)
14232 (setq rpl (- rpl ?0))
14233 (if (equal rpl 0) (setq rpl 10))
14234 (if (and (> rpl 0) (<= rpl (length allowed)))
14235 (car (nth (1- rpl) allowed))
14236 (org-completing-read "Effort: " allowed nil))))
14238 (let (org-completion-use-ido org-completion-use-iswitchb)
14239 (org-completing-read
14240 (concat "Effort " (if (and cur (string-match "\\S-" cur))
14241 (concat "[" cur "]") "")
14243 existing nil nil "" nil cur))))))
14244 (unless (equal (org-entry-get nil prop) val)
14245 (org-entry-put nil prop val))
14246 (message "%s is now %s" prop val)))
14248 (defun org-at-property-p ()
14249 "Is cursor inside a property drawer?"
14251 (beginning-of-line 1)
14252 (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
14253 (save-match-data ;; Used by calling procedures
14255 (range (unless (org-before-first-heading-p)
14256 (org-get-property-block))))
14257 (and range (<= (car range) p) (< p (cdr range))))))))
14259 (defun org-get-property-block (&optional beg end force)
14260 "Return the (beg . end) range of the body of the property drawer.
14261 BEG and END are the beginning and end of the current subtree, or of
14262 the part before the first headline. If they are not given, they will
14263 be found. If the drawer does not exist and FORCE is non-nil, create
14267 (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
14268 (progn (org-back-to-heading t) (point))))
14269 (end (or end (and (not (outline-next-heading)) (point-max))
14272 (if (re-search-forward org-property-start-re end t)
14273 (setq beg (1+ (match-end 0)))
14276 (org-insert-property-drawer)
14277 (setq end (progn (outline-next-heading) (point))))
14280 (if (re-search-forward org-property-start-re end t)
14281 (setq beg (1+ (match-end 0)))))
14282 (if (re-search-forward org-property-end-re end t)
14283 (setq end (match-beginning 0))
14284 (or force (throw 'exit nil))
14288 (insert ":END:\n"))
14291 (defun org-entry-properties (&optional pom which specific)
14292 "Get all properties of the entry at point-or-marker POM.
14293 This includes the TODO keyword, the tags, time strings for deadline,
14294 scheduled, and clocking, and any additional properties defined in the
14295 entry. The return value is an alist, keys may occur multiple times
14296 if the property key was used several times.
14297 POM may also be nil, in which case the current entry is used.
14298 If WHICH is nil or `all', get all properties. If WHICH is
14299 `special' or `standard', only get that subclass. If WHICH
14300 is a string only get exactly this property. SPECIFIC can be a string, the
14301 specific property we are interested in. Specifying it can speed
14302 things up because then unnecessary parsing is avoided."
14303 (setq which (or which 'all))
14304 (org-with-point-at pom
14305 (let ((clockstr (substring org-clock-string 0 -1))
14306 (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
14307 (case-fold-search nil)
14308 beg end range props sum-props key key1 value string clocksum clocksumt)
14310 (when (condition-case nil
14311 (and (derived-mode-p 'org-mode) (org-back-to-heading t))
14314 (setq sum-props (get-text-property (point) 'org-summaries))
14315 (setq clocksum (get-text-property (point) :org-clock-minutes)
14316 clocksumt (get-text-property (point) :org-clock-minutes-today))
14317 (outline-next-heading)
14319 (when (memq which '(all special))
14320 ;; Get the special properties, like TODO and tags
14322 (when (and (or (not specific) (string= specific "TODO"))
14323 (looking-at org-todo-line-regexp) (match-end 2))
14324 (push (cons "TODO" (org-match-string-no-properties 2)) props))
14325 (when (and (or (not specific) (string= specific "PRIORITY"))
14326 (looking-at org-priority-regexp))
14327 (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
14328 (when (or (not specific) (string= specific "FILE"))
14329 (push (cons "FILE" buffer-file-name) props))
14330 (when (and (or (not specific) (string= specific "TAGS"))
14331 (setq value (org-get-tags-string))
14332 (string-match "\\S-" value))
14333 (push (cons "TAGS" value) props))
14334 (when (and (or (not specific) (string= specific "ALLTAGS"))
14335 (setq value (org-get-tags-at)))
14336 (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
14339 (when (or (not specific) (string= specific "BLOCKED"))
14340 (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
14341 (when (or (not specific)
14343 '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
14344 "TIMESTAMP" "TIMESTAMP_IA")))
14346 (while (re-search-forward org-maybe-keyword-time-regexp end t)
14347 (setq key (if (match-end 1)
14348 (substring (org-match-string-no-properties 1)
14350 string (if (equal key clockstr)
14352 (buffer-substring-no-properties
14353 (match-beginning 3) (goto-char
14355 (substring (org-match-string-no-properties 3)
14357 ;; Get the correct property name from the key. This is
14358 ;; necessary if the user has configured time keywords.
14359 (setq key1 (concat key ":"))
14363 (if (= (char-after (match-beginning 3)) ?\[)
14364 "TIMESTAMP_IA" "TIMESTAMP")))
14365 ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
14366 ((equal key1 org-deadline-string) (setq key "DEADLINE"))
14367 ((equal key1 org-closed-string) (setq key "CLOSED"))
14368 ((equal key1 org-clock-string) (setq key "CLOCK")))
14369 (if (and specific (equal key specific) (not (equal key "CLOCK")))
14371 (push (cons key string) props)
14372 ;; no need to search further if match is found
14374 (when (or (equal key "CLOCK") (not (assoc key props)))
14375 (push (cons key string) props)))))))
14377 (when (memq which '(all standard))
14378 ;; Get the standard properties, like :PROP: ...
14379 (setq range (org-get-property-block beg end))
14381 (goto-char (car range))
14382 (while (re-search-forward
14383 (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
14385 (setq key (org-match-string-no-properties 1)
14386 value (org-trim (or (org-match-string-no-properties 2) "")))
14387 (unless (member key excluded)
14388 (push (cons key (or value "")) props)))))
14390 (push (cons "CLOCKSUM"
14391 (org-columns-number-to-string (/ (float clocksum) 60.)
14395 (push (cons "CLOCKSUM_T"
14396 (org-columns-number-to-string (/ (float clocksumt) 60.)
14399 (unless (assoc "CATEGORY" props)
14400 (push (cons "CATEGORY" (org-get-category)) props))
14401 (append sum-props (nreverse props)))))))
14403 (defun org-entry-get (pom property &optional inherit literal-nil)
14404 "Get value of PROPERTY for entry or content at point-or-marker POM.
14405 If INHERIT is non-nil and the entry does not have the property,
14406 then also check higher levels of the hierarchy.
14407 If INHERIT is the symbol `selective', use inheritance only if the setting
14408 in `org-use-property-inheritance' selects PROPERTY for inheritance.
14409 If the property is present but empty, the return value is the empty string.
14410 If the property is not present at all, nil is returned.
14412 If LITERAL-NIL is set, return the string value \"nil\" as a string,
14413 do not interpret it as the list atom nil. This is used for inheritance
14414 when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
14415 (org-with-point-at pom
14416 (if (and inherit (if (eq inherit 'selective)
14417 (org-property-inherit-p property)
14419 (org-entry-get-with-inheritance property literal-nil)
14420 (if (member property org-special-properties)
14421 ;; We need a special property. Use `org-entry-properties' to
14422 ;; retrieve it, but specify the wanted property
14423 (cdr (assoc property (org-entry-properties nil 'special property)))
14424 (let* ((range (org-get-property-block))
14425 (props (list (or (assoc property org-file-properties)
14426 (assoc property org-global-properties)
14427 (assoc property org-global-properties-fixed))))
14429 (when (re-search-forward
14430 (org-re-property key) (cdr range) t)
14432 (org-update-property-plist
14435 (org-match-string-no-properties 1) "")
14438 (when (and range (goto-char (car range)))
14439 (funcall ap property)
14440 (goto-char (car range))
14441 (while (funcall ap (concat property "+")))
14442 (setq val (cdr (assoc property props)))
14443 (when val (if literal-nil val (org-not-nil val)))))))))
14445 (defun org-property-or-variable-value (var &optional inherit)
14446 "Check if there is a property fixing the value of VAR.
14447 If yes, return this value. If not, return the current value of the variable."
14448 (let ((prop (org-entry-get nil (symbol-name var) inherit)))
14449 (if (and prop (stringp prop) (string-match "\\S-" prop))
14451 (symbol-value var))))
14453 (defun org-entry-delete (pom property)
14454 "Delete the property PROPERTY from entry at point-or-marker POM."
14455 (org-with-point-at pom
14456 (if (member property org-special-properties)
14457 nil ; cannot delete these properties.
14458 (let ((range (org-get-property-block)))
14460 (goto-char (car range))
14462 (org-re-property property)
14465 (delete-region (match-beginning 0) (1+ (point-at-eol)))
14469 ;; Multi-values properties are properties that contain multiple values
14470 ;; These values are assumed to be single words, separated by whitespace.
14471 (defun org-entry-add-to-multivalued-property (pom property value)
14472 "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
14473 (let* ((old (org-entry-get pom property))
14474 (values (and old (org-split-string old "[ \t]"))))
14475 (setq value (org-entry-protect-space value))
14476 (unless (member value values)
14477 (setq values (cons value values))
14478 (org-entry-put pom property
14479 (mapconcat 'identity values " ")))))
14481 (defun org-entry-remove-from-multivalued-property (pom property value)
14482 "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
14483 (let* ((old (org-entry-get pom property))
14484 (values (and old (org-split-string old "[ \t]"))))
14485 (setq value (org-entry-protect-space value))
14486 (when (member value values)
14487 (setq values (delete value values))
14488 (org-entry-put pom property
14489 (mapconcat 'identity values " ")))))
14491 (defun org-entry-member-in-multivalued-property (pom property value)
14492 "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
14493 (let* ((old (org-entry-get pom property))
14494 (values (and old (org-split-string old "[ \t]"))))
14495 (setq value (org-entry-protect-space value))
14496 (member value values)))
14498 (defun org-entry-get-multivalued-property (pom property)
14499 "Return a list of values in a multivalued property."
14500 (let* ((value (org-entry-get pom property))
14501 (values (and value (org-split-string value "[ \t]"))))
14502 (mapcar 'org-entry-restore-space values)))
14504 (defun org-entry-put-multivalued-property (pom property &rest values)
14505 "Set multivalued PROPERTY at point-or-marker POM to VALUES.
14506 VALUES should be a list of strings. Spaces will be protected."
14507 (org-entry-put pom property
14508 (mapconcat 'org-entry-protect-space values " "))
14509 (let* ((value (org-entry-get pom property))
14510 (values (and value (org-split-string value "[ \t]"))))
14511 (mapcar 'org-entry-restore-space values)))
14513 (defun org-entry-protect-space (s)
14514 "Protect spaces and newline in string S."
14515 (while (string-match " " s)
14516 (setq s (replace-match "%20" t t s)))
14517 (while (string-match "\n" s)
14518 (setq s (replace-match "%0A" t t s)))
14521 (defun org-entry-restore-space (s)
14522 "Restore spaces and newline in string S."
14523 (while (string-match "%20" s)
14524 (setq s (replace-match " " t t s)))
14525 (while (string-match "%0A" s)
14526 (setq s (replace-match "\n" t t s)))
14529 (defvar org-entry-property-inherited-from (make-marker)
14530 "Marker pointing to the entry from where a property was inherited.
14531 Each call to `org-entry-get-with-inheritance' will set this marker to the
14532 location of the entry where the inheritance search matched. If there was
14533 no match, the marker will point nowhere.
14534 Note that also `org-entry-get' calls this function, if the INHERIT flag
14537 (defun org-entry-get-with-inheritance (property &optional literal-nil)
14538 "Get PROPERTY of entry or content at point, search higher levels if needed.
14539 The search will stop at the first ancestor which has the property defined.
14540 If the value found is \"nil\", return nil to show that the property
14541 should be considered as undefined (this is the meaning of nil here).
14542 However, if LITERAL-NIL is set, return the string value \"nil\" instead."
14543 (move-marker org-entry-property-inherited-from nil)
14550 (when (setq tmp (org-entry-get nil property nil 'literal-nil))
14551 (or (ignore-errors (org-back-to-heading t))
14552 (goto-char (point-min)))
14553 (move-marker org-entry-property-inherited-from (point))
14555 (or (ignore-errors (org-up-heading-safe))
14556 (throw 'ex nil))))))
14558 (cdr (assoc property org-file-properties))
14559 (cdr (assoc property org-global-properties))
14560 (cdr (assoc property org-global-properties-fixed))))
14561 (if literal-nil tmp (org-not-nil tmp))))
14563 (defvar org-property-changed-functions nil
14564 "Hook called when the value of a property has changed.
14565 Each hook function should accept two arguments, the name of the property
14566 and the new value.")
14568 (defun org-entry-put (pom property value)
14569 "Set PROPERTY to VALUE for entry at point-or-marker POM."
14570 (org-with-point-at pom
14571 (org-back-to-heading t)
14572 (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
14575 ((equal property "TODO")
14576 (when (and (stringp value) (string-match "\\S-" value)
14577 (not (member value org-todo-keywords-1)))
14578 (error "\"%s\" is not a valid TODO state" value))
14579 (if (or (not value)
14580 (not (string-match "\\S-" value)))
14581 (setq value 'none))
14583 (org-set-tags nil 'align))
14584 ((equal property "PRIORITY")
14585 (org-priority (if (and value (stringp value) (string-match "\\S-" value))
14586 (string-to-char value) ?\ ))
14587 (org-set-tags nil 'align))
14588 ((equal property "SCHEDULED")
14589 (if (re-search-forward org-scheduled-time-regexp end t)
14591 ((eq value 'earlier) (org-timestamp-change -1 'day))
14592 ((eq value 'later) (org-timestamp-change 1 'day))
14593 (t (call-interactively 'org-schedule)))
14594 (call-interactively 'org-schedule)))
14595 ((equal property "DEADLINE")
14596 (if (re-search-forward org-deadline-time-regexp end t)
14598 ((eq value 'earlier) (org-timestamp-change -1 'day))
14599 ((eq value 'later) (org-timestamp-change 1 'day))
14600 (t (call-interactively 'org-deadline)))
14601 (call-interactively 'org-deadline)))
14602 ((member property org-special-properties)
14603 (error "The %s property can not yet be set with `org-entry-put'"
14605 (t ; a non-special property
14606 (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
14607 (setq range (org-get-property-block beg end 'force))
14608 (goto-char (car range))
14609 (if (re-search-forward
14610 (org-re-property-keyword property) (cdr range) t)
14612 (delete-region (match-beginning 0) (match-end 0))
14613 (goto-char (match-beginning 0)))
14614 (goto-char (cdr range))
14618 (insert ":" property ":")
14619 (and value (insert " " value))
14620 (org-indent-line)))))
14621 (run-hook-with-args 'org-property-changed-functions property value)))
14623 (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
14624 "Get all property keys in the current buffer.
14625 With INCLUDE-SPECIALS, also list the special properties that reflect things
14626 like tags and TODO state.
14627 With INCLUDE-DEFAULTS, also include properties that has special meaning
14628 internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
14630 With INCLUDE-COLUMNS, also include property names given in COLUMN
14631 formats in the current buffer."
14632 (let (rtn range cfmt s p)
14636 (goto-char (point-min))
14637 (while (re-search-forward org-property-start-re nil t)
14638 (setq range (org-get-property-block))
14639 (goto-char (car range))
14640 (while (re-search-forward
14641 (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
14643 (add-to-list 'rtn (org-match-string-no-properties 1)))
14644 (outline-next-heading))))
14646 (when include-specials
14647 (setq rtn (append org-special-properties rtn)))
14649 (when include-defaults
14650 (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
14651 (add-to-list 'rtn org-effort-property))
14653 (when include-columns
14657 (goto-char (point-min))
14658 (while (re-search-forward
14659 "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
14661 (setq cfmt (match-string 2) s 0)
14662 (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
14664 (setq s (match-end 0)
14665 p (match-string 1 cfmt))
14666 (unless (or (equal p "ITEM")
14667 (member p org-special-properties))
14668 (add-to-list 'rtn (match-string 1 cfmt))))))))
14670 (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
14672 (defun org-property-values (key)
14673 "Return a list of all values of property KEY in the current buffer."
14677 (goto-char (point-min))
14678 (let ((re (org-re-property key))
14680 (while (re-search-forward re nil t)
14681 (add-to-list 'values (org-trim (match-string 1))))
14682 (delete "" values)))))
14684 (defun org-insert-property-drawer ()
14685 "Insert a property drawer into the current entry."
14686 (org-back-to-heading t)
14687 (looking-at org-outline-regexp)
14688 (let ((indent (if org-adapt-indentation
14689 (- (match-end 0) (match-beginning 0))
14692 (re (concat "^[ \t]*" org-keyword-time-regexp))
14694 (outline-next-heading)
14697 (while (re-search-forward re end t))
14698 (setq hiddenp (outline-invisible-p))
14700 (and (equal (char-after) ?\n) (forward-char 1))
14701 (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
14702 (if (member (match-string 1) '("CLOCK:" ":END:"))
14703 ;; just skip this line
14704 (beginning-of-line 2)
14705 ;; Drawer start, find the end
14706 (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
14707 (beginning-of-line 1)))
14708 (org-skip-over-state-notes)
14709 (skip-chars-backward " \t\n\r")
14710 (if (eq (char-before) ?*) (forward-char 1))
14711 (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
14712 (beginning-of-line 0)
14713 (org-indent-to-column indent)
14714 (beginning-of-line 2)
14715 (org-indent-to-column indent)
14716 (beginning-of-line 0)
14719 (org-back-to-heading t)
14721 (org-flag-drawer t))))
14723 (defun org-insert-drawer (&optional arg drawer)
14724 "Insert a drawer at point.
14726 Optional argument DRAWER, when non-nil, is a string representing
14727 drawer's name. Otherwise, the user is prompted for a name.
14729 If a region is active, insert the drawer around that region
14732 Point is left between drawer's boundaries."
14734 (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
14736 ;; SYSTEM-DRAWERS is a list of drawer names that are used
14737 ;; internally by Org. They are meant to be inserted
14739 (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
14740 ;; Remove system drawers from list. Note: For some reason,
14741 ;; `org-completing-read' ignores the predicate while
14742 ;; `completing-read' handles it fine.
14743 (drawer (if arg "PROPERTIES"
14746 "Drawer: " org-drawers
14747 (lambda (d) (not (member d system-drawers))))))))
14749 ;; With C-u, fall back on `org-insert-property-drawer'
14750 (arg (org-insert-property-drawer))
14751 ;; With an active region, insert a drawer at point.
14752 ((not (org-region-active-p))
14754 (unless (bolp) (insert "\n"))
14755 (insert (format ":%s:\n\n:END:\n" drawer))
14756 (forward-line -2)))
14757 ;; Otherwise, insert the drawer at point
14759 (let ((rbeg (region-beginning))
14760 (rend (copy-marker (region-end))))
14764 (beginning-of-line)
14765 (when (save-excursion
14766 (re-search-forward org-outline-regexp-bol rend t))
14767 (error "Drawers cannot contain headlines"))
14768 ;; Position point at the beginning of the first
14769 ;; non-blank line in region. Insert drawer's opening
14770 ;; there, then indent it.
14771 (org-skip-whitespace)
14772 (beginning-of-line)
14773 (insert ":" drawer ":\n")
14775 (indent-for-tab-command)
14776 ;; Move point to the beginning of the first blank line
14777 ;; after the last non-blank line in region. Insert
14778 ;; drawer's closing, then indent it.
14780 (skip-chars-backward " \r\t\n")
14782 (deactivate-mark t)
14783 (indent-for-tab-command)
14784 (unless (eolp) (insert "\n")))
14785 ;; Clear marker, whatever the outcome of insertion is.
14786 (set-marker rend nil)))))))
14788 (defvar org-property-set-functions-alist nil
14789 "Property set function alist.
14790 Each entry should have the following format:
14792 (PROPERTY . READ-FUNCTION)
14794 The read function will be called with the same argument as
14795 `org-completing-read'.")
14797 (defun org-set-property-function (property)
14798 "Get the function that should be used to set PROPERTY.
14799 This is computed according to `org-property-set-functions-alist'."
14800 (or (cdr (assoc property org-property-set-functions-alist))
14801 'org-completing-read))
14803 (defun org-read-property-value (property)
14804 "Read PROPERTY value from user."
14805 (let* ((completion-ignore-case t)
14806 (allowed (org-property-get-allowed-values nil property 'table))
14807 (cur (org-entry-get nil property))
14808 (prompt (concat property " value"
14809 (if (and cur (string-match "\\S-" cur))
14810 (concat " [" cur "]") "") ": "))
14811 (set-function (org-set-property-function property))
14813 (funcall set-function prompt allowed nil
14814 (not (get-text-property 0 'org-unrestricted
14816 (let (org-completion-use-ido org-completion-use-iswitchb)
14817 (funcall set-function prompt
14818 (mapcar 'list (org-property-values property))
14819 nil nil "" nil cur)))))
14824 (defvar org-last-set-property nil)
14825 (defun org-read-property-name ()
14826 "Read a property name."
14827 (let* ((completion-ignore-case t)
14828 (keys (org-buffer-property-keys nil t t))
14829 (default-prop (or (save-excursion
14831 (beginning-of-line)
14832 (and (looking-at "^\\s-*:\\([^:\n]+\\):")
14833 (null (string= (match-string 1) "END"))
14834 (match-string 1))))
14835 org-last-set-property))
14836 (property (org-icompleting-read
14838 (if default-prop (concat " [" default-prop "]") "")
14840 (mapcar 'list keys)
14844 (if (member property keys)
14846 (or (cdr (assoc (downcase property)
14847 (mapcar (lambda (x) (cons (downcase x) x))
14851 (defun org-set-property (property value)
14852 "In the current entry, set PROPERTY to VALUE.
14853 When called interactively, this will prompt for a property name, offering
14854 completion on existing and default properties. And then it will prompt
14855 for a value, offering completion either on allowed values (via an inherited
14856 xxx_ALL property) or on existing values in other instances of this property
14857 in the current file."
14858 (interactive (list nil nil))
14859 (let* ((property (or property (org-read-property-name)))
14860 (value (or value (org-read-property-value property)))
14861 (fn (cdr (assoc property org-properties-postprocess-alist))))
14862 (setq org-last-set-property property)
14863 ;; Possibly postprocess the inserted value:
14864 (when fn (setq value (funcall fn value)))
14865 (unless (equal (org-entry-get nil property) value)
14866 (org-entry-put nil property value))))
14868 (defun org-delete-property (property)
14869 "In the current entry, delete PROPERTY."
14871 (let* ((completion-ignore-case t)
14872 (prop (org-icompleting-read "Property: "
14873 (org-entry-properties nil 'standard))))
14875 (message "Property %s %s" property
14876 (if (org-entry-delete nil property)
14878 "was not present in the entry")))
14880 (defun org-delete-property-globally (property)
14881 "Remove PROPERTY globally, from all entries."
14883 (let* ((completion-ignore-case t)
14884 (prop (org-icompleting-read
14885 "Globally remove property: "
14886 (mapcar 'list (org-buffer-property-keys)))))
14891 (goto-char (point-min))
14893 (while (re-search-forward
14894 (org-re-property property)
14896 (setq cnt (1+ cnt))
14897 (delete-region (match-beginning 0) (1+ (point-at-eol))))
14898 (message "Property \"%s\" removed from %d entries" property cnt)))))
14900 (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
14902 (defun org-compute-property-at-point ()
14903 "Compute the property at point.
14904 This looks for an enclosing column format, extracts the operator and
14905 then applies it to the property in the column format's scope."
14907 (unless (org-at-property-p)
14908 (error "Not at a property"))
14909 (let ((prop (org-match-string-no-properties 2)))
14910 (org-columns-get-format-and-top-level)
14911 (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
14912 (error "No operator defined for property %s" prop))
14913 (org-columns-compute prop)))
14915 (defvar org-property-allowed-value-functions nil
14916 "Hook for functions supplying allowed values for a specific property.
14917 The functions must take a single argument, the name of the property, and
14918 return a flat list of allowed values. If \":ETC\" is one of
14919 the values, this means that these values are intended as defaults for
14920 completion, but that other values should be allowed too.
14921 The functions must return nil if they are not responsible for this
14924 (defun org-property-get-allowed-values (pom property &optional table)
14925 "Get allowed values for the property PROPERTY.
14926 When TABLE is non-nil, return an alist that can directly be used for
14930 ((equal property "TODO")
14931 (setq vals (org-with-point-at pom
14932 (append org-todo-keywords-1 '("")))))
14933 ((equal property "PRIORITY")
14934 (let ((n org-lowest-priority))
14935 (while (>= n org-highest-priority)
14936 (push (char-to-string n) vals)
14938 ((member property org-special-properties))
14939 ((setq vals (run-hook-with-args-until-success
14940 'org-property-allowed-value-functions property)))
14942 (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
14943 (when (and vals (string-match "\\S-" vals))
14944 (setq vals (car (read-from-string (concat "(" vals ")"))))
14945 (setq vals (mapcar (lambda (x)
14946 (cond ((stringp x) x)
14947 ((numberp x) (number-to-string x))
14948 ((symbolp x) (symbol-name x))
14951 (when (member ":ETC" vals)
14952 (setq vals (remove ":ETC" vals))
14953 (org-add-props (car vals) '(org-unrestricted t)))
14954 (if table (mapcar 'list vals) vals)))
14956 (defun org-property-previous-allowed-value (&optional previous)
14957 "Switch to the next allowed value for this property."
14959 (org-property-next-allowed-value t))
14961 (defun org-property-next-allowed-value (&optional previous)
14962 "Switch to the next allowed value for this property."
14964 (unless (org-at-property-p)
14965 (error "Not at a property"))
14966 (let* ((key (match-string 2))
14967 (value (match-string 3))
14968 (allowed (or (org-property-get-allowed-values (point) key)
14969 (and (member value '("[ ]" "[-]" "[X]"))
14973 (error "Allowed values for this property have not been defined"))
14974 (if previous (setq allowed (reverse allowed)))
14975 (if (member value allowed)
14976 (setq nval (car (cdr (member value allowed)))))
14977 (setq nval (or nval (car allowed)))
14978 (if (equal nval value)
14979 (error "Only one allowed value for this property"))
14980 (org-at-property-p)
14981 (replace-match (concat " :" key ": " nval) t t)
14983 (beginning-of-line 1)
14984 (skip-chars-forward " \t")
14985 (run-hook-with-args 'org-property-changed-functions key nval)))
14987 (defun org-find-olp (path &optional this-buffer)
14988 "Return a marker pointing to the entry at outline path OLP.
14989 If anything goes wrong, throw an error.
14990 You can wrap this call to catch the error like this:
14992 (condition-case msg
14993 (org-mobile-locate-entry (match-string 4))
14994 (error (nth 1 msg)))
14996 The return value will then be either a string with the error message,
14997 or a marker if everything is OK.
14999 If THIS-BUFFER is set, the outline path does not contain a file,
15001 (let* ((file (if this-buffer buffer-file-name (pop path)))
15002 (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
15006 limit re end found pos heading cnt flevel)
15007 (unless buffer (error "File not found :%s" file))
15008 (with-current-buffer buffer
15012 (setq limit (point-max))
15013 (goto-char (point-min))
15014 (while (setq heading (pop path))
15015 (setq re (format org-complex-heading-regexp-format
15016 (regexp-quote heading)))
15017 (setq cnt 0 pos (point))
15018 (while (re-search-forward re end t)
15019 (setq level (- (match-end 1) (match-beginning 1)))
15020 (if (and (>= level lmin) (<= level lmax))
15021 (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
15022 (when (= cnt 0) (error "Heading not found on level %d: %s"
15024 (when (> cnt 1) (error "Heading not unique on level %d: %s"
15027 (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
15028 (setq end (save-excursion (org-end-of-subtree t t))))
15029 (when (org-at-heading-p)
15030 (move-marker (make-marker) (point))))))))
15032 (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
15033 "Find node HEADING in BUFFER.
15034 Return a marker to the heading if it was found, or nil if not.
15035 If POS-ONLY is set, return just the position instead of a marker.
15037 The heading text must match exact, but it may have a TODO keyword,
15038 a priority cookie and tags in the standard locations."
15039 (with-current-buffer (or buffer (current-buffer))
15043 (goto-char (point-min))
15044 (let (case-fold-search)
15045 (if (re-search-forward
15046 (format org-complex-heading-regexp-format
15047 (regexp-quote heading)) nil t)
15049 (match-beginning 0)
15050 (move-marker (make-marker) (match-beginning 0)))))))))
15052 (defun org-find-exact-heading-in-directory (heading &optional dir)
15053 "Find Org node headline HEADING in all .org files in directory DIR.
15054 When the target headline is found, return a marker to this location."
15055 (let ((files (directory-files (or dir default-directory)
15056 nil "\\`[^.#].*\\.org\\'"))
15057 file visiting m buffer)
15059 (while (setq file (pop files))
15060 (message "trying %s" file)
15061 (setq visiting (org-find-base-buffer-visiting file))
15062 (setq buffer (or visiting (find-file-noselect file)))
15063 (setq m (org-find-exact-headline-in-buffer
15065 (when (and (not m) (not visiting)) (kill-buffer buffer))
15066 (and m (throw 'found m))))))
15068 (defun org-find-entry-with-id (ident)
15069 "Locate the entry that contains the ID property with exact value IDENT.
15070 IDENT can be a string, a symbol or a number, this function will search for
15071 the string representation of it.
15072 Return the position where this entry starts, or nil if there is no such entry."
15073 (interactive "sID: ")
15075 ((stringp ident) ident)
15076 ((symbol-name ident) (symbol-name ident))
15077 ((numberp ident) (number-to-string ident))
15078 (t (error "IDENT %s must be a string, symbol or number" ident))))
15079 (case-fold-search nil))
15083 (goto-char (point-min))
15084 (when (re-search-forward
15085 (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
15087 (org-back-to-heading t)
15092 (defvar org-last-changed-timestamp nil)
15093 (defvar org-last-inserted-timestamp nil
15094 "The last time stamp inserted with `org-insert-time-stamp'.")
15095 (defvar org-time-was-given) ; dynamically scoped parameter
15096 (defvar org-end-time-was-given) ; dynamically scoped parameter
15097 (defvar org-ts-what) ; dynamically scoped parameter
15099 (defun org-time-stamp (arg &optional inactive)
15100 "Prompt for a date/time and insert a time stamp.
15101 If the user specifies a time like HH:MM or if this command is
15102 called with at least one prefix argument, the time stamp contains
15103 the date and the time. Otherwise, only the date is be included.
15105 All parts of a date not specified by the user is filled in from
15106 the current date/time. So if you just press return without
15107 typing anything, the time stamp will represent the current
15110 If there is already a timestamp at the cursor, it will be
15113 With two universal prefix arguments, insert an active timestamp
15114 with the current time without prompting the user."
15118 ;; Default time is either today, or, when entering a range,
15119 ;; the range start.
15120 (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
15122 (re-search-backward
15123 (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
15124 (- (point) 20) t)))
15125 (apply 'encode-time (org-parse-time-string (match-string 1)))
15127 (default-input (and ts (org-get-compact-tod ts)))
15128 (repeater (save-excursion
15130 (beginning-of-line)
15131 (when (re-search-forward
15132 "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
15133 (save-excursion (progn (end-of-line) (point))) t)
15134 (match-string 0)))))
15135 org-time-was-given org-end-time-was-given time)
15137 ((and (org-at-timestamp-p t)
15138 (memq last-command '(org-time-stamp org-time-stamp-inactive))
15139 (memq this-command '(org-time-stamp org-time-stamp-inactive)))
15141 (setq time (let ((this-command this-command))
15142 (org-read-date arg 'totime nil nil
15143 default-time default-input inactive)))
15144 (org-insert-time-stamp time (or org-time-was-given arg) inactive))
15145 ((org-at-timestamp-p t)
15146 (setq time (let ((this-command this-command))
15147 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15148 (when (org-at-timestamp-p t) ; just to get the match data
15149 ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
15151 (setq org-last-changed-timestamp
15152 (org-insert-time-stamp
15153 time (or org-time-was-given arg)
15154 inactive nil nil (list org-end-time-was-given)))
15155 (when repeater (goto-char (1- (point))) (insert " " repeater)
15156 (setq org-last-changed-timestamp
15157 (concat (substring org-last-inserted-timestamp 0 -1)
15158 " " repeater ">"))))
15159 (message "Timestamp updated"))
15161 (org-insert-time-stamp (current-time) t))
15163 (setq time (let ((this-command this-command))
15164 (org-read-date arg 'totime nil nil default-time default-input inactive)))
15165 (org-insert-time-stamp time (or org-time-was-given arg) inactive
15166 nil nil (list org-end-time-was-given))))))
15168 ;; FIXME: can we use this for something else, like computing time differences?
15169 (defun org-get-compact-tod (s)
15170 (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
15171 (let* ((t1 (match-string 1 s))
15172 (h1 (string-to-number (match-string 2 s)))
15173 (m1 (string-to-number (match-string 3 s)))
15174 (t2 (and (match-end 4) (match-string 5 s)))
15175 (h2 (and t2 (string-to-number (match-string 6 s))))
15176 (m2 (and t2 (string-to-number (match-string 7 s))))
15180 (setq dh (- h2 h1) dm (- m2 m1))
15181 (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
15182 (concat t1 "+" (number-to-string dh)
15183 (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
15185 (defun org-time-stamp-inactive (&optional arg)
15186 "Insert an inactive time stamp.
15187 An inactive time stamp is enclosed in square brackets instead of angle
15188 brackets. It is inactive in the sense that it does not trigger agenda entries,
15189 does not link to the calendar and cannot be changed with the S-cursor keys.
15190 So these are more for recording a certain time/date."
15192 (org-time-stamp arg 'inactive))
15194 (defvar org-date-ovl (make-overlay 1 1))
15195 (overlay-put org-date-ovl 'face 'org-date-selected)
15196 (org-detach-overlay org-date-ovl)
15198 (defvar org-ans1) ; dynamically scoped parameter
15199 (defvar org-ans2) ; dynamically scoped parameter
15201 (defvar org-plain-time-of-day-regexp) ; defined below
15203 (defvar org-overriding-default-time nil) ; dynamically scoped
15204 (defvar org-read-date-overlay nil)
15205 (defvar org-dcst nil) ; dynamically scoped
15206 (defvar org-read-date-history nil)
15207 (defvar org-read-date-final-answer nil)
15208 (defvar org-read-date-analyze-futurep nil)
15209 (defvar org-read-date-analyze-forced-year nil)
15210 (defvar org-read-date-inactive)
15212 (defun org-read-date (&optional org-with-time to-time from-string prompt
15213 default-time default-input inactive)
15214 "Read a date, possibly a time, and make things smooth for the user.
15215 The prompt will suggest to enter an ISO date, but you can also enter anything
15216 which will at least partially be understood by `parse-time-string'.
15217 Unrecognized parts of the date will default to the current day, month, year,
15218 hour and minute. If this command is called to replace a timestamp at point,
15219 or to enter the second timestamp of a range, the default time is taken
15220 from the existing stamp. Furthermore, the command prefers the future,
15221 so if you are giving a date where the year is not given, and the day-month
15222 combination is already past in the current year, it will assume you
15223 mean next year. For details, see the manual. A few examples:
15225 3-2-5 --> 2003-02-05
15226 feb 15 --> currentyear-02-15
15227 2/15 --> currentyear-02-15
15228 sep 12 9 --> 2009-09-12
15229 12:45 --> today 12:45
15230 22 sept 0:34 --> currentyear-09-22 0:34
15231 12 --> currentyear-currentmonth-12
15232 Fri --> nearest Friday (today or later)
15235 Furthermore you can specify a relative date by giving, as the *first* thing
15236 in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
15237 change in days weeks, months, years.
15238 With a single plus or minus, the date is relative to today. With a double
15239 plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
15240 +4d --> four days from today
15241 +4 --> same as above
15242 +2w --> two weeks from today
15243 ++5 --> five days from default date
15245 The function understands only English month and weekday abbreviations.
15247 While prompting, a calendar is popped up - you can also select the
15248 date with the mouse (button 1). The calendar shows a period of three
15249 months. To scroll it to other months, use the keys `>' and `<'.
15250 If you don't like the calendar, turn it off with
15251 \(setq org-read-date-popup-calendar nil)
15253 With optional argument TO-TIME, the date will immediately be converted
15254 to an internal time.
15255 With an optional argument ORG-WITH-TIME, the prompt will suggest to
15256 also insert a time. Note that when ORG-WITH-TIME is not set, you can
15257 still enter a time, and this function will inform the calling routine
15258 about this change. The calling routine may then choose to change the
15259 format used to insert the time stamp into the buffer to include the time.
15260 With optional argument FROM-STRING, read from this string instead from
15261 the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
15262 the time/date that is used for everything that is not specified by the
15264 (require 'parse-time)
15265 (let* ((org-time-stamp-rounding-minutes
15266 (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
15267 (org-dcst org-display-custom-times)
15268 (ct (org-current-time))
15269 (org-def (or org-overriding-default-time default-time ct))
15270 (org-defdecode (decode-time org-def))
15272 (when (< (nth 2 org-defdecode) org-extend-today-until)
15273 (setcar (nthcdr 2 org-defdecode) -1)
15274 (setcar (nthcdr 1 org-defdecode) 59)
15275 (setq org-def (apply 'encode-time org-defdecode)
15276 org-defdecode (decode-time org-def)))))
15277 (calendar-frame-setup nil)
15278 (calendar-setup nil)
15279 (calendar-move-hook nil)
15280 (calendar-view-diary-initially-flag nil)
15281 (calendar-view-holidays-initially-flag nil)
15282 (timestr (format-time-string
15283 (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
15284 (prompt (concat (if prompt (concat prompt " ") "")
15285 (format "Date+time [%s]: " timestr)))
15286 ans (org-ans0 "") org-ans1 org-ans2 final)
15289 (from-string (setq ans from-string))
15290 (org-read-date-popup-calendar
15292 (save-window-excursion
15294 (org-eval-in-calendar '(setq cursor-type nil) t)
15297 (calendar-forward-day (- (time-to-days org-def)
15298 (calendar-absolute-from-gregorian
15299 (calendar-current-date))))
15300 (org-eval-in-calendar nil t)
15301 (let* ((old-map (current-local-map))
15302 (map (copy-keymap calendar-mode-map))
15303 (minibuffer-local-map (copy-keymap minibuffer-local-map)))
15304 (org-defkey map (kbd "RET") 'org-calendar-select)
15305 (org-defkey map [mouse-1] 'org-calendar-select-mouse)
15306 (org-defkey map [mouse-2] 'org-calendar-select-mouse)
15307 (org-defkey minibuffer-local-map [(meta shift left)]
15308 (lambda () (interactive)
15309 (org-eval-in-calendar '(calendar-backward-month 1))))
15310 (org-defkey minibuffer-local-map [(meta shift right)]
15311 (lambda () (interactive)
15312 (org-eval-in-calendar '(calendar-forward-month 1))))
15313 (org-defkey minibuffer-local-map [(meta shift up)]
15314 (lambda () (interactive)
15315 (org-eval-in-calendar '(calendar-backward-year 1))))
15316 (org-defkey minibuffer-local-map [(meta shift down)]
15317 (lambda () (interactive)
15318 (org-eval-in-calendar '(calendar-forward-year 1))))
15319 (org-defkey minibuffer-local-map [?\e (shift left)]
15320 (lambda () (interactive)
15321 (org-eval-in-calendar '(calendar-backward-month 1))))
15322 (org-defkey minibuffer-local-map [?\e (shift right)]
15323 (lambda () (interactive)
15324 (org-eval-in-calendar '(calendar-forward-month 1))))
15325 (org-defkey minibuffer-local-map [?\e (shift up)]
15326 (lambda () (interactive)
15327 (org-eval-in-calendar '(calendar-backward-year 1))))
15328 (org-defkey minibuffer-local-map [?\e (shift down)]
15329 (lambda () (interactive)
15330 (org-eval-in-calendar '(calendar-forward-year 1))))
15331 (org-defkey minibuffer-local-map [(shift up)]
15332 (lambda () (interactive)
15333 (org-eval-in-calendar '(calendar-backward-week 1))))
15334 (org-defkey minibuffer-local-map [(shift down)]
15335 (lambda () (interactive)
15336 (org-eval-in-calendar '(calendar-forward-week 1))))
15337 (org-defkey minibuffer-local-map [(shift left)]
15338 (lambda () (interactive)
15339 (org-eval-in-calendar '(calendar-backward-day 1))))
15340 (org-defkey minibuffer-local-map [(shift right)]
15341 (lambda () (interactive)
15342 (org-eval-in-calendar '(calendar-forward-day 1))))
15343 (org-defkey minibuffer-local-map ">"
15344 (lambda () (interactive)
15345 (org-eval-in-calendar '(scroll-calendar-left 1))))
15346 (org-defkey minibuffer-local-map "<"
15347 (lambda () (interactive)
15348 (org-eval-in-calendar '(scroll-calendar-right 1))))
15349 (org-defkey minibuffer-local-map "\C-v"
15350 (lambda () (interactive)
15351 (org-eval-in-calendar
15352 '(calendar-scroll-left-three-months 1))))
15353 (org-defkey minibuffer-local-map "\M-v"
15354 (lambda () (interactive)
15355 (org-eval-in-calendar
15356 '(calendar-scroll-right-three-months 1))))
15357 (run-hooks 'org-read-date-minibuffer-setup-hook)
15360 (use-local-map map)
15361 (setq org-read-date-inactive inactive)
15362 (add-hook 'post-command-hook 'org-read-date-display)
15363 (setq org-ans0 (read-string prompt default-input
15364 'org-read-date-history nil))
15365 ;; org-ans0: from prompt
15366 ;; org-ans1: from mouse click
15367 ;; org-ans2: from calendar motion
15368 (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
15369 (remove-hook 'post-command-hook 'org-read-date-display)
15370 (use-local-map old-map)
15371 (when org-read-date-overlay
15372 (delete-overlay org-read-date-overlay)
15373 (setq org-read-date-overlay nil)))))
15374 (bury-buffer "*Calendar*")))))
15376 (t ; Naked prompt only
15378 (setq ans (read-string prompt default-input
15379 'org-read-date-history timestr))
15380 (when org-read-date-overlay
15381 (delete-overlay org-read-date-overlay)
15382 (setq org-read-date-overlay nil)))))
15384 (setq final (org-read-date-analyze ans org-def org-defdecode))
15386 (when org-read-date-analyze-forced-year
15387 (message "Year was forced into %s"
15388 (if org-read-date-force-compatible-dates
15389 "compatible range (1970-2037)"
15390 "range representable on this machine"))
15393 ;; One round trip to get rid of 34th of August and stuff like that....
15394 (setq final (decode-time (apply 'encode-time final)))
15396 (setq org-read-date-final-answer ans)
15399 (apply 'encode-time final)
15400 (if (and (boundp 'org-time-was-given) org-time-was-given)
15401 (format "%04d-%02d-%02d %02d:%02d"
15402 (nth 5 final) (nth 4 final) (nth 3 final)
15403 (nth 2 final) (nth 1 final))
15404 (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
15407 (defvar org-defdecode)
15408 (defvar org-with-time)
15409 (defun org-read-date-display ()
15410 "Display the current date prompt interpretation in the minibuffer."
15411 (when org-read-date-display-live
15412 (when org-read-date-overlay
15413 (delete-overlay org-read-date-overlay))
15414 (when (minibufferp (current-buffer))
15417 (while (not (equal (buffer-substring
15418 (max (point-min) (- (point) 4)) (point))
15421 (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
15422 " " (or org-ans1 org-ans2)))
15423 (org-end-time-was-given nil)
15424 (f (org-read-date-analyze ans org-def org-defdecode))
15426 org-time-stamp-custom-formats
15427 org-time-stamp-formats))
15428 (fmt (if (or org-with-time
15429 (and (boundp 'org-time-was-given) org-time-was-given))
15432 (txt (format-time-string fmt (apply 'encode-time f)))
15433 (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
15434 (txt (concat "=> " txt)))
15435 (when (and org-end-time-was-given
15436 (string-match org-plain-time-of-day-regexp txt))
15437 (setq txt (concat (substring txt 0 (match-end 0)) "-"
15438 org-end-time-was-given
15439 (substring txt (match-end 0)))))
15440 (when org-read-date-analyze-futurep
15441 (setq txt (concat txt " (=>F)")))
15442 (setq org-read-date-overlay
15443 (make-overlay (1- (point-at-eol)) (point-at-eol)))
15444 (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
15446 (defun org-read-date-analyze (ans org-def org-defdecode)
15447 "Analyze the combined answer of the date prompt."
15448 ;; FIXME: cleanup and comment
15449 (let ((nowdecode (decode-time (current-time)))
15450 delta deltan deltaw deltadef year month day
15451 hour minute second wday pm h2 m2 tl wday1
15452 iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
15453 (setq org-read-date-analyze-futurep nil
15454 org-read-date-analyze-forced-year nil)
15455 (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
15458 (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
15459 (setq ans (replace-match "" t t ans)
15461 deltaw (nth 1 delta)
15462 deltadef (nth 2 delta)))
15464 ;; Check if there is an iso week date in there. If yes, store the
15465 ;; info and postpone interpreting it until the rest of the parsing
15467 (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
15468 (setq iso-year (if (match-end 1)
15469 (org-small-year-to-year
15470 (string-to-number (match-string 1 ans))))
15471 iso-weekday (if (match-end 3)
15472 (string-to-number (match-string 3 ans)))
15473 iso-week (string-to-number (match-string 2 ans)))
15474 (setq ans (replace-match "" t t ans)))
15476 ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
15477 (when (string-match
15478 "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
15479 (setq year (if (match-end 2)
15480 (string-to-number (match-string 2 ans))
15481 (progn (setq kill-year t)
15482 (string-to-number (format-time-string "%Y"))))
15483 month (string-to-number (match-string 3 ans))
15484 day (string-to-number (match-string 4 ans)))
15485 (if (< year 100) (setq year (+ 2000 year)))
15486 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15489 ;; Help matching dotted european dates
15490 (when (string-match
15491 "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
15492 (setq year (if (match-end 3)
15493 (string-to-number (match-string 3 ans))
15494 (progn (setq kill-year t)
15495 (string-to-number (format-time-string "%Y"))))
15496 day (string-to-number (match-string 1 ans))
15497 month (string-to-number (match-string 2 ans))
15498 ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15501 ;; Help matching american dates, like 5/30 or 5/30/7
15502 (when (string-match
15503 "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
15504 (setq year (if (match-end 4)
15505 (string-to-number (match-string 4 ans))
15506 (progn (setq kill-year t)
15507 (string-to-number (format-time-string "%Y"))))
15508 month (string-to-number (match-string 1 ans))
15509 day (string-to-number (match-string 2 ans)))
15510 (if (< year 100) (setq year (+ 2000 year)))
15511 (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
15513 ;; Help matching am/pm times, because `parse-time-string' does not do that.
15514 ;; If there is a time with am/pm, and *no* time without it, we convert
15515 ;; so that matching will be successful.
15516 (loop for i from 1 to 2 do ; twice, for end time as well
15517 (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
15518 (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
15519 (setq hour (string-to-number (match-string 1 ans))
15520 minute (if (match-end 3)
15521 (string-to-number (match-string 3 ans))
15524 (string-to-char (downcase (match-string 4 ans)))))
15525 (if (and (= hour 12) (not pm))
15527 (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
15528 (setq ans (replace-match (format "%02d:%02d" hour minute)
15531 ;; Check if a time range is given as a duration
15532 (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
15533 (setq hour (string-to-number (match-string 1 ans))
15534 h2 (+ hour (string-to-number (match-string 3 ans)))
15535 minute (string-to-number (match-string 2 ans))
15536 m2 (+ minute (if (match-end 5) (string-to-number
15537 (match-string 5 ans))0)))
15538 (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
15539 (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
15542 ;; Check if there is a time range
15543 (when (boundp 'org-end-time-was-given)
15544 (setq org-time-was-given nil)
15545 (when (and (string-match org-plain-time-of-day-regexp ans)
15547 (setq org-end-time-was-given (match-string 8 ans))
15548 (setq ans (concat (substring ans 0 (match-beginning 7))
15549 (substring ans (match-end 7))))))
15551 (setq tl (parse-time-string ans)
15552 day (or (nth 3 tl) (nth 3 org-defdecode))
15553 month (or (nth 4 tl)
15554 (if (and org-read-date-prefer-future
15555 (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
15556 (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
15557 (nth 4 org-defdecode)))
15558 year (or (and (not kill-year) (nth 5 tl))
15559 (if (and org-read-date-prefer-future
15560 (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
15561 (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
15562 (nth 5 org-defdecode)))
15563 hour (or (nth 2 tl) (nth 2 org-defdecode))
15564 minute (or (nth 1 tl) (nth 1 org-defdecode))
15565 second (or (nth 0 tl) 0)
15568 (when (and (eq org-read-date-prefer-future 'time)
15569 (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
15570 (equal day (nth 3 nowdecode))
15571 (equal month (nth 4 nowdecode))
15572 (equal year (nth 5 nowdecode))
15574 (or (< (nth 2 tl) (nth 2 nowdecode))
15575 (and (= (nth 2 tl) (nth 2 nowdecode))
15577 (< (nth 1 tl) (nth 1 nowdecode)))))
15581 ;; Special date definitions below
15584 ;; There was an iso week
15587 (setq year (or iso-year year)
15588 day (or iso-weekday wday 1)
15589 wday nil ; to make sure that the trigger below does not match
15590 iso-date (calendar-gregorian-from-absolute
15591 (calendar-absolute-from-iso
15592 (list iso-week day year))))
15593 ; FIXME: Should we also push ISO weeks into the future?
15594 ; (when (and org-read-date-prefer-future
15596 ; (< (calendar-absolute-from-gregorian iso-date)
15597 ; (time-to-days (current-time))))
15598 ; (setq year (1+ year)
15599 ; iso-date (calendar-gregorian-from-absolute
15600 ; (calendar-absolute-from-iso
15601 ; (list iso-week day year)))))
15602 (setq month (car iso-date)
15603 year (nth 2 iso-date)
15604 day (nth 1 iso-date)))
15608 (let ((now (decode-time (current-time))))
15609 (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
15610 (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
15611 ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
15612 ((equal deltaw "m") (setq month (+ month deltan)))
15613 ((equal deltaw "y") (setq year (+ year deltan)))))
15614 ((and wday (not (nth 3 tl)))
15615 ;; Weekday was given, but no day, so pick that day in the week
15616 ;; on or after the derived date.
15617 (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
15618 (unless (equal wday wday1)
15619 (setq day (+ day (% (- wday wday1 -7) 7))))))
15620 (if (and (boundp 'org-time-was-given)
15622 (setq org-time-was-given t))
15623 (if (< year 100) (setq year (+ 2000 year)))
15624 ;; Check of the date is representable
15625 (if org-read-date-force-compatible-dates
15628 (setq year 1970 org-read-date-analyze-forced-year t))
15630 (setq year 2037 org-read-date-analyze-forced-year t)))
15631 (condition-case nil
15632 (ignore (encode-time second minute hour day month year))
15634 (setq year (nth 5 org-defdecode))
15635 (setq org-read-date-analyze-forced-year t))))
15636 (setq org-read-date-analyze-futurep futurep)
15637 (list second minute hour day month year)))
15639 (defvar parse-time-weekdays)
15640 (defun org-read-date-get-relative (s today default)
15641 "Check string S for special relative date string.
15642 TODAY and DEFAULT are internal times, for today and for a default.
15643 Return shift list (N what def-flag)
15644 WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
15645 N is the number of WHATs to shift.
15646 DEF-FLAG is t when a double ++ or -- indicates shift relative to
15647 the DEFAULT date rather than TODAY."
15648 (require 'parse-time)
15652 "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
15654 "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
15655 "\\([ \t]\\|$\\)") s)
15656 (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
15657 (let* ((dir (if (> (match-end 1) (match-beginning 1))
15658 (string-to-char (substring (match-string 1 s) -1))
15660 (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
15661 (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
15662 (what (if (match-end 3) (match-string 3 s) "d"))
15663 (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
15664 (date (if rel default today))
15665 (wday (nth 6 (decode-time date)))
15669 (setq delta (mod (+ 7 (- wday1 wday)) 7))
15670 (if (= dir ?-) (setq delta (- delta 7)))
15671 (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
15672 (list delta "d" rel))
15673 (list (* n (if (= dir ?-) -1 1)) what rel)))))
15675 (defun org-order-calendar-date-args (arg1 arg2 arg3)
15676 "Turn a user-specified date into the internal representation.
15677 The internal representation needed by the calendar is (month day year).
15678 This is a wrapper to handle the brain-dead convention in calendar that
15679 user function argument order change dependent on argument order."
15680 (if (boundp 'calendar-date-style)
15682 ((eq calendar-date-style 'american)
15683 (list arg1 arg2 arg3))
15684 ((eq calendar-date-style 'european)
15685 (list arg2 arg1 arg3))
15686 ((eq calendar-date-style 'iso)
15687 (list arg2 arg3 arg1)))
15688 (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
15689 (if (org-bound-and-true-p european-calendar-style)
15690 (list arg2 arg1 arg3)
15691 (list arg1 arg2 arg3)))))
15693 (defun org-eval-in-calendar (form &optional keepdate)
15694 "Eval FORM in the calendar window and return to current window.
15695 When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
15696 otherwise stick to the current value of `org-ans2'."
15697 (let ((sf (selected-frame))
15698 (sw (selected-window)))
15699 (select-window (get-buffer-window "*Calendar*" t))
15701 (when (and (not keepdate) (calendar-cursor-to-date))
15702 (let* ((date (calendar-cursor-to-date))
15703 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15704 (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
15705 (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
15707 (org-select-frame-set-input-focus sf)))
15709 (defun org-calendar-select ()
15710 "Return to `org-read-date' with the date currently selected.
15711 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15713 (when (calendar-cursor-to-date)
15714 (let* ((date (calendar-cursor-to-date))
15715 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15716 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15717 (if (active-minibuffer-window) (exit-minibuffer))))
15719 (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
15720 "Insert a date stamp for the date given by the internal TIME.
15721 WITH-HM means use the stamp format that includes the time of the day.
15722 INACTIVE means use square brackets instead of angular ones, so that the
15723 stamp will not contribute to the agenda.
15724 PRE and POST are optional strings to be inserted before and after the
15726 The command returns the inserted time stamp."
15727 (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
15729 (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
15730 (insert-before-markers (or pre ""))
15731 (when (listp extra)
15732 (setq extra (car extra))
15733 (if (and (stringp extra)
15734 (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
15735 (setq extra (format "-%02d:%02d"
15736 (string-to-number (match-string 1 extra))
15737 (string-to-number (match-string 2 extra))))
15740 (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
15741 (insert-before-markers (setq stamp (format-time-string fmt time)))
15742 (insert-before-markers (or post ""))
15743 (setq org-last-inserted-timestamp stamp)))
15745 (defun org-toggle-time-stamp-overlays ()
15746 "Toggle the use of custom time stamp formats."
15748 (setq org-display-custom-times (not org-display-custom-times))
15749 (unless org-display-custom-times
15750 (let ((p (point-min)) (bmp (buffer-modified-p)))
15751 (while (setq p (next-single-property-change p 'display))
15752 (if (and (get-text-property p 'display)
15753 (eq (get-text-property p 'face) 'org-date))
15754 (remove-text-properties
15755 p (setq p (next-single-property-change p 'display))
15757 (set-buffer-modified-p bmp)))
15758 (if (featurep 'xemacs)
15759 (remove-text-properties (point-min) (point-max) '(end-glyph t)))
15760 (org-restart-font-lock)
15761 (setq org-table-may-need-update t)
15762 (if org-display-custom-times
15763 (message "Time stamps are overlaid with custom format")
15764 (message "Time stamp overlays removed")))
15766 (defun org-display-custom-time (beg end)
15767 "Overlay modified time stamp format over timestamp between BEG and END."
15768 (let* ((ts (buffer-substring beg end))
15769 t1 w1 with-hm tf time str w2 (off 0))
15771 (setq t1 (org-parse-time-string ts t))
15772 (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
15773 (setq off (- (match-end 0) (match-beginning 0)))))
15774 (setq end (- end off))
15775 (setq w1 (- end beg)
15776 with-hm (and (nth 1 t1) (nth 2 t1))
15777 tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
15778 time (org-fix-decoded-time t1)
15780 (format-time-string
15781 (substring tf 1 -1) (apply 'encode-time time))
15782 nil 'mouse-face 'highlight)
15784 (if (not (= w2 w1))
15785 (add-text-properties (1+ beg) (+ 2 beg)
15786 (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
15787 (if (featurep 'xemacs)
15789 (put-text-property beg end 'invisible t)
15790 (put-text-property beg end 'end-glyph (make-glyph str)))
15791 (put-text-property beg end 'display str))))
15793 (defun org-translate-time (string)
15794 "Translate all timestamps in STRING to custom format.
15795 But do this only if the variable `org-display-custom-times' is set."
15796 (when org-display-custom-times
15799 (re org-ts-regexp-both)
15800 t1 with-hm inactive tf time str beg end)
15801 (while (setq start (string-match re string start))
15802 (setq beg (match-beginning 0)
15804 t1 (save-match-data
15805 (org-parse-time-string (substring string beg end) t))
15806 with-hm (and (nth 1 t1) (nth 2 t1))
15807 inactive (equal (substring string beg (1+ beg)) "[")
15808 tf (funcall (if with-hm 'cdr 'car)
15809 org-time-stamp-custom-formats)
15810 time (org-fix-decoded-time t1)
15811 str (format-time-string
15813 (if inactive "[" "<") (substring tf 1 -1)
15814 (if inactive "]" ">"))
15815 (apply 'encode-time time))
15816 string (replace-match str t t string)
15817 start (+ start (length str)))))))
15820 (defun org-fix-decoded-time (time)
15821 "Set 0 instead of nil for the first 6 elements of time.
15822 Don't touch the rest."
15824 (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
15826 (defun org-days-to-time (timestamp-string)
15827 "Difference between TIMESTAMP-STRING and now in days."
15828 (- (time-to-days (org-time-string-to-time timestamp-string))
15829 (time-to-days (current-time))))
15831 (defun org-deadline-close (timestamp-string &optional ndays)
15832 "Is the time in TIMESTAMP-STRING close to the current date?"
15833 (setq ndays (or ndays (org-get-wdays timestamp-string)))
15834 (and (< (org-days-to-time timestamp-string) ndays)
15835 (not (org-entry-is-done-p))))
15837 (defun org-get-wdays (ts)
15838 "Get the deadline lead time appropriate for timestring TS."
15840 ((<= org-deadline-warning-days 0)
15841 ;; 0 or negative, enforce this value no matter what
15842 (- org-deadline-warning-days))
15843 ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
15844 ;; lead time is specified.
15845 (floor (* (string-to-number (match-string 1 ts))
15846 (cdr (assoc (match-string 2 ts)
15847 '(("d" . 1) ("w" . 7)
15848 ("m" . 30.4) ("y" . 365.25)))))))
15849 ;; go for the default.
15850 (t org-deadline-warning-days)))
15852 (defun org-calendar-select-mouse (ev)
15853 "Return to `org-read-date' with the date currently selected.
15854 This is used by `org-read-date' in a temporary keymap for the calendar buffer."
15856 (mouse-set-point ev)
15857 (when (calendar-cursor-to-date)
15858 (let* ((date (calendar-cursor-to-date))
15859 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
15860 (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
15861 (if (active-minibuffer-window) (exit-minibuffer))))
15863 (defun org-check-deadlines (ndays)
15864 "Check if there are any deadlines due or past due.
15865 A deadline is considered due if it happens within `org-deadline-warning-days'
15866 days from today's date. If the deadline appears in an entry marked DONE,
15867 it is not shown. The prefix arg NDAYS can be used to test that many
15868 days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
15870 (let* ((org-warn-days
15872 ((equal ndays '(4)) 100000)
15873 (ndays (prefix-numeric-value ndays))
15874 (t (abs org-deadline-warning-days))))
15875 (case-fold-search nil)
15876 (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15878 (lambda () (org-deadline-close (match-string 1) org-warn-days))))
15880 (message "%d deadlines past-due or due within %d days"
15881 (org-occur regexp nil callback)
15884 (defsubst org-re-timestamp (type)
15885 "Return a regexp for timestamp TYPE.
15886 Allowed values for TYPE are:
15888 all: all timestamps
15889 active: only active timestamps (<...>)
15890 inactive: only inactive timestamps ([...])
15891 scheduled: only scheduled timestamps
15892 deadline: only deadline timestamps
15894 When TYPE is nil, fall back on returning a regexp that matches
15895 both scheduled and deadline timestamps."
15896 (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9>
\n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
15897 ((eq type 'active) org-ts-regexp)
15898 ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^
\n>]*?\\)\\]")
15899 ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
15900 ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
15901 ((eq type 'scheduled-or-deadline)
15902 (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
15904 (defun org-check-before-date (date)
15905 "Check if there are deadlines or scheduled entries before DATE."
15906 (interactive (list (org-read-date)))
15907 (let ((case-fold-search nil)
15908 (regexp (org-re-timestamp org-ts-type))
15910 (lambda () (time-less-p
15911 (org-time-string-to-time (match-string 1))
15912 (org-time-string-to-time date)))))
15913 (message "%d entries before %s"
15914 (org-occur regexp nil callback) date)))
15916 (defun org-check-after-date (date)
15917 "Check if there are deadlines or scheduled entries after DATE."
15918 (interactive (list (org-read-date)))
15919 (let ((case-fold-search nil)
15920 (regexp (org-re-timestamp org-ts-type))
15924 (org-time-string-to-time (match-string 1))
15925 (org-time-string-to-time date))))))
15926 (message "%d entries after %s"
15927 (org-occur regexp nil callback) date)))
15929 (defun org-check-dates-range (start-date end-date)
15930 "Check for deadlines/scheduled entries between START-DATE and END-DATE."
15931 (interactive (list (org-read-date nil nil nil "Range starts")
15932 (org-read-date nil nil nil "Range end")))
15933 (let ((case-fold-search nil)
15934 (regexp (org-re-timestamp org-ts-type))
15937 (let ((match (match-string 1)))
15940 (org-time-string-to-time match)
15941 (org-time-string-to-time start-date)))
15943 (org-time-string-to-time match)
15944 (org-time-string-to-time end-date)))))))
15945 (message "%d entries between %s and %s"
15946 (org-occur regexp nil callback) start-date end-date)))
15948 (defun org-evaluate-time-range (&optional to-buffer)
15949 "Evaluate a time range by computing the difference between start and end.
15950 Normally the result is just printed in the echo area, but with prefix arg
15951 TO-BUFFER, the result is inserted just after the date stamp into the buffer.
15952 If the time range is actually in a table, the result is inserted into the
15954 For time difference computation, a year is assumed to be exactly 365
15955 days in order to avoid rounding problems."
15958 (org-clock-update-time-maybe)
15960 (unless (org-at-date-range-p t)
15961 (goto-char (point-at-bol))
15962 (re-search-forward org-tr-regexp-both (point-at-eol) t))
15963 (if (not (org-at-date-range-p t))
15964 (error "Not at a time-stamp range, and none found in current line")))
15965 (let* ((ts1 (match-string 1))
15966 (ts2 (match-string 2))
15967 (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
15968 (match-end (match-end 0))
15969 (time1 (org-time-string-to-time ts1))
15970 (time2 (org-time-string-to-time ts2))
15971 (t1 (org-float-time time1))
15972 (t2 (org-float-time time2))
15973 (diff (abs (- t2 t1)))
15974 (negative (< (- t2 t1) 0))
15975 ;; (ys (floor (* 365 24 60 60)))
15978 (fy "%dy %dd %02d:%02d")
15980 (fd "%dd %02d:%02d")
15985 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15987 d (floor (/ diff ds)) diff (mod diff ds)
15988 h (floor (/ diff hs)) diff (mod diff hs)
15989 m (floor (/ diff 60)))
15990 (setq ; y (floor (/ diff ys)) diff (mod diff ys)
15992 d (floor (+ (/ diff ds) 0.5))
15994 (if (not to-buffer)
15995 (message "%s" (org-make-tdiff-string y d h m))
15996 (if (org-at-table-p)
15998 (goto-char match-end)
16000 (and (looking-at " *|") (goto-char (match-end 0))))
16001 (goto-char match-end))
16003 "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
16004 (replace-match ""))
16005 (if negative (insert " -"))
16006 (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
16007 (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
16008 (insert " " (format fh h m))))
16009 (if align (org-table-align))
16010 (message "Time difference inserted")))))
16012 (defun org-make-tdiff-string (y d h m)
16015 (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
16017 (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
16019 (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
16021 (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
16023 (apply 'format fmt (nreverse l))))
16025 (defun org-time-string-to-time (s &optional buffer pos)
16026 "Convert a timestamp string into internal time."
16027 (condition-case errdata
16028 (apply 'encode-time (org-parse-time-string s))
16029 (error (error "Bad timestamp `%s'%s\nError was: %s"
16030 s (if (not (and buffer pos))
16032 (format " at %d in buffer `%s'" pos buffer))
16035 (defun org-time-string-to-seconds (s)
16036 "Convert a timestamp string to a number of seconds."
16037 (org-float-time (org-time-string-to-time s)))
16039 (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
16040 "Convert a time stamp to an absolute day number.
16041 If there is a specifier for a cyclic time stamp, get the closest date to
16043 PREFER and SHOW-ALL are passed through to `org-closest-date'.
16044 The variable date is bound by the calendar when this is called."
16046 ((and daynr (string-match "\\`%%\\((.*)\\)" s))
16047 (if (org-diary-sexp-entry (match-string 1 s) "" date)
16050 ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
16051 (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
16052 (time-to-days (current-time))) (match-string 0 s)
16055 (condition-case errdata
16056 (apply 'encode-time (org-parse-time-string s))
16057 (error (error "Bad timestamp `%s'%s\nError was: %s"
16058 s (if (not (and buffer pos))
16060 (format " at %d in buffer `%s'" pos buffer))
16061 (cdr errdata))))))))
16063 (defun org-days-to-iso-week (days)
16064 "Return the iso week number."
16066 (car (calendar-iso-from-absolute days)))
16068 (defun org-small-year-to-year (year)
16069 "Convert 2-digit years into 4-digit years.
16070 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
16071 The year 2000 cannot be abbreviated. Any year larger than 99
16072 is returned unchanged."
16074 (setq year (+ 2000 year))
16076 (setq year (+ 1900 year))))
16079 (defun org-time-from-absolute (d)
16080 "Return the time corresponding to date D.
16081 D may be an absolute day number, or a calendar-type list (month day year)."
16082 (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
16083 (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
16085 (defun org-calendar-holiday ()
16086 "List of holidays, for Diary display in Org-mode."
16087 (require 'holidays)
16089 (if (fboundp 'calendar-check-holidays)
16090 'calendar-check-holidays 'check-calendar-holidays) date)))
16091 (if hl (mapconcat 'identity hl "; "))))
16093 (defun org-diary-sexp-entry (sexp entry date)
16094 "Process a SEXP diary ENTRY for DATE."
16095 (require 'diary-lib)
16096 (let ((result (if calendar-debug-sexp
16097 (let ((stack-trace-on-error t))
16098 (eval (car (read-from-string sexp))))
16099 (condition-case nil
16100 (eval (car (read-from-string sexp)))
16103 (message "Bad sexp at line %d in %s: %s"
16105 (buffer-file-name) sexp)
16107 (cond ((stringp result) (split-string result "; "))
16108 ((and (consp result)
16109 (not (consp (cdr result)))
16110 (stringp (cdr result))) (cdr result))
16111 ((and (consp result)
16112 (stringp (car result))) result)
16115 (defun org-diary-to-ical-string (frombuf)
16116 "Get iCalendar entries from diary entries in buffer FROMBUF.
16117 This uses the icalendar.el library."
16118 (let* ((tmpdir (if (featurep 'xemacs)
16120 temporary-file-directory))
16121 (tmpfile (make-temp-name
16122 (expand-file-name "orgics" tmpdir)))
16124 (with-current-buffer frombuf
16125 (icalendar-export-region (point-min) (point-max) tmpfile)
16126 (setq buf (find-buffer-visiting tmpfile))
16128 (goto-char (point-min))
16129 (if (re-search-forward "^BEGIN:VEVENT" nil t)
16130 (setq b (match-beginning 0)))
16131 (goto-char (point-max))
16132 (if (re-search-backward "^END:VEVENT" nil t)
16133 (setq e (match-end 0)))
16134 (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
16136 (delete-file tmpfile)
16139 (defun org-closest-date (start current change prefer show-all)
16140 "Find the date closest to CURRENT that is consistent with START and CHANGE.
16141 When PREFER is `past', return a date that is either CURRENT or past.
16142 When PREFER is `future', return a date that is either CURRENT or future.
16143 When SHOW-ALL is nil, only return the current occurrence of a time stamp."
16144 ;; Make the proper lists from the dates
16146 (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
16147 dn dw sday cday n1 n2 n0
16148 d m y y1 y2 date1 date2 nmonths nm ny m2)
16150 (setq start (org-date-to-gregorian start)
16151 current (org-date-to-gregorian
16154 (time-to-days (current-time))))
16155 sday (calendar-absolute-from-gregorian start)
16156 cday (calendar-absolute-from-gregorian current))
16158 (if (<= cday sday) (throw 'exit sday))
16160 (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
16161 (setq dn (string-to-number (match-string 1 change))
16162 dw (cdr (assoc (match-string 2 change) a1)))
16163 (error "Invalid change specifier: %s" change))
16164 (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
16167 (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
16170 (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
16171 (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
16172 (setq date1 (list m d y1)
16173 n1 (calendar-absolute-from-gregorian date1)
16174 date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
16175 n2 (calendar-absolute-from-gregorian date2)))
16177 ;; approx number of month between the two dates
16178 (setq nmonths (floor (/ (- cday sday) 30.436875)))
16179 ;; How often does dn fit in there?
16180 (setq d (nth 1 start) m (car start) y (nth 2 start)
16181 nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
16183 ny (floor (/ m 12))
16186 (while (> m 12) (setq m (- m 12) y (1+ y)))
16187 (setq n1 (calendar-absolute-from-gregorian (list m d y)))
16188 (setq m2 (+ m dn) y2 y)
16189 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16190 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
16191 (while (<= n2 cday)
16192 (setq n1 n2 m m2 y y2)
16193 (setq m2 (+ m dn) y2 y)
16194 (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
16195 (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
16196 ;; Make sure n1 is the earlier date
16197 (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
16200 ((eq prefer 'past) (if (= cday n2) n2 n1))
16201 ((eq prefer 'future) (if (= cday n1) n1 n2))
16202 (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
16204 ((eq prefer 'past) (if (= cday n2) n2 n1))
16205 ((eq prefer 'future) (if (= cday n1) n1 n2))
16206 (t (if (= cday n1) n1 n2)))))))
16208 (defun org-date-to-gregorian (date)
16209 "Turn any specification of DATE into a Gregorian date for the calendar."
16210 (cond ((integerp date) (calendar-gregorian-from-absolute date))
16211 ((and (listp date) (= (length date) 3)) date)
16213 (setq date (org-parse-time-string date))
16214 (list (nth 4 date) (nth 3 date) (nth 5 date)))
16216 (list (nth 4 date) (nth 3 date) (nth 5 date)))))
16218 (defun org-parse-time-string (s &optional nodefault)
16219 "Parse the standard Org-mode time string.
16220 This should be a lot faster than the normal `parse-time-string'.
16221 If time is not given, defaults to 0:00. However, with optional NODEFAULT,
16222 hour and minute fields will be nil if not given."
16223 (if (string-match org-ts-regexp0 s)
16225 (if (or (match-beginning 8) (not nodefault))
16226 (string-to-number (or (match-string 8 s) "0")))
16227 (if (or (match-beginning 7) (not nodefault))
16228 (string-to-number (or (match-string 7 s) "0")))
16229 (string-to-number (match-string 4 s))
16230 (string-to-number (match-string 3 s))
16231 (string-to-number (match-string 2 s))
16233 (error "Not a standard Org-mode time string: %s" s)))
16235 (defun org-timestamp-up (&optional arg)
16236 "Increase the date item at the cursor by one.
16237 If the cursor is on the year, change the year. If it is on the month,
16238 the day or the time, change that.
16239 With prefix ARG, change by that many units."
16241 (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
16243 (defun org-timestamp-down (&optional arg)
16244 "Decrease the date item at the cursor by one.
16245 If the cursor is on the year, change the year. If it is on the month,
16246 the day or the time, change that.
16247 With prefix ARG, change by that many units."
16249 (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
16251 (defun org-timestamp-up-day (&optional arg)
16252 "Increase the date in the time stamp by one day.
16253 With prefix ARG, change that many days."
16255 (if (and (not (org-at-timestamp-p t))
16256 (org-at-heading-p))
16258 (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
16260 (defun org-timestamp-down-day (&optional arg)
16261 "Decrease the date in the time stamp by one day.
16262 With prefix ARG, change that many days."
16264 (if (and (not (org-at-timestamp-p t))
16265 (org-at-heading-p))
16267 (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
16269 (defun org-at-timestamp-p (&optional inactive-ok)
16270 "Determine if the cursor is in or at a timestamp."
16272 (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
16274 (ans (or (looking-at tsr)
16276 (skip-chars-backward "^[<\n\r\t")
16277 (if (> (point) (point-min)) (backward-char 1))
16278 (and (looking-at tsr)
16279 (> (- (match-end 0) pos) -1))))))
16281 (boundp 'org-ts-what)
16284 ((= pos (match-beginning 0)) 'bracket)
16285 ;; Point is considered to be "on the bracket" whether
16286 ;; it's really on it or right after it.
16287 ((= pos (1- (match-end 0))) 'bracket)
16288 ((= pos (match-end 0)) 'after)
16289 ((org-pos-in-match-range pos 2) 'year)
16290 ((org-pos-in-match-range pos 3) 'month)
16291 ((org-pos-in-match-range pos 7) 'hour)
16292 ((org-pos-in-match-range pos 8) 'minute)
16293 ((or (org-pos-in-match-range pos 4)
16294 (org-pos-in-match-range pos 5)) 'day)
16295 ((and (> pos (or (match-end 8) (match-end 5)))
16296 (< pos (match-end 0)))
16297 (- pos (or (match-end 8) (match-end 5))))
16301 (defun org-toggle-timestamp-type ()
16302 "Toggle the type (<active> or [inactive]) of a time stamp."
16304 (when (org-at-timestamp-p t)
16305 (let ((beg (match-beginning 0)) (end (match-end 0))
16306 (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
16309 (while (re-search-forward "[][<>]" end t)
16310 (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
16312 (message "Timestamp is now %sactive"
16313 (if (equal (char-after beg) ?<) "" "in")))))
16315 (defvar org-clock-history) ; defined in org-clock.el
16316 (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
16317 (defun org-timestamp-change (n &optional what updown)
16318 "Change the date in the time stamp at point.
16319 The date will be changed by N times WHAT. WHAT can be `day', `month',
16320 `year', `minute', `second'. If WHAT is not given, the cursor position
16321 in the timestamp determines what will be changed."
16322 (let ((origin (point)) origin-cat
16324 (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
16327 ts time time0 fixnext clrgx)
16328 (if (not (org-at-timestamp-p t))
16329 (error "Not at a timestamp"))
16330 (if (and (not what) (eq org-ts-what 'bracket))
16331 (org-toggle-timestamp-type)
16332 ;; Point isn't on brackets. Remember the part of the time-stamp
16333 ;; the point was in. Indeed, size of time-stamps may change,
16334 ;; but point must be kept in the same category nonetheless.
16335 (setq origin-cat org-ts-what)
16336 (if (and (not what) (not (eq org-ts-what 'day))
16337 org-display-custom-times
16338 (get-text-property (point) 'display)
16339 (not (get-text-property (1- (point)) 'display)))
16340 (setq org-ts-what 'day))
16341 (setq org-ts-what (or what org-ts-what)
16342 inactive (= (char-after (match-beginning 0)) ?\[)
16343 ts (match-string 0))
16346 "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
16348 (setq extra (match-string 1 ts)))
16349 (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
16351 (setq time0 (org-parse-time-string ts))
16353 (eq org-ts-what 'minute)
16354 (not current-prefix-arg))
16355 ;; This looks like s-up and s-down. Change by one rounding step.
16356 (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
16357 (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
16358 (setcar (cdr time0) (+ (nth 1 time0)
16359 (if (> n 0) (- rem) (- dm rem))))))
16361 (encode-time (or (car time0) 0)
16362 (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
16363 (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
16364 (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
16365 (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
16366 (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
16368 (when (and (member org-ts-what '(hour minute))
16370 (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
16371 (setq extra (org-modify-ts-extra
16373 (if (eq org-ts-what 'hour) 2 5)
16375 (when (integerp org-ts-what)
16376 (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
16377 (if (eq what 'calendar)
16378 (let ((cal-date (org-get-date-from-calendar)))
16379 (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
16380 (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
16381 (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
16382 (setcar time0 (or (car time0) 0))
16383 (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
16384 (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
16385 (setq time (apply 'encode-time time0))))
16386 ;; Insert the new time-stamp, and ensure point stays in the same
16387 ;; category as before (i.e. not after the last position in that
16389 (let ((pos (point)))
16390 ;; Stay before inserted string. `save-excursion' is of no use.
16391 (setq org-last-changed-timestamp
16392 (org-insert-time-stamp time with-hm inactive nil nil extra))
16395 (looking-at org-ts-regexp3)
16397 ;; `day' category ends before `hour' if any, or at
16398 ;; the end of the day name.
16399 ((eq origin-cat 'day)
16400 (min (or (match-beginning 7) (1- (match-end 5))) origin))
16401 ((eq origin-cat 'hour) (min (match-end 7) origin))
16402 ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
16403 ((integerp origin-cat) (min (1- (match-end 0)) origin))
16404 ;; `year' and `month' have both fixed size: point
16405 ;; couldn't have moved into another part.
16407 ;; Update clock if on a CLOCK line.
16408 (org-clock-update-time-maybe)
16409 ;; Maybe adjust the closest clock in `org-clock-history'
16410 (when org-clock-adjust-closest
16411 (if (not (and (org-at-clock-log-p)
16412 (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
16413 org-clock-history))))))
16414 (message "No clock to adjust")
16415 (cond ((save-excursion ; fix previous clock?
16416 (re-search-backward org-ts-regexp0 nil t)
16417 (org-looking-back (concat org-clock-string " \\[")))
16418 (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
16419 ((save-excursion ; fix next clock?
16420 (re-search-backward org-ts-regexp0 nil t)
16421 (looking-at (concat org-ts-regexp0 "\\] =>")))
16422 (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
16423 (save-window-excursion
16424 ;; Find closest clock to point, adjust the previous/next one in history
16425 (let* ((p (save-excursion (org-back-to-heading t)))
16426 (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
16428 (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
16429 (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
16431 (message "No clock to adjust")
16433 (org-goto-marker-or-bmk clfixpos)
16435 (when (re-search-forward clrgx nil t)
16436 (goto-char (match-beginning 1))
16437 (let (org-clock-adjust-closest)
16438 (org-timestamp-change n org-ts-what updown))
16439 (message "Clock adjusted in %s for heading: %s"
16440 (file-name-nondirectory (buffer-file-name))
16441 (org-get-heading t t)))))))))
16442 ;; Try to recenter the calendar window, if any.
16443 (if (and org-calendar-follow-timestamp-change
16444 (get-buffer-window "*Calendar*" t)
16445 (memq org-ts-what '(day month year)))
16446 (org-recenter-calendar (time-to-days time))))))
16448 (defun org-modify-ts-extra (s pos n dm)
16449 "Change the different parts of the lead-time and repeat fields in timestamp."
16450 (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
16452 (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
16454 ((or (org-pos-in-match-range pos 2)
16455 (org-pos-in-match-range pos 3))
16456 (setq m (string-to-number (match-string 3 s))
16457 h (string-to-number (match-string 2 s)))
16458 (if (org-pos-in-match-range pos 2)
16460 (setq n (* dm (org-no-warnings (signum n))))
16461 (when (not (= 0 (setq rem (% m dm))))
16462 (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
16464 (if (< m 0) (setq m (+ m 60) h (1- h)))
16465 (if (> m 59) (setq m (- m 60) h (1+ h)))
16466 (setq h (min 24 (max 0 h)))
16467 (setq ng 1 new (format "-%02d:%02d" h m)))
16468 ((org-pos-in-match-range pos 6)
16469 (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
16470 ((org-pos-in-match-range pos 5)
16471 (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
16473 ((org-pos-in-match-range pos 9)
16474 (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
16475 ((org-pos-in-match-range pos 8)
16476 (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
16480 (substring s 0 (match-beginning ng))
16482 (substring s (match-end ng))))))
16485 (defun org-recenter-calendar (date)
16486 "If the calendar is visible, recenter it to DATE."
16487 (let ((cwin (get-buffer-window "*Calendar*" t)))
16489 (let ((calendar-move-hook nil))
16490 (with-selected-window cwin
16491 (calendar-goto-date (if (listp date) date
16492 (calendar-gregorian-from-absolute date))))))))
16494 (defun org-goto-calendar (&optional arg)
16495 "Go to the Emacs calendar at the current date.
16496 If there is a time stamp in the current line, go to that date.
16497 A prefix ARG can be used to force the current date."
16499 (let ((tsr org-ts-regexp) diff
16500 (calendar-move-hook nil)
16501 (calendar-view-holidays-initially-flag nil)
16502 (calendar-view-diary-initially-flag nil))
16503 (if (or (org-at-timestamp-p)
16505 (beginning-of-line 1)
16506 (looking-at (concat ".*" tsr))))
16507 (let ((d1 (time-to-days (current-time)))
16509 (org-time-string-to-time (match-string 1)))))
16510 (setq diff (- d2 d1))))
16512 (calendar-goto-today)
16513 (if (and diff (not arg)) (calendar-forward-day diff))))
16515 (defun org-get-date-from-calendar ()
16516 "Return a list (month day year) of date at point in calendar."
16517 (with-current-buffer "*Calendar*"
16519 (calendar-cursor-to-date))))
16521 (defun org-date-from-calendar ()
16522 "Insert time stamp corresponding to cursor date in *Calendar* buffer.
16523 If there is already a time stamp at the cursor position, update it."
16525 (if (org-at-timestamp-p t)
16526 (org-timestamp-change 0 'calendar)
16527 (let ((cal-date (org-get-date-from-calendar)))
16528 (org-insert-time-stamp
16529 (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
16531 (defun org-minutes-to-hh:mm-string (m)
16532 "Compute H:MM from a number of minutes."
16533 (let ((h (/ m 60)))
16534 (setq m (- m (* 60 h)))
16535 (format org-time-clocksum-format h m)))
16537 (defun org-hh:mm-string-to-minutes (s)
16538 "Convert a string H:MM to a number of minutes.
16539 If the string is just a number, interpret it as minutes.
16540 In fact, the first hh:mm or number in the string will be taken,
16541 there can be extra stuff in the string.
16542 If no number is found, the return value is 0."
16545 ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
16546 (+ (* (string-to-number (match-string 1 s)) 60)
16547 (string-to-number (match-string 2 s))))
16548 ((string-match "\\([0-9]+\\)" s)
16549 (string-to-number (match-string 1 s)))
16552 (defcustom org-effort-durations
16555 ("w" . ,(* 60 8 5))
16556 ("m" . ,(* 60 8 5 4))
16557 ("y" . ,(* 60 8 5 40)))
16558 "Conversion factor to minutes for an effort modifier.
16560 Each entry has the form (MODIFIER . MINUTES).
16562 In an effort string, a number followed by MODIFIER is multiplied
16563 by the specified number of MINUTES to obtain an effort in
16566 For example, if the value of this variable is ((\"hours\" . 60)), then an
16567 effort string \"2hours\" is equivalent to 120 minutes."
16570 :type '(alist :key-type (string :tag "Modifier")
16571 :value-type (number :tag "Minutes")))
16573 (defun org-duration-string-to-minutes (s &optional output-to-string)
16574 "Convert a duration string S to minutes.
16576 A bare number is interpreted as minutes, modifiers can be set by
16577 customizing `org-effort-durations' (which see).
16579 Entries containing a colon are interpreted as H:MM by
16580 `org-hh:mm-string-to-minutes'."
16582 (re (concat "\\([0-9.]+\\) *\\("
16583 (regexp-opt (mapcar 'car org-effort-durations))
16585 (while (string-match re s)
16586 (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
16587 (string-to-number (match-string 1 s))))
16588 (setq s (replace-match "" nil t s)))
16589 (setq result (floor result))
16590 (incf result (org-hh:mm-string-to-minutes s))
16591 (if output-to-string (number-to-string result) result)))
16595 (defun org-save-all-org-buffers ()
16596 "Save all Org-mode buffers without user confirmation."
16598 (message "Saving all Org-mode buffers...")
16599 (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
16600 (when (featurep 'org-id) (org-id-locations-save))
16601 (message "Saving all Org-mode buffers... done"))
16603 (defun org-revert-all-org-buffers ()
16604 "Revert all Org-mode buffers.
16605 Prompt for confirmation when there are unsaved changes.
16606 Be sure you know what you are doing before letting this function
16607 overwrite your changes.
16609 This function is useful in a setup where one tracks org files
16610 with a version control system, to revert on one machine after pulling
16611 changes from another. I believe the procedure must be like this:
16613 1. M-x org-save-all-org-buffers
16614 2. Pull changes from the other machine, resolve conflicts
16615 3. M-x org-revert-all-org-buffers"
16617 (unless (yes-or-no-p "Revert all Org buffers from their files? ")
16620 (save-window-excursion
16623 (when (and (with-current-buffer b (derived-mode-p 'org-mode))
16624 (with-current-buffer b buffer-file-name))
16625 (org-pop-to-buffer-same-window b)
16626 (revert-buffer t 'no-confirm)))
16628 (when (and (featurep 'org-id) org-id-track-globally)
16629 (org-id-locations-load)))))
16634 (defun org-switchb (&optional arg)
16635 "Switch between Org buffers.
16636 With one prefix argument, restrict available buffers to files.
16637 With two prefix arguments, restrict available buffers to agenda files.
16639 Defaults to `iswitchb' for buffer name completion.
16640 Set `org-completion-use-ido' to make it use ido instead."
16642 (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
16643 ((equal arg '(16)) (org-buffer-list 'agenda))
16644 (t (org-buffer-list))))
16645 (org-completion-use-iswitchb org-completion-use-iswitchb)
16646 (org-completion-use-ido org-completion-use-ido))
16647 (unless (or org-completion-use-ido org-completion-use-iswitchb)
16648 (setq org-completion-use-iswitchb t))
16649 (org-pop-to-buffer-same-window
16650 (org-icompleting-read "Org buffer: "
16651 (mapcar 'list (mapcar 'buffer-name blist))
16654 ;;; Define some older names previously used for this functionality
16656 (defalias 'org-ido-switchb 'org-switchb)
16658 (defalias 'org-iswitchb 'org-switchb)
16660 (defun org-buffer-list (&optional predicate exclude-tmp)
16661 "Return a list of Org buffers.
16662 PREDICATE can be `export', `files' or `agenda'.
16664 export restrict the list to Export buffers.
16665 files restrict the list to buffers visiting Org files.
16666 agenda restrict the list to buffers visiting agenda files.
16668 If EXCLUDE-TMP is non-nil, ignore temporary buffers."
16670 (agenda-files (and (eq predicate 'agenda)
16671 (mapcar 'file-truename (org-agenda-files t))))
16674 ((eq predicate 'files)
16675 (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
16676 ((eq predicate 'export)
16677 (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
16678 ((eq predicate 'agenda)
16680 (with-current-buffer b
16681 (and (derived-mode-p 'org-mode)
16682 (setq bfn (buffer-file-name b))
16683 (member (file-truename bfn) agenda-files)))))
16684 (t (lambda (b) (with-current-buffer b
16685 (or (derived-mode-p 'org-mode)
16686 (string-match "\*Org .*Export"
16687 (buffer-name b)))))))))
16691 (if (and (funcall filter b)
16692 (or (not exclude-tmp)
16693 (not (string-match "tmp" (buffer-name b)))))
16698 (defun org-agenda-files (&optional unrestricted archives)
16699 "Get the list of agenda files.
16700 Optional UNRESTRICTED means return the full list even if a restriction
16701 is currently in place.
16702 When ARCHIVES is t, include all archive files that are really being
16703 used by the agenda files. If ARCHIVE is `ifmode', do this only if
16704 `org-agenda-archives-mode' is t."
16707 ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
16708 ((stringp org-agenda-files) (org-read-agenda-file-list))
16709 ((listp org-agenda-files) org-agenda-files)
16710 (t (error "Invalid value of `org-agenda-files'")))))
16711 (setq files (apply 'append
16712 (mapcar (lambda (f)
16713 (if (file-directory-p f)
16715 f t org-agenda-file-regexp)
16718 (when org-agenda-skip-unavailable-files
16719 (setq files (delq nil
16722 (and (file-readable-p file) file)))
16724 (when (or (eq archives t)
16725 (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
16726 (setq files (org-add-archive-files files)))
16729 (defun org-agenda-file-p (&optional file)
16730 "Return non-nil, if FILE is an agenda file.
16731 If FILE is omitted, use the file associated with the current
16733 (member (or file (buffer-file-name))
16734 (org-agenda-files t)))
16736 (defun org-edit-agenda-file-list ()
16737 "Edit the list of agenda files.
16738 Depending on setup, this either uses customize to edit the variable
16739 `org-agenda-files', or it visits the file that is holding the list. In the
16740 latter case, the buffer is set up in a way that saving it automatically kills
16741 the buffer and restores the previous window configuration."
16743 (if (stringp org-agenda-files)
16744 (let ((cw (current-window-configuration)))
16745 (find-file org-agenda-files)
16746 (org-set-local 'org-window-configuration cw)
16747 (org-add-hook 'after-save-hook
16749 (set-window-configuration
16750 (prog1 org-window-configuration
16751 (kill-buffer (current-buffer))))
16752 (org-install-agenda-files-menu)
16753 (message "New agenda file list installed"))
16755 (message "%s" (substitute-command-keys
16756 "Edit list and finish with \\[save-buffer]")))
16757 (customize-variable 'org-agenda-files)))
16759 (defun org-store-new-agenda-file-list (list)
16760 "Set new value for the agenda file list and save it correctly."
16761 (if (stringp org-agenda-files)
16762 (let ((fe (org-read-agenda-file-list t)) b u)
16763 (while (setq b (find-buffer-visiting org-agenda-files))
16765 (with-temp-file org-agenda-files
16768 (lambda (f) ;; Keep un-expanded entries.
16769 (if (setq u (assoc f fe))
16774 (let ((org-mode-hook nil) (org-inhibit-startup t)
16775 (org-insert-mode-line-in-empty-file nil))
16776 (setq org-agenda-files list)
16777 (customize-save-variable 'org-agenda-files org-agenda-files))))
16779 (defun org-read-agenda-file-list (&optional pair-with-expansion)
16780 "Read the list of agenda files from a file.
16781 If PAIR-WITH-EXPANSION is t return pairs with un-expanded
16782 filenames, used by `org-store-new-agenda-file-list' to write back
16783 un-expanded file names."
16784 (when (file-directory-p org-agenda-files)
16785 (error "`org-agenda-files' cannot be a single directory"))
16786 (when (stringp org-agenda-files)
16788 (insert-file-contents org-agenda-files)
16791 (let ((e (expand-file-name (substitute-in-file-name f)
16793 (if pair-with-expansion
16796 (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
16799 (defun org-cycle-agenda-files ()
16800 "Cycle through the files in `org-agenda-files'.
16801 If the current buffer visits an agenda file, find the next one in the list.
16802 If the current buffer does not, find the first agenda file."
16804 (let* ((fs (org-agenda-files t))
16805 (files (append fs (list (car fs))))
16806 (tcf (if buffer-file-name (file-truename buffer-file-name)))
16808 (unless files (error "No agenda files"))
16810 (while (setq file (pop files))
16811 (if (equal (file-truename file) tcf)
16813 (find-file (car files))
16815 (find-file (car fs)))
16816 (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
16818 (defun org-agenda-file-to-front (&optional to-end)
16819 "Move/add the current file to the top of the agenda file list.
16820 If the file is not present in the list, it is added to the front. If it is
16821 present, it is moved there. With optional argument TO-END, add/move to the
16824 (let ((org-agenda-skip-unavailable-files nil)
16825 (file-alist (mapcar (lambda (x)
16826 (cons (file-truename x) x))
16827 (org-agenda-files t)))
16828 (ctf (file-truename buffer-file-name))
16830 (setq x (assoc ctf file-alist) had x)
16832 (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
16834 (setq file-alist (append (delq x file-alist) (list x)))
16835 (setq file-alist (cons x (delq x file-alist))))
16836 (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
16837 (org-install-agenda-files-menu)
16838 (message "File %s to %s of agenda file list"
16839 (if had "moved" "added") (if to-end "end" "front"))))
16841 (defun org-remove-file (&optional file)
16842 "Remove current file from the list of files in variable `org-agenda-files'.
16843 These are the files which are being checked for agenda entries.
16844 Optional argument FILE means use this file instead of the current."
16846 (let* ((org-agenda-skip-unavailable-files nil)
16847 (file (or file buffer-file-name))
16848 (true-file (file-truename file))
16849 (afile (abbreviate-file-name file))
16850 (files (delq nil (mapcar
16852 (if (equal true-file
16855 (org-agenda-files t)))))
16856 (if (not (= (length files) (length (org-agenda-files t))))
16858 (org-store-new-agenda-file-list files)
16859 (org-install-agenda-files-menu)
16860 (message "Removed file: %s" afile))
16861 (message "File was not in list: %s (not removed)" afile))))
16863 (defun org-file-menu-entry (file)
16864 (vector file (list 'find-file file) t))
16866 (defun org-check-agenda-file (file)
16867 "Make sure FILE exists. If not, ask user what to do."
16868 (when (not (file-exists-p file))
16869 (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
16870 (abbreviate-file-name file))
16871 (let ((r (downcase (read-char-exclusive))))
16874 (org-remove-file file)
16875 (throw 'nextfile t))
16876 (t (error "Abort"))))))
16878 (defun org-get-agenda-file-buffer (file)
16879 "Get a buffer visiting FILE. If the buffer needs to be created, add
16880 it to the list of buffers which might be released later."
16881 (let ((buf (org-find-base-buffer-visiting file)))
16883 buf ; just return it
16884 ;; Make a new buffer and remember it
16885 (setq buf (find-file-noselect file))
16886 (if buf (push buf org-agenda-new-buffers))
16889 (defun org-release-buffers (blist)
16890 "Release all buffers in list, asking the user for confirmation when needed.
16891 When a buffer is unmodified, it is just killed. When modified, it is saved
16892 \(if the user agrees) and then killed."
16894 (while (setq buf (pop blist))
16895 (setq file (buffer-file-name buf))
16896 (when (and (buffer-modified-p buf)
16898 (y-or-n-p (format "Save file %s? " file)))
16899 (with-current-buffer buf (save-buffer)))
16900 (kill-buffer buf))))
16902 (defun org-agenda-prepare-buffers (files)
16903 "Create buffers for all agenda files, protect archived trees and comments."
16905 (let ((pa '(:org-archived t))
16906 (pc '(:org-comment t))
16907 (pall '(:org-archived t :org-comment t))
16908 (inhibit-read-only t)
16909 (rea (concat ":" org-archive-tag ":"))
16913 (while (setq file (pop files))
16917 (org-check-agenda-file file)
16918 (set-buffer (org-get-agenda-file-buffer file)))
16920 (setq bmp (buffer-modified-p))
16921 (org-refresh-category-properties)
16922 (setq org-todo-keywords-for-agenda
16923 (append org-todo-keywords-for-agenda org-todo-keywords-1))
16924 (setq org-done-keywords-for-agenda
16925 (append org-done-keywords-for-agenda org-done-keywords))
16926 (setq org-todo-keyword-alist-for-agenda
16927 (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
16928 (setq org-drawers-for-agenda
16929 (append org-drawers-for-agenda org-drawers))
16930 (setq org-tag-alist-for-agenda
16931 (append org-tag-alist-for-agenda org-tag-alist))
16934 (remove-text-properties (point-min) (point-max) pall)
16935 (when org-agenda-skip-archived-trees
16936 (goto-char (point-min))
16937 (while (re-search-forward rea nil t)
16938 (if (org-at-heading-p t)
16939 (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
16940 (goto-char (point-min))
16941 (setq re (format org-heading-keyword-regexp-format
16942 org-comment-string))
16943 (while (re-search-forward re nil t)
16944 (add-text-properties
16945 (match-beginning 0) (org-end-of-subtree t) pc)))
16946 (set-buffer-modified-p bmp)))))
16947 (setq org-todo-keywords-for-agenda
16948 (org-uniquify org-todo-keywords-for-agenda))
16949 (setq org-todo-keyword-alist-for-agenda
16950 (org-uniquify org-todo-keyword-alist-for-agenda)
16951 org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
16953 ;;;; Embedded LaTeX
16955 (defvar org-cdlatex-mode-map (make-sparse-keymap)
16956 "Keymap for the minor `org-cdlatex-mode'.")
16958 (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
16959 (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
16960 (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
16961 (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
16962 (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
16964 (defvar org-cdlatex-texmathp-advice-is-done nil
16965 "Flag remembering if we have applied the advice to texmathp already.")
16967 (define-minor-mode org-cdlatex-mode
16968 "Toggle the minor `org-cdlatex-mode'.
16969 This mode supports entering LaTeX environment and math in LaTeX fragments
16971 \\{org-cdlatex-mode-map}"
16973 (when org-cdlatex-mode
16975 (run-hooks 'cdlatex-mode-hook)
16976 (cdlatex-compute-tables))
16977 (unless org-cdlatex-texmathp-advice-is-done
16978 (setq org-cdlatex-texmathp-advice-is-done t)
16979 (defadvice texmathp (around org-math-always-on activate)
16980 "Always return t in org-mode buffers.
16981 This is because we want to insert math symbols without dollars even outside
16982 the LaTeX math segments. If Orgmode thinks that point is actually inside
16983 an embedded LaTeX fragment, let texmathp do its job.
16984 \\[org-cdlatex-mode-map]"
16988 ((not (derived-mode-p 'org-mode)) ad-do-it)
16989 ((eq this-command 'cdlatex-math-symbol)
16990 (setq ad-return-value t
16991 texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
16993 (let ((p (org-inside-LaTeX-fragment-p)))
16994 (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
16995 (setq ad-return-value t
16996 texmathp-why '("Org-mode embedded math" . 0))
16997 (if p ad-do-it)))))))))
16999 (defun turn-on-org-cdlatex ()
17000 "Unconditionally turn on `org-cdlatex-mode'."
17001 (org-cdlatex-mode 1))
17003 (defun org-inside-LaTeX-fragment-p ()
17004 "Test if point is inside a LaTeX fragment.
17005 I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
17006 sequence appearing also before point.
17007 Even though the matchers for math are configurable, this function assumes
17008 that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
17009 delimiters are skipped when they have been removed by customization.
17010 The return value is nil, or a cons cell with the delimiter and the
17011 position of this delimiter.
17013 This function does a reasonably good job, but can locally be fooled by
17014 for example currency specifications. For example it will assume being in
17015 inline math after \"$22.34\". The LaTeX fragment formatter will only format
17016 fragments that are properly closed, but during editing, we have to live
17017 with the uncertainty caused by missing closing delimiters. This function
17018 looks only before point, not after."
17020 (let ((pos (point))
17021 (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
17023 (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
17025 dd-on str (start 0) m re)
17028 (setq str (concat (buffer-substring lim (point)) "\000 X$.")
17029 re (nth 1 (assoc "$" org-latex-regexps)))
17030 (while (string-match re str start)
17032 ((= (match-end 0) (length str))
17033 (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
17034 ((= (match-end 0) (- (length str) 5))
17036 (t (setq start (match-end 0))))))
17037 (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
17039 (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
17040 (and (match-beginning 2) (throw 'exit nil))
17042 (while (re-search-backward "\\$\\$" lim t)
17043 (setq dd-on (not dd-on)))
17045 (if dd-on (cons "$$" m))))))
17047 (defun org-inside-latex-macro-p ()
17048 "Is point inside a LaTeX macro or its arguments?"
17051 "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
17053 (defun org-try-cdlatex-tab ()
17054 "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
17055 It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
17056 - inside a LaTeX fragment, or
17057 - after the first word in a line, where an abbreviation expansion could
17058 insert a LaTeX environment."
17059 (when org-cdlatex-mode
17061 ;; Before any word on the line: No expansion possible.
17062 ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
17063 ;; Just after first word on the line: Expand it. Make sure it
17064 ;; cannot happen on headlines, though.
17066 (skip-chars-backward "a-zA-Z0-9*")
17067 (skip-chars-backward " \t")
17068 (and (bolp) (not (org-at-heading-p))))
17070 ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
17072 (defun org-cdlatex-underscore-caret (&optional arg)
17073 "Execute `cdlatex-sub-superscript' in LaTeX fragments.
17074 Revert to the normal definition outside of these fragments."
17076 (if (org-inside-LaTeX-fragment-p)
17077 (call-interactively 'cdlatex-sub-superscript)
17078 (let (org-cdlatex-mode)
17079 (call-interactively (key-binding (vector last-input-event))))))
17081 (defun org-cdlatex-math-modify (&optional arg)
17082 "Execute `cdlatex-math-modify' in LaTeX fragments.
17083 Revert to the normal definition outside of these fragments."
17085 (if (org-inside-LaTeX-fragment-p)
17086 (call-interactively 'cdlatex-math-modify)
17087 (let (org-cdlatex-mode)
17088 (call-interactively (key-binding (vector last-input-event))))))
17090 (defvar org-latex-fragment-image-overlays nil
17091 "List of overlays carrying the images of latex fragments.")
17092 (make-variable-buffer-local 'org-latex-fragment-image-overlays)
17094 (defun org-remove-latex-fragment-image-overlays ()
17095 "Remove all overlays with LaTeX fragment images in current buffer."
17096 (mapc 'delete-overlay org-latex-fragment-image-overlays)
17097 (setq org-latex-fragment-image-overlays nil))
17099 (defun org-preview-latex-fragment (&optional subtree)
17100 "Preview the LaTeX fragment at point, or all locally or globally.
17101 If the cursor is in a LaTeX fragment, create the image and overlay
17102 it over the source code. If there is no fragment at point, display
17103 all fragments in the current text, from one headline to the next. With
17104 prefix SUBTREE, display all fragments in the current subtree. With a
17105 double prefix arg \\[universal-argument] \\[universal-argument], or when \
17106 the cursor is before the first headline,
17107 display all fragments in the buffer.
17108 The images can be removed again with \\[org-ctrl-c-ctrl-c]."
17110 (unless buffer-file-name
17111 (error "Can't preview LaTeX fragment in a non-file buffer"))
17112 (org-remove-latex-fragment-image-overlays)
17115 (let (beg end at msg)
17117 ((or (equal subtree '(16))
17118 (not (save-excursion
17119 (re-search-backward org-outline-regexp-bol nil t))))
17120 (setq beg (point-min) end (point-max)
17121 msg "Creating images for buffer...%s"))
17122 ((equal subtree '(4))
17123 (org-back-to-heading)
17124 (setq beg (point) end (org-end-of-subtree t)
17125 msg "Creating images for subtree...%s"))
17127 (if (setq at (org-inside-LaTeX-fragment-p))
17128 (goto-char (max (point-min) (- (cdr at) 2)))
17129 (org-back-to-heading))
17130 (setq beg (point) end (progn (outline-next-heading) (point))
17131 msg (if at "Creating image...%s"
17132 "Creating images for entry...%s"))))
17134 (narrow-to-region beg end)
17137 (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
17138 (file-name-nondirectory
17139 buffer-file-name)))
17140 default-directory 'overlays msg at 'forbuffer
17141 org-latex-create-formula-image-program)
17142 (message msg "done. Use `C-c C-c' to remove images.")))))
17144 (defvar org-latex-regexps
17145 '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
17146 ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
17147 ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
17148 ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17149 ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
17150 ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
17151 ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
17152 ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
17153 "Regular expressions for matching embedded LaTeX.")
17155 (defvar org-export-have-math nil) ;; dynamic scoping
17156 (defun org-format-latex (prefix &optional dir overlays msg at
17157 forbuffer processing-type)
17158 "Replace LaTeX fragments with links to an image, and produce images.
17159 Some of the options can be changed using the variable
17160 `org-format-latex-options'."
17161 (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
17162 (let* ((prefixnodir (file-name-nondirectory prefix))
17163 (absprefix (expand-file-name prefix dir))
17164 (todir (file-name-directory absprefix))
17165 (opt org-format-latex-options)
17166 (matchers (plist-get opt :matchers))
17167 (re-list org-latex-regexps)
17168 (org-format-latex-header-extra
17169 (plist-get (org-infile-export-plist) :latex-header-extra))
17170 (cnt 0) txt hash link beg end re e checkdir
17171 executables-checked string
17172 m n block-type block linkfile movefile ov)
17173 ;; Check the different regular expressions
17174 (while (setq e (pop re-list))
17175 (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
17176 block (if block-type "\n\n" ""))
17177 (when (member m matchers)
17178 (goto-char (point-min))
17179 (while (re-search-forward re nil t)
17180 (when (and (or (not at) (equal (cdr at) (match-beginning n)))
17181 (not (get-text-property (match-beginning n)
17184 (not (eq (get-char-property (match-beginning n)
17186 'org-latex-overlay))))
17187 (setq org-export-have-math t)
17189 ((eq processing-type 'verbatim)
17190 ;; Leave the text verbatim, just protect it
17191 (add-text-properties (match-beginning n) (match-end n)
17192 '(org-protected t)))
17193 ((eq processing-type 'mathjax)
17194 ;; Prepare for MathJax processing
17195 (setq string (match-string n))
17196 (if (member m '("$" "$1"))
17198 (delete-region (match-beginning n) (match-end n))
17199 (goto-char (match-beginning n))
17200 (insert (org-add-props (concat "\\(" (substring string 1 -1)
17202 '(org-protected t))))
17203 (add-text-properties (match-beginning n) (match-end n)
17204 '(org-protected t))))
17205 ((or (eq processing-type 'dvipng)
17206 (eq processing-type 'imagemagick))
17207 ;; Process to an image
17208 (setq txt (match-string n)
17209 beg (match-beginning n) end (match-end n)
17211 (let (print-length print-level) ; make sure full list is printed
17212 (setq hash (sha1 (prin1-to-string
17213 (list org-format-latex-header
17214 org-format-latex-header-extra
17215 org-export-latex-default-packages-alist
17216 org-export-latex-packages-alist
17217 org-format-latex-options
17219 linkfile (format "%s_%s.png" prefix hash)
17220 movefile (format "%s_%s.png" absprefix hash)))
17221 (setq link (concat block "[[file:" linkfile "]]" block))
17222 (if msg (message msg cnt))
17224 (unless checkdir ; make sure the directory exists
17226 (or (file-directory-p todir) (make-directory todir t)))
17228 ((eq processing-type 'dvipng)
17229 (unless executables-checked
17230 (org-check-external-command
17231 "latex" "needed to convert LaTeX fragments to images")
17232 (org-check-external-command
17233 "dvipng" "needed to convert LaTeX fragments to images")
17234 (setq executables-checked t))
17235 (unless (file-exists-p movefile)
17236 (org-create-formula-image-with-dvipng
17237 txt movefile opt forbuffer)))
17238 ((eq processing-type 'imagemagick)
17239 (unless executables-checked
17240 (org-check-external-command
17241 "convert" "you need to install imagemagick")
17242 (setq executables-checked t))
17243 (unless (file-exists-p movefile)
17244 (org-create-formula-image-with-imagemagick
17245 txt movefile opt forbuffer))))
17249 (if (eq (overlay-get o 'org-overlay-type)
17250 'org-latex-overlay)
17251 (delete-overlay o)))
17252 (overlays-in beg end))
17253 (setq ov (make-overlay beg end))
17254 (overlay-put ov 'org-overlay-type 'org-latex-overlay)
17255 (if (featurep 'xemacs)
17257 (overlay-put ov 'invisible t)
17260 (make-glyph (vector 'png :file movefile))))
17263 (list 'image :type 'png :file movefile :ascent 'center)))
17264 (push ov org-latex-fragment-image-overlays)
17266 (delete-region beg end)
17267 (insert (org-add-props link
17268 (list 'org-latex-src
17269 (replace-regexp-in-string
17271 'org-latex-src-embed-type
17272 (if block-type 'paragraph 'character))))))
17273 ((eq processing-type 'mathml)
17274 ;; Process to MathML
17275 (unless executables-checked
17276 (unless (save-match-data (org-format-latex-mathml-available-p))
17277 (error "LaTeX to MathML converter not configured"))
17278 (setq executables-checked t))
17279 (setq txt (match-string n)
17280 beg (match-beginning n) end (match-end n)
17282 (if msg (message msg cnt))
17284 (delete-region beg end)
17285 (insert (org-format-latex-as-mathml
17286 txt block-type prefix dir)))
17288 (error "Unknown conversion type %s for latex fragments"
17289 processing-type)))))))))
17291 (defun org-create-math-formula (latex-frag &optional mathml-file)
17292 "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
17293 Use `org-latex-to-mathml-convert-command'. If the conversion is
17294 sucessful, return the portion between \"<math...> </math>\"
17295 elements otherwise return nil. When MATHML-FILE is specified,
17296 write the results in to that file. When invoked as an
17297 interactive command, prompt for LATEX-FRAG, with initial value
17298 set to the current active region and echo the results for user
17300 (interactive (list (let ((frag (when (org-region-active-p)
17301 (buffer-substring-no-properties
17302 (region-beginning) (region-end)))))
17303 (read-string "LaTeX Fragment: " frag nil frag))))
17304 (unless latex-frag (error "Invalid latex-frag"))
17305 (let* ((tmp-in-file (file-relative-name
17306 (make-temp-name (expand-file-name "ltxmathml-in"))))
17307 (ignore (write-region latex-frag nil tmp-in-file))
17308 (tmp-out-file (file-relative-name
17309 (make-temp-name (expand-file-name "ltxmathml-out"))))
17311 org-latex-to-mathml-convert-command
17312 `((?j . ,(shell-quote-argument
17313 (expand-file-name org-latex-to-mathml-jar-file)))
17314 (?I . ,(shell-quote-argument tmp-in-file))
17315 (?o . ,(shell-quote-argument tmp-out-file)))))
17316 mathml shell-command-output)
17317 (when (org-called-interactively-p 'any)
17318 (unless (org-format-latex-mathml-available-p)
17319 (error "LaTeX to MathML converter not configured")))
17320 (message "Running %s" cmd)
17321 (setq shell-command-output (shell-command-to-string cmd))
17323 (when (file-readable-p tmp-out-file)
17324 (with-current-buffer (find-file-noselect tmp-out-file t)
17325 (goto-char (point-min))
17326 (when (re-search-forward
17329 "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
17331 (regexp-quote "</math>")) nil t)
17332 (prog1 (match-string 0) (kill-buffer))))))
17336 (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
17338 (write-region mathml nil mathml-file))
17339 (when (org-called-interactively-p 'any)
17341 ((message "LaTeX to MathML conversion failed")
17342 (message shell-command-output)))
17343 (delete-file tmp-in-file)
17344 (when (file-exists-p tmp-out-file)
17345 (delete-file tmp-out-file))
17348 (defun org-format-latex-as-mathml (latex-frag latex-frag-type
17349 prefix &optional dir)
17350 "Use `org-create-math-formula' but check local cache first."
17351 (let* ((absprefix (expand-file-name prefix dir))
17352 (print-length nil) (print-level nil)
17353 (formula-id (concat
17358 org-latex-to-mathml-convert-command)))))
17359 (formula-cache (format "%s-%s.mathml" absprefix formula-id))
17360 (formula-cache-dir (file-name-directory formula-cache)))
17362 (unless (file-directory-p formula-cache-dir)
17363 (make-directory formula-cache-dir t))
17365 (unless (file-exists-p formula-cache)
17366 (org-create-math-formula latex-frag formula-cache))
17368 (if (file-exists-p formula-cache)
17369 ;; Successful conversion. Return the link to MathML file.
17371 (format "[[file:%s]]" (file-relative-name formula-cache dir))
17372 (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
17373 'org-latex-src-embed-type (if latex-frag-type
17374 'paragraph 'character)))
17375 ;; Failed conversion. Return the LaTeX fragment verbatim
17376 (add-text-properties
17377 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
17380 ;; This function borrows from Ganesh Swami's latex2png.el
17381 (defun org-create-formula-image-with-dvipng (string tofile options buffer)
17382 "This calls dvipng."
17383 (require 'org-latex)
17384 (let* ((tmpdir (if (featurep 'xemacs)
17386 temporary-file-directory))
17387 (texfilebase (make-temp-name
17388 (expand-file-name "orgtex" tmpdir)))
17389 (texfile (concat texfilebase ".tex"))
17390 (dvifile (concat texfilebase ".dvi"))
17391 (pngfile (concat texfilebase ".png"))
17392 (fnh (if (featurep 'xemacs)
17393 (font-height (face-font 'default))
17394 (face-attribute 'default :height nil)))
17395 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17396 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17397 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17399 (bg (or (plist-get options (if buffer :background :html-background))
17401 (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
17402 (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
17403 (with-temp-file texfile
17404 (insert (org-splice-latex-header
17405 org-format-latex-header
17406 org-export-latex-default-packages-alist
17407 org-export-latex-packages-alist t
17408 org-format-latex-header-extra))
17409 (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
17410 (require 'org-latex)
17411 (org-export-latex-fix-inputenc))
17412 (let ((dir default-directory))
17413 (condition-case nil
17416 (call-process "latex" nil nil nil texfile))
17419 (if (not (file-exists-p dvifile))
17420 (progn (message "Failed to create dvi file from %s" texfile) nil)
17421 (condition-case nil
17422 (if (featurep 'xemacs)
17423 (call-process "dvipng" nil nil nil
17428 (call-process "dvipng" nil nil nil
17431 ;;"-x" scale "-y" scale
17436 (if (not (file-exists-p pngfile))
17437 (if org-format-latex-signal-error
17438 (error "Failed to create png file from %s" texfile)
17439 (message "Failed to create png file from %s" texfile)
17441 ;; Use the requested file name and clean up
17442 (copy-file pngfile tofile 'replace)
17443 (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
17444 (if (file-exists-p (concat texfilebase e))
17445 (delete-file (concat texfilebase e))))
17448 (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
17449 (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
17450 "This calls convert, which is included into imagemagick."
17451 (require 'org-latex)
17452 (let* ((tmpdir (if (featurep 'xemacs)
17454 temporary-file-directory))
17455 (texfilebase (make-temp-name
17456 (expand-file-name "orgtex" tmpdir)))
17457 (texfile (concat texfilebase ".tex"))
17458 (pdffile (concat texfilebase ".pdf"))
17459 (pngfile (concat texfilebase ".png"))
17460 (fnh (if (featurep 'xemacs)
17461 (font-height (face-font 'default))
17462 (face-attribute 'default :height nil)))
17463 (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
17464 (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
17465 (fg (or (plist-get options (if buffer :foreground :html-foreground))
17467 (bg (or (plist-get options (if buffer :background :html-background))
17469 (if (eq fg 'default) (setq fg (org-latex-color :foreground))
17470 (setq fg (org-latex-color-format fg)))
17471 (if (eq bg 'default) (setq bg (org-latex-color :background))
17472 (setq bg (org-latex-color-format
17473 (if (string= bg "Transparent")(setq bg "white")))))
17474 (with-temp-file texfile
17475 (insert (org-splice-latex-header
17476 org-format-latex-header
17477 org-export-latex-default-packages-alist
17478 org-export-latex-packages-alist t
17479 org-format-latex-header-extra))
17480 (insert "\n\\begin{document}\n"
17481 "\\definecolor{fg}{rgb}{" fg "}\n"
17482 "\\definecolor{bg}{rgb}{" bg "}\n"
17483 "\n\\pagecolor{bg}\n"
17487 "\n\\end{document}\n" )
17488 (require 'org-latex)
17489 (org-export-latex-fix-inputenc))
17490 (let ((dir default-directory) cmd cmds latex-frags-cmds)
17491 (condition-case nil
17494 (setq cmds org-latex-to-pdf-process)
17496 (setq latex-frags-cmds (pop cmds))
17497 (if (listp latex-frags-cmds)
17499 (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
17500 (while latex-frags-cmds
17501 (setq cmd (pop latex-frags-cmds))
17502 (while (string-match "%b" cmd)
17503 (setq cmd (replace-match
17505 (shell-quote-argument texfile))
17507 (while (string-match "%f" cmd)
17508 (setq cmd (replace-match
17510 (shell-quote-argument (file-name-nondirectory texfile)))
17512 (while (string-match "%o" cmd)
17513 (setq cmd (replace-match
17515 (shell-quote-argument (file-name-directory texfile)))
17517 (setq cmd (split-string cmd))
17518 (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
17521 (if (not (file-exists-p pdffile))
17522 (progn (message "Failed to create pdf file from %s" texfile) nil)
17523 (condition-case nil
17524 (if (featurep 'xemacs)
17525 (call-process "convert" nil nil nil
17531 ;; "-sharpen" "0x1.0"
17533 (call-process "convert" nil nil nil
17539 ; "-sharpen" "0x1.0"
17542 (if (not (file-exists-p pngfile))
17543 (if org-format-latex-signal-error
17544 (error "Failed to create png file from %s" texfile)
17545 (message "Failed to create png file from %s" texfile)
17547 ;; Use the requested file name and clean up
17548 (copy-file pngfile tofile 'replace)
17549 (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
17550 (if (file-exists-p (concat texfilebase e))
17551 (delete-file (concat texfilebase e))))
17554 (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
17555 "Fill a LaTeX header template TPL.
17556 In the template, the following place holders will be recognized:
17558 [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
17559 [NO-DEFAULT-PACKAGES] do not include DEF-PKG
17560 [PACKAGES] \\usepackage statements for PKG
17561 [NO-PACKAGES] do not include PKG
17562 [EXTRA] the string EXTRA
17563 [NO-EXTRA] do not include EXTRA
17565 For backward compatibility, if both the positive and the negative place
17566 holder is missing, the positive one (without the \"NO-\") will be
17567 assumed to be present at the end of the template.
17568 DEF-PKG and PKG are assumed to be alists of options/packagename lists.
17570 SNIPPETS-P indicates if this is run to create snippet images for HTML."
17571 (let (rpl (end ""))
17572 (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
17573 (setq rpl (if (or (match-end 1) (not def-pkg))
17574 "" (org-latex-packages-to-string def-pkg snippets-p t))
17575 tpl (replace-match rpl t t tpl))
17576 (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
17578 (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
17579 (setq rpl (if (or (match-end 1) (not pkg))
17580 "" (org-latex-packages-to-string pkg snippets-p t))
17581 tpl (replace-match rpl t t tpl))
17584 (org-latex-packages-to-string pkg snippets-p)))))
17586 (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
17587 (setq rpl (if (or (match-end 1) (not extra))
17588 "" (concat extra "\n"))
17589 tpl (replace-match rpl t t tpl))
17590 (if (and extra (string-match "\\S-" extra))
17591 (setq end (concat end "\n" extra))))
17593 (if (string-match "\\S-" end)
17594 (concat tpl "\n" end)
17597 (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
17598 "Turn an alist of packages into a string with the \\usepackage macros."
17599 (setq pkg (mapconcat (lambda(p)
17602 ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
17603 (format "%% Package %s omitted" (cadr p)))
17604 ((equal "" (car p))
17605 (format "\\usepackage{%s}" (cadr p)))
17607 (format "\\usepackage[%s]{%s}"
17608 (car p) (cadr p)))))
17611 (if newline (concat pkg "\n") pkg))
17613 (defun org-dvipng-color (attr)
17614 "Return a RGB color specification for dvipng."
17615 (apply 'format "rgb %s %s %s"
17616 (mapcar 'org-normalize-color
17617 (if (featurep 'xemacs)
17618 (color-rgb-components
17619 (face-property 'default
17620 (cond ((eq attr :foreground) 'foreground)
17621 ((eq attr :background) 'background))))
17622 (color-values (face-attribute 'default attr nil))))))
17624 (defun org-latex-color (attr)
17625 "Return a RGB color for the LaTeX color package."
17626 (apply 'format "%s,%s,%s"
17627 (mapcar 'org-normalize-color
17628 (if (featurep 'xemacs)
17629 (color-rgb-components
17630 (face-property 'default
17631 (cond ((eq attr :foreground) 'foreground)
17632 ((eq attr :background) 'background))))
17633 (color-values (face-attribute 'default attr nil))))))
17635 (defun org-latex-color-format (color-name)
17636 "Convert COLOR-NAME to a RGB color value."
17637 (apply 'format "%s,%s,%s"
17638 (mapcar 'org-normalize-color
17639 (color-values color-name))))
17641 (defun org-normalize-color (value)
17642 "Return string to be used as color value for an RGB component."
17643 (format "%g" (/ value 65535.0)))
17648 (defvar org-inline-image-overlays nil)
17649 (make-variable-buffer-local 'org-inline-image-overlays)
17651 (defun org-toggle-inline-images (&optional include-linked)
17652 "Toggle the display of inline images.
17653 INCLUDE-LINKED is passed to `org-display-inline-images'."
17655 (if org-inline-image-overlays
17657 (org-remove-inline-images)
17658 (message "Inline image display turned off"))
17659 (org-display-inline-images include-linked)
17660 (if org-inline-image-overlays
17661 (message "%d images displayed inline"
17662 (length org-inline-image-overlays))
17663 (message "No images to display inline"))))
17665 (defun org-redisplay-inline-images ()
17666 "Refresh the display of inline images."
17668 (if (not org-inline-image-overlays)
17669 (org-toggle-inline-images)
17670 (org-toggle-inline-images)
17671 (org-toggle-inline-images)))
17673 (defun org-display-inline-images (&optional include-linked refresh beg end)
17674 "Display inline images.
17675 Normally only links without a description part are inlined, because this
17676 is how it will work for export. When INCLUDE-LINKED is set, also links
17677 with a description part will be inlined. This can be nice for a quick
17678 look at those images, but it does not reflect what exported files will look
17680 When REFRESH is set, refresh existing images between BEG and END.
17681 This will create new image displays only if necessary.
17682 BEG and END default to the buffer boundaries."
17685 (org-remove-inline-images)
17686 (if (fboundp 'clear-image-cache) (clear-image-cache)))
17690 (setq beg (or beg (point-min)) end (or end (point-max)))
17692 (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
17693 (substring (org-image-file-name-regexp) 0 -2)
17694 "\\)\\]" (if include-linked "" "\\]")))
17696 (while (re-search-forward re end t)
17697 (setq old (get-char-property-and-overlay (match-beginning 1)
17698 'org-image-overlay))
17699 (setq file (expand-file-name
17700 (concat (or (match-string 3) "") (match-string 4))))
17701 (when (file-exists-p file)
17702 (if (and (car-safe old) refresh)
17703 (image-refresh (overlay-get (cdr old) 'display))
17704 (setq img (save-match-data (create-image file)))
17706 (setq ov (make-overlay (match-beginning 0) (match-end 0)))
17707 (overlay-put ov 'display img)
17708 (overlay-put ov 'face 'default)
17709 (overlay-put ov 'org-image-overlay t)
17710 (overlay-put ov 'modification-hooks
17711 (list 'org-display-inline-remove-overlay))
17712 (push ov org-inline-image-overlays)))))))))
17714 (define-obsolete-function-alias
17715 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
17717 (defun org-display-inline-remove-overlay (ov after beg end &optional len)
17718 "Remove inline-display overlay if a corresponding region is modified."
17719 (let ((inhibit-modification-hooks t))
17720 (when (and ov after)
17721 (delete ov org-inline-image-overlays)
17722 (delete-overlay ov))))
17724 (defun org-remove-inline-images ()
17725 "Remove inline display of images."
17727 (mapc 'delete-overlay org-inline-image-overlays)
17728 (setq org-inline-image-overlays nil))
17732 ;; Outline functions from `outline-mode-prefix-map'
17733 ;; that can be remapped in Org:
17734 (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
17735 (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
17736 (define-key org-mode-map [remap outline-forward-same-level]
17737 'org-forward-heading-same-level)
17738 (define-key org-mode-map [remap outline-backward-same-level]
17739 'org-backward-heading-same-level)
17740 (define-key org-mode-map [remap show-branches]
17741 'org-kill-note-or-show-branches)
17742 (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
17743 (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
17744 (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
17746 ;; Outline functions from `outline-mode-prefix-map' that can not
17747 ;; be remapped in Org:
17749 ;; - the column "key binding" shows whether the Outline function is still
17750 ;; available in Org mode on the same key that it has been bound to in
17752 ;; - "overridden": key used for a different functionality in Org mode
17753 ;; - else: key still bound to the same Outline function in Org mode
17755 ;; | Outline function | key binding | Org replacement |
17756 ;; |------------------------------------+-------------+-----------------------|
17757 ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
17758 ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
17759 ;; | `outline-up-heading' | `C-c C-u' | still same function |
17760 ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
17761 ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
17762 ;; | `show-entry' | overridden | no replacement |
17763 ;; | `show-children' | `C-c C-i' | visibility cycling |
17764 ;; | `show-branches' | `C-c C-k' | still same function |
17765 ;; | `show-subtree' | overridden | visibility cycling |
17766 ;; | `show-all' | overridden | no replacement |
17767 ;; | `hide-subtree' | overridden | visibility cycling |
17768 ;; | `hide-body' | overridden | no replacement |
17769 ;; | `hide-entry' | overridden | visibility cycling |
17770 ;; | `hide-leaves' | overridden | no replacement |
17771 ;; | `hide-sublevels' | overridden | no replacement |
17772 ;; | `hide-other' | overridden | no replacement |
17774 ;; Make `C-c C-x' a prefix key
17775 (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
17777 ;; TAB key with modifiers
17778 (org-defkey org-mode-map "\C-i" 'org-cycle)
17779 (org-defkey org-mode-map [(tab)] 'org-cycle)
17780 (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
17781 (org-defkey org-mode-map "\M-\t" 'pcomplete)
17782 ;; The following line is necessary under Suse GNU/Linux
17783 (unless (featurep 'xemacs)
17784 (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
17785 (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
17786 (define-key org-mode-map [backtab] 'org-shifttab)
17788 (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
17789 (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
17790 (org-defkey org-mode-map [(meta return)] 'org-meta-return)
17792 ;; Cursor keys with modifiers
17793 (org-defkey org-mode-map [(meta left)] 'org-metaleft)
17794 (org-defkey org-mode-map [(meta right)] 'org-metaright)
17795 (org-defkey org-mode-map [(meta up)] 'org-metaup)
17796 (org-defkey org-mode-map [(meta down)] 'org-metadown)
17798 (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
17799 (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
17800 (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
17801 (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
17803 (org-defkey org-mode-map [(shift up)] 'org-shiftup)
17804 (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
17805 (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
17806 (org-defkey org-mode-map [(shift right)] 'org-shiftright)
17808 (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
17809 (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
17810 (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
17811 (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
17814 (define-key org-mode-map org-babel-key-prefix org-babel-map)
17815 (mapc (lambda (pair)
17816 (define-key org-babel-map (car pair) (cdr pair)))
17817 org-babel-key-bindings)
17819 ;;; Extra keys for tty access.
17820 ;; We only set them when really needed because otherwise the
17821 ;; menus don't show the simple keys
17823 (when (or org-use-extra-keys
17824 (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
17825 (not window-system))
17826 (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
17827 (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
17828 (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
17829 (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
17830 (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
17831 (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
17832 (org-defkey org-mode-map [?\e (right)] 'org-metaright)
17833 (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
17834 (org-defkey org-mode-map [?\e (up)] 'org-metaup)
17835 (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
17836 (org-defkey org-mode-map [?\e (down)] 'org-metadown)
17837 (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
17838 (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
17839 (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
17840 (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
17841 (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
17842 (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
17843 (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
17844 (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
17845 (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
17846 (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
17847 (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
17848 (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
17849 (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
17850 (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
17851 (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
17852 (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
17853 (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
17855 ;; All the other keys
17857 (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
17858 (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
17859 (if (boundp 'narrow-map)
17860 (org-defkey narrow-map "s" 'org-narrow-to-subtree)
17861 (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
17862 (if (boundp 'narrow-map)
17863 (org-defkey narrow-map "b" 'org-narrow-to-block)
17864 (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
17865 (if (boundp 'narrow-map)
17866 (org-defkey narrow-map "e" 'org-narrow-to-element)
17867 (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
17868 (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
17869 (org-defkey org-mode-map "\M-}" 'org-forward-element)
17870 (org-defkey org-mode-map "\M-{" 'org-backward-element)
17871 (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
17872 (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
17873 (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
17874 (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
17875 (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
17876 (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
17877 (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
17878 (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
17879 (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
17880 (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
17881 (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
17882 (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
17883 (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
17884 (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
17885 (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
17886 (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
17887 (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
17888 (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
17889 (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
17890 (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
17891 (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
17892 (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
17893 (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
17894 (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
17895 (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
17896 (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
17897 (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
17898 (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
17899 (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
17900 (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
17901 (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
17902 (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
17903 (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
17904 (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
17905 (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
17906 (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
17907 (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
17908 (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
17909 (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
17910 (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
17911 (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
17912 (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
17913 (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
17914 (org-defkey org-mode-map "\C-c]" 'org-remove-file)
17915 (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
17916 (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
17917 (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
17918 (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
17919 (org-defkey org-mode-map "\C-c^" 'org-sort)
17920 (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
17921 (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
17922 (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
17923 (org-defkey org-mode-map "\C-m" 'org-return)
17924 (org-defkey org-mode-map "\C-j" 'org-return-indent)
17925 (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
17926 (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
17927 (org-defkey org-mode-map "\C-c+" 'org-table-sum)
17928 (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
17929 (org-defkey org-mode-map "\C-c'" 'org-edit-special)
17930 (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
17931 (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
17932 (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
17933 (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
17934 (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
17935 (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
17936 (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
17937 (org-defkey org-mode-map "\C-c\C-e" 'org-export)
17938 (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
17939 (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
17940 (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
17941 (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
17942 (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
17943 (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
17944 (org-defkey org-mode-map "\M-h" 'org-mark-element)
17945 (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
17946 ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
17948 (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
17949 (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
17950 (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
17952 (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
17953 (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
17954 (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
17955 (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
17956 (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
17957 (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
17958 (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
17959 (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
17960 (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
17961 (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
17962 (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
17963 (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
17964 (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
17965 (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
17966 (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
17967 (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
17968 (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
17969 (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
17970 (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
17971 (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
17972 (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
17973 (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
17975 (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
17976 (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
17977 (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
17978 (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
17979 (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
17981 (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
17983 (define-key org-mode-map "\C-c\C-x!" 'org-reload)
17985 (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
17986 (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
17988 (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
17991 (when (featurep 'xemacs)
17992 (org-defkey org-mode-map 'button3 'popup-mode-menu))
17995 (defconst org-speed-commands-default
17997 ("Outline Navigation")
17998 ("n" . (org-speed-move-safe 'outline-next-visible-heading))
17999 ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
18000 ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
18001 ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
18002 ("u" . (org-speed-move-safe 'outline-up-heading))
18004 ("g" . (org-refile t))
18005 ("Outline Visibility")
18007 ("C" . org-shifttab)
18008 (" " . org-display-outline-path)
18009 (":" . org-columns)
18010 ("Outline Structure Editing")
18011 ("U" . org-shiftmetaup)
18012 ("D" . org-shiftmetadown)
18013 ("r" . org-metaright)
18014 ("l" . org-metaleft)
18015 ("R" . org-shiftmetaright)
18016 ("L" . org-shiftmetaleft)
18017 ("i" . (progn (forward-char 1) (call-interactively
18018 'org-insert-heading-respect-content)))
18021 ("a" . org-archive-subtree-default-with-confirmation)
18022 ("." . org-mark-subtree)
18023 ("#" . org-toggle-comment)
18025 ("I" . org-clock-in)
18026 ("O" . org-clock-out)
18027 ("Meta Data Editing")
18029 ("," . (org-priority))
18030 ("0" . (org-priority ?\ ))
18031 ("1" . (org-priority ?A))
18032 ("2" . (org-priority ?B))
18033 ("3" . (org-priority ?C))
18034 (";" . org-set-tags-command)
18035 ("e" . org-set-effort)
18036 ("E" . org-inc-effort)
18037 ("W" . (lambda(m) (interactive "sMinutes before warning: ")
18038 (org-entry-put (point) "APPT_WARNTIME" m)))
18039 ("Agenda Views etc")
18041 ("/" . org-sparse-tree)
18043 ("o" . org-open-at-point)
18044 ("?" . org-speed-command-help)
18045 ("<" . (org-agenda-set-restriction-lock 'subtree))
18046 (">" . (org-agenda-remove-restriction-lock))
18048 "The default speed commands.")
18050 (defun org-print-speed-command (e)
18051 (if (> (length (car e)) 1)
18056 (princ (make-string (length (car e)) ?-))
18060 (if (symbolp (cdr e))
18061 (princ (symbol-name (cdr e)))
18065 (defun org-speed-command-help ()
18066 "Show the available speed commands."
18068 (if (not org-use-speed-commands)
18069 (error "Speed commands are not activated, customize `org-use-speed-commands'")
18070 (with-output-to-temp-buffer "*Help*"
18071 (princ "User-defined Speed commands\n===========================\n")
18072 (mapc 'org-print-speed-command org-speed-commands-user)
18074 (princ "Built-in Speed commands\n=======================\n")
18075 (mapc 'org-print-speed-command org-speed-commands-default))
18076 (with-current-buffer "*Help*"
18077 (setq truncate-lines t))))
18079 (defun org-speed-move-safe (cmd)
18080 "Execute CMD, but make sure that the cursor always ends up in a headline.
18081 If not, return to the original position and throw an error."
18083 (let ((pos (point)))
18084 (call-interactively cmd)
18085 (unless (and (bolp) (org-at-heading-p))
18087 (error "Boundary reached while executing %s" cmd))))
18089 (defvar org-self-insert-command-undo-counter 0)
18091 (defvar org-table-auto-blank-field) ; defined in org-table.el
18092 (defvar org-speed-command nil)
18094 (define-obsolete-function-alias
18095 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
18097 (defun org-speed-command-activate (keys)
18098 "Hook for activating single-letter speed commands.
18099 `org-speed-commands-default' specifies a minimal command set.
18100 Use `org-speed-commands-user' for further customization."
18101 (when (or (and (bolp) (looking-at org-outline-regexp))
18102 (and (functionp org-use-speed-commands)
18103 (funcall org-use-speed-commands)))
18104 (cdr (assoc keys (append org-speed-commands-user
18105 org-speed-commands-default)))))
18107 (define-obsolete-function-alias
18108 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
18110 (defun org-babel-speed-command-activate (keys)
18111 "Hook for activating single-letter code block commands."
18112 (when (and (bolp) (looking-at org-babel-src-block-regexp))
18113 (cdr (assoc keys org-babel-key-bindings))))
18115 (defcustom org-speed-command-hook
18116 '(org-speed-command-default-hook org-babel-speed-command-hook)
18117 "Hook for activating speed commands at strategic locations.
18118 Hook functions are called in sequence until a valid handler is
18121 Each hook takes a single argument, a user-pressed command key
18122 which is also a `self-insert-command' from the global map.
18124 Within the hook, examine the cursor position and the command key
18125 and return nil or a valid handler as appropriate. Handler could
18126 be one of an interactive command, a function, or a form.
18128 Set `org-use-speed-commands' to non-nil value to enable this
18129 hook. The default setting is `org-speed-command-activate'."
18130 :group 'org-structure
18134 (defun org-self-insert-command (N)
18135 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
18136 If the cursor is in a table looking at whitespace, the whitespace is
18137 overwritten, and the table is not marked as requiring realignment."
18139 (org-check-before-invisible-edit 'insert)
18141 ((and org-use-speed-commands
18142 (setq org-speed-command
18143 (run-hook-with-args-until-success
18144 'org-speed-command-hook (this-command-keys))))
18146 ((commandp org-speed-command)
18147 (setq this-command org-speed-command)
18148 (call-interactively org-speed-command))
18149 ((functionp org-speed-command)
18150 (funcall org-speed-command))
18151 ((and org-speed-command (listp org-speed-command))
18152 (eval org-speed-command))
18153 (t (let (org-use-speed-commands)
18154 (call-interactively 'org-self-insert-command)))))
18158 ;; check if we blank the field, and if that triggers align
18159 (and (featurep 'org-table) org-table-auto-blank-field
18160 (member last-command
18161 '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
18162 (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
18163 ;; got extra space, this field does not determine column width
18164 (let (org-table-may-need-update) (org-table-blank-field))
18165 ;; no extra space, this field may determine column width
18166 (org-table-blank-field)))
18169 (looking-at "[^|\n]* |"))
18170 (let (org-table-may-need-update)
18171 (goto-char (1- (match-end 0)))
18172 (backward-delete-char 1)
18173 (goto-char (match-beginning 0))
18174 (self-insert-command N)))
18176 (setq org-table-may-need-update t)
18177 (self-insert-command N)
18178 (org-fix-tags-on-the-fly)
18179 (if org-self-insert-cluster-for-undo
18180 (if (not (eq last-command 'org-self-insert-command))
18181 (setq org-self-insert-command-undo-counter 1)
18182 (if (>= org-self-insert-command-undo-counter 20)
18183 (setq org-self-insert-command-undo-counter 1)
18184 (and (> org-self-insert-command-undo-counter 0)
18185 buffer-undo-list (listp buffer-undo-list)
18186 (not (cadr buffer-undo-list)) ; remove nil entry
18187 (setcdr buffer-undo-list (cddr buffer-undo-list)))
18188 (setq org-self-insert-command-undo-counter
18189 (1+ org-self-insert-command-undo-counter))))))))
18191 (defun org-check-before-invisible-edit (kind)
18192 "Check is editing if kind KIND would be dangerous with invisible text around.
18193 The detailed reaction depends on the user option `org-catch-invisible-edits'."
18194 ;; First, try to get out of here as quickly as possible, to reduce overhead
18195 (if (and org-catch-invisible-edits
18196 (or (not (boundp 'visible-mode)) (not visible-mode))
18197 (or (get-char-property (point) 'invisible)
18198 (get-char-property (max (point-min) (1- (point))) 'invisible)))
18199 ;; OK, we need to take a closer look
18200 (let* ((invisible-at-point (get-char-property (point) 'invisible))
18201 (invisible-before-point (if (bobp) nil (get-char-property
18202 (1- (point)) 'invisible)))
18203 (border-and-ok-direction
18205 ;; Check if we are acting predictably before invisible text
18206 (and invisible-at-point (not invisible-before-point)
18207 (memq kind '(insert delete-backward)))
18208 ;; Check if we are acting predictably after invisible text
18209 ;; This works not well, and I have turned it off. It seems
18210 ;; better to always show and stop after invisible text.
18211 ;; (and (not invisible-at-point) invisible-before-point
18212 ;; (memq kind '(insert delete)))
18214 (when (or (memq invisible-at-point '(outline org-hide-block t))
18215 (memq invisible-before-point '(outline org-hide-block t)))
18216 (if (eq org-catch-invisible-edits 'error)
18217 (error "Editing in invisible areas is prohibited - make visible first"))
18218 (if (and org-custom-properties-overlays
18219 (y-or-n-p "Display invisible properties in this buffer? "))
18220 (org-toggle-custom-properties-visibility)
18221 ;; Make the area visible
18223 (if invisible-before-point
18224 (goto-char (previous-single-char-property-change
18225 (point) 'invisible)))
18228 ((eq org-catch-invisible-edits 'show)
18229 ;; That's it, we do the edit after showing
18231 "Unfolding invisible region around point before editing")
18233 ((and (eq org-catch-invisible-edits 'smart)
18234 border-and-ok-direction)
18235 (message "Unfolding invisible region around point before editing"))
18237 ;; Don't do the edit, make the user repeat it in full visibility
18238 (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
18240 (defun org-fix-tags-on-the-fly ()
18241 (when (and (equal (char-after (point-at-bol)) ?*)
18242 (org-at-heading-p))
18243 (org-align-tags-here org-tags-column)))
18245 (defun org-delete-backward-char (N)
18246 "Like `delete-backward-char', insert whitespace at field end in tables.
18247 When deleting backwards, in tables this function will insert whitespace in
18248 front of the next \"|\" separator, to keep the table aligned. The table will
18249 still be marked for re-alignment if the field did fill the entire column,
18250 because, in this case the deletion might narrow the column."
18252 (org-check-before-invisible-edit 'delete-backward)
18253 (if (and (org-table-p)
18255 (string-match "|" (buffer-substring (point-at-bol) (point)))
18256 (looking-at ".*?|"))
18257 (let ((pos (point))
18258 (noalign (looking-at "[^|\n\r]* |"))
18259 (c org-table-may-need-update))
18260 (backward-delete-char N)
18261 (if (not overwrite-mode)
18263 (skip-chars-forward "^|")
18265 (goto-char (1- pos))))
18266 ;; noalign: if there were two spaces at the end, this field
18267 ;; does not determine the width of the column.
18268 (if noalign (setq org-table-may-need-update c)))
18269 (backward-delete-char N)
18270 (org-fix-tags-on-the-fly)))
18272 (defun org-delete-char (N)
18273 "Like `delete-char', but insert whitespace at field end in tables.
18274 When deleting characters, in tables this function will insert whitespace in
18275 front of the next \"|\" separator, to keep the table aligned. The table will
18276 still be marked for re-alignment if the field did fill the entire column,
18277 because, in this case the deletion might narrow the column."
18279 (org-check-before-invisible-edit 'delete)
18280 (if (and (org-table-p)
18282 (not (= (char-after) ?|))
18284 (if (looking-at ".*?|")
18285 (let ((pos (point))
18286 (noalign (looking-at "[^|\n\r]* |"))
18287 (c org-table-may-need-update))
18288 (replace-match (concat
18289 (substring (match-string 0) 1 -1)
18292 ;; noalign: if there were two spaces at the end, this field
18293 ;; does not determine the width of the column.
18294 (if noalign (setq org-table-may-need-update c)))
18297 (org-fix-tags-on-the-fly)))
18299 ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
18300 (put 'org-self-insert-command 'delete-selection t)
18301 (put 'orgtbl-self-insert-command 'delete-selection t)
18302 (put 'org-delete-char 'delete-selection 'supersede)
18303 (put 'org-delete-backward-char 'delete-selection 'supersede)
18304 (put 'org-yank 'delete-selection 'yank)
18306 ;; Make `flyspell-mode' delay after some commands
18307 (put 'org-self-insert-command 'flyspell-delayed t)
18308 (put 'orgtbl-self-insert-command 'flyspell-delayed t)
18309 (put 'org-delete-char 'flyspell-delayed t)
18310 (put 'org-delete-backward-char 'flyspell-delayed t)
18312 ;; Make pabbrev-mode expand after org-mode commands
18313 (put 'org-self-insert-command 'pabbrev-expand-after-command t)
18314 (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
18316 ;; How to do this: Measure non-white length of current string
18317 ;; If equal to column width, we should realign.
18319 (defun org-remap (map &rest commands)
18320 "In MAP, remap the functions given in COMMANDS.
18321 COMMANDS is a list of alternating OLDDEF NEWDEF command names."
18324 (setq old (pop commands) new (pop commands))
18325 (if (fboundp 'command-remapping)
18326 (org-defkey map (vector 'remap old) new)
18327 (substitute-key-definition old new map global-map)))))
18329 (when (eq org-enable-table-editor 'optimized)
18330 ;; If the user wants maximum table support, we need to hijack
18331 ;; some standard editing functions
18332 (org-remap org-mode-map
18333 'self-insert-command 'org-self-insert-command
18334 'delete-char 'org-delete-char
18335 'delete-backward-char 'org-delete-backward-char)
18336 (org-defkey org-mode-map "|" 'org-force-self-insert))
18338 (defvar org-ctrl-c-ctrl-c-hook nil
18339 "Hook for functions attaching themselves to `C-c C-c'.
18341 This can be used to add additional functionality to the C-c C-c
18342 key which executes context-dependent commands. This hook is run
18343 before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
18344 run after the last test.
18346 Each function will be called with no arguments. The function
18347 must check if the context is appropriate for it to act. If yes,
18348 it should do its thing and then return a non-nil value. If the
18349 context is wrong, just do nothing and return nil.")
18351 (defvar org-ctrl-c-ctrl-c-final-hook nil
18352 "Hook for functions attaching themselves to `C-c C-c'.
18354 This can be used to add additional functionality to the C-c C-c
18355 key which executes context-dependent commands. This hook is run
18356 after any other test, while `org-ctrl-c-ctrl-c-hook' is run
18357 before the first test.
18359 Each function will be called with no arguments. The function
18360 must check if the context is appropriate for it to act. If yes,
18361 it should do its thing and then return a non-nil value. If the
18362 context is wrong, just do nothing and return nil.")
18364 (defvar org-tab-first-hook nil
18365 "Hook for functions to attach themselves to TAB.
18366 See `org-ctrl-c-ctrl-c-hook' for more information.
18367 This hook runs as the first action when TAB is pressed, even before
18368 `org-cycle' messes around with the `outline-regexp' to cater for
18369 inline tasks and plain list item folding.
18370 If any function in this hook returns t, any other actions that
18371 would have been caused by TAB (such as table field motion or visibility
18372 cycling) will not occur.")
18374 (defvar org-tab-after-check-for-table-hook nil
18375 "Hook for functions to attach themselves to TAB.
18376 See `org-ctrl-c-ctrl-c-hook' for more information.
18377 This hook runs after it has been established that the cursor is not in a
18378 table, but before checking if the cursor is in a headline or if global cycling
18380 If any function in this hook returns t, not other actions like visibility
18381 cycling will be done.")
18383 (defvar org-tab-after-check-for-cycling-hook nil
18384 "Hook for functions to attach themselves to TAB.
18385 See `org-ctrl-c-ctrl-c-hook' for more information.
18386 This hook runs after it has been established that not table field motion and
18387 not visibility should be done because of current context. This is probably
18388 the place where a package like yasnippets can hook in.")
18390 (defvar org-tab-before-tab-emulation-hook nil
18391 "Hook for functions to attach themselves to TAB.
18392 See `org-ctrl-c-ctrl-c-hook' for more information.
18393 This hook runs after every other options for TAB have been exhausted, but
18394 before indentation and \t insertion takes place.")
18396 (defvar org-metaleft-hook nil
18397 "Hook for functions attaching themselves to `M-left'.
18398 See `org-ctrl-c-ctrl-c-hook' for more information.")
18399 (defvar org-metaright-hook nil
18400 "Hook for functions attaching themselves to `M-right'.
18401 See `org-ctrl-c-ctrl-c-hook' for more information.")
18402 (defvar org-metaup-hook nil
18403 "Hook for functions attaching themselves to `M-up'.
18404 See `org-ctrl-c-ctrl-c-hook' for more information.")
18405 (defvar org-metadown-hook nil
18406 "Hook for functions attaching themselves to `M-down'.
18407 See `org-ctrl-c-ctrl-c-hook' for more information.")
18408 (defvar org-shiftmetaleft-hook nil
18409 "Hook for functions attaching themselves to `M-S-left'.
18410 See `org-ctrl-c-ctrl-c-hook' for more information.")
18411 (defvar org-shiftmetaright-hook nil
18412 "Hook for functions attaching themselves to `M-S-right'.
18413 See `org-ctrl-c-ctrl-c-hook' for more information.")
18414 (defvar org-shiftmetaup-hook nil
18415 "Hook for functions attaching themselves to `M-S-up'.
18416 See `org-ctrl-c-ctrl-c-hook' for more information.")
18417 (defvar org-shiftmetadown-hook nil
18418 "Hook for functions attaching themselves to `M-S-down'.
18419 See `org-ctrl-c-ctrl-c-hook' for more information.")
18420 (defvar org-metareturn-hook nil
18421 "Hook for functions attaching themselves to `M-RET'.
18422 See `org-ctrl-c-ctrl-c-hook' for more information.")
18423 (defvar org-shiftup-hook nil
18424 "Hook for functions attaching themselves to `S-up'.
18425 See `org-ctrl-c-ctrl-c-hook' for more information.")
18426 (defvar org-shiftup-final-hook nil
18427 "Hook for functions attaching themselves to `S-up'.
18428 This one runs after all other options except shift-select have been excluded.
18429 See `org-ctrl-c-ctrl-c-hook' for more information.")
18430 (defvar org-shiftdown-hook nil
18431 "Hook for functions attaching themselves to `S-down'.
18432 See `org-ctrl-c-ctrl-c-hook' for more information.")
18433 (defvar org-shiftdown-final-hook nil
18434 "Hook for functions attaching themselves to `S-down'.
18435 This one runs after all other options except shift-select have been excluded.
18436 See `org-ctrl-c-ctrl-c-hook' for more information.")
18437 (defvar org-shiftleft-hook nil
18438 "Hook for functions attaching themselves to `S-left'.
18439 See `org-ctrl-c-ctrl-c-hook' for more information.")
18440 (defvar org-shiftleft-final-hook nil
18441 "Hook for functions attaching themselves to `S-left'.
18442 This one runs after all other options except shift-select have been excluded.
18443 See `org-ctrl-c-ctrl-c-hook' for more information.")
18444 (defvar org-shiftright-hook nil
18445 "Hook for functions attaching themselves to `S-right'.
18446 See `org-ctrl-c-ctrl-c-hook' for more information.")
18447 (defvar org-shiftright-final-hook nil
18448 "Hook for functions attaching themselves to `S-right'.
18449 This one runs after all other options except shift-select have been excluded.
18450 See `org-ctrl-c-ctrl-c-hook' for more information.")
18452 (defun org-modifier-cursor-error ()
18453 "Throw an error, a modified cursor command was applied in wrong context."
18454 (error "This command is active in special context like tables, headlines or items"))
18456 (defun org-shiftselect-error ()
18457 "Throw an error because Shift-Cursor command was applied in wrong context."
18458 (if (and (boundp 'shift-select-mode) shift-select-mode)
18459 (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
18460 (error "This command works only in special context like headlines or timestamps")))
18462 (defun org-call-for-shift-select (cmd)
18463 (let ((this-command-keys-shift-translated t))
18464 (call-interactively cmd)))
18466 (defun org-shifttab (&optional arg)
18467 "Global visibility cycling or move to previous table field.
18468 Calls `org-cycle' with argument t, or `org-table-previous-field', depending
18470 See the individual commands for more information."
18473 ((org-at-table-p) (call-interactively 'org-table-previous-field))
18475 (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
18476 (message "Content view to level: %d" arg)
18477 (org-content (prefix-numeric-value arg2))
18478 (setq org-cycle-global-status 'overview)))
18479 (t (call-interactively 'org-global-cycle))))
18481 (defun org-shiftmetaleft ()
18482 "Promote subtree or delete table column.
18483 Calls `org-promote-subtree', `org-outdent-item-tree', or
18484 `org-table-delete-column', depending on context. See the
18485 individual commands for more information."
18488 ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
18489 ((org-at-table-p) (call-interactively 'org-table-delete-column))
18490 ((org-at-heading-p) (call-interactively 'org-promote-subtree))
18491 ((if (not (org-region-active-p)) (org-at-item-p)
18492 (save-excursion (goto-char (region-beginning))
18494 (call-interactively 'org-outdent-item-tree))
18495 (t (org-modifier-cursor-error))))
18497 (defun org-shiftmetaright ()
18498 "Demote subtree or insert table column.
18499 Calls `org-demote-subtree', `org-indent-item-tree', or
18500 `org-table-insert-column', depending on context. See the
18501 individual commands for more information."
18504 ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
18505 ((org-at-table-p) (call-interactively 'org-table-insert-column))
18506 ((org-at-heading-p) (call-interactively 'org-demote-subtree))
18507 ((if (not (org-region-active-p)) (org-at-item-p)
18508 (save-excursion (goto-char (region-beginning))
18510 (call-interactively 'org-indent-item-tree))
18511 (t (org-modifier-cursor-error))))
18513 (defun org-shiftmetaup (&optional arg)
18514 "Move subtree up or kill table row.
18515 Calls `org-move-subtree-up' or `org-table-kill-row' or
18516 `org-move-item-up' or `org-timestamp-up', depending on context.
18517 See the individual commands for more information."
18520 ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
18521 ((org-at-table-p) (call-interactively 'org-table-kill-row))
18522 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18523 ((org-at-item-p) (call-interactively 'org-move-item-up))
18524 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18525 (call-interactively 'org-timestamp-up)))
18526 (t (org-modifier-cursor-error))))
18528 (defun org-shiftmetadown (&optional arg)
18529 "Move subtree down or insert table row.
18530 Calls `org-move-subtree-down' or `org-table-insert-row' or
18531 `org-move-item-down' or `org-timestamp-up', depending on context.
18532 See the individual commands for more information."
18535 ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
18536 ((org-at-table-p) (call-interactively 'org-table-insert-row))
18537 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18538 ((org-at-item-p) (call-interactively 'org-move-item-down))
18539 ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
18540 (call-interactively 'org-timestamp-down)))
18541 (t (org-modifier-cursor-error))))
18543 (defsubst org-hidden-tree-error ()
18545 "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
18547 (defun org-metaleft (&optional arg)
18548 "Promote heading or move table column to left.
18549 Calls `org-do-promote' or `org-table-move-column', depending on context.
18550 With no specific context, calls the Emacs default `backward-word'.
18551 See the individual commands for more information."
18554 ((run-hook-with-args-until-success 'org-metaleft-hook))
18555 ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
18556 ((org-with-limited-levels
18557 (or (org-at-heading-p)
18558 (and (org-region-active-p)
18560 (goto-char (region-beginning))
18561 (org-at-heading-p)))))
18562 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18563 (call-interactively 'org-do-promote))
18564 ;; At an inline task.
18565 ((org-at-heading-p)
18566 (call-interactively 'org-inlinetask-promote))
18567 ((or (org-at-item-p)
18568 (and (org-region-active-p)
18570 (goto-char (region-beginning))
18572 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18573 (call-interactively 'org-outdent-item))
18574 (t (call-interactively 'backward-word))))
18576 (defun org-metaright (&optional arg)
18577 "Demote a subtree, a list item or move table column to right.
18578 In front of a drawer or a block keyword, indent it correctly.
18579 With no specific context, calls the Emacs default `forward-word'.
18580 See the individual commands for more information."
18583 ((run-hook-with-args-until-success 'org-metaright-hook))
18584 ((org-at-table-p) (call-interactively 'org-table-move-column))
18585 ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
18586 ((org-at-block-p) (call-interactively 'org-indent-block))
18587 ((org-with-limited-levels
18588 (or (org-at-heading-p)
18589 (and (org-region-active-p)
18591 (goto-char (region-beginning))
18592 (org-at-heading-p)))))
18593 (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
18594 (call-interactively 'org-do-demote))
18595 ;; At an inline task.
18596 ((org-at-heading-p)
18597 (call-interactively 'org-inlinetask-demote))
18598 ((or (org-at-item-p)
18599 (and (org-region-active-p)
18601 (goto-char (region-beginning))
18603 (when (org-check-for-hidden 'items) (org-hidden-tree-error))
18604 (call-interactively 'org-indent-item))
18605 (t (call-interactively 'forward-word))))
18607 (defun org-check-for-hidden (what)
18608 "Check if there are hidden headlines/items in the current visual line.
18609 WHAT can be either `headlines' or `items'. If the current line is
18610 an outline or item heading and it has a folded subtree below it,
18611 this function returns t, nil otherwise."
18613 ((eq what 'headlines) org-outline-regexp-bol)
18614 ((eq what 'items) (org-item-beginning-re))
18615 (t (error "This should not happen"))))
18619 (unless (org-region-active-p)
18620 (setq beg (point-at-bol))
18621 (beginning-of-line 2)
18622 (while (and (not (eobp)) ;; this is like `next-line'
18623 (get-char-property (1- (point)) 'invisible))
18624 (beginning-of-line 2))
18627 (goto-char (point-at-eol))
18628 (setq end (max end (point)))
18629 (while (re-search-forward re end t)
18630 (if (get-char-property (match-beginning 0) 'invisible)
18634 (autoload 'org-element-at-point "org-element")
18636 (declare-function org-element-at-point "org-element" (&optional keep-trail))
18637 (declare-function org-element-type "org-element" (element))
18638 (declare-function org-element-context "org-element" ())
18639 (declare-function org-element-contents "org-element" (element))
18640 (declare-function org-element-property "org-element" (property element))
18641 (declare-function org-element-paragraph-parser "org-element" (limit))
18642 (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
18643 (declare-function org-element-nested-p "org-element" (elem-a elem-b))
18644 (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
18645 (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
18646 (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
18648 (defun org-metaup (&optional arg)
18649 "Move subtree up or move table row up.
18650 Calls `org-move-subtree-up' or `org-table-move-row' or
18651 `org-move-item-up', depending on context. See the individual commands
18652 for more information."
18655 ((run-hook-with-args-until-success 'org-metaup-hook))
18656 ((org-region-active-p)
18657 (let* ((a (min (region-beginning) (region-end)))
18658 (b (1- (max (region-beginning) (region-end))))
18659 (c (save-excursion (goto-char a)
18660 (move-beginning-of-line 0)))
18661 (d (save-excursion (goto-char a)
18662 (move-end-of-line 0) (point))))
18663 (transpose-regions a b c d)
18665 ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
18666 ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
18667 ((org-at-item-p) (call-interactively 'org-move-item-up))
18668 (t (org-drag-element-backward))))
18670 (defun org-metadown (&optional arg)
18671 "Move subtree down or move table row down.
18672 Calls `org-move-subtree-down' or `org-table-move-row' or
18673 `org-move-item-down', depending on context. See the individual
18674 commands for more information."
18677 ((run-hook-with-args-until-success 'org-metadown-hook))
18678 ((org-region-active-p)
18679 (let* ((a (min (region-beginning) (region-end)))
18680 (b (max (region-beginning) (region-end)))
18681 (c (save-excursion (goto-char b)
18682 (move-beginning-of-line 1)))
18683 (d (save-excursion (goto-char b)
18684 (move-end-of-line 1) (1+ (point)))))
18685 (transpose-regions a b c d)
18687 ((org-at-table-p) (call-interactively 'org-table-move-row))
18688 ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
18689 ((org-at-item-p) (call-interactively 'org-move-item-down))
18690 (t (org-drag-element-forward))))
18692 (defun org-shiftup (&optional arg)
18693 "Increase item in timestamp or increase priority of current headline.
18694 Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
18695 depending on context. See the individual commands for more information."
18698 ((run-hook-with-args-until-success 'org-shiftup-hook))
18699 ((and org-support-shift-select (org-region-active-p))
18700 (org-call-for-shift-select 'previous-line))
18701 ((org-at-timestamp-p t)
18702 (call-interactively (if org-edit-timestamp-down-means-later
18703 'org-timestamp-down 'org-timestamp-up)))
18704 ((and (not (eq org-support-shift-select 'always))
18705 org-enable-priority-commands
18706 (org-at-heading-p))
18707 (call-interactively 'org-priority-up))
18708 ((and (not org-support-shift-select) (org-at-item-p))
18709 (call-interactively 'org-previous-item))
18710 ((org-clocktable-try-shift 'up arg))
18711 ((run-hook-with-args-until-success 'org-shiftup-final-hook))
18712 (org-support-shift-select
18713 (org-call-for-shift-select 'previous-line))
18714 (t (org-shiftselect-error))))
18716 (defun org-shiftdown (&optional arg)
18717 "Decrease item in timestamp or decrease priority of current headline.
18718 Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
18719 depending on context. See the individual commands for more information."
18722 ((run-hook-with-args-until-success 'org-shiftdown-hook))
18723 ((and org-support-shift-select (org-region-active-p))
18724 (org-call-for-shift-select 'next-line))
18725 ((org-at-timestamp-p t)
18726 (call-interactively (if org-edit-timestamp-down-means-later
18727 'org-timestamp-up 'org-timestamp-down)))
18728 ((and (not (eq org-support-shift-select 'always))
18729 org-enable-priority-commands
18730 (org-at-heading-p))
18731 (call-interactively 'org-priority-down))
18732 ((and (not org-support-shift-select) (org-at-item-p))
18733 (call-interactively 'org-next-item))
18734 ((org-clocktable-try-shift 'down arg))
18735 ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
18736 (org-support-shift-select
18737 (org-call-for-shift-select 'next-line))
18738 (t (org-shiftselect-error))))
18740 (defun org-shiftright (&optional arg)
18741 "Cycle the thing at point or in the current line, depending on context.
18742 Depending on context, this does one of the following:
18744 - switch a timestamp at point one day into the future
18745 - on a headline, switch to the next TODO keyword.
18746 - on an item, switch entire list to the next bullet type
18747 - on a property line, switch to the next allowed value
18748 - on a clocktable definition line, move time block into the future"
18751 ((run-hook-with-args-until-success 'org-shiftright-hook))
18752 ((and org-support-shift-select (org-region-active-p))
18753 (org-call-for-shift-select 'forward-char))
18754 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
18755 ((and (not (eq org-support-shift-select 'always))
18756 (org-at-heading-p))
18757 (let ((org-inhibit-logging
18758 (not org-treat-S-cursor-todo-selection-as-state-change))
18759 (org-inhibit-blocking
18760 (not org-treat-S-cursor-todo-selection-as-state-change)))
18761 (org-call-with-arg 'org-todo 'right)))
18762 ((or (and org-support-shift-select
18763 (not (eq org-support-shift-select 'always))
18764 (org-at-item-bullet-p))
18765 (and (not org-support-shift-select) (org-at-item-p)))
18766 (org-call-with-arg 'org-cycle-list-bullet nil))
18767 ((and (not (eq org-support-shift-select 'always))
18768 (org-at-property-p))
18769 (call-interactively 'org-property-next-allowed-value))
18770 ((org-clocktable-try-shift 'right arg))
18771 ((run-hook-with-args-until-success 'org-shiftright-final-hook))
18772 (org-support-shift-select
18773 (org-call-for-shift-select 'forward-char))
18774 (t (org-shiftselect-error))))
18776 (defun org-shiftleft (&optional arg)
18777 "Cycle the thing at point or in the current line, depending on context.
18778 Depending on context, this does one of the following:
18780 - switch a timestamp at point one day into the past
18781 - on a headline, switch to the previous TODO keyword.
18782 - on an item, switch entire list to the previous bullet type
18783 - on a property line, switch to the previous allowed value
18784 - on a clocktable definition line, move time block into the past"
18787 ((run-hook-with-args-until-success 'org-shiftleft-hook))
18788 ((and org-support-shift-select (org-region-active-p))
18789 (org-call-for-shift-select 'backward-char))
18790 ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
18791 ((and (not (eq org-support-shift-select 'always))
18792 (org-at-heading-p))
18793 (let ((org-inhibit-logging
18794 (not org-treat-S-cursor-todo-selection-as-state-change))
18795 (org-inhibit-blocking
18796 (not org-treat-S-cursor-todo-selection-as-state-change)))
18797 (org-call-with-arg 'org-todo 'left)))
18798 ((or (and org-support-shift-select
18799 (not (eq org-support-shift-select 'always))
18800 (org-at-item-bullet-p))
18801 (and (not org-support-shift-select) (org-at-item-p)))
18802 (org-call-with-arg 'org-cycle-list-bullet 'previous))
18803 ((and (not (eq org-support-shift-select 'always))
18804 (org-at-property-p))
18805 (call-interactively 'org-property-previous-allowed-value))
18806 ((org-clocktable-try-shift 'left arg))
18807 ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
18808 (org-support-shift-select
18809 (org-call-for-shift-select 'backward-char))
18810 (t (org-shiftselect-error))))
18812 (defun org-shiftcontrolright ()
18813 "Switch to next TODO set."
18816 ((and org-support-shift-select (org-region-active-p))
18817 (org-call-for-shift-select 'forward-word))
18818 ((and (not (eq org-support-shift-select 'always))
18819 (org-at-heading-p))
18820 (org-call-with-arg 'org-todo 'nextset))
18821 (org-support-shift-select
18822 (org-call-for-shift-select 'forward-word))
18823 (t (org-shiftselect-error))))
18825 (defun org-shiftcontrolleft ()
18826 "Switch to previous TODO set."
18829 ((and org-support-shift-select (org-region-active-p))
18830 (org-call-for-shift-select 'backward-word))
18831 ((and (not (eq org-support-shift-select 'always))
18832 (org-at-heading-p))
18833 (org-call-with-arg 'org-todo 'previousset))
18834 (org-support-shift-select
18835 (org-call-for-shift-select 'backward-word))
18836 (t (org-shiftselect-error))))
18838 (defun org-shiftcontrolup ()
18839 "Change timestamps synchronously up in CLOCK log lines."
18841 (cond ((and (not org-support-shift-select)
18842 (org-at-clock-log-p)
18843 (org-at-timestamp-p t))
18844 (org-clock-timestamps-up))
18845 (t (org-shiftselect-error))))
18847 (defun org-shiftcontroldown ()
18848 "Change timestamps synchronously down in CLOCK log lines."
18850 (cond ((and (not org-support-shift-select)
18851 (org-at-clock-log-p)
18852 (org-at-timestamp-p t))
18853 (org-clock-timestamps-down))
18854 (t (org-shiftselect-error))))
18856 (defun org-ctrl-c-ret ()
18857 "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
18860 ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
18861 (t (call-interactively 'org-insert-heading))))
18863 (defun org-find-visible ()
18865 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18866 (get-char-property s 'invisible)))
18868 (defun org-find-invisible ()
18870 (while (and (not (= (point-max) (setq s (next-overlay-change s))))
18871 (not (get-char-property s 'invisible))))
18874 (defun org-copy-visible (beg end)
18875 "Copy the visible parts of the region."
18880 (narrow-to-region beg end)
18881 (setq s (goto-char (point-min)))
18882 (while (not (= (point) (point-max)))
18883 (goto-char (org-find-invisible))
18884 (push (buffer-substring s (point)) snippets)
18885 (setq s (goto-char (org-find-visible))))))
18886 (kill-new (apply 'concat (nreverse snippets)))))
18888 (defun org-copy-special ()
18889 "Copy region in table or copy current subtree.
18890 Calls `org-table-copy' or `org-copy-subtree', depending on context.
18891 See the individual commands for more information."
18893 (call-interactively
18894 (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
18896 (defun org-cut-special ()
18897 "Cut region in table or cut current subtree.
18898 Calls `org-table-copy' or `org-cut-subtree', depending on context.
18899 See the individual commands for more information."
18901 (call-interactively
18902 (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
18904 (defun org-paste-special (arg)
18905 "Paste rectangular region into table, or past subtree relative to level.
18906 Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
18907 See the individual commands for more information."
18909 (if (org-at-table-p)
18910 (org-table-paste-rectangle)
18911 (org-paste-subtree arg)))
18913 (defun org-edit-special (&optional arg)
18914 "Call a special editor for the stuff at point.
18915 When at a table, call the formula editor with `org-table-edit-formulas'.
18916 When at the first line of an src example, call `org-edit-src-code'.
18917 When in an #+include line, visit the include file. Otherwise call
18918 `ffap' to visit the file at point."
18920 ;; possibly prep session before editing source
18922 (let* ((info (org-babel-get-src-block-info))
18923 (lang (nth 0 info))
18924 (params (nth 2 info))
18925 (session (cdr (assoc :session params))))
18926 (when (and info session) ;; we are in a source-code block with a session
18928 (intern (concat "org-babel-prep-session:" lang)) session params))))
18929 (cond ;; proceed with `org-edit-special'
18931 (beginning-of-line 1)
18932 (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
18933 (find-file (org-trim (match-string 1))))
18934 ((org-edit-src-code))
18935 ((org-edit-fixed-width-region))
18936 ((org-at-table.el-p)
18937 (org-edit-src-code))
18938 ((or (org-at-table-p)
18940 (beginning-of-line 1)
18941 (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
18942 (call-interactively 'org-table-edit-formulas))
18943 (t (call-interactively 'ffap))))
18945 (defvar org-table-coordinate-overlays) ; defined in org-table.el
18946 (defun org-ctrl-c-ctrl-c (&optional arg)
18947 "Set tags in headline, or update according to changed information at point.
18949 This command does many different things, depending on context:
18951 - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
18952 this is what we do.
18954 - If the cursor is on a statistics cookie, update it.
18956 - If the cursor is in a headline, prompt for tags and insert them
18957 into the current line, aligned to `org-tags-column'. When called
18958 with prefix arg, realign all tags in the current buffer.
18960 - If the cursor is in one of the special #+KEYWORD lines, this
18961 triggers scanning the buffer for these lines and updating the
18964 - If the cursor is inside a table, realign the table. This command
18965 works even if the automatic table editor has been turned off.
18967 - If the cursor is on a #+TBLFM line, re-apply the formulas to
18970 - If the cursor is at a footnote reference or definition, jump to
18971 the corresponding definition or references, respectively.
18973 - If the cursor is a the beginning of a dynamic block, update it.
18975 - If the current buffer is a capture buffer, close note and file it.
18977 - If the cursor is on a <<<target>>>, update radio targets and
18978 corresponding links in this buffer.
18980 - If the cursor is on a numbered item in a plain list, renumber the
18983 - If the cursor is on a checkbox, toggle it.
18985 - If the cursor is on a code block, evaluate it. The variable
18986 `org-confirm-babel-evaluate' can be used to control prompting
18987 before code block evaluation, by default every code block
18988 evaluation requires confirmation. Code block evaluation can be
18989 inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
18991 (let ((org-enable-table-editor t))
18993 ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
18994 org-occur-highlights
18995 org-latex-fragment-image-overlays)
18996 (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
18997 (org-remove-occur-highlights)
18998 (org-remove-latex-fragment-image-overlays)
18999 (message "Temporary highlights/overlays removed from current buffer"))
19000 ((and (local-variable-p 'org-finish-function (current-buffer))
19001 (fboundp org-finish-function))
19002 (funcall org-finish-function))
19003 ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
19004 ((org-in-regexp org-ts-regexp-both)
19005 (org-timestamp-change 0 'day))
19006 ((or (looking-at org-property-start-re)
19007 (org-at-property-p))
19008 (call-interactively 'org-property-action))
19009 ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
19010 ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
19011 (or (org-at-heading-p) (org-at-item-p)))
19012 (call-interactively 'org-update-statistics-cookies))
19013 ((org-at-heading-p) (call-interactively 'org-set-tags))
19014 ((org-at-table.el-p)
19015 (message "Use C-c ' to edit table.el tables"))
19017 (org-table-maybe-eval-formula)
19019 (call-interactively 'org-table-recalculate)
19020 (org-table-maybe-recalculate-line))
19021 (call-interactively 'org-table-align)
19022 (orgtbl-send-table 'maybe))
19023 ((or (org-footnote-at-reference-p)
19024 (org-footnote-at-definition-p))
19025 (call-interactively 'org-footnote-action))
19026 ((org-at-item-checkbox-p)
19027 ;; Cursor at a checkbox: repair list and update checkboxes. Send
19028 ;; list only if at top item.
19029 (let* ((cbox (match-string 1))
19030 (struct (org-list-struct))
19031 (old-struct (copy-tree struct))
19032 (parents (org-list-parents-alist struct))
19033 (orderedp (org-entry-get nil "ORDERED"))
19034 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19036 ;; Use a light version of `org-toggle-checkbox' to avoid
19037 ;; computing list structure twice.
19038 (let ((new-box (cond
19039 ((equal arg '(16)) "[-]")
19040 ((equal arg '(4)) nil)
19041 ((equal "[X]" cbox) "[ ]")
19043 (if (and firstp arg)
19044 ;; If at first item of sub-list, remove check-box from
19045 ;; every item at the same level.
19047 (lambda (pos) (org-list-set-checkbox pos struct new-box))
19048 (org-list-get-all-items
19049 (point-at-bol) struct (org-list-prevs-alist struct)))
19050 (org-list-set-checkbox (point-at-bol) struct new-box)))
19051 ;; Replicate `org-list-write-struct', while grabbing a return
19052 ;; value from `org-list-struct-fix-box'.
19053 (org-list-struct-fix-ind struct parents 2)
19054 (org-list-struct-fix-item-end struct)
19055 (let ((prevs (org-list-prevs-alist struct)))
19056 (org-list-struct-fix-bul struct prevs)
19057 (org-list-struct-fix-ind struct parents)
19059 (org-list-struct-fix-box struct parents prevs orderedp)))
19060 (org-list-struct-apply-struct struct old-struct)
19061 (org-update-checkbox-count-maybe)
19064 "Checkboxes were removed due to unchecked box at line %d"
19065 (org-current-line block-item)))
19066 (when firstp (org-list-send-list 'maybe))))
19068 ;; Cursor at an item: repair list. Do checkbox related actions
19069 ;; only if function was called with an argument. Send list only
19071 (let* ((struct (org-list-struct))
19072 (firstp (= (org-list-get-top-point struct) (point-at-bol)))
19075 (setq old-struct (copy-tree struct))
19077 ;; If at first item of sub-list, add check-box to every
19078 ;; item at the same level.
19081 (unless (org-list-get-checkbox pos struct)
19082 (org-list-set-checkbox pos struct "[ ]")))
19083 (org-list-get-all-items
19084 (point-at-bol) struct (org-list-prevs-alist struct)))
19085 (org-list-set-checkbox (point-at-bol) struct "[ ]")))
19086 (org-list-write-struct
19087 struct (org-list-parents-alist struct) old-struct)
19088 (when arg (org-update-checkbox-count-maybe))
19089 (when firstp (org-list-send-list 'maybe))))
19090 ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
19092 (beginning-of-line 1)
19093 (save-excursion (org-update-dblock)))
19095 (let ((case-fold-search t))
19096 (beginning-of-line 1)
19097 (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
19099 ((or (equal (match-string 1) "TBLFM")
19100 (equal (match-string 1) "tblfm"))
19101 ;; Recalculate the table before this line
19103 (beginning-of-line 1)
19104 (skip-chars-backward " \r\n\t")
19105 (if (org-at-table-p)
19106 (org-call-with-arg 'org-table-recalculate (or arg t)))))
19108 (let ((org-inhibit-startup-visibility-stuff t)
19109 (org-startup-align-all-tables nil))
19110 (when (boundp 'org-table-coordinate-overlays)
19111 (mapc 'delete-overlay org-table-coordinate-overlays)
19112 (setq org-table-coordinate-overlays nil))
19113 (org-save-outline-visibility 'use-markers (org-mode-restart)))
19114 (message "Local setup has been refreshed"))))
19115 ((org-clock-update-time-maybe))
19117 (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
19118 (error "C-c C-c can do nothing useful at this location"))))))
19120 (defun org-mode-restart ()
19121 "Restart Org-mode, to scan again for special lines.
19122 Also updates the keyword regular expressions."
19125 (message "Org-mode restarted"))
19127 (defun org-kill-note-or-show-branches ()
19128 "If this is a Note buffer, abort storing the note. Else call `show-branches'."
19130 (if (not org-finish-function)
19133 (call-interactively 'show-branches))
19134 (let ((org-note-abort t))
19135 (funcall org-finish-function))))
19137 (defun org-return (&optional indent)
19138 "Goto next table row or insert a newline.
19139 Calls `org-table-next-row' or `newline', depending on context.
19140 See the individual commands for more information."
19144 ((or (bobp) (org-in-src-block-p))
19145 (if indent (newline-and-indent) (newline)))
19147 (org-table-justify-field-maybe)
19148 (call-interactively 'org-table-next-row))
19149 ;; when `newline-and-indent' is called within a list, make sure
19150 ;; text moved stays inside the item.
19151 ((and (org-in-item-p) indent)
19152 (if (and (org-at-item-p) (>= (point) (match-end 0)))
19154 (save-match-data (newline))
19155 (org-indent-line-to (length (match-string 0))))
19156 (let ((ind (org-get-indentation)))
19158 (if (org-looking-back org-list-end-re)
19160 (org-indent-line-to ind)))))
19161 ((and org-return-follows-link
19162 (org-at-timestamp-p t)
19163 (not (eq org-ts-what 'after)))
19164 (org-follow-timestamp-link))
19165 ((and org-return-follows-link
19166 (let ((tprop (get-text-property (point) 'face)))
19167 (or (eq tprop 'org-link)
19168 (and (listp tprop) (memq 'org-link tprop)))))
19169 (call-interactively 'org-open-at-point))
19170 ((and (org-at-heading-p)
19172 (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
19176 (t (if indent (newline-and-indent) (newline))))))
19178 (defun org-return-indent ()
19179 "Goto next table row or insert a newline and indent.
19180 Calls `org-table-next-row' or `newline-and-indent', depending on
19181 context. See the individual commands for more information."
19185 (defun org-ctrl-c-star ()
19186 "Compute table, or change heading status of lines.
19187 Calls `org-table-recalculate' or `org-toggle-heading',
19188 depending on context."
19192 (call-interactively 'org-table-recalculate))
19194 ;; Convert all lines in region to list items
19195 (call-interactively 'org-toggle-heading))))
19197 (defun org-ctrl-c-minus ()
19198 "Insert separator line in table or modify bullet status of line.
19199 Also turns a plain line or a region of lines into list items.
19200 Calls `org-table-insert-hline', `org-toggle-item', or
19201 `org-cycle-list-bullet', depending on context."
19205 (call-interactively 'org-table-insert-hline))
19206 ((org-region-active-p)
19207 (call-interactively 'org-toggle-item))
19209 (call-interactively 'org-cycle-list-bullet))
19211 (call-interactively 'org-toggle-item))))
19213 (defun org-toggle-item (arg)
19214 "Convert headings or normal lines to items, items to normal lines.
19215 If there is no active region, only the current line is considered.
19217 If the first non blank line in the region is an headline, convert
19218 all headlines to items, shifting text accordingly.
19220 If it is an item, convert all items to normal lines.
19222 If it is normal text, change region into an item. With a prefix
19223 argument ARG, change each line in region into an item."
19227 ;; Shift text in current section to IND, from point to END.
19228 ;; The function leaves point to END line.
19230 (let ((min-i 1000) (end (copy-marker end)))
19231 ;; First determine the minimum indentation (MIN-I) of
19235 (while (< (point) end)
19236 (let ((i (org-get-indentation)))
19238 ;; Skip blank lines and inline tasks.
19239 ((looking-at "^[ \t]*$"))
19240 ((looking-at org-outline-regexp-bol))
19241 ;; We can't find less than 0 indentation.
19242 ((zerop i) (throw 'exit (setq min-i 0)))
19243 ((< i min-i) (setq min-i i))))
19245 ;; Then indent each line so that a line indented to
19246 ;; MIN-I becomes indented to IND. Ignore blank lines
19247 ;; and inline tasks in the process.
19248 (let ((delta (- ind min-i)))
19249 (while (< (point) end)
19250 (unless (or (looking-at "^[ \t]*$")
19251 (looking-at org-outline-regexp-bol))
19252 (org-indent-line-to (+ (org-get-indentation) delta)))
19253 (forward-line)))))))
19256 ;; Return beginning of first non-blank line, starting from
19261 (skip-chars-forward " \r\t\n")
19264 ;; Determine boundaries of changes.
19265 (if (org-region-active-p)
19266 (setq beg (funcall skip-blanks (region-beginning))
19267 end (copy-marker (region-end)))
19268 (setq beg (funcall skip-blanks (point-at-bol))
19269 end (copy-marker (point-at-eol))))
19270 ;; Depending on the starting line, choose an action on the text
19271 ;; between BEG and END.
19272 (org-with-limited-levels
19276 ;; Case 1. Start at an item: de-itemize. Note that it only
19277 ;; happens when a region is active: `org-ctrl-c-minus'
19278 ;; would call `org-cycle-list-bullet' otherwise.
19280 (while (< (point) end)
19281 (when (org-at-item-p)
19282 (skip-chars-forward " \t")
19283 (delete-region (point) (match-end 0)))
19285 ;; Case 2. Start at an heading: convert to items.
19286 ((org-at-heading-p)
19287 (let* ((bul (org-list-bullet-string "-"))
19288 (bul-len (length bul))
19289 ;; Indentation of the first heading. It should be
19290 ;; relative to the indentation of its parent, if any.
19291 (start-ind (save-excursion
19293 ((not org-adapt-indentation) 0)
19294 ((not (outline-previous-heading)) 0)
19295 (t (length (match-string 0))))))
19296 ;; Level of first heading. Further headings will be
19297 ;; compared to it to determine hierarchy in the list.
19298 (ref-level (org-reduced-level (org-outline-level))))
19299 (while (< (point) end)
19300 (let* ((level (org-reduced-level (org-outline-level)))
19301 (delta (max 0 (- level ref-level))))
19302 ;; If current headline is less indented than the first
19303 ;; one, set it as reference, in order to preserve
19305 (when (< level ref-level) (setq ref-level level))
19306 (replace-match bul t t)
19307 (org-indent-line-to (+ start-ind (* delta bul-len)))
19308 ;; Ensure all text down to END (or SECTION-END) belongs
19309 ;; to the newly created item.
19310 (let ((section-end (save-excursion
19311 (or (outline-next-heading) (point)))))
19313 (funcall shift-text
19314 (+ start-ind (* (1+ delta) bul-len))
19315 (min end section-end)))))))
19316 ;; Case 3. Normal line with ARG: turn each non-item line into
19319 (while (< (point) end)
19320 (unless (or (org-at-heading-p) (org-at-item-p))
19321 (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
19323 (concat "\\1" (org-list-bullet-string "-") "\\2"))))
19325 ;; Case 4. Normal line without ARG: make the first line of
19326 ;; region an item, and shift indentation of others
19327 ;; lines to set them as item's body.
19328 (t (let* ((bul (org-list-bullet-string "-"))
19329 (bul-len (length bul))
19330 (ref-ind (org-get-indentation)))
19331 (skip-chars-forward " \t")
19334 (while (< (point) end)
19335 ;; Ensure that lines less indented than first one
19336 ;; still get included in item body.
19337 (funcall shift-text
19338 (+ ref-ind bul-len)
19339 (min end (save-excursion (or (outline-next-heading)
19341 (forward-line)))))))))
19343 (defun org-toggle-heading (&optional nstars)
19344 "Convert headings to normal text, or items or text to headings.
19345 If there is no active region, only the current line is considered.
19347 With a \\[universal-argument] prefix, convert the whole list at
19348 point into heading.
19352 - If the first non blank line is an headline, remove the stars
19353 from all headlines in the region.
19355 - If it is a normal line turn each and every normal line (i.e. not an
19356 heading or an item) in the region into a heading.
19358 - If it is a plain list item, turn all plain list items into headings.
19360 When converting a line into a heading, the number of stars is chosen
19361 such that the lines become children of the current entry. However,
19362 when a prefix argument is given, its value determines the number of
19367 ;; Return beginning of first non-blank line, starting from
19372 (while (org-at-comment-p) (forward-line))
19373 (skip-chars-forward " \r\t\n")
19376 ;; Determine boundaries of changes. If a universal prefix has
19377 ;; been given, put the list in a region. If region ends at a bol,
19378 ;; do not consider the last line to be in the region.
19380 (when (and current-prefix-arg (org-at-item-p))
19381 (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
19382 (org-mark-element))
19384 (if (org-region-active-p)
19385 (setq beg (funcall skip-blanks (region-beginning))
19386 end (copy-marker (save-excursion
19387 (goto-char (region-end))
19388 (if (bolp) (point) (point-at-eol)))))
19389 (setq beg (funcall skip-blanks (point-at-bol))
19390 end (copy-marker (point-at-eol))))
19391 ;; Ensure inline tasks don't count as headings.
19392 (org-with-limited-levels
19396 ;; Case 1. Started at an heading: de-star headings.
19397 ((org-at-heading-p)
19398 (while (< (point) end)
19399 (when (org-at-heading-p t)
19400 (looking-at org-outline-regexp) (replace-match "")
19403 ;; Case 2. Started at an item: change items into headlines.
19404 ;; One star will be added by `org-list-to-subtree'.
19406 (let* ((stars (make-string
19408 ;; subtract the star that will be added again by
19409 ;; `org-list-to-subtree'
19410 (1- (prefix-numeric-value current-prefix-arg))
19411 (or (org-current-level) 0))
19414 (cond (nstars "") ; stars from prefix only
19415 ((equal stars "") "") ; before first heading
19416 (org-odd-levels-only "*") ; inside heading, odd
19417 (t "")))) ; inside heading, oddeven
19418 (while (< (point) end)
19419 (when (org-at-item-p)
19420 ;; Pay attention to cases when region ends before list.
19421 (let* ((struct (org-list-struct))
19422 (list-end (min (org-list-get-bottom-point struct) (1+ end))))
19424 (narrow-to-region (point) list-end)
19426 (org-list-to-subtree
19427 (org-list-parse-list t)
19428 '(:istart (concat stars add-stars (funcall get-stars depth))
19429 :icount (concat stars add-stars (funcall get-stars depth)))))))
19432 ;; Case 3. Started at normal text: make every line an heading,
19433 ;; skipping headlines and items.
19434 (t (let* ((stars (make-string
19436 (prefix-numeric-value current-prefix-arg)
19437 (or (org-current-level) 0))
19440 (cond (nstars "") ; stars from prefix only
19441 ((equal stars "") "*") ; before first heading
19442 (org-odd-levels-only "**") ; inside heading, odd
19443 (t "*"))) ; inside heading, oddeven
19444 (rpl (concat stars add-stars " ")))
19445 (while (< (point) end)
19446 (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
19447 (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
19448 (replace-match (concat rpl (match-string 2))) (setq toggled t))
19449 (forward-line)))))))
19450 (unless toggled (message "Cannot toggle heading from here"))))
19452 (defun org-meta-return (&optional arg)
19453 "Insert a new heading or wrap a region in a table.
19454 Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
19455 See the individual commands for more information."
19458 ((run-hook-with-args-until-success 'org-metareturn-hook))
19459 ((or (org-at-drawer-p) (org-at-property-p))
19460 (newline-and-indent))
19462 (call-interactively 'org-table-wrap-region))
19463 (t (call-interactively 'org-insert-heading))))
19467 (defsubst org-in-subtree-not-table-p ()
19468 "Are we in a subtree and not in a table?"
19469 (and (not (org-before-first-heading-p))
19470 (not (org-at-table-p))))
19472 ;; Define the Org-mode menus
19473 (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
19475 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
19476 ["Next Field" org-cycle (org-at-table-p)]
19477 ["Previous Field" org-shifttab (org-at-table-p)]
19478 ["Next Row" org-return (org-at-table-p)]
19480 ["Blank Field" org-table-blank-field (org-at-table-p)]
19481 ["Edit Field" org-table-edit-field (org-at-table-p)]
19482 ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
19485 ["Move Column Left" org-metaleft (org-at-table-p)]
19486 ["Move Column Right" org-metaright (org-at-table-p)]
19487 ["Delete Column" org-shiftmetaleft (org-at-table-p)]
19488 ["Insert Column" org-shiftmetaright (org-at-table-p)])
19490 ["Move Row Up" org-metaup (org-at-table-p)]
19491 ["Move Row Down" org-metadown (org-at-table-p)]
19492 ["Delete Row" org-shiftmetaup (org-at-table-p)]
19493 ["Insert Row" org-shiftmetadown (org-at-table-p)]
19494 ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
19496 ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
19498 ["Copy Rectangle" org-copy-special (org-at-table-p)]
19499 ["Cut Rectangle" org-cut-special (org-at-table-p)]
19500 ["Paste Rectangle" org-paste-special (org-at-table-p)]
19501 ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
19504 ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
19505 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
19506 ["Edit Formulas" org-edit-special (org-at-table-p)]
19508 ["Recalculate line" org-table-recalculate (org-at-table-p)]
19509 ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
19510 ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
19512 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
19514 ["Sum Column/Rectangle" org-table-sum
19515 (or (org-at-table-p) (org-region-active-p))]
19516 ["Which Column?" org-table-current-column (org-at-table-p)])
19518 org-table-toggle-formula-debugger
19519 :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
19520 ["Show Col/Row Numbers"
19521 org-table-toggle-coordinate-overlays
19523 :selected (org-bound-and-true-p org-table-overlay-coordinates)]
19525 ["Create" org-table-create (and (not (org-at-table-p))
19526 org-enable-table-editor)]
19527 ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
19528 ["Import from File" org-table-import (not (org-at-table-p))]
19529 ["Export to File" org-table-export (org-at-table-p)]
19531 ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
19533 (easy-menu-define org-org-menu org-mode-map "Org menu"
19536 ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
19537 ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
19538 ["Sparse Tree..." org-sparse-tree t]
19539 ["Reveal Context" org-reveal t]
19540 ["Show All" show-all t]
19542 ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
19544 ["New Heading" org-insert-heading t]
19545 ("Navigate Headings"
19546 ["Up" outline-up-heading t]
19547 ["Next" outline-next-visible-heading t]
19548 ["Previous" outline-previous-visible-heading t]
19549 ["Next Same Level" outline-forward-same-level t]
19550 ["Previous Same Level" outline-backward-same-level t]
19552 ["Jump" org-goto t])
19554 ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
19556 ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
19557 ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
19559 ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
19560 ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
19561 ["Paste Subtree" org-paste-special (not (org-at-table-p))]
19563 ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
19565 ["Copy visible text" org-copy-visible t]
19567 ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
19568 ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
19569 ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
19570 ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
19572 ["Sort Region/Children" org-sort t]
19574 ["Convert to odd levels" org-convert-to-odd-levels t]
19575 ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
19577 ["Emphasis..." org-emphasize t]
19578 ["Edit Source Example" org-edit-special t]
19580 ["Footnote new/jump" org-footnote-action t]
19581 ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
19583 ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
19585 ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
19586 ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
19587 ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
19591 ["Store Link (Global)" org-store-link t]
19592 ["Find existing link to here" org-occur-link-in-agenda-files t]
19593 ["Insert Link" org-insert-link t]
19594 ["Follow Link" org-open-at-point t]
19596 ["Next link" org-next-link t]
19597 ["Previous link" org-previous-link t]
19599 ["Descriptive Links"
19600 org-toggle-link-display
19602 :selected org-descriptive-links
19605 org-toggle-link-display
19607 :selected (not org-descriptive-links)])
19610 ["TODO/DONE/-" org-todo t]
19612 ["Next keyword" org-shiftright (org-at-heading-p)]
19613 ["Previous keyword" org-shiftleft (org-at-heading-p)]
19614 ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
19615 ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
19616 ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
19617 ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
19618 ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
19620 ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
19621 :selected org-enforce-todo-dependencies :style toggle :active t]
19622 "Settings for tree at point"
19623 ["Do Children sequentially" org-toggle-ordered-property :style radio
19624 :selected (org-entry-get nil "ORDERED")
19625 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19626 ["Do Children parallel" org-toggle-ordered-property :style radio
19627 :selected (not (org-entry-get nil "ORDERED"))
19628 :active org-enforce-todo-dependencies :keys "C-c C-x o"]
19630 ["Set Priority" org-priority t]
19631 ["Priority Up" org-shiftup t]
19632 ["Priority Down" org-shiftdown t]
19634 ["Get news from all feeds" org-feed-update-all t]
19635 ["Go to the inbox of a feed..." org-feed-goto-inbox t]
19636 ["Customize feeds" (customize-variable 'org-feed-alist) t])
19637 ("TAGS and Properties"
19638 ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
19639 ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
19641 ["Set property" org-set-property (not (org-before-first-heading-p))]
19642 ["Column view of properties" org-columns t]
19643 ["Insert Column View DBlock" org-insert-columns-dblock t])
19644 ("Dates and Scheduling"
19645 ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
19646 ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
19648 ["1 Day Later" org-shiftright (org-at-timestamp-p)]
19649 ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
19650 ["1 ... Later" org-shiftup (org-at-timestamp-p)]
19651 ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
19652 ["Compute Time Range" org-evaluate-time-range t]
19653 ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
19654 ["Deadline" org-deadline (not (org-before-first-heading-p))]
19656 ["Custom time format" org-toggle-time-stamp-overlays
19657 :style radio :selected org-display-custom-times]
19659 ["Goto Calendar" org-goto-calendar t]
19660 ["Date from Calendar" org-date-from-calendar t]
19662 ["Start/Restart Timer" org-timer-start t]
19663 ["Pause/Continue Timer" org-timer-pause-or-continue t]
19664 ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
19665 ["Insert Timer String" org-timer t]
19666 ["Insert Timer Item" org-timer-item t])
19668 ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
19669 ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
19670 ["Clock out" org-clock-out t]
19671 ["Clock cancel" org-clock-cancel t]
19673 ["Mark as default task" org-clock-mark-default-task t]
19674 ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
19675 ["Goto running clock" org-clock-goto t]
19677 ["Display times" org-clock-display t]
19678 ["Create clock table" org-clock-report t]
19680 ["Record DONE time"
19681 (progn (setq org-log-done (not org-log-done))
19682 (message "Switching to %s will %s record a timestamp"
19683 (car org-done-keywords)
19684 (if org-log-done "automatically" "not")))
19685 :style toggle :selected org-log-done])
19687 ["Agenda Command..." org-agenda t]
19688 ["Set Restriction Lock" org-agenda-set-restriction-lock t]
19689 ("File List for Agenda")
19690 ("Special views current file"
19691 ["TODO Tree" org-show-todo-tree t]
19692 ["Check Deadlines" org-check-deadlines t]
19693 ["Timeline" org-timeline t]
19694 ["Tags/Property tree" org-match-sparse-tree t])
19696 ["Export/Publish..." org-export t]
19698 ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
19699 :selected org-cdlatex-mode]
19700 ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
19701 ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
19702 ["Modify math symbol" org-cdlatex-math-modify
19703 (org-inside-LaTeX-fragment-p)]
19704 ["Insert citation" org-reftex-citation t]
19706 ["Template for BEAMER" (progn (require 'org-beamer)
19707 (org-insert-beamer-options-template)) t])
19710 ["Push Files and Views" org-mobile-push t]
19711 ["Get Captured and Flagged" org-mobile-pull t]
19712 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
19714 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
19717 ["Show Version" org-version t]
19718 ["Info Documentation" org-info t])
19720 ["Browse Org Group" org-customize t]
19722 ["Expand This Menu" org-create-customize-menu
19723 (fboundp 'customize-menu-create)])
19724 ["Send bug report" org-submit-bug-report t]
19727 ["Refresh setup current buffer" org-mode-restart t]
19728 ["Reload Org (after update)" org-reload t]
19729 ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
19732 (defun org-info (&optional node)
19733 "Read documentation for Org-mode in the info system.
19734 With optional NODE, go directly to that node."
19736 (info (format "(org)%s" (or node ""))))
19739 (defun org-submit-bug-report ()
19740 "Submit a bug report on Org-mode via mail.
19742 Don't hesitate to report any problems or inaccurate documentation.
19744 If you don't have setup sending mail from (X)Emacs, please copy the
19745 output buffer into your mail program, as it gives us important
19746 information about your Org-mode version and configuration."
19748 (require 'reporter)
19749 (org-load-modules-maybe)
19750 (org-require-autoloaded-modules)
19751 (let ((reporter-prompt-for-summary-p "Bug report subject: "))
19752 (reporter-submit-bug-report
19753 "emacs-orgmode@gnu.org"
19754 (org-version nil 'full)
19756 (save-window-excursion
19757 (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
19758 (delete-other-windows)
19760 (insert "You are about to submit a bug report to the Org-mode mailing list.
19762 We would like to add your full Org-mode and Outline configuration to the
19763 bug report. This greatly simplifies the work of the maintainer and
19764 other experts on the mailing list.
19766 HOWEVER, some variables you have customized may contain private
19767 information. The names of customers, colleagues, or friends, might
19768 appear in the form of file names, tags, todo states, or search strings.
19769 If you answer yes to the prompt, you might want to check and remove
19770 such private information before sending the email.")
19771 (add-text-properties (point-min) (point-max) '(face org-warning))
19772 (when (yes-or-no-p "Include your Org-mode configuration ")
19776 (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
19777 (or (and (symbol-value v)
19778 (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
19780 (get v 'custom-type) (get v 'standard-value)
19781 (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
19783 (kill-buffer (get-buffer "*Warn about privacy*"))
19786 "Remember to cover the basics, that is, what you expected to happen and
19787 what in fact did happen. You don't know how to make a good report? See
19789 http://orgmode.org/manual/Feedback.html#Feedback
19791 Your bug report will be posted to the Org-mode mailing list.
19792 ------------------------------------------------------------------------")
19794 (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
19795 (replace-match "\\1Bug: \\3 [\\2]")))))
19798 (defun org-install-agenda-files-menu ()
19799 (let ((bl (buffer-list)))
19802 (set-buffer (pop bl))
19803 (if (derived-mode-p 'org-mode) (setq bl nil)))
19804 (when (derived-mode-p 'org-mode)
19806 '("Org") "File List for Agenda"
19809 ["Edit File List" (org-edit-agenda-file-list) t]
19810 ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
19811 ["Remove Current File from List" org-remove-file t]
19812 ["Cycle through agenda files" org-cycle-agenda-files t]
19813 ["Occur in all agenda files" org-occur-in-agenda-files t]
19815 (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
19819 (defun org-require-autoloaded-modules ()
19822 '(org-agenda org-archive org-ascii org-attach org-clock org-colview
19823 org-docbook org-exp org-html org-icalendar
19825 org-publish org-remember org-table
19826 org-timer org-xoxo)))
19829 (defun org-reload (&optional uncompiled)
19830 "Reload all org lisp files.
19831 With prefix arg UNCOMPILED, load the uncompiled versions."
19833 (require 'find-func)
19834 (let* ((file-re "^org\\(-.*\\)?\\.el")
19835 (dir-org (file-name-directory (org-find-library-dir "org")))
19836 (dir-org-contrib (ignore-errors
19837 (file-name-directory
19838 (org-find-library-dir "org-contribdir"))))
19840 (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
19841 (append (list nil "comint" "eval" "exp" "keys"
19842 "lob" "ref" "table" "tangle")
19846 (when (cdr lang) (symbol-name (car lang))))
19847 org-babel-load-languages)))))
19849 (append babel-files
19850 (and dir-org-contrib
19851 (directory-files dir-org-contrib t file-re))
19852 (directory-files dir-org t file-re)))
19853 (remove-re (concat (if (featurep 'xemacs)
19854 "org-colview" "org-colview-xemacs")
19856 (setq files (mapcar 'file-name-sans-extension files))
19857 (setq files (mapcar
19858 (lambda (x) (if (string-match remove-re x) nil x))
19860 (setq files (delq nil files))
19863 (when (featurep (intern (file-name-nondirectory f)))
19864 (if (and (not uncompiled)
19865 (file-exists-p (concat f ".elc")))
19866 (load (concat f ".elc") nil nil 'nosuffix)
19867 (load (concat f ".el") nil nil 'nosuffix))))
19869 (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
19870 (org-version nil 'full 'message))
19873 (defun org-customize ()
19874 "Call the customize function with org as argument."
19876 (org-load-modules-maybe)
19877 (org-require-autoloaded-modules)
19878 (customize-browse 'org))
19880 (defun org-create-customize-menu ()
19881 "Create a full customization menu for Org-mode, insert it into the menu."
19883 (org-load-modules-maybe)
19884 (org-require-autoloaded-modules)
19885 (if (fboundp 'customize-menu-create)
19888 '("Org") "Customize"
19889 `(["Browse Org group" org-customize t]
19891 ,(customize-menu-create 'org)
19892 ["Set" Custom-set t]
19893 ["Save" Custom-save t]
19894 ["Reset to Current" Custom-reset-current t]
19895 ["Reset to Saved" Custom-reset-saved t]
19896 ["Reset to Standard Settings" Custom-reset-standard t]))
19897 (message "\"Org\"-menu now contains full customization menu"))
19898 (error "Cannot expand menu (outdated version of cus-edit.el)")))
19900 ;;;; Miscellaneous stuff
19902 ;;; Generally useful functions
19904 (defun org-get-at-bol (property)
19905 "Get text property PROPERTY at beginning of line."
19906 (get-text-property (point-at-bol) property))
19908 (defun org-find-text-property-in-string (prop s)
19909 "Return the first non-nil value of property PROP in string S."
19910 (or (get-text-property 0 prop s)
19911 (get-text-property (or (next-single-property-change 0 prop s) 0)
19914 (defun org-display-warning (message) ;; Copied from Emacs-Muse
19915 "Display the given MESSAGE as a warning."
19916 (if (fboundp 'display-warning)
19917 (display-warning 'org message
19918 (if (featurep 'xemacs) 'warning :warning))
19919 (let ((buf (get-buffer-create "*Org warnings*")))
19920 (with-current-buffer buf
19921 (goto-char (point-max))
19922 (insert "Warning (Org): " message)
19925 (display-buffer buf)
19928 (defun org-eval (form)
19929 "Eval FORM and return result."
19930 (condition-case error
19932 (error (format "%%![Error: %s]" error))))
19934 (defun org-in-clocktable-p ()
19935 "Check if the cursor is in a clocktable."
19936 (let ((pos (point)) start)
19939 (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
19940 (setq start (match-beginning 0))
19941 (re-search-forward "^[ \t]*#\\+END:.*" nil t)
19942 (>= (match-end 0) pos)
19945 (defun org-in-commented-line ()
19946 "Is point in a line starting with `#'?"
19947 (equal (char-after (point-at-bol)) ?#))
19949 (defun org-in-indented-comment-line ()
19950 "Is point in a line starting with `#' after some white space?"
19953 (goto-char (point-at-bol))
19954 (looking-at "[ \t]*#"))))
19956 (defun org-in-verbatim-emphasis ()
19958 (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
19960 (defun org-goto-marker-or-bmk (marker &optional bookmark)
19961 "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
19962 (if (and marker (marker-buffer marker)
19963 (buffer-live-p (marker-buffer marker)))
19965 (org-pop-to-buffer-same-window (marker-buffer marker))
19966 (if (or (> marker (point-max)) (< marker (point-min)))
19969 (org-show-context 'org-goto))
19971 (bookmark-jump bookmark)
19972 (error "Cannot find location"))))
19974 (defun org-quote-csv-field (s)
19975 "Quote field for inclusion in CSV material."
19976 (if (string-match "[\",]" s)
19977 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
19980 (defun org-force-self-insert (N)
19981 "Needed to enforce self-insert under remapping."
19983 (self-insert-command N))
19985 (defun org-string-width (s)
19986 "Compute width of string, ignoring invisible characters.
19987 This ignores character with invisibility property `org-link', and also
19988 characters with property `org-cwidth', because these will become invisible
19989 upon the next fontification round."
19991 (when (or (eq t buffer-invisibility-spec)
19992 (assq 'org-link buffer-invisibility-spec))
19993 (while (setq b (text-property-any 0 (length s)
19994 'invisible 'org-link s))
19995 (setq s (concat (substring s 0 b)
19996 (substring s (or (next-single-property-change
19997 b 'invisible s) (length s)))))))
19998 (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
19999 (setq s (concat (substring s 0 b)
20000 (substring s (or (next-single-property-change
20001 b 'org-cwidth s) (length s))))))
20002 (setq l (string-width s) b -1)
20003 (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
20004 (setq l (- l (get-text-property b 'org-dwidth-n s))))
20007 (defun org-shorten-string (s maxlength)
20008 "Shorten string S so tht it is no longer than MAXLENGTH characters.
20009 If the string is shorter or has length MAXLENGTH, just return the
20010 original string. If it is longer, the functions finds a space in the
20011 string, breaks this string off at that locations and adds three dots
20012 as ellipsis. Including the ellipsis, the string will not be longer
20013 than MAXLENGTH. If finding a good breaking point in the string does
20014 not work, the string is just chopped off in the middle of a word
20016 (if (<= (length s) maxlength)
20018 (let* ((n (max (- maxlength 4) 1))
20019 (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
20020 (if (string-match re s)
20021 (concat (match-string 1 s) "...")
20022 (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
20024 (defun org-get-indentation (&optional line)
20025 "Get the indentation of the current line, interpreting tabs.
20026 When LINE is given, assume it represents a line and compute its indentation."
20028 (if (string-match "^ *" (org-remove-tabs line))
20031 (beginning-of-line 1)
20032 (skip-chars-forward " \t")
20033 (current-column))))
20035 (defun org-get-string-indentation (s)
20036 "What indentation has S due to SPACE and TAB at the beginning of the string?"
20037 (let ((n -1) (i 0) (w tab-width) c)
20039 (while (< (setq n (1+ n)) (length s))
20040 (setq c (aref s n))
20041 (cond ((= c ?\ ) (setq i (1+ i)))
20042 ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
20043 (t (throw 'exit t)))))
20046 (defun org-remove-tabs (s &optional width)
20047 "Replace tabulators in S with spaces.
20048 Assumes that s is a single line, starting in column 0."
20049 (setq width (or width tab-width))
20050 (while (string-match "\t" s)
20051 (setq s (replace-match
20053 (- (* width (/ (+ (match-beginning 0) width) width))
20054 (match-beginning 0)) ?\ )
20058 (defun org-fix-indentation (line ind)
20059 "Fix indentation in LINE.
20060 IND is a cons cell with target and minimum indentation.
20061 If the current indentation in LINE is smaller than the minimum,
20062 leave it alone. If it is larger than ind, set it to the target."
20063 (let* ((l (org-remove-tabs line))
20064 (i (org-get-indentation l))
20065 (i1 (car ind)) (i2 (cdr ind)))
20066 (if (>= i i2) (setq l (substring line i2)))
20068 (concat (make-string i1 ?\ ) l)
20071 (defun org-remove-indentation (code &optional n)
20072 "Remove the maximum common indentation from the lines in CODE.
20073 N may optionally be the number of spaces to remove."
20076 (org-do-remove-indentation n)
20079 (defun org-do-remove-indentation (&optional n)
20080 "Remove the maximum common indentation from the buffer."
20081 (untabify (point-min) (point-max))
20082 (let ((min 10000) re)
20085 (goto-char (point-min))
20086 (while (re-search-forward "^ *[^ \n]" nil t)
20087 (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
20088 (unless (or (= min 0) (= min 10000))
20089 (setq re (format "^ \\{%d\\}" min))
20090 (goto-char (point-min))
20091 (while (re-search-forward re nil t)
20096 (defun org-fill-template (template alist)
20097 "Find each %key of ALIST in TEMPLATE and replace it."
20098 (let ((case-fold-search nil)
20100 (setq alist (sort (copy-sequence alist)
20101 (lambda (a b) (< (length (car a)) (length (car b))))))
20102 (while (setq entry (pop alist))
20104 (replace-regexp-in-string
20105 (concat "%" (regexp-quote (car entry)))
20106 (or (cdr entry) "") template t t)))
20109 (defun org-base-buffer (buffer)
20110 "Return the base buffer of BUFFER, if it has one. Else return the buffer."
20113 (or (buffer-base-buffer buffer)
20116 (defun org-trim (s)
20117 "Remove whitespace at beginning and end of string."
20118 (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
20119 (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
20122 (defun org-wrap (string &optional width lines)
20123 "Wrap string to either a number of lines, or a width in characters.
20124 If WIDTH is non-nil, the string is wrapped to that width, however many lines
20125 that costs. If there is a word longer than WIDTH, the text is actually
20126 wrapped to the length of that word.
20127 IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
20128 many lines, whatever width that takes.
20129 The return value is a list of lines, without newlines at the end."
20130 (let* ((words (org-split-string string "[ \t\n]+"))
20131 (maxword (apply 'max (mapcar 'org-string-width words)))
20134 (org-do-wrap words (max maxword width)))
20137 (setq ll (org-do-wrap words maxword))
20138 (if (<= (length ll) lines)
20141 (while (> (length ll) lines)
20143 (setq ll (org-do-wrap words w)))
20145 (t (error "Cannot wrap this")))))
20147 (defun org-do-wrap (words width)
20148 "Create lines of maximum width WIDTH (in characters) from word list WORDS."
20151 (setq line (pop words))
20152 (while (and words (< (+ (length line) (length (car words))) width))
20153 (setq line (concat line " " (pop words))))
20154 (setq lines (push line lines)))
20157 (defun org-split-string (string &optional separators)
20158 "Splits STRING into substrings at SEPARATORS.
20159 No empty strings are returned if there are matches at the beginning
20160 and end of string."
20161 (let ((rexp (or separators "[ \f\t\n\r\v]+"))
20165 (while (and (string-match rexp string
20167 (= start (match-beginning 0))
20168 (< start (length string)))
20170 (< (match-beginning 0) (length string)))
20172 (or (eq (match-beginning 0) 0)
20173 (and (eq (match-beginning 0) (match-end 0))
20174 (eq (match-beginning 0) start))
20176 (cons (substring string start (match-beginning 0))
20178 (setq start (match-end 0)))
20179 (or (eq start (length string))
20181 (cons (substring string start)
20185 (defun org-quote-vert (s)
20186 "Replace \"|\" with \"\\vert\"."
20187 (while (string-match "|" s)
20188 (setq s (replace-match "\\vert" t t s)))
20191 (defun org-uuidgen-p (s)
20192 "Is S an ID created by UUIDGEN?"
20193 (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
20195 (defun org-in-src-block-p nil
20196 "Whether point is in a code source block."
20198 (when (setq ov (overlays-at (point)))
20199 (memq 'org-block-background
20200 (overlay-properties
20203 (defun org-context ()
20204 "Return a list of contexts of the current cursor position.
20205 If several contexts apply, all are returned.
20206 Each context entry is a list with a symbol naming the context, and
20207 two positions indicating start and end of the context. Possible
20210 :headline anywhere in a headline
20211 :headline-stars on the leading stars in a headline
20212 :todo-keyword on a TODO keyword (including DONE) in a headline
20213 :tags on the TAGS in a headline
20214 :priority on the priority cookie in a headline
20215 :item on the first line of a plain list item
20216 :item-bullet on the bullet/number of a plain list item
20217 :checkbox on the checkbox in a plain list item
20218 :table in an org-mode table
20219 :table-special on a special filed in a table
20220 :table-table in a table.el table
20221 :clocktable in a clocktable
20222 :src-block in a source block
20223 :link on a hyperlink
20224 :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
20225 :target on a <<target>>
20226 :radio-target on a <<<radio-target>>>
20227 :latex-fragment on a LaTeX fragment
20228 :latex-preview on a LaTeX fragment with overlaid preview image
20230 This function expects the position to be visible because it uses font-lock
20231 faces as a help to recognize the following contexts: :table-special, :link,
20233 (let* ((f (get-text-property (point) 'face))
20234 (faces (if (listp f) f (list f)))
20235 (case-fold-search t)
20236 (p (point)) clist o)
20237 ;; First the large context
20239 ((org-at-heading-p t)
20240 (push (list :headline (point-at-bol) (point-at-eol)) clist)
20242 (beginning-of-line 1)
20243 (looking-at org-todo-line-tags-regexp))
20244 (push (org-point-in-group p 1 :headline-stars) clist)
20245 (push (org-point-in-group p 2 :todo-keyword) clist)
20246 (push (org-point-in-group p 4 :tags) clist))
20248 (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
20249 (if (looking-at "\\[#[A-Z0-9]\\]")
20250 (push (org-point-in-group p 0 :priority) clist)))
20253 (push (org-point-in-group p 2 :item-bullet) clist)
20254 (push (list :item (point-at-bol)
20255 (save-excursion (org-end-of-item) (point)))
20257 (and (org-at-item-checkbox-p)
20258 (push (org-point-in-group p 0 :checkbox) clist)))
20261 (push (list :table (org-table-begin) (org-table-end)) clist)
20262 (if (memq 'org-formula faces)
20263 (push (list :table-special
20264 (previous-single-property-change p 'face)
20265 (next-single-property-change p 'face)) clist)))
20266 ((org-at-table-p 'any)
20267 (push (list :table-table) clist)))
20270 (let ((case-fold-search t))
20271 ;; New the "medium" contexts: clocktables, source blocks
20272 (cond ((org-in-clocktable-p)
20273 (push (list :clocktable
20274 (and (or (looking-at "#\\+BEGIN: clocktable")
20275 (search-backward "#+BEGIN: clocktable" nil t))
20276 (match-beginning 0))
20277 (and (re-search-forward "#\\+END:?" nil t)
20278 (match-end 0))) clist))
20279 ((org-in-src-block-p)
20280 (push (list :src-block
20281 (and (or (looking-at "#\\+BEGIN_SRC")
20282 (search-backward "#+BEGIN_SRC" nil t))
20283 (match-beginning 0))
20284 (and (search-forward "#+END_SRC" nil t)
20285 (match-beginning 0))) clist))))
20288 ;; Now the small context
20290 ((org-at-timestamp-p)
20291 (push (org-point-in-group p 0 :timestamp) clist))
20292 ((memq 'org-link faces)
20294 (previous-single-property-change p 'face)
20295 (next-single-property-change p 'face)) clist))
20296 ((memq 'org-special-keyword faces)
20297 (push (list :keyword
20298 (previous-single-property-change p 'face)
20299 (next-single-property-change p 'face)) clist))
20301 (push (org-point-in-group p 0 :target) clist)
20302 (goto-char (1- (match-beginning 0)))
20303 (if (looking-at org-radio-target-regexp)
20304 (push (org-point-in-group p 0 :radio-target) clist))
20306 ((setq o (car (delq nil
20309 (if (memq x org-latex-fragment-image-overlays) x))
20310 (overlays-at (point))))))
20311 (push (list :latex-fragment
20312 (overlay-start o) (overlay-end o)) clist)
20313 (push (list :latex-preview
20314 (overlay-start o) (overlay-end o)) clist))
20315 ((org-inside-LaTeX-fragment-p)
20316 ;; FIXME: positions wrong.
20317 (push (list :latex-fragment (point) (point)) clist)))
20319 (setq clist (nreverse (delq nil clist)))
20322 ;; FIXME: Compare with at-regexp-p Do we need both?
20323 (defun org-in-regexp (re &optional nlines visually)
20324 "Check if point is inside a match of regexp.
20325 Normally only the current line is checked, but you can include NLINES extra
20326 lines both before and after point into the search.
20327 If VISUALLY is set, require that the cursor is not after the match but
20328 really on, so that the block visually is on the match."
20330 (let ((pos (point))
20331 (eol (point-at-eol (+ 1 (or nlines 0))))
20332 (inc (if visually 1 0)))
20334 (beginning-of-line (- 1 (or nlines 0)))
20335 (while (re-search-forward re eol t)
20336 (if (and (<= (match-beginning 0) pos)
20337 (>= (+ inc (match-end 0)) pos))
20338 (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
20340 (defun org-at-regexp-p (regexp)
20341 "Is point inside a match of REGEXP in the current line?"
20344 (let ((pos (point)) (end (point-at-eol)))
20345 (beginning-of-line 1)
20346 (while (re-search-forward regexp end t)
20347 (if (and (<= (match-beginning 0) pos)
20348 (>= (match-end 0) pos))
20352 (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
20353 "Non-nil when point is between matches of START-RE and END-RE.
20355 Also return a non-nil value when point is on one of the matches.
20357 Optional arguments LIM-UP and LIM-DOWN bound the search; they are
20358 buffer positions. Default values are the positions of headlines
20359 surrounding the point.
20361 The functions returns a cons cell whose car (resp. cdr) is the
20362 position before START-RE (resp. after END-RE)."
20364 (let ((pos (point))
20365 (limit-up (or lim-up (save-excursion (outline-previous-heading))))
20366 (limit-down (or lim-down (save-excursion (outline-next-heading))))
20369 ;; Point is on a block when on START-RE or if START-RE can be
20370 ;; found before it...
20371 (and (or (org-at-regexp-p start-re)
20372 (re-search-backward start-re limit-up t))
20373 (setq beg (match-beginning 0))
20374 ;; ... and END-RE after it...
20375 (goto-char (match-end 0))
20376 (re-search-forward end-re limit-down t)
20377 (> (setq end (match-end 0)) pos)
20378 ;; ... without another START-RE in-between.
20379 (goto-char (match-beginning 0))
20380 (not (re-search-backward start-re (1+ beg) t))
20382 (cons beg end))))))
20384 (defun org-in-block-p (names)
20385 "Non-nil when point belongs to a block whose name belongs to NAMES.
20387 NAMES is a list of strings containing names of blocks.
20389 Return first block name matched, or nil. Beware that in case of
20390 nested blocks, the returned name may not belong to the closest
20394 (let ((case-fold-search t)
20395 (lim-up (save-excursion (outline-previous-heading)))
20396 (lim-down (save-excursion (outline-next-heading))))
20397 (mapc (lambda (name)
20398 (let ((n (regexp-quote name)))
20399 (when (org-between-regexps-p
20400 (concat "^[ \t]*#\\+begin_" n)
20401 (concat "^[ \t]*#\\+end_" n)
20407 (defun org-occur-in-agenda-files (regexp &optional nlines)
20408 "Call `multi-occur' with buffers for all agenda files."
20409 (interactive "sOrg-files matching: \np")
20410 (let* ((files (org-agenda-files))
20411 (tnames (mapcar 'file-truename files))
20412 (extra org-agenda-text-search-extra-files)
20414 (when (eq (car extra) 'agenda-archives)
20415 (setq extra (cdr extra))
20416 (setq files (org-add-archive-files files)))
20417 (while (setq f (pop extra))
20418 (unless (member (file-truename f) tnames)
20419 (add-to-list 'files f 'append)
20420 (add-to-list 'tnames (file-truename f) 'append)))
20422 (mapcar (lambda (x)
20423 (with-current-buffer
20424 (or (get-file-buffer x) (find-file-noselect x))
20430 (if (boundp 'occur-mode-find-occurrence-hook)
20432 (add-hook 'occur-mode-find-occurrence-hook
20434 (when (derived-mode-p 'org-mode)
20437 (defadvice occur-mode-goto-occurrence
20438 (after org-occur-reveal activate)
20439 (and (derived-mode-p 'org-mode) (org-reveal)))
20440 (defadvice occur-mode-goto-occurrence-other-window
20441 (after org-occur-reveal activate)
20442 (and (derived-mode-p 'org-mode) (org-reveal)))
20443 (defadvice occur-mode-display-occurrence
20444 (after org-occur-reveal activate)
20445 (when (derived-mode-p 'org-mode)
20446 (let ((pos (occur-mode-find-occurrence)))
20447 (with-current-buffer (marker-buffer pos)
20452 (defun org-occur-link-in-agenda-files ()
20453 "Create a link and search for it in the agendas.
20454 The link is not stored in `org-stored-links', it is just created
20455 for the search purpose."
20457 (let ((link (condition-case nil
20458 (org-store-link nil)
20459 (error "Unable to create a link to here"))))
20460 (org-occur-in-agenda-files (regexp-quote link))))
20462 (defun org-uniquify (list)
20463 "Remove duplicate elements from LIST."
20465 (mapc (lambda (x) (add-to-list 'res x 'append)) list)
20468 (defun org-delete-all (elts list)
20469 "Remove all elements in ELTS from LIST."
20471 (setq list (delete (pop elts) list)))
20474 (defun org-count (cl-item cl-seq)
20475 "Count the number of occurrences of ITEM in SEQ.
20476 Taken from `count' in cl-seq.el with all keyword arguments removed."
20477 (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
20478 (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
20479 (while (< cl-start cl-end)
20480 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
20481 (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
20482 (setq cl-start (1+ cl-start)))
20485 (defun org-remove-if (predicate seq)
20486 "Remove everything from SEQ that fulfills PREDICATE."
20490 (if (not (funcall predicate e)) (push e res)))
20493 (defun org-remove-if-not (predicate seq)
20494 "Remove everything from SEQ that does not fulfill PREDICATE."
20498 (if (funcall predicate e) (push e res)))
20501 (defun org-reduce (cl-func cl-seq &rest cl-keys)
20502 "Reduce two-argument FUNCTION across SEQ.
20503 Taken from `reduce' in cl-seq.el with all keyword arguments but
20504 \":initial-value\" removed."
20505 (let ((cl-accum (cond ((memq :initial-value cl-keys)
20506 (cadr (memq :initial-value cl-keys)))
20507 (cl-seq (pop cl-seq))
20508 (t (funcall cl-func)))))
20510 (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
20513 (defun org-back-over-empty-lines ()
20514 "Move backwards over whitespace, to the beginning of the first empty line.
20515 Returns the number of empty lines passed."
20516 (let ((pos (point)))
20517 (if (cdr (assoc 'heading org-blank-before-new-entry))
20518 (skip-chars-backward " \t\n\r")
20520 (forward-line -1)))
20521 (beginning-of-line 2)
20522 (goto-char (min (point) pos))
20523 (count-lines (point) pos)))
20525 (defun org-skip-whitespace ()
20526 (skip-chars-forward " \t\n\r"))
20528 (defun org-point-in-group (point group &optional context)
20529 "Check if POINT is in match-group GROUP.
20530 If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
20531 match. If the match group does not exist or point is not inside it,
20533 (and (match-beginning group)
20534 (>= point (match-beginning group))
20535 (<= point (match-end group))
20537 (list context (match-beginning group) (match-end group))
20540 (defun org-switch-to-buffer-other-window (&rest args)
20541 "Switch to buffer in a second window on the current frame.
20542 In particular, do not allow pop-up frames.
20543 Returns the newly created buffer."
20544 (let (pop-up-frames special-display-buffer-names special-display-regexps
20545 special-display-function)
20546 (apply 'switch-to-buffer-other-window args)))
20548 (defun org-combine-plists (&rest plists)
20549 "Create a single property list from all plists in PLISTS.
20550 The process starts by copying the first list, and then setting properties
20551 from the other lists. Settings in the last list are the most significant
20552 ones and overrule settings in the other lists."
20553 (let ((rtn (copy-sequence (pop plists)))
20556 (setq ls (pop plists))
20558 (setq p (pop ls) v (pop ls))
20559 (setq rtn (plist-put rtn p v))))
20562 (defun org-replace-escapes (string table)
20563 "Replace %-escapes in STRING with values in TABLE.
20564 TABLE is an association list with keys like \"%a\" and string values.
20565 The sequences in STRING may contain normal field width and padding information,
20566 for example \"%-5s\". Replacements happen in the sequence given by TABLE,
20567 so values can contain further %-escapes if they are define later in TABLE."
20568 (let ((tbl (copy-alist table))
20569 (case-fold-search nil)
20572 (while (setq e (pop tbl))
20573 (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
20574 (when (and (cdr e) (string-match re (cdr e)))
20575 (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
20577 (add-text-properties 0 3 (list 'sref sref) safe)
20578 (setcdr e (replace-match safe t t (cdr e)))))
20579 (while (string-match re string)
20580 (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
20582 (setq string (replace-match rpl t t string))))
20583 (while (setq pchg (next-property-change pchg string))
20584 (let ((sref (get-text-property pchg 'sref string)))
20585 (when (and sref (string-match "SREF" string pchg))
20586 (setq string (replace-match sref t t string)))))
20589 (defun org-sublist (list start end)
20590 "Return a section of LIST, from START to END.
20591 Counting starts at 1."
20592 (let (rtn (c start))
20593 (setq list (nthcdr (1- start) list))
20594 (while (and list (<= c end))
20595 (push (pop list) rtn)
20599 (defun org-find-base-buffer-visiting (file)
20600 "Like `find-buffer-visiting' but always return the base buffer and
20601 not an indirect buffer."
20602 (let ((buf (or (get-file-buffer file)
20603 (find-buffer-visiting file))))
20605 (or (buffer-base-buffer buf) buf)
20608 (defun org-image-file-name-regexp (&optional extensions)
20609 "Return regexp matching the file names of images.
20610 If EXTENSIONS is given, only match these."
20611 (if (and (not extensions) (fboundp 'image-file-name-regexp))
20612 (image-file-name-regexp)
20613 (let ((image-file-name-extensions
20615 '("png" "jpeg" "jpg" "gif" "tiff" "tif"
20616 "xbm" "xpm" "pbm" "pgm" "ppm"))))
20618 (regexp-opt (nconc (mapcar 'upcase
20619 image-file-name-extensions)
20620 image-file-name-extensions)
20624 (defun org-file-image-p (file &optional extensions)
20625 "Return non-nil if FILE is an image."
20627 (string-match (org-image-file-name-regexp extensions) file)))
20629 (defun org-get-cursor-date ()
20630 "Return the date at cursor in as a time.
20631 This works in the calendar and in the agenda, anywhere else it just
20632 returns the current time."
20633 (let (date day defd)
20635 ((eq major-mode 'calendar-mode)
20636 (setq date (calendar-cursor-to-date)
20637 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
20638 ((eq major-mode 'org-agenda-mode)
20639 (setq day (get-text-property (point) 'day))
20641 (setq date (calendar-gregorian-from-absolute day)
20642 defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
20644 (or defd (current-time))))
20646 (defun org-mark-subtree (&optional up)
20647 "Mark the current subtree.
20648 This puts point at the start of the current subtree, and mark at
20649 the end. If a numeric prefix UP is given, move up into the
20650 hierarchy of headlines by UP levels before marking the subtree."
20652 (org-with-limited-levels
20653 (cond ((org-at-heading-p) (beginning-of-line))
20654 ((org-before-first-heading-p) (error "Not in a subtree"))
20655 (t (outline-previous-visible-heading 1))))
20656 (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
20657 (if (org-called-interactively-p 'any)
20658 (call-interactively 'org-mark-element)
20659 (org-mark-element)))
20663 (defun org-indent-line ()
20664 "Indent line depending on context."
20666 (let* ((pos (point))
20667 (itemp (org-at-item-p))
20668 (case-fold-search t)
20669 (org-drawer-regexp (or org-drawer-regexp "\000"))
20670 (inline-task-p (and (featurep 'org-inlinetask)
20671 (org-inlinetask-in-task-p)))
20672 (inline-re (and inline-task-p
20673 (org-inlinetask-outline-regexp)))
20675 (if (and orgstruct-is-++ (eq pos (point)))
20676 (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
20677 (indent-according-to-mode))
20678 (beginning-of-line 1)
20681 ((looking-at org-outline-regexp) (setq column 0))
20683 ((looking-at "#\\+include:") (setq column 0))
20684 ;; Footnote definition
20685 ((looking-at org-footnote-definition-re) (setq column 0))
20686 ;; Literal examples
20687 ((looking-at "[ \t]*:\\( \\|$\\)")
20688 (setq column (org-get-indentation))) ; do nothing
20690 ((ignore-errors (goto-char (org-in-item-p)))
20691 (setq column (if itemp
20692 (org-get-indentation)
20693 (org-list-item-body-column (point))))
20696 ((and (looking-at "[ \t]*:END:")
20697 (save-excursion (re-search-backward org-drawer-regexp nil t)))
20699 (goto-char (1- (match-beginning 1)))
20700 (setq column (current-column))))
20702 ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
20704 (re-search-backward
20705 (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
20706 (setq column (org-get-indentation (match-string 0))))
20707 ((and (not (looking-at "[ \t]*#\\+begin_"))
20708 (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
20710 (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
20712 (cond ((equal (downcase (match-string 1)) "src")
20713 ;; src blocks: let `org-edit-src-exit' handle them
20714 (org-get-indentation))
20715 ((equal (downcase (match-string 1)) "example")
20716 (max (org-get-indentation)
20717 (org-get-indentation (match-string 0))))
20719 (org-get-indentation (match-string 0))))))
20720 ;; This line has nothing special, look at the previous relevant
20721 ;; line to compute indentation
20723 (beginning-of-line 0)
20724 (while (and (not (bobp))
20725 (not (looking-at org-drawer-regexp))
20726 ;; When point started in an inline task, do not move
20727 ;; above task starting line.
20728 (not (and inline-task-p (looking-at inline-re)))
20729 ;; Skip drawers, blocks, empty lines, verbatim,
20730 ;; comments, tables, footnotes definitions, lists,
20732 (or (and (looking-at "[ \t]*:END:")
20733 (re-search-backward org-drawer-regexp nil t))
20734 (and (looking-at "[ \t]*#\\+end_")
20735 (re-search-backward "[ \t]*#\\+begin_"nil t))
20736 (looking-at "[ \t]*[\n:#|]")
20737 (looking-at org-footnote-definition-re)
20738 (and (ignore-errors (goto-char (org-in-item-p)))
20740 (org-list-get-top-point (org-list-struct))))
20741 (and (not inline-task-p)
20742 (featurep 'org-inlinetask)
20743 (org-inlinetask-in-task-p)
20744 (or (org-inlinetask-goto-beginning) t))))
20745 (beginning-of-line 0))
20747 ;; There was an heading above.
20748 ((looking-at "\\*+[ \t]+")
20749 (if (not org-adapt-indentation)
20751 (goto-char (match-end 0))
20752 (setq column (current-column))))
20753 ;; A drawer had started and is unfinished
20754 ((looking-at org-drawer-regexp)
20755 (goto-char (1- (match-beginning 1)))
20756 (setq column (current-column)))
20757 ;; Else, nothing noticeable found: get indentation and go on.
20758 (t (setq column (org-get-indentation))))))
20759 ;; Now apply indentation and move cursor accordingly
20761 (if (<= (current-column) (current-indentation))
20762 (org-indent-line-to column)
20763 (save-excursion (org-indent-line-to column)))
20764 ;; Special polishing for properties, see `org-property-format'
20765 (setq column (current-column))
20766 (beginning-of-line 1)
20768 "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
20769 (replace-match (concat (match-string 1)
20770 (format org-property-format
20771 (match-string 2) (match-string 3)))
20773 (org-move-to-column column))))
20775 (defun org-indent-drawer ()
20776 "Indent the drawer at point."
20779 (e (and (save-excursion (re-search-forward ":END:" nil t))
20784 (when (overlays-at (point))
20785 (member 'invisible (overlay-properties
20786 (car (overlays-at (point)))))))))
20787 (when folded (org-cycle))
20788 (indent-for-tab-command)
20789 (while (and (move-beginning-of-line 2) (< (point) e))
20790 (indent-for-tab-command))
20792 (when folded (org-cycle)))
20793 (message "Drawer at point indented"))
20795 (defun org-indent-block ()
20796 "Indent the block at point."
20799 (case-fold-search t)
20800 (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
20805 (when (overlays-at (point))
20806 (member 'invisible (overlay-properties
20807 (car (overlays-at (point)))))))))
20808 (when folded (org-cycle))
20809 (indent-for-tab-command)
20810 (while (and (move-beginning-of-line 2) (< (point) e))
20811 (indent-for-tab-command))
20813 (when folded (org-cycle)))
20814 (message "Block at point indented"))
20816 (defun org-indent-region (start end)
20820 (let ((line-end (org-current-line end)))
20822 (while (< (org-current-line) line-end)
20823 (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
20824 (t (call-interactively 'org-indent-line)))
20825 (move-beginning-of-line 2)))))
20830 ;; We use our own fill-paragraph and auto-fill functions.
20832 ;; `org-fill-paragraph' relies on adaptive filling and context
20833 ;; checking. Appropriate `fill-prefix' is computed with
20834 ;; `org-adaptive-fill-function'.
20836 ;; `org-auto-fill-function' takes care of auto-filling. It calls
20837 ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
20838 ;; `org-adaptive-fill-function' value. Internally,
20839 ;; `org-comment-line-break-function' breaks the line.
20841 ;; `org-setup-filling' installs filling and auto-filling related
20842 ;; variables during `org-mode' initialization.
20844 (defun org-setup-filling ()
20846 ;; Prevent auto-fill from inserting unwanted new items.
20847 (when (boundp 'fill-nobreak-predicate)
20849 'fill-nobreak-predicate
20851 (append fill-nobreak-predicate
20852 '(org-fill-paragraph-separate-nobreak-p
20853 org-fill-line-break-nobreak-p)))))
20854 (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
20855 (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
20856 (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
20857 (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
20859 (defvar org-element-paragraph-separate) ; org-element.el
20860 (defun org-fill-paragraph-separate-nobreak-p ()
20861 "Non-nil when a line break at point would insert a new item."
20862 (looking-at (substring org-element-paragraph-separate 1)))
20864 (defun org-fill-line-break-nobreak-p ()
20865 "Non-nil when a line break at point would create an Org line break."
20867 (skip-chars-backward "[ \t]")
20868 (skip-chars-backward "\\\\")
20869 (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
20871 (declare-function message-in-body-p "message" ())
20872 (defvar org-element--affiliated-re) ; From org-element.el
20873 (defun org-adaptive-fill-function ()
20874 "Compute a fill prefix for the current line.
20875 Return fill prefix, as a string, or nil if current line isn't
20876 meant to be filled."
20877 (org-with-wide-buffer
20878 (unless (and (derived-mode-p 'message-mode) (not (message-in-body-p)))
20879 ;; FIXME: This is really the job of orgstruct++-mode
20880 (let* ((p (line-beginning-position))
20881 (element (save-excursion (beginning-of-line)
20882 (org-element-at-point)))
20883 (type (org-element-type element))
20886 (goto-char (org-element-property :begin element))
20887 (while (looking-at org-element--affiliated-re) (forward-line))
20889 (unless (< p post-affiliated)
20891 (comment (looking-at "[ \t]*# ?") (match-string 0))
20892 (footnote-definition "")
20894 (make-string (org-list-item-body-column post-affiliated) ? ))
20896 ;; Fill prefix is usually the same as the current line,
20897 ;; except if the paragraph is at the beginning of an item.
20898 (let ((parent (org-element-property :parent element)))
20899 (cond ((eq (org-element-type parent) 'item)
20900 (make-string (org-list-item-body-column
20901 (org-element-property :begin parent))
20903 ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
20907 ;; Only fill contents if P is within block boundaries.
20908 (let* ((cbeg (save-excursion (goto-char post-affiliated)
20911 (cend (save-excursion
20912 (goto-char (org-element-property :end element))
20913 (skip-chars-backward " \r\t\n")
20914 (line-beginning-position))))
20915 (when (and (>= p cbeg) (< p cend))
20916 (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
20920 (declare-function message-goto-body "message" ())
20921 (defvar message-cite-prefix-regexp) ; From message.el
20922 (defvar org-element-all-objects) ; From org-element.el
20923 (defun org-fill-paragraph (&optional justify)
20924 "Fill element at point, when applicable.
20926 This function only applies to comment blocks, comments, example
20927 blocks and paragraphs. Also, as a special case, re-align table
20928 when point is at one.
20930 If JUSTIFY is non-nil (interactively, with prefix argument),
20931 justify as well. If `sentence-end-double-space' is non-nil, then
20932 period followed by one space does not end a sentence, so don't
20933 break a line there. The variable `fill-column' controls the
20936 For convenience, when point is at a plain list, an item or
20937 a footnote definition, try to fill the first paragraph within."
20938 ;; Falls back on message-fill-paragraph when necessary
20940 (if (and (derived-mode-p 'message-mode)
20941 (or (not (message-in-body-p))
20942 (save-excursion (move-beginning-of-line 1)
20943 (looking-at message-cite-prefix-regexp))))
20944 (let ((fill-paragraph-function
20945 (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
20946 (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
20947 (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
20948 (paragraph-separate
20949 (cadadr (assoc 'paragraph-separate org-fb-vars))))
20950 (fill-paragraph nil))
20952 ;; Move to end of line in order to get the first paragraph
20953 ;; within a plain list or a footnote definition.
20955 (let ((element (org-element-at-point)))
20956 ;; First check if point is in a blank line at the beginning of
20957 ;; the buffer. In that case, ignore filling.
20958 (if (< (point) (org-element-property :begin element)) t
20959 (case (org-element-type element)
20960 ;; Align Org tables, leave table.el tables as-is.
20961 (table-row (org-table-align) t)
20963 (when (eq (org-element-property :type element) 'org)
20967 ;; Paragraphs may contain `line-break' type objects.
20968 (let ((beg (max (point-min)
20969 (org-element-property :contents-begin element)))
20970 (end (min (point-max)
20971 (org-element-property :contents-end element))))
20972 ;; Do nothing if point is at an affiliated keyword.
20973 (if (< (point) beg) t
20974 (when (derived-mode-p 'message-mode)
20975 ;; In `message-mode', do not fill following
20976 ;; citation in current paragraph nor text before
20978 (let ((body-start (save-excursion (message-goto-body))))
20979 (when body-start (setq beg (max body-start beg))))
20980 (when (save-excursion
20982 (concat "^" message-cite-prefix-regexp) end t))
20983 (setq end (match-beginning 0))))
20984 ;; Fill paragraph, taking line breaks into
20985 ;; consideration. For that, slice the paragraph
20986 ;; using line breaks as separators, and fill the
20987 ;; parts in reverse order to avoid messing with
20993 (fill-region-as-paragraph pos (point) justify)
20995 ;; Find the list of ending positions for line
20996 ;; breaks in the current paragraph. Add paragraph
20997 ;; beginning to include first slice.
21002 (org-element--parse-objects
21003 beg end nil org-element-all-objects)
21005 (lambda (lb) (org-element-property :end lb)))))))
21007 ;; Contents of `comment-block' type elements should be
21008 ;; filled as plain text, but only if point is within block
21011 (let* ((case-fold-search t)
21012 (beg (save-excursion
21013 (goto-char (org-element-property :begin element))
21014 (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
21017 (end (save-excursion
21018 (goto-char (org-element-property :end element))
21019 (re-search-backward "^[ \t]*#\\+end_comment" nil t)
21020 (line-beginning-position))))
21021 (when (and (>= (point) beg) (< (point) end))
21022 (fill-region-as-paragraph
21025 (re-search-backward "^[ \t]*$" beg 'move)
21026 (line-beginning-position))
21028 (beginning-of-line)
21029 (re-search-forward "^[ \t]*$" end 'move)
21030 (line-beginning-position))
21034 (comment (fill-comment-paragraph justify))
21035 ;; Ignore every other element.
21036 (otherwise t)))))))
21038 (defun org-auto-fill-function ()
21039 "Auto-fill function."
21040 ;; Check if auto-filling is meaningful.
21041 (let ((fc (current-fill-column)))
21042 (when (and fc (> (current-column) fc))
21043 (let ((fill-prefix (org-adaptive-fill-function)))
21044 (when fill-prefix (do-auto-fill))))))
21046 (defun org-comment-line-break-function (&optional soft)
21047 "Break line at point and indent, continuing comment if within one.
21048 The inserted newline is marked hard if variable
21049 `use-hard-newlines' is true, unless optional argument SOFT is
21051 (if soft (insert-and-inherit ?\n) (newline 1))
21052 (save-excursion (forward-char -1) (delete-horizontal-space))
21053 (delete-horizontal-space)
21054 (indent-to-left-margin)
21055 (insert-before-markers-and-inherit fill-prefix))
21060 ;; Org comments syntax is quite complex. It requires the entire line
21061 ;; to be just a comment. Also, even with the right syntax at the
21062 ;; beginning of line, some some elements (i.e. verse-block or
21063 ;; example-block) don't accept comments. Usual Emacs comment commands
21064 ;; cannot cope with those requirements. Therefore, Org replaces them.
21066 ;; Org still relies on `comment-dwim', but cannot trust
21067 ;; `comment-only-p'. So, `comment-region-function' and
21068 ;; `uncomment-region-function' both point
21069 ;; to`org-comment-or-uncomment-region'. Eventually,
21070 ;; `org-insert-comment' takes care of insertion of comments at the
21071 ;; beginning of line.
21073 ;; `org-setup-comments-handling' install comments related variables
21074 ;; during `org-mode' initialization.
21076 (defun org-setup-comments-handling ()
21078 (org-set-local 'comment-use-syntax nil)
21079 (org-set-local 'comment-start "# ")
21080 (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
21081 (org-set-local 'comment-insert-comment-function 'org-insert-comment)
21082 (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
21083 (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
21085 (defun org-insert-comment ()
21086 "Insert an empty comment above current line.
21087 If the line is empty, insert comment at its beginning."
21088 (beginning-of-line)
21089 (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
21093 (defvar comment-empty-lines) ; From newcomment.el.
21094 (defun org-comment-or-uncomment-region (beg end &rest ignore)
21095 "Comment or uncomment each non-blank line in the region.
21096 Uncomment each non-blank line between BEG and END if it only
21097 contains commented lines. Otherwise, comment them."
21100 (narrow-to-region (save-excursion (goto-char beg)
21101 (skip-chars-forward " \r\t\n" end)
21102 (line-beginning-position))
21103 (save-excursion (goto-char end)
21104 (skip-chars-backward " \r\t\n" beg)
21105 (line-end-position)))
21107 ;; UNCOMMENTP is non-nil when every non blank line between
21108 ;; BEG and END is a comment.
21110 (goto-char (point-min))
21111 (while (and (not (eobp))
21112 (let ((element (org-element-at-point)))
21113 (and (eq (org-element-type element) 'comment)
21114 (goto-char (min (point-max)
21115 (org-element-property
21116 :end element)))))))
21119 ;; Only blank lines and comments in region: uncomment it.
21121 (goto-char (point-min))
21122 (while (not (eobp))
21123 (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
21124 (replace-match "" nil nil nil 1))
21126 ;; Comment each line in region.
21127 (let ((min-indent (point-max)))
21128 ;; First find the minimum indentation across all lines.
21130 (goto-char (point-min))
21131 (while (and (not (eobp)) (not (zerop min-indent)))
21132 (unless (looking-at "[ \t]*$")
21133 (setq min-indent (min min-indent (current-indentation))))
21135 ;; Then loop over all lines.
21137 (goto-char (point-min))
21138 (while (not (eobp))
21139 (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
21140 (org-move-to-column min-indent t)
21141 (insert comment-start))
21142 (forward-line))))))))
21147 (defun org-toggle-fixed-width-section (arg)
21148 "Toggle the fixed-width export.
21149 If there is no active region, the QUOTE keyword at the current headline is
21150 inserted or removed. When present, it causes the text between this headline
21151 and the next to be exported as fixed-width text, and unmodified.
21152 If there is an active region, this command adds or removes a colon as the
21153 first character of this line. If the first character of a line is a colon,
21154 this line is also exported in fixed-width font."
21157 (regionp (org-region-active-p))
21158 (beg (if regionp (region-beginning) (point)))
21159 (end (if regionp (region-end)))
21160 (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
21161 (case-fold-search nil)
21162 (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
21167 (setq cc (current-column))
21168 (beginning-of-line 1)
21169 (setq off (looking-at re))
21170 (while (> nlines 0)
21171 (setq nlines (1- nlines))
21172 (beginning-of-line 1)
21175 (org-move-to-column cc t)
21178 ((and off (looking-at re))
21179 (replace-match "" t t nil 1))
21180 ((not off) (org-move-to-column cc t) (insert ": ")))
21183 (org-back-to-heading)
21185 ((looking-at (format org-heading-keyword-regexp-format
21187 (goto-char (match-end 1))
21188 (looking-at (concat " +" org-quote-string))
21189 (replace-match "" t t)
21190 (when (eolp) (insert " ")))
21191 ((looking-at org-outline-regexp)
21192 (goto-char (match-end 0))
21193 (insert org-quote-string " ")))))))
21195 (defun org-reftex-citation ()
21196 "Use reftex-citation to insert a citation into the buffer.
21197 This looks for a line like
21199 #+BIBLIOGRAPHY: foo plain option:-d
21201 and derives from it that foo.bib is the bibliography file relevant
21202 for this document. It then installs the necessary environment for RefTeX
21203 to work in this buffer and calls `reftex-citation' to insert a citation
21206 Export of such citations to both LaTeX and HTML is handled by the contributed
21207 package org-exp-bibtex by Taru Karttunen."
21209 (let ((reftex-docstruct-symbol 'rds)
21210 (reftex-cite-format "\\cite{%l}")
21215 (let ((case-fold-search t)
21216 (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
21217 (if (not (save-excursion
21218 (or (re-search-forward re nil t)
21219 (re-search-backward re nil t))))
21220 (error "No bibliography defined in file")
21221 (setq bib (concat (match-string 1) ".bib")
21222 rds (list (list 'bib bib)))))))
21223 (call-interactively 'reftex-citation)))
21225 ;;;; Functions extending outline functionality
21227 (defun org-beginning-of-line (&optional arg)
21228 "Go to the beginning of the current line. If that is invisible, continue
21229 to a visible line beginning. This makes the function of C-a more intuitive.
21230 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21231 first attempt, and only move to after the tags when the cursor is already
21232 beyond the end of the headline."
21234 (let ((pos (point))
21235 (special (if (consp org-special-ctrl-a/e)
21236 (car org-special-ctrl-a/e)
21237 org-special-ctrl-a/e))
21239 (if (org-bound-and-true-p line-move-visual)
21240 (beginning-of-visual-line 1)
21241 (beginning-of-line 1))
21242 (if (and arg (fboundp 'move-beginning-of-line))
21243 (call-interactively 'move-beginning-of-line)
21247 (if (org-truely-invisible-p)
21248 (while (and (not (bobp)) (org-truely-invisible-p))
21250 (beginning-of-line 1))
21251 (forward-char 1))))
21254 ((and (looking-at org-complex-heading-regexp)
21255 (= (char-after (match-end 1)) ?\ ))
21256 (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
21260 (cond ((> pos refpos) refpos)
21261 ((= pos (point)) refpos)
21263 (cond ((> pos (point)) (point))
21264 ((not (eq last-command this-command)) (point))
21267 ;; Being at an item and not looking at an the item means point
21268 ;; was previously moved to beginning of a visual line, which
21269 ;; doesn't contain the item. Therefore, do nothing special,
21271 (when (looking-at org-list-full-item-re)
21272 ;; Set special position at first white space character after
21273 ;; bullet, and check-box, if any.
21274 (let ((after-bullet
21275 (let ((box (match-end 3)))
21276 (if (not box) (match-end 1)
21277 (let ((after (char-after box)))
21278 (if (and after (= after ? )) (1+ box) box))))))
21279 ;; Special case: Move point to special position when
21280 ;; currently after it or at beginning of line.
21282 (when (or (> pos after-bullet) (= (point) pos))
21283 (goto-char after-bullet))
21284 ;; Reversed case: Move point to special position when
21285 ;; point was already at beginning of line and command is
21287 (when (and (= (point) pos) (eq last-command this-command))
21288 (goto-char after-bullet))))))))
21290 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21292 (defun org-end-of-line (&optional arg)
21293 "Go to the end of the line.
21294 If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
21295 first attempt, and only move to after the tags when the cursor is already
21296 beyond the end of the headline."
21298 (let ((special (if (consp org-special-ctrl-a/e)
21299 (cdr org-special-ctrl-a/e)
21300 org-special-ctrl-a/e)))
21302 ((or (not special) arg
21303 (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
21304 (call-interactively
21305 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
21306 ((fboundp 'move-end-of-line) 'move-end-of-line)
21307 (t 'end-of-line))))
21308 ((org-at-heading-p)
21309 (let ((pos (point)))
21310 (beginning-of-line 1)
21311 (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
21313 (if (or (< pos (match-beginning 1))
21314 (= pos (match-end 0)))
21315 (goto-char (match-beginning 1))
21316 (goto-char (match-end 0)))
21317 (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
21318 (goto-char (match-end 0))
21319 (goto-char (match-beginning 1))))
21320 (call-interactively (if (fboundp 'move-end-of-line)
21324 (move-end-of-line 1)
21325 (when (overlays-at (1- (point))) (backward-char 1)))
21326 ;; At an item: Move before any hidden text.
21327 (t (call-interactively
21328 (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
21329 ((fboundp 'move-end-of-line) 'move-end-of-line)
21330 (t 'end-of-line)))))
21332 (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
21334 (define-key org-mode-map "\C-a" 'org-beginning-of-line)
21335 (define-key org-mode-map "\C-e" 'org-end-of-line)
21337 (defun org-backward-sentence (&optional arg)
21338 "Go to beginning of sentence, or beginning of table field.
21339 This will call `backward-sentence' or `org-table-beginning-of-field',
21340 depending on context."
21343 ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
21344 (t (call-interactively 'backward-sentence))))
21346 (defun org-forward-sentence (&optional arg)
21347 "Go to end of sentence, or end of table field.
21348 This will call `forward-sentence' or `org-table-end-of-field',
21349 depending on context."
21352 ((org-at-table-p) (call-interactively 'org-table-end-of-field))
21353 (t (call-interactively 'forward-sentence))))
21355 (define-key org-mode-map "\M-a" 'org-backward-sentence)
21356 (define-key org-mode-map "\M-e" 'org-forward-sentence)
21358 (defun org-kill-line (&optional arg)
21359 "Kill line, to tags or end of line."
21362 ((or (not org-special-ctrl-k)
21364 (not (org-at-heading-p)))
21365 (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
21366 org-ctrl-k-protect-subtree)
21367 (if (or (eq org-ctrl-k-protect-subtree 'error)
21368 (not (y-or-n-p "Kill hidden subtree along with headline? ")))
21369 (error "C-k aborted - would kill hidden subtree")))
21370 (call-interactively
21371 (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
21372 ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
21373 (kill-region (point) (match-beginning 1))
21374 (org-set-tags nil t))
21375 (t (kill-region (point) (point-at-eol)))))
21377 (define-key org-mode-map "\C-k" 'org-kill-line)
21379 (defun org-yank (&optional arg)
21380 "Yank. If the kill is a subtree, treat it specially.
21381 This command will look at the current kill and check if is a single
21382 subtree, or a series of subtrees[1]. If it passes the test, and if the
21383 cursor is at the beginning of a line or after the stars of a currently
21384 empty headline, then the yank is handled specially. How exactly depends
21385 on the value of the following variables, both set by default.
21387 org-yank-folded-subtrees
21388 When set, the subtree(s) will be folded after insertion, but only
21389 if doing so would now swallow text after the yanked text.
21391 org-yank-adjusted-subtrees
21392 When set, the subtree will be promoted or demoted in order to
21393 fit into the local outline tree structure, which means that the level
21394 will be adjusted so that it becomes the smaller one of the two
21395 *visible* surrounding headings.
21397 Any prefix to this command will cause `yank' to be called directly with
21398 no special treatment. In particular, a simple \\[universal-argument] prefix \
21400 plainly yank the text as it is.
21402 \[1] The test checks if the first non-white line is a heading
21403 and if there are no other headings with fewer stars."
21405 (org-yank-generic 'yank arg))
21407 (defun org-yank-generic (command arg)
21408 "Perform some yank-like command.
21410 This function implements the behavior described in the `org-yank'
21411 documentation. However, it has been generalized to work for any
21412 interactive command with similar behavior."
21414 ;; pretend to be command COMMAND
21415 (setq this-command command)
21418 (call-interactively command)
21420 (let ((subtreep ; is kill a subtree, and the yank position appropriate?
21421 (and (org-kill-is-subtree-p)
21423 (and (looking-at "[ \t]*$")
21426 (buffer-substring (point-at-bol) (point)))))))
21429 ((and subtreep org-yank-folded-subtrees)
21430 (let ((beg (point))
21432 (if (and subtreep org-yank-adjusted-subtrees)
21433 (org-paste-subtree nil nil 'for-yank)
21434 (call-interactively command))
21438 (when (and (bolp) subtreep
21439 (not (setq swallowp
21440 (org-yank-folding-would-swallow-text beg end))))
21441 (org-with-limited-levels
21442 (or (looking-at org-outline-regexp)
21443 (re-search-forward org-outline-regexp-bol end t))
21444 (while (and (< (point) end) (looking-at org-outline-regexp))
21446 (org-cycle-show-empty-lines 'folded)
21447 (condition-case nil
21448 (outline-forward-same-level 1)
21449 (error (goto-char end))))))
21452 "Inserted text not folded because that would swallow text"))
21455 (skip-chars-forward " \t\n\r")
21456 (beginning-of-line 1)
21457 (push-mark beg 'nomsg)))
21458 ((and subtreep org-yank-adjusted-subtrees)
21459 (let ((beg (point-at-bol)))
21460 (org-paste-subtree nil nil 'for-yank)
21461 (push-mark beg 'nomsg)))
21463 (call-interactively command))))))
21465 (defun org-yank-folding-would-swallow-text (beg end)
21466 "Would hide-subtree at BEG swallow any text after END?"
21468 (org-with-limited-levels
21471 (when (or (looking-at org-outline-regexp)
21472 (re-search-forward org-outline-regexp-bol end t))
21473 (setq level (org-outline-level)))
21475 (skip-chars-forward " \t\r\n\v\f")
21477 (and (bolp) (looking-at org-outline-regexp)
21478 (<= (org-outline-level) level)))
21479 nil ; Nothing would be swallowed
21480 t))))) ; something would swallow
21482 (define-key org-mode-map "\C-y" 'org-yank)
21484 (defun org-truely-invisible-p ()
21485 "Check if point is at a character currently not visible.
21486 This version does not only check the character property, but also
21488 ;; Early versions of noutline don't have `outline-invisible-p'.
21489 (if (org-bound-and-true-p visible-mode)
21491 (outline-invisible-p)))
21493 (defun org-invisible-p2 ()
21494 "Check if point is at a character currently not visible."
21496 (if (and (eolp) (not (bobp))) (backward-char 1))
21497 ;; Early versions of noutline don't have `outline-invisible-p'.
21498 (outline-invisible-p)))
21500 (defun org-back-to-heading (&optional invisible-ok)
21501 "Call `outline-back-to-heading', but provide a better error message."
21502 (condition-case nil
21503 (outline-back-to-heading invisible-ok)
21504 (error (error "Before first headline at position %d in buffer %s"
21505 (point) (current-buffer)))))
21507 (defun org-before-first-heading-p ()
21508 "Before first heading?"
21511 (null (re-search-backward org-outline-regexp-bol nil t))))
21513 (defun org-at-heading-p (&optional ignored)
21514 (outline-on-heading-p t))
21515 ;; Compatibility alias with Org versions < 7.8.03
21516 (defalias 'org-on-heading-p 'org-at-heading-p)
21518 (defun org-at-comment-p nil
21519 "Is cursor in a line starting with a # character?"
21521 (beginning-of-line)
21522 (looking-at "^#")))
21524 (defun org-at-drawer-p nil
21525 "Is cursor at a drawer keyword?"
21527 (move-beginning-of-line 1)
21528 (looking-at org-drawer-regexp)))
21530 (defun org-at-block-p nil
21531 "Is cursor at a block keyword?"
21533 (move-beginning-of-line 1)
21534 (looking-at org-block-regexp)))
21536 (defun org-point-at-end-of-empty-headline ()
21537 "If point is at the end of an empty headline, return t, else nil.
21538 If the heading only contains a TODO keyword, it is still still considered
21540 (and (looking-at "[ \t]*$")
21541 (when org-todo-line-regexp
21543 (beginning-of-line 1)
21544 (let ((case-fold-search nil))
21545 (looking-at org-todo-line-regexp)
21546 (string= (match-string 3) ""))))))
21548 (defun org-at-heading-or-item-p ()
21549 (or (org-at-heading-p) (org-at-item-p)))
21551 (defun org-at-target-p ()
21552 (or (org-in-regexp org-radio-target-regexp)
21553 (org-in-regexp org-target-regexp)))
21554 ;; Compatibility alias with Org versions < 7.8.03
21555 (defalias 'org-on-target-p 'org-at-target-p)
21557 (defun org-up-heading-all (arg)
21558 "Move to the heading line of which the present line is a subheading.
21559 This function considers both visible and invisible heading lines.
21560 With argument, move up ARG levels."
21561 (if (fboundp 'outline-up-heading-all)
21562 (outline-up-heading-all arg) ; emacs 21 version of outline.el
21563 (outline-up-heading arg t))) ; emacs 22 version of outline.el
21565 (defun org-up-heading-safe ()
21566 "Move to the heading line of which the present line is a subheading.
21567 This version will not throw an error. It will return the level of the
21568 headline found, or nil if no higher level is found.
21570 Also, this function will be a lot faster than `outline-up-heading',
21571 because it relies on stars being the outline starters. This can really
21572 make a significant difference in outlines with very many siblings."
21573 (let (start-level re)
21574 (org-back-to-heading t)
21575 (setq start-level (funcall outline-level))
21576 (if (equal start-level 1)
21578 (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
21579 (if (re-search-backward re nil t)
21580 (funcall outline-level)))))
21582 (defun org-first-sibling-p ()
21583 "Is this heading the first child of its parents?"
21585 (let ((re org-outline-regexp-bol)
21587 (unless (org-at-heading-p t)
21588 (error "Not at a heading"))
21589 (setq level (funcall outline-level))
21591 (if (not (re-search-backward re nil t))
21593 (setq l (funcall outline-level))
21596 (defun org-goto-sibling (&optional previous)
21597 "Goto the next sibling, even if it is invisible.
21598 When PREVIOUS is set, go to the previous sibling instead. Returns t
21599 when a sibling was found. When none is found, return nil and don't
21601 (let ((fun (if previous 're-search-backward 're-search-forward))
21603 (re org-outline-regexp-bol)
21605 (when (condition-case nil (org-back-to-heading t) (error nil))
21606 (setq level (funcall outline-level))
21608 (or previous (forward-char 1))
21609 (while (funcall fun re nil t)
21610 (setq l (funcall outline-level))
21611 (when (< l level) (goto-char pos) (throw 'exit nil))
21612 (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
21616 (defun org-show-siblings ()
21617 "Show all siblings of the current headline."
21619 (while (org-goto-sibling) (org-flag-heading nil)))
21621 (while (org-goto-sibling 'previous)
21622 (org-flag-heading nil))))
21624 (defun org-goto-first-child ()
21625 "Goto the first child, even if it is invisible.
21626 Return t when a child was found. Otherwise don't move point and
21628 (let (level (pos (point)) (re org-outline-regexp-bol))
21629 (when (condition-case nil (org-back-to-heading t) (error nil))
21630 (setq level (outline-level))
21632 (if (and (re-search-forward re nil t) (> (outline-level) level))
21633 (progn (goto-char (match-beginning 0)) t)
21634 (goto-char pos) nil))))
21636 (defun org-show-hidden-entry ()
21637 "Show an entry where even the heading is hidden."
21641 (defun org-flag-heading (flag &optional entry)
21642 "Flag the current heading. FLAG non-nil means make invisible.
21643 When ENTRY is non-nil, show the entire entry."
21645 (org-back-to-heading t)
21646 ;; Check if we should show the entire entry
21651 (and (outline-next-heading)
21652 (org-flag-heading nil))))
21653 (outline-flag-region (max (point-min) (1- (point)))
21654 (save-excursion (outline-end-of-heading) (point))
21657 (defun org-get-next-sibling ()
21658 "Move to next heading of the same level, and return point.
21659 If there is no such heading, return nil.
21660 This is like outline-next-sibling, but invisible headings are ok."
21661 (let ((level (funcall outline-level)))
21662 (outline-next-heading)
21663 (while (and (not (eobp)) (> (funcall outline-level) level))
21664 (outline-next-heading))
21665 (if (or (eobp) (< (funcall outline-level) level))
21669 (defun org-get-last-sibling ()
21670 "Move to previous heading of the same level, and return point.
21671 If there is no such heading, return nil."
21672 (let ((opoint (point))
21673 (level (funcall outline-level)))
21674 (outline-previous-heading)
21675 (when (and (/= (point) opoint) (outline-on-heading-p t))
21676 (while (and (> (funcall outline-level) level)
21678 (outline-previous-heading))
21679 (if (< (funcall outline-level) level)
21683 (defun org-end-of-subtree (&optional invisible-ok to-heading)
21684 "Goto to the end of a subtree."
21685 ;; This contains an exact copy of the original function, but it uses
21686 ;; `org-back-to-heading', to make it work also in invisible
21687 ;; trees. And is uses an invisible-ok argument.
21688 ;; Under Emacs this is not needed, but the old outline.el needs this fix.
21689 ;; Furthermore, when used inside Org, finding the end of a large subtree
21690 ;; with many children and grandchildren etc, this can be much faster
21691 ;; than the outline version.
21692 (org-back-to-heading invisible-ok)
21694 (level (funcall outline-level)))
21695 (if (and (derived-mode-p 'org-mode) (< level 1000))
21696 ;; A true heading (not a plain list item), in Org-mode
21697 ;; This means we can easily find the end by looking
21698 ;; only for the right number of stars. Using a regexp to do
21699 ;; this is so much faster than using a Lisp loop.
21700 (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
21702 (and (re-search-forward re nil 'move) (beginning-of-line 1)))
21703 ;; something else, do it the slow way
21704 (while (and (not (eobp))
21705 (or first (> (funcall outline-level) level)))
21707 (outline-next-heading)))
21709 (if (memq (preceding-char) '(?\n ?\^M))
21711 ;; Go to end of line before heading
21713 (if (memq (preceding-char) '(?\n ?\^M))
21714 ;; leave blank line before heading
21715 (forward-char -1))))))
21718 (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
21719 "Use Org version in org-mode, for dramatic speed-up."
21720 (if (derived-mode-p 'org-mode)
21722 (org-end-of-subtree nil t)
21723 (unless (eobp) (backward-char 1)))
21726 (defun org-end-of-meta-data-and-drawers ()
21727 "Jump to the first text after meta data and drawers in the current entry.
21728 This will move over empty lines, lines with planning time stamps,
21729 clocking lines, and drawers."
21730 (org-back-to-heading t)
21731 (let ((end (save-excursion (outline-next-heading) (point)))
21732 (re (concat "\\(" org-drawer-regexp "\\)"
21733 "\\|" "[ \t]*" org-keyword-time-regexp)))
21735 (while (re-search-forward re end t)
21736 (if (not (match-end 1))
21737 ;; empty or planning line
21739 ;; a drawer, find the end
21740 (re-search-forward "^[ \t]*:END:" end 'move)
21742 (and (re-search-forward "[^\n]" nil t) (backward-char 1))
21745 (defun org-forward-heading-same-level (arg &optional invisible-ok)
21746 "Move forward to the arg'th subheading at same level as this one.
21747 Stop at the first and last subheadings of a superior heading.
21748 Normally this only looks at visible headings, but when INVISIBLE-OK is
21749 non-nil it will also look at invisible ones."
21751 (org-back-to-heading invisible-ok)
21753 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21754 (re (format "^\\*\\{1,%d\\} " level))
21758 (while (and (re-search-forward re nil 'move)
21759 (setq l (- (match-end 0) (match-beginning 0) 1))
21762 (progn (backward-char 1) (outline-invisible-p)))
21763 (if (< l level) (setq arg 1)))
21764 (setq arg (1- arg)))
21765 (beginning-of-line 1)))
21767 (defun org-backward-heading-same-level (arg &optional invisible-ok)
21768 "Move backward to the arg'th subheading at same level as this one.
21769 Stop at the first and last subheadings of a superior heading."
21771 (org-back-to-heading)
21773 (let* ((level (- (match-end 0) (match-beginning 0) 1))
21774 (re (format "^\\*\\{1,%d\\} " level))
21777 (while (and (re-search-backward re nil 'move)
21778 (setq l (- (match-end 0) (match-beginning 0) 1))
21781 (outline-invisible-p))
21782 (if (< l level) (setq arg 1)))
21783 (setq arg (1- arg)))))
21785 (defun org-forward-element ()
21786 "Move forward by one element.
21787 Move to the next element at the same level, when possible."
21789 (cond ((eobp) (error "Cannot move further down"))
21790 ((org-with-limited-levels (org-at-heading-p))
21791 (let ((origin (point)))
21792 (org-forward-heading-same-level 1)
21793 (unless (org-with-limited-levels (org-at-heading-p))
21795 (error "Cannot move further down"))))
21797 (let* ((elem (org-element-at-point))
21798 (end (org-element-property :end elem))
21799 (parent (org-element-property :parent elem)))
21800 (if (and parent (= (org-element-property :contents-end parent) end))
21801 (goto-char (org-element-property :end parent))
21802 (goto-char end))))))
21804 (defun org-backward-element ()
21805 "Move backward by one element.
21806 Move to the previous element at the same level, when possible."
21808 (cond ((bobp) (error "Cannot move further up"))
21809 ((org-with-limited-levels (org-at-heading-p))
21810 ;; At an headline, move to the previous one, if any, or stay
21812 (let ((origin (point)))
21813 (org-backward-heading-same-level 1)
21814 (unless (org-with-limited-levels (org-at-heading-p))
21816 (error "Cannot move further up"))))
21818 (let* ((trail (org-element-at-point 'keep-trail))
21820 (prev-elem (nth 1 trail))
21821 (beg (org-element-property :begin elem)))
21823 ;; Move to beginning of current element if point isn't
21825 ((/= (point) beg) (goto-char beg))
21826 (prev-elem (goto-char (org-element-property :begin prev-elem)))
21827 ((org-before-first-heading-p) (goto-char (point-min)))
21828 (t (org-back-to-heading)))))))
21830 (defun org-up-element ()
21831 "Move to upper element."
21833 (if (org-with-limited-levels (org-at-heading-p))
21834 (unless (org-up-heading-safe) (error "No surrounding element"))
21835 (let* ((elem (org-element-at-point))
21836 (parent (org-element-property :parent elem)))
21837 (if parent (goto-char (org-element-property :begin parent))
21838 (if (org-with-limited-levels (org-before-first-heading-p))
21839 (error "No surrounding element")
21840 (org-with-limited-levels (org-back-to-heading)))))))
21842 (defvar org-element-greater-elements)
21843 (defun org-down-element ()
21844 "Move to inner element."
21846 (let ((element (org-element-at-point)))
21848 ((memq (org-element-type element) '(plain-list table))
21849 (goto-char (org-element-property :contents-begin element))
21851 ((memq (org-element-type element) org-element-greater-elements)
21852 ;; If contents are hidden, first disclose them.
21853 (when (org-element-property :hiddenp element) (org-cycle))
21854 (goto-char (or (org-element-property :contents-begin element)
21855 (error "No content for this element"))))
21856 (t (error "No inner element")))))
21858 (defun org-drag-element-backward ()
21859 "Move backward element at point."
21861 (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
21862 (let* ((trail (org-element-at-point 'keep-trail))
21864 (prev-elem (nth 1 trail)))
21865 ;; Error out if no previous element or previous element is
21866 ;; a parent of the current one.
21867 (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
21868 (error "Cannot drag element backward")
21869 (let ((pos (point)))
21870 (org-element-swap-A-B prev-elem elem)
21871 (goto-char (+ (org-element-property :begin prev-elem)
21872 (- pos (org-element-property :begin elem)))))))))
21874 (defun org-drag-element-forward ()
21875 "Move forward element at point."
21877 (let* ((pos (point))
21878 (elem (org-element-at-point)))
21879 (when (= (point-max) (org-element-property :end elem))
21880 (error "Cannot drag element forward"))
21881 (goto-char (org-element-property :end elem))
21882 (let ((next-elem (org-element-at-point)))
21883 (when (or (org-element-nested-p elem next-elem)
21884 (and (eq (org-element-type next-elem) 'headline)
21885 (not (eq (org-element-type elem) 'headline))))
21887 (error "Cannot drag element forward"))
21888 ;; Compute new position of point: it's shifted by NEXT-ELEM
21889 ;; body's length (without final blanks) and by the length of
21890 ;; blanks between ELEM and NEXT-ELEM.
21891 (let ((size-next (- (save-excursion
21892 (goto-char (org-element-property :end next-elem))
21893 (skip-chars-backward " \r\t\n")
21895 ;; Small correction if buffer doesn't end
21896 ;; with a newline character.
21897 (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
21898 (org-element-property :begin next-elem)))
21899 (size-blank (- (org-element-property :end elem)
21901 (goto-char (org-element-property :end elem))
21902 (skip-chars-backward " \r\t\n")
21905 (org-element-swap-A-B elem next-elem)
21906 (goto-char (+ pos size-next size-blank))))))
21908 (defun org-mark-element ()
21909 "Put point at beginning of this element, mark at end.
21911 Interactively, if this command is repeated or (in Transient Mark
21912 mode) if the mark is active, it marks the next element after the
21913 ones already marked."
21915 (let (deactivate-mark)
21916 (if (and (org-called-interactively-p 'any)
21917 (or (and (eq last-command this-command) (mark t))
21918 (and transient-mark-mode mark-active)))
21922 (goto-char (org-element-property :end (org-element-at-point)))))
21923 (let ((element (org-element-at-point)))
21925 (push-mark (org-element-property :end element) t t)
21926 (goto-char (org-element-property :begin element))))))
21928 (defun org-narrow-to-element ()
21929 "Narrow buffer to current element."
21931 (let ((elem (org-element-at-point)))
21933 ((eq (car elem) 'headline)
21935 (org-element-property :begin elem)
21936 (org-element-property :end elem)))
21937 ((memq (car elem) org-element-greater-elements)
21939 (org-element-property :contents-begin elem)
21940 (org-element-property :contents-end elem)))
21943 (org-element-property :begin elem)
21944 (org-element-property :end elem))))))
21946 (defun org-transpose-element ()
21947 "Transpose current and previous elements, keeping blank lines between.
21948 Point is moved after both elements."
21950 (org-skip-whitespace)
21951 (let ((end (org-element-property :end (org-element-at-point))))
21952 (org-drag-element-backward)
21955 (defun org-unindent-buffer ()
21956 "Un-indent the visible part of the buffer.
21957 Relative indentation (between items, inside blocks, etc.) isn't
21960 (unless (eq major-mode 'org-mode)
21961 (error "Cannot un-indent a buffer not in Org mode"))
21962 (let* ((parse-tree (org-element-parse-buffer 'greater-element))
21963 unindent-tree ; For byte-compiler.
21969 (if (memq (org-element-type element) '(headline section))
21970 (funcall unindent-tree (org-element-contents element))
21974 (org-element-property :begin element)
21975 (org-element-property :end element))
21976 (org-do-remove-indentation)))))
21977 (reverse contents))))))
21978 (funcall unindent-tree (org-element-contents parse-tree))))
21980 (defun org-show-subtree ()
21981 "Show everything after this heading at deeper levels."
21983 (outline-flag-region
21986 (org-end-of-subtree t t))
21989 (defun org-show-entry ()
21990 "Show the body directly following this heading.
21991 Show the heading too, if it is currently invisible."
21994 (condition-case nil
21996 (org-back-to-heading t)
21997 (outline-flag-region
21998 (max (point-min) (1- (point)))
22000 (if (re-search-forward
22001 (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
22002 (match-beginning 1)
22005 (org-cycle-hide-drawers 'children))
22008 (defun org-make-options-regexp (kwds &optional extra)
22009 "Make a regular expression for keyword lines."
22012 (mapconcat 'regexp-quote kwds "\\|")
22013 (if extra (concat "\\|" extra))
22014 "\\):[ \t]*\\(.*\\)"))
22016 ;; Make isearch reveal the necessary context
22017 (defun org-isearch-end ()
22018 "Reveal context after isearch exits."
22019 (when isearch-success ; only if search was successful
22020 (if (featurep 'xemacs)
22021 ;; Under XEmacs, the hook is run in the correct place,
22022 ;; we directly show the context.
22023 (org-show-context 'isearch)
22024 ;; In Emacs the hook runs *before* restoring the overlays.
22025 ;; So we have to use a one-time post-command-hook to do this.
22026 ;; (Emacs 22 has a special variable, see function `org-mode')
22027 (unless (and (boundp 'isearch-mode-end-hook-quit)
22028 isearch-mode-end-hook-quit)
22029 ;; Only when the isearch was not quitted.
22030 (org-add-hook 'post-command-hook 'org-isearch-post-command
22031 'append 'local)))))
22033 (defun org-isearch-post-command ()
22034 "Remove self from hook, and show context."
22035 (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
22036 (org-show-context 'isearch))
22039 ;;;; Integration with and fixes for other packages
22043 (defvar org-imenu-markers nil
22044 "All markers currently used by Imenu.")
22045 (make-variable-buffer-local 'org-imenu-markers)
22047 (defun org-imenu-new-marker (&optional pos)
22048 "Return a new marker for use by Imenu, and remember the marker."
22049 (let ((m (make-marker)))
22050 (move-marker m (or pos (point)))
22051 (push m org-imenu-markers)
22054 (defun org-imenu-get-tree ()
22055 "Produce the index for Imenu."
22056 (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
22057 (setq org-imenu-markers nil)
22058 (let* ((n org-imenu-depth)
22059 (re (concat "^" (org-get-limited-outline-regexp)))
22060 (subs (make-vector (1+ n) nil))
22066 (goto-char (point-max))
22067 (while (re-search-backward re nil t)
22068 (setq level (org-reduced-level (funcall outline-level)))
22069 (when (and (<= level n)
22070 (looking-at org-complex-heading-regexp))
22071 (setq head (org-link-display-format
22072 (org-match-string-no-properties 4))
22073 m (org-imenu-new-marker))
22074 (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
22075 (if (>= level last-level)
22076 (push (cons head m) (aref subs level))
22077 (push (cons head (aref subs (1+ level))) (aref subs level))
22078 (loop for i from (1+ level) to n do (aset subs i nil)))
22079 (setq last-level level)))))
22082 (eval-after-load "imenu"
22084 (add-hook 'imenu-after-jump-hook
22086 (if (derived-mode-p 'org-mode)
22087 (org-show-context 'org-goto))))))
22089 (defun org-link-display-format (link)
22090 "Replace a link with either the description, or the link target
22091 if no description is present"
22093 (if (string-match org-bracket-link-analytic-regexp link)
22094 (replace-match (if (match-end 5)
22095 (match-string 5 link)
22096 (concat (match-string 1 link)
22097 (match-string 3 link)))
22101 (defun org-toggle-link-display ()
22102 "Toggle the literal or descriptive display of links."
22104 (if org-descriptive-links
22105 (progn (org-remove-from-invisibility-spec '(org-link))
22106 (org-restart-font-lock)
22107 (setq org-descriptive-links nil))
22108 (progn (add-to-invisibility-spec '(org-link))
22109 (org-restart-font-lock)
22110 (setq org-descriptive-links t))))
22112 ;; Speedbar support
22114 (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
22115 "Overlay marking the agenda restriction line in speedbar.")
22116 (overlay-put org-speedbar-restriction-lock-overlay
22117 'face 'org-agenda-restriction-lock)
22118 (overlay-put org-speedbar-restriction-lock-overlay
22119 'help-echo "Agendas are currently limited to this item.")
22120 (org-detach-overlay org-speedbar-restriction-lock-overlay)
22122 (defun org-speedbar-set-agenda-restriction ()
22123 "Restrict future agenda commands to the location at point in speedbar.
22124 To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
22126 (require 'org-agenda)
22127 (let (p m tp np dir txt)
22129 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22131 (setq m (get-text-property p 'org-imenu-marker))
22132 (with-current-buffer (marker-buffer m)
22134 (org-agenda-set-restriction-lock 'subtree)))
22135 ((setq p (text-property-any (point-at-bol) (point-at-eol)
22136 'speedbar-function 'speedbar-find-file))
22137 (setq tp (previous-single-property-change
22138 (1+ p) 'speedbar-function)
22139 np (next-single-property-change
22140 tp 'speedbar-function)
22141 dir (speedbar-line-directory)
22142 txt (buffer-substring-no-properties (or tp (point-min))
22143 (or np (point-max))))
22144 (with-current-buffer (find-file-noselect
22145 (let ((default-directory dir))
22146 (expand-file-name txt)))
22147 (unless (derived-mode-p 'org-mode)
22148 (error "Cannot restrict to non-Org-mode file"))
22149 (org-agenda-set-restriction-lock 'file)))
22150 (t (error "Don't know how to restrict Org-mode's agenda")))
22151 (move-overlay org-speedbar-restriction-lock-overlay
22152 (point-at-bol) (point-at-eol))
22153 (setq current-prefix-arg nil)
22154 (org-agenda-maybe-redo)))
22156 (eval-after-load "speedbar"
22158 (speedbar-add-supported-extension ".org")
22159 (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
22160 (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
22161 (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
22162 (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
22163 (add-hook 'speedbar-visiting-tag-hook
22164 (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
22166 ;;; Fixes and Hacks for problems with other packages
22168 ;; Make flyspell not check words in links, to not mess up our keymap
22169 (defun org-mode-flyspell-verify ()
22170 "Don't let flyspell put overlays at active buttons, or on
22171 {todo,all-time,additional-option-like}-keywords."
22172 (let ((pos (max (1- (point)) (point-min)))
22173 (word (thing-at-point 'word)))
22174 (and (not (get-text-property pos 'keymap))
22175 (not (get-text-property pos 'org-no-flyspell))
22176 (not (member word org-todo-keywords-1))
22177 (not (member word org-all-time-keywords))
22178 (not (member word org-options-keywords))
22179 (not (member word (mapcar 'car org-startup-options)))
22180 (not (member word org-additional-option-like-keywords-for-flyspell)))))
22182 (defun org-remove-flyspell-overlays-in (beg end)
22183 "Remove flyspell overlays in region."
22184 (and (org-bound-and-true-p flyspell-mode)
22185 (fboundp 'flyspell-delete-region-overlays)
22186 (flyspell-delete-region-overlays beg end))
22187 (add-text-properties beg end '(org-no-flyspell t)))
22189 ;; Make `bookmark-jump' shows the jump location if it was hidden.
22190 (eval-after-load "bookmark"
22191 '(if (boundp 'bookmark-after-jump-hook)
22192 ;; We can use the hook
22193 (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
22194 ;; Hook not available, use advice
22195 (defadvice bookmark-jump (after org-make-visible activate)
22196 "Make the position visible."
22197 (org-bookmark-jump-unhide))))
22199 ;; Make sure saveplace shows the location if it was hidden
22200 (eval-after-load "saveplace"
22201 '(defadvice save-place-find-file-hook (after org-make-visible activate)
22202 "Make the position visible."
22203 (org-bookmark-jump-unhide)))
22205 ;; Make sure ecb shows the location if it was hidden
22206 (eval-after-load "ecb"
22207 '(defadvice ecb-method-clicked (after esf/org-show-context activate)
22208 "Make hierarchy visible when jumping into location from ECB tree buffer."
22209 (if (derived-mode-p 'org-mode)
22210 (org-show-context))))
22212 (defun org-bookmark-jump-unhide ()
22213 "Unhide the current position, to show the bookmark location."
22214 (and (derived-mode-p 'org-mode)
22215 (or (outline-invisible-p)
22216 (save-excursion (goto-char (max (point-min) (1- (point))))
22217 (outline-invisible-p)))
22218 (org-show-context 'bookmark-jump)))
22220 ;; Make session.el ignore our circular variable
22221 (eval-after-load "session"
22222 '(add-to-list 'session-globals-exclude 'org-mark-ring))
22224 ;;;; Experimental code
22226 (defun org-closed-in-range ()
22227 "Sparse tree of items closed in a certain time range.
22228 Still experimental, may disappear in the future."
22230 ;; Get the time interval from the user.
22231 (let* ((time1 (org-float-time
22232 (org-read-date nil 'to-time nil "Starting date: ")))
22233 (time2 (org-float-time
22234 (org-read-date nil 'to-time nil "End date:")))
22235 ;; callback function
22236 (callback (lambda ()
22239 (apply 'encode-time
22240 (org-parse-time-string
22241 (match-string 1))))))
22242 ;; check if time in interval
22243 (and (>= time time1) (<= time time2))))))
22244 ;; make tree, check each match with the callback
22245 (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
22251 (run-hooks 'org-load-hook)
22253 ;;; org.el ends here